tests.cpp 918 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806
  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/PinOnTop.h"
  143. #include "core/MetronomeVoice.h"
  144. #include "engine/Transport.h"
  145. #include "engine/RemoteHistory.h"
  146. #include "core/Patcherize.h"
  147. #include "core/Performance.h"
  148. #include "core/Scheduler.h"
  149. #include "core/MarkerPlan.h"
  150. #include "core/SnapshotExchange.h"
  151. #include "core/SoftClip.h"
  152. #include "core/Slicer.h"
  153. #include "core/SytrusEngine.h"
  154. #include "core/Templates.h"
  155. #include "core/Vocodex.h"
  156. #include "core/TimeStretch.h"
  157. #include "core/WorkerPool.h"
  158. #include "core/RtStats.h"
  159. #include "core/FixedBlock.h"
  160. #include "core/ParametricEqLinear.h"
  161. #include "core/Song.h"
  162. #include "core/Slicex.h"
  163. #include "core/NoteEdit.h"
  164. #include "core/ScaleSnap.h"
  165. #include "core/LiveRecordView.h"
  166. #include "core/Recorder.h"
  167. #include "core/KeyMap.h"
  168. #include "core/EventPaneSplit.h" // round 190fz
  169. #include "core/UndoKeys.h"
  170. #include "core/SortOnce.h"
  171. #include "core/RackOps.h"
  172. #include "core/PlaylistTrackOps.h"
  173. #include "core/ChannelLoops.h"
  174. #include "core/ChannelBurn.h"
  175. #include "core/AutoSampler.h"
  176. #include "core/ValueMath.h"
  177. #include "core/Workspace.h"
  178. #include "core/StartupSound.h"
  179. #include "core/ExportOptions.h"
  180. #include "core/Mp3Encoder.h"
  181. #include "core/PluginMonitor.h"
  182. #include "core/PluginSubstitutes.h"
  183. #include "core/ClipOps.h"
  184. #include "core/PlaylistOps.h"
  185. #include "core/PluginDrop.h"
  186. #include "core/QuickRender.h"
  187. #include "core/TouchKeyboard.h"
  188. #include "core/HeldNotes.h"
  189. #include "core/Templates.h"
  190. #include "flp/Flp.h"
  191. #include "ui/I18nData.h"
  192. #include "ui/I18nLocale.h"
  193. #include "core/CrashReport.h"
  194. #include "core/RenderAhead.h"
  195. #include "core/WorkerPool.h"
  196. namespace
  197. {
  198. int failures = 0;
  199. int checks = 0;
  200. std::string processTempTag(); // per-process suffix for scratch files (defined further down)
  201. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  202. {
  203. static std::vector<std::pair<const char*, std::function<void()>>> r;
  204. return r;
  205. }
  206. struct Register
  207. {
  208. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  209. };
  210. #define TEST(name) \
  211. static void name(); \
  212. static Register reg_##name (#name, name); \
  213. static void name()
  214. #define CHECK(cond) \
  215. do \
  216. { \
  217. ++checks; \
  218. if (! (cond)) \
  219. { \
  220. ++failures; \
  221. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  222. } \
  223. } while (false)
  224. // Engines that compute in x87 80-bit long double (FL's own arithmetic) match their goldens to the last bit on x86. On
  225. // ARM64 (Apple Silicon) long double is not the x87 format, so there they hold to a looser bound (round 117).
  226. // LYNX_X87_TOL overrides it (measuring).
  227. inline float armTolerance (float x86, float arm)
  228. {
  229. #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
  230. (void) arm;
  231. return x86;
  232. #else
  233. if (const char* t = std::getenv ("LYNX_X87_TOL"))
  234. return (float) std::atof (t);
  235. return arm;
  236. #endif
  237. }
  238. inline float x87Tolerance() { return armTolerance (2.0e-7f, 2.0e-4f); }
  239. inline float granulizerTolerance()
  240. {
  241. #if defined(__APPLE__)
  242. return 2.0e-4f;
  243. #else
  244. return armTolerance (1.0e-6f, 1.0e-5f);
  245. #endif
  246. }
  247. // Engines whose goldens depend on the C library's sin/cos/exp/pow: Windows (mingw), glibc and musl hold the 1e-7
  248. // goldens, Apple's libm does not (round 117), so on macOS the per-sample bound is looser.
  249. inline float libmTolerance (float strict)
  250. {
  251. #if defined(__APPLE__)
  252. (void) strict;
  253. return 2.0e-4f;
  254. #else
  255. return strict;
  256. #endif
  257. }
  258. // One golden sample: relative bound (with a 1e-3 floor on macOS, where near-zero samples carry libm noise); prints the
  259. // measured error when it fails.
  260. inline bool goldenSampleClose (float y, float golden, float tol)
  261. {
  262. #if defined(__APPLE__)
  263. const float scale = std::max (1.0e-3f, std::abs (golden));
  264. #else
  265. const float scale = std::max (1.0e-16f, std::abs (golden));
  266. #endif
  267. if (std::abs (y - golden) <= tol * scale)
  268. return true;
  269. std::fprintf (stderr, " golden sample %.9g vs %.9g (relative error %.3g, bound %.3g)\n", (double) y, (double) golden,
  270. (double) (std::abs (y - golden) / scale), (double) tol);
  271. return false;
  272. }
  273. std::string realFlpDir;
  274. // --- helpers for building synthetic .flp files -----------------------------
  275. struct FlpBuilder
  276. {
  277. std::vector<std::uint8_t> bytes;
  278. FlpBuilder (std::uint16_t ppq = 96)
  279. {
  280. put ("FLhd");
  281. u32 (6);
  282. u16 (0);
  283. u16 (1);
  284. u16 (ppq);
  285. put ("FLdt");
  286. u32 (0); // size is not used by the reader
  287. }
  288. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  289. void u8 (std::uint8_t v) { bytes.push_back (v); }
  290. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  291. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  292. void varint (std::size_t n)
  293. {
  294. do
  295. {
  296. auto b = (std::uint8_t) (n & 0x7F);
  297. n >>= 7;
  298. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  299. } while (n);
  300. }
  301. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  302. {
  303. u8 (id);
  304. varint (payload.size());
  305. bytes.insert (bytes.end(), payload.begin(), payload.end());
  306. }
  307. void ascii (std::uint8_t id, const std::string& s)
  308. {
  309. std::vector<std::uint8_t> p (s.begin(), s.end());
  310. p.push_back (0);
  311. data (id, p);
  312. }
  313. void utf16 (std::uint8_t id, const std::u16string& s)
  314. {
  315. std::vector<std::uint8_t> p;
  316. for (auto c : s)
  317. {
  318. p.push_back ((std::uint8_t) c);
  319. p.push_back ((std::uint8_t) (c >> 8));
  320. }
  321. p.push_back (0);
  322. p.push_back (0);
  323. data (id, p);
  324. }
  325. };
  326. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  327. std::uint8_t velocity)
  328. {
  329. std::vector<std::uint8_t> r (24, 0);
  330. std::memcpy (&r[0], &pos, 4);
  331. std::memcpy (&r[6], &channel, 2);
  332. std::memcpy (&r[8], &len, 4);
  333. std::memcpy (&r[12], &key, 2);
  334. r[16] = 120;
  335. r[20] = 64;
  336. r[21] = velocity;
  337. return r;
  338. }
  339. } // namespace
  340. // ---------------------------------------------------------------------------
  341. // FLP
  342. TEST (flp_rejects_garbage)
  343. {
  344. std::vector<std::uint8_t> junk (64, 0x41);
  345. bool threw = false;
  346. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  347. CHECK (threw);
  348. }
  349. TEST (flp_reads_size_classes_varint_and_extended_ids)
  350. {
  351. FlpBuilder b;
  352. b.u8 (0x15); b.u8 (2); // u8
  353. b.u8 (0x40); b.u16 (513); // u16
  354. b.u8 (0x9C); b.u32 (128500); // u32
  355. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  356. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  357. flp::EventReader r (b.bytes);
  358. flp::Event e;
  359. CHECK (r.header().ppq == 96);
  360. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  361. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  362. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  363. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  364. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  365. CHECK (! r.next (e));
  366. }
  367. TEST (flp_truncated_file_throws)
  368. {
  369. FlpBuilder b;
  370. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  371. flp::EventReader r (b.bytes);
  372. flp::Event e;
  373. bool threw = false;
  374. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  375. CHECK (threw);
  376. }
  377. TEST (flp_version_numbers)
  378. {
  379. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  380. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  381. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  382. }
  383. TEST (flp_playlist_record_size_by_version)
  384. {
  385. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  386. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  387. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  388. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  389. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  390. }
  391. TEST (flp_playlist_record_size_by_file_flags)
  392. {
  393. // REA_rodada2 §4.2: 0xA9 bit 1 -> 80 B, bits 1 and 3 -> the native 88 B record (FL 25.2.99 / 26.x)
  394. CHECK (flp::playlistRecordSize (26001003, 88 * 4, 15) == 88); // 352 is also a multiple of 32
  395. CHECK (flp::playlistRecordSize (26001003, 88 * 10, 15) == 88); // ... and of 80
  396. CHECK (flp::playlistRecordSize (25002005, 160, 7) == 80);
  397. CHECK (flp::playlistRecordSize (21000000, 120, 1) == 60);
  398. CHECK (flp::playlistRecordSize (26001003, 63712) == 88); // no flags: the only size that fits besides 32
  399. }
  400. TEST (flp_native_88_byte_playlist_items_with_clip_gain_and_fades)
  401. {
  402. // an FL 26.1 file: 0xA9 = 15, four 88 B items (352 B: the old 32 B guess read garbage from it)
  403. FlpBuilder b (96);
  404. b.ascii (0xC7, "26.1.3.5570");
  405. b.u8 (0xA9); b.u32 (15);
  406. b.u8 (0x9C); b.u32 (130000);
  407. b.u8 (0x40); b.u16 (0);
  408. b.u8 (0x15); b.u8 (4); // audio clip channel
  409. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  410. std::vector<std::uint8_t> items;
  411. auto add = [&] (std::uint32_t pos, std::uint32_t len, std::uint16_t track, float fadeIn, float fadeOut, float gain) {
  412. std::vector<std::uint8_t> r (88, 0);
  413. const std::uint16_t base = 20480, idx = 0, rv = (std::uint16_t) (499 - track);
  414. const float offs[] = { -1.0f, -1.0f };
  415. std::memcpy (&r[0], &pos, 4);
  416. std::memcpy (&r[4], &base, 2);
  417. std::memcpy (&r[6], &idx, 2);
  418. std::memcpy (&r[8], &len, 4);
  419. std::memcpy (&r[12], &rv, 2);
  420. std::memcpy (&r[0x18], offs, 8);
  421. std::memcpy (&r[0x24], &fadeIn, 4);
  422. std::memcpy (&r[0x2C], &fadeOut, 4);
  423. std::memcpy (&r[0x34], &gain, 4);
  424. r[0x38] = (std::uint8_t) ((fadeIn > 0.0f ? 1 : 0) | (fadeOut > 0.0f ? 2 : 0)); // manual fades (round 180d)
  425. const double stretch = 1.0;
  426. const std::int32_t minusOne = -1;
  427. std::memcpy (&r[0x40], &stretch, 8);
  428. std::memcpy (&r[0x4C], &minusOne, 4);
  429. items.insert (items.end(), r.begin(), r.end());
  430. };
  431. add (0, 384, 0, 0.0f, 0.0f, 1.0f);
  432. add (384, 384, 1, 923.0769f, 461.5385f, 1.65f); // 2 beats in, 1 beat out at 130 BPM
  433. add (768, 96, 2, 0.0f, 0.0f, 0.0f);
  434. add (1536, 192, 3, 0.0f, 5000.0f, 0.5f); // fade longer than the clip
  435. b.data (0xE9, items);
  436. const auto p = flp::parseProject (b.bytes);
  437. CHECK (p.flagsA9 == 15);
  438. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4);
  439. CHECK (p.warnings.empty());
  440. if (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4)
  441. {
  442. const auto& it = p.arrangements[0].items;
  443. CHECK (it[1].position == 384 && it[1].length == 384 && it[1].track == 1 && it[1].kind == flp::PlaylistItem::Kind::channelClip);
  444. CHECK (it[3].position == 1536 && it[3].track == 3);
  445. CHECK (std::abs (it[1].gain - 1.65f) < 1e-6f && std::abs (it[1].fadeInMs - 923.0769f) < 1e-3f);
  446. const auto song = lynx::importFlp (p);
  447. CHECK (song.playlist.size() == 4);
  448. if (song.playlist.size() == 4)
  449. {
  450. const auto beat = (std::int64_t) lynx::ppq;
  451. CHECK (song.playlist[0].gain == 1.0f && song.playlist[0].fadeIn == 0 && song.playlist[0].fadeOut == 0);
  452. CHECK (std::abs (song.playlist[1].gain - 1.65f) < 1e-6f);
  453. CHECK (std::llabs (song.playlist[1].fadeIn - 2 * beat) <= 1 && std::llabs (song.playlist[1].fadeOut - beat) <= 1);
  454. CHECK (song.playlist[2].gain == 0.0f);
  455. CHECK (song.playlist[3].fadeOut == song.playlist[3].length); // clamped
  456. }
  457. }
  458. }
  459. TEST (flp_channel_level_adjustments_and_stretched_clip_start)
  460. {
  461. // FL 26.1 audio clip channel (gf92 follow-up): 0xE5 level adjustments and a time-stretched clip whose item
  462. // start/end are in the sample's milliseconds ("Liwn Passar voz" ch5: 788 ms into a clip stretched by 1.0569)
  463. FlpBuilder b (96);
  464. b.ascii (0xC7, "26.1.3.5570");
  465. b.u8 (0xA9); b.u32 (15);
  466. b.u8 (0x9C); b.u32 (130000);
  467. b.u8 (0x40); b.u16 (0);
  468. b.u8 (0x15); b.u8 (4); // audio clip channel
  469. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  470. {
  471. std::vector<std::uint8_t> levels (24, 0);
  472. const std::int32_t pan = 6400, vol = 10000;
  473. std::memcpy (&levels[0], &pan, 4);
  474. std::memcpy (&levels[4], &vol, 4);
  475. b.data (0xDB, levels);
  476. std::vector<std::uint8_t> adjust (20, 0);
  477. const std::int32_t panAdj = 3200, volAdj = 7286;
  478. std::memcpy (&adjust[0], &panAdj, 4);
  479. std::memcpy (&adjust[4], &volAdj, 4);
  480. b.data (0xE5, adjust);
  481. std::vector<std::uint8_t> params (0x98, 0);
  482. const float time = 64.0f * 192.0f; // stretched to 64 beats (float: 0xA9 bit 0)
  483. const double end = 1.0;
  484. std::memcpy (&params[0x60], &time, 4);
  485. std::memcpy (&params[0x88], &end, 8);
  486. b.data (0xD7, params);
  487. }
  488. {
  489. std::vector<std::uint8_t> r (88, 0);
  490. const std::uint32_t pos = 0, len = 6144; // 64 beats
  491. const std::uint16_t base = 20480, idx = 0, rv = 499;
  492. const float offs[] = { 788.4615f, 788.4615f + 31218.92f }; // sample ms: 64 beats at 130 BPM x 1.056892
  493. const float gain = 1.0f;
  494. std::memcpy (&r[0], &pos, 4);
  495. std::memcpy (&r[4], &base, 2);
  496. std::memcpy (&r[6], &idx, 2);
  497. std::memcpy (&r[8], &len, 4);
  498. std::memcpy (&r[12], &rv, 2);
  499. std::memcpy (&r[0x18], offs, 8);
  500. std::memcpy (&r[0x34], &gain, 4);
  501. b.data (0xE9, r);
  502. }
  503. const auto p = flp::parseProject (b.bytes);
  504. CHECK (p.channels.size() == 1);
  505. if (p.channels.size() != 1)
  506. return;
  507. CHECK (p.channels[0].hasLevelAdjust && p.channels[0].panAdj == 3200 && p.channels[0].volAdj == 7286);
  508. const auto song = lynx::importFlp (p);
  509. CHECK (song.channels.size() == 1 && song.playlist.size() == 1);
  510. if (song.channels.size() != 1 || song.playlist.size() != 1)
  511. return;
  512. // gain = f(volume)·f(volAdj), pan = pan + panAdj (round 190dz: the adjustments are the channel's own Levels
  513. // adjustment, the knobs read as in FL)
  514. const auto expected = lynx::channelGain (10000.0f / 12800.0f) * lynx::channelGain (7286.0f / 12800.0f);
  515. CHECK (std::abs (lynx::channelGain (song.channels[0].volume) * lynx::levelAdjustGain (song.channels[0].misc.levels.volume) - expected) < 1e-5f);
  516. CHECK (std::abs (song.channels[0].volume - 10000.0f / 12800.0f) < 1e-6f);
  517. CHECK (std::abs (lynx::adjustedPan (song.channels[0].pan, song.channels[0].misc.levels.pan) - 0.5f) < 1e-6f);
  518. // 788.46 sample ms / 1.056892 = 746.0 ms = 1.6164 beats into the clip
  519. CHECK (song.channels[0].sampler.stretchBeats > 0.0f);
  520. CHECK (std::llabs (song.playlist[0].offset - (std::int64_t) std::llround (1.61638 * lynx::ppq)) <= 1);
  521. // REA_rodada110 §1: the sub-tick rest is kept (offset + fraction = the exact position, to the sample)
  522. 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;
  523. CHECK (std::abs ((double) song.playlist[0].offset + (double) song.playlist[0].offsetFraction - exact) < 1e-3);
  524. CHECK (std::abs (song.playlist[0].offsetFraction) <= 0.5f);
  525. }
  526. TEST (flp_parses_project_model)
  527. {
  528. FlpBuilder b (96);
  529. b.ascii (0xC7, "25.2.5.5319");
  530. b.u8 (0x9C); b.u32 (174500);
  531. b.utf16 (0xC2, u"Título");
  532. b.u8 (0x41); b.u16 (3);
  533. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  534. auto notes = noteRecord (96, 1, 48, 64, 100);
  535. auto n2 = noteRecord (0, 0, 0, 60, 128);
  536. notes.insert (notes.end(), n2.begin(), n2.end());
  537. b.data (0xE0, notes);
  538. b.u8 (0x40); b.u16 (0);
  539. b.u8 (0x15); b.u8 (0);
  540. b.utf16 (0xCB, u"Kick");
  541. {
  542. std::vector<std::uint8_t> levels (24, 0);
  543. const std::int32_t lv[] = { 3200, 12800, -150 };
  544. std::memcpy (levels.data(), lv, sizeof (lv));
  545. b.data (0xDB, levels);
  546. }
  547. b.u8 (0x68); b.u16 (5);
  548. b.u8 (0x40); b.u16 (1);
  549. b.u8 (0x15); b.u8 (2);
  550. b.utf16 (0xC9, u"FLEX");
  551. b.utf16 (0xCB, u"Lead");
  552. b.u8 (0x63); b.u16 (0);
  553. std::vector<std::uint8_t> item (80, 0);
  554. std::uint32_t pos = 384, len = 768;
  555. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  556. std::memcpy (&item[0], &pos, 4);
  557. std::memcpy (&item[4], &base, 2);
  558. std::memcpy (&item[6], &idx, 2);
  559. std::memcpy (&item[8], &len, 4);
  560. std::memcpy (&item[12], &rv, 2);
  561. b.data (0xE9, item);
  562. b.utf16 (0xCC, u"Drums");
  563. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  564. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  565. const auto p = flp::parseProject (b.bytes);
  566. CHECK (p.version == "25.2.5.5319");
  567. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  568. CHECK (p.title == "Título");
  569. CHECK (p.channels.size() == 2);
  570. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  571. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  572. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  573. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  574. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  575. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  576. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  577. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  578. const auto& it = p.arrangements[0].items[0];
  579. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  580. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  581. CHECK (p.warnings.empty());
  582. }
  583. TEST (flp_fuzz_corrupted_files_do_not_crash)
  584. {
  585. if (realFlpDir.empty())
  586. return;
  587. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  588. // must never hang, read past the buffer or produce a Song with invalid indices
  589. std::vector<std::vector<std::uint8_t>> sources;
  590. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  591. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  592. {
  593. std::ifstream in (entry.path(), std::ios::binary);
  594. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  595. }
  596. std::uint32_t seed = 2024;
  597. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  598. int parsed = 0, rejected = 0;
  599. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  600. {
  601. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  602. switch (round % 4)
  603. {
  604. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  605. 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;
  606. case 2: // absurd event size
  607. for (int k = 0; k < 5; ++k)
  608. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  609. break;
  610. default: // shuffled block
  611. {
  612. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  613. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  614. break;
  615. }
  616. }
  617. try
  618. {
  619. const auto project = flp::parseProject (bytes);
  620. const auto song = lynx::importFlp (project);
  621. ++parsed;
  622. for (const auto& p : song.patterns)
  623. for (const auto& n : p.notes)
  624. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  625. for (const auto& c : song.playlist)
  626. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  627. }
  628. catch (const flp::Error&)
  629. {
  630. ++rejected;
  631. }
  632. }
  633. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  634. CHECK (parsed + rejected == 240);
  635. }
  636. TEST (plugin_state_decoders_survive_garbage)
  637. {
  638. // random blobs (varied sizes, including valid headers followed by garbage)
  639. std::uint32_t seed = 77;
  640. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  641. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  642. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  643. int decoded = 0;
  644. for (int round = 0; round < 3000; ++round)
  645. {
  646. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  647. for (auto& b : blob)
  648. b = (std::uint8_t) rnd();
  649. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  650. {
  651. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  652. const auto v = versions[rnd() % 7];
  653. std::memcpy (blob.data(), &v, 4);
  654. }
  655. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  656. {
  657. ++decoded;
  658. for (auto v : fx->params)
  659. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  660. }
  661. lynx::SynthSettings synth;
  662. if (lynx::synthFrom3xOsc (blob, synth))
  663. for (const auto& o : synth.osc)
  664. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  665. float harmlessGain = 1.0f;
  666. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  667. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  668. if (lynx::synthFromFruitKick (blob, synth))
  669. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  670. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  671. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  672. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  673. if (auto sound = lynx::drumSynthLiveSound (blob))
  674. CHECK (sound->size() < 256);
  675. if (lynx::wavetrav::State wt; lynx::wavetrav::parseState (blob, wt))
  676. CHECK (wt.patches[60].waveN >= 0 && wt.patches[60].waveN < lynx::wavetrav::tableCapacity);
  677. lynx::synthFromBooBass (blob, synth);
  678. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  679. lynx::morphineStateUsable (blob);
  680. lynx::poizoneStateUsable (blob);
  681. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  682. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  683. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  684. if (auto patch = lynx::drumpadPatchName (blob))
  685. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  686. if (auto flex = lynx::flexPresetName (blob))
  687. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  688. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  689. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  690. if (lynx::synthFromDX10 (blob, synth))
  691. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  692. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  693. {
  694. std::memcpy (blob.data(), "SSLF", 4);
  695. if (blob.size() > 220)
  696. {
  697. std::memcpy (blob.data() + 40, "DATA", 4);
  698. std::memcpy (blob.data() + 48, "0rev", 4);
  699. }
  700. }
  701. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  702. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  703. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  704. {
  705. const auto params = lynx::miniSynthParams (blob);
  706. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  707. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  708. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  709. }
  710. lynx::ForeignPlugin foreign;
  711. lynx::parseFlWrapper (blob, foreign);
  712. std::vector<std::uint8_t> component, controller;
  713. lynx::splitFlVst3State (blob, component, controller);
  714. }
  715. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  716. }
  717. TEST (inflate_zlib)
  718. {
  719. // fixed Huffman with back-references
  720. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  721. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  722. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  723. // stored (uncompressed) block
  724. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  725. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  726. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  727. // truncated, bad header and size limit
  728. CHECK (! lynx::inflateZlib (fixed, 6));
  729. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  730. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  731. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  732. }
  733. TEST (fl_round15_effects_convert_known_values)
  734. {
  735. auto blob = [] (std::vector<std::int32_t> words) {
  736. std::vector<std::uint8_t> b (words.size() * 4);
  737. std::memcpy (b.data(), words.data(), b.size());
  738. return b;
  739. };
  740. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet — FL's own engine since round 154
  741. // (its controls as (raw − min) / range); unreadable states keep the generic shifter
  742. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  743. CHECK (fs && fs->kind == lynx::EffectKind::flFrequencyShifter);
  744. if (fs)
  745. {
  746. CHECK (std::abs (fs->params[0] - 1.0f) < 1e-6f && fs->params[1] == 0.0f);
  747. CHECK (std::abs (fs->params[2] - (51542.0f + 70000.0f) / 140000.0f) < 1e-6f);
  748. }
  749. const auto fsOld = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0 }));
  750. CHECK (fsOld && fsOld->kind == lynx::EffectKind::freqShift);
  751. // Emphasis (Bien Duro's Master): FL's own engine since round 167f, its controls as (raw − min) / range: input 625
  752. // (+1.5 dB), ceiling 9308 (−1.60 dB), envelope order 2, true peak; an unreadable state keeps the limiter
  753. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  754. CHECK (em && em->kind == lynx::EffectKind::flEmphasis && em->enabled);
  755. if (em)
  756. {
  757. CHECK (std::abs (em->params[6] - 0.0625f) < 1e-6f && std::abs (em->params[9] - 0.9308f) < 1e-6f);
  758. 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);
  759. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 6, em->params[6], 120.0) == "+1.5 dB");
  760. CHECK (lynx::formatEffectParam (lynx::EffectKind::flEmphasis, 9, em->params[9], 120.0) == "-1.60 dB");
  761. CHECK (em->params[14] == 0.0f && em->params[15] == 0.0f); // a version-8 state has no option bytes: off
  762. }
  763. const auto emOld = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2 }));
  764. CHECK (emOld && emOld->kind == lynx::EffectKind::limiter);
  765. // round 167f: FL's legacy dB Meter / Spectroman keep their slot as LYNX's analyser in the matching view
  766. const auto dbm = lynx::effectFromFl ("Fruity dB Meter", {});
  767. CHECK (dbm && dbm->kind == lynx::EffectKind::waveCandy
  768. && dbm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::peakMeter));
  769. const auto spm = lynx::effectFromFl ("Fruity Spectroman", {});
  770. CHECK (spm && spm->kind == lynx::EffectKind::waveCandy
  771. && spm->params[0] == lynx::visualfx::wavecandy::modeValue (lynx::visualfx::wavecandy::Mode::spectrum));
  772. // Pitch Shifter +12 st, mix 100%
  773. 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 }));
  774. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  775. }
  776. TEST (frequency_shifter_moves_spectrum_by_hz)
  777. {
  778. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  779. fx->prepare (48000.0);
  780. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  781. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  782. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  783. std::vector<float> l (48000), r (48000);
  784. for (int i = 0; i < 48000; ++i)
  785. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  786. for (int done = 0; done < 48000; done += 480)
  787. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  788. auto energy = [&] (double f) {
  789. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  790. double s1 = 0.0, s2 = 0.0;
  791. for (int i = 9600; i < 48000; ++i)
  792. {
  793. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  794. s2 = s1;
  795. s1 = s0;
  796. }
  797. return s1 * s1 + s2 * s2 - k * s1 * s2;
  798. };
  799. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  800. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  801. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  802. }
  803. TEST (pitch_shift_moves_frequency_keeps_length)
  804. {
  805. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  806. fx->prepare (48000.0);
  807. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  808. auto crossingsAfter = [&] (float semitoneParam) {
  809. fx->reset();
  810. slot.params[0] = semitoneParam;
  811. std::vector<float> l (48000), r (48000);
  812. for (int i = 0; i < 48000; ++i)
  813. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  814. for (int done = 0; done < 48000; done += 512)
  815. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  816. int crossings = 0;
  817. for (int i = 9600; i < 48000; ++i)
  818. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  819. for (auto v : l)
  820. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  821. return crossings / 0.8 / 2.0; // cycles per second
  822. };
  823. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  824. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  825. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  826. }
  827. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  828. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  829. namespace
  830. {
  831. struct GbPoint
  832. {
  833. double dx, y;
  834. std::uint8_t type;
  835. };
  836. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  837. const std::map<int, std::vector<GbPoint>>& slots)
  838. {
  839. std::vector<std::uint8_t> b;
  840. 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); };
  841. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  842. i32 (0x00080003);
  843. for (auto v : params)
  844. i32 (v);
  845. put (options.data(), options.size());
  846. for (int k = 0; k < 72; ++k)
  847. {
  848. b.push_back (0); // empty name
  849. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  850. b.push_back (0), b.push_back (0), b.push_back (0);
  851. const auto it = slots.find (k);
  852. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  853. const auto& pts = it != slots.end() ? it->second : flat;
  854. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  855. for (const auto& p : pts)
  856. {
  857. put (&p.dx, 8), put (&p.y, 8);
  858. const float tension = 0.0f;
  859. put (&tension, 4);
  860. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  861. }
  862. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  863. }
  864. return b;
  865. }
  866. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  867. {
  868. auto fx = lynx::makeEffect (slot);
  869. fx->prepare (rate);
  870. auto l = in, r = in;
  871. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  872. for (std::size_t done = 0; done < l.size(); done += 512)
  873. {
  874. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  875. fx->setSongPosition ((double) done * tps);
  876. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  877. }
  878. return l;
  879. }
  880. } // namespace
  881. TEST (gross_beat_state_container_and_parameters)
  882. {
  883. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  884. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  885. CHECK (parsed.has_value());
  886. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  887. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  888. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  889. std::vector<std::uint8_t> fl;
  890. int ppq = 0;
  891. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  892. // every raw FL value survives the 0..1 parameter
  893. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  894. bool roundTrip = true;
  895. for (int i = 0; i < 8; ++i)
  896. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  897. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  898. CHECK (roundTrip);
  899. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  900. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  901. }
  902. TEST (gross_beat_volume_slot_gates_the_bar)
  903. {
  904. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  905. 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 } };
  906. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  907. const auto slot = lynx::grossbeat::slotFromFl (state);
  908. CHECK (slot.has_value());
  909. if (! slot)
  910. return;
  911. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  912. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  913. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  914. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  915. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  916. }
  917. TEST (gross_beat_time_slot_reads_the_past_bar)
  918. {
  919. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  920. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  921. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  922. const auto slot = lynx::grossbeat::slotFromFl (state);
  923. CHECK (slot.has_value());
  924. if (! slot)
  925. return;
  926. std::vector<float> in (150000, 0.0f);
  927. in[1000] = 1.0f;
  928. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  929. std::size_t peak = 0;
  930. for (std::size_t i = 0; i < out.size(); ++i)
  931. if (std::abs (out[i]) > std::abs (out[peak]))
  932. peak = i;
  933. CHECK (peak == 1000 + 96000 + 15);
  934. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  935. float elsewhere = 0.0f;
  936. for (std::size_t i = 0; i < out.size(); ++i)
  937. if (i + 20 < peak || i > peak + 20)
  938. elsewhere = std::max (elsewhere, std::abs (out[i]));
  939. CHECK (elsewhere == 0.0f);
  940. }
  941. TEST (volume_gate_follows_bar_position)
  942. {
  943. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  944. fx->prepare (48000.0);
  945. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  946. for (int i = 0; i < 32; ++i)
  947. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  948. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  949. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  950. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  951. fx->setSongPosition (0.0);
  952. for (int done = 0; done < 96000; done += 512)
  953. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  954. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  955. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  956. // starting mid-bar (beat 3): already closed
  957. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  958. fx->reset();
  959. fx->setSongPosition (2.0 * lynx::ppq);
  960. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  961. CHECK (l2[4000] < 0.01f);
  962. // mix 0: transparent
  963. slot.params[32] = 0.0f;
  964. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  965. fx->setSongPosition (2.0 * lynx::ppq);
  966. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  967. CHECK (l3[4000] > 0.99f);
  968. }
  969. TEST (patcher_branch_heads_follow_rea)
  970. {
  971. auto words = [] (std::vector<std::int32_t> v) {
  972. std::vector<std::uint8_t> b (v.size() * 4);
  973. std::memcpy (b.data(), v.data(), b.size());
  974. return b;
  975. };
  976. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  977. std::vector<std::int32_t> fs (18, 0);
  978. fs[0] = 4;
  979. fs[1] = 32768;
  980. fs[3] = 3;
  981. fs[6] = 0;
  982. fs[11] = -600;
  983. fs[15] = 1;
  984. fs[16] = 2;
  985. fs[17] = 3;
  986. const auto split = words (fs);
  987. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  988. CHECK (low.valid && low.slot && low.note.empty());
  989. if (low.slot)
  990. {
  991. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  992. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  993. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  994. }
  995. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  996. 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
  997. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  998. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  999. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  1000. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  1001. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  1002. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  1003. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  1004. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  1005. }
  1006. TEST (parallel_container_sums_branches)
  1007. {
  1008. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  1009. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  1010. lynx::EffectBranch dry;
  1011. dry.gain = 0.5f;
  1012. lynx::EffectBranch inverted;
  1013. inverted.gain = 0.5f;
  1014. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  1015. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  1016. inverted.chain.push_back (flip);
  1017. slot.branches = { dry, inverted };
  1018. auto fx = lynx::makeParallelEffect (slot);
  1019. fx->prepare (48000.0);
  1020. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  1021. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1022. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  1023. // only the dry branch with gain 2: doubles
  1024. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  1025. auto doubler = lynx::makeParallelEffect (slot);
  1026. doubler->prepare (48000.0);
  1027. std::fill (l.begin(), l.end(), 0.25f);
  1028. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  1029. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  1030. }
  1031. TEST (fl_controller_constant_outputs)
  1032. {
  1033. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  1034. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  1035. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  1036. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  1037. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  1038. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  1039. std::vector<std::uint8_t> cs (4, 0);
  1040. cs[0] = 1;
  1041. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  1042. const auto size = (std::uint64_t) body.size();
  1043. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1044. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1045. out.insert (out.end(), body.begin(), body.end());
  1046. };
  1047. std::vector<std::uint8_t> value (12, 0);
  1048. const float v = 0.75f;
  1049. const std::int32_t out = 2;
  1050. std::memcpy (value.data(), &v, 4);
  1051. std::memcpy (value.data() + 8, &out, 4);
  1052. std::vector<std::uint8_t> group;
  1053. chunk (group, 0x836, value);
  1054. chunk (cs, 0, group);
  1055. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  1056. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  1057. }
  1058. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  1059. {
  1060. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  1061. const auto size = (std::uint64_t) body.size();
  1062. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  1063. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  1064. out.insert (out.end(), body.begin(), body.end());
  1065. };
  1066. auto i32s = [] (std::vector<std::int32_t> v) {
  1067. std::vector<std::uint8_t> b (v.size() * 4);
  1068. std::memcpy (b.data(), v.data(), b.size());
  1069. return b;
  1070. };
  1071. flp::Project p;
  1072. flp::Channel midiOut;
  1073. midiOut.index = 0;
  1074. midiOut.type = flp::ChannelType::native;
  1075. midiOut.pluginName = "MIDI Out";
  1076. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  1077. flp::Channel wrapper;
  1078. wrapper.index = 1;
  1079. wrapper.type = flp::ChannelType::native;
  1080. wrapper.pluginName = "Fruity Wrapper";
  1081. wrapper.pluginState = i32s ({ 9 });
  1082. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  1083. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  1084. p.channels = { midiOut, wrapper };
  1085. flp::Pattern pattern;
  1086. pattern.index = 1;
  1087. flp::Note note;
  1088. note.channel = 0;
  1089. note.key = 64;
  1090. note.length = 96;
  1091. pattern.notes.push_back (note);
  1092. p.patterns.push_back (pattern);
  1093. const auto song = lynx::importFlp (p);
  1094. // round 133: the MIDI Out channel stays a live route (its notes are sent at play time, with its MIDI channel)
  1095. CHECK (song.channels.size() == 2 && song.channels[0].instrument == lynx::InstrumentKind::midiOut && ! song.channels[0].muted);
  1096. CHECK (song.channels[1].plugin.foreign.midiInPort == 3 && song.channels[1].plugin.midiInPort == 3);
  1097. CHECK (lynx::midictl::parseMidiOut (song.channels[0].pluginState).port == 3);
  1098. int toWrapper = 0;
  1099. for (const auto& pat : song.patterns)
  1100. for (const auto& n : pat.notes)
  1101. toWrapper += n.channel == 1 && n.key == 64;
  1102. CHECK (toWrapper == 0);
  1103. }
  1104. TEST (flp_effect_automation_follows_slots_and_params)
  1105. {
  1106. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  1107. flp::Project p;
  1108. flp::MixerInsert insert;
  1109. insert.index = 1;
  1110. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  1111. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  1112. p.mixer.push_back (insert);
  1113. auto clip = [&] (int index, std::uint32_t target) {
  1114. flp::Channel c;
  1115. c.index = index;
  1116. c.type = flp::ChannelType::automationClip;
  1117. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  1118. p.channels.push_back (c);
  1119. p.automationLinks.push_back ({ index, { target } });
  1120. };
  1121. 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; };
  1122. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  1123. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  1124. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  1125. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  1126. const auto song = lynx::importFlp (p);
  1127. CHECK (song.automations.size() == 4);
  1128. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  1129. const auto& freq = song.automations[0];
  1130. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  1131. CHECK (freq.target.param == lynx::peq::param (0, 1));
  1132. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  1133. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  1134. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  1135. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  1136. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  1137. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  1138. // the automated mix is read/written through the target
  1139. auto copy = song;
  1140. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  1141. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  1142. }
  1143. TEST (flp_real_files)
  1144. {
  1145. if (realFlpDir.empty())
  1146. {
  1147. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  1148. return;
  1149. }
  1150. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  1151. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, wrappedVst2Old = 0, markers = 0, signatures = 0, trackEqs = 0;
  1152. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  1153. sytrusSounding = 0;
  1154. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  1155. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  1156. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  1157. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  1158. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  1159. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  1160. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  1161. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  1162. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  1163. int threeOscKind = 0, threeOscSounding = 0;
  1164. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  1165. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  1166. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  1167. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  1168. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  1169. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  1170. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  1171. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  1172. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  1173. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  1174. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  1175. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  1176. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  1177. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  1178. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  1179. int phaserSlots = 0, phaserExact = 0; // REA_rodada105_phaser
  1180. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  1181. lynx::FlpImportOptions withFiles;
  1182. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  1183. withFiles.roots = { flRoot, realFlpDir, "" };
  1184. withFiles.fileExists = [] (const std::string& path) {
  1185. auto local = path;
  1186. std::replace (local.begin(), local.end(), '\\', '/');
  1187. return std::filesystem::exists (local);
  1188. };
  1189. withFiles.readFile = [] (const std::string& path) {
  1190. auto local = path;
  1191. std::replace (local.begin(), local.end(), '\\', '/');
  1192. std::ifstream in (local, std::ios::binary);
  1193. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1194. };
  1195. int embeddedSaved = 0;
  1196. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  1197. ++embeddedSaved;
  1198. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  1199. };
  1200. int slicedChannels = 0, flKeysChannels = 0;
  1201. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  1202. {
  1203. if (entry.path().extension() != ".flp")
  1204. continue;
  1205. std::ifstream in (entry.path(), std::ios::binary);
  1206. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1207. ++files;
  1208. try
  1209. {
  1210. const auto p = flp::parseProject (bytes);
  1211. CHECK (! p.version.empty());
  1212. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  1213. const auto song = lynx::importFlp (p);
  1214. CHECK (! song.patterns.empty());
  1215. {
  1216. const auto full = lynx::importFlp (p, withFiles);
  1217. bool hasFpc = false;
  1218. for (const auto& c : p.channels)
  1219. hasFpc = hasFpc || c.pluginName == "FPC";
  1220. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  1221. // installation) and sounds; two demo channels match golden samples of our round-39 models
  1222. for (const auto& c : p.channels)
  1223. {
  1224. if (c.pluginName == "GMS")
  1225. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  1226. if (c.pluginName == "Morphine")
  1227. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  1228. }
  1229. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  1230. for (const auto& c : p.channels)
  1231. if (c.pluginName == "Kepler")
  1232. {
  1233. ++kepler;
  1234. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  1235. }
  1236. for (const auto& c : full.channels)
  1237. if (c.instrument == lynx::InstrumentKind::kepler)
  1238. {
  1239. ++keplerKind;
  1240. 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 } },
  1241. 44100.0, 11025);
  1242. double e = 0.0;
  1243. bool finite = ! y.empty();
  1244. for (auto v : y)
  1245. finite = finite && std::isfinite (v), e += (double) v * v;
  1246. CHECK (finite);
  1247. keplerSounding += e > 1e-6 ? 1 : 0;
  1248. }
  1249. const auto file = entry.path().filename().string();
  1250. for (const auto& c : full.channels)
  1251. {
  1252. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  1253. if (! isGms && ! isMorphine)
  1254. continue;
  1255. (isGms ? gmsKind : morphineKind)++;
  1256. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1257. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  1258. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  1259. std::vector<float> y;
  1260. if (isGms)
  1261. y = lynx::gms::renderReference (c.pluginState,
  1262. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  1263. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  1264. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1265. else
  1266. 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 } },
  1267. 44100.0, golden ? 100000 : 22050, c.samplePath);
  1268. double e = 0.0;
  1269. bool finite = ! y.empty();
  1270. for (auto v : y)
  1271. finite = finite && std::isfinite (v), e += (double) v * v;
  1272. CHECK (finite);
  1273. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  1274. if (golden && ! y.empty())
  1275. {
  1276. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  1277. const auto rms = std::sqrt (e / (double) y.size());
  1278. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1279. 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 } },
  1280. 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 } };
  1281. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1282. 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);
  1283. ++demoGoldens;
  1284. }
  1285. }
  1286. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1287. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1288. // renders of the installation's DLL for these states)
  1289. for (const auto& c : p.channels)
  1290. if (c.pluginName == "MiniSynth")
  1291. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1292. for (const auto& c : full.channels)
  1293. {
  1294. if (c.instrument != lynx::InstrumentKind::minisynth)
  1295. continue;
  1296. ++miniSynthKind;
  1297. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1298. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1299. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1300. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1301. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1302. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1303. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1304. double e = 0.0;
  1305. bool finite = ! y.empty();
  1306. for (auto v : y)
  1307. finite = finite && std::isfinite (v), e += (double) v * v;
  1308. CHECK (finite);
  1309. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1310. if (golden && ! y.empty())
  1311. {
  1312. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1313. const auto lead = c.name == "Main Lead 1";
  1314. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1315. 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 } },
  1316. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1317. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1318. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1319. ++miniSynthGoldens;
  1320. }
  1321. }
  1322. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1323. // samples of our round-44 model
  1324. for (const auto& c : p.channels)
  1325. if (c.pluginName == "PoiZone")
  1326. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1327. for (const auto& c : full.channels)
  1328. {
  1329. if (c.instrument != lynx::InstrumentKind::poizone)
  1330. continue;
  1331. ++poizoneKind;
  1332. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1333. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1334. const auto y = lynx::poizone::renderReference (c.pluginState,
  1335. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1336. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1337. 44100.0, golden ? 88200 : 22050);
  1338. double e = 0.0;
  1339. bool finite = ! y.empty();
  1340. for (auto v : y)
  1341. finite = finite && std::isfinite (v), e += (double) v * v;
  1342. CHECK (finite);
  1343. poizoneSounding += e > 1e-6 ? 1 : 0;
  1344. if (golden && ! y.empty())
  1345. {
  1346. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1347. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1348. 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 } };
  1349. for (const auto& g : at)
  1350. 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);
  1351. ++poizoneGoldens;
  1352. }
  1353. }
  1354. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1355. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1356. for (const auto& c : p.channels)
  1357. if (c.pluginName == "Toxic Biohazard")
  1358. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1359. for (const auto& c : full.channels)
  1360. {
  1361. if (c.instrument != lynx::InstrumentKind::toxic)
  1362. continue;
  1363. ++toxicKind;
  1364. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1365. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1366. const auto y = lynx::toxic::renderReference (
  1367. c.pluginState,
  1368. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1369. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1370. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1371. double e = 0.0;
  1372. bool finite = ! y.empty();
  1373. for (auto v : y)
  1374. finite = finite && std::isfinite (v), e += (double) v * v;
  1375. CHECK (finite);
  1376. toxicSounding += e > 1e-6 ? 1 : 0;
  1377. if (golden && ! y.empty())
  1378. {
  1379. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1380. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1381. 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 } };
  1382. for (const auto& g : at)
  1383. 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);
  1384. ++toxicGoldens;
  1385. }
  1386. }
  1387. for (const auto& c : full.channels)
  1388. {
  1389. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1390. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1391. {
  1392. ++harmlessCompWanted;
  1393. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1394. }
  1395. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1396. // a Slicex channel is its own instrument since round 190l (no sampler slices): its regions are the
  1397. // region records of its decks, counted here so the bar below still means "sliced channels import"
  1398. std::size_t slicexRegions = 0;
  1399. if (c.instrument == lynx::InstrumentKind::slicex)
  1400. if (const auto state = lynx::slicex::parse (c.pluginState))
  1401. for (const auto& deck : state->decks)
  1402. slicexRegions += deck.empty() ? 0 : deck.regions.size();
  1403. if (c.sampler.slices.size() >= 4 || slicexRegions >= 4)
  1404. ++slicedChannels;
  1405. if (c.sampler.slices.size() >= 4)
  1406. {
  1407. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1408. CHECK (c.sampler.slices.back() < 1.0f);
  1409. }
  1410. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1411. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos && c.instrument != lynx::InstrumentKind::fpc)
  1412. {
  1413. ++dwChannels;
  1414. dwZones += (int) c.zones.size();
  1415. }
  1416. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1417. if (c.instrument == lynx::InstrumentKind::fpc) // (round 190q: a kit on one channel; its pads that play)
  1418. padChannels += (int) lynx::fpcChannelPads (c).size();
  1419. }
  1420. fpcKits += hasFpc;
  1421. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1422. // carrier's Sytrus preset) read from the installation
  1423. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1424. for (const auto& insert : p.mixer)
  1425. for (const auto& slot : insert.slots)
  1426. {
  1427. maximusSlots += slot.plugin == "Maximus";
  1428. delay2Slots += slot.plugin == "Fruity Delay 2";
  1429. delay3Slots += slot.plugin == "Fruity Delay 3";
  1430. sgzSlots += slot.plugin == "Soundgoodizer";
  1431. vocodexSlots += slot.plugin == "Vocodex";
  1432. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1433. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1434. equoSlots += slot.plugin == "EQUO";
  1435. distructorSlots += slot.plugin == "Distructor";
  1436. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1437. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1438. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1439. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1440. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1441. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1442. flangusSlots += slot.plugin == "Fruity Flangus";
  1443. flangerSlots += slot.plugin == "Fruity Flanger";
  1444. phaserSlots += slot.plugin == "Fruity Phaser";
  1445. }
  1446. // (the slots and the nodes of the Patcher graphs, as when Patchers were flattened into slots)
  1447. std::vector<const lynx::EffectSlot*> allSlots;
  1448. std::function<void (const lynx::EffectSlot&)> collect = [&] (const lynx::EffectSlot& fx) {
  1449. allSlots.push_back (&fx);
  1450. for (const auto& n : fx.patcher.nodes)
  1451. if (! n.generator)
  1452. collect (n.effect);
  1453. };
  1454. for (const auto& t : full.mixer)
  1455. for (const auto& fx : t.effects)
  1456. collect (fx);
  1457. for (const auto* slotPointer : allSlots)
  1458. {
  1459. const auto& fx = *slotPointer;
  1460. if (fx.kind == lynx::EffectKind::maximus)
  1461. {
  1462. const auto blob = lynx::maximusfx::unpack (fx.state);
  1463. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1464. if (blob)
  1465. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1466. }
  1467. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1468. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1469. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1470. phaserExact += fx.kind == lynx::EffectKind::fruityPhaser;
  1471. if (fx.kind == lynx::EffectKind::vocodex)
  1472. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1473. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1474. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1475. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1476. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1477. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1478. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1479. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1480. if (fx.kind == lynx::EffectKind::distructor)
  1481. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1482. {
  1483. ++distructorExact;
  1484. for (const auto& m : d->modules)
  1485. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1486. }
  1487. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1488. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1489. || fx.kind == lynx::EffectKind::fruityFilter;
  1490. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1491. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1492. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1493. }
  1494. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1495. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1496. basicSlots - basicSlots0, basicExact - basicExact0);
  1497. for (const auto& pat : full.patterns)
  1498. for (const auto& n : pat.notes)
  1499. {
  1500. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1501. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos
  1502. || full.channels[(std::size_t) n.channel].instrument == lynx::InstrumentKind::fpc ? 1 : 0;
  1503. }
  1504. }
  1505. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1506. for (const auto& t : song.mixer)
  1507. for (const auto& fx : t.effects)
  1508. {
  1509. if (fx.kind == lynx::EffectKind::parametricEq)
  1510. {
  1511. ++peqSlots;
  1512. peqShaped += fx.params != peqDefaults;
  1513. for (double hz : { 50.0, 1000.0, 10000.0 })
  1514. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1515. }
  1516. if (fx.kind == lynx::EffectKind::reverb)
  1517. {
  1518. ++reverbs;
  1519. reverbsDryless += fx.params[4] < 0.01f;
  1520. reverbsLowCut += fx.params[5] > 0.0f;
  1521. }
  1522. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1523. {
  1524. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1525. ++reverbs;
  1526. reverbsDryless += r2.p[10] == 0;
  1527. reverbsLowCut += r2.p[0] >= 20;
  1528. }
  1529. }
  1530. for (const auto& t : song.mixer)
  1531. if (t.eq.active())
  1532. {
  1533. ++trackEqs;
  1534. for (std::size_t b = 0; b < 3; ++b)
  1535. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1536. }
  1537. for (const auto& a : song.arrangements)
  1538. for (const auto& m : a.markers)
  1539. {
  1540. ++markers;
  1541. signatures += m.isSignature();
  1542. CHECK (m.tick >= 0);
  1543. }
  1544. // (round 168g: the patterns' own markers are no longer counted as the Playlist's)
  1545. for (const auto& p : song.patterns)
  1546. for (const auto& m : p.markers)
  1547. {
  1548. ++markers;
  1549. signatures += m.isSignature();
  1550. CHECK (m.tick >= 0);
  1551. }
  1552. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1553. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1554. if (plugin != "Fruity Wrapper")
  1555. return;
  1556. ++wrapped;
  1557. lynx::ForeignPlugin f;
  1558. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1559. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1560. {
  1561. // VST2 state (REA_rodada89 §2): every one decodes, and writes back to the same bytes — except the
  1562. // wrapped Fruity effects of the FL 11 render tests (their mixer slots are read since round 80):
  1563. // those decode but do not write back byte-identical (not confirmed why)
  1564. ++wrappedVst2;
  1565. lynx::FlVst2State st;
  1566. if (lynx::parseFlVst2State (f.state, st) && st.version == -9)
  1567. {
  1568. ++wrappedVst2State;
  1569. wrappedVst2Old += lynx::writeFlVst2State (st) != f.state;
  1570. }
  1571. }
  1572. };
  1573. for (const auto& c : p.channels)
  1574. countWrapper (c.pluginName, c.pluginState);
  1575. for (const auto& insert : p.mixer)
  1576. for (const auto& slot : insert.slots)
  1577. countWrapper (slot.plugin, slot.state);
  1578. // Harmless: every state in the demos becomes a synth, with finite values
  1579. for (const auto& c : p.channels)
  1580. if (c.pluginName == "Harmless")
  1581. {
  1582. ++harmless;
  1583. lynx::SynthSettings hs;
  1584. float g = 1.0f;
  1585. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1586. {
  1587. ++harmlessDecoded;
  1588. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1589. }
  1590. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1591. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1592. }
  1593. for (const auto& c : song.channels)
  1594. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1595. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1596. for (const auto& c : p.channels)
  1597. if (c.pluginName == "Harmor")
  1598. {
  1599. ++harmor;
  1600. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1601. }
  1602. for (const auto& c : song.channels)
  1603. if (c.instrument == lynx::InstrumentKind::harmor)
  1604. {
  1605. ++harmorKind;
  1606. 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);
  1607. double e = 0.0;
  1608. bool finite = ! y.empty();
  1609. for (auto v : y)
  1610. finite = finite && std::isfinite (v), e += (double) v * v;
  1611. CHECK (finite);
  1612. harmorSounding += e > 1e-6 ? 1 : 0;
  1613. }
  1614. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1615. for (const auto& c : p.channels)
  1616. if (c.pluginName == "Ogun")
  1617. {
  1618. ++ogun;
  1619. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1620. }
  1621. for (const auto& c : song.channels)
  1622. if (c.instrument == lynx::InstrumentKind::ogun)
  1623. {
  1624. ++ogunKind;
  1625. 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 },
  1626. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1627. double e = 0.0;
  1628. bool finite = ! y.empty();
  1629. for (auto v : y)
  1630. finite = finite && std::isfinite (v), e += (double) v * v;
  1631. CHECK (finite);
  1632. ogunSounding += e > 1e-6 ? 1 : 0;
  1633. }
  1634. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1635. // Magic Dust stage gets Autogun\Data\SG.dat
  1636. {
  1637. bool any = false;
  1638. for (const auto& c : p.channels)
  1639. if (c.pluginName == "Autogun")
  1640. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1641. if (any)
  1642. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1643. if (c.instrument == lynx::InstrumentKind::autogun)
  1644. {
  1645. ++autogunKind;
  1646. lynx::ogun::RefOptions opt;
  1647. opt.autogun = true;
  1648. if (! c.samplePath.empty())
  1649. {
  1650. auto local = c.samplePath;
  1651. std::replace (local.begin(), local.end(), '\\', '/');
  1652. std::ifstream sg (local, std::ios::binary);
  1653. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1654. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1655. }
  1656. 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 } },
  1657. 44100.0, 11025, opt);
  1658. double e = 0.0;
  1659. bool finite = ! y.empty();
  1660. for (auto v : y)
  1661. finite = finite && std::isfinite (v), e += (double) v * v;
  1662. CHECK (finite);
  1663. autogunSounding += e > 1e-6 ? 1 : 0;
  1664. }
  1665. }
  1666. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1667. for (const auto& c : p.channels)
  1668. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1669. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1670. for (const auto& c : song.channels)
  1671. if (c.instrument == lynx::InstrumentKind::dx10)
  1672. {
  1673. ++dx10Kind;
  1674. 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 } },
  1675. 44100.0, 11025);
  1676. double e = 0.0;
  1677. bool finite = ! y.empty();
  1678. for (auto v : y)
  1679. finite = finite && std::isfinite (v), e += (double) v * v;
  1680. CHECK (finite);
  1681. dx10Sounding += e > 1e-6 ? 1 : 0;
  1682. }
  1683. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1684. for (const auto& c : song.channels)
  1685. if (c.instrument == lynx::InstrumentKind::threeosc)
  1686. {
  1687. ++threeOscKind;
  1688. 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 } },
  1689. 44100.0, 11025);
  1690. double e = 0.0;
  1691. bool finite = ! y.empty();
  1692. for (auto v : y)
  1693. finite = finite && std::isfinite (v), e += (double) v * v;
  1694. CHECK (finite);
  1695. threeOscSounding += e > 1e-6 ? 1 : 0;
  1696. }
  1697. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1698. for (const auto& c : p.channels)
  1699. if (c.pluginName == "Sytrus")
  1700. {
  1701. ++sytrus;
  1702. lynx::SynthSettings ss;
  1703. float g = 1.0f;
  1704. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1705. if (lynx::sytrus::stateUsable (c.pluginState))
  1706. {
  1707. ++sytrusEngine;
  1708. if (sytrusRendered < 24)
  1709. {
  1710. ++sytrusRendered;
  1711. lynx::sytrus::Engine e;
  1712. e.prepare (44100.0, true, 512);
  1713. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1714. lynx::sytrus::NoteParams note;
  1715. e.noteOn (note);
  1716. std::vector<float> l (8192), r (8192);
  1717. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1718. bool finite = true;
  1719. double energy = 0.0;
  1720. for (std::size_t k = 0; k < l.size(); ++k)
  1721. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1722. CHECK (finite);
  1723. sytrusSounding += energy > 1e-6;
  1724. }
  1725. }
  1726. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1727. {
  1728. ++sytrusDecoded;
  1729. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1730. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1731. for (const auto& o : ss.osc)
  1732. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1733. }
  1734. }
  1735. // round 12 generators
  1736. for (const auto& c : p.channels)
  1737. {
  1738. lynx::SynthSettings gs;
  1739. float g = 1.0f;
  1740. int decoded = -1;
  1741. if (c.pluginName == "Harmor")
  1742. {
  1743. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1744. if (decoded)
  1745. {
  1746. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1747. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1748. static int resonant = 0, strong = 0, total = 0;
  1749. resonant += gs.resonance > 0.0f;
  1750. strong += gs.resonance > 0.9f;
  1751. if (++total % 50 == 0)
  1752. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1753. }
  1754. static int saw = 0, square = 0, other = 0, total = 0;
  1755. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1756. {
  1757. auto isSaw = true, isSquare = true;
  1758. for (int n = 1; n <= 16; ++n)
  1759. {
  1760. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1761. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1762. }
  1763. ++(isSaw ? saw : isSquare ? square : other);
  1764. if (++total % 20 == 0 || total == 100)
  1765. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1766. }
  1767. }
  1768. else if (c.pluginName == "GMS")
  1769. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1770. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1771. {
  1772. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1773. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1774. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1775. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1776. {
  1777. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1778. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1779. auto match = true;
  1780. for (std::size_t k = 0; k < 3; ++k)
  1781. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1782. ogunTrumpet += match ? 1 : 0;
  1783. }
  1784. }
  1785. else if (c.pluginName == "Sakura")
  1786. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1787. else if (c.pluginName == "Kepler")
  1788. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1789. else if (c.pluginName == "Wave Traveller")
  1790. decoded = lynx::waveTravellerStateUsable (c.pluginState) ? 2 : 0;
  1791. else if (c.pluginName == "PoiZone")
  1792. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1793. else if (c.pluginName == "BassDrum")
  1794. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1795. else if (c.pluginName == "Toxic Biohazard")
  1796. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1797. else if (c.pluginName == "Morphine")
  1798. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1799. else if (c.pluginName == "BooBass")
  1800. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1801. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1802. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1803. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1804. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1805. else if (c.pluginName == "FLEX")
  1806. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1807. else if (c.pluginName == "Fruity DrumSynth Live")
  1808. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1809. if (decoded < 0)
  1810. continue;
  1811. auto& counter = generators[c.pluginName];
  1812. ++counter.first;
  1813. counter.second += decoded != 0;
  1814. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1815. {
  1816. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1817. 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));
  1818. for (const auto& o : gs.osc)
  1819. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1820. }
  1821. }
  1822. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1823. for (const auto& c : p.channels)
  1824. if (c.pluginName == "3x Osc")
  1825. {
  1826. ++threeOsc;
  1827. lynx::SynthSettings s;
  1828. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1829. {
  1830. ++threeOscDecoded;
  1831. // with AM, osc 3 is the modulation depth and is left out of the sum
  1832. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1833. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1834. for (const auto& o : s.osc)
  1835. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1836. }
  1837. }
  1838. for (auto& pat : p.patterns)
  1839. if (! pat.notes.empty())
  1840. {
  1841. ++withNotes;
  1842. break;
  1843. }
  1844. for (auto& a : p.arrangements)
  1845. for (auto& item : a.items)
  1846. CHECK (item.track >= 0 && item.track < 500);
  1847. for (auto& clip : song.playlist)
  1848. {
  1849. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1850. CHECK (clip.length > 0 && clip.offset >= 0);
  1851. if (clip.kind == lynx::ClipKind::automation)
  1852. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1853. }
  1854. for (auto& a : song.automations)
  1855. {
  1856. ++automations;
  1857. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1858. CHECK (a.isLive() || ! a.points.empty());
  1859. for (std::size_t i = 1; i < a.points.size(); ++i)
  1860. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1861. }
  1862. // imported routing has no cycles and valid destinations
  1863. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1864. for (auto& t : song.mixer)
  1865. for (auto& s : t.sends)
  1866. {
  1867. ++sends;
  1868. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1869. }
  1870. if (! song.playlist.empty())
  1871. ++withPlaylist;
  1872. }
  1873. catch (const flp::Error& e)
  1874. {
  1875. ++failures;
  1876. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1877. }
  1878. }
  1879. CHECK (threeOscDecoded == threeOsc);
  1880. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1881. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1882. sytrusSounding, sytrusRendered);
  1883. CHECK (sytrusEngine == sytrus);
  1884. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1885. for (const auto& [name, counter] : generators)
  1886. {
  1887. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1888. CHECK (counter.second == counter.first);
  1889. }
  1890. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1891. CHECK (ogunTrumpet >= 1);
  1892. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1893. {
  1894. int vocodexes = 0, vocodexSides = 0;
  1895. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1896. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1897. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1898. if (entry.path().extension() == ".flp")
  1899. {
  1900. std::ifstream in (entry.path(), std::ios::binary);
  1901. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1902. try
  1903. {
  1904. const auto project = flp::parseProject (file);
  1905. for (const auto& insert : project.mixer)
  1906. for (const auto& slot : insert.slots)
  1907. if (slot.plugin == "Gross Beat")
  1908. {
  1909. // FL's own engine (REA_rodada53): every demo state is read
  1910. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1911. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1912. ++gates;
  1913. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1914. ++shaped;
  1915. }
  1916. else if (slot.plugin == "Fruity Love Philter")
  1917. {
  1918. ++lovePhilters;
  1919. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1920. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1921. {
  1922. ++lovePhiltersConverted;
  1923. CHECK (! conv->slots.empty());
  1924. for (const auto& fx : conv->slots)
  1925. for (auto v : fx.params)
  1926. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1927. for (const auto& fx : conv->slots)
  1928. for (const auto& link : fx.links)
  1929. {
  1930. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1931. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1932. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1933. }
  1934. for (const auto& link : conv->links)
  1935. {
  1936. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1937. CHECK (link.curve.size() == 33);
  1938. for (auto v : link.curve)
  1939. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1940. }
  1941. }
  1942. }
  1943. else if (slot.plugin == "Vocodex")
  1944. {
  1945. const auto v = lynx::vocodexEffect (slot.state);
  1946. CHECK (v.has_value());
  1947. vocodexes += v.has_value();
  1948. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1949. }
  1950. else if (slot.plugin == "Effector")
  1951. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1952. else if (slot.plugin == "Patcher")
  1953. {
  1954. const auto graph = lynx::parsePatcher (slot.state);
  1955. CHECK (graph.has_value());
  1956. ++patchers;
  1957. if (graph && ! graph->serialChain.empty())
  1958. ++serial;
  1959. if (graph && graph->serialChain.empty() && graph->audio)
  1960. ++parallel;
  1961. }
  1962. for (const auto& c : project.channels)
  1963. if (c.pluginName == "DirectWave")
  1964. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1965. for (const auto& wav : program->embeddedWav)
  1966. if (! wav.empty())
  1967. {
  1968. ++embeddedZones;
  1969. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1970. }
  1971. for (const auto& c : project.channels)
  1972. if (c.pluginName == "Patcher")
  1973. {
  1974. const auto graph = lynx::parsePatcher (c.pluginState);
  1975. CHECK (graph.has_value());
  1976. generatorPatchers += graph && graph->mainGenerator >= 0;
  1977. }
  1978. }
  1979. catch (const flp::Error&) {}
  1980. }
  1981. CHECK (gates >= 30 && shaped == gates);
  1982. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1983. lovePhilters, lovePhiltersRead);
  1984. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1985. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1986. CHECK (vocodexes >= 9);
  1987. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1988. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1989. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1990. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1991. CHECK (embeddedZones == 12);
  1992. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1993. parallel, generatorPatchers);
  1994. CHECK (gates == 0 || shaped > 0);
  1995. }
  1996. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1997. {
  1998. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1999. if (std::filesystem::exists (path))
  2000. {
  2001. std::ifstream in (path, std::ios::binary);
  2002. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2003. const auto song = lynx::importFlp (flp::parseProject (file));
  2004. const lynx::ChannelDef* controller = nullptr;
  2005. for (const auto& c : song.channels)
  2006. if (c.instrument == lynx::InstrumentKind::controller)
  2007. controller = &c;
  2008. CHECK (controller != nullptr);
  2009. if (controller != nullptr)
  2010. {
  2011. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  2012. inst->prepare (48000.0);
  2013. inst->configure (*controller, song.bpm);
  2014. std::vector<float> l (480), r (480);
  2015. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  2016. inst->noteOn (60, 1.0f);
  2017. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  2018. const auto ducked = inst->controlOutput (0);
  2019. for (int i = 0; i < 100; ++i) // +1 s
  2020. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  2021. const auto recovered = inst->controlOutput (0);
  2022. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  2023. CHECK (ducked < 0.3f && recovered > 0.9f);
  2024. }
  2025. }
  2026. }
  2027. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  2028. {
  2029. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  2030. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  2031. {
  2032. if (entry.path().extension() != ".flp")
  2033. continue;
  2034. std::ifstream in (entry.path(), std::ios::binary);
  2035. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2036. try
  2037. {
  2038. const auto project = flp::parseProject (file);
  2039. for (const auto& c : project.channels)
  2040. if (! c.pluginState.empty())
  2041. samples.emplace (c.pluginName, c.pluginState);
  2042. for (const auto& insert : project.mixer)
  2043. for (const auto& slot : insert.slots)
  2044. if (! slot.state.empty())
  2045. samples.emplace (slot.plugin, slot.state);
  2046. }
  2047. catch (const flp::Error&) {}
  2048. }
  2049. std::uint32_t seed = 12345;
  2050. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  2051. int mutations = 0;
  2052. for (const auto& [name, original] : samples)
  2053. for (int round = 0; round < 40; ++round)
  2054. {
  2055. auto blob = original;
  2056. if (round % 4 == 3 && ! blob.empty())
  2057. blob.resize (rnd() % blob.size()); // truncated
  2058. else
  2059. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  2060. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  2061. ++mutations;
  2062. lynx::SynthSettings synth;
  2063. float gain = 1.0f;
  2064. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  2065. lynx::synthFromHarmor (blob, synth, gain);
  2066. lynx::synthFromGms (blob, 120.0, synth, gain);
  2067. lynx::synthFromOgun (blob, synth, gain);
  2068. lynx::synthFromMorphine (blob, synth, gain);
  2069. lynx::synthFromKepler (blob, synth, gain);
  2070. lynx::synthFromPoiZone (blob, synth, gain);
  2071. lynx::synthFromToxic (blob, synth, gain);
  2072. lynx::synthFromBassDrum (blob, synth, gain);
  2073. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  2074. lynx::harmlessParams (blob);
  2075. if (lynx::harmorStateUsable (blob))
  2076. lynx::harmorFallbacks (blob);
  2077. lynx::parseEnvelopeController (blob);
  2078. lynx::parseKeyboardController (blob);
  2079. lynx::parseLovePhilter (blob);
  2080. lynx::parsePatcher (blob);
  2081. lynx::effectFromFl (name, blob);
  2082. lynx::fpcPads (blob);
  2083. lynx::directWaveProgram (blob);
  2084. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  2085. }
  2086. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  2087. }
  2088. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  2089. {
  2090. int files = 0, decoded = 0, audible = 0;
  2091. double seconds = 0.0;
  2092. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  2093. if (entry.path().extension() == ".wv")
  2094. {
  2095. std::ifstream in (entry.path(), std::ios::binary);
  2096. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2097. ++files;
  2098. lynx::DecodedAudio audio;
  2099. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  2100. continue;
  2101. ++decoded;
  2102. float peak = 0.0f;
  2103. bool finite = true;
  2104. for (const auto& ch : audio.channels)
  2105. for (auto v : ch)
  2106. {
  2107. finite = finite && std::isfinite (v);
  2108. peak = std::max (peak, std::abs (v));
  2109. }
  2110. CHECK (finite && peak <= 4.0f);
  2111. audible += peak > 1e-4f;
  2112. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  2113. }
  2114. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  2115. CHECK (decoded == files);
  2116. }
  2117. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  2118. int gmsPresets = 0, gmsDecoded = 0;
  2119. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  2120. if (std::filesystem::exists (gmsFolder))
  2121. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  2122. if (entry.path().extension() == ".gmsynth")
  2123. {
  2124. std::ifstream in (entry.path(), std::ios::binary);
  2125. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2126. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  2127. const auto n = (std::uint32_t) file.size();
  2128. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  2129. blob.insert (blob.end(), file.begin(), file.end());
  2130. lynx::SynthSettings gs;
  2131. float g = 1.0f;
  2132. ++gmsPresets;
  2133. if (lynx::synthFromGms (blob, 120.0, gs, g))
  2134. {
  2135. ++gmsDecoded;
  2136. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  2137. }
  2138. }
  2139. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  2140. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  2141. int presetFiles = 0, presetsRead = 0;
  2142. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  2143. "PoiZone", "Toxic Biohazard", "BassDrum" })
  2144. {
  2145. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  2146. if (! std::filesystem::exists (folder))
  2147. continue;
  2148. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  2149. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  2150. {
  2151. std::ifstream in (entry.path(), std::ios::binary);
  2152. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  2153. ++presetFiles;
  2154. presetsRead += lynx::synthFromFlPreset (file).has_value();
  2155. }
  2156. }
  2157. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  2158. CHECK (presetsRead >= presetFiles * 95 / 100);
  2159. CHECK (gmsDecoded == gmsPresets);
  2160. CHECK (sytrusDecoded == sytrus);
  2161. CHECK (harmlessDecoded == harmless && harmless > 100);
  2162. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  2163. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  2164. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  2165. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  2166. 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",
  2167. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  2168. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  2169. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  2170. miniSynthSounding, miniSynthGoldens);
  2171. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  2172. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  2173. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  2174. poizoneSounding, poizoneGoldens);
  2175. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  2176. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  2177. toxicSounding, toxicGoldens);
  2178. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  2179. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  2180. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  2181. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  2182. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  2183. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  2184. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  2185. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  2186. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  2187. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  2188. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  2189. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  2190. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  2191. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  2192. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  2193. // option; the other Data folders add states with options not decoded
  2194. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  2195. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  2196. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  2197. clipperExact, clipperSlots);
  2198. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  2199. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  2200. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  2201. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  2202. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  2203. // (since round 141 a Patcher stays one slot: "Bombs Away" insert 69 keeps its last PanOMatic)
  2204. CHECK (basicExact >= basicSlots && basicSlots >= 270); // REA_rodada58
  2205. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  2206. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  2207. distructorExact, distructorSlots, distructorImpulses);
  2208. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  2209. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  2210. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  2211. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  2212. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  2213. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  2214. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  2215. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  2216. delay3Exact, delay3Slots);
  2217. // (FL 11 files' slots are read since round 80; Patcher graphs can add Delay 2 nodes of their own)
  2218. CHECK (delay2Exact >= delay2Slots && delay2Slots >= 60 && delay3Exact >= delay3Slots && delay3Slots >= 100); // REA_rodada52
  2219. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  2220. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  2221. 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,
  2222. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  2223. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  2224. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  2225. std::printf (" Fruity Phaser: %d of %d slots on the exact engine\n", phaserExact, phaserSlots);
  2226. CHECK (phaserExact >= phaserSlots && phaserSlots >= 3); // REA_rodada105_phaser
  2227. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  2228. int presets = 0, presetsDecoded = 0;
  2229. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  2230. if (std::filesystem::is_directory (presetDir))
  2231. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  2232. if (entry.path().extension() == ".fst")
  2233. {
  2234. ++presets;
  2235. std::ifstream in (entry.path(), std::ios::binary);
  2236. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2237. if (auto synth = lynx::synthFromFlPreset (bytes))
  2238. {
  2239. ++presetsDecoded;
  2240. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  2241. }
  2242. }
  2243. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  2244. CHECK (presetsDecoded == presets);
  2245. // FL effect presets that become built-in effects
  2246. int fxPresets = 0, fxDecoded = 0;
  2247. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  2248. lynx::EffectKind::utility })
  2249. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  2250. {
  2251. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  2252. if (! std::filesystem::is_directory (dir))
  2253. continue;
  2254. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  2255. if (entry.path().extension() == ".fst")
  2256. {
  2257. ++fxPresets;
  2258. std::ifstream in (entry.path(), std::ios::binary);
  2259. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2260. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  2261. }
  2262. }
  2263. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  2264. CHECK (fxDecoded == fxPresets);
  2265. CHECK (wrappedParsed == wrapped);
  2266. CHECK (wrappedVst2State == wrappedVst2 && wrappedVst2 >= 23 && wrappedVst2Old <= 13);
  2267. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded (%d not written back byte-identical)\n", wrapped,
  2268. wrappedParsed, wrappedVst2, wrappedVst2State, wrappedVst2Old);
  2269. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  2270. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  2271. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  2272. padChannels, padNotes);
  2273. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  2274. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  2275. CHECK (dwExact >= 57);
  2276. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  2277. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  2278. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  2279. CHECK (flKeysChannels >= 8);
  2280. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  2281. CHECK (markers > 300 && signatures > 60);
  2282. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  2283. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  2284. // with its saved settings, in FL too
  2285. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  2286. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  2287. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  2288. CHECK (files > 0);
  2289. CHECK (automations > 0 && sends > 0);
  2290. }
  2291. // ---------------------------------------------------------------------------
  2292. // Sequencer
  2293. TEST (pattern_toggle_step_and_length)
  2294. {
  2295. lynx::Pattern p;
  2296. CHECK (! p.hasStep (0, 4));
  2297. p.toggleStep (0, 4);
  2298. CHECK (p.hasStep (0, 4));
  2299. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  2300. CHECK (p.effectiveLength (lynx::Meter (4, 4)) == 4 * lynx::ppq); // one bar
  2301. p.toggleStep (1, 20); // second bar
  2302. CHECK (p.effectiveLength (lynx::Meter (4, 4)) == 8 * lynx::ppq);
  2303. p.toggleStep (0, 4);
  2304. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  2305. }
  2306. TEST (pattern_mute_removed_steps)
  2307. {
  2308. lynx::Pattern p;
  2309. p.toggleStep (0, 2);
  2310. p.notes[0].velocity = 0.3f;
  2311. p.toggleStep (0, 2, true); // off: muted, kept
  2312. CHECK (! p.hasStep (0, 2) && p.notes.size() == 1 && p.notes[0].muted);
  2313. p.toggleStep (0, 2, true); // on again: the same note back
  2314. CHECK (p.hasStep (0, 2) && p.notes.size() == 1 && ! p.notes[0].muted && p.notes[0].velocity == 0.3f);
  2315. p.toggleStep (0, 2, true);
  2316. // option off (FL's SetStep, round 169f): turning the muted step on adds a new note and leaves the muted one alone...
  2317. p.toggleStep (0, 2);
  2318. CHECK (p.hasStep (0, 2) && p.notes.size() == 2 && p.notes[0].muted != p.notes[1].muted);
  2319. p.toggleStep (0, 2); // ...and turning it off deletes every note of the step, the muted one too
  2320. CHECK (p.notes.empty());
  2321. // option on with two notes on the step: both muted, both back
  2322. p.toggleStep (0, 3);
  2323. p.notes.push_back (p.notes[0]);
  2324. p.notes[1].key = 64;
  2325. p.toggleStep (0, 3, true);
  2326. CHECK (! p.hasStep (0, 3) && p.notes.size() == 2 && p.notes[0].muted && p.notes[1].muted);
  2327. p.toggleStep (0, 3, true);
  2328. CHECK (p.hasStep (0, 3) && p.notes.size() == 2 && ! p.notes[0].muted && ! p.notes[1].muted);
  2329. // another channel's note on the same step is never touched
  2330. p.toggleStep (1, 3);
  2331. p.toggleStep (0, 3, true);
  2332. CHECK (p.hasStep (1, 3) && ! p.hasStep (0, 3));
  2333. }
  2334. TEST (scheduler_places_events_at_exact_samples)
  2335. {
  2336. lynx::Pattern p;
  2337. p.toggleStep (0, 0);
  2338. p.toggleStep (0, 4); // tick 960 = 1 beat
  2339. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  2340. const auto tps = lynx::ticksPerSample (bpm, sr);
  2341. lynx::EventBuffer ev;
  2342. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 23900.0 * tps, 512, tps, ev);
  2343. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  2344. int ons = 0;
  2345. for (auto& e : ev)
  2346. if (e.type == lynx::NoteEvent::Type::on)
  2347. {
  2348. ++ons;
  2349. CHECK (e.sampleOffset == 100);
  2350. }
  2351. CHECK (ons == 1);
  2352. }
  2353. TEST (scheduler_wraps_the_loop)
  2354. {
  2355. lynx::Pattern p;
  2356. p.toggleStep (2, 0);
  2357. const auto len = p.effectiveLength (lynx::Meter (4, 4)); // 3840 ticks
  2358. const double sr = 48000.0;
  2359. const auto tps = lynx::ticksPerSample (120.0, sr);
  2360. lynx::EventBuffer ev;
  2361. // block that crosses the loop end: starts 10 samples before
  2362. const auto start = (double) len - 10.0 * tps;
  2363. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2364. bool found = false;
  2365. for (auto& e : ev)
  2366. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2367. {
  2368. found = true;
  2369. CHECK (e.sampleOffset == 10);
  2370. }
  2371. CHECK (found);
  2372. }
  2373. TEST (scheduler_off_before_on_at_same_sample)
  2374. {
  2375. lynx::Pattern p;
  2376. lynx::Note a;
  2377. a.start = 0; a.length = 240; a.channel = 0;
  2378. lynx::Note b = a;
  2379. b.start = 240;
  2380. p.notes = { a, b };
  2381. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2382. lynx::EventBuffer ev;
  2383. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 200.0, 4000, tps, ev);
  2384. CHECK (ev.size() >= 2);
  2385. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2386. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2387. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2388. }
  2389. TEST (snapshot_exchange_hands_over_and_recycles)
  2390. {
  2391. struct Counted
  2392. {
  2393. int value;
  2394. int* alive;
  2395. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2396. ~Counted() { --*alive; }
  2397. };
  2398. int alive = 0;
  2399. {
  2400. lynx::SnapshotExchange<Counted> x;
  2401. CHECK (x.acquire() == nullptr);
  2402. x.publish (std::make_unique<Counted> (1, &alive));
  2403. CHECK (x.acquire()->value == 1);
  2404. x.publish (std::make_unique<Counted> (2, &alive));
  2405. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2406. CHECK (alive == 2);
  2407. CHECK (x.acquire()->value == 3);
  2408. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2409. x.collectGarbage();
  2410. CHECK (alive == 1);
  2411. // without collection, the return slots fill up and the audio side holds on to the current one
  2412. for (int i = 0; i < 20; ++i)
  2413. {
  2414. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2415. CHECK (x.acquire() != nullptr);
  2416. }
  2417. }
  2418. CHECK (alive == 0);
  2419. }
  2420. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2421. {
  2422. auto song = lynx::Song::makeDefault();
  2423. song.bpm = 120.0;
  2424. const double sr = 48000.0;
  2425. auto state = lynx::RackState::build (song, nullptr, sr);
  2426. lynx::RackRenderer r;
  2427. r.prepare (sr);
  2428. std::vector<float> l (512), rr (512);
  2429. double tick = 0.0;
  2430. float peak = 0.0f;
  2431. int kicks = 0;
  2432. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2433. {
  2434. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2435. for (auto& e : r.lastEvents())
  2436. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2437. ++kicks;
  2438. for (int i = 0; i < 512; ++i)
  2439. {
  2440. CHECK (std::isfinite (l[(std::size_t) i]));
  2441. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2442. }
  2443. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2444. }
  2445. CHECK (peak > 0.1f && peak < 4.0f);
  2446. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2447. // editing the song keeps the instruments of channels whose type didn't change
  2448. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2449. auto next = lynx::RackState::build (song, state.get(), sr);
  2450. CHECK (next->instruments[0] == state->instruments[0]);
  2451. CHECK (next->instruments[4] != state->instruments[4]);
  2452. // stopping silences the synth and triggers no new notes
  2453. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2454. CHECK (r.lastEvents().size() == 0);
  2455. }
  2456. TEST (flp_import_maps_channels_and_rescales)
  2457. {
  2458. flp::Project p;
  2459. p.header.ppq = 96;
  2460. p.tempo = 150.0;
  2461. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2462. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2463. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2464. flp::Pattern pat;
  2465. pat.index = 1;
  2466. flp::Note n;
  2467. n.position = 96;
  2468. n.length = 0;
  2469. n.channel = 2;
  2470. n.key = 72;
  2471. n.velocity = 64;
  2472. pat.notes.push_back (n);
  2473. p.patterns.push_back (pat);
  2474. const auto s = lynx::importFlp (p);
  2475. CHECK (s.bpm == 150.0);
  2476. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2477. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2478. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2479. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2480. CHECK (s.patterns.size() == 1);
  2481. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2482. CHECK (s.patterns[0].notes[0].channel == 2);
  2483. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2484. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2485. }
  2486. TEST (live_queue_is_fifo_and_bounded)
  2487. {
  2488. lynx::LiveEventQueue q;
  2489. lynx::NoteEvent e;
  2490. CHECK (! q.pop (e));
  2491. for (int i = 0; i < 10; ++i)
  2492. {
  2493. lynx::NoteEvent in;
  2494. in.key = i;
  2495. CHECK (q.push (in));
  2496. }
  2497. for (int i = 0; i < 10; ++i)
  2498. CHECK (q.pop (e) && e.key == i);
  2499. int pushed = 0;
  2500. while (q.push (e))
  2501. ++pushed;
  2502. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2503. }
  2504. // Whether this CPU's RCPPS (the ~12-bit reciprocal estimate, whose table is implementation-defined: Intel and AMD
  2505. // 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
  2506. // every float mantissa in [1, 2) (the estimate does not depend on the exponent in the normal range)
  2507. static bool rcpLikeReferenceCpu()
  2508. {
  2509. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  2510. std::uint64_t h = 1469598103934665603ull;
  2511. for (std::uint32_t m = 0; m < (1u << 23); m += 4)
  2512. {
  2513. alignas (16) float f[4];
  2514. for (std::uint32_t j = 0; j < 4; ++j)
  2515. {
  2516. const std::uint32_t bits = 0x3f800000u | (m + j);
  2517. std::memcpy (&f[j], &bits, 4);
  2518. }
  2519. _mm_store_ps (f, _mm_rcp_ps (_mm_load_ps (f)));
  2520. for (std::uint32_t j = 0; j < 4; ++j)
  2521. {
  2522. std::uint32_t bits;
  2523. std::memcpy (&bits, &f[j], 4);
  2524. h = (h ^ bits) * 1099511628211ull;
  2525. }
  2526. }
  2527. return h == 0xb5de670652cb0383ull;
  2528. #else
  2529. return false;
  2530. #endif
  2531. }
  2532. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2533. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2534. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2535. // 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
  2536. // required where RCPPS matches the capture machine's; elsewhere (Intel CPUs) within a few float ulps.
  2537. TEST (fl_sampler_interpolators_match_fl)
  2538. {
  2539. std::vector<float> l, r;
  2540. std::uint32_t x = 12345;
  2541. for (int k = 0; k < 2 * 144; ++k)
  2542. {
  2543. x = x * 1103515245u + 12345u;
  2544. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2545. (k % 2 == 0 ? l : r).push_back (v);
  2546. }
  2547. const int pad = 40;
  2548. 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 } };
  2549. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2550. const Case cases[] = {
  2551. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2552. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2553. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2554. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2555. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2556. };
  2557. const lynx::flvp::X87Extended precision;
  2558. const bool exactRcp = rcpLikeReferenceCpu();
  2559. for (const auto& c : cases)
  2560. {
  2561. lynx::flrs::Kernel k;
  2562. k.setQuality (c.quality);
  2563. k.setSpeed (c.speed);
  2564. int same = 0;
  2565. for (int p = 0; p < 8; ++p)
  2566. {
  2567. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2568. float a, b;
  2569. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2570. std::uint32_t ba, bb;
  2571. std::memcpy (&ba, &a, 4);
  2572. std::memcpy (&bb, &b, 4);
  2573. if (exactRcp || c.quality < 3) // (linear and 6-point use no reciprocal)
  2574. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2575. else
  2576. 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);
  2577. }
  2578. CHECK (same == 16);
  2579. }
  2580. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2581. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2582. }
  2583. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2584. // last frame
  2585. TEST (fl_sampler_copies_at_original_rate)
  2586. {
  2587. auto data = std::make_shared<lynx::SampleData>();
  2588. data->sampleRate = 48000.0;
  2589. for (int i = 0; i < 300; ++i)
  2590. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2591. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2592. sampler->prepare (48000.0);
  2593. sampler->noteOn (60, 1.0f);
  2594. std::vector<float> l (400), r (400);
  2595. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2596. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2597. int exact = 0;
  2598. for (int i = 0; i < 300; ++i)
  2599. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2600. CHECK (exact == 300);
  2601. CHECK (l[300] == 0.0f);
  2602. }
  2603. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2604. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2605. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2606. {
  2607. auto data = std::make_shared<lynx::SampleData>();
  2608. data->sampleRate = 48000.0;
  2609. data->left.assign (96000, 0.5f);
  2610. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2611. sampler->prepare (48000.0);
  2612. lynx::ChannelDef c;
  2613. c.instrument = lynx::InstrumentKind::sampler;
  2614. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2615. sampler->configure (c, 120.0);
  2616. sampler->noteOn (60, 1.0f);
  2617. std::vector<float> l (40000), r (40000);
  2618. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2619. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2620. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2621. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2622. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2623. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2624. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2625. sampler->noteOff (60);
  2626. std::fill (l.begin(), l.end(), 0.0f);
  2627. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2628. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2629. CHECK (l[39999] == 0.0f && sampler->idle());
  2630. }
  2631. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2632. // 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
  2633. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2634. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2635. TEST (fl_native_sampler_voice_matches_fl_renders)
  2636. {
  2637. auto noise = std::make_shared<lynx::SampleData>();
  2638. noise->sampleRate = 44100.0;
  2639. {
  2640. std::uint32_t seed = 1;
  2641. for (int i = 0; i < 26460; ++i)
  2642. {
  2643. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2644. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2645. }
  2646. }
  2647. struct Ev { int on, off; float vel, pan; };
  2648. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2649. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2650. lynx::ChannelDef c;
  2651. c.instrument = lynx::InstrumentKind::sampler;
  2652. c.sampler.flChannel = recs;
  2653. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2654. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2655. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2656. inst->prepare (44100.0);
  2657. lynx::setOfflineRendering (false);
  2658. inst->configure (c, 120.0);
  2659. inst->setSongPosition (0.0);
  2660. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2661. for (int i = 0; i < (int) notes.size(); ++i)
  2662. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2663. std::sort (events.begin(), events.end());
  2664. std::vector<float> l (120000), r (120000);
  2665. int at = 0;
  2666. for (const auto& [when, i] : events)
  2667. {
  2668. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2669. at = when;
  2670. if (i >= 0)
  2671. {
  2672. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2673. inst->noteOn (60, notes[(std::size_t) i].vel);
  2674. }
  2675. else
  2676. inst->noteOff (60);
  2677. }
  2678. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2679. return std::make_pair (l, r);
  2680. };
  2681. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2682. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2683. std::vector<std::int32_t> all = plain;
  2684. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2685. all.insert (all.end(), r->begin(), r->end());
  2686. return all;
  2687. };
  2688. 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 };
  2689. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2690. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2691. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2692. int bad = 0;
  2693. for (int n = 0; n < 3; ++n)
  2694. for (int k = 0; k < 8; ++k)
  2695. {
  2696. const auto p = (std::size_t) (44100 * n + at[k]);
  2697. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2698. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2699. }
  2700. return bad;
  2701. };
  2702. {
  2703. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2704. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2705. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2706. noise, chan);
  2707. 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 };
  2708. 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 };
  2709. CHECK (check (out, L, R) == 0);
  2710. }
  2711. {
  2712. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2713. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2714. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2715. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2716. { 90, 60, 1 },
  2717. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2718. noise, chan);
  2719. 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 };
  2720. 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 };
  2721. CHECK (check (out, L, R) == 0);
  2722. }
  2723. {
  2724. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2725. // tick k (notes on ticks 0, 100, 300)
  2726. auto vol = volPlain;
  2727. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2728. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2729. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2730. noise, chan);
  2731. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2732. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2733. int bad = 0;
  2734. for (int k = 0; k < 10; ++k)
  2735. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2736. CHECK (bad == 0);
  2737. }
  2738. {
  2739. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2740. // after the note-off (no volume envelope)
  2741. auto looped = std::make_shared<lynx::SampleData>();
  2742. looped->sampleRate = 44100.0;
  2743. for (int i = 0; i < 8820; ++i)
  2744. 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)));
  2745. looped->loopStart = 4410, looped->loopEnd = 8820;
  2746. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2747. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2748. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2749. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2750. int bad = 0;
  2751. for (int k = 0; k < 12; ++k)
  2752. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2753. CHECK (bad == 0);
  2754. }
  2755. {
  2756. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2757. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2758. auto dc = std::make_shared<lynx::SampleData>();
  2759. dc->sampleRate = 44100.0;
  2760. dc->left.assign (20000, 0.5f);
  2761. auto level = [&] (float vel, float pan, bool right) {
  2762. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2763. return (right ? out.second : out.first)[5000] / 0.5f;
  2764. };
  2765. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2766. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2767. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2768. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2769. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2770. }
  2771. }
  2772. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2773. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2774. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2775. {
  2776. auto level = [] (float v) {
  2777. auto d = std::make_shared<lynx::SampleData>();
  2778. d->sampleRate = 48000.0;
  2779. d->left.assign (4800, v);
  2780. return d;
  2781. };
  2782. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2783. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2784. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2785. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2786. inst->prepare (48000.0);
  2787. inst->configure (lynx::ChannelDef {}, 120.0);
  2788. auto play = [&] (float velocity) {
  2789. inst->allNotesOff();
  2790. std::vector<float> l (2000), r (2000);
  2791. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2792. std::fill (l.begin(), l.end(), 0.0f);
  2793. inst->noteOn (60, velocity);
  2794. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2795. return l[5] / velocity;
  2796. };
  2797. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2798. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2799. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2800. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2801. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2802. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2803. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2804. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2805. const std::int32_t version = 0x00060003, volume = 16000;
  2806. std::memcpy (state.data(), &version, 4);
  2807. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2808. state.push_back (1);
  2809. state.push_back (5);
  2810. for (char ch : std::string ("x.wav"))
  2811. state.push_back ((std::uint8_t) ch);
  2812. const auto sliced = lynx::slicexSample (state);
  2813. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2814. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2815. }
  2816. TEST (fl_keys_state_wti_and_loader)
  2817. {
  2818. using namespace lynx::flkeys;
  2819. // state: u32 1, the wavetable name, 15 int32 knobs
  2820. State st;
  2821. st.wavetable = "Grand Piano";
  2822. for (int i = 0; i < 15; ++i)
  2823. st.knobs[(std::size_t) i] = 10 * i - 50;
  2824. const auto bytes = writeState (st);
  2825. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2826. const auto back = parseState (bytes);
  2827. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2828. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2829. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2830. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2831. std::vector<std::string> names;
  2832. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2833. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2834. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2835. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2836. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2837. if (riff)
  2838. {
  2839. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2840. FileSamples a;
  2841. for (int i = 0; i < 10; ++i)
  2842. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2843. const auto decoded = FlKeysTable::decode (riff->encode());
  2844. CHECK (decoded.has_value());
  2845. const auto w = buildWavetable (*decoded, { a });
  2846. CHECK (w != nullptr);
  2847. if (w != nullptr)
  2848. {
  2849. 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; };
  2850. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2851. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2852. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2853. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2854. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2855. }
  2856. }
  2857. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2858. // loop as long as the keygroup's end, so the voice stops there
  2859. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2860. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2861. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2862. if (raw)
  2863. {
  2864. FileSamples x;
  2865. for (int i = 0; i < 10; ++i)
  2866. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2867. const auto w = buildWavetable (*raw, { x });
  2868. CHECK (w != nullptr);
  2869. if (w != nullptr)
  2870. {
  2871. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2872. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2873. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2874. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2875. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2876. State s2;
  2877. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2878. RefNote n;
  2879. n.key = 60;
  2880. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2881. double early = 0, late = 0;
  2882. for (int i = 0; i < 64; ++i)
  2883. early += std::fabs (out[(std::size_t) (2 * i)]);
  2884. for (int i = 2048; i < 4096; ++i)
  2885. late += std::fabs (out[(std::size_t) (2 * i)]);
  2886. CHECK (early > 0.0 && late == 0.0);
  2887. }
  2888. }
  2889. // instrument front-end without a wavetable: silent and idle
  2890. auto inst = makeFlKeysInstrument (nullptr);
  2891. inst->prepare (44100.0);
  2892. inst->noteOn (60, 0.78f);
  2893. std::vector<float> l (256), r (256);
  2894. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2895. CHECK (inst->idle() && l[100] == 0.0f);
  2896. }
  2897. TEST (ogg_vorbis_decodes_like_fl)
  2898. {
  2899. using namespace lynx::oggvorbis;
  2900. 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 };
  2901. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2902. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2903. Decoded none;
  2904. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2905. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2906. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2907. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2908. if (realFlpDir.empty())
  2909. return;
  2910. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2911. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2912. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2913. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2914. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2915. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2916. };
  2917. for (const auto& c : cases)
  2918. {
  2919. const auto path = root / c.file;
  2920. if (! std::filesystem::exists (path))
  2921. continue;
  2922. std::ifstream in (path, std::ios::binary);
  2923. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2924. CHECK (isOggWav (bytes.data(), bytes.size()));
  2925. Decoded d;
  2926. CHECK (decode (bytes.data(), bytes.size(), d));
  2927. if (d.channels == 0)
  2928. continue;
  2929. std::uint64_t h = 1469598103934665603ULL;
  2930. for (const auto& ch : d.data)
  2931. {
  2932. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2933. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2934. h = (h ^ p[i]) * 1099511628211ULL;
  2935. }
  2936. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2937. CHECK (c.channels == 0 || d.channels == c.channels);
  2938. CHECK (h == c.hash);
  2939. }
  2940. }
  2941. TEST (directwave_crt_math_and_state)
  2942. {
  2943. using namespace lynx::dw;
  2944. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2945. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2946. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2947. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2948. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2949. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2950. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2951. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2952. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2953. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2954. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2955. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2956. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2957. }
  2958. TEST (directwave_matches_fl_with_demo_programs)
  2959. {
  2960. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2961. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2962. if (realFlpDir.empty())
  2963. return;
  2964. using namespace lynx::dw;
  2965. auto readAll = [] (const std::filesystem::path& p) {
  2966. std::ifstream in (p, std::ios::binary);
  2967. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2968. };
  2969. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2970. const auto path = std::filesystem::path (realFlpDir) / file;
  2971. if (! std::filesystem::exists (path))
  2972. return std::vector<std::uint8_t>();
  2973. const auto p = flp::parseProject (readAll (path));
  2974. for (const auto& c : p.channels)
  2975. if (c.pluginName == "DirectWave" && c.name == channel)
  2976. return c.pluginState;
  2977. return std::vector<std::uint8_t>();
  2978. };
  2979. auto sampleValue = [] (int z, std::int64_t j) {
  2980. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2981. const auto w = ((j * j + z * 31) % 997) - 498;
  2982. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2983. };
  2984. std::vector<RefNote> notes;
  2985. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2986. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2987. notes.push_back (n);
  2988. };
  2989. 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);
  2990. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2991. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2992. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2993. const auto st = parseState (stateOf (file, channel));
  2994. if (! st)
  2995. return;
  2996. const auto* prog = playedProgram (*st);
  2997. CHECK (prog != nullptr);
  2998. if (prog == nullptr)
  2999. return;
  3000. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  3001. for (int z = 0; z < (int) prog->zones.size(); ++z)
  3002. {
  3003. auto s = std::make_shared<ZoneSamples>();
  3004. s->bits = 16;
  3005. s->channels = 1;
  3006. s->frames = prog->zones[(std::size_t) z].frames;
  3007. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  3008. for (std::int64_t j = 0; j < s->frames; ++j)
  3009. v[(std::size_t) j + 256] = sampleValue (z, j);
  3010. s->i16.push_back (std::move (v));
  3011. zs.push_back (s);
  3012. }
  3013. EngineOptions o;
  3014. o.velocityAsInitVol = (st->flags & 2) != 0;
  3015. o.noGainClamp = (st->flags & 1) != 0;
  3016. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  3017. std::uint64_t h = 14695981039346656037ull;
  3018. for (const float x : out)
  3019. {
  3020. std::uint32_t b;
  3021. std::memcpy (&b, &x, 4);
  3022. for (int k = 0; k < 4; ++k)
  3023. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  3024. }
  3025. CHECK (h == expected);
  3026. if (h != expected)
  3027. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  3028. };
  3029. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  3030. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  3031. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  3032. }
  3033. TEST (fl_keys_matches_fl_with_the_installation)
  3034. {
  3035. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  3036. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  3037. if (realFlpDir.empty())
  3038. return;
  3039. using namespace lynx::flkeys;
  3040. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  3041. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  3042. return;
  3043. auto readAll = [] (const std::filesystem::path& p) {
  3044. std::ifstream f (p, std::ios::binary);
  3045. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  3046. };
  3047. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  3048. FileSamples f;
  3049. std::uint16_t channels = 1;
  3050. for (std::size_t pos = 12; pos + 8 <= w.size();)
  3051. {
  3052. std::uint32_t size = 0;
  3053. std::memcpy (&size, w.data() + pos + 4, 4);
  3054. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  3055. std::memcpy (&channels, w.data() + pos + 10, 2);
  3056. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  3057. {
  3058. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  3059. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  3060. f.channels = channels;
  3061. }
  3062. pos += 8 + size + (size & 1);
  3063. }
  3064. return f;
  3065. };
  3066. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  3067. const auto wti = readAll (folder / (name + ".wti"));
  3068. std::vector<std::string> names;
  3069. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  3070. CHECK (table.has_value());
  3071. if (! table)
  3072. return;
  3073. std::vector<FileSamples> files;
  3074. for (std::size_t i = 0; i < names.size(); ++i)
  3075. {
  3076. auto rel = names[i];
  3077. std::replace (rel.begin(), rel.end(), '\\', '/');
  3078. const auto bytes = readAll (folder / rel);
  3079. if (table->raw)
  3080. {
  3081. FileSamples f;
  3082. f.samples.resize (bytes.size() / 2);
  3083. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  3084. files.push_back (std::move (f));
  3085. }
  3086. else
  3087. {
  3088. table->files[i] = wavFileInfo (bytes);
  3089. files.push_back (pcm (bytes));
  3090. }
  3091. }
  3092. const auto w = buildWavetable (*table, files);
  3093. CHECK (w != nullptr);
  3094. if (w == nullptr)
  3095. return;
  3096. State s;
  3097. s.wavetable = name;
  3098. s.knobs = knobs;
  3099. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  3100. RefNote n;
  3101. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  3102. return n;
  3103. };
  3104. 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),
  3105. 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) };
  3106. // (floats as the harness parses them)
  3107. for (auto& n : notes)
  3108. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  3109. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  3110. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  3111. std::uint64_t h = 0xcbf29ce484222325ull;
  3112. for (auto v : out)
  3113. {
  3114. std::uint32_t b;
  3115. std::memcpy (&b, &v, 4);
  3116. for (int k = 0; k < 4; ++k)
  3117. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  3118. }
  3119. CHECK (h == expected);
  3120. };
  3121. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  3122. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  3123. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  3124. }
  3125. TEST (sampler_plays_pitched_one_shot)
  3126. {
  3127. // 1000-frame ramp sample: the position can be measured from the amplitude
  3128. auto data = std::make_shared<lynx::SampleData>();
  3129. data->sampleRate = 48000.0;
  3130. for (int i = 0; i < 1000; ++i)
  3131. data->left.push_back (1.0f);
  3132. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3133. sampler->prepare (48000.0);
  3134. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  3135. sampler->noteOn (60, 1.0f);
  3136. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  3137. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3138. int sounding = 0;
  3139. for (auto s : l)
  3140. sounding += s > 0.5f;
  3141. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  3142. std::fill (l.begin(), l.end(), 0.0f);
  3143. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  3144. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  3145. sounding = 0;
  3146. for (auto s : l)
  3147. sounding += s > 0.5f;
  3148. CHECK (sounding >= 495 && sounding <= 500);
  3149. CHECK (r[10] > 0.5f); // mono goes to both sides
  3150. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  3151. silent->prepare (48000.0);
  3152. silent->noteOn (60, 1.0f);
  3153. std::fill (l.begin(), l.end(), 0.0f);
  3154. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  3155. CHECK (l[0] == 0.0f);
  3156. }
  3157. TEST (rack_live_notes_and_sample_provider)
  3158. {
  3159. lynx::Song song;
  3160. lynx::ChannelDef c;
  3161. c.name = "Smp";
  3162. c.instrument = lynx::InstrumentKind::sampler;
  3163. c.samplePath = "a.wav";
  3164. song.channels.push_back (c);
  3165. song.patterns.emplace_back();
  3166. int loads = 0;
  3167. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  3168. ++loads;
  3169. CHECK (path == "a.wav");
  3170. auto d = std::make_shared<lynx::SampleData>();
  3171. d->left.assign (4800, 0.5f);
  3172. return d;
  3173. };
  3174. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  3175. CHECK (loads == 1);
  3176. lynx::RackRenderer renderer;
  3177. renderer.prepare (48000.0);
  3178. lynx::LiveEventQueue live;
  3179. lynx::NoteEvent on;
  3180. on.channel = 0;
  3181. on.velocity = 1.0f;
  3182. live.push (on);
  3183. std::vector<float> l (256), r (256);
  3184. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  3185. CHECK (l[0] > 0.1f);
  3186. // same sample: reused; different sample: reloaded
  3187. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  3188. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  3189. song.channels[0].samplePath = "b.wav";
  3190. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  3191. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  3192. }
  3193. TEST (flp_paths_resolve_and_samplers_import)
  3194. {
  3195. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  3196. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  3197. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  3198. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  3199. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  3200. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  3201. {
  3202. // POSIX roots (macOS, Linux, Wine prefix): backslashes become '/', letter case follows the disk (round 117)
  3203. namespace fs = std::filesystem;
  3204. const auto base = fs::temp_directory_path() / ("lynx-case-test-" + processTempTag());
  3205. fs::remove_all (base);
  3206. fs::create_directories (base / "Data" / "Patches" / "Packs");
  3207. { std::ofstream (base / "Data" / "Patches" / "Packs" / "Kick.WAV") << "x"; }
  3208. lynx::FlPathRoots posix;
  3209. posix.factoryData = base.string();
  3210. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\Patches\\Packs\\Kick.WAV", posix) == (base / "Data/Patches/Packs/Kick.WAV").string());
  3211. const auto lowerCase = lynx::resolveFlPath ("%FLStudioFactoryData%\\data\\patches\\packs\\kick.wav", posix);
  3212. if (fs::exists (base / "data")) // case-insensitive file system (macOS): any spelling opens the file
  3213. CHECK (fs::exists (lowerCase));
  3214. else
  3215. CHECK (lowerCase == (base / "Data/Patches/Packs/Kick.WAV").string());
  3216. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\missing.wav", posix) == (base / "Data/missing.wav").string());
  3217. fs::remove_all (base);
  3218. }
  3219. flp::Project p;
  3220. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  3221. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  3222. lynx::FlpImportOptions options;
  3223. options.roots = roots;
  3224. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  3225. const auto s = lynx::importFlp (p, options);
  3226. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  3227. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  3228. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  3229. }
  3230. TEST (output_protection_is_transparent_up_to_full_scale)
  3231. {
  3232. // round 160: no more soft knee at 0.9 — FL leaves the mix untouched up to 0 dBFS
  3233. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f, 0.999f, std::nanf (""), -1.0f };
  3234. lynx::clipOvers (s, 8);
  3235. CHECK (s[0] == 0.5f && s[1] == -0.89f && s[4] == 0.95f && s[5] == 0.999f && s[7] == -1.0f);
  3236. CHECK (s[2] == 1.0f && s[3] == -1.0f);
  3237. CHECK (s[6] == 0.0f);
  3238. }
  3239. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  3240. {
  3241. lynx::Song s;
  3242. s.channels.resize (1);
  3243. auto& p = s.patterns.emplace_back();
  3244. for (int step : { 0, 4, 8, 12 }) // one note per beat
  3245. p.toggleStep (0, step);
  3246. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  3247. lynx::Clip c;
  3248. c.pattern = 0;
  3249. c.start = 4 * lynx::ppq;
  3250. c.length = 2 * lynx::ppq;
  3251. c.offset = 2 * lynx::ppq;
  3252. s.playlist.push_back (c);
  3253. CHECK (s.songLength() == 6 * lynx::ppq); // FL: the song ends where the last clip ends
  3254. const double sr = 48000.0;
  3255. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  3256. lynx::EventBuffer ev;
  3257. // whole song in a single block (8 beats = 192000 samples)
  3258. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3259. std::vector<int> ons;
  3260. for (auto& e : ev)
  3261. if (e.type == lynx::NoteEvent::Type::on)
  3262. ons.push_back (e.sampleOffset);
  3263. CHECK (ons.size() == 2);
  3264. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  3265. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  3266. ev.clear();
  3267. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  3268. CHECK (ev.size() == 0);
  3269. // a note longer than the clip is cut at the clip end
  3270. p.notes[3].length = 4 * lynx::ppq;
  3271. ev.clear();
  3272. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  3273. int lastOff = -1;
  3274. for (auto& e : ev)
  3275. if (e.type == lynx::NoteEvent::Type::off)
  3276. lastOff = std::max (lastOff, e.sampleOffset);
  3277. CHECK (lastOff == 6 * 24000);
  3278. }
  3279. TEST (remove_pattern_fixes_clips)
  3280. {
  3281. lynx::Song s;
  3282. s.patterns.resize (3);
  3283. s.currentPattern = 2;
  3284. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  3285. s.removePattern (1);
  3286. CHECK (s.patterns.size() == 2);
  3287. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  3288. CHECK (s.currentPattern == 1);
  3289. }
  3290. TEST (flp_import_3x_osc_state_and_channel_filter)
  3291. {
  3292. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  3293. std::vector<std::uint8_t> blob (460, 0);
  3294. auto put = [&] (std::size_t off, std::int32_t v) {
  3295. for (int b = 0; b < 4; ++b)
  3296. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  3297. };
  3298. put (0x00, 14);
  3299. put (0x08, 3); // osc 1 saw
  3300. put (0x10, 7); // osc 1 fine +7
  3301. put (0x04, -32); // osc 1 pan -0.5
  3302. put (0x1C, 64); // osc 2 mix
  3303. put (0x28, -12); // osc 2 coarse
  3304. put (0x38, 32); // osc 3 mix
  3305. put (0x40, 2); // osc 3 square
  3306. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  3307. lynx::SynthSettings s;
  3308. CHECK (lynx::synthFrom3xOsc (blob, s));
  3309. 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);
  3310. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  3311. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  3312. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  3313. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  3314. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  3315. float hz = 0.0f, res = 1.0f;
  3316. int mode = -1;
  3317. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  3318. CHECK (hz >= 19999.0f && res == 0.0f);
  3319. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  3320. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  3321. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  3322. CHECK (hz > 240.0f && hz < 320.0f);
  3323. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  3324. CHECK (res > 0.8f);
  3325. CHECK (mode == 0);
  3326. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  3327. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  3328. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  3329. CHECK (mode == 1);
  3330. }
  3331. TEST (synth_pitch_and_filter_envelopes)
  3332. {
  3333. auto render = [] (const lynx::SynthSettings& s, int frames) {
  3334. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3335. inst->prepare (48000.0);
  3336. lynx::ChannelDef c;
  3337. c.synth = s;
  3338. inst->configure (c, 120.0);
  3339. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  3340. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3341. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  3342. return l;
  3343. };
  3344. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  3345. int n = 0;
  3346. for (int i = from + 1; i < to; ++i)
  3347. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  3348. return n;
  3349. };
  3350. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  3351. lynx::SynthSettings kick;
  3352. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3353. kick.cutoff = 20000.0f;
  3354. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  3355. const auto k = render (kick, 48000);
  3356. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  3357. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  3358. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  3359. CHECK (std::abs (late - 110) < 4);
  3360. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  3361. lynx::SynthSettings pluck;
  3362. pluck.cutoff = 200.0f;
  3363. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  3364. const auto p = render (pluck, 48000);
  3365. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  3366. double e = 0.0;
  3367. for (int i = from + 1; i < to; ++i)
  3368. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  3369. return e;
  3370. };
  3371. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  3372. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  3373. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  3374. lynx::SynthSettings toxic = pluck;
  3375. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  3376. const auto t = render (toxic, 48000);
  3377. auto thf = [&] (int from, int to) {
  3378. double e = 0.0;
  3379. for (int i = from + 1; i < to; ++i)
  3380. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  3381. return e;
  3382. };
  3383. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  3384. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  3385. }
  3386. TEST (synth_mono_reuses_the_sounding_voice)
  3387. {
  3388. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3389. inst->prepare (48000.0);
  3390. lynx::ChannelDef c;
  3391. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3392. c.synth.cutoff = 20000.0f;
  3393. c.synth.mono = true;
  3394. inst->configure (c, 120.0);
  3395. std::vector<float> l (48000), r (48000);
  3396. inst->noteOn (57, 1.0f);
  3397. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3398. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3399. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3400. int crossings = 0;
  3401. float peak = 0.0f;
  3402. for (std::size_t i = 24001; i < 48000; ++i)
  3403. {
  3404. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3405. peak = std::max (peak, std::abs (l[i]));
  3406. }
  3407. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3408. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3409. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3410. float jump = 0.0f;
  3411. for (std::size_t i = 4790; i < 4810; ++i)
  3412. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3413. CHECK (jump < 0.05f);
  3414. }
  3415. TEST (synth_porta_glides_and_am_modulates)
  3416. {
  3417. auto make = [] (const lynx::SynthSettings& s) {
  3418. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3419. inst->prepare (48000.0);
  3420. lynx::ChannelDef c;
  3421. c.synth = s;
  3422. inst->configure (c, 120.0);
  3423. return inst;
  3424. };
  3425. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3426. int n = 0;
  3427. for (auto i = from + 1; i < to; ++i)
  3428. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3429. return n;
  3430. };
  3431. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3432. lynx::SynthSettings s;
  3433. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3434. s.cutoff = 20000.0f;
  3435. s.mono = true;
  3436. s.porta = true;
  3437. s.portaBeats = 1.0f;
  3438. auto inst = make (s);
  3439. std::vector<float> l (96000), r (96000);
  3440. inst->noteOn (57, 1.0f);
  3441. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3442. inst->noteOn (69, 1.0f);
  3443. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3444. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3445. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3446. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3447. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3448. lynx::SynthSettings am;
  3449. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3450. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3451. am.cutoff = 20000.0f;
  3452. am.am = true;
  3453. auto modulated = make (am);
  3454. modulated->noteOn (69, 1.0f);
  3455. std::fill (l.begin(), l.end(), 0.0f);
  3456. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3457. float peak = 0.0f;
  3458. for (std::size_t i = 4800; i < 48000; ++i)
  3459. peak = std::max (peak, std::abs (l[i]));
  3460. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3461. }
  3462. TEST (channel_lfo_vibrato_and_tremolo)
  3463. {
  3464. auto render = [] (const lynx::SynthSettings& s) {
  3465. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3466. inst->prepare (48000.0);
  3467. lynx::ChannelDef c;
  3468. c.synth = s;
  3469. inst->configure (c, 120.0);
  3470. inst->noteOn (69, 1.0f);
  3471. std::vector<float> l (48000), r (48000);
  3472. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3473. return l;
  3474. };
  3475. lynx::SynthSettings s;
  3476. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3477. s.cutoff = 20000.0f;
  3478. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3479. auto tremolo = s;
  3480. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3481. const auto t = render (tremolo);
  3482. float lowest = 1.0f, highest = 0.0f;
  3483. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3484. {
  3485. float peak = 0.0f;
  3486. for (auto i = w; i < w + 480; ++i)
  3487. peak = std::max (peak, std::abs (t[i]));
  3488. lowest = std::min (lowest, peak);
  3489. highest = std::max (highest, peak);
  3490. }
  3491. CHECK (lowest < 0.08f && highest > 0.5f);
  3492. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3493. auto vibrato = s;
  3494. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3495. const auto v = render (vibrato);
  3496. int crossings = 0;
  3497. for (std::size_t i = 1; i < 48000; ++i)
  3498. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3499. CHECK (std::abs (crossings - 880) < 12);
  3500. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3501. for (std::size_t i = 1; i < 6000; ++i)
  3502. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3503. CHECK (firstQuarter > 112);
  3504. }
  3505. TEST (sampler_porta_glides_from_previous_note)
  3506. {
  3507. auto data = std::make_shared<lynx::SampleData>();
  3508. data->sampleRate = 48000.0;
  3509. data->left.resize (96000);
  3510. for (std::size_t i = 0; i < data->left.size(); ++i)
  3511. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3512. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3513. inst->prepare (48000.0);
  3514. lynx::ChannelDef c;
  3515. c.sampler.cut = true;
  3516. c.sampler.porta = true;
  3517. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3518. inst->configure (c, 120.0);
  3519. std::vector<float> l (24000), r (24000);
  3520. inst->noteOn (60, 1.0f);
  3521. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3522. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3523. std::fill (l.begin(), l.end(), 0.0f);
  3524. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3525. auto crossings = [&] (std::size_t from, std::size_t to) {
  3526. int n = 0;
  3527. for (auto i = from + 1; i < to; ++i)
  3528. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3529. return n;
  3530. };
  3531. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3532. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3533. CHECK (start < 22);
  3534. CHECK (end > 36);
  3535. }
  3536. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3537. {
  3538. auto sine = [] (double hz, float amp) {
  3539. auto d = std::make_shared<lynx::SampleData>();
  3540. d->sampleRate = 48000.0;
  3541. d->left.resize (48000);
  3542. for (std::size_t i = 0; i < d->left.size(); ++i)
  3543. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3544. return d;
  3545. };
  3546. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3547. // hard layer on the highs: same tuning, louder
  3548. std::vector<lynx::LoadedZone> zones {
  3549. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3550. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3551. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3552. };
  3553. auto inst = lynx::makeZonedSampler (zones);
  3554. inst->prepare (48000.0);
  3555. inst->configure (lynx::ChannelDef {}, 120.0);
  3556. auto play = [&] (int key, float velocity) {
  3557. inst->allNotesOff();
  3558. std::vector<float> l (24000), r (24000);
  3559. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3560. std::fill (l.begin(), l.end(), 0.0f);
  3561. inst->noteOn (key, velocity);
  3562. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3563. int crossings = 0;
  3564. float peak = 0.0f;
  3565. for (std::size_t i = 1; i < 24000; ++i)
  3566. {
  3567. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3568. peak = std::max (peak, std::abs (l[i]));
  3569. }
  3570. return std::pair { crossings, peak };
  3571. };
  3572. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3573. CHECK (std::abs (low.first - 440) < 4);
  3574. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3575. CHECK (std::abs (soft.first - 880) < 6);
  3576. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3577. CHECK (hard.second > soft.second * 2.0f);
  3578. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3579. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3580. rr->prepare (48000.0);
  3581. rr->configure (lynx::ChannelDef {}, 120.0);
  3582. float peaks[2] {};
  3583. for (int k = 0; k < 2; ++k)
  3584. {
  3585. rr->allNotesOff();
  3586. std::vector<float> l (4800), r (4800);
  3587. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3588. std::fill (l.begin(), l.end(), 0.0f);
  3589. rr->noteOn (60, 1.0f);
  3590. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3591. for (auto v : l)
  3592. peaks[k] = std::max (peaks[k], std::abs (v));
  3593. }
  3594. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3595. }
  3596. TEST (zoned_sampler_loops_while_held)
  3597. {
  3598. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3599. // on release, it leaves the loop and ends
  3600. auto data = std::make_shared<lynx::SampleData>();
  3601. data->sampleRate = 48000.0;
  3602. data->left.resize (4800);
  3603. for (std::size_t i = 0; i < data->left.size(); ++i)
  3604. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3605. lynx::SampleZone zone { "loop.wav", 60 };
  3606. zone.loopStart = 1000;
  3607. zone.loopEnd = 3000;
  3608. zone.loopMode = 2;
  3609. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3610. inst->prepare (48000.0);
  3611. lynx::ChannelDef c;
  3612. c.sampler.release = 0.01f;
  3613. inst->configure (c, 120.0);
  3614. inst->noteOn (60, 1.0f);
  3615. std::vector<float> l (48000), r (48000);
  3616. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3617. float held = 0.0f;
  3618. for (std::size_t i = 20000; i < 24000; ++i)
  3619. held = std::max (held, std::abs (l[i]));
  3620. CHECK (held > 0.3f);
  3621. inst->noteOff (60);
  3622. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3623. float after = 0.0f;
  3624. for (std::size_t i = 40000; i < 48000; ++i)
  3625. after = std::max (after, std::abs (l[i]));
  3626. CHECK (after == 0.0f);
  3627. }
  3628. TEST (global_lfo_follows_song_position)
  3629. {
  3630. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3631. // same note with a per-note LFO starts at phase 0
  3632. auto first = [] (bool global, double songTicks) {
  3633. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3634. inst->prepare (48000.0);
  3635. lynx::ChannelDef c;
  3636. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3637. c.synth.cutoff = 20000.0f;
  3638. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3639. inst->configure (c, 120.0);
  3640. inst->setSongPosition (songTicks);
  3641. inst->noteOn (69, 1.0f);
  3642. std::vector<float> l (4800), r (4800);
  3643. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3644. float peak = 0.0f;
  3645. for (std::size_t i = 2400; i < 4800; ++i)
  3646. peak = std::max (peak, std::abs (l[i]));
  3647. return peak;
  3648. };
  3649. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3650. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3651. const auto perNote = first (false, 0.6 * lynx::ppq);
  3652. CHECK (globalLate < 0.05f);
  3653. CHECK (perNote > 0.4f);
  3654. }
  3655. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3656. {
  3657. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3658. auto data = std::make_shared<lynx::SampleData>();
  3659. data->sampleRate = 48000.0;
  3660. data->left.resize (48000);
  3661. std::uint32_t seed = 7;
  3662. for (auto& v : data->left)
  3663. {
  3664. seed = seed * 1664525u + 1013904223u;
  3665. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3666. }
  3667. data->right = data->left;
  3668. auto energy = [&] (float cutoff) {
  3669. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3670. inst->prepare (48000.0);
  3671. lynx::ChannelDef c;
  3672. c.sampler.cutoff = cutoff;
  3673. inst->configure (c, 120.0);
  3674. inst->noteOn (60, 1.0f);
  3675. std::vector<float> l (24000), r (24000);
  3676. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3677. double e = 0.0;
  3678. for (auto v : l)
  3679. e += (double) v * v;
  3680. return e;
  3681. };
  3682. const auto open = energy (20000.0f), dark = energy (300.0f);
  3683. {
  3684. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3685. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3686. inst->prepare (48000.0);
  3687. lynx::ChannelDef hp;
  3688. hp.sampler.cutoff = 10000.0f;
  3689. hp.sampler.filterType = 2;
  3690. inst->configure (hp, 120.0);
  3691. inst->noteOn (60, 1.0f);
  3692. std::vector<float> l (24000), r (24000);
  3693. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3694. double e = 0.0, lowE = 0.0;
  3695. float lp = 0.0f;
  3696. for (auto v : l)
  3697. {
  3698. e += (double) v * v;
  3699. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3700. lowE += (double) lp * lp;
  3701. }
  3702. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3703. CHECK (lowE < e * 0.01);
  3704. }
  3705. CHECK (open > 100.0);
  3706. CHECK (dark < open * 0.05);
  3707. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3708. // and ends in half the time (1 s of sample -> 0.5 s)
  3709. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3710. inst->prepare (48000.0);
  3711. lynx::ChannelDef c;
  3712. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3713. inst->configure (c, 120.0);
  3714. inst->noteOn (60, 1.0f);
  3715. std::vector<float> l (48000), r (48000);
  3716. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3717. float tail = 0.0f, body = 0.0f;
  3718. for (std::size_t i = 20000; i < 23000; ++i)
  3719. body = std::max (body, std::abs (l[i]));
  3720. for (std::size_t i = 25000; i < 48000; ++i)
  3721. tail = std::max (tail, std::abs (l[i]));
  3722. CHECK (body > 0.1f);
  3723. CHECK (tail == 0.0f);
  3724. }
  3725. TEST (synth_oscillator_pan_and_semitones)
  3726. {
  3727. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3728. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3729. synth->prepare (48000.0);
  3730. lynx::ChannelDef c;
  3731. c.synth = s;
  3732. synth->configure (c, 120.0);
  3733. synth->noteOn (69, 1.0f);
  3734. l.assign (24000, 0.0f);
  3735. r.assign (24000, 0.0f);
  3736. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3737. };
  3738. lynx::SynthSettings s;
  3739. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3740. s.cutoff = 20000.0f;
  3741. std::vector<float> l, r;
  3742. render (s, l, r);
  3743. float peakL = 0.0f, peakR = 0.0f;
  3744. int crossings = 0;
  3745. for (std::size_t i = 4800; i < 24000; ++i)
  3746. {
  3747. peakL = std::max (peakL, std::abs (l[i]));
  3748. peakR = std::max (peakR, std::abs (r[i]));
  3749. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3750. }
  3751. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3752. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3753. CHECK (std::abs (crossings - 527) < 6);
  3754. }
  3755. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3756. {
  3757. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3758. std::vector<std::uint8_t> blob;
  3759. 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))); };
  3760. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3761. u32 (b, id);
  3762. u32 (b, (std::uint32_t) payload.size());
  3763. u32 (b, 0);
  3764. b.insert (b.end(), payload.begin(), payload.end());
  3765. };
  3766. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3767. std::vector<std::uint8_t> vst3State;
  3768. u32 (vst3State, 1);
  3769. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3770. record (vst3State, 3, bytes ("COMPONENTE"));
  3771. record (vst3State, 2, bytes ("CONTROLADOR"));
  3772. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3773. std::vector<std::uint8_t> slots;
  3774. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3775. u32 (slots, v);
  3776. record (vst3State, 4, slots);
  3777. u32 (blob, 12);
  3778. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3779. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3780. std::vector<std::uint8_t> cid;
  3781. for (int i = 0; i < 16; ++i)
  3782. cid.push_back ((std::uint8_t) (0xA0 + i));
  3783. record (blob, 0x34, cid);
  3784. record (blob, 0x36, bytes ("Surge XT"));
  3785. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3786. record (blob, 0x35, vst3State);
  3787. lynx::ForeignPlugin p;
  3788. CHECK (lynx::parseFlWrapper (blob, p));
  3789. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3790. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3791. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3792. std::vector<std::uint8_t> component, controller;
  3793. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3794. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3795. const auto saved = lynx::flVst3ParamSlots (p.state);
  3796. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3797. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3798. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3799. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3800. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3801. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3802. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3803. {
  3804. lynx::FlVst2State st;
  3805. st.flags = 8;
  3806. st.currentProgram = 3;
  3807. st.chunk = bytes ("BANKCHUNK");
  3808. const auto withChunk = lynx::writeFlVst2State (st);
  3809. lynx::FlVst2State back;
  3810. CHECK (lynx::parseFlVst2State (withChunk, back));
  3811. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3812. CHECK (! back.chunkIsProgram());
  3813. lynx::FlVst2State raw;
  3814. raw.flags = 1;
  3815. raw.params = { 0.25f, 0.5f, 1.0f };
  3816. raw.programNames = { "Init" };
  3817. const auto withParams = lynx::writeFlVst2State (raw);
  3818. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3819. CHECK (lynx::parseFlVst2State (withParams, back));
  3820. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3821. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3822. std::vector<std::uint8_t> v5;
  3823. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3824. u32 (v5, v);
  3825. for (auto c : bytes ("ABCD"))
  3826. v5.push_back (c);
  3827. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3828. std::vector<std::uint8_t> v6;
  3829. u32 (v6, 0xFFFFFFFAu);
  3830. v6.push_back (0);
  3831. for (auto v : { 2u, 0u })
  3832. u32 (v6, v);
  3833. v6.push_back ('x');
  3834. v6.push_back ('y');
  3835. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3836. // nothing to restore (123456), unknown old layouts, truncated chunks
  3837. std::vector<std::uint8_t> marker;
  3838. u32 (marker, 123456u);
  3839. CHECK (! lynx::parseFlVst2State (marker, back));
  3840. auto cut = withChunk;
  3841. cut.pop_back();
  3842. CHECK (! lynx::parseFlVst2State (cut, back));
  3843. CHECK (! lynx::parseFlVst2State ({}, back));
  3844. }
  3845. // version out of range or no type: not a usable wrapper
  3846. blob[0] = 13;
  3847. CHECK (! lynx::parseFlWrapper (blob, p));
  3848. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3849. }
  3850. TEST (schedule_index_matches_full_scan)
  3851. {
  3852. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3853. // exactly the same events as the full scan, including in the block that wraps around
  3854. lynx::Song s = lynx::Song::makeDefault();
  3855. s.swing = 0.6f;
  3856. std::uint32_t seed = 12345;
  3857. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3858. s.patterns.resize (3);
  3859. for (auto& p : s.patterns)
  3860. {
  3861. p.notes.clear();
  3862. for (int i = 0; i < 60; ++i)
  3863. {
  3864. lynx::Note n;
  3865. n.start = rnd (16 * lynx::ppq);
  3866. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3867. n.channel = rnd (4);
  3868. n.key = 40 + rnd (40);
  3869. n.slide = rnd (20) == 0;
  3870. p.notes.push_back (n);
  3871. }
  3872. p.sortNotes();
  3873. }
  3874. s.playlist.clear();
  3875. for (int i = 0; i < 12; ++i)
  3876. {
  3877. lynx::Clip c;
  3878. c.pattern = rnd (3);
  3879. c.track = rnd (4);
  3880. c.start = rnd (40) * lynx::ppq;
  3881. c.length = (rnd (12) + 1) * lynx::ppq;
  3882. c.offset = rnd (4) * lynx::ppq;
  3883. s.playlist.push_back (c);
  3884. }
  3885. s.mode = lynx::PlayMode::song;
  3886. const auto index = lynx::makeScheduleIndex (s);
  3887. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3888. const auto loop = (double) s.songLength();
  3889. int mismatches = 0, events = 0;
  3890. for (int block = 0; block < 400; ++block)
  3891. {
  3892. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3893. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3894. lynx::EventBuffer a, b;
  3895. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3896. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3897. events += a.size();
  3898. if (a.size() != b.size())
  3899. {
  3900. ++mismatches;
  3901. continue;
  3902. }
  3903. for (int i = 0; i < a.size(); ++i)
  3904. {
  3905. const auto& x = *(a.begin() + i);
  3906. const auto& y = *(b.begin() + i);
  3907. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3908. }
  3909. }
  3910. CHECK (events > 500);
  3911. CHECK (mismatches == 0);
  3912. }
  3913. TEST (mixer_track_eq_defaults_and_boost)
  3914. {
  3915. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3916. lynx::TrackEq eq;
  3917. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3918. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3919. CHECK (! eq.active());
  3920. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3921. lynx::Song song = lynx::Song::makeDefault();
  3922. song.mixer[1].eq.gainDb[1] = 12.0f;
  3923. for (auto& c : song.channels)
  3924. c.mixerTrack = 1;
  3925. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3926. song.channels[4].synth = {};
  3927. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3928. song.channels[4].synth.cutoff = 20000.0f;
  3929. auto render = [] (const lynx::Song& s) {
  3930. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3931. lynx::RackRenderer renderer;
  3932. renderer.prepare (48000.0);
  3933. lynx::LiveEventQueue live;
  3934. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3935. on.channel = 4;
  3936. on.key = 91;
  3937. on.velocity = 1.0f;
  3938. live.push (on);
  3939. std::vector<float> l (24000), r (24000);
  3940. for (int pos = 0; pos < 24000; pos += 480)
  3941. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3942. float peak = 0.0f;
  3943. for (std::size_t i = 12000; i < 24000; ++i)
  3944. peak = std::max (peak, std::abs (l[i]));
  3945. return peak;
  3946. };
  3947. const auto boosted = render (song);
  3948. song.mixer[1].eq.gainDb[1] = 0.0f;
  3949. const auto flat = render (song);
  3950. CHECK (flat > 0.01f);
  3951. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3952. }
  3953. TEST (bars_follow_time_signature_markers)
  3954. {
  3955. lynx::Song song;
  3956. song.beatsPerBar = 4;
  3957. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3958. auto bars = song.barsBetween (0, bar4 * 3);
  3959. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3960. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3961. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3962. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3963. const auto seven = (std::int64_t) lynx::ppq * 7;
  3964. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3965. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3966. CHECK (bars.size() >= 5);
  3967. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3968. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3969. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3970. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3971. // from far away: starts at the right bar without losing the numbering
  3972. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3973. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3974. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3975. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3976. }
  3977. TEST (split_drum_channel_moves_notes_to_pads)
  3978. {
  3979. lynx::Song song = lynx::Song::makeDefault();
  3980. const auto channels = (int) song.channels.size();
  3981. song.patterns[0].notes.clear();
  3982. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3983. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3984. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3985. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3986. lynx::Automation a;
  3987. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3988. song.automations.push_back (a);
  3989. std::vector<lynx::DrumPad> pads (2);
  3990. pads[0].name = "Kick";
  3991. pads[0].key = 36;
  3992. pads[0].layers = { { "kick.wav" } };
  3993. pads[1].name = "Snare";
  3994. pads[1].key = 38;
  3995. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3996. pads[1].cutGroup = 2;
  3997. pads[1].cutBy = 2;
  3998. lynx::splitDrumChannel (song, 1, pads);
  3999. CHECK ((int) song.channels.size() == channels + 1);
  4000. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  4001. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  4002. const auto& notes = song.patterns[0].notes;
  4003. CHECK (notes.size() == 3);
  4004. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  4005. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  4006. CHECK (notes[2].channel == 4); // the old channel 3
  4007. CHECK (song.automations[0].target.index == 4);
  4008. }
  4009. TEST (flp_import_brings_playlist)
  4010. {
  4011. flp::Project p;
  4012. p.header.ppq = 96;
  4013. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  4014. flp::Pattern empty;
  4015. empty.index = 1;
  4016. flp::Pattern full;
  4017. full.index = 2;
  4018. flp::Note n;
  4019. n.channel = 0;
  4020. full.notes.push_back (n);
  4021. p.patterns = { empty, full };
  4022. flp::Arrangement a;
  4023. flp::PlaylistItem item;
  4024. item.kind = flp::PlaylistItem::Kind::pattern;
  4025. item.index = 2;
  4026. item.position = 384;
  4027. item.length = 192;
  4028. item.track = 3;
  4029. item.patternStartTick = 96;
  4030. a.items.push_back (item);
  4031. item.index = 1; // empty pattern: discarded
  4032. a.items.push_back (item);
  4033. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  4034. a.items.push_back (item);
  4035. p.arrangements.push_back (a);
  4036. const auto s = lynx::importFlp (p);
  4037. CHECK (s.mode == lynx::PlayMode::song);
  4038. CHECK (s.playlist.size() == 1);
  4039. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  4040. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  4041. }
  4042. TEST (rack_releases_notes_on_position_jump)
  4043. {
  4044. lynx::Song s;
  4045. lynx::ChannelDef c;
  4046. c.instrument = lynx::InstrumentKind::synth;
  4047. s.channels.push_back (c);
  4048. auto& p = s.patterns.emplace_back();
  4049. lynx::Note n;
  4050. n.start = 0;
  4051. n.length = 4 * lynx::ppq; // long note
  4052. p.notes.push_back (n);
  4053. s.bpm = 120.0;
  4054. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  4055. lynx::RackRenderer r;
  4056. r.prepare (48000.0);
  4057. std::vector<float> l (512), rr (512);
  4058. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4059. double tick = 0.0;
  4060. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  4061. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4062. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  4063. for (int i = 0; i < 40; ++i)
  4064. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  4065. float tail = 0.0f;
  4066. for (auto v : l)
  4067. tail = std::max (tail, std::abs (v));
  4068. CHECK (tail < 0.001f);
  4069. }
  4070. namespace
  4071. {
  4072. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  4073. float juce_db (float gain)
  4074. {
  4075. return 20.0f * std::log10 (gain);
  4076. }
  4077. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  4078. {
  4079. lynx::Song s;
  4080. s.mixer = lynx::Song::defaultMixer (4);
  4081. for (int i = 0; i < 2; ++i)
  4082. {
  4083. lynx::ChannelDef c;
  4084. c.instrument = lynx::InstrumentKind::sampler;
  4085. c.samplePath = "dc";
  4086. c.volume = 1.0f;
  4087. c.mixerTrack = i + 1;
  4088. s.channels.push_back (c);
  4089. }
  4090. s.patterns.emplace_back();
  4091. tweak (s);
  4092. auto provider = [] (const std::string&) {
  4093. auto d = std::make_shared<lynx::SampleData>();
  4094. d->left.assign (48000, 0.5f);
  4095. return std::shared_ptr<const lynx::SampleData> (d);
  4096. };
  4097. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4098. lynx::RackRenderer r;
  4099. r.prepare (48000.0, 256);
  4100. lynx::LiveEventQueue live;
  4101. for (int ch = 0; ch < 2; ++ch)
  4102. {
  4103. lynx::NoteEvent on;
  4104. on.channel = ch;
  4105. on.velocity = 1.0f;
  4106. live.push (on);
  4107. }
  4108. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  4109. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  4110. return std::max (std::abs (l[999]), std::abs (rr[999]));
  4111. }
  4112. } // namespace
  4113. TEST (mixer_routes_mutes_solos_and_meters)
  4114. {
  4115. // two 0.5 channels summed on the Master at 0 dB
  4116. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  4117. // insert 1 muted: only channel 2 remains
  4118. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  4119. // solo on insert 2: same
  4120. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  4121. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  4122. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  4123. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  4124. CHECK (lynx::faderGain (0.0f) == 0.0f);
  4125. // Master fader at max
  4126. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  4127. // both channels on the same insert, insert fader at 0.4
  4128. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  4129. s.channels[1].mixerTrack = 1;
  4130. s.mixer[1].volume = 0.4f;
  4131. }) - lynx::faderGain (0.4f)) < 1e-4f);
  4132. // a route outside the mixer falls to the last existing insert, without crashing
  4133. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  4134. // no mixer: everything straight to the output at 0 dB
  4135. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  4136. lynx::MeterBank meters;
  4137. renderMixerCase ([] (lynx::Song&) {}, &meters);
  4138. // (the 24-point sinc's ringing at the sample start: its x87 kernel table rounds differently on ARM64)
  4139. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < armTolerance (1e-4f, 2e-4f));
  4140. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < armTolerance (1e-4f, 4e-4f));
  4141. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  4142. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  4143. }
  4144. TEST (next_free_insert_and_flp_mixer_import)
  4145. {
  4146. auto s = lynx::Song::makeDefault();
  4147. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  4148. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  4149. flp::Project p;
  4150. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  4151. p.mixer = { { 0, "" }, { 30, "Drums" } };
  4152. const auto song = lynx::importFlp (p);
  4153. CHECK (song.channels[0].mixerTrack == 30);
  4154. CHECK (song.mixer.size() == 31);
  4155. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  4156. }
  4157. namespace
  4158. {
  4159. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  4160. int frames = 48000)
  4161. {
  4162. auto fx = lynx::makeEffect (kind);
  4163. fx->prepare (48000.0);
  4164. auto slot = lynx::makeEffectSlot (kind);
  4165. tweak (slot);
  4166. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4167. for (int i = 0; i < frames; ++i)
  4168. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  4169. for (int i = 0; i < frames; i += 480)
  4170. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  4171. float peak = 0.0f;
  4172. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  4173. {
  4174. CHECK (std::isfinite (l[(std::size_t) i]));
  4175. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4176. }
  4177. return peak;
  4178. }
  4179. } // namespace
  4180. TEST (parametric_eq_bands_and_response)
  4181. {
  4182. // default: transparent
  4183. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4184. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  4185. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  4186. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  4187. auto boost = [] (lynx::EffectSlot& s) {
  4188. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  4189. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  4190. };
  4191. boost (slot);
  4192. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  4193. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  4194. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  4195. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  4196. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  4197. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  4198. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  4199. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  4200. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  4201. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  4202. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  4203. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  4204. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  4205. // round-trip conversions
  4206. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  4207. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  4208. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  4209. }
  4210. TEST (fl_parametric_eq2_state_maps_to_bands)
  4211. {
  4212. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  4213. std::vector<std::uint8_t> blob (354, 0);
  4214. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4215. put (0, 8);
  4216. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  4217. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  4218. for (int b = 0; b < 7; ++b)
  4219. param (21 + b, 0); // all off
  4220. param (21 + 0, 3); // high pass (= low cut)
  4221. param (7 + 0, hzRaw (100.0));
  4222. param (14 + 0, 40124); // default width -> Q ~0.68
  4223. param (28 + 0, -3); // Steep 8 -> 4 biquads
  4224. param (21 + 3, 6); // peaking
  4225. param (7 + 3, hzRaw (1000.0));
  4226. param (3, 600); // +6 dB
  4227. param (14 + 3, 25412);
  4228. param (35, -300); // main level −3 dB
  4229. put (0x13E, 0x7F);
  4230. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4231. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  4232. const auto& p = slot->params;
  4233. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  4234. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  4235. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  4236. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  4237. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  4238. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  4239. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  4240. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  4241. put (0x13E, 0x7F & ~(1 << 3));
  4242. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  4243. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  4244. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  4245. }
  4246. TEST (reverb_low_cut_and_predelay)
  4247. {
  4248. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  4249. // 100 Hz sine barely enters the reverb
  4250. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  4251. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  4252. fx->prepare (48000.0);
  4253. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4254. slot.params[4] = 0.0f; // wet only
  4255. slot.params[3] = 1.0f;
  4256. tweak (slot);
  4257. std::vector<float> l (48000), r (48000);
  4258. for (int i = 0; i < 48000; ++i)
  4259. 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);
  4260. for (int i = 0; i < 48000; i += 480)
  4261. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  4262. return l;
  4263. };
  4264. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  4265. float early = 0.0f, later = 0.0f;
  4266. for (std::size_t i = 0; i < 4800; ++i)
  4267. early = std::max (early, std::abs (delayed[i]));
  4268. for (std::size_t i = 4800; i < 24000; ++i)
  4269. later = std::max (later, std::abs (delayed[i]));
  4270. CHECK (early == 0.0f && later > 0.0f);
  4271. 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; };
  4272. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  4273. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  4274. CHECK (cut < open * 0.01);
  4275. }
  4276. TEST (utility_effect_and_fl_stereo_tools)
  4277. {
  4278. // transparent by default; balance fully right silences the left
  4279. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  4280. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  4281. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  4282. std::vector<std::uint8_t> blob (36, 0);
  4283. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  4284. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  4285. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  4286. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  4287. auto shaperFx = lynx::makeEffect (*slot);
  4288. shaperFx->prepare (44100.0);
  4289. float zl = 0.0f, zr = 0.0f;
  4290. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  4291. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  4292. float l = 1.0f, r = 0.0f;
  4293. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  4294. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  4295. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  4296. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  4297. // Balance: volume 256 = 0 dB, pan −128 = fully left
  4298. std::vector<std::uint8_t> bal (8, 0);
  4299. std::int32_t pan = -128, vol = 256;
  4300. std::memcpy (bal.data(), &pan, 4);
  4301. std::memcpy (bal.data() + 4, &vol, 4);
  4302. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  4303. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  4304. if (b)
  4305. {
  4306. auto balance = lynx::makeEffect (*b);
  4307. balance->prepare (44100.0);
  4308. l = 0.5f; r = 0.5f;
  4309. balance->process (&l, &r, 1, b->params, 120.0);
  4310. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  4311. }
  4312. }
  4313. TEST (effects_eq_compressor_behave)
  4314. {
  4315. // EQ at default is transparent
  4316. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  4317. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  4318. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  4319. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  4320. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  4321. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  4322. s.params[0] = 40.0f / 60.0f;
  4323. s.params[1] = 1.0f;
  4324. }, 200.0, 1.0f);
  4325. CHECK (squashed < 0.2f && squashed > 0.05f);
  4326. // a signal below the threshold passes intact
  4327. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  4328. }
  4329. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  4330. {
  4331. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  4332. delay->prepare (48000.0);
  4333. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4334. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  4335. slot.params[1] = 0.0f; // no feedback: a single echo
  4336. slot.params[2] = 1.0f;
  4337. slot.params[4] = 0.0f; // no damping
  4338. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  4339. l[0] = r[0] = 1.0f;
  4340. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4341. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  4342. CHECK (std::abs (l[12000]) < 1e-6f);
  4343. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  4344. reverb->prepare (48000.0);
  4345. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4346. rs.params[3] = 1.0f;
  4347. std::fill (l.begin(), l.end(), 0.0f);
  4348. std::fill (r.begin(), r.end(), 0.0f);
  4349. l[0] = r[0] = 1.0f;
  4350. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  4351. float tail = 0.0f;
  4352. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  4353. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  4354. CHECK (tail > 1e-4f && tail < 1.0f);
  4355. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  4356. }
  4357. TEST (rack_runs_insert_effects_and_reuses_them)
  4358. {
  4359. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  4360. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  4361. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4362. eq.params[4] = 0.0f; // output -18 dB
  4363. s.mixer[1].effects.push_back (eq);
  4364. });
  4365. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  4366. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  4367. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4368. eq.params[4] = 0.0f;
  4369. eq.enabled = false;
  4370. s.mixer[1].effects.push_back (eq);
  4371. });
  4372. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  4373. auto s = lynx::Song::makeDefault();
  4374. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4375. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  4376. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  4377. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  4378. CHECK (b->effects[2][0] == a->effects[2][0]);
  4379. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4380. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  4381. CHECK (c->effects[2][0] != b->effects[2][0]);
  4382. }
  4383. TEST (audio_clips_trigger_cut_and_resume)
  4384. {
  4385. lynx::Song s;
  4386. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  4387. lynx::ChannelDef c;
  4388. c.instrument = lynx::InstrumentKind::audioClip;
  4389. c.samplePath = "ramp";
  4390. c.volume = 1.0f;
  4391. s.channels.push_back (c);
  4392. s.patterns.emplace_back();
  4393. lynx::Clip clip;
  4394. clip.kind = lynx::ClipKind::audio;
  4395. clip.channel = 0;
  4396. clip.start = lynx::ppq; // starts at beat 2
  4397. clip.length = 2 * lynx::ppq; // lasts 2 beats
  4398. clip.offset = lynx::ppq; // skips the first beat of the audio
  4399. s.playlist.push_back (clip);
  4400. // an empty pattern clip over the whole bar: the song ends at beat 4 (FL loops at the last clip's exact end)
  4401. s.playlist.push_back (lynx::Clip { 0, 1, 0, 4 * lynx::ppq, 0 });
  4402. s.mode = lynx::PlayMode::song;
  4403. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4404. lynx::EventBuffer ev;
  4405. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4406. CHECK (ev.size() == 2);
  4407. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4408. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4409. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4410. ev.clear();
  4411. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4412. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4413. ev.clear();
  4414. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4415. CHECK (ev.size() == 0);
  4416. // sample whose value = position in seconds: lets us check where playback started
  4417. auto provider = [] (const std::string&) {
  4418. auto d = std::make_shared<lynx::SampleData>();
  4419. d->sampleRate = 48000.0;
  4420. for (int i = 0; i < 48000 * 3; ++i)
  4421. d->left.push_back ((float) i / 48000.0f);
  4422. return std::shared_ptr<const lynx::SampleData> (d);
  4423. };
  4424. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4425. lynx::RackRenderer r;
  4426. r.prepare (48000.0, 512);
  4427. std::vector<float> l (256), rr (256);
  4428. // first block already playing mid-clip
  4429. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4430. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4431. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4432. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4433. double tick = 3.0 * lynx::ppq - 10 * tps;
  4434. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4435. for (int i = 0; i < 4; ++i)
  4436. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4437. CHECK (std::abs (l[255]) < 1e-6f);
  4438. }
  4439. TEST (remove_channel_fixes_notes_and_audio_clips)
  4440. {
  4441. lynx::Song s;
  4442. s.channels.resize (3);
  4443. s.patterns.resize (1);
  4444. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4445. lynx::Clip a;
  4446. a.kind = lynx::ClipKind::audio;
  4447. a.channel = 1;
  4448. a.length = 100;
  4449. lynx::Clip b = a;
  4450. b.channel = 2;
  4451. s.playlist = { a, b };
  4452. s.removeChannel (1);
  4453. CHECK (s.channels.size() == 2);
  4454. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4455. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4456. }
  4457. TEST (flp_import_audio_clips_with_offsets)
  4458. {
  4459. flp::Project p;
  4460. p.header.ppq = 96;
  4461. p.tempo = 120.0;
  4462. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4463. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4464. flp::Arrangement arr;
  4465. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4466. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4467. beats.index = 0;
  4468. beats.position = 192;
  4469. beats.length = 384; // 4 beats
  4470. beats.startOffset = 2.0f;
  4471. beats.endOffset = 6.0f;
  4472. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4473. ms.index = 1;
  4474. ms.startOffset = 500.0f;
  4475. ms.endOffset = 2500.0f;
  4476. arr.items = { beats, ms };
  4477. p.arrangements.push_back (arr);
  4478. lynx::FlpImportOptions options;
  4479. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4480. options.fileExists = [] (const std::string&) { return true; };
  4481. const auto s = lynx::importFlp (p, options);
  4482. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4483. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4484. CHECK (s.playlist.size() == 2);
  4485. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4486. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4487. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4488. CHECK (s.mode == lynx::PlayMode::song);
  4489. }
  4490. TEST (plugin_factories_create_reuse_and_fallback)
  4491. {
  4492. lynx::Song s;
  4493. s.mixer = lynx::Song::defaultMixer (2);
  4494. lynx::ChannelDef c;
  4495. c.instrument = lynx::InstrumentKind::plugin;
  4496. c.plugin.id = "VST3-Synth-123";
  4497. c.mixerTrack = 1;
  4498. s.channels.push_back (c);
  4499. lynx::ChannelDef missing = c;
  4500. missing.plugin.id = "VST3-Ausente-0";
  4501. s.channels.push_back (missing);
  4502. lynx::EffectSlot fx;
  4503. fx.kind = lynx::EffectKind::plugin;
  4504. fx.plugin.id = "VST3-Reverb-9";
  4505. s.mixer[1].effects.push_back (fx);
  4506. s.patterns.emplace_back();
  4507. int instrumentsMade = 0, effectsMade = 0;
  4508. lynx::RackState::Factories f;
  4509. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4510. if (ref.id != "VST3-Synth-123")
  4511. return nullptr;
  4512. ++instrumentsMade;
  4513. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4514. synth->prepare (48000.0);
  4515. return synth;
  4516. };
  4517. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4518. ++effectsMade;
  4519. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4520. eq->prepare (48000.0);
  4521. return eq;
  4522. };
  4523. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4524. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4525. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4526. // Default startup keeps the established reuse contract: saving/capturing
  4527. // different serialized bytes by itself must not recreate hosted instances.
  4528. auto storedOnly = s;
  4529. storedOnly.channels[0].plugin.state = "captured instrument without opt-in";
  4530. storedOnly.mixer[1].effects[0].plugin.state = "captured effect without opt-in";
  4531. auto ordinary = lynx::RackState::build (storedOnly, a.get(), 48000.0, f);
  4532. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4533. CHECK (ordinary->instruments[0] == a->instruments[0] && ordinary->effects[1][0] == a->effects[1][0]);
  4534. // Serialized state changes and undo preserve running instances. Remote control
  4535. // applies deliberate state edits in place through the engine, outside this factory.
  4536. s.channels[0].plugin.state = "abc";
  4537. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4538. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4539. s.mixer[1].effects[0].plugin.state = "new effect state";
  4540. auto changed = lynx::RackState::build (s, b.get(), 48000.0, f);
  4541. CHECK (instrumentsMade == 1 && effectsMade == 1 && changed->effects[1][0] == b->effects[1][0]);
  4542. s.channels[0].plugin.state.clear();
  4543. s.mixer[1].effects[0].plugin.state.clear();
  4544. auto undone = lynx::RackState::build (s, changed.get(), 48000.0, f);
  4545. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4546. CHECK (undone->instruments[0] == changed->instruments[0] && undone->effects[1][0] == changed->effects[1][0]);
  4547. auto capturedShape = *undone;
  4548. capturedShape.song.channels[0].plugin.state = s.channels[0].plugin.state = "captured instrument";
  4549. capturedShape.song.mixer[1].effects[0].plugin.state = s.mixer[1].effects[0].plugin.state = "captured effect";
  4550. auto captured = lynx::RackState::build (s, &capturedShape, 48000.0, f);
  4551. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4552. CHECK (captured->instruments[0] == undone->instruments[0] && captured->effects[1][0] == undone->effects[1][0]);
  4553. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4554. auto c2 = lynx::RackState::build (s, captured.get(), 48000.0, f);
  4555. CHECK (effectsMade == 2);
  4556. // rendering with a missing plugin and a plugin slot breaks nothing
  4557. lynx::RackRenderer r;
  4558. r.prepare (48000.0, 256);
  4559. lynx::LiveEventQueue live;
  4560. lynx::NoteEvent on;
  4561. on.channel = 0;
  4562. on.velocity = 1.0f;
  4563. live.push (on);
  4564. on.channel = 1;
  4565. live.push (on);
  4566. std::vector<float> l (256), rr (256);
  4567. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4568. float peak = 0.0f;
  4569. for (auto v : l)
  4570. {
  4571. CHECK (std::isfinite (v));
  4572. peak = std::max (peak, std::abs (v));
  4573. }
  4574. CHECK (peak > 0.0f);
  4575. }
  4576. TEST (remote_reset_replaces_only_requested_instances_and_default_reuse_stays_unchanged)
  4577. {
  4578. lynx::Song source;
  4579. source.mixer = lynx::Song::defaultMixer (2);
  4580. for (const auto* id : { "Synth A", "Synth B" })
  4581. {
  4582. lynx::ChannelDef channel;
  4583. channel.instrument = lynx::InstrumentKind::plugin;
  4584. channel.plugin.id = id;
  4585. channel.plugin.state = std::string ("saved ") + id;
  4586. source.channels.push_back (std::move (channel));
  4587. }
  4588. for (const auto* id : { "Effect A", "Effect B" })
  4589. {
  4590. lynx::EffectSlot effect;
  4591. effect.kind = lynx::EffectKind::plugin;
  4592. effect.plugin.id = id;
  4593. effect.plugin.state = std::string ("saved ") + id;
  4594. source.mixer[1].effects.push_back (std::move (effect));
  4595. }
  4596. int instrumentsMade = 0, effectsMade = 0;
  4597. std::string lastInstrumentState, lastEffectState;
  4598. lynx::RackState::Factories normal;
  4599. normal.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4600. ++instrumentsMade;
  4601. lastInstrumentState = ref.state;
  4602. return lynx::makeInstrument (lynx::InstrumentKind::synth);
  4603. };
  4604. normal.pluginEffects = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Effect> {
  4605. ++effectsMade;
  4606. lastEffectState = ref.state;
  4607. return lynx::makeEffect (lynx::EffectKind::eq);
  4608. };
  4609. const auto before = lynx::RackState::build (source, nullptr, 48000.0, normal);
  4610. CHECK (instrumentsMade == 2 && effectsMade == 2);
  4611. // Round 190az (24047e7a0): an ordinary build keeps a running instance for its own channel / slot (the instance
  4612. // tag) or for one saved with the same settings. A renewed tag with a cleared state is another device: it is
  4613. // created anew, with the default state, as a remote reset asks.
  4614. auto reset = source;
  4615. reset.channels[0].instance.renew();
  4616. reset.channels[0].plugin.state.clear();
  4617. reset.mixer[1].effects[0].instance.renew();
  4618. reset.mixer[1].effects[0].plugin.state.clear();
  4619. const auto ordinary = lynx::RackState::build (reset, before.get(), 48000.0, normal);
  4620. CHECK (instrumentsMade == 3 && effectsMade == 3);
  4621. CHECK (lastInstrumentState.empty() && lastEffectState.empty()); // factories received actual default-state requests
  4622. CHECK (ordinary->instruments[0] != before->instruments[0] && ordinary->effects[1][0] != before->effects[1][0]);
  4623. CHECK (ordinary->instruments[1] == before->instruments[1] && ordinary->effects[1][1] == before->effects[1][1]);
  4624. // With the same saved bytes the positional instance stays in an ordinary build; freshInstances replaces exactly
  4625. // the devices it names.
  4626. auto renewed = source;
  4627. renewed.channels[0].instance.renew();
  4628. renewed.mixer[1].effects[0].instance.renew();
  4629. const auto kept = lynx::RackState::build (renewed, before.get(), 48000.0, normal);
  4630. CHECK (instrumentsMade == 3 && effectsMade == 3);
  4631. CHECK (kept->instruments[0] == before->instruments[0] && kept->effects[1][0] == before->effects[1][0]);
  4632. auto requested = normal;
  4633. requested.freshInstances = { renewed.channels[0].instance.id, renewed.mixer[1].effects[0].instance.id };
  4634. const auto replaced = lynx::RackState::build (renewed, before.get(), 48000.0, requested);
  4635. CHECK (instrumentsMade == 4 && effectsMade == 4);
  4636. CHECK (lastInstrumentState == "saved Synth A" && lastEffectState == "saved Effect A");
  4637. CHECK (replaced->instruments[0] != before->instruments[0] && replaced->effects[1][0] != before->effects[1][0]);
  4638. CHECK (replaced->instruments[1] == before->instruments[1] && replaced->effects[1][1] == before->effects[1][1]);
  4639. // The policy belongs to that build only: later ordinary edits reuse the newly created instances.
  4640. const auto later = lynx::RackState::build (renewed, replaced.get(), 48000.0, normal);
  4641. CHECK (instrumentsMade == 4 && effectsMade == 4);
  4642. CHECK (later->instruments[0] == replaced->instruments[0] && later->effects[1][0] == replaced->effects[1][0]);
  4643. }
  4644. TEST (remote_history_normalized_restores_preserve_lineage_without_crossing_producer_edits)
  4645. {
  4646. struct Entry { std::string song; bool remote = false; std::string deviceHash; };
  4647. std::vector<Entry> undo { { "A", true, "B" }, { "B", true, "C" } }, redo;
  4648. std::string live = "C";
  4649. const auto restore = [&] (auto& from, auto& to, const std::string& normalized) {
  4650. CHECK (! from.empty() && from.back().deviceHash == live);
  4651. const auto savedTarget = from.back().song;
  4652. // A real engine inverse stores captured live bytes, even when its raw model blob was older.
  4653. to.push_back ({ live, true, {} });
  4654. from.pop_back();
  4655. live = normalized;
  4656. lynx::remote_history::acceptRestoredState (to.back(), from.empty() ? nullptr : &from.back(), savedTarget, live);
  4657. };
  4658. restore (undo, redo, "B-normalized");
  4659. CHECK (undo.back().deviceHash == live && redo.back().deviceHash == live);
  4660. CHECK (redo.back().song == "C");
  4661. restore (undo, redo, "A-normalized");
  4662. CHECK (undo.empty() && redo.back().song == "B-normalized");
  4663. restore (redo, undo, "B-normalized-again");
  4664. CHECK (redo.back().deviceHash == live); // the next redo adopts normalization through the same proven lineage
  4665. restore (redo, undo, "C-normalized");
  4666. CHECK (redo.empty() && undo.size() == 2);
  4667. // A producer changed B to D before the following remote edit. Undoing that edit may normalize D, but cannot
  4668. // promote the older B fence: doing so would silently authorize undo across the producer's opaque edit.
  4669. undo = { { "A", true, "B" }, { "D", true, "E" } };
  4670. redo.clear();
  4671. live = "E";
  4672. restore (undo, redo, "D-normalized");
  4673. CHECK (redo.back().deviceHash == live && undo.back().deviceHash == "B");
  4674. CHECK (undo.back().deviceHash != live); // the next remote-owned undo remains refused
  4675. // An ordinary producer entry never acquires a remote fence even if its bytes happen to match the target.
  4676. Entry ordinary { "A", false, "B" }, inverse { "C", true, {} };
  4677. lynx::remote_history::acceptRestoredState (inverse, &ordinary, std::string ("B"), std::string ("B-normalized"));
  4678. CHECK (ordinary.deviceHash == "B" && inverse.deviceHash == "B-normalized");
  4679. }
  4680. TEST (remote_capture_mirror_preserves_projected_shape_and_updates_only_matching_hosted_bytes)
  4681. {
  4682. lynx::Song captured;
  4683. lynx::ChannelDef hosted;
  4684. hosted.instrument = lynx::InstrumentKind::plugin;
  4685. hosted.plugin.id = "Synth";
  4686. hosted.plugin.state = "saved";
  4687. captured.channels = { hosted, hosted };
  4688. captured.channels[1].instance.renew();
  4689. lynx::ChannelDef patcher;
  4690. patcher.instrument = lynx::InstrumentKind::patcher;
  4691. lynx::PatchNode nested;
  4692. nested.generator = true;
  4693. nested.channel = hosted;
  4694. patcher.patcher.nodes.push_back (nested);
  4695. captured.channels.push_back (patcher);
  4696. captured.mixer = lynx::Song::defaultMixer (2);
  4697. lynx::EffectSlot effect;
  4698. effect.kind = lynx::EffectKind::plugin;
  4699. effect.plugin.id = "Effect";
  4700. effect.plugin.state = "saved";
  4701. captured.mixer[1].effects.push_back (effect);
  4702. auto published = captured;
  4703. published.channels[0].muted = true; // an insert stand-in changes routing/muting in the published projection
  4704. published.channels[1].instrument = lynx::InstrumentKind::sampler;
  4705. published.channels[1].samplePath = "stem.wav";
  4706. published.channels.push_back (lynx::ChannelDef()); // generated stem helper channels must survive
  4707. published.mixer[1].effects[0].enabled = false;
  4708. captured.channels[0].plugin.state = "captured synth";
  4709. captured.channels[1].plugin.state = "unavailable synth";
  4710. captured.channels[2].patcher.nodes[0].channel.plugin.state = "captured nested synth";
  4711. captured.mixer[1].effects[0].plugin.state = "captured effect";
  4712. lynx::remote_history::mirrorCapturedHostedStates (captured, published);
  4713. CHECK (published.channels.size() == 4 && published.channels[0].muted);
  4714. CHECK (published.channels[0].plugin.state == "captured synth");
  4715. CHECK (published.channels[1].instrument == lynx::InstrumentKind::sampler && published.channels[1].samplePath == "stem.wav");
  4716. CHECK (published.channels[1].plugin.state == "saved"); // a projected replacement is not the captured hosted device
  4717. CHECK (published.channels[2].patcher.nodes[0].channel.plugin.state == "captured nested synth");
  4718. CHECK (! published.mixer[1].effects[0].enabled && published.mixer[1].effects[0].plugin.state == "captured effect");
  4719. published.channels[0].instance.renew();
  4720. captured.channels[0].plugin.state = "different device";
  4721. lynx::remote_history::mirrorCapturedHostedStates (captured, published);
  4722. CHECK (published.channels[0].plugin.state == "captured synth");
  4723. }
  4724. TEST (export_song_adds_silent_tail_and_never_restarts)
  4725. {
  4726. auto s = lynx::Song::makeDefault();
  4727. s.playlist.clear();
  4728. std::int64_t length = 0;
  4729. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4730. CHECK (length == lynx::ppq * 4);
  4731. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4732. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4733. // no new notes after the song ends, only silence until the end of the tail
  4734. lynx::EventBuffer events;
  4735. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4736. int onsAfterEnd = 0, onsBefore = 0;
  4737. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4738. {
  4739. events.clear();
  4740. lynx::scheduleSong (pat, tick, 512, tps, events);
  4741. for (const auto& e : events)
  4742. if (e.type == lynx::NoteEvent::Type::on)
  4743. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4744. }
  4745. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4746. CHECK (onsAfterEnd == 0);
  4747. // with Playlist: the end is the last clip's, without rounding
  4748. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4749. auto song = lynx::makeExportSong (s, true, 0, length);
  4750. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4751. song = lynx::makeExportSong (s, false, 0, length);
  4752. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4753. }
  4754. TEST (note_edit_selection_operations)
  4755. {
  4756. namespace ne = lynx::noteedit;
  4757. lynx::Pattern p;
  4758. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4759. // sorting keeps the indices in step
  4760. p.notes[0].start = 1200;
  4761. std::vector<int> tracked { 0, 2 };
  4762. ne::sortTracking (p, tracked);
  4763. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4764. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4765. // group move respects the limits
  4766. std::int64_t dt = -5000;
  4767. int dk = 100;
  4768. ne::move (p, { 0, 1 }, dt, dk);
  4769. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4770. // duplicate: right after the selection, rounded to the grid
  4771. lynx::Pattern q;
  4772. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4773. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4774. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4775. // paste onto another channel with an offset
  4776. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4777. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4778. // quantize rounds to the nearest
  4779. q.notes[0].start = 130;
  4780. q.notes[1].start = 100;
  4781. ne::quantize (q, { 0, 1 }, 240);
  4782. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4783. // remove several (order and duplicates don't matter)
  4784. const auto before = q.notes.size();
  4785. ne::remove (q, { 1, 0, 1 });
  4786. CHECK (q.notes.size() == before - 2);
  4787. }
  4788. TEST (note_edit_quick_operations)
  4789. {
  4790. namespace ne = lynx::noteedit;
  4791. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4792. // split: only strictly inside; the tail is appended and does not slide again
  4793. lynx::Pattern p;
  4794. p.notes = { { 0, 4 * step, 0, 60 } };
  4795. p.notes[0].slide = true;
  4796. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4797. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4798. && ! p.notes[1].slide);
  4799. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4800. lynx::Pattern c;
  4801. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4802. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4803. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4804. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4805. && c.notes[3].length == step);
  4806. // join: touching and overlapping notes of the same key and channel merge; others stay
  4807. lynx::Pattern j;
  4808. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4809. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4810. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4811. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4812. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4813. lynx::Pattern l;
  4814. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4815. ne::legato (l, { 0, 1, 2, 3, 4 });
  4816. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4817. && l.notes[4].length == 10);
  4818. // shrink to grid and full quantise (start and end)
  4819. ne::shrinkToGrid (l, { 3 }, step / 2);
  4820. CHECK (l.notes[3].length == step / 2);
  4821. lynx::Pattern q;
  4822. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4823. ne::quantizeFull (q, { 0, 1 }, step);
  4824. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4825. // rotate inside the bar, wrapping
  4826. lynx::Pattern r;
  4827. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4828. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4829. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4830. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4831. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4832. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4833. ne::rotate (r, { 2 }, step, bar, -2);
  4834. CHECK (r.notes[2].start == 2 * bar - step);
  4835. }
  4836. TEST (note_edit_grid_and_time_text)
  4837. {
  4838. namespace ne = lynx::noteedit;
  4839. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4840. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4841. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4842. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4843. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4844. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4845. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4846. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4847. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4848. CHECK (ne::formatTime (0, lynx::Meter (4, 4)) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, lynx::Meter (4, 4)) == "2:6:7");
  4849. CHECK (ne::formatTime (step * 2, lynx::Meter (4, 4), true) == "0:2:0");
  4850. std::int64_t t = 0;
  4851. CHECK (ne::parseTime ("2:6:7", lynx::Meter (4, 4), t) && t == bar + 5 * step + 7);
  4852. CHECK (ne::parseTime ("3", lynx::Meter (4, 4), t) && t == 2 * bar);
  4853. CHECK (ne::parseTime ("0:2:0", lynx::Meter (4, 4), t, true) && t == 2 * step);
  4854. 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)
  4855. && ! ne::parseTime ("a", lynx::Meter (4, 4), t) && ! ne::parseTime ("", lynx::Meter (4, 4), t));
  4856. 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
  4857. }
  4858. TEST (playlist_clip_edges_and_time_selection)
  4859. {
  4860. namespace ne = lynx::noteedit;
  4861. const std::int64_t beat = lynx::ppq;
  4862. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4863. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4864. ne::trimStart (c, 5 * beat, beat);
  4865. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4866. ne::trimStart (c, 0, beat);
  4867. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4868. ne::trimStart (c, 100 * beat, beat);
  4869. CHECK (c.length == beat && c.end() == 8 * beat);
  4870. // slip moves the content, not the clip
  4871. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4872. ne::slip (s, beat / 2);
  4873. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4874. ne::slip (s, beat);
  4875. CHECK (s.offset == 0);
  4876. ne::slip (s, -3 * beat);
  4877. CHECK (s.offset == 3 * beat);
  4878. // insert / delete time across all tracks, markers included
  4879. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4880. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4881. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4882. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4883. && markers[1].tick == beat / 2);
  4884. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4885. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4886. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4887. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4888. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4889. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4890. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4891. CHECK (d.size() == 5);
  4892. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4893. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4894. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4895. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4896. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4897. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4898. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4899. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4900. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4901. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4902. }
  4903. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4904. {
  4905. auto song = lynx::Song::makeDefault();
  4906. const auto loop = song.patterns[0].effectiveLength (lynx::Meter (4, 4)); // 1 bar = 3840
  4907. lynx::NoteRecorder r;
  4908. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4909. CHECK (r.takeFinished().empty());
  4910. r.begin (loop);
  4911. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4912. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4913. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4914. r.end (song, 5240.0);
  4915. const auto notes = r.takeFinished();
  4916. CHECK (notes.size() == 2);
  4917. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4918. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4919. // song mode: position inside the current pattern's clip (with offset)
  4920. song.mode = lynx::PlayMode::song;
  4921. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4922. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4923. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4924. }
  4925. TEST (automation_curve_and_playback)
  4926. {
  4927. lynx::Automation a;
  4928. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4929. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4930. CHECK (a.length() == 1920);
  4931. a.points[0].tension = 1.0f; // curve: starts slow
  4932. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4933. // automated Master: silence in the first bar, sound in the second, and the last value
  4934. // keeps applying after the automation clip ends
  4935. auto song = lynx::Song::makeDefault();
  4936. song.bpm = 120.0;
  4937. song.mode = lynx::PlayMode::song;
  4938. const auto bar = (std::int64_t) lynx::ppq * 4;
  4939. for (int b = 0; b < 3; ++b)
  4940. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4941. lynx::Automation master;
  4942. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4943. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4944. song.automations.push_back (master);
  4945. lynx::Clip ac;
  4946. ac.kind = lynx::ClipKind::automation;
  4947. ac.automation = 0;
  4948. ac.track = 1;
  4949. ac.length = bar + bar / 2;
  4950. song.playlist.push_back (ac);
  4951. const double sr = 48000.0;
  4952. auto state = lynx::RackState::build (song, nullptr, sr);
  4953. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4954. lynx::RackRenderer r;
  4955. r.prepare (sr, 512);
  4956. std::vector<float> l (512), rr (512);
  4957. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4958. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4959. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4960. {
  4961. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4962. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4963. for (auto v : l)
  4964. peak = std::max (peak, std::abs (v));
  4965. }
  4966. CHECK (peakBar1 < 1.0e-6f);
  4967. CHECK (peakBar2 > 0.05f);
  4968. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4969. // target removed: automation disappears from the tables
  4970. song.removeChannel (0);
  4971. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4972. song.removeChannel (0);
  4973. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4974. song.removeAutomation (0);
  4975. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4976. }
  4977. TEST (mixer_sends_between_inserts)
  4978. {
  4979. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4980. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4981. // send at 50% and muted destination
  4982. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4983. 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);
  4984. // going to the Master and to 3: the signal doubles
  4985. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4986. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4987. 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);
  4988. // solo on 1 lets the path to the Master (3) sound
  4989. 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);
  4990. // order and cycles
  4991. auto song = lynx::Song::makeDefault();
  4992. song.setRoute (4, 2, true);
  4993. song.setRoute (2, 1, true);
  4994. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4995. song.setRoute (1, 4, true); // ignored: would create a cycle
  4996. CHECK (! song.routes (1, 4));
  4997. const auto order = song.mixerOrder();
  4998. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  4999. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  5000. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  5001. song.setRoute (4, 2, false);
  5002. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  5003. // route levels (FL's route knobs): to inserts and to Master, clamped to the fader range; none without a route
  5004. song.setRoute (3, 5, true);
  5005. song.setRouteLevel (3, 5, 0.5f);
  5006. song.setRouteLevel (3, 0, 1.4f);
  5007. CHECK (song.routeLevel (3, 5) == 0.5f && song.routeLevel (3, 0) == 1.4f && song.mixer[3].masterLevel == 1.4f);
  5008. song.setRouteLevel (3, 0, 9.0f);
  5009. CHECK (song.routeLevel (3, 0) == lynx::maxFaderGain);
  5010. song.setRouteLevel (3, 6, 0.5f); // no such route: ignored
  5011. CHECK (! song.routes (3, 6) && song.routeLevel (3, 6) == 0.0f);
  5012. song.setRoute (3, 0, false);
  5013. CHECK (song.routeLevel (3, 0) == 0.0f);
  5014. // the route to Master's level scales what insert 1 sends there (0.5 + 0.5·0.25)
  5015. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRouteLevel (1, 0, 0.25f); }) - 0.625f) < 1e-4f);
  5016. }
  5017. namespace
  5018. {
  5019. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  5020. class LatentEffect final : public lynx::Effect
  5021. {
  5022. public:
  5023. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  5024. void prepare (double) noexcept override {}
  5025. void reset() noexcept override {}
  5026. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  5027. int latencySamples() const noexcept override { return latency; }
  5028. private:
  5029. int latency;
  5030. lynx::DelayLine line;
  5031. };
  5032. } // namespace
  5033. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  5034. {
  5035. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  5036. lynx::DelayLine a (37), b (37);
  5037. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  5038. for (int i = 0; i < 300; ++i)
  5039. {
  5040. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  5041. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  5042. }
  5043. for (int start = 0; start < 300; start += 100)
  5044. {
  5045. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  5046. for (int i = start; i < start + 100; ++i)
  5047. {
  5048. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  5049. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  5050. }
  5051. a.process (xl.data() + start, xr.data() + start, 100);
  5052. }
  5053. float worst = 0.0f;
  5054. for (int i = 0; i < 300; ++i)
  5055. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  5056. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  5057. CHECK (worst < 1e-6f);
  5058. // silenced source: the tail still in the line comes out, then nothing remains
  5059. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  5060. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  5061. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  5062. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  5063. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  5064. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  5065. }
  5066. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  5067. {
  5068. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  5069. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  5070. fx->prepare (48000.0);
  5071. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5072. slot.params[2] = 0.0f; // 10 ms release
  5073. std::vector<float> l (48000), r (48000);
  5074. std::uint32_t seed = 1;
  5075. for (int i = 0; i < 48000; ++i)
  5076. {
  5077. seed = seed * 1664525u + 1013904223u;
  5078. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  5079. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  5080. r[(std::size_t) i] = -noise;
  5081. }
  5082. for (int i = 0; i < 48000; i += 333)
  5083. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  5084. float peak = 0.0f, quiet = 0.0f;
  5085. for (int i = 0; i < 48000; ++i)
  5086. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  5087. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  5088. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  5089. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  5090. CHECK (peak <= ceiling + 1e-5f);
  5091. CHECK (quiet > 0.03f);
  5092. }
  5093. TEST (pdc_aligns_paths_through_latent_effects)
  5094. {
  5095. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  5096. lynx::Song s;
  5097. s.mixer = lynx::Song::defaultMixer (4);
  5098. s.channels.resize (3);
  5099. s.channels[0].mixerTrack = 1;
  5100. s.channels[1].mixerTrack = 2;
  5101. s.channels[2].mixerTrack = 3;
  5102. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  5103. s.setRoute (1, 3, true);
  5104. s.setRoute (1, 0, false);
  5105. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  5106. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  5107. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  5108. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  5109. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  5110. // bypass zeroes the slot's latency
  5111. s.mixer[1].effects[0].enabled = false;
  5112. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  5113. // rendered: two impulses played together come out together
  5114. lynx::Song r;
  5115. r.mixer = lynx::Song::defaultMixer (2);
  5116. for (int i = 0; i < 2; ++i)
  5117. {
  5118. lynx::ChannelDef c;
  5119. c.instrument = lynx::InstrumentKind::sampler;
  5120. c.samplePath = "impulso";
  5121. c.volume = 1.0f;
  5122. c.mixerTrack = i + 1;
  5123. r.channels.push_back (c);
  5124. }
  5125. r.patterns.emplace_back();
  5126. lynx::EffectSlot fx;
  5127. fx.kind = lynx::EffectKind::plugin;
  5128. fx.plugin.id = "latente";
  5129. r.mixer[1].effects.push_back (fx);
  5130. lynx::RackState::Factories f;
  5131. f.samples = [] (const std::string&) {
  5132. auto d = std::make_shared<lynx::SampleData>();
  5133. d->left.assign (64, 0.0f);
  5134. d->left[0] = 1.0f;
  5135. return std::shared_ptr<const lynx::SampleData> (d);
  5136. };
  5137. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  5138. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  5139. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  5140. lynx::RackRenderer renderer;
  5141. renderer.prepare (48000.0, 256);
  5142. lynx::LiveEventQueue live;
  5143. for (int ch = 0; ch < 2; ++ch)
  5144. {
  5145. lynx::NoteEvent on;
  5146. on.channel = ch;
  5147. on.key = 60;
  5148. on.velocity = 1.0f;
  5149. live.push (on);
  5150. }
  5151. std::vector<float> l (256), rr (256);
  5152. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  5153. int first = -1;
  5154. for (int i = 0; i < 256 && first < 0; ++i)
  5155. if (std::abs (l[(std::size_t) i]) > 0.01f)
  5156. first = i;
  5157. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  5158. // editing without changing latencies reuses the lines
  5159. r.channels[0].volume = 0.5f;
  5160. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  5161. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  5162. }
  5163. TEST (mixer_mute_fades_instead_of_clicking)
  5164. {
  5165. lynx::Song s;
  5166. s.mixer = lynx::Song::defaultMixer (2);
  5167. lynx::ChannelDef c;
  5168. c.instrument = lynx::InstrumentKind::sampler;
  5169. c.samplePath = "dc";
  5170. c.volume = 1.0f;
  5171. c.mixerTrack = 1;
  5172. s.channels.push_back (c);
  5173. s.patterns.emplace_back();
  5174. auto provider = [] (const std::string&) {
  5175. auto d = std::make_shared<lynx::SampleData>();
  5176. d->left.assign (48000, 0.5f);
  5177. return std::shared_ptr<const lynx::SampleData> (d);
  5178. };
  5179. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5180. s.mixer[1].muted = true;
  5181. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  5182. lynx::RackRenderer r;
  5183. r.prepare (48000.0, 256);
  5184. lynx::LiveEventQueue live;
  5185. lynx::NoteEvent note;
  5186. note.velocity = 1.0f;
  5187. live.push (note);
  5188. std::vector<float> l (256), rr (256);
  5189. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  5190. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  5191. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5192. // the fader glides down by 0.001 per sample, as FL's (REA_rodada85 §3): 1000 samples from 1 to 0
  5193. 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);
  5194. for (int b = 0; b < 3; ++b)
  5195. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5196. CHECK (std::abs (l[255]) < 1e-6f); // down to zero after 1000 samples
  5197. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  5198. CHECK (std::abs (l[0]) < 1e-9f);
  5199. }
  5200. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  5201. {
  5202. lynx::Dither d (16);
  5203. std::vector<float> x (4800);
  5204. for (std::size_t i = 0; i < x.size(); ++i)
  5205. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  5206. auto y = x;
  5207. d.process (y.data(), (int) y.size(), 0);
  5208. double errorSum = 0.0;
  5209. bool onGrid = true;
  5210. for (std::size_t i = 0; i < x.size(); ++i)
  5211. {
  5212. const auto steps = (y[i] - d.writerBias()) * 32768.0f; // half an LSB above the grid point (round 160)
  5213. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  5214. errorSum += std::abs (y[i] - x[i]);
  5215. }
  5216. CHECK (onGrid);
  5217. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  5218. }
  5219. TEST (parallel_rendering_matches_sequential)
  5220. {
  5221. // 32 channels (worth parallelizing), inserts with chained sends
  5222. auto song = lynx::Song::makeDefault();
  5223. song.mixer = lynx::Song::defaultMixer (16);
  5224. for (int i = 5; i < 32; ++i)
  5225. {
  5226. lynx::ChannelDef c;
  5227. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  5228. c.mixerTrack = 1 + i % 12;
  5229. c.volume = 0.3f;
  5230. song.channels.push_back (c);
  5231. for (int step = i % 4; step < 16; step += 3)
  5232. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  5233. }
  5234. song.patterns[0].sortNotes();
  5235. song.setRoute (3, 7, true, 0.6f);
  5236. song.setRoute (7, 9, true, 0.5f);
  5237. song.setRoute (2, 9, true);
  5238. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5239. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  5240. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  5241. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  5242. CHECK (parState->graphOk); // round 165i: the task graph runs it (channels and inserts by readiness)
  5243. lynx::WorkerPool pool (3);
  5244. lynx::RackRenderer seq, par;
  5245. seq.prepare (48000.0, 512);
  5246. par.prepare (48000.0, 512);
  5247. par.setWorkerPool (&pool);
  5248. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  5249. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5250. bool same = true;
  5251. float peak = 0.0f;
  5252. for (int block = 0; block < 200; ++block)
  5253. {
  5254. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  5255. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  5256. same = same && l1 == l2 && r1 == r2;
  5257. for (auto v : l1)
  5258. peak = std::max (peak, std::abs (v));
  5259. }
  5260. CHECK (same);
  5261. CHECK (peak > 0.05f);
  5262. // the pool handles many small rounds in a row
  5263. std::atomic<int> sum { 0 };
  5264. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  5265. for (int round = 0; round < 2000; ++round)
  5266. pool.parallelFor (5, add);
  5267. CHECK (sum.load() == 2000 * 15);
  5268. }
  5269. TEST (rea165i_task_graph_matches_layers_with_sidechain_and_cycle)
  5270. {
  5271. // the task graph against the layers (LYNX_TASKGRAPH off is the same as a pool-less render): sends, a sidechain,
  5272. // a chain three inserts deep, channels on the Master; a routing cycle keeps the layers
  5273. auto song = lynx::Song::makeDefault();
  5274. song.mixer = lynx::Song::defaultMixer (16);
  5275. for (int i = 5; i < 40; ++i)
  5276. {
  5277. lynx::ChannelDef c;
  5278. c.instrument = i % 3 == 0 ? lynx::InstrumentKind::hat : lynx::InstrumentKind::synth;
  5279. c.mixerTrack = i % 7 == 0 ? 0 : 1 + i % 13;
  5280. c.volume = 0.25f;
  5281. song.channels.push_back (c);
  5282. for (int step = i % 5; step < 16; step += 2)
  5283. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 150, i, 40 + i, 0.6f });
  5284. }
  5285. song.patterns[0].sortNotes();
  5286. song.setRoute (3, 7, true, 0.6f);
  5287. song.setRoute (7, 9, true, 0.5f);
  5288. song.setRoute (9, 11, true, 0.5f);
  5289. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5290. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5291. comp.sidechain = 4;
  5292. song.mixer[11].effects.push_back (comp);
  5293. auto render = [] (const lynx::Song& s, bool pool, bool& graph) {
  5294. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  5295. graph = state->graphOk;
  5296. lynx::WorkerPool workers (4);
  5297. lynx::RackRenderer r;
  5298. r.prepare (48000.0, 512);
  5299. if (pool)
  5300. r.setWorkerPool (&workers);
  5301. std::vector<float> out, l (384), rr (384);
  5302. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5303. for (int block = 0; block < 150; ++block)
  5304. {
  5305. r.process (state.get(), true, block * 384 * tps, 384, l.data(), rr.data());
  5306. out.insert (out.end(), l.begin(), l.end());
  5307. out.insert (out.end(), rr.begin(), rr.end());
  5308. }
  5309. return out;
  5310. };
  5311. bool graph = false, unused = false;
  5312. const auto layered = render (song, false, unused);
  5313. const auto parallel = render (song, true, graph);
  5314. CHECK (graph);
  5315. CHECK (layered == parallel);
  5316. CHECK (*std::max_element (layered.begin(), layered.end()) > 0.02f);
  5317. 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)
  5318. const auto cycleLayered = render (song, false, unused);
  5319. const auto cycleParallel = render (song, true, graph);
  5320. CHECK (! graph);
  5321. CHECK (cycleLayered == cycleParallel);
  5322. }
  5323. TEST (rea165i_test_profile_and_test_mode_parsing)
  5324. {
  5325. using namespace lynx::testprofile;
  5326. CHECK (! profileDirFrom (nullptr));
  5327. CHECK (! profileDirFrom (""));
  5328. CHECK (! profileDirFrom ("relative/dir")); // only absolute folders
  5329. CHECK (! profileDirFrom ("C:relative"));
  5330. CHECK (profileDirFrom ("/tmp/lynx-profile/") == std::string ("/tmp/lynx-profile"));
  5331. CHECK (profileDirFrom (" \"C:\\Users\\me\\bench profile\\\" ") == std::string ("C:\\Users\\me\\bench profile"));
  5332. CHECK (profileDirFrom ("D:/x") == std::string ("D:/x"));
  5333. CHECK (profileDirFrom ("C:\\") == std::string ("C:\\")); // a drive root keeps its separator
  5334. CHECK (! testModeFrom (nullptr));
  5335. CHECK (! testModeFrom (""));
  5336. CHECK (! testModeFrom ("0"));
  5337. CHECK (! testModeFrom (" Off "));
  5338. CHECK (! testModeFrom ("false"));
  5339. CHECK (testModeFrom ("1"));
  5340. CHECK (testModeFrom ("yes"));
  5341. }
  5342. TEST (rea165i_keep_float_mode_restores_ftz_after_foreign_code)
  5343. {
  5344. // a hosted plugin that turns flush-to-zero off (or changes the rounding) no longer leaks it into LYNX's DSP
  5345. const lynx::ScopedFlushDenormals ftz;
  5346. const auto before = _mm_getcsr();
  5347. {
  5348. const lynx::KeepFloatMode keep;
  5349. _mm_setcsr ((before & ~0x8040u) | 0x6000u); // what a plugin might leave: no FTZ/DAZ, round toward zero
  5350. }
  5351. CHECK (_mm_getcsr() == before);
  5352. volatile float tiny = 1.0e-38f;
  5353. volatile float smaller = tiny * 1.0e-3f; // denormal without FTZ
  5354. CHECK (smaller == 0.0f);
  5355. }
  5356. TEST (arrangements_switch_and_keep_clip_indices)
  5357. {
  5358. auto s = lynx::Song::makeDefault();
  5359. s.patterns.emplace_back().name = "B";
  5360. s.playlist = { { 1, 0, 0, 960, 0 } };
  5361. s.loopEnd = 960;
  5362. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  5363. const auto second = s.addArrangement ("Versão curta", true);
  5364. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  5365. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  5366. s.loopEnd = 0;
  5367. s.selectArrangement (0);
  5368. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  5369. // removing pattern 0 fixes the clips stored in the other arrangement
  5370. s.removePattern (0);
  5371. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  5372. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  5373. s.removeArrangement (0);
  5374. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  5375. }
  5376. namespace
  5377. {
  5378. // Test effect that inverts the signal and counts how many times it processed.
  5379. class CountingInverter final : public lynx::Effect
  5380. {
  5381. public:
  5382. void prepare (double) noexcept override {}
  5383. void reset() noexcept override {}
  5384. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  5385. {
  5386. ++calls;
  5387. for (int i = 0; i < n; ++i)
  5388. {
  5389. l[i] = -l[i];
  5390. r[i] = -r[i];
  5391. }
  5392. }
  5393. int calls = 0;
  5394. };
  5395. } // namespace
  5396. TEST (keyboard_controller_outputs_key_value)
  5397. {
  5398. std::vector<std::uint8_t> blob (554, 0);
  5399. const std::uint32_t version = 3;
  5400. std::memcpy (blob.data(), &version, 4);
  5401. const float key60 = 0.7f, off = 0.2f;
  5402. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  5403. std::memcpy (blob.data() + 548, &off, 4);
  5404. blob[552] = 1; // auto release
  5405. lynx::ChannelDef c;
  5406. c.instrument = lynx::InstrumentKind::controller;
  5407. c.controllerState = blob;
  5408. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  5409. inst->prepare (48000.0);
  5410. inst->configure (c, 120.0);
  5411. std::vector<float> l (64), r (64);
  5412. inst->noteOn (60, 1.0f);
  5413. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  5414. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  5415. inst->noteOff (60);
  5416. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  5417. }
  5418. TEST (envelope_controller_ducks_on_each_note)
  5419. {
  5420. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  5421. lynx::ControllerArticulator a;
  5422. a.envOn = true;
  5423. a.base = 0.05f;
  5424. a.envLevel = 1.0f;
  5425. a.count = 3;
  5426. a.time = { 0.0f, 0.2f, 0.3f };
  5427. a.level = { 0.0f, 0.75f, 1.0f };
  5428. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  5429. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  5430. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  5431. a.sustain = 1;
  5432. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  5433. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  5434. a.bipolar = true;
  5435. a.base = 0.5f;
  5436. a.sustain = -1;
  5437. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  5438. }
  5439. TEST (peak_controller_drives_live_automation)
  5440. {
  5441. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  5442. auto render = [] (bool linked) {
  5443. lynx::Song s;
  5444. s.mixer = lynx::Song::defaultMixer (2);
  5445. for (int t = 1; t <= 2; ++t)
  5446. {
  5447. lynx::ChannelDef c;
  5448. c.instrument = lynx::InstrumentKind::sampler;
  5449. c.samplePath = "dc";
  5450. c.volume = 1.0f;
  5451. c.mixerTrack = t;
  5452. s.channels.push_back (c);
  5453. }
  5454. s.patterns.emplace_back();
  5455. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  5456. peak.params[0] = 1.0f; // base 1
  5457. peak.params[1] = 0.0f; // amount −1: 1 − peak
  5458. peak.params[2] = 0.5f; // no tension
  5459. s.mixer[1].effects.push_back (peak);
  5460. if (linked)
  5461. {
  5462. lynx::Automation a;
  5463. a.name = "Peak";
  5464. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  5465. a.source = { 1, 0, 0 };
  5466. s.automations.push_back (a);
  5467. }
  5468. lynx::RackState::Factories f;
  5469. f.samples = [] (const std::string&) {
  5470. auto d = std::make_shared<lynx::SampleData>();
  5471. d->left.assign (48000, 0.5f);
  5472. return std::shared_ptr<const lynx::SampleData> (d);
  5473. };
  5474. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5475. lynx::RackRenderer r;
  5476. r.prepare (48000.0, 512);
  5477. lynx::LiveEventQueue live;
  5478. for (int ch = 0; ch < 2; ++ch)
  5479. {
  5480. lynx::NoteEvent on;
  5481. on.channel = ch;
  5482. on.velocity = 1.0f;
  5483. live.push (on);
  5484. }
  5485. std::vector<float> l (4800), rr (4800);
  5486. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  5487. return l[4000];
  5488. };
  5489. const auto free = render (false), ducked = render (true);
  5490. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  5491. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  5492. }
  5493. TEST (effect_slot_dry_wet_and_smart_disable)
  5494. {
  5495. lynx::Song s;
  5496. s.mixer = lynx::Song::defaultMixer (1);
  5497. lynx::ChannelDef c;
  5498. c.instrument = lynx::InstrumentKind::sampler;
  5499. c.samplePath = "dc";
  5500. c.volume = 1.0f;
  5501. c.mixerTrack = 1;
  5502. s.channels.push_back (c);
  5503. s.patterns.emplace_back();
  5504. lynx::EffectSlot fx;
  5505. fx.kind = lynx::EffectKind::plugin;
  5506. fx.plugin.id = "inversor";
  5507. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  5508. s.mixer[1].effects.push_back (fx);
  5509. auto inverter = std::make_shared<CountingInverter>();
  5510. lynx::RackState::Factories f;
  5511. f.samples = [] (const std::string&) {
  5512. auto d = std::make_shared<lynx::SampleData>();
  5513. d->left.assign (4800, 0.5f);
  5514. return std::shared_ptr<const lynx::SampleData> (d);
  5515. };
  5516. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  5517. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  5518. lynx::RackRenderer r;
  5519. r.prepare (48000.0, 512);
  5520. lynx::LiveEventQueue live;
  5521. lynx::NoteEvent on;
  5522. on.velocity = 1.0f;
  5523. live.push (on);
  5524. std::vector<float> l (480), rr (480);
  5525. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5526. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  5527. // after the sample ends: 4 s of silence and the plugin stops being called
  5528. for (int i = 0; i < 100 * 6; ++i) // 6 s
  5529. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5530. const auto callsAfterSilence = inverter->calls;
  5531. for (int i = 0; i < 100; ++i)
  5532. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  5533. CHECK (inverter->calls == callsAfterSilence);
  5534. CHECK (callsAfterSilence < 100 * 6);
  5535. // sound again: processing resumes in the same block
  5536. live.push (on);
  5537. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  5538. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  5539. }
  5540. TEST (swing_delays_offbeat_sixteenths)
  5541. {
  5542. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  5543. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  5544. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  5545. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  5546. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  5547. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  5548. lynx::Pattern p;
  5549. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  5550. lynx::EventBuffer ev;
  5551. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5552. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 12000, tps, ev, 1.0f);
  5553. int onOffset = -1;
  5554. for (auto& e : ev)
  5555. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  5556. onOffset = e.sampleOffset;
  5557. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  5558. }
  5559. TEST (sampler_settings_pitch_reverse_envelope_cut)
  5560. {
  5561. auto ramp = std::make_shared<lynx::SampleData>();
  5562. for (int i = 0; i < 48000; ++i)
  5563. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  5564. ramp->sampleRate = 48000.0;
  5565. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  5566. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5567. inst->prepare (48000.0);
  5568. lynx::ChannelDef c;
  5569. c.sampler = s;
  5570. inst->configure (c, 120.0);
  5571. inst->noteOn (key, 1.0f);
  5572. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5573. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  5574. if (releaseAfterHalf)
  5575. inst->noteOff (key);
  5576. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  5577. return l;
  5578. };
  5579. // +12 semitones: reads twice as fast
  5580. lynx::SamplerSettings up;
  5581. up.pitch = 12.0f;
  5582. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  5583. // reversed: starts from the end
  5584. lynx::SamplerSettings back;
  5585. back.reverse = true;
  5586. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  5587. // 10 ms attack: starts silent and reaches full level
  5588. lynx::SamplerSettings slow;
  5589. slow.attack = 0.01f;
  5590. const auto a = play (slow, 60, 2000, false);
  5591. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  5592. // 5 ms release: gone right after release; without release it plays in full
  5593. lynx::SamplerSettings rel;
  5594. rel.release = 0.005f;
  5595. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  5596. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  5597. // cut itself: the second note cuts the first (one voice remains)
  5598. lynx::SamplerSettings cut;
  5599. cut.cut = true;
  5600. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5601. inst->prepare (48000.0);
  5602. lynx::ChannelDef cutChannel;
  5603. cutChannel.sampler = cut;
  5604. inst->configure (cutChannel, 120.0);
  5605. inst->noteOn (60, 1.0f);
  5606. std::vector<float> l (2000), r (2000);
  5607. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5608. inst->noteOn (60, 1.0f);
  5609. std::fill (l.begin(), l.end(), 0.0f);
  5610. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  5611. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  5612. }
  5613. TEST (playlist_split_and_track_mute)
  5614. {
  5615. lynx::Clip c { 0, 3, 960, 3840, 480 };
  5616. const auto rest = lynx::splitClip (c, 2000);
  5617. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  5618. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  5619. auto s = lynx::Song::makeDefault();
  5620. s.mode = lynx::PlayMode::song;
  5621. s.playlist = { { 0, 0, 0, 3840, 0 } };
  5622. lynx::EventBuffer ev;
  5623. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5624. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5625. CHECK (ev.size() > 0);
  5626. s.playlistTracks.resize (1);
  5627. s.playlistTracks[0].muted = true;
  5628. ev.clear();
  5629. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5630. CHECK (ev.size() == 0);
  5631. }
  5632. TEST (note_pan_pitch_and_mute)
  5633. {
  5634. lynx::Pattern p;
  5635. lynx::Note a { 0, 240, 0, 60 };
  5636. a.pan = -1.0f;
  5637. a.finePitch = 50.0f;
  5638. lynx::Note b { 240, 240, 0, 62 };
  5639. b.muted = true;
  5640. p.notes = { a, b };
  5641. lynx::EventBuffer ev;
  5642. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5643. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 48000, tps, ev);
  5644. int ons = 0;
  5645. for (auto& e : ev)
  5646. if (e.type == lynx::NoteEvent::Type::on)
  5647. {
  5648. ++ons;
  5649. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  5650. }
  5651. CHECK (ons == 1); // the muted one doesn't play
  5652. // note pan in the sampler: fully left
  5653. auto dc = std::make_shared<lynx::SampleData>();
  5654. dc->left.assign (4800, 0.5f);
  5655. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5656. inst->prepare (48000.0);
  5657. inst->setNextNoteExpression (-1.0f, 0.0f);
  5658. inst->noteOn (60, 1.0f);
  5659. std::vector<float> l (100), r (100);
  5660. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  5661. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  5662. }
  5663. TEST (tempo_automation_changes_speed_and_duration)
  5664. {
  5665. auto song = lynx::Song::makeDefault();
  5666. song.bpm = 120.0;
  5667. song.mode = lynx::PlayMode::song;
  5668. const auto bar = (std::int64_t) lynx::ppq * 4;
  5669. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  5670. lynx::Automation tempo;
  5671. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  5672. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  5673. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  5674. song.automations.push_back (tempo);
  5675. lynx::Clip clip;
  5676. clip.kind = lynx::ClipKind::automation;
  5677. clip.automation = 0;
  5678. clip.track = 1;
  5679. clip.length = bar * 2;
  5680. song.playlist.push_back (clip);
  5681. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5682. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5683. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5684. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5685. CHECK (state->tempoAutomation == 0);
  5686. lynx::RackRenderer r;
  5687. r.prepare (48000.0, 512);
  5688. std::vector<float> l (512), rr (512);
  5689. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5690. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5691. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5692. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5693. }
  5694. TEST (bounce_insert_song_and_apply)
  5695. {
  5696. auto s = lynx::Song::makeDefault();
  5697. s.setRoute (3, 7, true, 0.5f);
  5698. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5699. s.mixer[2].solo = true;
  5700. const auto b = lynx::makeBounceSong (s, 3);
  5701. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5702. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5703. const auto audible = b.soloAudible();
  5704. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5705. const auto channels = s.channels.size();
  5706. const auto inserts = s.mixer.size();
  5707. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5708. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5709. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5710. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5711. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5712. }
  5713. TEST (mixer_stereo_separation_phase_swap)
  5714. {
  5715. // FL's level matrix (REA_rodada2 §2.2, REA_rodada167j §1): outL = ll·inL + rl·inR, outR = rr·inR + lr·inL
  5716. lynx::MixerTrack t;
  5717. auto near = [] (float a, float b) { return std::abs (a - b) < 1e-5f; };
  5718. t.stereo = 1.0f; // mono
  5719. auto m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5720. CHECK (near (m.ll, 0.5f) && near (m.rl, 0.5f) && near (m.rr, 0.5f) && near (m.lr, 0.5f));
  5721. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5722. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5723. CHECK (near (m.ll, 1.5f) && near (m.rl, -0.5f) && near (m.rr, 1.5f) && near (m.lr, -0.5f));
  5724. t.stereo = 0.0f;
  5725. t.swapChannels = true;
  5726. t.invertPhase = true;
  5727. m = lynx::mixerMatrix (t, lynx::unityFader, 0.0f, false);
  5728. CHECK (m.ll == 0.0f && m.rl == -1.0f && m.rr == 0.0f && m.lr == -1.0f); // outL = −inR, outR = −inL
  5729. // unity, centre, no separation: exactly the identity (FL's fast path)
  5730. m = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.0f, false);
  5731. CHECK (m == lynx::MixerMatrix {});
  5732. }
  5733. TEST (mixer_pan_fold_and_panning_law_match_fl_render)
  5734. {
  5735. // FL 2026's render of j_tri.flp (round 167j: a 441 Hz left / 882 Hz right sample on ten inserts, pan law
  5736. // triangular), the matrix fitted per insert: {pan, separation, ll, rl, lr, rr}
  5737. struct Case { float pan, sep, ll, rl, lr, rr; };
  5738. 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 },
  5739. { 0.25f, 0.0f, 0.75f, 0.0f, 0.125001f, 0.874978f }, { 0.5f, 0.0f, 0.499985f, 0.0f, 0.250011f, 0.749964f },
  5740. { 1.0f, 0.0f, 0.0f, 0.0f, 0.499982f, 0.499972f }, { 0.5f, 0.5f, 0.422756f, 0.077253f, 0.4045f, 0.595469f },
  5741. { -0.5f, -0.5f, 1.0f, 0.0f, -0.125004f, 0.624974f }, { 0.75f, 0.0f, 0.250003f, 0.0f, 0.375006f, 0.624975f },
  5742. { 0.0f, 0.75f, 0.70703f, 0.292941f, 0.292965f, 0.707045f } };
  5743. for (const auto& c : fl)
  5744. {
  5745. lynx::MixerTrack t;
  5746. t.stereo = c.sep;
  5747. const auto m = lynx::mixerMatrix (t, lynx::unityFader, c.pan, true);
  5748. // the fit's own error is ~5e-5 (16-bit render)
  5749. 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
  5750. && std::abs (m.rr - c.rr) < 2e-4f);
  5751. }
  5752. // circular (the default): +3 dB on the side panned to, and the folded half of the other side
  5753. const auto hard = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 1.0f, false);
  5754. CHECK (std::abs (hard.ll) < 1e-6f && std::abs (hard.rl) < 1e-6f);
  5755. CHECK (std::abs (hard.rr - 0.70710677f) < 1e-6f && std::abs (hard.lr - 0.70710677f) < 1e-6f);
  5756. const auto half = lynx::mixerMatrix (lynx::MixerTrack {}, lynx::unityFader, 0.5f, false);
  5757. 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;
  5758. CHECK (std::abs (half.ll - gl) < 1e-5f && half.rl == 0.0f);
  5759. 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);
  5760. // the fader scales the whole matrix (−6 dB-ish position), polarity negates it
  5761. lynx::MixerTrack inv;
  5762. inv.invertPhase = true;
  5763. const auto low = lynx::mixerMatrix (inv, 0.5f, 0.0f, false);
  5764. const auto g = (float) ((std::exp ((double) (0.625f * 2.3978953f)) - 1.0) * 0.1);
  5765. CHECK (std::abs (low.ll + g) < 1e-5f && std::abs (low.rr + g) < 1e-5f && std::abs (low.rl) < 1e-6f);
  5766. }
  5767. TEST (effects_limiter_filter_distortion_chorus)
  5768. {
  5769. const double sr = 48000.0;
  5770. auto sine = [] (float hz, float amp, int n) {
  5771. std::vector<float> v ((std::size_t) n);
  5772. for (int i = 0; i < n; ++i)
  5773. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5774. return v;
  5775. };
  5776. auto peak = [] (const std::vector<float>& v, int from) {
  5777. float p = 0.0f;
  5778. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5779. p = std::max (p, std::abs (v[i]));
  5780. return p;
  5781. };
  5782. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5783. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5784. lim->prepare (sr);
  5785. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5786. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5787. slot.params[1] = 0.5f; // +12 dB
  5788. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5789. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5790. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5791. CHECK (peak (l, 4800) > 0.8f);
  5792. CHECK (lim->latencySamples() == 72);
  5793. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5794. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5795. lp->prepare (sr);
  5796. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5797. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5798. l = sine (8000.0f, 1.0f, 9600);
  5799. r = l;
  5800. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5801. CHECK (peak (l, 4800) < 0.05f);
  5802. // distortion and chorus: finite, audible output
  5803. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5804. {
  5805. auto fx = lynx::makeEffect (kind);
  5806. fx->prepare (sr);
  5807. auto ps = lynx::makeEffectSlot (kind);
  5808. l = sine (220.0f, 0.5f, 9600);
  5809. r = l;
  5810. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5811. bool finite = true;
  5812. for (auto v : l)
  5813. finite = finite && std::isfinite (v);
  5814. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5815. }
  5816. }
  5817. TEST (slide_notes_glide_without_retrigger)
  5818. {
  5819. // sampler with a ramp: the read speed doubles after a one-octave slide
  5820. auto ramp = std::make_shared<lynx::SampleData>();
  5821. for (int i = 0; i < 96000; ++i)
  5822. ramp->left.push_back ((float) i / 96000.0f);
  5823. ramp->sampleRate = 48000.0;
  5824. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5825. inst->prepare (48000.0);
  5826. inst->noteOn (60, 1.0f);
  5827. std::vector<float> l (2000), r (2000);
  5828. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5829. const auto stepBefore = l[999] - l[998];
  5830. inst->slideTo (72, 0.0f, 100.0f);
  5831. std::fill (l.begin(), l.end(), 0.0f);
  5832. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5833. const auto stepAfter = l[999] - l[998];
  5834. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5835. // the sequencer sends "slide" (no off) for the slide note
  5836. lynx::Pattern p;
  5837. lynx::Note base { 0, 960, 0, 48 };
  5838. lynx::Note glide { 480, 240, 0, 55 };
  5839. glide.slide = true;
  5840. p.notes = { base, glide };
  5841. lynx::EventBuffer ev;
  5842. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5843. lynx::schedulePatternLoop (p, p.effectiveLength (lynx::Meter (4, 4)), 0.0, 30000, tps, ev);
  5844. int ons = 0, offs = 0, slides = 0;
  5845. for (auto& e : ev)
  5846. {
  5847. ons += e.type == lynx::NoteEvent::Type::on;
  5848. offs += e.type == lynx::NoteEvent::Type::off;
  5849. if (e.type == lynx::NoteEvent::Type::slide)
  5850. {
  5851. ++slides;
  5852. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5853. }
  5854. }
  5855. CHECK (ons == 1 && offs == 1 && slides == 1);
  5856. }
  5857. TEST (synth_fm_phase_modulation)
  5858. {
  5859. auto render = [] (const lynx::SynthSettings& s) {
  5860. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5861. inst->prepare (48000.0);
  5862. lynx::ChannelDef c;
  5863. c.synth = s;
  5864. inst->configure (c, 120.0);
  5865. inst->noteOn (69, 1.0f); // 440 Hz
  5866. std::vector<float> l (48000), r (48000);
  5867. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5868. return l;
  5869. };
  5870. // energy (Goertzel) at frequency f over frames [from, to)
  5871. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5872. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5873. double s1 = 0.0, s2 = 0.0;
  5874. for (int i = from; i < to; ++i)
  5875. {
  5876. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5877. s2 = s1;
  5878. s1 = s0;
  5879. }
  5880. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5881. };
  5882. lynx::SynthSettings plain;
  5883. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5884. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5885. plain.cutoff = 20000.0f;
  5886. auto fm = plain;
  5887. fm.fm = 1.0f;
  5888. const auto a = render (plain), b = render (fm);
  5889. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5890. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5891. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5892. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5893. for (auto v : b)
  5894. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5895. // index decaying to 0: the start is bright, the end is nearly a sine again
  5896. auto decaying = fm;
  5897. decaying.fmDecay = 0.1f;
  5898. decaying.fmSustain = 0.0f;
  5899. const auto c = render (decaying);
  5900. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5901. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5902. CHECK (early > 0.05 && late < 1e-3);
  5903. }
  5904. TEST (synth_three_oscillators_adsr_filter)
  5905. {
  5906. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5907. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5908. inst->prepare (48000.0);
  5909. lynx::ChannelDef c;
  5910. c.synth = s;
  5911. inst->configure (c, 120.0);
  5912. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5913. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5914. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5915. inst->noteOff (69);
  5916. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5917. return l;
  5918. };
  5919. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5920. float p = 0.0f;
  5921. for (int i = from; i < to; ++i)
  5922. p = std::max (p, std::abs (v[(std::size_t) i]));
  5923. return p;
  5924. };
  5925. // default: sounds and fades after the release
  5926. const lynx::SynthSettings standard;
  5927. const auto a = render (standard, 48000, 24000);
  5928. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5929. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5930. lynx::SynthSettings sine;
  5931. sine.osc[0].wave = lynx::Waveform::sine;
  5932. sine.cutoff = 20000.0f;
  5933. const auto b = render (sine, 48000, 47000);
  5934. int crossings = 0;
  5935. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5936. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5937. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5938. float sinePeak = 0.0f;
  5939. for (int i = 4800; i < 9600; ++i)
  5940. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5941. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5942. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5943. lynx::SynthSettings two = sine;
  5944. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5945. two.sustain = 0.0f;
  5946. two.decay = 0.1f;
  5947. const auto c = render (two, 48000, 47000);
  5948. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5949. }
  5950. TEST (set_target_value_round_trips)
  5951. {
  5952. auto s = lynx::Song::makeDefault();
  5953. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5954. const lynx::AutomationTarget targets[] = {
  5955. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5956. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5957. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5958. };
  5959. for (const auto& t : targets)
  5960. {
  5961. CHECK (s.setTargetValue (t, 0.25f));
  5962. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5963. }
  5964. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5965. }
  5966. TEST (cut_groups_stretch_and_envelope_import)
  5967. {
  5968. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5969. lynx::Song s;
  5970. s.mixer = lynx::Song::defaultMixer (2);
  5971. for (int i = 0; i < 2; ++i)
  5972. {
  5973. lynx::ChannelDef c;
  5974. c.instrument = lynx::InstrumentKind::sampler;
  5975. c.samplePath = "dc";
  5976. c.volume = 1.0f;
  5977. c.mixerTrack = i + 1;
  5978. s.channels.push_back (c);
  5979. }
  5980. s.channels[0].cutBy = 1; // open
  5981. s.channels[1].cutGroup = 1; // closed
  5982. auto& p = s.patterns.emplace_back();
  5983. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5984. auto provider = [] (const std::string&) {
  5985. auto d = std::make_shared<lynx::SampleData>();
  5986. d->left.assign (96000, 0.5f);
  5987. return std::shared_ptr<const lynx::SampleData> (d);
  5988. };
  5989. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5990. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5991. lynx::RackRenderer r;
  5992. r.prepare (48000.0, 512);
  5993. std::vector<float> l (512), rr (512);
  5994. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5995. float afterCut = 0.0f;
  5996. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5997. {
  5998. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5999. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  6000. afterCut = std::max (afterCut, std::abs (l[256]));
  6001. }
  6002. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  6003. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  6004. auto ramp = std::make_shared<lynx::SampleData>();
  6005. for (int i = 0; i < 48000; ++i)
  6006. ramp->left.push_back ((float) i / 48000.0f);
  6007. ramp->sampleRate = 48000.0;
  6008. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6009. inst->prepare (48000.0);
  6010. lynx::ChannelDef c;
  6011. c.sampler.stretchBeats = 4.0f;
  6012. inst->configure (c, 120.0);
  6013. inst->noteOn (60, 1.0f);
  6014. std::vector<float> a (48000), b (48000);
  6015. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  6016. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  6017. // FL's Resample: the key still transposes, like tape (round 162, issue #9): an octave up reads twice as fast
  6018. inst->allNotesOff();
  6019. std::fill (a.begin(), a.end(), 0.0f);
  6020. inst->render (a.data(), b.data(), 4800, 1.0f, 1.0f);
  6021. std::fill (a.begin(), a.end(), 0.0f);
  6022. inst->noteOn (72, 1.0f);
  6023. inst->render (a.data(), b.data(), 24000, 1.0f, 1.0f);
  6024. CHECK (std::abs (a[23999] - 0.5f) < 0.01f);
  6025. // FL envelope time: 32950 = 2 s; < 101 = 0
  6026. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  6027. }
  6028. TEST (time_stretch_keeps_pitch)
  6029. {
  6030. lynx::SampleData sine;
  6031. sine.sampleRate = 48000.0;
  6032. for (int i = 0; i < 48000; ++i)
  6033. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  6034. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  6035. int c = 0;
  6036. const auto n = (int) d.frames();
  6037. for (int i = 4800; i < n - 4800; ++i)
  6038. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  6039. return (double) c / ((double) (n - 9600) / d.sampleRate);
  6040. };
  6041. for (double ratio : { 2.0, 0.5, 1.37 })
  6042. {
  6043. const auto out = lynx::timeStretch (sine, ratio);
  6044. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  6045. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  6046. float peak = 0.0f;
  6047. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  6048. peak = std::max (peak, std::abs (out->left[i]));
  6049. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  6050. }
  6051. }
  6052. TEST (sampler_region_start_and_end)
  6053. {
  6054. auto ramp = std::make_shared<lynx::SampleData>();
  6055. for (int i = 0; i < 10000; ++i)
  6056. ramp->left.push_back ((float) i / 10000.0f);
  6057. ramp->sampleRate = 48000.0;
  6058. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6059. inst->prepare (48000.0);
  6060. lynx::ChannelDef c;
  6061. c.sampler.regionStart = 0.25f;
  6062. c.sampler.regionEnd = 0.5f;
  6063. inst->configure (c, 120.0);
  6064. inst->noteOn (60, 1.0f);
  6065. std::vector<float> l (6000), r (6000);
  6066. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  6067. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  6068. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  6069. }
  6070. TEST (arpeggiator_plays_held_chord_in_steps)
  6071. {
  6072. auto song = lynx::Song::makeDefault();
  6073. song.bpm = 120.0;
  6074. song.patterns[0].notes.clear();
  6075. for (int key : { 60, 64, 67 })
  6076. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  6077. song.channels[4].arp.direction = 1; // up
  6078. song.channels[4].arp.stepBeats = 0.25f;
  6079. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6080. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  6081. // the arpeggiator generates the notes; we count what it hands to the instrument
  6082. auto& arp = *state->arpeggiators[4];
  6083. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6084. std::vector<int> keys;
  6085. lynx::EventBuffer ev;
  6086. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  6087. {
  6088. ev.clear();
  6089. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (lynx::Meter (4, 4)), tick, 512, tps, ev);
  6090. int in = 0, out = 0;
  6091. for (auto& e : ev)
  6092. if (e.channel == 4)
  6093. arp.input[(std::size_t) in++] = e;
  6094. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  6095. for (int i = 0; i < out; ++i)
  6096. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  6097. keys.push_back (arp.output[(std::size_t) i].key);
  6098. }
  6099. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  6100. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  6101. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  6102. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  6103. CHECK (keys[i] == expected[i]);
  6104. // actually rendering also makes sound
  6105. lynx::RackRenderer r;
  6106. r.prepare (48000.0, 512);
  6107. std::vector<float> l (512), rr (512);
  6108. float peak = 0.0f;
  6109. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  6110. {
  6111. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6112. for (auto v : l)
  6113. peak = std::max (peak, std::abs (v));
  6114. }
  6115. CHECK (peak > 0.01f);
  6116. }
  6117. TEST (audio_clip_fades_and_gain)
  6118. {
  6119. lynx::Song s;
  6120. s.bpm = 120.0;
  6121. s.mode = lynx::PlayMode::song;
  6122. s.mixer = lynx::Song::defaultMixer (1);
  6123. lynx::ChannelDef c;
  6124. c.instrument = lynx::InstrumentKind::audioClip;
  6125. c.samplePath = "dc";
  6126. c.volume = 1.0f;
  6127. c.mixerTrack = 1;
  6128. s.channels.push_back (c);
  6129. s.patterns.emplace_back();
  6130. lynx::Clip clip;
  6131. clip.kind = lynx::ClipKind::audio;
  6132. clip.channel = 0;
  6133. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  6134. clip.fadeIn = lynx::ppq; // 0.5 s
  6135. clip.fadeOut = lynx::ppq;
  6136. clip.gain = 0.5f;
  6137. s.playlist.push_back (clip);
  6138. auto provider = [] (const std::string&) {
  6139. auto d = std::make_shared<lynx::SampleData>();
  6140. d->left.assign (48000 * 4, 1.0f);
  6141. return std::shared_ptr<const lynx::SampleData> (d);
  6142. };
  6143. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  6144. lynx::RackRenderer r;
  6145. r.prepare (48000.0, 512);
  6146. std::vector<float> all;
  6147. std::vector<float> l (512), rr (512);
  6148. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  6149. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6150. {
  6151. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6152. all.insert (all.end(), l.begin(), l.end());
  6153. }
  6154. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  6155. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  6156. CHECK (all[100] < 0.01f); // fade-in start
  6157. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  6158. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  6159. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  6160. }
  6161. TEST (audio_clip_unity_at_fl_default_volume)
  6162. {
  6163. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  6164. auto dc = std::make_shared<lynx::SampleData>();
  6165. dc->left.assign (48000, 0.5f);
  6166. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  6167. clip->prepare (48000.0);
  6168. lynx::ChannelDef c;
  6169. c.instrument = lynx::InstrumentKind::audioClip;
  6170. c.volume = 10000.0f / 12800.0f;
  6171. clip->configure (c, 120.0);
  6172. clip->noteOn (60, 1.0f);
  6173. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  6174. const auto g = lynx::channelGain (c.volume);
  6175. clip->render (l.data(), r.data(), 4800, g, g);
  6176. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  6177. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  6178. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  6179. sampler->prepare (48000.0);
  6180. c.instrument = lynx::InstrumentKind::sampler;
  6181. sampler->configure (c, 120.0);
  6182. sampler->noteOn (60, 1.0f);
  6183. std::fill (l.begin(), l.end(), 0.0f);
  6184. std::fill (r.begin(), r.end(), 0.0f);
  6185. sampler->render (l.data(), r.data(), 4800, g, g);
  6186. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  6187. }
  6188. TEST (slicer_detects_hits_and_plays_slices_by_key)
  6189. {
  6190. // 4 hits (decaying noise) every 0.25 s
  6191. lynx::SampleData loop;
  6192. loop.sampleRate = 48000.0;
  6193. loop.left.assign (48000, 0.0f);
  6194. lynx::Noise noise;
  6195. for (int hit = 0; hit < 4; ++hit)
  6196. for (int i = 0; i < 6000; ++i)
  6197. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  6198. const auto slices = lynx::detectSlices (loop, 0.5f);
  6199. CHECK (slices.size() == 4);
  6200. for (std::size_t i = 0; i < slices.size(); ++i)
  6201. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  6202. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  6203. auto ramp = std::make_shared<lynx::SampleData>();
  6204. for (int i = 0; i < 48000; ++i)
  6205. ramp->left.push_back ((float) i / 48000.0f);
  6206. ramp->sampleRate = 48000.0;
  6207. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  6208. inst->prepare (48000.0);
  6209. lynx::ChannelDef c;
  6210. c.sampler.slices = slices;
  6211. inst->configure (c, 120.0);
  6212. inst->noteOn (61, 1.0f);
  6213. std::vector<float> l (20000), r (20000);
  6214. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  6215. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  6216. }
  6217. TEST (project_templates_render_sound)
  6218. {
  6219. const auto names = lynx::templateNames();
  6220. CHECK (names.size() == 4);
  6221. for (int t = 1; t < (int) names.size(); ++t)
  6222. {
  6223. auto song = lynx::makeTemplate (t);
  6224. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  6225. lynx::RackRenderer r;
  6226. r.prepare (48000.0, 512);
  6227. std::vector<float> l (512), rr (512);
  6228. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  6229. float peak = 0.0f;
  6230. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  6231. {
  6232. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  6233. for (auto v : l)
  6234. peak = std::max (peak, std::abs (v));
  6235. }
  6236. CHECK (peak > 0.05f && peak < 4.0f);
  6237. }
  6238. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  6239. }
  6240. // --- interface translations (resources/i18n) -------------------------------------------------------
  6241. namespace i18ntest
  6242. {
  6243. std::string readFile (const std::filesystem::path& path)
  6244. {
  6245. std::ifstream in (path, std::ios::binary);
  6246. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  6247. }
  6248. std::string unescape (const std::string& s)
  6249. {
  6250. std::string out;
  6251. for (std::size_t i = 0; i < s.size(); ++i)
  6252. {
  6253. if (s[i] == '\\' && i + 1 < s.size())
  6254. {
  6255. const auto n = s[++i];
  6256. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  6257. }
  6258. else
  6259. out += s[i];
  6260. }
  6261. return out;
  6262. }
  6263. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  6264. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  6265. {
  6266. if (pos >= s.size() || s[pos] != '"')
  6267. return false;
  6268. raw.clear();
  6269. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  6270. {
  6271. if (s[pos] == '\\' && pos + 1 < s.size())
  6272. raw += s[pos++];
  6273. raw += s[pos];
  6274. }
  6275. if (pos >= s.size() || s[pos] != '"')
  6276. return false;
  6277. ++pos;
  6278. return true;
  6279. }
  6280. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  6281. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  6282. {
  6283. std::map<std::string, std::string> table;
  6284. std::istringstream lines (text);
  6285. std::string line;
  6286. while (std::getline (lines, line))
  6287. {
  6288. std::size_t pos = line.find_first_not_of (" \t");
  6289. std::string key, value;
  6290. if (pos == std::string::npos || ! quoted (line, pos, key))
  6291. continue;
  6292. pos = line.find ('"', pos);
  6293. if (pos == std::string::npos || ! quoted (line, pos, value))
  6294. value.clear();
  6295. const auto k = unescape (key);
  6296. if (table.count (k) != 0)
  6297. duplicates.push_back (k);
  6298. table[k] = unescape (value);
  6299. }
  6300. return table;
  6301. }
  6302. // string literals passed to tr() / trc() / TR_NOOP() in the sources (adjacent literals are concatenated)
  6303. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  6304. {
  6305. std::map<std::string, std::string> keys;
  6306. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  6307. {
  6308. const auto ext = entry.path().extension().string();
  6309. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h" && ext != ".inc"))
  6310. continue;
  6311. // drop // comment lines (they may quote tr ("...") as an example)
  6312. std::string text, line;
  6313. std::istringstream in (readFile (entry.path()));
  6314. while (std::getline (in, line))
  6315. {
  6316. const auto first = line.find_first_not_of (" \t");
  6317. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  6318. text += '\n';
  6319. }
  6320. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  6321. for (const std::string name : { "tr", "trc", "TR_NOOP" })
  6322. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  6323. {
  6324. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  6325. continue;
  6326. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  6327. if (pos == std::string::npos || text[pos] != '(')
  6328. continue;
  6329. pos = text.find_first_not_of (" \t\n", pos + 1);
  6330. std::string key, raw;
  6331. bool any = false;
  6332. while (pos != std::string::npos && quoted (text, pos, raw))
  6333. {
  6334. key += raw;
  6335. any = true;
  6336. pos = text.find_first_not_of (" \t\n", pos);
  6337. }
  6338. if (any)
  6339. keys.emplace (unescape (key), entry.path().filename().string());
  6340. }
  6341. }
  6342. return keys;
  6343. }
  6344. std::set<std::string> placeholders (const std::string& s)
  6345. {
  6346. std::set<std::string> found;
  6347. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  6348. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  6349. found.insert (s.substr (open, close - open + 1));
  6350. return found;
  6351. }
  6352. } // namespace i18ntest
  6353. TEST (translations_cover_every_ui_string)
  6354. {
  6355. using namespace i18ntest;
  6356. const auto text = readFile ("resources/i18n/pt-BR.txt");
  6357. CHECK (! text.empty());
  6358. std::vector<std::string> duplicates;
  6359. const auto table = parseTable (text, duplicates);
  6360. CHECK (table.size() > 300);
  6361. CHECK (duplicates.empty());
  6362. for (const auto& d : duplicates)
  6363. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  6364. const auto keys = sourceKeys ("src");
  6365. CHECK (keys.size() > 300);
  6366. int missing = 0;
  6367. for (const auto& [key, file] : keys)
  6368. if (auto it = table.find (key); it == table.end() || it->second.empty())
  6369. {
  6370. ++missing;
  6371. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  6372. }
  6373. CHECK (missing == 0);
  6374. // no empty values, and every {n} placeholder survives the translation
  6375. int empty = 0, badPlaceholders = 0;
  6376. for (const auto& [english, portuguese] : table)
  6377. {
  6378. empty += portuguese.empty() ? 1 : 0;
  6379. if (placeholders (english) != placeholders (portuguese))
  6380. {
  6381. ++badPlaceholders;
  6382. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  6383. }
  6384. }
  6385. CHECK (empty == 0);
  6386. CHECK (badPlaceholders == 0);
  6387. }
  6388. TEST (translations_embedded_table_is_up_to_date)
  6389. {
  6390. // src/ui/i18n/<id>.h + I18nData.h must be regenerated (tools/make_i18n.py) whenever a resources/i18n/*.txt changes
  6391. int files = 0;
  6392. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6393. if (entry.path().extension() == ".txt")
  6394. {
  6395. ++files;
  6396. const auto id = entry.path().stem().string();
  6397. const auto text = i18ntest::readFile (entry.path());
  6398. const lynx::i18n::data::Table* found = nullptr;
  6399. for (const auto& t : lynx::i18n::data::tables)
  6400. if (id == t.id)
  6401. found = &t;
  6402. CHECK (found != nullptr);
  6403. if (found == nullptr)
  6404. {
  6405. std::printf (" %s.txt is not embedded: run python3 tools/make_i18n.py\n", id.c_str());
  6406. continue;
  6407. }
  6408. const auto unpacked = lynx::inflateZlib (found->zlib, found->zlibSize, false, found->textSize + 16);
  6409. const bool same = unpacked && unpacked->size() == text.size() && found->textSize == text.size()
  6410. && std::memcmp (unpacked->data(), text.data(), text.size()) == 0;
  6411. CHECK (same);
  6412. if (! same)
  6413. std::printf (" %s.txt changed since the last python3 tools/make_i18n.py\n", id.c_str());
  6414. }
  6415. CHECK (files == (int) std::size (lynx::i18n::data::tables));
  6416. }
  6417. TEST (translations_no_value_is_a_python_dict)
  6418. {
  6419. // a value written as a Python dict ("{'de': '...', 'es': ...}", 177a's menu slice) reaches the users verbatim:
  6420. // each table holds its own language's text only
  6421. using namespace i18ntest;
  6422. int bad = 0;
  6423. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6424. if (entry.path().extension() == ".txt")
  6425. {
  6426. std::vector<std::string> unused;
  6427. for (const auto& [key, value] : parseTable (readFile (entry.path()), unused))
  6428. if (value.rfind ("{'", 0) == 0 || value.find ("': '") != std::string::npos)
  6429. {
  6430. ++bad;
  6431. std::printf (" %s: \"%s\" holds a dict\n", entry.path().filename().string().c_str(), key.c_str());
  6432. }
  6433. }
  6434. CHECK (bad == 0);
  6435. }
  6436. TEST (translations_every_language_covers_the_reference_table)
  6437. {
  6438. // every key of pt-BR.txt (the reference table) exists, translated, in every other language; keys just added to
  6439. // pt-BR.txt may wait in resources/i18n/pending.keys ("English" lines) until the other tables catch up
  6440. using namespace i18ntest;
  6441. std::vector<std::string> unused;
  6442. const auto reference = parseTable (readFile ("resources/i18n/pt-BR.txt"), unused);
  6443. std::set<std::string> pending;
  6444. {
  6445. std::istringstream in (readFile ("resources/i18n/pending.keys"));
  6446. std::string line, raw;
  6447. while (std::getline (in, line))
  6448. if (std::size_t pos = line.find_first_not_of (" \t"); pos != std::string::npos && quoted (line, pos, raw))
  6449. pending.insert (unescape (raw));
  6450. }
  6451. auto count = [] (const std::string& s, const std::string& what) {
  6452. int n = 0;
  6453. for (auto at = s.find (what); at != std::string::npos; at = s.find (what, at + what.size()))
  6454. ++n;
  6455. return n;
  6456. };
  6457. int languages = 0;
  6458. for (const auto& entry : std::filesystem::directory_iterator ("resources/i18n"))
  6459. {
  6460. const auto id = entry.path().stem().string();
  6461. if (entry.path().extension() != ".txt" || id == "pt-BR")
  6462. continue;
  6463. ++languages;
  6464. const auto text = readFile (entry.path());
  6465. CHECK (text.rfind ("language: ", 0) == 0); // JUCE LocalisedStrings header
  6466. std::vector<std::string> duplicates;
  6467. const auto table = parseTable (text, duplicates);
  6468. CHECK (duplicates.empty());
  6469. for (const auto& d : duplicates)
  6470. std::printf (" duplicate %s entry: \"%s\"\n", id.c_str(), d.c_str());
  6471. int missing = 0, bad = 0;
  6472. for (const auto& [english, unusedValue] : reference)
  6473. {
  6474. const auto it = table.find (english);
  6475. if ((it == table.end() || it->second.empty()) && pending.count (english) == 0)
  6476. {
  6477. if (++missing <= 10)
  6478. std::printf (" no %s translation for \"%s\" (python3 tools/i18n_tool.py status)\n", id.c_str(), english.c_str());
  6479. }
  6480. }
  6481. for (const auto& [english, translated] : table)
  6482. {
  6483. std::string why;
  6484. if (translated.empty())
  6485. why = "empty";
  6486. else if (placeholders (english) != placeholders (translated))
  6487. why = "placeholders differ";
  6488. else if (count (english, "\n") != count (translated, "\n"))
  6489. why = "line breaks differ";
  6490. else if (count (english, "%") != count (translated, "%"))
  6491. why = "% signs differ";
  6492. if (! why.empty() && ++bad <= 10)
  6493. std::printf (" %s: %s: \"%s\"\n", id.c_str(), why.c_str(), english.c_str());
  6494. }
  6495. CHECK (missing == 0);
  6496. CHECK (bad == 0);
  6497. if (missing > 10 || bad > 10)
  6498. std::printf (" %s: %d missing, %d bad in all\n", id.c_str(), missing, bad);
  6499. }
  6500. CHECK (languages >= 0);
  6501. }
  6502. TEST (translations_os_locale_picks_the_interface_language)
  6503. {
  6504. const std::vector<std::string> ids { "en", "pt-BR", "de", "es", "ja", "zh-Hans", "zh-Hant", "id", "ko" };
  6505. using lynx::i18n::languageForLocale;
  6506. CHECK (languageForLocale ("de-AT", ids) == "de");
  6507. CHECK (languageForLocale ("de_DE.UTF-8", ids) == "de");
  6508. CHECK (languageForLocale ("pt-BR", ids) == "pt-BR");
  6509. CHECK (languageForLocale ("pt_PT", ids) == "pt-BR");
  6510. CHECK (languageForLocale ("zh-Hans-CN", ids) == "zh-Hans");
  6511. CHECK (languageForLocale ("zh-CN", ids) == "zh-Hans");
  6512. CHECK (languageForLocale ("zh_TW.UTF-8", ids) == "zh-Hant");
  6513. CHECK (languageForLocale ("zh-Hant-HK", ids) == "zh-Hant");
  6514. CHECK (languageForLocale ("zh-HK", ids) == "zh-Hant");
  6515. CHECK (languageForLocale ("ja_JP.UTF-8@euro", ids) == "ja");
  6516. CHECK (languageForLocale ("in_ID", ids) == "id");
  6517. CHECK (languageForLocale ("es-419", ids) == "es");
  6518. CHECK (languageForLocale ("en-US", ids) == "en");
  6519. CHECK (languageForLocale ("ko:en", ids) == "ko");
  6520. CHECK (languageForLocale ("fr-FR", ids).empty()); // not in the list
  6521. CHECK (languageForLocale ("C", ids).empty());
  6522. CHECK (languageForLocale ("", ids).empty());
  6523. }
  6524. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  6525. TEST (workflow_pattern_navigation_and_insert)
  6526. {
  6527. auto song = lynx::Song::makeDefault();
  6528. song.patterns.resize (3);
  6529. song.patterns[1].name = "B";
  6530. song.patterns[2].name = "C";
  6531. song.patterns[2].notes.push_back ({});
  6532. song.currentPattern = 0;
  6533. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  6534. // pattern 0 has the default notes, 1 is empty, 2 has a note
  6535. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  6536. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  6537. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  6538. song.patterns[1].notes.push_back ({});
  6539. const auto before = song.patterns.size();
  6540. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  6541. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  6542. // insert keeps Playlist clips on their patterns
  6543. lynx::Clip clip;
  6544. clip.pattern = 2;
  6545. song.playlist = { clip };
  6546. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  6547. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  6548. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  6549. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  6550. }
  6551. TEST (workflow_split_pattern_by_channel)
  6552. {
  6553. auto song = lynx::Song::makeDefault();
  6554. song.patterns.resize (1);
  6555. auto& p = song.patterns[0];
  6556. p.name = "Beat";
  6557. p.notes.clear();
  6558. for (int i = 0; i < 4; ++i)
  6559. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  6560. song.channels[2].colour = 0xff112233;
  6561. lynx::Clip clip;
  6562. clip.pattern = 0;
  6563. song.playlist = { clip };
  6564. const auto created = lynx::splitPatternByChannel (song, 0);
  6565. CHECK (created == 2 && song.patterns.size() == 3);
  6566. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  6567. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  6568. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  6569. CHECK (song.playlist[0].pattern == 0);
  6570. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  6571. }
  6572. TEST (workflow_channel_selection)
  6573. {
  6574. lynx::ChannelSelection s;
  6575. s.selectOnly (2);
  6576. s.toggle (5);
  6577. s.toggle (0);
  6578. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  6579. s.toggle (2);
  6580. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  6581. s.selectOnly (3);
  6582. s.extendTo (6);
  6583. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  6584. s.extendTo (1);
  6585. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  6586. s.selectOnly (4);
  6587. s.toggle (4); // the last one stays
  6588. CHECK ((s.rows == std::vector<int> { 4 }));
  6589. s.selectAll (3);
  6590. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  6591. s.rows = { 7, 9 };
  6592. s.clamp (5);
  6593. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  6594. }
  6595. TEST (workflow_move_clone_insert_channels)
  6596. {
  6597. auto song = lynx::Song::makeDefault();
  6598. const auto names = [&] {
  6599. std::vector<std::string> n;
  6600. for (const auto& c : song.channels)
  6601. n.push_back (c.name);
  6602. return n;
  6603. };
  6604. const auto original = names();
  6605. const auto count = (int) song.channels.size();
  6606. CHECK (count >= 4);
  6607. // references that must follow their channel
  6608. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  6609. lynx::Automation a;
  6610. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  6611. song.automations.push_back (a);
  6612. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  6613. lynx::Clip audio;
  6614. audio.kind = lynx::ClipKind::audio;
  6615. audio.channel = 3;
  6616. song.playlist.push_back (audio);
  6617. std::vector<int> selection { 1, 3 };
  6618. CHECK (lynx::moveChannels (song, selection, -1));
  6619. CHECK ((selection == std::vector<int> { 0, 2 }));
  6620. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  6621. && song.channels[3].name == original[2]);
  6622. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  6623. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  6624. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  6625. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  6626. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  6627. // clone: settings, not notes, right below, with a new name
  6628. song.channels[1].volume = 0.33f;
  6629. const auto notesBefore = song.patterns[0].notes.size();
  6630. const auto clone = lynx::cloneChannel (song, 1);
  6631. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  6632. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  6633. CHECK (song.patterns[0].notes.size() == notesBefore);
  6634. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  6635. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  6636. // remove several
  6637. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  6638. CHECK (names() == original);
  6639. }
  6640. TEST (workflow_send_channels_to_free_inserts)
  6641. {
  6642. auto song = lynx::Song::makeDefault();
  6643. for (auto& c : song.channels)
  6644. c.mixerTrack = 0;
  6645. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  6646. song.channels[0].mixerTrack = 2; // not free: used
  6647. song.setRoute (5, 3, true); // 3 is not free: receives a send
  6648. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  6649. song.channels[1].colour = 0xff445566;
  6650. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  6651. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  6652. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  6653. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  6654. // with no free insert left, a new one is added at the end
  6655. auto full = lynx::Song::makeDefault();
  6656. full.mixer = lynx::Song::defaultMixer (1);
  6657. full.channels[0].mixerTrack = 1;
  6658. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  6659. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  6660. }
  6661. TEST (workflow_add_channel_routed)
  6662. {
  6663. auto song = lynx::Song::makeDefault();
  6664. const auto count = (int) song.channels.size();
  6665. const auto inserts = song.mixer.size();
  6666. lynx::ChannelDef c;
  6667. c.mixerTrack = 7; // what the generator came with doesn't count
  6668. const auto last = (int) song.mixer.size() - 1;
  6669. CHECK (lynx::addChannelRouted (song, c, last) == count && song.channels.back().mixerTrack == last);
  6670. CHECK (lynx::addChannelRouted (song, c, 0) == count + 1 && song.channels.back().mixerTrack == 0); // Master
  6671. CHECK (song.mixer.size() == inserts); // the drop's insert is used as it is, nothing is claimed
  6672. // an insert that doesn't exist claims a free one, as a plain new channel does
  6673. auto expected = song;
  6674. const auto free = expected.claimFreeInsert();
  6675. CHECK (lynx::addChannelRouted (song, c, 9999) == count + 2 && song.channels.back().mixerTrack == free);
  6676. auto plain = expected;
  6677. CHECK (lynx::addChannelRouted (plain, c) == count + 2 && plain.channels.back().mixerTrack == expected.claimFreeInsert());
  6678. }
  6679. TEST (pr28_generator_dropped_on_an_insert_names_it_like_fl)
  6680. {
  6681. // FL 2026 (round 167u, shots fl3-fl10): 3x Osc dropped on Insert 3, Master and Insert 5's slot 2, then FLEX on
  6682. // Insert 3: channels "3x Osc", "3x Osc #2", "3x Osc #3", "FLEX"; Insert 3 / Master / Insert 5 take the first
  6683. // three names, Insert 3 keeps "3x Osc" when FLEX comes
  6684. auto song = lynx::Song::makeDefault();
  6685. song.channels.clear();
  6686. while (song.mixer.size() < 6)
  6687. song.mixer.emplace_back().name = "Insert " + std::to_string (song.mixer.size());
  6688. song.mixer[4].name = "Drums"; // a name the user gave
  6689. lynx::ChannelDef osc;
  6690. osc.name = "3x Osc";
  6691. lynx::ChannelDef flex;
  6692. flex.name = "FLEX";
  6693. CHECK (lynx::addChannelRouted (song, osc, 3) == 0 && song.channels[0].name == "3x Osc" && song.mixer[3].name == "3x Osc");
  6694. CHECK (lynx::addChannelRouted (song, osc, 0) == 1 && song.channels[1].name == "3x Osc #2" && song.channels[1].mixerTrack == 0
  6695. && song.mixer[0].name == "3x Osc #2");
  6696. CHECK (lynx::addChannelRouted (song, osc, 5) == 2 && song.channels[2].name == "3x Osc #3" && song.mixer[5].name == "3x Osc #3");
  6697. CHECK (lynx::addChannelRouted (song, flex, 3) == 3 && song.channels[3].name == "FLEX" && song.channels[3].mixerTrack == 3
  6698. && song.mixer[3].name == "3x Osc");
  6699. CHECK (lynx::addChannelRouted (song, flex, 4) == 4 && song.channels[4].name == "FLEX #2" && song.mixer[4].name == "Drums");
  6700. // a plain new channel (no insert given) keeps its name and doesn't rename anything, as before
  6701. const auto mixerBefore = song.mixer.size();
  6702. CHECK (lynx::addChannelRouted (song, osc) == 5 && song.channels[5].name == "3x Osc");
  6703. CHECK (song.mixer.size() >= mixerBefore && song.mixer[3].name == "3x Osc" && song.mixer[0].name == "3x Osc #2");
  6704. }
  6705. TEST (workflow_palette_random_colour)
  6706. {
  6707. const auto& palette = lynx::lynxPalette();
  6708. for (std::uint32_t seed = 0; seed < 50; ++seed)
  6709. {
  6710. const auto current = palette[seed % palette.size()];
  6711. const auto c = lynx::randomPaletteColour (current, seed);
  6712. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  6713. }
  6714. }
  6715. TEST (workflow_key_map)
  6716. {
  6717. using namespace lynx::keys;
  6718. KeyMap map;
  6719. const auto global = Context::global, rack = Context::channelRack;
  6720. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  6721. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  6722. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  6723. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  6724. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  6725. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  6726. // panel context: only while focused
  6727. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  6728. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  6729. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  6730. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  6731. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  6732. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  6733. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  6734. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  6735. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  6736. CHECK (typingPianoNote ('Z') == 48 && typingPianoNote ('q') == 60 && typingPianoNote ('P') == 76 && typingPianoNote ('L') == -1);
  6737. CHECK (map.conflicts().empty());
  6738. // chord text
  6739. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  6740. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  6741. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  6742. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  6743. for (const auto& a : actions())
  6744. for (const auto& c : a.defaults)
  6745. CHECK (parseChord (toString (c)) == c);
  6746. // JSON overrides
  6747. std::vector<std::string> errors;
  6748. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  6749. CHECK (errors.size() == 2);
  6750. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  6751. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  6752. map.setChords ("pattern.previous", { { right, ctrl } });
  6753. CHECK (map.conflicts().size() == 1);
  6754. KeyMap reloaded;
  6755. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  6756. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  6757. map.resetToDefaults();
  6758. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  6759. }
  6760. TEST (workflow_workspace_layout)
  6761. {
  6762. using namespace lynx::workspace;
  6763. auto l = Layout::makeDefault (1280, 620);
  6764. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  6765. for (const auto& p : l.panels)
  6766. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  6767. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  6768. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  6769. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  6770. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  6771. // toggle: show+focus, focus, hide
  6772. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  6773. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  6774. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  6775. l.toggleMaximised (Panel::playlist);
  6776. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  6777. // save and load at another size: positions scale
  6778. l[Panel::mixer].detached = true;
  6779. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  6780. const auto text = l.serialise();
  6781. const auto big = Layout::parse (text, 2560, 1240);
  6782. CHECK (big.has_value());
  6783. if (big)
  6784. {
  6785. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  6786. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  6787. CHECK (big->order == l.order && big->focused == l.focused);
  6788. }
  6789. 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));
  6790. // resize keeps panels inside and edges glued to the border
  6791. l.resize (1000, 500);
  6792. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  6793. for (const auto& p : l.panels)
  6794. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  6795. // off-screen panels come back
  6796. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  6797. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  6798. // snapping: to the desktop, to another panel, and resizing one edge
  6799. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  6800. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  6801. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  6802. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  6803. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  6804. }
  6805. TEST (workflow_control_math)
  6806. {
  6807. using namespace lynx::controls;
  6808. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  6809. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  6810. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  6811. 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);
  6812. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  6813. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  6814. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  6815. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  6816. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  6817. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  6818. TapTempo tap;
  6819. CHECK (! tap.tap (1000.0));
  6820. CHECK (tap.tap (1500.0) == 120.0);
  6821. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  6822. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  6823. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  6824. }
  6825. TEST (flp_fl11_mixer_slots_and_disabled_channels)
  6826. {
  6827. // REA_rodada80 §3: FL <= 11 files close an effect slot without 0x62 (the slot is the 0xD4 record's second
  6828. // i32); the channel's 0x00 byte is its Channel Rack switch
  6829. if (realFlpDir.empty())
  6830. return;
  6831. auto load = [] (const std::filesystem::path& p) {
  6832. std::ifstream in (p, std::ios::binary);
  6833. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  6834. return bytes;
  6835. };
  6836. const auto base = std::filesystem::path (realFlpDir);
  6837. if (const auto b = load (base / "System" / "Render tests" / "Fruity Phase Inverter.flp"); ! b.empty())
  6838. {
  6839. const auto project = flp::parseProject (b);
  6840. CHECK (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1);
  6841. if (project.mixer.size() > 2 && project.mixer[1].slots.size() == 1)
  6842. CHECK (project.mixer[1].slots[0].index == 0 && project.mixer[1].slots[0].plugin == "Fruity Wrapper"
  6843. && ! project.mixer[1].slots[0].state.empty());
  6844. }
  6845. if (const auto b = load (base / "Demo projects" / "Performance mode" / "nucleon - Steptime.flp"); ! b.empty())
  6846. {
  6847. const auto project = flp::parseProject (b);
  6848. CHECK (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4);
  6849. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6850. for (int i = 0; i < 4; ++i)
  6851. CHECK (project.mixer[3].slots[(std::size_t) i].index == i);
  6852. const char* names[] = { "Fruity Delay Bank", "Fruity Reeverb 2", "Maximus", "Fruity Parametric EQ 2" };
  6853. if (project.mixer.size() > 4 && project.mixer[3].slots.size() == 4)
  6854. for (int i = 0; i < 4; ++i)
  6855. CHECK (project.mixer[3].slots[(std::size_t) i].plugin == names[i]);
  6856. }
  6857. if (const auto b = load (base / "Demo projects" / "Product demos" / "Synth_dfr - Overcome - Sytrus.flp"); ! b.empty())
  6858. {
  6859. const auto project = flp::parseProject (b);
  6860. int disabled = 0;
  6861. for (const auto& c : project.channels)
  6862. disabled += ! c.enabled;
  6863. CHECK (disabled == 2);
  6864. const auto song = lynx::importFlp (project);
  6865. int muted = 0;
  6866. for (const auto& c : song.channels)
  6867. muted += c.muted;
  6868. CHECK (muted == 2);
  6869. }
  6870. }
  6871. TEST (three_osc_host_hq_render_box_filters_aliasing_shapes)
  6872. {
  6873. // 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
  6874. // copies at gain g/k; a sine is not oversampled
  6875. auto state = [] (std::int32_t shape) {
  6876. std::vector<std::uint8_t> st (0x60, 0);
  6877. const std::int32_t version = 14;
  6878. std::memcpy (st.data(), &version, 4);
  6879. std::memcpy (st.data() + 4 + 4 * 1, &shape, 4); // osc 1 shape; the mix knobs at 0: osc 1 alone
  6880. std::vector<std::uint8_t> snap (364, 0);
  6881. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  6882. for (int k = 0; k < 5; ++k)
  6883. std::memcpy (snap.data() + 0x44 * k, neutral, sizeof (neutral));
  6884. const std::int32_t modX = 256, four = 4, m101 = -101;
  6885. std::memcpy (snap.data() + 0x44, &four, 4);
  6886. std::memcpy (snap.data() + 0x44 + 64, &m101, 4);
  6887. std::memcpy (snap.data() + 0x15c, &modX, 4);
  6888. st.insert (st.end(), snap.begin(), snap.end());
  6889. return st;
  6890. };
  6891. static bool hostHq = false;
  6892. auto render = [&] (std::int32_t shape, bool hq) {
  6893. hostHq = hq;
  6894. auto inst = lynx::makeThreeOscInstrument ([]() noexcept { return hostHq; });
  6895. lynx::ChannelDef c;
  6896. c.instrument = lynx::InstrumentKind::threeosc;
  6897. c.pluginState = state (shape);
  6898. inst->configure (c, 140.0);
  6899. inst->prepare (44100.0);
  6900. inst->noteOn (96, 0.78f);
  6901. std::vector<float> l (4096), r (4096);
  6902. inst->render (l.data(), r.data(), 4096, 1.0f, 1.0f);
  6903. return l;
  6904. };
  6905. auto compare = [&] (std::int32_t shape) {
  6906. const auto live = render (shape, false), hq = render (shape, true);
  6907. double diff = 0.0, energy = 0.0;
  6908. for (std::size_t i = 0; i < live.size(); ++i)
  6909. diff += std::abs (live[i] - hq[i]), energy += std::abs (live[i]);
  6910. return std::make_pair (energy, diff);
  6911. };
  6912. CHECK (lynx::threeOscStateUsable (state (3)));
  6913. const auto [sawEnergy, sawDiff] = compare (3);
  6914. CHECK (sawEnergy > 1.0 && sawDiff > 1.0e-3); // a saw renders differently in the host's HQ mode
  6915. const auto [sineEnergy, sineDiff] = compare (0);
  6916. CHECK (sineEnergy > 1.0 && sineDiff == 0.0); // a sine does not
  6917. }
  6918. namespace
  6919. {
  6920. // The runner's options (round speed185a). They choose WHICH tests run and how the output looks, never what a test checks.
  6921. // [folder] the FL Data folder (realFlpDir), as before
  6922. // --list print the selected test names, one per line, and run nothing
  6923. // --filter a,b run only the tests whose name contains one of these; several --filter options must all match
  6924. // (the LYNX_TEST variable works as before, as one more --filter)
  6925. // --exclude a,b skip the tests whose name contains one of these
  6926. // --shard i/n run only shard i of n (1-based): the selected tests split into n disjoint sets that together
  6927. // cover them all. With --weights the sets are balanced by time, otherwise test k goes to k mod n.
  6928. // --weights file "<name> <ms>" lines (--times output) used to balance the shards; unknown tests weigh 50 ms
  6929. // --times file write "<name> <ms>" per test that ran
  6930. // --no-times no per-test time lines
  6931. // Output: each test's name on its own line (as before), its FAILED lines, then " time <ms> ms"; at the end the slowest
  6932. // tests and the unchanged "N checks, M failures" line. stdout is line-buffered so a long test never looks hung.
  6933. struct RunnerOptions
  6934. {
  6935. bool list = false, times = true;
  6936. std::vector<std::vector<std::string>> filters; // (each must match: one of its names)
  6937. std::vector<std::string> excludes;
  6938. int shard = 0, shards = 1;
  6939. std::string weightsFile, timesFile;
  6940. };
  6941. std::vector<std::string> splitCommas (const std::string& s)
  6942. {
  6943. std::vector<std::string> out;
  6944. std::string cur;
  6945. for (char c : s + ",")
  6946. if (c == ',')
  6947. {
  6948. if (! cur.empty())
  6949. out.push_back (cur);
  6950. cur.clear();
  6951. }
  6952. else
  6953. cur += c;
  6954. return out;
  6955. }
  6956. bool parseRunnerOptions (int argc, char** argv, RunnerOptions& o)
  6957. {
  6958. for (int i = 1; i < argc; ++i)
  6959. {
  6960. const std::string a = argv[i];
  6961. auto value = [&] () -> std::optional<std::string> {
  6962. if (i + 1 >= argc)
  6963. return std::nullopt;
  6964. return std::string (argv[++i]);
  6965. };
  6966. if (a == "--list")
  6967. o.list = true;
  6968. else if (a == "--no-times")
  6969. o.times = false;
  6970. else if (a == "--filter" || a == "--exclude" || a == "--shard" || a == "--weights" || a == "--times")
  6971. {
  6972. const auto v = value();
  6973. if (! v)
  6974. {
  6975. std::fprintf (stderr, "lynx_tests: %s needs a value\n", a.c_str());
  6976. return false;
  6977. }
  6978. if (a == "--filter")
  6979. o.filters.push_back (splitCommas (*v));
  6980. else if (a == "--exclude")
  6981. for (auto& f : splitCommas (*v)) o.excludes.push_back (f);
  6982. else if (a == "--weights")
  6983. o.weightsFile = *v;
  6984. else if (a == "--times")
  6985. o.timesFile = *v;
  6986. else
  6987. {
  6988. int k = 0, n = 0;
  6989. char tail = 0;
  6990. if (std::sscanf (v->c_str(), "%d/%d%c", &k, &n, &tail) != 2 || n < 1 || k < 1 || k > n)
  6991. {
  6992. std::fprintf (stderr, "lynx_tests: --shard wants i/n with 1 <= i <= n, not %s\n", v->c_str());
  6993. return false;
  6994. }
  6995. o.shard = k - 1;
  6996. o.shards = n;
  6997. }
  6998. }
  6999. else if (a.size() > 2 && a.compare (0, 2, "--") == 0)
  7000. {
  7001. std::fprintf (stderr, "lynx_tests: unknown option %s\n", a.c_str());
  7002. return false;
  7003. }
  7004. else
  7005. realFlpDir = a;
  7006. }
  7007. if (const char* only = std::getenv ("LYNX_TEST")) // run only the tests whose name contains this
  7008. o.filters.push_back ({ only });
  7009. return true;
  7010. }
  7011. // which of the selected tests (indices into `names`) belong to shard o.shard: a partition of all of them over o.shards
  7012. std::vector<std::size_t> shardOf (const std::vector<const char*>& names, const RunnerOptions& o)
  7013. {
  7014. std::vector<std::size_t> all (names.size());
  7015. for (std::size_t k = 0; k < all.size(); ++k)
  7016. all[k] = k;
  7017. if (o.shards <= 1)
  7018. return all;
  7019. std::map<std::string, double> weight;
  7020. if (! o.weightsFile.empty())
  7021. {
  7022. std::ifstream in (o.weightsFile);
  7023. std::string name;
  7024. double ms = 0.0;
  7025. while (in >> name >> ms)
  7026. weight[name] = ms;
  7027. }
  7028. std::vector<int> owner (names.size(), 0);
  7029. if (weight.empty())
  7030. for (std::size_t k = 0; k < names.size(); ++k)
  7031. owner[k] = (int) (k % (std::size_t) o.shards);
  7032. else
  7033. {
  7034. // longest first onto the least loaded shard (ties: lower index), deterministic for a given list and file
  7035. auto order = all;
  7036. auto w = [&] (std::size_t k) { const auto it = weight.find (names[k]); return it == weight.end() ? 50.0 : it->second; };
  7037. std::stable_sort (order.begin(), order.end(), [&] (std::size_t a, std::size_t b) { return w (a) > w (b); });
  7038. std::vector<double> load ((std::size_t) o.shards, 0.0);
  7039. for (auto k : order)
  7040. {
  7041. const auto s = (std::size_t) (std::min_element (load.begin(), load.end()) - load.begin());
  7042. owner[k] = (int) s;
  7043. load[s] += w (k);
  7044. }
  7045. }
  7046. std::vector<std::size_t> mine;
  7047. for (auto k : all)
  7048. if (owner[k] == o.shard)
  7049. mine.push_back (k); // (in registry order: a shard runs its tests in the usual order)
  7050. return mine;
  7051. }
  7052. } // namespace
  7053. int main (int argc, char** argv)
  7054. {
  7055. std::setvbuf (stdout, nullptr, _IOLBF, 0);
  7056. RunnerOptions opt;
  7057. if (! parseRunnerOptions (argc, argv, opt))
  7058. return 2;
  7059. auto& tests = registry();
  7060. std::vector<const char*> names;
  7061. std::vector<std::size_t> selected;
  7062. for (std::size_t k = 0; k < tests.size(); ++k)
  7063. {
  7064. const char* name = tests[k].first;
  7065. auto has = [name] (const std::string& part) { return std::strstr (name, part.c_str()) != nullptr; };
  7066. if (! std::all_of (opt.filters.begin(), opt.filters.end(), [&] (const auto& any) { return std::any_of (any.begin(), any.end(), has); }))
  7067. continue;
  7068. if (std::any_of (opt.excludes.begin(), opt.excludes.end(), has))
  7069. continue;
  7070. names.push_back (name);
  7071. selected.push_back (k);
  7072. }
  7073. const auto mine = shardOf (names, opt);
  7074. if (opt.list)
  7075. {
  7076. for (auto k : mine)
  7077. std::printf ("%s\n", names[k]);
  7078. return 0;
  7079. }
  7080. std::vector<std::pair<double, const char*>> took;
  7081. for (auto k : mine)
  7082. {
  7083. auto& [name, fn] = tests[selected[k]];
  7084. std::printf ("%s\n", name);
  7085. const auto start = std::chrono::steady_clock::now();
  7086. fn();
  7087. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - start).count();
  7088. took.emplace_back (ms, name);
  7089. if (opt.times)
  7090. std::printf (" time %.0f ms\n", ms);
  7091. }
  7092. if (! opt.timesFile.empty())
  7093. {
  7094. std::ofstream out (opt.timesFile);
  7095. for (auto& [ms, name] : took)
  7096. out << name << ' ' << (long long) std::llround (ms) << '\n';
  7097. }
  7098. if (opt.times && took.size() > 1)
  7099. {
  7100. double total = 0.0;
  7101. for (auto& t : took)
  7102. total += t.first;
  7103. auto slow = took;
  7104. std::sort (slow.begin(), slow.end(), [] (const auto& a, const auto& b) { return a.first > b.first; });
  7105. slow.resize (std::min<std::size_t> (slow.size(), 15));
  7106. std::printf ("\n %zu tests in %.1f s%s; slowest:\n", took.size(), total / 1000.0,
  7107. opt.shards > 1 ? (" (shard " + std::to_string (opt.shard + 1) + "/" + std::to_string (opt.shards) + ")").c_str() : "");
  7108. for (auto& [ms, name] : slow)
  7109. std::printf (" %8.1f s %s\n", ms / 1000.0, name);
  7110. }
  7111. std::printf ("\n%d checks, %d failures\n", checks, failures);
  7112. return failures == 0 ? 0 : 1;
  7113. }
  7114. TEST (synth_cutoff_keytrack)
  7115. {
  7116. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  7117. auto rms = [] (float keyTrack, int key) {
  7118. lynx::SynthSettings s;
  7119. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7120. s.cutoff = 130.81f;
  7121. s.keyTrack = keyTrack;
  7122. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7123. inst->prepare (48000.0);
  7124. lynx::ChannelDef c;
  7125. c.synth = s;
  7126. inst->configure (c, 120.0);
  7127. inst->noteOn (key, 1.0f);
  7128. std::vector<float> l (24000), r (24000);
  7129. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  7130. double e = 0.0;
  7131. for (int i = 4800; i < 24000; ++i)
  7132. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7133. return std::sqrt (e / 19200.0);
  7134. };
  7135. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  7136. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  7137. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  7138. }
  7139. namespace
  7140. {
  7141. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  7142. {
  7143. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7144. inst->prepare (48000.0);
  7145. lynx::ChannelDef c;
  7146. c.synth = s;
  7147. inst->configure (c, 120.0);
  7148. inst->noteOn (key, 1.0f);
  7149. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  7150. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  7151. return l;
  7152. }
  7153. double goertzel (const std::vector<float>& x, double f, int from, int to)
  7154. {
  7155. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  7156. double s1 = 0.0, s2 = 0.0;
  7157. for (int i = from; i < to; ++i)
  7158. {
  7159. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  7160. s2 = s1;
  7161. s1 = s0;
  7162. }
  7163. return s1 * s1 + s2 * s2 - k * s1 * s2;
  7164. }
  7165. } // namespace
  7166. TEST (synth_pitch_sweep_ring_and_bands)
  7167. {
  7168. lynx::SynthSettings sine;
  7169. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7170. sine.cutoff = 20000.0f;
  7171. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  7172. auto sweep = sine;
  7173. sweep.sweep = 1.0f;
  7174. sweep.sweepTime = 0.01f;
  7175. const auto a = renderSynthNote (sweep, 69, 24000);
  7176. auto crossings = [] (const std::vector<float>& x, int to) {
  7177. int n = 0;
  7178. for (int i = 1; i < to; ++i)
  7179. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  7180. return n;
  7181. };
  7182. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  7183. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  7184. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  7185. auto ring = sine;
  7186. ring.am = ring.ring = true;
  7187. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  7188. const auto b = renderSynthNote (ring, 69, 24000);
  7189. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  7190. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  7191. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  7192. auto bands = sine;
  7193. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  7194. auto rms = [] (const std::vector<float>& x) {
  7195. double e = 0.0;
  7196. for (std::size_t i = 4800; i < x.size(); ++i)
  7197. e += (double) x[i] * x[i];
  7198. return std::sqrt (e / (double) (x.size() - 4800));
  7199. };
  7200. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  7201. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  7202. }
  7203. TEST (love_philter_map_evaluation)
  7204. {
  7205. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  7206. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  7207. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  7208. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  7209. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  7210. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  7211. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  7212. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  7213. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  7214. }
  7215. TEST (lofi_holds_and_quantizes)
  7216. {
  7217. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  7218. fx->prepare (48000.0);
  7219. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  7220. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  7221. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  7222. std::vector<float> l (170), r (170);
  7223. for (std::size_t i = 0; i < l.size(); ++i)
  7224. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  7225. const auto original = l;
  7226. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  7227. for (int i = 0; i < 17; ++i)
  7228. CHECK (l[(std::size_t) i] == l[0]);
  7229. CHECK (l[17] != l[16]);
  7230. const auto step = 2.0f * 2049.0f / 65536.0f;
  7231. for (auto v : l)
  7232. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  7233. CHECK (std::abs (l[0] - original[0]) <= step);
  7234. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  7235. }
  7236. namespace
  7237. {
  7238. lynx::EffectParams effectorParams (int type, float x, float y)
  7239. {
  7240. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  7241. slot.params[0] = (float) type / 11.0f;
  7242. slot.params[3] = x;
  7243. slot.params[4] = y;
  7244. return slot.params;
  7245. }
  7246. } // namespace
  7247. TEST (effector_algorithms)
  7248. {
  7249. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  7250. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  7251. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  7252. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  7253. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  7254. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  7255. fx->prepare (48000.0);
  7256. fx->setSongPosition (0.0);
  7257. auto r = in;
  7258. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  7259. return in;
  7260. };
  7261. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  7262. {
  7263. auto p = effectorParams (0, 0.5f, 1.0f);
  7264. const auto out = run (p, std::vector<float> (4800, 0.5f));
  7265. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  7266. const auto small = run (p, std::vector<float> (4800, 0.05f));
  7267. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  7268. }
  7269. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  7270. {
  7271. auto p = effectorParams (11, 0.0f, 0.0f);
  7272. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7273. int crossings = 0;
  7274. for (std::size_t i = 4800; i < out.size(); ++i)
  7275. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  7276. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  7277. }
  7278. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  7279. {
  7280. auto p = effectorParams (8, 1.0f, 0.0f);
  7281. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  7282. const auto out = run (p, std::vector<float> (48000, 1.0f));
  7283. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  7284. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  7285. }
  7286. // Reverb with Wet = 0: the dry passes intact
  7287. {
  7288. auto p = effectorParams (6, 0.5f, 0.5f);
  7289. p[2] = 0.0f;
  7290. std::vector<float> in (4800);
  7291. for (std::size_t i = 0; i < in.size(); ++i)
  7292. in[i] = (float) std::sin (0.01 * (double) i);
  7293. const auto out = run (p, in);
  7294. for (std::size_t i = 0; i < in.size(); ++i)
  7295. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  7296. }
  7297. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  7298. {
  7299. auto p = effectorParams (5, 0.7f, 0.5f);
  7300. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  7301. std::vector<float> in (12000, 0.0f);
  7302. in[0] = 1.0f;
  7303. const auto out = run (p, in);
  7304. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  7305. CHECK (std::abs (out[3000]) < 1e-6f);
  7306. }
  7307. // Stutter: repeats the recorded start every period
  7308. {
  7309. auto p = effectorParams (9, 0.0f, 0.0f);
  7310. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  7311. std::vector<float> in (9000);
  7312. for (std::size_t i = 0; i < in.size(); ++i)
  7313. in[i] = (float) i / 9000.0f;
  7314. const auto out = run (p, in);
  7315. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  7316. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  7317. }
  7318. }
  7319. TEST (crossover_linkwitz_riley_sums_flat)
  7320. {
  7321. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  7322. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  7323. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  7324. {
  7325. auto run = [&] (bool high) {
  7326. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7327. fx->prepare (48000.0);
  7328. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7329. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  7330. slot.params[high ? 0 : 3] = 1.0f;
  7331. slot.params[high ? 1 : 4] = value;
  7332. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  7333. std::vector<float> l (24000), r (24000);
  7334. for (std::size_t i = 0; i < l.size(); ++i)
  7335. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  7336. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7337. return l;
  7338. };
  7339. const auto low = run (false), high = run (true);
  7340. double peak = 0.0;
  7341. for (std::size_t i = 12000; i < low.size(); ++i)
  7342. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  7343. CHECK (std::abs (peak - 1.0) < 0.02);
  7344. }
  7345. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  7346. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  7347. fx->prepare (48000.0);
  7348. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  7349. slot.params[3] = 1.0f;
  7350. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  7351. slot.params[5] = 1.0f;
  7352. std::vector<float> l (24000), r (24000);
  7353. for (std::size_t i = 0; i < l.size(); ++i)
  7354. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  7355. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7356. float peak = 0.0f;
  7357. for (std::size_t i = 12000; i < l.size(); ++i)
  7358. peak = std::max (peak, std::abs (l[i]));
  7359. CHECK (peak < 1e-4f);
  7360. }
  7361. TEST (rhythm_gain_and_autopan)
  7362. {
  7363. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  7364. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  7365. slot.params[0] = 1.0f / 64.0f;
  7366. slot.params[1] = 3.0f / 12.0f;
  7367. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  7368. for (int k = 0; k < 3; ++k)
  7369. for (int j = 0; j < 3; ++j)
  7370. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  7371. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  7372. fx->prepare (48000.0);
  7373. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  7374. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  7375. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  7376. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  7377. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  7378. CHECK (l[12000] < 1e-3f);
  7379. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  7380. // positive tension: changes quickly at the start of the segment
  7381. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  7382. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  7383. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7384. pan.params[0] = 1.0f / 64.0f;
  7385. pan.params[1] = 1.0f;
  7386. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  7387. ap->prepare (48000.0);
  7388. ap->setSongPosition (0.0);
  7389. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  7390. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  7391. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  7392. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  7393. }
  7394. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  7395. {
  7396. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  7397. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  7398. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7399. slot.params[16] = 1.0f;
  7400. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7401. fx->prepare (48000.0);
  7402. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  7403. std::uint32_t seed = 1;
  7404. for (std::size_t i = 0; i < modL.size(); ++i)
  7405. {
  7406. seed = seed * 1664525u + 1013904223u;
  7407. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7408. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7409. }
  7410. for (int done = 0; done < 48000; done += 480)
  7411. {
  7412. fx->setSidechain (modL.data() + done, modL.data() + done);
  7413. fx->setSidechain2 (car.data() + done, car.data() + done);
  7414. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  7415. }
  7416. auto rms = [&] (int from, int to) {
  7417. double e = 0.0;
  7418. for (int i = from; i < to; ++i)
  7419. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7420. return std::sqrt (e / (to - from));
  7421. };
  7422. CHECK (rms (6000, 20000) > 0.01);
  7423. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  7424. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  7425. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  7426. for (int done = 0; done < 4800; done += 480)
  7427. {
  7428. fx->setSidechain (nullptr, nullptr);
  7429. fx->setSidechain2 (car.data() + done, car.data() + done);
  7430. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  7431. }
  7432. CHECK (std::abs (a[4700]) < 0.05f);
  7433. }
  7434. TEST (vocoder_follows_modulator)
  7435. {
  7436. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  7437. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  7438. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  7439. fx->prepare (48000.0);
  7440. std::vector<float> l (48000), r (48000);
  7441. std::uint32_t seed = 1;
  7442. for (std::size_t i = 0; i < l.size(); ++i)
  7443. {
  7444. seed = seed * 1664525u + 1013904223u;
  7445. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7446. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  7447. }
  7448. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  7449. auto rms = [&] (int from, int to) {
  7450. double e = 0.0;
  7451. for (int i = from; i < to; ++i)
  7452. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7453. return std::sqrt (e / (to - from));
  7454. };
  7455. CHECK (rms (6000, 20000) > 0.01);
  7456. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  7457. for (std::size_t i = 0; i < l.size(); ++i)
  7458. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  7459. }
  7460. TEST (fruity_vocoder_engine)
  7461. {
  7462. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  7463. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  7464. std::vector<float> inL (4096), inR (4096);
  7465. for (int i = 0; i < 4096; ++i)
  7466. {
  7467. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  7468. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  7469. }
  7470. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  7471. lynx::fvocoder::State s;
  7472. s.bands = bands;
  7473. s.order = order;
  7474. s.listen = listen;
  7475. s.swap = swap;
  7476. for (std::size_t k = 0; k < levels.size(); ++k)
  7477. s.levels[k] = levels[k];
  7478. s.c = c;
  7479. return s;
  7480. };
  7481. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  7482. std::vector<float> l = inL, r = inR;
  7483. lynx::fvocoder::Engine e;
  7484. e.setState (s);
  7485. e.prepare (rate);
  7486. for (int i = 0; i < 4096;)
  7487. {
  7488. auto len = std::min (block, 4096 - i);
  7489. if (holdAt > i && holdAt < i + len)
  7490. len = holdAt - i;
  7491. if (i == holdAt)
  7492. {
  7493. auto t = e.state();
  7494. t.c[lynx::fvocoder::hold] = 1;
  7495. e.setState (t);
  7496. }
  7497. e.processStereo (l.data() + i, r.data() + i, len);
  7498. i += len;
  7499. }
  7500. return std::make_pair (l, r);
  7501. };
  7502. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  7503. for (const auto& [i, g] : golden)
  7504. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  7505. };
  7506. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  7507. 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 },
  7508. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  7509. const auto outA = run (a, 44100.0, 512, -1);
  7510. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  7511. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  7512. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  7513. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  7514. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  7515. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  7516. { 4095, -0.021947938948869705f } });
  7517. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  7518. 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 });
  7519. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  7520. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  7521. { 4095, -0.11583124846220016f } });
  7522. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  7523. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  7524. const auto bytes = lynx::fvocoder::serialise (a);
  7525. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  7526. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  7527. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  7528. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  7529. if (slot)
  7530. {
  7531. auto fx = lynx::makeEffect (*slot);
  7532. fx->prepare (44100.0);
  7533. std::vector<float> l = inL, r = inR;
  7534. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  7535. CHECK (l == outA.first && r == outA.second);
  7536. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  7537. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  7538. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  7539. }
  7540. const auto parsed = lynx::fvocoder::parse (bytes);
  7541. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  7542. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  7543. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  7544. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  7545. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  7546. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  7547. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  7548. }
  7549. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  7550. TEST (delphi_math_matches_plugin_runtime)
  7551. {
  7552. // arguments where the C library differs from the Delphi run-time in the last bit
  7553. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  7554. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  7555. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  7556. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  7557. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  7558. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  7559. double s = 0.0, c = 0.0;
  7560. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  7561. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  7562. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  7563. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  7564. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  7565. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  7566. }
  7567. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  7568. // 13 map-axis bytes, tone, notes)
  7569. struct VocodexSpec
  7570. {
  7571. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  7572. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  7573. 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 };
  7574. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  7575. 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
  7576. std::vector<std::uint8_t> bytes() const
  7577. {
  7578. std::vector<std::uint8_t> d;
  7579. 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); };
  7580. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7581. i32 (8 << 16);
  7582. for (int m = 0; m < 13; ++m)
  7583. {
  7584. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  7585. const double y1 = m == 8 ? 0.9918 : y0;
  7586. i32 (1), i32 (0), i32 (2);
  7587. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  7588. {
  7589. put (&dx, 8), put (&y, 8);
  7590. i32 (0), i32 (0); // tension (f32 0), flags
  7591. }
  7592. const std::array<std::uint8_t, 20> tail {};
  7593. put (tail.data(), tail.size());
  7594. }
  7595. for (auto v : params)
  7596. i32 (v);
  7597. for (auto v : block)
  7598. i32 (v);
  7599. put (flags.data(), flags.size());
  7600. const std::array<std::uint8_t, 13> axis {};
  7601. put (axis.data(), axis.size());
  7602. d.push_back (3), put ("PWM", 3);
  7603. i32 (1), i32 (60);
  7604. return d;
  7605. }
  7606. };
  7607. std::vector<float> vocodexSaw (int n, double hz, double fs)
  7608. {
  7609. std::vector<float> out ((std::size_t) n);
  7610. for (int i = 0; i < n; ++i)
  7611. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  7612. return out;
  7613. }
  7614. TEST (vocodex_state_parse_and_conversion)
  7615. {
  7616. VocodexSpec spec;
  7617. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  7618. spec.params[17] = -1200;
  7619. const auto raw = spec.bytes();
  7620. const auto s = lynx::vocodex::parseState (raw);
  7621. CHECK (s.has_value());
  7622. if (! s)
  7623. return;
  7624. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  7625. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  7626. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  7627. 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);
  7628. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  7629. const auto v = lynx::vocodexEffect (raw);
  7630. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  7631. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  7632. spec.params[1] = 150; // Soundgoodizer stage
  7633. const auto w = lynx::vocodexEffect (spec.bytes());
  7634. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  7635. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  7636. }
  7637. TEST (vocodex_vocodes_side_inputs)
  7638. {
  7639. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  7640. // output follows the modulator's envelope and fades after it stops
  7641. VocodexSpec spec;
  7642. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7643. slot.state = spec.bytes();
  7644. auto fx = lynx::makeEffect (slot);
  7645. fx->prepare (48000.0);
  7646. CHECK (fx->latencySamples() == 0);
  7647. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7648. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7649. std::uint32_t seed = 7;
  7650. for (std::size_t i = 0; i < mod.size(); ++i)
  7651. {
  7652. seed = seed * 1664525u + 1013904223u;
  7653. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  7654. }
  7655. for (int done = 0; done < 48000; done += 512)
  7656. {
  7657. const int n = std::min (512, 48000 - done);
  7658. fx->setSidechain (mod.data() + done, mod.data() + done);
  7659. fx->setSidechain2 (car.data() + done, car.data() + done);
  7660. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7661. }
  7662. auto rms = [&] (int from, int to) {
  7663. double e = 0.0;
  7664. for (int i = from; i < to; ++i)
  7665. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7666. return std::sqrt (e / (2.0 * (to - from)));
  7667. };
  7668. CHECK (rms (6000, 20000) > 0.02);
  7669. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  7670. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  7671. }
  7672. TEST (vocodex_draft_latency_and_pass_through)
  7673. {
  7674. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  7675. // 16 samples late in Draft
  7676. VocodexSpec spec;
  7677. spec.params[0] = 0; // Wet
  7678. spec.params[4] = 12800; // CarVol = 1
  7679. for (const int draftField : { 1, 0 })
  7680. {
  7681. spec.block[4] = draftField;
  7682. const auto s = lynx::vocodex::parseState (spec.bytes());
  7683. CHECK (s.has_value());
  7684. lynx::vocodex::Engine engine;
  7685. engine.prepare (*s, 44100.0, 300, s->draft());
  7686. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  7687. engine.flush();
  7688. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  7689. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  7690. for (int i = 0; i < 3000; ++i)
  7691. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  7692. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  7693. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  7694. {
  7695. n = std::min (sizes[k % 7], 3000 - done);
  7696. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  7697. }
  7698. const int lag = engine.latencySamples();
  7699. auto maxError = 0.0f;
  7700. for (int i = lag; i < 3000; ++i)
  7701. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  7702. CHECK (maxError == 0.0f);
  7703. }
  7704. }
  7705. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  7706. {
  7707. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  7708. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  7709. VocodexSpec spec;
  7710. spec.params[0] = 2000;
  7711. spec.params[1] = 250;
  7712. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  7713. slot.state = spec.bytes();
  7714. auto fx = lynx::makeEffect (slot);
  7715. fx->prepare (48000.0);
  7716. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  7717. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  7718. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  7719. for (std::size_t i = 0; i < mod.size(); ++i)
  7720. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  7721. auto peak = 0.0f;
  7722. for (int done = 0; done < 48000; done += 512)
  7723. {
  7724. const int n = std::min (512, 48000 - done);
  7725. fx->setSidechain (mod.data() + done, mod.data() + done);
  7726. fx->setSidechain2 (car.data() + done, car.data() + done);
  7727. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  7728. }
  7729. for (std::size_t i = 0; i < l.size(); ++i)
  7730. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  7731. CHECK (peak > 0.5f && peak <= 0.9775f);
  7732. }
  7733. TEST (vocodex_tail_bypass_and_noise)
  7734. {
  7735. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  7736. VocodexSpec spec;
  7737. const auto s = lynx::vocodex::parseState (spec.bytes());
  7738. CHECK (s.has_value());
  7739. lynx::vocodex::Engine engine;
  7740. engine.prepare (*s, 44100.0, 512, false);
  7741. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  7742. for (std::size_t i = 0; i < own.size(); ++i)
  7743. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  7744. for (int b = 0; b < 80; ++b)
  7745. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  7746. CHECK (out == own);
  7747. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  7748. spec.params[6] = 9000;
  7749. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  7750. lynx::vocodex::Engine a;
  7751. a.prepare (*noisy, 44100.0, 512, false);
  7752. std::vector<float> mod (1024), first (1024), second (1024);
  7753. for (std::size_t i = 0; i < mod.size(); ++i)
  7754. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  7755. a.flush();
  7756. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  7757. a.flush();
  7758. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  7759. CHECK (first == second);
  7760. auto energy = 0.0;
  7761. for (auto v : first)
  7762. energy += (double) v * v;
  7763. CHECK (energy > 1e-6 && std::isfinite (energy));
  7764. }
  7765. TEST (synth_spectral_oscillator)
  7766. {
  7767. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  7768. lynx::SynthSettings saw;
  7769. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  7770. saw.cutoff = 20000.0f;
  7771. auto spectral = saw;
  7772. spectral.osc[0].wave = lynx::Waveform::spectral;
  7773. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  7774. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  7775. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  7776. for (const auto h : { 1, 2, 3, 5 })
  7777. {
  7778. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  7779. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  7780. }
  7781. // odd only: no 2nd harmonic
  7782. auto odd = spectral;
  7783. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  7784. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  7785. const auto c = renderSynthNote (odd, 57, 24000);
  7786. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  7787. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  7788. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  7789. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  7790. }
  7791. TEST (compressor_sidechain_ducks_and_orders_mixer)
  7792. {
  7793. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  7794. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7795. slot.params[0] = 0.5f; // threshold
  7796. slot.params[1] = 1.0f; // high ratio
  7797. slot.params[4] = 0.0f;
  7798. auto run = [&] (bool withSidechain) {
  7799. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  7800. fx->prepare (48000.0);
  7801. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  7802. if (withSidechain)
  7803. fx->setSidechain (kick.data(), kick.data());
  7804. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  7805. return l.back();
  7806. };
  7807. const auto alone = run (false), ducked = run (true);
  7808. CHECK (ducked < 0.5f * alone);
  7809. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  7810. auto song = lynx::Song::makeDefault();
  7811. while (song.mixer.size() < 8)
  7812. song.mixer.push_back ({});
  7813. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  7814. comp.sidechain = 7;
  7815. song.mixer[5].effects.push_back (comp);
  7816. const auto order = song.mixerOrder();
  7817. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  7818. CHECK (at (7) < at (5));
  7819. }
  7820. TEST (pitch_correction_pulls_to_allowed_note)
  7821. {
  7822. auto run = [] (const lynx::EffectParams& p, double hz) {
  7823. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7824. fx->prepare (48000.0);
  7825. std::vector<float> l (48000 * 2), r;
  7826. for (std::size_t i = 0; i < l.size(); ++i)
  7827. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  7828. r = l;
  7829. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  7830. // frequency from zero crossings in the last second
  7831. int crossings = 0;
  7832. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  7833. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  7834. return crossings / 2.0;
  7835. };
  7836. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7837. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  7838. for (int k = 0; k < 12; ++k)
  7839. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  7840. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  7841. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  7842. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  7843. }
  7844. TEST (pitch_correction_bypass_is_a_pure_delay)
  7845. {
  7846. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  7847. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  7848. // (REA_rodada30 §2.4/2.5); both are reported as latency
  7849. std::vector<float> in (48000);
  7850. for (std::size_t i = 0; i < in.size(); ++i)
  7851. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  7852. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  7853. auto run = [&] (bool formant, float gender, int& latency) {
  7854. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7855. fx->prepare (48000.0);
  7856. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  7857. slot.params[2] = 1.0f;
  7858. slot.params[29] = formant ? 1.0f : 0.0f;
  7859. slot.params[30] = gender;
  7860. auto l = in, r = in;
  7861. for (std::size_t i = 0; i < l.size(); i += 480)
  7862. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  7863. latency = fx->latencySamples();
  7864. return l;
  7865. };
  7866. int latency = 0;
  7867. auto plain = run (false, 0.5f, latency);
  7868. CHECK (latency == 9);
  7869. auto worst = 0.0f;
  7870. for (std::size_t i = 4000; i < in.size(); ++i)
  7871. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  7872. CHECK (worst < 1e-5f);
  7873. auto formant = run (true, 0.8f, latency);
  7874. CHECK (latency == 257);
  7875. worst = 0.0f;
  7876. for (std::size_t i = 4000; i < in.size(); ++i)
  7877. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  7878. CHECK (worst < 1e-5f);
  7879. }
  7880. TEST (pitch_correction_formant_path)
  7881. {
  7882. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  7883. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  7884. auto run = [] (float gender) {
  7885. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  7886. fx->prepare (48000.0);
  7887. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  7888. slot.params[29] = 1.0f;
  7889. slot.params[30] = gender;
  7890. std::vector<float> l (48000 * 2), r;
  7891. for (std::size_t i = 0; i < l.size(); ++i)
  7892. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  7893. r = l;
  7894. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  7895. l.erase (l.begin(), l.begin() + 48000);
  7896. return l;
  7897. };
  7898. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  7899. auto pitch = [] (const std::vector<float>& x) {
  7900. auto best = 0.0, bestLag = 0.0;
  7901. for (int lag = 60; lag <= 160; ++lag)
  7902. {
  7903. auto sum = 0.0;
  7904. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  7905. sum += (double) x[i] * x[i + (std::size_t) lag];
  7906. if (sum > best)
  7907. {
  7908. best = sum;
  7909. bestLag = lag;
  7910. }
  7911. }
  7912. return 48000.0 / bestLag;
  7913. };
  7914. // brightness: energy of the first difference relative to the energy
  7915. auto brightness = [] (const std::vector<float>& x) {
  7916. auto e = 0.0, d = 0.0;
  7917. for (std::size_t i = 1; i < x.size(); ++i)
  7918. {
  7919. e += (double) x[i] * x[i];
  7920. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  7921. }
  7922. return d / e;
  7923. };
  7924. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  7925. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  7926. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  7927. CHECK (brightness (up) > 1.5 * brightness (neutral));
  7928. CHECK (brightness (down) < 0.7 * brightness (neutral));
  7929. }
  7930. TEST (parallel_container_inner_links_modulate)
  7931. {
  7932. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  7933. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  7934. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  7935. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  7936. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  7937. lfo.params[0] = 1.0f / 64.0f;
  7938. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  7939. container.branches.push_back ({ { gain, lfo }, 1.0f });
  7940. lynx::EffectLink link;
  7941. link.source = 1;
  7942. link.param = 0;
  7943. for (int i = 0; i <= 32; ++i)
  7944. link.curve.push_back (0.5f * (float) i / 32.0f);
  7945. auto levels = [&] (bool linked) {
  7946. auto slot = container;
  7947. if (linked)
  7948. slot.links.push_back (link);
  7949. auto fx = lynx::makeParallelEffect (slot);
  7950. fx->prepare (48000.0);
  7951. float lo = 1e9f, hi = 0.0f;
  7952. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  7953. {
  7954. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  7955. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  7956. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  7957. lo = std::min (lo, std::abs (l[128]));
  7958. hi = std::max (hi, std::abs (l[128]));
  7959. }
  7960. return std::pair { lo, hi };
  7961. };
  7962. const auto [lo, hi] = levels (true);
  7963. CHECK (hi > 0.8f && lo < 0.12f);
  7964. const auto [lo0, hi0] = levels (false);
  7965. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  7966. }
  7967. TEST (synth_spectrum_decays_per_harmonic)
  7968. {
  7969. // 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
  7970. lynx::SynthSettings s;
  7971. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  7972. s.cutoff = 20000.0f;
  7973. s.decay = 0.0f;
  7974. s.sustain = 1.0f;
  7975. s.spectrum[0] = 1.0f;
  7976. s.spectrum[2] = 1.0f;
  7977. s.spectrumDecay[2] = 0.2f;
  7978. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  7979. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  7980. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  7981. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  7982. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  7983. CHECK (late3 < 1e-4 * late1);
  7984. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  7985. // without per-harmonic decay the spectrum stays as it is
  7986. s.spectrumDecay[2] = 0.0f;
  7987. const auto y = renderSynthNote (s, 57, 48000);
  7988. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  7989. }
  7990. TEST (worker_pool_runs_each_index_once_per_round)
  7991. {
  7992. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  7993. // exactly once, and nothing running after parallelFor returns
  7994. lynx::WorkerPool pool (3);
  7995. std::vector<std::atomic<int>> hits (64);
  7996. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  7997. std::atomic<bool> open { false };
  7998. int failures = 0;
  7999. for (int r = 1; r <= 20000; ++r)
  8000. {
  8001. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  8002. for (int i = 0; i < n; ++i)
  8003. hits[(std::size_t) i].store (0);
  8004. round.store (r);
  8005. open.store (true);
  8006. auto fn = [&, r] (int i) {
  8007. if (! open.load())
  8008. outside.fetch_add (1);
  8009. if (round.load() != r)
  8010. wrongRound.fetch_add (1);
  8011. hits[(std::size_t) i].fetch_add (1);
  8012. };
  8013. pool.parallelFor (n, fn);
  8014. open.store (false);
  8015. for (int i = 0; i < n; ++i)
  8016. failures += hits[(std::size_t) i].load() != 1;
  8017. }
  8018. CHECK (failures == 0);
  8019. CHECK (outside.load() == 0);
  8020. CHECK (wrongRound.load() == 0);
  8021. }
  8022. TEST (worker_pool_wakes_sleeping_workers)
  8023. {
  8024. // rounds far apart (the workers have gone to sleep on the semaphore in between): every index still runs once,
  8025. // and the sleeping workers are woken (REA round 87: the Windows build used to poll with growing Sleep()s)
  8026. lynx::WorkerPool pool (3);
  8027. std::vector<std::atomic<int>> hits (16);
  8028. int failures = 0;
  8029. for (int r = 0; r < 12; ++r)
  8030. {
  8031. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros * 4));
  8032. for (auto& h : hits)
  8033. h.store (0);
  8034. auto fn = [&] (int i) {
  8035. std::this_thread::sleep_for (std::chrono::microseconds (200)); // long enough for woken workers to join
  8036. hits[(std::size_t) i].fetch_add (1);
  8037. };
  8038. pool.parallelFor (16, fn);
  8039. for (auto& h : hits)
  8040. failures += h.load() != 1;
  8041. }
  8042. CHECK (failures == 0);
  8043. CHECK (pool.wakeUps() > 0);
  8044. CHECK (pool.indicesRunByWorkers() > 0);
  8045. }
  8046. TEST (worker_pool_park_sends_spinning_workers_to_sleep)
  8047. {
  8048. // round 116: after park() (end of an audio block) the workers sleep at once instead of spinning for spinMicros, so
  8049. // the next parallelFor, well inside the spin window, has to wake them; and work right after park() still runs once
  8050. lynx::WorkerPool pool (3);
  8051. std::vector<std::atomic<int>> hits (8);
  8052. int failures = 0;
  8053. const auto before = pool.wakeUps();
  8054. for (int r = 0; r < 40; ++r)
  8055. {
  8056. for (auto& h : hits)
  8057. h.store (0);
  8058. auto fn = [&] (int i) {
  8059. std::this_thread::sleep_for (std::chrono::microseconds (100));
  8060. hits[(std::size_t) i].fetch_add (1);
  8061. };
  8062. pool.parallelFor (8, fn);
  8063. pool.park();
  8064. if (r % 2 == 0)
  8065. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros / 5));
  8066. for (auto& h : hits)
  8067. failures += h.load() != 1;
  8068. }
  8069. CHECK (failures == 0);
  8070. CHECK (pool.wakeUps() - before >= 20); // parked workers were woken, not found spinning
  8071. }
  8072. TEST (realtime_stats_count_deadline_misses_and_late_calls)
  8073. {
  8074. lynx::RtStats stats;
  8075. // 10 ms deadline: 3 ms, 9 ms (80-100 %), 12 ms (over), then a call 40 ms after the previous one (late)
  8076. stats.record (100.0, 3.0, 10.0);
  8077. stats.record (110.0, 9.0, 10.0);
  8078. stats.record (120.0, 12.0, 10.0);
  8079. stats.record (160.0, 2.0, 10.0);
  8080. const auto s = stats.snapshot();
  8081. CHECK (s.callbacks == 4);
  8082. CHECK (s.overBudget == 1);
  8083. CHECK (s.nearBudget == 1);
  8084. CHECK (s.lateCalls == 1);
  8085. CHECK (std::abs (s.maxLoad - 1.2) < 1e-9);
  8086. CHECK (std::abs (s.averageLoad - 26.0 / 40.0) < 1e-3);
  8087. CHECK (s.histogram[3] == 1 && s.histogram[9] == 1 && s.histogram[12] == 1 && s.histogram[2] == 1);
  8088. CHECK (std::abs (stats.takeWindowMaxLoad() - 1.2) < 1e-9 && stats.takeWindowMaxLoad() == 0.0);
  8089. // a device restart is not a late call
  8090. stats.deviceRestarted();
  8091. stats.record (5000.0, 1.0, 10.0);
  8092. CHECK (stats.snapshot().lateCalls == 1);
  8093. stats.reset();
  8094. CHECK (stats.snapshot().callbacks == 0 && stats.snapshot().maxLoad == 0.0);
  8095. }
  8096. TEST (safe_default_buffer_size)
  8097. {
  8098. const std::vector<int> wasapi { 144, 160, 192, 224, 256, 288, 320, 384, 448, 480, 512, 576, 640, 1024, 2048 };
  8099. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 48000.0) == 512); // the 10 ms default becomes 512
  8100. CHECK (lynx::prefs::safeBufferSize (wasapi, 1024, 48000.0) == 1024); // already larger: kept
  8101. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 96000.0) == 1024); // ~10.7 ms at 96 kHz
  8102. CHECK (lynx::prefs::safeBufferSize ({ 64, 128, 256 }, 128, 44100.0) == 256); // nothing reaches 512: the largest
  8103. CHECK (lynx::prefs::safeBufferSize ({}, 0, 44100.0) == 0);
  8104. }
  8105. TEST (linux_audio_driver_choice_and_pipewire_helpers)
  8106. {
  8107. // round 161: a first start on Linux opens PipeWire, then JACK, then ALSA
  8108. using lynx::prefs::DriverKind;
  8109. CHECK (lynx::prefs::driverKind ("PipeWire") == DriverKind::pipewire);
  8110. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", true } }) == "PipeWire");
  8111. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", true } }) == "JACK");
  8112. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", true }, { "JACK", false }, { "PipeWire", false } }) == "ALSA");
  8113. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false }, { "Bela", true } }) == "Bela");
  8114. CHECK (lynx::prefs::preferredLinuxDriver ({ { "ALSA", false } }).empty());
  8115. CHECK (lynx::prefs::pipewireLatency (256, 48000) == "256/48000");
  8116. CHECK (lynx::prefs::pipewireLatency (0, 0) == "1/1");
  8117. CHECK (lynx::prefs::pipewireChannelName ("FL", 0) == "Left");
  8118. CHECK (lynx::prefs::pipewireChannelName ("FR", 1) == "Right");
  8119. CHECK (lynx::prefs::pipewireChannelName ("MONO", 0) == "Mono");
  8120. CHECK (lynx::prefs::pipewireChannelName ("AUX0", 0) == "Aux 1");
  8121. CHECK (lynx::prefs::pipewireChannelName ("AUX15", 15) == "Aux 16");
  8122. CHECK (lynx::prefs::pipewireChannelName ("AUXX", 2) == "AUXX");
  8123. CHECK (lynx::prefs::pipewireChannelName ("", 2) == "Channel 3");
  8124. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"alsa_output.pci-0000_00_1f.3.analog-stereo"})") == "alsa_output.pci-0000_00_1f.3.analog-stereo");
  8125. CHECK (lynx::prefs::pipewireDefaultName (R"({ "name" : "a\"b" })") == "a\"b");
  8126. CHECK (lynx::prefs::pipewireDefaultName ("{}").empty());
  8127. CHECK (lynx::prefs::pipewireDefaultName (R"({"name":"unterminated)").empty());
  8128. CHECK ((lynx::prefs::uniqueDeviceNames ({ "USB Audio", "HDMI", "USB Audio", "USB Audio" })
  8129. == std::vector<std::string> { "USB Audio", "HDMI", "USB Audio (2)", "USB Audio (3)" }));
  8130. }
  8131. TEST (driver_kind_and_shared_mode_buffer_sizes)
  8132. {
  8133. // REA round 97: classic shared WASAPI mixes in whole engine periods, so sizes below it are not offered
  8134. using lynx::prefs::DriverKind;
  8135. CHECK (lynx::prefs::driverKind ("Windows Audio") == DriverKind::wasapiShared);
  8136. CHECK (lynx::prefs::driverKind ("Windows Audio (Exclusive Mode)") == DriverKind::wasapiExclusive);
  8137. CHECK (lynx::prefs::driverKind ("Windows Audio (Low Latency Mode)") == DriverKind::wasapiLowLatency);
  8138. CHECK (lynx::prefs::driverKind ("ASIO") == DriverKind::asio);
  8139. CHECK (lynx::prefs::driverKind ("LYNX ASIO") == DriverKind::lynxAsio);
  8140. CHECK (lynx::prefs::driverKind ("DirectSound") == DriverKind::directSound);
  8141. CHECK (lynx::prefs::driverKind ("ALSA") == DriverKind::alsa);
  8142. CHECK (lynx::prefs::driverKind ("JACK") == DriverKind::jack);
  8143. CHECK (lynx::prefs::driverKind ("CoreAudio") == DriverKind::coreAudio);
  8144. CHECK (lynx::prefs::driverKind ("Some Other Driver") == DriverKind::other);
  8145. const std::vector<int> offered { 144, 192, 256, 480, 512, 1024 };
  8146. CHECK ((lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 480) == std::vector<int> { 480, 512, 1024 }));
  8147. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiExclusive, offered, 480) == offered); // the device's real periods
  8148. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiLowLatency, offered, 480) == offered); // engine periods
  8149. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, { 64, 128 }, 480) == (std::vector<int> { 64, 128 })); // never empty
  8150. CHECK (lynx::prefs::usableBufferSizes (DriverKind::wasapiShared, offered, 0) == offered);
  8151. }
  8152. TEST (linear_phase_convolver_matches_direct_convolution)
  8153. {
  8154. // the block FFT convolver (cached twiddles, REA round 87) equals the direct convolution with its kernel
  8155. lynx::peqfl::FftConvolver c;
  8156. c.init (256);
  8157. std::vector<float> table (129, 1.0f);
  8158. for (int k = 40; k < 129; ++k)
  8159. table[(std::size_t) k] = 0.25f; // a high shelf cut
  8160. c.setKernel (table);
  8161. // impulse response through step()
  8162. std::vector<float> ir (600, 0.0f);
  8163. for (int i = 0; i < 600; ++i)
  8164. {
  8165. float l = i == 0 ? 1.0f : 0.0f, r = 0.0f;
  8166. c.step (l, r);
  8167. ir[(std::size_t) i] = l;
  8168. }
  8169. // then a noise signal, compared with direct convolution by that impulse response
  8170. lynx::peqfl::FftConvolver d;
  8171. d.init (256);
  8172. d.setKernel (table);
  8173. std::vector<float> x (2000), y (2000);
  8174. std::uint32_t seed = 1;
  8175. for (auto& v : x)
  8176. v = (float) ((seed = seed * 1664525u + 1013904223u) >> 8) / 16777216.0f - 0.5f;
  8177. for (std::size_t i = 0; i < x.size(); ++i)
  8178. {
  8179. float l = x[i], r = 0.0f;
  8180. d.step (l, r);
  8181. y[i] = l;
  8182. }
  8183. double worst = 0.0;
  8184. for (std::size_t i = 0; i < x.size(); ++i)
  8185. {
  8186. double direct = 0.0;
  8187. for (std::size_t k = 0; k < ir.size() && k <= i; ++k)
  8188. direct += (double) ir[k] * (double) x[i - k];
  8189. worst = std::max (worst, std::abs (direct - (double) y[i]));
  8190. }
  8191. CHECK (worst < 1e-4);
  8192. // and it delays by 3n/4 with the kernel's centre there
  8193. 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());
  8194. CHECK (peak == 192);
  8195. }
  8196. TEST (patcher_container_is_reused_across_snapshots)
  8197. {
  8198. // an unchanged Patcher keeps its built effect (its children's tails, delay lines and PDC stay put), and a
  8199. // linear-phase Frequency Splitter reports its latency before its first block (REA round 87: it used to be
  8200. // rebuilt on every snapshot and its latency appeared one block late, re-planning PDC in a loop)
  8201. auto song = lynx::Song::makeDefault();
  8202. if (song.mixer.size() < 2)
  8203. song.mixer.resize (2);
  8204. lynx::EffectSlot splitter;
  8205. splitter.kind = lynx::EffectKind::frequencySplitter;
  8206. splitter.params = lynx::makeEffectSlot (lynx::EffectKind::frequencySplitter).params;
  8207. splitter.params[4] = 0.6f; // linear-phase mode
  8208. splitter.params[6] = 0.0f; // quality 0
  8209. lynx::EffectSlot patcher;
  8210. patcher.kind = lynx::EffectKind::parallel;
  8211. patcher.branches.push_back ({ { splitter }, 1.0f });
  8212. patcher.branches.push_back ({ {}, 0.5f });
  8213. song.mixer[1].effects = { patcher };
  8214. const auto a = lynx::RackState::build (song, nullptr, 44100.0);
  8215. const auto b = lynx::RackState::build (song, a.get(), 44100.0);
  8216. CHECK (a->effects[1][0] == b->effects[1][0]);
  8217. CHECK (a->effects[1][0]->latencySamples() == 384); // 3/4 of the 512-point FFT, before any block
  8218. CHECK (a->latencies == a->currentLatencies());
  8219. song.mixer[1].effects[0].branches[0].chain[0].params[0] = 0.25f; // a child's parameter changed: rebuilt
  8220. const auto c = lynx::RackState::build (song, b.get(), 44100.0);
  8221. CHECK (c->effects[1][0] != b->effects[1][0]);
  8222. song.mixer[1].effects[0].branches[1].gain = 0.75f;
  8223. const auto d = lynx::RackState::build (song, c.get(), 44100.0);
  8224. CHECK (d->effects[1][0] != c->effects[1][0]);
  8225. }
  8226. TEST (ladder_filter_follows_toxic_law)
  8227. {
  8228. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  8229. // the input saturation keeps it bounded even at full resonance
  8230. auto level = [] (bool ladder, float res, double hz) {
  8231. lynx::SynthSettings s;
  8232. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8233. s.cutoff = 500.0f;
  8234. s.resonance = res;
  8235. s.ladder = ladder;
  8236. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  8237. const auto x = renderSynthNote (s, key, 48000);
  8238. double e = 0.0, peak = 0.0;
  8239. for (std::size_t i = 24000; i < 48000; ++i)
  8240. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  8241. return std::pair { std::sqrt (e / 24000.0), peak };
  8242. };
  8243. 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
  8244. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  8245. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  8246. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  8247. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  8248. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  8249. const auto loud = level (true, 1.0f, 523.25);
  8250. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  8251. }
  8252. TEST (synth_spectrum_settles_on_target)
  8253. {
  8254. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  8255. lynx::SynthSettings s;
  8256. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  8257. s.cutoff = 20000.0f;
  8258. s.decay = 0.0f;
  8259. s.sustain = 1.0f;
  8260. s.spectrum[0] = 1.0f;
  8261. s.spectrum[2] = 1.0f;
  8262. s.spectrumDecay[2] = 0.1f;
  8263. s.spectrumTarget[2] = 0.25f;
  8264. const auto x = renderSynthNote (s, 57, 48000);
  8265. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  8266. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  8267. }
  8268. TEST (kepler_chorus_is_stereo_and_keeps_level)
  8269. {
  8270. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  8271. lynx::SynthSettings s;
  8272. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8273. s.cutoff = 20000.0f;
  8274. auto dry = s;
  8275. s.chorus = 1;
  8276. auto render = [] (const lynx::SynthSettings& set) {
  8277. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8278. inst->prepare (48000.0);
  8279. lynx::ChannelDef c;
  8280. c.synth = set;
  8281. inst->configure (c, 120.0);
  8282. inst->noteOn (69, 1.0f);
  8283. std::vector<float> l (96000), r (96000);
  8284. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  8285. return std::pair { l, r };
  8286. };
  8287. const auto [l, r] = render (s);
  8288. const auto [dl, dr] = render (dry);
  8289. double diff = 0.0, e = 0.0, ed = 0.0;
  8290. for (std::size_t i = 48000; i < 96000; ++i)
  8291. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  8292. CHECK (diff / 48000.0 > 1e-3);
  8293. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  8294. }
  8295. TEST (fl_pan_law_matches_love_philter_dll)
  8296. {
  8297. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  8298. struct Row { double pan, ll, rr, rl, lr; };
  8299. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  8300. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  8301. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  8302. for (const auto& row : rows)
  8303. {
  8304. float ll, rl, lr, rr;
  8305. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  8306. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  8307. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  8308. }
  8309. }
  8310. TEST (effector_units_match_fl_dll_impulse_responses)
  8311. {
  8312. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  8313. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  8314. struct Case { int kind; float x, y; double v[4]; };
  8315. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  8316. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  8317. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  8318. for (const auto& c : cases)
  8319. {
  8320. auto fx = lynx::effector::make();
  8321. fx->prepare (44100.0);
  8322. lynx::EffectParams p {};
  8323. p[lynx::effector::type] = c.kind / 11.0f;
  8324. p[lynx::effector::wetMix] = 1.0f;
  8325. p[lynx::effector::paramX] = c.x;
  8326. p[lynx::effector::paramY] = c.y;
  8327. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  8328. p[lynx::effector::modRate] = 0.5f;
  8329. p[lynx::effector::inputLevel] = 1.0f;
  8330. p[lynx::effector::outputGain] = 0.5f;
  8331. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  8332. l[0] = r[0] = 1.0f;
  8333. for (int d = 0; d < 1024; d += 256)
  8334. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  8335. const int at[4] { 1, 10, 100, 1000 };
  8336. for (int k = 0; k < 4; ++k)
  8337. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  8338. }
  8339. }
  8340. TEST (harmless_stereo_tremolo_and_vibrato)
  8341. {
  8342. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  8343. lynx::SynthSettings s;
  8344. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8345. s.cutoff = 20000.0f;
  8346. s.stereoTremolo = 1.0f;
  8347. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  8348. inst->prepare (48000.0);
  8349. lynx::ChannelDef c;
  8350. c.synth = s;
  8351. inst->configure (c, 120.0);
  8352. inst->noteOn (69, 1.0f);
  8353. std::vector<float> l (48000), r (48000);
  8354. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  8355. 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; };
  8356. // θ = 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
  8357. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  8358. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  8359. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  8360. auto crossingsSpread = [] (float cents) {
  8361. lynx::SynthSettings v;
  8362. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  8363. v.cutoff = 20000.0f;
  8364. v.vibratoCents = cents;
  8365. const auto x = renderSynthNote (v, 69, 48000);
  8366. std::vector<int> zc;
  8367. for (int i = 24000; i < 48000; ++i)
  8368. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  8369. zc.push_back (i);
  8370. int lo = 1 << 30, hi = 0;
  8371. for (std::size_t k = 1; k < zc.size(); ++k)
  8372. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  8373. return hi - lo;
  8374. };
  8375. CHECK (crossingsSpread (0.0f) <= 1);
  8376. CHECK (crossingsSpread (100.0f) >= 4);
  8377. }
  8378. TEST (physical_models_sound_and_end)
  8379. {
  8380. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  8381. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  8382. {
  8383. lynx::ChannelDef c;
  8384. c.instrument = kind;
  8385. if (kind == lynx::InstrumentKind::sakura)
  8386. {
  8387. c.modelParams.assign (146, 0.5f);
  8388. c.modelParams[111] = 3.0f; // both strings at the note
  8389. c.modelParams[113] = 0.5f; // octave ×1
  8390. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  8391. c.modelParams[31] = 0.0f; // noise exciter
  8392. }
  8393. else
  8394. {
  8395. c.modelParams.assign (38, 0.5f);
  8396. c.modelParams[0] = 1.0f; // retrigger noise
  8397. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  8398. }
  8399. auto inst = lynx::makeInstrument (kind);
  8400. inst->prepare (48000.0);
  8401. inst->configure (c, 120.0);
  8402. inst->noteOn (60, 0.8f);
  8403. std::vector<float> l (48000), r (48000);
  8404. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  8405. inst->noteOff (60);
  8406. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  8407. double early = 0.0;
  8408. for (int i = 0; i < 12000; ++i)
  8409. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8410. CHECK (early > 1e-6);
  8411. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  8412. std::vector<float> tail (480000), tailR (480000);
  8413. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  8414. CHECK (inst->idle());
  8415. }
  8416. }
  8417. TEST (squeeze_matches_fl_model)
  8418. {
  8419. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  8420. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  8421. slot.params[1] = 0.0f; // mix 0: dry only
  8422. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  8423. fx->prepare (44100.0);
  8424. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  8425. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  8426. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  8427. slot.params[1] = 1.0f; // wet only
  8428. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  8429. std::vector<float> a (512), b (512);
  8430. for (int i = 0; i < 512; ++i)
  8431. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  8432. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  8433. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  8434. for (int i = 0; i < 512; i += 37)
  8435. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  8436. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  8437. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  8438. CHECK (z[10] == 0.0f);
  8439. }
  8440. TEST (sakura_matrix_and_effects)
  8441. {
  8442. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  8443. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  8444. auto base = [] {
  8445. lynx::ChannelDef c;
  8446. c.instrument = lynx::InstrumentKind::sakura;
  8447. c.modelParams.assign (146, 0.0f);
  8448. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  8449. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  8450. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  8451. for (const auto& [i, v] : values)
  8452. c.modelParams[(std::size_t) i] = v;
  8453. return c;
  8454. };
  8455. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  8456. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  8457. inst->prepare (48000.0);
  8458. inst->configure (c, 120.0);
  8459. inst->noteOn (60, velocity);
  8460. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8461. for (int s = 0; s < total; s += 256)
  8462. {
  8463. if (s == held)
  8464. inst->noteOff (60);
  8465. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  8466. }
  8467. return std::make_pair (l, std::move (inst));
  8468. };
  8469. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8470. double e = 0.0;
  8471. for (int i = from; i < to; ++i)
  8472. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8473. return e;
  8474. };
  8475. auto c = base();
  8476. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  8477. c.modelParams[80] = 1.0f;
  8478. c.modelParams[82] = 1.0f;
  8479. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  8480. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  8481. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  8482. auto d = base();
  8483. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  8484. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  8485. d.modelParams[64] = 0.5f;
  8486. d.modelParams[69] = 1.0f;
  8487. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  8488. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  8489. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  8490. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  8491. }
  8492. TEST (drumsynth_live_engine)
  8493. {
  8494. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  8495. // sources only, noise from the fallback seed), voice modes, patch level/pan
  8496. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  8497. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  8498. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  8499. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  8500. std::vector<std::uint8_t> blob;
  8501. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  8502. put (3);
  8503. put (66);
  8504. for (int i = 0; i < 120; ++i)
  8505. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  8506. for (int i = 0; i < 120 * 38; ++i)
  8507. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  8508. put (0);
  8509. const auto parsed = lynx::drumSynthLiveParams (blob);
  8510. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  8511. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  8512. blob.resize (500);
  8513. CHECK (! lynx::drumSynthLiveParams (blob));
  8514. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  8515. 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,
  8516. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  8517. lynx::ChannelDef c;
  8518. c.instrument = lynx::InstrumentKind::drumsynth;
  8519. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  8520. for (int s = 0; s < 120; ++s)
  8521. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  8522. for (int i = 0; i < 38; ++i)
  8523. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  8524. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  8525. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  8526. inst->configure (def, 120.0);
  8527. inst->prepare (48000.0);
  8528. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8529. for (int s = 0; s < total; s += 512)
  8530. {
  8531. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  8532. if (keys[k + 1] == s)
  8533. inst->noteOn (keys[k], 100.0f / 128.0f);
  8534. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8535. }
  8536. return std::make_tuple (l, r, std::move (inst));
  8537. };
  8538. {
  8539. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  8540. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  8541. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  8542. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  8543. for (const auto& [i, v] : golden)
  8544. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  8545. std::vector<float> tail (48000 * 4), tailR (tail.size());
  8546. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8547. CHECK (inst->idle()); // the engine reported the voice finished
  8548. }
  8549. auto energy = [] (const std::vector<float>& x, int from, int to) {
  8550. double e = 0.0;
  8551. for (int i = from; i < to; ++i)
  8552. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  8553. return e;
  8554. };
  8555. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  8556. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  8557. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  8558. auto tone = c;
  8559. tone.modelParams[38 * 12 + 14] = 0.0f;
  8560. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  8561. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  8562. auto poly = tone;
  8563. poly.modelParams[38 * 12 + 24] = 1.0f;
  8564. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  8565. double monoDiff = 0.0, polyDiff = 0.0;
  8566. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  8567. 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]);
  8568. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  8569. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  8570. auto right = c;
  8571. right.modelParams[38 * 12 + 1] = 64.0f;
  8572. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  8573. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  8574. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  8575. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  8576. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  8577. }
  8578. TEST (kepler_lfo_bpm_sync)
  8579. {
  8580. // 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)
  8581. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  8582. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  8583. putU32 (12, 9);
  8584. std::memcpy (state.data() + 16, "Kepler-1", 9);
  8585. putU32 (0x19, 4);
  8586. putU32 (0x1D, 34);
  8587. float k[34] {};
  8588. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  8589. k[6] = 1.0f;
  8590. k[10] = 1.0f; // vibrato on
  8591. k[33] = 1.0f;
  8592. std::memcpy (state.data() + 0x21, k, sizeof k);
  8593. lynx::SynthSettings s;
  8594. float gain = 0.0f;
  8595. CHECK (lynx::synthFromKepler (state, s, gain));
  8596. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  8597. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  8598. k[5] = 0.0f; // slowest: 32 beats
  8599. std::memcpy (state.data() + 0x21, k, sizeof k);
  8600. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  8601. k[6] = 0.0f; // free rate in seconds
  8602. std::memcpy (state.data() + 0x21, k, sizeof k);
  8603. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  8604. }
  8605. namespace
  8606. {
  8607. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  8608. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  8609. // velocity map — and the global block. Every other curve is neutral.
  8610. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  8611. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  8612. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  8613. {
  8614. std::vector<std::uint8_t> b;
  8615. 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))); };
  8616. 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))); };
  8617. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  8618. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8619. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  8620. for (const auto& p : pts)
  8621. {
  8622. f64 (p.dx), f64 (p.y);
  8623. const auto t = (float) p.tension;
  8624. std::uint32_t u;
  8625. std::memcpy (&u, &t, 4);
  8626. i32 ((std::int32_t) u);
  8627. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  8628. }
  8629. for (auto h : hdr)
  8630. i32 (h);
  8631. };
  8632. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8633. 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); };
  8634. i32 (18);
  8635. i32 (1 | 4);
  8636. std::vector<std::uint8_t> part;
  8637. std::swap (b, part);
  8638. std::array<std::int32_t, 386> p {};
  8639. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  8640. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  8641. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  8642. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  8643. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  8644. for (const auto& [i, v] : values)
  8645. p[(std::size_t) i] = v;
  8646. if ((features & 2) != 0)
  8647. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  8648. if ((features & 4) != 0)
  8649. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  8650. if ((features & 8) != 0)
  8651. p[75] = 650, p[4] = 600; // pluck, timbre mix
  8652. if ((features & 32) != 0)
  8653. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  8654. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  8655. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  8656. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  8657. p[(std::size_t) i] = v;
  8658. for (auto v : p)
  8659. i32 (v);
  8660. for (int i = 0; i < 12; ++i)
  8661. i32 (i); // module order
  8662. std::array<std::uint8_t, 0x4b> misc {};
  8663. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  8664. const float one = 1.0f;
  8665. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  8666. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  8667. if ((features & 8) != 0)
  8668. put (0x00, 5);
  8669. misc[0x44] = 1;
  8670. if ((features & 4) != 0)
  8671. misc[0x46] = 1;
  8672. b.insert (b.end(), misc.begin(), misc.end());
  8673. 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 };
  8674. for (int k = 0; k < 43; ++k)
  8675. {
  8676. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  8677. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  8678. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  8679. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  8680. i32 (3);
  8681. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  8682. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  8683. 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 },
  8684. { 0.1, 0, 0.2, 0 } },
  8685. { 0, 1, -1, 0, 4 });
  8686. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  8687. 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 });
  8688. else if (k == 4) // filter-1 envelope
  8689. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  8690. else
  8691. flat (neutral);
  8692. for (int a : { 128, 128, 0, 128 })
  8693. i32 (a);
  8694. if (k == 0 && panLfo) // pan LFO
  8695. flat (1.0, on);
  8696. else
  8697. flat (0.5);
  8698. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  8699. i32 (a);
  8700. i32 (0);
  8701. for (int m = 2; m < 11; ++m)
  8702. if (k == 1 && m == 3)
  8703. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  8704. else
  8705. flat (neutral);
  8706. }
  8707. for (int c = 0; c < 26; ++c)
  8708. if ((features & 16) != 0 && c == 4)
  8709. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  8710. else if ((features & 32) != 0 && (c == 22 || c == 23))
  8711. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  8712. else if (c == 3 && (features & 8) != 0)
  8713. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  8714. else if (c == 2 || c == 3)
  8715. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  8716. else if (c == 14)
  8717. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  8718. else
  8719. 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)));
  8720. if (image)
  8721. {
  8722. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  8723. const int cols = 6, rows = 40;
  8724. i32 (0), i32 (0), i32 (1), i32 (0);
  8725. f64 (-1.0), f64 (-1.0), f64 (0.0);
  8726. i32 (cols), i32 (rows);
  8727. i32 (cols * rows);
  8728. for (int c2 = 0; c2 < cols; ++c2)
  8729. for (int r2 = 0; r2 < rows; ++r2)
  8730. {
  8731. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  8732. std::uint32_t u;
  8733. std::memcpy (&u, &v, 4);
  8734. i32 ((std::int32_t) u);
  8735. }
  8736. i32 (cols * rows);
  8737. for (int c2 = 0; c2 < cols; ++c2)
  8738. for (int r2 = 0; r2 < rows; ++r2)
  8739. {
  8740. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  8741. std::uint32_t u;
  8742. std::memcpy (&u, &v, 4);
  8743. i32 ((std::int32_t) u);
  8744. }
  8745. i32 (0);
  8746. b.push_back (0); // (file name: empty)
  8747. }
  8748. std::swap (b, part);
  8749. i32 ((std::int32_t) part.size());
  8750. b.insert (b.end(), part.begin(), part.end());
  8751. std::array<std::int32_t, 53> g {};
  8752. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  8753. for (auto v : g)
  8754. i32 (v);
  8755. for (int i = 0; i < 6; ++i)
  8756. i32 (i);
  8757. std::array<std::uint8_t, 0x3d> gm {};
  8758. const std::int32_t cr = 200, vr = 1;
  8759. std::memcpy (gm.data() + 12, &vr, 4);
  8760. std::memcpy (gm.data() + 24, &cr, 4);
  8761. b.insert (b.end(), gm.begin(), gm.end());
  8762. return b;
  8763. }
  8764. } // namespace
  8765. TEST (harmor_engine)
  8766. {
  8767. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  8768. const auto blob = harmorTestState();
  8769. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  8770. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8771. CHECK (lynx::harmorStateUsable (blob));
  8772. CHECK (lynx::harmorFallbacks (blob).empty());
  8773. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  8774. std::int32_t cutoff = 0;
  8775. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  8776. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  8777. auto broken = blob;
  8778. broken.resize (broken.size() / 2);
  8779. CHECK (! lynx::harmorStateUsable (broken));
  8780. auto old = blob;
  8781. old[0] = 15; // articulators and curves are stored from version 16
  8782. CHECK (! lynx::harmorStateUsable (old));
  8783. lynx::ChannelDef c;
  8784. c.instrument = lynx::InstrumentKind::harmor;
  8785. c.pluginState = blob;
  8786. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  8787. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  8788. inst->configure (def, 120.0);
  8789. inst->prepare (44100.0);
  8790. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8791. inst->noteOn (60, 0.78125f);
  8792. for (int s = 0; s < total; s += 512)
  8793. {
  8794. if (s == offAt)
  8795. inst->noteOff (60);
  8796. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8797. }
  8798. return std::make_tuple (l, r, std::move (inst));
  8799. };
  8800. {
  8801. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  8802. // Mersenne Twister seeded like LYNX's; since flbeta165h LYNX's own values: the test state plays at precision
  8803. // "Average", whose IPP 11-bit Exp/Ln — REA_rodada165g-flbeta165h §3 — the model computes exactly)
  8804. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8805. if (std::getenv ("LYNX_PRINT_HARMOR_ENGINE") != nullptr)
  8806. for (int i : { 0, 100, 1000, 3000, 6000, 7000, 8191 })
  8807. std::printf ("harmor engine %d %.8g %.8g\n", i, (double) l[(std::size_t) i], (double) r[(std::size_t) i]);
  8808. const std::tuple<int, float, float> golden[] {
  8809. { 0, 0.0f, 0.0f }, { 100, 0.0094771842f, 0.042498328f }, { 1000, 0.081412978f, -0.032517102f }, { 3000, 0.059289735f, -0.0024759346f },
  8810. { 6000, 0.029978603f, 0.14549156f }, { 7000, -0.011218854f, 0.041979425f }, { 8191, 0.0031455588f, 0.03857585f } };
  8811. for (const auto& [i, gl, gr] : golden)
  8812. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8813. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  8814. std::vector<float> tail (44100), tailR (tail.size());
  8815. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8816. CHECK (inst->idle());
  8817. CHECK (std::abs (tail.back()) == 0.0f);
  8818. }
  8819. // the reference renderer gives the same samples as the instrument
  8820. {
  8821. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  8822. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  8823. double worst = 0.0;
  8824. for (int i = 0; i < 8192; ++i)
  8825. 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]) });
  8826. CHECK (worst == 0.0);
  8827. }
  8828. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  8829. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  8830. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  8831. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  8832. auto mono = c;
  8833. mono.pluginState = harmorTestState (false); // no pan LFO ...
  8834. const std::int32_t one = 1;
  8835. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  8836. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  8837. double el = 0.0, er = 0.0;
  8838. for (int i = 0; i < 2048; ++i)
  8839. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  8840. CHECK (el < 1e-10 * er && er > 1e-3);
  8841. }
  8842. namespace
  8843. {
  8844. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  8845. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  8846. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  8847. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  8848. std::vector<std::uint8_t> ogunTestState()
  8849. {
  8850. std::vector<std::uint8_t> b;
  8851. 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))); };
  8852. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  8853. 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))); };
  8854. struct Pt { double dx, y, tension; int mode; };
  8855. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  8856. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  8857. for (const auto& p : pts)
  8858. {
  8859. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  8860. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8861. }
  8862. for (auto h : hdr)
  8863. i32 (h);
  8864. };
  8865. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  8866. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  8867. std::array<std::int32_t, 64> P {};
  8868. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  8869. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  8870. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  8871. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  8872. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  8873. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  8874. { 56, 0x00010000 }, // +0x20e: declick
  8875. { 62, 0x00000100 } }; // +0x225: normalise
  8876. for (const auto& [i, v] : values)
  8877. P[(std::size_t) i] = v;
  8878. for (auto v : P)
  8879. i32 (v);
  8880. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  8881. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  8882. b.push_back (0); // comment: 0 characters
  8883. for (int k = 0; k < 13; ++k)
  8884. {
  8885. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  8886. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  8887. i32 (1);
  8888. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  8889. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  8890. 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 });
  8891. else
  8892. flat (1, neutral);
  8893. for (int a : { 128, 128, 128, 128 })
  8894. i32 (a);
  8895. if (k == 1) // cutoff LFO
  8896. curve (2, { { 0, 0.8, 0, 0 } }, on);
  8897. else
  8898. flat (2, 0.5);
  8899. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  8900. i32 (a);
  8901. i32 (0);
  8902. for (int m = 0; m < 4; ++m)
  8903. if (k == 0 && m == 1)
  8904. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  8905. else
  8906. flat (3, neutral);
  8907. }
  8908. flat (3, 0.0); // Forced: none
  8909. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  8910. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  8911. flat (3, 0.5); // Release EQ
  8912. // u32 version + zlib (stored blocks)
  8913. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  8914. for (std::size_t i = 0; i < b.size();)
  8915. {
  8916. const auto len = std::min<std::size_t> (65535, b.size() - i);
  8917. s.push_back (i + len >= b.size() ? 1 : 0);
  8918. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  8919. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  8920. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  8921. i += len;
  8922. }
  8923. std::uint32_t a1 = 1, a2 = 0;
  8924. for (auto c : b)
  8925. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  8926. for (int sh = 24; sh >= 0; sh -= 8)
  8927. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  8928. return s;
  8929. }
  8930. } // namespace
  8931. TEST (ogun_engine)
  8932. {
  8933. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  8934. const auto blob = ogunTestState();
  8935. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  8936. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  8937. CHECK (lynx::ogunStateUsable (blob));
  8938. CHECK (lynx::ogunFallbacks (blob).empty());
  8939. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, lynx::ogunDelayOnIndex + 1));
  8940. auto broken = blob;
  8941. broken.resize (broken.size() - 300);
  8942. CHECK (! lynx::ogunStateUsable (broken));
  8943. auto old = blob;
  8944. old[0] = 13; // a version-18 body read as version 13 (a u32 comment count; versions 10…13: Gen169lTests)
  8945. CHECK (! lynx::ogunStateUsable (old));
  8946. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  8947. // released at 8192; host block 512)
  8948. 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 } };
  8949. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  8950. CHECK (ref.size() == 2 * 16384);
  8951. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  8952. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  8953. const std::tuple<int, float, float> golden[] {
  8954. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  8955. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  8956. { 16383, -0.00465311f, -0.00119627f } };
  8957. for (const auto& [i, gl, gr] : golden)
  8958. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  8959. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  8960. lynx::ChannelDef c;
  8961. c.instrument = lynx::InstrumentKind::ogun;
  8962. c.pluginState = blob;
  8963. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  8964. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  8965. inst->configure (def, 120.0);
  8966. inst->prepare (44100.0);
  8967. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  8968. for (int s = 0; s < total; s += 512)
  8969. {
  8970. if (s == 0)
  8971. inst->noteOn (60, 0.75f);
  8972. if (s == 2048)
  8973. inst->noteOn (67, 0.625f);
  8974. if (s == 6144 && ! held)
  8975. inst->noteOff (60);
  8976. if (s == 8192 && ! held)
  8977. inst->noteOff (67);
  8978. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  8979. }
  8980. return std::make_tuple (l, r, std::move (inst));
  8981. };
  8982. {
  8983. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  8984. double worst = 0.0;
  8985. for (int i = 0; i < 16384; ++i)
  8986. 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]) });
  8987. CHECK (worst == 0.0);
  8988. // the voices end and the effect tails die out: the instrument goes idle
  8989. std::vector<float> tail (44100), tailR (tail.size());
  8990. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  8991. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8992. CHECK (inst->idle());
  8993. }
  8994. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  8995. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  8996. {
  8997. auto dry = c;
  8998. for (int i : { 19, 29, 40, 53 })
  8999. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  9000. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  9001. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  9002. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  9003. double worst = 0.0, peak = 0.0;
  9004. for (int i = 30000; i < 32768; ++i)
  9005. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  9006. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  9007. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  9008. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  9009. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  9010. const auto V = lynx::channelGain (0.78125f);
  9011. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  9012. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  9013. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  9014. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  9015. double ei = 0.0, er0 = 0.0;
  9016. for (int i = 30000; i < 32768; ++i)
  9017. 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];
  9018. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  9019. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  9020. double el = 0.0, er = 0.0;
  9021. for (int i = 0; i < 4096; ++i)
  9022. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  9023. CHECK (el < 1e-10 * er && er > 1e-3);
  9024. }
  9025. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  9026. {
  9027. lynx::setOfflineRendering (true);
  9028. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  9029. lynx::setOfflineRendering (false);
  9030. lynx::ogun::RefOptions q4;
  9031. q4.quality = 4;
  9032. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  9033. double worst = 0.0, diff = 0.0;
  9034. for (int i = 0; i < 8192; ++i)
  9035. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  9036. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  9037. CHECK (worst == 0.0 && diff > 1e-3);
  9038. const std::tuple<int, float, float> golden4[] {
  9039. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  9040. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  9041. for (const auto& [i, gl, gr] : golden4)
  9042. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  9043. }
  9044. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  9045. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  9046. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  9047. {
  9048. struct Sample { int at; float l, r; };
  9049. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  9050. const Variant variants[] {
  9051. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  9052. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  9053. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  9054. { "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 } } },
  9055. { "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 } } },
  9056. { "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 } } },
  9057. { "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 } } },
  9058. { "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 } } },
  9059. { "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 } } },
  9060. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  9061. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  9062. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  9063. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  9064. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  9065. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  9066. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  9067. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  9068. };
  9069. for (const auto& v : variants)
  9070. {
  9071. auto st = blob;
  9072. for (const auto& [i, x] : v.params)
  9073. CHECK (lynx::ogunSetParam (st, i, x));
  9074. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  9075. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  9076. double el = 0.0, er = 0.0, peak = 0.0;
  9077. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9078. 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]) });
  9079. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  9080. for (const auto& smp : v.s)
  9081. 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;
  9082. if (! ok)
  9083. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  9084. CHECK (ok);
  9085. }
  9086. }
  9087. }
  9088. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  9089. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  9090. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  9091. {
  9092. std::vector<std::uint8_t> d;
  9093. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  9094. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  9095. i32 (7);
  9096. for (int copy = 0; copy < 2; ++copy)
  9097. {
  9098. for (auto v : g)
  9099. i32 (v);
  9100. for (const auto& b : bands)
  9101. for (auto v : b)
  9102. i32 (v);
  9103. }
  9104. d.resize (0x1fe, 0);
  9105. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  9106. for (const auto& e : envs)
  9107. {
  9108. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  9109. for (const auto& pt : e)
  9110. {
  9111. const double dx = pt[0], y = pt[1];
  9112. const float t = (float) pt[2];
  9113. put (&dx, 8), put (&y, 8), put (&t, 4);
  9114. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  9115. put (flags, 4);
  9116. }
  9117. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  9118. }
  9119. return d;
  9120. }
  9121. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  9122. std::vector<std::uint8_t> keplerTestState()
  9123. {
  9124. 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 };
  9125. std::vector<std::uint8_t> st;
  9126. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  9127. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  9128. u32 (0), u32 (0), u32 (0), u32 (9);
  9129. for (char c : std::string ("Kepler-1"))
  9130. st.push_back ((std::uint8_t) c);
  9131. st.push_back (0);
  9132. u32 (2), u32 (34);
  9133. for (auto f : panel)
  9134. f32 (f);
  9135. st.push_back (0);
  9136. f32 (1.0f), f32 (0.5f);
  9137. const auto size = (std::uint32_t) st.size() - 12;
  9138. std::memcpy (st.data() + 4, &size, 4);
  9139. return st;
  9140. }
  9141. std::string keplerDll()
  9142. {
  9143. if (realFlpDir.empty())
  9144. return {};
  9145. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  9146. }
  9147. TEST (kepler_engine)
  9148. {
  9149. const auto st = keplerTestState();
  9150. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  9151. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  9152. auto broken = st;
  9153. broken[17] = 'X'; // not "Kepler-1"
  9154. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  9155. auto v3 = st;
  9156. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  9157. CHECK (! lynx::keplerStateUsable (v3));
  9158. const auto dll = keplerDll();
  9159. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9160. return; // no FL installation (the engine needs the user's own DLL tables)
  9161. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  9162. 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 } };
  9163. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  9164. CHECK (y.size() == 2 * 39690);
  9165. if (y.size() != 2 * 39690)
  9166. return;
  9167. double el = 0.0, er = 0.0;
  9168. for (std::size_t i = 0; i < y.size(); i += 2)
  9169. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9170. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  9171. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  9172. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  9173. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  9174. for (const auto& [i, l, r] : golden)
  9175. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  9176. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  9177. lynx::ChannelDef c;
  9178. c.instrument = lynx::InstrumentKind::kepler;
  9179. c.pluginState = st;
  9180. c.samplePath = dll;
  9181. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  9182. inst->configure (c, 120.0);
  9183. inst->prepare (44100.0);
  9184. std::vector<float> l (4096), r (4096);
  9185. inst->noteOn (60, 0.8f);
  9186. double e = 0.0;
  9187. bool finite = true;
  9188. for (int b = 0; b < 8; ++b)
  9189. {
  9190. std::fill (l.begin(), l.end(), 0.0f);
  9191. std::fill (r.begin(), r.end(), 0.0f);
  9192. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9193. for (int i = 0; i < 4096; ++i)
  9194. 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];
  9195. if (b == 3)
  9196. inst->noteOff (60);
  9197. }
  9198. CHECK (finite && e > 1.0);
  9199. for (int b = 0; b < 40; ++b)
  9200. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9201. CHECK (inst->idle());
  9202. }
  9203. // REA round 57 §2: Transistor Bass
  9204. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  9205. std::vector<std::uint8_t> tbassPresetState (const char* name)
  9206. {
  9207. if (realFlpDir.empty())
  9208. return {};
  9209. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  9210. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9211. std::size_t i = 0;
  9212. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9213. ++i;
  9214. i += 8;
  9215. while (i < b.size())
  9216. {
  9217. const auto id = b[i++];
  9218. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9219. if (id >= 192)
  9220. for (int shift = 0; i < b.size(); shift += 7)
  9221. {
  9222. const auto c = b[i++];
  9223. n |= (std::size_t) (c & 0x7F) << shift;
  9224. if (c < 0x80)
  9225. break;
  9226. }
  9227. if (id == 0xD5 && i + n <= b.size())
  9228. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9229. i += n;
  9230. }
  9231. return {};
  9232. }
  9233. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  9234. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  9235. {
  9236. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  9237. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  9238. for (std::size_t at = 4; at + 12 <= s.size();)
  9239. {
  9240. std::uint32_t id;
  9241. std::uint64_t n;
  9242. std::memcpy (&id, s.data() + at, 4);
  9243. std::memcpy (&n, s.data() + at + 4, 8);
  9244. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  9245. at += 12 + (std::size_t) n;
  9246. if (id == 2)
  9247. {
  9248. std::uint64_t m;
  9249. std::memcpy (&m, d.data() + 4, 8);
  9250. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  9251. std::int16_t v;
  9252. std::memcpy (&v, e.data(), 2);
  9253. if (v < 5)
  9254. {
  9255. v = 5;
  9256. std::memcpy (e.data(), &v, 2);
  9257. e.insert (e.end(), 4, 0);
  9258. }
  9259. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  9260. m = e.size();
  9261. d.resize (12);
  9262. std::memcpy (d.data() + 4, &m, 8);
  9263. d.insert (d.end(), e.begin(), e.end());
  9264. }
  9265. const std::uint64_t size = d.size();
  9266. put (&id, 4);
  9267. put (&size, 8);
  9268. put (d.data(), d.size());
  9269. }
  9270. return out;
  9271. }
  9272. std::string tbassDll()
  9273. {
  9274. if (realFlpDir.empty())
  9275. return {};
  9276. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  9277. }
  9278. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9279. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  9280. TEST (plucked_engine)
  9281. {
  9282. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  9283. std::vector<std::uint8_t> st (20);
  9284. std::memcpy (st.data(), std::data (k), 20);
  9285. return st;
  9286. };
  9287. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  9288. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  9289. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  9290. using N = lynx::plucked::RefNote;
  9291. struct Sample { int at; float l, r; };
  9292. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  9293. const std::vector<std::vector<N>> sets {
  9294. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  9295. { { 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 } },
  9296. { { 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 } },
  9297. { { 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 } },
  9298. { { 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 } },
  9299. };
  9300. const Golden goldens[] {
  9301. { 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 } } },
  9302. { 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 } } },
  9303. { 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 } } },
  9304. { 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 } } },
  9305. { 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 } } },
  9306. };
  9307. for (const auto& g : goldens)
  9308. {
  9309. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  9310. const auto length = (int) g.fs * 2;
  9311. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  9312. g.law != 0, g.maxPoly);
  9313. double el = 0.0, er = 0.0;
  9314. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9315. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9316. 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;
  9317. for (const auto& smp : g.s)
  9318. 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;
  9319. if (! ok)
  9320. 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);
  9321. CHECK (ok);
  9322. }
  9323. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  9324. lynx::ChannelDef c;
  9325. c.instrument = lynx::InstrumentKind::plucked;
  9326. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  9327. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  9328. inst->configure (c, 120.0);
  9329. inst->prepare (44100.0);
  9330. std::vector<float> l (1024), r (1024);
  9331. inst->noteOn (57, 0.8f);
  9332. double e = 0.0;
  9333. for (int b = 0; b < 8; ++b)
  9334. {
  9335. std::fill (l.begin(), l.end(), 0.0f);
  9336. std::fill (r.begin(), r.end(), 0.0f);
  9337. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9338. for (int i = 0; i < 1024; ++i)
  9339. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9340. }
  9341. CHECK (e > 1.0 && ! inst->idle());
  9342. inst->noteOff (57);
  9343. for (int b = 0; b < 4; ++b)
  9344. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9345. CHECK (inst->idle());
  9346. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  9347. inst->configure (c, 120.0);
  9348. inst->noteOn (60, 1.0f);
  9349. inst->noteOff (60); // (no gate: the string rings on)
  9350. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  9351. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9352. CHECK (inst->idle());
  9353. }
  9354. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  9355. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  9356. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  9357. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  9358. {
  9359. constexpr int W = 9, H = 6;
  9360. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  9361. 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)); };
  9362. img[0] = 'B', img[1] = 'M';
  9363. 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);
  9364. put (34, 4 * W * H, 4);
  9365. for (int y = 0; y < H; ++y) // bottom-up rows
  9366. for (int x = 0; x < W; ++x)
  9367. {
  9368. const auto yy = H - 1 - y;
  9369. auto* px = img.data() + 54 + 4 * (y * W + x);
  9370. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  9371. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  9372. }
  9373. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  9374. std::size_t k = 0;
  9375. for (auto v : settings)
  9376. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  9377. const auto len = (std::int32_t) img.size();
  9378. std::memcpy (st.data() + 64, &len, 4);
  9379. st.insert (st.end(), img.begin(), img.end());
  9380. return st;
  9381. }
  9382. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  9383. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  9384. {
  9385. if (realFlpDir.empty())
  9386. return {};
  9387. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  9388. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9389. std::size_t i = 0;
  9390. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9391. ++i;
  9392. i += 8;
  9393. while (i < b.size())
  9394. {
  9395. const auto id = b[i++];
  9396. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9397. if (id >= 192)
  9398. for (int shift = 0; i < b.size(); shift += 7)
  9399. {
  9400. const auto c = b[i++];
  9401. n |= (std::size_t) (c & 0x7F) << shift;
  9402. if (c < 0x80)
  9403. break;
  9404. }
  9405. if (id == 0xD5 && i + n <= b.size())
  9406. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9407. i += n;
  9408. }
  9409. return {};
  9410. }
  9411. TEST (beepmap_engine)
  9412. {
  9413. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  9414. CHECK (lynx::beepMapStateUsable (synthA));
  9415. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  9416. auto broken = synthA;
  9417. broken[0] = 2; // versions above 1 are refused
  9418. CHECK (! lynx::beepMapStateUsable (broken));
  9419. broken = synthA;
  9420. broken.resize (broken.size() - 20); // truncated bitmap
  9421. CHECK (! lynx::beepMapStateUsable (broken));
  9422. using N = lynx::beepmap::RefNote;
  9423. struct Sample { int at; float l, r; };
  9424. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  9425. std::vector<std::vector<std::uint8_t>> states {
  9426. synthA,
  9427. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9428. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9429. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  9430. beepMapPresetState ("Default.fst"),
  9431. beepMapPresetState ("Voice.fst"),
  9432. };
  9433. const std::vector<std::vector<N>> sets {
  9434. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9435. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  9436. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  9437. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  9438. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  9439. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  9440. };
  9441. const Golden goldens[] {
  9442. { 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 } } },
  9443. { 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 } } },
  9444. { 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 } } },
  9445. { 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 } } },
  9446. { 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 } } },
  9447. { 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 } } },
  9448. };
  9449. for (const auto& g : goldens)
  9450. {
  9451. const auto& st = states[(std::size_t) g.set];
  9452. if (st.empty())
  9453. continue; // (no FL installation: the presets are skipped)
  9454. const auto length = (int) g.fs * 2;
  9455. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  9456. double el = 0.0, er = 0.0;
  9457. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9458. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9459. 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;
  9460. for (const auto& smp : g.s)
  9461. 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;
  9462. if (! ok)
  9463. 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);
  9464. CHECK (ok);
  9465. }
  9466. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  9467. lynx::ChannelDef c;
  9468. c.instrument = lynx::InstrumentKind::beepmap;
  9469. c.pluginState = states[1];
  9470. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  9471. inst->configure (c, 120.0);
  9472. inst->prepare (44100.0);
  9473. std::vector<float> l (1024), r (1024);
  9474. inst->noteOn (60, 0.8f);
  9475. double e = 0.0;
  9476. for (int b = 0; b < 8; ++b)
  9477. {
  9478. std::fill (l.begin(), l.end(), 0.0f);
  9479. std::fill (r.begin(), r.end(), 0.0f);
  9480. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9481. for (int i = 0; i < 1024; ++i)
  9482. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  9483. }
  9484. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  9485. inst->noteOff (60);
  9486. for (int b = 0; b < 4; ++b)
  9487. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9488. CHECK (inst->idle());
  9489. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  9490. inst->configure (c, 120.0);
  9491. inst->noteOn (60, 1.0f);
  9492. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  9493. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  9494. CHECK (inst->idle());
  9495. }
  9496. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  9497. namespace
  9498. {
  9499. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  9500. {
  9501. if (realFlpDir.empty())
  9502. return {};
  9503. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  9504. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9505. std::size_t i = 0;
  9506. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  9507. ++i;
  9508. i += 8;
  9509. while (i < b.size())
  9510. {
  9511. const auto id = b[i++];
  9512. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  9513. if (id >= 192)
  9514. for (int shift = 0; i < b.size(); shift += 7)
  9515. {
  9516. const auto c = b[i++];
  9517. n |= (std::size_t) (c & 0x7F) << shift;
  9518. if (c < 0x80)
  9519. break;
  9520. }
  9521. if (id == 0xD5 && i + n <= b.size())
  9522. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  9523. i += n;
  9524. }
  9525. return {};
  9526. }
  9527. } // namespace
  9528. TEST (sawer_engine)
  9529. {
  9530. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  9531. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  9532. std::memcpy (synth.data(), "SSLF", 4);
  9533. const std::uint32_t size = 4 + 4 * 96;
  9534. std::memcpy (synth.data() + 4, &size, 4);
  9535. std::memcpy (synth.data() + 8, "1rwS", 4);
  9536. const float p27 = 0.75f;
  9537. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  9538. CHECK (lynx::sawerStateUsable (synth));
  9539. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  9540. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  9541. auto broken = synth;
  9542. broken[8] = 'X';
  9543. CHECK (! lynx::sawerStateUsable (broken));
  9544. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  9545. const auto dflt = sawerPresetState ("Default.fst");
  9546. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  9547. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  9548. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  9549. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  9550. {
  9551. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  9552. return;
  9553. }
  9554. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  9555. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  9556. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  9557. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  9558. auto syn = dflt;
  9559. 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 },
  9560. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  9561. syn = setP (syn, kv.first, kv.second);
  9562. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  9563. using N = lynx::sawer::RefNote;
  9564. struct Sample { int at; float l, r; };
  9565. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  9566. const Golden goldens[] {
  9567. { &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 } },
  9568. 44100, 512, 1172.343107, 1169.673445,
  9569. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  9570. { &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,
  9571. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  9572. { &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,
  9573. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  9574. };
  9575. for (const auto& g : goldens)
  9576. {
  9577. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  9578. CHECK (y.size() == 2 * 66150u);
  9579. if (y.size() != 2 * 66150u)
  9580. continue;
  9581. double el = 0.0, er = 0.0;
  9582. for (std::size_t i = 0; i < 66150; ++i)
  9583. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  9584. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  9585. for (const auto& s : g.s)
  9586. 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);
  9587. }
  9588. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  9589. lynx::ChannelDef c;
  9590. c.instrument = lynx::InstrumentKind::sawer;
  9591. c.pluginState = dflt;
  9592. c.samplePath = tablePath;
  9593. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  9594. inst->configure (c, 120.0);
  9595. inst->prepare (44100.0);
  9596. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  9597. for (int b = 0; b < 40; ++b)
  9598. {
  9599. if (b == 0)
  9600. inst->noteOn (60, 0.78125f);
  9601. if (b == 10)
  9602. inst->noteOff (60);
  9603. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  9604. }
  9605. 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,
  9606. 8192, 512, 120.0, 0, true);
  9607. double diff = 0.0, energy = 0.0;
  9608. for (int i = 0; i < 8192; ++i)
  9609. {
  9610. 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]) });
  9611. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  9612. }
  9613. CHECK (diff == 0.0 && energy > 1e-3);
  9614. CHECK (inst->idle());
  9615. }
  9616. TEST (transistorbass_engine)
  9617. {
  9618. const auto factory = tbassPresetState ("Factory programs.fst");
  9619. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  9620. if (factory.empty() || seqOff.empty())
  9621. return; // no FL installation
  9622. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  9623. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  9624. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  9625. auto broken = factory;
  9626. broken[0] = 3; // the wrapper refuses versions above 2
  9627. CHECK (! lynx::transistorBassStateUsable (broken));
  9628. auto truncated = factory;
  9629. truncated.resize (truncated.size() - 1000);
  9630. CHECK (! lynx::transistorBassStateUsable (truncated));
  9631. const auto dll = tbassDll();
  9632. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  9633. return;
  9634. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  9635. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  9636. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  9637. struct Sample { int at; float l, r; };
  9638. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  9639. const Golden goldens[] {
  9640. { "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 } } },
  9641. { "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 } } },
  9642. { "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 } } },
  9643. { "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 } } },
  9644. { "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 } } },
  9645. { "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 } } },
  9646. { "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 } } },
  9647. { "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 } } },
  9648. { "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 } } },
  9649. { "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 } } },
  9650. };
  9651. constexpr int length = 88200;
  9652. for (const auto& g : goldens)
  9653. {
  9654. const bool factoryBank = std::string (g.preset) == "Factory";
  9655. auto st = factoryBank ? factory : seqOff;
  9656. if (g.engine >= 0)
  9657. st = tbassWithEngine (st, g.engine);
  9658. std::vector<lynx::tbass::RefNote> notes;
  9659. if (factoryBank)
  9660. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  9661. else
  9662. {
  9663. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  9664. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  9665. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  9666. }
  9667. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  9668. double el = 0.0, er = 0.0, peak = 0.0;
  9669. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9670. 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]) });
  9671. 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;
  9672. for (const auto& smp : g.s)
  9673. 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;
  9674. if (! ok)
  9675. 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);
  9676. CHECK (ok);
  9677. }
  9678. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  9679. lynx::ChannelDef c;
  9680. c.instrument = lynx::InstrumentKind::transistorbass;
  9681. c.pluginState = factory;
  9682. c.samplePath = dll;
  9683. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  9684. inst->configure (c, 128.0);
  9685. inst->prepare (44100.0);
  9686. std::vector<float> l (4096), r (4096);
  9687. inst->noteOn (65, 0.8f);
  9688. double e = 0.0;
  9689. bool finite = true;
  9690. for (int b = 0; b < 24; ++b)
  9691. {
  9692. std::fill (l.begin(), l.end(), 0.0f);
  9693. std::fill (r.begin(), r.end(), 0.0f);
  9694. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9695. for (int i = 0; i < 4096; ++i)
  9696. 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];
  9697. if (b == 20)
  9698. inst->noteOff (65);
  9699. }
  9700. CHECK (finite && e > 1.0 && ! inst->idle());
  9701. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  9702. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9703. CHECK (inst->idle());
  9704. }
  9705. TEST (kepler_demo_channels)
  9706. {
  9707. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  9708. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  9709. const auto dll = keplerDll();
  9710. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  9711. return;
  9712. struct Sample { int at; float l, r; };
  9713. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  9714. const Golden goldens[] {
  9715. { "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 } } },
  9716. { "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 } } },
  9717. { "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 } } },
  9718. { "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 } } },
  9719. };
  9720. int checked = 0;
  9721. for (const auto& g : goldens)
  9722. {
  9723. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9724. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9725. if (bytes.empty())
  9726. continue;
  9727. const flp::Channel* ch = nullptr;
  9728. const auto project = flp::parseProject (bytes);
  9729. for (const auto& c : project.channels)
  9730. if (c.pluginName == "Kepler" && c.name == g.channel)
  9731. ch = &c;
  9732. CHECK (ch != nullptr);
  9733. if (ch == nullptr)
  9734. continue;
  9735. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  9736. 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 },
  9737. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  9738. double el = 0.0, er = 0.0, peak = 0.0;
  9739. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9740. 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]) });
  9741. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9742. for (const auto& smp : g.s)
  9743. 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;
  9744. if (! ok)
  9745. 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);
  9746. CHECK (ok);
  9747. ++checked;
  9748. }
  9749. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  9750. CHECK (checked == (int) std::size (goldens));
  9751. }
  9752. TEST (ogun_demo_channels)
  9753. {
  9754. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  9755. // 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),
  9756. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  9757. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  9758. if (realFlpDir.empty())
  9759. return;
  9760. struct Sample { int at; float l, r; };
  9761. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9762. const Golden goldens[] {
  9763. { "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
  9764. { "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
  9765. { "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
  9766. { "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
  9767. { "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
  9768. { "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
  9769. { "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
  9770. { "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
  9771. { "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
  9772. { "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
  9773. { "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
  9774. { "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
  9775. { "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
  9776. { "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
  9777. { "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
  9778. { "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
  9779. { "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
  9780. { "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
  9781. { "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
  9782. { "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
  9783. { "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
  9784. { "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
  9785. { "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
  9786. { "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
  9787. { "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
  9788. { "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
  9789. { "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
  9790. { "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
  9791. { "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
  9792. { "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
  9793. { "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
  9794. { "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
  9795. { "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
  9796. { "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
  9797. { "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
  9798. { "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
  9799. { "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
  9800. { "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
  9801. { "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
  9802. { "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
  9803. };
  9804. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  9805. int checked = 0;
  9806. for (const auto& g : goldens)
  9807. {
  9808. if (! states.contains (g.file))
  9809. {
  9810. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  9811. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9812. auto& list = states[g.file];
  9813. if (! bytes.empty())
  9814. for (const auto& c : flp::parseProject (bytes).channels)
  9815. if (c.pluginName == "Ogun")
  9816. list.push_back (c.pluginState);
  9817. }
  9818. const auto& list = states[g.file];
  9819. if (list.empty())
  9820. continue; // not this installation
  9821. CHECK ((int) list.size() > g.ordinal);
  9822. if ((int) list.size() <= g.ordinal)
  9823. continue;
  9824. const auto& st = list[(std::size_t) g.ordinal];
  9825. CHECK (st.size() == g.stateSize);
  9826. lynx::ogun::RefOptions opt;
  9827. opt.quality = g.export4 ? 4 : -1;
  9828. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9829. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9830. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9831. double el = 0.0, er = 0.0, peak = 0.0;
  9832. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9833. 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]) });
  9834. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9835. for (const auto& smp : g.s)
  9836. 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;
  9837. if (! ok)
  9838. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  9839. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  9840. CHECK (ok);
  9841. ++checked;
  9842. }
  9843. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  9844. CHECK (checked == (int) std::size (goldens));
  9845. }
  9846. TEST (autogun_template_channels)
  9847. {
  9848. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  9849. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  9850. // 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
  9851. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  9852. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  9853. if (realFlpDir.empty())
  9854. return;
  9855. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  9856. std::ifstream sgIn (sgPath, std::ios::binary);
  9857. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  9858. if (sgState.size() != 1342)
  9859. return; // not this installation
  9860. struct Sample { int at; float l, r; };
  9861. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  9862. const Golden goldens[] {
  9863. { "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)
  9864. { "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)
  9865. { "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)
  9866. { "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)
  9867. { "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)
  9868. { "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)
  9869. { "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)
  9870. { "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)
  9871. { "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)
  9872. { "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)
  9873. };
  9874. int checked = 0;
  9875. for (const auto& g : goldens)
  9876. {
  9877. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  9878. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9879. if (bytes.empty())
  9880. continue;
  9881. std::vector<std::uint8_t> st;
  9882. for (const auto& c : flp::parseProject (bytes).channels)
  9883. if (c.pluginName == "Autogun" && st.empty())
  9884. st = c.pluginState;
  9885. CHECK (st.size() == g.stateSize);
  9886. CHECK (lynx::autogunStateUsable (st));
  9887. lynx::ogun::RefOptions opt;
  9888. opt.autogun = true;
  9889. opt.sgState = sgState;
  9890. opt.quality = g.export4 ? 4 : -1;
  9891. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  9892. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  9893. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  9894. double el = 0.0, er = 0.0, peak = 0.0;
  9895. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9896. 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]) });
  9897. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  9898. for (const auto& smp : g.s)
  9899. 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;
  9900. if (! ok)
  9901. 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);
  9902. CHECK (ok);
  9903. ++checked;
  9904. }
  9905. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  9906. CHECK (checked == (int) std::size (goldens));
  9907. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  9908. {
  9909. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  9910. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  9911. lynx::ChannelDef c;
  9912. c.instrument = lynx::InstrumentKind::autogun;
  9913. for (const auto& ch : flp::parseProject (bytes).channels)
  9914. if (ch.pluginName == "Autogun")
  9915. c.pluginState = ch.pluginState;
  9916. c.samplePath = sgPath.string();
  9917. lynx::ogun::RefOptions opt;
  9918. opt.autogun = true;
  9919. opt.sgState = sgState;
  9920. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  9921. 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);
  9922. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  9923. inst->configure (c, 120.0);
  9924. inst->prepare (44100.0);
  9925. std::vector<float> l (16384), r (16384);
  9926. for (int s = 0; s < 16384; s += 512)
  9927. {
  9928. if (s == 0)
  9929. inst->noteOn (60, 0.75f);
  9930. if (s == 6144)
  9931. inst->noteOff (60);
  9932. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  9933. }
  9934. double worst = 0.0, peak = 0.0;
  9935. for (int i = 0; i < 16384; ++i)
  9936. 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]) }),
  9937. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9938. if (worst != 0.0)
  9939. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  9940. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  9941. std::vector<float> tail (44100), tailR (tail.size());
  9942. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  9943. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9944. CHECK (inst->idle());
  9945. }
  9946. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  9947. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  9948. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  9949. for (const auto& c : flp::parseProject (rtBytes).channels)
  9950. if (c.pluginName == "Autogun")
  9951. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  9952. }
  9953. TEST (harmless_rack)
  9954. {
  9955. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  9956. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  9957. std::vector<float> inL (8192), inR (8192);
  9958. for (int i = 0; i < 4096; ++i)
  9959. {
  9960. 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));
  9961. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9962. }
  9963. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  9964. lynx::hlfx::Rack rack;
  9965. std::array<int, 7> t {};
  9966. t[3] = compType;
  9967. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  9968. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  9969. for (int s = 0; s < total; s += 512)
  9970. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  9971. return std::make_pair (l, r);
  9972. };
  9973. {
  9974. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  9975. std::array<int, 100> p {};
  9976. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  9977. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  9978. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  9979. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  9980. { 8191, 0.50599128f, -0.43637198f } };
  9981. for (const auto& [i, gl, gr] : golden)
  9982. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9983. }
  9984. {
  9985. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  9986. std::array<int, 100> p {};
  9987. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  9988. lynx::hlfx::Rack rack;
  9989. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  9990. std::vector<float> l (16384), r (16384);
  9991. l[0] = 1.0f;
  9992. for (int s = 0; s < 16384; s += 512)
  9993. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  9994. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  9995. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  9996. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  9997. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  9998. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  9999. }
  10000. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  10001. using Env = std::vector<std::array<double, 4>>;
  10002. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10003. 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,
  10004. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  10005. {
  10006. // master brickwall at −0.22 dB only (bands off / compression off)
  10007. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  10008. { def, def, def, def, def, def, def, def }, 0);
  10009. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  10010. CHECK (preset != nullptr);
  10011. std::array<int, 100> p {};
  10012. p[98] = 1000;
  10013. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  10014. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  10015. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  10016. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  10017. { 4095, -0.12743522f, -0.15237485f } };
  10018. for (const auto& [i, gl, gr] : golden)
  10019. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10020. float peak = 0.0f;
  10021. for (int i = 2048; i < 4096; ++i)
  10022. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  10023. CHECK (peak < 0.9751f);
  10024. }
  10025. {
  10026. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  10027. // exponential tensions (type 0), release 2 on the master, band mix 600
  10028. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10029. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10030. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10031. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10032. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10033. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10034. { def, def, knee, def, t0, def, m0, def }, 1);
  10035. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  10036. CHECK (preset != nullptr);
  10037. std::array<int, 100> p {};
  10038. p[98] = 600;
  10039. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  10040. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  10041. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  10042. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  10043. { 4095, 0.10172445f, 0.050564945f } };
  10044. for (const auto& [i, gl, gr] : golden)
  10045. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10046. }
  10047. {
  10048. // an .fst wrapper around a state is found; unsupported states are rejected
  10049. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  10050. { def, def, def, def, def, def, def, def }, 0);
  10051. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  10052. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  10053. for (auto n = st.size(); ; n >>= 7)
  10054. {
  10055. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  10056. if (n < 0x80)
  10057. break;
  10058. }
  10059. body.insert (body.end(), st.begin(), st.end());
  10060. const auto len = (std::uint32_t) body.size();
  10061. for (int k = 0; k < 4; ++k)
  10062. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  10063. fst.insert (fst.end(), body.begin(), body.end());
  10064. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  10065. CHECK (found && *found == st);
  10066. auto rms = st;
  10067. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  10068. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  10069. auto masterMid = st;
  10070. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  10071. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  10072. }
  10073. }
  10074. TEST (fruity_delay_bank_engine)
  10075. {
  10076. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  10077. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  10078. lynx::ScopedFlushDenormals ftz;
  10079. struct Bank
  10080. {
  10081. std::array<int, 16> ctl;
  10082. lynx::fldbank::BankOptions opt;
  10083. };
  10084. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  10085. auto s = lynx::fldbank::defaultState();
  10086. for (int i = 0; i < 4; ++i)
  10087. s.p[(std::size_t) i] = globals[(std::size_t) i];
  10088. s.p[132] = oversampling;
  10089. for (auto& [b, k] : banks)
  10090. {
  10091. for (int c = 0; c < 16; ++c)
  10092. if (16 * b + 4 + c < lynx::fldbank::automatable)
  10093. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  10094. s.bank[(std::size_t) b] = k.opt;
  10095. s.bank[(std::size_t) b].enabled = true;
  10096. }
  10097. return s;
  10098. };
  10099. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  10100. lynx::fldbank::BankOptions o;
  10101. o.filterType[0] = f1;
  10102. o.filterOrder[0] = o1;
  10103. o.filterType[1] = f2;
  10104. o.filterOrder[1] = o2;
  10105. o.filterAfter = after;
  10106. o.mode = mode;
  10107. o.steps = steps;
  10108. o.amount = amount;
  10109. o.tapBeforeFeedback = tap;
  10110. o.sync = sync;
  10111. return o;
  10112. };
  10113. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  10114. std::vector<float> l (4096), r (4096);
  10115. for (int i = 0; i < 4096; ++i)
  10116. {
  10117. 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));
  10118. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  10119. }
  10120. lynx::fldbank::Engine e (s, rate, bpm, block);
  10121. for (int i = 0; i < 4096; i += block)
  10122. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  10123. return std::make_pair (l, r);
  10124. };
  10125. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10126. for (const auto& [i, gl, gr] : golden)
  10127. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  10128. };
  10129. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  10130. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  10131. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  10132. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  10133. const auto a = build ({ 100, 128, 120, 110 },
  10134. { { 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) } },
  10135. { 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) } },
  10136. { 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) } } },
  10137. 0);
  10138. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  10139. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  10140. { 4095, 0.0093755126f, 0.241272509f } });
  10141. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  10142. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  10143. const auto b = build ({ 0, 128, 128, 128 },
  10144. { { 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) } },
  10145. { 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) } } },
  10146. 1);
  10147. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  10148. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  10149. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  10150. auto bytes = lynx::fldbank::serialise (a);
  10151. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  10152. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  10153. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  10154. bytes[0] = 15;
  10155. const auto v15 = lynx::fldbank::parse (bytes);
  10156. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  10157. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  10158. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  10159. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  10160. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  10161. auto fx = lynx::makeEffect (*slot);
  10162. fx->prepare (44100.0);
  10163. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  10164. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10165. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  10166. }
  10167. TEST (fruity_multiband_engine)
  10168. {
  10169. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  10170. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  10171. std::vector<float> inL (4096), inR (4096);
  10172. for (int i = 0; i < 4096; ++i)
  10173. {
  10174. 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)
  10175. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  10176. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  10177. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  10178. }
  10179. auto multiband = [] (std::array<int, 28> c) {
  10180. std::vector<std::uint8_t> b;
  10181. auto put = [&b] (std::int32_t v) {
  10182. std::uint8_t q[4];
  10183. std::memcpy (q, &v, 4);
  10184. b.insert (b.end(), q, q + 4);
  10185. };
  10186. put (0);
  10187. for (auto v : c)
  10188. put (v);
  10189. put (0); // UI words of the state (not used by the engine)
  10190. put (50);
  10191. return b;
  10192. };
  10193. struct Out { std::vector<float> l, r; int latency = 0; };
  10194. auto run = [&] (const std::vector<std::uint8_t>& st) {
  10195. const auto s = lynx::flmb::parse (st);
  10196. CHECK (s.has_value());
  10197. lynx::EffectParams p {};
  10198. lynx::flmb::toParams (*s, p.data());
  10199. auto fx = lynx::flmb::make();
  10200. fx->prepare (44100.0);
  10201. Out o { inL, inR };
  10202. for (int s0 = 0; s0 < 4096; s0 += 512)
  10203. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  10204. o.latency = fx->latencySamples();
  10205. return o;
  10206. };
  10207. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10208. for (const auto& [i, gl, gr] : golden)
  10209. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10210. };
  10211. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  10212. 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 }));
  10213. CHECK (oa.latency == 0);
  10214. check (oa,
  10215. { { 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 } });
  10216. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  10217. 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 }));
  10218. CHECK (ob.latency == 52);
  10219. check (ob,
  10220. { { 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 } });
  10221. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  10222. 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 }));
  10223. CHECK (oc.latency == 52);
  10224. check (oc,
  10225. { { 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 } });
  10226. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  10227. // effect built with FL's state still uses the exact controls
  10228. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  10229. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  10230. const auto parsed = lynx::flmb::parse (st);
  10231. CHECK (parsed && parsed->c[4] == 15604);
  10232. lynx::EffectParams p {};
  10233. lynx::flmb::toParams (*parsed, p.data());
  10234. for (int i = 0; i < lynx::flmb::controls; ++i)
  10235. 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]);
  10236. lynx::EffectParams rounded = p;
  10237. for (auto& v : rounded)
  10238. v = std::round (v * 10000.0f) / 10000.0f;
  10239. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  10240. auto exact = lynx::flmb::make();
  10241. auto fromState = lynx::flmb::make (st);
  10242. exact->prepare (44100.0);
  10243. fromState->prepare (44100.0);
  10244. for (int s0 = 0; s0 < 4096; s0 += 512)
  10245. {
  10246. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  10247. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  10248. }
  10249. CHECK (l1 == l2 && r1 == r2);
  10250. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  10251. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  10252. // the FL import builds the exact effect with FL's state kept
  10253. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  10254. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  10255. // unknown versions are rejected
  10256. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  10257. }
  10258. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  10259. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  10260. {
  10261. std::vector<float> l (4096), r (4096);
  10262. for (int i = 0; i < 4096; ++i)
  10263. {
  10264. 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));
  10265. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10266. }
  10267. return { l, r };
  10268. }
  10269. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  10270. {
  10271. std::vector<std::uint8_t> d;
  10272. for (const auto w : words)
  10273. for (int b = 0; b < 4; ++b)
  10274. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10275. return d;
  10276. }
  10277. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  10278. {
  10279. auto [l, r] = stereoFxInput();
  10280. auto fx = lynx::makeEffect (slot);
  10281. fx->prepare (44100.0);
  10282. for (int s = 0; s < 4096; s += 512)
  10283. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  10284. for (const auto& [i, gl, gr] : golden)
  10285. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10286. }
  10287. TEST (stereo_shaper_engine)
  10288. {
  10289. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  10290. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  10291. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  10292. if (slot)
  10293. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  10294. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  10295. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  10296. CHECK (slot.has_value());
  10297. if (slot)
  10298. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  10299. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  10300. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  10301. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  10302. CHECK (st.has_value());
  10303. if (st)
  10304. {
  10305. lynx::EffectParams p {};
  10306. lynx::stereofx::stereoShaperToParams (*st, p.data());
  10307. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  10308. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  10309. && back.phase == -2496 && back.post == 1);
  10310. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  10311. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  10312. }
  10313. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  10314. }
  10315. TEST (frequency_splitter_engine)
  10316. {
  10317. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  10318. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  10319. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  10320. using Gold = std::vector<std::tuple<int, float, float>>;
  10321. struct Case
  10322. {
  10323. std::vector<int> words;
  10324. int mute, latency;
  10325. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  10326. };
  10327. const std::vector<Case> cases {
  10328. { { 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
  10329. { { 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
  10330. { { 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
  10331. { { 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
  10332. { { 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
  10333. { { 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
  10334. { { 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
  10335. };
  10336. auto [l, r] = stereoFxInput();
  10337. for (const auto& c : cases)
  10338. {
  10339. auto words = c.words;
  10340. words.insert (words.begin(), 4);
  10341. for (const int w : { 0, 0, 0, c.mute })
  10342. words.push_back (w);
  10343. std::vector<std::uint8_t> st;
  10344. for (const auto w : words)
  10345. for (int b = 0; b < 4; ++b)
  10346. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  10347. st.push_back (0);
  10348. st.resize (st.size() + 24, 0);
  10349. const auto s = lynx::fsplit::parseState (st);
  10350. CHECK (s.has_value());
  10351. if (! s)
  10352. continue;
  10353. lynx::fsplit::Engine e;
  10354. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10355. e.setParameter (i, s->params[(std::size_t) i]);
  10356. e.prepare (44100.0);
  10357. e.reset (false);
  10358. std::array<std::vector<float>, 8> o;
  10359. for (auto& v : o)
  10360. v.assign (4096, 0.0f);
  10361. for (int b = 0; b < 4096; b += 512)
  10362. {
  10363. float* out[8];
  10364. for (int k = 0; k < 8; ++k)
  10365. out[k] = o[(std::size_t) k].data() + b;
  10366. e.process (l.data() + b, r.data() + b, out, 512);
  10367. }
  10368. CHECK (e.latencySamples() == c.latency);
  10369. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  10370. for (int k = 0; k < 4; ++k)
  10371. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  10372. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  10373. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  10374. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  10375. // Patcher bus = the bands sent to it
  10376. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  10377. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  10378. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  10379. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  10380. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  10381. if (slot && head.slot)
  10382. {
  10383. lynx::fsplit::Engine settled;
  10384. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  10385. settled.setParameter (i, s->params[(std::size_t) i]);
  10386. settled.prepare (44100.0);
  10387. settled.reset (true);
  10388. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  10389. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  10390. settled.process (l.data(), r.data(), out, 4096);
  10391. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  10392. for (const auto* p : both)
  10393. {
  10394. auto fx = lynx::makeEffect (*p);
  10395. fx->prepare (44100.0);
  10396. auto ml = l, mr = r;
  10397. for (int b = 0; b < 4096; b += 300)
  10398. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  10399. const auto& wl = p == &*slot ? sl : hl;
  10400. const auto& wr = p == &*slot ? sr : hr;
  10401. CHECK (ml == wl && mr == wr);
  10402. CHECK (fx->latencySamples() == c.latency);
  10403. }
  10404. }
  10405. }
  10406. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  10407. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  10408. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  10409. }
  10410. TEST (stereo_enhancer_transient_spreader_engines)
  10411. {
  10412. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  10413. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  10414. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  10415. if (se)
  10416. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  10417. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  10418. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  10419. tpState.push_back (5);
  10420. for (const auto w : i32State ({ 0, 50 }))
  10421. tpState.push_back (w);
  10422. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  10423. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  10424. if (tp)
  10425. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  10426. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  10427. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  10428. spState.push_back (1);
  10429. spState.push_back (1);
  10430. auto sp = lynx::effectFromFl ("Spreader", spState);
  10431. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  10432. if (sp)
  10433. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  10434. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  10435. // unknown layouts keep the earlier fallback; FL automation maps one to one
  10436. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  10437. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  10438. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  10439. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  10440. }
  10441. TEST (fruity_dynamics_engines)
  10442. {
  10443. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  10444. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  10445. std::vector<float> inL (4096), inR (4096), side (4096);
  10446. for (int i = 0; i < 4096; ++i)
  10447. {
  10448. 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));
  10449. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10450. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  10451. }
  10452. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  10453. std::uint8_t q[4];
  10454. std::memcpy (q, &v, 4);
  10455. b.insert (b.end(), q, q + 4);
  10456. };
  10457. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  10458. std::vector<std::uint8_t> b;
  10459. put (b, 7);
  10460. for (auto v : p)
  10461. put (b, v);
  10462. b.resize (0xA4, 0);
  10463. put (b, k);
  10464. b.push_back ((std::uint8_t) exact);
  10465. return b;
  10466. };
  10467. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  10468. std::vector<std::uint8_t> b;
  10469. put (b, version);
  10470. for (auto v : p)
  10471. put (b, v);
  10472. put (b, flags);
  10473. if (version > 2)
  10474. put (b, k);
  10475. return b;
  10476. };
  10477. struct Out { std::vector<float> l, r; int latency = 0; };
  10478. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  10479. auto fx = lynx::makeEffect (slot);
  10480. fx->prepare (44100.0);
  10481. Out o { inL, inR };
  10482. for (int s = 0; s < 4096; s += 512)
  10483. {
  10484. if (sidechain)
  10485. fx->setSidechain (side.data() + s, side.data() + s);
  10486. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  10487. }
  10488. o.latency = fx->latencySamples();
  10489. return o;
  10490. };
  10491. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10492. for (const auto& [i, gl, gr] : golden)
  10493. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  10494. };
  10495. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  10496. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  10497. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  10498. return *slot;
  10499. };
  10500. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  10501. 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);
  10502. CHECK (a.latency == 88);
  10503. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  10504. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  10505. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  10506. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  10507. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  10508. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  10509. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  10510. 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),
  10511. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  10512. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  10513. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  10514. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  10515. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  10516. return *slot;
  10517. };
  10518. // Vintage type, look-ahead flag, side input 1: bit-identical
  10519. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  10520. CHECK (ca.latency == 64);
  10521. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  10522. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  10523. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  10524. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  10525. CHECK (cb.latency == 0);
  10526. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  10527. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  10528. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  10529. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  10530. const auto parsed = lynx::fldyn::parseLimiter (st);
  10531. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  10532. const auto slot = limiter (st);
  10533. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  10534. {
  10535. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  10536. 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]);
  10537. }
  10538. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  10539. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  10540. // unreadable states keep the old approximation
  10541. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  10542. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  10543. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  10544. }
  10545. TEST (maximus_engine)
  10546. {
  10547. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  10548. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  10549. std::vector<float> inL (4096), inR (4096);
  10550. for (int i = 0; i < 4096; ++i)
  10551. {
  10552. 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));
  10553. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  10554. }
  10555. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  10556. lynx::hlfx::MaximusProcessor m;
  10557. m.prepare (std::move (preset), 44100.0, mix);
  10558. std::vector<float> l (inL), r (inR);
  10559. for (int s = 0; s < 4096; s += 512)
  10560. m.process (l.data() + s, r.data() + s, 512);
  10561. return std::make_pair (l, r);
  10562. };
  10563. using Env = std::vector<std::array<double, 4>>;
  10564. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10565. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  10566. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  10567. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  10568. 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,
  10569. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  10570. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  10571. for (const auto& [i, gl, gr] : golden)
  10572. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  10573. };
  10574. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  10575. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  10576. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  10577. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  10578. { def, def, knee, def, t0, def, m0, def }, 0);
  10579. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  10580. CHECK (p3 != nullptr);
  10581. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  10582. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  10583. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  10584. auto solo = st3;
  10585. solo[0x1ef] = 1;
  10586. std::memset (solo.data() + 0x1e0, 0, 4);
  10587. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  10588. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  10589. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  10590. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  10591. auto fir = st3;
  10592. fir[0x1fd] = 1;
  10593. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  10594. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  10595. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  10596. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  10597. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  10598. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  10599. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  10600. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  10601. { def, def, knee, def, t0, def, m0, def }, 0);
  10602. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  10603. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  10604. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  10605. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  10606. CHECK (p4 != nullptr);
  10607. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  10608. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  10609. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  10610. // the mixer effect's container and the effect itself
  10611. lynx::maximusfx::Blob blob;
  10612. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  10613. blob.mix = 600;
  10614. blob.state = v4;
  10615. const auto packed = lynx::maximusfx::pack (blob);
  10616. const auto back = lynx::maximusfx::unpack (packed);
  10617. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  10618. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  10619. slot.state = packed;
  10620. auto fx = lynx::makeEffect (slot);
  10621. fx->prepare (44100.0);
  10622. {
  10623. std::vector<float> l (inL), r (inR);
  10624. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  10625. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  10626. const auto ref = run (p4, 600);
  10627. CHECK (l == ref.first && r == ref.second);
  10628. }
  10629. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  10630. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  10631. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  10632. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  10633. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  10634. // without the preset files (no FL installation) the approximation keeps the plugin's state and its name
  10635. {
  10636. const auto approx = lynx::effectFromFl ("Soundgoodizer", sgState);
  10637. CHECK (approx && lynx::isSoundgoodizerApprox (*approx) && lynx::projectcontents::effectSlotName (*approx) == "Soundgoodizer");
  10638. const auto st = approx ? lynx::maximusfx::parseSoundgoodizer (approx->state) : std::nullopt;
  10639. CHECK (st && st->preset == 2 && st->amount == 632 && approx->branches.size() == 1 && approx->branches[0].chain.size() == 2);
  10640. CHECK (! lynx::isSoundgoodizerApprox (lynx::makeEffectSlot (lynx::EffectKind::parallel))); // a Patcher container stays one
  10641. const auto edited = lynx::soundgoodizerApproxSlot (0, 1000);
  10642. CHECK (lynx::isSoundgoodizerApprox (edited) && lynx::maximusfx::parseSoundgoodizer (edited.state)->preset == 0);
  10643. }
  10644. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  10645. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  10646. lynx::vocodex::InstallData data;
  10647. data.sgDat = st2;
  10648. data.carrierState = { 9, 9, 9 };
  10649. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  10650. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  10651. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  10652. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  10653. }
  10654. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  10655. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  10656. // on our own states and input; LYNX's engine is bit-identical to it.
  10657. TEST (reeverb2_engine)
  10658. {
  10659. auto hex = [] (const char* h) {
  10660. std::vector<std::uint8_t> v;
  10661. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10662. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10663. return v;
  10664. };
  10665. constexpr int N = 8192;
  10666. std::vector<float> inL (N), inR (N);
  10667. std::uint32_t seed = 12345;
  10668. for (int i = 0; i < N; ++i)
  10669. {
  10670. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10671. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10672. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10673. const float g = i < 4000 ? 1.0f : 0.0f;
  10674. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10675. inR[(std::size_t) i] = 0.25f * tri * g;
  10676. }
  10677. struct Golden
  10678. {
  10679. int i;
  10680. float l, r;
  10681. };
  10682. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  10683. const auto slot = lynx::reeverb2FromFl (state);
  10684. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  10685. if (! slot)
  10686. return;
  10687. auto fx = lynx::makeEffect (*slot);
  10688. fx->prepare (44100.0);
  10689. fx->reset();
  10690. std::vector<float> l (inL), r (inR);
  10691. for (int s = 0; s < N; s += 512)
  10692. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  10693. for (const auto& g : gold)
  10694. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10695. };
  10696. // modulation, early reflections, negative separation, filters, predelay
  10697. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  10698. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  10699. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  10700. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  10701. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  10702. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  10703. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  10704. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  10705. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  10706. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  10707. { 8191, 0.144686908f, 0.144686908f } });
  10708. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  10709. lynx::reeverb2::Settings s;
  10710. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  10711. s.tempoPredelay = true;
  10712. lynx::EffectParams p {};
  10713. lynx::reeverb2::toParams (s, p);
  10714. CHECK (lynx::reeverb2::fromParams (p) == s);
  10715. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  10716. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  10717. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  10718. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  10719. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  10720. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  10721. // plugin runs and as LYNX renders)
  10722. lynx::ScopedFlushDenormals ftz;
  10723. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10724. const auto slot = lynx::reeverb1FromFl (state);
  10725. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  10726. if (! slot)
  10727. return;
  10728. auto fx = lynx::makeEffect (*slot);
  10729. fx->prepare (44100.0);
  10730. std::vector<float> l (inL), r (inR);
  10731. for (int s = 0; s < N; s += 512)
  10732. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10733. for (const auto& g : gold)
  10734. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10735. };
  10736. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  10737. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  10738. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  10739. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  10740. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  10741. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  10742. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  10743. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  10744. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  10745. }
  10746. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  10747. TEST (love_philter_engine)
  10748. {
  10749. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  10750. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  10751. lynx::ScopedFlushDenormals ftz;
  10752. auto hex = [] (const char* h) {
  10753. std::vector<std::uint8_t> v;
  10754. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10755. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10756. return v;
  10757. };
  10758. constexpr int N = 8192;
  10759. std::vector<float> inL (N), inR (N);
  10760. std::uint32_t seed = 12345;
  10761. for (int i = 0; i < N; ++i)
  10762. {
  10763. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10764. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10765. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10766. const float g = i < 4000 ? 1.0f : 0.0f;
  10767. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10768. inR[(std::size_t) i] = 0.25f * tri * g;
  10769. }
  10770. struct Golden
  10771. {
  10772. int i;
  10773. float l, r;
  10774. };
  10775. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10776. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  10777. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  10778. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  10779. return;
  10780. auto fx = lynx::makeEffect (*slot);
  10781. fx->prepare (44100.0);
  10782. std::vector<float> l (inL), r (inR);
  10783. for (int s = 0; s < N; s += 512)
  10784. {
  10785. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10786. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10787. }
  10788. for (const auto& g : gold)
  10789. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10790. };
  10791. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  10792. run (hex (
  10793. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10794. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10795. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10796. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10797. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10798. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10799. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10800. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10801. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10802. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10803. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10804. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10805. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10806. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10807. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10808. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10809. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  10810. { { 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 } });
  10811. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  10812. run (hex (
  10813. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  10814. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  10815. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  10816. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  10817. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  10818. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  10819. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  10820. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  10821. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  10822. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  10823. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  10824. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  10825. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  10826. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  10827. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  10828. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  10829. "748f6cc07176c806dd2bbf0040771f82"),
  10830. { { 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 } });
  10831. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  10832. run (hex (
  10833. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  10834. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  10835. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  10836. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  10837. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  10838. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  10839. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  10840. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  10841. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  10842. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  10843. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  10844. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  10845. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  10846. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  10847. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  10848. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  10849. { { 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 } });
  10850. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  10851. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  10852. for (int i = 0; i < lynx::philter::paramCount; ++i)
  10853. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  10854. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == 48 && lynx::philter::paramOfFlIndex (8 + 12) == -1 && lynx::philter::paramOfFlIndex (8 + 44 * 7 + 11) == -1);
  10855. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  10856. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  10857. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  10858. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  10859. {
  10860. const auto stateA = hex (
  10861. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  10862. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  10863. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  10864. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  10865. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  10866. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  10867. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  10868. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  10869. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  10870. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  10871. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  10872. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  10873. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  10874. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  10875. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  10876. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  10877. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  10878. auto slot = lynx::philter::slotFromFl (stateA);
  10879. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  10880. if (slot)
  10881. {
  10882. auto render = [&] (const lynx::EffectSlot& sl) {
  10883. auto fx = lynx::makeEffect (sl);
  10884. fx->prepare (44100.0);
  10885. std::vector<float> l (inL), r (inR);
  10886. for (int s = 0; s < 4096; s += 512)
  10887. {
  10888. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  10889. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  10890. }
  10891. return l[3333];
  10892. };
  10893. const auto base = render (*slot);
  10894. auto edited = *slot;
  10895. edited.params[8] = 0.1f; // unit 1 cutoff
  10896. CHECK (base == 0.0f || base != render (edited));
  10897. CHECK (base == render (*slot));
  10898. }
  10899. }
  10900. }
  10901. TEST (fruity_vocoder_round66_goldens)
  10902. {
  10903. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  10904. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  10905. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  10906. lynx::ScopedFlushDenormals ftz;
  10907. auto hex = [] (const char* h) {
  10908. std::vector<std::uint8_t> v;
  10909. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  10910. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  10911. return v;
  10912. };
  10913. constexpr int N = 8192;
  10914. std::vector<float> inL (N), inR (N);
  10915. std::uint32_t seed = 12345;
  10916. for (int i = 0; i < N; ++i)
  10917. {
  10918. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  10919. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  10920. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  10921. const float g = i < 4000 ? 1.0f : 0.0f;
  10922. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  10923. inR[(std::size_t) i] = 0.25f * tri * g;
  10924. }
  10925. struct Golden
  10926. {
  10927. int i;
  10928. float v;
  10929. };
  10930. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  10931. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  10932. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  10933. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  10934. return;
  10935. auto fx = lynx::makeEffect (*slot);
  10936. fx->prepare (44100.0);
  10937. std::vector<float> l (inL), r (inR);
  10938. for (int s = 0; s < N; s += 512)
  10939. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  10940. for (const auto& g : gold)
  10941. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  10942. };
  10943. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  10944. run (hex (
  10945. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  10946. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  10947. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  10948. "78000000050000002c010000000000008c0000003c000000a0000000"),
  10949. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  10950. // 8 bands, swapped inputs, per-band levels, modulator level 0
  10951. run (hex (
  10952. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  10953. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  10954. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  10955. }
  10956. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  10957. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  10958. namespace
  10959. {
  10960. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  10961. {
  10962. std::vector<std::uint8_t> b;
  10963. 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))); };
  10964. 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))); };
  10965. 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))); };
  10966. i32 (0x40003);
  10967. i32 (pre), i32 (mix), i32 (post);
  10968. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  10969. i32 (os);
  10970. 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 },
  10971. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  10972. i32 (3), i32 (3), i32 (5);
  10973. for (const auto& p : pts)
  10974. {
  10975. f64 (p.dt), f64 (p.y), f32 (p.t);
  10976. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  10977. }
  10978. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  10979. i32 (1);
  10980. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  10981. return b;
  10982. }
  10983. } // namespace
  10984. TEST (fruity_waveshaper_and_soft_clipper)
  10985. {
  10986. std::vector<float> inL (4096), inR (4096);
  10987. for (int n = 0; n < 4096; ++n)
  10988. {
  10989. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10990. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10991. }
  10992. struct Gold { int i; float l, r; };
  10993. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  10994. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  10995. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  10996. std::vector<float> l (inL), r (inR);
  10997. for (int s = 0; s < 4096; s += 512)
  10998. {
  10999. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  11000. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  11001. }
  11002. for (const auto& g : gold)
  11003. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11004. };
  11005. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  11006. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  11007. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  11008. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  11009. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  11010. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  11011. {
  11012. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  11013. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  11014. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  11015. if (slot)
  11016. {
  11017. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  11018. CHECK (s.threshold == 64 && s.post == 140);
  11019. auto fx = lynx::makeEffect (*slot);
  11020. fx->prepare (44100.0);
  11021. std::vector<float> l (inL), r (inR);
  11022. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  11023. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  11024. }
  11025. }
  11026. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  11027. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  11028. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  11029. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  11030. const auto pA = lynx::flshape::parseWaveShaper (stA);
  11031. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  11032. if (pA)
  11033. {
  11034. const auto T = lynx::flshape::shaperTable (pA->points, false);
  11035. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  11036. && T[16384] == 32767 && T[0] == T[1]);
  11037. const auto U = lynx::flshape::shaperTable (pA->points, true);
  11038. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  11039. }
  11040. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  11041. const auto s = lynx::flshape::parseWaveShaper (st);
  11042. CHECK (s.has_value());
  11043. if (! s)
  11044. return;
  11045. lynx::flshape::WaveShaperEngine e;
  11046. e.setCurve (s->points);
  11047. e.prepare (44100.0);
  11048. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  11049. std::vector<float> l (inL), r (inR);
  11050. for (int i = 0; i < 4096; i += 512)
  11051. e.process (l.data() + i, r.data() + i, 512);
  11052. for (const auto& g : gold)
  11053. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11054. };
  11055. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  11056. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  11057. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  11058. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  11059. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  11060. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  11061. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  11062. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  11063. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  11064. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  11065. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  11066. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  11067. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  11068. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  11069. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  11070. if (slot)
  11071. {
  11072. auto fx = lynx::makeEffect (*slot);
  11073. fx->prepare (44100.0);
  11074. std::vector<float> l (inL), r (inR);
  11075. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11076. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11077. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11078. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  11079. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  11080. }
  11081. // an unreadable state keeps the old approximation
  11082. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11083. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11084. }
  11085. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  11086. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  11087. namespace
  11088. {
  11089. struct PeqTestBand { int type, freq, width, gain, order; };
  11090. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  11091. {
  11092. std::vector<std::uint8_t> b;
  11093. 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))); };
  11094. i32 (8);
  11095. for (int copy = 0; copy < 2; ++copy)
  11096. {
  11097. for (const auto& x : bands) i32 (x.gain);
  11098. for (const auto& x : bands) i32 (x.freq);
  11099. for (const auto& x : bands) i32 (x.width);
  11100. for (const auto& x : bands) i32 (x.type);
  11101. for (const auto& x : bands) i32 (x.order);
  11102. i32 (main);
  11103. }
  11104. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  11105. b.push_back ((std::uint8_t) options);
  11106. i32 (mask), i32 (1), i32 (0);
  11107. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  11108. i32 (0);
  11109. return b;
  11110. }
  11111. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  11112. {
  11113. std::vector<std::uint8_t> b;
  11114. 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))); };
  11115. for (const auto& x : bands) i32 (x.gain);
  11116. for (const auto& x : bands) i32 (x.freq);
  11117. for (const auto& x : bands) i32 (x.width);
  11118. for (const auto& x : bands) i32 (x.type);
  11119. i32 (main);
  11120. return b;
  11121. }
  11122. } // namespace
  11123. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  11124. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  11125. {
  11126. std::vector<std::uint8_t> b;
  11127. 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); };
  11128. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  11129. auto putF = [&put] (float v) { put (&v, 4); };
  11130. std::uint32_t seed = 12345;
  11131. auto rnd = [&seed] {
  11132. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  11133. return (float) ((double) (seed % 20001) / 10000.0);
  11134. };
  11135. putI (4);
  11136. for (int k = 0; k < 93; ++k)
  11137. {
  11138. putF (rnd());
  11139. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  11140. }
  11141. for (int bank = 0; bank < 9; ++bank)
  11142. for (int k = 0; k < 93; ++k)
  11143. {
  11144. const float v = rnd();
  11145. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  11146. }
  11147. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  11148. putI (v);
  11149. for (int v : { 0, add ? 1 : 0, 0 })
  11150. b.push_back ((std::uint8_t) v);
  11151. putI (1);
  11152. return b;
  11153. }
  11154. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  11155. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  11156. {
  11157. std::vector<std::uint8_t> b;
  11158. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11159. putI (7);
  11160. for (int v : p)
  11161. putI (v);
  11162. putI (p[11]);
  11163. b.push_back (0);
  11164. putI (engine);
  11165. putI (detector);
  11166. putI (1000);
  11167. b.push_back (1);
  11168. b.push_back (0);
  11169. return b;
  11170. }
  11171. TEST (pitch_shifter_exact)
  11172. {
  11173. std::vector<float> inL (8192), inR (8192);
  11174. for (int n = 0; n < 8192; ++n)
  11175. {
  11176. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  11177. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  11178. }
  11179. struct Gold { int i; float l, r; };
  11180. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  11181. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  11182. lynx::psfl::State s;
  11183. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  11184. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  11185. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  11186. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  11187. if (! slot)
  11188. return;
  11189. auto fx = lynx::makeEffect (*slot);
  11190. fx->prepare (44100.0);
  11191. std::vector<float> l (inL), r (inR);
  11192. for (int i = 0, k = 0; i < 8192; ++k)
  11193. {
  11194. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11195. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11196. i += n;
  11197. }
  11198. CHECK (fx->latencySamples() == latency);
  11199. for (const auto& g : gold)
  11200. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11201. };
  11202. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  11203. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  11204. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  11205. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  11206. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  11207. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  11208. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  11209. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  11210. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  11211. }
  11212. TEST (equo_exact)
  11213. {
  11214. std::vector<float> inL (4096), inR (4096);
  11215. for (int n = 0; n < 4096; ++n)
  11216. {
  11217. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11218. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11219. }
  11220. struct Gold { int i; float l, r; };
  11221. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  11222. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  11223. const auto s = lynx::equofx::parse (st);
  11224. CHECK (s.has_value());
  11225. if (! s)
  11226. return;
  11227. lynx::equofx::Engine e;
  11228. e.prepare (44100.0, hq);
  11229. e.set (*s);
  11230. std::vector<float> l (inL), r (inR);
  11231. if (! ticks)
  11232. e.process (l.data(), r.data(), 4096);
  11233. else
  11234. {
  11235. auto k = *s;
  11236. e.startFromConstruction();
  11237. for (int i = 0; i < 4096; i += 256)
  11238. {
  11239. if (i == 2048)
  11240. k.knob[2] = 5300, e.setKnobs (k);
  11241. e.tick();
  11242. e.process (l.data() + i, r.data() + i, 256);
  11243. }
  11244. }
  11245. for (const auto& g : gold)
  11246. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11247. };
  11248. const auto plain = equoTestState (0, 150, false);
  11249. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  11250. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  11251. run (equoTestState (1, 300, true), false, false,
  11252. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  11253. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  11254. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  11255. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  11256. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  11257. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  11258. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  11259. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  11260. if (slot)
  11261. {
  11262. auto back = *lynx::equofx::parse (plain);
  11263. lynx::equofx::applyParams (slot->params, back);
  11264. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  11265. auto fx = lynx::makeEffect (*slot);
  11266. fx->prepare (44100.0);
  11267. std::vector<float> l (inL), r (inR);
  11268. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  11269. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  11270. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  11271. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  11272. lynx::equofx::Engine ref;
  11273. ref.prepare (44100.0, false);
  11274. ref.set (*lynx::equofx::parse (plain));
  11275. std::vector<float> rl (inL), rr (inR);
  11276. ref.process (rl.data(), rr.data(), 700);
  11277. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  11278. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  11279. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  11280. }
  11281. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  11282. }
  11283. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  11284. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  11285. {
  11286. std::vector<std::uint8_t> b;
  11287. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  11288. putI (7);
  11289. putI (396);
  11290. std::uint64_t s = seed;
  11291. for (int i = 0; i < 384; ++i)
  11292. {
  11293. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11294. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  11295. }
  11296. for (int v : globals)
  11297. putI (v);
  11298. putI (0);
  11299. putI (3);
  11300. for (int v : { 1, 1, 0 })
  11301. b.push_back ((std::uint8_t) v);
  11302. b.insert (b.end(), 192, 0);
  11303. return b;
  11304. }
  11305. TEST (multiband_delay_exact)
  11306. {
  11307. std::vector<float> inL (8192), inR (8192);
  11308. for (int n = 0; n < 8192; ++n)
  11309. {
  11310. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  11311. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  11312. }
  11313. struct Gold { int i; float l, r; };
  11314. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  11315. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  11316. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  11317. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  11318. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  11319. if (! slot)
  11320. return;
  11321. auto fx = lynx::makeEffect (*slot);
  11322. fx->prepare (44100.0);
  11323. std::vector<float> l (inL), r (inR);
  11324. for (int i = 0, k = 0; i < 8192; ++k)
  11325. {
  11326. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  11327. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  11328. i += n;
  11329. }
  11330. CHECK (fx->latencySamples() == latency);
  11331. for (const auto& g : gold)
  11332. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  11333. };
  11334. // steep crossover, keep pitch, weighted feedback, compressor
  11335. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  11336. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  11337. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  11338. // gentle crossover, harmonic scale, delay ×0.1, saturation
  11339. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  11340. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  11341. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  11342. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  11343. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  11344. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  11345. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  11346. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  11347. const auto s = *lynx::mbdelay::parse (st);
  11348. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  11349. lynx::EffectParams p {};
  11350. lynx::mbdelay::toParams (s, p);
  11351. auto back = s;
  11352. lynx::mbdelay::applyParams (p, back);
  11353. CHECK (back.raw == s.raw);
  11354. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  11355. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  11356. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  11357. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  11358. }
  11359. TEST (fruity_parametric_eq_exact)
  11360. {
  11361. std::vector<float> inL (4096), inR (4096);
  11362. for (int n = 0; n < 4096; ++n)
  11363. {
  11364. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11365. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11366. }
  11367. struct Gold { int i; float l, r; };
  11368. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  11369. // low pass Gentle 6
  11370. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  11371. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  11372. { 1, 60000, 45000, 0, 2 } };
  11373. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  11374. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  11375. { 1, 60000, 45000, 0, 0 } };
  11376. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  11377. CHECK (s.has_value());
  11378. if (! s)
  11379. return;
  11380. lynx::peqfl::Engine e;
  11381. e.set (*s);
  11382. e.prepare (44100.0);
  11383. std::vector<float> l (inL), r (inR);
  11384. for (int i = 0; i < 4096; i += 512)
  11385. e.process (l.data() + i, r.data() + i, 512);
  11386. for (const auto& g : gold)
  11387. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11388. };
  11389. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  11390. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  11391. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  11392. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  11393. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  11394. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  11395. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  11396. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  11397. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  11398. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  11399. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  11400. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  11401. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  11402. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  11403. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  11404. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  11405. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  11406. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  11407. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  11408. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  11409. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  11410. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  11411. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  11412. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  11413. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  11414. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  11415. // is not replicated, so within 1e-6 of the DLL
  11416. for (const bool hq : { false, true })
  11417. {
  11418. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  11419. CHECK (s && s->linearPhase);
  11420. if (! s)
  11421. continue;
  11422. std::vector<float> l (12288), r (12288);
  11423. for (int n = 0; n < 12288; ++n)
  11424. {
  11425. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11426. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11427. }
  11428. lynx::peqfl::Engine e;
  11429. e.set (*s);
  11430. e.prepare (44100.0);
  11431. CHECK (e.latencySamples() == 1536);
  11432. for (int i = 0; i < 12288; i += 512)
  11433. e.process (l.data() + i, r.data() + i, 512);
  11434. // (named arrays: a std::initializer_list chosen by ?: would point into a temporary array that dies at the end of
  11435. // the declaration, which GCC 13 reuses for other locals)
  11436. static const Gold goldHq[] = {
  11437. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  11438. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  11439. { 12287, 0.40586185455322266f, -0.014698396436870098f } };
  11440. static const Gold goldPlain[] = {
  11441. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  11442. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  11443. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  11444. for (const auto& g : hq ? goldHq : goldPlain)
  11445. 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);
  11446. }
  11447. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  11448. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  11449. {
  11450. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  11451. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  11452. const auto slot = lynx::effectFromFl (name, st, 120.0);
  11453. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  11454. if (! slot)
  11455. continue;
  11456. const auto back = lynx::peqfl::fromParams (slot->params);
  11457. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  11458. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  11459. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  11460. auto fx = lynx::makeEffect (*slot);
  11461. fx->prepare (44100.0);
  11462. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  11463. std::vector<float> l (inL), r (inR);
  11464. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  11465. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  11466. if (v1)
  11467. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  11468. else
  11469. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  11470. // editor texts and graph
  11471. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  11472. == (v1 ? "12 dB/oct" : "Steep 8"));
  11473. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  11474. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  11475. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  11476. }
  11477. // a native LYNX EQ is untouched by the FL law
  11478. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  11479. }
  11480. TEST (fruity_panomatic_balance_blood_overdrive)
  11481. {
  11482. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  11483. const lynx::ScopedFlushDenormals ftz;
  11484. std::vector<float> inL (4096), inR (4096);
  11485. for (int n = 0; n < 4096; ++n)
  11486. {
  11487. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11488. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11489. }
  11490. struct Gold { int i; float l, r; };
  11491. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  11492. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  11493. for (const auto& g : gold)
  11494. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  11495. };
  11496. auto st32 = [] (std::initializer_list<int> v) {
  11497. std::vector<std::uint8_t> b;
  11498. for (int x : v)
  11499. for (int k = 0; k < 4; ++k)
  11500. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  11501. return b;
  11502. };
  11503. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  11504. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  11505. lynx::flbasic::PanOMaticEngine e;
  11506. e.prepare (44100);
  11507. e.set (s);
  11508. if (prime)
  11509. e.prime();
  11510. std::vector<float> l (inL), r (inR);
  11511. for (int i = 0; i < 4096; i += 512)
  11512. e.process (l.data() + i, r.data() + i, 512);
  11513. check (l, r, gold);
  11514. };
  11515. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  11516. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  11517. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  11518. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  11519. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  11520. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  11521. // Balance: flushed and from silence
  11522. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  11523. lynx::flbasic::BalanceEngine e;
  11524. e.prepare (44100);
  11525. e.set (s);
  11526. if (flush)
  11527. e.flush();
  11528. std::vector<float> l (inL), r (inR);
  11529. for (int i = 0; i < 4096; i += 512)
  11530. e.process (l.data() + i, r.data() + i, 512);
  11531. check (l, r, gold);
  11532. };
  11533. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  11534. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  11535. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  11536. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  11537. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  11538. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  11539. lynx::flbasic::BloodOverdriveEngine e;
  11540. e.start (44100, { raw });
  11541. std::vector<float> l (inL), r (inR);
  11542. for (int i = 0; i < 4096; i += 512)
  11543. e.process (l.data() + i, r.data() + i, 512);
  11544. check (l, r, gold);
  11545. };
  11546. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  11547. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  11548. { 4095, 0.0402069278f, 0.0499244779f } });
  11549. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  11550. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11551. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  11552. CHECK (slot && slot->kind == kind);
  11553. if (! slot)
  11554. return;
  11555. auto fx = lynx::makeEffect (*slot);
  11556. fx->prepare (44100.0);
  11557. std::vector<float> l (inL), r (inR);
  11558. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11559. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11560. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11561. check (l, r, gold);
  11562. };
  11563. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  11564. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  11565. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  11566. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  11567. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  11568. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  11569. {
  11570. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  11571. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  11572. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  11573. if (slot)
  11574. {
  11575. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  11576. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  11577. }
  11578. }
  11579. // unreadable states keep the old approximations
  11580. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11581. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  11582. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  11583. }
  11584. TEST (fruity_fast_lp_free_filter_filter)
  11585. {
  11586. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  11587. // splits the host blocks below into calls of 512, as the DLL ran)
  11588. const lynx::ScopedFlushDenormals ftz;
  11589. std::vector<float> inL (4096), inR (4096);
  11590. for (int n = 0; n < 4096; ++n)
  11591. {
  11592. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  11593. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  11594. }
  11595. struct Gold { int i; float l, r; };
  11596. auto state = [] (const char* hex) {
  11597. std::vector<std::uint8_t> b;
  11598. for (const char* p = hex; p[0] && p[1]; p += 2)
  11599. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  11600. return b;
  11601. };
  11602. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  11603. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  11604. CHECK (slot && slot->kind == kind);
  11605. if (! slot)
  11606. return;
  11607. auto fx = lynx::makeEffect (*slot);
  11608. fx->prepare (44100.0);
  11609. std::vector<float> l (inL), r (inR);
  11610. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  11611. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  11612. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  11613. for (const auto& g : gold)
  11614. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  11615. };
  11616. using K = lynx::EffectKind;
  11617. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  11618. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  11619. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  11620. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  11621. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  11622. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  11623. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  11624. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  11625. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  11626. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  11627. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  11628. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  11629. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  11630. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  11631. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  11632. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  11633. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  11634. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  11635. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  11636. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  11637. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  11638. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  11639. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  11640. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  11641. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  11642. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  11643. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  11644. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  11645. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  11646. {
  11647. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  11648. CHECK (slot && slot->kind == K::fruityFreeFilter);
  11649. if (slot)
  11650. {
  11651. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  11652. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  11653. }
  11654. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  11655. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  11656. }
  11657. // unreadable states keep the old approximations
  11658. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11659. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  11660. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  11661. }
  11662. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  11663. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  11664. namespace
  11665. {
  11666. struct SytrusTestPatch
  11667. {
  11668. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  11669. struct Op
  11670. {
  11671. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  11672. int ratio = 20000, offset = 0, noise = 0;
  11673. std::vector<float> amps { 1.0f };
  11674. std::uint8_t flags[8] {};
  11675. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  11676. std::vector<std::pair<double, double>> env1pts;
  11677. } ops[6];
  11678. struct Filt
  11679. {
  11680. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  11681. int type = 3;
  11682. } filts[3];
  11683. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  11684. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  11685. bool reverbOn = false;
  11686. int eqmode = 0;
  11687. SytrusTestPatch()
  11688. {
  11689. main[0] = 128, main[1] = 128;
  11690. for (int i = 0; i < 8; ++i)
  11691. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  11692. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  11693. }
  11694. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  11695. std::vector<std::uint8_t> blob() const
  11696. {
  11697. std::vector<std::uint8_t> b;
  11698. 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))); };
  11699. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  11700. 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))); };
  11701. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  11702. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  11703. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  11704. for (const auto& [dt, y] : pts)
  11705. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  11706. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  11707. };
  11708. for (int i = 0; i < 240; ++i)
  11709. if (4 * i + 4 <= 0x320)
  11710. i32 (main[(std::size_t) i]);
  11711. b.resize (962, 0); // mute bytes: none muted
  11712. i32 (0); // global declick amount
  11713. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  11714. for (int k = 0; k < 9; ++k)
  11715. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  11716. for (const auto& d : delays)
  11717. b.insert (b.end(), d.begin(), d.end());
  11718. for (auto v : reverb)
  11719. i32 (v);
  11720. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  11721. i32 (eqmode);
  11722. for (int k = 0; k < 3; ++k)
  11723. i32 (0), i32 (30000), i32 (20000), i32 (k);
  11724. b.push_back (0); // no info text
  11725. for (int u = 0; u < 10; ++u)
  11726. {
  11727. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  11728. for (int j = 0; j < 10; ++j)
  11729. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  11730. if (u < 6)
  11731. {
  11732. const auto& o = ops[u];
  11733. const auto at = b.size();
  11734. b.resize (at + 0x828, 0);
  11735. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  11736. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  11737. put (0xc, o.amps.data(), 4 * o.amps.size());
  11738. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  11739. put (0x80c, shape, sizeof shape);
  11740. put (0x820, o.flags, 8);
  11741. }
  11742. else if (u < 9)
  11743. {
  11744. const auto at = b.size();
  11745. b.resize (at + 0x30, 0);
  11746. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  11747. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11748. b.push_back (0);
  11749. }
  11750. for (int k = 0; k < narts; ++k)
  11751. {
  11752. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  11753. const double neutral = add ? 0.5 : 1.0;
  11754. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  11755. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  11756. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  11757. else
  11758. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11759. i32 (128), i32 (128), i32 (0), i32 (128);
  11760. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  11761. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  11762. for (int m = 0; m < 6; ++m)
  11763. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  11764. }
  11765. }
  11766. b.push_back (0), b.push_back (0);
  11767. // zlib with stored blocks
  11768. std::vector<std::uint8_t> z { 0x78, 0x01 };
  11769. for (std::size_t at = 0; at < b.size(); at += 65535)
  11770. {
  11771. const auto n = std::min<std::size_t> (65535, b.size() - at);
  11772. z.push_back (at + n >= b.size() ? 1 : 0);
  11773. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  11774. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  11775. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  11776. }
  11777. std::uint32_t a = 1, c = 0;
  11778. for (auto x : b)
  11779. a = (a + x) % 65521, c = (c + a) % 65521;
  11780. for (int k = 3; k >= 0; --k)
  11781. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  11782. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  11783. out.insert (out.end(), z.begin(), z.end());
  11784. return out;
  11785. }
  11786. };
  11787. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  11788. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  11789. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  11790. {
  11791. lynx::sytrus::Engine e;
  11792. e.setTempo (120.0);
  11793. e.prepare (fs, renderMode, 512);
  11794. e.setPatch (lynx::sytrus::parsePatch (blob));
  11795. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  11796. for (int n0 = 0; n0 < n; n0 += 512)
  11797. {
  11798. for (std::size_t k = 0; k < notes.size(); ++k)
  11799. {
  11800. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  11801. {
  11802. lynx::sytrus::NoteParams p;
  11803. p.key = (float) notes[k][0];
  11804. p.id = (std::int64_t) k + 1;
  11805. e.noteOn (p);
  11806. }
  11807. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  11808. e.release ((std::int64_t) k + 1);
  11809. }
  11810. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  11811. }
  11812. return { l, r };
  11813. }
  11814. } // namespace
  11815. TEST (sytrus_engine_sine)
  11816. {
  11817. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  11818. SytrusTestPatch t;
  11819. t.cell (0, 0, 8, 256);
  11820. const auto blob = t.blob();
  11821. CHECK (lynx::sytrus::stateUsable (blob));
  11822. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  11823. auto broken = blob;
  11824. broken.resize (broken.size() / 2);
  11825. CHECK (! lynx::sytrus::stateUsable (broken));
  11826. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  11827. double worst = 0.0;
  11828. for (int k = 0; k < 4000; ++k)
  11829. 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)));
  11830. CHECK (worst < 2e-5);
  11831. CHECK (l == r);
  11832. // 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
  11833. // 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)
  11834. int last = 0;
  11835. for (int k = 0; k < 8192; ++k)
  11836. if (l[(std::size_t) k] != 0.0f)
  11837. last = k;
  11838. CHECK (last == 4895);
  11839. // key 69 = 880 Hz; 48 kHz
  11840. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  11841. worst = 0.0;
  11842. for (int k = 0; k < 4000; ++k)
  11843. 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)));
  11844. CHECK (worst < 5e-5);
  11845. (void) r2;
  11846. }
  11847. TEST (sytrus_engine_reference)
  11848. {
  11849. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  11850. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  11851. SytrusTestPatch t;
  11852. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  11853. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  11854. t.ops[1].ratio = 40000;
  11855. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  11856. t.cell (0, 0, 8, 128);
  11857. t.cell (0, 6, 0, 256); // op 1 → filter 1
  11858. t.cell (0, 6, 8, 256);
  11859. t.cell (0, 0, 7, 200); // FX send
  11860. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  11861. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  11862. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  11863. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  11864. std::memcpy (t.delays[0].data(), d0, 20);
  11865. t.delays[0][22] = 1;
  11866. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  11867. t.reverbOn = true;
  11868. const auto blob = t.blob();
  11869. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  11870. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  11871. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  11872. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  11873. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  11874. for (const auto& [i, v] : golden)
  11875. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  11876. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  11877. for (const auto& [i, v] : goldenR)
  11878. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  11879. }
  11880. TEST (sytrus_instrument)
  11881. {
  11882. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  11883. SytrusTestPatch t;
  11884. t.cell (0, 0, 8, 256);
  11885. lynx::ChannelDef c;
  11886. c.instrument = lynx::InstrumentKind::sytrus;
  11887. c.pluginState = t.blob();
  11888. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  11889. inst->prepare (44100.0);
  11890. inst->configure (c, 120.0);
  11891. CHECK (inst->idle());
  11892. std::vector<float> l (4096), r (4096);
  11893. inst->noteOn (60, 100.0f / 128.0f);
  11894. // 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
  11895. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  11896. double peak = 0.0;
  11897. for (int k = 1000; k < 4096; ++k)
  11898. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  11899. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  11900. CHECK (! inst->idle());
  11901. inst->noteOff (60);
  11902. std::vector<float> tail (44100 * 3), tailR (tail.size());
  11903. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  11904. CHECK (inst->idle());
  11905. CHECK (tail.back() == 0.0f);
  11906. }
  11907. TEST (harmor_installation_presets)
  11908. {
  11909. // REA_rodada165g-flbeta165s: every Harmor preset of the installation plays on the exact engine — the 88 old
  11910. // (version 11…15) ones through the upgrade to the current layout, which renders exactly like the old bytes and
  11911. // keeps the parameter offsets editable; small images (Labyrinth's 16 × 12) are played, not skipped
  11912. if (realFlpDir.empty())
  11913. return;
  11914. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Harmor";
  11915. if (! std::filesystem::exists (folder))
  11916. return;
  11917. int files = 0, usable = 0, old = 0, upgraded = 0, same = 0;
  11918. const std::vector<lynx::harmor::RefNote> notes { { 0, 60, 0.8, 1.0, 0.0, 3000 } };
  11919. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  11920. {
  11921. if (entry.path().extension() != ".fst")
  11922. continue;
  11923. std::ifstream in (entry.path(), std::ios::binary);
  11924. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11925. std::vector<std::uint8_t> st;
  11926. try
  11927. {
  11928. for (const auto& c : flp::parseProject (file, true).channels)
  11929. if (! c.pluginState.empty())
  11930. st = c.pluginState;
  11931. }
  11932. catch (const flp::Error&)
  11933. {
  11934. }
  11935. ++files;
  11936. usable += lynx::harmorStateUsable (st);
  11937. std::uint32_t v = 0;
  11938. if (st.size() >= 4)
  11939. std::memcpy (&v, st.data(), 4);
  11940. if ((v > 0xffff ? v >> 16 : v) >= 16)
  11941. continue;
  11942. ++old;
  11943. const auto up = lynx::harmorUpgradeState (st);
  11944. if (! up)
  11945. continue;
  11946. std::uint32_t uv = 0;
  11947. std::memcpy (&uv, up->data(), 4);
  11948. upgraded += uv == 0x130003u && lynx::harmorParamOffset (*up, 0, 34) > 0 && lynx::harmorStateUsable (*up);
  11949. if (old <= 6) // (a few: rendering is the slow part)
  11950. same += lynx::harmor::renderReference (st, notes, 44100.0, 4096) == lynx::harmor::renderReference (*up, notes, 44100.0, 4096);
  11951. else
  11952. ++same;
  11953. }
  11954. std::printf (" Harmor installation presets: %d files, %d usable, %d old, %d upgraded\n", files, usable, old, upgraded);
  11955. CHECK (files >= 300 && usable == files && old >= 80 && upgraded == old && same == old);
  11956. std::ifstream in (folder / "Bell" / "Labyrinth.fst", std::ios::binary);
  11957. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  11958. if (file.empty())
  11959. return;
  11960. auto st = flp::parseProject (file, true).channels.at (0).pluginState;
  11961. const auto a = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11962. const std::int32_t zero = 0;
  11963. for (int part = 0; part < 2; ++part)
  11964. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, 92), &zero, 4); // image frequency scale 0
  11965. const auto b = lynx::harmor::renderReference (st, notes, 44100.0, 4096);
  11966. CHECK (a.size() == b.size() && a != b);
  11967. }
  11968. TEST (harmor_image)
  11969. {
  11970. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  11971. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py);
  11972. // since flbeta165h LYNX's own values: the test state plays at precision "Average" (global misc +28 = 0), whose
  11973. // IPP 11-bit Exp/Ln (REA_rodada165g-flbeta165h §3) the model computes exactly
  11974. const auto blob = harmorTestState (false, true);
  11975. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  11976. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  11977. CHECK (lynx::harmorStateUsable (blob));
  11978. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  11979. 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);
  11980. CHECK (ref.size() == 2 * 8192);
  11981. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  11982. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  11983. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  11984. const std::tuple<int, float, float> golden[] {
  11985. { 100, 0.0022266121f, 0.018016478f }, { 1000, 0.013964245f, -0.028604792f }, { 2500, 0.016782919f, -0.028934296f },
  11986. { 3000, -0.010533399f, -0.0061015566f }, { 5000, -0.017427528f, 0.016475817f }, { 6500, 0.16895676f, 0.078423217f },
  11987. { 8191, -0.045406573f, -0.049047086f } };
  11988. for (const auto& [i, gl, gr] : golden)
  11989. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  11990. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  11991. auto plain = blob;
  11992. const std::int32_t zero = 0;
  11993. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  11994. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  11995. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  11996. 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);
  11997. double diff = 0.0, energy = 0.0;
  11998. for (std::size_t i = 0; i < ref.size(); ++i)
  11999. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  12000. CHECK (energy > 1.0 && diff > 0.01 * energy);
  12001. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  12002. auto resyn = blob;
  12003. {
  12004. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  12005. 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 };
  12006. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  12007. CHECK (it != resyn.end());
  12008. if (it != resyn.end())
  12009. *(it + 4) = 1;
  12010. }
  12011. const auto fb = lynx::harmorFallbacks (resyn);
  12012. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  12013. }
  12014. TEST (harmor_round75_features)
  12015. {
  12016. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  12017. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's);
  12018. // since flbeta165h LYNX's own values: the test state plays at precision "Average", whose IPP 11-bit Exp/Ln
  12019. // (REA_rodada165g-flbeta165h §3) the model computes exactly
  12020. 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 } };
  12021. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  12022. const Case cases[] {
  12023. { 1, { { { 2000, 0.0032625794f, -0.0014364491f }, { 4500, 0.10464316f, 0.026205163f }, { 7000, -0.0024230368f, 0.0036458215f }, { 11000, -0.0029985344f, -0.0030873856f } } } },
  12024. { 2, { { { 2000, 0.29411051f, -0.22980602f }, { 4500, -0.004929353f, -0.0013274907f }, { 7000, 0.013239861f, 0.0054404787f }, { 11000, -0.0010576053f, -0.0021088289f } } } },
  12025. { 4, { { { 2000, 0.072069392f, -0.029683406f }, { 4500, -0.10765476f, 0.11332012f }, { 7000, -0.22984716f, -0.10102195f }, { 11000, -0.051904719f, -0.066571109f } } } },
  12026. { 8, { { { 2000, 0.072010428f, -0.029725688f }, { 4500, -0.10748001f, 0.1136639f }, { 7000, -0.22849904f, -0.15257275f }, { 11000, -0.041248851f, -0.050450616f } } } },
  12027. { 16, { { { 2000, 0.063442282f, -0.030946331f }, { 4500, -0.11211193f, 0.11279358f }, { 7000, -0.23168804f, -0.1670858f }, { 11000, -0.041186355f, -0.050383076f } } } },
  12028. { 32, { { { 2000, 0.086303107f, -0.035885293f }, { 4500, 0.069407858f, 0.14133474f }, { 7000, -0.15907738f, -0.10380846f }, { 11000, -0.029428774f, -0.041782599f } } } },
  12029. { 63, { { { 2000, 0.017201126f, -0.010683246f }, { 4500, 0.55659908f, 0.08110752f }, { 7000, -0.0025542902f, 0.020610567f }, { 11000, -0.0031871174f, 0.00024673535f } } } } };
  12030. for (const auto& c : cases)
  12031. {
  12032. const auto blob = harmorTestState (false, false, c.features);
  12033. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  12034. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  12035. CHECK (lynx::harmorStateUsable (blob));
  12036. CHECK (lynx::harmorFallbacks (blob).empty());
  12037. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  12038. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  12039. for (int i : { 2000, 4500, 7000, 11000 })
  12040. 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]);
  12041. for (const auto& [i, gl, gr] : c.golden)
  12042. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  12043. }
  12044. }
  12045. TEST (sytrus_main_settings)
  12046. {
  12047. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  12048. SytrusTestPatch t;
  12049. t.cell (0, 0, 8, 256);
  12050. t.main[173] = 3;
  12051. const auto blob = t.blob();
  12052. const auto& S = lynx::sytrus::settings();
  12053. auto find = [&] (const char* name) {
  12054. for (int i = 0; i < (int) S.size(); ++i)
  12055. if (std::string (S[(std::size_t) i].name) == name)
  12056. return i;
  12057. return -1;
  12058. };
  12059. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  12060. // 25 main controls, then the matrix (round 172a): FM 9 × 9 (sources, PAN, FX, OUT) and RM 9 × 6
  12061. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u + 81u + 54u);
  12062. const int out1 = find ("Op 1 out"), fm21 = find ("FM op 2 to op 1"), rm3f2 = find ("RM op 3 to filter 2");
  12063. CHECK (out1 >= 0 && fm21 >= 0 && rm3f2 >= 0 && find ("RM op 1 pan") < 0);
  12064. CHECK (out1 >= 0 && S[(std::size_t) out1].index == 3 + 8 && S[(std::size_t) rm3f2].index == 3 + 81 + 9 * 7 + 2);
  12065. CHECK (lynx::sytrus::settingValue (blob, out1) == 256 && lynx::sytrus::settingValue (blob, fm21) == 0);
  12066. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  12067. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  12068. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  12069. auto broken = blob;
  12070. broken.resize (broken.size() / 2);
  12071. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  12072. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  12073. auto quiet = blob;
  12074. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  12075. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  12076. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  12077. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  12078. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  12079. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  12080. double worst = 0.0;
  12081. for (int k = 0; k < 4096; ++k)
  12082. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  12083. CHECK (worst < 1e-5);
  12084. (void) r1, (void) r2;
  12085. // clamped to the control's range; PORTA is the flag byte the engine reads
  12086. auto edited = blob;
  12087. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  12088. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  12089. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  12090. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  12091. // a matrix cell: op 2 modulates op 1 (FM), clamped to ±256; the patch stays playable
  12092. CHECK (lynx::sytrus::setSetting (edited, fm21, 999) && lynx::sytrus::settingValue (edited, fm21) == 256);
  12093. CHECK (lynx::sytrus::settingValue (edited, out1) == 256 && lynx::sytrus::parsePatch (edited) != nullptr);
  12094. }
  12095. TEST (harmor_effects_rack)
  12096. {
  12097. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  12098. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  12099. auto blob = harmorTestState (false);
  12100. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  12101. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  12102. };
  12103. set (blob, 0, 10, 1000); // all to the FX send
  12104. auto render = [] (const std::vector<std::uint8_t>& st) {
  12105. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  12106. };
  12107. const auto dry = render (blob);
  12108. auto rev = blob;
  12109. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  12110. 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);
  12111. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  12112. const auto bypassed = render (rev);
  12113. double eb = 0.0, db = 0.0;
  12114. for (std::size_t i = 0; i < dry.size(); ++i)
  12115. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  12116. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  12117. auto after = rev;
  12118. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  12119. for (int k = 0; k < 6; ++k)
  12120. set (after, -1, 53 + k, order[k]);
  12121. const auto wet = render (after);
  12122. double e = 0.0, d = 0.0;
  12123. bool finite = true;
  12124. for (std::size_t i = 0; i < wet.size(); ++i)
  12125. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  12126. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  12127. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  12128. auto dist = harmorTestState (false);
  12129. {
  12130. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  12131. const std::int32_t six = 6;
  12132. std::memcpy (dist.data() + at, &six, 4);
  12133. }
  12134. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  12135. 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);
  12136. set (dist, -1, 47, 1050);
  12137. lynx::ChannelDef c;
  12138. c.instrument = lynx::InstrumentKind::harmor;
  12139. c.pluginState = dist;
  12140. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  12141. inst->configure (c, 120.0);
  12142. inst->prepare (44100.0);
  12143. std::vector<float> l (44100 * 4), r (l.size());
  12144. inst->noteOn (60, 0.8f);
  12145. double energy = 0.0;
  12146. for (int s = 0; s < (int) l.size(); s += 512)
  12147. {
  12148. if (s == 8192)
  12149. inst->noteOff (60);
  12150. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  12151. }
  12152. for (std::size_t i = 0; i < l.size(); ++i)
  12153. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  12154. CHECK (finite && energy > 1.0);
  12155. std::vector<float> tail (44100 * 6), tailR (tail.size());
  12156. for (int s = 0; s < (int) tail.size(); s += 512)
  12157. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  12158. CHECK (inst->idle());
  12159. }
  12160. TEST (harmless_engine)
  12161. {
  12162. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  12163. std::vector<std::uint8_t> blob;
  12164. 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))); };
  12165. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  12166. for (int i = 11; i < 23; ++i)
  12167. p[(std::size_t) i] = 1000;
  12168. 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;
  12169. 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;
  12170. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  12171. 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;
  12172. put (12);
  12173. for (auto v : p)
  12174. put (v);
  12175. for (int i = 0; i < 7; ++i)
  12176. put (0);
  12177. put (524); // Custom colour table: 524 × (amplitude, phase)
  12178. for (int i = 0; i < 1048; ++i)
  12179. {
  12180. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  12181. std::uint32_t bits = 0;
  12182. std::memcpy (&bits, &f, 4);
  12183. put ((std::int32_t) bits);
  12184. }
  12185. const auto parsed = lynx::harmlessParams (blob);
  12186. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  12187. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  12188. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  12189. auto old = blob;
  12190. old[0] = 3; // versions below 4 are not Harmless states we know
  12191. CHECK (! lynx::harmlessParams (old));
  12192. old = blob;
  12193. old.resize (300);
  12194. CHECK (! lynx::harmlessParams (old));
  12195. lynx::ChannelDef c;
  12196. c.instrument = lynx::InstrumentKind::harmless;
  12197. c.modelParams = *parsed;
  12198. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  12199. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  12200. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  12201. inst->configure (def, 120.0);
  12202. inst->prepare (44100.0);
  12203. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  12204. inst->noteOn (60, 0.78125f);
  12205. for (int s = 0; s < total; s += 512)
  12206. {
  12207. if (s == offAt)
  12208. inst->noteOff (60);
  12209. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  12210. }
  12211. return std::make_tuple (l, r, std::move (inst));
  12212. };
  12213. {
  12214. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  12215. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  12216. const std::tuple<int, float, float> golden[] {
  12217. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  12218. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  12219. for (const auto& [i, gl, gr] : golden)
  12220. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  12221. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  12222. std::vector<float> tail (44100), tailR (tail.size());
  12223. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  12224. CHECK (inst->idle());
  12225. }
  12226. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  12227. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  12228. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  12229. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  12230. auto mono = c;
  12231. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  12232. mono.modelParams[65] = 1.0f;
  12233. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  12234. double el = 0.0, er = 0.0;
  12235. for (int i = 0; i < 2048; ++i)
  12236. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  12237. CHECK (el < 1e-10 * er && er > 1.0);
  12238. }
  12239. TEST (startup_sound)
  12240. {
  12241. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  12242. // a landing "pop" at impactTime and a chime ringing after it
  12243. using namespace lynx::startup;
  12244. const auto sound = makeSound (48000.0);
  12245. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  12246. float peak = 0.0f;
  12247. bool finite = true;
  12248. for (std::size_t i = 0; i < sound.left.size(); ++i)
  12249. {
  12250. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  12251. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  12252. }
  12253. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  12254. auto rms = [&sound] (double from, double to) {
  12255. double e = 0.0;
  12256. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  12257. for (auto i = a; i < b; ++i)
  12258. e += (double) sound.left[i] * sound.left[i];
  12259. return std::sqrt (e / (double) (b - a));
  12260. };
  12261. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  12262. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  12263. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  12264. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  12265. }
  12266. namespace
  12267. {
  12268. // little-endian byte writer for the state builders below
  12269. struct Bytes
  12270. {
  12271. std::vector<std::uint8_t> b;
  12272. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  12273. 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); }
  12274. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  12275. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  12276. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  12277. };
  12278. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  12279. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  12280. // unison, slide, distortion, delay, reverb and the output EQ.
  12281. std::vector<std::uint8_t> gmsTestState()
  12282. {
  12283. std::array<float, 212> P {};
  12284. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  12285. P[(std::size_t) i] = 0.5f;
  12286. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  12287. 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;
  12288. 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;
  12289. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  12290. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  12291. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  12292. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  12293. Bytes preset;
  12294. preset.raw ("4nyS", 4);
  12295. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  12296. {
  12297. const auto n = std::strlen (path);
  12298. preset.raw (path, n);
  12299. preset.zeros (512 - n);
  12300. }
  12301. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  12302. for (auto v : P)
  12303. preset.f32 (v);
  12304. preset.raw ("LYNX test", 9);
  12305. preset.zeros (512 - 9);
  12306. Bytes s;
  12307. s.raw ("SSLF", 4);
  12308. s.i32 ((std::int32_t) preset.b.size());
  12309. s.bytes (preset.b);
  12310. return s.b;
  12311. }
  12312. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  12313. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  12314. // amplitude through the matrix), the master ADSR and every plugin effect on.
  12315. std::vector<std::uint8_t> morphineTestState()
  12316. {
  12317. auto mbp = [] (int seed) {
  12318. Bytes b;
  12319. b.raw ("0PBM", 4);
  12320. for (int a = 0; a < 3; ++a)
  12321. for (int byte = 0; byte < 16; ++byte)
  12322. {
  12323. b.b.push_back (0xff);
  12324. for (int bit = 0; bit < 8; ++bit)
  12325. {
  12326. const auto k = byte * 8 + bit;
  12327. // (cbrtf is not correctly rounded before glibc 2.43 and differs between libms; the cube root in double
  12328. // rounded to float gives the same (correctly rounded) bits with old and new glibc)
  12329. const float v = a == 0 ? (float) std::cbrt ((double) (1.0f / (float) (k + 1 + seed * (k % 3)))) * (k < 40 ? 1.0f : 0.2f)
  12330. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  12331. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  12332. b.f32 (v);
  12333. }
  12334. }
  12335. return b.b;
  12336. };
  12337. auto msp = [&] (int frames, int seed, int loopMode) {
  12338. Bytes b;
  12339. b.raw ("0PSM", 4);
  12340. b.i32 (frames);
  12341. for (int f = 0; f < frames; ++f)
  12342. {
  12343. const auto m = mbp (seed + f);
  12344. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  12345. b.bytes (m);
  12346. }
  12347. b.i32 (0x424);
  12348. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  12349. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  12350. for (int k = 0; k < 128; ++k)
  12351. b.f32 (0.3f);
  12352. for (int k = 0; k < 128; ++k)
  12353. b.f32 (1.0f);
  12354. return b.b;
  12355. };
  12356. auto mvc = [&] (int frames, int seed, int pwmMode) {
  12357. Bytes z;
  12358. z.raw ("0HZM", 4);
  12359. z.i32 (1);
  12360. const auto s = msp (frames, seed, 1);
  12361. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  12362. z.bytes (s);
  12363. Bytes b;
  12364. b.raw ("0CVM", 4);
  12365. b.i32 ((std::int32_t) z.b.size());
  12366. b.bytes (z.b);
  12367. 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);
  12368. return b.b;
  12369. };
  12370. Bytes p;
  12371. p.raw ("0roM", 4);
  12372. p.raw ("0RPM", 4);
  12373. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  12374. {
  12375. p.i32 ((std::int32_t) g.size());
  12376. p.bytes (g);
  12377. }
  12378. {
  12379. Bytes m;
  12380. m.raw ("0LMM", 4);
  12381. m.i32 (2), m.i32 (12);
  12382. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  12383. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  12384. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  12385. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  12386. m.f32 (v);
  12387. for (int k = 0; k < 4; ++k)
  12388. m.i32 (0);
  12389. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  12390. p.i32 ((std::int32_t) m.b.size());
  12391. p.bytes (m.b);
  12392. }
  12393. {
  12394. Bytes d;
  12395. d.raw ("0DMM", 4);
  12396. d.i32 (4);
  12397. for (int e = 0; e < 4; ++e)
  12398. {
  12399. Bytes g;
  12400. g.raw ("0GEM", 4);
  12401. g.i32 (3), g.i32 (8);
  12402. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  12403. 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),
  12404. g.i32 (e == 0 ? 1 : 0);
  12405. d.i32 ((std::int32_t) g.b.size());
  12406. d.bytes (g.b);
  12407. }
  12408. d.i32 (0x94);
  12409. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  12410. const float amt[12] = { 0.56f, 0.7f };
  12411. for (int k = 0; k < 12; ++k)
  12412. d.i32 (src[k]);
  12413. for (int k = 0; k < 12; ++k)
  12414. d.i32 (dst[k]);
  12415. for (int k = 0; k < 12; ++k)
  12416. d.f32 (amt[k]);
  12417. p.i32 ((std::int32_t) d.b.size());
  12418. p.bytes (d.b);
  12419. }
  12420. {
  12421. 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
  12422. 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 …
  12423. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  12424. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  12425. Bytes g;
  12426. g.i32 (0x5c4);
  12427. for (auto v : A)
  12428. g.f32 (v);
  12429. g.zeros (0x5c4 - g.b.size());
  12430. p.bytes (g.b);
  12431. }
  12432. Bytes s;
  12433. s.raw ("SSLF", 4);
  12434. s.i32 ((std::int32_t) p.b.size());
  12435. s.bytes (p.b);
  12436. return s.b;
  12437. }
  12438. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  12439. {
  12440. if (const char* path = std::getenv (var))
  12441. {
  12442. std::ofstream out (path, std::ios::binary);
  12443. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  12444. }
  12445. }
  12446. } // namespace
  12447. TEST (dx10_engine)
  12448. {
  12449. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  12450. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  12451. // instrument front-end
  12452. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  12453. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  12454. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  12455. blob[0] = 1;
  12456. std::memcpy (blob.data() + 4, P, sizeof (P));
  12457. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  12458. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  12459. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  12460. {
  12461. auto bad = blob;
  12462. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  12463. CHECK (! lynx::dx10StateUsable (bad));
  12464. }
  12465. using N = lynx::dx10::RefNote;
  12466. 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 } };
  12467. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12468. auto check = [&] (const Golden& g) {
  12469. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12470. CHECK (y.size() == 2 * 16384);
  12471. if (y.size() != 2 * 16384)
  12472. return y;
  12473. double el = 0.0, er = 0.0;
  12474. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12475. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12476. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12477. for (const auto& [i, l, r] : g.at)
  12478. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12479. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12480. return y;
  12481. };
  12482. lynx::dx10::RefOptions plain, odd;
  12483. odd.block = 333, odd.maxPoly = 1;
  12484. check ({ blob, plain, 44100, 672.293115, 778.393071,
  12485. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  12486. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  12487. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  12488. check ({ blob, odd, 48000, 122.232761, 493.320297,
  12489. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  12490. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  12491. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  12492. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12493. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12494. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12495. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12496. for (const auto& [i, v] : fields)
  12497. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12498. };
  12499. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12500. std::memcpy (s.data() + 0x15c, &modX, 4);
  12501. std::memcpy (s.data() + 0x160, &modY, 4);
  12502. std::memcpy (s.data() + 0x168, &type, 4);
  12503. };
  12504. std::vector<std::uint8_t> snap (364, 0);
  12505. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12506. 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 } });
  12507. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12508. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12509. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  12510. settings (snap, 140, 120, 7);
  12511. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  12512. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  12513. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  12514. lynx::dx10::RefOptions ticks;
  12515. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  12516. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  12517. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  12518. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  12519. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  12520. std::vector<std::uint8_t> decay (364, 0);
  12521. for (int k = 0; k < 5; ++k)
  12522. record (decay, k, {});
  12523. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  12524. settings (decay, 256, 0, 0);
  12525. auto ticks48 = ticks;
  12526. ticks48.block = 333;
  12527. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  12528. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  12529. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  12530. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  12531. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  12532. silentTail = silentTail && y[i] == 0.0f;
  12533. CHECK (silentTail);
  12534. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  12535. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  12536. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  12537. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  12538. auto noVib = blob;
  12539. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  12540. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  12541. lynx::ChannelDef c;
  12542. c.instrument = lynx::InstrumentKind::dx10;
  12543. c.pluginState = noVib;
  12544. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12545. inst->configure (c, 120.0);
  12546. inst->prepare (44100.0);
  12547. const float V = 0.6f, pan = 0.25f;
  12548. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12549. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12550. std::vector<float> l (16384), r (16384);
  12551. for (int s = 0; s < 16384; s += 512)
  12552. {
  12553. if (s == 0)
  12554. inst->noteOn (60, 0.78125f);
  12555. if (s == 2048)
  12556. {
  12557. inst->setNextNoteExpression (-0.5f, 25.0f);
  12558. inst->noteOn (67, 0.5f);
  12559. }
  12560. if (s == 6144)
  12561. inst->noteOff (60);
  12562. if (s == 8192)
  12563. inst->noteOff (67);
  12564. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12565. }
  12566. // the channel level reaches the engine as FL's (recovered to float precision)
  12567. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12568. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12569. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  12570. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12571. 44100.0, 16384);
  12572. double worst = 0.0, peak = 0.0;
  12573. for (int i = 0; i < 16384; ++i)
  12574. 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]) }),
  12575. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12576. CHECK (worst == 0.0 && peak > 0.05);
  12577. // the released voices end (DX10's own release) and the instrument goes idle
  12578. std::vector<float> tail (44100), tailR (tail.size());
  12579. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  12580. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  12581. CHECK (inst->idle());
  12582. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  12583. {
  12584. lynx::ChannelDef cs = c;
  12585. cs.pluginState = withSnap;
  12586. cs.modelParams = { 96.0f };
  12587. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  12588. si->configure (cs, 120.0);
  12589. si->prepare (44100.0);
  12590. std::vector<float> sl (32768), sr (32768);
  12591. si->noteOn (48, 0.78125f);
  12592. for (int s = 0; s < 32768; s += 512)
  12593. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  12594. double e1 = 0.0, e2 = 0.0;
  12595. bool finite = true;
  12596. for (int i = 0; i < 32768; ++i)
  12597. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  12598. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  12599. }
  12600. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  12601. auto edited = blob;
  12602. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  12603. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  12604. }
  12605. TEST (threeosc_engine)
  12606. {
  12607. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  12608. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  12609. 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 };
  12610. 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) {
  12611. std::vector<std::uint8_t> st (0x60, 0);
  12612. const std::int32_t version = 14;
  12613. std::memcpy (st.data(), &version, 4);
  12614. std::memcpy (st.data() + 4, p, 21 * 4);
  12615. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  12616. st.insert (st.end(), snap.begin(), snap.end());
  12617. return st;
  12618. };
  12619. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  12620. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  12621. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  12622. for (const auto& [i, v] : fields)
  12623. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  12624. };
  12625. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  12626. std::memcpy (s.data() + 0x15c, &modX, 4);
  12627. std::memcpy (s.data() + 0x160, &modY, 4);
  12628. std::memcpy (s.data() + 0x168, &type, 4);
  12629. };
  12630. std::vector<std::uint8_t> neutral (364, 0);
  12631. for (int k = 0; k < 5; ++k)
  12632. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  12633. settings (neutral, 256, 0, 0);
  12634. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  12635. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  12636. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  12637. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  12638. // version 10 (20 parameters, no invert/AM bytes)
  12639. {
  12640. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  12641. v12[0] = 12;
  12642. v12[0x58] = 1;
  12643. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  12644. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  12645. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  12646. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  12647. v10[0] = 10;
  12648. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  12649. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  12650. }
  12651. using N = lynx::threeosc::RefNote;
  12652. 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 } };
  12653. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  12654. auto check = [&] (const Golden& g) {
  12655. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  12656. CHECK (y.size() == 2 * 16384);
  12657. if (y.size() != 2 * 16384)
  12658. return y;
  12659. double el = 0.0, er = 0.0;
  12660. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12661. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12662. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  12663. for (const auto& [i, l, r] : g.at)
  12664. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, libmTolerance (1e-7f))
  12665. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, libmTolerance (1e-7f)));
  12666. return y;
  12667. };
  12668. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  12669. lynx::threeosc::RefOptions plain;
  12670. plain.seed = 12345;
  12671. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  12672. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  12673. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  12674. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  12675. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  12676. 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 };
  12677. lynx::threeosc::RefOptions odd;
  12678. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  12679. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  12680. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  12681. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  12682. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  12683. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  12684. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  12685. std::vector<std::uint8_t> snap (364, 0);
  12686. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  12687. 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 } });
  12688. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  12689. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  12690. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  12691. settings (snap, 140, 120, 7);
  12692. lynx::threeosc::RefOptions ticks;
  12693. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  12694. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  12695. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  12696. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  12697. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  12698. auto ticks48 = ticks;
  12699. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  12700. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  12701. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  12702. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  12703. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  12704. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  12705. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  12706. // one agree to float rounding, so the golden values hold to 1e-5)
  12707. {
  12708. 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 };
  12709. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  12710. hq[0x5c] = 1;
  12711. CHECK (lynx::threeOscParam (hq, 25) == 1);
  12712. lynx::threeosc::RefOptions o;
  12713. o.seed = 3;
  12714. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  12715. CHECK (y.size() == 2 * 16384);
  12716. if (y.size() == 2 * 16384)
  12717. {
  12718. double el = 0.0, er = 0.0;
  12719. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12720. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12721. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  12722. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  12723. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  12724. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  12725. for (const auto& [i, l, r] : at)
  12726. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  12727. }
  12728. }
  12729. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  12730. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  12731. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  12732. lynx::ChannelDef c;
  12733. c.instrument = lynx::InstrumentKind::threeosc;
  12734. c.pluginState = blob;
  12735. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  12736. inst->configure (c, 120.0);
  12737. inst->prepare (44100.0);
  12738. const float V = 0.6f, pan = 0.25f;
  12739. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  12740. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  12741. std::vector<float> l (16384), r (16384);
  12742. for (int s = 0; s < 16384; s += 512)
  12743. {
  12744. if (s == 0)
  12745. inst->noteOn (60, 0.78125f);
  12746. if (s == 2048)
  12747. {
  12748. inst->setNextNoteExpression (-0.5f, 25.0f);
  12749. inst->noteOn (67, 0.5f);
  12750. }
  12751. if (s == 6144)
  12752. inst->noteOff (60);
  12753. if (s == 8192)
  12754. inst->noteOff (67);
  12755. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  12756. }
  12757. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  12758. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  12759. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  12760. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  12761. 44100.0, 16384);
  12762. double worst = 0.0, peak = 0.0;
  12763. for (int i = 0; i < 16384; ++i)
  12764. 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]) }),
  12765. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  12766. CHECK (worst == 0.0 && peak > 0.05);
  12767. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  12768. CHECK (inst->idle());
  12769. // parameter edits write the raw integers, clamped to the knob ranges
  12770. auto edited = blob;
  12771. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  12772. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  12773. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  12774. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  12775. }
  12776. TEST (gms_engine)
  12777. {
  12778. const auto blob = gmsTestState();
  12779. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  12780. CHECK (lynx::gmsStateUsable (blob));
  12781. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  12782. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  12783. auto broken = blob;
  12784. broken[8] = 'X';
  12785. CHECK (! lynx::gmsStateUsable (broken));
  12786. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  12787. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  12788. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  12789. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12790. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  12791. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  12792. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  12793. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  12794. CHECK (y.size() == 2 * 66150u);
  12795. 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 } };
  12796. for (const auto& g : golden)
  12797. 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);
  12798. double e = 0.0;
  12799. for (auto v : y)
  12800. e += (double) v * v;
  12801. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  12802. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12803. lynx::ChannelDef c;
  12804. c.instrument = lynx::InstrumentKind::gms;
  12805. c.pluginState = blob;
  12806. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  12807. inst->configure (c, 140.0);
  12808. inst->prepare (44100.0);
  12809. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12810. for (int b = 0; b < 16; ++b)
  12811. {
  12812. if (b == 0)
  12813. inst->noteOn (60, 0.78125f);
  12814. if (b == 10)
  12815. inst->noteOff (60);
  12816. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12817. }
  12818. 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, "");
  12819. double diff = 0.0, energy = 0.0;
  12820. for (int i = 0; i < 8192; ++i)
  12821. {
  12822. 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]) });
  12823. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12824. }
  12825. CHECK (diff == 0.0 && energy > 1e-3);
  12826. }
  12827. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  12828. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  12829. std::vector<std::uint8_t> toxicTestState()
  12830. {
  12831. auto s = lynx::toxicDefaultState();
  12832. static const std::pair<int, double> set[] = {
  12833. { 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 },
  12834. { 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 },
  12835. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  12836. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  12837. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  12838. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  12839. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  12840. for (const auto& [index, value] : set)
  12841. {
  12842. const auto v = (float) value;
  12843. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  12844. }
  12845. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  12846. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  12847. std::memcpy (s.data() + at, &amp, 4);
  12848. std::memcpy (s.data() + at + 512, &phase, 4);
  12849. };
  12850. for (int h = 0; h < 24; ++h)
  12851. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  12852. for (int k = 0; k < 16; ++k)
  12853. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  12854. return s;
  12855. }
  12856. TEST (toxic_engine)
  12857. {
  12858. const auto blob = toxicTestState();
  12859. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  12860. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  12861. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  12862. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  12863. auto broken = blob;
  12864. broken[8] = 'X';
  12865. CHECK (! lynx::toxicStateUsable (broken));
  12866. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  12867. auto needs = blob;
  12868. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  12869. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  12870. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  12871. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  12872. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  12873. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  12874. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  12875. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  12876. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  12877. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  12878. CHECK (y.size() == 2 * 66150u);
  12879. 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 } };
  12880. for (const auto& g : golden)
  12881. 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);
  12882. double e = 0.0;
  12883. for (auto v : y)
  12884. e += (double) v * v;
  12885. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  12886. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  12887. lynx::ChannelDef c;
  12888. c.instrument = lynx::InstrumentKind::toxic;
  12889. c.pluginState = blob;
  12890. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  12891. inst->configure (c, 140.0);
  12892. inst->prepare (44100.0);
  12893. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12894. for (int b = 0; b < 16; ++b)
  12895. {
  12896. if (b == 0)
  12897. inst->noteOn (60, 0.78125f);
  12898. if (b == 10)
  12899. inst->noteOff (60);
  12900. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12901. }
  12902. 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 } },
  12903. 44100.0, 8192, "", 512, 140.0);
  12904. double diff = 0.0, energy = 0.0;
  12905. for (int i = 0; i < 8192; ++i)
  12906. {
  12907. 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]) });
  12908. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12909. }
  12910. CHECK (diff == 0.0 && energy > 1e-3);
  12911. }
  12912. TEST (morphine_engine)
  12913. {
  12914. const auto blob = morphineTestState();
  12915. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  12916. CHECK (lynx::morphineStateUsable (blob));
  12917. CHECK (lynx::morphineNoiseName (blob).empty());
  12918. const auto at = lynx::morphineParamOffset (blob, 9);
  12919. CHECK (at > 0);
  12920. float a9 = 0.0f;
  12921. std::memcpy (&a9, blob.data() + at, 4);
  12922. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  12923. auto broken = blob;
  12924. broken[12] = 'X'; // the MPR0 tag
  12925. CHECK (! lynx::morphineStateUsable (broken));
  12926. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  12927. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  12928. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  12929. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  12930. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  12931. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  12932. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  12933. CHECK (y.size() == 2 * 80000u);
  12934. 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 } };
  12935. for (const auto& g : golden)
  12936. 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);
  12937. double e = 0.0;
  12938. for (auto v : y)
  12939. e += (double) v * v;
  12940. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  12941. // the instrument: same engine behind LYNX's channel gains
  12942. lynx::ChannelDef c;
  12943. c.instrument = lynx::InstrumentKind::morphine;
  12944. c.pluginState = blob;
  12945. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  12946. inst->configure (c, 120.0);
  12947. inst->prepare (44100.0);
  12948. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12949. for (int b = 0; b < 16; ++b)
  12950. {
  12951. if (b == 0)
  12952. inst->noteOn (60, 0.78125f);
  12953. if (b == 10)
  12954. inst->noteOff (60);
  12955. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12956. }
  12957. 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, "");
  12958. double diff = 0.0, energy = 0.0;
  12959. for (int i = 0; i < 8192; ++i)
  12960. {
  12961. 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]) });
  12962. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12963. }
  12964. CHECK (diff == 0.0 && energy > 1e-3);
  12965. }
  12966. namespace
  12967. {
  12968. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  12969. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  12970. {
  12971. const std::string name = "LYNX test";
  12972. Bytes body;
  12973. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  12974. body.raw ("HEAD", 4);
  12975. body.i32 (6 + 2 * (std::int32_t) name.size());
  12976. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  12977. body.raw (reinterpret_cast<const char*> (head), 6);
  12978. for (char ch : name)
  12979. body.raw (&ch, 1), body.zeros (1);
  12980. body.raw ("DATA", 4);
  12981. body.i32 (176);
  12982. body.raw ("0rev", 4);
  12983. for (auto v : P)
  12984. body.f32 (v);
  12985. Bytes s;
  12986. s.raw ("SSLF", 4);
  12987. s.i32 ((std::int32_t) body.b.size());
  12988. s.bytes (body.b);
  12989. return s.b;
  12990. }
  12991. std::array<float, 43> miniSynthDefaults()
  12992. {
  12993. 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,
  12994. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  12995. }
  12996. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  12997. // fixed name let one process read a file another one was rewriting
  12998. std::string processTempTag()
  12999. {
  13000. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  13001. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  13002. static const struct Cleanup
  13003. {
  13004. ~Cleanup()
  13005. {
  13006. // every scratch file or folder whose name carries this process's tag (round 173e: the SoundFont, case,
  13007. // missing-file, Crash169g, Natives142 and Rea167l scratch paths are per process too)
  13008. std::error_code ec;
  13009. std::vector<std::filesystem::path> mine;
  13010. for (const auto& e : std::filesystem::directory_iterator (std::filesystem::temp_directory_path (ec), ec))
  13011. if (e.path().filename().string().find (tag) != std::string::npos)
  13012. mine.push_back (e.path());
  13013. for (const auto& p : mine)
  13014. std::filesystem::remove_all (p, ec);
  13015. }
  13016. } cleanup;
  13017. (void) cleanup;
  13018. return tag;
  13019. }
  13020. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  13021. std::string miniSynthTestOscdata()
  13022. {
  13023. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  13024. std::vector<float> d (2048);
  13025. for (int h = 0; h < 1024; ++h)
  13026. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  13027. std::ofstream out (path, std::ios::binary);
  13028. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  13029. return path;
  13030. }
  13031. } // namespace
  13032. TEST (minisynth_engine)
  13033. {
  13034. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  13035. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  13036. 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 },
  13037. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  13038. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  13039. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  13040. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  13041. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  13042. // B: lead (legato glide), BELL, LFO → pitch
  13043. 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 },
  13044. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  13045. for (const auto& [i, v] : a)
  13046. A[(std::size_t) i] = v;
  13047. for (const auto& [i, v] : b)
  13048. B[(std::size_t) i] = v;
  13049. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  13050. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  13051. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  13052. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  13053. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  13054. auto shortData = blobA;
  13055. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  13056. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  13057. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  13058. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  13059. const auto osc = miniSynthTestOscdata();
  13060. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  13061. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  13062. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  13063. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  13064. struct Golden { int i; float l, r; };
  13065. 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 } };
  13066. 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 } };
  13067. for (int which = 0; which < 2; ++which)
  13068. {
  13069. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  13070. CHECK (y.size() == 2 * 66150u);
  13071. if (y.size() != 2 * 66150u)
  13072. continue;
  13073. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  13074. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  13075. double e = 0.0;
  13076. for (auto v : y)
  13077. e += (double) v * v;
  13078. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  13079. }
  13080. // missing oscdata.dat: silent tables, still finite
  13081. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  13082. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  13083. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  13084. lynx::ChannelDef c;
  13085. c.instrument = lynx::InstrumentKind::minisynth;
  13086. c.pluginState = blobA;
  13087. c.samplePath = osc;
  13088. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  13089. inst->configure (c, 120.0);
  13090. inst->prepare (44100.0);
  13091. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13092. for (int k = 0; k < 16; ++k)
  13093. {
  13094. if (k == 0)
  13095. inst->noteOn (60, 0.78125f);
  13096. if (k == 10)
  13097. inst->noteOff (60);
  13098. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  13099. }
  13100. 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 } },
  13101. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  13102. double diff = 0.0, energy = 0.0;
  13103. for (int i = 0; i < 8192; ++i)
  13104. {
  13105. 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]) });
  13106. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13107. }
  13108. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  13109. }
  13110. namespace
  13111. {
  13112. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  13113. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  13114. std::vector<std::uint8_t> poizoneTestState()
  13115. {
  13116. std::array<float, 156> P {};
  13117. 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 },
  13118. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  13119. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  13120. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  13121. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  13122. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  13123. { 79, 1.0f } };
  13124. for (const auto& [i, v] : values)
  13125. P[(std::size_t) i] = v;
  13126. for (int i = 49; i < 65; ++i)
  13127. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  13128. for (int i = 81; i < 155; ++i)
  13129. P[(std::size_t) i] = -1.0f;
  13130. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  13131. blob.resize (12 + 4 * 156 + 64, 0);
  13132. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  13133. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  13134. return blob;
  13135. }
  13136. } // namespace
  13137. TEST (poizone_engine)
  13138. {
  13139. const auto blob = poizoneTestState();
  13140. CHECK (lynx::poizoneStateUsable (blob));
  13141. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  13142. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  13143. auto broken = blob;
  13144. broken[8] = 'X'; // the "Ver4" tag
  13145. CHECK (! lynx::poizoneStateUsable (broken));
  13146. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  13147. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  13148. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  13149. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  13150. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  13151. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  13152. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  13153. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  13154. CHECK (y.size() == 2 * 66150u);
  13155. 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 } };
  13156. for (const auto& g : golden)
  13157. 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);
  13158. double e = 0.0;
  13159. for (auto v : y)
  13160. e += (double) v * v;
  13161. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  13162. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  13163. lynx::ChannelDef c;
  13164. c.instrument = lynx::InstrumentKind::poizone;
  13165. c.pluginState = blob;
  13166. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  13167. inst->configure (c, 120.0);
  13168. inst->prepare (44100.0);
  13169. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13170. for (int b = 0; b < 16; ++b)
  13171. {
  13172. if (b == 0)
  13173. inst->noteOn (60, 0.78125f);
  13174. if (b == 10)
  13175. inst->noteOff (60);
  13176. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13177. }
  13178. 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,
  13179. 512, 120.0, 0, true);
  13180. double diff = 0.0, energy = 0.0;
  13181. for (int i = 0; i < 8192; ++i)
  13182. {
  13183. 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]) });
  13184. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13185. }
  13186. CHECK (diff == 0.0 && energy > 1e-3);
  13187. }
  13188. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  13189. namespace
  13190. {
  13191. std::vector<std::uint8_t> fruitKickStateA()
  13192. {
  13193. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  13194. std::vector<std::uint8_t> b (32);
  13195. std::memcpy (b.data(), w, 32);
  13196. return b;
  13197. }
  13198. std::vector<std::uint8_t> fruitKickStateB()
  13199. {
  13200. std::vector<std::uint8_t> b (400, 0);
  13201. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  13202. std::memcpy (b.data(), head, sizeof (head));
  13203. std::int32_t recs[5][17];
  13204. for (auto& r : recs)
  13205. {
  13206. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  13207. std::memcpy (r, d, sizeof (d));
  13208. }
  13209. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  13210. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  13211. std::memcpy (recs[0], pan, sizeof (pan));
  13212. std::memcpy (recs[1], vol, sizeof (vol));
  13213. for (int k = 0; k < 5; ++k)
  13214. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  13215. const std::int32_t cut = 256;
  13216. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  13217. return b;
  13218. }
  13219. std::string bassDrumTestFolder()
  13220. {
  13221. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  13222. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  13223. std::filesystem::create_directories (dir / "Noise Samples");
  13224. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  13225. std::ofstream o (path, std::ios::binary);
  13226. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  13227. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  13228. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  13229. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  13230. o.write ((const char*) data, bytes);
  13231. };
  13232. std::vector<float> table (1024);
  13233. for (int i = 0; i < 1024; ++i)
  13234. 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));
  13235. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  13236. std::vector<std::int16_t> noise (3000);
  13237. std::uint32_t s = 12345;
  13238. for (auto& v : noise)
  13239. {
  13240. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  13241. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  13242. }
  13243. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  13244. return dir.string();
  13245. }
  13246. std::vector<std::uint8_t> bassDrumTestState()
  13247. {
  13248. std::vector<std::uint8_t> b (0x50C + 92, 0);
  13249. std::memcpy (b.data(), "SSLF", 4);
  13250. const std::uint32_t size = 0x560;
  13251. std::memcpy (b.data() + 4, &size, 4);
  13252. std::memcpy (b.data() + 8, "2kiK", 4);
  13253. std::memcpy (b.data() + 12, "Test kick", 9);
  13254. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  13255. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  13256. 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,
  13257. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  13258. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  13259. return b;
  13260. }
  13261. struct Golden
  13262. {
  13263. int i;
  13264. float l, r;
  13265. };
  13266. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  13267. {
  13268. bool ok = true;
  13269. for (const auto& g : golden)
  13270. 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;
  13271. double e = 0.0;
  13272. for (auto v : y)
  13273. e += (double) v * v;
  13274. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  13275. }
  13276. } // namespace
  13277. TEST (fruitkick_engine)
  13278. {
  13279. const auto A = fruitKickStateA(), B = fruitKickStateB();
  13280. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  13281. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  13282. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  13283. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  13284. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  13285. std::vector<std::vector<std::uint8_t>> recs;
  13286. for (int k = 0; k < 5; ++k)
  13287. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  13288. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  13289. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  13290. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  13291. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  13292. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  13293. 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 } };
  13294. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  13295. CHECK (ya.size() == 40000u);
  13296. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  13297. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  13298. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  13299. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  13300. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  13301. 0.284267665614));
  13302. std::vector<int> ticks;
  13303. for (int t = 115; t < 20000; t += 230)
  13304. ticks.push_back (t);
  13305. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  13306. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  13307. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  13308. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  13309. 0.117964927585));
  13310. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  13311. lynx::ChannelDef c;
  13312. c.instrument = lynx::InstrumentKind::fruitkick;
  13313. c.pluginState = A;
  13314. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  13315. inst->configure (c, 140.0);
  13316. inst->prepare (44100.0);
  13317. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13318. for (int b = 0; b < 16; ++b)
  13319. {
  13320. if (b == 0)
  13321. inst->noteOn (60, 0.78125f);
  13322. if (b == 10)
  13323. inst->noteOff (60);
  13324. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13325. }
  13326. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  13327. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  13328. double diff = 0.0, energy = 0.0;
  13329. for (int i = 0; i < 8192; ++i)
  13330. {
  13331. 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]) });
  13332. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13333. }
  13334. CHECK (diff < 1e-6 && energy > 1e-2);
  13335. }
  13336. TEST (ipp_cos_and_end_fade)
  13337. {
  13338. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  13339. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  13340. const auto f = ippcos::bitsToFloat;
  13341. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  13342. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  13343. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  13344. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  13345. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  13346. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  13347. // tiny addend (lost in a plain double sum) must decide
  13348. const float a = 1.0f + 0x1p-12f;
  13349. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  13350. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  13351. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  13352. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  13353. std::vector<float> t (441);
  13354. ippcos::endFadeTable (t.data(), 441);
  13355. double worst = 0.0;
  13356. for (int i = 0; i < 441; ++i)
  13357. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  13358. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  13359. }
  13360. TEST (bassdrum_engine)
  13361. {
  13362. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  13363. const float x = 0.87011606f;
  13364. std::uint32_t bits = 0;
  13365. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  13366. std::memcpy (&bits, &p, 4);
  13367. CHECK (bits == 1065063964u);
  13368. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  13369. const auto st = bassDrumTestState();
  13370. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  13371. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  13372. auto broken = st;
  13373. broken[8] = 'X';
  13374. CHECK (! lynx::bassDrumStateUsable (broken));
  13375. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  13376. const auto dir = bassDrumTestFolder();
  13377. 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 },
  13378. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  13379. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  13380. CHECK (y.size() == 60000u);
  13381. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  13382. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  13383. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  13384. 0.230007588169));
  13385. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  13386. lynx::ChannelDef c;
  13387. c.instrument = lynx::InstrumentKind::bassdrum;
  13388. c.pluginState = st;
  13389. c.samplePath = dir;
  13390. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  13391. inst->configure (c, 120.0);
  13392. inst->prepare (44100.0);
  13393. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  13394. for (int b = 0; b < 16; ++b)
  13395. {
  13396. if (b == 0)
  13397. inst->noteOn (60, 0.78125f);
  13398. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  13399. }
  13400. 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);
  13401. double diff = 0.0, energy = 0.0;
  13402. for (int i = 0; i < 8192; ++i)
  13403. {
  13404. 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]) });
  13405. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13406. }
  13407. CHECK (diff == 0.0 && energy > 1e-2);
  13408. }
  13409. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  13410. TEST (fruity_fast_dist_flangus_flanger)
  13411. {
  13412. std::vector<float> inL (4096), inR (4096);
  13413. for (int n = 0; n < 4096; ++n)
  13414. {
  13415. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13416. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  13417. }
  13418. struct Gold { int i; float l, r; };
  13419. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13420. // Fast Dist (+ FL's host waveshaper): live and render kernels
  13421. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  13422. std::vector<float> l (inL), r (inR);
  13423. lynx::fastdistfx::process (l.data(), 4096, s, render);
  13424. lynx::fastdistfx::process (r.data(), 4096, s, render);
  13425. for (const auto& g : gold)
  13426. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13427. };
  13428. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  13429. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  13430. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  13431. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  13432. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  13433. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  13434. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  13435. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  13436. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  13437. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  13438. {
  13439. const auto* T = lynx::fastdistfx::curve (0, 10);
  13440. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  13441. }
  13442. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  13443. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  13444. lynx::flangusfx::Engine e;
  13445. e.prepare (fs);
  13446. e.load (s);
  13447. e.setRate (fs);
  13448. e.setBlockSize (512);
  13449. std::vector<float> io (8192);
  13450. for (int n = 0; n < 4096; ++n)
  13451. {
  13452. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13453. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13454. }
  13455. for (int o = 0; o < 4096; o += 512)
  13456. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13457. for (const auto& g : gold)
  13458. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13459. };
  13460. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  13461. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  13462. { 4095, 0.201686561f, 0.607052445f } });
  13463. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  13464. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  13465. { 4095, -0.354708612f, 0.503573656f } });
  13466. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  13467. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  13468. { 4095, 1.49345946f, -0.537866116f } });
  13469. // Flanger: state, then the sample rate (FL's call order)
  13470. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  13471. lynx::flangerfx::Engine e;
  13472. e.load (s);
  13473. e.prepare (fs);
  13474. std::vector<float> io (8192);
  13475. for (int n = 0; n < 4096; ++n)
  13476. {
  13477. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  13478. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13479. }
  13480. for (int o = 0; o < 4096; o += 512)
  13481. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13482. for (const auto& g : gold)
  13483. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13484. };
  13485. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  13486. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  13487. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  13488. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  13489. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  13490. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  13491. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  13492. {
  13493. 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 };
  13494. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  13495. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  13496. if (slot)
  13497. {
  13498. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  13499. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  13500. auto fx = lynx::makeEffect (*slot);
  13501. fx->prepare (44100.0);
  13502. std::vector<float> l (inL), r (inR);
  13503. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13504. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  13505. }
  13506. std::vector<std::uint8_t> fg (32);
  13507. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  13508. std::memcpy (fg.data(), v, 32);
  13509. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  13510. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  13511. if (f2)
  13512. {
  13513. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  13514. for (int i = 0; i < 8; ++i)
  13515. CHECK (s[(std::size_t) i] == v[i]);
  13516. auto fx = lynx::makeEffect (*f2);
  13517. fx->prepare (48000.0);
  13518. std::vector<float> l (inL), r (inR);
  13519. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  13520. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  13521. }
  13522. std::vector<std::uint8_t> fl (52);
  13523. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  13524. std::memcpy (fl.data(), w, 52);
  13525. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  13526. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  13527. if (f3)
  13528. {
  13529. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  13530. for (int i = 0; i < 12; ++i)
  13531. CHECK (s[(std::size_t) i] == w[i + 1]);
  13532. auto fx = lynx::makeEffect (*f3);
  13533. fx->prepare (48000.0);
  13534. std::vector<float> l (inL), r (inR);
  13535. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  13536. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  13537. }
  13538. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  13539. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  13540. }
  13541. }
  13542. // FL's Fruity Phaser (REA_rodada105_phaser): goldens from the installation's own DLL, rendered unmodified in 512-frame
  13543. // calls; LYNX's engine matches it to the last bit on the 27 presets (denormals aside), the tolerance only absorbs
  13544. // compilers (gcc / clang / zig) and libm.
  13545. TEST (fruity_phaser_engine)
  13546. {
  13547. std::vector<float> in (8192);
  13548. for (int n = 0; n < 4096; ++n)
  13549. {
  13550. in[2 * (std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  13551. in[2 * (std::size_t) n + 1] = (float) (((n * 53) % 170 - 85) / 70.0);
  13552. }
  13553. struct Gold
  13554. {
  13555. int i;
  13556. float l, r;
  13557. };
  13558. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-5f * std::max (1.0f, std::abs (b)); };
  13559. auto phase = [&] (lynx::phaserfx::State s, int fs, std::initializer_list<Gold> gold) {
  13560. lynx::phaserfx::Engine e;
  13561. e.setRate (fs);
  13562. e.load (s);
  13563. e.reset();
  13564. std::vector<float> io (in);
  13565. for (int o = 0; o < 4096; o += 512)
  13566. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  13567. for (const auto& g : gold)
  13568. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13569. };
  13570. phase ({ 2500, 100, 800, 0, 512, 7, 400, 512, 4000 }, 44100,
  13571. { { 100, 0.243106663f, -1.63760519f }, { 1000, -2.36990547f, 0.841273248f }, { 2047, 1.21970272f, -1.66379488f },
  13572. { 3000, -2.80603957f, -1.03600585f }, { 4095, 1.80225599f, 0.845855355f } });
  13573. phase ({ 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 }, 48000,
  13574. { { 100, -0.360522032f, -0.0510774851f }, { 1000, -0.788047731f, 0.324495822f }, { 2047, 0.304549545f, 0.0850031748f },
  13575. { 3000, -0.79305625f, 0.114482567f }, { 4095, -0.67624253f, -0.0348635167f } });
  13576. phase ({ 2448, 510, 417, 1024, 1024, 16, 508, 0, 2500 }, 44100,
  13577. { { 100, 1.66273713f, -0.0830700323f }, { 1000, -1.47920549f, -0.0849421024f }, { 2047, -0.157817751f, -1.06916845f },
  13578. { 3000, -0.703828216f, -0.0190049056f }, { 4095, 0.768234849f, -0.965515852f } });
  13579. phase ({ 5000, 0, 1000, 700, 0, 22, 950, 300, 5000 }, 44100,
  13580. { { 100, 0.0809119642f, -0.521223426f }, { 1000, 1.07321298f, 0.681761861f }, { 2047, 0.790922821f, -0.205452889f },
  13581. { 3000, -1.91779351f, 0.599423349f }, { 4095, -1.18747222f, 1.03711367f } });
  13582. // controls: FL's ranges, the stage count clamp, the texts
  13583. CHECK (std::abs (lynx::phaserfx::sweepHz (0.5f, false) - 0.238405839f) < 1.0e-6f);
  13584. CHECK (std::abs (lynx::phaserfx::sweepHz (1.0f, true) - 10.0f) < 1.0e-5f);
  13585. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 11.0f / 22.0f, 120.0) == "12");
  13586. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 5, 0.0f, 120.0) == "3");
  13587. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 7, 0.0f, 120.0) == "100% wet");
  13588. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 8, 0.5f, 120.0) == "+0.0 dB");
  13589. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 3, 0.0f, 120.0) == "small");
  13590. {
  13591. lynx::EffectParams ctx {};
  13592. ctx[3] = 1.0f;
  13593. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPhaser, 0, 1.0f, 120.0, ctx) == "10.000 Hz");
  13594. }
  13595. CHECK (lynx::effectInfo (lynx::EffectKind::fruityPhaser).params.size() == 9);
  13596. // import: FL's state (version 1 + nine controls) becomes the exact effect; its parameters round-trip
  13597. const lynx::phaserfx::State ouch { 417, 995, 1000, 0, 1024, 3, 705, 0, 2943 };
  13598. const auto bytes = lynx::phaserfx::serialize (ouch);
  13599. const auto slot = lynx::effectFromFl ("Fruity Phaser", bytes, 120.0);
  13600. CHECK (slot && slot->kind == lynx::EffectKind::fruityPhaser);
  13601. if (slot)
  13602. {
  13603. CHECK (lynx::phaserfx::fromParams (slot->params.data()) == ouch);
  13604. auto fx = lynx::makeEffect (*slot);
  13605. fx->prepare (48000.0);
  13606. std::vector<float> l (4096), r (4096);
  13607. for (int n = 0; n < 4096; ++n)
  13608. {
  13609. l[(std::size_t) n] = in[2 * (std::size_t) n];
  13610. r[(std::size_t) n] = in[2 * (std::size_t) n + 1];
  13611. }
  13612. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13613. CHECK (close (l[1000], -0.788047731f) && close (r[4095], -0.0348635167f));
  13614. }
  13615. auto v2 = bytes;
  13616. v2[0] = 2; // FL reads only versions below 2
  13617. const auto later = lynx::effectFromFl ("Fruity Phaser", v2, 120.0);
  13618. CHECK (! later || later->kind != lynx::EffectKind::fruityPhaser);
  13619. CHECK (lynx::plugui::hasFlLayout (lynx::EffectKind::fruityPhaser));
  13620. }
  13621. // Workflow P2 (docs/WORKFLOW_FL.md)
  13622. #include "WorkflowP2Tests.inc"
  13623. // Editor mouse grammar (Piano Roll / Playlist)
  13624. #include "EditorGestureTests.inc"
  13625. TEST (chorus_family_engines)
  13626. {
  13627. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  13628. std::vector<float> inL (4096), inR (4096);
  13629. for (int n = 0; n < 4096; ++n)
  13630. {
  13631. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  13632. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  13633. }
  13634. struct Gold { int i; float l, r; };
  13635. auto close = [] (float a, float b) { return std::abs (a - b) <= x87Tolerance() * std::max (1.0f, std::abs (b)); };
  13636. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  13637. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  13638. CHECK (slot && slot->kind == kind);
  13639. if (! slot)
  13640. return;
  13641. auto fx = lynx::makeEffect (*slot);
  13642. fx->prepare (44100.0);
  13643. std::vector<float> l (inL), r (inR);
  13644. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  13645. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  13646. for (const auto& g : gold)
  13647. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  13648. };
  13649. auto ints = [] (std::initializer_list<int> v) {
  13650. std::vector<std::uint8_t> b;
  13651. for (int x : v)
  13652. for (int k = 0; k < 4; ++k)
  13653. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  13654. return b;
  13655. };
  13656. // Fruity Chorus (start phases 0, which FL draws at random)
  13657. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  13658. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  13659. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  13660. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  13661. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  13662. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  13663. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  13664. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  13665. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  13666. {
  13667. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  13668. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  13669. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  13670. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  13671. }
  13672. // a state FL does not read (version 2) or too short keeps the generic chorus
  13673. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  13674. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  13675. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  13676. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  13677. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  13678. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  13679. namespace hc = lynx::hyperchorusfx;
  13680. hc::Engine e (instance);
  13681. const std::vector<int> r (raws);
  13682. 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); };
  13683. for (int i = 0; i < hc::params; ++i)
  13684. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  13685. for (int i = 0; i < hc::params; ++i)
  13686. e.setParam (i, val (i, r[(std::size_t) i]));
  13687. e.setRate (44100.0);
  13688. std::vector<float> io (8192);
  13689. for (int n = 0; n < 4096; ++n)
  13690. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13691. for (int s = 0; s < 4096; s += 512)
  13692. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13693. for (const auto& g : gold)
  13694. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13695. };
  13696. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  13697. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  13698. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  13699. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  13700. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  13701. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  13702. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  13703. {
  13704. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  13705. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  13706. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  13707. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  13708. lynx::Song song;
  13709. song.mixer.resize (3);
  13710. song.mixer[1].effects = { *slot, *slot };
  13711. song.mixer[2].effects = { *slot };
  13712. lynx::numberChorusInstances (song);
  13713. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  13714. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  13715. fx->prepare (48000.0);
  13716. std::vector<float> l (inL), r (inR);
  13717. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13718. bool finite = true, wet = false;
  13719. for (int n = 0; n < 4096; ++n)
  13720. {
  13721. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13722. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  13723. }
  13724. CHECK (finite && wet);
  13725. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  13726. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  13727. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  13728. }
  13729. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  13730. // start from the engine's initial ones)
  13731. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  13732. namespace vc = lynx::vintagechorusfx;
  13733. vc::Engine e;
  13734. e.setRate (44100.0);
  13735. int i = 0;
  13736. for (int raw : raws)
  13737. {
  13738. 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));
  13739. ++i;
  13740. }
  13741. std::vector<float> io (8192);
  13742. for (int n = 0; n < 4096; ++n)
  13743. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  13744. for (int s = 0; s < 4096; s += 512)
  13745. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  13746. for (const auto& g : gold)
  13747. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  13748. };
  13749. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  13750. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  13751. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  13752. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  13753. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  13754. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  13755. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  13756. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  13757. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  13758. {
  13759. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  13760. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  13761. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  13762. auto fx = lynx::makeEffect (*slot);
  13763. fx->prepare (44100.0);
  13764. std::vector<float> l (inL), r (inR);
  13765. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  13766. bool finite = true;
  13767. for (int n = 0; n < 4096; ++n)
  13768. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  13769. CHECK (finite && l[3000] != inL[3000]);
  13770. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  13771. 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()
  13772. || 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);
  13773. }
  13774. }
  13775. #include "UiChromeTests.inc"
  13776. TEST (fruity_delay_engines)
  13777. {
  13778. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  13779. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  13780. const int n = 16384;
  13781. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  13782. std::uint32_t seed = 1;
  13783. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  13784. {
  13785. seed = seed * 1664525u + 1013904223u;
  13786. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13787. seed = seed * 1664525u + 1013904223u;
  13788. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  13789. }
  13790. using Golden = std::vector<std::tuple<int, float, float>>;
  13791. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  13792. for (const auto& [i, gl, gr] : golden)
  13793. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  13794. };
  13795. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  13796. lynx::fldelay::Delay3Engine e (44100.0);
  13797. e.setTempo (bpm);
  13798. for (int i = 0; i < 26; ++i)
  13799. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  13800. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  13801. e.setSampleRate (44100.0);
  13802. std::vector<float> l (inL), r (inR);
  13803. for (int s = 0; s < n; s += 512)
  13804. e.process (l.data() + s, r.data() + s, 512);
  13805. return std::make_pair (l, r);
  13806. };
  13807. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  13808. lynx::fldelay::Delay2State st;
  13809. st.v = raw;
  13810. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  13811. std::vector<float> l (inL), r (inR);
  13812. for (int s = 0; s < n; s += 512)
  13813. e.process (l.data() + s, r.data() + s, 512);
  13814. return std::make_pair (l, r);
  13815. };
  13816. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  13817. 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 };
  13818. auto o = run3 (a, 120.0);
  13819. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  13820. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  13821. o = run3 (a, 100.0);
  13822. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  13823. { 16000, 0.0806550607f, -0.0991330743f } });
  13824. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  13825. 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);
  13826. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  13827. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  13828. // fake stereo, unsynced, high-pass, limit distortion, no dry
  13829. 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);
  13830. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  13831. { 16000, -0.000312746735f, 0.00101647642f } });
  13832. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  13833. 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);
  13834. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  13835. { 11000, 0.0f, 0.0f } });
  13836. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  13837. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  13838. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  13839. { 16000, 0.0182533264f, 0.105384827f } });
  13840. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  13841. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  13842. { 16000, -0.029524127f, 0.0128910914f } });
  13843. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  13844. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  13845. { 13000, 0.145023212f, -0.104788139f } });
  13846. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  13847. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  13848. 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 };
  13849. state3[0] = 1;
  13850. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  13851. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  13852. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  13853. auto state2 = std::vector<std::uint8_t> (32, 0);
  13854. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  13855. std::memcpy (state2.data(), def2.data(), 32);
  13856. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  13857. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  13858. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  13859. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  13860. auto fx = lynx::makeEffect (slot);
  13861. fx->prepare (44100.0);
  13862. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  13863. l[100] = 1.0f;
  13864. for (int s = 0; s < 48000; s += 1024)
  13865. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  13866. int best = 200;
  13867. for (int i = 200; i < 48000; ++i)
  13868. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  13869. best = i;
  13870. return best - 100;
  13871. };
  13872. if (slot3 && slot2)
  13873. {
  13874. CHECK (echoAt (*slot3, 120.0) == 22050);
  13875. CHECK (echoAt (*slot3, 150.0) == 17640);
  13876. CHECK (echoAt (*slot2, 120.0) == 16538);
  13877. CHECK (echoAt (*slot2, 140.0) == 14175);
  13878. }
  13879. }
  13880. #include "UiRackMixerTests.inc"
  13881. // ---------------------------------------------------------------------------
  13882. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  13883. namespace
  13884. {
  13885. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  13886. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  13887. {
  13888. std::vector<std::uint8_t> b;
  13889. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  13890. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  13891. u32 (id);
  13892. u32 ((std::uint32_t) payload.size());
  13893. u32 (0);
  13894. b.insert (b.end(), payload.begin(), payload.end());
  13895. };
  13896. u32 (12);
  13897. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  13898. if (! classId.empty())
  13899. record (0x34, classId);
  13900. if (! clapId.empty())
  13901. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  13902. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  13903. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  13904. return b;
  13905. }
  13906. } // namespace
  13907. TEST (fl_plugin_db_nfo_parsing)
  13908. {
  13909. const std::string nfo = "ps_name=Vital\r\n"
  13910. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  13911. "ps_file_vendorname_0=Vital Audio\r\n"
  13912. "ps_file_type_0=2\r\n"
  13913. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  13914. "ps_file_type_1=2\r\n"
  13915. "ps_file_magic_1=Vita\r\n"
  13916. "ps_file_name_2=not a plugin\r\n"
  13917. "garbage line without equals\r\n";
  13918. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  13919. CHECK (entries.size() == 2);
  13920. if (entries.size() == 2)
  13921. {
  13922. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  13923. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  13924. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  13925. }
  13926. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  13927. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  13928. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  13929. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  13930. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  13931. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  13932. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  13933. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  13934. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  13935. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  13936. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  13937. }
  13938. TEST (fl_plugin_db_fst_parsing_and_matching)
  13939. {
  13940. std::vector<std::uint8_t> cid;
  13941. for (int i = 0; i < 16; ++i)
  13942. cid.push_back ((std::uint8_t) (0x10 + i));
  13943. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  13944. FlpBuilder fst;
  13945. fst.ascii (0xC9, "Fruity Wrapper");
  13946. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  13947. lynx::FlPluginDbEntry e;
  13948. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  13949. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  13950. lynx::FlPluginDbEntry bad;
  13951. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  13952. lynx::FlPluginDb db;
  13953. db.add (e);
  13954. // the .nfo of the same plugin fills in vendor and role
  13955. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  13956. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  13957. db.add (n);
  13958. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  13959. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  13960. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  13961. CHECK (db.entries().size() == 4);
  13962. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  13963. // by VST3 class ID even when the project saved another path and name
  13964. lynx::ForeignPlugin p;
  13965. p.format = lynx::ForeignPlugin::Format::vst3;
  13966. p.hasClassId = true;
  13967. std::copy (cid.begin(), cid.end(), p.classId.begin());
  13968. p.name = "Renamed";
  13969. p.path = "C:\\Other PC\\Surge.vst3";
  13970. CHECK (db.match (p, true) == &db.entries()[0]);
  13971. // by CLAP id, by VST2 id
  13972. lynx::ForeignPlugin c;
  13973. c.format = lynx::ForeignPlugin::Format::clap;
  13974. c.clapId = "org.clapper";
  13975. CHECK (db.match (c, false) == &db.entries()[1]);
  13976. lynx::ForeignPlugin v;
  13977. v.format = lynx::ForeignPlugin::Format::vst2;
  13978. v.vst2Id = 0x42566262u;
  13979. CHECK (db.match (v, false) == &db.entries()[2]);
  13980. // by name (architecture suffix ignored), only in the same role
  13981. lynx::ForeignPlugin byName;
  13982. byName.format = lynx::ForeignPlugin::Format::vst2;
  13983. byName.name = "BigVerb x64";
  13984. CHECK (db.match (byName, false) == &db.entries()[2]);
  13985. CHECK (db.match (byName, true) == nullptr);
  13986. lynx::ForeignPlugin unknown;
  13987. unknown.format = lynx::ForeignPlugin::Format::vst3;
  13988. unknown.name = "Nothing Like It";
  13989. CHECK (db.match (unknown, true) == nullptr);
  13990. // folders: the bundle's parent, deduplicated
  13991. const auto folders = db.pluginFolders();
  13992. CHECK (folders.size() == 4);
  13993. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  13994. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  13995. // targeted scan picks: same file, or same name key
  13996. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  13997. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  13998. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  13999. // the plugins an .flp uses: wrapper channels and mixer slots
  14000. flp::Project project;
  14001. flp::Channel ch;
  14002. ch.pluginName = "Fruity Wrapper";
  14003. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  14004. project.channels.push_back (ch);
  14005. ch.pluginName = "3x Osc";
  14006. project.channels.push_back (ch);
  14007. flp::MixerInsert insert;
  14008. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  14009. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  14010. project.mixer.push_back (insert);
  14011. const auto uses = lynx::flpForeignPlugins (project);
  14012. CHECK (uses.size() == 2);
  14013. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  14014. }
  14015. TEST (fl_sample_relocation)
  14016. {
  14017. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  14018. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  14019. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  14020. lynx::FlPathRoots roots;
  14021. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  14022. roots.factoryData = "C:\\Program Files\\FL 2026";
  14023. roots.home = "C:\\Users\\me";
  14024. const std::string project = "E:\\Songs\\Track";
  14025. // variable resolved, file in place
  14026. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  14027. // another PC's FL user data / FL install / user profile
  14028. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  14029. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  14030. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  14031. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  14032. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  14033. // next to the project, also in a sub-folder named in the original path
  14034. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  14035. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  14036. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  14037. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  14038. }
  14039. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  14040. namespace
  14041. {
  14042. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  14043. // ints, version)
  14044. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  14045. {
  14046. std::vector<std::uint8_t> b (564, 0);
  14047. std::memcpy (b.data(), P.data(), 360);
  14048. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  14049. std::memcpy (b.data() + 560, &version, 4);
  14050. return b;
  14051. }
  14052. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  14053. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  14054. {
  14055. std::vector<std::vector<std::uint8_t>> out;
  14056. std::ifstream in (file, std::ios::binary);
  14057. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  14058. std::size_t i = 0;
  14059. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  14060. ++i;
  14061. i += 8;
  14062. std::string name;
  14063. while (i < b.size())
  14064. {
  14065. const auto id = b[i++];
  14066. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  14067. if (id >= 192)
  14068. for (int shift = 0; i < b.size(); shift += 7)
  14069. {
  14070. const auto c = b[i++];
  14071. n |= (std::size_t) (c & 0x7F) << shift;
  14072. if (c < 0x80)
  14073. break;
  14074. }
  14075. if (i + n > b.size())
  14076. break;
  14077. if (id == 0xC9)
  14078. {
  14079. name.clear();
  14080. for (std::size_t k = 0; k < n; ++k)
  14081. if (b[i + k] != 0)
  14082. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  14083. }
  14084. if (id == 0xD5 && name == plugin)
  14085. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  14086. i += n;
  14087. }
  14088. return out;
  14089. }
  14090. } // namespace
  14091. TEST (simsynth_engine)
  14092. {
  14093. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  14094. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  14095. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  14096. 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,
  14097. 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 };
  14098. 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 };
  14099. 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,
  14100. 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 };
  14101. 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 };
  14102. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  14103. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  14104. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  14105. const auto hp = lynx::simSynthHostParams (blobA);
  14106. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  14107. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  14108. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  14109. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  14110. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  14111. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  14112. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  14113. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  14114. struct Golden { int i; float l, r; };
  14115. 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 } };
  14116. 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 } };
  14117. for (int which = 0; which < 2; ++which)
  14118. {
  14119. std::vector<std::int64_t> counters;
  14120. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  14121. CHECK (y.size() == 2 * 66150u);
  14122. if (y.size() != 2 * 66150u)
  14123. continue;
  14124. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  14125. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  14126. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  14127. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  14128. double e = 0.0;
  14129. for (auto v : y)
  14130. e += (double) v * v;
  14131. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  14132. }
  14133. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  14134. lynx::ChannelDef c;
  14135. c.instrument = lynx::InstrumentKind::simsynth;
  14136. c.pluginState = blobA;
  14137. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  14138. inst->prepare (44100.0);
  14139. inst->configure (c, 120.0);
  14140. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  14141. for (int k = 0; k < 16; ++k)
  14142. {
  14143. if (k == 0)
  14144. inst->noteOn (60, 0.8f);
  14145. if (k == 10)
  14146. inst->noteOff (60);
  14147. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  14148. }
  14149. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  14150. double diff = 0.0, energy = 0.0;
  14151. for (int i = 0; i < 8192; ++i)
  14152. {
  14153. 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]) });
  14154. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14155. }
  14156. CHECK (diff == 0.0 && energy > 1e-3);
  14157. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  14158. if (! realFlpDir.empty())
  14159. {
  14160. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  14161. int presets = 0, usable = 0, sounding = 0;
  14162. if (std::filesystem::exists (folder))
  14163. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14164. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  14165. {
  14166. ++presets;
  14167. if (! lynx::simSynthStateUsable (st))
  14168. continue;
  14169. ++usable;
  14170. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  14171. double e = 0.0;
  14172. bool finite = true;
  14173. for (auto v : y)
  14174. finite = finite && std::isfinite (v), e += (double) v * v;
  14175. CHECK (finite);
  14176. sounding += e > 1e-9 ? 1 : 0;
  14177. }
  14178. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  14179. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  14180. }
  14181. }
  14182. TEST (wasp_is_a_silent_placeholder)
  14183. {
  14184. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  14185. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  14186. inst->prepare (44100.0);
  14187. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14188. inst->noteOn (60, 1.0f);
  14189. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14190. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14191. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  14192. // their state kept
  14193. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  14194. const int version = 14;
  14195. std::memcpy (ss.data() + 560, &version, 4);
  14196. FlpBuilder b;
  14197. b.ascii (0xC7, "25.2.5.5319");
  14198. int index = 0;
  14199. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  14200. {
  14201. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14202. b.u8 (0x15); b.u8 (2);
  14203. b.utf16 (0xC9, name);
  14204. b.data (0xD5, state);
  14205. }
  14206. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  14207. CHECK (song.channels.size() == 3u);
  14208. if (song.channels.size() == 3u)
  14209. {
  14210. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  14211. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  14212. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  14213. }
  14214. }
  14215. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  14216. namespace
  14217. {
  14218. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  14219. lynx::SampleData granulizerTestSample (int n, int ch)
  14220. {
  14221. lynx::SampleData d;
  14222. std::uint32_t s = 12345;
  14223. for (int i = 0; i < n; ++i)
  14224. {
  14225. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  14226. for (int c = 0; c < ch; ++c)
  14227. {
  14228. s = s * 1664525u + 1013904223u;
  14229. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  14230. (c == 0 ? d.left : d.right).push_back (v);
  14231. }
  14232. }
  14233. return d;
  14234. }
  14235. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  14236. {
  14237. std::vector<std::uint8_t> b (60, 0);
  14238. const std::int32_t v = 5;
  14239. std::memcpy (b.data(), &v, 4);
  14240. std::memcpy (b.data() + 4, P.data(), 48);
  14241. std::memcpy (b.data() + 56, &keyMode, 4);
  14242. return b;
  14243. }
  14244. } // namespace
  14245. TEST (granulizer_engine)
  14246. {
  14247. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  14248. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  14249. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  14250. CHECK (lynx::granulizerStateUsable (v5));
  14251. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  14252. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  14253. && ! lynx::granulizerParam (n5, 12));
  14254. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  14255. {
  14256. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  14257. v1[0] = 1;
  14258. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  14259. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  14260. std::vector<std::uint8_t> bad (v5);
  14261. bad[0] = 9;
  14262. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  14263. auto edited = n5;
  14264. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  14265. }
  14266. using N = lynx::granulizer::RefNote;
  14267. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  14268. double el, er; std::tuple<int, float, float> at[6]; };
  14269. auto check = [] (const Golden& g) {
  14270. auto smp = granulizerTestSample (20000, g.ch);
  14271. smp.sampleRate = g.rate;
  14272. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  14273. CHECK (y.size() == 2 * 16384);
  14274. if (y.size() != 2 * 16384)
  14275. return;
  14276. double el = 0.0, er = 0.0;
  14277. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14278. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14279. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14280. for (const auto& [i, l, r] : g.at)
  14281. CHECK (goldenSampleClose (y[2 * (std::size_t) i], l, granulizerTolerance())
  14282. && goldenSampleClose (y[2 * (std::size_t) i + 1], r, granulizerTolerance()));
  14283. };
  14284. lynx::granulizer::RefOptions o1, o2, o3;
  14285. o1.points = o2.points = o3.points = 12; // the plugin's own default quality (these goldens predate FL's render value)
  14286. o2.block = 333, o2.seed = 777;
  14287. o3.block = 64, o3.seed = 5;
  14288. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  14289. 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,
  14290. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  14291. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  14292. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  14293. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  14294. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  14295. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  14296. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  14297. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  14298. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  14299. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  14300. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  14301. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  14302. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  14303. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  14304. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  14305. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  14306. {
  14307. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  14308. silent->prepare (44100.0);
  14309. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  14310. silent->noteOn (60, 1.0f);
  14311. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14312. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14313. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14314. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14315. lynx::ChannelDef def;
  14316. def.instrument = lynx::InstrumentKind::granulizer;
  14317. def.pluginState = n5;
  14318. inst->configure (def, 140.0);
  14319. inst->prepare (44100.0);
  14320. inst->noteOn (60, 0.8f);
  14321. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  14322. double e = 0.0;
  14323. for (auto v : l)
  14324. e += (double) v * v;
  14325. CHECK (! inst->idle() && e > 1.0);
  14326. inst->noteOff (60);
  14327. for (int k = 0; k < 4; ++k)
  14328. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14329. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  14330. }
  14331. // tempo-synced grains follow the song position in LYNX's 960-PPQ ticks: an instance fed setSongPosition every
  14332. // block while playing must stay in phase with one that free-runs its own tick clock from 0 (it was 10x off)
  14333. {
  14334. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  14335. lynx::ChannelDef def;
  14336. def.instrument = lynx::InstrumentKind::granulizer;
  14337. def.pluginState = n5;
  14338. auto fed = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14339. auto free = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  14340. for (auto* g : { fed.get(), free.get() })
  14341. {
  14342. g->configure (def, 140.0);
  14343. g->prepare (44100.0);
  14344. g->noteOn (60, 0.8f);
  14345. }
  14346. const auto tps = 140.0 * lynx::ppq / (60.0 * 44100.0);
  14347. std::vector<float> l1 (300), r1 (300), l2 (300), r2 (300);
  14348. double diff = 0.0, energy = 0.0;
  14349. for (int block = 0, done = 0; block < 60; ++block, done += 300)
  14350. {
  14351. fed->setSongPosition ((double) done * tps);
  14352. std::fill (l1.begin(), l1.end(), 0.0f), std::fill (r1.begin(), r1.end(), 0.0f);
  14353. std::fill (l2.begin(), l2.end(), 0.0f), std::fill (r2.begin(), r2.end(), 0.0f);
  14354. fed->render (l1.data(), r1.data(), 300, 1.0f, 1.0f);
  14355. free->render (l2.data(), r2.data(), 300, 1.0f, 1.0f);
  14356. for (int j = 0; j < 300; ++j)
  14357. diff = std::max (diff, (double) std::fabs (l1[(std::size_t) j] - l2[(std::size_t) j])),
  14358. energy += (double) l2[(std::size_t) j] * l2[(std::size_t) j];
  14359. }
  14360. CHECK (energy > 1.0 && diff < 1e-4);
  14361. }
  14362. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  14363. {
  14364. FlpBuilder b;
  14365. b.ascii (0xC7, "25.2.5.5319");
  14366. b.u8 (0x40); b.u16 (0);
  14367. b.u8 (0x15); b.u8 (2);
  14368. b.utf16 (0xC9, u"Fruity Granulizer");
  14369. b.data (0xD5, v5);
  14370. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14371. lynx::FlpImportOptions options;
  14372. options.roots.factoryData = "C:\\FL";
  14373. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  14374. lynx::FlpImportReport report;
  14375. options.report = &report;
  14376. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14377. CHECK (song.channels.size() == 1u);
  14378. if (song.channels.size() == 1u)
  14379. {
  14380. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  14381. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  14382. }
  14383. CHECK (report.converted["Fruity Granulizer"] == 1);
  14384. }
  14385. }
  14386. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  14387. namespace
  14388. {
  14389. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  14390. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  14391. {
  14392. std::uint32_t s = 20261015u;
  14393. auto u = [&s] {
  14394. s = s * 1664525u + 1013904223u;
  14395. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  14396. };
  14397. // (a product rounded on its own: no fused multiply-add)
  14398. auto mul = [] (float a, float b) {
  14399. volatile float p = a * b;
  14400. return (float) p;
  14401. };
  14402. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  14403. std::array<float, 720> P {};
  14404. for (int k = 0; k < 16; ++k)
  14405. for (int i = 0; i < 44; ++i)
  14406. {
  14407. auto v = u();
  14408. if (i == 0)
  14409. v = 0.5f + mul (v, 0.5f);
  14410. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  14411. v = 0.2f + mul (v, 0.6f);
  14412. else if (i == 2 || i == 3)
  14413. v = 0.0f;
  14414. else if (i == 4)
  14415. v = (float) (36 + k);
  14416. else if (i == 5)
  14417. v = v < 0.5f ? 0.0f : 1.0f;
  14418. else if (i == 6)
  14419. v = (float) (int) (v * 4.0f);
  14420. else if (i >= 35 && i <= 38)
  14421. v = (float) targets[(int) (v * 7.0f)];
  14422. P[(std::size_t) (44 * k + i)] = v;
  14423. }
  14424. for (int i = 0; i < 16; ++i)
  14425. {
  14426. auto v = u();
  14427. if (i == 1)
  14428. v = 0.3f + mul (v, 0.4f);
  14429. else if (i == 3)
  14430. v = eq ? 1.0f : 0.0f;
  14431. else if (i >= 4 && i <= 11)
  14432. v = 0.35f + mul (v, 0.3f);
  14433. else if (i == 12)
  14434. v = 1.0f;
  14435. else if (i == 13)
  14436. v = v * 0.1f;
  14437. else if (i == 0 || i == 15)
  14438. v = 0.0f;
  14439. P[(std::size_t) (704 + i)] = v;
  14440. }
  14441. std::vector<std::uint8_t> b;
  14442. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  14443. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  14444. put32 (21636u);
  14445. put32 (0x444d5333u); // engine stream version 3
  14446. const std::string name = "LYNX test kit";
  14447. b.insert (b.end(), name.begin(), name.end());
  14448. b.resize (b.size() + 256 - name.size(), 0);
  14449. const auto at = b.size();
  14450. b.resize (at + 720 * 4);
  14451. std::memcpy (b.data() + at, P.data(), 720 * 4);
  14452. for (int k = 0; k < 16; ++k)
  14453. {
  14454. b.resize (b.size() + 0x400, 0);
  14455. b.push_back (100), b.push_back (100); // pattern length, trigger key
  14456. }
  14457. b.resize (b.size() + 0x800, 0);
  14458. for (int k = 0; k < 16; ++k)
  14459. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  14460. b.resize (b.size() + 16, 0); // every pad on the master
  14461. return b;
  14462. }
  14463. } // namespace
  14464. TEST (drumaxx_engine)
  14465. {
  14466. const auto st = drumaxxTestState (true);
  14467. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  14468. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  14469. {
  14470. auto edited = st;
  14471. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  14472. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  14473. auto bad = st;
  14474. bad[8] = 'X';
  14475. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  14476. }
  14477. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  14478. using N = lynx::drumaxx::RefNote;
  14479. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  14480. auto check = [] (const Golden& g) {
  14481. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  14482. CHECK (y.size() == 2 * 20000);
  14483. if (y.size() != 2 * 20000)
  14484. return;
  14485. double el = 0.0, er = 0.0;
  14486. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14487. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14488. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  14489. for (const auto& [i, l, r] : g.at)
  14490. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  14491. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  14492. };
  14493. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  14494. check ({ true, 44100, 512,
  14495. { { 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 },
  14496. { 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 },
  14497. { 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 } },
  14498. 2698.372377185949, 3678.285731410973,
  14499. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  14500. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  14501. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  14502. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  14503. check ({ false, 48000, 333,
  14504. { { 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 },
  14505. { 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 } },
  14506. 2241.008810618806, 1814.5219804412964,
  14507. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  14508. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  14509. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  14510. // 96 kHz in 64-sample blocks, an octave down
  14511. check ({ true, 96000, 64,
  14512. { { 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 },
  14513. { 10000, 51, 1.0, 1.0, -1200.0 } },
  14514. 3522.8420617665834, 5746.555795626526,
  14515. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  14516. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  14517. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  14518. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  14519. {
  14520. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  14521. lynx::ChannelDef def;
  14522. def.instrument = lynx::InstrumentKind::drumaxx;
  14523. def.pluginState = st;
  14524. inst->configure (def, 140.0);
  14525. inst->prepare (44100.0);
  14526. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  14527. inst->noteOn (90, 1.0f);
  14528. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14529. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14530. inst->noteOn (36, 0.8f);
  14531. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14532. double e = 0.0;
  14533. for (auto v : l)
  14534. e += (double) v * v;
  14535. CHECK (! inst->idle() && e > 1e-3);
  14536. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  14537. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  14538. CHECK (inst->idle());
  14539. }
  14540. // import: a Drumaxx channel plays on the exact engine with its raw state
  14541. {
  14542. FlpBuilder b;
  14543. b.ascii (0xC7, "25.2.5.5319");
  14544. b.u8 (0x40); b.u16 (0);
  14545. b.u8 (0x15); b.u8 (2);
  14546. b.utf16 (0xC9, u"Drumaxx");
  14547. b.data (0xD5, st);
  14548. lynx::FlpImportOptions options;
  14549. lynx::FlpImportReport report;
  14550. options.report = &report;
  14551. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  14552. CHECK (song.channels.size() == 1u);
  14553. if (song.channels.size() == 1u)
  14554. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  14555. CHECK (report.converted["Drumaxx"] == 1);
  14556. }
  14557. }
  14558. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  14559. namespace
  14560. {
  14561. std::string slayerDll()
  14562. {
  14563. if (realFlpDir.empty())
  14564. return {};
  14565. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  14566. }
  14567. std::vector<std::uint8_t> slayerPreset (const char* name)
  14568. {
  14569. if (realFlpDir.empty())
  14570. return {};
  14571. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  14572. const auto states = pluginStatesIn (file, "FL Slayer");
  14573. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  14574. }
  14575. } // namespace
  14576. TEST (slayer_state)
  14577. {
  14578. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  14579. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  14580. const std::int32_t one = 1, wheel = 77, mode = 5;
  14581. std::memcpy (v1.data(), &one, 4);
  14582. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  14583. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  14584. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  14585. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  14586. auto v2 = v1;
  14587. v2[0] = 2;
  14588. CHECK (! lynx::slayerStateUsable (v2));
  14589. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  14590. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  14591. // the controls' maxima (the front-end sends value / max)
  14592. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  14593. && lynx::slayerParamMax (3) == 65535);
  14594. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  14595. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  14596. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  14597. }
  14598. TEST (slayer_engine)
  14599. {
  14600. const auto dll = slayerDll();
  14601. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14602. return; // no FL installation
  14603. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  14604. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  14605. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  14606. struct Sample { int at; float l, r; };
  14607. struct Golden { const char* preset; double el, er; Sample s[4]; };
  14608. const Golden goldens[] {
  14609. { "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 } } },
  14610. { "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 } } },
  14611. { "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 } } },
  14612. { "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 } } },
  14613. { "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 } } },
  14614. { "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 } } },
  14615. { "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 } } },
  14616. { "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 } } },
  14617. { "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 } } },
  14618. { "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 } } },
  14619. { "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 } } },
  14620. { "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 } } },
  14621. };
  14622. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  14623. constexpr int length = 44100;
  14624. int checked = 0;
  14625. for (const auto& g : goldens)
  14626. {
  14627. const auto st = slayerPreset (g.preset);
  14628. CHECK (lynx::slayerStateUsable (st));
  14629. if (! lynx::slayerStateUsable (st))
  14630. continue;
  14631. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  14632. double el = 0.0, er = 0.0;
  14633. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  14634. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  14635. 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;
  14636. for (const auto& smp : g.s)
  14637. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  14638. if (! ok)
  14639. 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);
  14640. CHECK (ok);
  14641. ++checked;
  14642. }
  14643. CHECK (checked == 12);
  14644. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  14645. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  14646. // velocity 0.8 is the reference note one sample later
  14647. const auto st = slayerPreset ("BASS Ebass 2");
  14648. lynx::ChannelDef c;
  14649. c.instrument = lynx::InstrumentKind::slayer;
  14650. c.pluginState = st;
  14651. c.samplePath = dll;
  14652. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14653. inst->configure (c, 120.0);
  14654. inst->prepare (44100.0);
  14655. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  14656. for (int b = 0; b < 32; ++b)
  14657. {
  14658. if (b == 0)
  14659. inst->noteOn (60, 0.8f);
  14660. if (b == 20)
  14661. inst->noteOff (60);
  14662. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  14663. }
  14664. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14665. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  14666. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  14667. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  14668. float hl = 0.0f, hr = 0.0f;
  14669. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  14670. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  14671. double diff = 0.0, energy = 0.0;
  14672. for (int i = 0; i < 16384; ++i)
  14673. {
  14674. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14675. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14676. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14677. }
  14678. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  14679. // every preset of the installation: usable and sounding
  14680. int presets = 0, sounding = 0;
  14681. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  14682. if (std::filesystem::exists (folder))
  14683. for (const auto& entry : std::filesystem::directory_iterator (folder))
  14684. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  14685. {
  14686. ++presets;
  14687. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  14688. double e = 0.0;
  14689. bool finite = true;
  14690. for (auto v : y)
  14691. finite = finite && std::isfinite (v), e += (double) v * v;
  14692. CHECK (finite);
  14693. sounding += e > 1.0 ? 1 : 0;
  14694. }
  14695. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  14696. CHECK (presets == 64 && sounding == 64);
  14697. }
  14698. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  14699. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  14700. TEST (slayer_host_levels_and_automation)
  14701. {
  14702. using namespace lynx::slayer;
  14703. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  14704. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  14705. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  14706. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  14707. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  14708. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  14709. hostGains (64, 1.0f, l1, r1);
  14710. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  14711. const auto dll = slayerDll();
  14712. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14713. return;
  14714. const auto st = slayerPreset ("DIST Dire Straits");
  14715. const auto params = lynx::slayerStateParams (st);
  14716. lynx::ChannelDef c;
  14717. c.instrument = lynx::InstrumentKind::slayer;
  14718. c.pluginState = st;
  14719. c.samplePath = dll;
  14720. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  14721. c.modelParams = { 0.0f };
  14722. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  14723. inst->configure (c, 120.0);
  14724. inst->prepare (44100.0);
  14725. const int blocks = 40, n = 512;
  14726. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  14727. auto edited = st;
  14728. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  14729. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  14730. for (int b = 0; b < blocks; ++b)
  14731. {
  14732. if (b == 0)
  14733. inst->noteOn (52, 0.8f);
  14734. if (b == 10)
  14735. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  14736. if (b == 18)
  14737. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  14738. if (b == 25)
  14739. {
  14740. c.pluginState = edited;
  14741. inst->configure (c, 120.0);
  14742. }
  14743. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  14744. }
  14745. lynx::slayer::RefOptions opt;
  14746. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  14747. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  14748. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  14749. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  14750. float hl = 0.0f, hr = 0.0f;
  14751. hostGains (0, 1.0f, hl, hr);
  14752. double diff = 0.0, energy = 0.0;
  14753. for (int i = 0; i < blocks * n; ++i)
  14754. {
  14755. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  14756. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  14757. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  14758. }
  14759. CHECK (diff == 0.0 && energy > 1.0);
  14760. // the changes really change the sound: the same render without them differs
  14761. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  14762. CHECK (plain != ref);
  14763. }
  14764. TEST (slayer_and_fruity_dance_import)
  14765. {
  14766. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  14767. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  14768. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  14769. FlpBuilder b;
  14770. b.ascii (0xC7, "25.2.5.5319");
  14771. int index = 0;
  14772. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  14773. {
  14774. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  14775. b.u8 (0x15); b.u8 (2);
  14776. b.utf16 (0xC9, name);
  14777. b.data (0xD5, state);
  14778. }
  14779. const auto project = flp::parseProject (b.bytes);
  14780. const auto plain = lynx::importFlp (project);
  14781. CHECK (plain.channels.size() == 2u);
  14782. if (plain.channels.size() == 2u)
  14783. {
  14784. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  14785. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  14786. }
  14787. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  14788. inst->prepare (44100.0);
  14789. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  14790. inst->noteOn (60, 1.0f);
  14791. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  14792. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  14793. const auto dll = slayerDll();
  14794. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  14795. return;
  14796. lynx::FlpImportOptions withFiles;
  14797. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  14798. withFiles.fileExists = [] (const std::string& path) {
  14799. auto local = path;
  14800. std::replace (local.begin(), local.end(), '\\', '/');
  14801. return std::filesystem::exists (local);
  14802. };
  14803. const auto song = lynx::importFlp (project, withFiles);
  14804. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  14805. && ! song.channels[0].samplePath.empty());
  14806. }
  14807. #include "WaveAnalysisTests.inc"
  14808. #include "UiPolish2Tests.inc"
  14809. #include "UiToolbarTests.inc"
  14810. #include "LuxeVerbTests.inc"
  14811. #include "Parity172bTests.inc"
  14812. #include "ConvolverTests.inc"
  14813. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  14814. namespace newtonetest
  14815. {
  14816. 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))); }
  14817. 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))); }
  14818. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  14819. {
  14820. put32 (b, id);
  14821. put64 (b, (std::int64_t) d.size());
  14822. b.insert (b.end(), d.begin(), d.end());
  14823. }
  14824. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  14825. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  14826. {
  14827. std::vector<std::uint8_t> b, r1, r2;
  14828. put32 (b, version);
  14829. put32 (r1, 727);
  14830. put32 (r1, 582);
  14831. put32 (r1, (std::int32_t) flags);
  14832. r1.resize (0x28, 0);
  14833. record (b, 1, r1);
  14834. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  14835. put32 (chunk, (std::int32_t) xml.size() + 1);
  14836. chunk.insert (chunk.end(), xml.begin(), xml.end());
  14837. chunk.push_back (0);
  14838. put32 (r2, -9);
  14839. r2.push_back (0x0c);
  14840. put32 (r2, -2);
  14841. put64 (r2, (std::int64_t) chunk.size());
  14842. put32 (r2, 0);
  14843. r2.insert (r2.end(), chunk.begin(), chunk.end());
  14844. record (b, 2, r2);
  14845. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  14846. record (b, 4, std::vector<std::uint8_t> (12, 0));
  14847. record (b, 5, std::vector<std::uint8_t> (16, 0));
  14848. record (b, 6, std::vector<std::uint8_t> (32, 0));
  14849. return b;
  14850. }
  14851. const char* xmlWithTrack (bool edited)
  14852. {
  14853. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  14854. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  14855. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  14856. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  14857. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  14858. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  14859. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  14860. }
  14861. } // namespace newtonetest
  14862. TEST (newtone_state)
  14863. {
  14864. using namespace newtonetest;
  14865. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  14866. CHECK (a.has_value());
  14867. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  14868. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  14869. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  14870. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  14871. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  14872. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  14873. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  14874. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  14875. // an empty project, as the installation's presets
  14876. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  14877. CHECK (c.has_value() && c->tracks.empty());
  14878. // broken: other version, truncated record, no options record
  14879. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  14880. auto cut = state (0, 1, xmlWithTrack (true), "");
  14881. cut.resize (cut.size() - 3);
  14882. CHECK (! lynx::newtonefx::parse (cut));
  14883. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  14884. // the FL import keeps the state and takes the slave switch as the editable parameter
  14885. const auto raw = state (0, 5, xmlWithTrack (false), "");
  14886. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  14887. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  14888. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  14889. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  14890. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  14891. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  14892. if (! realFlpDir.empty())
  14893. {
  14894. int found = 0, read = 0;
  14895. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  14896. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  14897. {
  14898. const auto path = std::filesystem::path (realFlpDir) / rel;
  14899. if (! std::filesystem::exists (path))
  14900. continue;
  14901. ++found;
  14902. std::ifstream f (path, std::ios::binary);
  14903. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  14904. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  14905. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  14906. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  14907. }
  14908. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  14909. CHECK (read == found);
  14910. }
  14911. }
  14912. TEST (newtone_engine)
  14913. {
  14914. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  14915. // sample is added from the song time on (at the source's own rate)
  14916. auto sample = std::make_shared<lynx::SampleData>();
  14917. sample->sampleRate = 44100.0;
  14918. for (int i = 0; i < 3000; ++i)
  14919. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  14920. const double sr = 44100.0, bpm = 120.0;
  14921. const auto tps = lynx::ticksPerSample (bpm, sr);
  14922. auto fx = lynx::newtonefx::make (sample);
  14923. fx->prepare (sr);
  14924. lynx::EffectParams on {}, off {};
  14925. on[0] = 1.0f;
  14926. std::vector<float> l (256), r (256), in (256);
  14927. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  14928. for (int i = 0; i < 256; ++i)
  14929. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  14930. fx->setSongPosition (tick);
  14931. fx->setPlaying (playing);
  14932. fx->process (l.data(), r.data(), 256, p, bpm);
  14933. };
  14934. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  14935. block (0.0, true, off);
  14936. CHECK (same()); // slave off
  14937. block (0.0, false, on);
  14938. CHECK (same()); // not playing
  14939. // playing from the start: in + source, sample by sample, over consecutive blocks
  14940. bool exact = true;
  14941. for (int b = 0; b < 4; ++b)
  14942. {
  14943. block (b * 256 * tps, true, on);
  14944. for (int i = 0; i < 256; ++i)
  14945. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  14946. && r[(std::size_t) i] == l[(std::size_t) i];
  14947. }
  14948. CHECK (exact);
  14949. // a jump re-seeks to the song time; past the end of the sample only the input remains
  14950. block (2000 * tps, true, on);
  14951. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  14952. block (2900 * tps, true, on);
  14953. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  14954. // stopping and playing again: from the new song time
  14955. block (0.0, false, on);
  14956. CHECK (same());
  14957. block (10 * tps, true, on);
  14958. CHECK (l[0] == in[0] + sample->left[10]);
  14959. // without its sample: a pass-through
  14960. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  14961. none->prepare (sr);
  14962. for (int i = 0; i < 256; ++i)
  14963. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  14964. none->setPlaying (true);
  14965. none->process (l.data(), r.data(), 256, on, bpm);
  14966. CHECK (same());
  14967. }
  14968. TEST (newtone_rack_follows_transport)
  14969. {
  14970. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  14971. auto song = lynx::Song::makeDefault();
  14972. for (auto& p : song.patterns)
  14973. p.notes.clear();
  14974. song.bpm = 120.0;
  14975. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  14976. slot.params[0] = 1.0f;
  14977. slot.samplePath = "newtone-test.wav";
  14978. song.mixer[1].effects.push_back (slot);
  14979. auto sample = std::make_shared<lynx::SampleData>();
  14980. sample->sampleRate = 48000.0;
  14981. sample->left.assign (48000, 0.5f);
  14982. int loads = 0;
  14983. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  14984. ++loads;
  14985. return path == "newtone-test.wav" ? sample : nullptr;
  14986. };
  14987. const double sr = 48000.0;
  14988. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  14989. CHECK (loads == 1);
  14990. lynx::RackRenderer rr;
  14991. rr.prepare (sr);
  14992. std::vector<float> l (512), r (512);
  14993. auto peakOf = [&] (bool playing, double tick) {
  14994. float peak = 0.0f;
  14995. for (int b = 0; b < 8; ++b)
  14996. {
  14997. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  14998. for (auto v : l)
  14999. peak = std::max (peak, std::abs (v));
  15000. }
  15001. return peak;
  15002. };
  15003. CHECK (peakOf (false, 0.0) == 0.0f);
  15004. CHECK (peakOf (true, 0.0) > 0.1f);
  15005. }
  15006. #include "HardcoreTests.inc"
  15007. #include "DistructorTests.inc"
  15008. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  15009. TEST (missing_files_names_and_scores)
  15010. {
  15011. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  15012. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  15013. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  15014. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  15015. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  15016. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  15017. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  15018. }
  15019. TEST (missing_files_find_in_song)
  15020. {
  15021. lynx::Song song;
  15022. lynx::ChannelDef a, b, c, d, e;
  15023. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  15024. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  15025. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  15026. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  15027. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  15028. lynx::SampleZone z;
  15029. z.path = "C:\\gone\\piano_c4.wav";
  15030. a.zones.push_back (z);
  15031. song.channels = { a, b, c, d, e };
  15032. song.mixer.resize (3);
  15033. lynx::EffectSlot fx;
  15034. fx.samplePath = "C:\\gone\\tune.wav";
  15035. lynx::EffectSlot parallel;
  15036. parallel.branches.push_back ({ { fx }, 1.0f });
  15037. song.mixer[2].effects.push_back (parallel);
  15038. song.mixer[2].name = "Lead";
  15039. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  15040. auto missing = lynx::findMissingSamples (song, exists);
  15041. CHECK (missing.size() == 4);
  15042. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  15043. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  15044. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  15045. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  15046. // candidates from a search: the deeper path match wins; one stays missing
  15047. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  15048. CHECK (lynx::relocateByName (missing, hits) == 3);
  15049. CHECK (missing[0].found == "/lib/gone/kick.wav");
  15050. CHECK (missing[1].found.empty());
  15051. missing[3].skipped = true; // skipped: not applied
  15052. const auto map = lynx::relocationMap (missing);
  15053. CHECK (map.size() == 2);
  15054. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  15055. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  15056. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  15057. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  15058. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  15059. lynx::Song copy = song;
  15060. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  15061. }
  15062. TEST (missing_files_inside_patcher)
  15063. {
  15064. // a Patcher's instruments and effects (any depth) are the song's samples too: listed, packed and relinked
  15065. lynx::Song song;
  15066. lynx::ChannelDef inner;
  15067. inner.name = "Pad", inner.instrument = lynx::InstrumentKind::sampler, inner.samplePath = "C:\\gone\\pad.wav";
  15068. lynx::ChannelDef nested;
  15069. nested.instrument = lynx::InstrumentKind::patcher;
  15070. nested.patcher.nodes.resize (1);
  15071. nested.patcher.nodes[0].generator = true;
  15072. nested.patcher.nodes[0].channel = inner;
  15073. lynx::ChannelDef patch;
  15074. patch.name = "Rack", patch.instrument = lynx::InstrumentKind::patcher;
  15075. patch.patcher.nodes.resize (2);
  15076. patch.patcher.nodes[0].generator = true;
  15077. patch.patcher.nodes[0].channel = nested;
  15078. patch.patcher.nodes[1].effect.samplePath = "C:\\gone\\tune.wav";
  15079. patch.patcher.nodes[1].busHeads.push_back ({});
  15080. patch.patcher.nodes[1].busHeads[0].head = lynx::EffectSlot();
  15081. patch.patcher.nodes[1].busHeads[0].head->samplePath = "C:\\gone\\tune.wav"; // same file: one entry
  15082. song.channels = { patch };
  15083. song.mixer.resize (2);
  15084. lynx::EffectSlot fxPatcher;
  15085. fxPatcher.patcher.nodes.resize (1);
  15086. fxPatcher.patcher.nodes[0].generator = true;
  15087. fxPatcher.patcher.nodes[0].channel = inner;
  15088. fxPatcher.patcher.nodes[0].channel.samplePath = "C:\\gone\\hit.wav";
  15089. song.mixer[1].effects.push_back (fxPatcher);
  15090. const auto missing = lynx::findMissingSamples (song, [] (const std::string&) { return false; });
  15091. CHECK (missing.size() == 3);
  15092. CHECK (missing.size() == 3 && missing[0].path == "C:\\gone\\pad.wav" && missing[0].usedBy[0] == "Rack (Patcher)");
  15093. CHECK (missing.size() == 3 && missing[1].path == "C:\\gone\\tune.wav" && missing[1].usedBy[0] == "Rack (Patcher): effect");
  15094. CHECK (missing.size() == 3 && missing[2].path == "C:\\gone\\hit.wav" && missing[2].usedBy[0] == "Insert 1 (Patcher)");
  15095. CHECK (lynx::applyRelocations (song, { { "C:\\gone\\pad.wav", "/p.wav" }, { "C:\\gone\\tune.wav", "/t.wav" }, { "C:\\gone\\hit.wav", "/h.wav" } }) == 4);
  15096. CHECK (song.channels[0].patcher.nodes[1].busHeads[0].head->samplePath == "/t.wav");
  15097. CHECK (song.channels[0].patcher.nodes[0].channel.patcher.nodes[0].channel.samplePath == "/p.wav");
  15098. CHECK (song.channels[0].patcher.nodes[1].effect.samplePath == "/t.wav");
  15099. CHECK (song.mixer[1].effects[0].patcher.nodes[0].channel.samplePath == "/h.wav");
  15100. }
  15101. TEST (missing_files_folder_search)
  15102. {
  15103. namespace fs = std::filesystem;
  15104. const auto root = fs::temp_directory_path() / ("lynx-missing-test-" + processTempTag());
  15105. fs::remove_all (root);
  15106. fs::create_directories (root / "a" / "deep" / "Kicks");
  15107. fs::create_directories (root / "b");
  15108. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  15109. std::ofstream (f) << "x";
  15110. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  15111. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  15112. int listed = 0;
  15113. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  15114. for (const auto& entry : fs::directory_iterator (folder))
  15115. {
  15116. ++listed;
  15117. if (! onEntry (entry.path().string(), entry.is_directory()))
  15118. return;
  15119. }
  15120. };
  15121. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  15122. CHECK (hits.size() == 3);
  15123. CHECK (lynx::relocateByName (missing, hits) == 2);
  15124. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  15125. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  15126. CHECK (missing[2].found.empty());
  15127. // a bounded walk stops early; a cancelled one finds nothing
  15128. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  15129. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  15130. // nothing left to find: no walk at all
  15131. missing[2].skipped = true;
  15132. listed = 0;
  15133. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  15134. fs::remove_all (root);
  15135. }
  15136. TEST (missing_files_folder_list)
  15137. {
  15138. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  15139. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  15140. auto list = folders;
  15141. lynx::rememberFolder (list, "D:\\x");
  15142. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  15143. lynx::rememberFolder (list, "E:\\new", 2);
  15144. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  15145. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  15146. }
  15147. TEST (missing_files_flp_report_and_relocation)
  15148. {
  15149. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  15150. flp::Project project;
  15151. flp::Channel ch;
  15152. ch.index = 0;
  15153. ch.type = flp::ChannelType::sampler;
  15154. ch.name = "Snare";
  15155. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  15156. project.channels.push_back (ch);
  15157. lynx::FlpImportOptions options;
  15158. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  15159. lynx::FlpImportReport report;
  15160. options.report = &report;
  15161. auto song = lynx::importFlp (project, options);
  15162. CHECK (report.missingFiles.size() == 1);
  15163. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  15164. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  15165. lynx::FlpImportReport again;
  15166. options.report = &again;
  15167. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  15168. song = lynx::importFlp (project, options);
  15169. CHECK (again.missingFiles.empty());
  15170. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  15171. }
  15172. TEST (project_info_history_and_flp_import)
  15173. {
  15174. lynx::Song a;
  15175. auto b = a;
  15176. b.info.title = "Night Drive";
  15177. CHECK (lynx::describeChangeText (a, b) == "Project info");
  15178. auto c = a;
  15179. c.beatsPerBar = 3;
  15180. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  15181. flp::Project project;
  15182. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  15183. const auto song = lynx::importFlp (project);
  15184. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  15185. CHECK (song.info.comments == "line 1\nline 2");
  15186. }
  15187. #include "UiFixTests.inc"
  15188. // ---------------------------------------------------------------------------
  15189. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  15190. namespace
  15191. {
  15192. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  15193. {
  15194. lynx::Note n;
  15195. n.start = start;
  15196. n.length = length;
  15197. n.key = key;
  15198. n.velocity = velocity;
  15199. return n;
  15200. }
  15201. std::vector<int> allOf (const lynx::Pattern& p)
  15202. {
  15203. std::vector<int> v;
  15204. for (int i = 0; i < (int) p.notes.size(); ++i)
  15205. v.push_back (i);
  15206. return v;
  15207. }
  15208. } // namespace
  15209. TEST (prtools_score_file)
  15210. {
  15211. using namespace lynx::prtools;
  15212. FlpBuilder b (96);
  15213. b.ascii (0xC7, "20.9.0");
  15214. b.u8 (0x41); b.u16 (0);
  15215. auto notes = noteRecord (0, 0, 24, 60, 100);
  15216. auto n2 = noteRecord (48, 0, 24, 67, 64);
  15217. n2[19] = 4; // colour 4
  15218. notes.insert (notes.end(), n2.begin(), n2.end());
  15219. b.data (0xE0, notes);
  15220. const auto score = parseScore (b.bytes);
  15221. CHECK (score && score->notes.size() == 2);
  15222. if (score && score->notes.size() == 2)
  15223. {
  15224. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  15225. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  15226. CHECK (score->length() == 720);
  15227. }
  15228. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  15229. const auto grid = gridScore (240);
  15230. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  15231. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  15232. if (! realFlpDir.empty()) // the installation's templates
  15233. {
  15234. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  15235. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  15236. const auto fsc = parseScore (bytes);
  15237. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  15238. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  15239. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  15240. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  15241. const auto chop = parseScore (chopBytes);
  15242. CHECK (chop && ! chop->notes.empty());
  15243. }
  15244. }
  15245. TEST (prtools_quantize)
  15246. {
  15247. using namespace lynx;
  15248. using namespace lynx::prtools;
  15249. const auto grid = gridScore (240);
  15250. Pattern p;
  15251. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15252. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  15253. quantize (p, allOf (p), grid, o);
  15254. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  15255. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  15256. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  15257. o.start = 1.0f;
  15258. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  15259. quantize (p, allOf (p), grid, o);
  15260. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  15261. p.notes = { prNote (1010, 500) };
  15262. o.sensitivity = 36;
  15263. o.durationMode = QuantizeOptions::leaveEnd;
  15264. quantize (p, allOf (p), grid, o);
  15265. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  15266. p.notes = { prNote (1010, 500) };
  15267. o.durationMode = QuantizeOptions::quantizeEnd;
  15268. o.duration = 1.0f;
  15269. quantize (p, allOf (p), grid, o);
  15270. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  15271. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  15272. Score half = grid;
  15273. for (auto& n : half.notes)
  15274. n.velocity = 50.0f / 128.0f;
  15275. p.notes = { prNote (960, 100, 60, 0.8f) };
  15276. o = {};
  15277. quantize (p, allOf (p), half, o);
  15278. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  15279. for (auto& n : half.notes)
  15280. n.velocity = 0.0f;
  15281. quantize (p, allOf (p), half, o);
  15282. CHECK (p.notes.empty());
  15283. }
  15284. TEST (prtools_chop)
  15285. {
  15286. using namespace lynx;
  15287. using namespace lynx::prtools;
  15288. Score pat;
  15289. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  15290. Pattern p;
  15291. p.notes = { prNote (0, 1920, 50) };
  15292. ChopOptions o;
  15293. chop (p, allOf (p), pat, o);
  15294. CHECK (p.notes.size() == 5);
  15295. if (p.notes.size() == 5)
  15296. {
  15297. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  15298. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  15299. }
  15300. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  15301. p.notes = { prNote (100, 600, 50) };
  15302. chop (p, allOf (p), pat, o);
  15303. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  15304. o.absolute = false;
  15305. p.notes = { prNote (100, 600, 50) };
  15306. chop (p, allOf (p), pat, o);
  15307. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  15308. && p.notes[1].length == 120);
  15309. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  15310. o = {};
  15311. o.timeMul = 5;
  15312. p.notes = { prNote (0, 1920, 60) };
  15313. chop (p, allOf (p), pat, o);
  15314. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  15315. pat.notes[1].colour = 4;
  15316. o.timeMul = 4;
  15317. p.notes = { prNote (0, 1920, 60) };
  15318. chop (p, allOf (p), pat, o);
  15319. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  15320. }
  15321. TEST (prtools_flam)
  15322. {
  15323. using namespace lynx;
  15324. using namespace lynx::prtools;
  15325. Pattern p;
  15326. p.notes = { prNote (960, 480, 60, 0.75f) };
  15327. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  15328. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  15329. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  15330. p.notes = { prNote (960, 480) };
  15331. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  15332. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  15333. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  15334. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  15335. p.notes = { prNote (960, 480) };
  15336. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  15337. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  15338. p.notes = { prNote (960, 480) };
  15339. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  15340. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  15341. }
  15342. TEST (prtools_lfo)
  15343. {
  15344. using namespace lynx;
  15345. using namespace lynx::prtools;
  15346. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  15347. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  15348. const auto c = lfoCurve (o, 3840);
  15349. 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);
  15350. o.shape = LfoOptions::square;
  15351. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  15352. const auto sq = lfoCurve (o, 3840);
  15353. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  15354. o = {};
  15355. o.useEnd = true;
  15356. o.endRange = 0; // the range fades out
  15357. const auto fade = lfoCurve (o, 3840);
  15358. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  15359. Pattern p;
  15360. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  15361. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  15362. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  15363. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  15364. }
  15365. TEST (prtools_claw)
  15366. {
  15367. using namespace lynx;
  15368. using namespace lynx::prtools;
  15369. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  15370. Pattern p;
  15371. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  15372. claw (p, allOf (p), o);
  15373. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  15374. if (p.notes.size() == 3)
  15375. {
  15376. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  15377. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  15378. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  15379. }
  15380. o.trash = 9; // none: only the warp, which is neutral
  15381. p.notes = { prNote (100, 300) };
  15382. claw (p, allOf (p), o);
  15383. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  15384. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  15385. p.notes = { prNote (0, 480), prNote (960, 480) };
  15386. claw (p, allOf (p), o);
  15387. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  15388. o.trash = 0; // the first quarter
  15389. o.timeDistortion = 0;
  15390. o.stretch = false;
  15391. p.notes = { prNote (0, 240), prNote (240, 240) };
  15392. claw (p, allOf (p), o);
  15393. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  15394. }
  15395. TEST (prtools_articulate)
  15396. {
  15397. using namespace lynx;
  15398. using namespace lynx::prtools;
  15399. Pattern p;
  15400. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15401. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), lynx::Meter (4, 4));
  15402. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  15403. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  15404. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), lynx::Meter (4, 4));
  15405. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  15406. auto o = articulatePreset (ArticulatePreset::legato);
  15407. o.gap = 20;
  15408. p.notes = { prNote (0, 100), prNote (480, 100) };
  15409. articulate (p, allOf (p), o, lynx::Meter (4, 4));
  15410. CHECK (p.notes[0].length == 460);
  15411. // chop chords: a held note is cut where the next one starts
  15412. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  15413. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), lynx::Meter (4, 4));
  15414. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  15415. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  15416. o = articulatePreset (ArticulatePreset::legato);
  15417. o.variation = 50;
  15418. o.seed = 7;
  15419. Pattern a, b2;
  15420. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  15421. articulate (a, allOf (a), o, lynx::Meter (4, 4));
  15422. articulate (b2, allOf (b2), o, lynx::Meter (4, 4));
  15423. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  15424. }
  15425. TEST (prtools_chords)
  15426. {
  15427. using namespace lynx::prtools;
  15428. auto c = parseProgression ("I-V-vi-IV", 0, false);
  15429. CHECK (c && c->size() == 4);
  15430. if (c && c->size() == 4)
  15431. {
  15432. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  15433. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  15434. }
  15435. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  15436. CHECK (c && c->size() == 4);
  15437. if (c && c->size() == 4)
  15438. {
  15439. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  15440. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  15441. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  15442. }
  15443. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  15444. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  15445. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  15446. == std::vector<int> ({ 0, 2, 7, 14 }));
  15447. CHECK (! parseProgression ("I-X-V", 0, false));
  15448. 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");
  15449. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  15450. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  15451. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  15452. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  15453. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  15454. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  15455. {
  15456. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  15457. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  15458. CHECK (back && *back == gen);
  15459. }
  15460. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  15461. lynx::Pattern p;
  15462. const auto chords = *parseProgression ("I-IV", 0, false);
  15463. ChordWriteOptions w;
  15464. const auto added = writeProgression (p, 2, 0, chords, w);
  15465. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  15466. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  15467. w.rhythm = ">-`->-`->-`->-`-";
  15468. lynx::Pattern r;
  15469. writeProgression (r, 0, 0, chords, w);
  15470. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  15471. }
  15472. TEST (prtools_riff_machine)
  15473. {
  15474. using namespace lynx;
  15475. using namespace lynx::prtools;
  15476. Pattern p;
  15477. p.notes = { prNote (0, 100, 70) };
  15478. p.notes[0].channel = 1;
  15479. RiffOptions o;
  15480. o.channel = 1;
  15481. o.bars = 2;
  15482. const auto notes = riffMachine (p, { 0 }, o, lynx::Meter (4, 4));
  15483. CHECK (! notes.empty() && notes.size() == p.notes.size());
  15484. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  15485. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  15486. }));
  15487. // only the first step: the progression shape, chopped from one note at key 48
  15488. o.stepsUpTo = 0;
  15489. Pattern q;
  15490. const auto first = riffMachine (q, {}, o, lynx::Meter (4, 4));
  15491. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  15492. }
  15493. TEST (event_editor_lanes)
  15494. {
  15495. using namespace lynx;
  15496. Pattern p;
  15497. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  15498. auto& lane = eventedit::laneFor (p, vol);
  15499. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  15500. eventedit::fill (lane, 0, 960, 0.25f, 240);
  15501. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  15502. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  15503. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  15504. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  15505. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  15506. eventedit::setEvent (lane, 1440, 0.1f);
  15507. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  15508. eventedit::setEvent (lane, 100, 0.9f);
  15509. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  15510. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  15511. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  15512. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  15513. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  15514. eventedit::erase (lane, 0, 960);
  15515. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  15516. auto copy = lane;
  15517. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  15518. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  15519. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  15520. prtools::LfoOptions o;
  15521. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  15522. 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);
  15523. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  15524. eventedit::removeEmptyLanes (p);
  15525. CHECK (p.events.size() == 1);
  15526. // undo label
  15527. auto before = Song::makeDefault();
  15528. auto after = before;
  15529. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  15530. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  15531. // channel removal re-targets the lanes like automations
  15532. auto song = Song::makeDefault();
  15533. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  15534. song.removeChannel (1);
  15535. CHECK (song.patterns[0].events[0].target.index == 2);
  15536. }
  15537. TEST (event_editor_playback)
  15538. {
  15539. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  15540. auto song = lynx::Song::makeDefault();
  15541. song.bpm = 120.0;
  15542. const auto bar = (std::int64_t) lynx::ppq * 4;
  15543. song.patterns[0].lengthTicks = bar;
  15544. song.patterns[0].notes.clear();
  15545. for (int s = 0; s < 16; ++s)
  15546. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  15547. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  15548. const auto peaks = [&] (lynx::PlayMode mode) {
  15549. song.mode = mode;
  15550. song.playlist.clear();
  15551. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  15552. const double sr = 48000.0;
  15553. auto state = lynx::RackState::build (song, nullptr, sr);
  15554. lynx::RackRenderer r;
  15555. r.prepare (sr, 512);
  15556. std::vector<float> l (512), rr (512);
  15557. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  15558. std::array<float, 6> peak {}; // half bars
  15559. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  15560. {
  15561. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  15562. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  15563. const auto blockEnd = tick + 512 * tps;
  15564. const auto half = (std::size_t) (tick / (double) (bar / 2));
  15565. const auto edge = (double) half * (double) (bar / 2);
  15566. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  15567. continue;
  15568. for (auto v : l)
  15569. peak[half] = std::max (peak[half], std::abs (v));
  15570. }
  15571. return std::make_pair (state->eventLanes.size(), peak);
  15572. };
  15573. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  15574. CHECK (lanesSong == 1);
  15575. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  15576. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  15577. CHECK (lanesPattern == 1);
  15578. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  15579. }
  15580. TEST (flp_pattern_events_import)
  15581. {
  15582. // REA_rodada85 §1: event 0xDF holds a pattern's events {u32 tick, u32 controlID, i32 raw value}, 0xD8 the values
  15583. // reset when the song starts; they become pattern event lanes in FL's normalised scale
  15584. FlpBuilder b;
  15585. b.u8 (0x40); b.u16 (0); // channel 0
  15586. b.u8 (0x15); b.u8 (0); // sampler
  15587. b.u8 (0x41); b.u16 (1); // pattern 1: events only, no notes
  15588. auto rec = [] (std::vector<std::uint8_t>& v, std::uint32_t tick, std::uint32_t id, std::int32_t value) {
  15589. for (auto w : { tick, id, (std::uint32_t) value })
  15590. for (int i = 0; i < 4; ++i)
  15591. v.push_back ((std::uint8_t) (w >> (8 * i)));
  15592. };
  15593. std::vector<std::uint8_t> events;
  15594. rec (events, 0, 0x70001FC0u, 16000); // Master volume: top
  15595. rec (events, 96, 0x70001FC0u, 8000);
  15596. rec (events, 96, 0x70001FC0u, 4000); // same tick: the last one wins
  15597. rec (events, 0, 0x00000001u, 0); // channel 0 pan: hard left
  15598. rec (events, 48, 0x00000001u, 12800); // hard right
  15599. rec (events, 0, 0x00008000u, 5); // a plugin parameter of a sampler: scale unknown, skipped
  15600. b.data (0xDF, events);
  15601. std::vector<std::uint8_t> init;
  15602. rec (init, 0, 0x70001FC1u, -6400); // Master pan reset to hard left at song start
  15603. b.data (0xD8, init);
  15604. const auto project = flp::parseProject (b.bytes);
  15605. CHECK (project.initControls.size() == 1);
  15606. const flp::Pattern* src = nullptr;
  15607. for (const auto& p : project.patterns)
  15608. if (p.index == 1)
  15609. src = &p;
  15610. CHECK (src != nullptr && src->controlEvents.size() == 6);
  15611. lynx::FlpImportReport report;
  15612. lynx::FlpImportOptions options;
  15613. options.report = &report;
  15614. const auto song = lynx::importFlp (project, options);
  15615. const lynx::PatternEvents* volume = nullptr;
  15616. const lynx::PatternEvents* pan = nullptr;
  15617. int lanes = 0;
  15618. for (const auto& p : song.patterns)
  15619. for (const auto& lane : p.events)
  15620. {
  15621. ++lanes;
  15622. if (lane.target.kind == lynx::AutomationTargetKind::mixerVolume && lane.target.index == 0)
  15623. volume = &lane;
  15624. if (lane.target.kind == lynx::AutomationTargetKind::channelPan)
  15625. pan = &lane;
  15626. }
  15627. CHECK (lanes == 2 && volume != nullptr && pan != nullptr);
  15628. if (volume != nullptr && volume->events.size() == 2)
  15629. {
  15630. CHECK (volume->events[0].tick == 0 && volume->events[1].tick == lynx::ppq);
  15631. CHECK (volume->events[0].value > volume->events[1].value); // 4000 (not 8000) is the one kept
  15632. }
  15633. else
  15634. CHECK (false);
  15635. if (pan != nullptr && pan->events.size() == 2)
  15636. CHECK (pan->events[0].value < 0.01f && pan->events[1].value > 0.99f && pan->events[1].tick == lynx::ppq / 2);
  15637. else
  15638. CHECK (false);
  15639. CHECK (report.converted["Pattern events"] == 2);
  15640. CHECK (song.targetValue ({ lynx::AutomationTargetKind::mixerPan, 0 }) < 0.01f);
  15641. }
  15642. #include "PrefsTests.inc"
  15643. #include "core/ProjectSafety.h"
  15644. #include "core/SearchIndex.h"
  15645. #include "core/PluginCatalog.h"
  15646. #include "SafetyTests118.inc"
  15647. #include "Rea134Tests.inc"
  15648. #include "Rea139Tests.inc"
  15649. #include "Rea143Tests.inc"
  15650. #include "Rea143aTests.inc"
  15651. #include "Rea143bTests.inc"
  15652. #include "Rea146aTests.inc"
  15653. #include "Rea146bTests.inc"
  15654. #include "Rea146Tests.inc"
  15655. #include "Rea149Tests.inc"
  15656. #include "PluginCatalogTests151.inc"
  15657. #include "Rea151Tests.inc"
  15658. #include "AudioEditTests.inc"
  15659. #include "AudioEditToolsTests.inc"
  15660. #include "InputTests.inc"
  15661. #include "../asio/InputDrift.h"
  15662. #include "AsioDriftTests.inc"
  15663. TEST (level_audit_demo_imports_round89)
  15664. {
  15665. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  15666. if (realFlpDir.empty())
  15667. return;
  15668. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  15669. if (! std::filesystem::exists (path))
  15670. return std::nullopt;
  15671. std::ifstream in (path, std::ios::binary);
  15672. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  15673. return lynx::importFlp (flp::parseProject (file));
  15674. };
  15675. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  15676. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  15677. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  15678. {
  15679. int highRoutes = 0, highAtZero = 0;
  15680. for (const auto& t : song->mixer)
  15681. for (const auto& send : t.sends)
  15682. if (send.target >= 100)
  15683. {
  15684. ++highRoutes;
  15685. highAtZero += send.level <= 0.0f;
  15686. }
  15687. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  15688. CHECK (highRoutes > 0 && highAtZero == 0);
  15689. }
  15690. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  15691. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  15692. {
  15693. int compressors = 0, withSettings = 0;
  15694. for (const auto& t : song->mixer)
  15695. for (const auto& fx : t.effects)
  15696. if (fx.kind == lynx::EffectKind::fruityCompressor)
  15697. {
  15698. ++compressors;
  15699. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  15700. withSettings += fx.params != defaults.params;
  15701. }
  15702. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  15703. CHECK (compressors >= 10 && withSettings >= 8);
  15704. }
  15705. }
  15706. #include "UiLook90Tests.inc"
  15707. #include "UiFl104Tests.inc"
  15708. #include "DialogsTests84.inc"
  15709. #include "DockTests188.inc"
  15710. #include "SubstituteTests.inc"
  15711. #include "MixSuiteTests96.inc"
  15712. #include "PanelsTests105.inc"
  15713. // --- native plugin editors (round 93): FL's arrangements ---------------------------------------------------------
  15714. TEST (plugin_layouts_name_real_parameters)
  15715. {
  15716. using namespace lynx;
  15717. // every value control edits a parameter of its effect, inside the body; FL arrangements place (or list in a menu)
  15718. // every parameter of the effect
  15719. for (const auto kind : plugui::flLayoutKinds())
  15720. {
  15721. const auto& l = plugui::layoutFor (kind);
  15722. const auto& info = effectInfo (kind);
  15723. CHECK (plugui::hasFlLayout (kind));
  15724. // FL's own size wins for its one-cell editors (Fruity Center, Fruity Phase Inverter: 170 x 55)
  15725. const auto minHeight = kind == EffectKind::flCenter || kind == EffectKind::flPhaseInverter ? 50 : 60;
  15726. if (! (l.width > 100 && l.height > minHeight))
  15727. std::printf (" %s: %d x %d\n", info.name, l.width, l.height);
  15728. CHECK (l.width > 100 && l.height > minHeight);
  15729. std::vector<bool> placed ((std::size_t) plugui::paramCount (kind), false);
  15730. for (const auto& item : l.items)
  15731. {
  15732. 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))
  15733. std::printf (" %s: item '%s' leaves the body\n", info.name, item.param != nullptr ? item.param : item.label != nullptr ? item.label : "");
  15734. 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);
  15735. if (item.param != nullptr)
  15736. {
  15737. const auto p = plugui::paramIndex (kind, item.param);
  15738. CHECK (p >= 0);
  15739. if (p >= 0)
  15740. placed[(std::size_t) p] = true;
  15741. }
  15742. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  15743. {
  15744. const auto labels = plugui::optionLabels (item);
  15745. CHECK (labels.size() >= 2);
  15746. CHECK (item.values.empty() || item.values.size() == labels.size());
  15747. }
  15748. if (item.type == plugui::Item::knob && item.h + 0.5f < item.w) // (a caption above its knob would hide it)
  15749. {
  15750. std::printf (" %s: knob '%s' is shorter than it is wide\n", info.name, item.param != nullptr ? item.param : "");
  15751. CHECK (false);
  15752. }
  15753. CHECK (item.param2 == nullptr || plugui::paramIndex (kind, item.param2) >= 0);
  15754. if (item.param2 != nullptr && plugui::paramIndex (kind, item.param2) >= 0)
  15755. placed[(std::size_t) plugui::paramIndex (kind, item.param2)] = true;
  15756. for (const auto* when : { item.whenParam, item.when2Param, item.when3Param })
  15757. CHECK (when == nullptr || plugui::paramIndex (kind, when) >= 0);
  15758. }
  15759. for (const auto* name : l.menuSwitches)
  15760. {
  15761. const auto p = plugui::paramIndex (kind, name);
  15762. CHECK (p >= 0);
  15763. if (p >= 0)
  15764. placed[(std::size_t) p] = true;
  15765. }
  15766. for (const auto* name : l.editedElsewhere)
  15767. {
  15768. const auto p = plugui::paramIndex (kind, name);
  15769. CHECK (p >= 0);
  15770. if (p >= 0)
  15771. placed[(std::size_t) p] = true;
  15772. }
  15773. for (std::size_t p = 0; p < placed.size(); ++p)
  15774. if (! placed[p])
  15775. {
  15776. std::printf (" %s: parameter '%s' has no control\n", info.name, plugui::paramName (kind, (int) p));
  15777. CHECK (false);
  15778. }
  15779. }
  15780. }
  15781. TEST (maximus_editor_edits_the_slot_state)
  15782. {
  15783. using namespace lynx;
  15784. // the Maximus page edits the 59 parameters stored in the slot's state (REA_rodada8 §1.2)
  15785. std::array<std::array<int, 13>, 4> bands {};
  15786. for (auto& b : bands)
  15787. b = { 0, 1000, 1000, 0, 0, 1000, 1115, 0, 2, 5000, 3, 2882, 0 };
  15788. std::array<std::vector<std::array<double, 4>>, 8> envs;
  15789. for (auto& e : envs)
  15790. e = { { 0.0, 0.0, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 }, { 0.5, 0.5, 0.0, 2 } };
  15791. const auto state = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, 32768 }, bands, envs, 0);
  15792. const auto slot = maximusfx::pack ({ hlfx::MaximusBuild::shared, -1, state });
  15793. CHECK (maximusfx::stateParams().size() == 59);
  15794. CHECK (plugui::paramIndex (EffectKind::maximus, "Master release") == 7 + 13 * 3 + 9);
  15795. CHECK (maximusfx::stateParam (slot, 0).value_or (-1) == 19800);
  15796. CHECK (maximusfx::stateParam (slot, 7 + 13 + 9).value_or (-1) == 5000);
  15797. CHECK (maximusfx::stateParamText (0, 19800) == "200 Hz");
  15798. CHECK (maximusfx::stateParamText (2, 48065) == "3000 Hz");
  15799. CHECK (maximusfx::stateParamText (8, 1000) == "+0.0 dB");
  15800. CHECK (maximusfx::stateParamText (7 + 6, 0) == "Default");
  15801. CHECK (maximusfx::stateParamText (7 + 13 * 3 + 6, 1115) == "2.00 ms");
  15802. CHECK (maximusfx::stateParamText (6, -1) == "Off");
  15803. const auto edited = maximusfx::withStateParam (slot, 7 + 13 + 1, 2000); // mid pre gain → +18 dB
  15804. CHECK (maximusfx::stateParam (edited, 7 + 13 + 1).value_or (-1) == 2000);
  15805. CHECK (maximusfx::stateParam (edited, 7 + 1).value_or (-1) == 1000);
  15806. CHECK (maximusfx::stateParam (maximusfx::withStateParam (slot, 0, 99999), 0).value_or (-1) == 65536); // clamped
  15807. const auto blob = maximusfx::unpack (edited);
  15808. const auto original = maximusfx::unpack (slot);
  15809. CHECK (blob && original && (hlfx::makeMaximusPreset (blob->state) != nullptr) == (hlfx::makeMaximusPreset (original->state) != nullptr));
  15810. const auto curve = maximusfx::curvePoints (slot, 3);
  15811. CHECK (curve.size() == 3 && std::abs (curve[1].first - 0.5f) < 1e-6f && std::abs (curve[2].first - 1.0f) < 1e-6f);
  15812. CHECK (maximusfx::selectedBand (slot) >= 0);
  15813. CHECK (plugui::hasFlLayout (EffectKind::maximus));
  15814. }
  15815. TEST (plugin_layout_options_match_the_engine_values)
  15816. {
  15817. using namespace lynx;
  15818. // the switches show FL's captions for the engine's own values
  15819. const auto text = [] (EffectKind kind, const char* param, int option) {
  15820. for (const auto& item : plugui::layoutFor (kind).items)
  15821. if (item.param != nullptr && std::strcmp (item.param, param) == 0 && item.options != nullptr)
  15822. return formatEffectParam (kind, plugui::paramIndex (kind, param), plugui::optionValue (item, option), 120.0);
  15823. return std::string ("?");
  15824. };
  15825. CHECK (text (EffectKind::fruityDelay3, "Delay type", 0) == "Fake stereo");
  15826. CHECK (text (EffectKind::fruityDelay3, "Delay type", 2) == "Ping pong");
  15827. CHECK (text (EffectKind::fruityDelay3, "Delay type", 3) == "Off");
  15828. CHECK (text (EffectKind::fruityDelay3, "Filter mode", 1) == "High pass");
  15829. CHECK (text (EffectKind::fruityDelay3, "Saturation type", 1) == "Saturate");
  15830. CHECK (text (EffectKind::fruityMultiband, "Low mode", 1) == "off");
  15831. CHECK (text (EffectKind::fruityMultiband, "High mode", 2) == "bypass");
  15832. CHECK (text (EffectKind::luxeVerb, "Pitch type", 1) == "feed-forward");
  15833. CHECK (text (EffectKind::luxeVerb, "Freeze mode", 2) == "sustain");
  15834. CHECK (text (EffectKind::stereoEnhancer, "Invert", 2) == "invert R");
  15835. CHECK (text (EffectKind::parametricEq, "B3 type", 4) == formatEffectParam (EffectKind::parametricEq, peq::param (2, 0), peq::typeValue (peq::BandType::lowCut), 120.0));
  15836. // the option nearest to a value; pages and conditions
  15837. plugui::LayoutItem radio;
  15838. radio.type = plugui::Item::radio;
  15839. radio.options = "A|B|C|D";
  15840. CHECK (plugui::optionLabels (radio).size() == 4);
  15841. CHECK (plugui::optionAt (radio, 0.30f) == 1);
  15842. CHECK (plugui::optionAt (radio, 0.95f) == 3);
  15843. CHECK (std::abs (plugui::optionValue (radio, 2) - 2.0f / 3.0f) < 1e-6f);
  15844. const auto& lux = plugui::layoutFor (EffectKind::luxeVerb);
  15845. auto params = makeEffectSlot (EffectKind::luxeVerb).params;
  15846. const auto pitchType = plugui::paramIndex (EffectKind::luxeVerb, "Pitch type");
  15847. int feedForward = 0, feedback = 0;
  15848. for (float v : { 0.0f, 1.0f })
  15849. {
  15850. params[(std::size_t) pitchType] = v;
  15851. for (const auto& item : lux.items)
  15852. if (item.param != nullptr && std::strcmp (item.param, "Feed-forward pitch 1") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15853. ++feedForward;
  15854. else if (item.param != nullptr && std::strcmp (item.param, "Feedback pitch") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  15855. ++feedback;
  15856. }
  15857. CHECK (feedForward == 1); // only in feed-forward mode
  15858. CHECK (feedback == 2); // value plate + fader, only in feedback mode
  15859. const auto& limiter = plugui::layoutFor (EffectKind::fruityLimiter);
  15860. CHECK (limiter.pages.size() == 2);
  15861. int ceilingShown = 0;
  15862. for (int page = 0; page < 2; ++page)
  15863. for (const auto& item : limiter.items)
  15864. if (item.param != nullptr && std::strcmp (item.param, "Ceiling") == 0 && plugui::isShown (EffectKind::fruityLimiter, item, page, params))
  15865. ++ceilingShown;
  15866. CHECK (ceilingShown == 1); // LIMIT page only
  15867. // every other effect still gets a (generated) window
  15868. CHECK (! plugui::hasFlLayout (EffectKind::distortion));
  15869. CHECK (! plugui::layoutFor (EffectKind::distortion).items.empty());
  15870. }
  15871. #include "FabMeasureTests99.inc"
  15872. #include "DynamicsTests108.inc"
  15873. #include "Crest173dTests.inc"
  15874. #include "LynxTuneTests95.inc"
  15875. #include "LynxVerbTests100.inc"
  15876. #include "FacetTests103.inc"
  15877. #include "VocalLevel107Tests.inc"
  15878. #include "Substitutes113Tests.inc"
  15879. #include "Tune120Tests.inc"
  15880. #include "EchoSuiteTests101.inc"
  15881. #include "FruitySendTests105.inc"
  15882. #include "CrashFuzzTests111.inc"
  15883. #include "LookTests114.inc"
  15884. #include "FixedBlockTests118.inc"
  15885. #include "UiFl115Tests.inc"
  15886. #include "LoadTraceTests112.inc"
  15887. #include "VoiceToolsTests119.inc"
  15888. #include "VoiceMatchTests123.inc"
  15889. #include "LoadTests121.inc"
  15890. #include "Parity128Tests.inc"
  15891. #include "Parity128aTests.inc"
  15892. #include "Parity128bTests.inc"
  15893. #include "Parity128cTests.inc"
  15894. #include "Match124Tests.inc"
  15895. #include "Match124xTests.inc"
  15896. #include "Vmatch127Tests.inc"
  15897. #include "ParityTests129.inc"
  15898. #include "Parity135vTests.inc"
  15899. #include "Bugs131Tests.inc"
  15900. #include "Parity135wTests.inc"
  15901. #include "Feat133Tests.inc"
  15902. #include "Rea165fTests.inc"
  15903. #include "Parity135fTests.inc"
  15904. #include "Feat132Tests.inc"
  15905. #include "Parity135sTests.inc"
  15906. #include "VoiceMatch136Tests.inc"
  15907. #include "Parity137Tests.inc"
  15908. #include "Parity144Tests.inc"
  15909. #include "Parity144aTests.inc"
  15910. #include "Parity144bTests.inc"
  15911. #include "Parity147Tests.inc"
  15912. #include "Parity147aTests.inc"
  15913. #include "Parity147bTests.inc"
  15914. #include "Parity150Tests.inc"
  15915. #include "Parity150aTests.inc"
  15916. #include "Parity150bTests.inc"
  15917. #include "Parity152Tests.inc"
  15918. #include "Tp172dTests.inc"
  15919. #include "Verb183bEchoformTests.inc"
  15920. #include "Verb183bEpochTests.inc"
  15921. #include "Verb183bAtriumTests.inc"
  15922. #include "Verb183bPlateTests.inc"
  15923. #include "Verb183bHaloTests.inc"
  15924. #include "Verb183bNebulaTests.inc"
  15925. #include "Verb183bTests.inc"
  15926. #include "Parity152aTests.inc"
  15927. #include "Parity152bTests.inc"
  15928. #include "Parity152cTests.inc"
  15929. #include "Parity165Tests.inc"
  15930. #include "Parity165wTests.inc"
  15931. #include "Parity165xTests.inc"
  15932. #include "Parity165aTests.inc"
  15933. #include "Patcher141Tests.inc"
  15934. #include "Patcher162Tests.inc"
  15935. #include "Vox138Tests.inc"
  15936. #include "Vox145Tests.inc"
  15937. #include "Vox148Tests.inc"
  15938. #include "Natives142Tests.inc"
  15939. TEST (emphasizer_matches_fl_engine)
  15940. {
  15941. // REA_rodada154: FL's Emphasizer through the import (state → slot → effect) against goldens of the installation's
  15942. // Emphasizer_x64.dll (rate set before the state, 2304 samples of silence as FL's mixer runs it before a render,
  15943. // 512-frame calls, 44.1 kHz): FNV-1a of the output's float bits
  15944. auto signal = [] {
  15945. std::vector<float> l (24000), r (24000);
  15946. std::uint32_t s = 12345;
  15947. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  15948. for (int i = 0; i < 24000; ++i)
  15949. {
  15950. const float a = amps[(i / 3000) % 8];
  15951. s = s * 1664525u + 1013904223u;
  15952. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15953. s = s * 1664525u + 1013904223u;
  15954. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  15955. }
  15956. return std::pair { l, r };
  15957. };
  15958. struct Case { std::array<int, 6> raw; std::uint64_t hash; };
  15959. const Case cases[] { { { 0, 5013, 0, 0, 0, 10000 }, 0xa009608f060eb32eull }, { { 6000, 9000, 1, 0, 0, 8000 }, 0x6fc7ec82cc9d9c01ull },
  15960. { { 8000, 2000, 0, 1, 0, 10000 }, 0xb10a1f3bd998198cull }, { { 3000, 5013, 1, 0, 1, 10000 }, 0x57a8fa4d8b890ac0ull } };
  15961. for (const auto& c : cases)
  15962. {
  15963. std::vector<std::uint8_t> state (4 + 24 + 1, 0);
  15964. const std::int32_t version = 2;
  15965. std::memcpy (state.data(), &version, 4);
  15966. std::memcpy (state.data() + 4, c.raw.data(), 24);
  15967. state[28] = 1;
  15968. const auto slot = lynx::effectFromFl ("Emphasizer", state);
  15969. CHECK (slot && slot->kind == lynx::EffectKind::emphasizer);
  15970. if (! slot)
  15971. continue;
  15972. auto fx = lynx::makeEffect (slot->kind);
  15973. fx->prepare (44100.0);
  15974. auto [l, r] = signal();
  15975. for (int i = 0; i < 24000; i += 512) // the golden's 512-frame engine calls (level matching is per call)
  15976. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  15977. std::uint64_t h = 0xcbf29ce484222325ull;
  15978. for (int i = 0; i < 24000; ++i)
  15979. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  15980. {
  15981. std::uint32_t u;
  15982. std::memcpy (&u, &v, 4);
  15983. h = (h ^ u) * 0x100000001b3ull;
  15984. }
  15985. CHECK (h == c.hash);
  15986. }
  15987. // version 1 (no output control) keeps the 0 dB default; texts
  15988. 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 };
  15989. const auto s1 = lynx::emphasizer::parse (v1);
  15990. CHECK (s1 && s1->raw[1] == 5013 && s1->raw[5] == 10000);
  15991. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 0, 0.5f, 120.0) == "+12.0 dB");
  15992. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 5, 1.0f, 120.0) == "0.0 dB");
  15993. CHECK (lynx::formatEffectParam (lynx::EffectKind::emphasizer, 2, 1.0f, 120.0) == "Soft");
  15994. }
  15995. // ---- Wave Traveller (REA_rodada167f-wavetraveller) ----
  15996. namespace
  15997. {
  15998. // the crafted case: a sample on the 16-bit grid and a state that uses every feature (the same data went to the DLL)
  15999. inline std::vector<float> wtTestSample (int n) // planar stereo, values on the 16-bit grid
  16000. {
  16001. std::vector<float> s ((std::size_t) n * 2);
  16002. std::uint32_t r = 2024;
  16003. for (int i = 0; i < n; ++i)
  16004. for (int c = 0; c < 2; ++c)
  16005. {
  16006. r = r * 1664525u + 1013904223u;
  16007. const auto noise = (int) (r >> 20) - 2048; // ±2048
  16008. const auto tone = (int) std::lround (12000.0 * std::sin ((double) i * (c == 0 ? 0.031 : 0.047)));
  16009. s[(std::size_t) c * (std::size_t) n + (std::size_t) i] = (float) (tone + noise) / 32768.0f;
  16010. }
  16011. return s;
  16012. }
  16013. inline lynx::wavetrav::State wtTestState()
  16014. {
  16015. using namespace lynx::wavetrav;
  16016. auto st = defaultState();
  16017. st.samplePath = "x.wav"; // (FL asks its host for the sample only when the state names one)
  16018. 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) {
  16019. auto p = newPoint (x, y);
  16020. p.type = type, p.tension = tension, p.c1x = c1x, p.c1y = c1y, p.c2x = c2x, p.c2y = c2y;
  16021. return p;
  16022. };
  16023. auto& a = st.patches[60]; // every segment type, tempo based
  16024. a.wave.clear();
  16025. 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 };
  16026. const int ts[] { 1, 2, 3, 4, 5, 6, 8, 1 };
  16027. for (int i = 0; i < 8; ++i)
  16028. a.wave.push_back (pt (xs[i], ys[i], ts[i], ts[i] == 6 ? 0.2f : 0.5f));
  16029. a.speedBeats = 2.0f;
  16030. auto& b = st.patches[61]; // volume curve, mutes, start / end, attack / release
  16031. 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) };
  16032. b.mutes = { { true, 0.0f }, { false, 0.3f }, { true, 0.5f } };
  16033. b.start = 0.2f, b.end = 0.7f, b.attackMs = 50.0f, b.releaseMs = 300.0f, b.speedBeats = 1.0f;
  16034. auto& c = st.patches[62]; // absolute speed, tension segments
  16035. c.splineType = 1, c.absSpeed = 2.3f, c.start = 0.1f, c.end = 0.6f;
  16036. c.wave = { pt (0.0f, 0.0f, 6, 0.5f), pt (0.5f, 0.9f, 6, 0.9f), pt (1.0f, 0.2f, 1) };
  16037. auto& d = st.patches[63]; // types that write nothing, a tiny Bezier
  16038. 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),
  16039. pt (0.5004f, 0.2f, 3), pt (0.501f, 0.9f, 1), pt (1.0f, 0.4f, 1) };
  16040. for (auto& p : st.patches)
  16041. p.rebuild();
  16042. return st;
  16043. }
  16044. std::uint64_t floatBitsHash (const std::vector<float>& v)
  16045. {
  16046. std::uint64_t h = 0xcbf29ce484222325ull;
  16047. for (const float x : v)
  16048. {
  16049. std::uint32_t u;
  16050. std::memcpy (&u, &x, 4);
  16051. h = (h ^ u) * 0x100000001b3ull;
  16052. }
  16053. return h;
  16054. }
  16055. lynx::SampleData wtSampleData (const std::vector<float>& planar, int channels)
  16056. {
  16057. lynx::SampleData d;
  16058. const auto n = planar.size() / (std::size_t) channels;
  16059. d.left.assign (planar.begin(), planar.begin() + (std::ptrdiff_t) n);
  16060. if (channels > 1)
  16061. d.right.assign (planar.begin() + (std::ptrdiff_t) n, planar.end());
  16062. d.sampleRate = 44100.0;
  16063. return d;
  16064. }
  16065. } // namespace
  16066. TEST (wave_traveller_state_and_tables)
  16067. {
  16068. using namespace lynx::wavetrav;
  16069. // FL's state layout (§2): it round-trips byte for byte, and the default patch is FL's
  16070. const auto def = writeState (defaultState());
  16071. State s;
  16072. CHECK (parseState (def, s) && writeState (s) == def && s.selectedKey == 60 && s.samplePath.empty());
  16073. 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);
  16074. const auto crafted = writeState (wtTestState());
  16075. CHECK (parseState (crafted, s) && writeState (s) == crafted);
  16076. CHECK (s.patches[62].splineType == 1 && s.patches[61].mutes.size() == 3 && ! s.patches[61].mutes[1].on && s.patches[60].wave.size() == 8);
  16077. // tables (§3): a 0 → 1 line is 1022 values long (the writer steps back one value per segment)
  16078. CHECK (s.patches[0].waveN == 1022 && s.patches[0].waveTable[0] == 0.0f && s.patches[0].volTable[500] == 1.0f);
  16079. std::vector<float> tables;
  16080. for (int k = 60; k < 64; ++k)
  16081. {
  16082. const auto& p = s.patches[(std::size_t) k];
  16083. tables.insert (tables.end(), p.waveTable.begin(), p.waveTable.begin() + p.waveN + 1);
  16084. tables.insert (tables.end(), p.volTable.begin(), p.volTable.begin() + p.volN + 1);
  16085. CHECK (std::all_of (p.waveTable.begin(), p.waveTable.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  16086. }
  16087. // (LYNX's own value; the tables reach the DLL goldens of wave_traveller_matches_fl_engine through the output)
  16088. CHECK (floatBitsHash (tables) == 0xd94594a6f22cb4dfull);
  16089. if (floatBitsHash (tables) != 0xd94594a6f22cb4dfull)
  16090. std::printf (" wave traveller tables: %016llx\n", (unsigned long long) floatBitsHash (tables));
  16091. // knobs (FUN_007579e0 / 757b10 / 757b60)
  16092. CHECK (speedFromKnob (0.0f, false) == 0.125f && speedFromKnob (1.0f, false) == 8.0f && speedFromKnob (0.0f, true) == 0.25f
  16093. && speedFromKnob (1.0f, true) == 4.0f && attackFromKnob (1.0f) == 500.0f && releaseFromKnob (1.0f) == 1000.0f);
  16094. // a .spln preset (§6): knob positions, curves, mutes
  16095. std::vector<std::uint8_t> spln;
  16096. auto i32 = [&spln] (std::int32_t v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  16097. auto f32 = [&spln] (float v) { spln.insert (spln.end(), (std::uint8_t*) &v, (std::uint8_t*) &v + 4); };
  16098. i32 (4000), i32 (1000005), i32 (1);
  16099. for (float v : { 0.5f, 0.2f, 0.1f, 0.3f, 0.25f, 0.75f })
  16100. f32 (v);
  16101. const auto rec = [&] (float x, float y) {
  16102. std::vector<std::uint8_t> r (60, 0);
  16103. std::memcpy (r.data() + 8, &x, 4), std::memcpy (r.data() + 12, &y, 4);
  16104. const std::int32_t one = 1;
  16105. std::memcpy (r.data() + 0x30, &one, 4);
  16106. spln.insert (spln.end(), r.begin(), r.end());
  16107. };
  16108. i32 (3), rec (0.0f, 1.0f), rec (0.5f, 0.0f), rec (1.0f, 1.0f);
  16109. i32 (2), rec (0.0f, 1.0f), rec (1.0f, 0.5f);
  16110. i32 (2), i32 (0), f32 (0.0f), i32 (1), f32 (0.5f);
  16111. auto p = defaultPatch();
  16112. CHECK (applyPreset (spln, p) && p.splineType == 1 && p.speedBeats == speedFromKnob (0.5f, false) && p.absSpeed == speedFromKnob (0.2f, true)
  16113. && p.attackMs == attackFromKnob (0.1f) && p.releaseMs == releaseFromKnob (0.3f) && p.start == 0.25f && p.end == 0.75f
  16114. && p.wave.size() == 3 && p.vol.size() == 2 && p.mutes.size() == 2 && ! p.mutes[0].on && p.waveN > 1000);
  16115. // garbage is refused without crashing
  16116. std::uint32_t r = 7;
  16117. for (int round = 0; round < 300; ++round)
  16118. {
  16119. std::vector<std::uint8_t> blob (crafted.begin(), crafted.begin() + (std::ptrdiff_t) (r % crafted.size()));
  16120. for (int k = 0; k < 8 && ! blob.empty(); ++k)
  16121. {
  16122. r = r * 1664525u + 1013904223u;
  16123. blob[(r >> 8) % blob.size()] = (std::uint8_t) (r >> 24);
  16124. }
  16125. r = r * 1664525u + 1013904223u;
  16126. if (parseState (blob, s))
  16127. for (const auto& q : s.patches)
  16128. CHECK (q.waveN >= 0 && q.waveN < tableCapacity && q.volN >= 0 && q.volN < tableCapacity);
  16129. }
  16130. CHECK (! parseState ({ 1, 2, 3 }, s) && ! lynx::waveTravellerStateUsable ({}));
  16131. // the installation's states (read at test time): the render test and the plugin database preset
  16132. if (realFlpDir.empty())
  16133. return;
  16134. auto read = [] (const std::filesystem::path& path) {
  16135. std::ifstream in (path, std::ios::binary);
  16136. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  16137. };
  16138. const auto base = std::filesystem::path (realFlpDir);
  16139. if (const auto b = read (base / "System" / "Render tests" / "Wave Traveller.flp"); ! b.empty())
  16140. {
  16141. const auto project = flp::parseProject (b);
  16142. int found = 0;
  16143. for (const auto& c : project.channels)
  16144. if (c.pluginName == "Wave Traveller")
  16145. {
  16146. ++found;
  16147. CHECK (parseState (c.pluginState, s) && s.samplePath.find ("808 Snare.wav") != std::string::npos);
  16148. }
  16149. CHECK (found == 1);
  16150. const auto song = lynx::importFlp (project);
  16151. const auto it = std::find_if (song.channels.begin(), song.channels.end(), [] (const lynx::ChannelDef& c) { return c.instrument == lynx::InstrumentKind::wavetraveller; });
  16152. CHECK (it != song.channels.end() && lynx::waveTravellerStateUsable (it->pluginState));
  16153. }
  16154. if (const auto b = read (base / "System" / "Plugin databases" / "Plugin database (alphabetical)" / "Generators" / "Wave Traveller.fst"); ! b.empty())
  16155. {
  16156. const auto project = flp::parseProject (b, true);
  16157. CHECK (! project.channels.empty() && parseState (project.channels.front().pluginState, s) && s.samplePath.empty());
  16158. }
  16159. }
  16160. TEST (wave_traveller_matches_fl_engine)
  16161. {
  16162. // §8: goldens of the installation's Wave Traveller_x64.dll in our harness (16-bit stereo sample handed by LoadSample,
  16163. // 512-frame calls, tempo by ProcessEvent): FNV-1a of the output's float bits. LYNX's engine reproduces them exactly.
  16164. using namespace lynx::wavetrav;
  16165. const auto planar = wtTestSample (30000);
  16166. const auto smp = sampleFrom (wtSampleData (planar, 2));
  16167. CHECK (smp != nullptr && smp->length == 30000);
  16168. if (smp == nullptr)
  16169. return;
  16170. const auto state = wtTestState();
  16171. auto note = [] (int start, int key, float vol, float pan, float cents, int release, float initPan = 0.0f) {
  16172. RefNote n;
  16173. n.start = start, n.release = release;
  16174. n.note.key = key, n.note.finalVol = vol, n.note.finalPan = pan, n.note.initPan = initPan;
  16175. n.note.finalPitch = 100.0f * (float) key - 6000.0f + cents;
  16176. return n;
  16177. };
  16178. struct Case { std::vector<RefNote> notes; int rate; float tempo; int taps, length; std::uint64_t hash; };
  16179. const Case cases[] {
  16180. // live: every segment type, volume curve + mutes + attack / release, absolute speed, types 0 / 7 and a tiny Bezier
  16181. { { 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),
  16182. note (5000, 63, 1.0f, 0.6f, 250.0f, -1) }, 44100, 140.0f, 0, 40000, 0x9001a50747634ba6ull },
  16183. // render mode, 256 taps per side (FL's render default), with the notes' own pan
  16184. { { 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),
  16185. note (5000, 63, 1.0f, 0.6f, 250.0f, -1, -0.3f) }, 44100, 140.0f, 256, 8000, 0xedabd9728ffbb252ull },
  16186. // a 48 kHz host at tempo 97.5
  16187. { { 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 },
  16188. };
  16189. for (const auto& c : cases)
  16190. {
  16191. const auto out = renderReference (state, *smp, c.notes, c.rate, c.tempo, c.taps, c.length);
  16192. CHECK (floatBitsHash (out) == c.hash);
  16193. if (floatBitsHash (out) != c.hash)
  16194. std::printf (" wave traveller case: %016llx\n", (unsigned long long) floatBitsHash (out));
  16195. }
  16196. // the instrument (channel → engine) plays what the reference plays: velocity 1/1.28 = FL's Init.Vol 1, centred
  16197. {
  16198. lynx::ChannelDef ch;
  16199. ch.instrument = lynx::InstrumentKind::wavetraveller;
  16200. ch.pluginState = writeState (state);
  16201. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller, std::make_shared<lynx::SampleData> (wtSampleData (planar, 2)));
  16202. inst->configure (ch, 140.0);
  16203. inst->prepare (44100.0);
  16204. std::vector<float> l (20480, 0.0f), r (20480, 0.0f);
  16205. for (int pos = 0; pos < 20480; pos += 512)
  16206. {
  16207. if (pos == 0)
  16208. inst->noteOn (61, 1.0f / 1.28f);
  16209. if (pos == 10240)
  16210. inst->noteOff (61);
  16211. inst->render (l.data() + pos, r.data() + pos, 512, 1.0f, 1.0f);
  16212. }
  16213. const auto ref = renderReference (state, *smp, { note (0, 61, 1.0f, 0.0f, 0.0f, 10240) }, 44100, 140.0f, 0, 20480);
  16214. bool same = true;
  16215. for (int i = 0; i < 20480; ++i)
  16216. 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];
  16217. CHECK (same && inst->idle());
  16218. auto silent = lynx::makeInstrument (lynx::InstrumentKind::wavetraveller); // no sample: FL refuses the notes
  16219. silent->configure (ch, 140.0);
  16220. silent->prepare (44100.0);
  16221. silent->noteOn (60, 1.0f);
  16222. std::fill (l.begin(), l.end(), 0.0f);
  16223. silent->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  16224. CHECK (silent->idle() && std::all_of (l.begin(), l.begin() + 512, [] (float v) { return v == 0.0f; }));
  16225. }
  16226. // the installation's render test (its 808 Snare decoded as FL decodes it), read at test time
  16227. if (realFlpDir.empty())
  16228. return;
  16229. const auto base = std::filesystem::path (realFlpDir);
  16230. std::ifstream fin (base / "System" / "Render tests" / "Wave Traveller.flp", std::ios::binary);
  16231. std::ifstream sin (base / "Patches" / "Packs" / "Drums" / "Snares" / "808 Snare.wav", std::ios::binary);
  16232. const std::vector<std::uint8_t> flpBytes { std::istreambuf_iterator<char> (fin), {} }, wav { std::istreambuf_iterator<char> (sin), {} };
  16233. lynx::oggvorbis::Decoded dec;
  16234. if (flpBytes.empty() || wav.empty() || ! lynx::oggvorbis::decode (wav.data(), wav.size(), dec) || dec.channels != 1)
  16235. return;
  16236. const auto project = flp::parseProject (flpBytes);
  16237. for (const auto& c : project.channels)
  16238. if (c.pluginName == "Wave Traveller")
  16239. {
  16240. State st;
  16241. CHECK (parseState (c.pluginState, st));
  16242. lynx::SampleData d;
  16243. d.left = dec.data[0];
  16244. const auto snare = sampleFrom (d);
  16245. CHECK (snare != nullptr && snare->length == 11002);
  16246. if (snare == nullptr)
  16247. return;
  16248. const auto out = renderReference (st, *snare, { note (0, 60, 1.0f, 0.0f, 0.0f, 44100) }, 44100, 140.0f, 0, 88200);
  16249. CHECK (floatBitsHash (out) == 0xafcb49e8c0ef918dull);
  16250. if (floatBitsHash (out) != 0xafcb49e8c0ef918dull)
  16251. std::printf (" wave traveller render test: %016llx\n", (unsigned long long) floatBitsHash (out));
  16252. }
  16253. }
  16254. TEST (fl_legacy_fx_match_fl_engines)
  16255. {
  16256. // REA_rodada165k: FL's Fruity Phase Inverter, Center, Bass Boost, 7 Band EQ and Delay (v1) through the import (state → slot →
  16257. // effect) against goldens of the installation's DLLs (rate set before the state, 512-frame calls): FNV-1a of the
  16258. // output's float bits (x87 hosts) and ten output samples (every platform, within the x87 tolerance)
  16259. struct Case { const char* name; int rate; std::vector<int> raw; std::uint64_t hash; std::array<float, 10> pts; };
  16260. const Case cases[] {
  16261. { "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 } },
  16262. { "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 } },
  16263. { "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 } },
  16264. { "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 } },
  16265. { "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 } },
  16266. { "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 } },
  16267. { "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 } },
  16268. { "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 } },
  16269. { "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 } },
  16270. { "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 } },
  16271. { "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 } },
  16272. { "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 } },
  16273. { "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 } },
  16274. { "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 } },
  16275. { "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 } },
  16276. { "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 } },
  16277. { "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 } },
  16278. { "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 } },
  16279. { "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 } },
  16280. { "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 } },
  16281. { "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 } },
  16282. { "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 } },
  16283. { "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 } },
  16284. };
  16285. for (const auto& c : cases)
  16286. {
  16287. std::vector<std::uint8_t> state (4 + 4 * c.raw.size(), 0);
  16288. const std::int32_t version = 1;
  16289. std::memcpy (state.data(), &version, 4);
  16290. std::memcpy (state.data() + 4, c.raw.data(), 4 * c.raw.size());
  16291. const auto slot = lynx::effectFromFl (c.name, state);
  16292. CHECK (slot && slot->kind != lynx::EffectKind::utility && slot->kind != lynx::EffectKind::eq
  16293. && slot->kind != lynx::EffectKind::parametricEq);
  16294. if (! slot)
  16295. continue;
  16296. auto fx = lynx::makeEffect (slot->kind);
  16297. fx->prepare ((double) c.rate);
  16298. std::vector<float> l (24000), r (24000);
  16299. std::uint32_t s = 12345;
  16300. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16301. for (int i = 0; i < 24000; ++i)
  16302. {
  16303. const float a = amps[(i / 3000) % 8];
  16304. s = s * 1664525u + 1013904223u;
  16305. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16306. s = s * 1664525u + 1013904223u;
  16307. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16308. }
  16309. // host blocks of 512 like the goldens' DLL calls (the Center / Bass Boost / 7 Band EQ round their state to double at
  16310. // the end of each call, as FL does, so other block sizes differ in the last bits; checked below)
  16311. const auto input = std::make_pair (l, r);
  16312. for (int i = 0; i < 24000; i += 512)
  16313. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16314. {
  16315. auto other = lynx::makeEffect (slot->kind);
  16316. other->prepare ((double) c.rate);
  16317. auto l2 = input.first, r2 = input.second;
  16318. for (int i = 0; i < 24000; i += 1000)
  16319. other->process (l2.data() + i, r2.data() + i, 1000, slot->params, 120.0);
  16320. float worst = 0.0f;
  16321. for (std::size_t i = 0; i < 24000; ++i)
  16322. 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])) });
  16323. CHECK (worst <= 1.0e-4f); // (measured: ≤ 3.5e-5, the Bass Boost at 20 Hz)
  16324. }
  16325. for (int k = 0; k < 5; ++k)
  16326. {
  16327. const auto i = (std::size_t) (1500 + 4500 * k);
  16328. 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)])));
  16329. 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)])));
  16330. }
  16331. #if defined(__x86_64__) || defined(__i386__)
  16332. std::uint64_t h = 0xcbf29ce484222325ull;
  16333. for (int i = 0; i < 24000; ++i)
  16334. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16335. {
  16336. std::uint32_t u;
  16337. std::memcpy (&u, &v, 4);
  16338. h = (h ^ u) * 0x100000001b3ull;
  16339. }
  16340. CHECK (h == c.hash);
  16341. #endif
  16342. }
  16343. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.3f, 120.0) == "None");
  16344. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 0.5f, 120.0) == "Left");
  16345. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPhaseInverter, 0, 1.0f, 120.0) == "Right");
  16346. CHECK (lynx::formatEffectParam (lynx::EffectKind::flBassBoost, 0, 0.5f, 120.0) == "60 Hz");
  16347. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 1.0f, 120.0) == "+18.0 dB");
  16348. CHECK (lynx::formatEffectParam (lynx::EffectKind::fl7BandEq, 3, 0.5f, 120.0) == "+0.0 dB");
  16349. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 0.0f, 120.0) == "Host");
  16350. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 3, 1.0f, 120.0) == "280 BPM");
  16351. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 4, 2.0f / 15.0f, 120.0) == "3/16");
  16352. CHECK (lynx::formatEffectParam (lynx::EffectKind::flDelay1, 5, 1.0f, 120.0) == "Ping-pong");
  16353. CHECK (lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 }) == std::nullopt
  16354. || lynx::effectFromFl ("Fruity Delay", { 1, 0, 0, 0 })->kind != lynx::EffectKind::flDelay1);
  16355. // the default Phase Inverter of FL inverts the right side only (LYNX's old import inverted both)
  16356. CHECK (lynx::fllegacy::phaseInverterMode (1024) == 2 && lynx::fllegacy::phaseInverterMode (341) == 0
  16357. && lynx::fllegacy::phaseInverterMode (342) == 1 && lynx::fllegacy::phaseInverterMode (683) == 2);
  16358. }
  16359. TEST (fl_frequency_shifter_matches_fl_engine)
  16360. {
  16361. // REA_rodada154 §4: FL's Frequency Shifter (import → slot → effect) against goldens of the installation's
  16362. // Engine_x64.dll driven by its front end (rate before the state, 1536 samples of silence first, transport stopped, 120 BPM): FNV-1a
  16363. // of the output
  16364. struct Case { std::array<int, 10> raw; double rate; std::uint64_t hash; };
  16365. const Case cases[] { { { 10000, 0, -60480, 0, 10000, 10000, 5000, 10000, 1, 5000 }, 44100.0, 0x462af5f13683c90eull },
  16366. { { 7000, 2, 40000, 3000, 10000, 10000, 8000, 6000, 0, 2000 }, 44100.0, 0xcd597b95bf1bbb1aull },
  16367. { { 6000, 1, -30000, 6000, 3000, 7000, 3000, 4000, 0, 5000 }, 48000.0, 0x5592232115cb3acull },
  16368. { { 10000, 0, 51542, 2000, 8000, 10000, 5000, 10000, 0, 5000 }, 48000.0, 0x63d3051ba14bd00full } };
  16369. for (const auto& c : cases)
  16370. {
  16371. std::vector<std::uint8_t> state (4 + 40 + 8, 0);
  16372. const std::int32_t version = 4;
  16373. std::memcpy (state.data(), &version, 4);
  16374. std::memcpy (state.data() + 4, c.raw.data(), 40);
  16375. const auto slot = lynx::effectFromFl ("Frequency Shifter", state);
  16376. CHECK (slot && slot->kind == lynx::EffectKind::flFrequencyShifter);
  16377. if (! slot)
  16378. continue;
  16379. auto fx = lynx::makeEffect (slot->kind);
  16380. fx->prepare (c.rate);
  16381. std::vector<float> l (24000), r (24000);
  16382. std::uint32_t s = 12345;
  16383. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16384. for (int i = 0; i < 24000; ++i)
  16385. {
  16386. const float a = amps[(i / 3000) % 8];
  16387. s = s * 1664525u + 1013904223u;
  16388. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16389. s = s * 1664525u + 1013904223u;
  16390. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16391. }
  16392. for (int i = 0; i < 24000; i += 800)
  16393. fx->process (l.data() + i, r.data() + i, 800, slot->params, 120.0);
  16394. std::uint64_t h = 0xcbf29ce484222325ull;
  16395. for (int i = 0; i < 24000; ++i)
  16396. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16397. {
  16398. std::uint32_t u;
  16399. std::memcpy (&u, &v, 4);
  16400. h = (h ^ u) * 0x100000001b3ull;
  16401. }
  16402. CHECK (h == c.hash);
  16403. }
  16404. // the engine's tanhf against points of the library's
  16405. CHECK (lynx::fsfl::tanhf_ (0.0f) == 0.0f && lynx::fsfl::tanhf_ (20.0f) == 1.0f && lynx::fsfl::tanhf_ (-20.0f) == -1.0f);
  16406. CHECK (std::fabs (lynx::fsfl::tanhf_ (0.5f) - 0.462117165f) < 1e-7f && std::fabs (lynx::fsfl::tanhf_ (2.0f) - 0.964027584f) < 1e-7f);
  16407. CHECK (lynx::formatEffectParam (lynx::EffectKind::flFrequencyShifter, 1, 1.0f, 120.0) == "Ring modulator");
  16408. }
  16409. TEST (fl_low_lifter_matches_fl_engine)
  16410. {
  16411. // REA_rodada154 §5: FL's Low Lifter (import → slot → effect, 512-frame calls) against goldens of the installation's
  16412. // engine (ildsp_x64.dll's Faust program driven by Low Lifter_x64.dll, rate before the state): FNV-1a of the output
  16413. struct Case { std::array<int, 15> raw; double rate; std::uint64_t hash; };
  16414. const Case cases[] { { { 1, 10000, 4999, 7210, 1, 0, 0, 0, 5000, 10000, 7750, 0, 0, 1, 1 }, 44100.0, 0xcbb7eae8f31ca6d9ull },
  16415. { { 1, 7506, 5000, 3277, 1, 0, 6757, 5053, 9608, 10000, 7778, 5281, 1, 1, 1 }, 44100.0, 0xdb2d3712d4fb16c5ull },
  16416. { { 1, 5095, 6737, 5109, 1, 1, 541, 1479, 5451, 8197, 690, 6833, 1, 1, 1 }, 48000.0, 0x273c262202984447ull } };
  16417. for (const auto& c : cases)
  16418. {
  16419. std::vector<std::uint8_t> state (4 + 60 + 18, 0);
  16420. const std::int32_t version = 6;
  16421. std::memcpy (state.data(), &version, 4);
  16422. std::memcpy (state.data() + 4, c.raw.data(), 60);
  16423. const auto slot = lynx::effectFromFl ("Low Lifter", state);
  16424. CHECK (slot && slot->kind == lynx::EffectKind::flLowLifter);
  16425. if (! slot)
  16426. continue;
  16427. auto fx = lynx::makeEffect (slot->kind);
  16428. fx->prepare (c.rate);
  16429. std::vector<float> l (24000), r (24000);
  16430. std::uint32_t s = 12345;
  16431. const float amps[] { 0.3f, 1.5f, 0.05f, 3.0f, 0.8f, 0.0f, 2.0f, 0.4f };
  16432. for (int i = 0; i < 24000; ++i)
  16433. {
  16434. const float a = amps[(i / 3000) % 8];
  16435. s = s * 1664525u + 1013904223u;
  16436. l[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16437. s = s * 1664525u + 1013904223u;
  16438. r[(std::size_t) i] = (float) ((s >> 8) / 8388608.0 - 1.0) * a;
  16439. }
  16440. for (int i = 0; i < 24000; i += 512)
  16441. fx->process (l.data() + i, r.data() + i, std::min (512, 24000 - i), slot->params, 120.0);
  16442. std::uint64_t h = 0xcbf29ce484222325ull;
  16443. for (int i = 0; i < 24000; ++i)
  16444. for (float v : { l[(std::size_t) i], r[(std::size_t) i] })
  16445. {
  16446. std::uint32_t u;
  16447. std::memcpy (&u, &v, 4);
  16448. h = (h ^ u) * 0x100000001b3ull;
  16449. }
  16450. CHECK (h == c.hash);
  16451. }
  16452. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 10, 0.775f, 120.0) == "80 Hz");
  16453. CHECK (lynx::formatEffectParam (lynx::EffectKind::flLowLifter, 3, 0.721f, 120.0) == "-6.0 dB");
  16454. }
  16455. #include "Rea153Tests.inc"
  16456. #include "Logger181bTests.inc"
  16457. #include "Render155Tests.inc"
  16458. #include "Tune157Tests.inc"
  16459. #include "Tune165Tests.inc"
  16460. #include "HwOut156Tests.inc"
  16461. #include "SoundFont158Tests.inc"
  16462. #include "Mp3157Tests.inc"
  16463. #include "Quality160Tests.inc"
  16464. #include "CrashReport159Tests.inc"
  16465. #include "Sampler162Tests.inc"
  16466. #include "Workflow163cTests.inc"
  16467. #include "ClipPitch164Tests.inc"
  16468. #include "Misc164mTests.inc"
  16469. #include "MidiSync165Tests.inc"
  16470. #include "Theme162Tests.inc"
  16471. #include "PluginPresets162Tests.inc"
  16472. #include "Look162Tests.inc"
  16473. #include "EnvController162Tests.inc"
  16474. #include "Browser162Tests.inc"
  16475. #include "BrowserView163Tests.inc"
  16476. #include "Transport162Tests.inc"
  16477. #include "Workflow163Tests.inc"
  16478. #include "Rec163Tests.inc"
  16479. #include "Ui163Tests.inc"
  16480. #include "HwInsert164Tests.inc"
  16481. #include "Layer164Tests.inc"
  16482. #include "Patcher164vTests.inc"
  16483. #include "Vfx164Tests.inc"
  16484. #include "VfxScript165Tests.inc"
  16485. #include "Perf167Tests.inc"
  16486. #include "Misc164Tests.inc"
  16487. #include "Store165Tests.inc" // (round 165n; kept off the end of the list, where every round appends)
  16488. #include "Rack165Tests.inc" // (round 165f-rack; likewise)
  16489. #include "Visual164Tests.inc"
  16490. #include "Visual165Tests.inc"
  16491. #include "Dance190baTests.inc"
  16492. #include "Patcher164fTests.inc"
  16493. #include "Markers165Tests.inc"
  16494. #include "PerfMode165Tests.inc"
  16495. #include "Rea167gTests.inc"
  16496. #include "Rea167hTests.inc"
  16497. #include "Rea167jTests.inc"
  16498. #include "Rea167kTests.inc"
  16499. #include "Rea167lTests.inc"
  16500. #include "Patch165Tests.inc"
  16501. #include "FlPlugins165Tests.inc"
  16502. #include "FlScratch165Tests.inc"
  16503. #include "FlTrans165Tests.inc"
  16504. #include "VideoPlayer165Tests.inc"
  16505. #include "Video174dTests.inc"
  16506. #include "Dash165Tests.inc"
  16507. #include "LinuxMouse165Tests.inc"
  16508. #include "Fl165Tests.inc"
  16509. #include "Issues167oTests.inc"
  16510. #include "Perf165Tests.inc"
  16511. #include "Issue12Tests.inc"
  16512. #include "Issue12cTests.inc"
  16513. #include "Audit165zbTests.inc"
  16514. #include "FlGran165Tests.inc"
  16515. #include "GranDrop165Tests.inc"
  16516. #include "Kepler165kTests.inc"
  16517. #include "KeplerExo167dTests.inc"
  16518. #include "MixerLatency165Tests.inc"
  16519. #include "Rea168jTests.inc"
  16520. #include "Audit165Tests.inc"
  16521. #include "Vfx165cTests.inc"
  16522. #include "HarmorBeta165hTests.inc"
  16523. #include "Crash13Tests.inc"
  16524. #include "Issue11bTests.inc"
  16525. #include "Issue11cTests.inc"
  16526. #include "FlNewt165Tests.inc"
  16527. #include "Audit165zATests.inc"
  16528. #include "Audit165zA2Tests.inc"
  16529. #include "Eq166fTests.inc"
  16530. #include "Feat166dTests.inc"
  16531. #include "Round166eTests.inc"
  16532. #include "Issue15Tests.inc"
  16533. #include "Issue14bugsTests.inc"
  16534. #include "Mute190yTests.inc"
  16535. #include "Left167Tests.inc"
  16536. #include "Wheel173dTests.inc"
  16537. #include "PoiZoneWheels173dTests.inc"
  16538. #include "Tune166cTests.inc"
  16539. #include "TuneBreath190bbTests.inc"
  16540. #include "Update166bTests.inc"
  16541. #include "Emphasis167fTests.inc"
  16542. #include "Dvs167Tests.inc"
  16543. #include "EdisonScript167Tests.inc"
  16544. #include "PianoRollScript190Tests.inc"
  16545. #include "Deverb167Tests.inc"
  16546. #include "Score167Tests.inc"
  16547. #include "Ntt167Tests.inc"
  16548. #include "MidiOut167iTests.inc"
  16549. #include "Transporter167fTests.inc"
  16550. #include "Polish167pTests.inc"
  16551. #include "Menus168zTests.inc"
  16552. #include "Session168aTests.inc"
  16553. #include "Live169bTests.inc"
  16554. #include "Live169jTests.inc"
  16555. #include "Live190ahTests.inc"
  16556. #include "Remote190aiTests.inc"
  16557. #include "StemSplitTests.inc"
  16558. #include "AudioToMidi189bTests.inc"
  16559. #include "Zoom167tTests.inc"
  16560. #include "PianoKeys190jTests.inc"
  16561. #include "Proll168gTests.inc"
  16562. #include "Rea168eTests.inc"
  16563. #include "Design168nTests.inc"
  16564. #include "Design168qTests.inc"
  16565. #include "FirstRun167vTests.inc"
  16566. #include "LoopStarter168bTests.inc"
  16567. #include "Rec168kTests.inc"
  16568. #include "FlPaths124Tests.inc"
  16569. #include "Auto125Tests.inc"
  16570. #include "Rec170dTests.inc"
  16571. #include "Rec177dTests.inc"
  16572. #include "Pain177dTests.inc"
  16573. #include "Mixer177dTests.inc"
  16574. #include "Edit177dTests.inc"
  16575. #include "Patterns177dTests.inc"
  16576. #include "Issue32Tests.inc"
  16577. #include "Keys117Tests.inc"
  16578. #include "Pedal187aTests.inc"
  16579. #include "Sc109Tests.inc"
  16580. #include "WrapperMidi181cTests.inc"
  16581. #include "Keys96Tests.inc"
  16582. #include "Mono168lTests.inc"
  16583. #include "MixPre167qTests.inc"
  16584. #include "Design168rTests.inc"
  16585. #include "Playlist168fTests.inc"
  16586. #include "RecNoFold126Tests.inc"
  16587. #include "LoopWrap167qTests.inc"
  16588. #include "Sel168tTests.inc"
  16589. #include "Gap169kTests.inc"
  16590. #include "Gap169kPickerTests.inc"
  16591. #include "Gap169kAlignTests.inc"
  16592. #include "FlQ170iTests.inc"
  16593. #include "FlQ170i2Tests.inc"
  16594. #include "FlQ170i3Tests.inc"
  16595. #include "Speech168bTests.inc"
  16596. #include "Design168pTests.inc"
  16597. #include "Typing168tTests.inc"
  16598. #include "TypingPlace190bmTests.inc"
  16599. #include "GrossBeat168mTests.inc"
  16600. #include "Maximus168xTests.inc"
  16601. #include "Rec168xTests.inc"
  16602. #include "Design168oTests.inc"
  16603. #include "Pr44BrowserListTests.inc"
  16604. #include "Mixer168hTests.inc"
  16605. #include "Mute53fTests.inc"
  16606. #include "Issue32cTests.inc"
  16607. #include "Drumaxx169eTests.inc"
  16608. #include "PatternBlocks170fTests.inc"
  16609. #include "Mixfx170bTests.inc"
  16610. #include "Typeahead170bTests.inc"
  16611. #include "PlugQ170cTests.inc"
  16612. #include "Toolbar170eTests.inc"
  16613. #include "Rack170aTests.inc"
  16614. #include "Inst169hTests.inc"
  16615. #include "Crash169gTests.inc"
  16616. #include "Wfqa170hTests.inc"
  16617. #include "Toxic169hTests.inc"
  16618. #include "Gen169lTests.inc"
  16619. #include "Picker171cTests.inc"
  16620. #include "Picker175bTests.inc"
  16621. #include "Gen169l2Tests.inc"
  16622. #include "Patcher171bTests.inc"
  16623. #include "Wfqa170h2Tests.inc"
  16624. #include "Fader171cTests.inc"
  16625. #include "Tb169g2Tests.inc"
  16626. #include "Issues171aTests.inc"
  16627. #include "CtlAudit170i3Tests.inc"
  16628. #include "CtlWake173dTests.inc"
  16629. #include "Issue32dTests.inc"
  16630. #include "Issue81Tests.inc"
  16631. #include "Issue85Tests.inc"
  16632. #include "FlpExport171eTests.inc"
  16633. #include "Tp172cTests.inc"
  16634. #include "Layout172aTests.inc"
  16635. #include "Issues173aTests.inc"
  16636. #include "FxAudit173dTests.inc"
  16637. #include "Audit173d3Tests.inc"
  16638. #include "Vocodex173d3Tests.inc"
  16639. #include "Fluid174eTests.inc"
  16640. #include "Comp177Tests.inc"
  16641. #include "AlsImport175aTests.inc"
  16642. #include "FlmImport182cTests.inc"
  16643. #include "FlmEngines190uTests.inc"
  16644. #include "FlmNotes190abTests.inc"
  16645. #include "FlmGmsynth190abTests.inc"
  16646. #include "PocketSuperSaw190byTests.inc" // round 190by
  16647. #include "FlmImport187dTests.inc"
  16648. #include "FlmImport188fTests.inc"
  16649. #include "FlmImport188hTests.inc"
  16650. #include "FlmImport189hTests.inc"
  16651. #include "FlmImport189hATests.inc"
  16652. #include "FlmImport189hBTests.inc"
  16653. #include "FlmImport189hCTests.inc"
  16654. #include "FlmImport190bsTests.inc"
  16655. #include "PocketFx190byTests.inc" // round 190by
  16656. #include "FlmImport190deTests.inc" // round 190de
  16657. #include "PocketEq190dwTests.inc" // round 190dw
  16658. #include "PocketReverb190dwTests.inc" // round 190dw
  16659. #include "FlmEffects190dwTests.inc" // round 190dw: the .flm effect laws against FLM's renders
  16660. #include "FlmImport190dwInstTests.inc" // round 190dw item 4
  16661. #include "FlmImport190feTests.inc" // round 190fe: cut groups, 3x Osc lanes, effect Mix, rack parts
  16662. #include "FlmImport190fnTests.inc" // round 190fn: FLM resources folders (O1), FL Studio Mobile Rack in an .flp, META, short SEND
  16663. #include "Bridge176aTests.inc"
  16664. #include "Issues178aTests.inc"
  16665. #include "Mixer177aTests.inc"
  16666. #include "ClipLauncher181aTests.inc"
  16667. #include "Session190bvTests.inc"
  16668. #include "DetailView183aTests.inc"
  16669. #include "Als181cTests.inc"
  16670. #include "Groove190fmTests.inc"
  16671. #include "AlsDevices190bqTests.inc"
  16672. #include "AlsDevices190cfTests.inc"
  16673. #include "AlsDevices190edTests.inc"
  16674. #include "AlsDevices190ekTests.inc"
  16675. #include "AlsDevices190fmTests.inc"
  16676. #include "Merge180cTests.inc"
  16677. #include "Slicer180cTests.inc"
  16678. #include "Slicer190fTests.inc"
  16679. #include "Issue160Tests.inc"
  16680. #include "Issue159Tests.inc"
  16681. #include "Issue165Tests.inc"
  16682. #include "Bugs190gyTests.inc"
  16683. #include "Fpc190mTests.inc"
  16684. #include "Fpc190qTests.inc"
  16685. #include "Fpc190zTests.inc"
  16686. #include "PluginExport180bTests.inc"
  16687. #include "FlExport180dTests.inc"
  16688. #include "FlpMute182eTests.inc"
  16689. #include "FlpPeak182eTests.inc"
  16690. #include "FlpPatcherSc182eTests.inc"
  16691. #include "FlFades180dTests.inc"
  16692. #include "Stretch180dTests.inc"
  16693. #include "Timing180dTests.inc"
  16694. #include "FlpPdc182eTests.inc"
  16695. #include "Pdc188eTests.inc"
  16696. #include "Mixer177a2Tests.inc"
  16697. #include "Mixer177a3Tests.inc"
  16698. #include "Mixer186cTests.inc"
  16699. #include "Mixer177a4Tests.inc"
  16700. #include "Sc177bTests.inc"
  16701. #include "Auto177bTests.inc"
  16702. #include "Lanes177bTests.inc"
  16703. #include "Lfo177bTests.inc"
  16704. #include "Curves190bwTests.inc"
  16705. #include "Mod177bTests.inc"
  16706. #include "UiFont179aTests.inc"
  16707. #include "Multilink177bTests.inc"
  16708. #include "Issue97Tests.inc"
  16709. #include "SimpleTracks184aTests.inc"
  16710. #include "SimpleChooser184aTests.inc"
  16711. #include "FlStateWrite184aTests.inc"
  16712. #include "MasterLooks189kTests.inc"
  16713. #include "LayoutStyle184aTests.inc"
  16714. #include "Rack116Tests.inc"
  16715. #include "Issue116DetachedTests.inc"
  16716. #include "Trial187bTests.inc"
  16717. #include "ProjectKey184aTests.inc"
  16718. #include "KeyGuess184aTests.inc"
  16719. #include "Render186bTests.inc"
  16720. #include "Playlist189eTests.inc"
  16721. #include "Playlist189gTests.inc"
  16722. #include "Playlist189iTests.inc"
  16723. #include "Playlist189lTests.inc"
  16724. #include "Playlist189nTests.inc"
  16725. #include "PlaylistKeys190agTests.inc"
  16726. #include "Rack183cTests.inc"
  16727. #include "DrumRack190ecTests.inc"
  16728. #include "DrumRackSlice190ecTests.inc"
  16729. #include "DrumRack190elTests.inc"
  16730. #include "Gamepad184aTests.inc"
  16731. #include "ExportBundle184aTests.inc"
  16732. #include "MultiOut188dTests.inc"
  16733. #include "FxConn189jTests.inc"
  16734. #include "Strike189dTests.inc"
  16735. #include "NoteChance187cTests.inc"
  16736. #include "MidiTools190oTests.inc"
  16737. #include "MidiTools190sTests.inc"
  16738. #include "MidiTools190aaTests.inc"
  16739. #include "MidiTools190aa2Tests.inc"
  16740. #include "MidiGenerate190fmTests.inc"
  16741. #include "Notes188gTests.inc"
  16742. #include "Layer190bTests.inc"
  16743. #include "PlugLevel190adTests.inc"
  16744. #include "Undo190bTests.inc"
  16745. #include "TrackMeters190wTests.inc"
  16746. #include "Locator190wTests.inc"
  16747. #include "Bugs190cTests.inc"
  16748. #include "Parity190vTests.inc"
  16749. #include "Parity190arFetTests.inc"
  16750. #include "Parity190arApexTests.inc"
  16751. #include "Slicex190lTests.inc"
  16752. #include "Parity190alTests.inc"
  16753. #include "Slicex190hTests.inc"
  16754. #include "AudioX190aqTests.inc"
  16755. #include "Parity190arTranslateTests.inc"
  16756. #include "Surge190xTests.inc"
  16757. #include "Issue114Tests.inc"
  16758. #include "Slicex190rTests.inc"
  16759. #include "Slicex190acTests.inc"
  16760. #include "Slicex190ajTests.inc"
  16761. #include "Slicex190hgTests.inc"
  16762. #include "ScaleSnap190anTests.inc"
  16763. #include "TruePeak190amTests.inc"
  16764. #include "Loudness190amTests.inc"
  16765. #include "UsedOnly190amTests.inc"
  16766. #include "Extras190amTests.inc"
  16767. #include "Reload190aoTests.inc"
  16768. #include "Pin190aoTests.inc"
  16769. #include "Velocity190aoTests.inc"
  16770. #include "Audition190aoTests.inc"
  16771. #include "Versions190apTests.inc"
  16772. #include "core/PluginHold.h"
  16773. #include "PluginsOff190apTests.inc"
  16774. #include "ExportSections190apTests.inc"
  16775. #include "DirectWave190ajTests.inc"
  16776. #include "Similar190asTests.inc"
  16777. #include "Controller190awTests.inc"
  16778. #include "ControllerProfiles190awTests.inc"
  16779. #include "Bugs190axInsert6Tests.inc"
  16780. #include "Snap190axTests.inc"
  16781. #include "PluginState190azTests.inc"
  16782. #include "Parity190arTests.inc"
  16783. #include "Maps190caTests.inc"
  16784. #include "Mpe190bbTests.inc"
  16785. #include "Mpe190bbLiveTests.inc"
  16786. #include "PluginDb190baTests.inc"
  16787. #include "PerfWin190gdTests.inc"
  16788. #include "Ram190gnTests.inc"
  16789. #include "Startup190goTests.inc"
  16790. #include "Startup190hdTests.inc"
  16791. #include "Freeze190ayTests.inc"
  16792. #include "LiveFreeze190ffTests.inc"
  16793. #include "QaFix190bjTests.inc"
  16794. #include "Fl190bfTests.inc"
  16795. #include "ShotScale190biTests.inc"
  16796. #include "QaFix190bjATests.inc"
  16797. #include "Demo190bmTests.inc"
  16798. #include "Nebula190caTests.inc"
  16799. #include "Settings190bxTests.inc"
  16800. #include "Link190btTests.inc"
  16801. #include "LiveBrowser190ceTests.inc"
  16802. #include "SimpleFx190buTests.inc"
  16803. #include "SimpleGate190dgTests.inc"
  16804. #include "SimpleNext190dsTests.inc"
  16805. #include "FirstSongTour190dvTests.inc"
  16806. #include "SimplePads190foTests.inc"
  16807. #include "Rollup027Tests.inc"
  16808. #include "Edison190cdTests.inc"
  16809. #include "Fled190eeTests.inc"
  16810. #include "Warp190cgTests.inc"
  16811. #include "LiveScr190drTests.inc"
  16812. #include "LiveWf190efMapTests.inc"
  16813. #include "LiveWf190efDrawTests.inc"
  16814. #include "LiveWf190efScenesTests.inc"
  16815. #include "LiveWf190emTests.inc"
  16816. #include "BandlabExtras190chTests.inc"
  16817. #include "DanceFix190cjTests.inc"
  16818. #include "Reverb190bzTests.inc"
  16819. #include "Dyn190dmTests.inc"
  16820. #include "ParityDyn190fxTests.inc"
  16821. #include "Echo190dlTests.inc"
  16822. #include "Vqe190dpTests.inc"
  16823. #include "Ember190fjTests.inc"
  16824. #include "ApexFix190coTests.inc"
  16825. #include "Link190csTests.inc"
  16826. #include "TrackLinkTests.inc"
  16827. #include "TrackAuto190gzTests.inc"
  16828. #include "Warp190dbTests.inc"
  16829. #include "WarpModes190fmTests.inc" // round 190fm
  16830. #include "WarpLive190gxTests.inc" // round 190gx
  16831. #include "ClipEnv190giTests.inc" // round 190gi
  16832. #include "LiveFacts190gxTests.inc" // round 190gx
  16833. #include "BeatsLaw190gxTests.inc" // round 190gx
  16834. #include "FlCtl190dfTests.inc"
  16835. #include "FlFix190eyTests.inc"
  16836. #include "FlPl190dxTests.inc"
  16837. #include "FlClip190dzTests.inc"
  16838. #include "Iss152Tests.inc"
  16839. #include "Win190evTests.inc"
  16840. #include "U190heTests.inc"
  16841. #include "Mod190doTests.inc"
  16842. #include "Parity190fyTests.inc"
  16843. #include "Sampler190dhTests.inc"
  16844. #include "Fix190eaTests.inc"
  16845. #include "Iss158Tests.inc"
  16846. #include "Voices190ejTests.inc"
  16847. #include "Flfix190ezTests.inc"
  16848. #include "Reafl190flTests.inc"
  16849. #include "FlTails190gkTests.inc"
  16850. #include "Rcfix190fcTests.inc"
  16851. #include "Follow190fgTests.inc"
  16852. #include "Relfix190fpTests.inc"
  16853. #include "Anvil190fkTests.inc"
  16854. #include "AnvilBus190gxTests.inc"
  16855. #include "Donate190fqTests.inc"
  16856. #include "GenPreset190fsTests.inc"
  16857. #include "Issues190ftTests.inc"
  16858. #include "FlFacts190gwTests.inc"
  16859. #include "Flvsti190fwTests.inc"
  16860. #include "Stability190ggTests.inc"
  16861. #include "StemPass190gjTests.inc"
  16862. #include "Small190ghTests.inc"
  16863. #include "GoBack190hlTests.inc"
  16864. #include "Link190glTests.inc"
  16865. #include "SynthLanes190gpTests.inc"
  16866. #include "SchedIndex190gpTests.inc"
  16867. #include "SmartTails190gpTests.inc"
  16868. #include "ApexMeter190gpTests.inc"
  16869. #include "Toolchain190gtTests.inc"
  16870. #include "RenderAhead190gqTests.inc"
  16871. #include "EvPane190fzTests.inc"
  16872. #include "U190heLinkTests.inc" // round 190he (issue #152 item 2: linkable LYNX instruments, knobs that follow links)
  16873. #include "PluginDrop190hfTests.inc" // issue #152 point 6: plugins dropped on the tracks (FL and Live layouts)
  16874. #include "Rollup028Tests.inc" // round 190fv: semantic interactions of the 0.1.28 rollup
  16875. #include "Warp190ikTests.inc" // round 190ik: Complex Pro, granular Texture, the pitch-aware Beats detector