tests.cpp 918 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files] [--list] [--filter a,b] [--exclude a,b] [--shard i/n] [--weights f] [--times f] [--no-times]
  2. #include <algorithm>
  3. #include <optional>
  4. #include <atomic>
  5. #include <chrono>
  6. #include <thread>
  7. #include <random>
  8. #include <cctype>
  9. #include <bit>
  10. #include <cmath>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <filesystem>
  14. #include <map>
  15. #include <set>
  16. #include <sstream>
  17. #include <fstream>
  18. #include <functional>
  19. #include <iterator>
  20. #include <string>
  21. #include <tuple>
  22. #include <vector>
  23. #include "core/FlTrackMenus.h" // round 190dx
  24. #include "core/OneClickRec.h"
  25. #include "core/TakeRecording.h"
  26. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  27. #include "core/FlControlMenus.h"
  28. #include "core/LinkFormula.h"
  29. #include "core/Multilink.h"
  30. #include "core/Simd.h"
  31. #endif
  32. #include "core/AutomationDisplay.h"
  33. #include "core/ShownEffectSlot.h"
  34. #include "core/Dither.h"
  35. #include "core/DrumSynthLive.h"
  36. #include "core/Effects.h"
  37. #include "core/MixSuite.h"
  38. #include "core/StrikeShaper.h"
  39. #include "core/EmberClassic.h"
  40. #include "core/Effector.h"
  41. #include "core/FruityDelayBank.h"
  42. #include "core/FruityMultiband.h"
  43. #include "core/Denormals.h"
  44. #include "core/TestProfile.h"
  45. #include "core/FlCurve.h"
  46. #include "core/FlpImport.h"
  47. #include "core/MidiControls.h"
  48. #include "core/MidiImport.h"
  49. #include "core/ChannelMiscFx.h"
  50. #include "core/FlEffects.h"
  51. #include "core/ProjectContents.h"
  52. #include "core/FlPatcher.h"
  53. #include "core/Patcher.h"
  54. #include "core/Rack.h"
  55. #include "core/Layer.h"
  56. #include "core/XyController.h"
  57. #include "core/ControlSurface.h"
  58. #include "core/VoltageController.h"
  59. #include "core/FlSamplers.h"
  60. #include "core/Fpc.h"
  61. #include "core/FpcInstrument.h"
  62. #include "core/FpcEnvelope.h"
  63. #include "core/FlSynths.h"
  64. #include "core/Inflate.h"
  65. #include "core/WavPack.h"
  66. #include "core/SampleFiles.h"
  67. #include "core/FlWrapper.h"
  68. #include "core/FlPluginDb.h"
  69. #include "core/MissingFiles.h"
  70. #include "core/PianoKeys.h"
  71. #include "core/PianoRollTools.h"
  72. #include "core/EventEdit.h"
  73. #include "core/Preferences.h"
  74. #include "core/MidiSync.h"
  75. #include "core/ControllerSession.h"
  76. #include "core/History.h"
  77. #include "core/Gms.h"
  78. #include "core/GmsMobile.h"
  79. #include "core/PoiZone.h"
  80. #include "core/MiniSynth.h"
  81. #include "core/SimSynth.h"
  82. #include "core/Granulizer.h"
  83. #include "core/WaveTraveller.h"
  84. #include "core/Drumaxx.h"
  85. #include "core/Wasp.h"
  86. #include "core/PluginLayout.h"
  87. #include "core/Plucked.h"
  88. #include "core/BeepMap.h"
  89. #include "core/Sawer.h"
  90. #include "core/Slayer.h"
  91. #include "core/Toxic.h"
  92. #include "core/Harmless.h"
  93. #include "core/Harmor.h"
  94. #include "core/Morphine.h"
  95. #include "core/Ogun.h"
  96. #include "core/BassDrum.h"
  97. #include "core/FruitKick.h"
  98. #include "core/IppCos.h"
  99. #include "core/Dx10.h"
  100. #include "core/Kepler.h"
  101. #include "core/ThreeOsc.h"
  102. #include "core/FlResampler.h"
  103. #include "core/FlKeys.h"
  104. #include "core/DirectWave.h"
  105. #include "core/OggVorbis.h"
  106. #include "core/FlVoiceProcessor.h"
  107. #include "core/TransistorBass.h"
  108. #include "core/HarmlessFx.h"
  109. #include "core/MaximusFx.h"
  110. #include "core/EmphasisFx.h"
  111. #include "core/VisualFx.h"
  112. #include "core/EmphasizerFx.h"
  113. #include "core/FlLegacyFx.h"
  114. #include "core/FreqShifterFl.h"
  115. #include "core/LowLifterFl.h"
  116. #include "core/Reeverb2.h"
  117. #include "core/LuxeVerb.h"
  118. #include "core/Denormals.h"
  119. #include "core/FruityDynamics.h"
  120. #include "core/FruityShapers.h"
  121. #include "core/ParametricEqFl.h"
  122. #include "core/FruityBasicFx.h"
  123. #include "core/EquoFx.h"
  124. #include "core/DistructorFx.h"
  125. #include "core/MultibandDelayFx.h"
  126. #include "core/PitchShifterFl.h"
  127. #include "core/FruityFilters.h"
  128. #include "core/NewtoneFx.h"
  129. #include "core/EdisonFx.h"
  130. #include "core/FruityDelay.h"
  131. #include "core/StereoFx.h"
  132. #include "core/FrequencySplitter.h"
  133. #include "core/ChorusFx.h"
  134. #include "core/GrossBeat.h"
  135. #include "core/LovePhilter.h"
  136. #include "core/FlangusFastDist.h"
  137. #include "core/FruityPhaser.h"
  138. #include "core/FruityVocoder.h"
  139. #include "core/DelphiMath.h"
  140. #include "core/LiveEventQueue.h"
  141. #include "core/RackRenderer.h"
  142. #include "core/PerfProjects.h"
  143. #include "core/PinOnTop.h"
  144. #include "core/MetronomeVoice.h"
  145. #include "engine/Transport.h"
  146. #include "engine/RemoteHistory.h"
  147. #include "core/Patcherize.h"
  148. #include "core/Performance.h"
  149. #include "core/Scheduler.h"
  150. #include "core/MarkerPlan.h"
  151. #include "core/SnapshotExchange.h"
  152. #include "core/SoftClip.h"
  153. #include "core/Slicer.h"
  154. #include "core/SytrusEngine.h"
  155. #include "core/Templates.h"
  156. #include "core/Vocodex.h"
  157. #include "core/TimeStretch.h"
  158. #include "core/WorkerPool.h"
  159. #include "core/RtStats.h"
  160. #include "core/FixedBlock.h"
  161. #include "core/ParametricEqLinear.h"
  162. #include "core/Song.h"
  163. #include "core/Slicex.h"
  164. #include "core/NoteEdit.h"
  165. #include "core/ScaleSnap.h"
  166. #include "core/LiveRecordView.h"
  167. #include "core/Recorder.h"
  168. #include "core/KeyMap.h"
  169. #include "core/EventPaneSplit.h" // round 190fz
  170. #include "core/UndoKeys.h"
  171. #include "core/SortOnce.h"
  172. #include "core/RackOps.h"
  173. #include "core/PlaylistTrackOps.h"
  174. #include "core/ChannelLoops.h"
  175. #include "core/ChannelBurn.h"
  176. #include "core/AutoSampler.h"
  177. #include "core/ValueMath.h"
  178. #include "core/Workspace.h"
  179. #include "core/StartupSound.h"
  180. #include "core/ExportOptions.h"
  181. #include "core/Mp3Encoder.h"
  182. #include "core/PluginMonitor.h"
  183. #include "core/PluginSubstitutes.h"
  184. #include "core/ClipOps.h"
  185. #include "core/PlaylistOps.h"
  186. #include "core/PluginDrop.h"
  187. #include "core/QuickRender.h"
  188. #include "core/TouchKeyboard.h"
  189. #include "core/HeldNotes.h"
  190. #include "core/Templates.h"
  191. #include "flp/Flp.h"
  192. #include "ui/I18nData.h"
  193. #include "ui/I18nLocale.h"
  194. #include "core/CrashReport.h"
  195. #include "core/RenderAhead.h"
  196. #include "core/WorkerPool.h"
  197. namespace
  198. {
  199. int failures = 0;
  200. int checks = 0;
  201. std::string processTempTag(); // per-process suffix for scratch files (defined further down)
  202. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  203. {
  204. static std::vector<std::pair<const char*, std::function<void()>>> r;
  205. return r;
  206. }
  207. struct Register
  208. {
  209. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  210. };
  211. #define TEST(name) \
  212. static void name(); \
  213. static Register reg_##name (#name, name); \
  214. static void name()
  215. #define CHECK(cond) \
  216. do \
  217. { \
  218. ++checks; \
  219. if (! (cond)) \
  220. { \
  221. ++failures; \
  222. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  223. } \
  224. } while (false)
  225. // Engines that compute in x87 80-bit long double (FL's own arithmetic) match their goldens to the last bit on x86. On
  226. // ARM64 (Apple Silicon) long double is not the x87 format, so there they hold to a looser bound (round 117).
  227. // LYNX_X87_TOL overrides it (measuring).
  228. inline float armTolerance (float x86, float arm)
  229. {
  230. #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
  231. (void) arm;
  232. return x86;
  233. #else
  234. if (const char* t = std::getenv ("LYNX_X87_TOL"))
  235. return (float) std::atof (t);
  236. return arm;
  237. #endif
  238. }
  239. inline float x87Tolerance() { return armTolerance (2.0e-7f, 2.0e-4f); }
  240. inline float granulizerTolerance()
  241. {
  242. #if defined(__APPLE__)
  243. return 2.0e-4f;
  244. #else
  245. return armTolerance (1.0e-6f, 1.0e-5f);
  246. #endif
  247. }
  248. // Engines whose goldens depend on the C library's sin/cos/exp/pow: Windows (mingw), glibc and musl hold the 1e-7
  249. // goldens, Apple's libm does not (round 117), so on macOS the per-sample bound is looser.
  250. inline float libmTolerance (float strict)
  251. {
  252. #if defined(__APPLE__)
  253. (void) strict;
  254. return 2.0e-4f;
  255. #else
  256. return strict;
  257. #endif
  258. }
  259. // One golden sample: relative bound (with a 1e-3 floor on macOS, where near-zero samples carry libm noise); prints the
  260. // measured error when it fails.
  261. inline bool goldenSampleClose (float y, float golden, float tol)
  262. {
  263. #if defined(__APPLE__)
  264. const float scale = std::max (1.0e-3f, std::abs (golden));
  265. #else
  266. const float scale = std::max (1.0e-16f, std::abs (golden));
  267. #endif
  268. if (std::abs (y - golden) <= tol * scale)
  269. return true;
  270. std::fprintf (stderr, " golden sample %.9g vs %.9g (relative error %.3g, bound %.3g)\n", (double) y, (double) golden,
  271. (double) (std::abs (y - golden) / scale), (double) tol);
  272. return false;
  273. }
  274. std::string realFlpDir;
  275. // --- helpers for building synthetic .flp files -----------------------------
  276. struct FlpBuilder
  277. {
  278. std::vector<std::uint8_t> bytes;
  279. FlpBuilder (std::uint16_t ppq = 96)
  280. {
  281. put ("FLhd");
  282. u32 (6);
  283. u16 (0);
  284. u16 (1);
  285. u16 (ppq);
  286. put ("FLdt");
  287. u32 (0); // size is not used by the reader
  288. }
  289. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  290. void u8 (std::uint8_t v) { bytes.push_back (v); }
  291. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  292. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  293. void varint (std::size_t n)
  294. {
  295. do
  296. {
  297. auto b = (std::uint8_t) (n & 0x7F);
  298. n >>= 7;
  299. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  300. } while (n);
  301. }
  302. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  303. {
  304. u8 (id);
  305. varint (payload.size());
  306. bytes.insert (bytes.end(), payload.begin(), payload.end());
  307. }
  308. void ascii (std::uint8_t id, const std::string& s)
  309. {
  310. std::vector<std::uint8_t> p (s.begin(), s.end());
  311. p.push_back (0);
  312. data (id, p);
  313. }
  314. void utf16 (std::uint8_t id, const std::u16string& s)
  315. {
  316. std::vector<std::uint8_t> p;
  317. for (auto c : s)
  318. {
  319. p.push_back ((std::uint8_t) c);
  320. p.push_back ((std::uint8_t) (c >> 8));
  321. }
  322. p.push_back (0);
  323. p.push_back (0);
  324. data (id, p);
  325. }
  326. };
  327. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  328. std::uint8_t velocity)
  329. {
  330. std::vector<std::uint8_t> r (24, 0);
  331. std::memcpy (&r[0], &pos, 4);
  332. std::memcpy (&r[6], &channel, 2);
  333. std::memcpy (&r[8], &len, 4);
  334. std::memcpy (&r[12], &key, 2);
  335. r[16] = 120;
  336. r[20] = 64;
  337. r[21] = velocity;
  338. return r;
  339. }
  340. } // namespace
  341. // ---------------------------------------------------------------------------
  342. // FLP
  343. TEST (flp_rejects_garbage)
  344. {
  345. std::vector<std::uint8_t> junk (64, 0x41);
  346. bool threw = false;
  347. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  348. CHECK (threw);
  349. }
  350. TEST (flp_reads_size_classes_varint_and_extended_ids)
  351. {
  352. FlpBuilder b;
  353. b.u8 (0x15); b.u8 (2); // u8
  354. b.u8 (0x40); b.u16 (513); // u16
  355. b.u8 (0x9C); b.u32 (128500); // u32
  356. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  357. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  358. flp::EventReader r (b.bytes);
  359. flp::Event e;
  360. CHECK (r.header().ppq == 96);
  361. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  362. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  363. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  364. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  365. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  366. CHECK (! r.next (e));
  367. }
  368. TEST (flp_truncated_file_throws)
  369. {
  370. FlpBuilder b;
  371. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  372. flp::EventReader r (b.bytes);
  373. flp::Event e;
  374. bool threw = false;
  375. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  376. CHECK (threw);
  377. }
  378. TEST (flp_version_numbers)
  379. {
  380. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  381. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  382. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  383. }
  384. TEST (flp_playlist_record_size_by_version)
  385. {
  386. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  387. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  388. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  389. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  390. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  391. }
  392. TEST (flp_playlist_record_size_by_file_flags)
  393. {
  394. // REA_rodada2 §4.2: 0xA9 bit 1 -> 80 B, bits 1 and 3 -> the native 88 B record (FL 25.2.99 / 26.x)
  395. CHECK (flp::playlistRecordSize (26001003, 88 * 4, 15) == 88); // 352 is also a multiple of 32
  396. CHECK (flp::playlistRecordSize (26001003, 88 * 10, 15) == 88); // ... and of 80
  397. CHECK (flp::playlistRecordSize (25002005, 160, 7) == 80);
  398. CHECK (flp::playlistRecordSize (21000000, 120, 1) == 60);
  399. CHECK (flp::playlistRecordSize (26001003, 63712) == 88); // no flags: the only size that fits besides 32
  400. }
  401. TEST (flp_native_88_byte_playlist_items_with_clip_gain_and_fades)
  402. {
  403. // an FL 26.1 file: 0xA9 = 15, four 88 B items (352 B: the old 32 B guess read garbage from it)
  404. FlpBuilder b (96);
  405. b.ascii (0xC7, "26.1.3.5570");
  406. b.u8 (0xA9); b.u32 (15);
  407. b.u8 (0x9C); b.u32 (130000);
  408. b.u8 (0x40); b.u16 (0);
  409. b.u8 (0x15); b.u8 (4); // audio clip channel
  410. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  411. std::vector<std::uint8_t> items;
  412. auto add = [&] (std::uint32_t pos, std::uint32_t len, std::uint16_t track, float fadeIn, float fadeOut, float gain) {
  413. std::vector<std::uint8_t> r (88, 0);
  414. const std::uint16_t base = 20480, idx = 0, rv = (std::uint16_t) (499 - track);
  415. const float offs[] = { -1.0f, -1.0f };
  416. std::memcpy (&r[0], &pos, 4);
  417. std::memcpy (&r[4], &base, 2);
  418. std::memcpy (&r[6], &idx, 2);
  419. std::memcpy (&r[8], &len, 4);
  420. std::memcpy (&r[12], &rv, 2);
  421. std::memcpy (&r[0x18], offs, 8);
  422. std::memcpy (&r[0x24], &fadeIn, 4);
  423. std::memcpy (&r[0x2C], &fadeOut, 4);
  424. std::memcpy (&r[0x34], &gain, 4);
  425. r[0x38] = (std::uint8_t) ((fadeIn > 0.0f ? 1 : 0) | (fadeOut > 0.0f ? 2 : 0)); // manual fades (round 180d)
  426. const double stretch = 1.0;
  427. const std::int32_t minusOne = -1;
  428. std::memcpy (&r[0x40], &stretch, 8);
  429. std::memcpy (&r[0x4C], &minusOne, 4);
  430. items.insert (items.end(), r.begin(), r.end());
  431. };
  432. add (0, 384, 0, 0.0f, 0.0f, 1.0f);
  433. add (384, 384, 1, 923.0769f, 461.5385f, 1.65f); // 2 beats in, 1 beat out at 130 BPM
  434. add (768, 96, 2, 0.0f, 0.0f, 0.0f);
  435. add (1536, 192, 3, 0.0f, 5000.0f, 0.5f); // fade longer than the clip
  436. b.data (0xE9, items);
  437. const auto p = flp::parseProject (b.bytes);
  438. CHECK (p.flagsA9 == 15);
  439. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4);
  440. CHECK (p.warnings.empty());
  441. if (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4)
  442. {
  443. const auto& it = p.arrangements[0].items;
  444. CHECK (it[1].position == 384 && it[1].length == 384 && it[1].track == 1 && it[1].kind == flp::PlaylistItem::Kind::channelClip);
  445. CHECK (it[3].position == 1536 && it[3].track == 3);
  446. CHECK (std::abs (it[1].gain - 1.65f) < 1e-6f && std::abs (it[1].fadeInMs - 923.0769f) < 1e-3f);
  447. const auto song = lynx::importFlp (p);
  448. CHECK (song.playlist.size() == 4);
  449. if (song.playlist.size() == 4)
  450. {
  451. const auto beat = (std::int64_t) lynx::ppq;
  452. CHECK (song.playlist[0].gain == 1.0f && song.playlist[0].fadeIn == 0 && song.playlist[0].fadeOut == 0);
  453. CHECK (std::abs (song.playlist[1].gain - 1.65f) < 1e-6f);
  454. CHECK (std::llabs (song.playlist[1].fadeIn - 2 * beat) <= 1 && std::llabs (song.playlist[1].fadeOut - beat) <= 1);
  455. CHECK (song.playlist[2].gain == 0.0f);
  456. CHECK (song.playlist[3].fadeOut == song.playlist[3].length); // clamped
  457. }
  458. }
  459. }
  460. TEST (flp_channel_level_adjustments_and_stretched_clip_start)
  461. {
  462. // FL 26.1 audio clip channel (gf92 follow-up): 0xE5 level adjustments and a time-stretched clip whose item
  463. // start/end are in the sample's milliseconds ("Liwn Passar voz" ch5: 788 ms into a clip stretched by 1.0569)
  464. FlpBuilder b (96);
  465. b.ascii (0xC7, "26.1.3.5570");
  466. b.u8 (0xA9); b.u32 (15);
  467. b.u8 (0x9C); b.u32 (130000);
  468. b.u8 (0x40); b.u16 (0);
  469. b.u8 (0x15); b.u8 (4); // audio clip channel
  470. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  471. {
  472. std::vector<std::uint8_t> levels (24, 0);
  473. const std::int32_t pan = 6400, vol = 10000;
  474. std::memcpy (&levels[0], &pan, 4);
  475. std::memcpy (&levels[4], &vol, 4);
  476. b.data (0xDB, levels);
  477. std::vector<std::uint8_t> adjust (20, 0);
  478. const std::int32_t panAdj = 3200, volAdj = 7286;
  479. std::memcpy (&adjust[0], &panAdj, 4);
  480. std::memcpy (&adjust[4], &volAdj, 4);
  481. b.data (0xE5, adjust);
  482. std::vector<std::uint8_t> params (0x98, 0);
  483. const float time = 64.0f * 192.0f; // stretched to 64 beats (float: 0xA9 bit 0)
  484. const double end = 1.0;
  485. std::memcpy (&params[0x60], &time, 4);
  486. std::memcpy (&params[0x88], &end, 8);
  487. b.data (0xD7, params);
  488. }
  489. {
  490. std::vector<std::uint8_t> r (88, 0);
  491. const std::uint32_t pos = 0, len = 6144; // 64 beats
  492. const std::uint16_t base = 20480, idx = 0, rv = 499;
  493. const float offs[] = { 788.4615f, 788.4615f + 31218.92f }; // sample ms: 64 beats at 130 BPM x 1.056892
  494. const float gain = 1.0f;
  495. std::memcpy (&r[0], &pos, 4);
  496. std::memcpy (&r[4], &base, 2);
  497. std::memcpy (&r[6], &idx, 2);
  498. std::memcpy (&r[8], &len, 4);
  499. std::memcpy (&r[12], &rv, 2);
  500. std::memcpy (&r[0x18], offs, 8);
  501. std::memcpy (&r[0x34], &gain, 4);
  502. b.data (0xE9, r);
  503. }
  504. const auto p = flp::parseProject (b.bytes);
  505. CHECK (p.channels.size() == 1);
  506. if (p.channels.size() != 1)
  507. return;
  508. CHECK (p.channels[0].hasLevelAdjust && p.channels[0].panAdj == 3200 && p.channels[0].volAdj == 7286);
  509. const auto song = lynx::importFlp (p);
  510. CHECK (song.channels.size() == 1 && song.playlist.size() == 1);
  511. if (song.channels.size() != 1 || song.playlist.size() != 1)
  512. return;
  513. // gain = f(volume)·f(volAdj), pan = pan + panAdj (round 190dz: the adjustments are the channel's own Levels
  514. // adjustment, the knobs read as in FL)
  515. const auto expected = lynx::channelGain (10000.0f / 12800.0f) * lynx::channelGain (7286.0f / 12800.0f);
  516. CHECK (std::abs (lynx::channelGain (song.channels[0].volume) * lynx::levelAdjustGain (song.channels[0].misc.levels.volume) - expected) < 1e-5f);
  517. CHECK (std::abs (song.channels[0].volume - 10000.0f / 12800.0f) < 1e-6f);
  518. CHECK (std::abs (lynx::adjustedPan (song.channels[0].pan, song.channels[0].misc.levels.pan) - 0.5f) < 1e-6f);
  519. // 788.46 sample ms / 1.056892 = 746.0 ms = 1.6164 beats into the clip
  520. CHECK (song.channels[0].sampler.stretchBeats > 0.0f);
  521. CHECK (std::llabs (song.playlist[0].offset - (std::int64_t) std::llround (1.61638 * lynx::ppq)) <= 1);
  522. // REA_rodada110 §1: the sub-tick rest is kept (offset + fraction = the exact position, to the sample)
  523. const auto exact = 788.4615 / ((double) (788.4615f + 31218.92f - 788.4615f) / (64.0 * 60000.0 / 130.0)) / 1000.0 * 130.0 / 60.0 * lynx::ppq;
  524. CHECK (std::abs ((double) song.playlist[0].offset + (double) song.playlist[0].offsetFraction - exact) < 1e-3);
  525. CHECK (std::abs (song.playlist[0].offsetFraction) <= 0.5f);
  526. }
  527. TEST (flp_parses_project_model)
  528. {
  529. FlpBuilder b (96);
  530. b.ascii (0xC7, "25.2.5.5319");
  531. b.u8 (0x9C); b.u32 (174500);
  532. b.utf16 (0xC2, u"Título");
  533. b.u8 (0x41); b.u16 (3);
  534. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  535. auto notes = noteRecord (96, 1, 48, 64, 100);
  536. auto n2 = noteRecord (0, 0, 0, 60, 128);
  537. notes.insert (notes.end(), n2.begin(), n2.end());
  538. b.data (0xE0, notes);
  539. b.u8 (0x40); b.u16 (0);
  540. b.u8 (0x15); b.u8 (0);
  541. b.utf16 (0xCB, u"Kick");
  542. {
  543. std::vector<std::uint8_t> levels (24, 0);
  544. const std::int32_t lv[] = { 3200, 12800, -150 };
  545. std::memcpy (levels.data(), lv, sizeof (lv));
  546. b.data (0xDB, levels);
  547. }
  548. b.u8 (0x68); b.u16 (5);
  549. b.u8 (0x40); b.u16 (1);
  550. b.u8 (0x15); b.u8 (2);
  551. b.utf16 (0xC9, u"FLEX");
  552. b.utf16 (0xCB, u"Lead");
  553. b.u8 (0x63); b.u16 (0);
  554. std::vector<std::uint8_t> item (80, 0);
  555. std::uint32_t pos = 384, len = 768;
  556. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  557. std::memcpy (&item[0], &pos, 4);
  558. std::memcpy (&item[4], &base, 2);
  559. std::memcpy (&item[6], &idx, 2);
  560. std::memcpy (&item[8], &len, 4);
  561. std::memcpy (&item[12], &rv, 2);
  562. b.data (0xE9, item);
  563. b.utf16 (0xCC, u"Drums");
  564. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  565. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  566. const auto p = flp::parseProject (b.bytes);
  567. CHECK (p.version == "25.2.5.5319");
  568. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  569. CHECK (p.title == "Título");
  570. CHECK (p.channels.size() == 2);
  571. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  572. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  573. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  574. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  575. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  576. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  577. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  578. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  579. const auto& it = p.arrangements[0].items[0];
  580. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  581. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  582. CHECK (p.warnings.empty());
  583. }
  584. TEST (flp_fuzz_corrupted_files_do_not_crash)
  585. {
  586. if (realFlpDir.empty())
  587. return;
  588. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  589. // must never hang, read past the buffer or produce a Song with invalid indices
  590. std::vector<std::vector<std::uint8_t>> sources;
  591. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  592. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  593. {
  594. std::ifstream in (entry.path(), std::ios::binary);
  595. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  596. }
  597. std::uint32_t seed = 2024;
  598. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  599. int parsed = 0, rejected = 0;
  600. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  601. {
  602. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  603. switch (round % 4)
  604. {
  605. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  606. 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;
  607. case 2: // absurd event size
  608. for (int k = 0; k < 5; ++k)
  609. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  610. break;
  611. default: // shuffled block
  612. {
  613. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  614. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  615. break;
  616. }
  617. }
  618. try
  619. {
  620. const auto project = flp::parseProject (bytes);
  621. const auto song = lynx::importFlp (project);
  622. ++parsed;
  623. for (const auto& p : song.patterns)
  624. for (const auto& n : p.notes)
  625. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  626. for (const auto& c : song.playlist)
  627. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  628. }
  629. catch (const flp::Error&)
  630. {
  631. ++rejected;
  632. }
  633. }
  634. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  635. CHECK (parsed + rejected == 240);
  636. }
  637. TEST (plugin_state_decoders_survive_garbage)
  638. {
  639. // random blobs (varied sizes, including valid headers followed by garbage)
  640. std::uint32_t seed = 77;
  641. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  642. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  643. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  644. int decoded = 0;
  645. for (int round = 0; round < 3000; ++round)
  646. {
  647. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  648. for (auto& b : blob)
  649. b = (std::uint8_t) rnd();
  650. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  651. {
  652. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  653. const auto v = versions[rnd() % 7];
  654. std::memcpy (blob.data(), &v, 4);
  655. }
  656. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  657. {
  658. ++decoded;
  659. for (auto v : fx->params)
  660. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  661. }
  662. lynx::SynthSettings synth;
  663. if (lynx::synthFrom3xOsc (blob, synth))
  664. for (const auto& o : synth.osc)
  665. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  666. float harmlessGain = 1.0f;
  667. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  668. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  669. if (lynx::synthFromFruitKick (blob, synth))
  670. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  671. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  672. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  673. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  674. if (auto sound = lynx::drumSynthLiveSound (blob))
  675. CHECK (sound->size() < 256);
  676. if (lynx::wavetrav::State wt; lynx::wavetrav::parseState (blob, wt))
  677. CHECK (wt.patches[60].waveN >= 0 && wt.patches[60].waveN < lynx::wavetrav::tableCapacity);
  678. lynx::synthFromBooBass (blob, synth);
  679. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  680. lynx::morphineStateUsable (blob);
  681. lynx::poizoneStateUsable (blob);
  682. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  683. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  684. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  685. if (auto patch = lynx::drumpadPatchName (blob))
  686. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  687. if (auto flex = lynx::flexPresetName (blob))
  688. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  689. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  690. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  691. if (lynx::synthFromDX10 (blob, synth))
  692. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  693. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  694. {
  695. std::memcpy (blob.data(), "SSLF", 4);
  696. if (blob.size() > 220)
  697. {
  698. std::memcpy (blob.data() + 40, "DATA", 4);
  699. std::memcpy (blob.data() + 48, "0rev", 4);
  700. }
  701. }
  702. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  703. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  704. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  705. {
  706. const auto params = lynx::miniSynthParams (blob);
  707. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  708. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  709. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  710. }
  711. lynx::ForeignPlugin foreign;
  712. lynx::parseFlWrapper (blob, foreign);
  713. std::vector<std::uint8_t> component, controller;
  714. lynx::splitFlVst3State (blob, component, controller);
  715. }
  716. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  717. }
  718. TEST (inflate_zlib)
  719. {
  720. // fixed Huffman with back-references
  721. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  722. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  723. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  724. // stored (uncompressed) block
  725. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  726. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  727. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  728. // truncated, bad header and size limit
  729. CHECK (! lynx::inflateZlib (fixed, 6));
  730. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  731. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  732. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  733. }
  734. TEST (fl_round15_effects_convert_known_values)
  735. {
  736. auto blob = [] (std::vector<std::int32_t> words) {
  737. std::vector<std::uint8_t> b (words.size() * 4);
  738. std::memcpy (b.data(), words.data(), b.size());
  739. return b;
  740. };
  741. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet — FL's own engine since round 154
  742. // (its controls as (raw − min) / range); unreadable states keep the generic shifter
  743. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  744. CHECK (fs && fs->kind == lynx::EffectKind::flFrequencyShifter);
  745. if (fs)
  746. {
  747. CHECK (std::abs (fs->params[0] - 1.0f) < 1e-6f && fs->params[1] == 0.0f);
  748. CHECK (std::abs (fs->params[2] - (51542.0f + 70000.0f) / 140000.0f) < 1e-6f);
  749. }
  750. const auto fsOld = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0 }));
  751. CHECK (fsOld && fsOld->kind == lynx::EffectKind::freqShift);
  752. // Emphasis (Bien Duro's Master): FL's own engine since round 167f, its controls as (raw − min) / range: input 625
  753. // (+1.5 dB), ceiling 9308 (−1.60 dB), envelope order 2, true peak; an unreadable state keeps the limiter
  754. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  755. CHECK (em && em->kind == lynx::EffectKind::flEmphasis && em->enabled);
  756. if (em)
  757. {
  758. CHECK (std::abs (em->params[6] - 0.0625f) < 1e-6f && std::abs (em->params[9] - 0.9308f) < 1e-6f);
  759. CHECK (std::abs (em->params[2] - 1.0f / 3.0f) < 1e-6f && em->params[3] == 1.0f && std::abs (em->params[11] - 0.5f) < 1e-6f);
  760. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 6, em->params[6], 120.0) == "+1.5 dB");
  761. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 9, em->params[9], 120.0) == "-1.60 dB");
  762. CHECK (em->params[14] == 0.0f && em->params[15] == 0.0f); // a version-8 state has no option bytes: off
  763. }
  764. const auto emOld = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2 }));
  765. CHECK (emOld && emOld->kind == lynx::EffectKind::limiter);
  766. // round 167f: FL's legacy dB Meter / Spectroman keep their slot as LYNX's analyser in the matching view
  767. const auto dbm = lynx::effectFromFl ("Fruity dB Meter", {});
  768. CHECK (dbm && dbm->kind == lynx::EffectKind::waveCandy
  769. && dbm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::peakMeter));
  770. const auto spm = lynx::effectFromFl ("Fruity Spectroman", {});
  771. CHECK (spm && spm->kind == lynx::EffectKind::waveCandy
  772. && spm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::spectrum));
  773. // Pitch Shifter +12 st, mix 100%
  774. 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 }));
  775. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  776. }
  777. TEST (frequency_shifter_moves_spectrum_by_hz)
  778. {
  779. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  780. fx->prepare (48000.0);
  781. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  782. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  783. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  784. std::vector<float> l (48000), r (48000);
  785. for (int i = 0; i < 48000; ++i)
  786. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  787. for (int done = 0; done < 48000; done += 480)
  788. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  789. auto energy = [&] (double f) {
  790. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  791. double s1 = 0.0, s2 = 0.0;
  792. for (int i = 9600; i < 48000; ++i)
  793. {
  794. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  795. s2 = s1;
  796. s1 = s0;
  797. }
  798. return s1 * s1 + s2 * s2 - k * s1 * s2;
  799. };
  800. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  801. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  802. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  803. }
  804. TEST (pitch_shift_moves_frequency_keeps_length)
  805. {
  806. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  807. fx->prepare (48000.0);
  808. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  809. auto crossingsAfter = [&] (float semitoneParam) {
  810. fx->reset();
  811. slot.params[0] = semitoneParam;
  812. std::vector<float> l (48000), r (48000);
  813. for (int i = 0; i < 48000; ++i)
  814. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  815. for (int done = 0; done < 48000; done += 512)
  816. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  817. int crossings = 0;
  818. for (int i = 9600; i < 48000; ++i)
  819. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  820. for (auto v : l)
  821. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  822. return crossings / 0.8 / 2.0; // cycles per second
  823. };
  824. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  825. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  826. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  827. }
  828. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  829. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  830. namespace
  831. {
  832. struct GbPoint
  833. {
  834. double dx, y;
  835. std::uint8_t type;
  836. };
  837. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  838. const std::map<int, std::vector<GbPoint>>& slots)
  839. {
  840. std::vector<std::uint8_t> b;
  841. 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); };
  842. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  843. i32 (0x00080003);
  844. for (auto v : params)
  845. i32 (v);
  846. put (options.data(), options.size());
  847. for (int k = 0; k < 72; ++k)
  848. {
  849. b.push_back (0); // empty name
  850. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  851. b.push_back (0), b.push_back (0), b.push_back (0);
  852. const auto it = slots.find (k);
  853. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  854. const auto& pts = it != slots.end() ? it->second : flat;
  855. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  856. for (const auto& p : pts)
  857. {
  858. put (&p.dx, 8), put (&p.y, 8);
  859. const float tension = 0.0f;
  860. put (&tension, 4);
  861. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  862. }
  863. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  864. }
  865. return b;
  866. }
  867. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  868. {
  869. auto fx = lynx::makeEffect (slot);
  870. fx->prepare (rate);
  871. auto l = in, r = in;
  872. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  873. for (std::size_t done = 0; done < l.size(); done += 512)
  874. {
  875. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  876. fx->setSongPosition ((double) done * tps);
  877. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  878. }
  879. return l;
  880. }
  881. } // namespace
  882. TEST (gross_beat_state_container_and_parameters)
  883. {
  884. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  885. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  886. CHECK (parsed.has_value());
  887. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  888. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  889. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  890. std::vector<std::uint8_t> fl;
  891. int ppq = 0;
  892. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  893. // every raw FL value survives the 0..1 parameter
  894. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  895. bool roundTrip = true;
  896. for (int i = 0; i < 8; ++i)
  897. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  898. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  899. CHECK (roundTrip);
  900. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  901. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  902. }
  903. TEST (gross_beat_volume_slot_gates_the_bar)
  904. {
  905. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  906. 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 } };
  907. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  908. const auto slot = lynx::grossbeat::slotFromFl (state);
  909. CHECK (slot.has_value());
  910. if (! slot)
  911. return;
  912. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  913. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  914. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  915. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  916. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  917. }
  918. TEST (gross_beat_time_slot_reads_the_past_bar)
  919. {
  920. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  921. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  922. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  923. const auto slot = lynx::grossbeat::slotFromFl (state);
  924. CHECK (slot.has_value());
  925. if (! slot)
  926. return;
  927. std::vector<float> in (150000, 0.0f);
  928. in[1000] = 1.0f;
  929. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  930. std::size_t peak = 0;
  931. for (std::size_t i = 0; i < out.size(); ++i)
  932. if (std::abs (out[i]) > std::abs (out[peak]))
  933. peak = i;
  934. CHECK (peak == 1000 + 96000 + 15);
  935. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  936. float elsewhere = 0.0f;
  937. for (std::size_t i = 0; i < out.size(); ++i)
  938. if (i + 20 < peak || i > peak + 20)
  939. elsewhere = std::max (elsewhere, std::abs (out[i]));
  940. CHECK (elsewhere == 0.0f);
  941. }
  942. TEST (volume_gate_follows_bar_position)
  943. {
  944. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  945. fx->prepare (48000.0);
  946. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  947. for (int i = 0; i < 32; ++i)
  948. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  949. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  950. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  951. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  952. fx->setSongPosition (0.0);
  953. for (int done = 0; done < 96000; done += 512)
  954. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  955. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  956. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  957. // starting mid-bar (beat 3): already closed
  958. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  959. fx->reset();
  960. fx->setSongPosition (2.0 * lynx::ppq);
  961. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  962. CHECK (l2[4000] < 0.01f);
  963. // mix 0: transparent
  964. slot.params[32] = 0.0f;
  965. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  966. fx->setSongPosition (2.0 * lynx::ppq);
  967. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  968. CHECK (l3[4000] > 0.99f);
  969. }
  970. TEST (patcher_branch_heads_follow_rea)
  971. {
  972. auto words = [] (std::vector<std::int32_t> v) {
  973. std::vector<std::uint8_t> b (v.size() * 4);
  974. std::memcpy (b.data(), v.data(), b.size());
  975. return b;
  976. };
  977. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  978. std::vector<std::int32_t> fs (18, 0);
  979. fs[0] = 4;
  980. fs[1] = 32768;
  981. fs[3] = 3;
  982. fs[6] = 0;
  983. fs[11] = -600;
  984. fs[15] = 1;
  985. fs[16] = 2;
  986. fs[17] = 3;
  987. const auto split = words (fs);
  988. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  989. CHECK (low.valid && low.slot && low.note.empty());
  990. if (low.slot)
  991. {
  992. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  993. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  994. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  995. }
  996. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  997. 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
  998. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  999. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  1000. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  1001. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  1002. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  1003. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  1004. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  1005. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  1006. }
  1007. TEST (parallel_container_sums_branches)
  1008. {
  1009. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  1010. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  1011. lynx::EffectBranch dry;
  1012. dry.gain = 0.5f;
  1013. lynx::EffectBranch inverted;
  1014. inverted.gain = 0.5f;
  1015. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  1016. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  1017. inverted.chain.push_back (flip);
  1018. slot.branches = { dry, inverted };
  1019. auto fx = lynx::makeParallelEffect (slot);
  1020. fx->prepare (48000.0);
  1021. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  1022. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1023. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  1024. // only the dry branch with gain 2: doubles
  1025. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  1026. auto doubler = lynx::makeParallelEffect (slot);
  1027. doubler->prepare (48000.0);
  1028. std::fill (l.begin(), l.end(), 0.25f);
  1029. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1030. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  1031. }
  1032. TEST (fl_controller_constant_outputs)
  1033. {
  1034. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  1035. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  1036. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  1037. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  1038. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  1039. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  1040. std::vector<std::uint8_t> cs (4, 0);
  1041. cs[0] = 1;
  1042. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  1043. const auto size = (std::uint64_t) body.size();
  1044. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1045. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1046. out.insert (out.end(), body.begin(), body.end());
  1047. };
  1048. std::vector<std::uint8_t> value (12, 0);
  1049. const float v = 0.75f;
  1050. const std::int32_t out = 2;
  1051. std::memcpy (value.data(), &v, 4);
  1052. std::memcpy (value.data() + 8, &out, 4);
  1053. std::vector<std::uint8_t> group;
  1054. chunk (group, 0x836, value);
  1055. chunk (cs, 0, group);
  1056. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  1057. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  1058. }
  1059. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  1060. {
  1061. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  1062. const auto size = (std::uint64_t) body.size();
  1063. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1064. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1065. out.insert (out.end(), body.begin(), body.end());
  1066. };
  1067. auto i32s = [] (std::vector<std::int32_t> v) {
  1068. std::vector<std::uint8_t> b (v.size() * 4);
  1069. std::memcpy (b.data(), v.data(), b.size());
  1070. return b;
  1071. };
  1072. flp::Project p;
  1073. flp::Channel midiOut;
  1074. midiOut.index = 0;
  1075. midiOut.type = flp::ChannelType::native;
  1076. midiOut.pluginName = "MIDI Out";
  1077. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  1078. flp::Channel wrapper;
  1079. wrapper.index = 1;
  1080. wrapper.type = flp::ChannelType::native;
  1081. wrapper.pluginName = "Fruity Wrapper";
  1082. wrapper.pluginState = i32s ({ 9 });
  1083. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  1084. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  1085. p.channels = { midiOut, wrapper };
  1086. flp::Pattern pattern;
  1087. pattern.index = 1;
  1088. flp::Note note;
  1089. note.channel = 0;
  1090. note.key = 64;
  1091. note.length = 96;
  1092. pattern.notes.push_back (note);
  1093. p.patterns.push_back (pattern);
  1094. const auto song = lynx::importFlp (p);
  1095. // round 133: the MIDI Out channel stays a live route (its notes are sent at play time, with its MIDI channel)
  1096. CHECK (song.channels.size() == 2 && song.channels[0].instrument == lynx::InstrumentKind::midiOut && ! song.channels[0].muted);
  1097. CHECK (song.channels[1].plugin.foreign.midiInPort == 3 && song.channels[1].plugin.midiInPort == 3);
  1098. CHECK (lynx::midictl::parseMidiOut (song.channels[0].pluginState).port == 3);
  1099. int toWrapper = 0;
  1100. for (const auto& pat : song.patterns)
  1101. for (const auto& n : pat.notes)
  1102. toWrapper += n.channel == 1 && n.key == 64;
  1103. CHECK (toWrapper == 0);
  1104. }
  1105. TEST (flp_effect_automation_follows_slots_and_params)
  1106. {
  1107. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  1108. flp::Project p;
  1109. flp::MixerInsert insert;
  1110. insert.index = 1;
  1111. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  1112. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  1113. p.mixer.push_back (insert);
  1114. auto clip = [&] (int index, std::uint32_t target) {
  1115. flp::Channel c;
  1116. c.index = index;
  1117. c.type = flp::ChannelType::automationClip;
  1118. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  1119. p.channels.push_back (c);
  1120. p.automationLinks.push_back ({ index, { target } });
  1121. };
  1122. 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; };
  1123. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  1124. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  1125. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  1126. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  1127. const auto song = lynx::importFlp (p);
  1128. CHECK (song.automations.size() == 4);
  1129. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  1130. const auto& freq = song.automations[0];
  1131. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  1132. CHECK (freq.target.param == lynx::peq::param (0, 1));
  1133. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  1134. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  1135. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  1136. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  1137. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  1138. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  1139. // the automated mix is read/written through the target
  1140. auto copy = song;
  1141. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  1142. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  1143. }
  1144. TEST (flp_real_files)
  1145. {
  1146. if (realFlpDir.empty())
  1147. {
  1148. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  1149. return;
  1150. }
  1151. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  1152. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, wrappedVst2Old = 0, markers = 0, signatures = 0, trackEqs = 0;
  1153. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  1154. sytrusSounding = 0;
  1155. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  1156. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  1157. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  1158. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  1159. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  1160. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  1161. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  1162. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  1163. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  1164. int threeOscKind = 0, threeOscSounding = 0;
  1165. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  1166. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  1167. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  1168. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  1169. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  1170. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  1171. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  1172. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  1173. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  1174. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  1175. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  1176. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  1177. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  1178. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  1179. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  1180. int phaserSlots = 0, phaserExact = 0; // REA_rodada105_phaser
  1181. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  1182. lynx::FlpImportOptions withFiles;
  1183. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  1184. withFiles.roots = { flRoot, realFlpDir, "" };
  1185. withFiles.fileExists = [] (const std::string& path) {
  1186. auto local = path;
  1187. std::replace (local.begin(), local.end(), '\\', '/');
  1188. return std::filesystem::exists (local);
  1189. };
  1190. withFiles.readFile = [] (const std::string& path) {
  1191. auto local = path;
  1192. std::replace (local.begin(), local.end(), '\\', '/');
  1193. std::ifstream in (local, std::ios::binary);
  1194. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1195. };
  1196. int embeddedSaved = 0;
  1197. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  1198. ++embeddedSaved;
  1199. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  1200. };
  1201. int slicedChannels = 0, flKeysChannels = 0;
  1202. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  1203. {
  1204. if (entry.path().extension() != ".flp")
  1205. continue;
  1206. std::ifstream in (entry.path(), std::ios::binary);
  1207. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1208. ++files;
  1209. try
  1210. {
  1211. const auto p = flp::parseProject (bytes);
  1212. CHECK (! p.version.empty());
  1213. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  1214. const auto song = lynx::importFlp (p);
  1215. CHECK (! song.patterns.empty());
  1216. {
  1217. const auto full = lynx::importFlp (p, withFiles);
  1218. bool hasFpc = false;
  1219. for (const auto& c : p.channels)
  1220. hasFpc = hasFpc || c.pluginName == "FPC";
  1221. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  1222. // installation) and sounds; two demo channels match golden samples of our round-39 models
  1223. for (const auto& c : p.channels)
  1224. {
  1225. if (c.pluginName == "GMS")
  1226. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  1227. if (c.pluginName == "Morphine")
  1228. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  1229. }
  1230. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  1231. for (const auto& c : p.channels)
  1232. if (c.pluginName == "Kepler")
  1233. {
  1234. ++kepler;
  1235. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  1236. }
  1237. for (const auto& c : full.channels)
  1238. if (c.instrument == lynx::InstrumentKind::kepler)
  1239. {
  1240. ++keplerKind;
  1241. 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 } },
  1242. 44100.0, 11025);
  1243. double e = 0.0;
  1244. bool finite = ! y.empty();
  1245. for (auto v : y)
  1246. finite = finite && std::isfinite (v), e += (double) v * v;
  1247. CHECK (finite);
  1248. keplerSounding += e > 1e-6 ? 1 : 0;
  1249. }
  1250. const auto file = entry.path().filename().string();
  1251. for (const auto& c : full.channels)
  1252. {
  1253. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  1254. if (! isGms && ! isMorphine)
  1255. continue;
  1256. (isGms ? gmsKind : morphineKind)++;
  1257. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1258. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  1259. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  1260. std::vector<float> y;
  1261. if (isGms)
  1262. y = lynx::gms::renderReference (c.pluginState,
  1263. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  1264. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  1265. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1266. else
  1267. 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 } },
  1268. 44100.0, golden ? 100000 : 22050, c.samplePath);
  1269. double e = 0.0;
  1270. bool finite = ! y.empty();
  1271. for (auto v : y)
  1272. finite = finite && std::isfinite (v), e += (double) v * v;
  1273. CHECK (finite);
  1274. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  1275. if (golden && ! y.empty())
  1276. {
  1277. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  1278. const auto rms = std::sqrt (e / (double) y.size());
  1279. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1280. 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 } },
  1281. 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 } };
  1282. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1283. 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);
  1284. ++demoGoldens;
  1285. }
  1286. }
  1287. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1288. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1289. // renders of the installation's DLL for these states)
  1290. for (const auto& c : p.channels)
  1291. if (c.pluginName == "MiniSynth")
  1292. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1293. for (const auto& c : full.channels)
  1294. {
  1295. if (c.instrument != lynx::InstrumentKind::minisynth)
  1296. continue;
  1297. ++miniSynthKind;
  1298. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1299. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1300. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1301. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1302. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1303. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1304. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1305. double e = 0.0;
  1306. bool finite = ! y.empty();
  1307. for (auto v : y)
  1308. finite = finite && std::isfinite (v), e += (double) v * v;
  1309. CHECK (finite);
  1310. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1311. if (golden && ! y.empty())
  1312. {
  1313. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1314. const auto lead = c.name == "Main Lead 1";
  1315. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1316. 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 } },
  1317. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1318. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1319. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1320. ++miniSynthGoldens;
  1321. }
  1322. }
  1323. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1324. // samples of our round-44 model
  1325. for (const auto& c : p.channels)
  1326. if (c.pluginName == "PoiZone")
  1327. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1328. for (const auto& c : full.channels)
  1329. {
  1330. if (c.instrument != lynx::InstrumentKind::poizone)
  1331. continue;
  1332. ++poizoneKind;
  1333. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1334. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1335. const auto y = lynx::poizone::renderReference (c.pluginState,
  1336. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1337. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1338. 44100.0, golden ? 88200 : 22050);
  1339. double e = 0.0;
  1340. bool finite = ! y.empty();
  1341. for (auto v : y)
  1342. finite = finite && std::isfinite (v), e += (double) v * v;
  1343. CHECK (finite);
  1344. poizoneSounding += e > 1e-6 ? 1 : 0;
  1345. if (golden && ! y.empty())
  1346. {
  1347. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1348. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1349. 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 } };
  1350. for (const auto& g : at)
  1351. 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);
  1352. ++poizoneGoldens;
  1353. }
  1354. }
  1355. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1356. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1357. for (const auto& c : p.channels)
  1358. if (c.pluginName == "Toxic Biohazard")
  1359. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1360. for (const auto& c : full.channels)
  1361. {
  1362. if (c.instrument != lynx::InstrumentKind::toxic)
  1363. continue;
  1364. ++toxicKind;
  1365. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1366. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1367. const auto y = lynx::toxic::renderReference (
  1368. c.pluginState,
  1369. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1370. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1371. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1372. double e = 0.0;
  1373. bool finite = ! y.empty();
  1374. for (auto v : y)
  1375. finite = finite && std::isfinite (v), e += (double) v * v;
  1376. CHECK (finite);
  1377. toxicSounding += e > 1e-6 ? 1 : 0;
  1378. if (golden && ! y.empty())
  1379. {
  1380. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1381. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1382. 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 } };
  1383. for (const auto& g : at)
  1384. 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);
  1385. ++toxicGoldens;
  1386. }
  1387. }
  1388. for (const auto& c : full.channels)
  1389. {
  1390. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1391. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1392. {
  1393. ++harmlessCompWanted;
  1394. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1395. }
  1396. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1397. // a Slicex channel is its own instrument since round 190l (no sampler slices): its regions are the
  1398. // region records of its decks, counted here so the bar below still means "sliced channels import"
  1399. std::size_t slicexRegions = 0;
  1400. if (c.instrument == lynx::InstrumentKind::slicex)
  1401. if (const auto state = lynx::slicex::parse (c.pluginState))
  1402. for (const auto& deck : state->decks)
  1403. slicexRegions += deck.empty() ? 0 : deck.regions.size();
  1404. if (c.sampler.slices.size() >= 4 || slicexRegions >= 4)
  1405. ++slicedChannels;
  1406. if (c.sampler.slices.size() >= 4)
  1407. {
  1408. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1409. CHECK (c.sampler.slices.back() < 1.0f);
  1410. }
  1411. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1412. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos && c.instrument != lynx::InstrumentKind::fpc)
  1413. {
  1414. ++dwChannels;
  1415. dwZones += (int) c.zones.size();
  1416. }
  1417. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1418. if (c.instrument == lynx::InstrumentKind::fpc) // (round 190q: a kit on one channel; its pads that play)
  1419. padChannels += (int) lynx::fpcChannelPads (c).size();
  1420. }
  1421. fpcKits += hasFpc;
  1422. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1423. // carrier's Sytrus preset) read from the installation
  1424. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1425. for (const auto& insert : p.mixer)
  1426. for (const auto& slot : insert.slots)
  1427. {
  1428. maximusSlots += slot.plugin == "Maximus";
  1429. delay2Slots += slot.plugin == "Fruity Delay 2";
  1430. delay3Slots += slot.plugin == "Fruity Delay 3";
  1431. sgzSlots += slot.plugin == "Soundgoodizer";
  1432. vocodexSlots += slot.plugin == "Vocodex";
  1433. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1434. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1435. equoSlots += slot.plugin == "EQUO";
  1436. distructorSlots += slot.plugin == "Distructor";
  1437. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1438. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1439. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1440. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1441. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1442. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1443. flangusSlots += slot.plugin == "Fruity Flangus";
  1444. flangerSlots += slot.plugin == "Fruity Flanger";
  1445. phaserSlots += slot.plugin == "Fruity Phaser";
  1446. }
  1447. // (the slots and the nodes of the Patcher graphs, as when Patchers were flattened into slots)
  1448. std::vector<const lynx::EffectSlot*> allSlots;
  1449. std::function<void (const lynx::EffectSlot&)> collect = [&] (const lynx::EffectSlot& fx) {
  1450. allSlots.push_back (&fx);
  1451. for (const auto& n : fx.patcher.nodes)
  1452. if (! n.generator)
  1453. collect (n.effect);
  1454. };
  1455. for (const auto& t : full.mixer)
  1456. for (const auto& fx : t.effects)
  1457. collect (fx);
  1458. for (const auto* slotPointer : allSlots)
  1459. {
  1460. const auto& fx = *slotPointer;
  1461. if (fx.kind == lynx::EffectKind::maximus)
  1462. {
  1463. const auto blob = lynx::maximusfx::unpack (fx.state);
  1464. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1465. if (blob)
  1466. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1467. }
  1468. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1469. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1470. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1471. phaserExact += fx.kind == lynx::EffectKind::fruityPhaser;
  1472. if (fx.kind == lynx::EffectKind::vocodex)
  1473. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1474. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1475. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1476. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1477. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1478. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1479. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1480. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1481. if (fx.kind == lynx::EffectKind::distructor)
  1482. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1483. {
  1484. ++distructorExact;
  1485. for (const auto& m : d->modules)
  1486. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1487. }
  1488. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1489. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1490. || fx.kind == lynx::EffectKind::fruityFilter;
  1491. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1492. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1493. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1494. }
  1495. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1496. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1497. basicSlots - basicSlots0, basicExact - basicExact0);
  1498. for (const auto& pat : full.patterns)
  1499. for (const auto& n : pat.notes)
  1500. {
  1501. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1502. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos
  1503. || full.channels[(std::size_t) n.channel].instrument == lynx::InstrumentKind::fpc ? 1 : 0;
  1504. }
  1505. }
  1506. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1507. for (const auto& t : song.mixer)
  1508. for (const auto& fx : t.effects)
  1509. {
  1510. if (fx.kind == lynx::EffectKind::parametricEq)
  1511. {
  1512. ++peqSlots;
  1513. peqShaped += fx.params != peqDefaults;
  1514. for (double hz : { 50.0, 1000.0, 10000.0 })
  1515. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1516. }
  1517. if (fx.kind == lynx::EffectKind::reverb)
  1518. {
  1519. ++reverbs;
  1520. reverbsDryless += fx.params[4] < 0.01f;
  1521. reverbsLowCut += fx.params[5] > 0.0f;
  1522. }
  1523. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1524. {
  1525. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1526. ++reverbs;
  1527. reverbsDryless += r2.p[10] == 0;
  1528. reverbsLowCut += r2.p[0] >= 20;
  1529. }
  1530. }
  1531. for (const auto& t : song.mixer)
  1532. if (t.eq.active())
  1533. {
  1534. ++trackEqs;
  1535. for (std::size_t b = 0; b < 3; ++b)
  1536. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1537. }
  1538. for (const auto& a : song.arrangements)
  1539. for (const auto& m : a.markers)
  1540. {
  1541. ++markers;
  1542. signatures += m.isSignature();
  1543. CHECK (m.tick >= 0);
  1544. }
  1545. // (round 168g: the patterns' own markers are no longer counted as the Playlist's)
  1546. for (const auto& p : song.patterns)
  1547. for (const auto& m : p.markers)
  1548. {
  1549. ++markers;
  1550. signatures += m.isSignature();
  1551. CHECK (m.tick >= 0);
  1552. }
  1553. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1554. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1555. if (plugin != "Fruity Wrapper")
  1556. return;
  1557. ++wrapped;
  1558. lynx::ForeignPlugin f;
  1559. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1560. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1561. {
  1562. // VST2 state (REA_rodada89 §2): every one decodes, and writes back to the same bytes — except the
  1563. // wrapped Fruity effects of the FL 11 render tests (their mixer slots are read since round 80):
  1564. // those decode but do not write back byte-identical (not confirmed why)
  1565. ++wrappedVst2;
  1566. lynx::FlVst2State st;
  1567. if (lynx::parseFlVst2State (f.state, st) && st.version == -9)
  1568. {
  1569. ++wrappedVst2State;
  1570. wrappedVst2Old += lynx::writeFlVst2State (st) != f.state;
  1571. }
  1572. }
  1573. };
  1574. for (const auto& c : p.channels)
  1575. countWrapper (c.pluginName, c.pluginState);
  1576. for (const auto& insert : p.mixer)
  1577. for (const auto& slot : insert.slots)
  1578. countWrapper (slot.plugin, slot.state);
  1579. // Harmless: every state in the demos becomes a synth, with finite values
  1580. for (const auto& c : p.channels)
  1581. if (c.pluginName == "Harmless")
  1582. {
  1583. ++harmless;
  1584. lynx::SynthSettings hs;
  1585. float g = 1.0f;
  1586. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1587. {
  1588. ++harmlessDecoded;
  1589. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1590. }
  1591. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1592. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1593. }
  1594. for (const auto& c : song.channels)
  1595. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1596. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1597. for (const auto& c : p.channels)
  1598. if (c.pluginName == "Harmor")
  1599. {
  1600. ++harmor;
  1601. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1602. }
  1603. for (const auto& c : song.channels)
  1604. if (c.instrument == lynx::InstrumentKind::harmor)
  1605. {
  1606. ++harmorKind;
  1607. 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);
  1608. double e = 0.0;
  1609. bool finite = ! y.empty();
  1610. for (auto v : y)
  1611. finite = finite && std::isfinite (v), e += (double) v * v;
  1612. CHECK (finite);
  1613. harmorSounding += e > 1e-6 ? 1 : 0;
  1614. }
  1615. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1616. for (const auto& c : p.channels)
  1617. if (c.pluginName == "Ogun")
  1618. {
  1619. ++ogun;
  1620. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1621. }
  1622. for (const auto& c : song.channels)
  1623. if (c.instrument == lynx::InstrumentKind::ogun)
  1624. {
  1625. ++ogunKind;
  1626. 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 },
  1627. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1628. double e = 0.0;
  1629. bool finite = ! y.empty();
  1630. for (auto v : y)
  1631. finite = finite && std::isfinite (v), e += (double) v * v;
  1632. CHECK (finite);
  1633. ogunSounding += e > 1e-6 ? 1 : 0;
  1634. }
  1635. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1636. // Magic Dust stage gets Autogun\Data\SG.dat
  1637. {
  1638. bool any = false;
  1639. for (const auto& c : p.channels)
  1640. if (c.pluginName == "Autogun")
  1641. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1642. if (any)
  1643. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1644. if (c.instrument == lynx::InstrumentKind::autogun)
  1645. {
  1646. ++autogunKind;
  1647. lynx::ogun::RefOptions opt;
  1648. opt.autogun = true;
  1649. if (! c.samplePath.empty())
  1650. {
  1651. auto local = c.samplePath;
  1652. std::replace (local.begin(), local.end(), '\\', '/');
  1653. std::ifstream sg (local, std::ios::binary);
  1654. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1655. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1656. }
  1657. 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 } },
  1658. 44100.0, 11025, opt);
  1659. double e = 0.0;
  1660. bool finite = ! y.empty();
  1661. for (auto v : y)
  1662. finite = finite && std::isfinite (v), e += (double) v * v;
  1663. CHECK (finite);
  1664. autogunSounding += e > 1e-6 ? 1 : 0;
  1665. }
  1666. }
  1667. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1668. for (const auto& c : p.channels)
  1669. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1670. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1671. for (const auto& c : song.channels)
  1672. if (c.instrument == lynx::InstrumentKind::dx10)
  1673. {
  1674. ++dx10Kind;
  1675. 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 } },
  1676. 44100.0, 11025);
  1677. double e = 0.0;
  1678. bool finite = ! y.empty();
  1679. for (auto v : y)
  1680. finite = finite && std::isfinite (v), e += (double) v * v;
  1681. CHECK (finite);
  1682. dx10Sounding += e > 1e-6 ? 1 : 0;
  1683. }
  1684. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1685. for (const auto& c : song.channels)
  1686. if (c.instrument == lynx::InstrumentKind::threeosc)
  1687. {
  1688. ++threeOscKind;
  1689. 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 } },
  1690. 44100.0, 11025);
  1691. double e = 0.0;
  1692. bool finite = ! y.empty();
  1693. for (auto v : y)
  1694. finite = finite && std::isfinite (v), e += (double) v * v;
  1695. CHECK (finite);
  1696. threeOscSounding += e > 1e-6 ? 1 : 0;
  1697. }
  1698. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1699. for (const auto& c : p.channels)
  1700. if (c.pluginName == "Sytrus")
  1701. {
  1702. ++sytrus;
  1703. lynx::SynthSettings ss;
  1704. float g = 1.0f;
  1705. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1706. if (lynx::sytrus::stateUsable (c.pluginState))
  1707. {
  1708. ++sytrusEngine;
  1709. if (sytrusRendered < 24)
  1710. {
  1711. ++sytrusRendered;
  1712. lynx::sytrus::Engine e;
  1713. e.prepare (44100.0, true, 512);
  1714. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1715. lynx::sytrus::NoteParams note;
  1716. e.noteOn (note);
  1717. std::vector<float> l (8192), r (8192);
  1718. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1719. bool finite = true;
  1720. double energy = 0.0;
  1721. for (std::size_t k = 0; k < l.size(); ++k)
  1722. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1723. CHECK (finite);
  1724. sytrusSounding += energy > 1e-6;
  1725. }
  1726. }
  1727. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1728. {
  1729. ++sytrusDecoded;
  1730. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1731. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1732. for (const auto& o : ss.osc)
  1733. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1734. }
  1735. }
  1736. // round 12 generators
  1737. for (const auto& c : p.channels)
  1738. {
  1739. lynx::SynthSettings gs;
  1740. float g = 1.0f;
  1741. int decoded = -1;
  1742. if (c.pluginName == "Harmor")
  1743. {
  1744. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1745. if (decoded)
  1746. {
  1747. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1748. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1749. static int resonant = 0, strong = 0, total = 0;
  1750. resonant += gs.resonance > 0.0f;
  1751. strong += gs.resonance > 0.9f;
  1752. if (++total % 50 == 0)
  1753. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1754. }
  1755. static int saw = 0, square = 0, other = 0, total = 0;
  1756. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1757. {
  1758. auto isSaw = true, isSquare = true;
  1759. for (int n = 1; n <= 16; ++n)
  1760. {
  1761. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1762. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1763. }
  1764. ++(isSaw ? saw : isSquare ? square : other);
  1765. if (++total % 20 == 0 || total == 100)
  1766. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1767. }
  1768. }
  1769. else if (c.pluginName == "GMS")
  1770. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1771. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1772. {
  1773. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1774. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1775. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1776. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1777. {
  1778. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1779. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1780. auto match = true;
  1781. for (std::size_t k = 0; k < 3; ++k)
  1782. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1783. ogunTrumpet += match ? 1 : 0;
  1784. }
  1785. }
  1786. else if (c.pluginName == "Sakura")
  1787. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1788. else if (c.pluginName == "Kepler")
  1789. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1790. else if (c.pluginName == "Wave Traveller")
  1791. decoded = lynx::waveTravellerStateUsable (c.pluginState) ? 2 : 0;
  1792. else if (c.pluginName == "PoiZone")
  1793. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1794. else if (c.pluginName == "BassDrum")
  1795. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1796. else if (c.pluginName == "Toxic Biohazard")
  1797. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1798. else if (c.pluginName == "Morphine")
  1799. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1800. else if (c.pluginName == "BooBass")
  1801. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1802. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1803. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1804. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1805. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1806. else if (c.pluginName == "FLEX")
  1807. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1808. else if (c.pluginName == "Fruity DrumSynth Live")
  1809. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1810. if (decoded < 0)
  1811. continue;
  1812. auto& counter = generators[c.pluginName];
  1813. ++counter.first;
  1814. counter.second += decoded != 0;
  1815. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1816. {
  1817. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1818. 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));
  1819. for (const auto& o : gs.osc)
  1820. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1821. }
  1822. }
  1823. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1824. for (const auto& c : p.channels)
  1825. if (c.pluginName == "3x Osc")
  1826. {
  1827. ++threeOsc;
  1828. lynx::SynthSettings s;
  1829. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1830. {
  1831. ++threeOscDecoded;
  1832. // with AM, osc 3 is the modulation depth and is left out of the sum
  1833. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1834. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1835. for (const auto& o : s.osc)
  1836. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1837. }
  1838. }
  1839. for (auto& pat : p.patterns)
  1840. if (! pat.notes.empty())
  1841. {
  1842. ++withNotes;
  1843. break;
  1844. }
  1845. for (auto& a : p.arrangements)
  1846. for (auto& item : a.items)
  1847. CHECK (item.track >= 0 && item.track < 500);
  1848. for (auto& clip : song.playlist)
  1849. {
  1850. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1851. CHECK (clip.length > 0 && clip.offset >= 0);
  1852. if (clip.kind == lynx::ClipKind::automation)
  1853. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1854. }
  1855. for (auto& a : song.automations)
  1856. {
  1857. ++automations;
  1858. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1859. CHECK (a.isLive() || ! a.points.empty());
  1860. for (std::size_t i = 1; i < a.points.size(); ++i)
  1861. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1862. }
  1863. // imported routing has no cycles and valid destinations
  1864. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1865. for (auto& t : song.mixer)
  1866. for (auto& s : t.sends)
  1867. {
  1868. ++sends;
  1869. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1870. }
  1871. if (! song.playlist.empty())
  1872. ++withPlaylist;
  1873. }
  1874. catch (const flp::Error& e)
  1875. {
  1876. ++failures;
  1877. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1878. }
  1879. }
  1880. CHECK (threeOscDecoded == threeOsc);
  1881. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1882. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1883. sytrusSounding, sytrusRendered);
  1884. CHECK (sytrusEngine == sytrus);
  1885. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1886. for (const auto& [name, counter] : generators)
  1887. {
  1888. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1889. CHECK (counter.second == counter.first);
  1890. }
  1891. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1892. CHECK (ogunTrumpet >= 1);
  1893. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1894. {
  1895. int vocodexes = 0, vocodexSides = 0;
  1896. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1897. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1898. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1899. if (entry.path().extension() == ".flp")
  1900. {
  1901. std::ifstream in (entry.path(), std::ios::binary);
  1902. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1903. try
  1904. {
  1905. const auto project = flp::parseProject (file);
  1906. for (const auto& insert : project.mixer)
  1907. for (const auto& slot : insert.slots)
  1908. if (slot.plugin == "Gross Beat")
  1909. {
  1910. // FL's own engine (REA_rodada53): every demo state is read
  1911. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1912. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1913. ++gates;
  1914. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1915. ++shaped;
  1916. }
  1917. else if (slot.plugin == "Fruity Love Philter")
  1918. {
  1919. ++lovePhilters;
  1920. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1921. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1922. {
  1923. ++lovePhiltersConverted;
  1924. CHECK (! conv->slots.empty());
  1925. for (const auto& fx : conv->slots)
  1926. for (auto v : fx.params)
  1927. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1928. for (const auto& fx : conv->slots)
  1929. for (const auto& link : fx.links)
  1930. {
  1931. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1932. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1933. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1934. }
  1935. for (const auto& link : conv->links)
  1936. {
  1937. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1938. CHECK (link.curve.size() == 33);
  1939. for (auto v : link.curve)
  1940. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1941. }
  1942. }
  1943. }
  1944. else if (slot.plugin == "Vocodex")
  1945. {
  1946. const auto v = lynx::vocodexEffect (slot.state);
  1947. CHECK (v.has_value());
  1948. vocodexes += v.has_value();
  1949. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1950. }
  1951. else if (slot.plugin == "Effector")
  1952. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1953. else if (slot.plugin == "Patcher")
  1954. {
  1955. const auto graph = lynx::parsePatcher (slot.state);
  1956. CHECK (graph.has_value());
  1957. ++patchers;
  1958. if (graph && ! graph->serialChain.empty())
  1959. ++serial;
  1960. if (graph && graph->serialChain.empty() && graph->audio)
  1961. ++parallel;
  1962. }
  1963. for (const auto& c : project.channels)
  1964. if (c.pluginName == "DirectWave")
  1965. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1966. for (const auto& wav : program->embeddedWav)
  1967. if (! wav.empty())
  1968. {
  1969. ++embeddedZones;
  1970. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1971. }
  1972. for (const auto& c : project.channels)
  1973. if (c.pluginName == "Patcher")
  1974. {
  1975. const auto graph = lynx::parsePatcher (c.pluginState);
  1976. CHECK (graph.has_value());
  1977. generatorPatchers += graph && graph->mainGenerator >= 0;
  1978. }
  1979. }
  1980. catch (const flp::Error&) {}
  1981. }
  1982. CHECK (gates >= 30 && shaped == gates);
  1983. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1984. lovePhilters, lovePhiltersRead);
  1985. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1986. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1987. CHECK (vocodexes >= 9);
  1988. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1989. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1990. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1991. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1992. CHECK (embeddedZones == 12);
  1993. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1994. parallel, generatorPatchers);
  1995. CHECK (gates == 0 || shaped > 0);
  1996. }
  1997. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1998. {
  1999. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  2000. if (std::filesystem::exists (path))
  2001. {
  2002. std::ifstream in (path, std::ios::binary);
  2003. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2004. const auto song = lynx::importFlp (flp::parseProject (file));
  2005. const lynx::ChannelDef* controller = nullptr;
  2006. for (const auto& c : song.channels)
  2007. if (c.instrument == lynx::InstrumentKind::controller)
  2008. controller = &c;
  2009. CHECK (controller != nullptr);
  2010. if (controller != nullptr)
  2011. {
  2012. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  2013. inst->prepare (48000.0);
  2014. inst->configure (*controller, song.bpm);
  2015. std::vector<float> l (480), r (480);
  2016. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  2017. inst->noteOn (60, 1.0f);
  2018. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  2019. const auto ducked = inst->controlOutput (0);
  2020. for (int i = 0; i < 100; ++i) // +1 s
  2021. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  2022. const auto recovered = inst->controlOutput (0);
  2023. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  2024. CHECK (ducked < 0.3f && recovered > 0.9f);
  2025. }
  2026. }
  2027. }
  2028. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  2029. {
  2030. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  2031. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  2032. {
  2033. if (entry.path().extension() != ".flp")
  2034. continue;
  2035. std::ifstream in (entry.path(), std::ios::binary);
  2036. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2037. try
  2038. {
  2039. const auto project = flp::parseProject (file);
  2040. for (const auto& c : project.channels)
  2041. if (! c.pluginState.empty())
  2042. samples.emplace (c.pluginName, c.pluginState);
  2043. for (const auto& insert : project.mixer)
  2044. for (const auto& slot : insert.slots)
  2045. if (! slot.state.empty())
  2046. samples.emplace (slot.plugin, slot.state);
  2047. }
  2048. catch (const flp::Error&) {}
  2049. }
  2050. std::uint32_t seed = 12345;
  2051. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  2052. int mutations = 0;
  2053. for (const auto& [name, original] : samples)
  2054. for (int round = 0; round < 40; ++round)
  2055. {
  2056. auto blob = original;
  2057. if (round % 4 == 3 && ! blob.empty())
  2058. blob.resize (rnd() % blob.size()); // truncated
  2059. else
  2060. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  2061. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  2062. ++mutations;
  2063. lynx::SynthSettings synth;
  2064. float gain = 1.0f;
  2065. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  2066. lynx::synthFromHarmor (blob, synth, gain);
  2067. lynx::synthFromGms (blob, 120.0, synth, gain);
  2068. lynx::synthFromOgun (blob, synth, gain);
  2069. lynx::synthFromMorphine (blob, synth, gain);
  2070. lynx::synthFromKepler (blob, synth, gain);
  2071. lynx::synthFromPoiZone (blob, synth, gain);
  2072. lynx::synthFromToxic (blob, synth, gain);
  2073. lynx::synthFromBassDrum (blob, synth, gain);
  2074. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  2075. lynx::harmlessParams (blob);
  2076. if (lynx::harmorStateUsable (blob))
  2077. lynx::harmorFallbacks (blob);
  2078. lynx::parseEnvelopeController (blob);
  2079. lynx::parseKeyboardController (blob);
  2080. lynx::parseLovePhilter (blob);
  2081. lynx::parsePatcher (blob);
  2082. lynx::effectFromFl (name, blob);
  2083. lynx::fpcPads (blob);
  2084. lynx::directWaveProgram (blob);
  2085. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  2086. }
  2087. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  2088. }
  2089. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  2090. {
  2091. int files = 0, decoded = 0, audible = 0;
  2092. double seconds = 0.0;
  2093. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  2094. if (entry.path().extension() == ".wv")
  2095. {
  2096. std::ifstream in (entry.path(), std::ios::binary);
  2097. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2098. ++files;
  2099. lynx::DecodedAudio audio;
  2100. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  2101. continue;
  2102. ++decoded;
  2103. float peak = 0.0f;
  2104. bool finite = true;
  2105. for (const auto& ch : audio.channels)
  2106. for (auto v : ch)
  2107. {
  2108. finite = finite && std::isfinite (v);
  2109. peak = std::max (peak, std::abs (v));
  2110. }
  2111. CHECK (finite && peak <= 4.0f);
  2112. audible += peak > 1e-4f;
  2113. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  2114. }
  2115. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  2116. CHECK (decoded == files);
  2117. }
  2118. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  2119. int gmsPresets = 0, gmsDecoded = 0;
  2120. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  2121. if (std::filesystem::exists (gmsFolder))
  2122. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  2123. if (entry.path().extension() == ".gmsynth")
  2124. {
  2125. std::ifstream in (entry.path(), std::ios::binary);
  2126. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2127. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  2128. const auto n = (std::uint32_t) file.size();
  2129. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  2130. blob.insert (blob.end(), file.begin(), file.end());
  2131. lynx::SynthSettings gs;
  2132. float g = 1.0f;
  2133. ++gmsPresets;
  2134. if (lynx::synthFromGms (blob, 120.0, gs, g))
  2135. {
  2136. ++gmsDecoded;
  2137. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  2138. }
  2139. }
  2140. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  2141. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  2142. int presetFiles = 0, presetsRead = 0;
  2143. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  2144. "PoiZone", "Toxic Biohazard", "BassDrum" })
  2145. {
  2146. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  2147. if (! std::filesystem::exists (folder))
  2148. continue;
  2149. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  2150. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  2151. {
  2152. std::ifstream in (entry.path(), std::ios::binary);
  2153. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2154. ++presetFiles;
  2155. presetsRead += lynx::synthFromFlPreset (file).has_value();
  2156. }
  2157. }
  2158. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  2159. CHECK (presetsRead >= presetFiles * 95 / 100);
  2160. CHECK (gmsDecoded == gmsPresets);
  2161. CHECK (sytrusDecoded == sytrus);
  2162. CHECK (harmlessDecoded == harmless && harmless > 100);
  2163. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  2164. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  2165. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  2166. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  2167. 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",
  2168. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  2169. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  2170. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  2171. miniSynthSounding, miniSynthGoldens);
  2172. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  2173. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  2174. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  2175. poizoneSounding, poizoneGoldens);
  2176. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  2177. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  2178. toxicSounding, toxicGoldens);
  2179. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  2180. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  2181. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  2182. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  2183. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  2184. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  2185. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  2186. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  2187. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  2188. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  2189. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  2190. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  2191. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  2192. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  2193. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  2194. // option; the other Data folders add states with options not decoded
  2195. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  2196. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  2197. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  2198. clipperExact, clipperSlots);
  2199. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  2200. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  2201. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  2202. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  2203. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  2204. // (since round 141 a Patcher stays one slot: "Bombs Away" insert 69 keeps its last PanOMatic)
  2205. CHECK (basicExact >= basicSlots && basicSlots >= 270); // REA_rodada58
  2206. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  2207. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  2208. distructorExact, distructorSlots, distructorImpulses);
  2209. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  2210. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  2211. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  2212. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  2213. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  2214. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  2215. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  2216. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  2217. delay3Exact, delay3Slots);
  2218. // (FL 11 files' slots are read since round 80; Patcher graphs can add Delay 2 nodes of their own)
  2219. CHECK (delay2Exact >= delay2Slots && delay2Slots >= 60 && delay3Exact >= delay3Slots && delay3Slots >= 100); // REA_rodada52
  2220. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  2221. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  2222. 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,
  2223. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  2224. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  2225. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  2226. std::printf (" Fruity Phaser: %d of %d slots on the exact engine\n", phaserExact, phaserSlots);
  2227. CHECK (phaserExact >= phaserSlots && phaserSlots >= 3); // REA_rodada105_phaser
  2228. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  2229. int presets = 0, presetsDecoded = 0;
  2230. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  2231. if (std::filesystem::is_directory (presetDir))
  2232. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  2233. if (entry.path().extension() == ".fst")
  2234. {
  2235. ++presets;
  2236. std::ifstream in (entry.path(), std::ios::binary);
  2237. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2238. if (auto synth = lynx::synthFromFlPreset (bytes))
  2239. {
  2240. ++presetsDecoded;
  2241. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  2242. }
  2243. }
  2244. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  2245. CHECK (presetsDecoded == presets);
  2246. // FL effect presets that become built-in effects
  2247. int fxPresets = 0, fxDecoded = 0;
  2248. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  2249. lynx::EffectKind::utility })
  2250. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  2251. {
  2252. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  2253. if (! std::filesystem::is_directory (dir))
  2254. continue;
  2255. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  2256. if (entry.path().extension() == ".fst")
  2257. {
  2258. ++fxPresets;
  2259. std::ifstream in (entry.path(), std::ios::binary);
  2260. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2261. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  2262. }
  2263. }
  2264. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  2265. CHECK (fxDecoded == fxPresets);
  2266. CHECK (wrappedParsed == wrapped);
  2267. CHECK (wrappedVst2State == wrappedVst2 && wrappedVst2 >= 23 && wrappedVst2Old <= 13);
  2268. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded (%d not written back byte-identical)\n", wrapped,
  2269. wrappedParsed, wrappedVst2, wrappedVst2State, wrappedVst2Old);
  2270. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  2271. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  2272. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  2273. padChannels, padNotes);
  2274. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  2275. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  2276. CHECK (dwExact >= 57);
  2277. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  2278. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  2279. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  2280. CHECK (flKeysChannels >= 8);
  2281. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  2282. CHECK (markers > 300 && signatures > 60);
  2283. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  2284. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  2285. // with its saved settings, in FL too
  2286. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  2287. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  2288. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  2289. CHECK (files > 0);
  2290. CHECK (automations > 0 && sends > 0);
  2291. }
  2292. // ---------------------------------------------------------------------------
  2293. // Sequencer
  2294. TEST (pattern_toggle_step_and_length)
  2295. {
  2296. lynx::Pattern p;
  2297. CHECK (! p.hasStep (0, 4));
  2298. p.toggleStep (0, 4);
  2299. CHECK (p.hasStep (0, 4));
  2300. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  2301. CHECK (p.effectiveLength (lynx::Meter (4, 4)) == 4 * lynx::ppq); // one bar
  2302. p.toggleStep (1, 20); // second bar
  2303. CHECK (p.effectiveLength (lynx::Meter (4, 4)) == 8 * lynx::ppq);
  2304. p.toggleStep (0, 4);
  2305. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  2306. }
  2307. TEST (pattern_mute_removed_steps)
  2308. {
  2309. lynx::Pattern p;
  2310. p.toggleStep (0, 2);
  2311. p.notes[0].velocity = 0.3f;
  2312. p.toggleStep (0, 2, true); // off: muted, kept
  2313. CHECK (! p.hasStep (0, 2) && p.notes.size() == 1 && p.notes[0].muted);
  2314. p.toggleStep (0, 2, true); // on again: the same note back
  2315. CHECK (p.hasStep (0, 2) && p.notes.size() == 1 && ! p.notes[0].muted && p.notes[0].velocity == 0.3f);
  2316. p.toggleStep (0, 2, true);
  2317. // option off (FL's SetStep, round 169f): turning the muted step on adds a new note and leaves the muted one alone...
  2318. p.toggleStep (0, 2);
  2319. CHECK (p.hasStep (0, 2) && p.notes.size() == 2 && p.notes[0].muted != p.notes[1].muted);
  2320. p.toggleStep (0, 2); // ...and turning it off deletes every note of the step, the muted one too
  2321. CHECK (p.notes.empty());
  2322. // option on with two notes on the step: both muted, both back
  2323. p.toggleStep (0, 3);
  2324. p.notes.push_back (p.notes[0]);
  2325. p.notes[1].key = 64;
  2326. p.toggleStep (0, 3, true);
  2327. CHECK (! p.hasStep (0, 3) && p.notes.size() == 2 && p.notes[0].muted && p.notes[1].muted);
  2328. p.toggleStep (0, 3, true);
  2329. CHECK (p.hasStep (0, 3) && p.notes.size() == 2 && ! p.notes[0].muted && ! p.notes[1].muted);
  2330. // another channel's note on the same step is never touched
  2331. p.toggleStep (1, 3);
  2332. p.toggleStep (0, 3, true);
  2333. CHECK (p.hasStep (1, 3) && ! p.hasStep (0, 3));
  2334. }
  2335. TEST (scheduler_places_events_at_exact_samples)
  2336. {
  2337. lynx::Pattern p;
  2338. p.toggleStep (0, 0);
  2339. p.toggleStep (0, 4); // tick 960 = 1 beat
  2340. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  2341. const auto tps = lynx::ticksPerSample (bpm, sr);
  2342. lynx::EventBuffer ev;
  2343. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 23900.0 * tps, 512, tps, ev);
  2344. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  2345. int ons = 0;
  2346. for (auto& e : ev)
  2347. if (e.type == lynx::NoteEvent::Type::on)
  2348. {
  2349. ++ons;
  2350. CHECK (e.sampleOffset == 100);
  2351. }
  2352. CHECK (ons == 1);
  2353. }
  2354. TEST (scheduler_wraps_the_loop)
  2355. {
  2356. lynx::Pattern p;
  2357. p.toggleStep (2, 0);
  2358. const auto len = p.effectiveLength (lynx::Meter (4, 4)); // 3840 ticks
  2359. const double sr = 48000.0;
  2360. const auto tps = lynx::ticksPerSample (120.0, sr);
  2361. lynx::EventBuffer ev;
  2362. // block that crosses the loop end: starts 10 samples before
  2363. const auto start = (double) len - 10.0 * tps;
  2364. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2365. bool found = false;
  2366. for (auto& e : ev)
  2367. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2368. {
  2369. found = true;
  2370. CHECK (e.sampleOffset == 10);
  2371. }
  2372. CHECK (found);
  2373. }
  2374. TEST (scheduler_off_before_on_at_same_sample)
  2375. {
  2376. lynx::Pattern p;
  2377. lynx::Note a;
  2378. a.start = 0; a.length = 240; a.channel = 0;
  2379. lynx::Note b = a;
  2380. b.start = 240;
  2381. p.notes = { a, b };
  2382. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2383. lynx::EventBuffer ev;
  2384. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 200.0, 4000, tps, ev);
  2385. CHECK (ev.size() >= 2);
  2386. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2387. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2388. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2389. }
  2390. TEST (snapshot_exchange_hands_over_and_recycles)
  2391. {
  2392. struct Counted
  2393. {
  2394. int value;
  2395. int* alive;
  2396. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2397. ~Counted() { --*alive; }
  2398. };
  2399. int alive = 0;
  2400. {
  2401. lynx::SnapshotExchange<Counted> x;
  2402. CHECK (x.acquire() == nullptr);
  2403. x.publish (std::make_unique<Counted> (1, &alive));
  2404. CHECK (x.acquire()->value == 1);
  2405. x.publish (std::make_unique<Counted> (2, &alive));
  2406. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2407. CHECK (alive == 2);
  2408. CHECK (x.acquire()->value == 3);
  2409. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2410. x.collectGarbage();
  2411. CHECK (alive == 1);
  2412. // without collection, the return slots fill up and the audio side holds on to the current one
  2413. for (int i = 0; i < 20; ++i)
  2414. {
  2415. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2416. CHECK (x.acquire() != nullptr);
  2417. }
  2418. }
  2419. CHECK (alive == 0);
  2420. }
  2421. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2422. {
  2423. auto song = lynx::Song::makeDefault();
  2424. song.bpm = 120.0;
  2425. const double sr = 48000.0;
  2426. auto state = lynx::RackState::build (song, nullptr, sr);
  2427. lynx::RackRenderer r;
  2428. r.prepare (sr);
  2429. std::vector<float> l (512), rr (512);
  2430. double tick = 0.0;
  2431. float peak = 0.0f;
  2432. int kicks = 0;
  2433. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2434. {
  2435. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2436. for (auto& e : r.lastEvents())
  2437. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2438. ++kicks;
  2439. for (int i = 0; i < 512; ++i)
  2440. {
  2441. CHECK (std::isfinite (l[(std::size_t) i]));
  2442. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2443. }
  2444. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2445. }
  2446. CHECK (peak > 0.1f && peak < 4.0f);
  2447. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2448. // editing the song keeps the instruments of channels whose type didn't change
  2449. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2450. auto next = lynx::RackState::build (song, state.get(), sr);
  2451. CHECK (next->instruments[0] == state->instruments[0]);
  2452. CHECK (next->instruments[4] != state->instruments[4]);
  2453. // stopping silences the synth and triggers no new notes
  2454. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2455. CHECK (r.lastEvents().size() == 0);
  2456. }
  2457. TEST (flp_import_maps_channels_and_rescales)
  2458. {
  2459. flp::Project p;
  2460. p.header.ppq = 96;
  2461. p.tempo = 150.0;
  2462. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2463. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2464. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2465. flp::Pattern pat;
  2466. pat.index = 1;
  2467. flp::Note n;
  2468. n.position = 96;
  2469. n.length = 0;
  2470. n.channel = 2;
  2471. n.key = 72;
  2472. n.velocity = 64;
  2473. pat.notes.push_back (n);
  2474. p.patterns.push_back (pat);
  2475. const auto s = lynx::importFlp (p);
  2476. CHECK (s.bpm == 150.0);
  2477. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2478. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2479. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2480. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2481. CHECK (s.patterns.size() == 1);
  2482. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2483. CHECK (s.patterns[0].notes[0].channel == 2);
  2484. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2485. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2486. }
  2487. TEST (live_queue_is_fifo_and_bounded)
  2488. {
  2489. lynx::LiveEventQueue q;
  2490. lynx::NoteEvent e;
  2491. CHECK (! q.pop (e));
  2492. for (int i = 0; i < 10; ++i)
  2493. {
  2494. lynx::NoteEvent in;
  2495. in.key = i;
  2496. CHECK (q.push (in));
  2497. }
  2498. for (int i = 0; i < 10; ++i)
  2499. CHECK (q.pop (e) && e.key == i);
  2500. int pushed = 0;
  2501. while (q.push (e))
  2502. ++pushed;
  2503. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2504. }
  2505. // Whether this CPU's RCPPS (the ~12-bit reciprocal estimate, whose table is implementation-defined: Intel and AMD
  2506. // differ) gives the same bits as the CPU that the FL golden data was captured on (AMD Zen 2): a hash of the estimate for
  2507. // every float mantissa in [1, 2) (the estimate does not depend on the exponent in the normal range)
  2508. static bool rcpLikeReferenceCpu()
  2509. {
  2510. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  2511. std::uint64_t h = 1469598103934665603ull;
  2512. for (std::uint32_t m = 0; m < (1u << 23); m += 4)
  2513. {
  2514. alignas (16) float f[4];
  2515. for (std::uint32_t j = 0; j < 4; ++j)
  2516. {
  2517. const std::uint32_t bits = 0x3f800000u | (m + j);
  2518. std::memcpy (&f[j], &bits, 4);
  2519. }
  2520. _mm_store_ps (f, _mm_rcp_ps (_mm_load_ps (f)));
  2521. for (std::uint32_t j = 0; j < 4; ++j)
  2522. {
  2523. std::uint32_t bits;
  2524. std::memcpy (&bits, &f[j], 4);
  2525. h = (h ^ bits) * 1099511628211ull;
  2526. }
  2527. }
  2528. return h == 0xb5de670652cb0383ull;
  2529. #else
  2530. return false;
  2531. #endif
  2532. }
  2533. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2534. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2535. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2536. // The sinc kernels take 1/x from RCPPS plus one Newton step, as FL does, so FL's own bits depend on the CPU: they are
  2537. // required where RCPPS matches the capture machine's; elsewhere (Intel CPUs) within a few float ulps.
  2538. TEST (fl_sampler_interpolators_match_fl)
  2539. {
  2540. std::vector<float> l, r;
  2541. std::uint32_t x = 12345;
  2542. for (int k = 0; k < 2 * 144; ++k)
  2543. {
  2544. x = x * 1103515245u + 12345u;
  2545. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2546. (k % 2 == 0 ? l : r).push_back (v);
  2547. }
  2548. const int pad = 40;
  2549. 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 } };
  2550. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2551. const Case cases[] = {
  2552. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2553. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2554. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2555. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2556. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2557. };
  2558. const lynx::flvp::X87Extended precision;
  2559. const bool exactRcp = rcpLikeReferenceCpu();
  2560. for (const auto& c : cases)
  2561. {
  2562. lynx::flrs::Kernel k;
  2563. k.setQuality (c.quality);
  2564. k.setSpeed (c.speed);
  2565. int same = 0;
  2566. for (int p = 0; p < 8; ++p)
  2567. {
  2568. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2569. float a, b;
  2570. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2571. std::uint32_t ba, bb;
  2572. std::memcpy (&ba, &a, 4);
  2573. std::memcpy (&bb, &b, 4);
  2574. if (exactRcp || c.quality < 3) // (linear and 6-point use no reciprocal)
  2575. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2576. else
  2577. same += (std::abs (a - std::bit_cast<float> (c.bits[2 * p])) < 5e-7f) + (std::abs (b - std::bit_cast<float> (c.bits[2 * p + 1])) < 5e-7f);
  2578. }
  2579. CHECK (same == 16);
  2580. }
  2581. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2582. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2583. }
  2584. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2585. // last frame
  2586. TEST (fl_sampler_copies_at_original_rate)
  2587. {
  2588. auto data = std::make_shared<lynx::SampleData>();
  2589. data->sampleRate = 48000.0;
  2590. for (int i = 0; i < 300; ++i)
  2591. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2592. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2593. sampler->prepare (48000.0);
  2594. sampler->noteOn (60, 1.0f);
  2595. std::vector<float> l (400), r (400);
  2596. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2597. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2598. int exact = 0;
  2599. for (int i = 0; i < 300; ++i)
  2600. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2601. CHECK (exact == 300);
  2602. CHECK (l[300] == 0.0f);
  2603. }
  2604. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2605. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2606. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2607. {
  2608. auto data = std::make_shared<lynx::SampleData>();
  2609. data->sampleRate = 48000.0;
  2610. data->left.assign (96000, 0.5f);
  2611. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2612. sampler->prepare (48000.0);
  2613. lynx::ChannelDef c;
  2614. c.instrument = lynx::InstrumentKind::sampler;
  2615. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2616. sampler->configure (c, 120.0);
  2617. sampler->noteOn (60, 1.0f);
  2618. std::vector<float> l (40000), r (40000);
  2619. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2620. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2621. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2622. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2623. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2624. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2625. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2626. sampler->noteOff (60);
  2627. std::fill (l.begin(), l.end(), 0.0f);
  2628. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2629. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2630. CHECK (l[39999] == 0.0f && sampler->idle());
  2631. }
  2632. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2633. // our noise sample, notes at ticks 0/192/384 of 96 PPQ at 120 BPM, 16-bit output) — the fast LP with resonance and a
  2634. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2635. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2636. TEST (fl_native_sampler_voice_matches_fl_renders)
  2637. {
  2638. auto noise = std::make_shared<lynx::SampleData>();
  2639. noise->sampleRate = 44100.0;
  2640. {
  2641. std::uint32_t seed = 1;
  2642. for (int i = 0; i < 26460; ++i)
  2643. {
  2644. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2645. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2646. }
  2647. }
  2648. struct Ev { int on, off; float vel, pan; };
  2649. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2650. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2651. lynx::ChannelDef c;
  2652. c.instrument = lynx::InstrumentKind::sampler;
  2653. c.sampler.flChannel = recs;
  2654. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2655. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2656. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2657. inst->prepare (44100.0);
  2658. lynx::setOfflineRendering (false);
  2659. inst->configure (c, 120.0);
  2660. inst->setSongPosition (0.0);
  2661. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2662. for (int i = 0; i < (int) notes.size(); ++i)
  2663. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2664. std::sort (events.begin(), events.end());
  2665. std::vector<float> l (120000), r (120000);
  2666. int at = 0;
  2667. for (const auto& [when, i] : events)
  2668. {
  2669. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2670. at = when;
  2671. if (i >= 0)
  2672. {
  2673. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2674. inst->noteOn (60, notes[(std::size_t) i].vel);
  2675. }
  2676. else
  2677. inst->noteOff (60);
  2678. }
  2679. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2680. return std::make_pair (l, r);
  2681. };
  2682. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2683. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2684. std::vector<std::int32_t> all = plain;
  2685. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2686. all.insert (all.end(), r->begin(), r->end());
  2687. return all;
  2688. };
  2689. const auto volPlain = std::vector<std::int32_t> { 4, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 };
  2690. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2691. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2692. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2693. int bad = 0;
  2694. for (int n = 0; n < 3; ++n)
  2695. for (int k = 0; k < 8; ++k)
  2696. {
  2697. const auto p = (std::size_t) (44100 * n + at[k]);
  2698. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2699. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2700. }
  2701. return bad;
  2702. };
  2703. {
  2704. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2705. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2706. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2707. noise, chan);
  2708. const int L[] = { -194, -789, 883, 803, 614, -557, -983, -1641, -141, -572, 640, 582, 445, -403, -712, -1189, -207, -841, 942, 856, 655, -593, -1048, -1750 };
  2709. const int R[] = { -194, -789, 883, 803, 614, -557, -983, -1641, -75, -305, 342, 311, 238, -216, -381, -636, -411, -1671, 1871, 1700, 1300, -1179, -2082, -3477 };
  2710. CHECK (check (out, L, R) == 0);
  2711. }
  2712. {
  2713. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2714. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2715. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2716. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2717. { 90, 60, 1 },
  2718. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2719. noise, chan);
  2720. const int L[] = { -1, -203, 29, -298, 97, 0, 0, 0, 0, -156, 22, -228, 74, 0, 0, 0, -1, -203, 29, -2, 0, 0, 0, 0 };
  2721. const int R[] = { -1, -203, 29, -298, 97, 0, 0, 0, 0, -60, 9, -88, 29, 0, 0, 0, -1, -203, 29, -2, 0, 0, 0, 0 };
  2722. CHECK (check (out, L, R) == 0);
  2723. }
  2724. {
  2725. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2726. // tick k (notes on ticks 0, 100, 300)
  2727. auto vol = volPlain;
  2728. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2729. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2730. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2731. noise, chan);
  2732. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2733. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2734. int bad = 0;
  2735. for (int k = 0; k < 10; ++k)
  2736. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2737. CHECK (bad == 0);
  2738. }
  2739. {
  2740. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2741. // after the note-off (no volume envelope)
  2742. auto looped = std::make_shared<lynx::SampleData>();
  2743. looped->sampleRate = 44100.0;
  2744. for (int i = 0; i < 8820; ++i)
  2745. looped->left.push_back ((float) ((0.5 * (1.0 - 0.5 * i / 8820.0)) * std::sin (2.0 * 3.141592653589793 * 441.0 * i / 44100.0)));
  2746. looped->loopStart = 4410, looped->loopEnd = 8820;
  2747. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2748. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2749. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2750. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2751. int bad = 0;
  2752. for (int k = 0; k < 12; ++k)
  2753. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2754. CHECK (bad == 0);
  2755. }
  2756. {
  2757. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2758. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2759. auto dc = std::make_shared<lynx::SampleData>();
  2760. dc->sampleRate = 44100.0;
  2761. dc->left.assign (20000, 0.5f);
  2762. auto level = [&] (float vel, float pan, bool right) {
  2763. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2764. return (right ? out.second : out.first)[5000] / 0.5f;
  2765. };
  2766. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2767. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2768. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2769. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2770. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2771. }
  2772. }
  2773. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2774. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2775. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2776. {
  2777. auto level = [] (float v) {
  2778. auto d = std::make_shared<lynx::SampleData>();
  2779. d->sampleRate = 48000.0;
  2780. d->left.assign (4800, v);
  2781. return d;
  2782. };
  2783. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2784. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2785. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2786. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2787. inst->prepare (48000.0);
  2788. inst->configure (lynx::ChannelDef {}, 120.0);
  2789. auto play = [&] (float velocity) {
  2790. inst->allNotesOff();
  2791. std::vector<float> l (2000), r (2000);
  2792. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2793. std::fill (l.begin(), l.end(), 0.0f);
  2794. inst->noteOn (60, velocity);
  2795. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2796. return l[5] / velocity;
  2797. };
  2798. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2799. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2800. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2801. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2802. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2803. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2804. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2805. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2806. const std::int32_t version = 0x00060003, volume = 16000;
  2807. std::memcpy (state.data(), &version, 4);
  2808. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2809. state.push_back (1);
  2810. state.push_back (5);
  2811. for (char ch : std::string ("x.wav"))
  2812. state.push_back ((std::uint8_t) ch);
  2813. const auto sliced = lynx::slicexSample (state);
  2814. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2815. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2816. }
  2817. TEST (fl_keys_state_wti_and_loader)
  2818. {
  2819. using namespace lynx::flkeys;
  2820. // state: u32 1, the wavetable name, 15 int32 knobs
  2821. State st;
  2822. st.wavetable = "Grand Piano";
  2823. for (int i = 0; i < 15; ++i)
  2824. st.knobs[(std::size_t) i] = 10 * i - 50;
  2825. const auto bytes = writeState (st);
  2826. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2827. const auto back = parseState (bytes);
  2828. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2829. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2830. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2831. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2832. std::vector<std::string> names;
  2833. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2834. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2835. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2836. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2837. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2838. if (riff)
  2839. {
  2840. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2841. FileSamples a;
  2842. for (int i = 0; i < 10; ++i)
  2843. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2844. const auto decoded = FlKeysTable::decode (riff->encode());
  2845. CHECK (decoded.has_value());
  2846. const auto w = buildWavetable (*decoded, { a });
  2847. CHECK (w != nullptr);
  2848. if (w != nullptr)
  2849. {
  2850. 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; };
  2851. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2852. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2853. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2854. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2855. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2856. }
  2857. }
  2858. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2859. // loop as long as the keygroup's end, so the voice stops there
  2860. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2861. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2862. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2863. if (raw)
  2864. {
  2865. FileSamples x;
  2866. for (int i = 0; i < 10; ++i)
  2867. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2868. const auto w = buildWavetable (*raw, { x });
  2869. CHECK (w != nullptr);
  2870. if (w != nullptr)
  2871. {
  2872. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2873. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2874. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2875. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2876. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2877. State s2;
  2878. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2879. RefNote n;
  2880. n.key = 60;
  2881. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2882. double early = 0, late = 0;
  2883. for (int i = 0; i < 64; ++i)
  2884. early += std::fabs (out[(std::size_t) (2 * i)]);
  2885. for (int i = 2048; i < 4096; ++i)
  2886. late += std::fabs (out[(std::size_t) (2 * i)]);
  2887. CHECK (early > 0.0 && late == 0.0);
  2888. }
  2889. }
  2890. // instrument front-end without a wavetable: silent and idle
  2891. auto inst = makeFlKeysInstrument (nullptr);
  2892. inst->prepare (44100.0);
  2893. inst->noteOn (60, 0.78f);
  2894. std::vector<float> l (256), r (256);
  2895. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2896. CHECK (inst->idle() && l[100] == 0.0f);
  2897. }
  2898. TEST (ogg_vorbis_decodes_like_fl)
  2899. {
  2900. using namespace lynx::oggvorbis;
  2901. const std::uint8_t wavHeader[] = { 'R', 'I', 'F', 'F', 30, 0, 0, 0, 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ', 2, 0, 0, 0, 0x4F, 0x67 };
  2902. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2903. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2904. Decoded none;
  2905. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2906. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2907. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2908. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2909. if (realFlpDir.empty())
  2910. return;
  2911. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2912. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2913. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2914. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2915. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2916. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2917. };
  2918. for (const auto& c : cases)
  2919. {
  2920. const auto path = root / c.file;
  2921. if (! std::filesystem::exists (path))
  2922. continue;
  2923. std::ifstream in (path, std::ios::binary);
  2924. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2925. CHECK (isOggWav (bytes.data(), bytes.size()));
  2926. Decoded d;
  2927. CHECK (decode (bytes.data(), bytes.size(), d));
  2928. if (d.channels == 0)
  2929. continue;
  2930. std::uint64_t h = 1469598103934665603ULL;
  2931. for (const auto& ch : d.data)
  2932. {
  2933. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2934. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2935. h = (h ^ p[i]) * 1099511628211ULL;
  2936. }
  2937. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2938. CHECK (c.channels == 0 || d.channels == c.channels);
  2939. CHECK (h == c.hash);
  2940. }
  2941. }
  2942. TEST (directwave_crt_math_and_state)
  2943. {
  2944. using namespace lynx::dw;
  2945. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2946. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2947. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2948. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2949. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2950. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2951. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2952. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2953. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2954. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2955. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2956. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2957. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2958. }
  2959. TEST (directwave_matches_fl_with_demo_programs)
  2960. {
  2961. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2962. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2963. if (realFlpDir.empty())
  2964. return;
  2965. using namespace lynx::dw;
  2966. auto readAll = [] (const std::filesystem::path& p) {
  2967. std::ifstream in (p, std::ios::binary);
  2968. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2969. };
  2970. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2971. const auto path = std::filesystem::path (realFlpDir) / file;
  2972. if (! std::filesystem::exists (path))
  2973. return std::vector<std::uint8_t>();
  2974. const auto p = flp::parseProject (readAll (path));
  2975. for (const auto& c : p.channels)
  2976. if (c.pluginName == "DirectWave" && c.name == channel)
  2977. return c.pluginState;
  2978. return std::vector<std::uint8_t>();
  2979. };
  2980. auto sampleValue = [] (int z, std::int64_t j) {
  2981. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2982. const auto w = ((j * j + z * 31) % 997) - 498;
  2983. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2984. };
  2985. std::vector<RefNote> notes;
  2986. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2987. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2988. notes.push_back (n);
  2989. };
  2990. 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);
  2991. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2992. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2993. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2994. const auto st = parseState (stateOf (file, channel));
  2995. if (! st)
  2996. return;
  2997. const auto* prog = playedProgram (*st);
  2998. CHECK (prog != nullptr);
  2999. if (prog == nullptr)
  3000. return;
  3001. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  3002. for (int z = 0; z < (int) prog->zones.size(); ++z)
  3003. {
  3004. auto s = std::make_shared<ZoneSamples>();
  3005. s->bits = 16;
  3006. s->channels = 1;
  3007. s->frames = prog->zones[(std::size_t) z].frames;
  3008. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  3009. for (std::int64_t j = 0; j < s->frames; ++j)
  3010. v[(std::size_t) j + 256] = sampleValue (z, j);
  3011. s->i16.push_back (std::move (v));
  3012. zs.push_back (s);
  3013. }
  3014. EngineOptions o;
  3015. o.velocityAsInitVol = (st->flags & 2) != 0;
  3016. o.noGainClamp = (st->flags & 1) != 0;
  3017. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  3018. std::uint64_t h = 14695981039346656037ull;
  3019. for (const float x : out)
  3020. {
  3021. std::uint32_t b;
  3022. std::memcpy (&b, &x, 4);
  3023. for (int k = 0; k < 4; ++k)
  3024. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  3025. }
  3026. CHECK (h == expected);
  3027. if (h != expected)
  3028. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  3029. };
  3030. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  3031. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  3032. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  3033. }
  3034. TEST (fl_keys_matches_fl_with_the_installation)
  3035. {
  3036. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  3037. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  3038. if (realFlpDir.empty())
  3039. return;
  3040. using namespace lynx::flkeys;
  3041. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  3042. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  3043. return;
  3044. auto readAll = [] (const std::filesystem::path& p) {
  3045. std::ifstream f (p, std::ios::binary);
  3046. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  3047. };
  3048. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  3049. FileSamples f;
  3050. std::uint16_t channels = 1;
  3051. for (std::size_t pos = 12; pos + 8 <= w.size();)
  3052. {
  3053. std::uint32_t size = 0;
  3054. std::memcpy (&size, w.data() + pos + 4, 4);
  3055. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  3056. std::memcpy (&channels, w.data() + pos + 10, 2);
  3057. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  3058. {
  3059. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  3060. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  3061. f.channels = channels;
  3062. }
  3063. pos += 8 + size + (size & 1);
  3064. }
  3065. return f;
  3066. };
  3067. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  3068. const auto wti = readAll (folder / (name + ".wti"));
  3069. std::vector<std::string> names;
  3070. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  3071. CHECK (table.has_value());
  3072. if (! table)
  3073. return;
  3074. std::vector<FileSamples> files;
  3075. for (std::size_t i = 0; i < names.size(); ++i)
  3076. {
  3077. auto rel = names[i];
  3078. std::replace (rel.begin(), rel.end(), '\\', '/');
  3079. const auto bytes = readAll (folder / rel);
  3080. if (table->raw)
  3081. {
  3082. FileSamples f;
  3083. f.samples.resize (bytes.size() / 2);
  3084. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  3085. files.push_back (std::move (f));
  3086. }
  3087. else
  3088. {
  3089. table->files[i] = wavFileInfo (bytes);
  3090. files.push_back (pcm (bytes));
  3091. }
  3092. }
  3093. const auto w = buildWavetable (*table, files);
  3094. CHECK (w != nullptr);
  3095. if (w == nullptr)
  3096. return;
  3097. State s;
  3098. s.wavetable = name;
  3099. s.knobs = knobs;
  3100. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  3101. RefNote n;
  3102. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  3103. return n;
  3104. };
  3105. 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),
  3106. 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) };
  3107. // (floats as the harness parses them)
  3108. for (auto& n : notes)
  3109. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  3110. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  3111. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  3112. std::uint64_t h = 0xcbf29ce484222325ull;
  3113. for (auto v : out)
  3114. {
  3115. std::uint32_t b;
  3116. std::memcpy (&b, &v, 4);
  3117. for (int k = 0; k < 4; ++k)
  3118. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  3119. }
  3120. CHECK (h == expected);
  3121. };
  3122. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  3123. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  3124. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  3125. }
  3126. TEST (sampler_plays_pitched_one_shot)
  3127. {
  3128. // 1000-frame ramp sample: the position can be measured from the amplitude
  3129. auto data = std::make_shared<lynx::SampleData>();
  3130. data->sampleRate = 48000.0;
  3131. for (int i = 0; i < 1000; ++i)
  3132. data->left.push_back (1.0f);
  3133. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3134. sampler->prepare (48000.0);
  3135. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  3136. sampler->noteOn (60, 1.0f);
  3137. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  3138. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3139. int sounding = 0;
  3140. for (auto s : l)
  3141. sounding += s > 0.5f;
  3142. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  3143. std::fill (l.begin(), l.end(), 0.0f);
  3144. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  3145. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3146. sounding = 0;
  3147. for (auto s : l)
  3148. sounding += s > 0.5f;
  3149. CHECK (sounding >= 495 && sounding <= 500);
  3150. CHECK (r[10] > 0.5f); // mono goes to both sides
  3151. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  3152. silent->prepare (48000.0);
  3153. silent->noteOn (60, 1.0f);
  3154. std::fill (l.begin(), l.end(), 0.0f);
  3155. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  3156. CHECK (l[0] == 0.0f);
  3157. }
  3158. TEST (rack_live_notes_and_sample_provider)
  3159. {
  3160. lynx::Song song;
  3161. lynx::ChannelDef c;
  3162. c.name = "Smp";
  3163. c.instrument = lynx::InstrumentKind::sampler;
  3164. c.samplePath = "a.wav";
  3165. song.channels.push_back (c);
  3166. song.patterns.emplace_back();
  3167. int loads = 0;
  3168. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  3169. ++loads;
  3170. CHECK (path == "a.wav");
  3171. auto d = std::make_shared<lynx::SampleData>();
  3172. d->left.assign (4800, 0.5f);
  3173. return d;
  3174. };
  3175. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  3176. CHECK (loads == 1);
  3177. lynx::RackRenderer renderer;
  3178. renderer.prepare (48000.0);
  3179. lynx::LiveEventQueue live;
  3180. lynx::NoteEvent on;
  3181. on.channel = 0;
  3182. on.velocity = 1.0f;
  3183. live.push (on);
  3184. std::vector<float> l (256), r (256);
  3185. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  3186. CHECK (l[0] > 0.1f);
  3187. // same sample: reused; different sample: reloaded
  3188. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  3189. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  3190. song.channels[0].samplePath = "b.wav";
  3191. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  3192. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  3193. }
  3194. TEST (flp_paths_resolve_and_samplers_import)
  3195. {
  3196. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  3197. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  3198. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  3199. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  3200. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  3201. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  3202. {
  3203. // POSIX roots (macOS, Linux, Wine prefix): backslashes become '/', letter case follows the disk (round 117)
  3204. namespace fs = std::filesystem;
  3205. const auto base = fs::temp_directory_path() / ("lynx-case-test-" + processTempTag());
  3206. fs::remove_all (base);
  3207. fs::create_directories (base / "Data" / "Patches" / "Packs");
  3208. { std::ofstream (base / "Data" / "Patches" / "Packs" / "Kick.WAV") << "x"; }
  3209. lynx::FlPathRoots posix;
  3210. posix.factoryData = base.string();
  3211. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\Patches\\Packs\\Kick.WAV", posix) == (base / "Data/Patches/Packs/Kick.WAV").string());
  3212. const auto lowerCase = lynx::resolveFlPath ("%FLStudioFactoryData%\\data\\patches\\packs\\kick.wav", posix);
  3213. if (fs::exists (base / "data")) // case-insensitive file system (macOS): any spelling opens the file
  3214. CHECK (fs::exists (lowerCase));
  3215. else
  3216. CHECK (lowerCase == (base / "Data/Patches/Packs/Kick.WAV").string());
  3217. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\missing.wav", posix) == (base / "Data/missing.wav").string());
  3218. fs::remove_all (base);
  3219. }
  3220. flp::Project p;
  3221. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  3222. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  3223. lynx::FlpImportOptions options;
  3224. options.roots = roots;
  3225. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  3226. const auto s = lynx::importFlp (p, options);
  3227. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  3228. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  3229. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  3230. }
  3231. TEST (output_protection_is_transparent_up_to_full_scale)
  3232. {
  3233. // round 160: no more soft knee at 0.9 — FL leaves the mix untouched up to 0 dBFS
  3234. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f, 0.999f, std::nanf (""), -1.0f };
  3235. lynx::clipOvers (s, 8);
  3236. CHECK (s[0] == 0.5f && s[1] == -0.89f && s[4] == 0.95f && s[5] == 0.999f && s[7] == -1.0f);
  3237. CHECK (s[2] == 1.0f && s[3] == -1.0f);
  3238. CHECK (s[6] == 0.0f);
  3239. }
  3240. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  3241. {
  3242. lynx::Song s;
  3243. s.channels.resize (1);
  3244. auto& p = s.patterns.emplace_back();
  3245. for (int step : { 0, 4, 8, 12 }) // one note per beat
  3246. p.toggleStep (0, step);
  3247. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  3248. lynx::Clip c;
  3249. c.pattern = 0;
  3250. c.start = 4 * lynx::ppq;
  3251. c.length = 2 * lynx::ppq;
  3252. c.offset = 2 * lynx::ppq;
  3253. s.playlist.push_back (c);
  3254. CHECK (s.songLength() == 6 * lynx::ppq); // FL: the song ends where the last clip ends
  3255. const double sr = 48000.0;
  3256. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  3257. lynx::EventBuffer ev;
  3258. // whole song in a single block (8 beats = 192000 samples)
  3259. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3260. std::vector<int> ons;
  3261. for (auto& e : ev)
  3262. if (e.type == lynx::NoteEvent::Type::on)
  3263. ons.push_back (e.sampleOffset);
  3264. CHECK (ons.size() == 2);
  3265. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  3266. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  3267. ev.clear();
  3268. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  3269. CHECK (ev.size() == 0);
  3270. // a note longer than the clip is cut at the clip end
  3271. p.notes[3].length = 4 * lynx::ppq;
  3272. ev.clear();
  3273. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3274. int lastOff = -1;
  3275. for (auto& e : ev)
  3276. if (e.type == lynx::NoteEvent::Type::off)
  3277. lastOff = std::max (lastOff, e.sampleOffset);
  3278. CHECK (lastOff == 6 * 24000);
  3279. }
  3280. TEST (remove_pattern_fixes_clips)
  3281. {
  3282. lynx::Song s;
  3283. s.patterns.resize (3);
  3284. s.currentPattern = 2;
  3285. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  3286. s.removePattern (1);
  3287. CHECK (s.patterns.size() == 2);
  3288. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  3289. CHECK (s.currentPattern == 1);
  3290. }
  3291. TEST (flp_import_3x_osc_state_and_channel_filter)
  3292. {
  3293. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  3294. std::vector<std::uint8_t> blob (460, 0);
  3295. auto put = [&] (std::size_t off, std::int32_t v) {
  3296. for (int b = 0; b < 4; ++b)
  3297. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  3298. };
  3299. put (0x00, 14);
  3300. put (0x08, 3); // osc 1 saw
  3301. put (0x10, 7); // osc 1 fine +7
  3302. put (0x04, -32); // osc 1 pan -0.5
  3303. put (0x1C, 64); // osc 2 mix
  3304. put (0x28, -12); // osc 2 coarse
  3305. put (0x38, 32); // osc 3 mix
  3306. put (0x40, 2); // osc 3 square
  3307. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  3308. lynx::SynthSettings s;
  3309. CHECK (lynx::synthFrom3xOsc (blob, s));
  3310. 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);
  3311. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  3312. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  3313. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  3314. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  3315. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  3316. float hz = 0.0f, res = 1.0f;
  3317. int mode = -1;
  3318. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  3319. CHECK (hz >= 19999.0f && res == 0.0f);
  3320. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  3321. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  3322. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  3323. CHECK (hz > 240.0f && hz < 320.0f);
  3324. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  3325. CHECK (res > 0.8f);
  3326. CHECK (mode == 0);
  3327. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  3328. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  3329. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  3330. CHECK (mode == 1);
  3331. }
  3332. TEST (synth_pitch_and_filter_envelopes)
  3333. {
  3334. auto render = [] (const lynx::SynthSettings& s, int frames) {
  3335. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3336. inst->prepare (48000.0);
  3337. lynx::ChannelDef c;
  3338. c.synth = s;
  3339. inst->configure (c, 120.0);
  3340. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  3341. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3342. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  3343. return l;
  3344. };
  3345. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  3346. int n = 0;
  3347. for (int i = from + 1; i < to; ++i)
  3348. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  3349. return n;
  3350. };
  3351. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  3352. lynx::SynthSettings kick;
  3353. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3354. kick.cutoff = 20000.0f;
  3355. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  3356. const auto k = render (kick, 48000);
  3357. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  3358. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  3359. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  3360. CHECK (std::abs (late - 110) < 4);
  3361. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  3362. lynx::SynthSettings pluck;
  3363. pluck.cutoff = 200.0f;
  3364. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  3365. const auto p = render (pluck, 48000);
  3366. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  3367. double e = 0.0;
  3368. for (int i = from + 1; i < to; ++i)
  3369. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  3370. return e;
  3371. };
  3372. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  3373. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  3374. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  3375. lynx::SynthSettings toxic = pluck;
  3376. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  3377. const auto t = render (toxic, 48000);
  3378. auto thf = [&] (int from, int to) {
  3379. double e = 0.0;
  3380. for (int i = from + 1; i < to; ++i)
  3381. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  3382. return e;
  3383. };
  3384. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  3385. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  3386. }
  3387. TEST (synth_mono_reuses_the_sounding_voice)
  3388. {
  3389. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3390. inst->prepare (48000.0);
  3391. lynx::ChannelDef c;
  3392. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3393. c.synth.cutoff = 20000.0f;
  3394. c.synth.mono = true;
  3395. inst->configure (c, 120.0);
  3396. std::vector<float> l (48000), r (48000);
  3397. inst->noteOn (57, 1.0f);
  3398. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3399. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3400. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3401. int crossings = 0;
  3402. float peak = 0.0f;
  3403. for (std::size_t i = 24001; i < 48000; ++i)
  3404. {
  3405. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3406. peak = std::max (peak, std::abs (l[i]));
  3407. }
  3408. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3409. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3410. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3411. float jump = 0.0f;
  3412. for (std::size_t i = 4790; i < 4810; ++i)
  3413. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3414. CHECK (jump < 0.05f);
  3415. }
  3416. TEST (synth_porta_glides_and_am_modulates)
  3417. {
  3418. auto make = [] (const lynx::SynthSettings& s) {
  3419. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3420. inst->prepare (48000.0);
  3421. lynx::ChannelDef c;
  3422. c.synth = s;
  3423. inst->configure (c, 120.0);
  3424. return inst;
  3425. };
  3426. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3427. int n = 0;
  3428. for (auto i = from + 1; i < to; ++i)
  3429. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3430. return n;
  3431. };
  3432. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3433. lynx::SynthSettings s;
  3434. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3435. s.cutoff = 20000.0f;
  3436. s.mono = true;
  3437. s.porta = true;
  3438. s.portaBeats = 1.0f;
  3439. auto inst = make (s);
  3440. std::vector<float> l (96000), r (96000);
  3441. inst->noteOn (57, 1.0f);
  3442. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3443. inst->noteOn (69, 1.0f);
  3444. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3445. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3446. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3447. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3448. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3449. lynx::SynthSettings am;
  3450. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3451. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3452. am.cutoff = 20000.0f;
  3453. am.am = true;
  3454. auto modulated = make (am);
  3455. modulated->noteOn (69, 1.0f);
  3456. std::fill (l.begin(), l.end(), 0.0f);
  3457. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3458. float peak = 0.0f;
  3459. for (std::size_t i = 4800; i < 48000; ++i)
  3460. peak = std::max (peak, std::abs (l[i]));
  3461. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3462. }
  3463. TEST (channel_lfo_vibrato_and_tremolo)
  3464. {
  3465. auto render = [] (const lynx::SynthSettings& s) {
  3466. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3467. inst->prepare (48000.0);
  3468. lynx::ChannelDef c;
  3469. c.synth = s;
  3470. inst->configure (c, 120.0);
  3471. inst->noteOn (69, 1.0f);
  3472. std::vector<float> l (48000), r (48000);
  3473. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3474. return l;
  3475. };
  3476. lynx::SynthSettings s;
  3477. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3478. s.cutoff = 20000.0f;
  3479. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3480. auto tremolo = s;
  3481. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3482. const auto t = render (tremolo);
  3483. float lowest = 1.0f, highest = 0.0f;
  3484. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3485. {
  3486. float peak = 0.0f;
  3487. for (auto i = w; i < w + 480; ++i)
  3488. peak = std::max (peak, std::abs (t[i]));
  3489. lowest = std::min (lowest, peak);
  3490. highest = std::max (highest, peak);
  3491. }
  3492. CHECK (lowest < 0.08f && highest > 0.5f);
  3493. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3494. auto vibrato = s;
  3495. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3496. const auto v = render (vibrato);
  3497. int crossings = 0;
  3498. for (std::size_t i = 1; i < 48000; ++i)
  3499. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3500. CHECK (std::abs (crossings - 880) < 12);
  3501. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3502. for (std::size_t i = 1; i < 6000; ++i)
  3503. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3504. CHECK (firstQuarter > 112);
  3505. }
  3506. TEST (sampler_porta_glides_from_previous_note)
  3507. {
  3508. auto data = std::make_shared<lynx::SampleData>();
  3509. data->sampleRate = 48000.0;
  3510. data->left.resize (96000);
  3511. for (std::size_t i = 0; i < data->left.size(); ++i)
  3512. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3513. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3514. inst->prepare (48000.0);
  3515. lynx::ChannelDef c;
  3516. c.sampler.cut = true;
  3517. c.sampler.porta = true;
  3518. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3519. inst->configure (c, 120.0);
  3520. std::vector<float> l (24000), r (24000);
  3521. inst->noteOn (60, 1.0f);
  3522. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3523. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3524. std::fill (l.begin(), l.end(), 0.0f);
  3525. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3526. auto crossings = [&] (std::size_t from, std::size_t to) {
  3527. int n = 0;
  3528. for (auto i = from + 1; i < to; ++i)
  3529. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3530. return n;
  3531. };
  3532. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3533. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3534. CHECK (start < 22);
  3535. CHECK (end > 36);
  3536. }
  3537. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3538. {
  3539. auto sine = [] (double hz, float amp) {
  3540. auto d = std::make_shared<lynx::SampleData>();
  3541. d->sampleRate = 48000.0;
  3542. d->left.resize (48000);
  3543. for (std::size_t i = 0; i < d->left.size(); ++i)
  3544. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3545. return d;
  3546. };
  3547. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3548. // hard layer on the highs: same tuning, louder
  3549. std::vector<lynx::LoadedZone> zones {
  3550. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3551. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3552. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3553. };
  3554. auto inst = lynx::makeZonedSampler (zones);
  3555. inst->prepare (48000.0);
  3556. inst->configure (lynx::ChannelDef {}, 120.0);
  3557. auto play = [&] (int key, float velocity) {
  3558. inst->allNotesOff();
  3559. std::vector<float> l (24000), r (24000);
  3560. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3561. std::fill (l.begin(), l.end(), 0.0f);
  3562. inst->noteOn (key, velocity);
  3563. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3564. int crossings = 0;
  3565. float peak = 0.0f;
  3566. for (std::size_t i = 1; i < 24000; ++i)
  3567. {
  3568. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3569. peak = std::max (peak, std::abs (l[i]));
  3570. }
  3571. return std::pair { crossings, peak };
  3572. };
  3573. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3574. CHECK (std::abs (low.first - 440) < 4);
  3575. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3576. CHECK (std::abs (soft.first - 880) < 6);
  3577. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3578. CHECK (hard.second > soft.second * 2.0f);
  3579. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3580. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3581. rr->prepare (48000.0);
  3582. rr->configure (lynx::ChannelDef {}, 120.0);
  3583. float peaks[2] {};
  3584. for (int k = 0; k < 2; ++k)
  3585. {
  3586. rr->allNotesOff();
  3587. std::vector<float> l (4800), r (4800);
  3588. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3589. std::fill (l.begin(), l.end(), 0.0f);
  3590. rr->noteOn (60, 1.0f);
  3591. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3592. for (auto v : l)
  3593. peaks[k] = std::max (peaks[k], std::abs (v));
  3594. }
  3595. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3596. }
  3597. TEST (zoned_sampler_loops_while_held)
  3598. {
  3599. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3600. // on release, it leaves the loop and ends
  3601. auto data = std::make_shared<lynx::SampleData>();
  3602. data->sampleRate = 48000.0;
  3603. data->left.resize (4800);
  3604. for (std::size_t i = 0; i < data->left.size(); ++i)
  3605. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3606. lynx::SampleZone zone { "loop.wav", 60 };
  3607. zone.loopStart = 1000;
  3608. zone.loopEnd = 3000;
  3609. zone.loopMode = 2;
  3610. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3611. inst->prepare (48000.0);
  3612. lynx::ChannelDef c;
  3613. c.sampler.release = 0.01f;
  3614. inst->configure (c, 120.0);
  3615. inst->noteOn (60, 1.0f);
  3616. std::vector<float> l (48000), r (48000);
  3617. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3618. float held = 0.0f;
  3619. for (std::size_t i = 20000; i < 24000; ++i)
  3620. held = std::max (held, std::abs (l[i]));
  3621. CHECK (held > 0.3f);
  3622. inst->noteOff (60);
  3623. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3624. float after = 0.0f;
  3625. for (std::size_t i = 40000; i < 48000; ++i)
  3626. after = std::max (after, std::abs (l[i]));
  3627. CHECK (after == 0.0f);
  3628. }
  3629. TEST (global_lfo_follows_song_position)
  3630. {
  3631. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3632. // same note with a per-note LFO starts at phase 0
  3633. auto first = [] (bool global, double songTicks) {
  3634. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3635. inst->prepare (48000.0);
  3636. lynx::ChannelDef c;
  3637. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3638. c.synth.cutoff = 20000.0f;
  3639. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3640. inst->configure (c, 120.0);
  3641. inst->setSongPosition (songTicks);
  3642. inst->noteOn (69, 1.0f);
  3643. std::vector<float> l (4800), r (4800);
  3644. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3645. float peak = 0.0f;
  3646. for (std::size_t i = 2400; i < 4800; ++i)
  3647. peak = std::max (peak, std::abs (l[i]));
  3648. return peak;
  3649. };
  3650. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3651. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3652. const auto perNote = first (false, 0.6 * lynx::ppq);
  3653. CHECK (globalLate < 0.05f);
  3654. CHECK (perNote > 0.4f);
  3655. }
  3656. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3657. {
  3658. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3659. auto data = std::make_shared<lynx::SampleData>();
  3660. data->sampleRate = 48000.0;
  3661. data->left.resize (48000);
  3662. std::uint32_t seed = 7;
  3663. for (auto& v : data->left)
  3664. {
  3665. seed = seed * 1664525u + 1013904223u;
  3666. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3667. }
  3668. data->right = data->left;
  3669. auto energy = [&] (float cutoff) {
  3670. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3671. inst->prepare (48000.0);
  3672. lynx::ChannelDef c;
  3673. c.sampler.cutoff = cutoff;
  3674. inst->configure (c, 120.0);
  3675. inst->noteOn (60, 1.0f);
  3676. std::vector<float> l (24000), r (24000);
  3677. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3678. double e = 0.0;
  3679. for (auto v : l)
  3680. e += (double) v * v;
  3681. return e;
  3682. };
  3683. const auto open = energy (20000.0f), dark = energy (300.0f);
  3684. {
  3685. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3686. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3687. inst->prepare (48000.0);
  3688. lynx::ChannelDef hp;
  3689. hp.sampler.cutoff = 10000.0f;
  3690. hp.sampler.filterType = 2;
  3691. inst->configure (hp, 120.0);
  3692. inst->noteOn (60, 1.0f);
  3693. std::vector<float> l (24000), r (24000);
  3694. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3695. double e = 0.0, lowE = 0.0;
  3696. float lp = 0.0f;
  3697. for (auto v : l)
  3698. {
  3699. e += (double) v * v;
  3700. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3701. lowE += (double) lp * lp;
  3702. }
  3703. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3704. CHECK (lowE < e * 0.01);
  3705. }
  3706. CHECK (open > 100.0);
  3707. CHECK (dark < open * 0.05);
  3708. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3709. // and ends in half the time (1 s of sample -> 0.5 s)
  3710. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3711. inst->prepare (48000.0);
  3712. lynx::ChannelDef c;
  3713. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3714. inst->configure (c, 120.0);
  3715. inst->noteOn (60, 1.0f);
  3716. std::vector<float> l (48000), r (48000);
  3717. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3718. float tail = 0.0f, body = 0.0f;
  3719. for (std::size_t i = 20000; i < 23000; ++i)
  3720. body = std::max (body, std::abs (l[i]));
  3721. for (std::size_t i = 25000; i < 48000; ++i)
  3722. tail = std::max (tail, std::abs (l[i]));
  3723. CHECK (body > 0.1f);
  3724. CHECK (tail == 0.0f);
  3725. }
  3726. TEST (synth_oscillator_pan_and_semitones)
  3727. {
  3728. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3729. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3730. synth->prepare (48000.0);
  3731. lynx::ChannelDef c;
  3732. c.synth = s;
  3733. synth->configure (c, 120.0);
  3734. synth->noteOn (69, 1.0f);
  3735. l.assign (24000, 0.0f);
  3736. r.assign (24000, 0.0f);
  3737. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3738. };
  3739. lynx::SynthSettings s;
  3740. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3741. s.cutoff = 20000.0f;
  3742. std::vector<float> l, r;
  3743. render (s, l, r);
  3744. float peakL = 0.0f, peakR = 0.0f;
  3745. int crossings = 0;
  3746. for (std::size_t i = 4800; i < 24000; ++i)
  3747. {
  3748. peakL = std::max (peakL, std::abs (l[i]));
  3749. peakR = std::max (peakR, std::abs (r[i]));
  3750. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3751. }
  3752. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3753. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3754. CHECK (std::abs (crossings - 527) < 6);
  3755. }
  3756. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3757. {
  3758. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3759. std::vector<std::uint8_t> blob;
  3760. 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))); };
  3761. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3762. u32 (b, id);
  3763. u32 (b, (std::uint32_t) payload.size());
  3764. u32 (b, 0);
  3765. b.insert (b.end(), payload.begin(), payload.end());
  3766. };
  3767. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3768. std::vector<std::uint8_t> vst3State;
  3769. u32 (vst3State, 1);
  3770. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3771. record (vst3State, 3, bytes ("COMPONENTE"));
  3772. record (vst3State, 2, bytes ("CONTROLADOR"));
  3773. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3774. std::vector<std::uint8_t> slots;
  3775. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3776. u32 (slots, v);
  3777. record (vst3State, 4, slots);
  3778. u32 (blob, 12);
  3779. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3780. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3781. std::vector<std::uint8_t> cid;
  3782. for (int i = 0; i < 16; ++i)
  3783. cid.push_back ((std::uint8_t) (0xA0 + i));
  3784. record (blob, 0x34, cid);
  3785. record (blob, 0x36, bytes ("Surge XT"));
  3786. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3787. record (blob, 0x35, vst3State);
  3788. lynx::ForeignPlugin p;
  3789. CHECK (lynx::parseFlWrapper (blob, p));
  3790. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3791. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3792. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3793. std::vector<std::uint8_t> component, controller;
  3794. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3795. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3796. const auto saved = lynx::flVst3ParamSlots (p.state);
  3797. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3798. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3799. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3800. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3801. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3802. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3803. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3804. {
  3805. lynx::FlVst2State st;
  3806. st.flags = 8;
  3807. st.currentProgram = 3;
  3808. st.chunk = bytes ("BANKCHUNK");
  3809. const auto withChunk = lynx::writeFlVst2State (st);
  3810. lynx::FlVst2State back;
  3811. CHECK (lynx::parseFlVst2State (withChunk, back));
  3812. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3813. CHECK (! back.chunkIsProgram());
  3814. lynx::FlVst2State raw;
  3815. raw.flags = 1;
  3816. raw.params = { 0.25f, 0.5f, 1.0f };
  3817. raw.programNames = { "Init" };
  3818. const auto withParams = lynx::writeFlVst2State (raw);
  3819. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3820. CHECK (lynx::parseFlVst2State (withParams, back));
  3821. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3822. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3823. std::vector<std::uint8_t> v5;
  3824. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3825. u32 (v5, v);
  3826. for (auto c : bytes ("ABCD"))
  3827. v5.push_back (c);
  3828. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3829. std::vector<std::uint8_t> v6;
  3830. u32 (v6, 0xFFFFFFFAu);
  3831. v6.push_back (0);
  3832. for (auto v : { 2u, 0u })
  3833. u32 (v6, v);
  3834. v6.push_back ('x');
  3835. v6.push_back ('y');
  3836. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3837. // nothing to restore (123456), unknown old layouts, truncated chunks
  3838. std::vector<std::uint8_t> marker;
  3839. u32 (marker, 123456u);
  3840. CHECK (! lynx::parseFlVst2State (marker, back));
  3841. auto cut = withChunk;
  3842. cut.pop_back();
  3843. CHECK (! lynx::parseFlVst2State (cut, back));
  3844. CHECK (! lynx::parseFlVst2State ({}, back));
  3845. }
  3846. // version out of range or no type: not a usable wrapper
  3847. blob[0] = 13;
  3848. CHECK (! lynx::parseFlWrapper (blob, p));
  3849. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3850. }
  3851. TEST (schedule_index_matches_full_scan)
  3852. {
  3853. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3854. // exactly the same events as the full scan, including in the block that wraps around
  3855. lynx::Song s = lynx::Song::makeDefault();
  3856. s.swing = 0.6f;
  3857. std::uint32_t seed = 12345;
  3858. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3859. s.patterns.resize (3);
  3860. for (auto& p : s.patterns)
  3861. {
  3862. p.notes.clear();
  3863. for (int i = 0; i < 60; ++i)
  3864. {
  3865. lynx::Note n;
  3866. n.start = rnd (16 * lynx::ppq);
  3867. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3868. n.channel = rnd (4);
  3869. n.key = 40 + rnd (40);
  3870. n.slide = rnd (20) == 0;
  3871. p.notes.push_back (n);
  3872. }
  3873. p.sortNotes();
  3874. }
  3875. s.playlist.clear();
  3876. for (int i = 0; i < 12; ++i)
  3877. {
  3878. lynx::Clip c;
  3879. c.pattern = rnd (3);
  3880. c.track = rnd (4);
  3881. c.start = rnd (40) * lynx::ppq;
  3882. c.length = (rnd (12) + 1) * lynx::ppq;
  3883. c.offset = rnd (4) * lynx::ppq;
  3884. s.playlist.push_back (c);
  3885. }
  3886. s.mode = lynx::PlayMode::song;
  3887. const auto index = lynx::makeScheduleIndex (s);
  3888. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3889. const auto loop = (double) s.songLength();
  3890. int mismatches = 0, events = 0;
  3891. for (int block = 0; block < 400; ++block)
  3892. {
  3893. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3894. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3895. lynx::EventBuffer a, b;
  3896. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3897. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3898. events += a.size();
  3899. if (a.size() != b.size())
  3900. {
  3901. ++mismatches;
  3902. continue;
  3903. }
  3904. for (int i = 0; i < a.size(); ++i)
  3905. {
  3906. const auto& x = *(a.begin() + i);
  3907. const auto& y = *(b.begin() + i);
  3908. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3909. }
  3910. }
  3911. CHECK (events > 500);
  3912. CHECK (mismatches == 0);
  3913. }
  3914. TEST (mixer_track_eq_defaults_and_boost)
  3915. {
  3916. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3917. lynx::TrackEq eq;
  3918. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3919. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3920. CHECK (! eq.active());
  3921. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3922. lynx::Song song = lynx::Song::makeDefault();
  3923. song.mixer[1].eq.gainDb[1] = 12.0f;
  3924. for (auto& c : song.channels)
  3925. c.mixerTrack = 1;
  3926. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3927. song.channels[4].synth = {};
  3928. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3929. song.channels[4].synth.cutoff = 20000.0f;
  3930. auto render = [] (const lynx::Song& s) {
  3931. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3932. lynx::RackRenderer renderer;
  3933. renderer.prepare (48000.0);
  3934. lynx::LiveEventQueue live;
  3935. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3936. on.channel = 4;
  3937. on.key = 91;
  3938. on.velocity = 1.0f;
  3939. live.push (on);
  3940. std::vector<float> l (24000), r (24000);
  3941. for (int pos = 0; pos < 24000; pos += 480)
  3942. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3943. float peak = 0.0f;
  3944. for (std::size_t i = 12000; i < 24000; ++i)
  3945. peak = std::max (peak, std::abs (l[i]));
  3946. return peak;
  3947. };
  3948. const auto boosted = render (song);
  3949. song.mixer[1].eq.gainDb[1] = 0.0f;
  3950. const auto flat = render (song);
  3951. CHECK (flat > 0.01f);
  3952. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3953. }
  3954. TEST (bars_follow_time_signature_markers)
  3955. {
  3956. lynx::Song song;
  3957. song.beatsPerBar = 4;
  3958. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3959. auto bars = song.barsBetween (0, bar4 * 3);
  3960. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3961. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3962. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3963. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3964. const auto seven = (std::int64_t) lynx::ppq * 7;
  3965. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3966. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3967. CHECK (bars.size() >= 5);
  3968. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3969. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3970. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3971. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3972. // from far away: starts at the right bar without losing the numbering
  3973. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3974. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3975. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3976. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3977. }
  3978. TEST (split_drum_channel_moves_notes_to_pads)
  3979. {
  3980. lynx::Song song = lynx::Song::makeDefault();
  3981. const auto channels = (int) song.channels.size();
  3982. song.patterns[0].notes.clear();
  3983. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3984. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3985. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3986. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3987. lynx::Automation a;
  3988. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3989. song.automations.push_back (a);
  3990. std::vector<lynx::DrumPad> pads (2);
  3991. pads[0].name = "Kick";
  3992. pads[0].key = 36;
  3993. pads[0].layers = { { "kick.wav" } };
  3994. pads[1].name = "Snare";
  3995. pads[1].key = 38;
  3996. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3997. pads[1].cutGroup = 2;
  3998. pads[1].cutBy = 2;
  3999. lynx::splitDrumChannel (song, 1, pads);
  4000. CHECK ((int) song.channels.size() == channels + 1);
  4001. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  4002. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  4003. const auto& notes = song.patterns[0].notes;
  4004. CHECK (notes.size() == 3);
  4005. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  4006. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  4007. CHECK (notes[2].channel == 4); // the old channel 3
  4008. CHECK (song.automations[0].target.index == 4);
  4009. }
  4010. TEST (flp_import_brings_playlist)
  4011. {
  4012. flp::Project p;
  4013. p.header.ppq = 96;
  4014. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  4015. flp::Pattern empty;
  4016. empty.index = 1;
  4017. flp::Pattern full;
  4018. full.index = 2;
  4019. flp::Note n;
  4020. n.channel = 0;
  4021. full.notes.push_back (n);
  4022. p.patterns = { empty, full };
  4023. flp::Arrangement a;
  4024. flp::PlaylistItem item;
  4025. item.kind = flp::PlaylistItem::Kind::pattern;
  4026. item.index = 2;
  4027. item.position = 384;
  4028. item.length = 192;
  4029. item.track = 3;
  4030. item.patternStartTick = 96;
  4031. a.items.push_back (item);
  4032. item.index = 1; // empty pattern: discarded
  4033. a.items.push_back (item);
  4034. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  4035. a.items.push_back (item);
  4036. p.arrangements.push_back (a);
  4037. const auto s = lynx::importFlp (p);
  4038. CHECK (s.mode == lynx::PlayMode::song);
  4039. CHECK (s.playlist.size() == 1);
  4040. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  4041. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  4042. }
  4043. TEST (rack_releases_notes_on_position_jump)
  4044. {
  4045. lynx::Song s;
  4046. lynx::ChannelDef c;
  4047. c.instrument = lynx::InstrumentKind::synth;
  4048. s.channels.push_back (c);
  4049. auto& p = s.patterns.emplace_back();
  4050. lynx::Note n;
  4051. n.start = 0;
  4052. n.length = 4 * lynx::ppq; // long note
  4053. p.notes.push_back (n);
  4054. s.bpm = 120.0;
  4055. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  4056. lynx::RackRenderer r;
  4057. r.prepare (48000.0);
  4058. std::vector<float> l (512), rr (512);
  4059. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4060. double tick = 0.0;
  4061. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  4062. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4063. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  4064. for (int i = 0; i < 40; ++i)
  4065. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  4066. float tail = 0.0f;
  4067. for (auto v : l)
  4068. tail = std::max (tail, std::abs (v));
  4069. CHECK (tail < 0.001f);
  4070. }
  4071. namespace
  4072. {
  4073. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  4074. float juce_db (float gain)
  4075. {
  4076. return 20.0f * std::log10 (gain);
  4077. }
  4078. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  4079. {
  4080. lynx::Song s;
  4081. s.mixer = lynx::Song::defaultMixer (4);
  4082. for (int i = 0; i < 2; ++i)
  4083. {
  4084. lynx::ChannelDef c;
  4085. c.instrument = lynx::InstrumentKind::sampler;
  4086. c.samplePath = "dc";
  4087. c.volume = 1.0f;
  4088. c.mixerTrack = i + 1;
  4089. s.channels.push_back (c);
  4090. }
  4091. s.patterns.emplace_back();
  4092. tweak (s);
  4093. auto provider = [] (const std::string&) {
  4094. auto d = std::make_shared<lynx::SampleData>();
  4095. d->left.assign (48000, 0.5f);
  4096. return std::shared_ptr<const lynx::SampleData> (d);
  4097. };
  4098. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4099. lynx::RackRenderer r;
  4100. r.prepare (48000.0, 256);
  4101. lynx::LiveEventQueue live;
  4102. for (int ch = 0; ch < 2; ++ch)
  4103. {
  4104. lynx::NoteEvent on;
  4105. on.channel = ch;
  4106. on.velocity = 1.0f;
  4107. live.push (on);
  4108. }
  4109. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  4110. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  4111. return std::max (std::abs (l[999]), std::abs (rr[999]));
  4112. }
  4113. } // namespace
  4114. TEST (mixer_routes_mutes_solos_and_meters)
  4115. {
  4116. // two 0.5 channels summed on the Master at 0 dB
  4117. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  4118. // insert 1 muted: only channel 2 remains
  4119. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  4120. // solo on insert 2: same
  4121. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  4122. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  4123. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  4124. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  4125. CHECK (lynx::faderGain (0.0f) == 0.0f);
  4126. // Master fader at max
  4127. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  4128. // both channels on the same insert, insert fader at 0.4
  4129. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  4130. s.channels[1].mixerTrack = 1;
  4131. s.mixer[1].volume = 0.4f;
  4132. }) - lynx::faderGain (0.4f)) < 1e-4f);
  4133. // a route outside the mixer falls to the last existing insert, without crashing
  4134. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  4135. // no mixer: everything straight to the output at 0 dB
  4136. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  4137. lynx::MeterBank meters;
  4138. renderMixerCase ([] (lynx::Song&) {}, &meters);
  4139. // (the 24-point sinc's ringing at the sample start: its x87 kernel table rounds differently on ARM64)
  4140. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < armTolerance (1e-4f, 2e-4f));
  4141. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < armTolerance (1e-4f, 4e-4f));
  4142. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  4143. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  4144. }
  4145. TEST (next_free_insert_and_flp_mixer_import)
  4146. {
  4147. auto s = lynx::Song::makeDefault();
  4148. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  4149. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  4150. flp::Project p;
  4151. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  4152. p.mixer = { { 0, "" }, { 30, "Drums" } };
  4153. const auto song = lynx::importFlp (p);
  4154. CHECK (song.channels[0].mixerTrack == 30);
  4155. CHECK (song.mixer.size() == 31);
  4156. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  4157. }
  4158. namespace
  4159. {
  4160. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  4161. int frames = 48000)
  4162. {
  4163. auto fx = lynx::makeEffect (kind);
  4164. fx->prepare (48000.0);
  4165. auto slot = lynx::makeEffectSlot (kind);
  4166. tweak (slot);
  4167. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4168. for (int i = 0; i < frames; ++i)
  4169. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  4170. for (int i = 0; i < frames; i += 480)
  4171. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  4172. float peak = 0.0f;
  4173. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  4174. {
  4175. CHECK (std::isfinite (l[(std::size_t) i]));
  4176. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4177. }
  4178. return peak;
  4179. }
  4180. } // namespace
  4181. TEST (parametric_eq_bands_and_response)
  4182. {
  4183. // default: transparent
  4184. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4185. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  4186. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  4187. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  4188. auto boost = [] (lynx::EffectSlot& s) {
  4189. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  4190. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  4191. };
  4192. boost (slot);
  4193. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  4194. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  4195. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  4196. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  4197. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  4198. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4199. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  4200. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  4201. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  4202. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  4203. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  4204. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  4205. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  4206. // round-trip conversions
  4207. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  4208. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  4209. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  4210. }
  4211. TEST (fl_parametric_eq2_state_maps_to_bands)
  4212. {
  4213. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  4214. std::vector<std::uint8_t> blob (354, 0);
  4215. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4216. put (0, 8);
  4217. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  4218. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  4219. for (int b = 0; b < 7; ++b)
  4220. param (21 + b, 0); // all off
  4221. param (21 + 0, 3); // high pass (= low cut)
  4222. param (7 + 0, hzRaw (100.0));
  4223. param (14 + 0, 40124); // default width -> Q ~0.68
  4224. param (28 + 0, -3); // Steep 8 -> 4 biquads
  4225. param (21 + 3, 6); // peaking
  4226. param (7 + 3, hzRaw (1000.0));
  4227. param (3, 600); // +6 dB
  4228. param (14 + 3, 25412);
  4229. param (35, -300); // main level −3 dB
  4230. put (0x13E, 0x7F);
  4231. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4232. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  4233. const auto& p = slot->params;
  4234. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  4235. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  4236. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  4237. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  4238. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  4239. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  4240. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  4241. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  4242. put (0x13E, 0x7F & ~(1 << 3));
  4243. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4244. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  4245. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  4246. }
  4247. TEST (reverb_low_cut_and_predelay)
  4248. {
  4249. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  4250. // 100 Hz sine barely enters the reverb
  4251. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  4252. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  4253. fx->prepare (48000.0);
  4254. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4255. slot.params[4] = 0.0f; // wet only
  4256. slot.params[3] = 1.0f;
  4257. tweak (slot);
  4258. std::vector<float> l (48000), r (48000);
  4259. for (int i = 0; i < 48000; ++i)
  4260. 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);
  4261. for (int i = 0; i < 48000; i += 480)
  4262. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  4263. return l;
  4264. };
  4265. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  4266. float early = 0.0f, later = 0.0f;
  4267. for (std::size_t i = 0; i < 4800; ++i)
  4268. early = std::max (early, std::abs (delayed[i]));
  4269. for (std::size_t i = 4800; i < 24000; ++i)
  4270. later = std::max (later, std::abs (delayed[i]));
  4271. CHECK (early == 0.0f && later > 0.0f);
  4272. 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; };
  4273. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  4274. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  4275. CHECK (cut < open * 0.01);
  4276. }
  4277. TEST (utility_effect_and_fl_stereo_tools)
  4278. {
  4279. // transparent by default; balance fully right silences the left
  4280. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  4281. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  4282. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  4283. std::vector<std::uint8_t> blob (36, 0);
  4284. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4285. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  4286. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  4287. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  4288. auto shaperFx = lynx::makeEffect (*slot);
  4289. shaperFx->prepare (44100.0);
  4290. float zl = 0.0f, zr = 0.0f;
  4291. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  4292. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  4293. float l = 1.0f, r = 0.0f;
  4294. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  4295. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  4296. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  4297. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  4298. // Balance: volume 256 = 0 dB, pan −128 = fully left
  4299. std::vector<std::uint8_t> bal (8, 0);
  4300. std::int32_t pan = -128, vol = 256;
  4301. std::memcpy (bal.data(), &pan, 4);
  4302. std::memcpy (bal.data() + 4, &vol, 4);
  4303. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  4304. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  4305. if (b)
  4306. {
  4307. auto balance = lynx::makeEffect (*b);
  4308. balance->prepare (44100.0);
  4309. l = 0.5f; r = 0.5f;
  4310. balance->process (&l, &r, 1, b->params, 120.0);
  4311. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  4312. }
  4313. }
  4314. TEST (effects_eq_compressor_behave)
  4315. {
  4316. // EQ at default is transparent
  4317. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  4318. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  4319. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  4320. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  4321. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  4322. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  4323. s.params[0] = 40.0f / 60.0f;
  4324. s.params[1] = 1.0f;
  4325. }, 200.0, 1.0f);
  4326. CHECK (squashed < 0.2f && squashed > 0.05f);
  4327. // a signal below the threshold passes intact
  4328. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  4329. }
  4330. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  4331. {
  4332. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  4333. delay->prepare (48000.0);
  4334. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4335. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  4336. slot.params[1] = 0.0f; // no feedback: a single echo
  4337. slot.params[2] = 1.0f;
  4338. slot.params[4] = 0.0f; // no damping
  4339. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  4340. l[0] = r[0] = 1.0f;
  4341. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4342. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  4343. CHECK (std::abs (l[12000]) < 1e-6f);
  4344. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  4345. reverb->prepare (48000.0);
  4346. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4347. rs.params[3] = 1.0f;
  4348. std::fill (l.begin(), l.end(), 0.0f);
  4349. std::fill (r.begin(), r.end(), 0.0f);
  4350. l[0] = r[0] = 1.0f;
  4351. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  4352. float tail = 0.0f;
  4353. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  4354. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  4355. CHECK (tail > 1e-4f && tail < 1.0f);
  4356. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  4357. }
  4358. TEST (rack_runs_insert_effects_and_reuses_them)
  4359. {
  4360. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  4361. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  4362. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4363. eq.params[4] = 0.0f; // output -18 dB
  4364. s.mixer[1].effects.push_back (eq);
  4365. });
  4366. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  4367. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  4368. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4369. eq.params[4] = 0.0f;
  4370. eq.enabled = false;
  4371. s.mixer[1].effects.push_back (eq);
  4372. });
  4373. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  4374. auto s = lynx::Song::makeDefault();
  4375. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4376. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  4377. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  4378. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  4379. CHECK (b->effects[2][0] == a->effects[2][0]);
  4380. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4381. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  4382. CHECK (c->effects[2][0] != b->effects[2][0]);
  4383. }
  4384. TEST (audio_clips_trigger_cut_and_resume)
  4385. {
  4386. lynx::Song s;
  4387. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  4388. lynx::ChannelDef c;
  4389. c.instrument = lynx::InstrumentKind::audioClip;
  4390. c.samplePath = "ramp";
  4391. c.volume = 1.0f;
  4392. s.channels.push_back (c);
  4393. s.patterns.emplace_back();
  4394. lynx::Clip clip;
  4395. clip.kind = lynx::ClipKind::audio;
  4396. clip.channel = 0;
  4397. clip.start = lynx::ppq; // starts at beat 2
  4398. clip.length = 2 * lynx::ppq; // lasts 2 beats
  4399. clip.offset = lynx::ppq; // skips the first beat of the audio
  4400. s.playlist.push_back (clip);
  4401. // an empty pattern clip over the whole bar: the song ends at beat 4 (FL loops at the last clip's exact end)
  4402. s.playlist.push_back (lynx::Clip { 0, 1, 0, 4 * lynx::ppq, 0 });
  4403. s.mode = lynx::PlayMode::song;
  4404. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4405. lynx::EventBuffer ev;
  4406. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4407. CHECK (ev.size() == 2);
  4408. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4409. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4410. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4411. ev.clear();
  4412. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4413. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4414. ev.clear();
  4415. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4416. CHECK (ev.size() == 0);
  4417. // sample whose value = position in seconds: lets us check where playback started
  4418. auto provider = [] (const std::string&) {
  4419. auto d = std::make_shared<lynx::SampleData>();
  4420. d->sampleRate = 48000.0;
  4421. for (int i = 0; i < 48000 * 3; ++i)
  4422. d->left.push_back ((float) i / 48000.0f);
  4423. return std::shared_ptr<const lynx::SampleData> (d);
  4424. };
  4425. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4426. lynx::RackRenderer r;
  4427. r.prepare (48000.0, 512);
  4428. std::vector<float> l (256), rr (256);
  4429. // first block already playing mid-clip
  4430. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4431. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4432. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4433. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4434. double tick = 3.0 * lynx::ppq - 10 * tps;
  4435. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4436. for (int i = 0; i < 4; ++i)
  4437. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4438. CHECK (std::abs (l[255]) < 1e-6f);
  4439. }
  4440. TEST (remove_channel_fixes_notes_and_audio_clips)
  4441. {
  4442. lynx::Song s;
  4443. s.channels.resize (3);
  4444. s.patterns.resize (1);
  4445. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4446. lynx::Clip a;
  4447. a.kind = lynx::ClipKind::audio;
  4448. a.channel = 1;
  4449. a.length = 100;
  4450. lynx::Clip b = a;
  4451. b.channel = 2;
  4452. s.playlist = { a, b };
  4453. s.removeChannel (1);
  4454. CHECK (s.channels.size() == 2);
  4455. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4456. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4457. }
  4458. TEST (flp_import_audio_clips_with_offsets)
  4459. {
  4460. flp::Project p;
  4461. p.header.ppq = 96;
  4462. p.tempo = 120.0;
  4463. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4464. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4465. flp::Arrangement arr;
  4466. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4467. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4468. beats.index = 0;
  4469. beats.position = 192;
  4470. beats.length = 384; // 4 beats
  4471. beats.startOffset = 2.0f;
  4472. beats.endOffset = 6.0f;
  4473. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4474. ms.index = 1;
  4475. ms.startOffset = 500.0f;
  4476. ms.endOffset = 2500.0f;
  4477. arr.items = { beats, ms };
  4478. p.arrangements.push_back (arr);
  4479. lynx::FlpImportOptions options;
  4480. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4481. options.fileExists = [] (const std::string&) { return true; };
  4482. const auto s = lynx::importFlp (p, options);
  4483. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4484. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4485. CHECK (s.playlist.size() == 2);
  4486. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4487. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4488. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4489. CHECK (s.mode == lynx::PlayMode::song);
  4490. }
  4491. TEST (plugin_factories_create_reuse_and_fallback)
  4492. {
  4493. lynx::Song s;
  4494. s.mixer = lynx::Song::defaultMixer (2);
  4495. lynx::ChannelDef c;
  4496. c.instrument = lynx::InstrumentKind::plugin;
  4497. c.plugin.id = "VST3-Synth-123";
  4498. c.mixerTrack = 1;
  4499. s.channels.push_back (c);
  4500. lynx::ChannelDef missing = c;
  4501. missing.plugin.id = "VST3-Ausente-0";
  4502. s.channels.push_back (missing);
  4503. lynx::EffectSlot fx;
  4504. fx.kind = lynx::EffectKind::plugin;
  4505. fx.plugin.id = "VST3-Reverb-9";
  4506. s.mixer[1].effects.push_back (fx);
  4507. s.patterns.emplace_back();
  4508. int instrumentsMade = 0, effectsMade = 0;
  4509. lynx::RackState::Factories f;
  4510. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4511. if (ref.id != "VST3-Synth-123")
  4512. return nullptr;
  4513. ++instrumentsMade;
  4514. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4515. synth->prepare (48000.0);
  4516. return synth;
  4517. };
  4518. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4519. ++effectsMade;
  4520. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4521. eq->prepare (48000.0);
  4522. return eq;
  4523. };
  4524. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4525. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4526. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4527. // Default startup keeps the established reuse contract: saving/capturing
  4528. // different serialized bytes by itself must not recreate hosted instances.
  4529. auto storedOnly = s;
  4530. storedOnly.channels[0].plugin.state = "captured instrument without opt-in";
  4531. storedOnly.mixer[1].effects[0].plugin.state = "captured effect without opt-in";
  4532. auto ordinary = lynx::RackState::build (storedOnly, a.get(), 48000.0, f);
  4533. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4534. CHECK (ordinary->instruments[0] == a->instruments[0] && ordinary->effects[1][0] == a->effects[1][0]);
  4535. // Serialized state changes and undo preserve running instances. Remote control
  4536. // applies deliberate state edits in place through the engine, outside this factory.
  4537. s.channels[0].plugin.state = "abc";
  4538. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4539. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4540. s.mixer[1].effects[0].plugin.state = "new effect state";
  4541. auto changed = lynx::RackState::build (s, b.get(), 48000.0, f);
  4542. CHECK (instrumentsMade == 1 && effectsMade == 1 && changed->effects[1][0] == b->effects[1][0]);
  4543. s.channels[0].plugin.state.clear();
  4544. s.mixer[1].effects[0].plugin.state.clear();
  4545. auto undone = lynx::RackState::build (s, changed.get(), 48000.0, f);
  4546. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4547. CHECK (undone->instruments[0] == changed->instruments[0] && undone->effects[1][0] == changed->effects[1][0]);
  4548. auto capturedShape = *undone;
  4549. capturedShape.song.channels[0].plugin.state = s.channels[0].plugin.state = "captured instrument";
  4550. capturedShape.song.mixer[1].effects[0].plugin.state = s.mixer[1].effects[0].plugin.state = "captured effect";
  4551. auto captured = lynx::RackState::build (s, &capturedShape, 48000.0, f);
  4552. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4553. CHECK (captured->instruments[0] == undone->instruments[0] && captured->effects[1][0] == undone->effects[1][0]);
  4554. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4555. auto c2 = lynx::RackState::build (s, captured.get(), 48000.0, f);
  4556. CHECK (effectsMade == 2);
  4557. // rendering with a missing plugin and a plugin slot breaks nothing
  4558. lynx::RackRenderer r;
  4559. r.prepare (48000.0, 256);
  4560. lynx::LiveEventQueue live;
  4561. lynx::NoteEvent on;
  4562. on.channel = 0;
  4563. on.velocity = 1.0f;
  4564. live.push (on);
  4565. on.channel = 1;
  4566. live.push (on);
  4567. std::vector<float> l (256), rr (256);
  4568. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4569. float peak = 0.0f;
  4570. for (auto v : l)
  4571. {
  4572. CHECK (std::isfinite (v));
  4573. peak = std::max (peak, std::abs (v));
  4574. }
  4575. CHECK (peak > 0.0f);
  4576. }
  4577. TEST (remote_reset_replaces_only_requested_instances_and_default_reuse_stays_unchanged)
  4578. {
  4579. lynx::Song source;
  4580. source.mixer = lynx::Song::defaultMixer (2);
  4581. for (const auto* id : { "Synth A", "Synth B" })
  4582. {
  4583. lynx::ChannelDef channel;
  4584. channel.instrument = lynx::InstrumentKind::plugin;
  4585. channel.plugin.id = id;
  4586. channel.plugin.state = std::string ("saved ") + id;
  4587. source.channels.push_back (std::move (channel));
  4588. }
  4589. for (const auto* id : { "Effect A", "Effect B" })
  4590. {
  4591. lynx::EffectSlot effect;
  4592. effect.kind = lynx::EffectKind::plugin;
  4593. effect.plugin.id = id;
  4594. effect.plugin.state = std::string ("saved ") + id;
  4595. source.mixer[1].effects.push_back (std::move (effect));
  4596. }
  4597. int instrumentsMade = 0, effectsMade = 0;
  4598. std::string lastInstrumentState, lastEffectState;
  4599. lynx::RackState::Factories normal;
  4600. normal.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4601. ++instrumentsMade;
  4602. lastInstrumentState = ref.state;
  4603. return lynx::makeInstrument (lynx::InstrumentKind::synth);
  4604. };
  4605. normal.pluginEffects = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Effect> {
  4606. ++effectsMade;
  4607. lastEffectState = ref.state;
  4608. return lynx::makeEffect (lynx::EffectKind::eq);
  4609. };
  4610. const auto before = lynx::RackState::build (source, nullptr, 48000.0, normal);
  4611. CHECK (instrumentsMade == 2 && effectsMade == 2);
  4612. // Round 190az (24047e7a0): an ordinary build keeps a running instance for its own channel / slot (the instance
  4613. // tag) or for one saved with the same settings. A renewed tag with a cleared state is another device: it is
  4614. // created anew, with the default state, as a remote reset asks.
  4615. auto reset = source;
  4616. reset.channels[0].instance.renew();
  4617. reset.channels[0].plugin.state.clear();
  4618. reset.mixer[1].effects[0].instance.renew();
  4619. reset.mixer[1].effects[0].plugin.state.clear();
  4620. const auto ordinary = lynx::RackState::build (reset, before.get(), 48000.0, normal);
  4621. CHECK (instrumentsMade == 3 && effectsMade == 3);
  4622. CHECK (lastInstrumentState.empty() && lastEffectState.empty()); // factories received actual default-state requests
  4623. CHECK (ordinary->instruments[0] != before->instruments[0] && ordinary->effects[1][0] != before->effects[1][0]);
  4624. CHECK (ordinary->instruments[1] == before->instruments[1] && ordinary->effects[1][1] == before->effects[1][1]);
  4625. // With the same saved bytes the positional instance stays in an ordinary build; freshInstances replaces exactly
  4626. // the devices it names.
  4627. auto renewed = source;
  4628. renewed.channels[0].instance.renew();
  4629. renewed.mixer[1].effects[0].instance.renew();
  4630. const auto kept = lynx::RackState::build (renewed, before.get(), 48000.0, normal);
  4631. CHECK (instrumentsMade == 3 && effectsMade == 3);
  4632. CHECK (kept->instruments[0] == before->instruments[0] && kept->effects[1][0] == before->effects[1][0]);
  4633. auto requested = normal;
  4634. requested.freshInstances = { renewed.channels[0].instance.id, renewed.mixer[1].effects[0].instance.id };
  4635. const auto replaced = lynx::RackState::build (renewed, before.get(), 48000.0, requested);
  4636. CHECK (instrumentsMade == 4 && effectsMade == 4);
  4637. CHECK (lastInstrumentState == "saved Synth A" && lastEffectState == "saved Effect A");
  4638. CHECK (replaced->instruments[0] != before->instruments[0] && replaced->effects[1][0] != before->effects[1][0]);
  4639. CHECK (replaced->instruments[1] == before->instruments[1] && replaced->effects[1][1] == before->effects[1][1]);
  4640. // The policy belongs to that build only: later ordinary edits reuse the newly created instances.
  4641. const auto later = lynx::RackState::build (renewed, replaced.get(), 48000.0, normal);
  4642. CHECK (instrumentsMade == 4 && effectsMade == 4);
  4643. CHECK (later->instruments[0] == replaced->instruments[0] && later->effects[1][0] == replaced->effects[1][0]);
  4644. }
  4645. TEST (remote_history_normalized_restores_preserve_lineage_without_crossing_producer_edits)
  4646. {
  4647. struct Entry { std::string song; bool remote = false; std::string deviceHash; };
  4648. std::vector<Entry> undo { { "A", true, "B" }, { "B", true, "C" } }, redo;
  4649. std::string live = "C";
  4650. const auto restore = [&] (auto& from, auto& to, const std::string& normalized) {
  4651. CHECK (! from.empty() && from.back().deviceHash == live);
  4652. const auto savedTarget = from.back().song;
  4653. // A real engine inverse stores captured live bytes, even when its raw model blob was older.
  4654. to.push_back ({ live, true, {} });
  4655. from.pop_back();
  4656. live = normalized;
  4657. lynx::remote_history::acceptRestoredState (to.back(), from.empty() ? nullptr : &from.back(), savedTarget, live);
  4658. };
  4659. restore (undo, redo, "B-normalized");
  4660. CHECK (undo.back().deviceHash == live && redo.back().deviceHash == live);
  4661. CHECK (redo.back().song == "C");
  4662. restore (undo, redo, "A-normalized");
  4663. CHECK (undo.empty() && redo.back().song == "B-normalized");
  4664. restore (redo, undo, "B-normalized-again");
  4665. CHECK (redo.back().deviceHash == live); // the next redo adopts normalization through the same proven lineage
  4666. restore (redo, undo, "C-normalized");
  4667. CHECK (redo.empty() && undo.size() == 2);
  4668. // A producer changed B to D before the following remote edit. Undoing that edit may normalize D, but cannot
  4669. // promote the older B fence: doing so would silently authorize undo across the producer's opaque edit.
  4670. undo = { { "A", true, "B" }, { "D", true, "E" } };
  4671. redo.clear();
  4672. live = "E";
  4673. restore (undo, redo, "D-normalized");
  4674. CHECK (redo.back().deviceHash == live && undo.back().deviceHash == "B");
  4675. CHECK (undo.back().deviceHash != live); // the next remote-owned undo remains refused
  4676. // An ordinary producer entry never acquires a remote fence even if its bytes happen to match the target.
  4677. Entry ordinary { "A", false, "B" }, inverse { "C", true, {} };
  4678. lynx::remote_history::acceptRestoredState (inverse, &ordinary, std::string ("B"), std::string ("B-normalized"));
  4679. CHECK (ordinary.deviceHash == "B" && inverse.deviceHash == "B-normalized");
  4680. }
  4681. TEST (remote_capture_mirror_preserves_projected_shape_and_updates_only_matching_hosted_bytes)
  4682. {
  4683. lynx::Song captured;
  4684. lynx::ChannelDef hosted;
  4685. hosted.instrument = lynx::InstrumentKind::plugin;
  4686. hosted.plugin.id = "Synth";
  4687. hosted.plugin.state = "saved";
  4688. captured.channels = { hosted, hosted };
  4689. captured.channels[1].instance.renew();
  4690. lynx::ChannelDef patcher;
  4691. patcher.instrument = lynx::InstrumentKind::patcher;
  4692. lynx::PatchNode nested;
  4693. nested.generator = true;
  4694. nested.channel = hosted;
  4695. patcher.patcher.nodes.push_back (nested);
  4696. captured.channels.push_back (patcher);
  4697. captured.mixer = lynx::Song::defaultMixer (2);
  4698. lynx::EffectSlot effect;
  4699. effect.kind = lynx::EffectKind::plugin;
  4700. effect.plugin.id = "Effect";
  4701. effect.plugin.state = "saved";
  4702. captured.mixer[1].effects.push_back (effect);
  4703. auto published = captured;
  4704. published.channels[0].muted = true; // an insert stand-in changes routing/muting in the published projection
  4705. published.channels[1].instrument = lynx::InstrumentKind::sampler;
  4706. published.channels[1].samplePath = "stem.wav";
  4707. published.channels.push_back (lynx::ChannelDef()); // generated stem helper channels must survive
  4708. published.mixer[1].effects[0].enabled = false;
  4709. captured.channels[0].plugin.state = "captured synth";
  4710. captured.channels[1].plugin.state = "unavailable synth";
  4711. captured.channels[2].patcher.nodes[0].channel.plugin.state = "captured nested synth";
  4712. captured.mixer[1].effects[0].plugin.state = "captured effect";
  4713. lynx::remote_history::mirrorCapturedHostedStates (captured, published);
  4714. CHECK (published.channels.size() == 4 && published.channels[0].muted);
  4715. CHECK (published.channels[0].plugin.state == "captured synth");
  4716. CHECK (published.channels[1].instrument == lynx::InstrumentKind::sampler && published.channels[1].samplePath == "stem.wav");
  4717. CHECK (published.channels[1].plugin.state == "saved"); // a projected replacement is not the captured hosted device
  4718. CHECK (published.channels[2].patcher.nodes[0].channel.plugin.state == "captured nested synth");
  4719. CHECK (! published.mixer[1].effects[0].enabled && published.mixer[1].effects[0].plugin.state == "captured effect");
  4720. published.channels[0].instance.renew();
  4721. captured.channels[0].plugin.state = "different device";
  4722. lynx::remote_history::mirrorCapturedHostedStates (captured, published);
  4723. CHECK (published.channels[0].plugin.state == "captured synth");
  4724. }
  4725. TEST (export_song_adds_silent_tail_and_never_restarts)
  4726. {
  4727. auto s = lynx::Song::makeDefault();
  4728. s.playlist.clear();
  4729. std::int64_t length = 0;
  4730. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4731. CHECK (length == lynx::ppq * 4);
  4732. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4733. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4734. // no new notes after the song ends, only silence until the end of the tail
  4735. lynx::EventBuffer events;
  4736. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4737. int onsAfterEnd = 0, onsBefore = 0;
  4738. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4739. {
  4740. events.clear();
  4741. lynx::scheduleSong (pat, tick, 512, tps, events);
  4742. for (const auto& e : events)
  4743. if (e.type == lynx::NoteEvent::Type::on)
  4744. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4745. }
  4746. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4747. CHECK (onsAfterEnd == 0);
  4748. // with Playlist: the end is the last clip's, without rounding
  4749. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4750. auto song = lynx::makeExportSong (s, true, 0, length);
  4751. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4752. song = lynx::makeExportSong (s, false, 0, length);
  4753. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4754. }
  4755. TEST (note_edit_selection_operations)
  4756. {
  4757. namespace ne = lynx::noteedit;
  4758. lynx::Pattern p;
  4759. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4760. // sorting keeps the indices in step
  4761. p.notes[0].start = 1200;
  4762. std::vector<int> tracked { 0, 2 };
  4763. ne::sortTracking (p, tracked);
  4764. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4765. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4766. // group move respects the limits
  4767. std::int64_t dt = -5000;
  4768. int dk = 100;
  4769. ne::move (p, { 0, 1 }, dt, dk);
  4770. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4771. // duplicate: right after the selection, rounded to the grid
  4772. lynx::Pattern q;
  4773. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4774. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4775. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4776. // paste onto another channel with an offset
  4777. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4778. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4779. // quantize rounds to the nearest
  4780. q.notes[0].start = 130;
  4781. q.notes[1].start = 100;
  4782. ne::quantize (q, { 0, 1 }, 240);
  4783. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4784. // remove several (order and duplicates don't matter)
  4785. const auto before = q.notes.size();
  4786. ne::remove (q, { 1, 0, 1 });
  4787. CHECK (q.notes.size() == before - 2);
  4788. }
  4789. TEST (note_edit_quick_operations)
  4790. {
  4791. namespace ne = lynx::noteedit;
  4792. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4793. // split: only strictly inside; the tail is appended and does not slide again
  4794. lynx::Pattern p;
  4795. p.notes = { { 0, 4 * step, 0, 60 } };
  4796. p.notes[0].slide = true;
  4797. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4798. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4799. && ! p.notes[1].slide);
  4800. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4801. lynx::Pattern c;
  4802. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4803. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4804. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4805. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4806. && c.notes[3].length == step);
  4807. // join: touching and overlapping notes of the same key and channel merge; others stay
  4808. lynx::Pattern j;
  4809. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4810. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4811. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4812. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4813. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4814. lynx::Pattern l;
  4815. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4816. ne::legato (l, { 0, 1, 2, 3, 4 });
  4817. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4818. && l.notes[4].length == 10);
  4819. // shrink to grid and full quantise (start and end)
  4820. ne::shrinkToGrid (l, { 3 }, step / 2);
  4821. CHECK (l.notes[3].length == step / 2);
  4822. lynx::Pattern q;
  4823. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4824. ne::quantizeFull (q, { 0, 1 }, step);
  4825. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4826. // rotate inside the bar, wrapping
  4827. lynx::Pattern r;
  4828. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4829. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4830. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4831. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4832. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4833. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4834. ne::rotate (r, { 2 }, step, bar, -2);
  4835. CHECK (r.notes[2].start == 2 * bar - step);
  4836. }
  4837. TEST (note_edit_grid_and_time_text)
  4838. {
  4839. namespace ne = lynx::noteedit;
  4840. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4841. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4842. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4843. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4844. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4845. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4846. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4847. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4848. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4849. CHECK (ne::formatTime (0, lynx::Meter (4, 4)) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, lynx::Meter (4, 4)) == "2:6:7");
  4850. CHECK (ne::formatTime (step * 2, lynx::Meter (4, 4), true) == "0:2:0");
  4851. std::int64_t t = 0;
  4852. CHECK (ne::parseTime ("2:6:7", lynx::Meter (4, 4), t) && t == bar + 5 * step + 7);
  4853. CHECK (ne::parseTime ("3", lynx::Meter (4, 4), t) && t == 2 * bar);
  4854. CHECK (ne::parseTime ("0:2:0", lynx::Meter (4, 4), t, true) && t == 2 * step);
  4855. CHECK (! ne::parseTime ("0:1:0", lynx::Meter (4, 4), t) && ! ne::parseTime ("1:17:0", lynx::Meter (4, 4), t) && ! ne::parseTime ("1:1:240", lynx::Meter (4, 4), t)
  4856. && ! ne::parseTime ("a", lynx::Meter (4, 4), t) && ! ne::parseTime ("", lynx::Meter (4, 4), t));
  4857. CHECK (ne::parseTime ("1:12:0", lynx::Meter (3, 4), t) && ! ne::parseTime ("1:13:0", lynx::Meter (3, 4), t)); // 3/4: 12 steps per bar
  4858. }
  4859. TEST (playlist_clip_edges_and_time_selection)
  4860. {
  4861. namespace ne = lynx::noteedit;
  4862. const std::int64_t beat = lynx::ppq;
  4863. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4864. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4865. ne::trimStart (c, 5 * beat, beat);
  4866. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4867. ne::trimStart (c, 0, beat);
  4868. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4869. ne::trimStart (c, 100 * beat, beat);
  4870. CHECK (c.length == beat && c.end() == 8 * beat);
  4871. // slip moves the content, not the clip
  4872. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4873. ne::slip (s, beat / 2);
  4874. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4875. ne::slip (s, beat);
  4876. CHECK (s.offset == 0);
  4877. ne::slip (s, -3 * beat);
  4878. CHECK (s.offset == 3 * beat);
  4879. // insert / delete time across all tracks, markers included
  4880. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4881. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4882. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4883. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4884. && markers[1].tick == beat / 2);
  4885. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4886. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4887. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4888. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4889. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4890. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4891. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4892. CHECK (d.size() == 5);
  4893. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4894. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4895. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4896. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4897. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4898. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4899. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4900. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4901. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4902. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4903. }
  4904. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4905. {
  4906. auto song = lynx::Song::makeDefault();
  4907. const auto loop = song.patterns[0].effectiveLength (lynx::Meter (4, 4)); // 1 bar = 3840
  4908. lynx::NoteRecorder r;
  4909. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4910. CHECK (r.takeFinished().empty());
  4911. r.begin (loop);
  4912. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4913. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4914. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4915. r.end (song, 5240.0);
  4916. const auto notes = r.takeFinished();
  4917. CHECK (notes.size() == 2);
  4918. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4919. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4920. // song mode: position inside the current pattern's clip (with offset)
  4921. song.mode = lynx::PlayMode::song;
  4922. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4923. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4924. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4925. }
  4926. TEST (automation_curve_and_playback)
  4927. {
  4928. lynx::Automation a;
  4929. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4930. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4931. CHECK (a.length() == 1920);
  4932. a.points[0].tension = 1.0f; // curve: starts slow
  4933. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4934. // automated Master: silence in the first bar, sound in the second, and the last value
  4935. // keeps applying after the automation clip ends
  4936. auto song = lynx::Song::makeDefault();
  4937. song.bpm = 120.0;
  4938. song.mode = lynx::PlayMode::song;
  4939. const auto bar = (std::int64_t) lynx::ppq * 4;
  4940. for (int b = 0; b < 3; ++b)
  4941. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4942. lynx::Automation master;
  4943. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4944. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4945. song.automations.push_back (master);
  4946. lynx::Clip ac;
  4947. ac.kind = lynx::ClipKind::automation;
  4948. ac.automation = 0;
  4949. ac.track = 1;
  4950. ac.length = bar + bar / 2;
  4951. song.playlist.push_back (ac);
  4952. const double sr = 48000.0;
  4953. auto state = lynx::RackState::build (song, nullptr, sr);
  4954. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4955. lynx::RackRenderer r;
  4956. r.prepare (sr, 512);
  4957. std::vector<float> l (512), rr (512);
  4958. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4959. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4960. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4961. {
  4962. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4963. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4964. for (auto v : l)
  4965. peak = std::max (peak, std::abs (v));
  4966. }
  4967. CHECK (peakBar1 < 1.0e-6f);
  4968. CHECK (peakBar2 > 0.05f);
  4969. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4970. // target removed: automation disappears from the tables
  4971. song.removeChannel (0);
  4972. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4973. song.removeChannel (0);
  4974. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4975. song.removeAutomation (0);
  4976. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4977. }
  4978. TEST (mixer_sends_between_inserts)
  4979. {
  4980. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4981. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4982. // send at 50% and muted destination
  4983. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4984. 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);
  4985. // going to the Master and to 3: the signal doubles
  4986. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4987. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4988. 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);
  4989. // solo on 1 lets the path to the Master (3) sound
  4990. 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);
  4991. // order and cycles
  4992. auto song = lynx::Song::makeDefault();
  4993. song.setRoute (4, 2, true);
  4994. song.setRoute (2, 1, true);
  4995. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4996. song.setRoute (1, 4, true); // ignored: would create a cycle
  4997. CHECK (! song.routes (1, 4));
  4998. const auto order = song.mixerOrder();
  4999. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  5000. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  5001. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  5002. song.setRoute (4, 2, false);
  5003. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  5004. // route levels (FL's route knobs): to inserts and to Master, clamped to the fader range; none without a route
  5005. song.setRoute (3, 5, true);
  5006. song.setRouteLevel (3, 5, 0.5f);
  5007. song.setRouteLevel (3, 0, 1.4f);
  5008. CHECK (song.routeLevel (3, 5) == 0.5f && song.routeLevel (3, 0) == 1.4f && song.mixer[3].masterLevel == 1.4f);
  5009. song.setRouteLevel (3, 0, 9.0f);
  5010. CHECK (song.routeLevel (3, 0) == lynx::maxFaderGain);
  5011. song.setRouteLevel (3, 6, 0.5f); // no such route: ignored
  5012. CHECK (! song.routes (3, 6) && song.routeLevel (3, 6) == 0.0f);
  5013. song.setRoute (3, 0, false);
  5014. CHECK (song.routeLevel (3, 0) == 0.0f);
  5015. // the route to Master's level scales what insert 1 sends there (0.5 + 0.5·0.25)
  5016. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRouteLevel (1, 0, 0.25f); }) - 0.625f) < 1e-4f);
  5017. }
  5018. namespace
  5019. {
  5020. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  5021. class LatentEffect final : public lynx::Effect
  5022. {
  5023. public:
  5024. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  5025. void prepare (double) noexcept override {}
  5026. void reset() noexcept override {}
  5027. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  5028. int latencySamples() const noexcept override { return latency; }
  5029. private:
  5030. int latency;
  5031. lynx::DelayLine line;
  5032. };
  5033. } // namespace
  5034. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  5035. {
  5036. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  5037. lynx::DelayLine a (37), b (37);
  5038. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  5039. for (int i = 0; i < 300; ++i)
  5040. {
  5041. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  5042. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  5043. }
  5044. for (int start = 0; start < 300; start += 100)
  5045. {
  5046. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  5047. for (int i = start; i < start + 100; ++i)
  5048. {
  5049. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  5050. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  5051. }
  5052. a.process (xl.data() + start, xr.data() + start, 100);
  5053. }
  5054. float worst = 0.0f;
  5055. for (int i = 0; i < 300; ++i)
  5056. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  5057. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  5058. CHECK (worst < 1e-6f);
  5059. // silenced source: the tail still in the line comes out, then nothing remains
  5060. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  5061. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  5062. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  5063. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  5064. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  5065. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  5066. }
  5067. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  5068. {
  5069. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  5070. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  5071. fx->prepare (48000.0);
  5072. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5073. slot.params[2] = 0.0f; // 10 ms release
  5074. std::vector<float> l (48000), r (48000);
  5075. std::uint32_t seed = 1;
  5076. for (int i = 0; i < 48000; ++i)
  5077. {
  5078. seed = seed * 1664525u + 1013904223u;
  5079. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  5080. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  5081. r[(std::size_t) i] = -noise;
  5082. }
  5083. for (int i = 0; i < 48000; i += 333)
  5084. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  5085. float peak = 0.0f, quiet = 0.0f;
  5086. for (int i = 0; i < 48000; ++i)
  5087. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  5088. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  5089. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  5090. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  5091. CHECK (peak <= ceiling + 1e-5f);
  5092. CHECK (quiet > 0.03f);
  5093. }
  5094. TEST (pdc_aligns_paths_through_latent_effects)
  5095. {
  5096. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  5097. lynx::Song s;
  5098. s.mixer = lynx::Song::defaultMixer (4);
  5099. s.channels.resize (3);
  5100. s.channels[0].mixerTrack = 1;
  5101. s.channels[1].mixerTrack = 2;
  5102. s.channels[2].mixerTrack = 3;
  5103. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  5104. s.setRoute (1, 3, true);
  5105. s.setRoute (1, 0, false);
  5106. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  5107. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  5108. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  5109. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  5110. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  5111. // bypass zeroes the slot's latency
  5112. s.mixer[1].effects[0].enabled = false;
  5113. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  5114. // rendered: two impulses played together come out together
  5115. lynx::Song r;
  5116. r.mixer = lynx::Song::defaultMixer (2);
  5117. for (int i = 0; i < 2; ++i)
  5118. {
  5119. lynx::ChannelDef c;
  5120. c.instrument = lynx::InstrumentKind::sampler;
  5121. c.samplePath = "impulso";
  5122. c.volume = 1.0f;
  5123. c.mixerTrack = i + 1;
  5124. r.channels.push_back (c);
  5125. }
  5126. r.patterns.emplace_back();
  5127. lynx::EffectSlot fx;
  5128. fx.kind = lynx::EffectKind::plugin;
  5129. fx.plugin.id = "latente";
  5130. r.mixer[1].effects.push_back (fx);
  5131. lynx::RackState::Factories f;
  5132. f.samples = [] (const std::string&) {
  5133. auto d = std::make_shared<lynx::SampleData>();
  5134. d->left.assign (64, 0.0f);
  5135. d->left[0] = 1.0f;
  5136. return std::shared_ptr<const lynx::SampleData> (d);
  5137. };
  5138. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  5139. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  5140. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  5141. lynx::RackRenderer renderer;
  5142. renderer.prepare (48000.0, 256);
  5143. lynx::LiveEventQueue live;
  5144. for (int ch = 0; ch < 2; ++ch)
  5145. {
  5146. lynx::NoteEvent on;
  5147. on.channel = ch;
  5148. on.key = 60;
  5149. on.velocity = 1.0f;
  5150. live.push (on);
  5151. }
  5152. std::vector<float> l (256), rr (256);
  5153. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  5154. int first = -1;
  5155. for (int i = 0; i < 256 && first < 0; ++i)
  5156. if (std::abs (l[(std::size_t) i]) > 0.01f)
  5157. first = i;
  5158. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  5159. // editing without changing latencies reuses the lines
  5160. r.channels[0].volume = 0.5f;
  5161. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  5162. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  5163. }
  5164. TEST (mixer_mute_fades_instead_of_clicking)
  5165. {
  5166. lynx::Song s;
  5167. s.mixer = lynx::Song::defaultMixer (2);
  5168. lynx::ChannelDef c;
  5169. c.instrument = lynx::InstrumentKind::sampler;
  5170. c.samplePath = "dc";
  5171. c.volume = 1.0f;
  5172. c.mixerTrack = 1;
  5173. s.channels.push_back (c);
  5174. s.patterns.emplace_back();
  5175. auto provider = [] (const std::string&) {
  5176. auto d = std::make_shared<lynx::SampleData>();
  5177. d->left.assign (48000, 0.5f);
  5178. return std::shared_ptr<const lynx::SampleData> (d);
  5179. };
  5180. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5181. s.mixer[1].muted = true;
  5182. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  5183. lynx::RackRenderer r;
  5184. r.prepare (48000.0, 256);
  5185. lynx::LiveEventQueue live;
  5186. lynx::NoteEvent note;
  5187. note.velocity = 1.0f;
  5188. live.push (note);
  5189. std::vector<float> l (256), rr (256);
  5190. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  5191. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  5192. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5193. // the fader glides down by 0.001 per sample, as FL's (REA_rodada85 §3): 1000 samples from 1 to 0
  5194. CHECK (l[0] > 0.49f && std::abs (l[128] - 0.5f * (1.0f - 0.129f)) < 1e-3f && std::abs (l[255] - 0.5f * (1.0f - 0.256f)) < 1e-3f);
  5195. for (int b = 0; b < 3; ++b)
  5196. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5197. CHECK (std::abs (l[255]) < 1e-6f); // down to zero after 1000 samples
  5198. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5199. CHECK (std::abs (l[0]) < 1e-9f);
  5200. }
  5201. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  5202. {
  5203. lynx::Dither d (16);
  5204. std::vector<float> x (4800);
  5205. for (std::size_t i = 0; i < x.size(); ++i)
  5206. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  5207. auto y = x;
  5208. d.process (y.data(), (int) y.size(), 0);
  5209. double errorSum = 0.0;
  5210. bool onGrid = true;
  5211. for (std::size_t i = 0; i < x.size(); ++i)
  5212. {
  5213. const auto steps = (y[i] - d.writerBias()) * 32768.0f; // half an LSB above the grid point (round 160)
  5214. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  5215. errorSum += std::abs (y[i] - x[i]);
  5216. }
  5217. CHECK (onGrid);
  5218. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  5219. }
  5220. TEST (parallel_rendering_matches_sequential)
  5221. {
  5222. // 32 channels (worth parallelizing), inserts with chained sends
  5223. auto song = lynx::Song::makeDefault();
  5224. song.mixer = lynx::Song::defaultMixer (16);
  5225. for (int i = 5; i < 32; ++i)
  5226. {
  5227. lynx::ChannelDef c;
  5228. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  5229. c.mixerTrack = 1 + i % 12;
  5230. c.volume = 0.3f;
  5231. song.channels.push_back (c);
  5232. for (int step = i % 4; step < 16; step += 3)
  5233. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  5234. }
  5235. song.patterns[0].sortNotes();
  5236. song.setRoute (3, 7, true, 0.6f);
  5237. song.setRoute (7, 9, true, 0.5f);
  5238. song.setRoute (2, 9, true);
  5239. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5240. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  5241. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  5242. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  5243. CHECK (parState->graphOk); // round 165i: the task graph runs it (channels and inserts by readiness)
  5244. lynx::WorkerPool pool (3);
  5245. lynx::RackRenderer seq, par;
  5246. seq.prepare (48000.0, 512);
  5247. par.prepare (48000.0, 512);
  5248. par.setWorkerPool (&pool);
  5249. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  5250. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5251. bool same = true;
  5252. float peak = 0.0f;
  5253. for (int block = 0; block < 200; ++block)
  5254. {
  5255. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  5256. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  5257. same = same && l1 == l2 && r1 == r2;
  5258. for (auto v : l1)
  5259. peak = std::max (peak, std::abs (v));
  5260. }
  5261. CHECK (same);
  5262. CHECK (peak > 0.05f);
  5263. // the pool handles many small rounds in a row
  5264. std::atomic<int> sum { 0 };
  5265. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  5266. for (int round = 0; round < 2000; ++round)
  5267. pool.parallelFor (5, add);
  5268. CHECK (sum.load() == 2000 * 15);
  5269. }
  5270. TEST (rea165i_task_graph_matches_layers_with_sidechain_and_cycle)
  5271. {
  5272. // the task graph against the layers (LYNX_TASKGRAPH off is the same as a pool-less render): sends, a sidechain,
  5273. // a chain three inserts deep, channels on the Master; a routing cycle keeps the layers
  5274. auto song = lynx::Song::makeDefault();
  5275. song.mixer = lynx::Song::defaultMixer (16);
  5276. for (int i = 5; i < 40; ++i)
  5277. {
  5278. lynx::ChannelDef c;
  5279. c.instrument = i % 3 == 0 ? lynx::InstrumentKind::hat : lynx::InstrumentKind::synth;
  5280. c.mixerTrack = i % 7 == 0 ? 0 : 1 + i % 13;
  5281. c.volume = 0.25f;
  5282. song.channels.push_back (c);
  5283. for (int step = i % 5; step < 16; step += 2)
  5284. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 150, i, 40 + i, 0.6f });
  5285. }
  5286. song.patterns[0].sortNotes();
  5287. song.setRoute (3, 7, true, 0.6f);
  5288. song.setRoute (7, 9, true, 0.5f);
  5289. song.setRoute (9, 11, true, 0.5f);
  5290. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5291. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5292. comp.sidechain = 4;
  5293. song.mixer[11].effects.push_back (comp);
  5294. auto render = [] (const lynx::Song& s, bool pool, bool& graph) {
  5295. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  5296. graph = state->graphOk;
  5297. lynx::WorkerPool workers (4);
  5298. lynx::RackRenderer r;
  5299. r.prepare (48000.0, 512);
  5300. if (pool)
  5301. r.setWorkerPool (&workers);
  5302. std::vector<float> out, l (384), rr (384);
  5303. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5304. for (int block = 0; block < 150; ++block)
  5305. {
  5306. r.process (state.get(), true, block * 384 * tps, 384, l.data(), rr.data());
  5307. out.insert (out.end(), l.begin(), l.end());
  5308. out.insert (out.end(), rr.begin(), rr.end());
  5309. }
  5310. return out;
  5311. };
  5312. bool graph = false, unused = false;
  5313. const auto layered = render (song, false, unused);
  5314. const auto parallel = render (song, true, graph);
  5315. CHECK (graph);
  5316. CHECK (layered == parallel);
  5317. CHECK (*std::max_element (layered.begin(), layered.end()) > 0.02f);
  5318. song.mixer[11].sends.push_back ({ 3, 0.3f }); // a cycle 3 → 7 → 9 → 11 → 3 (setRoute refuses it; an old project may hold one)
  5319. const auto cycleLayered = render (song, false, unused);
  5320. const auto cycleParallel = render (song, true, graph);
  5321. CHECK (! graph);
  5322. CHECK (cycleLayered == cycleParallel);
  5323. }
  5324. TEST (rea165i_test_profile_and_test_mode_parsing)
  5325. {
  5326. using namespace lynx::testprofile;
  5327. CHECK (! profileDirFrom (nullptr));
  5328. CHECK (! profileDirFrom (""));
  5329. CHECK (! profileDirFrom ("relative/dir")); // only absolute folders
  5330. CHECK (! profileDirFrom ("C:relative"));
  5331. CHECK (profileDirFrom ("/tmp/lynx-profile/") == std::string ("/tmp/lynx-profile"));
  5332. CHECK (profileDirFrom (" \"C:\\Users\\me\\bench profile\\\" ") == std::string ("C:\\Users\\me\\bench profile"));
  5333. CHECK (profileDirFrom ("D:/x") == std::string ("D:/x"));
  5334. CHECK (profileDirFrom ("C:\\") == std::string ("C:\\")); // a drive root keeps its separator
  5335. CHECK (! testModeFrom (nullptr));
  5336. CHECK (! testModeFrom (""));
  5337. CHECK (! testModeFrom ("0"));
  5338. CHECK (! testModeFrom (" Off "));
  5339. CHECK (! testModeFrom ("false"));
  5340. CHECK (testModeFrom ("1"));
  5341. CHECK (testModeFrom ("yes"));
  5342. }
  5343. TEST (rea165i_keep_float_mode_restores_ftz_after_foreign_code)
  5344. {
  5345. // a hosted plugin that turns flush-to-zero off (or changes the rounding) no longer leaks it into LYNX's DSP
  5346. const lynx::ScopedFlushDenormals ftz;
  5347. const auto before = _mm_getcsr();
  5348. {
  5349. const lynx::KeepFloatMode keep;
  5350. _mm_setcsr ((before & ~0x8040u) | 0x6000u); // what a plugin might leave: no FTZ/DAZ, round toward zero
  5351. }
  5352. CHECK (_mm_getcsr() == before);
  5353. volatile float tiny = 1.0e-38f;
  5354. volatile float smaller = tiny * 1.0e-3f; // denormal without FTZ
  5355. CHECK (smaller == 0.0f);
  5356. }
  5357. TEST (arrangements_switch_and_keep_clip_indices)
  5358. {
  5359. auto s = lynx::Song::makeDefault();
  5360. s.patterns.emplace_back().name = "B";
  5361. s.playlist = { { 1, 0, 0, 960, 0 } };
  5362. s.loopEnd = 960;
  5363. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  5364. const auto second = s.addArrangement ("Versão curta", true);
  5365. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  5366. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  5367. s.loopEnd = 0;
  5368. s.selectArrangement (0);
  5369. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  5370. // removing pattern 0 fixes the clips stored in the other arrangement
  5371. s.removePattern (0);
  5372. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  5373. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  5374. s.removeArrangement (0);
  5375. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  5376. }
  5377. namespace
  5378. {
  5379. // Test effect that inverts the signal and counts how many times it processed.
  5380. class CountingInverter final : public lynx::Effect
  5381. {
  5382. public:
  5383. void prepare (double) noexcept override {}
  5384. void reset() noexcept override {}
  5385. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  5386. {
  5387. ++calls;
  5388. for (int i = 0; i < n; ++i)
  5389. {
  5390. l[i] = -l[i];
  5391. r[i] = -r[i];
  5392. }
  5393. }
  5394. int calls = 0;
  5395. };
  5396. } // namespace
  5397. TEST (keyboard_controller_outputs_key_value)
  5398. {
  5399. std::vector<std::uint8_t> blob (554, 0);
  5400. const std::uint32_t version = 3;
  5401. std::memcpy (blob.data(), &version, 4);
  5402. const float key60 = 0.7f, off = 0.2f;
  5403. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  5404. std::memcpy (blob.data() + 548, &off, 4);
  5405. blob[552] = 1; // auto release
  5406. lynx::ChannelDef c;
  5407. c.instrument = lynx::InstrumentKind::controller;
  5408. c.controllerState = blob;
  5409. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  5410. inst->prepare (48000.0);
  5411. inst->configure (c, 120.0);
  5412. std::vector<float> l (64), r (64);
  5413. inst->noteOn (60, 1.0f);
  5414. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  5415. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  5416. inst->noteOff (60);
  5417. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  5418. }
  5419. TEST (envelope_controller_ducks_on_each_note)
  5420. {
  5421. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  5422. lynx::ControllerArticulator a;
  5423. a.envOn = true;
  5424. a.base = 0.05f;
  5425. a.envLevel = 1.0f;
  5426. a.count = 3;
  5427. a.time = { 0.0f, 0.2f, 0.3f };
  5428. a.level = { 0.0f, 0.75f, 1.0f };
  5429. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  5430. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  5431. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  5432. a.sustain = 1;
  5433. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  5434. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  5435. a.bipolar = true;
  5436. a.base = 0.5f;
  5437. a.sustain = -1;
  5438. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  5439. }
  5440. TEST (peak_controller_drives_live_automation)
  5441. {
  5442. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  5443. auto render = [] (bool linked) {
  5444. lynx::Song s;
  5445. s.mixer = lynx::Song::defaultMixer (2);
  5446. for (int t = 1; t <= 2; ++t)
  5447. {
  5448. lynx::ChannelDef c;
  5449. c.instrument = lynx::InstrumentKind::sampler;
  5450. c.samplePath = "dc";
  5451. c.volume = 1.0f;
  5452. c.mixerTrack = t;
  5453. s.channels.push_back (c);
  5454. }
  5455. s.patterns.emplace_back();
  5456. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  5457. peak.params[0] = 1.0f; // base 1
  5458. peak.params[1] = 0.0f; // amount −1: 1 − peak
  5459. peak.params[2] = 0.5f; // no tension
  5460. s.mixer[1].effects.push_back (peak);
  5461. if (linked)
  5462. {
  5463. lynx::Automation a;
  5464. a.name = "Peak";
  5465. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  5466. a.source = { 1, 0, 0 };
  5467. s.automations.push_back (a);
  5468. }
  5469. lynx::RackState::Factories f;
  5470. f.samples = [] (const std::string&) {
  5471. auto d = std::make_shared<lynx::SampleData>();
  5472. d->left.assign (48000, 0.5f);
  5473. return std::shared_ptr<const lynx::SampleData> (d);
  5474. };
  5475. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5476. lynx::RackRenderer r;
  5477. r.prepare (48000.0, 512);
  5478. lynx::LiveEventQueue live;
  5479. for (int ch = 0; ch < 2; ++ch)
  5480. {
  5481. lynx::NoteEvent on;
  5482. on.channel = ch;
  5483. on.velocity = 1.0f;
  5484. live.push (on);
  5485. }
  5486. std::vector<float> l (4800), rr (4800);
  5487. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  5488. return l[4000];
  5489. };
  5490. const auto free = render (false), ducked = render (true);
  5491. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  5492. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  5493. }
  5494. TEST (effect_slot_dry_wet_and_smart_disable)
  5495. {
  5496. lynx::Song s;
  5497. s.mixer = lynx::Song::defaultMixer (1);
  5498. lynx::ChannelDef c;
  5499. c.instrument = lynx::InstrumentKind::sampler;
  5500. c.samplePath = "dc";
  5501. c.volume = 1.0f;
  5502. c.mixerTrack = 1;
  5503. s.channels.push_back (c);
  5504. s.patterns.emplace_back();
  5505. lynx::EffectSlot fx;
  5506. fx.kind = lynx::EffectKind::plugin;
  5507. fx.plugin.id = "inversor";
  5508. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  5509. s.mixer[1].effects.push_back (fx);
  5510. auto inverter = std::make_shared<CountingInverter>();
  5511. lynx::RackState::Factories f;
  5512. f.samples = [] (const std::string&) {
  5513. auto d = std::make_shared<lynx::SampleData>();
  5514. d->left.assign (4800, 0.5f);
  5515. return std::shared_ptr<const lynx::SampleData> (d);
  5516. };
  5517. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  5518. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5519. lynx::RackRenderer r;
  5520. r.prepare (48000.0, 512);
  5521. lynx::LiveEventQueue live;
  5522. lynx::NoteEvent on;
  5523. on.velocity = 1.0f;
  5524. live.push (on);
  5525. std::vector<float> l (480), rr (480);
  5526. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5527. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  5528. // after the sample ends: 4 s of silence and the plugin stops being called
  5529. for (int i = 0; i < 100 * 6; ++i) // 6 s
  5530. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5531. const auto callsAfterSilence = inverter->calls;
  5532. for (int i = 0; i < 100; ++i)
  5533. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5534. CHECK (inverter->calls == callsAfterSilence);
  5535. CHECK (callsAfterSilence < 100 * 6);
  5536. // sound again: processing resumes in the same block
  5537. live.push (on);
  5538. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5539. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  5540. }
  5541. TEST (swing_delays_offbeat_sixteenths)
  5542. {
  5543. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  5544. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  5545. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  5546. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  5547. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  5548. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  5549. lynx::Pattern p;
  5550. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  5551. lynx::EventBuffer ev;
  5552. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5553. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 12000, tps, ev, 1.0f);
  5554. int onOffset = -1;
  5555. for (auto& e : ev)
  5556. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  5557. onOffset = e.sampleOffset;
  5558. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  5559. }
  5560. TEST (sampler_settings_pitch_reverse_envelope_cut)
  5561. {
  5562. auto ramp = std::make_shared<lynx::SampleData>();
  5563. for (int i = 0; i < 48000; ++i)
  5564. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  5565. ramp->sampleRate = 48000.0;
  5566. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  5567. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5568. inst->prepare (48000.0);
  5569. lynx::ChannelDef c;
  5570. c.sampler = s;
  5571. inst->configure (c, 120.0);
  5572. inst->noteOn (key, 1.0f);
  5573. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5574. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  5575. if (releaseAfterHalf)
  5576. inst->noteOff (key);
  5577. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  5578. return l;
  5579. };
  5580. // +12 semitones: reads twice as fast
  5581. lynx::SamplerSettings up;
  5582. up.pitch = 12.0f;
  5583. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  5584. // reversed: starts from the end
  5585. lynx::SamplerSettings back;
  5586. back.reverse = true;
  5587. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  5588. // 10 ms attack: starts silent and reaches full level
  5589. lynx::SamplerSettings slow;
  5590. slow.attack = 0.01f;
  5591. const auto a = play (slow, 60, 2000, false);
  5592. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  5593. // 5 ms release: gone right after release; without release it plays in full
  5594. lynx::SamplerSettings rel;
  5595. rel.release = 0.005f;
  5596. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  5597. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  5598. // cut itself: the second note cuts the first (one voice remains)
  5599. lynx::SamplerSettings cut;
  5600. cut.cut = true;
  5601. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5602. inst->prepare (48000.0);
  5603. lynx::ChannelDef cutChannel;
  5604. cutChannel.sampler = cut;
  5605. inst->configure (cutChannel, 120.0);
  5606. inst->noteOn (60, 1.0f);
  5607. std::vector<float> l (2000), r (2000);
  5608. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5609. inst->noteOn (60, 1.0f);
  5610. std::fill (l.begin(), l.end(), 0.0f);
  5611. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  5612. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  5613. }
  5614. TEST (playlist_split_and_track_mute)
  5615. {
  5616. lynx::Clip c { 0, 3, 960, 3840, 480 };
  5617. const auto rest = lynx::splitClip (c, 2000);
  5618. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  5619. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  5620. auto s = lynx::Song::makeDefault();
  5621. s.mode = lynx::PlayMode::song;
  5622. s.playlist = { { 0, 0, 0, 3840, 0 } };
  5623. lynx::EventBuffer ev;
  5624. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5625. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5626. CHECK (ev.size() > 0);
  5627. s.playlistTracks.resize (1);
  5628. s.playlistTracks[0].muted = true;
  5629. ev.clear();
  5630. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5631. CHECK (ev.size() == 0);
  5632. }
  5633. TEST (note_pan_pitch_and_mute)
  5634. {
  5635. lynx::Pattern p;
  5636. lynx::Note a { 0, 240, 0, 60 };
  5637. a.pan = -1.0f;
  5638. a.finePitch = 50.0f;
  5639. lynx::Note b { 240, 240, 0, 62 };
  5640. b.muted = true;
  5641. p.notes = { a, b };
  5642. lynx::EventBuffer ev;
  5643. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5644. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 48000, tps, ev);
  5645. int ons = 0;
  5646. for (auto& e : ev)
  5647. if (e.type == lynx::NoteEvent::Type::on)
  5648. {
  5649. ++ons;
  5650. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  5651. }
  5652. CHECK (ons == 1); // the muted one doesn't play
  5653. // note pan in the sampler: fully left
  5654. auto dc = std::make_shared<lynx::SampleData>();
  5655. dc->left.assign (4800, 0.5f);
  5656. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5657. inst->prepare (48000.0);
  5658. inst->setNextNoteExpression (-1.0f, 0.0f);
  5659. inst->noteOn (60, 1.0f);
  5660. std::vector<float> l (100), r (100);
  5661. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  5662. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  5663. }
  5664. TEST (tempo_automation_changes_speed_and_duration)
  5665. {
  5666. auto song = lynx::Song::makeDefault();
  5667. song.bpm = 120.0;
  5668. song.mode = lynx::PlayMode::song;
  5669. const auto bar = (std::int64_t) lynx::ppq * 4;
  5670. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  5671. lynx::Automation tempo;
  5672. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  5673. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  5674. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  5675. song.automations.push_back (tempo);
  5676. lynx::Clip clip;
  5677. clip.kind = lynx::ClipKind::automation;
  5678. clip.automation = 0;
  5679. clip.track = 1;
  5680. clip.length = bar * 2;
  5681. song.playlist.push_back (clip);
  5682. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5683. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5684. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5685. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5686. CHECK (state->tempoAutomation == 0);
  5687. lynx::RackRenderer r;
  5688. r.prepare (48000.0, 512);
  5689. std::vector<float> l (512), rr (512);
  5690. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5691. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5692. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5693. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5694. }
  5695. TEST (bounce_insert_song_and_apply)
  5696. {
  5697. auto s = lynx::Song::makeDefault();
  5698. s.setRoute (3, 7, true, 0.5f);
  5699. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5700. s.mixer[2].solo = true;
  5701. const auto b = lynx::makeBounceSong (s, 3);
  5702. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5703. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5704. const auto audible = b.soloAudible();
  5705. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5706. const auto channels = s.channels.size();
  5707. const auto inserts = s.mixer.size();
  5708. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5709. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5710. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5711. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5712. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5713. }
  5714. TEST (mixer_stereo_separation_phase_swap)
  5715. {
  5716. // FL's level matrix (REA_rodada2 §2.2, REA_rodada167j §1): outL = ll·inL + rl·inR, outR = rr·inR + lr·inL
  5717. lynx::MixerTrack t;
  5718. auto near = [] (float a, float b) { return std::abs (a - b) < 1e-5f; };
  5719. t.stereo = 1.0f; // mono
  5720. auto m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5721. CHECK (near (m.ll, 0.5f) && near (m.rl, 0.5f) && near (m.rr, 0.5f) && near (m.lr, 0.5f));
  5722. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5723. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5724. CHECK (near (m.ll, 1.5f) && near (m.rl, -0.5f) && near (m.rr, 1.5f) && near (m.lr, -0.5f));
  5725. t.stereo = 0.0f;
  5726. t.swapChannels = true;
  5727. t.invertPhase = true;
  5728. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5729. CHECK (m.ll == 0.0f && m.rl == -1.0f && m.rr == 0.0f && m.lr == -1.0f); // outL = −inR, outR = −inL
  5730. // unity, centre, no separation: exactly the identity (FL's fast path)
  5731. m = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.0f, false);
  5732. CHECK (m == lynx::MixerMatrix {});
  5733. }
  5734. TEST (mixer_pan_fold_and_panning_law_match_fl_render)
  5735. {
  5736. // FL 2026's render of j_tri.flp (round 167j: a 441 Hz left / 882 Hz right sample on ten inserts, pan law
  5737. // triangular), the matrix fitted per insert: {pan, separation, ll, rl, lr, rr}
  5738. struct Case { float pan, sep, ll, rl, lr, rr; };
  5739. const Case fl[] = { { -1.0f, 0.0f, 0.499982f, 0.499972f, 0.0f, 0.0f }, { -0.5f, 0.0f, 0.749991f, 0.249984f, 0.0f, 0.499971f },
  5740. { 0.25f, 0.0f, 0.75f, 0.0f, 0.125001f, 0.874978f }, { 0.5f, 0.0f, 0.499985f, 0.0f, 0.250011f, 0.749964f },
  5741. { 1.0f, 0.0f, 0.0f, 0.0f, 0.499982f, 0.499972f }, { 0.5f, 0.5f, 0.422756f, 0.077253f, 0.4045f, 0.595469f },
  5742. { -0.5f, -0.5f, 1.0f, 0.0f, -0.125004f, 0.624974f }, { 0.75f, 0.0f, 0.250003f, 0.0f, 0.375006f, 0.624975f },
  5743. { 0.0f, 0.75f, 0.70703f, 0.292941f, 0.292965f, 0.707045f } };
  5744. for (const auto& c : fl)
  5745. {
  5746. lynx::MixerTrack t;
  5747. t.stereo = c.sep;
  5748. const auto m = lynx::mixerMatrix (t, lynx::unityFader, c.pan, true);
  5749. // the fit's own error is ~5e-5 (16-bit render)
  5750. CHECK (std::abs (m.ll - c.ll) < 2e-4f && std::abs (m.rl - c.rl) < 2e-4f && std::abs (m.lr - c.lr) < 2e-4f
  5751. && std::abs (m.rr - c.rr) < 2e-4f);
  5752. }
  5753. // circular (the default): +3 dB on the side panned to, and the folded half of the other side
  5754. const auto hard = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 1.0f, false);
  5755. CHECK (std::abs (hard.ll) < 1e-6f && std::abs (hard.rl) < 1e-6f);
  5756. CHECK (std::abs (hard.rr - 0.70710677f) < 1e-6f && std::abs (hard.lr - 0.70710677f) < 1e-6f);
  5757. const auto half = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.5f, false);
  5758. const auto gl = (float) std::cos (0.75 * 3.14159265358979 / 2.0) * 1.4142135f, gr = (float) std::sin (0.75 * 3.14159265358979 / 2.0) * 1.4142135f;
  5759. CHECK (std::abs (half.ll - gl) < 1e-5f && half.rl == 0.0f);
  5760. CHECK (std::abs (half.rr - gr * (1.0f - (1.0f - gl) * 0.5f)) < 1e-5f && std::abs (half.lr - gr * (1.0f - gl) * 0.5f) < 1e-5f);
  5761. // the fader scales the whole matrix (−6 dB-ish position), polarity negates it
  5762. lynx::MixerTrack inv;
  5763. inv.invertPhase = true;
  5764. const auto low = lynx::mixerMatrix (inv, 0.5f, 0.0f, false);
  5765. const auto g = (float) ((std::exp ((double) (0.625f * 2.3978953f)) - 1.0) * 0.1);
  5766. CHECK (std::abs (low.ll + g) < 1e-5f && std::abs (low.rr + g) < 1e-5f && std::abs (low.rl) < 1e-6f);
  5767. }
  5768. TEST (effects_limiter_filter_distortion_chorus)
  5769. {
  5770. const double sr = 48000.0;
  5771. auto sine = [] (float hz, float amp, int n) {
  5772. std::vector<float> v ((std::size_t) n);
  5773. for (int i = 0; i < n; ++i)
  5774. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5775. return v;
  5776. };
  5777. auto peak = [] (const std::vector<float>& v, int from) {
  5778. float p = 0.0f;
  5779. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5780. p = std::max (p, std::abs (v[i]));
  5781. return p;
  5782. };
  5783. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5784. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5785. lim->prepare (sr);
  5786. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5787. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5788. slot.params[1] = 0.5f; // +12 dB
  5789. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5790. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5791. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5792. CHECK (peak (l, 4800) > 0.8f);
  5793. CHECK (lim->latencySamples() == 72);
  5794. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5795. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5796. lp->prepare (sr);
  5797. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5798. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5799. l = sine (8000.0f, 1.0f, 9600);
  5800. r = l;
  5801. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5802. CHECK (peak (l, 4800) < 0.05f);
  5803. // distortion and chorus: finite, audible output
  5804. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5805. {
  5806. auto fx = lynx::makeEffect (kind);
  5807. fx->prepare (sr);
  5808. auto ps = lynx::makeEffectSlot (kind);
  5809. l = sine (220.0f, 0.5f, 9600);
  5810. r = l;
  5811. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5812. bool finite = true;
  5813. for (auto v : l)
  5814. finite = finite && std::isfinite (v);
  5815. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5816. }
  5817. }
  5818. TEST (slide_notes_glide_without_retrigger)
  5819. {
  5820. // sampler with a ramp: the read speed doubles after a one-octave slide
  5821. auto ramp = std::make_shared<lynx::SampleData>();
  5822. for (int i = 0; i < 96000; ++i)
  5823. ramp->left.push_back ((float) i / 96000.0f);
  5824. ramp->sampleRate = 48000.0;
  5825. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5826. inst->prepare (48000.0);
  5827. inst->noteOn (60, 1.0f);
  5828. std::vector<float> l (2000), r (2000);
  5829. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5830. const auto stepBefore = l[999] - l[998];
  5831. inst->slideTo (72, 0.0f, 100.0f);
  5832. std::fill (l.begin(), l.end(), 0.0f);
  5833. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5834. const auto stepAfter = l[999] - l[998];
  5835. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5836. // the sequencer sends "slide" (no off) for the slide note
  5837. lynx::Pattern p;
  5838. lynx::Note base { 0, 960, 0, 48 };
  5839. lynx::Note glide { 480, 240, 0, 55 };
  5840. glide.slide = true;
  5841. p.notes = { base, glide };
  5842. lynx::EventBuffer ev;
  5843. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5844. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 30000, tps, ev);
  5845. int ons = 0, offs = 0, slides = 0;
  5846. for (auto& e : ev)
  5847. {
  5848. ons += e.type == lynx::NoteEvent::Type::on;
  5849. offs += e.type == lynx::NoteEvent::Type::off;
  5850. if (e.type == lynx::NoteEvent::Type::slide)
  5851. {
  5852. ++slides;
  5853. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5854. }
  5855. }
  5856. CHECK (ons == 1 && offs == 1 && slides == 1);
  5857. }
  5858. TEST (synth_fm_phase_modulation)
  5859. {
  5860. auto render = [] (const lynx::SynthSettings& s) {
  5861. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5862. inst->prepare (48000.0);
  5863. lynx::ChannelDef c;
  5864. c.synth = s;
  5865. inst->configure (c, 120.0);
  5866. inst->noteOn (69, 1.0f); // 440 Hz
  5867. std::vector<float> l (48000), r (48000);
  5868. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5869. return l;
  5870. };
  5871. // energy (Goertzel) at frequency f over frames [from, to)
  5872. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5873. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5874. double s1 = 0.0, s2 = 0.0;
  5875. for (int i = from; i < to; ++i)
  5876. {
  5877. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5878. s2 = s1;
  5879. s1 = s0;
  5880. }
  5881. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5882. };
  5883. lynx::SynthSettings plain;
  5884. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5885. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5886. plain.cutoff = 20000.0f;
  5887. auto fm = plain;
  5888. fm.fm = 1.0f;
  5889. const auto a = render (plain), b = render (fm);
  5890. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5891. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5892. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5893. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5894. for (auto v : b)
  5895. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5896. // index decaying to 0: the start is bright, the end is nearly a sine again
  5897. auto decaying = fm;
  5898. decaying.fmDecay = 0.1f;
  5899. decaying.fmSustain = 0.0f;
  5900. const auto c = render (decaying);
  5901. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5902. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5903. CHECK (early > 0.05 && late < 1e-3);
  5904. }
  5905. TEST (synth_three_oscillators_adsr_filter)
  5906. {
  5907. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5908. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5909. inst->prepare (48000.0);
  5910. lynx::ChannelDef c;
  5911. c.synth = s;
  5912. inst->configure (c, 120.0);
  5913. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5914. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5915. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5916. inst->noteOff (69);
  5917. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5918. return l;
  5919. };
  5920. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5921. float p = 0.0f;
  5922. for (int i = from; i < to; ++i)
  5923. p = std::max (p, std::abs (v[(std::size_t) i]));
  5924. return p;
  5925. };
  5926. // default: sounds and fades after the release
  5927. const lynx::SynthSettings standard;
  5928. const auto a = render (standard, 48000, 24000);
  5929. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5930. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5931. lynx::SynthSettings sine;
  5932. sine.osc[0].wave = lynx::Waveform::sine;
  5933. sine.cutoff = 20000.0f;
  5934. const auto b = render (sine, 48000, 47000);
  5935. int crossings = 0;
  5936. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5937. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5938. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5939. float sinePeak = 0.0f;
  5940. for (int i = 4800; i < 9600; ++i)
  5941. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5942. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5943. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5944. lynx::SynthSettings two = sine;
  5945. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5946. two.sustain = 0.0f;
  5947. two.decay = 0.1f;
  5948. const auto c = render (two, 48000, 47000);
  5949. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5950. }
  5951. TEST (set_target_value_round_trips)
  5952. {
  5953. auto s = lynx::Song::makeDefault();
  5954. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5955. const lynx::AutomationTarget targets[] = {
  5956. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5957. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5958. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5959. };
  5960. for (const auto& t : targets)
  5961. {
  5962. CHECK (s.setTargetValue (t, 0.25f));
  5963. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5964. }
  5965. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5966. }
  5967. TEST (cut_groups_stretch_and_envelope_import)
  5968. {
  5969. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5970. lynx::Song s;
  5971. s.mixer = lynx::Song::defaultMixer (2);
  5972. for (int i = 0; i < 2; ++i)
  5973. {
  5974. lynx::ChannelDef c;
  5975. c.instrument = lynx::InstrumentKind::sampler;
  5976. c.samplePath = "dc";
  5977. c.volume = 1.0f;
  5978. c.mixerTrack = i + 1;
  5979. s.channels.push_back (c);
  5980. }
  5981. s.channels[0].cutBy = 1; // open
  5982. s.channels[1].cutGroup = 1; // closed
  5983. auto& p = s.patterns.emplace_back();
  5984. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5985. auto provider = [] (const std::string&) {
  5986. auto d = std::make_shared<lynx::SampleData>();
  5987. d->left.assign (96000, 0.5f);
  5988. return std::shared_ptr<const lynx::SampleData> (d);
  5989. };
  5990. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5991. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5992. lynx::RackRenderer r;
  5993. r.prepare (48000.0, 512);
  5994. std::vector<float> l (512), rr (512);
  5995. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5996. float afterCut = 0.0f;
  5997. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5998. {
  5999. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6000. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  6001. afterCut = std::max (afterCut, std::abs (l[256]));
  6002. }
  6003. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  6004. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  6005. auto ramp = std::make_shared<lynx::SampleData>();
  6006. for (int i = 0; i < 48000; ++i)
  6007. ramp->left.push_back ((float) i / 48000.0f);
  6008. ramp->sampleRate = 48000.0;
  6009. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6010. inst->prepare (48000.0);
  6011. lynx::ChannelDef c;
  6012. c.sampler.stretchBeats = 4.0f;
  6013. inst->configure (c, 120.0);
  6014. inst->noteOn (60, 1.0f);
  6015. std::vector<float> a (48000), b (48000);
  6016. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  6017. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  6018. // FL's Resample: the key still transposes, like tape (round 162, issue #9): an octave up reads twice as fast
  6019. inst->allNotesOff();
  6020. std::fill (a.begin(), a.end(), 0.0f);
  6021. inst->render (a.data(), b.data(), 4800, 1.0f, 1.0f);
  6022. std::fill (a.begin(), a.end(), 0.0f);
  6023. inst->noteOn (72, 1.0f);
  6024. inst->render (a.data(), b.data(), 24000, 1.0f, 1.0f);
  6025. CHECK (std::abs (a[23999] - 0.5f) < 0.01f);
  6026. // FL envelope time: 32950 = 2 s; < 101 = 0
  6027. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  6028. }
  6029. TEST (time_stretch_keeps_pitch)
  6030. {
  6031. lynx::SampleData sine;
  6032. sine.sampleRate = 48000.0;
  6033. for (int i = 0; i < 48000; ++i)
  6034. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  6035. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  6036. int c = 0;
  6037. const auto n = (int) d.frames();
  6038. for (int i = 4800; i < n - 4800; ++i)
  6039. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  6040. return (double) c / ((double) (n - 9600) / d.sampleRate);
  6041. };
  6042. for (double ratio : { 2.0, 0.5, 1.37 })
  6043. {
  6044. const auto out = lynx::timeStretch (sine, ratio);
  6045. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  6046. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  6047. float peak = 0.0f;
  6048. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  6049. peak = std::max (peak, std::abs (out->left[i]));
  6050. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  6051. }
  6052. }
  6053. TEST (sampler_region_start_and_end)
  6054. {
  6055. auto ramp = std::make_shared<lynx::SampleData>();
  6056. for (int i = 0; i < 10000; ++i)
  6057. ramp->left.push_back ((float) i / 10000.0f);
  6058. ramp->sampleRate = 48000.0;
  6059. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6060. inst->prepare (48000.0);
  6061. lynx::ChannelDef c;
  6062. c.sampler.regionStart = 0.25f;
  6063. c.sampler.regionEnd = 0.5f;
  6064. inst->configure (c, 120.0);
  6065. inst->noteOn (60, 1.0f);
  6066. std::vector<float> l (6000), r (6000);
  6067. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  6068. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  6069. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  6070. }
  6071. TEST (arpeggiator_plays_held_chord_in_steps)
  6072. {
  6073. auto song = lynx::Song::makeDefault();
  6074. song.bpm = 120.0;
  6075. song.patterns[0].notes.clear();
  6076. for (int key : { 60, 64, 67 })
  6077. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  6078. song.channels[4].arp.direction = 1; // up
  6079. song.channels[4].arp.stepBeats = 0.25f;
  6080. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6081. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  6082. // the arpeggiator generates the notes; we count what it hands to the instrument
  6083. auto& arp = *state->arpeggiators[4];
  6084. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6085. std::vector<int> keys;
  6086. lynx::EventBuffer ev;
  6087. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  6088. {
  6089. ev.clear();
  6090. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (lynx::Meter (4, 4)), tick, 512, tps, ev);
  6091. int in = 0, out = 0;
  6092. for (auto& e : ev)
  6093. if (e.channel == 4)
  6094. arp.input[(std::size_t) in++] = e;
  6095. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  6096. for (int i = 0; i < out; ++i)
  6097. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  6098. keys.push_back (arp.output[(std::size_t) i].key);
  6099. }
  6100. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  6101. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  6102. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  6103. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  6104. CHECK (keys[i] == expected[i]);
  6105. // actually rendering also makes sound
  6106. lynx::RackRenderer r;
  6107. r.prepare (48000.0, 512);
  6108. std::vector<float> l (512), rr (512);
  6109. float peak = 0.0f;
  6110. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  6111. {
  6112. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6113. for (auto v : l)
  6114. peak = std::max (peak, std::abs (v));
  6115. }
  6116. CHECK (peak > 0.01f);
  6117. }
  6118. TEST (audio_clip_fades_and_gain)
  6119. {
  6120. lynx::Song s;
  6121. s.bpm = 120.0;
  6122. s.mode = lynx::PlayMode::song;
  6123. s.mixer = lynx::Song::defaultMixer (1);
  6124. lynx::ChannelDef c;
  6125. c.instrument = lynx::InstrumentKind::audioClip;
  6126. c.samplePath = "dc";
  6127. c.volume = 1.0f;
  6128. c.mixerTrack = 1;
  6129. s.channels.push_back (c);
  6130. s.patterns.emplace_back();
  6131. lynx::Clip clip;
  6132. clip.kind = lynx::ClipKind::audio;
  6133. clip.channel = 0;
  6134. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  6135. clip.fadeIn = lynx::ppq; // 0.5 s
  6136. clip.fadeOut = lynx::ppq;
  6137. clip.gain = 0.5f;
  6138. s.playlist.push_back (clip);
  6139. auto provider = [] (const std::string&) {
  6140. auto d = std::make_shared<lynx::SampleData>();
  6141. d->left.assign (48000 * 4, 1.0f);
  6142. return std::shared_ptr<const lynx::SampleData> (d);
  6143. };
  6144. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  6145. lynx::RackRenderer r;
  6146. r.prepare (48000.0, 512);
  6147. std::vector<float> all;
  6148. std::vector<float> l (512), rr (512);
  6149. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  6150. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6151. {
  6152. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6153. all.insert (all.end(), l.begin(), l.end());
  6154. }
  6155. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  6156. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  6157. CHECK (all[100] < 0.01f); // fade-in start
  6158. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  6159. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  6160. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  6161. }
  6162. TEST (audio_clip_unity_at_fl_default_volume)
  6163. {
  6164. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  6165. auto dc = std::make_shared<lynx::SampleData>();
  6166. dc->left.assign (48000, 0.5f);
  6167. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  6168. clip->prepare (48000.0);
  6169. lynx::ChannelDef c;
  6170. c.instrument = lynx::InstrumentKind::audioClip;
  6171. c.volume = 10000.0f / 12800.0f;
  6172. clip->configure (c, 120.0);
  6173. clip->noteOn (60, 1.0f);
  6174. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  6175. const auto g = lynx::channelGain (c.volume);
  6176. clip->render (l.data(), r.data(), 4800, g, g);
  6177. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  6178. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  6179. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  6180. sampler->prepare (48000.0);
  6181. c.instrument = lynx::InstrumentKind::sampler;
  6182. sampler->configure (c, 120.0);
  6183. sampler->noteOn (60, 1.0f);
  6184. std::fill (l.begin(), l.end(), 0.0f);
  6185. std::fill (r.begin(), r.end(), 0.0f);
  6186. sampler->render (l.data(), r.data(), 4800, g, g);
  6187. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  6188. }
  6189. TEST (slicer_detects_hits_and_plays_slices_by_key)
  6190. {
  6191. // 4 hits (decaying noise) every 0.25 s
  6192. lynx::SampleData loop;
  6193. loop.sampleRate = 48000.0;
  6194. loop.left.assign (48000, 0.0f);
  6195. lynx::Noise noise;
  6196. for (int hit = 0; hit < 4; ++hit)
  6197. for (int i = 0; i < 6000; ++i)
  6198. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  6199. const auto slices = lynx::detectSlices (loop, 0.5f);
  6200. CHECK (slices.size() == 4);
  6201. for (std::size_t i = 0; i < slices.size(); ++i)
  6202. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  6203. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  6204. auto ramp = std::make_shared<lynx::SampleData>();
  6205. for (int i = 0; i < 48000; ++i)
  6206. ramp->left.push_back ((float) i / 48000.0f);
  6207. ramp->sampleRate = 48000.0;
  6208. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6209. inst->prepare (48000.0);
  6210. lynx::ChannelDef c;
  6211. c.sampler.slices = slices;
  6212. inst->configure (c, 120.0);
  6213. inst->noteOn (61, 1.0f);
  6214. std::vector<float> l (20000), r (20000);
  6215. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  6216. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  6217. }
  6218. TEST (project_templates_render_sound)
  6219. {
  6220. const auto names = lynx::templateNames();
  6221. CHECK (names.size() == 4);
  6222. for (int t = 1; t < (int) names.size(); ++t)
  6223. {
  6224. auto song = lynx::makeTemplate (t);
  6225. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6226. lynx::RackRenderer r;
  6227. r.prepare (48000.0, 512);
  6228. std::vector<float> l (512), rr (512);
  6229. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6230. float peak = 0.0f;
  6231. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6232. {
  6233. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6234. for (auto v : l)
  6235. peak = std::max (peak, std::abs (v));
  6236. }
  6237. CHECK (peak > 0.05f && peak < 4.0f);
  6238. }
  6239. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  6240. }
  6241. // --- interface translations (resources/i18n) -------------------------------------------------------
  6242. namespace i18ntest
  6243. {
  6244. std::string readFile (const std::filesystem::path& path)
  6245. {
  6246. std::ifstream in (path, std::ios::binary);
  6247. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  6248. }
  6249. std::string unescape (const std::string& s)
  6250. {
  6251. std::string out;
  6252. for (std::size_t i = 0; i < s.size(); ++i)
  6253. {
  6254. if (s[i] == '\\' && i + 1 < s.size())
  6255. {
  6256. const auto n = s[++i];
  6257. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  6258. }
  6259. else
  6260. out += s[i];
  6261. }
  6262. return out;
  6263. }
  6264. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  6265. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  6266. {
  6267. if (pos >= s.size() || s[pos] != '"')
  6268. return false;
  6269. raw.clear();
  6270. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  6271. {
  6272. if (s[pos] == '\\' && pos + 1 < s.size())
  6273. raw += s[pos++];
  6274. raw += s[pos];
  6275. }
  6276. if (pos >= s.size() || s[pos] != '"')
  6277. return false;
  6278. ++pos;
  6279. return true;
  6280. }
  6281. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  6282. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  6283. {
  6284. std::map<std::string, std::string> table;
  6285. std::istringstream lines (text);
  6286. std::string line;
  6287. while (std::getline (lines, line))
  6288. {
  6289. std::size_t pos = line.find_first_not_of (" \t");
  6290. std::string key, value;
  6291. if (pos == std::string::npos || ! quoted (line, pos, key))
  6292. continue;
  6293. pos = line.find ('"', pos);
  6294. if (pos == std::string::npos || ! quoted (line, pos, value))
  6295. value.clear();
  6296. const auto k = unescape (key);
  6297. if (table.count (k) != 0)
  6298. duplicates.push_back (k);
  6299. table[k] = unescape (value);
  6300. }
  6301. return table;
  6302. }
  6303. // string literals passed to tr() / trc() / TR_NOOP() in the sources (adjacent literals are concatenated)
  6304. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  6305. {
  6306. std::map<std::string, std::string> keys;
  6307. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  6308. {
  6309. const auto ext = entry.path().extension().string();
  6310. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h" && ext != ".inc"))
  6311. continue;
  6312. // drop // comment lines (they may quote tr ("...") as an example)
  6313. std::string text, line;
  6314. std::istringstream in (readFile (entry.path()));
  6315. while (std::getline (in, line))
  6316. {
  6317. const auto first = line.find_first_not_of (" \t");
  6318. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  6319. text += '\n';
  6320. }
  6321. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  6322. for (const std::string name : { "tr", "trc", "TR_NOOP" })
  6323. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  6324. {
  6325. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  6326. continue;
  6327. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  6328. if (pos == std::string::npos || text[pos] != '(')
  6329. continue;
  6330. pos = text.find_first_not_of (" \t\n", pos + 1);
  6331. std::string key, raw;
  6332. bool any = false;
  6333. while (pos != std::string::npos && quoted (text, pos, raw))
  6334. {
  6335. key += raw;
  6336. any = true;
  6337. pos = text.find_first_not_of (" \t\n", pos);
  6338. }
  6339. if (any)
  6340. keys.emplace (unescape (key), entry.path().filename().string());
  6341. }
  6342. }
  6343. return keys;
  6344. }
  6345. std::set<std::string> placeholders (const std::string& s)
  6346. {
  6347. std::set<std::string> found;
  6348. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  6349. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  6350. found.insert (s.substr (open, close - open + 1));
  6351. return found;
  6352. }
  6353. } // namespace i18ntest
  6354. TEST (translations_cover_every_ui_string)
  6355. {
  6356. using namespace i18ntest;
  6357. const auto text = readFile ("resources/i18n/pt-BR.txt");
  6358. CHECK (! text.empty());
  6359. std::vector<std::string> duplicates;
  6360. const auto table = parseTable (text, duplicates);
  6361. CHECK (table.size() > 300);
  6362. CHECK (duplicates.empty());
  6363. for (const auto& d : duplicates)
  6364. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  6365. const auto keys = sourceKeys ("src");
  6366. CHECK (keys.size() > 300);
  6367. int missing = 0;
  6368. for (const auto& [key, file] : keys)
  6369. if (auto it = table.find (key); it == table.end() || it->second.empty())
  6370. {
  6371. ++missing;
  6372. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  6373. }
  6374. CHECK (missing == 0);
  6375. // no empty values, and every {n} placeholder survives the translation
  6376. int empty = 0, badPlaceholders = 0;
  6377. for (const auto& [english, portuguese] : table)
  6378. {
  6379. empty += portuguese.empty() ? 1 : 0;
  6380. if (placeholders (english) != placeholders (portuguese))
  6381. {
  6382. ++badPlaceholders;
  6383. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  6384. }
  6385. }
  6386. CHECK (empty == 0);
  6387. CHECK (badPlaceholders == 0);
  6388. }
  6389. TEST (translations_embedded_table_is_up_to_date)
  6390. {
  6391. // src/ui/i18n/<id>.h + I18nData.h must be regenerated (tools/make_i18n.py) whenever a resources/i18n/*.txt changes
  6392. int files = 0;
  6393. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6394. if (entry.path().extension() == ".txt")
  6395. {
  6396. ++files;
  6397. const auto id = entry.path().stem().string();
  6398. const auto text = i18ntest::readFile (entry.path());
  6399. const lynx::i18n::data::Table* found = nullptr;
  6400. for (const auto& t : lynx::i18n::data::tables)
  6401. if (id == t.id)
  6402. found = &t;
  6403. CHECK (found != nullptr);
  6404. if (found == nullptr)
  6405. {
  6406. std::printf (" %s.txt is not embedded: run python3 tools/make_i18n.py\n", id.c_str());
  6407. continue;
  6408. }
  6409. const auto unpacked = lynx::inflateZlib (found->zlib, found->zlibSize, false, found->textSize + 16);
  6410. const bool same = unpacked && unpacked->size() == text.size() && found->textSize == text.size()
  6411. && std::memcmp (unpacked->data(), text.data(), text.size()) == 0;
  6412. CHECK (same);
  6413. if (! same)
  6414. std::printf (" %s.txt changed since the last python3 tools/make_i18n.py\n", id.c_str());
  6415. }
  6416. CHECK (files == (int) std::size (lynx::i18n::data::tables));
  6417. }
  6418. TEST (translations_no_value_is_a_python_dict)
  6419. {
  6420. // a value written as a Python dict ("{'de': '...', 'es': ...}", 177a's menu slice) reaches the users verbatim:
  6421. // each table holds its own language's text only
  6422. using namespace i18ntest;
  6423. int bad = 0;
  6424. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6425. if (entry.path().extension() == ".txt")
  6426. {
  6427. std::vector<std::string> unused;
  6428. for (const auto& [key, value] : parseTable (readFile (entry.path()), unused))
  6429. if (value.rfind ("{'", 0) == 0 || value.find ("': '") != std::string::npos)
  6430. {
  6431. ++bad;
  6432. std::printf (" %s: \"%s\" holds a dict\n", entry.path().filename().string().c_str(), key.c_str());
  6433. }
  6434. }
  6435. CHECK (bad == 0);
  6436. }
  6437. TEST (translations_every_language_covers_the_reference_table)
  6438. {
  6439. // every key of pt-BR.txt (the reference table) exists, translated, in every other language; keys just added to
  6440. // pt-BR.txt may wait in resources/i18n/pending.keys ("English" lines) until the other tables catch up
  6441. using namespace i18ntest;
  6442. std::vector<std::string> unused;
  6443. const auto reference = parseTable (readFile ("resources/i18n/pt-BR.txt"), unused);
  6444. std::set<std::string> pending;
  6445. {
  6446. std::istringstream in (readFile ("resources/i18n/pending.keys"));
  6447. std::string line, raw;
  6448. while (std::getline (in, line))
  6449. if (std::size_t pos = line.find_first_not_of (" \t"); pos != std::string::npos && quoted (line, pos, raw))
  6450. pending.insert (unescape (raw));
  6451. }
  6452. auto count = [] (const std::string& s, const std::string& what) {
  6453. int n = 0;
  6454. for (auto at = s.find (what); at != std::string::npos; at = s.find (what, at + what.size()))
  6455. ++n;
  6456. return n;
  6457. };
  6458. int languages = 0;
  6459. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6460. {
  6461. const auto id = entry.path().stem().string();
  6462. if (entry.path().extension() != ".txt" || id == "pt-BR")
  6463. continue;
  6464. ++languages;
  6465. const auto text = readFile (entry.path());
  6466. CHECK (text.rfind ("language: ", 0) == 0); // JUCE LocalisedStrings header
  6467. std::vector<std::string> duplicates;
  6468. const auto table = parseTable (text, duplicates);
  6469. CHECK (duplicates.empty());
  6470. for (const auto& d : duplicates)
  6471. std::printf (" duplicate %s entry: \"%s\"\n", id.c_str(), d.c_str());
  6472. int missing = 0, bad = 0;
  6473. for (const auto& [english, unusedValue] : reference)
  6474. {
  6475. const auto it = table.find (english);
  6476. if ((it == table.end() || it->second.empty()) && pending.count (english) == 0)
  6477. {
  6478. if (++missing <= 10)
  6479. std::printf (" no %s translation for \"%s\" (python3 tools/i18n_tool.py status)\n", id.c_str(), english.c_str());
  6480. }
  6481. }
  6482. for (const auto& [english, translated] : table)
  6483. {
  6484. std::string why;
  6485. if (translated.empty())
  6486. why = "empty";
  6487. else if (placeholders (english) != placeholders (translated))
  6488. why = "placeholders differ";
  6489. else if (count (english, "\n") != count (translated, "\n"))
  6490. why = "line breaks differ";
  6491. else if (count (english, "%") != count (translated, "%"))
  6492. why = "% signs differ";
  6493. if (! why.empty() && ++bad <= 10)
  6494. std::printf (" %s: %s: \"%s\"\n", id.c_str(), why.c_str(), english.c_str());
  6495. }
  6496. CHECK (missing == 0);
  6497. CHECK (bad == 0);
  6498. if (missing > 10 || bad > 10)
  6499. std::printf (" %s: %d missing, %d bad in all\n", id.c_str(), missing, bad);
  6500. }
  6501. CHECK (languages >= 0);
  6502. }
  6503. TEST (translations_os_locale_picks_the_interface_language)
  6504. {
  6505. const std::vector<std::string> ids { "en", "pt-BR", "de", "es", "ja", "zh-Hans", "zh-Hant", "id", "ko" };
  6506. using lynx::i18n::languageForLocale;
  6507. CHECK (languageForLocale ("de-AT", ids) == "de");
  6508. CHECK (languageForLocale ("de_DE.UTF-8", ids) == "de");
  6509. CHECK (languageForLocale ("pt-BR", ids) == "pt-BR");
  6510. CHECK (languageForLocale ("pt_PT", ids) == "pt-BR");
  6511. CHECK (languageForLocale ("zh-Hans-CN", ids) == "zh-Hans");
  6512. CHECK (languageForLocale ("zh-CN", ids) == "zh-Hans");
  6513. CHECK (languageForLocale ("zh_TW.UTF-8", ids) == "zh-Hant");
  6514. CHECK (languageForLocale ("zh-Hant-HK", ids) == "zh-Hant");
  6515. CHECK (languageForLocale ("zh-HK", ids) == "zh-Hant");
  6516. CHECK (languageForLocale ("ja_JP.UTF-8@euro", ids) == "ja");
  6517. CHECK (languageForLocale ("in_ID", ids) == "id");
  6518. CHECK (languageForLocale ("es-419", ids) == "es");
  6519. CHECK (languageForLocale ("en-US", ids) == "en");
  6520. CHECK (languageForLocale ("ko:en", ids) == "ko");
  6521. CHECK (languageForLocale ("fr-FR", ids).empty()); // not in the list
  6522. CHECK (languageForLocale ("C", ids).empty());
  6523. CHECK (languageForLocale ("", ids).empty());
  6524. }
  6525. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  6526. TEST (workflow_pattern_navigation_and_insert)
  6527. {
  6528. auto song = lynx::Song::makeDefault();
  6529. song.patterns.resize (3);
  6530. song.patterns[1].name = "B";
  6531. song.patterns[2].name = "C";
  6532. song.patterns[2].notes.push_back ({});
  6533. song.currentPattern = 0;
  6534. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  6535. // pattern 0 has the default notes, 1 is empty, 2 has a note
  6536. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  6537. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  6538. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  6539. song.patterns[1].notes.push_back ({});
  6540. const auto before = song.patterns.size();
  6541. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  6542. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  6543. // insert keeps Playlist clips on their patterns
  6544. lynx::Clip clip;
  6545. clip.pattern = 2;
  6546. song.playlist = { clip };
  6547. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  6548. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  6549. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  6550. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  6551. }
  6552. TEST (workflow_split_pattern_by_channel)
  6553. {
  6554. auto song = lynx::Song::makeDefault();
  6555. song.patterns.resize (1);
  6556. auto& p = song.patterns[0];
  6557. p.name = "Beat";
  6558. p.notes.clear();
  6559. for (int i = 0; i < 4; ++i)
  6560. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  6561. song.channels[2].colour = 0xff112233;
  6562. lynx::Clip clip;
  6563. clip.pattern = 0;
  6564. song.playlist = { clip };
  6565. const auto created = lynx::splitPatternByChannel (song, 0);
  6566. CHECK (created == 2 && song.patterns.size() == 3);
  6567. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  6568. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  6569. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  6570. CHECK (song.playlist[0].pattern == 0);
  6571. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  6572. }
  6573. TEST (workflow_channel_selection)
  6574. {
  6575. lynx::ChannelSelection s;
  6576. s.selectOnly (2);
  6577. s.toggle (5);
  6578. s.toggle (0);
  6579. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  6580. s.toggle (2);
  6581. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  6582. s.selectOnly (3);
  6583. s.extendTo (6);
  6584. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  6585. s.extendTo (1);
  6586. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  6587. s.selectOnly (4);
  6588. s.toggle (4); // the last one stays
  6589. CHECK ((s.rows == std::vector<int> { 4 }));
  6590. s.selectAll (3);
  6591. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  6592. s.rows = { 7, 9 };
  6593. s.clamp (5);
  6594. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  6595. }
  6596. TEST (workflow_move_clone_insert_channels)
  6597. {
  6598. auto song = lynx::Song::makeDefault();
  6599. const auto names = [&] {
  6600. std::vector<std::string> n;
  6601. for (const auto& c : song.channels)
  6602. n.push_back (c.name);
  6603. return n;
  6604. };
  6605. const auto original = names();
  6606. const auto count = (int) song.channels.size();
  6607. CHECK (count >= 4);
  6608. // references that must follow their channel
  6609. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  6610. lynx::Automation a;
  6611. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  6612. song.automations.push_back (a);
  6613. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  6614. lynx::Clip audio;
  6615. audio.kind = lynx::ClipKind::audio;
  6616. audio.channel = 3;
  6617. song.playlist.push_back (audio);
  6618. std::vector<int> selection { 1, 3 };
  6619. CHECK (lynx::moveChannels (song, selection, -1));
  6620. CHECK ((selection == std::vector<int> { 0, 2 }));
  6621. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  6622. && song.channels[3].name == original[2]);
  6623. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  6624. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  6625. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  6626. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  6627. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  6628. // clone: settings, not notes, right below, with a new name
  6629. song.channels[1].volume = 0.33f;
  6630. const auto notesBefore = song.patterns[0].notes.size();
  6631. const auto clone = lynx::cloneChannel (song, 1);
  6632. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  6633. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  6634. CHECK (song.patterns[0].notes.size() == notesBefore);
  6635. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  6636. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  6637. // remove several
  6638. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  6639. CHECK (names() == original);
  6640. }
  6641. TEST (workflow_send_channels_to_free_inserts)
  6642. {
  6643. auto song = lynx::Song::makeDefault();
  6644. for (auto& c : song.channels)
  6645. c.mixerTrack = 0;
  6646. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  6647. song.channels[0].mixerTrack = 2; // not free: used
  6648. song.setRoute (5, 3, true); // 3 is not free: receives a send
  6649. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  6650. song.channels[1].colour = 0xff445566;
  6651. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  6652. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  6653. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  6654. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  6655. // with no free insert left, a new one is added at the end
  6656. auto full = lynx::Song::makeDefault();
  6657. full.mixer = lynx::Song::defaultMixer (1);
  6658. full.channels[0].mixerTrack = 1;
  6659. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  6660. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  6661. }
  6662. TEST (workflow_add_channel_routed)
  6663. {
  6664. auto song = lynx::Song::makeDefault();
  6665. const auto count = (int) song.channels.size();
  6666. const auto inserts = song.mixer.size();
  6667. lynx::ChannelDef c;
  6668. c.mixerTrack = 7; // what the generator came with doesn't count
  6669. const auto last = (int) song.mixer.size() - 1;
  6670. CHECK (lynx::addChannelRouted (song, c, last) == count && song.channels.back().mixerTrack == last);
  6671. CHECK (lynx::addChannelRouted (song, c, 0) == count + 1 && song.channels.back().mixerTrack == 0); // Master
  6672. CHECK (song.mixer.size() == inserts); // the drop's insert is used as it is, nothing is claimed
  6673. // an insert that doesn't exist claims a free one, as a plain new channel does
  6674. auto expected = song;
  6675. const auto free = expected.claimFreeInsert();
  6676. CHECK (lynx::addChannelRouted (song, c, 9999) == count + 2 && song.channels.back().mixerTrack == free);
  6677. auto plain = expected;
  6678. CHECK (lynx::addChannelRouted (plain, c) == count + 2 && plain.channels.back().mixerTrack == expected.claimFreeInsert());
  6679. }
  6680. TEST (pr28_generator_dropped_on_an_insert_names_it_like_fl)
  6681. {
  6682. // FL 2026 (round 167u, shots fl3-fl10): 3x Osc dropped on Insert 3, Master and Insert 5's slot 2, then FLEX on
  6683. // Insert 3: channels "3x Osc", "3x Osc #2", "3x Osc #3", "FLEX"; Insert 3 / Master / Insert 5 take the first
  6684. // three names, Insert 3 keeps "3x Osc" when FLEX comes
  6685. auto song = lynx::Song::makeDefault();
  6686. song.channels.clear();
  6687. while (song.mixer.size() < 6)
  6688. song.mixer.emplace_back().name = "Insert " + std::to_string (song.mixer.size());
  6689. song.mixer[4].name = "Drums"; // a name the user gave
  6690. lynx::ChannelDef osc;
  6691. osc.name = "3x Osc";
  6692. lynx::ChannelDef flex;
  6693. flex.name = "FLEX";
  6694. CHECK (lynx::addChannelRouted (song, osc, 3) == 0 && song.channels[0].name == "3x Osc" && song.mixer[3].name == "3x Osc");
  6695. CHECK (lynx::addChannelRouted (song, osc, 0) == 1 && song.channels[1].name == "3x Osc #2" && song.channels[1].mixerTrack == 0
  6696. && song.mixer[0].name == "3x Osc #2");
  6697. CHECK (lynx::addChannelRouted (song, osc, 5) == 2 && song.channels[2].name == "3x Osc #3" && song.mixer[5].name == "3x Osc #3");
  6698. CHECK (lynx::addChannelRouted (song, flex, 3) == 3 && song.channels[3].name == "FLEX" && song.channels[3].mixerTrack == 3
  6699. && song.mixer[3].name == "3x Osc");
  6700. CHECK (lynx::addChannelRouted (song, flex, 4) == 4 && song.channels[4].name == "FLEX #2" && song.mixer[4].name == "Drums");
  6701. // a plain new channel (no insert given) keeps its name and doesn't rename anything, as before
  6702. const auto mixerBefore = song.mixer.size();
  6703. CHECK (lynx::addChannelRouted (song, osc) == 5 && song.channels[5].name == "3x Osc");
  6704. CHECK (song.mixer.size() >= mixerBefore && song.mixer[3].name == "3x Osc" && song.mixer[0].name == "3x Osc #2");
  6705. }
  6706. TEST (workflow_palette_random_colour)
  6707. {
  6708. const auto& palette = lynx::lynxPalette();
  6709. for (std::uint32_t seed = 0; seed < 50; ++seed)
  6710. {
  6711. const auto current = palette[seed % palette.size()];
  6712. const auto c = lynx::randomPaletteColour (current, seed);
  6713. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  6714. }
  6715. }
  6716. TEST (workflow_key_map)
  6717. {
  6718. using namespace lynx::keys;
  6719. KeyMap map;
  6720. const auto global = Context::global, rack = Context::channelRack;
  6721. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  6722. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  6723. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  6724. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  6725. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  6726. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  6727. // panel context: only while focused
  6728. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  6729. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  6730. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  6731. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  6732. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  6733. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  6734. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  6735. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  6736. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  6737. CHECK (typingPianoNote ('Z') == 48 && typingPianoNote ('q') == 60 && typingPianoNote ('P') == 76 && typingPianoNote ('L') == -1);
  6738. CHECK (map.conflicts().empty());
  6739. // chord text
  6740. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  6741. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  6742. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  6743. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  6744. for (const auto& a : actions())
  6745. for (const auto& c : a.defaults)
  6746. CHECK (parseChord (toString (c)) == c);
  6747. // JSON overrides
  6748. std::vector<std::string> errors;
  6749. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  6750. CHECK (errors.size() == 2);
  6751. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  6752. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  6753. map.setChords ("pattern.previous", { { right, ctrl } });
  6754. CHECK (map.conflicts().size() == 1);
  6755. KeyMap reloaded;
  6756. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  6757. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  6758. map.resetToDefaults();
  6759. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  6760. }
  6761. TEST (workflow_workspace_layout)
  6762. {
  6763. using namespace lynx::workspace;
  6764. auto l = Layout::makeDefault (1280, 620);
  6765. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  6766. for (const auto& p : l.panels)
  6767. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  6768. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  6769. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  6770. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  6771. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  6772. // toggle: show+focus, focus, hide
  6773. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  6774. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  6775. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  6776. l.toggleMaximised (Panel::playlist);
  6777. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  6778. // save and load at another size: positions scale
  6779. l[Panel::mixer].detached = true;
  6780. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  6781. const auto text = l.serialise();
  6782. const auto big = Layout::parse (text, 2560, 1240);
  6783. CHECK (big.has_value());
  6784. if (big)
  6785. {
  6786. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  6787. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  6788. CHECK (big->order == l.order && big->focused == l.focused);
  6789. }
  6790. 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));
  6791. // resize keeps panels inside and edges glued to the border
  6792. l.resize (1000, 500);
  6793. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  6794. for (const auto& p : l.panels)
  6795. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  6796. // off-screen panels come back
  6797. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  6798. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  6799. // snapping: to the desktop, to another panel, and resizing one edge
  6800. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  6801. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  6802. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  6803. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  6804. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  6805. }
  6806. TEST (workflow_control_math)
  6807. {
  6808. using namespace lynx::controls;
  6809. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  6810. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  6811. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  6812. 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);
  6813. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  6814. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  6815. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  6816. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  6817. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  6818. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  6819. TapTempo tap;
  6820. CHECK (! tap.tap (1000.0));
  6821. CHECK (tap.tap (1500.0) == 120.0);
  6822. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  6823. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  6824. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  6825. }
  6826. TEST (flp_fl11_mixer_slots_and_disabled_channels)
  6827. {
  6828. // REA_rodada80 §3: FL <= 11 files close an effect slot without 0x62 (the slot is the 0xD4 record's second
  6829. // i32); the channel's 0x00 byte is its Channel Rack switch
  6830. if (realFlpDir.empty())
  6831. return;
  6832. auto load = [] (const std::filesystem::path& p) {
  6833. std::ifstream in (p, std::ios::binary);
  6834. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6835. return bytes;
  6836. };
  6837. const auto base = std::filesystem::path (realFlpDir);
  6838. if (const auto b = load (base / "System" / "Render tests" / "Fruity Phase Inverter.flp"); ! b.empty())
  6839. {
  6840. const auto project = flp::parseProject (b);
  6841. CHECK (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1);
  6842. if (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1)
  6843. CHECK (project.mixer[1].slots[0].index == 0 && project.mixer[1].slots[0].plugin == "Fruity Wrapper"
  6844. && ! project.mixer[1].slots[0].state.empty());
  6845. }
  6846. if (const auto b = load (base / "Demo projects" / "Performance mode" / "nucleon - Steptime.flp"); ! b.empty())
  6847. {
  6848. const auto project = flp::parseProject (b);
  6849. CHECK (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4);
  6850. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6851. for (int i = 0; i < 4; ++i)
  6852. CHECK (project.mixer[3].slots[(std::size_t) i].index == i);
  6853. const char* names[] = { "Fruity Delay Bank", "Fruity Reeverb 2", "Maximus", "Fruity Parametric EQ 2" };
  6854. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6855. for (int i = 0; i < 4; ++i)
  6856. CHECK (project.mixer[3].slots[(std::size_t) i].plugin == names[i]);
  6857. }
  6858. if (const auto b = load (base / "Demo projects" / "Product demos" / "Synth_dfr - Overcome - Sytrus.flp"); ! b.empty())
  6859. {
  6860. const auto project = flp::parseProject (b);
  6861. int disabled = 0;
  6862. for (const auto& c : project.channels)
  6863. disabled += ! c.enabled;
  6864. CHECK (disabled == 2);
  6865. const auto song = lynx::importFlp (project);
  6866. int muted = 0;
  6867. for (const auto& c : song.channels)
  6868. muted += c.muted;
  6869. CHECK (muted == 2);
  6870. }
  6871. }
  6872. TEST (three_osc_host_hq_render_box_filters_aliasing_shapes)
  6873. {
  6874. // REA_rodada80 §3.3 (3x Osc FUN_0073a000): in FL's HQ render a saw is the sum of k = max(inc >> 22, 1) phase-shifted
  6875. // copies at gain g/k; a sine is not oversampled
  6876. auto state = [] (std::int32_t shape) {
  6877. std::vector<std::uint8_t> st (0x60, 0);
  6878. const std::int32_t version = 14;
  6879. std::memcpy (st.data(), &version, 4);
  6880. std::memcpy (st.data() + 4 + 4 * 1, &shape, 4); // osc 1 shape; the mix knobs at 0: osc 1 alone
  6881. std::vector<std::uint8_t> snap (364, 0);
  6882. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  6883. for (int k = 0; k < 5; ++k)
  6884. std::memcpy (snap.data() + 0x44 * k, neutral, sizeof (neutral));
  6885. const std::int32_t modX = 256, four = 4, m101 = -101;
  6886. std::memcpy (snap.data() + 0x44, &four, 4);
  6887. std::memcpy (snap.data() + 0x44 + 64, &m101, 4);
  6888. std::memcpy (snap.data() + 0x15c, &modX, 4);
  6889. st.insert (st.end(), snap.begin(), snap.end());
  6890. return st;
  6891. };
  6892. static bool hostHq = false;
  6893. auto render = [&] (std::int32_t shape, bool hq) {
  6894. hostHq = hq;
  6895. auto inst = lynx::makeThreeOscInstrument ([]() noexcept { return hostHq; });
  6896. lynx::ChannelDef c;
  6897. c.instrument = lynx::InstrumentKind::threeosc;
  6898. c.pluginState = state (shape);
  6899. inst->configure (c, 140.0);
  6900. inst->prepare (44100.0);
  6901. inst->noteOn (96, 0.78f);
  6902. std::vector<float> l (4096), r (4096);
  6903. inst->render (l.data(), r.data(), 4096, 1.0f, 1.0f);
  6904. return l;
  6905. };
  6906. auto compare = [&] (std::int32_t shape) {
  6907. const auto live = render (shape, false), hq = render (shape, true);
  6908. double diff = 0.0, energy = 0.0;
  6909. for (std::size_t i = 0; i < live.size(); ++i)
  6910. diff += std::abs (live[i] - hq[i]), energy += std::abs (live[i]);
  6911. return std::make_pair (energy, diff);
  6912. };
  6913. CHECK (lynx::threeOscStateUsable (state (3)));
  6914. const auto [sawEnergy, sawDiff] = compare (3);
  6915. CHECK (sawEnergy > 1.0 && sawDiff > 1.0e-3); // a saw renders differently in the host's HQ mode
  6916. const auto [sineEnergy, sineDiff] = compare (0);
  6917. CHECK (sineEnergy > 1.0 && sineDiff == 0.0); // a sine does not
  6918. }
  6919. namespace
  6920. {
  6921. // The runner's options (round speed185a). They choose WHICH tests run and how the output looks, never what a test checks.
  6922. // [folder] the FL Data folder (realFlpDir), as before
  6923. // --list print the selected test names, one per line, and run nothing
  6924. // --filter a,b run only the tests whose name contains one of these; several --filter options must all match
  6925. // (the LYNX_TEST variable works as before, as one more --filter)
  6926. // --exclude a,b skip the tests whose name contains one of these
  6927. // --shard i/n run only shard i of n (1-based): the selected tests split into n disjoint sets that together
  6928. // cover them all. With --weights the sets are balanced by time, otherwise test k goes to k mod n.
  6929. // --weights file "<name> <ms>" lines (--times output) used to balance the shards; unknown tests weigh 50 ms
  6930. // --times file write "<name> <ms>" per test that ran
  6931. // --no-times no per-test time lines
  6932. // Output: each test's name on its own line (as before), its FAILED lines, then " time <ms> ms"; at the end the slowest
  6933. // tests and the unchanged "N checks, M failures" line. stdout is line-buffered so a long test never looks hung.
  6934. struct RunnerOptions
  6935. {
  6936. bool list = false, times = true;
  6937. std::vector<std::vector<std::string>> filters; // (each must match: one of its names)
  6938. std::vector<std::string> excludes;
  6939. int shard = 0, shards = 1;
  6940. std::string weightsFile, timesFile;
  6941. };
  6942. std::vector<std::string> splitCommas (const std::string& s)
  6943. {
  6944. std::vector<std::string> out;
  6945. std::string cur;
  6946. for (char c : s + ",")
  6947. if (c == ',')
  6948. {
  6949. if (! cur.empty())
  6950. out.push_back (cur);
  6951. cur.clear();
  6952. }
  6953. else
  6954. cur += c;
  6955. return out;
  6956. }
  6957. bool parseRunnerOptions (int argc, char** argv, RunnerOptions& o)
  6958. {
  6959. for (int i = 1; i < argc; ++i)
  6960. {
  6961. const std::string a = argv[i];
  6962. auto value = [&] () -> std::optional<std::string> {
  6963. if (i + 1 >= argc)
  6964. return std::nullopt;
  6965. return std::string (argv[++i]);
  6966. };
  6967. if (a == "--list")
  6968. o.list = true;
  6969. else if (a == "--no-times")
  6970. o.times = false;
  6971. else if (a == "--filter" || a == "--exclude" || a == "--shard" || a == "--weights" || a == "--times")
  6972. {
  6973. const auto v = value();
  6974. if (! v)
  6975. {
  6976. std::fprintf (stderr, "lynx_tests: %s needs a value\n", a.c_str());
  6977. return false;
  6978. }
  6979. if (a == "--filter")
  6980. o.filters.push_back (splitCommas (*v));
  6981. else if (a == "--exclude")
  6982. for (auto& f : splitCommas (*v)) o.excludes.push_back (f);
  6983. else if (a == "--weights")
  6984. o.weightsFile = *v;
  6985. else if (a == "--times")
  6986. o.timesFile = *v;
  6987. else
  6988. {
  6989. int k = 0, n = 0;
  6990. char tail = 0;
  6991. if (std::sscanf (v->c_str(), "%d/%d%c", &k, &n, &tail) != 2 || n < 1 || k < 1 || k > n)
  6992. {
  6993. std::fprintf (stderr, "lynx_tests: --shard wants i/n with 1 <= i <= n, not %s\n", v->c_str());
  6994. return false;
  6995. }
  6996. o.shard = k - 1;
  6997. o.shards = n;
  6998. }
  6999. }
  7000. else if (a.size() > 2 && a.compare (0, 2, "--") == 0)
  7001. {
  7002. std::fprintf (stderr, "lynx_tests: unknown option %s\n", a.c_str());
  7003. return false;
  7004. }
  7005. else
  7006. realFlpDir = a;
  7007. }
  7008. if (const char* only = std::getenv ("LYNX_TEST")) // run only the tests whose name contains this
  7009. o.filters.push_back ({ only });
  7010. return true;
  7011. }
  7012. // which of the selected tests (indices into `names`) belong to shard o.shard: a partition of all of them over o.shards
  7013. std::vector<std::size_t> shardOf (const std::vector<const char*>& names, const RunnerOptions& o)
  7014. {
  7015. std::vector<std::size_t> all (names.size());
  7016. for (std::size_t k = 0; k < all.size(); ++k)
  7017. all[k] = k;
  7018. if (o.shards <= 1)
  7019. return all;
  7020. std::map<std::string, double> weight;
  7021. if (! o.weightsFile.empty())
  7022. {
  7023. std::ifstream in (o.weightsFile);
  7024. std::string name;
  7025. double ms = 0.0;
  7026. while (in >> name >> ms)
  7027. weight[name] = ms;
  7028. }
  7029. std::vector<int> owner (names.size(), 0);
  7030. if (weight.empty())
  7031. for (std::size_t k = 0; k < names.size(); ++k)
  7032. owner[k] = (int) (k % (std::size_t) o.shards);
  7033. else
  7034. {
  7035. // longest first onto the least loaded shard (ties: lower index), deterministic for a given list and file
  7036. auto order = all;
  7037. auto w = [&] (std::size_t k) { const auto it = weight.find (names[k]); return it == weight.end() ? 50.0 : it->second; };
  7038. std::stable_sort (order.begin(), order.end(), [&] (std::size_t a, std::size_t b) { return w (a) > w (b); });
  7039. std::vector<double> load ((std::size_t) o.shards, 0.0);
  7040. for (auto k : order)
  7041. {
  7042. const auto s = (std::size_t) (std::min_element (load.begin(), load.end()) - load.begin());
  7043. owner[k] = (int) s;
  7044. load[s] += w (k);
  7045. }
  7046. }
  7047. std::vector<std::size_t> mine;
  7048. for (auto k : all)
  7049. if (owner[k] == o.shard)
  7050. mine.push_back (k); // (in registry order: a shard runs its tests in the usual order)
  7051. return mine;
  7052. }
  7053. } // namespace
  7054. int main (int argc, char** argv)
  7055. {
  7056. std::setvbuf (stdout, nullptr, _IOLBF, 0);
  7057. RunnerOptions opt;
  7058. if (! parseRunnerOptions (argc, argv, opt))
  7059. return 2;
  7060. auto& tests = registry();
  7061. std::vector<const char*> names;
  7062. std::vector<std::size_t> selected;
  7063. for (std::size_t k = 0; k < tests.size(); ++k)
  7064. {
  7065. const char* name = tests[k].first;
  7066. auto has = [name] (const std::string& part) { return std::strstr (name, part.c_str()) != nullptr; };
  7067. if (! std::all_of (opt.filters.begin(), opt.filters.end(), [&] (const auto& any) { return std::any_of (any.begin(), any.end(), has); }))
  7068. continue;
  7069. if (std::any_of (opt.excludes.begin(), opt.excludes.end(), has))
  7070. continue;
  7071. names.push_back (name);
  7072. selected.push_back (k);
  7073. }
  7074. const auto mine = shardOf (names, opt);
  7075. if (opt.list)
  7076. {
  7077. for (auto k : mine)
  7078. std::printf ("%s\n", names[k]);
  7079. return 0;
  7080. }
  7081. std::vector<std::pair<double, const char*>> took;
  7082. for (auto k : mine)
  7083. {
  7084. auto& [name, fn] = tests[selected[k]];
  7085. std::printf ("%s\n", name);
  7086. const auto start = std::chrono::steady_clock::now();
  7087. fn();
  7088. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - start).count();
  7089. took.emplace_back (ms, name);
  7090. if (opt.times)
  7091. std::printf (" time %.0f ms\n", ms);
  7092. }
  7093. if (! opt.timesFile.empty())
  7094. {
  7095. std::ofstream out (opt.timesFile);
  7096. for (auto& [ms, name] : took)
  7097. out << name << ' ' << (long long) std::llround (ms) << '\n';
  7098. }
  7099. if (opt.times && took.size() > 1)
  7100. {
  7101. double total = 0.0;
  7102. for (auto& t : took)
  7103. total += t.first;
  7104. auto slow = took;
  7105. std::sort (slow.begin(), slow.end(), [] (const auto& a, const auto& b) { return a.first > b.first; });
  7106. slow.resize (std::min<std::size_t> (slow.size(), 15));
  7107. std::printf ("\n %zu tests in %.1f s%s; slowest:\n", took.size(), total / 1000.0,
  7108. opt.shards > 1 ? (" (shard " + std::to_string (opt.shard + 1) + "/" + std::to_string (opt.shards) + ")").c_str() : "");
  7109. for (auto& [ms, name] : slow)
  7110. std::printf (" %8.1f s %s\n", ms / 1000.0, name);
  7111. }
  7112. std::printf ("\n%d checks, %d failures\n", checks, failures);
  7113. return failures == 0 ? 0 : 1;
  7114. }
  7115. TEST (synth_cutoff_keytrack)
  7116. {
  7117. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  7118. auto rms = [] (float keyTrack, int key) {
  7119. lynx::SynthSettings s;
  7120. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7121. s.cutoff = 130.81f;
  7122. s.keyTrack = keyTrack;
  7123. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7124. inst->prepare (48000.0);
  7125. lynx::ChannelDef c;
  7126. c.synth = s;
  7127. inst->configure (c, 120.0);
  7128. inst->noteOn (key, 1.0f);
  7129. std::vector<float> l (24000), r (24000);
  7130. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  7131. double e = 0.0;
  7132. for (int i = 4800; i < 24000; ++i)
  7133. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7134. return std::sqrt (e / 19200.0);
  7135. };
  7136. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  7137. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  7138. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  7139. }
  7140. namespace
  7141. {
  7142. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  7143. {
  7144. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7145. inst->prepare (48000.0);
  7146. lynx::ChannelDef c;
  7147. c.synth = s;
  7148. inst->configure (c, 120.0);
  7149. inst->noteOn (key, 1.0f);
  7150. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  7151. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  7152. return l;
  7153. }
  7154. double goertzel (const std::vector<float>& x, double f, int from, int to)
  7155. {
  7156. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  7157. double s1 = 0.0, s2 = 0.0;
  7158. for (int i = from; i < to; ++i)
  7159. {
  7160. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  7161. s2 = s1;
  7162. s1 = s0;
  7163. }
  7164. return s1 * s1 + s2 * s2 - k * s1 * s2;
  7165. }
  7166. } // namespace
  7167. TEST (synth_pitch_sweep_ring_and_bands)
  7168. {
  7169. lynx::SynthSettings sine;
  7170. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7171. sine.cutoff = 20000.0f;
  7172. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  7173. auto sweep = sine;
  7174. sweep.sweep = 1.0f;
  7175. sweep.sweepTime = 0.01f;
  7176. const auto a = renderSynthNote (sweep, 69, 24000);
  7177. auto crossings = [] (const std::vector<float>& x, int to) {
  7178. int n = 0;
  7179. for (int i = 1; i < to; ++i)
  7180. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  7181. return n;
  7182. };
  7183. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  7184. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  7185. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  7186. auto ring = sine;
  7187. ring.am = ring.ring = true;
  7188. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  7189. const auto b = renderSynthNote (ring, 69, 24000);
  7190. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  7191. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  7192. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  7193. auto bands = sine;
  7194. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  7195. auto rms = [] (const std::vector<float>& x) {
  7196. double e = 0.0;
  7197. for (std::size_t i = 4800; i < x.size(); ++i)
  7198. e += (double) x[i] * x[i];
  7199. return std::sqrt (e / (double) (x.size() - 4800));
  7200. };
  7201. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  7202. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  7203. }
  7204. TEST (love_philter_map_evaluation)
  7205. {
  7206. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  7207. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  7208. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  7209. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  7210. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  7211. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  7212. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  7213. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  7214. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  7215. }
  7216. TEST (lofi_holds_and_quantizes)
  7217. {
  7218. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  7219. fx->prepare (48000.0);
  7220. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  7221. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  7222. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  7223. std::vector<float> l (170), r (170);
  7224. for (std::size_t i = 0; i < l.size(); ++i)
  7225. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  7226. const auto original = l;
  7227. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  7228. for (int i = 0; i < 17; ++i)
  7229. CHECK (l[(std::size_t) i] == l[0]);
  7230. CHECK (l[17] != l[16]);
  7231. const auto step = 2.0f * 2049.0f / 65536.0f;
  7232. for (auto v : l)
  7233. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  7234. CHECK (std::abs (l[0] - original[0]) <= step);
  7235. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  7236. }
  7237. namespace
  7238. {
  7239. lynx::EffectParams effectorParams (int type, float x, float y)
  7240. {
  7241. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  7242. slot.params[0] = (float) type / 11.0f;
  7243. slot.params[3] = x;
  7244. slot.params[4] = y;
  7245. return slot.params;
  7246. }
  7247. } // namespace
  7248. TEST (effector_algorithms)
  7249. {
  7250. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  7251. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  7252. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  7253. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  7254. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  7255. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  7256. fx->prepare (48000.0);
  7257. fx->setSongPosition (0.0);
  7258. auto r = in;
  7259. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  7260. return in;
  7261. };
  7262. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  7263. {
  7264. auto p = effectorParams (0, 0.5f, 1.0f);
  7265. const auto out = run (p, std::vector<float> (4800, 0.5f));
  7266. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  7267. const auto small = run (p, std::vector<float> (4800, 0.05f));
  7268. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  7269. }
  7270. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  7271. {
  7272. auto p = effectorParams (11, 0.0f, 0.0f);
  7273. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7274. int crossings = 0;
  7275. for (std::size_t i = 4800; i < out.size(); ++i)
  7276. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  7277. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  7278. }
  7279. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  7280. {
  7281. auto p = effectorParams (8, 1.0f, 0.0f);
  7282. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  7283. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7284. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  7285. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  7286. }
  7287. // Reverb with Wet = 0: the dry passes intact
  7288. {
  7289. auto p = effectorParams (6, 0.5f, 0.5f);
  7290. p[2] = 0.0f;
  7291. std::vector<float> in (4800);
  7292. for (std::size_t i = 0; i < in.size(); ++i)
  7293. in[i] = (float) std::sin (0.01 * (double) i);
  7294. const auto out = run (p, in);
  7295. for (std::size_t i = 0; i < in.size(); ++i)
  7296. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  7297. }
  7298. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  7299. {
  7300. auto p = effectorParams (5, 0.7f, 0.5f);
  7301. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  7302. std::vector<float> in (12000, 0.0f);
  7303. in[0] = 1.0f;
  7304. const auto out = run (p, in);
  7305. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  7306. CHECK (std::abs (out[3000]) < 1e-6f);
  7307. }
  7308. // Stutter: repeats the recorded start every period
  7309. {
  7310. auto p = effectorParams (9, 0.0f, 0.0f);
  7311. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  7312. std::vector<float> in (9000);
  7313. for (std::size_t i = 0; i < in.size(); ++i)
  7314. in[i] = (float) i / 9000.0f;
  7315. const auto out = run (p, in);
  7316. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  7317. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  7318. }
  7319. }
  7320. TEST (crossover_linkwitz_riley_sums_flat)
  7321. {
  7322. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  7323. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  7324. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  7325. {
  7326. auto run = [&] (bool high) {
  7327. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7328. fx->prepare (48000.0);
  7329. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7330. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  7331. slot.params[high ? 0 : 3] = 1.0f;
  7332. slot.params[high ? 1 : 4] = value;
  7333. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  7334. std::vector<float> l (24000), r (24000);
  7335. for (std::size_t i = 0; i < l.size(); ++i)
  7336. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  7337. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7338. return l;
  7339. };
  7340. const auto low = run (false), high = run (true);
  7341. double peak = 0.0;
  7342. for (std::size_t i = 12000; i < low.size(); ++i)
  7343. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  7344. CHECK (std::abs (peak - 1.0) < 0.02);
  7345. }
  7346. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  7347. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7348. fx->prepare (48000.0);
  7349. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7350. slot.params[3] = 1.0f;
  7351. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  7352. slot.params[5] = 1.0f;
  7353. std::vector<float> l (24000), r (24000);
  7354. for (std::size_t i = 0; i < l.size(); ++i)
  7355. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  7356. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7357. float peak = 0.0f;
  7358. for (std::size_t i = 12000; i < l.size(); ++i)
  7359. peak = std::max (peak, std::abs (l[i]));
  7360. CHECK (peak < 1e-4f);
  7361. }
  7362. TEST (rhythm_gain_and_autopan)
  7363. {
  7364. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  7365. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  7366. slot.params[0] = 1.0f / 64.0f;
  7367. slot.params[1] = 3.0f / 12.0f;
  7368. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  7369. for (int k = 0; k < 3; ++k)
  7370. for (int j = 0; j < 3; ++j)
  7371. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  7372. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  7373. fx->prepare (48000.0);
  7374. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  7375. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  7376. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7377. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  7378. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  7379. CHECK (l[12000] < 1e-3f);
  7380. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  7381. // positive tension: changes quickly at the start of the segment
  7382. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  7383. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  7384. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7385. pan.params[0] = 1.0f / 64.0f;
  7386. pan.params[1] = 1.0f;
  7387. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  7388. ap->prepare (48000.0);
  7389. ap->setSongPosition (0.0);
  7390. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  7391. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  7392. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  7393. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  7394. }
  7395. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  7396. {
  7397. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  7398. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  7399. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7400. slot.params[16] = 1.0f;
  7401. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7402. fx->prepare (48000.0);
  7403. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  7404. std::uint32_t seed = 1;
  7405. for (std::size_t i = 0; i < modL.size(); ++i)
  7406. {
  7407. seed = seed * 1664525u + 1013904223u;
  7408. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7409. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7410. }
  7411. for (int done = 0; done < 48000; done += 480)
  7412. {
  7413. fx->setSidechain (modL.data() + done, modL.data() + done);
  7414. fx->setSidechain2 (car.data() + done, car.data() + done);
  7415. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  7416. }
  7417. auto rms = [&] (int from, int to) {
  7418. double e = 0.0;
  7419. for (int i = from; i < to; ++i)
  7420. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7421. return std::sqrt (e / (to - from));
  7422. };
  7423. CHECK (rms (6000, 20000) > 0.01);
  7424. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  7425. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  7426. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  7427. for (int done = 0; done < 4800; done += 480)
  7428. {
  7429. fx->setSidechain (nullptr, nullptr);
  7430. fx->setSidechain2 (car.data() + done, car.data() + done);
  7431. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  7432. }
  7433. CHECK (std::abs (a[4700]) < 0.05f);
  7434. }
  7435. TEST (vocoder_follows_modulator)
  7436. {
  7437. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  7438. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7439. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7440. fx->prepare (48000.0);
  7441. std::vector<float> l (48000), r (48000);
  7442. std::uint32_t seed = 1;
  7443. for (std::size_t i = 0; i < l.size(); ++i)
  7444. {
  7445. seed = seed * 1664525u + 1013904223u;
  7446. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7447. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7448. }
  7449. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  7450. auto rms = [&] (int from, int to) {
  7451. double e = 0.0;
  7452. for (int i = from; i < to; ++i)
  7453. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7454. return std::sqrt (e / (to - from));
  7455. };
  7456. CHECK (rms (6000, 20000) > 0.01);
  7457. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  7458. for (std::size_t i = 0; i < l.size(); ++i)
  7459. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  7460. }
  7461. TEST (fruity_vocoder_engine)
  7462. {
  7463. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  7464. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  7465. std::vector<float> inL (4096), inR (4096);
  7466. for (int i = 0; i < 4096; ++i)
  7467. {
  7468. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  7469. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  7470. }
  7471. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  7472. lynx::fvocoder::State s;
  7473. s.bands = bands;
  7474. s.order = order;
  7475. s.listen = listen;
  7476. s.swap = swap;
  7477. for (std::size_t k = 0; k < levels.size(); ++k)
  7478. s.levels[k] = levels[k];
  7479. s.c = c;
  7480. return s;
  7481. };
  7482. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  7483. std::vector<float> l = inL, r = inR;
  7484. lynx::fvocoder::Engine e;
  7485. e.setState (s);
  7486. e.prepare (rate);
  7487. for (int i = 0; i < 4096;)
  7488. {
  7489. auto len = std::min (block, 4096 - i);
  7490. if (holdAt > i && holdAt < i + len)
  7491. len = holdAt - i;
  7492. if (i == holdAt)
  7493. {
  7494. auto t = e.state();
  7495. t.c[lynx::fvocoder::hold] = 1;
  7496. e.setState (t);
  7497. }
  7498. e.processStereo (l.data() + i, r.data() + i, len);
  7499. i += len;
  7500. }
  7501. return std::make_pair (l, r);
  7502. };
  7503. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  7504. for (const auto& [i, g] : golden)
  7505. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  7506. };
  7507. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  7508. 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 },
  7509. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  7510. const auto outA = run (a, 44100.0, 512, -1);
  7511. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  7512. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  7513. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  7514. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  7515. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  7516. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  7517. { 4095, -0.021947938948869705f } });
  7518. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  7519. 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 });
  7520. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  7521. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  7522. { 4095, -0.11583124846220016f } });
  7523. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  7524. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  7525. const auto bytes = lynx::fvocoder::serialise (a);
  7526. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  7527. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  7528. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  7529. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  7530. if (slot)
  7531. {
  7532. auto fx = lynx::makeEffect (*slot);
  7533. fx->prepare (44100.0);
  7534. std::vector<float> l = inL, r = inR;
  7535. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  7536. CHECK (l == outA.first && r == outA.second);
  7537. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  7538. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  7539. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  7540. }
  7541. const auto parsed = lynx::fvocoder::parse (bytes);
  7542. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  7543. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  7544. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  7545. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  7546. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  7547. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  7548. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  7549. }
  7550. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  7551. TEST (delphi_math_matches_plugin_runtime)
  7552. {
  7553. // arguments where the C library differs from the Delphi run-time in the last bit
  7554. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  7555. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  7556. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  7557. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  7558. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  7559. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  7560. double s = 0.0, c = 0.0;
  7561. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  7562. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  7563. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  7564. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  7565. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  7566. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  7567. }
  7568. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  7569. // 13 map-axis bytes, tone, notes)
  7570. struct VocodexSpec
  7571. {
  7572. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  7573. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  7574. 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 };
  7575. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  7576. 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
  7577. std::vector<std::uint8_t> bytes() const
  7578. {
  7579. std::vector<std::uint8_t> d;
  7580. 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); };
  7581. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7582. i32 (8 << 16);
  7583. for (int m = 0; m < 13; ++m)
  7584. {
  7585. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  7586. const double y1 = m == 8 ? 0.9918 : y0;
  7587. i32 (1), i32 (0), i32 (2);
  7588. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  7589. {
  7590. put (&dx, 8), put (&y, 8);
  7591. i32 (0), i32 (0); // tension (f32 0), flags
  7592. }
  7593. const std::array<std::uint8_t, 20> tail {};
  7594. put (tail.data(), tail.size());
  7595. }
  7596. for (auto v : params)
  7597. i32 (v);
  7598. for (auto v : block)
  7599. i32 (v);
  7600. put (flags.data(), flags.size());
  7601. const std::array<std::uint8_t, 13> axis {};
  7602. put (axis.data(), axis.size());
  7603. d.push_back (3), put ("PWM", 3);
  7604. i32 (1), i32 (60);
  7605. return d;
  7606. }
  7607. };
  7608. std::vector<float> vocodexSaw (int n, double hz, double fs)
  7609. {
  7610. std::vector<float> out ((std::size_t) n);
  7611. for (int i = 0; i < n; ++i)
  7612. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  7613. return out;
  7614. }
  7615. TEST (vocodex_state_parse_and_conversion)
  7616. {
  7617. VocodexSpec spec;
  7618. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  7619. spec.params[17] = -1200;
  7620. const auto raw = spec.bytes();
  7621. const auto s = lynx::vocodex::parseState (raw);
  7622. CHECK (s.has_value());
  7623. if (! s)
  7624. return;
  7625. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  7626. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  7627. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  7628. 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);
  7629. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  7630. const auto v = lynx::vocodexEffect (raw);
  7631. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  7632. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  7633. spec.params[1] = 150; // Soundgoodizer stage
  7634. const auto w = lynx::vocodexEffect (spec.bytes());
  7635. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  7636. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  7637. }
  7638. TEST (vocodex_vocodes_side_inputs)
  7639. {
  7640. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  7641. // output follows the modulator's envelope and fades after it stops
  7642. VocodexSpec spec;
  7643. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7644. slot.state = spec.bytes();
  7645. auto fx = lynx::makeEffect (slot);
  7646. fx->prepare (48000.0);
  7647. CHECK (fx->latencySamples() == 0);
  7648. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7649. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7650. std::uint32_t seed = 7;
  7651. for (std::size_t i = 0; i < mod.size(); ++i)
  7652. {
  7653. seed = seed * 1664525u + 1013904223u;
  7654. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7655. }
  7656. for (int done = 0; done < 48000; done += 512)
  7657. {
  7658. const int n = std::min (512, 48000 - done);
  7659. fx->setSidechain (mod.data() + done, mod.data() + done);
  7660. fx->setSidechain2 (car.data() + done, car.data() + done);
  7661. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7662. }
  7663. auto rms = [&] (int from, int to) {
  7664. double e = 0.0;
  7665. for (int i = from; i < to; ++i)
  7666. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7667. return std::sqrt (e / (2.0 * (to - from)));
  7668. };
  7669. CHECK (rms (6000, 20000) > 0.02);
  7670. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  7671. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  7672. }
  7673. TEST (vocodex_draft_latency_and_pass_through)
  7674. {
  7675. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  7676. // 16 samples late in Draft
  7677. VocodexSpec spec;
  7678. spec.params[0] = 0; // Wet
  7679. spec.params[4] = 12800; // CarVol = 1
  7680. for (const int draftField : { 1, 0 })
  7681. {
  7682. spec.block[4] = draftField;
  7683. const auto s = lynx::vocodex::parseState (spec.bytes());
  7684. CHECK (s.has_value());
  7685. lynx::vocodex::Engine engine;
  7686. engine.prepare (*s, 44100.0, 300, s->draft());
  7687. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  7688. engine.flush();
  7689. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  7690. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  7691. for (int i = 0; i < 3000; ++i)
  7692. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  7693. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  7694. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  7695. {
  7696. n = std::min (sizes[k % 7], 3000 - done);
  7697. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  7698. }
  7699. const int lag = engine.latencySamples();
  7700. auto maxError = 0.0f;
  7701. for (int i = lag; i < 3000; ++i)
  7702. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  7703. CHECK (maxError == 0.0f);
  7704. }
  7705. }
  7706. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  7707. {
  7708. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  7709. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  7710. VocodexSpec spec;
  7711. spec.params[0] = 2000;
  7712. spec.params[1] = 250;
  7713. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7714. slot.state = spec.bytes();
  7715. auto fx = lynx::makeEffect (slot);
  7716. fx->prepare (48000.0);
  7717. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  7718. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7719. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7720. for (std::size_t i = 0; i < mod.size(); ++i)
  7721. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  7722. auto peak = 0.0f;
  7723. for (int done = 0; done < 48000; done += 512)
  7724. {
  7725. const int n = std::min (512, 48000 - done);
  7726. fx->setSidechain (mod.data() + done, mod.data() + done);
  7727. fx->setSidechain2 (car.data() + done, car.data() + done);
  7728. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7729. }
  7730. for (std::size_t i = 0; i < l.size(); ++i)
  7731. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  7732. CHECK (peak > 0.5f && peak <= 0.9775f);
  7733. }
  7734. TEST (vocodex_tail_bypass_and_noise)
  7735. {
  7736. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  7737. VocodexSpec spec;
  7738. const auto s = lynx::vocodex::parseState (spec.bytes());
  7739. CHECK (s.has_value());
  7740. lynx::vocodex::Engine engine;
  7741. engine.prepare (*s, 44100.0, 512, false);
  7742. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  7743. for (std::size_t i = 0; i < own.size(); ++i)
  7744. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  7745. for (int b = 0; b < 80; ++b)
  7746. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  7747. CHECK (out == own);
  7748. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  7749. spec.params[6] = 9000;
  7750. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  7751. lynx::vocodex::Engine a;
  7752. a.prepare (*noisy, 44100.0, 512, false);
  7753. std::vector<float> mod (1024), first (1024), second (1024);
  7754. for (std::size_t i = 0; i < mod.size(); ++i)
  7755. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  7756. a.flush();
  7757. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  7758. a.flush();
  7759. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  7760. CHECK (first == second);
  7761. auto energy = 0.0;
  7762. for (auto v : first)
  7763. energy += (double) v * v;
  7764. CHECK (energy > 1e-6 && std::isfinite (energy));
  7765. }
  7766. TEST (synth_spectral_oscillator)
  7767. {
  7768. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  7769. lynx::SynthSettings saw;
  7770. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  7771. saw.cutoff = 20000.0f;
  7772. auto spectral = saw;
  7773. spectral.osc[0].wave = lynx::Waveform::spectral;
  7774. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  7775. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  7776. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  7777. for (const auto h : { 1, 2, 3, 5 })
  7778. {
  7779. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  7780. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  7781. }
  7782. // odd only: no 2nd harmonic
  7783. auto odd = spectral;
  7784. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  7785. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  7786. const auto c = renderSynthNote (odd, 57, 24000);
  7787. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  7788. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  7789. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  7790. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  7791. }
  7792. TEST (compressor_sidechain_ducks_and_orders_mixer)
  7793. {
  7794. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  7795. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7796. slot.params[0] = 0.5f; // threshold
  7797. slot.params[1] = 1.0f; // high ratio
  7798. slot.params[4] = 0.0f;
  7799. auto run = [&] (bool withSidechain) {
  7800. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  7801. fx->prepare (48000.0);
  7802. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  7803. if (withSidechain)
  7804. fx->setSidechain (kick.data(), kick.data());
  7805. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  7806. return l.back();
  7807. };
  7808. const auto alone = run (false), ducked = run (true);
  7809. CHECK (ducked < 0.5f * alone);
  7810. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  7811. auto song = lynx::Song::makeDefault();
  7812. while (song.mixer.size() < 8)
  7813. song.mixer.push_back ({});
  7814. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7815. comp.sidechain = 7;
  7816. song.mixer[5].effects.push_back (comp);
  7817. const auto order = song.mixerOrder();
  7818. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  7819. CHECK (at (7) < at (5));
  7820. }
  7821. TEST (pitch_correction_pulls_to_allowed_note)
  7822. {
  7823. auto run = [] (const lynx::EffectParams& p, double hz) {
  7824. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7825. fx->prepare (48000.0);
  7826. std::vector<float> l (48000 * 2), r;
  7827. for (std::size_t i = 0; i < l.size(); ++i)
  7828. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  7829. r = l;
  7830. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  7831. // frequency from zero crossings in the last second
  7832. int crossings = 0;
  7833. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  7834. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  7835. return crossings / 2.0;
  7836. };
  7837. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7838. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  7839. for (int k = 0; k < 12; ++k)
  7840. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  7841. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  7842. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  7843. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  7844. }
  7845. TEST (pitch_correction_bypass_is_a_pure_delay)
  7846. {
  7847. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  7848. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  7849. // (REA_rodada30 §2.4/2.5); both are reported as latency
  7850. std::vector<float> in (48000);
  7851. for (std::size_t i = 0; i < in.size(); ++i)
  7852. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  7853. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  7854. auto run = [&] (bool formant, float gender, int& latency) {
  7855. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7856. fx->prepare (48000.0);
  7857. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  7858. slot.params[2] = 1.0f;
  7859. slot.params[29] = formant ? 1.0f : 0.0f;
  7860. slot.params[30] = gender;
  7861. auto l = in, r = in;
  7862. for (std::size_t i = 0; i < l.size(); i += 480)
  7863. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  7864. latency = fx->latencySamples();
  7865. return l;
  7866. };
  7867. int latency = 0;
  7868. auto plain = run (false, 0.5f, latency);
  7869. CHECK (latency == 9);
  7870. auto worst = 0.0f;
  7871. for (std::size_t i = 4000; i < in.size(); ++i)
  7872. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  7873. CHECK (worst < 1e-5f);
  7874. auto formant = run (true, 0.8f, latency);
  7875. CHECK (latency == 257);
  7876. worst = 0.0f;
  7877. for (std::size_t i = 4000; i < in.size(); ++i)
  7878. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  7879. CHECK (worst < 1e-5f);
  7880. }
  7881. TEST (pitch_correction_formant_path)
  7882. {
  7883. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  7884. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  7885. auto run = [] (float gender) {
  7886. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7887. fx->prepare (48000.0);
  7888. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7889. slot.params[29] = 1.0f;
  7890. slot.params[30] = gender;
  7891. std::vector<float> l (48000 * 2), r;
  7892. for (std::size_t i = 0; i < l.size(); ++i)
  7893. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  7894. r = l;
  7895. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  7896. l.erase (l.begin(), l.begin() + 48000);
  7897. return l;
  7898. };
  7899. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  7900. auto pitch = [] (const std::vector<float>& x) {
  7901. auto best = 0.0, bestLag = 0.0;
  7902. for (int lag = 60; lag <= 160; ++lag)
  7903. {
  7904. auto sum = 0.0;
  7905. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  7906. sum += (double) x[i] * x[i + (std::size_t) lag];
  7907. if (sum > best)
  7908. {
  7909. best = sum;
  7910. bestLag = lag;
  7911. }
  7912. }
  7913. return 48000.0 / bestLag;
  7914. };
  7915. // brightness: energy of the first difference relative to the energy
  7916. auto brightness = [] (const std::vector<float>& x) {
  7917. auto e = 0.0, d = 0.0;
  7918. for (std::size_t i = 1; i < x.size(); ++i)
  7919. {
  7920. e += (double) x[i] * x[i];
  7921. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  7922. }
  7923. return d / e;
  7924. };
  7925. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  7926. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  7927. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  7928. CHECK (brightness (up) > 1.5 * brightness (neutral));
  7929. CHECK (brightness (down) < 0.7 * brightness (neutral));
  7930. }
  7931. TEST (parallel_container_inner_links_modulate)
  7932. {
  7933. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  7934. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  7935. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  7936. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  7937. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7938. lfo.params[0] = 1.0f / 64.0f;
  7939. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  7940. container.branches.push_back ({ { gain, lfo }, 1.0f });
  7941. lynx::EffectLink link;
  7942. link.source = 1;
  7943. link.param = 0;
  7944. for (int i = 0; i <= 32; ++i)
  7945. link.curve.push_back (0.5f * (float) i / 32.0f);
  7946. auto levels = [&] (bool linked) {
  7947. auto slot = container;
  7948. if (linked)
  7949. slot.links.push_back (link);
  7950. auto fx = lynx::makeParallelEffect (slot);
  7951. fx->prepare (48000.0);
  7952. float lo = 1e9f, hi = 0.0f;
  7953. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  7954. {
  7955. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  7956. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  7957. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  7958. lo = std::min (lo, std::abs (l[128]));
  7959. hi = std::max (hi, std::abs (l[128]));
  7960. }
  7961. return std::pair { lo, hi };
  7962. };
  7963. const auto [lo, hi] = levels (true);
  7964. CHECK (hi > 0.8f && lo < 0.12f);
  7965. const auto [lo0, hi0] = levels (false);
  7966. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  7967. }
  7968. TEST (synth_spectrum_decays_per_harmonic)
  7969. {
  7970. // 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
  7971. lynx::SynthSettings s;
  7972. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  7973. s.cutoff = 20000.0f;
  7974. s.decay = 0.0f;
  7975. s.sustain = 1.0f;
  7976. s.spectrum[0] = 1.0f;
  7977. s.spectrum[2] = 1.0f;
  7978. s.spectrumDecay[2] = 0.2f;
  7979. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  7980. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  7981. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  7982. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  7983. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  7984. CHECK (late3 < 1e-4 * late1);
  7985. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  7986. // without per-harmonic decay the spectrum stays as it is
  7987. s.spectrumDecay[2] = 0.0f;
  7988. const auto y = renderSynthNote (s, 57, 48000);
  7989. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  7990. }
  7991. TEST (worker_pool_runs_each_index_once_per_round)
  7992. {
  7993. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  7994. // exactly once, and nothing running after parallelFor returns
  7995. lynx::WorkerPool pool (3);
  7996. std::vector<std::atomic<int>> hits (64);
  7997. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  7998. std::atomic<bool> open { false };
  7999. int failures = 0;
  8000. for (int r = 1; r <= 20000; ++r)
  8001. {
  8002. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  8003. for (int i = 0; i < n; ++i)
  8004. hits[(std::size_t) i].store (0);
  8005. round.store (r);
  8006. open.store (true);
  8007. auto fn = [&, r] (int i) {
  8008. if (! open.load())
  8009. outside.fetch_add (1);
  8010. if (round.load() != r)
  8011. wrongRound.fetch_add (1);
  8012. hits[(std::size_t) i].fetch_add (1);
  8013. };
  8014. pool.parallelFor (n, fn);
  8015. open.store (false);
  8016. for (int i = 0; i < n; ++i)
  8017. failures += hits[(std::size_t) i].load() != 1;
  8018. }
  8019. CHECK (failures == 0);
  8020. CHECK (outside.load() == 0);
  8021. CHECK (wrongRound.load() == 0);
  8022. }
  8023. TEST (worker_pool_wakes_sleeping_workers)
  8024. {
  8025. // rounds far apart (the workers have gone to sleep on the semaphore in between): every index still runs once,
  8026. // and the sleeping workers are woken (REA round 87: the Windows build used to poll with growing Sleep()s)
  8027. lynx::WorkerPool pool (3);
  8028. std::vector<std::atomic<int>> hits (16);
  8029. int failures = 0;
  8030. for (int r = 0; r < 12; ++r)
  8031. {
  8032. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros * 4));
  8033. for (auto& h : hits)
  8034. h.store (0);
  8035. auto fn = [&] (int i) {
  8036. std::this_thread::sleep_for (std::chrono::microseconds (200)); // long enough for woken workers to join
  8037. hits[(std::size_t) i].fetch_add (1);
  8038. };
  8039. pool.parallelFor (16, fn);
  8040. for (auto& h : hits)
  8041. failures += h.load() != 1;
  8042. }
  8043. CHECK (failures == 0);
  8044. CHECK (pool.wakeUps() > 0);
  8045. CHECK (pool.indicesRunByWorkers() > 0);
  8046. }
  8047. TEST (worker_pool_park_sends_spinning_workers_to_sleep)
  8048. {
  8049. // round 116: after park() (end of an audio block) the workers sleep at once instead of spinning for spinMicros, so
  8050. // the next parallelFor, well inside the spin window, has to wake them; and work right after park() still runs once
  8051. lynx::WorkerPool pool (3);
  8052. std::vector<std::atomic<int>> hits (8);
  8053. int failures = 0;
  8054. const auto before = pool.wakeUps();
  8055. for (int r = 0; r < 40; ++r)
  8056. {
  8057. for (auto& h : hits)
  8058. h.store (0);
  8059. auto fn = [&] (int i) {
  8060. std::this_thread::sleep_for (std::chrono::microseconds (100));
  8061. hits[(std::size_t) i].fetch_add (1);
  8062. };
  8063. pool.parallelFor (8, fn);
  8064. pool.park();
  8065. if (r % 2 == 0)
  8066. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros / 5));
  8067. for (auto& h : hits)
  8068. failures += h.load() != 1;
  8069. }
  8070. CHECK (failures == 0);
  8071. CHECK (pool.wakeUps() - before >= 20); // parked workers were woken, not found spinning
  8072. }
  8073. TEST (realtime_stats_count_deadline_misses_and_late_calls)
  8074. {
  8075. lynx::RtStats stats;
  8076. // 10 ms deadline: 3 ms, 9 ms (80-100 %), 12 ms (over), then a call 40 ms after the previous one (late)
  8077. stats.record (100.0, 3.0, 10.0);
  8078. stats.record (110.0, 9.0, 10.0);
  8079. stats.record (120.0, 12.0, 10.0);
  8080. stats.record (160.0, 2.0, 10.0);
  8081. const auto s = stats.snapshot();
  8082. CHECK (s.callbacks == 4);
  8083. CHECK (s.overBudget == 1);
  8084. CHECK (s.nearBudget == 1);
  8085. CHECK (s.lateCalls == 1);
  8086. CHECK (std::abs (s.maxLoad - 1.2) < 1e-9);
  8087. CHECK (std::abs (s.averageLoad - 26.0 / 40.0) < 1e-3);
  8088. CHECK (s.histogram[3] == 1 && s.histogram[9] == 1 && s.histogram[12] == 1 && s.histogram[2] == 1);
  8089. CHECK (std::abs (stats.takeWindowMaxLoad() - 1.2) < 1e-9 && stats.takeWindowMaxLoad() == 0.0);
  8090. // a device restart is not a late call
  8091. stats.deviceRestarted();
  8092. stats.record (5000.0, 1.0, 10.0);
  8093. CHECK (stats.snapshot().lateCalls == 1);
  8094. stats.reset();
  8095. CHECK (stats.snapshot().callbacks == 0 && stats.snapshot().maxLoad == 0.0);
  8096. }
  8097. TEST (safe_default_buffer_size)
  8098. {
  8099. const std::vector<int> wasapi { 144, 160, 192, 224, 256, 288, 320, 384, 448, 480, 512, 576, 640, 1024, 2048 };
  8100. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 48000.0) == 512); // the 10 ms default becomes 512
  8101. CHECK (lynx::prefs::safeBufferSize (wasapi, 1024, 48000.0) == 1024); // already larger: kept
  8102. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 96000.0) == 1024); // ~10.7 ms at 96 kHz
  8103. CHECK (lynx::prefs::safeBufferSize ({ 64, 128, 256 }, 128, 44100.0) == 256); // nothing reaches 512: the largest
  8104. CHECK (lynx::prefs::safeBufferSize ({}, 0, 44100.0) == 0);
  8105. }
  8106. TEST (linux_audio_driver_choice_and_pipewire_helpers)
  8107. {
  8108. // round 161: a first start on Linux opens PipeWire, then JACK, then ALSA
  8109. using lynx::prefs::DriverKind;
  8110. CHECK (lynx::prefs::driverKind ("PipeWire") == DriverKind::pipewire);
  8111. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", true } }) == "PipeWire");
  8112. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", true } }) == "JACK");
  8113. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", false } }) == "ALSA");
  8114. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false }, { "Bela", true } }) == "Bela");
  8115. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false } }).empty());
  8116. CHECK (lynx::prefs::pipewireLatency (256, 48000) == "256/48000");
  8117. CHECK (lynx::prefs::pipewireLatency (0, 0) == "1/1");
  8118. CHECK (lynx::prefs::pipewireChannelName ("FL", 0) == "Left");
  8119. CHECK (lynx::prefs::pipewireChannelName ("FR", 1) == "Right");
  8120. CHECK (lynx::prefs::pipewireChannelName ("MONO", 0) == "Mono");
  8121. CHECK (lynx::prefs::pipewireChannelName ("AUX0", 0) == "Aux 1");
  8122. CHECK (lynx::prefs::pipewireChannelName ("AUX15", 15) == "Aux 16");
  8123. CHECK (lynx::prefs::pipewireChannelName ("AUXX", 2) == "AUXX");
  8124. CHECK (lynx::prefs::pipewireChannelName ("", 2) == "Channel 3");
  8125. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"alsa_output.pci-0000_00_1f.3.analog-stereo"})") == "alsa_output.pci-0000_00_1f.3.analog-stereo");
  8126. CHECK (lynx::prefs::pipewireDefaultName (R"({ "name" : "a\"b" })") == "a\"b");
  8127. CHECK (lynx::prefs::pipewireDefaultName ("{}").empty());
  8128. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"unterminated)").empty());
  8129. CHECK ((lynx::prefs::uniqueDeviceNames ({ "USB Audio", "HDMI", "USB Audio", "USB Audio" })
  8130. == std::vector<std::string> { "USB Audio", "HDMI", "USB Audio (2)", "USB Audio (3)" }));
  8131. }
  8132. TEST (driver_kind_and_shared_mode_buffer_sizes)
  8133. {
  8134. // REA round 97: classic shared WASAPI mixes in whole engine periods, so sizes below it are not offered
  8135. using lynx::prefs::DriverKind;
  8136. CHECK (lynx::prefs::driverKind ("Windows Audio") == DriverKind::wasapiShared);
  8137. CHECK (lynx::prefs::driverKind ("Windows Audio (Exclusive Mode)") == DriverKind::wasapiExclusive);
  8138. CHECK (lynx::prefs::driverKind ("Windows Audio (Low Latency Mode)") == DriverKind::wasapiLowLatency);
  8139. CHECK (lynx::prefs::driverKind ("ASIO") == DriverKind::asio);
  8140. CHECK (lynx::prefs::driverKind ("LYNX ASIO") == DriverKind::lynxAsio);
  8141. CHECK (lynx::prefs::driverKind ("DirectSound") == DriverKind::directSound);
  8142. CHECK (lynx::prefs::driverKind ("ALSA") == DriverKind::alsa);
  8143. CHECK (lynx::prefs::driverKind ("JACK") == DriverKind::jack);
  8144. CHECK (lynx::prefs::driverKind ("CoreAudio") == DriverKind::coreAudio);
  8145. CHECK (lynx::prefs::driverKind ("Some Other Driver") == DriverKind::other);
  8146. const std::vector<int> offered { 144, 192, 256, 480, 512, 1024 };
  8147. CHECK ((lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 480) == std::vector<int> { 480, 512, 1024 }));
  8148. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiExclusive, offered, 480) == offered); // the device's real periods
  8149. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiLowLatency, offered, 480) == offered); // engine periods
  8150. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, { 64, 128 }, 480) == (std::vector<int> { 64, 128 })); // never empty
  8151. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 0) == offered);
  8152. }
  8153. TEST (linear_phase_convolver_matches_direct_convolution)
  8154. {
  8155. // the block FFT convolver (cached twiddles, REA round 87) equals the direct convolution with its kernel
  8156. lynx::peqfl::FftConvolver c;
  8157. c.init (256);
  8158. std::vector<float> table (129, 1.0f);
  8159. for (int k = 40; k < 129; ++k)
  8160. table[(std::size_t) k] = 0.25f; // a high shelf cut
  8161. c.setKernel (table);
  8162. // impulse response through step()
  8163. std::vector<float> ir (600, 0.0f);
  8164. for (int i = 0; i < 600; ++i)
  8165. {
  8166. float l = i == 0 ? 1.0f : 0.0f, r = 0.0f;
  8167. c.step (l, r);
  8168. ir[(std::size_t) i] = l;
  8169. }
  8170. // then a noise signal, compared with direct convolution by that impulse response
  8171. lynx::peqfl::FftConvolver d;
  8172. d.init (256);
  8173. d.setKernel (table);
  8174. std::vector<float> x (2000), y (2000);
  8175. std::uint32_t seed = 1;
  8176. for (auto& v : x)
  8177. v = (float) ((seed = seed * 1664525u + 1013904223u) >> 8) / 16777216.0f - 0.5f;
  8178. for (std::size_t i = 0; i < x.size(); ++i)
  8179. {
  8180. float l = x[i], r = 0.0f;
  8181. d.step (l, r);
  8182. y[i] = l;
  8183. }
  8184. double worst = 0.0;
  8185. for (std::size_t i = 0; i < x.size(); ++i)
  8186. {
  8187. double direct = 0.0;
  8188. for (std::size_t k = 0; k < ir.size() && k <= i; ++k)
  8189. direct += (double) ir[k] * (double) x[i - k];
  8190. worst = std::max (worst, std::abs (direct - (double) y[i]));
  8191. }
  8192. CHECK (worst < 1e-4);
  8193. // and it delays by 3n/4 with the kernel's centre there
  8194. const auto peak = (std::size_t) (std::max_element (ir.begin(), ir.end(), [] (float a, float b) { return std::abs (a) < std::abs (b); }) - ir.begin());
  8195. CHECK (peak == 192);
  8196. }
  8197. TEST (patcher_container_is_reused_across_snapshots)
  8198. {
  8199. // an unchanged Patcher keeps its built effect (its children's tails, delay lines and PDC stay put), and a
  8200. // linear-phase Frequency Splitter reports its latency before its first block (REA round 87: it used to be
  8201. // rebuilt on every snapshot and its latency appeared one block late, re-planning PDC in a loop)
  8202. auto song = lynx::Song::makeDefault();
  8203. if (song.mixer.size() < 2)
  8204. song.mixer.resize (2);
  8205. lynx::EffectSlot splitter;
  8206. splitter.kind = lynx::EffectKind::frequencySplitter;
  8207. splitter.params = lynx::makeEffectSlot (lynx::EffectKind::frequencySplitter).params;
  8208. splitter.params[4] = 0.6f; // linear-phase mode
  8209. splitter.params[6] = 0.0f; // quality 0
  8210. lynx::EffectSlot patcher;
  8211. patcher.kind = lynx::EffectKind::parallel;
  8212. patcher.branches.push_back ({ { splitter }, 1.0f });
  8213. patcher.branches.push_back ({ {}, 0.5f });
  8214. song.mixer[1].effects = { patcher };
  8215. const auto a = lynx::RackState::build (song, nullptr, 44100.0);
  8216. const auto b = lynx::RackState::build (song, a.get(), 44100.0);
  8217. CHECK (a->effects[1][0] == b->effects[1][0]);
  8218. CHECK (a->effects[1][0]->latencySamples() == 384); // 3/4 of the 512-point FFT, before any block
  8219. CHECK (a->latencies == a->currentLatencies());
  8220. song.mixer[1].effects[0].branches[0].chain[0].params[0] = 0.25f; // a child's parameter changed: rebuilt
  8221. const auto c = lynx::RackState::build (song, b.get(), 44100.0);
  8222. CHECK (c->effects[1][0] != b->effects[1][0]);
  8223. song.mixer[1].effects[0].branches[1].gain = 0.75f;
  8224. const auto d = lynx::RackState::build (song, c.get(), 44100.0);
  8225. CHECK (d->effects[1][0] != c->effects[1][0]);
  8226. }
  8227. TEST (ladder_filter_follows_toxic_law)
  8228. {
  8229. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  8230. // the input saturation keeps it bounded even at full resonance
  8231. auto level = [] (bool ladder, float res, double hz) {
  8232. lynx::SynthSettings s;
  8233. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8234. s.cutoff = 500.0f;
  8235. s.resonance = res;
  8236. s.ladder = ladder;
  8237. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  8238. const auto x = renderSynthNote (s, key, 48000);
  8239. double e = 0.0, peak = 0.0;
  8240. for (std::size_t i = 24000; i < 48000; ++i)
  8241. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  8242. return std::pair { std::sqrt (e / 24000.0), peak };
  8243. };
  8244. 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
  8245. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  8246. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  8247. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  8248. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  8249. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  8250. const auto loud = level (true, 1.0f, 523.25);
  8251. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  8252. }
  8253. TEST (synth_spectrum_settles_on_target)
  8254. {
  8255. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  8256. lynx::SynthSettings s;
  8257. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  8258. s.cutoff = 20000.0f;
  8259. s.decay = 0.0f;
  8260. s.sustain = 1.0f;
  8261. s.spectrum[0] = 1.0f;
  8262. s.spectrum[2] = 1.0f;
  8263. s.spectrumDecay[2] = 0.1f;
  8264. s.spectrumTarget[2] = 0.25f;
  8265. const auto x = renderSynthNote (s, 57, 48000);
  8266. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  8267. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  8268. }
  8269. TEST (kepler_chorus_is_stereo_and_keeps_level)
  8270. {
  8271. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  8272. lynx::SynthSettings s;
  8273. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8274. s.cutoff = 20000.0f;
  8275. auto dry = s;
  8276. s.chorus = 1;
  8277. auto render = [] (const lynx::SynthSettings& set) {
  8278. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8279. inst->prepare (48000.0);
  8280. lynx::ChannelDef c;
  8281. c.synth = set;
  8282. inst->configure (c, 120.0);
  8283. inst->noteOn (69, 1.0f);
  8284. std::vector<float> l (96000), r (96000);
  8285. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  8286. return std::pair { l, r };
  8287. };
  8288. const auto [l, r] = render (s);
  8289. const auto [dl, dr] = render (dry);
  8290. double diff = 0.0, e = 0.0, ed = 0.0;
  8291. for (std::size_t i = 48000; i < 96000; ++i)
  8292. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  8293. CHECK (diff / 48000.0 > 1e-3);
  8294. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  8295. }
  8296. TEST (fl_pan_law_matches_love_philter_dll)
  8297. {
  8298. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  8299. struct Row { double pan, ll, rr, rl, lr; };
  8300. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  8301. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  8302. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  8303. for (const auto& row : rows)
  8304. {
  8305. float ll, rl, lr, rr;
  8306. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  8307. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  8308. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  8309. }
  8310. }
  8311. TEST (effector_units_match_fl_dll_impulse_responses)
  8312. {
  8313. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  8314. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  8315. struct Case { int kind; float x, y; double v[4]; };
  8316. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  8317. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  8318. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  8319. for (const auto& c : cases)
  8320. {
  8321. auto fx = lynx::effector::make();
  8322. fx->prepare (44100.0);
  8323. lynx::EffectParams p {};
  8324. p[lynx::effector::type] = c.kind / 11.0f;
  8325. p[lynx::effector::wetMix] = 1.0f;
  8326. p[lynx::effector::paramX] = c.x;
  8327. p[lynx::effector::paramY] = c.y;
  8328. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  8329. p[lynx::effector::modRate] = 0.5f;
  8330. p[lynx::effector::inputLevel] = 1.0f;
  8331. p[lynx::effector::outputGain] = 0.5f;
  8332. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  8333. l[0] = r[0] = 1.0f;
  8334. for (int d = 0; d < 1024; d += 256)
  8335. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  8336. const int at[4] { 1, 10, 100, 1000 };
  8337. for (int k = 0; k < 4; ++k)
  8338. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  8339. }
  8340. }
  8341. TEST (harmless_stereo_tremolo_and_vibrato)
  8342. {
  8343. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  8344. lynx::SynthSettings s;
  8345. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8346. s.cutoff = 20000.0f;
  8347. s.stereoTremolo = 1.0f;
  8348. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8349. inst->prepare (48000.0);
  8350. lynx::ChannelDef c;
  8351. c.synth = s;
  8352. inst->configure (c, 120.0);
  8353. inst->noteOn (69, 1.0f);
  8354. std::vector<float> l (48000), r (48000);
  8355. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  8356. 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; };
  8357. // θ = 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
  8358. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  8359. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  8360. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  8361. auto crossingsSpread = [] (float cents) {
  8362. lynx::SynthSettings v;
  8363. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8364. v.cutoff = 20000.0f;
  8365. v.vibratoCents = cents;
  8366. const auto x = renderSynthNote (v, 69, 48000);
  8367. std::vector<int> zc;
  8368. for (int i = 24000; i < 48000; ++i)
  8369. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  8370. zc.push_back (i);
  8371. int lo = 1 << 30, hi = 0;
  8372. for (std::size_t k = 1; k < zc.size(); ++k)
  8373. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  8374. return hi - lo;
  8375. };
  8376. CHECK (crossingsSpread (0.0f) <= 1);
  8377. CHECK (crossingsSpread (100.0f) >= 4);
  8378. }
  8379. TEST (physical_models_sound_and_end)
  8380. {
  8381. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  8382. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  8383. {
  8384. lynx::ChannelDef c;
  8385. c.instrument = kind;
  8386. if (kind == lynx::InstrumentKind::sakura)
  8387. {
  8388. c.modelParams.assign (146, 0.5f);
  8389. c.modelParams[111] = 3.0f; // both strings at the note
  8390. c.modelParams[113] = 0.5f; // octave ×1
  8391. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  8392. c.modelParams[31] = 0.0f; // noise exciter
  8393. }
  8394. else
  8395. {
  8396. c.modelParams.assign (38, 0.5f);
  8397. c.modelParams[0] = 1.0f; // retrigger noise
  8398. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  8399. }
  8400. auto inst = lynx::makeInstrument (kind);
  8401. inst->prepare (48000.0);
  8402. inst->configure (c, 120.0);
  8403. inst->noteOn (60, 0.8f);
  8404. std::vector<float> l (48000), r (48000);
  8405. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  8406. inst->noteOff (60);
  8407. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  8408. double early = 0.0;
  8409. for (int i = 0; i < 12000; ++i)
  8410. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8411. CHECK (early > 1e-6);
  8412. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  8413. std::vector<float> tail (480000), tailR (480000);
  8414. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  8415. CHECK (inst->idle());
  8416. }
  8417. }
  8418. TEST (squeeze_matches_fl_model)
  8419. {
  8420. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  8421. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  8422. slot.params[1] = 0.0f; // mix 0: dry only
  8423. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  8424. fx->prepare (44100.0);
  8425. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  8426. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  8427. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  8428. slot.params[1] = 1.0f; // wet only
  8429. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  8430. std::vector<float> a (512), b (512);
  8431. for (int i = 0; i < 512; ++i)
  8432. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  8433. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  8434. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  8435. for (int i = 0; i < 512; i += 37)
  8436. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  8437. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  8438. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  8439. CHECK (z[10] == 0.0f);
  8440. }
  8441. TEST (sakura_matrix_and_effects)
  8442. {
  8443. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  8444. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  8445. auto base = [] {
  8446. lynx::ChannelDef c;
  8447. c.instrument = lynx::InstrumentKind::sakura;
  8448. c.modelParams.assign (146, 0.0f);
  8449. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  8450. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  8451. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  8452. for (const auto& [i, v] : values)
  8453. c.modelParams[(std::size_t) i] = v;
  8454. return c;
  8455. };
  8456. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  8457. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  8458. inst->prepare (48000.0);
  8459. inst->configure (c, 120.0);
  8460. inst->noteOn (60, velocity);
  8461. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8462. for (int s = 0; s < total; s += 256)
  8463. {
  8464. if (s == held)
  8465. inst->noteOff (60);
  8466. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  8467. }
  8468. return std::make_pair (l, std::move (inst));
  8469. };
  8470. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8471. double e = 0.0;
  8472. for (int i = from; i < to; ++i)
  8473. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8474. return e;
  8475. };
  8476. auto c = base();
  8477. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  8478. c.modelParams[80] = 1.0f;
  8479. c.modelParams[82] = 1.0f;
  8480. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  8481. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  8482. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  8483. auto d = base();
  8484. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  8485. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  8486. d.modelParams[64] = 0.5f;
  8487. d.modelParams[69] = 1.0f;
  8488. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  8489. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  8490. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  8491. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  8492. }
  8493. TEST (drumsynth_live_engine)
  8494. {
  8495. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  8496. // sources only, noise from the fallback seed), voice modes, patch level/pan
  8497. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  8498. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  8499. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  8500. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  8501. std::vector<std::uint8_t> blob;
  8502. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  8503. put (3);
  8504. put (66);
  8505. for (int i = 0; i < 120; ++i)
  8506. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  8507. for (int i = 0; i < 120 * 38; ++i)
  8508. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  8509. put (0);
  8510. const auto parsed = lynx::drumSynthLiveParams (blob);
  8511. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  8512. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  8513. blob.resize (500);
  8514. CHECK (! lynx::drumSynthLiveParams (blob));
  8515. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  8516. 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,
  8517. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  8518. lynx::ChannelDef c;
  8519. c.instrument = lynx::InstrumentKind::drumsynth;
  8520. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  8521. for (int s = 0; s < 120; ++s)
  8522. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  8523. for (int i = 0; i < 38; ++i)
  8524. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  8525. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  8526. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  8527. inst->configure (def, 120.0);
  8528. inst->prepare (48000.0);
  8529. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8530. for (int s = 0; s < total; s += 512)
  8531. {
  8532. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  8533. if (keys[k + 1] == s)
  8534. inst->noteOn (keys[k], 100.0f / 128.0f);
  8535. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8536. }
  8537. return std::make_tuple (l, r, std::move (inst));
  8538. };
  8539. {
  8540. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  8541. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  8542. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  8543. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  8544. for (const auto& [i, v] : golden)
  8545. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  8546. std::vector<float> tail (48000 * 4), tailR (tail.size());
  8547. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8548. CHECK (inst->idle()); // the engine reported the voice finished
  8549. }
  8550. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8551. double e = 0.0;
  8552. for (int i = from; i < to; ++i)
  8553. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8554. return e;
  8555. };
  8556. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  8557. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  8558. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  8559. auto tone = c;
  8560. tone.modelParams[38 * 12 + 14] = 0.0f;
  8561. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  8562. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  8563. auto poly = tone;
  8564. poly.modelParams[38 * 12 + 24] = 1.0f;
  8565. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  8566. double monoDiff = 0.0, polyDiff = 0.0;
  8567. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  8568. 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]);
  8569. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  8570. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  8571. auto right = c;
  8572. right.modelParams[38 * 12 + 1] = 64.0f;
  8573. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  8574. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  8575. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  8576. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  8577. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  8578. }
  8579. TEST (kepler_lfo_bpm_sync)
  8580. {
  8581. // 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)
  8582. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  8583. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  8584. putU32 (12, 9);
  8585. std::memcpy (state.data() + 16, "Kepler-1", 9);
  8586. putU32 (0x19, 4);
  8587. putU32 (0x1D, 34);
  8588. float k[34] {};
  8589. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  8590. k[6] = 1.0f;
  8591. k[10] = 1.0f; // vibrato on
  8592. k[33] = 1.0f;
  8593. std::memcpy (state.data() + 0x21, k, sizeof k);
  8594. lynx::SynthSettings s;
  8595. float gain = 0.0f;
  8596. CHECK (lynx::synthFromKepler (state, s, gain));
  8597. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  8598. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  8599. k[5] = 0.0f; // slowest: 32 beats
  8600. std::memcpy (state.data() + 0x21, k, sizeof k);
  8601. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  8602. k[6] = 0.0f; // free rate in seconds
  8603. std::memcpy (state.data() + 0x21, k, sizeof k);
  8604. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  8605. }
  8606. namespace
  8607. {
  8608. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  8609. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  8610. // velocity map — and the global block. Every other curve is neutral.
  8611. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  8612. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  8613. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  8614. {
  8615. std::vector<std::uint8_t> b;
  8616. 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))); };
  8617. 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))); };
  8618. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  8619. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8620. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  8621. for (const auto& p : pts)
  8622. {
  8623. f64 (p.dx), f64 (p.y);
  8624. const auto t = (float) p.tension;
  8625. std::uint32_t u;
  8626. std::memcpy (&u, &t, 4);
  8627. i32 ((std::int32_t) u);
  8628. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  8629. }
  8630. for (auto h : hdr)
  8631. i32 (h);
  8632. };
  8633. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8634. 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); };
  8635. i32 (18);
  8636. i32 (1 | 4);
  8637. std::vector<std::uint8_t> part;
  8638. std::swap (b, part);
  8639. std::array<std::int32_t, 386> p {};
  8640. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  8641. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  8642. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  8643. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  8644. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  8645. for (const auto& [i, v] : values)
  8646. p[(std::size_t) i] = v;
  8647. if ((features & 2) != 0)
  8648. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  8649. if ((features & 4) != 0)
  8650. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  8651. if ((features & 8) != 0)
  8652. p[75] = 650, p[4] = 600; // pluck, timbre mix
  8653. if ((features & 32) != 0)
  8654. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  8655. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  8656. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  8657. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  8658. p[(std::size_t) i] = v;
  8659. for (auto v : p)
  8660. i32 (v);
  8661. for (int i = 0; i < 12; ++i)
  8662. i32 (i); // module order
  8663. std::array<std::uint8_t, 0x4b> misc {};
  8664. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  8665. const float one = 1.0f;
  8666. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  8667. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  8668. if ((features & 8) != 0)
  8669. put (0x00, 5);
  8670. misc[0x44] = 1;
  8671. if ((features & 4) != 0)
  8672. misc[0x46] = 1;
  8673. b.insert (b.end(), misc.begin(), misc.end());
  8674. 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 };
  8675. for (int k = 0; k < 43; ++k)
  8676. {
  8677. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  8678. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  8679. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  8680. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  8681. i32 (3);
  8682. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  8683. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  8684. 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 },
  8685. { 0.1, 0, 0.2, 0 } },
  8686. { 0, 1, -1, 0, 4 });
  8687. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  8688. 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 });
  8689. else if (k == 4) // filter-1 envelope
  8690. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  8691. else
  8692. flat (neutral);
  8693. for (int a : { 128, 128, 0, 128 })
  8694. i32 (a);
  8695. if (k == 0 && panLfo) // pan LFO
  8696. flat (1.0, on);
  8697. else
  8698. flat (0.5);
  8699. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  8700. i32 (a);
  8701. i32 (0);
  8702. for (int m = 2; m < 11; ++m)
  8703. if (k == 1 && m == 3)
  8704. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  8705. else
  8706. flat (neutral);
  8707. }
  8708. for (int c = 0; c < 26; ++c)
  8709. if ((features & 16) != 0 && c == 4)
  8710. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  8711. else if ((features & 32) != 0 && (c == 22 || c == 23))
  8712. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  8713. else if (c == 3 && (features & 8) != 0)
  8714. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  8715. else if (c == 2 || c == 3)
  8716. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  8717. else if (c == 14)
  8718. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  8719. else
  8720. 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)));
  8721. if (image)
  8722. {
  8723. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  8724. const int cols = 6, rows = 40;
  8725. i32 (0), i32 (0), i32 (1), i32 (0);
  8726. f64 (-1.0), f64 (-1.0), f64 (0.0);
  8727. i32 (cols), i32 (rows);
  8728. i32 (cols * rows);
  8729. for (int c2 = 0; c2 < cols; ++c2)
  8730. for (int r2 = 0; r2 < rows; ++r2)
  8731. {
  8732. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  8733. std::uint32_t u;
  8734. std::memcpy (&u, &v, 4);
  8735. i32 ((std::int32_t) u);
  8736. }
  8737. i32 (cols * rows);
  8738. for (int c2 = 0; c2 < cols; ++c2)
  8739. for (int r2 = 0; r2 < rows; ++r2)
  8740. {
  8741. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  8742. std::uint32_t u;
  8743. std::memcpy (&u, &v, 4);
  8744. i32 ((std::int32_t) u);
  8745. }
  8746. i32 (0);
  8747. b.push_back (0); // (file name: empty)
  8748. }
  8749. std::swap (b, part);
  8750. i32 ((std::int32_t) part.size());
  8751. b.insert (b.end(), part.begin(), part.end());
  8752. std::array<std::int32_t, 53> g {};
  8753. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  8754. for (auto v : g)
  8755. i32 (v);
  8756. for (int i = 0; i < 6; ++i)
  8757. i32 (i);
  8758. std::array<std::uint8_t, 0x3d> gm {};
  8759. const std::int32_t cr = 200, vr = 1;
  8760. std::memcpy (gm.data() + 12, &vr, 4);
  8761. std::memcpy (gm.data() + 24, &cr, 4);
  8762. b.insert (b.end(), gm.begin(), gm.end());
  8763. return b;
  8764. }
  8765. } // namespace
  8766. TEST (harmor_engine)
  8767. {
  8768. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  8769. const auto blob = harmorTestState();
  8770. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  8771. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8772. CHECK (lynx::harmorStateUsable (blob));
  8773. CHECK (lynx::harmorFallbacks (blob).empty());
  8774. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  8775. std::int32_t cutoff = 0;
  8776. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  8777. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  8778. auto broken = blob;
  8779. broken.resize (broken.size() / 2);
  8780. CHECK (! lynx::harmorStateUsable (broken));
  8781. auto old = blob;
  8782. old[0] = 15; // articulators and curves are stored from version 16
  8783. CHECK (! lynx::harmorStateUsable (old));
  8784. lynx::ChannelDef c;
  8785. c.instrument = lynx::InstrumentKind::harmor;
  8786. c.pluginState = blob;
  8787. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  8788. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  8789. inst->configure (def, 120.0);
  8790. inst->prepare (44100.0);
  8791. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8792. inst->noteOn (60, 0.78125f);
  8793. for (int s = 0; s < total; s += 512)
  8794. {
  8795. if (s == offAt)
  8796. inst->noteOff (60);
  8797. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8798. }
  8799. return std::make_tuple (l, r, std::move (inst));
  8800. };
  8801. {
  8802. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  8803. // Mersenne Twister seeded like LYNX's; since flbeta165h LYNX's own values: the test state plays at precision
  8804. // "Average", whose IPP 11-bit Exp/Ln — REA_rodada165g-flbeta165h §3 — the model computes exactly)
  8805. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8806. if (std::getenv ("LYNX_PRINT_HARMOR_ENGINE") != nullptr)
  8807. for (int i : { 0, 100, 1000, 3000, 6000, 7000, 8191 })
  8808. std::printf ("harmor engine %d %.8g %.8g\n", i, (double) l[(std::size_t) i], (double) r[(std::size_t) i]);
  8809. const std::tuple<int, float, float> golden[] {
  8810. { 0, 0.0f, 0.0f }, { 100, 0.0094771842f, 0.042498328f }, { 1000, 0.081412978f, -0.032517102f }, { 3000, 0.059289735f, -0.0024759346f },
  8811. { 6000, 0.029978603f, 0.14549156f }, { 7000, -0.011218854f, 0.041979425f }, { 8191, 0.0031455588f, 0.03857585f } };
  8812. for (const auto& [i, gl, gr] : golden)
  8813. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8814. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  8815. std::vector<float> tail (44100), tailR (tail.size());
  8816. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8817. CHECK (inst->idle());
  8818. CHECK (std::abs (tail.back()) == 0.0f);
  8819. }
  8820. // the reference renderer gives the same samples as the instrument
  8821. {
  8822. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  8823. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8824. double worst = 0.0;
  8825. for (int i = 0; i < 8192; ++i)
  8826. 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]) });
  8827. CHECK (worst == 0.0);
  8828. }
  8829. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  8830. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  8831. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  8832. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  8833. auto mono = c;
  8834. mono.pluginState = harmorTestState (false); // no pan LFO ...
  8835. const std::int32_t one = 1;
  8836. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  8837. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  8838. double el = 0.0, er = 0.0;
  8839. for (int i = 0; i < 2048; ++i)
  8840. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8841. CHECK (el < 1e-10 * er && er > 1e-3);
  8842. }
  8843. namespace
  8844. {
  8845. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  8846. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  8847. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  8848. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  8849. std::vector<std::uint8_t> ogunTestState()
  8850. {
  8851. std::vector<std::uint8_t> b;
  8852. 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))); };
  8853. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  8854. 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))); };
  8855. struct Pt { double dx, y, tension; int mode; };
  8856. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8857. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  8858. for (const auto& p : pts)
  8859. {
  8860. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  8861. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8862. }
  8863. for (auto h : hdr)
  8864. i32 (h);
  8865. };
  8866. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8867. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  8868. std::array<std::int32_t, 64> P {};
  8869. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  8870. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  8871. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  8872. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  8873. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  8874. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  8875. { 56, 0x00010000 }, // +0x20e: declick
  8876. { 62, 0x00000100 } }; // +0x225: normalise
  8877. for (const auto& [i, v] : values)
  8878. P[(std::size_t) i] = v;
  8879. for (auto v : P)
  8880. i32 (v);
  8881. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  8882. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  8883. b.push_back (0); // comment: 0 characters
  8884. for (int k = 0; k < 13; ++k)
  8885. {
  8886. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  8887. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  8888. i32 (1);
  8889. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  8890. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  8891. 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 });
  8892. else
  8893. flat (1, neutral);
  8894. for (int a : { 128, 128, 128, 128 })
  8895. i32 (a);
  8896. if (k == 1) // cutoff LFO
  8897. curve (2, { { 0, 0.8, 0, 0 } }, on);
  8898. else
  8899. flat (2, 0.5);
  8900. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  8901. i32 (a);
  8902. i32 (0);
  8903. for (int m = 0; m < 4; ++m)
  8904. if (k == 0 && m == 1)
  8905. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  8906. else
  8907. flat (3, neutral);
  8908. }
  8909. flat (3, 0.0); // Forced: none
  8910. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  8911. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  8912. flat (3, 0.5); // Release EQ
  8913. // u32 version + zlib (stored blocks)
  8914. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  8915. for (std::size_t i = 0; i < b.size();)
  8916. {
  8917. const auto len = std::min<std::size_t> (65535, b.size() - i);
  8918. s.push_back (i + len >= b.size() ? 1 : 0);
  8919. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  8920. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  8921. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  8922. i += len;
  8923. }
  8924. std::uint32_t a1 = 1, a2 = 0;
  8925. for (auto c : b)
  8926. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  8927. for (int sh = 24; sh >= 0; sh -= 8)
  8928. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  8929. return s;
  8930. }
  8931. } // namespace
  8932. TEST (ogun_engine)
  8933. {
  8934. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  8935. const auto blob = ogunTestState();
  8936. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  8937. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8938. CHECK (lynx::ogunStateUsable (blob));
  8939. CHECK (lynx::ogunFallbacks (blob).empty());
  8940. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, lynx::ogunDelayOnIndex + 1));
  8941. auto broken = blob;
  8942. broken.resize (broken.size() - 300);
  8943. CHECK (! lynx::ogunStateUsable (broken));
  8944. auto old = blob;
  8945. old[0] = 13; // a version-18 body read as version 13 (a u32 comment count; versions 10…13: Gen169lTests)
  8946. CHECK (! lynx::ogunStateUsable (old));
  8947. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  8948. // released at 8192; host block 512)
  8949. 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 } };
  8950. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  8951. CHECK (ref.size() == 2 * 16384);
  8952. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  8953. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  8954. const std::tuple<int, float, float> golden[] {
  8955. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  8956. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  8957. { 16383, -0.00465311f, -0.00119627f } };
  8958. for (const auto& [i, gl, gr] : golden)
  8959. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  8960. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  8961. lynx::ChannelDef c;
  8962. c.instrument = lynx::InstrumentKind::ogun;
  8963. c.pluginState = blob;
  8964. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  8965. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  8966. inst->configure (def, 120.0);
  8967. inst->prepare (44100.0);
  8968. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8969. for (int s = 0; s < total; s += 512)
  8970. {
  8971. if (s == 0)
  8972. inst->noteOn (60, 0.75f);
  8973. if (s == 2048)
  8974. inst->noteOn (67, 0.625f);
  8975. if (s == 6144 && ! held)
  8976. inst->noteOff (60);
  8977. if (s == 8192 && ! held)
  8978. inst->noteOff (67);
  8979. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8980. }
  8981. return std::make_tuple (l, r, std::move (inst));
  8982. };
  8983. {
  8984. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  8985. double worst = 0.0;
  8986. for (int i = 0; i < 16384; ++i)
  8987. 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]) });
  8988. CHECK (worst == 0.0);
  8989. // the voices end and the effect tails die out: the instrument goes idle
  8990. std::vector<float> tail (44100), tailR (tail.size());
  8991. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  8992. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8993. CHECK (inst->idle());
  8994. }
  8995. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  8996. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  8997. {
  8998. auto dry = c;
  8999. for (int i : { 19, 29, 40, 53 })
  9000. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  9001. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  9002. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  9003. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  9004. double worst = 0.0, peak = 0.0;
  9005. for (int i = 30000; i < 32768; ++i)
  9006. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  9007. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  9008. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  9009. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  9010. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  9011. const auto V = lynx::channelGain (0.78125f);
  9012. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  9013. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  9014. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  9015. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  9016. double ei = 0.0, er0 = 0.0;
  9017. for (int i = 30000; i < 32768; ++i)
  9018. 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];
  9019. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  9020. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  9021. double el = 0.0, er = 0.0;
  9022. for (int i = 0; i < 4096; ++i)
  9023. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  9024. CHECK (el < 1e-10 * er && er > 1e-3);
  9025. }
  9026. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  9027. {
  9028. lynx::setOfflineRendering (true);
  9029. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  9030. lynx::setOfflineRendering (false);
  9031. lynx::ogun::RefOptions q4;
  9032. q4.quality = 4;
  9033. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  9034. double worst = 0.0, diff = 0.0;
  9035. for (int i = 0; i < 8192; ++i)
  9036. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  9037. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  9038. CHECK (worst == 0.0 && diff > 1e-3);
  9039. const std::tuple<int, float, float> golden4[] {
  9040. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  9041. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  9042. for (const auto& [i, gl, gr] : golden4)
  9043. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  9044. }
  9045. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  9046. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  9047. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  9048. {
  9049. struct Sample { int at; float l, r; };
  9050. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  9051. const Variant variants[] {
  9052. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  9053. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  9054. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  9055. { "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 } } },
  9056. { "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 } } },
  9057. { "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 } } },
  9058. { "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 } } },
  9059. { "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 } } },
  9060. { "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 } } },
  9061. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  9062. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  9063. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  9064. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  9065. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  9066. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  9067. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  9068. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  9069. };
  9070. for (const auto& v : variants)
  9071. {
  9072. auto st = blob;
  9073. for (const auto& [i, x] : v.params)
  9074. CHECK (lynx::ogunSetParam (st, i, x));
  9075. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  9076. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  9077. double el = 0.0, er = 0.0, peak = 0.0;
  9078. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9079. 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]) });
  9080. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  9081. for (const auto& smp : v.s)
  9082. 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;
  9083. if (! ok)
  9084. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  9085. CHECK (ok);
  9086. }
  9087. }
  9088. }
  9089. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  9090. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  9091. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  9092. {
  9093. std::vector<std::uint8_t> d;
  9094. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  9095. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  9096. i32 (7);
  9097. for (int copy = 0; copy < 2; ++copy)
  9098. {
  9099. for (auto v : g)
  9100. i32 (v);
  9101. for (const auto& b : bands)
  9102. for (auto v : b)
  9103. i32 (v);
  9104. }
  9105. d.resize (0x1fe, 0);
  9106. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  9107. for (const auto& e : envs)
  9108. {
  9109. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  9110. for (const auto& pt : e)
  9111. {
  9112. const double dx = pt[0], y = pt[1];
  9113. const float t = (float) pt[2];
  9114. put (&dx, 8), put (&y, 8), put (&t, 4);
  9115. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  9116. put (flags, 4);
  9117. }
  9118. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  9119. }
  9120. return d;
  9121. }
  9122. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  9123. std::vector<std::uint8_t> keplerTestState()
  9124. {
  9125. 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 };
  9126. std::vector<std::uint8_t> st;
  9127. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  9128. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  9129. u32 (0), u32 (0), u32 (0), u32 (9);
  9130. for (char c : std::string ("Kepler-1"))
  9131. st.push_back ((std::uint8_t) c);
  9132. st.push_back (0);
  9133. u32 (2), u32 (34);
  9134. for (auto f : panel)
  9135. f32 (f);
  9136. st.push_back (0);
  9137. f32 (1.0f), f32 (0.5f);
  9138. const auto size = (std::uint32_t) st.size() - 12;
  9139. std::memcpy (st.data() + 4, &size, 4);
  9140. return st;
  9141. }
  9142. std::string keplerDll()
  9143. {
  9144. if (realFlpDir.empty())
  9145. return {};
  9146. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  9147. }
  9148. TEST (kepler_engine)
  9149. {
  9150. const auto st = keplerTestState();
  9151. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  9152. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  9153. auto broken = st;
  9154. broken[17] = 'X'; // not "Kepler-1"
  9155. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  9156. auto v3 = st;
  9157. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  9158. CHECK (! lynx::keplerStateUsable (v3));
  9159. const auto dll = keplerDll();
  9160. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9161. return; // no FL installation (the engine needs the user's own DLL tables)
  9162. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  9163. 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 } };
  9164. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  9165. CHECK (y.size() == 2 * 39690);
  9166. if (y.size() != 2 * 39690)
  9167. return;
  9168. double el = 0.0, er = 0.0;
  9169. for (std::size_t i = 0; i < y.size(); i += 2)
  9170. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9171. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  9172. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  9173. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  9174. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  9175. for (const auto& [i, l, r] : golden)
  9176. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  9177. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  9178. lynx::ChannelDef c;
  9179. c.instrument = lynx::InstrumentKind::kepler;
  9180. c.pluginState = st;
  9181. c.samplePath = dll;
  9182. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  9183. inst->configure (c, 120.0);
  9184. inst->prepare (44100.0);
  9185. std::vector<float> l (4096), r (4096);
  9186. inst->noteOn (60, 0.8f);
  9187. double e = 0.0;
  9188. bool finite = true;
  9189. for (int b = 0; b < 8; ++b)
  9190. {
  9191. std::fill (l.begin(), l.end(), 0.0f);
  9192. std::fill (r.begin(), r.end(), 0.0f);
  9193. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9194. for (int i = 0; i < 4096; ++i)
  9195. 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];
  9196. if (b == 3)
  9197. inst->noteOff (60);
  9198. }
  9199. CHECK (finite && e > 1.0);
  9200. for (int b = 0; b < 40; ++b)
  9201. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9202. CHECK (inst->idle());
  9203. }
  9204. // REA round 57 §2: Transistor Bass
  9205. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  9206. std::vector<std::uint8_t> tbassPresetState (const char* name)
  9207. {
  9208. if (realFlpDir.empty())
  9209. return {};
  9210. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  9211. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9212. std::size_t i = 0;
  9213. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9214. ++i;
  9215. i += 8;
  9216. while (i < b.size())
  9217. {
  9218. const auto id = b[i++];
  9219. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9220. if (id >= 192)
  9221. for (int shift = 0; i < b.size(); shift += 7)
  9222. {
  9223. const auto c = b[i++];
  9224. n |= (std::size_t) (c & 0x7F) << shift;
  9225. if (c < 0x80)
  9226. break;
  9227. }
  9228. if (id == 0xD5 && i + n <= b.size())
  9229. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9230. i += n;
  9231. }
  9232. return {};
  9233. }
  9234. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  9235. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  9236. {
  9237. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  9238. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  9239. for (std::size_t at = 4; at + 12 <= s.size();)
  9240. {
  9241. std::uint32_t id;
  9242. std::uint64_t n;
  9243. std::memcpy (&id, s.data() + at, 4);
  9244. std::memcpy (&n, s.data() + at + 4, 8);
  9245. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  9246. at += 12 + (std::size_t) n;
  9247. if (id == 2)
  9248. {
  9249. std::uint64_t m;
  9250. std::memcpy (&m, d.data() + 4, 8);
  9251. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  9252. std::int16_t v;
  9253. std::memcpy (&v, e.data(), 2);
  9254. if (v < 5)
  9255. {
  9256. v = 5;
  9257. std::memcpy (e.data(), &v, 2);
  9258. e.insert (e.end(), 4, 0);
  9259. }
  9260. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  9261. m = e.size();
  9262. d.resize (12);
  9263. std::memcpy (d.data() + 4, &m, 8);
  9264. d.insert (d.end(), e.begin(), e.end());
  9265. }
  9266. const std::uint64_t size = d.size();
  9267. put (&id, 4);
  9268. put (&size, 8);
  9269. put (d.data(), d.size());
  9270. }
  9271. return out;
  9272. }
  9273. std::string tbassDll()
  9274. {
  9275. if (realFlpDir.empty())
  9276. return {};
  9277. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  9278. }
  9279. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9280. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  9281. TEST (plucked_engine)
  9282. {
  9283. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  9284. std::vector<std::uint8_t> st (20);
  9285. std::memcpy (st.data(), std::data (k), 20);
  9286. return st;
  9287. };
  9288. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  9289. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  9290. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  9291. using N = lynx::plucked::RefNote;
  9292. struct Sample { int at; float l, r; };
  9293. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  9294. const std::vector<std::vector<N>> sets {
  9295. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  9296. { { 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 } },
  9297. { { 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 } },
  9298. { { 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 } },
  9299. { { 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 } },
  9300. };
  9301. const Golden goldens[] {
  9302. { 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 } } },
  9303. { 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 } } },
  9304. { 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 } } },
  9305. { 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 } } },
  9306. { 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 } } },
  9307. };
  9308. for (const auto& g : goldens)
  9309. {
  9310. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  9311. const auto length = (int) g.fs * 2;
  9312. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  9313. g.law != 0, g.maxPoly);
  9314. double el = 0.0, er = 0.0;
  9315. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9316. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9317. 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;
  9318. for (const auto& smp : g.s)
  9319. 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;
  9320. if (! ok)
  9321. 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);
  9322. CHECK (ok);
  9323. }
  9324. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  9325. lynx::ChannelDef c;
  9326. c.instrument = lynx::InstrumentKind::plucked;
  9327. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  9328. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  9329. inst->configure (c, 120.0);
  9330. inst->prepare (44100.0);
  9331. std::vector<float> l (1024), r (1024);
  9332. inst->noteOn (57, 0.8f);
  9333. double e = 0.0;
  9334. for (int b = 0; b < 8; ++b)
  9335. {
  9336. std::fill (l.begin(), l.end(), 0.0f);
  9337. std::fill (r.begin(), r.end(), 0.0f);
  9338. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9339. for (int i = 0; i < 1024; ++i)
  9340. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9341. }
  9342. CHECK (e > 1.0 && ! inst->idle());
  9343. inst->noteOff (57);
  9344. for (int b = 0; b < 4; ++b)
  9345. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9346. CHECK (inst->idle());
  9347. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  9348. inst->configure (c, 120.0);
  9349. inst->noteOn (60, 1.0f);
  9350. inst->noteOff (60); // (no gate: the string rings on)
  9351. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  9352. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9353. CHECK (inst->idle());
  9354. }
  9355. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9356. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  9357. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  9358. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  9359. {
  9360. constexpr int W = 9, H = 6;
  9361. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  9362. 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)); };
  9363. img[0] = 'B', img[1] = 'M';
  9364. 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);
  9365. put (34, 4 * W * H, 4);
  9366. for (int y = 0; y < H; ++y) // bottom-up rows
  9367. for (int x = 0; x < W; ++x)
  9368. {
  9369. const auto yy = H - 1 - y;
  9370. auto* px = img.data() + 54 + 4 * (y * W + x);
  9371. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  9372. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  9373. }
  9374. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  9375. std::size_t k = 0;
  9376. for (auto v : settings)
  9377. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  9378. const auto len = (std::int32_t) img.size();
  9379. std::memcpy (st.data() + 64, &len, 4);
  9380. st.insert (st.end(), img.begin(), img.end());
  9381. return st;
  9382. }
  9383. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  9384. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  9385. {
  9386. if (realFlpDir.empty())
  9387. return {};
  9388. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  9389. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9390. std::size_t i = 0;
  9391. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9392. ++i;
  9393. i += 8;
  9394. while (i < b.size())
  9395. {
  9396. const auto id = b[i++];
  9397. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9398. if (id >= 192)
  9399. for (int shift = 0; i < b.size(); shift += 7)
  9400. {
  9401. const auto c = b[i++];
  9402. n |= (std::size_t) (c & 0x7F) << shift;
  9403. if (c < 0x80)
  9404. break;
  9405. }
  9406. if (id == 0xD5 && i + n <= b.size())
  9407. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9408. i += n;
  9409. }
  9410. return {};
  9411. }
  9412. TEST (beepmap_engine)
  9413. {
  9414. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  9415. CHECK (lynx::beepMapStateUsable (synthA));
  9416. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  9417. auto broken = synthA;
  9418. broken[0] = 2; // versions above 1 are refused
  9419. CHECK (! lynx::beepMapStateUsable (broken));
  9420. broken = synthA;
  9421. broken.resize (broken.size() - 20); // truncated bitmap
  9422. CHECK (! lynx::beepMapStateUsable (broken));
  9423. using N = lynx::beepmap::RefNote;
  9424. struct Sample { int at; float l, r; };
  9425. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  9426. std::vector<std::vector<std::uint8_t>> states {
  9427. synthA,
  9428. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9429. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9430. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9431. beepMapPresetState ("Default.fst"),
  9432. beepMapPresetState ("Voice.fst"),
  9433. };
  9434. const std::vector<std::vector<N>> sets {
  9435. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9436. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  9437. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  9438. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  9439. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9440. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  9441. };
  9442. const Golden goldens[] {
  9443. { 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 } } },
  9444. { 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 } } },
  9445. { 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 } } },
  9446. { 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 } } },
  9447. { 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 } } },
  9448. { 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 } } },
  9449. };
  9450. for (const auto& g : goldens)
  9451. {
  9452. const auto& st = states[(std::size_t) g.set];
  9453. if (st.empty())
  9454. continue; // (no FL installation: the presets are skipped)
  9455. const auto length = (int) g.fs * 2;
  9456. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  9457. double el = 0.0, er = 0.0;
  9458. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9459. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9460. 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;
  9461. for (const auto& smp : g.s)
  9462. 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;
  9463. if (! ok)
  9464. 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);
  9465. CHECK (ok);
  9466. }
  9467. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  9468. lynx::ChannelDef c;
  9469. c.instrument = lynx::InstrumentKind::beepmap;
  9470. c.pluginState = states[1];
  9471. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  9472. inst->configure (c, 120.0);
  9473. inst->prepare (44100.0);
  9474. std::vector<float> l (1024), r (1024);
  9475. inst->noteOn (60, 0.8f);
  9476. double e = 0.0;
  9477. for (int b = 0; b < 8; ++b)
  9478. {
  9479. std::fill (l.begin(), l.end(), 0.0f);
  9480. std::fill (r.begin(), r.end(), 0.0f);
  9481. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9482. for (int i = 0; i < 1024; ++i)
  9483. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9484. }
  9485. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  9486. inst->noteOff (60);
  9487. for (int b = 0; b < 4; ++b)
  9488. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9489. CHECK (inst->idle());
  9490. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  9491. inst->configure (c, 120.0);
  9492. inst->noteOn (60, 1.0f);
  9493. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  9494. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9495. CHECK (inst->idle());
  9496. }
  9497. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  9498. namespace
  9499. {
  9500. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  9501. {
  9502. if (realFlpDir.empty())
  9503. return {};
  9504. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  9505. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9506. std::size_t i = 0;
  9507. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9508. ++i;
  9509. i += 8;
  9510. while (i < b.size())
  9511. {
  9512. const auto id = b[i++];
  9513. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9514. if (id >= 192)
  9515. for (int shift = 0; i < b.size(); shift += 7)
  9516. {
  9517. const auto c = b[i++];
  9518. n |= (std::size_t) (c & 0x7F) << shift;
  9519. if (c < 0x80)
  9520. break;
  9521. }
  9522. if (id == 0xD5 && i + n <= b.size())
  9523. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9524. i += n;
  9525. }
  9526. return {};
  9527. }
  9528. } // namespace
  9529. TEST (sawer_engine)
  9530. {
  9531. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  9532. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  9533. std::memcpy (synth.data(), "SSLF", 4);
  9534. const std::uint32_t size = 4 + 4 * 96;
  9535. std::memcpy (synth.data() + 4, &size, 4);
  9536. std::memcpy (synth.data() + 8, "1rwS", 4);
  9537. const float p27 = 0.75f;
  9538. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  9539. CHECK (lynx::sawerStateUsable (synth));
  9540. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  9541. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  9542. auto broken = synth;
  9543. broken[8] = 'X';
  9544. CHECK (! lynx::sawerStateUsable (broken));
  9545. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  9546. const auto dflt = sawerPresetState ("Default.fst");
  9547. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  9548. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  9549. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  9550. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  9551. {
  9552. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  9553. return;
  9554. }
  9555. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  9556. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  9557. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  9558. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  9559. auto syn = dflt;
  9560. 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 },
  9561. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  9562. syn = setP (syn, kv.first, kv.second);
  9563. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  9564. using N = lynx::sawer::RefNote;
  9565. struct Sample { int at; float l, r; };
  9566. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  9567. const Golden goldens[] {
  9568. { &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 } },
  9569. 44100, 512, 1172.343107, 1169.673445,
  9570. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  9571. { &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,
  9572. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  9573. { &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,
  9574. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  9575. };
  9576. for (const auto& g : goldens)
  9577. {
  9578. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  9579. CHECK (y.size() == 2 * 66150u);
  9580. if (y.size() != 2 * 66150u)
  9581. continue;
  9582. double el = 0.0, er = 0.0;
  9583. for (std::size_t i = 0; i < 66150; ++i)
  9584. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  9585. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  9586. for (const auto& s : g.s)
  9587. 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);
  9588. }
  9589. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  9590. lynx::ChannelDef c;
  9591. c.instrument = lynx::InstrumentKind::sawer;
  9592. c.pluginState = dflt;
  9593. c.samplePath = tablePath;
  9594. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  9595. inst->configure (c, 120.0);
  9596. inst->prepare (44100.0);
  9597. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  9598. for (int b = 0; b < 40; ++b)
  9599. {
  9600. if (b == 0)
  9601. inst->noteOn (60, 0.78125f);
  9602. if (b == 10)
  9603. inst->noteOff (60);
  9604. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  9605. }
  9606. 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,
  9607. 8192, 512, 120.0, 0, true);
  9608. double diff = 0.0, energy = 0.0;
  9609. for (int i = 0; i < 8192; ++i)
  9610. {
  9611. 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]) });
  9612. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  9613. }
  9614. CHECK (diff == 0.0 && energy > 1e-3);
  9615. CHECK (inst->idle());
  9616. }
  9617. TEST (transistorbass_engine)
  9618. {
  9619. const auto factory = tbassPresetState ("Factory programs.fst");
  9620. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  9621. if (factory.empty() || seqOff.empty())
  9622. return; // no FL installation
  9623. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  9624. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  9625. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  9626. auto broken = factory;
  9627. broken[0] = 3; // the wrapper refuses versions above 2
  9628. CHECK (! lynx::transistorBassStateUsable (broken));
  9629. auto truncated = factory;
  9630. truncated.resize (truncated.size() - 1000);
  9631. CHECK (! lynx::transistorBassStateUsable (truncated));
  9632. const auto dll = tbassDll();
  9633. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  9634. return;
  9635. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  9636. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  9637. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  9638. struct Sample { int at; float l, r; };
  9639. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  9640. const Golden goldens[] {
  9641. { "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 } } },
  9642. { "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 } } },
  9643. { "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 } } },
  9644. { "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 } } },
  9645. { "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 } } },
  9646. { "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 } } },
  9647. { "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 } } },
  9648. { "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 } } },
  9649. { "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 } } },
  9650. { "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 } } },
  9651. };
  9652. constexpr int length = 88200;
  9653. for (const auto& g : goldens)
  9654. {
  9655. const bool factoryBank = std::string (g.preset) == "Factory";
  9656. auto st = factoryBank ? factory : seqOff;
  9657. if (g.engine >= 0)
  9658. st = tbassWithEngine (st, g.engine);
  9659. std::vector<lynx::tbass::RefNote> notes;
  9660. if (factoryBank)
  9661. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  9662. else
  9663. {
  9664. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  9665. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  9666. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  9667. }
  9668. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  9669. double el = 0.0, er = 0.0, peak = 0.0;
  9670. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9671. 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]) });
  9672. 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;
  9673. for (const auto& smp : g.s)
  9674. 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;
  9675. if (! ok)
  9676. 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);
  9677. CHECK (ok);
  9678. }
  9679. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  9680. lynx::ChannelDef c;
  9681. c.instrument = lynx::InstrumentKind::transistorbass;
  9682. c.pluginState = factory;
  9683. c.samplePath = dll;
  9684. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  9685. inst->configure (c, 128.0);
  9686. inst->prepare (44100.0);
  9687. std::vector<float> l (4096), r (4096);
  9688. inst->noteOn (65, 0.8f);
  9689. double e = 0.0;
  9690. bool finite = true;
  9691. for (int b = 0; b < 24; ++b)
  9692. {
  9693. std::fill (l.begin(), l.end(), 0.0f);
  9694. std::fill (r.begin(), r.end(), 0.0f);
  9695. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9696. for (int i = 0; i < 4096; ++i)
  9697. 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];
  9698. if (b == 20)
  9699. inst->noteOff (65);
  9700. }
  9701. CHECK (finite && e > 1.0 && ! inst->idle());
  9702. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  9703. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9704. CHECK (inst->idle());
  9705. }
  9706. TEST (kepler_demo_channels)
  9707. {
  9708. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  9709. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  9710. const auto dll = keplerDll();
  9711. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9712. return;
  9713. struct Sample { int at; float l, r; };
  9714. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  9715. const Golden goldens[] {
  9716. { "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 } } },
  9717. { "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 } } },
  9718. { "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 } } },
  9719. { "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 } } },
  9720. };
  9721. int checked = 0;
  9722. for (const auto& g : goldens)
  9723. {
  9724. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9725. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9726. if (bytes.empty())
  9727. continue;
  9728. const flp::Channel* ch = nullptr;
  9729. const auto project = flp::parseProject (bytes);
  9730. for (const auto& c : project.channels)
  9731. if (c.pluginName == "Kepler" && c.name == g.channel)
  9732. ch = &c;
  9733. CHECK (ch != nullptr);
  9734. if (ch == nullptr)
  9735. continue;
  9736. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  9737. 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 },
  9738. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  9739. double el = 0.0, er = 0.0, peak = 0.0;
  9740. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9741. 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]) });
  9742. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9743. for (const auto& smp : g.s)
  9744. 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;
  9745. if (! ok)
  9746. 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);
  9747. CHECK (ok);
  9748. ++checked;
  9749. }
  9750. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  9751. CHECK (checked == (int) std::size (goldens));
  9752. }
  9753. TEST (ogun_demo_channels)
  9754. {
  9755. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  9756. // 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),
  9757. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  9758. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  9759. if (realFlpDir.empty())
  9760. return;
  9761. struct Sample { int at; float l, r; };
  9762. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9763. const Golden goldens[] {
  9764. { "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
  9765. { "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
  9766. { "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
  9767. { "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
  9768. { "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
  9769. { "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
  9770. { "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
  9771. { "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
  9772. { "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
  9773. { "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
  9774. { "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
  9775. { "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
  9776. { "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
  9777. { "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
  9778. { "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
  9779. { "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
  9780. { "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
  9781. { "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
  9782. { "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
  9783. { "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
  9784. { "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
  9785. { "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
  9786. { "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
  9787. { "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
  9788. { "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
  9789. { "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
  9790. { "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
  9791. { "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
  9792. { "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
  9793. { "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
  9794. { "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
  9795. { "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
  9796. { "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
  9797. { "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
  9798. { "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
  9799. { "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
  9800. { "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
  9801. { "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
  9802. { "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
  9803. { "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
  9804. };
  9805. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  9806. int checked = 0;
  9807. for (const auto& g : goldens)
  9808. {
  9809. if (! states.contains (g.file))
  9810. {
  9811. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9812. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9813. auto& list = states[g.file];
  9814. if (! bytes.empty())
  9815. for (const auto& c : flp::parseProject (bytes).channels)
  9816. if (c.pluginName == "Ogun")
  9817. list.push_back (c.pluginState);
  9818. }
  9819. const auto& list = states[g.file];
  9820. if (list.empty())
  9821. continue; // not this installation
  9822. CHECK ((int) list.size() > g.ordinal);
  9823. if ((int) list.size() <= g.ordinal)
  9824. continue;
  9825. const auto& st = list[(std::size_t) g.ordinal];
  9826. CHECK (st.size() == g.stateSize);
  9827. lynx::ogun::RefOptions opt;
  9828. opt.quality = g.export4 ? 4 : -1;
  9829. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9830. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9831. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9832. double el = 0.0, er = 0.0, peak = 0.0;
  9833. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9834. 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]) });
  9835. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9836. for (const auto& smp : g.s)
  9837. 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;
  9838. if (! ok)
  9839. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  9840. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  9841. CHECK (ok);
  9842. ++checked;
  9843. }
  9844. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  9845. CHECK (checked == (int) std::size (goldens));
  9846. }
  9847. TEST (autogun_template_channels)
  9848. {
  9849. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  9850. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  9851. // 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
  9852. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  9853. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  9854. if (realFlpDir.empty())
  9855. return;
  9856. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  9857. std::ifstream sgIn (sgPath, std::ios::binary);
  9858. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  9859. if (sgState.size() != 1342)
  9860. return; // not this installation
  9861. struct Sample { int at; float l, r; };
  9862. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9863. const Golden goldens[] {
  9864. { "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)
  9865. { "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)
  9866. { "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)
  9867. { "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)
  9868. { "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)
  9869. { "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)
  9870. { "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)
  9871. { "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)
  9872. { "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)
  9873. { "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)
  9874. };
  9875. int checked = 0;
  9876. for (const auto& g : goldens)
  9877. {
  9878. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  9879. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9880. if (bytes.empty())
  9881. continue;
  9882. std::vector<std::uint8_t> st;
  9883. for (const auto& c : flp::parseProject (bytes).channels)
  9884. if (c.pluginName == "Autogun" && st.empty())
  9885. st = c.pluginState;
  9886. CHECK (st.size() == g.stateSize);
  9887. CHECK (lynx::autogunStateUsable (st));
  9888. lynx::ogun::RefOptions opt;
  9889. opt.autogun = true;
  9890. opt.sgState = sgState;
  9891. opt.quality = g.export4 ? 4 : -1;
  9892. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9893. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9894. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9895. double el = 0.0, er = 0.0, peak = 0.0;
  9896. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9897. 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]) });
  9898. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9899. for (const auto& smp : g.s)
  9900. 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;
  9901. if (! ok)
  9902. 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);
  9903. CHECK (ok);
  9904. ++checked;
  9905. }
  9906. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  9907. CHECK (checked == (int) std::size (goldens));
  9908. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  9909. {
  9910. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  9911. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9912. lynx::ChannelDef c;
  9913. c.instrument = lynx::InstrumentKind::autogun;
  9914. for (const auto& ch : flp::parseProject (bytes).channels)
  9915. if (ch.pluginName == "Autogun")
  9916. c.pluginState = ch.pluginState;
  9917. c.samplePath = sgPath.string();
  9918. lynx::ogun::RefOptions opt;
  9919. opt.autogun = true;
  9920. opt.sgState = sgState;
  9921. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  9922. 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);
  9923. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  9924. inst->configure (c, 120.0);
  9925. inst->prepare (44100.0);
  9926. std::vector<float> l (16384), r (16384);
  9927. for (int s = 0; s < 16384; s += 512)
  9928. {
  9929. if (s == 0)
  9930. inst->noteOn (60, 0.75f);
  9931. if (s == 6144)
  9932. inst->noteOff (60);
  9933. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  9934. }
  9935. double worst = 0.0, peak = 0.0;
  9936. for (int i = 0; i < 16384; ++i)
  9937. 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]) }),
  9938. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9939. if (worst != 0.0)
  9940. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  9941. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  9942. std::vector<float> tail (44100), tailR (tail.size());
  9943. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  9944. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9945. CHECK (inst->idle());
  9946. }
  9947. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  9948. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  9949. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  9950. for (const auto& c : flp::parseProject (rtBytes).channels)
  9951. if (c.pluginName == "Autogun")
  9952. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  9953. }
  9954. TEST (harmless_rack)
  9955. {
  9956. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  9957. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  9958. std::vector<float> inL (8192), inR (8192);
  9959. for (int i = 0; i < 4096; ++i)
  9960. {
  9961. 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));
  9962. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9963. }
  9964. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  9965. lynx::hlfx::Rack rack;
  9966. std::array<int, 7> t {};
  9967. t[3] = compType;
  9968. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  9969. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  9970. for (int s = 0; s < total; s += 512)
  9971. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  9972. return std::make_pair (l, r);
  9973. };
  9974. {
  9975. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  9976. std::array<int, 100> p {};
  9977. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  9978. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  9979. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  9980. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  9981. { 8191, 0.50599128f, -0.43637198f } };
  9982. for (const auto& [i, gl, gr] : golden)
  9983. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9984. }
  9985. {
  9986. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  9987. std::array<int, 100> p {};
  9988. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  9989. lynx::hlfx::Rack rack;
  9990. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  9991. std::vector<float> l (16384), r (16384);
  9992. l[0] = 1.0f;
  9993. for (int s = 0; s < 16384; s += 512)
  9994. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  9995. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  9996. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  9997. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  9998. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  9999. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  10000. }
  10001. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  10002. using Env = std::vector<std::array<double, 4>>;
  10003. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10004. 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,
  10005. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  10006. {
  10007. // master brickwall at −0.22 dB only (bands off / compression off)
  10008. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  10009. { def, def, def, def, def, def, def, def }, 0);
  10010. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  10011. CHECK (preset != nullptr);
  10012. std::array<int, 100> p {};
  10013. p[98] = 1000;
  10014. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  10015. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  10016. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  10017. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  10018. { 4095, -0.12743522f, -0.15237485f } };
  10019. for (const auto& [i, gl, gr] : golden)
  10020. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10021. float peak = 0.0f;
  10022. for (int i = 2048; i < 4096; ++i)
  10023. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  10024. CHECK (peak < 0.9751f);
  10025. }
  10026. {
  10027. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  10028. // exponential tensions (type 0), release 2 on the master, band mix 600
  10029. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10030. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10031. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10032. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10033. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10034. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10035. { def, def, knee, def, t0, def, m0, def }, 1);
  10036. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  10037. CHECK (preset != nullptr);
  10038. std::array<int, 100> p {};
  10039. p[98] = 600;
  10040. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  10041. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  10042. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  10043. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  10044. { 4095, 0.10172445f, 0.050564945f } };
  10045. for (const auto& [i, gl, gr] : golden)
  10046. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10047. }
  10048. {
  10049. // an .fst wrapper around a state is found; unsupported states are rejected
  10050. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  10051. { def, def, def, def, def, def, def, def }, 0);
  10052. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  10053. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  10054. for (auto n = st.size(); ; n >>= 7)
  10055. {
  10056. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  10057. if (n < 0x80)
  10058. break;
  10059. }
  10060. body.insert (body.end(), st.begin(), st.end());
  10061. const auto len = (std::uint32_t) body.size();
  10062. for (int k = 0; k < 4; ++k)
  10063. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  10064. fst.insert (fst.end(), body.begin(), body.end());
  10065. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  10066. CHECK (found && *found == st);
  10067. auto rms = st;
  10068. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  10069. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  10070. auto masterMid = st;
  10071. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  10072. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  10073. }
  10074. }
  10075. TEST (fruity_delay_bank_engine)
  10076. {
  10077. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  10078. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  10079. lynx::ScopedFlushDenormals ftz;
  10080. struct Bank
  10081. {
  10082. std::array<int, 16> ctl;
  10083. lynx::fldbank::BankOptions opt;
  10084. };
  10085. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  10086. auto s = lynx::fldbank::defaultState();
  10087. for (int i = 0; i < 4; ++i)
  10088. s.p[(std::size_t) i] = globals[(std::size_t) i];
  10089. s.p[132] = oversampling;
  10090. for (auto& [b, k] : banks)
  10091. {
  10092. for (int c = 0; c < 16; ++c)
  10093. if (16 * b + 4 + c < lynx::fldbank::automatable)
  10094. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  10095. s.bank[(std::size_t) b] = k.opt;
  10096. s.bank[(std::size_t) b].enabled = true;
  10097. }
  10098. return s;
  10099. };
  10100. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  10101. lynx::fldbank::BankOptions o;
  10102. o.filterType[0] = f1;
  10103. o.filterOrder[0] = o1;
  10104. o.filterType[1] = f2;
  10105. o.filterOrder[1] = o2;
  10106. o.filterAfter = after;
  10107. o.mode = mode;
  10108. o.steps = steps;
  10109. o.amount = amount;
  10110. o.tapBeforeFeedback = tap;
  10111. o.sync = sync;
  10112. return o;
  10113. };
  10114. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  10115. std::vector<float> l (4096), r (4096);
  10116. for (int i = 0; i < 4096; ++i)
  10117. {
  10118. 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));
  10119. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  10120. }
  10121. lynx::fldbank::Engine e (s, rate, bpm, block);
  10122. for (int i = 0; i < 4096; i += block)
  10123. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  10124. return std::make_pair (l, r);
  10125. };
  10126. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10127. for (const auto& [i, gl, gr] : golden)
  10128. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  10129. };
  10130. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  10131. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  10132. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  10133. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  10134. const auto a = build ({ 100, 128, 120, 110 },
  10135. { { 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) } },
  10136. { 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) } },
  10137. { 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) } } },
  10138. 0);
  10139. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  10140. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  10141. { 4095, 0.0093755126f, 0.241272509f } });
  10142. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  10143. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  10144. const auto b = build ({ 0, 128, 128, 128 },
  10145. { { 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) } },
  10146. { 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) } } },
  10147. 1);
  10148. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  10149. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  10150. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  10151. auto bytes = lynx::fldbank::serialise (a);
  10152. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  10153. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  10154. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  10155. bytes[0] = 15;
  10156. const auto v15 = lynx::fldbank::parse (bytes);
  10157. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  10158. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  10159. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  10160. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  10161. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  10162. auto fx = lynx::makeEffect (*slot);
  10163. fx->prepare (44100.0);
  10164. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  10165. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10166. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  10167. }
  10168. TEST (fruity_multiband_engine)
  10169. {
  10170. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  10171. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  10172. std::vector<float> inL (4096), inR (4096);
  10173. for (int i = 0; i < 4096; ++i)
  10174. {
  10175. 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)
  10176. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  10177. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  10178. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  10179. }
  10180. auto multiband = [] (std::array<int, 28> c) {
  10181. std::vector<std::uint8_t> b;
  10182. auto put = [&b] (std::int32_t v) {
  10183. std::uint8_t q[4];
  10184. std::memcpy (q, &v, 4);
  10185. b.insert (b.end(), q, q + 4);
  10186. };
  10187. put (0);
  10188. for (auto v : c)
  10189. put (v);
  10190. put (0); // UI words of the state (not used by the engine)
  10191. put (50);
  10192. return b;
  10193. };
  10194. struct Out { std::vector<float> l, r; int latency = 0; };
  10195. auto run = [&] (const std::vector<std::uint8_t>& st) {
  10196. const auto s = lynx::flmb::parse (st);
  10197. CHECK (s.has_value());
  10198. lynx::EffectParams p {};
  10199. lynx::flmb::toParams (*s, p.data());
  10200. auto fx = lynx::flmb::make();
  10201. fx->prepare (44100.0);
  10202. Out o { inL, inR };
  10203. for (int s0 = 0; s0 < 4096; s0 += 512)
  10204. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  10205. o.latency = fx->latencySamples();
  10206. return o;
  10207. };
  10208. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10209. for (const auto& [i, gl, gr] : golden)
  10210. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10211. };
  10212. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  10213. 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 }));
  10214. CHECK (oa.latency == 0);
  10215. check (oa,
  10216. { { 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 } });
  10217. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  10218. 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 }));
  10219. CHECK (ob.latency == 52);
  10220. check (ob,
  10221. { { 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 } });
  10222. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  10223. 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 }));
  10224. CHECK (oc.latency == 52);
  10225. check (oc,
  10226. { { 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 } });
  10227. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  10228. // effect built with FL's state still uses the exact controls
  10229. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  10230. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  10231. const auto parsed = lynx::flmb::parse (st);
  10232. CHECK (parsed && parsed->c[4] == 15604);
  10233. lynx::EffectParams p {};
  10234. lynx::flmb::toParams (*parsed, p.data());
  10235. for (int i = 0; i < lynx::flmb::controls; ++i)
  10236. 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]);
  10237. lynx::EffectParams rounded = p;
  10238. for (auto& v : rounded)
  10239. v = std::round (v * 10000.0f) / 10000.0f;
  10240. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  10241. auto exact = lynx::flmb::make();
  10242. auto fromState = lynx::flmb::make (st);
  10243. exact->prepare (44100.0);
  10244. fromState->prepare (44100.0);
  10245. for (int s0 = 0; s0 < 4096; s0 += 512)
  10246. {
  10247. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  10248. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  10249. }
  10250. CHECK (l1 == l2 && r1 == r2);
  10251. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  10252. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  10253. // the FL import builds the exact effect with FL's state kept
  10254. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  10255. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  10256. // unknown versions are rejected
  10257. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  10258. }
  10259. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  10260. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  10261. {
  10262. std::vector<float> l (4096), r (4096);
  10263. for (int i = 0; i < 4096; ++i)
  10264. {
  10265. 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));
  10266. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10267. }
  10268. return { l, r };
  10269. }
  10270. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  10271. {
  10272. std::vector<std::uint8_t> d;
  10273. for (const auto w : words)
  10274. for (int b = 0; b < 4; ++b)
  10275. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10276. return d;
  10277. }
  10278. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  10279. {
  10280. auto [l, r] = stereoFxInput();
  10281. auto fx = lynx::makeEffect (slot);
  10282. fx->prepare (44100.0);
  10283. for (int s = 0; s < 4096; s += 512)
  10284. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  10285. for (const auto& [i, gl, gr] : golden)
  10286. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10287. }
  10288. TEST (stereo_shaper_engine)
  10289. {
  10290. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  10291. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  10292. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  10293. if (slot)
  10294. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  10295. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  10296. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  10297. CHECK (slot.has_value());
  10298. if (slot)
  10299. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  10300. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  10301. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  10302. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  10303. CHECK (st.has_value());
  10304. if (st)
  10305. {
  10306. lynx::EffectParams p {};
  10307. lynx::stereofx::stereoShaperToParams (*st, p.data());
  10308. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  10309. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  10310. && back.phase == -2496 && back.post == 1);
  10311. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  10312. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  10313. }
  10314. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  10315. }
  10316. TEST (frequency_splitter_engine)
  10317. {
  10318. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  10319. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  10320. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  10321. using Gold = std::vector<std::tuple<int, float, float>>;
  10322. struct Case
  10323. {
  10324. std::vector<int> words;
  10325. int mute, latency;
  10326. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  10327. };
  10328. const std::vector<Case> cases {
  10329. { { 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
  10330. { { 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
  10331. { { 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
  10332. { { 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
  10333. { { 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
  10334. { { 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
  10335. { { 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
  10336. };
  10337. auto [l, r] = stereoFxInput();
  10338. for (const auto& c : cases)
  10339. {
  10340. auto words = c.words;
  10341. words.insert (words.begin(), 4);
  10342. for (const int w : { 0, 0, 0, c.mute })
  10343. words.push_back (w);
  10344. std::vector<std::uint8_t> st;
  10345. for (const auto w : words)
  10346. for (int b = 0; b < 4; ++b)
  10347. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10348. st.push_back (0);
  10349. st.resize (st.size() + 24, 0);
  10350. const auto s = lynx::fsplit::parseState (st);
  10351. CHECK (s.has_value());
  10352. if (! s)
  10353. continue;
  10354. lynx::fsplit::Engine e;
  10355. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10356. e.setParameter (i, s->params[(std::size_t) i]);
  10357. e.prepare (44100.0);
  10358. e.reset (false);
  10359. std::array<std::vector<float>, 8> o;
  10360. for (auto& v : o)
  10361. v.assign (4096, 0.0f);
  10362. for (int b = 0; b < 4096; b += 512)
  10363. {
  10364. float* out[8];
  10365. for (int k = 0; k < 8; ++k)
  10366. out[k] = o[(std::size_t) k].data() + b;
  10367. e.process (l.data() + b, r.data() + b, out, 512);
  10368. }
  10369. CHECK (e.latencySamples() == c.latency);
  10370. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  10371. for (int k = 0; k < 4; ++k)
  10372. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  10373. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  10374. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  10375. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  10376. // Patcher bus = the bands sent to it
  10377. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  10378. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  10379. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  10380. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  10381. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  10382. if (slot && head.slot)
  10383. {
  10384. lynx::fsplit::Engine settled;
  10385. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10386. settled.setParameter (i, s->params[(std::size_t) i]);
  10387. settled.prepare (44100.0);
  10388. settled.reset (true);
  10389. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  10390. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  10391. settled.process (l.data(), r.data(), out, 4096);
  10392. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  10393. for (const auto* p : both)
  10394. {
  10395. auto fx = lynx::makeEffect (*p);
  10396. fx->prepare (44100.0);
  10397. auto ml = l, mr = r;
  10398. for (int b = 0; b < 4096; b += 300)
  10399. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  10400. const auto& wl = p == &*slot ? sl : hl;
  10401. const auto& wr = p == &*slot ? sr : hr;
  10402. CHECK (ml == wl && mr == wr);
  10403. CHECK (fx->latencySamples() == c.latency);
  10404. }
  10405. }
  10406. }
  10407. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  10408. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  10409. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  10410. }
  10411. TEST (stereo_enhancer_transient_spreader_engines)
  10412. {
  10413. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  10414. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  10415. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  10416. if (se)
  10417. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  10418. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  10419. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  10420. tpState.push_back (5);
  10421. for (const auto w : i32State ({ 0, 50 }))
  10422. tpState.push_back (w);
  10423. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  10424. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  10425. if (tp)
  10426. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  10427. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  10428. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  10429. spState.push_back (1);
  10430. spState.push_back (1);
  10431. auto sp = lynx::effectFromFl ("Spreader", spState);
  10432. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  10433. if (sp)
  10434. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  10435. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  10436. // unknown layouts keep the earlier fallback; FL automation maps one to one
  10437. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  10438. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  10439. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  10440. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  10441. }
  10442. TEST (fruity_dynamics_engines)
  10443. {
  10444. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  10445. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  10446. std::vector<float> inL (4096), inR (4096), side (4096);
  10447. for (int i = 0; i < 4096; ++i)
  10448. {
  10449. 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));
  10450. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10451. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  10452. }
  10453. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  10454. std::uint8_t q[4];
  10455. std::memcpy (q, &v, 4);
  10456. b.insert (b.end(), q, q + 4);
  10457. };
  10458. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  10459. std::vector<std::uint8_t> b;
  10460. put (b, 7);
  10461. for (auto v : p)
  10462. put (b, v);
  10463. b.resize (0xA4, 0);
  10464. put (b, k);
  10465. b.push_back ((std::uint8_t) exact);
  10466. return b;
  10467. };
  10468. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  10469. std::vector<std::uint8_t> b;
  10470. put (b, version);
  10471. for (auto v : p)
  10472. put (b, v);
  10473. put (b, flags);
  10474. if (version > 2)
  10475. put (b, k);
  10476. return b;
  10477. };
  10478. struct Out { std::vector<float> l, r; int latency = 0; };
  10479. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  10480. auto fx = lynx::makeEffect (slot);
  10481. fx->prepare (44100.0);
  10482. Out o { inL, inR };
  10483. for (int s = 0; s < 4096; s += 512)
  10484. {
  10485. if (sidechain)
  10486. fx->setSidechain (side.data() + s, side.data() + s);
  10487. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  10488. }
  10489. o.latency = fx->latencySamples();
  10490. return o;
  10491. };
  10492. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10493. for (const auto& [i, gl, gr] : golden)
  10494. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10495. };
  10496. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  10497. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  10498. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  10499. return *slot;
  10500. };
  10501. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  10502. 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);
  10503. CHECK (a.latency == 88);
  10504. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  10505. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  10506. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  10507. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  10508. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  10509. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  10510. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  10511. 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),
  10512. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  10513. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  10514. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  10515. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  10516. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  10517. return *slot;
  10518. };
  10519. // Vintage type, look-ahead flag, side input 1: bit-identical
  10520. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  10521. CHECK (ca.latency == 64);
  10522. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  10523. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  10524. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  10525. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  10526. CHECK (cb.latency == 0);
  10527. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  10528. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  10529. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  10530. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  10531. const auto parsed = lynx::fldyn::parseLimiter (st);
  10532. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  10533. const auto slot = limiter (st);
  10534. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  10535. {
  10536. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  10537. 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]);
  10538. }
  10539. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  10540. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  10541. // unreadable states keep the old approximation
  10542. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  10543. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  10544. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  10545. }
  10546. TEST (maximus_engine)
  10547. {
  10548. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  10549. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  10550. std::vector<float> inL (4096), inR (4096);
  10551. for (int i = 0; i < 4096; ++i)
  10552. {
  10553. 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));
  10554. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10555. }
  10556. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  10557. lynx::hlfx::MaximusProcessor m;
  10558. m.prepare (std::move (preset), 44100.0, mix);
  10559. std::vector<float> l (inL), r (inR);
  10560. for (int s = 0; s < 4096; s += 512)
  10561. m.process (l.data() + s, r.data() + s, 512);
  10562. return std::make_pair (l, r);
  10563. };
  10564. using Env = std::vector<std::array<double, 4>>;
  10565. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10566. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10567. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10568. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10569. 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,
  10570. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  10571. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10572. for (const auto& [i, gl, gr] : golden)
  10573. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  10574. };
  10575. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  10576. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  10577. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  10578. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  10579. { def, def, knee, def, t0, def, m0, def }, 0);
  10580. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  10581. CHECK (p3 != nullptr);
  10582. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  10583. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  10584. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  10585. auto solo = st3;
  10586. solo[0x1ef] = 1;
  10587. std::memset (solo.data() + 0x1e0, 0, 4);
  10588. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  10589. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  10590. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  10591. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  10592. auto fir = st3;
  10593. fir[0x1fd] = 1;
  10594. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  10595. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  10596. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  10597. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  10598. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  10599. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10600. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10601. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10602. { def, def, knee, def, t0, def, m0, def }, 0);
  10603. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  10604. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  10605. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  10606. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  10607. CHECK (p4 != nullptr);
  10608. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  10609. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  10610. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  10611. // the mixer effect's container and the effect itself
  10612. lynx::maximusfx::Blob blob;
  10613. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  10614. blob.mix = 600;
  10615. blob.state = v4;
  10616. const auto packed = lynx::maximusfx::pack (blob);
  10617. const auto back = lynx::maximusfx::unpack (packed);
  10618. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  10619. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  10620. slot.state = packed;
  10621. auto fx = lynx::makeEffect (slot);
  10622. fx->prepare (44100.0);
  10623. {
  10624. std::vector<float> l (inL), r (inR);
  10625. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  10626. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  10627. const auto ref = run (p4, 600);
  10628. CHECK (l == ref.first && r == ref.second);
  10629. }
  10630. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  10631. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  10632. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  10633. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  10634. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  10635. // without the preset files (no FL installation) the approximation keeps the plugin's state and its name
  10636. {
  10637. const auto approx = lynx::effectFromFl ("Soundgoodizer", sgState);
  10638. CHECK (approx && lynx::isSoundgoodizerApprox (*approx) && lynx::projectcontents::effectSlotName (*approx) == "Soundgoodizer");
  10639. const auto st = approx ? lynx::maximusfx::parseSoundgoodizer (approx->state) : std::nullopt;
  10640. CHECK (st && st->preset == 2 && st->amount == 632 && approx->branches.size() == 1 && approx->branches[0].chain.size() == 2);
  10641. CHECK (! lynx::isSoundgoodizerApprox (lynx::makeEffectSlot (lynx::EffectKind::parallel))); // a Patcher container stays one
  10642. const auto edited = lynx::soundgoodizerApproxSlot (0, 1000);
  10643. CHECK (lynx::isSoundgoodizerApprox (edited) && lynx::maximusfx::parseSoundgoodizer (edited.state)->preset == 0);
  10644. }
  10645. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  10646. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  10647. lynx::vocodex::InstallData data;
  10648. data.sgDat = st2;
  10649. data.carrierState = { 9, 9, 9 };
  10650. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  10651. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  10652. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  10653. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  10654. }
  10655. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  10656. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  10657. // on our own states and input; LYNX's engine is bit-identical to it.
  10658. TEST (reeverb2_engine)
  10659. {
  10660. auto hex = [] (const char* h) {
  10661. std::vector<std::uint8_t> v;
  10662. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10663. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10664. return v;
  10665. };
  10666. constexpr int N = 8192;
  10667. std::vector<float> inL (N), inR (N);
  10668. std::uint32_t seed = 12345;
  10669. for (int i = 0; i < N; ++i)
  10670. {
  10671. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10672. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10673. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10674. const float g = i < 4000 ? 1.0f : 0.0f;
  10675. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10676. inR[(std::size_t) i] = 0.25f * tri * g;
  10677. }
  10678. struct Golden
  10679. {
  10680. int i;
  10681. float l, r;
  10682. };
  10683. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  10684. const auto slot = lynx::reeverb2FromFl (state);
  10685. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  10686. if (! slot)
  10687. return;
  10688. auto fx = lynx::makeEffect (*slot);
  10689. fx->prepare (44100.0);
  10690. fx->reset();
  10691. std::vector<float> l (inL), r (inR);
  10692. for (int s = 0; s < N; s += 512)
  10693. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  10694. for (const auto& g : gold)
  10695. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10696. };
  10697. // modulation, early reflections, negative separation, filters, predelay
  10698. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  10699. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  10700. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  10701. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  10702. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  10703. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  10704. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  10705. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  10706. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  10707. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  10708. { 8191, 0.144686908f, 0.144686908f } });
  10709. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  10710. lynx::reeverb2::Settings s;
  10711. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  10712. s.tempoPredelay = true;
  10713. lynx::EffectParams p {};
  10714. lynx::reeverb2::toParams (s, p);
  10715. CHECK (lynx::reeverb2::fromParams (p) == s);
  10716. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  10717. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  10718. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  10719. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  10720. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  10721. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  10722. // plugin runs and as LYNX renders)
  10723. lynx::ScopedFlushDenormals ftz;
  10724. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10725. const auto slot = lynx::reeverb1FromFl (state);
  10726. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  10727. if (! slot)
  10728. return;
  10729. auto fx = lynx::makeEffect (*slot);
  10730. fx->prepare (44100.0);
  10731. std::vector<float> l (inL), r (inR);
  10732. for (int s = 0; s < N; s += 512)
  10733. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10734. for (const auto& g : gold)
  10735. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10736. };
  10737. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  10738. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  10739. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  10740. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  10741. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  10742. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  10743. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  10744. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  10745. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  10746. }
  10747. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  10748. TEST (love_philter_engine)
  10749. {
  10750. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  10751. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  10752. lynx::ScopedFlushDenormals ftz;
  10753. auto hex = [] (const char* h) {
  10754. std::vector<std::uint8_t> v;
  10755. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10756. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10757. return v;
  10758. };
  10759. constexpr int N = 8192;
  10760. std::vector<float> inL (N), inR (N);
  10761. std::uint32_t seed = 12345;
  10762. for (int i = 0; i < N; ++i)
  10763. {
  10764. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10765. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10766. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10767. const float g = i < 4000 ? 1.0f : 0.0f;
  10768. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10769. inR[(std::size_t) i] = 0.25f * tri * g;
  10770. }
  10771. struct Golden
  10772. {
  10773. int i;
  10774. float l, r;
  10775. };
  10776. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10777. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  10778. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  10779. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  10780. return;
  10781. auto fx = lynx::makeEffect (*slot);
  10782. fx->prepare (44100.0);
  10783. std::vector<float> l (inL), r (inR);
  10784. for (int s = 0; s < N; s += 512)
  10785. {
  10786. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10787. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10788. }
  10789. for (const auto& g : gold)
  10790. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10791. };
  10792. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  10793. run (hex (
  10794. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10795. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10796. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10797. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10798. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10799. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10800. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10801. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10802. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10803. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10804. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10805. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10806. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10807. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10808. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10809. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10810. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  10811. { { 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 } });
  10812. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  10813. run (hex (
  10814. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  10815. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  10816. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  10817. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  10818. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  10819. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  10820. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  10821. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  10822. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  10823. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  10824. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  10825. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  10826. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  10827. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  10828. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  10829. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  10830. "748f6cc07176c806dd2bbf0040771f82"),
  10831. { { 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 } });
  10832. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  10833. run (hex (
  10834. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  10835. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  10836. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  10837. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  10838. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  10839. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  10840. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  10841. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  10842. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  10843. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  10844. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  10845. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  10846. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  10847. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  10848. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  10849. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  10850. { { 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 } });
  10851. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  10852. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  10853. for (int i = 0; i < lynx::philter::paramCount; ++i)
  10854. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  10855. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == 48 && lynx::philter::paramOfFlIndex (8 + 12) == -1 && lynx::philter::paramOfFlIndex (8 + 44 * 7 + 11) == -1);
  10856. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  10857. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  10858. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  10859. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  10860. {
  10861. const auto stateA = hex (
  10862. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10863. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10864. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10865. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10866. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10867. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10868. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10869. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10870. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10871. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10872. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10873. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10874. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10875. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10876. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10877. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10878. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  10879. auto slot = lynx::philter::slotFromFl (stateA);
  10880. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  10881. if (slot)
  10882. {
  10883. auto render = [&] (const lynx::EffectSlot& sl) {
  10884. auto fx = lynx::makeEffect (sl);
  10885. fx->prepare (44100.0);
  10886. std::vector<float> l (inL), r (inR);
  10887. for (int s = 0; s < 4096; s += 512)
  10888. {
  10889. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10890. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  10891. }
  10892. return l[3333];
  10893. };
  10894. const auto base = render (*slot);
  10895. auto edited = *slot;
  10896. edited.params[8] = 0.1f; // unit 1 cutoff
  10897. CHECK (base == 0.0f || base != render (edited));
  10898. CHECK (base == render (*slot));
  10899. }
  10900. }
  10901. }
  10902. TEST (fruity_vocoder_round66_goldens)
  10903. {
  10904. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  10905. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  10906. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  10907. lynx::ScopedFlushDenormals ftz;
  10908. auto hex = [] (const char* h) {
  10909. std::vector<std::uint8_t> v;
  10910. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10911. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10912. return v;
  10913. };
  10914. constexpr int N = 8192;
  10915. std::vector<float> inL (N), inR (N);
  10916. std::uint32_t seed = 12345;
  10917. for (int i = 0; i < N; ++i)
  10918. {
  10919. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10920. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10921. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10922. const float g = i < 4000 ? 1.0f : 0.0f;
  10923. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10924. inR[(std::size_t) i] = 0.25f * tri * g;
  10925. }
  10926. struct Golden
  10927. {
  10928. int i;
  10929. float v;
  10930. };
  10931. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10932. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  10933. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  10934. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  10935. return;
  10936. auto fx = lynx::makeEffect (*slot);
  10937. fx->prepare (44100.0);
  10938. std::vector<float> l (inL), r (inR);
  10939. for (int s = 0; s < N; s += 512)
  10940. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10941. for (const auto& g : gold)
  10942. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  10943. };
  10944. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  10945. run (hex (
  10946. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  10947. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  10948. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  10949. "78000000050000002c010000000000008c0000003c000000a0000000"),
  10950. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  10951. // 8 bands, swapped inputs, per-band levels, modulator level 0
  10952. run (hex (
  10953. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  10954. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  10955. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  10956. }
  10957. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  10958. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  10959. namespace
  10960. {
  10961. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  10962. {
  10963. std::vector<std::uint8_t> b;
  10964. 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))); };
  10965. 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))); };
  10966. 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))); };
  10967. i32 (0x40003);
  10968. i32 (pre), i32 (mix), i32 (post);
  10969. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  10970. i32 (os);
  10971. 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 },
  10972. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  10973. i32 (3), i32 (3), i32 (5);
  10974. for (const auto& p : pts)
  10975. {
  10976. f64 (p.dt), f64 (p.y), f32 (p.t);
  10977. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  10978. }
  10979. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  10980. i32 (1);
  10981. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  10982. return b;
  10983. }
  10984. } // namespace
  10985. TEST (fruity_waveshaper_and_soft_clipper)
  10986. {
  10987. std::vector<float> inL (4096), inR (4096);
  10988. for (int n = 0; n < 4096; ++n)
  10989. {
  10990. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10991. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10992. }
  10993. struct Gold { int i; float l, r; };
  10994. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  10995. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  10996. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  10997. std::vector<float> l (inL), r (inR);
  10998. for (int s = 0; s < 4096; s += 512)
  10999. {
  11000. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  11001. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  11002. }
  11003. for (const auto& g : gold)
  11004. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11005. };
  11006. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  11007. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  11008. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  11009. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  11010. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  11011. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  11012. {
  11013. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  11014. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  11015. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  11016. if (slot)
  11017. {
  11018. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  11019. CHECK (s.threshold == 64 && s.post == 140);
  11020. auto fx = lynx::makeEffect (*slot);
  11021. fx->prepare (44100.0);
  11022. std::vector<float> l (inL), r (inR);
  11023. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  11024. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  11025. }
  11026. }
  11027. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  11028. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  11029. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  11030. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  11031. const auto pA = lynx::flshape::parseWaveShaper (stA);
  11032. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  11033. if (pA)
  11034. {
  11035. const auto T = lynx::flshape::shaperTable (pA->points, false);
  11036. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  11037. && T[16384] == 32767 && T[0] == T[1]);
  11038. const auto U = lynx::flshape::shaperTable (pA->points, true);
  11039. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  11040. }
  11041. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  11042. const auto s = lynx::flshape::parseWaveShaper (st);
  11043. CHECK (s.has_value());
  11044. if (! s)
  11045. return;
  11046. lynx::flshape::WaveShaperEngine e;
  11047. e.setCurve (s->points);
  11048. e.prepare (44100.0);
  11049. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  11050. std::vector<float> l (inL), r (inR);
  11051. for (int i = 0; i < 4096; i += 512)
  11052. e.process (l.data() + i, r.data() + i, 512);
  11053. for (const auto& g : gold)
  11054. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11055. };
  11056. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  11057. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  11058. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  11059. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  11060. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  11061. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  11062. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  11063. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  11064. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  11065. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  11066. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  11067. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  11068. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  11069. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  11070. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  11071. if (slot)
  11072. {
  11073. auto fx = lynx::makeEffect (*slot);
  11074. fx->prepare (44100.0);
  11075. std::vector<float> l (inL), r (inR);
  11076. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11077. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11078. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11079. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  11080. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  11081. }
  11082. // an unreadable state keeps the old approximation
  11083. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11084. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11085. }
  11086. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  11087. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  11088. namespace
  11089. {
  11090. struct PeqTestBand { int type, freq, width, gain, order; };
  11091. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  11092. {
  11093. std::vector<std::uint8_t> b;
  11094. 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))); };
  11095. i32 (8);
  11096. for (int copy = 0; copy < 2; ++copy)
  11097. {
  11098. for (const auto& x : bands) i32 (x.gain);
  11099. for (const auto& x : bands) i32 (x.freq);
  11100. for (const auto& x : bands) i32 (x.width);
  11101. for (const auto& x : bands) i32 (x.type);
  11102. for (const auto& x : bands) i32 (x.order);
  11103. i32 (main);
  11104. }
  11105. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  11106. b.push_back ((std::uint8_t) options);
  11107. i32 (mask), i32 (1), i32 (0);
  11108. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  11109. i32 (0);
  11110. return b;
  11111. }
  11112. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  11113. {
  11114. std::vector<std::uint8_t> b;
  11115. 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))); };
  11116. for (const auto& x : bands) i32 (x.gain);
  11117. for (const auto& x : bands) i32 (x.freq);
  11118. for (const auto& x : bands) i32 (x.width);
  11119. for (const auto& x : bands) i32 (x.type);
  11120. i32 (main);
  11121. return b;
  11122. }
  11123. } // namespace
  11124. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  11125. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  11126. {
  11127. std::vector<std::uint8_t> b;
  11128. 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); };
  11129. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  11130. auto putF = [&put] (float v) { put (&v, 4); };
  11131. std::uint32_t seed = 12345;
  11132. auto rnd = [&seed] {
  11133. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  11134. return (float) ((double) (seed % 20001) / 10000.0);
  11135. };
  11136. putI (4);
  11137. for (int k = 0; k < 93; ++k)
  11138. {
  11139. putF (rnd());
  11140. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  11141. }
  11142. for (int bank = 0; bank < 9; ++bank)
  11143. for (int k = 0; k < 93; ++k)
  11144. {
  11145. const float v = rnd();
  11146. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  11147. }
  11148. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  11149. putI (v);
  11150. for (int v : { 0, add ? 1 : 0, 0 })
  11151. b.push_back ((std::uint8_t) v);
  11152. putI (1);
  11153. return b;
  11154. }
  11155. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  11156. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  11157. {
  11158. std::vector<std::uint8_t> b;
  11159. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11160. putI (7);
  11161. for (int v : p)
  11162. putI (v);
  11163. putI (p[11]);
  11164. b.push_back (0);
  11165. putI (engine);
  11166. putI (detector);
  11167. putI (1000);
  11168. b.push_back (1);
  11169. b.push_back (0);
  11170. return b;
  11171. }
  11172. TEST (pitch_shifter_exact)
  11173. {
  11174. std::vector<float> inL (8192), inR (8192);
  11175. for (int n = 0; n < 8192; ++n)
  11176. {
  11177. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  11178. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  11179. }
  11180. struct Gold { int i; float l, r; };
  11181. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  11182. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  11183. lynx::psfl::State s;
  11184. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  11185. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  11186. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  11187. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  11188. if (! slot)
  11189. return;
  11190. auto fx = lynx::makeEffect (*slot);
  11191. fx->prepare (44100.0);
  11192. std::vector<float> l (inL), r (inR);
  11193. for (int i = 0, k = 0; i < 8192; ++k)
  11194. {
  11195. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11196. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11197. i += n;
  11198. }
  11199. CHECK (fx->latencySamples() == latency);
  11200. for (const auto& g : gold)
  11201. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11202. };
  11203. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  11204. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  11205. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  11206. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  11207. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  11208. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  11209. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  11210. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  11211. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  11212. }
  11213. TEST (equo_exact)
  11214. {
  11215. std::vector<float> inL (4096), inR (4096);
  11216. for (int n = 0; n < 4096; ++n)
  11217. {
  11218. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11219. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11220. }
  11221. struct Gold { int i; float l, r; };
  11222. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  11223. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  11224. const auto s = lynx::equofx::parse (st);
  11225. CHECK (s.has_value());
  11226. if (! s)
  11227. return;
  11228. lynx::equofx::Engine e;
  11229. e.prepare (44100.0, hq);
  11230. e.set (*s);
  11231. std::vector<float> l (inL), r (inR);
  11232. if (! ticks)
  11233. e.process (l.data(), r.data(), 4096);
  11234. else
  11235. {
  11236. auto k = *s;
  11237. e.startFromConstruction();
  11238. for (int i = 0; i < 4096; i += 256)
  11239. {
  11240. if (i == 2048)
  11241. k.knob[2] = 5300, e.setKnobs (k);
  11242. e.tick();
  11243. e.process (l.data() + i, r.data() + i, 256);
  11244. }
  11245. }
  11246. for (const auto& g : gold)
  11247. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11248. };
  11249. const auto plain = equoTestState (0, 150, false);
  11250. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  11251. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  11252. run (equoTestState (1, 300, true), false, false,
  11253. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  11254. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  11255. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  11256. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  11257. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  11258. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  11259. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  11260. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  11261. if (slot)
  11262. {
  11263. auto back = *lynx::equofx::parse (plain);
  11264. lynx::equofx::applyParams (slot->params, back);
  11265. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  11266. auto fx = lynx::makeEffect (*slot);
  11267. fx->prepare (44100.0);
  11268. std::vector<float> l (inL), r (inR);
  11269. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  11270. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  11271. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  11272. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  11273. lynx::equofx::Engine ref;
  11274. ref.prepare (44100.0, false);
  11275. ref.set (*lynx::equofx::parse (plain));
  11276. std::vector<float> rl (inL), rr (inR);
  11277. ref.process (rl.data(), rr.data(), 700);
  11278. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  11279. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  11280. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  11281. }
  11282. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  11283. }
  11284. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  11285. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  11286. {
  11287. std::vector<std::uint8_t> b;
  11288. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11289. putI (7);
  11290. putI (396);
  11291. std::uint64_t s = seed;
  11292. for (int i = 0; i < 384; ++i)
  11293. {
  11294. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11295. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  11296. }
  11297. for (int v : globals)
  11298. putI (v);
  11299. putI (0);
  11300. putI (3);
  11301. for (int v : { 1, 1, 0 })
  11302. b.push_back ((std::uint8_t) v);
  11303. b.insert (b.end(), 192, 0);
  11304. return b;
  11305. }
  11306. TEST (multiband_delay_exact)
  11307. {
  11308. std::vector<float> inL (8192), inR (8192);
  11309. for (int n = 0; n < 8192; ++n)
  11310. {
  11311. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  11312. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  11313. }
  11314. struct Gold { int i; float l, r; };
  11315. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  11316. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  11317. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  11318. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  11319. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  11320. if (! slot)
  11321. return;
  11322. auto fx = lynx::makeEffect (*slot);
  11323. fx->prepare (44100.0);
  11324. std::vector<float> l (inL), r (inR);
  11325. for (int i = 0, k = 0; i < 8192; ++k)
  11326. {
  11327. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11328. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11329. i += n;
  11330. }
  11331. CHECK (fx->latencySamples() == latency);
  11332. for (const auto& g : gold)
  11333. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  11334. };
  11335. // steep crossover, keep pitch, weighted feedback, compressor
  11336. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  11337. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  11338. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  11339. // gentle crossover, harmonic scale, delay ×0.1, saturation
  11340. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  11341. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  11342. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  11343. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  11344. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  11345. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  11346. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  11347. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  11348. const auto s = *lynx::mbdelay::parse (st);
  11349. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  11350. lynx::EffectParams p {};
  11351. lynx::mbdelay::toParams (s, p);
  11352. auto back = s;
  11353. lynx::mbdelay::applyParams (p, back);
  11354. CHECK (back.raw == s.raw);
  11355. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  11356. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  11357. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  11358. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  11359. }
  11360. TEST (fruity_parametric_eq_exact)
  11361. {
  11362. std::vector<float> inL (4096), inR (4096);
  11363. for (int n = 0; n < 4096; ++n)
  11364. {
  11365. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11366. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11367. }
  11368. struct Gold { int i; float l, r; };
  11369. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  11370. // low pass Gentle 6
  11371. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  11372. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  11373. { 1, 60000, 45000, 0, 2 } };
  11374. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  11375. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  11376. { 1, 60000, 45000, 0, 0 } };
  11377. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  11378. CHECK (s.has_value());
  11379. if (! s)
  11380. return;
  11381. lynx::peqfl::Engine e;
  11382. e.set (*s);
  11383. e.prepare (44100.0);
  11384. std::vector<float> l (inL), r (inR);
  11385. for (int i = 0; i < 4096; i += 512)
  11386. e.process (l.data() + i, r.data() + i, 512);
  11387. for (const auto& g : gold)
  11388. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11389. };
  11390. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  11391. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  11392. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  11393. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  11394. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  11395. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  11396. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  11397. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  11398. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  11399. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  11400. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  11401. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  11402. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  11403. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  11404. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  11405. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  11406. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  11407. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  11408. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  11409. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  11410. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  11411. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  11412. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  11413. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  11414. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  11415. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  11416. // is not replicated, so within 1e-6 of the DLL
  11417. for (const bool hq : { false, true })
  11418. {
  11419. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  11420. CHECK (s && s->linearPhase);
  11421. if (! s)
  11422. continue;
  11423. std::vector<float> l (12288), r (12288);
  11424. for (int n = 0; n < 12288; ++n)
  11425. {
  11426. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11427. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11428. }
  11429. lynx::peqfl::Engine e;
  11430. e.set (*s);
  11431. e.prepare (44100.0);
  11432. CHECK (e.latencySamples() == 1536);
  11433. for (int i = 0; i < 12288; i += 512)
  11434. e.process (l.data() + i, r.data() + i, 512);
  11435. // (named arrays: a std::initializer_list chosen by ?: would point into a temporary array that dies at the end of
  11436. // the declaration, which GCC 13 reuses for other locals)
  11437. static const Gold goldHq[] = {
  11438. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  11439. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  11440. { 12287, 0.40586185455322266f, -0.014698396436870098f } };
  11441. static const Gold goldPlain[] = {
  11442. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  11443. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  11444. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  11445. for (const auto& g : hq ? goldHq : goldPlain)
  11446. 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);
  11447. }
  11448. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  11449. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  11450. {
  11451. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  11452. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  11453. const auto slot = lynx::effectFromFl (name, st, 120.0);
  11454. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  11455. if (! slot)
  11456. continue;
  11457. const auto back = lynx::peqfl::fromParams (slot->params);
  11458. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  11459. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  11460. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  11461. auto fx = lynx::makeEffect (*slot);
  11462. fx->prepare (44100.0);
  11463. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  11464. std::vector<float> l (inL), r (inR);
  11465. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  11466. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  11467. if (v1)
  11468. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  11469. else
  11470. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  11471. // editor texts and graph
  11472. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  11473. == (v1 ? "12 dB/oct" : "Steep 8"));
  11474. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  11475. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  11476. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  11477. }
  11478. // a native LYNX EQ is untouched by the FL law
  11479. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  11480. }
  11481. TEST (fruity_panomatic_balance_blood_overdrive)
  11482. {
  11483. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  11484. const lynx::ScopedFlushDenormals ftz;
  11485. std::vector<float> inL (4096), inR (4096);
  11486. for (int n = 0; n < 4096; ++n)
  11487. {
  11488. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11489. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11490. }
  11491. struct Gold { int i; float l, r; };
  11492. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  11493. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  11494. for (const auto& g : gold)
  11495. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11496. };
  11497. auto st32 = [] (std::initializer_list<int> v) {
  11498. std::vector<std::uint8_t> b;
  11499. for (int x : v)
  11500. for (int k = 0; k < 4; ++k)
  11501. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  11502. return b;
  11503. };
  11504. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  11505. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  11506. lynx::flbasic::PanOMaticEngine e;
  11507. e.prepare (44100);
  11508. e.set (s);
  11509. if (prime)
  11510. e.prime();
  11511. std::vector<float> l (inL), r (inR);
  11512. for (int i = 0; i < 4096; i += 512)
  11513. e.process (l.data() + i, r.data() + i, 512);
  11514. check (l, r, gold);
  11515. };
  11516. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  11517. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  11518. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  11519. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  11520. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  11521. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  11522. // Balance: flushed and from silence
  11523. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  11524. lynx::flbasic::BalanceEngine e;
  11525. e.prepare (44100);
  11526. e.set (s);
  11527. if (flush)
  11528. e.flush();
  11529. std::vector<float> l (inL), r (inR);
  11530. for (int i = 0; i < 4096; i += 512)
  11531. e.process (l.data() + i, r.data() + i, 512);
  11532. check (l, r, gold);
  11533. };
  11534. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  11535. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  11536. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  11537. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  11538. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  11539. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  11540. lynx::flbasic::BloodOverdriveEngine e;
  11541. e.start (44100, { raw });
  11542. std::vector<float> l (inL), r (inR);
  11543. for (int i = 0; i < 4096; i += 512)
  11544. e.process (l.data() + i, r.data() + i, 512);
  11545. check (l, r, gold);
  11546. };
  11547. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  11548. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  11549. { 4095, 0.0402069278f, 0.0499244779f } });
  11550. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  11551. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11552. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  11553. CHECK (slot && slot->kind == kind);
  11554. if (! slot)
  11555. return;
  11556. auto fx = lynx::makeEffect (*slot);
  11557. fx->prepare (44100.0);
  11558. std::vector<float> l (inL), r (inR);
  11559. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11560. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11561. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11562. check (l, r, gold);
  11563. };
  11564. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  11565. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  11566. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  11567. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  11568. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  11569. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  11570. {
  11571. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  11572. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  11573. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  11574. if (slot)
  11575. {
  11576. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  11577. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  11578. }
  11579. }
  11580. // unreadable states keep the old approximations
  11581. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11582. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11583. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11584. }
  11585. TEST (fruity_fast_lp_free_filter_filter)
  11586. {
  11587. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  11588. // splits the host blocks below into calls of 512, as the DLL ran)
  11589. const lynx::ScopedFlushDenormals ftz;
  11590. std::vector<float> inL (4096), inR (4096);
  11591. for (int n = 0; n < 4096; ++n)
  11592. {
  11593. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11594. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11595. }
  11596. struct Gold { int i; float l, r; };
  11597. auto state = [] (const char* hex) {
  11598. std::vector<std::uint8_t> b;
  11599. for (const char* p = hex; p[0] && p[1]; p += 2)
  11600. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  11601. return b;
  11602. };
  11603. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11604. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  11605. CHECK (slot && slot->kind == kind);
  11606. if (! slot)
  11607. return;
  11608. auto fx = lynx::makeEffect (*slot);
  11609. fx->prepare (44100.0);
  11610. std::vector<float> l (inL), r (inR);
  11611. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11612. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11613. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11614. for (const auto& g : gold)
  11615. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11616. };
  11617. using K = lynx::EffectKind;
  11618. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  11619. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  11620. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  11621. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  11622. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  11623. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  11624. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  11625. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  11626. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  11627. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  11628. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  11629. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  11630. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  11631. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  11632. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  11633. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  11634. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  11635. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  11636. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  11637. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  11638. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  11639. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  11640. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  11641. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  11642. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  11643. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  11644. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  11645. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  11646. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  11647. {
  11648. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  11649. CHECK (slot && slot->kind == K::fruityFreeFilter);
  11650. if (slot)
  11651. {
  11652. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  11653. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  11654. }
  11655. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  11656. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  11657. }
  11658. // unreadable states keep the old approximations
  11659. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11660. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11661. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  11662. }
  11663. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  11664. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  11665. namespace
  11666. {
  11667. struct SytrusTestPatch
  11668. {
  11669. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  11670. struct Op
  11671. {
  11672. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  11673. int ratio = 20000, offset = 0, noise = 0;
  11674. std::vector<float> amps { 1.0f };
  11675. std::uint8_t flags[8] {};
  11676. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  11677. std::vector<std::pair<double, double>> env1pts;
  11678. } ops[6];
  11679. struct Filt
  11680. {
  11681. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  11682. int type = 3;
  11683. } filts[3];
  11684. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  11685. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  11686. bool reverbOn = false;
  11687. int eqmode = 0;
  11688. SytrusTestPatch()
  11689. {
  11690. main[0] = 128, main[1] = 128;
  11691. for (int i = 0; i < 8; ++i)
  11692. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  11693. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  11694. }
  11695. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  11696. std::vector<std::uint8_t> blob() const
  11697. {
  11698. std::vector<std::uint8_t> b;
  11699. 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))); };
  11700. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  11701. 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))); };
  11702. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  11703. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  11704. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  11705. for (const auto& [dt, y] : pts)
  11706. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  11707. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  11708. };
  11709. for (int i = 0; i < 240; ++i)
  11710. if (4 * i + 4 <= 0x320)
  11711. i32 (main[(std::size_t) i]);
  11712. b.resize (962, 0); // mute bytes: none muted
  11713. i32 (0); // global declick amount
  11714. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  11715. for (int k = 0; k < 9; ++k)
  11716. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  11717. for (const auto& d : delays)
  11718. b.insert (b.end(), d.begin(), d.end());
  11719. for (auto v : reverb)
  11720. i32 (v);
  11721. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  11722. i32 (eqmode);
  11723. for (int k = 0; k < 3; ++k)
  11724. i32 (0), i32 (30000), i32 (20000), i32 (k);
  11725. b.push_back (0); // no info text
  11726. for (int u = 0; u < 10; ++u)
  11727. {
  11728. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  11729. for (int j = 0; j < 10; ++j)
  11730. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  11731. if (u < 6)
  11732. {
  11733. const auto& o = ops[u];
  11734. const auto at = b.size();
  11735. b.resize (at + 0x828, 0);
  11736. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  11737. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  11738. put (0xc, o.amps.data(), 4 * o.amps.size());
  11739. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  11740. put (0x80c, shape, sizeof shape);
  11741. put (0x820, o.flags, 8);
  11742. }
  11743. else if (u < 9)
  11744. {
  11745. const auto at = b.size();
  11746. b.resize (at + 0x30, 0);
  11747. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  11748. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11749. b.push_back (0);
  11750. }
  11751. for (int k = 0; k < narts; ++k)
  11752. {
  11753. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  11754. const double neutral = add ? 0.5 : 1.0;
  11755. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  11756. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  11757. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  11758. else
  11759. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11760. i32 (128), i32 (128), i32 (0), i32 (128);
  11761. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11762. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  11763. for (int m = 0; m < 6; ++m)
  11764. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  11765. }
  11766. }
  11767. b.push_back (0), b.push_back (0);
  11768. // zlib with stored blocks
  11769. std::vector<std::uint8_t> z { 0x78, 0x01 };
  11770. for (std::size_t at = 0; at < b.size(); at += 65535)
  11771. {
  11772. const auto n = std::min<std::size_t> (65535, b.size() - at);
  11773. z.push_back (at + n >= b.size() ? 1 : 0);
  11774. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  11775. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  11776. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  11777. }
  11778. std::uint32_t a = 1, c = 0;
  11779. for (auto x : b)
  11780. a = (a + x) % 65521, c = (c + a) % 65521;
  11781. for (int k = 3; k >= 0; --k)
  11782. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  11783. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  11784. out.insert (out.end(), z.begin(), z.end());
  11785. return out;
  11786. }
  11787. };
  11788. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  11789. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  11790. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  11791. {
  11792. lynx::sytrus::Engine e;
  11793. e.setTempo (120.0);
  11794. e.prepare (fs, renderMode, 512);
  11795. e.setPatch (lynx::sytrus::parsePatch (blob));
  11796. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  11797. for (int n0 = 0; n0 < n; n0 += 512)
  11798. {
  11799. for (std::size_t k = 0; k < notes.size(); ++k)
  11800. {
  11801. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  11802. {
  11803. lynx::sytrus::NoteParams p;
  11804. p.key = (float) notes[k][0];
  11805. p.id = (std::int64_t) k + 1;
  11806. e.noteOn (p);
  11807. }
  11808. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  11809. e.release ((std::int64_t) k + 1);
  11810. }
  11811. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  11812. }
  11813. return { l, r };
  11814. }
  11815. } // namespace
  11816. TEST (sytrus_engine_sine)
  11817. {
  11818. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  11819. SytrusTestPatch t;
  11820. t.cell (0, 0, 8, 256);
  11821. const auto blob = t.blob();
  11822. CHECK (lynx::sytrus::stateUsable (blob));
  11823. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  11824. auto broken = blob;
  11825. broken.resize (broken.size() / 2);
  11826. CHECK (! lynx::sytrus::stateUsable (broken));
  11827. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  11828. double worst = 0.0;
  11829. for (int k = 0; k < 4000; ++k)
  11830. 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)));
  11831. CHECK (worst < 2e-5);
  11832. CHECK (l == r);
  11833. // 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
  11834. // 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)
  11835. int last = 0;
  11836. for (int k = 0; k < 8192; ++k)
  11837. if (l[(std::size_t) k] != 0.0f)
  11838. last = k;
  11839. CHECK (last == 4895);
  11840. // key 69 = 880 Hz; 48 kHz
  11841. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  11842. worst = 0.0;
  11843. for (int k = 0; k < 4000; ++k)
  11844. 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)));
  11845. CHECK (worst < 5e-5);
  11846. (void) r2;
  11847. }
  11848. TEST (sytrus_engine_reference)
  11849. {
  11850. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  11851. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  11852. SytrusTestPatch t;
  11853. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  11854. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  11855. t.ops[1].ratio = 40000;
  11856. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  11857. t.cell (0, 0, 8, 128);
  11858. t.cell (0, 6, 0, 256); // op 1 → filter 1
  11859. t.cell (0, 6, 8, 256);
  11860. t.cell (0, 0, 7, 200); // FX send
  11861. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  11862. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  11863. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  11864. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  11865. std::memcpy (t.delays[0].data(), d0, 20);
  11866. t.delays[0][22] = 1;
  11867. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  11868. t.reverbOn = true;
  11869. const auto blob = t.blob();
  11870. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  11871. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  11872. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  11873. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  11874. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  11875. for (const auto& [i, v] : golden)
  11876. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  11877. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  11878. for (const auto& [i, v] : goldenR)
  11879. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  11880. }
  11881. TEST (sytrus_instrument)
  11882. {
  11883. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  11884. SytrusTestPatch t;
  11885. t.cell (0, 0, 8, 256);
  11886. lynx::ChannelDef c;
  11887. c.instrument = lynx::InstrumentKind::sytrus;
  11888. c.pluginState = t.blob();
  11889. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  11890. inst->prepare (44100.0);
  11891. inst->configure (c, 120.0);
  11892. CHECK (inst->idle());
  11893. std::vector<float> l (4096), r (4096);
  11894. inst->noteOn (60, 100.0f / 128.0f);
  11895. // 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
  11896. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  11897. double peak = 0.0;
  11898. for (int k = 1000; k < 4096; ++k)
  11899. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  11900. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  11901. CHECK (! inst->idle());
  11902. inst->noteOff (60);
  11903. std::vector<float> tail (44100 * 3), tailR (tail.size());
  11904. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  11905. CHECK (inst->idle());
  11906. CHECK (tail.back() == 0.0f);
  11907. }
  11908. TEST (harmor_installation_presets)
  11909. {
  11910. // REA_rodada165g-flbeta165s: every Harmor preset of the installation plays on the exact engine — the 88 old
  11911. // (version 11…15) ones through the upgrade to the current layout, which renders exactly like the old bytes and
  11912. // keeps the parameter offsets editable; small images (Labyrinth's 16 × 12) are played, not skipped
  11913. if (realFlpDir.empty())
  11914. return;
  11915. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Harmor";
  11916. if (! std::filesystem::exists (folder))
  11917. return;
  11918. int files = 0, usable = 0, old = 0, upgraded = 0, same = 0;
  11919. const std::vector<lynx::harmor::RefNote> notes { { 0, 60, 0.8, 1.0, 0.0, 3000 } };
  11920. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  11921. {
  11922. if (entry.path().extension() != ".fst")
  11923. continue;
  11924. std::ifstream in (entry.path(), std::ios::binary);
  11925. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11926. std::vector<std::uint8_t> st;
  11927. try
  11928. {
  11929. for (const auto& c : flp::parseProject (file, true).channels)
  11930. if (! c.pluginState.empty())
  11931. st = c.pluginState;
  11932. }
  11933. catch (const flp::Error&)
  11934. {
  11935. }
  11936. ++files;
  11937. usable += lynx::harmorStateUsable (st);
  11938. std::uint32_t v = 0;
  11939. if (st.size() >= 4)
  11940. std::memcpy (&v, st.data(), 4);
  11941. if ((v > 0xffff ? v >> 16 : v) >= 16)
  11942. continue;
  11943. ++old;
  11944. const auto up = lynx::harmorUpgradeState (st);
  11945. if (! up)
  11946. continue;
  11947. std::uint32_t uv = 0;
  11948. std::memcpy (&uv, up->data(), 4);
  11949. upgraded += uv == 0x130003u && lynx::harmorParamOffset (*up, 0, 34) > 0 && lynx::harmorStateUsable (*up);
  11950. if (old <= 6) // (a few: rendering is the slow part)
  11951. same += lynx::harmor::renderReference (st, notes, 44100.0, 4096) == lynx::harmor::renderReference (*up, notes, 44100.0, 4096);
  11952. else
  11953. ++same;
  11954. }
  11955. std::printf (" Harmor installation presets: %d files, %d usable, %d old, %d upgraded\n", files, usable, old, upgraded);
  11956. CHECK (files >= 300 && usable == files && old >= 80 && upgraded == old && same == old);
  11957. std::ifstream in (folder / "Bell" / "Labyrinth.fst", std::ios::binary);
  11958. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11959. if (file.empty())
  11960. return;
  11961. auto st = flp::parseProject (file, true).channels.at (0).pluginState;
  11962. const auto a = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11963. const std::int32_t zero = 0;
  11964. for (int part = 0; part < 2; ++part)
  11965. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, 92), &zero, 4); // image frequency scale 0
  11966. const auto b = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11967. CHECK (a.size() == b.size() && a != b);
  11968. }
  11969. TEST (harmor_image)
  11970. {
  11971. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  11972. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py);
  11973. // since flbeta165h LYNX's own values: the test state plays at precision "Average" (global misc +28 = 0), whose
  11974. // IPP 11-bit Exp/Ln (REA_rodada165g-flbeta165h §3) the model computes exactly
  11975. const auto blob = harmorTestState (false, true);
  11976. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  11977. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  11978. CHECK (lynx::harmorStateUsable (blob));
  11979. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  11980. 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);
  11981. CHECK (ref.size() == 2 * 8192);
  11982. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  11983. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  11984. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  11985. const std::tuple<int, float, float> golden[] {
  11986. { 100, 0.0022266121f, 0.018016478f }, { 1000, 0.013964245f, -0.028604792f }, { 2500, 0.016782919f, -0.028934296f },
  11987. { 3000, -0.010533399f, -0.0061015566f }, { 5000, -0.017427528f, 0.016475817f }, { 6500, 0.16895676f, 0.078423217f },
  11988. { 8191, -0.045406573f, -0.049047086f } };
  11989. for (const auto& [i, gl, gr] : golden)
  11990. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  11991. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  11992. auto plain = blob;
  11993. const std::int32_t zero = 0;
  11994. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  11995. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  11996. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  11997. 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);
  11998. double diff = 0.0, energy = 0.0;
  11999. for (std::size_t i = 0; i < ref.size(); ++i)
  12000. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  12001. CHECK (energy > 1.0 && diff > 0.01 * energy);
  12002. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  12003. auto resyn = blob;
  12004. {
  12005. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  12006. 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 };
  12007. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  12008. CHECK (it != resyn.end());
  12009. if (it != resyn.end())
  12010. *(it + 4) = 1;
  12011. }
  12012. const auto fb = lynx::harmorFallbacks (resyn);
  12013. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  12014. }
  12015. TEST (harmor_round75_features)
  12016. {
  12017. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  12018. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's);
  12019. // since flbeta165h LYNX's own values: the test state plays at precision "Average", whose IPP 11-bit Exp/Ln
  12020. // (REA_rodada165g-flbeta165h §3) the model computes exactly
  12021. 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 } };
  12022. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  12023. const Case cases[] {
  12024. { 1, { { { 2000, 0.0032625794f, -0.0014364491f }, { 4500, 0.10464316f, 0.026205163f }, { 7000, -0.0024230368f, 0.0036458215f }, { 11000, -0.0029985344f, -0.0030873856f } } } },
  12025. { 2, { { { 2000, 0.29411051f, -0.22980602f }, { 4500, -0.004929353f, -0.0013274907f }, { 7000, 0.013239861f, 0.0054404787f }, { 11000, -0.0010576053f, -0.0021088289f } } } },
  12026. { 4, { { { 2000, 0.072069392f, -0.029683406f }, { 4500, -0.10765476f, 0.11332012f }, { 7000, -0.22984716f, -0.10102195f }, { 11000, -0.051904719f, -0.066571109f } } } },
  12027. { 8, { { { 2000, 0.072010428f, -0.029725688f }, { 4500, -0.10748001f, 0.1136639f }, { 7000, -0.22849904f, -0.15257275f }, { 11000, -0.041248851f, -0.050450616f } } } },
  12028. { 16, { { { 2000, 0.063442282f, -0.030946331f }, { 4500, -0.11211193f, 0.11279358f }, { 7000, -0.23168804f, -0.1670858f }, { 11000, -0.041186355f, -0.050383076f } } } },
  12029. { 32, { { { 2000, 0.086303107f, -0.035885293f }, { 4500, 0.069407858f, 0.14133474f }, { 7000, -0.15907738f, -0.10380846f }, { 11000, -0.029428774f, -0.041782599f } } } },
  12030. { 63, { { { 2000, 0.017201126f, -0.010683246f }, { 4500, 0.55659908f, 0.08110752f }, { 7000, -0.0025542902f, 0.020610567f }, { 11000, -0.0031871174f, 0.00024673535f } } } } };
  12031. for (const auto& c : cases)
  12032. {
  12033. const auto blob = harmorTestState (false, false, c.features);
  12034. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  12035. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  12036. CHECK (lynx::harmorStateUsable (blob));
  12037. CHECK (lynx::harmorFallbacks (blob).empty());
  12038. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  12039. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  12040. for (int i : { 2000, 4500, 7000, 11000 })
  12041. 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]);
  12042. for (const auto& [i, gl, gr] : c.golden)
  12043. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  12044. }
  12045. }
  12046. TEST (sytrus_main_settings)
  12047. {
  12048. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  12049. SytrusTestPatch t;
  12050. t.cell (0, 0, 8, 256);
  12051. t.main[173] = 3;
  12052. const auto blob = t.blob();
  12053. const auto& S = lynx::sytrus::settings();
  12054. auto find = [&] (const char* name) {
  12055. for (int i = 0; i < (int) S.size(); ++i)
  12056. if (std::string (S[(std::size_t) i].name) == name)
  12057. return i;
  12058. return -1;
  12059. };
  12060. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  12061. // 25 main controls, then the matrix (round 172a): FM 9 × 9 (sources, PAN, FX, OUT) and RM 9 × 6
  12062. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u + 81u + 54u);
  12063. const int out1 = find ("Op 1 out"), fm21 = find ("FM op 2 to op 1"), rm3f2 = find ("RM op 3 to filter 2");
  12064. CHECK (out1 >= 0 && fm21 >= 0 && rm3f2 >= 0 && find ("RM op 1 pan") < 0);
  12065. CHECK (out1 >= 0 && S[(std::size_t) out1].index == 3 + 8 && S[(std::size_t) rm3f2].index == 3 + 81 + 9 * 7 + 2);
  12066. CHECK (lynx::sytrus::settingValue (blob, out1) == 256 && lynx::sytrus::settingValue (blob, fm21) == 0);
  12067. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  12068. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  12069. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  12070. auto broken = blob;
  12071. broken.resize (broken.size() / 2);
  12072. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  12073. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  12074. auto quiet = blob;
  12075. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  12076. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  12077. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  12078. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  12079. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  12080. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  12081. double worst = 0.0;
  12082. for (int k = 0; k < 4096; ++k)
  12083. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  12084. CHECK (worst < 1e-5);
  12085. (void) r1, (void) r2;
  12086. // clamped to the control's range; PORTA is the flag byte the engine reads
  12087. auto edited = blob;
  12088. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  12089. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  12090. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  12091. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  12092. // a matrix cell: op 2 modulates op 1 (FM), clamped to ±256; the patch stays playable
  12093. CHECK (lynx::sytrus::setSetting (edited, fm21, 999) && lynx::sytrus::settingValue (edited, fm21) == 256);
  12094. CHECK (lynx::sytrus::settingValue (edited, out1) == 256 && lynx::sytrus::parsePatch (edited) != nullptr);
  12095. }
  12096. TEST (harmor_effects_rack)
  12097. {
  12098. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  12099. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  12100. auto blob = harmorTestState (false);
  12101. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  12102. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  12103. };
  12104. set (blob, 0, 10, 1000); // all to the FX send
  12105. auto render = [] (const std::vector<std::uint8_t>& st) {
  12106. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  12107. };
  12108. const auto dry = render (blob);
  12109. auto rev = blob;
  12110. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  12111. 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);
  12112. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  12113. const auto bypassed = render (rev);
  12114. double eb = 0.0, db = 0.0;
  12115. for (std::size_t i = 0; i < dry.size(); ++i)
  12116. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  12117. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  12118. auto after = rev;
  12119. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  12120. for (int k = 0; k < 6; ++k)
  12121. set (after, -1, 53 + k, order[k]);
  12122. const auto wet = render (after);
  12123. double e = 0.0, d = 0.0;
  12124. bool finite = true;
  12125. for (std::size_t i = 0; i < wet.size(); ++i)
  12126. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  12127. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  12128. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  12129. auto dist = harmorTestState (false);
  12130. {
  12131. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  12132. const std::int32_t six = 6;
  12133. std::memcpy (dist.data() + at, &six, 4);
  12134. }
  12135. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  12136. 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);
  12137. set (dist, -1, 47, 1050);
  12138. lynx::ChannelDef c;
  12139. c.instrument = lynx::InstrumentKind::harmor;
  12140. c.pluginState = dist;
  12141. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  12142. inst->configure (c, 120.0);
  12143. inst->prepare (44100.0);
  12144. std::vector<float> l (44100 * 4), r (l.size());
  12145. inst->noteOn (60, 0.8f);
  12146. double energy = 0.0;
  12147. for (int s = 0; s < (int) l.size(); s += 512)
  12148. {
  12149. if (s == 8192)
  12150. inst->noteOff (60);
  12151. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  12152. }
  12153. for (std::size_t i = 0; i < l.size(); ++i)
  12154. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  12155. CHECK (finite && energy > 1.0);
  12156. std::vector<float> tail (44100 * 6), tailR (tail.size());
  12157. for (int s = 0; s < (int) tail.size(); s += 512)
  12158. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  12159. CHECK (inst->idle());
  12160. }
  12161. TEST (harmless_engine)
  12162. {
  12163. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  12164. std::vector<std::uint8_t> blob;
  12165. 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))); };
  12166. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  12167. for (int i = 11; i < 23; ++i)
  12168. p[(std::size_t) i] = 1000;
  12169. 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;
  12170. 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;
  12171. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  12172. 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;
  12173. put (12);
  12174. for (auto v : p)
  12175. put (v);
  12176. for (int i = 0; i < 7; ++i)
  12177. put (0);
  12178. put (524); // Custom colour table: 524 × (amplitude, phase)
  12179. for (int i = 0; i < 1048; ++i)
  12180. {
  12181. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  12182. std::uint32_t bits = 0;
  12183. std::memcpy (&bits, &f, 4);
  12184. put ((std::int32_t) bits);
  12185. }
  12186. const auto parsed = lynx::harmlessParams (blob);
  12187. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  12188. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  12189. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  12190. auto old = blob;
  12191. old[0] = 3; // versions below 4 are not Harmless states we know
  12192. CHECK (! lynx::harmlessParams (old));
  12193. old = blob;
  12194. old.resize (300);
  12195. CHECK (! lynx::harmlessParams (old));
  12196. lynx::ChannelDef c;
  12197. c.instrument = lynx::InstrumentKind::harmless;
  12198. c.modelParams = *parsed;
  12199. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  12200. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  12201. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  12202. inst->configure (def, 120.0);
  12203. inst->prepare (44100.0);
  12204. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  12205. inst->noteOn (60, 0.78125f);
  12206. for (int s = 0; s < total; s += 512)
  12207. {
  12208. if (s == offAt)
  12209. inst->noteOff (60);
  12210. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  12211. }
  12212. return std::make_tuple (l, r, std::move (inst));
  12213. };
  12214. {
  12215. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  12216. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  12217. const std::tuple<int, float, float> golden[] {
  12218. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  12219. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  12220. for (const auto& [i, gl, gr] : golden)
  12221. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  12222. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  12223. std::vector<float> tail (44100), tailR (tail.size());
  12224. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  12225. CHECK (inst->idle());
  12226. }
  12227. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  12228. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  12229. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  12230. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  12231. auto mono = c;
  12232. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  12233. mono.modelParams[65] = 1.0f;
  12234. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  12235. double el = 0.0, er = 0.0;
  12236. for (int i = 0; i < 2048; ++i)
  12237. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  12238. CHECK (el < 1e-10 * er && er > 1.0);
  12239. }
  12240. TEST (startup_sound)
  12241. {
  12242. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  12243. // a landing "pop" at impactTime and a chime ringing after it
  12244. using namespace lynx::startup;
  12245. const auto sound = makeSound (48000.0);
  12246. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  12247. float peak = 0.0f;
  12248. bool finite = true;
  12249. for (std::size_t i = 0; i < sound.left.size(); ++i)
  12250. {
  12251. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  12252. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  12253. }
  12254. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  12255. auto rms = [&sound] (double from, double to) {
  12256. double e = 0.0;
  12257. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  12258. for (auto i = a; i < b; ++i)
  12259. e += (double) sound.left[i] * sound.left[i];
  12260. return std::sqrt (e / (double) (b - a));
  12261. };
  12262. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  12263. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  12264. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  12265. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  12266. }
  12267. namespace
  12268. {
  12269. // little-endian byte writer for the state builders below
  12270. struct Bytes
  12271. {
  12272. std::vector<std::uint8_t> b;
  12273. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  12274. 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); }
  12275. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  12276. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  12277. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  12278. };
  12279. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  12280. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  12281. // unison, slide, distortion, delay, reverb and the output EQ.
  12282. std::vector<std::uint8_t> gmsTestState()
  12283. {
  12284. std::array<float, 212> P {};
  12285. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  12286. P[(std::size_t) i] = 0.5f;
  12287. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  12288. 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;
  12289. 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;
  12290. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  12291. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  12292. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  12293. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  12294. Bytes preset;
  12295. preset.raw ("4nyS", 4);
  12296. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  12297. {
  12298. const auto n = std::strlen (path);
  12299. preset.raw (path, n);
  12300. preset.zeros (512 - n);
  12301. }
  12302. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  12303. for (auto v : P)
  12304. preset.f32 (v);
  12305. preset.raw ("LYNX test", 9);
  12306. preset.zeros (512 - 9);
  12307. Bytes s;
  12308. s.raw ("SSLF", 4);
  12309. s.i32 ((std::int32_t) preset.b.size());
  12310. s.bytes (preset.b);
  12311. return s.b;
  12312. }
  12313. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  12314. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  12315. // amplitude through the matrix), the master ADSR and every plugin effect on.
  12316. std::vector<std::uint8_t> morphineTestState()
  12317. {
  12318. auto mbp = [] (int seed) {
  12319. Bytes b;
  12320. b.raw ("0PBM", 4);
  12321. for (int a = 0; a < 3; ++a)
  12322. for (int byte = 0; byte < 16; ++byte)
  12323. {
  12324. b.b.push_back (0xff);
  12325. for (int bit = 0; bit < 8; ++bit)
  12326. {
  12327. const auto k = byte * 8 + bit;
  12328. // (cbrtf is not correctly rounded before glibc 2.43 and differs between libms; the cube root in double
  12329. // rounded to float gives the same (correctly rounded) bits with old and new glibc)
  12330. const float v = a == 0 ? (float) std::cbrt ((double) (1.0f / (float) (k + 1 + seed * (k % 3)))) * (k < 40 ? 1.0f : 0.2f)
  12331. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  12332. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  12333. b.f32 (v);
  12334. }
  12335. }
  12336. return b.b;
  12337. };
  12338. auto msp = [&] (int frames, int seed, int loopMode) {
  12339. Bytes b;
  12340. b.raw ("0PSM", 4);
  12341. b.i32 (frames);
  12342. for (int f = 0; f < frames; ++f)
  12343. {
  12344. const auto m = mbp (seed + f);
  12345. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  12346. b.bytes (m);
  12347. }
  12348. b.i32 (0x424);
  12349. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  12350. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  12351. for (int k = 0; k < 128; ++k)
  12352. b.f32 (0.3f);
  12353. for (int k = 0; k < 128; ++k)
  12354. b.f32 (1.0f);
  12355. return b.b;
  12356. };
  12357. auto mvc = [&] (int frames, int seed, int pwmMode) {
  12358. Bytes z;
  12359. z.raw ("0HZM", 4);
  12360. z.i32 (1);
  12361. const auto s = msp (frames, seed, 1);
  12362. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  12363. z.bytes (s);
  12364. Bytes b;
  12365. b.raw ("0CVM", 4);
  12366. b.i32 ((std::int32_t) z.b.size());
  12367. b.bytes (z.b);
  12368. 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);
  12369. return b.b;
  12370. };
  12371. Bytes p;
  12372. p.raw ("0roM", 4);
  12373. p.raw ("0RPM", 4);
  12374. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  12375. {
  12376. p.i32 ((std::int32_t) g.size());
  12377. p.bytes (g);
  12378. }
  12379. {
  12380. Bytes m;
  12381. m.raw ("0LMM", 4);
  12382. m.i32 (2), m.i32 (12);
  12383. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  12384. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  12385. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  12386. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  12387. m.f32 (v);
  12388. for (int k = 0; k < 4; ++k)
  12389. m.i32 (0);
  12390. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  12391. p.i32 ((std::int32_t) m.b.size());
  12392. p.bytes (m.b);
  12393. }
  12394. {
  12395. Bytes d;
  12396. d.raw ("0DMM", 4);
  12397. d.i32 (4);
  12398. for (int e = 0; e < 4; ++e)
  12399. {
  12400. Bytes g;
  12401. g.raw ("0GEM", 4);
  12402. g.i32 (3), g.i32 (8);
  12403. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  12404. 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),
  12405. g.i32 (e == 0 ? 1 : 0);
  12406. d.i32 ((std::int32_t) g.b.size());
  12407. d.bytes (g.b);
  12408. }
  12409. d.i32 (0x94);
  12410. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  12411. const float amt[12] = { 0.56f, 0.7f };
  12412. for (int k = 0; k < 12; ++k)
  12413. d.i32 (src[k]);
  12414. for (int k = 0; k < 12; ++k)
  12415. d.i32 (dst[k]);
  12416. for (int k = 0; k < 12; ++k)
  12417. d.f32 (amt[k]);
  12418. p.i32 ((std::int32_t) d.b.size());
  12419. p.bytes (d.b);
  12420. }
  12421. {
  12422. 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
  12423. 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 …
  12424. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  12425. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  12426. Bytes g;
  12427. g.i32 (0x5c4);
  12428. for (auto v : A)
  12429. g.f32 (v);
  12430. g.zeros (0x5c4 - g.b.size());
  12431. p.bytes (g.b);
  12432. }
  12433. Bytes s;
  12434. s.raw ("SSLF", 4);
  12435. s.i32 ((std::int32_t) p.b.size());
  12436. s.bytes (p.b);
  12437. return s.b;
  12438. }
  12439. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  12440. {
  12441. if (const char* path = std::getenv (var))
  12442. {
  12443. std::ofstream out (path, std::ios::binary);
  12444. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  12445. }
  12446. }
  12447. } // namespace
  12448. TEST (dx10_engine)
  12449. {
  12450. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  12451. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  12452. // instrument front-end
  12453. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  12454. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  12455. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  12456. blob[0] = 1;
  12457. std::memcpy (blob.data() + 4, P, sizeof (P));
  12458. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  12459. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  12460. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  12461. {
  12462. auto bad = blob;
  12463. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  12464. CHECK (! lynx::dx10StateUsable (bad));
  12465. }
  12466. using N = lynx::dx10::RefNote;
  12467. 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 } };
  12468. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12469. auto check = [&] (const Golden& g) {
  12470. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12471. CHECK (y.size() == 2 * 16384);
  12472. if (y.size() != 2 * 16384)
  12473. return y;
  12474. double el = 0.0, er = 0.0;
  12475. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12476. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12477. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12478. for (const auto& [i, l, r] : g.at)
  12479. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12480. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12481. return y;
  12482. };
  12483. lynx::dx10::RefOptions plain, odd;
  12484. odd.block = 333, odd.maxPoly = 1;
  12485. check ({ blob, plain, 44100, 672.293115, 778.393071,
  12486. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  12487. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  12488. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  12489. check ({ blob, odd, 48000, 122.232761, 493.320297,
  12490. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  12491. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  12492. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  12493. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12494. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12495. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12496. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12497. for (const auto& [i, v] : fields)
  12498. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12499. };
  12500. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12501. std::memcpy (s.data() + 0x15c, &modX, 4);
  12502. std::memcpy (s.data() + 0x160, &modY, 4);
  12503. std::memcpy (s.data() + 0x168, &type, 4);
  12504. };
  12505. std::vector<std::uint8_t> snap (364, 0);
  12506. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12507. 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 } });
  12508. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12509. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12510. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  12511. settings (snap, 140, 120, 7);
  12512. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  12513. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  12514. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  12515. lynx::dx10::RefOptions ticks;
  12516. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  12517. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  12518. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  12519. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  12520. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  12521. std::vector<std::uint8_t> decay (364, 0);
  12522. for (int k = 0; k < 5; ++k)
  12523. record (decay, k, {});
  12524. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  12525. settings (decay, 256, 0, 0);
  12526. auto ticks48 = ticks;
  12527. ticks48.block = 333;
  12528. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  12529. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  12530. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  12531. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  12532. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  12533. silentTail = silentTail && y[i] == 0.0f;
  12534. CHECK (silentTail);
  12535. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  12536. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  12537. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  12538. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  12539. auto noVib = blob;
  12540. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  12541. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  12542. lynx::ChannelDef c;
  12543. c.instrument = lynx::InstrumentKind::dx10;
  12544. c.pluginState = noVib;
  12545. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12546. inst->configure (c, 120.0);
  12547. inst->prepare (44100.0);
  12548. const float V = 0.6f, pan = 0.25f;
  12549. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12550. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12551. std::vector<float> l (16384), r (16384);
  12552. for (int s = 0; s < 16384; s += 512)
  12553. {
  12554. if (s == 0)
  12555. inst->noteOn (60, 0.78125f);
  12556. if (s == 2048)
  12557. {
  12558. inst->setNextNoteExpression (-0.5f, 25.0f);
  12559. inst->noteOn (67, 0.5f);
  12560. }
  12561. if (s == 6144)
  12562. inst->noteOff (60);
  12563. if (s == 8192)
  12564. inst->noteOff (67);
  12565. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12566. }
  12567. // the channel level reaches the engine as FL's (recovered to float precision)
  12568. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12569. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12570. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  12571. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12572. 44100.0, 16384);
  12573. double worst = 0.0, peak = 0.0;
  12574. for (int i = 0; i < 16384; ++i)
  12575. 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]) }),
  12576. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12577. CHECK (worst == 0.0 && peak > 0.05);
  12578. // the released voices end (DX10's own release) and the instrument goes idle
  12579. std::vector<float> tail (44100), tailR (tail.size());
  12580. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  12581. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  12582. CHECK (inst->idle());
  12583. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  12584. {
  12585. lynx::ChannelDef cs = c;
  12586. cs.pluginState = withSnap;
  12587. cs.modelParams = { 96.0f };
  12588. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12589. si->configure (cs, 120.0);
  12590. si->prepare (44100.0);
  12591. std::vector<float> sl (32768), sr (32768);
  12592. si->noteOn (48, 0.78125f);
  12593. for (int s = 0; s < 32768; s += 512)
  12594. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  12595. double e1 = 0.0, e2 = 0.0;
  12596. bool finite = true;
  12597. for (int i = 0; i < 32768; ++i)
  12598. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  12599. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  12600. }
  12601. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  12602. auto edited = blob;
  12603. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  12604. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  12605. }
  12606. TEST (threeosc_engine)
  12607. {
  12608. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  12609. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  12610. 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 };
  12611. 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) {
  12612. std::vector<std::uint8_t> st (0x60, 0);
  12613. const std::int32_t version = 14;
  12614. std::memcpy (st.data(), &version, 4);
  12615. std::memcpy (st.data() + 4, p, 21 * 4);
  12616. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  12617. st.insert (st.end(), snap.begin(), snap.end());
  12618. return st;
  12619. };
  12620. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12621. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12622. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12623. for (const auto& [i, v] : fields)
  12624. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12625. };
  12626. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12627. std::memcpy (s.data() + 0x15c, &modX, 4);
  12628. std::memcpy (s.data() + 0x160, &modY, 4);
  12629. std::memcpy (s.data() + 0x168, &type, 4);
  12630. };
  12631. std::vector<std::uint8_t> neutral (364, 0);
  12632. for (int k = 0; k < 5; ++k)
  12633. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  12634. settings (neutral, 256, 0, 0);
  12635. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  12636. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  12637. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  12638. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  12639. // version 10 (20 parameters, no invert/AM bytes)
  12640. {
  12641. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  12642. v12[0] = 12;
  12643. v12[0x58] = 1;
  12644. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  12645. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  12646. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  12647. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  12648. v10[0] = 10;
  12649. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  12650. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  12651. }
  12652. using N = lynx::threeosc::RefNote;
  12653. 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 } };
  12654. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12655. auto check = [&] (const Golden& g) {
  12656. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12657. CHECK (y.size() == 2 * 16384);
  12658. if (y.size() != 2 * 16384)
  12659. return y;
  12660. double el = 0.0, er = 0.0;
  12661. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12662. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12663. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12664. for (const auto& [i, l, r] : g.at)
  12665. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12666. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12667. return y;
  12668. };
  12669. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  12670. lynx::threeosc::RefOptions plain;
  12671. plain.seed = 12345;
  12672. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  12673. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  12674. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  12675. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  12676. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  12677. 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 };
  12678. lynx::threeosc::RefOptions odd;
  12679. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  12680. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  12681. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  12682. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  12683. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  12684. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  12685. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12686. std::vector<std::uint8_t> snap (364, 0);
  12687. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12688. 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 } });
  12689. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12690. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12691. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  12692. settings (snap, 140, 120, 7);
  12693. lynx::threeosc::RefOptions ticks;
  12694. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  12695. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  12696. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  12697. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  12698. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  12699. auto ticks48 = ticks;
  12700. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  12701. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  12702. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  12703. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  12704. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  12705. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  12706. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  12707. // one agree to float rounding, so the golden values hold to 1e-5)
  12708. {
  12709. 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 };
  12710. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  12711. hq[0x5c] = 1;
  12712. CHECK (lynx::threeOscParam (hq, 25) == 1);
  12713. lynx::threeosc::RefOptions o;
  12714. o.seed = 3;
  12715. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  12716. CHECK (y.size() == 2 * 16384);
  12717. if (y.size() == 2 * 16384)
  12718. {
  12719. double el = 0.0, er = 0.0;
  12720. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12721. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12722. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  12723. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  12724. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  12725. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  12726. for (const auto& [i, l, r] : at)
  12727. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  12728. }
  12729. }
  12730. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  12731. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  12732. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  12733. lynx::ChannelDef c;
  12734. c.instrument = lynx::InstrumentKind::threeosc;
  12735. c.pluginState = blob;
  12736. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  12737. inst->configure (c, 120.0);
  12738. inst->prepare (44100.0);
  12739. const float V = 0.6f, pan = 0.25f;
  12740. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12741. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12742. std::vector<float> l (16384), r (16384);
  12743. for (int s = 0; s < 16384; s += 512)
  12744. {
  12745. if (s == 0)
  12746. inst->noteOn (60, 0.78125f);
  12747. if (s == 2048)
  12748. {
  12749. inst->setNextNoteExpression (-0.5f, 25.0f);
  12750. inst->noteOn (67, 0.5f);
  12751. }
  12752. if (s == 6144)
  12753. inst->noteOff (60);
  12754. if (s == 8192)
  12755. inst->noteOff (67);
  12756. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12757. }
  12758. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12759. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12760. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  12761. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12762. 44100.0, 16384);
  12763. double worst = 0.0, peak = 0.0;
  12764. for (int i = 0; i < 16384; ++i)
  12765. 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]) }),
  12766. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12767. CHECK (worst == 0.0 && peak > 0.05);
  12768. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  12769. CHECK (inst->idle());
  12770. // parameter edits write the raw integers, clamped to the knob ranges
  12771. auto edited = blob;
  12772. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  12773. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  12774. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  12775. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  12776. }
  12777. TEST (gms_engine)
  12778. {
  12779. const auto blob = gmsTestState();
  12780. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  12781. CHECK (lynx::gmsStateUsable (blob));
  12782. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  12783. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  12784. auto broken = blob;
  12785. broken[8] = 'X';
  12786. CHECK (! lynx::gmsStateUsable (broken));
  12787. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  12788. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  12789. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12790. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12791. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  12792. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  12793. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  12794. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  12795. CHECK (y.size() == 2 * 66150u);
  12796. 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 } };
  12797. for (const auto& g : golden)
  12798. 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);
  12799. double e = 0.0;
  12800. for (auto v : y)
  12801. e += (double) v * v;
  12802. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  12803. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12804. lynx::ChannelDef c;
  12805. c.instrument = lynx::InstrumentKind::gms;
  12806. c.pluginState = blob;
  12807. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  12808. inst->configure (c, 140.0);
  12809. inst->prepare (44100.0);
  12810. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12811. for (int b = 0; b < 16; ++b)
  12812. {
  12813. if (b == 0)
  12814. inst->noteOn (60, 0.78125f);
  12815. if (b == 10)
  12816. inst->noteOff (60);
  12817. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12818. }
  12819. 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, "");
  12820. double diff = 0.0, energy = 0.0;
  12821. for (int i = 0; i < 8192; ++i)
  12822. {
  12823. 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]) });
  12824. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12825. }
  12826. CHECK (diff == 0.0 && energy > 1e-3);
  12827. }
  12828. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  12829. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  12830. std::vector<std::uint8_t> toxicTestState()
  12831. {
  12832. auto s = lynx::toxicDefaultState();
  12833. static const std::pair<int, double> set[] = {
  12834. { 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 },
  12835. { 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 },
  12836. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  12837. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  12838. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  12839. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  12840. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  12841. for (const auto& [index, value] : set)
  12842. {
  12843. const auto v = (float) value;
  12844. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  12845. }
  12846. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  12847. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  12848. std::memcpy (s.data() + at, &amp, 4);
  12849. std::memcpy (s.data() + at + 512, &phase, 4);
  12850. };
  12851. for (int h = 0; h < 24; ++h)
  12852. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  12853. for (int k = 0; k < 16; ++k)
  12854. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  12855. return s;
  12856. }
  12857. TEST (toxic_engine)
  12858. {
  12859. const auto blob = toxicTestState();
  12860. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  12861. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  12862. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  12863. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  12864. auto broken = blob;
  12865. broken[8] = 'X';
  12866. CHECK (! lynx::toxicStateUsable (broken));
  12867. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  12868. auto needs = blob;
  12869. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  12870. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  12871. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  12872. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  12873. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  12874. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12875. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12876. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12877. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12878. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  12879. CHECK (y.size() == 2 * 66150u);
  12880. 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 } };
  12881. for (const auto& g : golden)
  12882. 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);
  12883. double e = 0.0;
  12884. for (auto v : y)
  12885. e += (double) v * v;
  12886. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  12887. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12888. lynx::ChannelDef c;
  12889. c.instrument = lynx::InstrumentKind::toxic;
  12890. c.pluginState = blob;
  12891. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  12892. inst->configure (c, 140.0);
  12893. inst->prepare (44100.0);
  12894. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12895. for (int b = 0; b < 16; ++b)
  12896. {
  12897. if (b == 0)
  12898. inst->noteOn (60, 0.78125f);
  12899. if (b == 10)
  12900. inst->noteOff (60);
  12901. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12902. }
  12903. 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 } },
  12904. 44100.0, 8192, "", 512, 140.0);
  12905. double diff = 0.0, energy = 0.0;
  12906. for (int i = 0; i < 8192; ++i)
  12907. {
  12908. 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]) });
  12909. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12910. }
  12911. CHECK (diff == 0.0 && energy > 1e-3);
  12912. }
  12913. TEST (morphine_engine)
  12914. {
  12915. const auto blob = morphineTestState();
  12916. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  12917. CHECK (lynx::morphineStateUsable (blob));
  12918. CHECK (lynx::morphineNoiseName (blob).empty());
  12919. const auto at = lynx::morphineParamOffset (blob, 9);
  12920. CHECK (at > 0);
  12921. float a9 = 0.0f;
  12922. std::memcpy (&a9, blob.data() + at, 4);
  12923. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  12924. auto broken = blob;
  12925. broken[12] = 'X'; // the MPR0 tag
  12926. CHECK (! lynx::morphineStateUsable (broken));
  12927. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  12928. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  12929. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  12930. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  12931. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  12932. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  12933. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  12934. CHECK (y.size() == 2 * 80000u);
  12935. 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 } };
  12936. for (const auto& g : golden)
  12937. 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);
  12938. double e = 0.0;
  12939. for (auto v : y)
  12940. e += (double) v * v;
  12941. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  12942. // the instrument: same engine behind LYNX's channel gains
  12943. lynx::ChannelDef c;
  12944. c.instrument = lynx::InstrumentKind::morphine;
  12945. c.pluginState = blob;
  12946. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  12947. inst->configure (c, 120.0);
  12948. inst->prepare (44100.0);
  12949. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12950. for (int b = 0; b < 16; ++b)
  12951. {
  12952. if (b == 0)
  12953. inst->noteOn (60, 0.78125f);
  12954. if (b == 10)
  12955. inst->noteOff (60);
  12956. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12957. }
  12958. 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, "");
  12959. double diff = 0.0, energy = 0.0;
  12960. for (int i = 0; i < 8192; ++i)
  12961. {
  12962. 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]) });
  12963. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12964. }
  12965. CHECK (diff == 0.0 && energy > 1e-3);
  12966. }
  12967. namespace
  12968. {
  12969. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  12970. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  12971. {
  12972. const std::string name = "LYNX test";
  12973. Bytes body;
  12974. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  12975. body.raw ("HEAD", 4);
  12976. body.i32 (6 + 2 * (std::int32_t) name.size());
  12977. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  12978. body.raw (reinterpret_cast<const char*> (head), 6);
  12979. for (char ch : name)
  12980. body.raw (&ch, 1), body.zeros (1);
  12981. body.raw ("DATA", 4);
  12982. body.i32 (176);
  12983. body.raw ("0rev", 4);
  12984. for (auto v : P)
  12985. body.f32 (v);
  12986. Bytes s;
  12987. s.raw ("SSLF", 4);
  12988. s.i32 ((std::int32_t) body.b.size());
  12989. s.bytes (body.b);
  12990. return s.b;
  12991. }
  12992. std::array<float, 43> miniSynthDefaults()
  12993. {
  12994. 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,
  12995. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  12996. }
  12997. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  12998. // fixed name let one process read a file another one was rewriting
  12999. std::string processTempTag()
  13000. {
  13001. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  13002. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  13003. static const struct Cleanup
  13004. {
  13005. ~Cleanup()
  13006. {
  13007. // every scratch file or folder whose name carries this process's tag (round 173e: the SoundFont, case,
  13008. // missing-file, Crash169g, Natives142 and Rea167l scratch paths are per process too)
  13009. std::error_code ec;
  13010. std::vector<std::filesystem::path> mine;
  13011. for (const auto& e : std::filesystem::directory_iterator (std::filesystem::temp_directory_path (ec), ec))
  13012. if (e.path().filename().string().find (tag) != std::string::npos)
  13013. mine.push_back (e.path());
  13014. for (const auto& p : mine)
  13015. std::filesystem::remove_all (p, ec);
  13016. }
  13017. } cleanup;
  13018. (void) cleanup;
  13019. return tag;
  13020. }
  13021. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  13022. std::string miniSynthTestOscdata()
  13023. {
  13024. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  13025. std::vector<float> d (2048);
  13026. for (int h = 0; h < 1024; ++h)
  13027. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  13028. std::ofstream out (path, std::ios::binary);
  13029. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  13030. return path;
  13031. }
  13032. } // namespace
  13033. TEST (minisynth_engine)
  13034. {
  13035. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  13036. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  13037. 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 },
  13038. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  13039. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  13040. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  13041. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  13042. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  13043. // B: lead (legato glide), BELL, LFO → pitch
  13044. 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 },
  13045. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  13046. for (const auto& [i, v] : a)
  13047. A[(std::size_t) i] = v;
  13048. for (const auto& [i, v] : b)
  13049. B[(std::size_t) i] = v;
  13050. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  13051. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  13052. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  13053. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  13054. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  13055. auto shortData = blobA;
  13056. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  13057. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  13058. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  13059. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  13060. const auto osc = miniSynthTestOscdata();
  13061. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  13062. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  13063. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  13064. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  13065. struct Golden { int i; float l, r; };
  13066. 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 } };
  13067. 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 } };
  13068. for (int which = 0; which < 2; ++which)
  13069. {
  13070. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  13071. CHECK (y.size() == 2 * 66150u);
  13072. if (y.size() != 2 * 66150u)
  13073. continue;
  13074. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  13075. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  13076. double e = 0.0;
  13077. for (auto v : y)
  13078. e += (double) v * v;
  13079. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  13080. }
  13081. // missing oscdata.dat: silent tables, still finite
  13082. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  13083. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  13084. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  13085. lynx::ChannelDef c;
  13086. c.instrument = lynx::InstrumentKind::minisynth;
  13087. c.pluginState = blobA;
  13088. c.samplePath = osc;
  13089. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  13090. inst->configure (c, 120.0);
  13091. inst->prepare (44100.0);
  13092. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13093. for (int k = 0; k < 16; ++k)
  13094. {
  13095. if (k == 0)
  13096. inst->noteOn (60, 0.78125f);
  13097. if (k == 10)
  13098. inst->noteOff (60);
  13099. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  13100. }
  13101. 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 } },
  13102. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  13103. double diff = 0.0, energy = 0.0;
  13104. for (int i = 0; i < 8192; ++i)
  13105. {
  13106. 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]) });
  13107. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13108. }
  13109. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  13110. }
  13111. namespace
  13112. {
  13113. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  13114. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  13115. std::vector<std::uint8_t> poizoneTestState()
  13116. {
  13117. std::array<float, 156> P {};
  13118. 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 },
  13119. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  13120. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  13121. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  13122. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  13123. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  13124. { 79, 1.0f } };
  13125. for (const auto& [i, v] : values)
  13126. P[(std::size_t) i] = v;
  13127. for (int i = 49; i < 65; ++i)
  13128. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  13129. for (int i = 81; i < 155; ++i)
  13130. P[(std::size_t) i] = -1.0f;
  13131. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  13132. blob.resize (12 + 4 * 156 + 64, 0);
  13133. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  13134. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  13135. return blob;
  13136. }
  13137. } // namespace
  13138. TEST (poizone_engine)
  13139. {
  13140. const auto blob = poizoneTestState();
  13141. CHECK (lynx::poizoneStateUsable (blob));
  13142. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  13143. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  13144. auto broken = blob;
  13145. broken[8] = 'X'; // the "Ver4" tag
  13146. CHECK (! lynx::poizoneStateUsable (broken));
  13147. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  13148. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  13149. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  13150. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  13151. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  13152. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  13153. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  13154. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  13155. CHECK (y.size() == 2 * 66150u);
  13156. 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 } };
  13157. for (const auto& g : golden)
  13158. 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);
  13159. double e = 0.0;
  13160. for (auto v : y)
  13161. e += (double) v * v;
  13162. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  13163. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  13164. lynx::ChannelDef c;
  13165. c.instrument = lynx::InstrumentKind::poizone;
  13166. c.pluginState = blob;
  13167. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  13168. inst->configure (c, 120.0);
  13169. inst->prepare (44100.0);
  13170. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13171. for (int b = 0; b < 16; ++b)
  13172. {
  13173. if (b == 0)
  13174. inst->noteOn (60, 0.78125f);
  13175. if (b == 10)
  13176. inst->noteOff (60);
  13177. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13178. }
  13179. 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,
  13180. 512, 120.0, 0, true);
  13181. double diff = 0.0, energy = 0.0;
  13182. for (int i = 0; i < 8192; ++i)
  13183. {
  13184. 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]) });
  13185. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13186. }
  13187. CHECK (diff == 0.0 && energy > 1e-3);
  13188. }
  13189. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  13190. namespace
  13191. {
  13192. std::vector<std::uint8_t> fruitKickStateA()
  13193. {
  13194. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  13195. std::vector<std::uint8_t> b (32);
  13196. std::memcpy (b.data(), w, 32);
  13197. return b;
  13198. }
  13199. std::vector<std::uint8_t> fruitKickStateB()
  13200. {
  13201. std::vector<std::uint8_t> b (400, 0);
  13202. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  13203. std::memcpy (b.data(), head, sizeof (head));
  13204. std::int32_t recs[5][17];
  13205. for (auto& r : recs)
  13206. {
  13207. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  13208. std::memcpy (r, d, sizeof (d));
  13209. }
  13210. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  13211. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  13212. std::memcpy (recs[0], pan, sizeof (pan));
  13213. std::memcpy (recs[1], vol, sizeof (vol));
  13214. for (int k = 0; k < 5; ++k)
  13215. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  13216. const std::int32_t cut = 256;
  13217. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  13218. return b;
  13219. }
  13220. std::string bassDrumTestFolder()
  13221. {
  13222. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  13223. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  13224. std::filesystem::create_directories (dir / "Noise Samples");
  13225. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  13226. std::ofstream o (path, std::ios::binary);
  13227. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  13228. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  13229. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  13230. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  13231. o.write ((const char*) data, bytes);
  13232. };
  13233. std::vector<float> table (1024);
  13234. for (int i = 0; i < 1024; ++i)
  13235. 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));
  13236. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  13237. std::vector<std::int16_t> noise (3000);
  13238. std::uint32_t s = 12345;
  13239. for (auto& v : noise)
  13240. {
  13241. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  13242. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  13243. }
  13244. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  13245. return dir.string();
  13246. }
  13247. std::vector<std::uint8_t> bassDrumTestState()
  13248. {
  13249. std::vector<std::uint8_t> b (0x50C + 92, 0);
  13250. std::memcpy (b.data(), "SSLF", 4);
  13251. const std::uint32_t size = 0x560;
  13252. std::memcpy (b.data() + 4, &size, 4);
  13253. std::memcpy (b.data() + 8, "2kiK", 4);
  13254. std::memcpy (b.data() + 12, "Test kick", 9);
  13255. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  13256. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  13257. 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,
  13258. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  13259. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  13260. return b;
  13261. }
  13262. struct Golden
  13263. {
  13264. int i;
  13265. float l, r;
  13266. };
  13267. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  13268. {
  13269. bool ok = true;
  13270. for (const auto& g : golden)
  13271. 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;
  13272. double e = 0.0;
  13273. for (auto v : y)
  13274. e += (double) v * v;
  13275. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  13276. }
  13277. } // namespace
  13278. TEST (fruitkick_engine)
  13279. {
  13280. const auto A = fruitKickStateA(), B = fruitKickStateB();
  13281. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  13282. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  13283. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  13284. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  13285. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  13286. std::vector<std::vector<std::uint8_t>> recs;
  13287. for (int k = 0; k < 5; ++k)
  13288. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  13289. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  13290. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  13291. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  13292. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  13293. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  13294. 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 } };
  13295. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  13296. CHECK (ya.size() == 40000u);
  13297. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  13298. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  13299. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  13300. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  13301. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  13302. 0.284267665614));
  13303. std::vector<int> ticks;
  13304. for (int t = 115; t < 20000; t += 230)
  13305. ticks.push_back (t);
  13306. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  13307. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  13308. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  13309. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  13310. 0.117964927585));
  13311. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  13312. lynx::ChannelDef c;
  13313. c.instrument = lynx::InstrumentKind::fruitkick;
  13314. c.pluginState = A;
  13315. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  13316. inst->configure (c, 140.0);
  13317. inst->prepare (44100.0);
  13318. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13319. for (int b = 0; b < 16; ++b)
  13320. {
  13321. if (b == 0)
  13322. inst->noteOn (60, 0.78125f);
  13323. if (b == 10)
  13324. inst->noteOff (60);
  13325. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13326. }
  13327. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  13328. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  13329. double diff = 0.0, energy = 0.0;
  13330. for (int i = 0; i < 8192; ++i)
  13331. {
  13332. 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]) });
  13333. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13334. }
  13335. CHECK (diff < 1e-6 && energy > 1e-2);
  13336. }
  13337. TEST (ipp_cos_and_end_fade)
  13338. {
  13339. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  13340. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  13341. const auto f = ippcos::bitsToFloat;
  13342. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  13343. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  13344. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  13345. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  13346. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  13347. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  13348. // tiny addend (lost in a plain double sum) must decide
  13349. const float a = 1.0f + 0x1p-12f;
  13350. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  13351. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  13352. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  13353. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  13354. std::vector<float> t (441);
  13355. ippcos::endFadeTable (t.data(), 441);
  13356. double worst = 0.0;
  13357. for (int i = 0; i < 441; ++i)
  13358. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  13359. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  13360. }
  13361. TEST (bassdrum_engine)
  13362. {
  13363. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  13364. const float x = 0.87011606f;
  13365. std::uint32_t bits = 0;
  13366. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  13367. std::memcpy (&bits, &p, 4);
  13368. CHECK (bits == 1065063964u);
  13369. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  13370. const auto st = bassDrumTestState();
  13371. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  13372. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  13373. auto broken = st;
  13374. broken[8] = 'X';
  13375. CHECK (! lynx::bassDrumStateUsable (broken));
  13376. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  13377. const auto dir = bassDrumTestFolder();
  13378. 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 },
  13379. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  13380. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  13381. CHECK (y.size() == 60000u);
  13382. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  13383. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  13384. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  13385. 0.230007588169));
  13386. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  13387. lynx::ChannelDef c;
  13388. c.instrument = lynx::InstrumentKind::bassdrum;
  13389. c.pluginState = st;
  13390. c.samplePath = dir;
  13391. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  13392. inst->configure (c, 120.0);
  13393. inst->prepare (44100.0);
  13394. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13395. for (int b = 0; b < 16; ++b)
  13396. {
  13397. if (b == 0)
  13398. inst->noteOn (60, 0.78125f);
  13399. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13400. }
  13401. 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);
  13402. double diff = 0.0, energy = 0.0;
  13403. for (int i = 0; i < 8192; ++i)
  13404. {
  13405. 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]) });
  13406. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13407. }
  13408. CHECK (diff == 0.0 && energy > 1e-2);
  13409. }
  13410. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  13411. TEST (fruity_fast_dist_flangus_flanger)
  13412. {
  13413. std::vector<float> inL (4096), inR (4096);
  13414. for (int n = 0; n < 4096; ++n)
  13415. {
  13416. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13417. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  13418. }
  13419. struct Gold { int i; float l, r; };
  13420. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13421. // Fast Dist (+ FL's host waveshaper): live and render kernels
  13422. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  13423. std::vector<float> l (inL), r (inR);
  13424. lynx::fastdistfx::process (l.data(), 4096, s, render);
  13425. lynx::fastdistfx::process (r.data(), 4096, s, render);
  13426. for (const auto& g : gold)
  13427. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13428. };
  13429. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  13430. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  13431. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  13432. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  13433. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  13434. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  13435. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  13436. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  13437. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  13438. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  13439. {
  13440. const auto* T = lynx::fastdistfx::curve (0, 10);
  13441. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  13442. }
  13443. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  13444. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  13445. lynx::flangusfx::Engine e;
  13446. e.prepare (fs);
  13447. e.load (s);
  13448. e.setRate (fs);
  13449. e.setBlockSize (512);
  13450. std::vector<float> io (8192);
  13451. for (int n = 0; n < 4096; ++n)
  13452. {
  13453. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13454. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13455. }
  13456. for (int o = 0; o < 4096; o += 512)
  13457. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13458. for (const auto& g : gold)
  13459. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13460. };
  13461. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  13462. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  13463. { 4095, 0.201686561f, 0.607052445f } });
  13464. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  13465. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  13466. { 4095, -0.354708612f, 0.503573656f } });
  13467. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  13468. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  13469. { 4095, 1.49345946f, -0.537866116f } });
  13470. // Flanger: state, then the sample rate (FL's call order)
  13471. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  13472. lynx::flangerfx::Engine e;
  13473. e.load (s);
  13474. e.prepare (fs);
  13475. std::vector<float> io (8192);
  13476. for (int n = 0; n < 4096; ++n)
  13477. {
  13478. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13479. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13480. }
  13481. for (int o = 0; o < 4096; o += 512)
  13482. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13483. for (const auto& g : gold)
  13484. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13485. };
  13486. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  13487. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  13488. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  13489. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  13490. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  13491. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  13492. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  13493. {
  13494. 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 };
  13495. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  13496. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  13497. if (slot)
  13498. {
  13499. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  13500. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  13501. auto fx = lynx::makeEffect (*slot);
  13502. fx->prepare (44100.0);
  13503. std::vector<float> l (inL), r (inR);
  13504. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13505. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  13506. }
  13507. std::vector<std::uint8_t> fg (32);
  13508. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  13509. std::memcpy (fg.data(), v, 32);
  13510. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  13511. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  13512. if (f2)
  13513. {
  13514. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  13515. for (int i = 0; i < 8; ++i)
  13516. CHECK (s[(std::size_t) i] == v[i]);
  13517. auto fx = lynx::makeEffect (*f2);
  13518. fx->prepare (48000.0);
  13519. std::vector<float> l (inL), r (inR);
  13520. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  13521. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  13522. }
  13523. std::vector<std::uint8_t> fl (52);
  13524. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  13525. std::memcpy (fl.data(), w, 52);
  13526. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  13527. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  13528. if (f3)
  13529. {
  13530. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  13531. for (int i = 0; i < 12; ++i)
  13532. CHECK (s[(std::size_t) i] == w[i + 1]);
  13533. auto fx = lynx::makeEffect (*f3);
  13534. fx->prepare (48000.0);
  13535. std::vector<float> l (inL), r (inR);
  13536. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  13537. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  13538. }
  13539. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  13540. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  13541. }
  13542. }
  13543. // FL's Fruity Phaser (REA_rodada105_phaser): goldens from the installation's own DLL, rendered unmodified in 512-frame
  13544. // calls; LYNX's engine matches it to the last bit on the 27 presets (denormals aside), the tolerance only absorbs
  13545. // compilers (gcc / clang / zig) and libm.
  13546. TEST (fruity_phaser_engine)
  13547. {
  13548. std::vector<float> in (8192);
  13549. for (int n = 0; n < 4096; ++n)
  13550. {
  13551. in[2 * (std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13552. in[2 * (std::size_t) n + 1] = (float) (((n * 53) % 170 - 85) / 70.0);
  13553. }
  13554. struct Gold
  13555. {
  13556. int i;
  13557. float l, r;
  13558. };
  13559. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-5f * std::max (1.0f, std::abs (b)); };
  13560. auto phase = [&] (lynx::phaserfx::State s, int fs, std::initializer_list<Gold> gold) {
  13561. lynx::phaserfx::Engine e;
  13562. e.setRate (fs);
  13563. e.load (s);
  13564. e.reset();
  13565. std::vector<float> io (in);
  13566. for (int o = 0; o < 4096; o += 512)
  13567. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13568. for (const auto& g : gold)
  13569. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13570. };
  13571. phase ({ 2500, 100, 800, 0, 512, 7, 400, 512, 4000 }, 44100,
  13572. { { 100, 0.243106663f, -1.63760519f }, { 1000, -2.36990547f, 0.841273248f }, { 2047, 1.21970272f, -1.66379488f },
  13573. { 3000, -2.80603957f, -1.03600585f }, { 4095, 1.80225599f, 0.845855355f } });
  13574. phase ({ 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 }, 48000,
  13575. { { 100, -0.360522032f, -0.0510774851f }, { 1000, -0.788047731f, 0.324495822f }, { 2047, 0.304549545f, 0.0850031748f },
  13576. { 3000, -0.79305625f, 0.114482567f }, { 4095, -0.67624253f, -0.0348635167f } });
  13577. phase ({ 2448, 510, 417, 1024, 1024, 16, 508, 0, 2500 }, 44100,
  13578. { { 100, 1.66273713f, -0.0830700323f }, { 1000, -1.47920549f, -0.0849421024f }, { 2047, -0.157817751f, -1.06916845f },
  13579. { 3000, -0.703828216f, -0.0190049056f }, { 4095, 0.768234849f, -0.965515852f } });
  13580. phase ({ 5000, 0, 1000, 700, 0, 22, 950, 300, 5000 }, 44100,
  13581. { { 100, 0.0809119642f, -0.521223426f }, { 1000, 1.07321298f, 0.681761861f }, { 2047, 0.790922821f, -0.205452889f },
  13582. { 3000, -1.91779351f, 0.599423349f }, { 4095, -1.18747222f, 1.03711367f } });
  13583. // controls: FL's ranges, the stage count clamp, the texts
  13584. CHECK (std::abs (lynx::phaserfx::sweepHz (0.5f, false) - 0.238405839f) < 1.0e-6f);
  13585. CHECK (std::abs (lynx::phaserfx::sweepHz (1.0f, true) - 10.0f) < 1.0e-5f);
  13586. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 11.0f / 22.0f, 120.0) == "12");
  13587. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 0.0f, 120.0) == "3");
  13588. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 7, 0.0f, 120.0) == "100% wet");
  13589. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 8, 0.5f, 120.0) == "+0.0 dB");
  13590. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 3, 0.0f, 120.0) == "small");
  13591. {
  13592. lynx::EffectParams ctx {};
  13593. ctx[3] = 1.0f;
  13594. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 0, 1.0f, 120.0, ctx) == "10.000 Hz");
  13595. }
  13596. CHECK (lynx::effectInfo (lynx::EffectKind::fruityPhaser).params.size() == 9);
  13597. // import: FL's state (version 1 + nine controls) becomes the exact effect; its parameters round-trip
  13598. const lynx::phaserfx::State ouch { 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 };
  13599. const auto bytes = lynx::phaserfx::serialize (ouch);
  13600. const auto slot = lynx::effectFromFl ("Fruity Phaser", bytes, 120.0);
  13601. CHECK (slot && slot->kind == lynx::EffectKind::fruityPhaser);
  13602. if (slot)
  13603. {
  13604. CHECK (lynx::phaserfx::fromParams (slot->params.data()) == ouch);
  13605. auto fx = lynx::makeEffect (*slot);
  13606. fx->prepare (48000.0);
  13607. std::vector<float> l (4096), r (4096);
  13608. for (int n = 0; n < 4096; ++n)
  13609. {
  13610. l[(std::size_t) n] = in[2 * (std::size_t) n];
  13611. r[(std::size_t) n] = in[2 * (std::size_t) n + 1];
  13612. }
  13613. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13614. CHECK (close (l[1000], -0.788047731f) && close (r[4095], -0.0348635167f));
  13615. }
  13616. auto v2 = bytes;
  13617. v2[0] = 2; // FL reads only versions below 2
  13618. const auto later = lynx::effectFromFl ("Fruity Phaser", v2, 120.0);
  13619. CHECK (! later || later->kind != lynx::EffectKind::fruityPhaser);
  13620. CHECK (lynx::plugui::hasFlLayout (lynx::EffectKind::fruityPhaser));
  13621. }
  13622. // Workflow P2 (docs/WORKFLOW_FL.md)
  13623. #include "WorkflowP2Tests.inc"
  13624. // Editor mouse grammar (Piano Roll / Playlist)
  13625. #include "EditorGestureTests.inc"
  13626. TEST (chorus_family_engines)
  13627. {
  13628. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  13629. std::vector<float> inL (4096), inR (4096);
  13630. for (int n = 0; n < 4096; ++n)
  13631. {
  13632. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  13633. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  13634. }
  13635. struct Gold { int i; float l, r; };
  13636. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13637. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  13638. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  13639. CHECK (slot && slot->kind == kind);
  13640. if (! slot)
  13641. return;
  13642. auto fx = lynx::makeEffect (*slot);
  13643. fx->prepare (44100.0);
  13644. std::vector<float> l (inL), r (inR);
  13645. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  13646. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  13647. for (const auto& g : gold)
  13648. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13649. };
  13650. auto ints = [] (std::initializer_list<int> v) {
  13651. std::vector<std::uint8_t> b;
  13652. for (int x : v)
  13653. for (int k = 0; k < 4; ++k)
  13654. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  13655. return b;
  13656. };
  13657. // Fruity Chorus (start phases 0, which FL draws at random)
  13658. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  13659. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  13660. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  13661. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  13662. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  13663. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  13664. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  13665. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  13666. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  13667. {
  13668. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  13669. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  13670. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  13671. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  13672. }
  13673. // a state FL does not read (version 2) or too short keeps the generic chorus
  13674. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  13675. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  13676. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  13677. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  13678. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  13679. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  13680. namespace hc = lynx::hyperchorusfx;
  13681. hc::Engine e (instance);
  13682. const std::vector<int> r (raws);
  13683. 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); };
  13684. for (int i = 0; i < hc::params; ++i)
  13685. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  13686. for (int i = 0; i < hc::params; ++i)
  13687. e.setParam (i, val (i, r[(std::size_t) i]));
  13688. e.setRate (44100.0);
  13689. std::vector<float> io (8192);
  13690. for (int n = 0; n < 4096; ++n)
  13691. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13692. for (int s = 0; s < 4096; s += 512)
  13693. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13694. for (const auto& g : gold)
  13695. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13696. };
  13697. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  13698. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  13699. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  13700. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  13701. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  13702. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  13703. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  13704. {
  13705. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  13706. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  13707. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  13708. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  13709. lynx::Song song;
  13710. song.mixer.resize (3);
  13711. song.mixer[1].effects = { *slot, *slot };
  13712. song.mixer[2].effects = { *slot };
  13713. lynx::numberChorusInstances (song);
  13714. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  13715. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  13716. fx->prepare (48000.0);
  13717. std::vector<float> l (inL), r (inR);
  13718. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13719. bool finite = true, wet = false;
  13720. for (int n = 0; n < 4096; ++n)
  13721. {
  13722. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13723. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  13724. }
  13725. CHECK (finite && wet);
  13726. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  13727. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  13728. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  13729. }
  13730. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  13731. // start from the engine's initial ones)
  13732. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  13733. namespace vc = lynx::vintagechorusfx;
  13734. vc::Engine e;
  13735. e.setRate (44100.0);
  13736. int i = 0;
  13737. for (int raw : raws)
  13738. {
  13739. 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));
  13740. ++i;
  13741. }
  13742. std::vector<float> io (8192);
  13743. for (int n = 0; n < 4096; ++n)
  13744. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13745. for (int s = 0; s < 4096; s += 512)
  13746. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13747. for (const auto& g : gold)
  13748. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13749. };
  13750. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  13751. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  13752. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  13753. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  13754. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  13755. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  13756. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  13757. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  13758. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  13759. {
  13760. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  13761. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  13762. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  13763. auto fx = lynx::makeEffect (*slot);
  13764. fx->prepare (44100.0);
  13765. std::vector<float> l (inL), r (inR);
  13766. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13767. bool finite = true;
  13768. for (int n = 0; n < 4096; ++n)
  13769. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13770. CHECK (finite && l[3000] != inL[3000]);
  13771. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  13772. 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()
  13773. || 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);
  13774. }
  13775. }
  13776. #include "UiChromeTests.inc"
  13777. TEST (fruity_delay_engines)
  13778. {
  13779. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  13780. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  13781. const int n = 16384;
  13782. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  13783. std::uint32_t seed = 1;
  13784. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  13785. {
  13786. seed = seed * 1664525u + 1013904223u;
  13787. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13788. seed = seed * 1664525u + 1013904223u;
  13789. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13790. }
  13791. using Golden = std::vector<std::tuple<int, float, float>>;
  13792. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  13793. for (const auto& [i, gl, gr] : golden)
  13794. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  13795. };
  13796. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  13797. lynx::fldelay::Delay3Engine e (44100.0);
  13798. e.setTempo (bpm);
  13799. for (int i = 0; i < 26; ++i)
  13800. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  13801. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  13802. e.setSampleRate (44100.0);
  13803. std::vector<float> l (inL), r (inR);
  13804. for (int s = 0; s < n; s += 512)
  13805. e.process (l.data() + s, r.data() + s, 512);
  13806. return std::make_pair (l, r);
  13807. };
  13808. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  13809. lynx::fldelay::Delay2State st;
  13810. st.v = raw;
  13811. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  13812. std::vector<float> l (inL), r (inR);
  13813. for (int s = 0; s < n; s += 512)
  13814. e.process (l.data() + s, r.data() + s, 512);
  13815. return std::make_pair (l, r);
  13816. };
  13817. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  13818. 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 };
  13819. auto o = run3 (a, 120.0);
  13820. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  13821. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  13822. o = run3 (a, 100.0);
  13823. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  13824. { 16000, 0.0806550607f, -0.0991330743f } });
  13825. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  13826. 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);
  13827. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  13828. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  13829. // fake stereo, unsynced, high-pass, limit distortion, no dry
  13830. 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);
  13831. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  13832. { 16000, -0.000312746735f, 0.00101647642f } });
  13833. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  13834. 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);
  13835. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  13836. { 11000, 0.0f, 0.0f } });
  13837. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  13838. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  13839. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  13840. { 16000, 0.0182533264f, 0.105384827f } });
  13841. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  13842. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  13843. { 16000, -0.029524127f, 0.0128910914f } });
  13844. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  13845. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  13846. { 13000, 0.145023212f, -0.104788139f } });
  13847. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  13848. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  13849. 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 };
  13850. state3[0] = 1;
  13851. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  13852. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  13853. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  13854. auto state2 = std::vector<std::uint8_t> (32, 0);
  13855. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  13856. std::memcpy (state2.data(), def2.data(), 32);
  13857. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  13858. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  13859. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  13860. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  13861. auto fx = lynx::makeEffect (slot);
  13862. fx->prepare (44100.0);
  13863. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  13864. l[100] = 1.0f;
  13865. for (int s = 0; s < 48000; s += 1024)
  13866. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  13867. int best = 200;
  13868. for (int i = 200; i < 48000; ++i)
  13869. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  13870. best = i;
  13871. return best - 100;
  13872. };
  13873. if (slot3 && slot2)
  13874. {
  13875. CHECK (echoAt (*slot3, 120.0) == 22050);
  13876. CHECK (echoAt (*slot3, 150.0) == 17640);
  13877. CHECK (echoAt (*slot2, 120.0) == 16538);
  13878. CHECK (echoAt (*slot2, 140.0) == 14175);
  13879. }
  13880. }
  13881. #include "UiRackMixerTests.inc"
  13882. // ---------------------------------------------------------------------------
  13883. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  13884. namespace
  13885. {
  13886. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  13887. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  13888. {
  13889. std::vector<std::uint8_t> b;
  13890. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  13891. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  13892. u32 (id);
  13893. u32 ((std::uint32_t) payload.size());
  13894. u32 (0);
  13895. b.insert (b.end(), payload.begin(), payload.end());
  13896. };
  13897. u32 (12);
  13898. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  13899. if (! classId.empty())
  13900. record (0x34, classId);
  13901. if (! clapId.empty())
  13902. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  13903. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  13904. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  13905. return b;
  13906. }
  13907. } // namespace
  13908. TEST (fl_plugin_db_nfo_parsing)
  13909. {
  13910. const std::string nfo = "ps_name=Vital\r\n"
  13911. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  13912. "ps_file_vendorname_0=Vital Audio\r\n"
  13913. "ps_file_type_0=2\r\n"
  13914. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  13915. "ps_file_type_1=2\r\n"
  13916. "ps_file_magic_1=Vita\r\n"
  13917. "ps_file_name_2=not a plugin\r\n"
  13918. "garbage line without equals\r\n";
  13919. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  13920. CHECK (entries.size() == 2);
  13921. if (entries.size() == 2)
  13922. {
  13923. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  13924. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  13925. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  13926. }
  13927. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  13928. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  13929. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  13930. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  13931. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  13932. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  13933. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  13934. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  13935. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  13936. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  13937. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  13938. }
  13939. TEST (fl_plugin_db_fst_parsing_and_matching)
  13940. {
  13941. std::vector<std::uint8_t> cid;
  13942. for (int i = 0; i < 16; ++i)
  13943. cid.push_back ((std::uint8_t) (0x10 + i));
  13944. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  13945. FlpBuilder fst;
  13946. fst.ascii (0xC9, "Fruity Wrapper");
  13947. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  13948. lynx::FlPluginDbEntry e;
  13949. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  13950. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  13951. lynx::FlPluginDbEntry bad;
  13952. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  13953. lynx::FlPluginDb db;
  13954. db.add (e);
  13955. // the .nfo of the same plugin fills in vendor and role
  13956. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  13957. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  13958. db.add (n);
  13959. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  13960. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  13961. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  13962. CHECK (db.entries().size() == 4);
  13963. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  13964. // by VST3 class ID even when the project saved another path and name
  13965. lynx::ForeignPlugin p;
  13966. p.format = lynx::ForeignPlugin::Format::vst3;
  13967. p.hasClassId = true;
  13968. std::copy (cid.begin(), cid.end(), p.classId.begin());
  13969. p.name = "Renamed";
  13970. p.path = "C:\\Other PC\\Surge.vst3";
  13971. CHECK (db.match (p, true) == &db.entries()[0]);
  13972. // by CLAP id, by VST2 id
  13973. lynx::ForeignPlugin c;
  13974. c.format = lynx::ForeignPlugin::Format::clap;
  13975. c.clapId = "org.clapper";
  13976. CHECK (db.match (c, false) == &db.entries()[1]);
  13977. lynx::ForeignPlugin v;
  13978. v.format = lynx::ForeignPlugin::Format::vst2;
  13979. v.vst2Id = 0x42566262u;
  13980. CHECK (db.match (v, false) == &db.entries()[2]);
  13981. // by name (architecture suffix ignored), only in the same role
  13982. lynx::ForeignPlugin byName;
  13983. byName.format = lynx::ForeignPlugin::Format::vst2;
  13984. byName.name = "BigVerb x64";
  13985. CHECK (db.match (byName, false) == &db.entries()[2]);
  13986. CHECK (db.match (byName, true) == nullptr);
  13987. lynx::ForeignPlugin unknown;
  13988. unknown.format = lynx::ForeignPlugin::Format::vst3;
  13989. unknown.name = "Nothing Like It";
  13990. CHECK (db.match (unknown, true) == nullptr);
  13991. // folders: the bundle's parent, deduplicated
  13992. const auto folders = db.pluginFolders();
  13993. CHECK (folders.size() == 4);
  13994. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  13995. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  13996. // targeted scan picks: same file, or same name key
  13997. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  13998. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  13999. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  14000. // the plugins an .flp uses: wrapper channels and mixer slots
  14001. flp::Project project;
  14002. flp::Channel ch;
  14003. ch.pluginName = "Fruity Wrapper";
  14004. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  14005. project.channels.push_back (ch);
  14006. ch.pluginName = "3x Osc";
  14007. project.channels.push_back (ch);
  14008. flp::MixerInsert insert;
  14009. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  14010. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  14011. project.mixer.push_back (insert);
  14012. const auto uses = lynx::flpForeignPlugins (project);
  14013. CHECK (uses.size() == 2);
  14014. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  14015. }
  14016. TEST (fl_sample_relocation)
  14017. {
  14018. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  14019. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  14020. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  14021. lynx::FlPathRoots roots;
  14022. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  14023. roots.factoryData = "C:\\Program Files\\FL 2026";
  14024. roots.home = "C:\\Users\\me";
  14025. const std::string project = "E:\\Songs\\Track";
  14026. // variable resolved, file in place
  14027. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  14028. // another PC's FL user data / FL install / user profile
  14029. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  14030. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  14031. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  14032. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  14033. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  14034. // next to the project, also in a sub-folder named in the original path
  14035. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  14036. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  14037. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  14038. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  14039. }
  14040. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  14041. namespace
  14042. {
  14043. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  14044. // ints, version)
  14045. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  14046. {
  14047. std::vector<std::uint8_t> b (564, 0);
  14048. std::memcpy (b.data(), P.data(), 360);
  14049. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  14050. std::memcpy (b.data() + 560, &version, 4);
  14051. return b;
  14052. }
  14053. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  14054. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  14055. {
  14056. std::vector<std::vector<std::uint8_t>> out;
  14057. std::ifstream in (file, std::ios::binary);
  14058. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  14059. std::size_t i = 0;
  14060. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  14061. ++i;
  14062. i += 8;
  14063. std::string name;
  14064. while (i < b.size())
  14065. {
  14066. const auto id = b[i++];
  14067. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  14068. if (id >= 192)
  14069. for (int shift = 0; i < b.size(); shift += 7)
  14070. {
  14071. const auto c = b[i++];
  14072. n |= (std::size_t) (c & 0x7F) << shift;
  14073. if (c < 0x80)
  14074. break;
  14075. }
  14076. if (i + n > b.size())
  14077. break;
  14078. if (id == 0xC9)
  14079. {
  14080. name.clear();
  14081. for (std::size_t k = 0; k < n; ++k)
  14082. if (b[i + k] != 0)
  14083. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  14084. }
  14085. if (id == 0xD5 && name == plugin)
  14086. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  14087. i += n;
  14088. }
  14089. return out;
  14090. }
  14091. } // namespace
  14092. TEST (simsynth_engine)
  14093. {
  14094. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  14095. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  14096. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  14097. 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,
  14098. 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 };
  14099. 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 };
  14100. 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,
  14101. 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 };
  14102. 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 };
  14103. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  14104. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  14105. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  14106. const auto hp = lynx::simSynthHostParams (blobA);
  14107. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  14108. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  14109. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  14110. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  14111. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  14112. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  14113. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  14114. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  14115. struct Golden { int i; float l, r; };
  14116. 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 } };
  14117. 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 } };
  14118. for (int which = 0; which < 2; ++which)
  14119. {
  14120. std::vector<std::int64_t> counters;
  14121. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  14122. CHECK (y.size() == 2 * 66150u);
  14123. if (y.size() != 2 * 66150u)
  14124. continue;
  14125. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  14126. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  14127. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  14128. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  14129. double e = 0.0;
  14130. for (auto v : y)
  14131. e += (double) v * v;
  14132. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  14133. }
  14134. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  14135. lynx::ChannelDef c;
  14136. c.instrument = lynx::InstrumentKind::simsynth;
  14137. c.pluginState = blobA;
  14138. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  14139. inst->prepare (44100.0);
  14140. inst->configure (c, 120.0);
  14141. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  14142. for (int k = 0; k < 16; ++k)
  14143. {
  14144. if (k == 0)
  14145. inst->noteOn (60, 0.8f);
  14146. if (k == 10)
  14147. inst->noteOff (60);
  14148. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  14149. }
  14150. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  14151. double diff = 0.0, energy = 0.0;
  14152. for (int i = 0; i < 8192; ++i)
  14153. {
  14154. 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]) });
  14155. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14156. }
  14157. CHECK (diff == 0.0 && energy > 1e-3);
  14158. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  14159. if (! realFlpDir.empty())
  14160. {
  14161. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  14162. int presets = 0, usable = 0, sounding = 0;
  14163. if (std::filesystem::exists (folder))
  14164. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14165. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  14166. {
  14167. ++presets;
  14168. if (! lynx::simSynthStateUsable (st))
  14169. continue;
  14170. ++usable;
  14171. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  14172. double e = 0.0;
  14173. bool finite = true;
  14174. for (auto v : y)
  14175. finite = finite && std::isfinite (v), e += (double) v * v;
  14176. CHECK (finite);
  14177. sounding += e > 1e-9 ? 1 : 0;
  14178. }
  14179. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  14180. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  14181. }
  14182. }
  14183. TEST (wasp_is_a_silent_placeholder)
  14184. {
  14185. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  14186. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  14187. inst->prepare (44100.0);
  14188. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14189. inst->noteOn (60, 1.0f);
  14190. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14191. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14192. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  14193. // their state kept
  14194. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  14195. const int version = 14;
  14196. std::memcpy (ss.data() + 560, &version, 4);
  14197. FlpBuilder b;
  14198. b.ascii (0xC7, "25.2.5.5319");
  14199. int index = 0;
  14200. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  14201. {
  14202. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14203. b.u8 (0x15); b.u8 (2);
  14204. b.utf16 (0xC9, name);
  14205. b.data (0xD5, state);
  14206. }
  14207. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  14208. CHECK (song.channels.size() == 3u);
  14209. if (song.channels.size() == 3u)
  14210. {
  14211. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  14212. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  14213. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  14214. }
  14215. }
  14216. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  14217. namespace
  14218. {
  14219. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  14220. lynx::SampleData granulizerTestSample (int n, int ch)
  14221. {
  14222. lynx::SampleData d;
  14223. std::uint32_t s = 12345;
  14224. for (int i = 0; i < n; ++i)
  14225. {
  14226. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  14227. for (int c = 0; c < ch; ++c)
  14228. {
  14229. s = s * 1664525u + 1013904223u;
  14230. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  14231. (c == 0 ? d.left : d.right).push_back (v);
  14232. }
  14233. }
  14234. return d;
  14235. }
  14236. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  14237. {
  14238. std::vector<std::uint8_t> b (60, 0);
  14239. const std::int32_t v = 5;
  14240. std::memcpy (b.data(), &v, 4);
  14241. std::memcpy (b.data() + 4, P.data(), 48);
  14242. std::memcpy (b.data() + 56, &keyMode, 4);
  14243. return b;
  14244. }
  14245. } // namespace
  14246. TEST (granulizer_engine)
  14247. {
  14248. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  14249. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  14250. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  14251. CHECK (lynx::granulizerStateUsable (v5));
  14252. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  14253. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  14254. && ! lynx::granulizerParam (n5, 12));
  14255. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  14256. {
  14257. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  14258. v1[0] = 1;
  14259. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  14260. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  14261. std::vector<std::uint8_t> bad (v5);
  14262. bad[0] = 9;
  14263. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  14264. auto edited = n5;
  14265. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  14266. }
  14267. using N = lynx::granulizer::RefNote;
  14268. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  14269. double el, er; std::tuple<int, float, float> at[6]; };
  14270. auto check = [] (const Golden& g) {
  14271. auto smp = granulizerTestSample (20000, g.ch);
  14272. smp.sampleRate = g.rate;
  14273. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  14274. CHECK (y.size() == 2 * 16384);
  14275. if (y.size() != 2 * 16384)
  14276. return;
  14277. double el = 0.0, er = 0.0;
  14278. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14279. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14280. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14281. for (const auto& [i, l, r] : g.at)
  14282. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, granulizerTolerance())
  14283. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, granulizerTolerance()));
  14284. };
  14285. lynx::granulizer::RefOptions o1, o2, o3;
  14286. o1.points = o2.points = o3.points = 12; // the plugin's own default quality (these goldens predate FL's render value)
  14287. o2.block = 333, o2.seed = 777;
  14288. o3.block = 64, o3.seed = 5;
  14289. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  14290. 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,
  14291. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  14292. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  14293. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  14294. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  14295. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  14296. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  14297. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  14298. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  14299. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  14300. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  14301. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  14302. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  14303. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  14304. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  14305. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  14306. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  14307. {
  14308. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  14309. silent->prepare (44100.0);
  14310. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  14311. silent->noteOn (60, 1.0f);
  14312. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14313. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14314. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14315. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14316. lynx::ChannelDef def;
  14317. def.instrument = lynx::InstrumentKind::granulizer;
  14318. def.pluginState = n5;
  14319. inst->configure (def, 140.0);
  14320. inst->prepare (44100.0);
  14321. inst->noteOn (60, 0.8f);
  14322. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14323. double e = 0.0;
  14324. for (auto v : l)
  14325. e += (double) v * v;
  14326. CHECK (! inst->idle() && e > 1.0);
  14327. inst->noteOff (60);
  14328. for (int k = 0; k < 4; ++k)
  14329. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14330. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  14331. }
  14332. // tempo-synced grains follow the song position in LYNX's 960-PPQ ticks: an instance fed setSongPosition every
  14333. // block while playing must stay in phase with one that free-runs its own tick clock from 0 (it was 10x off)
  14334. {
  14335. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14336. lynx::ChannelDef def;
  14337. def.instrument = lynx::InstrumentKind::granulizer;
  14338. def.pluginState = n5;
  14339. auto fed = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14340. auto free = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14341. for (auto* g : { fed.get(), free.get() })
  14342. {
  14343. g->configure (def, 140.0);
  14344. g->prepare (44100.0);
  14345. g->noteOn (60, 0.8f);
  14346. }
  14347. const auto tps = 140.0 * lynx::ppq / (60.0 * 44100.0);
  14348. std::vector<float> l1 (300), r1 (300), l2 (300), r2 (300);
  14349. double diff = 0.0, energy = 0.0;
  14350. for (int block = 0, done = 0; block < 60; ++block, done += 300)
  14351. {
  14352. fed->setSongPosition ((double) done * tps);
  14353. std::fill (l1.begin(), l1.end(), 0.0f), std::fill (r1.begin(), r1.end(), 0.0f);
  14354. std::fill (l2.begin(), l2.end(), 0.0f), std::fill (r2.begin(), r2.end(), 0.0f);
  14355. fed->render (l1.data(), r1.data(), 300, 1.0f, 1.0f);
  14356. free->render (l2.data(), r2.data(), 300, 1.0f, 1.0f);
  14357. for (int j = 0; j < 300; ++j)
  14358. diff = std::max (diff, (double) std::fabs (l1[(std::size_t) j] - l2[(std::size_t) j])),
  14359. energy += (double) l2[(std::size_t) j] * l2[(std::size_t) j];
  14360. }
  14361. CHECK (energy > 1.0 && diff < 1e-4);
  14362. }
  14363. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  14364. {
  14365. FlpBuilder b;
  14366. b.ascii (0xC7, "25.2.5.5319");
  14367. b.u8 (0x40); b.u16 (0);
  14368. b.u8 (0x15); b.u8 (2);
  14369. b.utf16 (0xC9, u"Fruity Granulizer");
  14370. b.data (0xD5, v5);
  14371. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14372. lynx::FlpImportOptions options;
  14373. options.roots.factoryData = "C:\\FL";
  14374. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  14375. lynx::FlpImportReport report;
  14376. options.report = &report;
  14377. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14378. CHECK (song.channels.size() == 1u);
  14379. if (song.channels.size() == 1u)
  14380. {
  14381. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  14382. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14383. }
  14384. CHECK (report.converted["Fruity Granulizer"] == 1);
  14385. }
  14386. }
  14387. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  14388. namespace
  14389. {
  14390. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  14391. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  14392. {
  14393. std::uint32_t s = 20261015u;
  14394. auto u = [&s] {
  14395. s = s * 1664525u + 1013904223u;
  14396. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  14397. };
  14398. // (a product rounded on its own: no fused multiply-add)
  14399. auto mul = [] (float a, float b) {
  14400. volatile float p = a * b;
  14401. return (float) p;
  14402. };
  14403. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  14404. std::array<float, 720> P {};
  14405. for (int k = 0; k < 16; ++k)
  14406. for (int i = 0; i < 44; ++i)
  14407. {
  14408. auto v = u();
  14409. if (i == 0)
  14410. v = 0.5f + mul (v, 0.5f);
  14411. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  14412. v = 0.2f + mul (v, 0.6f);
  14413. else if (i == 2 || i == 3)
  14414. v = 0.0f;
  14415. else if (i == 4)
  14416. v = (float) (36 + k);
  14417. else if (i == 5)
  14418. v = v < 0.5f ? 0.0f : 1.0f;
  14419. else if (i == 6)
  14420. v = (float) (int) (v * 4.0f);
  14421. else if (i >= 35 && i <= 38)
  14422. v = (float) targets[(int) (v * 7.0f)];
  14423. P[(std::size_t) (44 * k + i)] = v;
  14424. }
  14425. for (int i = 0; i < 16; ++i)
  14426. {
  14427. auto v = u();
  14428. if (i == 1)
  14429. v = 0.3f + mul (v, 0.4f);
  14430. else if (i == 3)
  14431. v = eq ? 1.0f : 0.0f;
  14432. else if (i >= 4 && i <= 11)
  14433. v = 0.35f + mul (v, 0.3f);
  14434. else if (i == 12)
  14435. v = 1.0f;
  14436. else if (i == 13)
  14437. v = v * 0.1f;
  14438. else if (i == 0 || i == 15)
  14439. v = 0.0f;
  14440. P[(std::size_t) (704 + i)] = v;
  14441. }
  14442. std::vector<std::uint8_t> b;
  14443. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  14444. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  14445. put32 (21636u);
  14446. put32 (0x444d5333u); // engine stream version 3
  14447. const std::string name = "LYNX test kit";
  14448. b.insert (b.end(), name.begin(), name.end());
  14449. b.resize (b.size() + 256 - name.size(), 0);
  14450. const auto at = b.size();
  14451. b.resize (at + 720 * 4);
  14452. std::memcpy (b.data() + at, P.data(), 720 * 4);
  14453. for (int k = 0; k < 16; ++k)
  14454. {
  14455. b.resize (b.size() + 0x400, 0);
  14456. b.push_back (100), b.push_back (100); // pattern length, trigger key
  14457. }
  14458. b.resize (b.size() + 0x800, 0);
  14459. for (int k = 0; k < 16; ++k)
  14460. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  14461. b.resize (b.size() + 16, 0); // every pad on the master
  14462. return b;
  14463. }
  14464. } // namespace
  14465. TEST (drumaxx_engine)
  14466. {
  14467. const auto st = drumaxxTestState (true);
  14468. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  14469. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  14470. {
  14471. auto edited = st;
  14472. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  14473. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  14474. auto bad = st;
  14475. bad[8] = 'X';
  14476. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  14477. }
  14478. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  14479. using N = lynx::drumaxx::RefNote;
  14480. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  14481. auto check = [] (const Golden& g) {
  14482. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  14483. CHECK (y.size() == 2 * 20000);
  14484. if (y.size() != 2 * 20000)
  14485. return;
  14486. double el = 0.0, er = 0.0;
  14487. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14488. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14489. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14490. for (const auto& [i, l, r] : g.at)
  14491. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  14492. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  14493. };
  14494. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  14495. check ({ true, 44100, 512,
  14496. { { 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 },
  14497. { 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 },
  14498. { 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 } },
  14499. 2698.372377185949, 3678.285731410973,
  14500. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  14501. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  14502. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  14503. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  14504. check ({ false, 48000, 333,
  14505. { { 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 },
  14506. { 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 } },
  14507. 2241.008810618806, 1814.5219804412964,
  14508. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  14509. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  14510. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  14511. // 96 kHz in 64-sample blocks, an octave down
  14512. check ({ true, 96000, 64,
  14513. { { 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 },
  14514. { 10000, 51, 1.0, 1.0, -1200.0 } },
  14515. 3522.8420617665834, 5746.555795626526,
  14516. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  14517. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  14518. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  14519. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  14520. {
  14521. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  14522. lynx::ChannelDef def;
  14523. def.instrument = lynx::InstrumentKind::drumaxx;
  14524. def.pluginState = st;
  14525. inst->configure (def, 140.0);
  14526. inst->prepare (44100.0);
  14527. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  14528. inst->noteOn (90, 1.0f);
  14529. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14530. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14531. inst->noteOn (36, 0.8f);
  14532. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14533. double e = 0.0;
  14534. for (auto v : l)
  14535. e += (double) v * v;
  14536. CHECK (! inst->idle() && e > 1e-3);
  14537. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  14538. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14539. CHECK (inst->idle());
  14540. }
  14541. // import: a Drumaxx channel plays on the exact engine with its raw state
  14542. {
  14543. FlpBuilder b;
  14544. b.ascii (0xC7, "25.2.5.5319");
  14545. b.u8 (0x40); b.u16 (0);
  14546. b.u8 (0x15); b.u8 (2);
  14547. b.utf16 (0xC9, u"Drumaxx");
  14548. b.data (0xD5, st);
  14549. lynx::FlpImportOptions options;
  14550. lynx::FlpImportReport report;
  14551. options.report = &report;
  14552. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14553. CHECK (song.channels.size() == 1u);
  14554. if (song.channels.size() == 1u)
  14555. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  14556. CHECK (report.converted["Drumaxx"] == 1);
  14557. }
  14558. }
  14559. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  14560. namespace
  14561. {
  14562. std::string slayerDll()
  14563. {
  14564. if (realFlpDir.empty())
  14565. return {};
  14566. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  14567. }
  14568. std::vector<std::uint8_t> slayerPreset (const char* name)
  14569. {
  14570. if (realFlpDir.empty())
  14571. return {};
  14572. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  14573. const auto states = pluginStatesIn (file, "FL Slayer");
  14574. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  14575. }
  14576. } // namespace
  14577. TEST (slayer_state)
  14578. {
  14579. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  14580. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  14581. const std::int32_t one = 1, wheel = 77, mode = 5;
  14582. std::memcpy (v1.data(), &one, 4);
  14583. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  14584. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  14585. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  14586. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  14587. auto v2 = v1;
  14588. v2[0] = 2;
  14589. CHECK (! lynx::slayerStateUsable (v2));
  14590. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  14591. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  14592. // the controls' maxima (the front-end sends value / max)
  14593. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  14594. && lynx::slayerParamMax (3) == 65535);
  14595. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  14596. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  14597. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  14598. }
  14599. TEST (slayer_engine)
  14600. {
  14601. const auto dll = slayerDll();
  14602. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14603. return; // no FL installation
  14604. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  14605. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  14606. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  14607. struct Sample { int at; float l, r; };
  14608. struct Golden { const char* preset; double el, er; Sample s[4]; };
  14609. const Golden goldens[] {
  14610. { "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 } } },
  14611. { "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 } } },
  14612. { "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 } } },
  14613. { "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 } } },
  14614. { "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 } } },
  14615. { "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 } } },
  14616. { "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 } } },
  14617. { "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 } } },
  14618. { "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 } } },
  14619. { "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 } } },
  14620. { "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 } } },
  14621. { "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 } } },
  14622. };
  14623. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  14624. constexpr int length = 44100;
  14625. int checked = 0;
  14626. for (const auto& g : goldens)
  14627. {
  14628. const auto st = slayerPreset (g.preset);
  14629. CHECK (lynx::slayerStateUsable (st));
  14630. if (! lynx::slayerStateUsable (st))
  14631. continue;
  14632. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  14633. double el = 0.0, er = 0.0;
  14634. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14635. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14636. 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;
  14637. for (const auto& smp : g.s)
  14638. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  14639. if (! ok)
  14640. 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);
  14641. CHECK (ok);
  14642. ++checked;
  14643. }
  14644. CHECK (checked == 12);
  14645. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  14646. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  14647. // velocity 0.8 is the reference note one sample later
  14648. const auto st = slayerPreset ("BASS Ebass 2");
  14649. lynx::ChannelDef c;
  14650. c.instrument = lynx::InstrumentKind::slayer;
  14651. c.pluginState = st;
  14652. c.samplePath = dll;
  14653. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14654. inst->configure (c, 120.0);
  14655. inst->prepare (44100.0);
  14656. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  14657. for (int b = 0; b < 32; ++b)
  14658. {
  14659. if (b == 0)
  14660. inst->noteOn (60, 0.8f);
  14661. if (b == 20)
  14662. inst->noteOff (60);
  14663. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  14664. }
  14665. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14666. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  14667. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  14668. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  14669. float hl = 0.0f, hr = 0.0f;
  14670. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  14671. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  14672. double diff = 0.0, energy = 0.0;
  14673. for (int i = 0; i < 16384; ++i)
  14674. {
  14675. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14676. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14677. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14678. }
  14679. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  14680. // every preset of the installation: usable and sounding
  14681. int presets = 0, sounding = 0;
  14682. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  14683. if (std::filesystem::exists (folder))
  14684. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14685. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  14686. {
  14687. ++presets;
  14688. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  14689. double e = 0.0;
  14690. bool finite = true;
  14691. for (auto v : y)
  14692. finite = finite && std::isfinite (v), e += (double) v * v;
  14693. CHECK (finite);
  14694. sounding += e > 1.0 ? 1 : 0;
  14695. }
  14696. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  14697. CHECK (presets == 64 && sounding == 64);
  14698. }
  14699. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  14700. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  14701. TEST (slayer_host_levels_and_automation)
  14702. {
  14703. using namespace lynx::slayer;
  14704. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  14705. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  14706. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  14707. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  14708. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  14709. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  14710. hostGains (64, 1.0f, l1, r1);
  14711. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  14712. const auto dll = slayerDll();
  14713. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14714. return;
  14715. const auto st = slayerPreset ("DIST Dire Straits");
  14716. const auto params = lynx::slayerStateParams (st);
  14717. lynx::ChannelDef c;
  14718. c.instrument = lynx::InstrumentKind::slayer;
  14719. c.pluginState = st;
  14720. c.samplePath = dll;
  14721. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  14722. c.modelParams = { 0.0f };
  14723. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14724. inst->configure (c, 120.0);
  14725. inst->prepare (44100.0);
  14726. const int blocks = 40, n = 512;
  14727. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  14728. auto edited = st;
  14729. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  14730. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  14731. for (int b = 0; b < blocks; ++b)
  14732. {
  14733. if (b == 0)
  14734. inst->noteOn (52, 0.8f);
  14735. if (b == 10)
  14736. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  14737. if (b == 18)
  14738. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  14739. if (b == 25)
  14740. {
  14741. c.pluginState = edited;
  14742. inst->configure (c, 120.0);
  14743. }
  14744. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  14745. }
  14746. lynx::slayer::RefOptions opt;
  14747. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  14748. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  14749. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14750. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  14751. float hl = 0.0f, hr = 0.0f;
  14752. hostGains (0, 1.0f, hl, hr);
  14753. double diff = 0.0, energy = 0.0;
  14754. for (int i = 0; i < blocks * n; ++i)
  14755. {
  14756. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14757. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14758. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14759. }
  14760. CHECK (diff == 0.0 && energy > 1.0);
  14761. // the changes really change the sound: the same render without them differs
  14762. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  14763. CHECK (plain != ref);
  14764. }
  14765. TEST (slayer_and_fruity_dance_import)
  14766. {
  14767. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  14768. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  14769. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  14770. FlpBuilder b;
  14771. b.ascii (0xC7, "25.2.5.5319");
  14772. int index = 0;
  14773. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  14774. {
  14775. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14776. b.u8 (0x15); b.u8 (2);
  14777. b.utf16 (0xC9, name);
  14778. b.data (0xD5, state);
  14779. }
  14780. const auto project = flp::parseProject (b.bytes);
  14781. const auto plain = lynx::importFlp (project);
  14782. CHECK (plain.channels.size() == 2u);
  14783. if (plain.channels.size() == 2u)
  14784. {
  14785. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  14786. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  14787. }
  14788. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  14789. inst->prepare (44100.0);
  14790. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14791. inst->noteOn (60, 1.0f);
  14792. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14793. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14794. const auto dll = slayerDll();
  14795. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14796. return;
  14797. lynx::FlpImportOptions withFiles;
  14798. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  14799. withFiles.fileExists = [] (const std::string& path) {
  14800. auto local = path;
  14801. std::replace (local.begin(), local.end(), '\\', '/');
  14802. return std::filesystem::exists (local);
  14803. };
  14804. const auto song = lynx::importFlp (project, withFiles);
  14805. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  14806. && ! song.channels[0].samplePath.empty());
  14807. }
  14808. #include "WaveAnalysisTests.inc"
  14809. #include "UiPolish2Tests.inc"
  14810. #include "UiToolbarTests.inc"
  14811. #include "LuxeVerbTests.inc"
  14812. #include "Parity172bTests.inc"
  14813. #include "ConvolverTests.inc"
  14814. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  14815. namespace newtonetest
  14816. {
  14817. 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))); }
  14818. 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))); }
  14819. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  14820. {
  14821. put32 (b, id);
  14822. put64 (b, (std::int64_t) d.size());
  14823. b.insert (b.end(), d.begin(), d.end());
  14824. }
  14825. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  14826. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  14827. {
  14828. std::vector<std::uint8_t> b, r1, r2;
  14829. put32 (b, version);
  14830. put32 (r1, 727);
  14831. put32 (r1, 582);
  14832. put32 (r1, (std::int32_t) flags);
  14833. r1.resize (0x28, 0);
  14834. record (b, 1, r1);
  14835. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  14836. put32 (chunk, (std::int32_t) xml.size() + 1);
  14837. chunk.insert (chunk.end(), xml.begin(), xml.end());
  14838. chunk.push_back (0);
  14839. put32 (r2, -9);
  14840. r2.push_back (0x0c);
  14841. put32 (r2, -2);
  14842. put64 (r2, (std::int64_t) chunk.size());
  14843. put32 (r2, 0);
  14844. r2.insert (r2.end(), chunk.begin(), chunk.end());
  14845. record (b, 2, r2);
  14846. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  14847. record (b, 4, std::vector<std::uint8_t> (12, 0));
  14848. record (b, 5, std::vector<std::uint8_t> (16, 0));
  14849. record (b, 6, std::vector<std::uint8_t> (32, 0));
  14850. return b;
  14851. }
  14852. const char* xmlWithTrack (bool edited)
  14853. {
  14854. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  14855. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  14856. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  14857. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  14858. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  14859. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  14860. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  14861. }
  14862. } // namespace newtonetest
  14863. TEST (newtone_state)
  14864. {
  14865. using namespace newtonetest;
  14866. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  14867. CHECK (a.has_value());
  14868. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  14869. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  14870. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  14871. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  14872. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  14873. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  14874. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  14875. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  14876. // an empty project, as the installation's presets
  14877. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  14878. CHECK (c.has_value() && c->tracks.empty());
  14879. // broken: other version, truncated record, no options record
  14880. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  14881. auto cut = state (0, 1, xmlWithTrack (true), "");
  14882. cut.resize (cut.size() - 3);
  14883. CHECK (! lynx::newtonefx::parse (cut));
  14884. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  14885. // the FL import keeps the state and takes the slave switch as the editable parameter
  14886. const auto raw = state (0, 5, xmlWithTrack (false), "");
  14887. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  14888. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  14889. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  14890. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  14891. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  14892. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  14893. if (! realFlpDir.empty())
  14894. {
  14895. int found = 0, read = 0;
  14896. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  14897. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  14898. {
  14899. const auto path = std::filesystem::path (realFlpDir) / rel;
  14900. if (! std::filesystem::exists (path))
  14901. continue;
  14902. ++found;
  14903. std::ifstream f (path, std::ios::binary);
  14904. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  14905. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  14906. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  14907. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  14908. }
  14909. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  14910. CHECK (read == found);
  14911. }
  14912. }
  14913. TEST (newtone_engine)
  14914. {
  14915. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  14916. // sample is added from the song time on (at the source's own rate)
  14917. auto sample = std::make_shared<lynx::SampleData>();
  14918. sample->sampleRate = 44100.0;
  14919. for (int i = 0; i < 3000; ++i)
  14920. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  14921. const double sr = 44100.0, bpm = 120.0;
  14922. const auto tps = lynx::ticksPerSample (bpm, sr);
  14923. auto fx = lynx::newtonefx::make (sample);
  14924. fx->prepare (sr);
  14925. lynx::EffectParams on {}, off {};
  14926. on[0] = 1.0f;
  14927. std::vector<float> l (256), r (256), in (256);
  14928. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  14929. for (int i = 0; i < 256; ++i)
  14930. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  14931. fx->setSongPosition (tick);
  14932. fx->setPlaying (playing);
  14933. fx->process (l.data(), r.data(), 256, p, bpm);
  14934. };
  14935. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  14936. block (0.0, true, off);
  14937. CHECK (same()); // slave off
  14938. block (0.0, false, on);
  14939. CHECK (same()); // not playing
  14940. // playing from the start: in + source, sample by sample, over consecutive blocks
  14941. bool exact = true;
  14942. for (int b = 0; b < 4; ++b)
  14943. {
  14944. block (b * 256 * tps, true, on);
  14945. for (int i = 0; i < 256; ++i)
  14946. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  14947. && r[(std::size_t) i] == l[(std::size_t) i];
  14948. }
  14949. CHECK (exact);
  14950. // a jump re-seeks to the song time; past the end of the sample only the input remains
  14951. block (2000 * tps, true, on);
  14952. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  14953. block (2900 * tps, true, on);
  14954. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  14955. // stopping and playing again: from the new song time
  14956. block (0.0, false, on);
  14957. CHECK (same());
  14958. block (10 * tps, true, on);
  14959. CHECK (l[0] == in[0] + sample->left[10]);
  14960. // without its sample: a pass-through
  14961. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  14962. none->prepare (sr);
  14963. for (int i = 0; i < 256; ++i)
  14964. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  14965. none->setPlaying (true);
  14966. none->process (l.data(), r.data(), 256, on, bpm);
  14967. CHECK (same());
  14968. }
  14969. TEST (newtone_rack_follows_transport)
  14970. {
  14971. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  14972. auto song = lynx::Song::makeDefault();
  14973. for (auto& p : song.patterns)
  14974. p.notes.clear();
  14975. song.bpm = 120.0;
  14976. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  14977. slot.params[0] = 1.0f;
  14978. slot.samplePath = "newtone-test.wav";
  14979. song.mixer[1].effects.push_back (slot);
  14980. auto sample = std::make_shared<lynx::SampleData>();
  14981. sample->sampleRate = 48000.0;
  14982. sample->left.assign (48000, 0.5f);
  14983. int loads = 0;
  14984. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  14985. ++loads;
  14986. return path == "newtone-test.wav" ? sample : nullptr;
  14987. };
  14988. const double sr = 48000.0;
  14989. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  14990. CHECK (loads == 1);
  14991. lynx::RackRenderer rr;
  14992. rr.prepare (sr);
  14993. std::vector<float> l (512), r (512);
  14994. auto peakOf = [&] (bool playing, double tick) {
  14995. float peak = 0.0f;
  14996. for (int b = 0; b < 8; ++b)
  14997. {
  14998. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  14999. for (auto v : l)
  15000. peak = std::max (peak, std::abs (v));
  15001. }
  15002. return peak;
  15003. };
  15004. CHECK (peakOf (false, 0.0) == 0.0f);
  15005. CHECK (peakOf (true, 0.0) > 0.1f);
  15006. }
  15007. #include "HardcoreTests.inc"
  15008. #include "DistructorTests.inc"
  15009. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  15010. TEST (missing_files_names_and_scores)
  15011. {
  15012. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  15013. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  15014. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  15015. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  15016. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  15017. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  15018. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  15019. }
  15020. TEST (missing_files_find_in_song)
  15021. {
  15022. lynx::Song song;
  15023. lynx::ChannelDef a, b, c, d, e;
  15024. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  15025. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  15026. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  15027. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  15028. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  15029. lynx::SampleZone z;
  15030. z.path = "C:\\gone\\piano_c4.wav";
  15031. a.zones.push_back (z);
  15032. song.channels = { a, b, c, d, e };
  15033. song.mixer.resize (3);
  15034. lynx::EffectSlot fx;
  15035. fx.samplePath = "C:\\gone\\tune.wav";
  15036. lynx::EffectSlot parallel;
  15037. parallel.branches.push_back ({ { fx }, 1.0f });
  15038. song.mixer[2].effects.push_back (parallel);
  15039. song.mixer[2].name = "Lead";
  15040. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  15041. auto missing = lynx::findMissingSamples (song, exists);
  15042. CHECK (missing.size() == 4);
  15043. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  15044. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  15045. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  15046. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  15047. // candidates from a search: the deeper path match wins; one stays missing
  15048. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  15049. CHECK (lynx::relocateByName (missing, hits) == 3);
  15050. CHECK (missing[0].found == "/lib/gone/kick.wav");
  15051. CHECK (missing[1].found.empty());
  15052. missing[3].skipped = true; // skipped: not applied
  15053. const auto map = lynx::relocationMap (missing);
  15054. CHECK (map.size() == 2);
  15055. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  15056. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  15057. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  15058. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  15059. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  15060. lynx::Song copy = song;
  15061. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  15062. }
  15063. TEST (missing_files_inside_patcher)
  15064. {
  15065. // a Patcher's instruments and effects (any depth) are the song's samples too: listed, packed and relinked
  15066. lynx::Song song;
  15067. lynx::ChannelDef inner;
  15068. inner.name = "Pad", inner.instrument = lynx::InstrumentKind::sampler, inner.samplePath = "C:\\gone\\pad.wav";
  15069. lynx::ChannelDef nested;
  15070. nested.instrument = lynx::InstrumentKind::patcher;
  15071. nested.patcher.nodes.resize (1);
  15072. nested.patcher.nodes[0].generator = true;
  15073. nested.patcher.nodes[0].channel = inner;
  15074. lynx::ChannelDef patch;
  15075. patch.name = "Rack", patch.instrument = lynx::InstrumentKind::patcher;
  15076. patch.patcher.nodes.resize (2);
  15077. patch.patcher.nodes[0].generator = true;
  15078. patch.patcher.nodes[0].channel = nested;
  15079. patch.patcher.nodes[1].effect.samplePath = "C:\\gone\\tune.wav";
  15080. patch.patcher.nodes[1].busHeads.push_back ({});
  15081. patch.patcher.nodes[1].busHeads[0].head = lynx::EffectSlot();
  15082. patch.patcher.nodes[1].busHeads[0].head->samplePath = "C:\\gone\\tune.wav"; // same file: one entry
  15083. song.channels = { patch };
  15084. song.mixer.resize (2);
  15085. lynx::EffectSlot fxPatcher;
  15086. fxPatcher.patcher.nodes.resize (1);
  15087. fxPatcher.patcher.nodes[0].generator = true;
  15088. fxPatcher.patcher.nodes[0].channel = inner;
  15089. fxPatcher.patcher.nodes[0].channel.samplePath = "C:\\gone\\hit.wav";
  15090. song.mixer[1].effects.push_back (fxPatcher);
  15091. const auto missing = lynx::findMissingSamples (song, [] (const std::string&) { return false; });
  15092. CHECK (missing.size() == 3);
  15093. CHECK (missing.size() == 3 && missing[0].path == "C:\\gone\\pad.wav" && missing[0].usedBy[0] == "Rack (Patcher)");
  15094. CHECK (missing.size() == 3 && missing[1].path == "C:\\gone\\tune.wav" && missing[1].usedBy[0] == "Rack (Patcher): effect");
  15095. CHECK (missing.size() == 3 && missing[2].path == "C:\\gone\\hit.wav" && missing[2].usedBy[0] == "Insert 1 (Patcher)");
  15096. CHECK (lynx::applyRelocations (song, { { "C:\\gone\\pad.wav", "/p.wav" }, { "C:\\gone\\tune.wav", "/t.wav" }, { "C:\\gone\\hit.wav", "/h.wav" } }) == 4);
  15097. CHECK (song.channels[0].patcher.nodes[1].busHeads[0].head->samplePath == "/t.wav");
  15098. CHECK (song.channels[0].patcher.nodes[0].channel.patcher.nodes[0].channel.samplePath == "/p.wav");
  15099. CHECK (song.channels[0].patcher.nodes[1].effect.samplePath == "/t.wav");
  15100. CHECK (song.mixer[1].effects[0].patcher.nodes[0].channel.samplePath == "/h.wav");
  15101. }
  15102. TEST (missing_files_folder_search)
  15103. {
  15104. namespace fs = std::filesystem;
  15105. const auto root = fs::temp_directory_path() / ("lynx-missing-test-" + processTempTag());
  15106. fs::remove_all (root);
  15107. fs::create_directories (root / "a" / "deep" / "Kicks");
  15108. fs::create_directories (root / "b");
  15109. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  15110. std::ofstream (f) << "x";
  15111. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  15112. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  15113. int listed = 0;
  15114. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  15115. for (const auto& entry : fs::directory_iterator (folder))
  15116. {
  15117. ++listed;
  15118. if (! onEntry (entry.path().string(), entry.is_directory()))
  15119. return;
  15120. }
  15121. };
  15122. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  15123. CHECK (hits.size() == 3);
  15124. CHECK (lynx::relocateByName (missing, hits) == 2);
  15125. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  15126. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  15127. CHECK (missing[2].found.empty());
  15128. // a bounded walk stops early; a cancelled one finds nothing
  15129. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  15130. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  15131. // nothing left to find: no walk at all
  15132. missing[2].skipped = true;
  15133. listed = 0;
  15134. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  15135. fs::remove_all (root);
  15136. }
  15137. TEST (missing_files_folder_list)
  15138. {
  15139. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  15140. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  15141. auto list = folders;
  15142. lynx::rememberFolder (list, "D:\\x");
  15143. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  15144. lynx::rememberFolder (list, "E:\\new", 2);
  15145. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  15146. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  15147. }
  15148. TEST (missing_files_flp_report_and_relocation)
  15149. {
  15150. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  15151. flp::Project project;
  15152. flp::Channel ch;
  15153. ch.index = 0;
  15154. ch.type = flp::ChannelType::sampler;
  15155. ch.name = "Snare";
  15156. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  15157. project.channels.push_back (ch);
  15158. lynx::FlpImportOptions options;
  15159. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  15160. lynx::FlpImportReport report;
  15161. options.report = &report;
  15162. auto song = lynx::importFlp (project, options);
  15163. CHECK (report.missingFiles.size() == 1);
  15164. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  15165. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  15166. lynx::FlpImportReport again;
  15167. options.report = &again;
  15168. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  15169. song = lynx::importFlp (project, options);
  15170. CHECK (again.missingFiles.empty());
  15171. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  15172. }
  15173. TEST (project_info_history_and_flp_import)
  15174. {
  15175. lynx::Song a;
  15176. auto b = a;
  15177. b.info.title = "Night Drive";
  15178. CHECK (lynx::describeChangeText (a, b) == "Project info");
  15179. auto c = a;
  15180. c.beatsPerBar = 3;
  15181. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  15182. flp::Project project;
  15183. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  15184. const auto song = lynx::importFlp (project);
  15185. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  15186. CHECK (song.info.comments == "line 1\nline 2");
  15187. }
  15188. #include "UiFixTests.inc"
  15189. // ---------------------------------------------------------------------------
  15190. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  15191. namespace
  15192. {
  15193. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  15194. {
  15195. lynx::Note n;
  15196. n.start = start;
  15197. n.length = length;
  15198. n.key = key;
  15199. n.velocity = velocity;
  15200. return n;
  15201. }
  15202. std::vector<int> allOf (const lynx::Pattern& p)
  15203. {
  15204. std::vector<int> v;
  15205. for (int i = 0; i < (int) p.notes.size(); ++i)
  15206. v.push_back (i);
  15207. return v;
  15208. }
  15209. } // namespace
  15210. TEST (prtools_score_file)
  15211. {
  15212. using namespace lynx::prtools;
  15213. FlpBuilder b (96);
  15214. b.ascii (0xC7, "20.9.0");
  15215. b.u8 (0x41); b.u16 (0);
  15216. auto notes = noteRecord (0, 0, 24, 60, 100);
  15217. auto n2 = noteRecord (48, 0, 24, 67, 64);
  15218. n2[19] = 4; // colour 4
  15219. notes.insert (notes.end(), n2.begin(), n2.end());
  15220. b.data (0xE0, notes);
  15221. const auto score = parseScore (b.bytes);
  15222. CHECK (score && score->notes.size() == 2);
  15223. if (score && score->notes.size() == 2)
  15224. {
  15225. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  15226. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  15227. CHECK (score->length() == 720);
  15228. }
  15229. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  15230. const auto grid = gridScore (240);
  15231. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  15232. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  15233. if (! realFlpDir.empty()) // the installation's templates
  15234. {
  15235. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  15236. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  15237. const auto fsc = parseScore (bytes);
  15238. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  15239. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  15240. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  15241. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  15242. const auto chop = parseScore (chopBytes);
  15243. CHECK (chop && ! chop->notes.empty());
  15244. }
  15245. }
  15246. TEST (prtools_quantize)
  15247. {
  15248. using namespace lynx;
  15249. using namespace lynx::prtools;
  15250. const auto grid = gridScore (240);
  15251. Pattern p;
  15252. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15253. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  15254. quantize (p, allOf (p), grid, o);
  15255. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  15256. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  15257. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15258. o.start = 1.0f;
  15259. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  15260. quantize (p, allOf (p), grid, o);
  15261. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  15262. p.notes = { prNote (1010, 500) };
  15263. o.sensitivity = 36;
  15264. o.durationMode = QuantizeOptions::leaveEnd;
  15265. quantize (p, allOf (p), grid, o);
  15266. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  15267. p.notes = { prNote (1010, 500) };
  15268. o.durationMode = QuantizeOptions::quantizeEnd;
  15269. o.duration = 1.0f;
  15270. quantize (p, allOf (p), grid, o);
  15271. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  15272. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  15273. Score half = grid;
  15274. for (auto& n : half.notes)
  15275. n.velocity = 50.0f / 128.0f;
  15276. p.notes = { prNote (960, 100, 60, 0.8f) };
  15277. o = {};
  15278. quantize (p, allOf (p), half, o);
  15279. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  15280. for (auto& n : half.notes)
  15281. n.velocity = 0.0f;
  15282. quantize (p, allOf (p), half, o);
  15283. CHECK (p.notes.empty());
  15284. }
  15285. TEST (prtools_chop)
  15286. {
  15287. using namespace lynx;
  15288. using namespace lynx::prtools;
  15289. Score pat;
  15290. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  15291. Pattern p;
  15292. p.notes = { prNote (0, 1920, 50) };
  15293. ChopOptions o;
  15294. chop (p, allOf (p), pat, o);
  15295. CHECK (p.notes.size() == 5);
  15296. if (p.notes.size() == 5)
  15297. {
  15298. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  15299. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  15300. }
  15301. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  15302. p.notes = { prNote (100, 600, 50) };
  15303. chop (p, allOf (p), pat, o);
  15304. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  15305. o.absolute = false;
  15306. p.notes = { prNote (100, 600, 50) };
  15307. chop (p, allOf (p), pat, o);
  15308. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  15309. && p.notes[1].length == 120);
  15310. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  15311. o = {};
  15312. o.timeMul = 5;
  15313. p.notes = { prNote (0, 1920, 60) };
  15314. chop (p, allOf (p), pat, o);
  15315. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  15316. pat.notes[1].colour = 4;
  15317. o.timeMul = 4;
  15318. p.notes = { prNote (0, 1920, 60) };
  15319. chop (p, allOf (p), pat, o);
  15320. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  15321. }
  15322. TEST (prtools_flam)
  15323. {
  15324. using namespace lynx;
  15325. using namespace lynx::prtools;
  15326. Pattern p;
  15327. p.notes = { prNote (960, 480, 60, 0.75f) };
  15328. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  15329. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  15330. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  15331. p.notes = { prNote (960, 480) };
  15332. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  15333. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  15334. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  15335. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  15336. p.notes = { prNote (960, 480) };
  15337. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  15338. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  15339. p.notes = { prNote (960, 480) };
  15340. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  15341. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  15342. }
  15343. TEST (prtools_lfo)
  15344. {
  15345. using namespace lynx;
  15346. using namespace lynx::prtools;
  15347. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  15348. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  15349. const auto c = lfoCurve (o, 3840);
  15350. CHECK (std::abs (c[0] - 0.25f) < 1e-4f && std::abs (c[960] - 0.5f) < 1e-3f && std::abs (c[1920] - 0.75f) < 1e-3f);
  15351. o.shape = LfoOptions::square;
  15352. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  15353. const auto sq = lfoCurve (o, 3840);
  15354. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  15355. o = {};
  15356. o.useEnd = true;
  15357. o.endRange = 0; // the range fades out
  15358. const auto fade = lfoCurve (o, 3840);
  15359. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  15360. Pattern p;
  15361. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  15362. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  15363. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  15364. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  15365. }
  15366. TEST (prtools_claw)
  15367. {
  15368. using namespace lynx;
  15369. using namespace lynx::prtools;
  15370. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  15371. Pattern p;
  15372. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  15373. claw (p, allOf (p), o);
  15374. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  15375. if (p.notes.size() == 3)
  15376. {
  15377. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  15378. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  15379. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  15380. }
  15381. o.trash = 9; // none: only the warp, which is neutral
  15382. p.notes = { prNote (100, 300) };
  15383. claw (p, allOf (p), o);
  15384. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  15385. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  15386. p.notes = { prNote (0, 480), prNote (960, 480) };
  15387. claw (p, allOf (p), o);
  15388. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  15389. o.trash = 0; // the first quarter
  15390. o.timeDistortion = 0;
  15391. o.stretch = false;
  15392. p.notes = { prNote (0, 240), prNote (240, 240) };
  15393. claw (p, allOf (p), o);
  15394. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  15395. }
  15396. TEST (prtools_articulate)
  15397. {
  15398. using namespace lynx;
  15399. using namespace lynx::prtools;
  15400. Pattern p;
  15401. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15402. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), lynx::Meter (4, 4));
  15403. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  15404. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  15405. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), lynx::Meter (4, 4));
  15406. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  15407. auto o = articulatePreset (ArticulatePreset::legato);
  15408. o.gap = 20;
  15409. p.notes = { prNote (0, 100), prNote (480, 100) };
  15410. articulate (p, allOf (p), o, lynx::Meter (4, 4));
  15411. CHECK (p.notes[0].length == 460);
  15412. // chop chords: a held note is cut where the next one starts
  15413. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  15414. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), lynx::Meter (4, 4));
  15415. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  15416. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  15417. o = articulatePreset (ArticulatePreset::legato);
  15418. o.variation = 50;
  15419. o.seed = 7;
  15420. Pattern a, b2;
  15421. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15422. articulate (a, allOf (a), o, lynx::Meter (4, 4));
  15423. articulate (b2, allOf (b2), o, lynx::Meter (4, 4));
  15424. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  15425. }
  15426. TEST (prtools_chords)
  15427. {
  15428. using namespace lynx::prtools;
  15429. auto c = parseProgression ("I-V-vi-IV", 0, false);
  15430. CHECK (c && c->size() == 4);
  15431. if (c && c->size() == 4)
  15432. {
  15433. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  15434. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  15435. }
  15436. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  15437. CHECK (c && c->size() == 4);
  15438. if (c && c->size() == 4)
  15439. {
  15440. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  15441. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  15442. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  15443. }
  15444. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  15445. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  15446. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  15447. == std::vector<int> ({ 0, 2, 7, 14 }));
  15448. CHECK (! parseProgression ("I-X-V", 0, false));
  15449. const auto presets = parseProgressionPresets ("-Major\r\n\r\nI-V-vi-IV: Hopeful\r\nI-vi-IV-V: Optimistic\r\n\r\n-Minor\r\n\r\ni-iv-v-i\r\n");
  15450. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  15451. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  15452. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  15453. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  15454. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  15455. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  15456. {
  15457. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  15458. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  15459. CHECK (back && *back == gen);
  15460. }
  15461. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  15462. lynx::Pattern p;
  15463. const auto chords = *parseProgression ("I-IV", 0, false);
  15464. ChordWriteOptions w;
  15465. const auto added = writeProgression (p, 2, 0, chords, w);
  15466. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  15467. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  15468. w.rhythm = ">-`->-`->-`->-`-";
  15469. lynx::Pattern r;
  15470. writeProgression (r, 0, 0, chords, w);
  15471. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  15472. }
  15473. TEST (prtools_riff_machine)
  15474. {
  15475. using namespace lynx;
  15476. using namespace lynx::prtools;
  15477. Pattern p;
  15478. p.notes = { prNote (0, 100, 70) };
  15479. p.notes[0].channel = 1;
  15480. RiffOptions o;
  15481. o.channel = 1;
  15482. o.bars = 2;
  15483. const auto notes = riffMachine (p, { 0 }, o, lynx::Meter (4, 4));
  15484. CHECK (! notes.empty() && notes.size() == p.notes.size());
  15485. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  15486. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  15487. }));
  15488. // only the first step: the progression shape, chopped from one note at key 48
  15489. o.stepsUpTo = 0;
  15490. Pattern q;
  15491. const auto first = riffMachine (q, {}, o, lynx::Meter (4, 4));
  15492. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  15493. }
  15494. TEST (event_editor_lanes)
  15495. {
  15496. using namespace lynx;
  15497. Pattern p;
  15498. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  15499. auto& lane = eventedit::laneFor (p, vol);
  15500. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  15501. eventedit::fill (lane, 0, 960, 0.25f, 240);
  15502. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  15503. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  15504. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  15505. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  15506. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  15507. eventedit::setEvent (lane, 1440, 0.1f);
  15508. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  15509. eventedit::setEvent (lane, 100, 0.9f);
  15510. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  15511. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  15512. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  15513. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  15514. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  15515. eventedit::erase (lane, 0, 960);
  15516. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  15517. auto copy = lane;
  15518. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  15519. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  15520. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  15521. prtools::LfoOptions o;
  15522. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  15523. CHECK (lane.events.size() == 4 && std::abs (lane.events[0].value - 0.25f) < 1e-4f && std::abs (lane.events[2].value - 0.75f) < 1e-3f);
  15524. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  15525. eventedit::removeEmptyLanes (p);
  15526. CHECK (p.events.size() == 1);
  15527. // undo label
  15528. auto before = Song::makeDefault();
  15529. auto after = before;
  15530. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  15531. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  15532. // channel removal re-targets the lanes like automations
  15533. auto song = Song::makeDefault();
  15534. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  15535. song.removeChannel (1);
  15536. CHECK (song.patterns[0].events[0].target.index == 2);
  15537. }
  15538. TEST (event_editor_playback)
  15539. {
  15540. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  15541. auto song = lynx::Song::makeDefault();
  15542. song.bpm = 120.0;
  15543. const auto bar = (std::int64_t) lynx::ppq * 4;
  15544. song.patterns[0].lengthTicks = bar;
  15545. song.patterns[0].notes.clear();
  15546. for (int s = 0; s < 16; ++s)
  15547. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  15548. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  15549. const auto peaks = [&] (lynx::PlayMode mode) {
  15550. song.mode = mode;
  15551. song.playlist.clear();
  15552. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  15553. const double sr = 48000.0;
  15554. auto state = lynx::RackState::build (song, nullptr, sr);
  15555. lynx::RackRenderer r;
  15556. r.prepare (sr, 512);
  15557. std::vector<float> l (512), rr (512);
  15558. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  15559. std::array<float, 6> peak {}; // half bars
  15560. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  15561. {
  15562. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  15563. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  15564. const auto blockEnd = tick + 512 * tps;
  15565. const auto half = (std::size_t) (tick / (double) (bar / 2));
  15566. const auto edge = (double) half * (double) (bar / 2);
  15567. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  15568. continue;
  15569. for (auto v : l)
  15570. peak[half] = std::max (peak[half], std::abs (v));
  15571. }
  15572. return std::make_pair (state->eventLanes.size(), peak);
  15573. };
  15574. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  15575. CHECK (lanesSong == 1);
  15576. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  15577. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  15578. CHECK (lanesPattern == 1);
  15579. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  15580. }
  15581. TEST (flp_pattern_events_import)
  15582. {
  15583. // REA_rodada85 §1: event 0xDF holds a pattern's events {u32 tick, u32 controlID, i32 raw value}, 0xD8 the values
  15584. // reset when the song starts; they become pattern event lanes in FL's normalised scale
  15585. FlpBuilder b;
  15586. b.u8 (0x40); b.u16 (0); // channel 0
  15587. b.u8 (0x15); b.u8 (0); // sampler
  15588. b.u8 (0x41); b.u16 (1); // pattern 1: events only, no notes
  15589. auto rec = [] (std::vector<std::uint8_t>& v, std::uint32_t tick, std::uint32_t id, std::int32_t value) {
  15590. for (auto w : { tick, id, (std::uint32_t) value })
  15591. for (int i = 0; i < 4; ++i)
  15592. v.push_back ((std::uint8_t) (w >> (8 * i)));
  15593. };
  15594. std::vector<std::uint8_t> events;
  15595. rec (events, 0, 0x70001FC0u, 16000); // Master volume: top
  15596. rec (events, 96, 0x70001FC0u, 8000);
  15597. rec (events, 96, 0x70001FC0u, 4000); // same tick: the last one wins
  15598. rec (events, 0, 0x00000001u, 0); // channel 0 pan: hard left
  15599. rec (events, 48, 0x00000001u, 12800); // hard right
  15600. rec (events, 0, 0x00008000u, 5); // a plugin parameter of a sampler: scale unknown, skipped
  15601. b.data (0xDF, events);
  15602. std::vector<std::uint8_t> init;
  15603. rec (init, 0, 0x70001FC1u, -6400); // Master pan reset to hard left at song start
  15604. b.data (0xD8, init);
  15605. const auto project = flp::parseProject (b.bytes);
  15606. CHECK (project.initControls.size() == 1);
  15607. const flp::Pattern* src = nullptr;
  15608. for (const auto& p : project.patterns)
  15609. if (p.index == 1)
  15610. src = &p;
  15611. CHECK (src != nullptr && src->controlEvents.size() == 6);
  15612. lynx::FlpImportReport report;
  15613. lynx::FlpImportOptions options;
  15614. options.report = &report;
  15615. const auto song = lynx::importFlp (project, options);
  15616. const lynx::PatternEvents* volume = nullptr;
  15617. const lynx::PatternEvents* pan = nullptr;
  15618. int lanes = 0;
  15619. for (const auto& p : song.patterns)
  15620. for (const auto& lane : p.events)
  15621. {
  15622. ++lanes;
  15623. if (lane.target.kind == lynx::AutomationTargetKind::mixerVolume && lane.target.index == 0)
  15624. volume = &lane;
  15625. if (lane.target.kind == lynx::AutomationTargetKind::channelPan)
  15626. pan = &lane;
  15627. }
  15628. CHECK (lanes == 2 && volume != nullptr && pan != nullptr);
  15629. if (volume != nullptr && volume->events.size() == 2)
  15630. {
  15631. CHECK (volume->events[0].tick == 0 && volume->events[1].tick == lynx::ppq);
  15632. CHECK (volume->events[0].value > volume->events[1].value); // 4000 (not 8000) is the one kept
  15633. }
  15634. else
  15635. CHECK (false);
  15636. if (pan != nullptr && pan->events.size() == 2)
  15637. CHECK (pan->events[0].value < 0.01f && pan->events[1].value > 0.99f && pan->events[1].tick == lynx::ppq / 2);
  15638. else
  15639. CHECK (false);
  15640. CHECK (report.converted["Pattern events"] == 2);
  15641. CHECK (song.targetValue ({ lynx::AutomationTargetKind::mixerPan, 0 }) < 0.01f);
  15642. }
  15643. #include "PrefsTests.inc"
  15644. #include "core/ProjectSafety.h"
  15645. #include "core/SearchIndex.h"
  15646. #include "core/PluginCatalog.h"
  15647. #include "SafetyTests118.inc"
  15648. #include "Rea134Tests.inc"
  15649. #include "Rea139Tests.inc"
  15650. #include "Rea143Tests.inc"
  15651. #include "Rea143aTests.inc"
  15652. #include "Rea143bTests.inc"
  15653. #include "Rea146aTests.inc"
  15654. #include "Rea146bTests.inc"
  15655. #include "Rea146Tests.inc"
  15656. #include "Rea149Tests.inc"
  15657. #include "PluginCatalogTests151.inc"
  15658. #include "Rea151Tests.inc"
  15659. #include "AudioEditTests.inc"
  15660. #include "AudioEditToolsTests.inc"
  15661. #include "InputTests.inc"
  15662. #include "../asio/InputDrift.h"
  15663. #include "AsioDriftTests.inc"
  15664. TEST (level_audit_demo_imports_round89)
  15665. {
  15666. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  15667. if (realFlpDir.empty())
  15668. return;
  15669. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  15670. if (! std::filesystem::exists (path))
  15671. return std::nullopt;
  15672. std::ifstream in (path, std::ios::binary);
  15673. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  15674. return lynx::importFlp (flp::parseProject (file));
  15675. };
  15676. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  15677. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  15678. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  15679. {
  15680. int highRoutes = 0, highAtZero = 0;
  15681. for (const auto& t : song->mixer)
  15682. for (const auto& send : t.sends)
  15683. if (send.target >= 100)
  15684. {
  15685. ++highRoutes;
  15686. highAtZero += send.level <= 0.0f;
  15687. }
  15688. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  15689. CHECK (highRoutes > 0 && highAtZero == 0);
  15690. }
  15691. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  15692. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  15693. {
  15694. int compressors = 0, withSettings = 0;
  15695. for (const auto& t : song->mixer)
  15696. for (const auto& fx : t.effects)
  15697. if (fx.kind == lynx::EffectKind::fruityCompressor)
  15698. {
  15699. ++compressors;
  15700. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  15701. withSettings += fx.params != defaults.params;
  15702. }
  15703. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  15704. CHECK (compressors >= 10 && withSettings >= 8);
  15705. }
  15706. }
  15707. #include "UiLook90Tests.inc"
  15708. #include "UiFl104Tests.inc"
  15709. #include "DialogsTests84.inc"
  15710. #include "DockTests188.inc"
  15711. #include "SubstituteTests.inc"
  15712. #include "MixSuiteTests96.inc"
  15713. #include "PanelsTests105.inc"
  15714. // --- native plugin editors (round 93): FL's arrangements ---------------------------------------------------------
  15715. TEST (plugin_layouts_name_real_parameters)
  15716. {
  15717. using namespace lynx;
  15718. // every value control edits a parameter of its effect, inside the body; FL arrangements place (or list in a menu)
  15719. // every parameter of the effect
  15720. for (const auto kind : plugui::flLayoutKinds())
  15721. {
  15722. const auto& l = plugui::layoutFor (kind);
  15723. const auto& info = effectInfo (kind);
  15724. CHECK (plugui::hasFlLayout (kind));
  15725. // FL's own size wins for its one-cell editors (Fruity Center, Fruity Phase Inverter: 170 x 55)
  15726. const auto minHeight = kind == EffectKind::flCenter || kind == EffectKind::flPhaseInverter ? 50 : 60;
  15727. if (! (l.width > 100 && l.height > minHeight))
  15728. std::printf (" %s: %d x %d\n", info.name, l.width, l.height);
  15729. CHECK (l.width > 100 && l.height > minHeight);
  15730. std::vector<bool> placed ((std::size_t) plugui::paramCount (kind), false);
  15731. for (const auto& item : l.items)
  15732. {
  15733. if (! (item.x >= 0.0f && item.y >= 0.0f && item.x + item.w <= (float) l.width + 0.5f && item.y + item.h <= (float) l.height + 0.5f))
  15734. std::printf (" %s: item '%s' leaves the body\n", info.name, item.param != nullptr ? item.param : item.label != nullptr ? item.label : "");
  15735. CHECK (item.x >= 0.0f && item.y >= 0.0f && item.x + item.w <= (float) l.width + 0.5f && item.y + item.h <= (float) l.height + 0.5f);
  15736. if (item.param != nullptr)
  15737. {
  15738. const auto p = plugui::paramIndex (kind, item.param);
  15739. CHECK (p >= 0);
  15740. if (p >= 0)
  15741. placed[(std::size_t) p] = true;
  15742. }
  15743. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  15744. {
  15745. const auto labels = plugui::optionLabels (item);
  15746. CHECK (labels.size() >= 2);
  15747. CHECK (item.values.empty() || item.values.size() == labels.size());
  15748. }
  15749. if (item.type == plugui::Item::knob && item.h + 0.5f < item.w) // (a caption above its knob would hide it)
  15750. {
  15751. std::printf (" %s: knob '%s' is shorter than it is wide\n", info.name, item.param != nullptr ? item.param : "");
  15752. CHECK (false);
  15753. }
  15754. CHECK (item.param2 == nullptr || plugui::paramIndex (kind, item.param2) >= 0);
  15755. if (item.param2 != nullptr && plugui::paramIndex (kind, item.param2) >= 0)
  15756. placed[(std::size_t) plugui::paramIndex (kind, item.param2)] = true;
  15757. for (const auto* when : { item.whenParam, item.when2Param, item.when3Param })
  15758. CHECK (when == nullptr || plugui::paramIndex (kind, when) >= 0);
  15759. }
  15760. for (const auto* name : l.menuSwitches)
  15761. {
  15762. const auto p = plugui::paramIndex (kind, name);
  15763. CHECK (p >= 0);
  15764. if (p >= 0)
  15765. placed[(std::size_t) p] = true;
  15766. }
  15767. for (const auto* name : l.editedElsewhere)
  15768. {
  15769. const auto p = plugui::paramIndex (kind, name);
  15770. CHECK (p >= 0);
  15771. if (p >= 0)
  15772. placed[(std::size_t) p] = true;
  15773. }
  15774. for (std::size_t p = 0; p < placed.size(); ++p)
  15775. if (! placed[p])
  15776. {
  15777. std::printf (" %s: parameter '%s' has no control\n", info.name, plugui::paramName (kind, (int) p));
  15778. CHECK (false);
  15779. }
  15780. }
  15781. }
  15782. TEST (maximus_editor_edits_the_slot_state)
  15783. {
  15784. using namespace lynx;
  15785. // the Maximus page edits the 59 parameters stored in the slot's state (REA_rodada8 §1.2)
  15786. std::array<std::array<int, 13>, 4> bands {};
  15787. for (auto& b : bands)
  15788. b = { 0, 1000, 1000, 0, 0, 1000, 1115, 0, 2, 5000, 3, 2882, 0 };
  15789. std::array<std::vector<std::array<double, 4>>, 8> envs;
  15790. for (auto& e : envs)
  15791. e = { { 0.0, 0.0, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 } };
  15792. const auto state = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, 32768 }, bands, envs, 0);
  15793. const auto slot = maximusfx::pack ({ hlfx::MaximusBuild::shared, -1, state });
  15794. CHECK (maximusfx::stateParams().size() == 59);
  15795. CHECK (plugui::paramIndex (EffectKind::maximus, "Master release") == 7 + 13 * 3 + 9);
  15796. CHECK (maximusfx::stateParam (slot, 0).value_or (-1) == 19800);
  15797. CHECK (maximusfx::stateParam (slot, 7 + 13 + 9).value_or (-1) == 5000);
  15798. CHECK (maximusfx::stateParamText (0, 19800) == "200 Hz");
  15799. CHECK (maximusfx::stateParamText (2, 48065) == "3000 Hz");
  15800. CHECK (maximusfx::stateParamText (8, 1000) == "+0.0 dB");
  15801. CHECK (maximusfx::stateParamText (7 + 6, 0) == "Default");
  15802. CHECK (maximusfx::stateParamText (7 + 13 * 3 + 6, 1115) == "2.00 ms");
  15803. CHECK (maximusfx::stateParamText (6, -1) == "Off");
  15804. const auto edited = maximusfx::withStateParam (slot, 7 + 13 + 1, 2000); // mid pre gain → +18 dB
  15805. CHECK (maximusfx::stateParam (edited, 7 + 13 + 1).value_or (-1) == 2000);
  15806. CHECK (maximusfx::stateParam (edited, 7 + 1).value_or (-1) == 1000);
  15807. CHECK (maximusfx::stateParam (maximusfx::withStateParam (slot, 0, 99999), 0).value_or (-1) == 65536); // clamped
  15808. const auto blob = maximusfx::unpack (edited);
  15809. const auto original = maximusfx::unpack (slot);
  15810. CHECK (blob && original && (hlfx::makeMaximusPreset (blob->state) != nullptr) == (hlfx::makeMaximusPreset (original->state) != nullptr));
  15811. const auto curve = maximusfx::curvePoints (slot, 3);
  15812. CHECK (curve.size() == 3 && std::abs (curve[1].first - 0.5f) < 1e-6f && std::abs (curve[2].first - 1.0f) < 1e-6f);
  15813. CHECK (maximusfx::selectedBand (slot) >= 0);
  15814. CHECK (plugui::hasFlLayout (EffectKind::maximus));
  15815. }
  15816. TEST (plugin_layout_options_match_the_engine_values)
  15817. {
  15818. using namespace lynx;
  15819. // the switches show FL's captions for the engine's own values
  15820. const auto text = [] (EffectKind kind, const char* param, int option) {
  15821. for (const auto& item : plugui::layoutFor (kind).items)
  15822. if (item.param != nullptr && std::strcmp (item.param, param) == 0 && item.options != nullptr)
  15823. return formatEffectParam (kind, plugui::paramIndex (kind, param), plugui::optionValue (item, option), 120.0);
  15824. return std::string ("?");
  15825. };
  15826. CHECK (text (EffectKind::fruityDelay3, "Delay type", 0) == "Fake stereo");
  15827. CHECK (text (EffectKind::fruityDelay3, "Delay type", 2) == "Ping pong");
  15828. CHECK (text (EffectKind::fruityDelay3, "Delay type", 3) == "Off");
  15829. CHECK (text (EffectKind::fruityDelay3, "Filter mode", 1) == "High pass");
  15830. CHECK (text (EffectKind::fruityDelay3, "Saturation type", 1) == "Saturate");
  15831. CHECK (text (EffectKind::fruityMultiband, "Low mode", 1) == "off");
  15832. CHECK (text (EffectKind::fruityMultiband, "High mode", 2) == "bypass");
  15833. CHECK (text (EffectKind::luxeVerb, "Pitch type", 1) == "feed-forward");
  15834. CHECK (text (EffectKind::luxeVerb, "Freeze mode", 2) == "sustain");
  15835. CHECK (text (EffectKind::stereoEnhancer, "Invert", 2) == "invert R");
  15836. CHECK (text (EffectKind::parametricEq, "B3 type", 4) == formatEffectParam (EffectKind::parametricEq, peq::param (2, 0), peq::typeValue (peq::BandType::lowCut), 120.0));
  15837. // the option nearest to a value; pages and conditions
  15838. plugui::LayoutItem radio;
  15839. radio.type = plugui::Item::radio;
  15840. radio.options = "A|B|C|D";
  15841. CHECK (plugui::optionLabels (radio).size() == 4);
  15842. CHECK (plugui::optionAt (radio, 0.30f) == 1);
  15843. CHECK (plugui::optionAt (radio, 0.95f) == 3);
  15844. CHECK (std::abs (plugui::optionValue (radio, 2) - 2.0f / 3.0f) < 1e-6f);
  15845. const auto& lux = plugui::layoutFor (EffectKind::luxeVerb);
  15846. auto params = makeEffectSlot (EffectKind::luxeVerb).params;
  15847. const auto pitchType = plugui::paramIndex (EffectKind::luxeVerb, "Pitch type");
  15848. int feedForward = 0, feedback = 0;
  15849. for (float v : { 0.0f, 1.0f })
  15850. {
  15851. params[(std::size_t) pitchType] = v;
  15852. for (const auto& item : lux.items)
  15853. if (item.param != nullptr && std::strcmp (item.param, "Feed-forward pitch 1") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15854. ++feedForward;
  15855. else if (item.param != nullptr && std::strcmp (item.param, "Feedback pitch") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15856. ++feedback;
  15857. }
  15858. CHECK (feedForward == 1); // only in feed-forward mode
  15859. CHECK (feedback == 2); // value plate + fader, only in feedback mode
  15860. const auto& limiter = plugui::layoutFor (EffectKind::fruityLimiter);
  15861. CHECK (limiter.pages.size() == 2);
  15862. int ceilingShown = 0;
  15863. for (int page = 0; page < 2; ++page)
  15864. for (const auto& item : limiter.items)
  15865. if (item.param != nullptr && std::strcmp (item.param, "Ceiling") == 0 && plugui::isShown (EffectKind::fruityLimiter, item, page, params))
  15866. ++ceilingShown;
  15867. CHECK (ceilingShown == 1); // LIMIT page only
  15868. // every other effect still gets a (generated) window
  15869. CHECK (! plugui::hasFlLayout (EffectKind::distortion));
  15870. CHECK (! plugui::layoutFor (EffectKind::distortion).items.empty());
  15871. }
  15872. #include "FabMeasureTests99.inc"
  15873. #include "DynamicsTests108.inc"
  15874. #include "Crest173dTests.inc"
  15875. #include "LynxTuneTests95.inc"
  15876. #include "LynxVerbTests100.inc"
  15877. #include "FacetTests103.inc"
  15878. #include "VocalLevel107Tests.inc"
  15879. #include "Substitutes113Tests.inc"
  15880. #include "Tune120Tests.inc"
  15881. #include "EchoSuiteTests101.inc"
  15882. #include "FruitySendTests105.inc"
  15883. #include "CrashFuzzTests111.inc"
  15884. #include "LookTests114.inc"
  15885. #include "FixedBlockTests118.inc"
  15886. #include "UiFl115Tests.inc"
  15887. #include "LoadTraceTests112.inc"
  15888. #include "VoiceToolsTests119.inc"
  15889. #include "VoiceMatchTests123.inc"
  15890. #include "LoadTests121.inc"
  15891. #include "Parity128Tests.inc"
  15892. #include "Parity128aTests.inc"
  15893. #include "Parity128bTests.inc"
  15894. #include "Parity128cTests.inc"
  15895. #include "Match124Tests.inc"
  15896. #include "Match124xTests.inc"
  15897. #include "Vmatch127Tests.inc"
  15898. #include "ParityTests129.inc"
  15899. #include "Parity135vTests.inc"
  15900. #include "Bugs131Tests.inc"
  15901. #include "Parity135wTests.inc"
  15902. #include "Feat133Tests.inc"
  15903. #include "Rea165fTests.inc"
  15904. #include "Parity135fTests.inc"
  15905. #include "Feat132Tests.inc"
  15906. #include "Parity135sTests.inc"
  15907. #include "VoiceMatch136Tests.inc"
  15908. #include "Parity137Tests.inc"
  15909. #include "Parity144Tests.inc"
  15910. #include "Parity144aTests.inc"
  15911. #include "Parity144bTests.inc"
  15912. #include "Parity147Tests.inc"
  15913. #include "Parity147aTests.inc"
  15914. #include "Parity147bTests.inc"
  15915. #include "Parity150Tests.inc"
  15916. #include "Parity150aTests.inc"
  15917. #include "Parity150bTests.inc"
  15918. #include "Parity152Tests.inc"
  15919. #include "Tp172dTests.inc"
  15920. #include "Verb183bEchoformTests.inc"
  15921. #include "Verb183bEpochTests.inc"
  15922. #include "Verb183bAtriumTests.inc"
  15923. #include "Verb183bPlateTests.inc"
  15924. #include "Verb183bHaloTests.inc"
  15925. #include "Verb183bNebulaTests.inc"
  15926. #include "Verb183bTests.inc"
  15927. #include "Parity152aTests.inc"
  15928. #include "Parity152bTests.inc"
  15929. #include "Parity152cTests.inc"
  15930. #include "Parity165Tests.inc"
  15931. #include "Parity165wTests.inc"
  15932. #include "Parity165xTests.inc"
  15933. #include "Parity165aTests.inc"
  15934. #include "Patcher141Tests.inc"
  15935. #include "Patcher162Tests.inc"
  15936. #include "Vox138Tests.inc"
  15937. #include "Vox145Tests.inc"
  15938. #include "Vox148Tests.inc"
  15939. #include "Natives142Tests.inc"
  15940. TEST (emphasizer_matches_fl_engine)
  15941. {
  15942. // REA_rodada154: FL's Emphasizer through the import (state → slot → effect) against goldens of the installation's
  15943. // Emphasizer_x64.dll (rate set before the state, 2304 samples of silence as FL's mixer runs it before a render,
  15944. // 512-frame calls, 44.1 kHz): FNV-1a of the output's float bits
  15945. auto signal = [] {
  15946. std::vector<float> l (24000), r (24000);
  15947. std::uint32_t s = 12345;
  15948. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  15949. for (int i = 0; i < 24000; ++i)
  15950. {
  15951. const float a = amps[(i / 3000) % 8];
  15952. s = s * 1664525u + 1013904223u;
  15953. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15954. s = s * 1664525u + 1013904223u;
  15955. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15956. }
  15957. return std::pair { l, r };
  15958. };
  15959. struct Case { std::array<int, 6> raw; std::uint64_t hash; };
  15960. const Case cases[] { { { 0, 5013, 0, 0, 0, 10000 }, 0xa009608f060eb32eull }, { { 6000, 9000, 1, 0, 0, 8000 }, 0x6fc7ec82cc9d9c01ull },
  15961. { { 8000, 2000, 0, 1, 0, 10000 }, 0xb10a1f3bd998198cull }, { { 3000, 5013, 1, 0, 1, 10000 }, 0x57a8fa4d8b890ac0ull } };
  15962. for (const auto& c : cases)
  15963. {
  15964. std::vector<std::uint8_t> state (4 + 24 + 1, 0);
  15965. const std::int32_t version = 2;
  15966. std::memcpy (state.data(), &version, 4);
  15967. std::memcpy (state.data() + 4, c.raw.data(), 24);
  15968. state[28] = 1;
  15969. const auto slot = lynx::effectFromFl ("Emphasizer", state);
  15970. CHECK (slot && slot->kind == lynx::EffectKind::emphasizer);
  15971. if (! slot)
  15972. continue;
  15973. auto fx = lynx::makeEffect (slot->kind);
  15974. fx->prepare (44100.0);
  15975. auto [l, r] = signal();
  15976. for (int i = 0; i < 24000; i += 512) // the golden's 512-frame engine calls (level matching is per call)
  15977. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  15978. std::uint64_t h = 0xcbf29ce484222325ull;
  15979. for (int i = 0; i < 24000; ++i)
  15980. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  15981. {
  15982. std::uint32_t u;
  15983. std::memcpy (&u, &v, 4);
  15984. h = (h ^ u) * 0x100000001b3ull;
  15985. }
  15986. CHECK (h == c.hash);
  15987. }
  15988. // version 1 (no output control) keeps the 0 dB default; texts
  15989. const std::vector<std::uint8_t> v1 { 1, 0, 0, 0, 0, 0, 0, 0, 0x95, 0x13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  15990. const auto s1 = lynx::emphasizer::parse (v1);
  15991. CHECK (s1 && s1->raw[1] == 5013 && s1->raw[5] == 10000);
  15992. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 0, 0.5f, 120.0) == "+12.0 dB");
  15993. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 5, 1.0f, 120.0) == "0.0 dB");
  15994. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 2, 1.0f, 120.0) == "Soft");
  15995. }
  15996. // ---- Wave Traveller (REA_rodada167f-wavetraveller) ----
  15997. namespace
  15998. {
  15999. // the crafted case: a sample on the 16-bit grid and a state that uses every feature (the same data went to the DLL)
  16000. inline std::vector<float> wtTestSample (int n) // planar stereo, values on the 16-bit grid
  16001. {
  16002. std::vector<float> s ((std::size_t) n * 2);
  16003. std::uint32_t r = 2024;
  16004. for (int i = 0; i < n; ++i)
  16005. for (int c = 0; c < 2; ++c)
  16006. {
  16007. r = r * 1664525u + 1013904223u;
  16008. const auto noise = (int) (r >> 20) - 2048; // ±2048
  16009. const auto tone = (int) std::lround (12000.0 * std::sin ((double) i * (c == 0 ? 0.031 : 0.047)));
  16010. s[(std::size_t) c * (std::size_t) n + (std::size_t) i] = (float) (tone + noise) / 32768.0f;
  16011. }
  16012. return s;
  16013. }
  16014. inline lynx::wavetrav::State wtTestState()
  16015. {
  16016. using namespace lynx::wavetrav;
  16017. auto st = defaultState();
  16018. st.samplePath = "x.wav"; // (FL asks its host for the sample only when the state names one)
  16019. auto pt = [] (float x, float y, int type, float tension = 0.5f, float c1x = -0.05f, float c1y = -0.2f, float c2x = 0.1f, float c2y = 0.3f) {
  16020. auto p = newPoint (x, y);
  16021. p.type = type, p.tension = tension, p.c1x = c1x, p.c1y = c1y, p.c2x = c2x, p.c2y = c2y;
  16022. return p;
  16023. };
  16024. auto& a = st.patches[60]; // every segment type, tempo based
  16025. a.wave.clear();
  16026. const float xs[] { 0.0f, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 0.9f, 1.0f }, ys[] { 0.1f, 0.8f, 0.2f, 0.9f, 0.05f, 0.7f, 0.3f, 1.0f };
  16027. const int ts[] { 1, 2, 3, 4, 5, 6, 8, 1 };
  16028. for (int i = 0; i < 8; ++i)
  16029. a.wave.push_back (pt (xs[i], ys[i], ts[i], ts[i] == 6 ? 0.2f : 0.5f));
  16030. a.speedBeats = 2.0f;
  16031. auto& b = st.patches[61]; // volume curve, mutes, start / end, attack / release
  16032. b.vol = { pt (0.0f, 0.2f, 2), pt (0.4f, 1.0f, 6, 0.8f), pt (0.7f, 0.3f, 4), pt (1.0f, 0.9f, 1) };
  16033. b.mutes = { { true, 0.0f }, { false, 0.3f }, { true, 0.5f } };
  16034. b.start = 0.2f, b.end = 0.7f, b.attackMs = 50.0f, b.releaseMs = 300.0f, b.speedBeats = 1.0f;
  16035. auto& c = st.patches[62]; // absolute speed, tension segments
  16036. c.splineType = 1, c.absSpeed = 2.3f, c.start = 0.1f, c.end = 0.6f;
  16037. c.wave = { pt (0.0f, 0.0f, 6, 0.5f), pt (0.5f, 0.9f, 6, 0.9f), pt (1.0f, 0.2f, 1) };
  16038. auto& d = st.patches[63]; // types that write nothing, a tiny Bezier
  16039. d.wave = { pt (0.0f, 0.0f, 1), pt (0.2f, 0.5f, 7), pt (0.3f, 0.1f, 0), pt (0.5f, 0.6f, 3, 0.5f, -0.05f, -0.2f, 0.0f, 0.0f),
  16040. pt (0.5004f, 0.2f, 3), pt (0.501f, 0.9f, 1), pt (1.0f, 0.4f, 1) };
  16041. for (auto& p : st.patches)
  16042. p.rebuild();
  16043. return st;
  16044. }
  16045. std::uint64_t floatBitsHash (const std::vector<float>& v)
  16046. {
  16047. std::uint64_t h = 0xcbf29ce484222325ull;
  16048. for (const float x : v)
  16049. {
  16050. std::uint32_t u;
  16051. std::memcpy (&u, &x, 4);
  16052. h = (h ^ u) * 0x100000001b3ull;
  16053. }
  16054. return h;
  16055. }
  16056. lynx::SampleData wtSampleData (const std::vector<float>& planar, int channels)
  16057. {
  16058. lynx::SampleData d;
  16059. const auto n = planar.size() / (std::size_t) channels;
  16060. d.left.assign (planar.begin(), planar.begin() + (std::ptrdiff_t) n);
  16061. if (channels > 1)
  16062. d.right.assign (planar.begin() + (std::ptrdiff_t) n, planar.end());
  16063. d.sampleRate = 44100.0;
  16064. return d;
  16065. }
  16066. } // namespace
  16067. TEST (wave_traveller_state_and_tables)
  16068. {
  16069. using namespace lynx::wavetrav;
  16070. // FL's state layout (§2): it round-trips byte for byte, and the default patch is FL's
  16071. const auto def = writeState (defaultState());
  16072. State s;
  16073. CHECK (parseState (def, s) && writeState (s) == def && s.selectedKey == 60 && s.samplePath.empty());
  16074. CHECK (s.patches[0].wave.size() == 2 && s.patches[119].vol.size() == 2 && s.patches[60].mutes.size() == 1 && s.patches[60].mutes[0].on);
  16075. const auto crafted = writeState (wtTestState());
  16076. CHECK (parseState (crafted, s) && writeState (s) == crafted);
  16077. CHECK (s.patches[62].splineType == 1 && s.patches[61].mutes.size() == 3 && ! s.patches[61].mutes[1].on && s.patches[60].wave.size() == 8);
  16078. // tables (§3): a 0 → 1 line is 1022 values long (the writer steps back one value per segment)
  16079. CHECK (s.patches[0].waveN == 1022 && s.patches[0].waveTable[0] == 0.0f && s.patches[0].volTable[500] == 1.0f);
  16080. std::vector<float> tables;
  16081. for (int k = 60; k < 64; ++k)
  16082. {
  16083. const auto& p = s.patches[(std::size_t) k];
  16084. tables.insert (tables.end(), p.waveTable.begin(), p.waveTable.begin() + p.waveN + 1);
  16085. tables.insert (tables.end(), p.volTable.begin(), p.volTable.begin() + p.volN + 1);
  16086. CHECK (std::all_of (p.waveTable.begin(), p.waveTable.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  16087. }
  16088. // (LYNX's own value; the tables reach the DLL goldens of wave_traveller_matches_fl_engine through the output)
  16089. CHECK (floatBitsHash (tables) == 0xd94594a6f22cb4dfull);
  16090. if (floatBitsHash (tables) != 0xd94594a6f22cb4dfull)
  16091. std::printf (" wave traveller tables: %016llx\n", (unsigned long long) floatBitsHash (tables));
  16092. // knobs (FUN_007579e0 / 757b10 / 757b60)
  16093. CHECK (speedFromKnob (0.0f, false) == 0.125f && speedFromKnob (1.0f, false) == 8.0f && speedFromKnob (0.0f, true) == 0.25f
  16094. && speedFromKnob (1.0f, true) == 4.0f && attackFromKnob (1.0f) == 500.0f && releaseFromKnob (1.0f) == 1000.0f);
  16095. // a .spln preset (§6): knob positions, curves, mutes
  16096. std::vector<std::uint8_t> spln;
  16097. auto i32 = [&spln] (std::int32_t v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  16098. auto f32 = [&spln] (float v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  16099. i32 (4000), i32 (1000005), i32 (1);
  16100. for (float v : { 0.5f, 0.2f, 0.1f, 0.3f, 0.25f, 0.75f })
  16101. f32 (v);
  16102. const auto rec = [&] (float x, float y) {
  16103. std::vector<std::uint8_t> r (60, 0);
  16104. std::memcpy (r.data() + 8, &x, 4), std::memcpy (r.data() + 12, &y, 4);
  16105. const std::int32_t one = 1;
  16106. std::memcpy (r.data() + 0x30, &one, 4);
  16107. spln.insert (spln.end(), r.begin(), r.end());
  16108. };
  16109. i32 (3), rec (0.0f, 1.0f), rec (0.5f, 0.0f), rec (1.0f, 1.0f);
  16110. i32 (2), rec (0.0f, 1.0f), rec (1.0f, 0.5f);
  16111. i32 (2), i32 (0), f32 (0.0f), i32 (1), f32 (0.5f);
  16112. auto p = defaultPatch();
  16113. CHECK (applyPreset (spln, p) && p.splineType == 1 && p.speedBeats == speedFromKnob (0.5f, false) && p.absSpeed == speedFromKnob (0.2f, true)
  16114. && p.attackMs == attackFromKnob (0.1f) && p.releaseMs == releaseFromKnob (0.3f) && p.start == 0.25f && p.end == 0.75f
  16115. && p.wave.size() == 3 && p.vol.size() == 2 && p.mutes.size() == 2 && ! p.mutes[0].on && p.waveN > 1000);
  16116. // garbage is refused without crashing
  16117. std::uint32_t r = 7;
  16118. for (int round = 0; round < 300; ++round)
  16119. {
  16120. std::vector<std::uint8_t> blob (crafted.begin(), crafted.begin() + (std::ptrdiff_t) (r % crafted.size()));
  16121. for (int k = 0; k < 8 && ! blob.empty(); ++k)
  16122. {
  16123. r = r * 1664525u + 1013904223u;
  16124. blob[(r >> 8) % blob.size()] = (std::uint8_t) (r >> 24);
  16125. }
  16126. r = r * 1664525u + 1013904223u;
  16127. if (parseState (blob, s))
  16128. for (const auto& q : s.patches)
  16129. CHECK (q.waveN >= 0 && q.waveN < tableCapacity && q.volN >= 0 && q.volN < tableCapacity);
  16130. }
  16131. CHECK (! parseState ({ 1, 2, 3 }, s) && ! lynx::waveTravellerStateUsable ({}));
  16132. // the installation's states (read at test time): the render test and the plugin database preset
  16133. if (realFlpDir.empty())
  16134. return;
  16135. auto read = [] (const std::filesystem::path& path) {
  16136. std::ifstream in (path, std::ios::binary);
  16137. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  16138. };
  16139. const auto base = std::filesystem::path (realFlpDir);
  16140. if (const auto b = read (base / "System" / "Render tests" / "Wave Traveller.flp"); ! b.empty())
  16141. {
  16142. const auto project = flp::parseProject (b);
  16143. int found = 0;
  16144. for (const auto& c : project.channels)
  16145. if (c.pluginName == "Wave Traveller")
  16146. {
  16147. ++found;
  16148. CHECK (parseState (c.pluginState, s) && s.samplePath.find ("808 Snare.wav") != std::string::npos);
  16149. }
  16150. CHECK (found == 1);
  16151. const auto song = lynx::importFlp (project);
  16152. const auto it = std::find_if (song.channels.begin(), song.channels.end(), [] (const lynx::ChannelDef& c) { return c.instrument == lynx::InstrumentKind::wavetraveller; });
  16153. CHECK (it != song.channels.end() && lynx::waveTravellerStateUsable (it->pluginState));
  16154. }
  16155. if (const auto b = read (base / "System" / "Plugin databases" / "Plugin database (alphabetical)" / "Generators" / "Wave Traveller.fst"); ! b.empty())
  16156. {
  16157. const auto project = flp::parseProject (b, true);
  16158. CHECK (! project.channels.empty() && parseState (project.channels.front().pluginState, s) && s.samplePath.empty());
  16159. }
  16160. }
  16161. TEST (wave_traveller_matches_fl_engine)
  16162. {
  16163. // §8: goldens of the installation's Wave Traveller_x64.dll in our harness (16-bit stereo sample handed by LoadSample,
  16164. // 512-frame calls, tempo by ProcessEvent): FNV-1a of the output's float bits. LYNX's engine reproduces them exactly.
  16165. using namespace lynx::wavetrav;
  16166. const auto planar = wtTestSample (30000);
  16167. const auto smp = sampleFrom (wtSampleData (planar, 2));
  16168. CHECK (smp != nullptr && smp->length == 30000);
  16169. if (smp == nullptr)
  16170. return;
  16171. const auto state = wtTestState();
  16172. auto note = [] (int start, int key, float vol, float pan, float cents, int release, float initPan = 0.0f) {
  16173. RefNote n;
  16174. n.start = start, n.release = release;
  16175. n.note.key = key, n.note.finalVol = vol, n.note.finalPan = pan, n.note.initPan = initPan;
  16176. n.note.finalPitch = 100.0f * (float) key - 6000.0f + cents;
  16177. return n;
  16178. };
  16179. struct Case { std::vector<RefNote> notes; int rate; float tempo; int taps, length; std::uint64_t hash; };
  16180. const Case cases[] {
  16181. // live: every segment type, volume curve + mutes + attack / release, absolute speed, types 0 / 7 and a tiny Bezier
  16182. { { note (0, 60, 0.9f, 0.25f, 30.0f, 20000), note (100, 61, 0.7f, -0.5f, -120.0f, 15000), note (2000, 62, 0.8f, 0.0f, 0.0f, -1),
  16183. note (5000, 63, 1.0f, 0.6f, 250.0f, -1) }, 44100, 140.0f, 0, 40000, 0x9001a50747634ba6ull },
  16184. // render mode, 256 taps per side (FL's render default), with the notes' own pan
  16185. { { note (0, 60, 0.9f, 0.25f, 30.0f, 20000, -0.3f), note (100, 61, 0.7f, -0.5f, -120.0f, 15000, -0.3f), note (2000, 62, 0.8f, 0.0f, 0.0f, -1, -0.3f),
  16186. note (5000, 63, 1.0f, 0.6f, 250.0f, -1, -0.3f) }, 44100, 140.0f, 256, 8000, 0xedabd9728ffbb252ull },
  16187. // a 48 kHz host at tempo 97.5
  16188. { { note (0, 62, 0.9f, -0.6f, -250.0f, 20000), note (300, 60, 0.8f, 0.3f, 100.0f, -1) }, 48000, 97.5f, 0, 30000, 0x71b988c298c5ab31ull },
  16189. };
  16190. for (const auto& c : cases)
  16191. {
  16192. const auto out = renderReference (state, *smp, c.notes, c.rate, c.tempo, c.taps, c.length);
  16193. CHECK (floatBitsHash (out) == c.hash);
  16194. if (floatBitsHash (out) != c.hash)
  16195. std::printf (" wave traveller case: %016llx\n", (unsigned long long) floatBitsHash (out));
  16196. }
  16197. // the instrument (channel → engine) plays what the reference plays: velocity 1/1.28 = FL's Init.Vol 1, centred
  16198. {
  16199. lynx::ChannelDef ch;
  16200. ch.instrument = lynx::InstrumentKind::wavetraveller;
  16201. ch.pluginState = writeState (state);
  16202. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller, std::make_shared<lynx::SampleData> (wtSampleData (planar, 2)));
  16203. inst->configure (ch, 140.0);
  16204. inst->prepare (44100.0);
  16205. std::vector<float> l (20480, 0.0f), r (20480, 0.0f);
  16206. for (int pos = 0; pos < 20480; pos += 512)
  16207. {
  16208. if (pos == 0)
  16209. inst->noteOn (61, 1.0f / 1.28f);
  16210. if (pos == 10240)
  16211. inst->noteOff (61);
  16212. inst->render (l.data() + pos, r.data() + pos, 512, 1.0f, 1.0f);
  16213. }
  16214. const auto ref = renderReference (state, *smp, { note (0, 61, 1.0f, 0.0f, 0.0f, 10240) }, 44100, 140.0f, 0, 20480);
  16215. bool same = true;
  16216. for (int i = 0; i < 20480; ++i)
  16217. same = same && l[(std::size_t) i] == ref[2 * (std::size_t) i] && r[(std::size_t) i] == ref[2 * (std::size_t) i + 1];
  16218. CHECK (same && inst->idle());
  16219. auto silent = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller); // no sample: FL refuses the notes
  16220. silent->configure (ch, 140.0);
  16221. silent->prepare (44100.0);
  16222. silent->noteOn (60, 1.0f);
  16223. std::fill (l.begin(), l.end(), 0.0f);
  16224. silent->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  16225. CHECK (silent->idle() && std::all_of (l.begin(), l.begin() + 512, [] (float v) { return v == 0.0f; }));
  16226. }
  16227. // the installation's render test (its 808 Snare decoded as FL decodes it), read at test time
  16228. if (realFlpDir.empty())
  16229. return;
  16230. const auto base = std::filesystem::path (realFlpDir);
  16231. std::ifstream fin (base / "System" / "Render tests" / "Wave Traveller.flp", std::ios::binary);
  16232. std::ifstream sin (base / "Patches" / "Packs" / "Drums" / "Snares" / "808 Snare.wav", std::ios::binary);
  16233. const std::vector<std::uint8_t> flpBytes { std::istreambuf_iterator<char> (fin), {} }, wav { std::istreambuf_iterator<char> (sin), {} };
  16234. lynx::oggvorbis::Decoded dec;
  16235. if (flpBytes.empty() || wav.empty() || ! lynx::oggvorbis::decode (wav.data(), wav.size(), dec) || dec.channels != 1)
  16236. return;
  16237. const auto project = flp::parseProject (flpBytes);
  16238. for (const auto& c : project.channels)
  16239. if (c.pluginName == "Wave Traveller")
  16240. {
  16241. State st;
  16242. CHECK (parseState (c.pluginState, st));
  16243. lynx::SampleData d;
  16244. d.left = dec.data[0];
  16245. const auto snare = sampleFrom (d);
  16246. CHECK (snare != nullptr && snare->length == 11002);
  16247. if (snare == nullptr)
  16248. return;
  16249. const auto out = renderReference (st, *snare, { note (0, 60, 1.0f, 0.0f, 0.0f, 44100) }, 44100, 140.0f, 0, 88200);
  16250. CHECK (floatBitsHash (out) == 0xafcb49e8c0ef918dull);
  16251. if (floatBitsHash (out) != 0xafcb49e8c0ef918dull)
  16252. std::printf (" wave traveller render test: %016llx\n", (unsigned long long) floatBitsHash (out));
  16253. }
  16254. }
  16255. TEST (fl_legacy_fx_match_fl_engines)
  16256. {
  16257. // REA_rodada165k: FL's Fruity Phase Inverter, Center, Bass Boost, 7 Band EQ and Delay (v1) through the import (state → slot →
  16258. // effect) against goldens of the installation's DLLs (rate set before the state, 512-frame calls): FNV-1a of the
  16259. // output's float bits (x87 hosts) and ten output samples (every platform, within the x87 tolerance)
  16260. struct Case { const char* name; int rate; std::vector<int> raw; std::uint64_t hash; std::array<float, 10> pts; };
  16261. const Case cases[] {
  16262. { "Fruity Phase Inverter", 44100, { 1024 }, 0xd397f60e2df38284ull, { 0.0363710076f, -0.0391986258f, -0.013737631f, 1.81496143e-05f, -2.10248637f, 1.46080184f, 0.0f, -0.0f, -1.1587677f, 0.471242905f } },
  16263. { "Fruity Phase Inverter", 44100, { 400 }, 0xda4a63932df38284ull, { -0.0363710076f, 0.0391986258f, 0.013737631f, -1.81496143e-05f, 2.10248637f, -1.46080184f, -0.0f, 0.0f, 1.1587677f, -0.471242905f } },
  16264. { "Fruity Phase Inverter", 44100, { 100 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16265. { "Fruity Phase Inverter", 48000, { 100 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16266. { "Fruity Center", 44100, { 1 }, 0x3bf5f676fa605f15ull, { 0.0366865396f, 0.0394224077f, -0.0162539873f, 0.00418465072f, -2.09711552f, -1.46810758f, 0.00764796557f, -0.00145012443f, -1.15346813f, -0.472040445f } },
  16267. { "Fruity Center", 48000, { 1 }, 0x4a8ec21dbef0f963ull, { 0.0366563909f, 0.0394078083f, -0.0160636567f, 0.00388090475f, -2.09756589f, -1.46745622f, 0.00721568894f, -0.00133169827f, -1.15359187f, -0.472001195f } },
  16268. { "Fruity Center", 44100, { 0 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16269. { "Fruity Center", 48000, { 0 }, 0xe5593cda2df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16270. { "Fruity Bass Boost", 44100, { 512, 768 }, 0x18162c85cf8081c1ull, { 0.0119505953f, 0.0342054106f, -0.13173604f, -0.137279794f, -2.28246212f, -1.31898761f, 0.01532559f, 0.048792243f, -0.967867851f, -0.387037605f } },
  16271. { "Fruity Bass Boost", 48000, { 512, 768 }, 0x882a7fa498a70215ull, { 0.00987694506f, 0.036732085f, -0.12154711f, -0.134889036f, -2.26724982f, -1.32030582f, 0.0145790791f, 0.0490387827f, -0.998028994f, -0.401082516f } },
  16272. { "Fruity Bass Boost", 44100, { 0, 1024 }, 0x928bfcf5bf6de195ull, { -0.00218012393f, 0.0478172116f, 0.0580603182f, -0.12624f, -2.2277863f, -1.17777693f, -0.0367000513f, 0.0577705353f, -1.24190557f, -0.492130041f } },
  16273. { "Fruity Bass Boost", 48000, { 0, 1024 }, 0x6c5ab3f609af2c3cull, { 0.0024052544f, 0.0460729003f, 0.0709654391f, -0.1167804f, -2.22533989f, -1.18373549f, -0.0448203459f, 0.053043291f, -1.24824715f, -0.494166315f } },
  16274. { "Fruity Bass Boost", 44100, { 1024, 300 }, 0xb246920f1ff60b3ull, { 0.0356629528f, 0.0353008583f, -0.0425309055f, -0.0178884063f, -2.15630841f, -1.42872798f, 0.00398741197f, 0.00680476194f, -1.08475125f, -0.437377959f } },
  16275. { "Fruity Bass Boost", 48000, { 1024, 300 }, 0x89122d0b3fa096baull, { 0.034816049f, 0.0356596299f, -0.0426218137f, -0.0213548262f, -2.15343547f, -1.42917442f, 0.00368365366f, 0.00760280434f, -1.08964729f, -0.439649194f } },
  16276. { "Fruity 7 band EQ", 44100, { 0, 0, 0, 0, 0, 0, 0 }, 0x3acef8692df38284ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0f, 0.0f, -1.1587677f, -0.471242905f } },
  16277. { "Fruity 7 band EQ", 44100, { 1800, -1800, 900, 0, -450, 1200, -1700 }, 0x968d9fdd0e5c53fdull, { 0.0559784994f, -0.0457369909f, -0.20320268f, -0.583957613f, -0.555740654f, -0.159529299f, 0.26959905f, -0.0863189623f, 1.0195893f, -0.781531453f } },
  16278. { "Fruity 7 band EQ", 44100, { -1200, 0, 0, 600, 0, 0, 1800 }, 0x96f6b413d4fc2b65ull, { -0.396190405f, 0.381581873f, 0.639778018f, 2.95740342f, -15.632513f, 0.385985613f, -1.42023098f, 0.503577054f, -9.11049557f, 1.05593979f } },
  16279. { "Fruity Delay", 44100, { 600, 700, 900, 1024, 4, 0 }, 0x3b93cd8ce56d8c32ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.11485457f, -1.40214801f, 0.333820492f, -0.0261738673f, -0.786855578f, -1.16315055f } },
  16280. { "Fruity Delay", 48000, { 600, 700, 900, 1024, 4, 0 }, 0xc8e499107ccc6eeeull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.08610582f, -1.45314789f, 0.441712886f, 0.50612998f, -1.54570627f, 0.225791931f } },
  16281. { "Fruity Delay", 44100, { 700, 1024, 64, 800, 2, 600 }, 0x4b5cb4d05f1013cdull, { 0.0363710076f, 0.0391986258f, -0.014354689f, 0.0176514238f, -2.10405254f, -1.38042021f, -0.000323641405f, -0.0877400041f, -1.13004076f, -0.513286889f } },
  16282. { "Fruity Delay", 48000, { 700, 1024, 64, 800, 2, 600 }, 0x834a89d0aaa9a96eull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.1319468f, -1.44643033f, 0.00283452589f, -0.00713274023f, -1.14618802f, -0.506712079f } },
  16283. { "Fruity Delay", 44100, { 500, 900, 500, 512, 3, 1000 }, 0x2080bc496fcffb2cull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, -0.0331584699f, -0.26390475f, -1.16307354f, -0.476940066f } },
  16284. { "Fruity Delay", 48000, { 500, 900, 500, 512, 3, 1000 }, 0x4c19478535905bb1ull, { 0.0363710076f, 0.0391986258f, -0.013737631f, -1.81496143e-05f, -2.10248637f, -1.46080184f, 0.0120966472f, 0.0150470948f, -1.15484953f, -0.475222349f } },
  16285. };
  16286. for (const auto& c : cases)
  16287. {
  16288. std::vector<std::uint8_t> state (4 + 4 * c.raw.size(), 0);
  16289. const std::int32_t version = 1;
  16290. std::memcpy (state.data(), &version, 4);
  16291. std::memcpy (state.data() + 4, c.raw.data(), 4 * c.raw.size());
  16292. const auto slot = lynx::effectFromFl (c.name, state);
  16293. CHECK (slot && slot->kind != lynx::EffectKind::utility && slot->kind != lynx::EffectKind::eq
  16294. && slot->kind != lynx::EffectKind::parametricEq);
  16295. if (! slot)
  16296. continue;
  16297. auto fx = lynx::makeEffect (slot->kind);
  16298. fx->prepare ((double) c.rate);
  16299. std::vector<float> l (24000), r (24000);
  16300. std::uint32_t s = 12345;
  16301. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16302. for (int i = 0; i < 24000; ++i)
  16303. {
  16304. const float a = amps[(i / 3000) % 8];
  16305. s = s * 1664525u + 1013904223u;
  16306. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16307. s = s * 1664525u + 1013904223u;
  16308. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16309. }
  16310. // host blocks of 512 like the goldens' DLL calls (the Center / Bass Boost / 7 Band EQ round their state to double at
  16311. // the end of each call, as FL does, so other block sizes differ in the last bits; checked below)
  16312. const auto input = std::make_pair (l, r);
  16313. for (int i = 0; i < 24000; i += 512)
  16314. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16315. {
  16316. auto other = lynx::makeEffect (slot->kind);
  16317. other->prepare ((double) c.rate);
  16318. auto l2 = input.first, r2 = input.second;
  16319. for (int i = 0; i < 24000; i += 1000)
  16320. other->process (l2.data() + i, r2.data() + i, 1000, slot->params, 120.0);
  16321. float worst = 0.0f;
  16322. for (std::size_t i = 0; i < 24000; ++i)
  16323. worst = std::max ({ worst, std::abs (l2[i] - l[i]) / std::max (1.0f, std::abs (l[i])), std::abs (r2[i] - r[i]) / std::max (1.0f, std::abs (r[i])) });
  16324. CHECK (worst <= 1.0e-4f); // (measured: ≤ 3.5e-5, the Bass Boost at 20 Hz)
  16325. }
  16326. for (int k = 0; k < 5; ++k)
  16327. {
  16328. const auto i = (std::size_t) (1500 + 4500 * k);
  16329. CHECK (std::abs (l[i] - c.pts[(std::size_t) (2 * k)]) <= x87Tolerance() * std::max (1.0f, std::abs (c.pts[(std::size_t) (2 * k)])));
  16330. CHECK (std::abs (r[i] - c.pts[(std::size_t) (2 * k + 1)]) <= x87Tolerance() * std::max (1.0f, std::abs (c.pts[(std::size_t) (2 * k + 1)])));
  16331. }
  16332. #if defined(__x86_64__) || defined(__i386__)
  16333. std::uint64_t h = 0xcbf29ce484222325ull;
  16334. for (int i = 0; i < 24000; ++i)
  16335. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16336. {
  16337. std::uint32_t u;
  16338. std::memcpy (&u, &v, 4);
  16339. h = (h ^ u) * 0x100000001b3ull;
  16340. }
  16341. CHECK (h == c.hash);
  16342. #endif
  16343. }
  16344. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.3f, 120.0) == "None");
  16345. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.5f, 120.0) == "Left");
  16346. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 1.0f, 120.0) == "Right");
  16347. CHECK (lynx::formatEffectParam (lynx::EffectKind::flBassBoost, 0, 0.5f, 120.0) == "60 Hz");
  16348. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 1.0f, 120.0) == "+18.0 dB");
  16349. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 0.5f, 120.0) == "+0.0 dB");
  16350. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 0.0f, 120.0) == "Host");
  16351. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 1.0f, 120.0) == "280 BPM");
  16352. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 4, 2.0f / 15.0f, 120.0) == "3/16");
  16353. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 5, 1.0f, 120.0) == "Ping-pong");
  16354. CHECK (lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 }) == std::nullopt
  16355. || lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 })->kind != lynx::EffectKind::flDelay1);
  16356. // the default Phase Inverter of FL inverts the right side only (LYNX's old import inverted both)
  16357. CHECK (lynx::fllegacy::phaseInverterMode (1024) == 2 && lynx::fllegacy::phaseInverterMode (341) == 0
  16358. && lynx::fllegacy::phaseInverterMode (342) == 1 && lynx::fllegacy::phaseInverterMode (683) == 2);
  16359. }
  16360. TEST (fl_frequency_shifter_matches_fl_engine)
  16361. {
  16362. // REA_rodada154 §4: FL's Frequency Shifter (import → slot → effect) against goldens of the installation's
  16363. // Engine_x64.dll driven by its front end (rate before the state, 1536 samples of silence first, transport stopped, 120 BPM): FNV-1a
  16364. // of the output
  16365. struct Case { std::array<int, 10> raw; double rate; std::uint64_t hash; };
  16366. const Case cases[] { { { 10000, 0, -60480, 0, 10000, 10000, 5000, 10000, 1, 5000 }, 44100.0, 0x462af5f13683c90eull },
  16367. { { 7000, 2, 40000, 3000, 10000, 10000, 8000, 6000, 0, 2000 }, 44100.0, 0xcd597b95bf1bbb1aull },
  16368. { { 6000, 1, -30000, 6000, 3000, 7000, 3000, 4000, 0, 5000 }, 48000.0, 0x5592232115cb3acull },
  16369. { { 10000, 0, 51542, 2000, 8000, 10000, 5000, 10000, 0, 5000 }, 48000.0, 0x63d3051ba14bd00full } };
  16370. for (const auto& c : cases)
  16371. {
  16372. std::vector<std::uint8_t> state (4 + 40 + 8, 0);
  16373. const std::int32_t version = 4;
  16374. std::memcpy (state.data(), &version, 4);
  16375. std::memcpy (state.data() + 4, c.raw.data(), 40);
  16376. const auto slot = lynx::effectFromFl ("Frequency Shifter", state);
  16377. CHECK (slot && slot->kind == lynx::EffectKind::flFrequencyShifter);
  16378. if (! slot)
  16379. continue;
  16380. auto fx = lynx::makeEffect (slot->kind);
  16381. fx->prepare (c.rate);
  16382. std::vector<float> l (24000), r (24000);
  16383. std::uint32_t s = 12345;
  16384. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16385. for (int i = 0; i < 24000; ++i)
  16386. {
  16387. const float a = amps[(i / 3000) % 8];
  16388. s = s * 1664525u + 1013904223u;
  16389. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16390. s = s * 1664525u + 1013904223u;
  16391. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16392. }
  16393. for (int i = 0; i < 24000; i += 800)
  16394. fx->process (l.data() + i, r.data() + i, 800, slot->params, 120.0);
  16395. std::uint64_t h = 0xcbf29ce484222325ull;
  16396. for (int i = 0; i < 24000; ++i)
  16397. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16398. {
  16399. std::uint32_t u;
  16400. std::memcpy (&u, &v, 4);
  16401. h = (h ^ u) * 0x100000001b3ull;
  16402. }
  16403. CHECK (h == c.hash);
  16404. }
  16405. // the engine's tanhf against points of the library's
  16406. CHECK (lynx::fsfl::tanhf_ (0.0f) == 0.0f && lynx::fsfl::tanhf_ (20.0f) == 1.0f && lynx::fsfl::tanhf_ (-20.0f) == -1.0f);
  16407. CHECK (std::fabs (lynx::fsfl::tanhf_ (0.5f) - 0.462117165f) < 1e-7f && std::fabs (lynx::fsfl::tanhf_ (2.0f) - 0.964027584f) < 1e-7f);
  16408. CHECK (lynx::formatEffectParam (lynx::EffectKind::flFrequencyShifter, 1, 1.0f, 120.0) == "Ring modulator");
  16409. }
  16410. TEST (fl_low_lifter_matches_fl_engine)
  16411. {
  16412. // REA_rodada154 §5: FL's Low Lifter (import → slot → effect, 512-frame calls) against goldens of the installation's
  16413. // engine (ildsp_x64.dll's Faust program driven by Low Lifter_x64.dll, rate before the state): FNV-1a of the output
  16414. struct Case { std::array<int, 15> raw; double rate; std::uint64_t hash; };
  16415. const Case cases[] { { { 1, 10000, 4999, 7210, 1, 0, 0, 0, 5000, 10000, 7750, 0, 0, 1, 1 }, 44100.0, 0xcbb7eae8f31ca6d9ull },
  16416. { { 1, 7506, 5000, 3277, 1, 0, 6757, 5053, 9608, 10000, 7778, 5281, 1, 1, 1 }, 44100.0, 0xdb2d3712d4fb16c5ull },
  16417. { { 1, 5095, 6737, 5109, 1, 1, 541, 1479, 5451, 8197, 690, 6833, 1, 1, 1 }, 48000.0, 0x273c262202984447ull } };
  16418. for (const auto& c : cases)
  16419. {
  16420. std::vector<std::uint8_t> state (4 + 60 + 18, 0);
  16421. const std::int32_t version = 6;
  16422. std::memcpy (state.data(), &version, 4);
  16423. std::memcpy (state.data() + 4, c.raw.data(), 60);
  16424. const auto slot = lynx::effectFromFl ("Low Lifter", state);
  16425. CHECK (slot && slot->kind == lynx::EffectKind::flLowLifter);
  16426. if (! slot)
  16427. continue;
  16428. auto fx = lynx::makeEffect (slot->kind);
  16429. fx->prepare (c.rate);
  16430. std::vector<float> l (24000), r (24000);
  16431. std::uint32_t s = 12345;
  16432. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16433. for (int i = 0; i < 24000; ++i)
  16434. {
  16435. const float a = amps[(i / 3000) % 8];
  16436. s = s * 1664525u + 1013904223u;
  16437. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16438. s = s * 1664525u + 1013904223u;
  16439. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16440. }
  16441. for (int i = 0; i < 24000; i += 512)
  16442. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16443. std::uint64_t h = 0xcbf29ce484222325ull;
  16444. for (int i = 0; i < 24000; ++i)
  16445. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16446. {
  16447. std::uint32_t u;
  16448. std::memcpy (&u, &v, 4);
  16449. h = (h ^ u) * 0x100000001b3ull;
  16450. }
  16451. CHECK (h == c.hash);
  16452. }
  16453. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 10, 0.775f, 120.0) == "80 Hz");
  16454. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 3, 0.721f, 120.0) == "-6.0 dB");
  16455. }
  16456. #include "Rea153Tests.inc"
  16457. #include "Logger181bTests.inc"
  16458. #include "Render155Tests.inc"
  16459. #include "Tune157Tests.inc"
  16460. #include "Tune165Tests.inc"
  16461. #include "HwOut156Tests.inc"
  16462. #include "SoundFont158Tests.inc"
  16463. #include "Mp3157Tests.inc"
  16464. #include "Quality160Tests.inc"
  16465. #include "CrashReport159Tests.inc"
  16466. #include "Sampler162Tests.inc"
  16467. #include "Workflow163cTests.inc"
  16468. #include "ClipPitch164Tests.inc"
  16469. #include "Misc164mTests.inc"
  16470. #include "MidiSync165Tests.inc"
  16471. #include "Theme162Tests.inc"
  16472. #include "PluginPresets162Tests.inc"
  16473. #include "Look162Tests.inc"
  16474. #include "EnvController162Tests.inc"
  16475. #include "Browser162Tests.inc"
  16476. #include "BrowserView163Tests.inc"
  16477. #include "Transport162Tests.inc"
  16478. #include "Workflow163Tests.inc"
  16479. #include "Rec163Tests.inc"
  16480. #include "Ui163Tests.inc"
  16481. #include "HwInsert164Tests.inc"
  16482. #include "Layer164Tests.inc"
  16483. #include "Patcher164vTests.inc"
  16484. #include "Vfx164Tests.inc"
  16485. #include "VfxScript165Tests.inc"
  16486. #include "Perf167Tests.inc"
  16487. #include "Misc164Tests.inc"
  16488. #include "Store165Tests.inc" // (round 165n; kept off the end of the list, where every round appends)
  16489. #include "Rack165Tests.inc" // (round 165f-rack; likewise)
  16490. #include "Visual164Tests.inc"
  16491. #include "Visual165Tests.inc"
  16492. #include "Dance190baTests.inc"
  16493. #include "Patcher164fTests.inc"
  16494. #include "Markers165Tests.inc"
  16495. #include "PerfMode165Tests.inc"
  16496. #include "Rea167gTests.inc"
  16497. #include "Rea167hTests.inc"
  16498. #include "Rea167jTests.inc"
  16499. #include "Rea167kTests.inc"
  16500. #include "Rea167lTests.inc"
  16501. #include "Patch165Tests.inc"
  16502. #include "FlPlugins165Tests.inc"
  16503. #include "FlScratch165Tests.inc"
  16504. #include "FlTrans165Tests.inc"
  16505. #include "VideoPlayer165Tests.inc"
  16506. #include "Video174dTests.inc"
  16507. #include "Dash165Tests.inc"
  16508. #include "LinuxMouse165Tests.inc"
  16509. #include "Fl165Tests.inc"
  16510. #include "Issues167oTests.inc"
  16511. #include "Perf165Tests.inc"
  16512. #include "Issue12Tests.inc"
  16513. #include "Issue12cTests.inc"
  16514. #include "Audit165zbTests.inc"
  16515. #include "FlGran165Tests.inc"
  16516. #include "GranDrop165Tests.inc"
  16517. #include "Kepler165kTests.inc"
  16518. #include "KeplerExo167dTests.inc"
  16519. #include "MixerLatency165Tests.inc"
  16520. #include "Rea168jTests.inc"
  16521. #include "Audit165Tests.inc"
  16522. #include "Vfx165cTests.inc"
  16523. #include "HarmorBeta165hTests.inc"
  16524. #include "Crash13Tests.inc"
  16525. #include "Issue11bTests.inc"
  16526. #include "Issue11cTests.inc"
  16527. #include "FlNewt165Tests.inc"
  16528. #include "Audit165zATests.inc"
  16529. #include "Audit165zA2Tests.inc"
  16530. #include "Eq166fTests.inc"
  16531. #include "Feat166dTests.inc"
  16532. #include "Round166eTests.inc"
  16533. #include "Issue15Tests.inc"
  16534. #include "Issue14bugsTests.inc"
  16535. #include "Mute190yTests.inc"
  16536. #include "Left167Tests.inc"
  16537. #include "Wheel173dTests.inc"
  16538. #include "PoiZoneWheels173dTests.inc"
  16539. #include "Tune166cTests.inc"
  16540. #include "TuneBreath190bbTests.inc"
  16541. #include "Update166bTests.inc"
  16542. #include "Emphasis167fTests.inc"
  16543. #include "Dvs167Tests.inc"
  16544. #include "EdisonScript167Tests.inc"
  16545. #include "PianoRollScript190Tests.inc"
  16546. #include "Deverb167Tests.inc"
  16547. #include "Score167Tests.inc"
  16548. #include "Ntt167Tests.inc"
  16549. #include "MidiOut167iTests.inc"
  16550. #include "Transporter167fTests.inc"
  16551. #include "Polish167pTests.inc"
  16552. #include "Menus168zTests.inc"
  16553. #include "Session168aTests.inc"
  16554. #include "Live169bTests.inc"
  16555. #include "Live169jTests.inc"
  16556. #include "Live190ahTests.inc"
  16557. #include "Remote190aiTests.inc"
  16558. #include "StemSplitTests.inc"
  16559. #include "AudioToMidi189bTests.inc"
  16560. #include "Zoom167tTests.inc"
  16561. #include "PianoKeys190jTests.inc"
  16562. #include "Proll168gTests.inc"
  16563. #include "Rea168eTests.inc"
  16564. #include "Design168nTests.inc"
  16565. #include "Design168qTests.inc"
  16566. #include "FirstRun167vTests.inc"
  16567. #include "LoopStarter168bTests.inc"
  16568. #include "Rec168kTests.inc"
  16569. #include "FlPaths124Tests.inc"
  16570. #include "Auto125Tests.inc"
  16571. #include "Rec170dTests.inc"
  16572. #include "Rec177dTests.inc"
  16573. #include "Pain177dTests.inc"
  16574. #include "Mixer177dTests.inc"
  16575. #include "Edit177dTests.inc"
  16576. #include "Patterns177dTests.inc"
  16577. #include "Issue32Tests.inc"
  16578. #include "Keys117Tests.inc"
  16579. #include "Pedal187aTests.inc"
  16580. #include "Sc109Tests.inc"
  16581. #include "WrapperMidi181cTests.inc"
  16582. #include "Keys96Tests.inc"
  16583. #include "Mono168lTests.inc"
  16584. #include "MixPre167qTests.inc"
  16585. #include "Design168rTests.inc"
  16586. #include "Playlist168fTests.inc"
  16587. #include "RecNoFold126Tests.inc"
  16588. #include "LoopWrap167qTests.inc"
  16589. #include "Sel168tTests.inc"
  16590. #include "Gap169kTests.inc"
  16591. #include "Gap169kPickerTests.inc"
  16592. #include "Gap169kAlignTests.inc"
  16593. #include "FlQ170iTests.inc"
  16594. #include "FlQ170i2Tests.inc"
  16595. #include "FlQ170i3Tests.inc"
  16596. #include "Speech168bTests.inc"
  16597. #include "Design168pTests.inc"
  16598. #include "Typing168tTests.inc"
  16599. #include "TypingPlace190bmTests.inc"
  16600. #include "GrossBeat168mTests.inc"
  16601. #include "Maximus168xTests.inc"
  16602. #include "Rec168xTests.inc"
  16603. #include "Design168oTests.inc"
  16604. #include "Pr44BrowserListTests.inc"
  16605. #include "Mixer168hTests.inc"
  16606. #include "Mute53fTests.inc"
  16607. #include "Issue32cTests.inc"
  16608. #include "Drumaxx169eTests.inc"
  16609. #include "PatternBlocks170fTests.inc"
  16610. #include "Mixfx170bTests.inc"
  16611. #include "Typeahead170bTests.inc"
  16612. #include "PlugQ170cTests.inc"
  16613. #include "Toolbar170eTests.inc"
  16614. #include "Rack170aTests.inc"
  16615. #include "Inst169hTests.inc"
  16616. #include "Crash169gTests.inc"
  16617. #include "Wfqa170hTests.inc"
  16618. #include "Toxic169hTests.inc"
  16619. #include "Gen169lTests.inc"
  16620. #include "Picker171cTests.inc"
  16621. #include "Picker175bTests.inc"
  16622. #include "Gen169l2Tests.inc"
  16623. #include "Patcher171bTests.inc"
  16624. #include "Wfqa170h2Tests.inc"
  16625. #include "Fader171cTests.inc"
  16626. #include "Tb169g2Tests.inc"
  16627. #include "Issues171aTests.inc"
  16628. #include "CtlAudit170i3Tests.inc"
  16629. #include "CtlWake173dTests.inc"
  16630. #include "Issue32dTests.inc"
  16631. #include "Issue81Tests.inc"
  16632. #include "Issue85Tests.inc"
  16633. #include "FlpExport171eTests.inc"
  16634. #include "Tp172cTests.inc"
  16635. #include "Layout172aTests.inc"
  16636. #include "Issues173aTests.inc"
  16637. #include "FxAudit173dTests.inc"
  16638. #include "Audit173d3Tests.inc"
  16639. #include "Vocodex173d3Tests.inc"
  16640. #include "Fluid174eTests.inc"
  16641. #include "Comp177Tests.inc"
  16642. #include "AlsImport175aTests.inc"
  16643. #include "FlmImport182cTests.inc"
  16644. #include "FlmEngines190uTests.inc"
  16645. #include "FlmNotes190abTests.inc"
  16646. #include "FlmGmsynth190abTests.inc"
  16647. #include "PocketSuperSaw190byTests.inc" // round 190by
  16648. #include "FlmImport187dTests.inc"
  16649. #include "FlmImport188fTests.inc"
  16650. #include "FlmImport188hTests.inc"
  16651. #include "FlmImport189hTests.inc"
  16652. #include "FlmImport189hATests.inc"
  16653. #include "FlmImport189hBTests.inc"
  16654. #include "FlmImport189hCTests.inc"
  16655. #include "FlmImport190bsTests.inc"
  16656. #include "PocketFx190byTests.inc" // round 190by
  16657. #include "FlmImport190deTests.inc" // round 190de
  16658. #include "PocketEq190dwTests.inc" // round 190dw
  16659. #include "PocketReverb190dwTests.inc" // round 190dw
  16660. #include "FlmEffects190dwTests.inc" // round 190dw: the .flm effect laws against FLM's renders
  16661. #include "FlmImport190dwInstTests.inc" // round 190dw item 4
  16662. #include "FlmImport190feTests.inc" // round 190fe: cut groups, 3x Osc lanes, effect Mix, rack parts
  16663. #include "FlmImport190fnTests.inc" // round 190fn: FLM resources folders (O1), FL Studio Mobile Rack in an .flp, META, short SEND
  16664. #include "Bridge176aTests.inc"
  16665. #include "Issues178aTests.inc"
  16666. #include "Mixer177aTests.inc"
  16667. #include "ClipLauncher181aTests.inc"
  16668. #include "Session190bvTests.inc"
  16669. #include "DetailView183aTests.inc"
  16670. #include "Als181cTests.inc"
  16671. #include "Groove190fmTests.inc"
  16672. #include "AlsDevices190bqTests.inc"
  16673. #include "AlsDevices190cfTests.inc"
  16674. #include "AlsDevices190edTests.inc"
  16675. #include "AlsDevices190ekTests.inc"
  16676. #include "AlsDevices190fmTests.inc"
  16677. #include "Merge180cTests.inc"
  16678. #include "Slicer180cTests.inc"
  16679. #include "Slicer190fTests.inc"
  16680. #include "Issue160Tests.inc"
  16681. #include "Issue159Tests.inc"
  16682. #include "Issue165Tests.inc"
  16683. #include "Bugs190gyTests.inc"
  16684. #include "Fpc190mTests.inc"
  16685. #include "Fpc190qTests.inc"
  16686. #include "Fpc190zTests.inc"
  16687. #include "PluginExport180bTests.inc"
  16688. #include "FlExport180dTests.inc"
  16689. #include "FlpMute182eTests.inc"
  16690. #include "FlpPeak182eTests.inc"
  16691. #include "FlpPatcherSc182eTests.inc"
  16692. #include "FlFades180dTests.inc"
  16693. #include "Stretch180dTests.inc"
  16694. #include "Timing180dTests.inc"
  16695. #include "FlpPdc182eTests.inc"
  16696. #include "Pdc188eTests.inc"
  16697. #include "Mixer177a2Tests.inc"
  16698. #include "Mixer177a3Tests.inc"
  16699. #include "Mixer186cTests.inc"
  16700. #include "Mixer177a4Tests.inc"
  16701. #include "Sc177bTests.inc"
  16702. #include "Auto177bTests.inc"
  16703. #include "Lanes177bTests.inc"
  16704. #include "Lfo177bTests.inc"
  16705. #include "Curves190bwTests.inc"
  16706. #include "Mod177bTests.inc"
  16707. #include "UiFont179aTests.inc"
  16708. #include "Multilink177bTests.inc"
  16709. #include "Issue97Tests.inc"
  16710. #include "SimpleTracks184aTests.inc"
  16711. #include "SimpleChooser184aTests.inc"
  16712. #include "FlStateWrite184aTests.inc"
  16713. #include "MasterLooks189kTests.inc"
  16714. #include "LayoutStyle184aTests.inc"
  16715. #include "Rack116Tests.inc"
  16716. #include "Issue116DetachedTests.inc"
  16717. #include "Trial187bTests.inc"
  16718. #include "ProjectKey184aTests.inc"
  16719. #include "KeyGuess184aTests.inc"
  16720. #include "Render186bTests.inc"
  16721. #include "Playlist189eTests.inc"
  16722. #include "Playlist189gTests.inc"
  16723. #include "Playlist189iTests.inc"
  16724. #include "Playlist189lTests.inc"
  16725. #include "Playlist189nTests.inc"
  16726. #include "PlaylistKeys190agTests.inc"
  16727. #include "Rack183cTests.inc"
  16728. #include "DrumRack190ecTests.inc"
  16729. #include "DrumRackSlice190ecTests.inc"
  16730. #include "DrumRack190elTests.inc"
  16731. #include "Gamepad184aTests.inc"
  16732. #include "ExportBundle184aTests.inc"
  16733. #include "MultiOut188dTests.inc"
  16734. #include "FxConn189jTests.inc"
  16735. #include "Strike189dTests.inc"
  16736. #include "NoteChance187cTests.inc"
  16737. #include "MidiTools190oTests.inc"
  16738. #include "MidiTools190sTests.inc"
  16739. #include "MidiTools190aaTests.inc"
  16740. #include "MidiTools190aa2Tests.inc"
  16741. #include "MidiGenerate190fmTests.inc"
  16742. #include "Notes188gTests.inc"
  16743. #include "Layer190bTests.inc"
  16744. #include "PlugLevel190adTests.inc"
  16745. #include "Undo190bTests.inc"
  16746. #include "TrackMeters190wTests.inc"
  16747. #include "Locator190wTests.inc"
  16748. #include "Bugs190cTests.inc"
  16749. #include "Parity190vTests.inc"
  16750. #include "Parity190arFetTests.inc"
  16751. #include "Parity190arApexTests.inc"
  16752. #include "Slicex190lTests.inc"
  16753. #include "Parity190alTests.inc"
  16754. #include "Slicex190hTests.inc"
  16755. #include "AudioX190aqTests.inc"
  16756. #include "Parity190arTranslateTests.inc"
  16757. #include "Surge190xTests.inc"
  16758. #include "Issue114Tests.inc"
  16759. #include "Slicex190rTests.inc"
  16760. #include "Slicex190acTests.inc"
  16761. #include "Slicex190ajTests.inc"
  16762. #include "Slicex190hgTests.inc"
  16763. #include "ScaleSnap190anTests.inc"
  16764. #include "TruePeak190amTests.inc"
  16765. #include "Loudness190amTests.inc"
  16766. #include "UsedOnly190amTests.inc"
  16767. #include "Extras190amTests.inc"
  16768. #include "Reload190aoTests.inc"
  16769. #include "Pin190aoTests.inc"
  16770. #include "Velocity190aoTests.inc"
  16771. #include "Audition190aoTests.inc"
  16772. #include "Versions190apTests.inc"
  16773. #include "core/PluginHold.h"
  16774. #include "PluginsOff190apTests.inc"
  16775. #include "ExportSections190apTests.inc"
  16776. #include "DirectWave190ajTests.inc"
  16777. #include "Similar190asTests.inc"
  16778. #include "Controller190awTests.inc"
  16779. #include "ControllerProfiles190awTests.inc"
  16780. #include "Bugs190axInsert6Tests.inc"
  16781. #include "Snap190axTests.inc"
  16782. #include "PluginState190azTests.inc"
  16783. #include "Parity190arTests.inc"
  16784. #include "Maps190caTests.inc"
  16785. #include "Mpe190bbTests.inc"
  16786. #include "Mpe190bbLiveTests.inc"
  16787. #include "PluginDb190baTests.inc"
  16788. #include "PerfWin190gdTests.inc"
  16789. #include "Ram190gnTests.inc"
  16790. #include "Startup190goTests.inc"
  16791. #include "Startup190hdTests.inc"
  16792. #include "Freeze190ayTests.inc"
  16793. #include "LiveFreeze190ffTests.inc"
  16794. #include "QaFix190bjTests.inc"
  16795. #include "Fl190bfTests.inc"
  16796. #include "ShotScale190biTests.inc"
  16797. #include "QaFix190bjATests.inc"
  16798. #include "Demo190bmTests.inc"
  16799. #include "Nebula190caTests.inc"
  16800. #include "Settings190bxTests.inc"
  16801. #include "Link190btTests.inc"
  16802. #include "LiveBrowser190ceTests.inc"
  16803. #include "SimpleFx190buTests.inc"
  16804. #include "SimpleGate190dgTests.inc"
  16805. #include "SimpleNext190dsTests.inc"
  16806. #include "FirstSongTour190dvTests.inc"
  16807. #include "SimplePads190foTests.inc"
  16808. #include "Rollup027Tests.inc"
  16809. #include "Edison190cdTests.inc"
  16810. #include "Fled190eeTests.inc"
  16811. #include "Warp190cgTests.inc"
  16812. #include "LiveScr190drTests.inc"
  16813. #include "LiveWf190efMapTests.inc"
  16814. #include "LiveWf190efDrawTests.inc"
  16815. #include "LiveWf190efScenesTests.inc"
  16816. #include "LiveWf190emTests.inc"
  16817. #include "BandlabExtras190chTests.inc"
  16818. #include "DanceFix190cjTests.inc"
  16819. #include "Reverb190bzTests.inc"
  16820. #include "Dyn190dmTests.inc"
  16821. #include "ParityDyn190fxTests.inc"
  16822. #include "Echo190dlTests.inc"
  16823. #include "Vqe190dpTests.inc"
  16824. #include "Ember190fjTests.inc"
  16825. #include "ApexFix190coTests.inc"
  16826. #include "Link190csTests.inc"
  16827. #include "TrackLinkTests.inc"
  16828. #include "TrackAuto190gzTests.inc"
  16829. #include "Warp190dbTests.inc"
  16830. #include "WarpModes190fmTests.inc" // round 190fm
  16831. #include "WarpLive190gxTests.inc" // round 190gx
  16832. #include "ClipEnv190giTests.inc" // round 190gi
  16833. #include "LiveFacts190gxTests.inc" // round 190gx
  16834. #include "BeatsLaw190gxTests.inc" // round 190gx
  16835. #include "FlCtl190dfTests.inc"
  16836. #include "FlFix190eyTests.inc"
  16837. #include "FlPl190dxTests.inc"
  16838. #include "FlClip190dzTests.inc"
  16839. #include "Iss152Tests.inc"
  16840. #include "Win190evTests.inc"
  16841. #include "U190heTests.inc"
  16842. #include "Mod190doTests.inc"
  16843. #include "Parity190fyTests.inc"
  16844. #include "Sampler190dhTests.inc"
  16845. #include "Fix190eaTests.inc"
  16846. #include "Iss158Tests.inc"
  16847. #include "Voices190ejTests.inc"
  16848. #include "Flfix190ezTests.inc"
  16849. #include "Reafl190flTests.inc"
  16850. #include "FlTails190gkTests.inc"
  16851. #include "Rcfix190fcTests.inc"
  16852. #include "Follow190fgTests.inc"
  16853. #include "Relfix190fpTests.inc"
  16854. #include "Anvil190fkTests.inc"
  16855. #include "AnvilBus190gxTests.inc"
  16856. #include "Donate190fqTests.inc"
  16857. #include "GenPreset190fsTests.inc"
  16858. #include "Issues190ftTests.inc"
  16859. #include "FlFacts190gwTests.inc"
  16860. #include "Flvsti190fwTests.inc"
  16861. #include "Stability190ggTests.inc"
  16862. #include "StemPass190gjTests.inc"
  16863. #include "Small190ghTests.inc"
  16864. #include "GoBack190hlTests.inc"
  16865. #include "Link190glTests.inc"
  16866. #include "SynthLanes190gpTests.inc"
  16867. #include "SchedIndex190gpTests.inc"
  16868. #include "SmartTails190gpTests.inc"
  16869. #include "ApexMeter190gpTests.inc"
  16870. #include "Toolchain190gtTests.inc"
  16871. #include "RenderAhead190gqTests.inc"
  16872. #include "EvPane190fzTests.inc"
  16873. #include "U190heLinkTests.inc" // round 190he (issue #152 item 2: linkable LYNX instruments, knobs that follow links)
  16874. #include "PluginDrop190hfTests.inc" // issue #152 point 6: plugins dropped on the tracks (FL and Live layouts)
  16875. #include "Rollup028Tests.inc" // round 190fv: semantic interactions of the 0.1.28 rollup
  16876. #include "PerfGate190idTests.inc" // round perfgate190id: the performance gate's synthetic projects (community slice B12)