tests.cpp 787 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <optional>
  3. #include <atomic>
  4. #include <chrono>
  5. #include <thread>
  6. #include <random>
  7. #include <cctype>
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <filesystem>
  12. #include <map>
  13. #include <set>
  14. #include <sstream>
  15. #include <fstream>
  16. #include <functional>
  17. #include <iterator>
  18. #include <string>
  19. #include <tuple>
  20. #include <vector>
  21. #include "core/Dither.h"
  22. #include "core/DrumSynthLive.h"
  23. #include "core/Effects.h"
  24. #include "core/Effector.h"
  25. #include "core/FruityDelayBank.h"
  26. #include "core/FruityMultiband.h"
  27. #include "core/Denormals.h"
  28. #include "core/FlpImport.h"
  29. #include "core/FlEffects.h"
  30. #include "core/FlPatcher.h"
  31. #include "core/FlSamplers.h"
  32. #include "core/FlSynths.h"
  33. #include "core/Inflate.h"
  34. #include "core/WavPack.h"
  35. #include "core/FlWrapper.h"
  36. #include "core/FlPluginDb.h"
  37. #include "core/MissingFiles.h"
  38. #include "core/PianoRollTools.h"
  39. #include "core/EventEdit.h"
  40. #include "core/Preferences.h"
  41. #include "core/History.h"
  42. #include "core/Gms.h"
  43. #include "core/PoiZone.h"
  44. #include "core/MiniSynth.h"
  45. #include "core/SimSynth.h"
  46. #include "core/Granulizer.h"
  47. #include "core/Drumaxx.h"
  48. #include "core/Wasp.h"
  49. #include "core/PluginLayout.h"
  50. #include "core/Plucked.h"
  51. #include "core/BeepMap.h"
  52. #include "core/Sawer.h"
  53. #include "core/Slayer.h"
  54. #include "core/Toxic.h"
  55. #include "core/Harmless.h"
  56. #include "core/Harmor.h"
  57. #include "core/Morphine.h"
  58. #include "core/Ogun.h"
  59. #include "core/BassDrum.h"
  60. #include "core/FruitKick.h"
  61. #include "core/IppCos.h"
  62. #include "core/Dx10.h"
  63. #include "core/Kepler.h"
  64. #include "core/ThreeOsc.h"
  65. #include "core/FlResampler.h"
  66. #include "core/FlKeys.h"
  67. #include "core/DirectWave.h"
  68. #include "core/OggVorbis.h"
  69. #include "core/FlVoiceProcessor.h"
  70. #include "core/TransistorBass.h"
  71. #include "core/HarmlessFx.h"
  72. #include "core/MaximusFx.h"
  73. #include "core/Reeverb2.h"
  74. #include "core/LuxeVerb.h"
  75. #include "core/Denormals.h"
  76. #include "core/FruityDynamics.h"
  77. #include "core/FruityShapers.h"
  78. #include "core/ParametricEqFl.h"
  79. #include "core/FruityBasicFx.h"
  80. #include "core/EquoFx.h"
  81. #include "core/DistructorFx.h"
  82. #include "core/MultibandDelayFx.h"
  83. #include "core/PitchShifterFl.h"
  84. #include "core/FruityFilters.h"
  85. #include "core/NewtoneFx.h"
  86. #include "core/FruityDelay.h"
  87. #include "core/StereoFx.h"
  88. #include "core/FrequencySplitter.h"
  89. #include "core/ChorusFx.h"
  90. #include "core/GrossBeat.h"
  91. #include "core/LovePhilter.h"
  92. #include "core/FlangusFastDist.h"
  93. #include "core/FruityVocoder.h"
  94. #include "core/DelphiMath.h"
  95. #include "core/LiveEventQueue.h"
  96. #include "core/RackRenderer.h"
  97. #include "core/Scheduler.h"
  98. #include "core/SnapshotExchange.h"
  99. #include "core/SoftClip.h"
  100. #include "core/Slicer.h"
  101. #include "core/SytrusEngine.h"
  102. #include "core/Templates.h"
  103. #include "core/Vocodex.h"
  104. #include "core/TimeStretch.h"
  105. #include "core/WorkerPool.h"
  106. #include "core/RtStats.h"
  107. #include "core/ParametricEqLinear.h"
  108. #include "core/Song.h"
  109. #include "core/NoteEdit.h"
  110. #include "core/Recorder.h"
  111. #include "core/KeyMap.h"
  112. #include "core/RackOps.h"
  113. #include "core/ValueMath.h"
  114. #include "core/Workspace.h"
  115. #include "core/StartupSound.h"
  116. #include "core/ExportOptions.h"
  117. #include "core/Mp3Encoder.h"
  118. #include "core/PluginMonitor.h"
  119. #include "core/PlaylistOps.h"
  120. #include "core/TouchKeyboard.h"
  121. #include "core/HeldNotes.h"
  122. #include "core/Templates.h"
  123. #include "flp/Flp.h"
  124. #include "ui/I18nData.h"
  125. namespace
  126. {
  127. int failures = 0;
  128. int checks = 0;
  129. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  130. {
  131. static std::vector<std::pair<const char*, std::function<void()>>> r;
  132. return r;
  133. }
  134. struct Register
  135. {
  136. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  137. };
  138. #define TEST(name) \
  139. static void name(); \
  140. static Register reg_##name (#name, name); \
  141. static void name()
  142. #define CHECK(cond) \
  143. do \
  144. { \
  145. ++checks; \
  146. if (! (cond)) \
  147. { \
  148. ++failures; \
  149. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  150. } \
  151. } while (false)
  152. std::string realFlpDir;
  153. // --- helpers for building synthetic .flp files -----------------------------
  154. struct FlpBuilder
  155. {
  156. std::vector<std::uint8_t> bytes;
  157. FlpBuilder (std::uint16_t ppq = 96)
  158. {
  159. put ("FLhd");
  160. u32 (6);
  161. u16 (0);
  162. u16 (1);
  163. u16 (ppq);
  164. put ("FLdt");
  165. u32 (0); // size is not used by the reader
  166. }
  167. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  168. void u8 (std::uint8_t v) { bytes.push_back (v); }
  169. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  170. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  171. void varint (std::size_t n)
  172. {
  173. do
  174. {
  175. auto b = (std::uint8_t) (n & 0x7F);
  176. n >>= 7;
  177. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  178. } while (n);
  179. }
  180. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  181. {
  182. u8 (id);
  183. varint (payload.size());
  184. bytes.insert (bytes.end(), payload.begin(), payload.end());
  185. }
  186. void ascii (std::uint8_t id, const std::string& s)
  187. {
  188. std::vector<std::uint8_t> p (s.begin(), s.end());
  189. p.push_back (0);
  190. data (id, p);
  191. }
  192. void utf16 (std::uint8_t id, const std::u16string& s)
  193. {
  194. std::vector<std::uint8_t> p;
  195. for (auto c : s)
  196. {
  197. p.push_back ((std::uint8_t) c);
  198. p.push_back ((std::uint8_t) (c >> 8));
  199. }
  200. p.push_back (0);
  201. p.push_back (0);
  202. data (id, p);
  203. }
  204. };
  205. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  206. std::uint8_t velocity)
  207. {
  208. std::vector<std::uint8_t> r (24, 0);
  209. std::memcpy (&r[0], &pos, 4);
  210. std::memcpy (&r[6], &channel, 2);
  211. std::memcpy (&r[8], &len, 4);
  212. std::memcpy (&r[12], &key, 2);
  213. r[16] = 120;
  214. r[20] = 64;
  215. r[21] = velocity;
  216. return r;
  217. }
  218. } // namespace
  219. // ---------------------------------------------------------------------------
  220. // FLP
  221. TEST (flp_rejects_garbage)
  222. {
  223. std::vector<std::uint8_t> junk (64, 0x41);
  224. bool threw = false;
  225. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  226. CHECK (threw);
  227. }
  228. TEST (flp_reads_size_classes_varint_and_extended_ids)
  229. {
  230. FlpBuilder b;
  231. b.u8 (0x15); b.u8 (2); // u8
  232. b.u8 (0x40); b.u16 (513); // u16
  233. b.u8 (0x9C); b.u32 (128500); // u32
  234. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  235. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  236. flp::EventReader r (b.bytes);
  237. flp::Event e;
  238. CHECK (r.header().ppq == 96);
  239. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  240. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  241. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  242. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  243. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  244. CHECK (! r.next (e));
  245. }
  246. TEST (flp_truncated_file_throws)
  247. {
  248. FlpBuilder b;
  249. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  250. flp::EventReader r (b.bytes);
  251. flp::Event e;
  252. bool threw = false;
  253. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  254. CHECK (threw);
  255. }
  256. TEST (flp_version_numbers)
  257. {
  258. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  259. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  260. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  261. }
  262. TEST (flp_playlist_record_size_by_version)
  263. {
  264. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  265. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  266. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  267. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  268. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  269. }
  270. TEST (flp_playlist_record_size_by_file_flags)
  271. {
  272. // REA_rodada2 §4.2: 0xA9 bit 1 -> 80 B, bits 1 and 3 -> the native 88 B record (FL 25.2.99 / 26.x)
  273. CHECK (flp::playlistRecordSize (26001003, 88 * 4, 15) == 88); // 352 is also a multiple of 32
  274. CHECK (flp::playlistRecordSize (26001003, 88 * 10, 15) == 88); // ... and of 80
  275. CHECK (flp::playlistRecordSize (25002005, 160, 7) == 80);
  276. CHECK (flp::playlistRecordSize (21000000, 120, 1) == 60);
  277. CHECK (flp::playlistRecordSize (26001003, 63712) == 88); // no flags: the only size that fits besides 32
  278. }
  279. TEST (flp_native_88_byte_playlist_items_with_clip_gain_and_fades)
  280. {
  281. // an FL 26.1 file: 0xA9 = 15, four 88 B items (352 B: the old 32 B guess read garbage from it)
  282. FlpBuilder b (96);
  283. b.ascii (0xC7, "26.1.3.5570");
  284. b.u8 (0xA9); b.u32 (15);
  285. b.u8 (0x9C); b.u32 (130000);
  286. b.u8 (0x40); b.u16 (0);
  287. b.u8 (0x15); b.u8 (4); // audio clip channel
  288. b.utf16 (0xC4, u"C:\\nowhere\\clip.wav");
  289. std::vector<std::uint8_t> items;
  290. auto add = [&] (std::uint32_t pos, std::uint32_t len, std::uint16_t track, float fadeIn, float fadeOut, float gain) {
  291. std::vector<std::uint8_t> r (88, 0);
  292. const std::uint16_t base = 20480, idx = 0, rv = (std::uint16_t) (499 - track);
  293. const float offs[] = { -1.0f, -1.0f };
  294. std::memcpy (&r[0], &pos, 4);
  295. std::memcpy (&r[4], &base, 2);
  296. std::memcpy (&r[6], &idx, 2);
  297. std::memcpy (&r[8], &len, 4);
  298. std::memcpy (&r[12], &rv, 2);
  299. std::memcpy (&r[0x18], offs, 8);
  300. std::memcpy (&r[0x24], &fadeIn, 4);
  301. std::memcpy (&r[0x2C], &fadeOut, 4);
  302. std::memcpy (&r[0x34], &gain, 4);
  303. const double stretch = 1.0;
  304. const std::int32_t minusOne = -1;
  305. std::memcpy (&r[0x40], &stretch, 8);
  306. std::memcpy (&r[0x4C], &minusOne, 4);
  307. items.insert (items.end(), r.begin(), r.end());
  308. };
  309. add (0, 384, 0, 0.0f, 0.0f, 1.0f);
  310. add (384, 384, 1, 923.0769f, 461.5385f, 1.65f); // 2 beats in, 1 beat out at 130 BPM
  311. add (768, 96, 2, 0.0f, 0.0f, 0.0f);
  312. add (1536, 192, 3, 0.0f, 5000.0f, 0.5f); // fade longer than the clip
  313. b.data (0xE9, items);
  314. const auto p = flp::parseProject (b.bytes);
  315. CHECK (p.flagsA9 == 15);
  316. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4);
  317. CHECK (p.warnings.empty());
  318. if (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 4)
  319. {
  320. const auto& it = p.arrangements[0].items;
  321. CHECK (it[1].position == 384 && it[1].length == 384 && it[1].track == 1 && it[1].kind == flp::PlaylistItem::Kind::channelClip);
  322. CHECK (it[3].position == 1536 && it[3].track == 3);
  323. CHECK (std::abs (it[1].gain - 1.65f) < 1e-6f && std::abs (it[1].fadeInMs - 923.0769f) < 1e-3f);
  324. const auto song = lynx::importFlp (p);
  325. CHECK (song.playlist.size() == 4);
  326. if (song.playlist.size() == 4)
  327. {
  328. const auto beat = (std::int64_t) lynx::ppq;
  329. CHECK (song.playlist[0].gain == 1.0f && song.playlist[0].fadeIn == 0 && song.playlist[0].fadeOut == 0);
  330. CHECK (std::abs (song.playlist[1].gain - 1.65f) < 1e-6f);
  331. CHECK (std::llabs (song.playlist[1].fadeIn - 2 * beat) <= 1 && std::llabs (song.playlist[1].fadeOut - beat) <= 1);
  332. CHECK (song.playlist[2].gain == 0.0f);
  333. CHECK (song.playlist[3].fadeOut == song.playlist[3].length); // clamped
  334. }
  335. }
  336. }
  337. TEST (flp_parses_project_model)
  338. {
  339. FlpBuilder b (96);
  340. b.ascii (0xC7, "25.2.5.5319");
  341. b.u8 (0x9C); b.u32 (174500);
  342. b.utf16 (0xC2, u"Título");
  343. b.u8 (0x41); b.u16 (3);
  344. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  345. auto notes = noteRecord (96, 1, 48, 64, 100);
  346. auto n2 = noteRecord (0, 0, 0, 60, 128);
  347. notes.insert (notes.end(), n2.begin(), n2.end());
  348. b.data (0xE0, notes);
  349. b.u8 (0x40); b.u16 (0);
  350. b.u8 (0x15); b.u8 (0);
  351. b.utf16 (0xCB, u"Kick");
  352. {
  353. std::vector<std::uint8_t> levels (24, 0);
  354. const std::int32_t lv[] = { 3200, 12800, -150 };
  355. std::memcpy (levels.data(), lv, sizeof (lv));
  356. b.data (0xDB, levels);
  357. }
  358. b.u8 (0x68); b.u16 (5);
  359. b.u8 (0x40); b.u16 (1);
  360. b.u8 (0x15); b.u8 (2);
  361. b.utf16 (0xC9, u"FLEX");
  362. b.utf16 (0xCB, u"Lead");
  363. b.u8 (0x63); b.u16 (0);
  364. std::vector<std::uint8_t> item (80, 0);
  365. std::uint32_t pos = 384, len = 768;
  366. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  367. std::memcpy (&item[0], &pos, 4);
  368. std::memcpy (&item[4], &base, 2);
  369. std::memcpy (&item[6], &idx, 2);
  370. std::memcpy (&item[8], &len, 4);
  371. std::memcpy (&item[12], &rv, 2);
  372. b.data (0xE9, item);
  373. b.utf16 (0xCC, u"Drums");
  374. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  375. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  376. const auto p = flp::parseProject (b.bytes);
  377. CHECK (p.version == "25.2.5.5319");
  378. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  379. CHECK (p.title == "Título");
  380. CHECK (p.channels.size() == 2);
  381. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  382. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  383. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  384. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  385. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  386. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  387. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  388. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  389. const auto& it = p.arrangements[0].items[0];
  390. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  391. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  392. CHECK (p.warnings.empty());
  393. }
  394. TEST (flp_fuzz_corrupted_files_do_not_crash)
  395. {
  396. if (realFlpDir.empty())
  397. return;
  398. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  399. // must never hang, read past the buffer or produce a Song with invalid indices
  400. std::vector<std::vector<std::uint8_t>> sources;
  401. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  402. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  403. {
  404. std::ifstream in (entry.path(), std::ios::binary);
  405. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  406. }
  407. std::uint32_t seed = 2024;
  408. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  409. int parsed = 0, rejected = 0;
  410. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  411. {
  412. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  413. switch (round % 4)
  414. {
  415. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  416. 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;
  417. case 2: // absurd event size
  418. for (int k = 0; k < 5; ++k)
  419. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  420. break;
  421. default: // shuffled block
  422. {
  423. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  424. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  425. break;
  426. }
  427. }
  428. try
  429. {
  430. const auto project = flp::parseProject (bytes);
  431. const auto song = lynx::importFlp (project);
  432. ++parsed;
  433. for (const auto& p : song.patterns)
  434. for (const auto& n : p.notes)
  435. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  436. for (const auto& c : song.playlist)
  437. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  438. }
  439. catch (const flp::Error&)
  440. {
  441. ++rejected;
  442. }
  443. }
  444. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  445. CHECK (parsed + rejected == 240);
  446. }
  447. TEST (plugin_state_decoders_survive_garbage)
  448. {
  449. // random blobs (varied sizes, including valid headers followed by garbage)
  450. std::uint32_t seed = 77;
  451. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  452. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  453. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  454. int decoded = 0;
  455. for (int round = 0; round < 3000; ++round)
  456. {
  457. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  458. for (auto& b : blob)
  459. b = (std::uint8_t) rnd();
  460. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  461. {
  462. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  463. const auto v = versions[rnd() % 7];
  464. std::memcpy (blob.data(), &v, 4);
  465. }
  466. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  467. {
  468. ++decoded;
  469. for (auto v : fx->params)
  470. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  471. }
  472. lynx::SynthSettings synth;
  473. if (lynx::synthFrom3xOsc (blob, synth))
  474. for (const auto& o : synth.osc)
  475. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  476. float harmlessGain = 1.0f;
  477. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  478. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  479. if (lynx::synthFromFruitKick (blob, synth))
  480. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  481. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  482. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  483. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  484. if (auto sound = lynx::drumSynthLiveSound (blob))
  485. CHECK (sound->size() < 256);
  486. if (auto wave = lynx::waveTravellerSample (blob))
  487. CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
  488. lynx::synthFromBooBass (blob, synth);
  489. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  490. lynx::morphineStateUsable (blob);
  491. lynx::poizoneStateUsable (blob);
  492. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  493. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  494. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  495. if (auto patch = lynx::drumpadPatchName (blob))
  496. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  497. if (auto flex = lynx::flexPresetName (blob))
  498. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  499. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  500. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  501. if (lynx::synthFromDX10 (blob, synth))
  502. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  503. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  504. {
  505. std::memcpy (blob.data(), "SSLF", 4);
  506. if (blob.size() > 220)
  507. {
  508. std::memcpy (blob.data() + 40, "DATA", 4);
  509. std::memcpy (blob.data() + 48, "0rev", 4);
  510. }
  511. }
  512. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  513. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  514. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  515. {
  516. const auto params = lynx::miniSynthParams (blob);
  517. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  518. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  519. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  520. }
  521. lynx::ForeignPlugin foreign;
  522. lynx::parseFlWrapper (blob, foreign);
  523. std::vector<std::uint8_t> component, controller;
  524. lynx::splitFlVst3State (blob, component, controller);
  525. }
  526. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  527. }
  528. TEST (inflate_zlib)
  529. {
  530. // fixed Huffman with back-references
  531. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  532. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  533. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  534. // stored (uncompressed) block
  535. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  536. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  537. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  538. // truncated, bad header and size limit
  539. CHECK (! lynx::inflateZlib (fixed, 6));
  540. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  541. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  542. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  543. }
  544. TEST (fl_round15_effects_convert_known_values)
  545. {
  546. auto blob = [] (std::vector<std::int32_t> words) {
  547. std::vector<std::uint8_t> b (words.size() * 4);
  548. std::memcpy (b.data(), words.data(), b.size());
  549. return b;
  550. };
  551. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
  552. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  553. CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
  554. if (fs)
  555. {
  556. const auto x = (fs->params[0] - 0.5) * 2.0;
  557. CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
  558. CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
  559. }
  560. // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
  561. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  562. CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
  563. if (em)
  564. {
  565. CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
  566. CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
  567. }
  568. // Pitch Shifter +12 st, mix 100%
  569. 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 }));
  570. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  571. }
  572. TEST (frequency_shifter_moves_spectrum_by_hz)
  573. {
  574. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  575. fx->prepare (48000.0);
  576. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  577. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  578. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  579. std::vector<float> l (48000), r (48000);
  580. for (int i = 0; i < 48000; ++i)
  581. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  582. for (int done = 0; done < 48000; done += 480)
  583. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  584. auto energy = [&] (double f) {
  585. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  586. double s1 = 0.0, s2 = 0.0;
  587. for (int i = 9600; i < 48000; ++i)
  588. {
  589. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  590. s2 = s1;
  591. s1 = s0;
  592. }
  593. return s1 * s1 + s2 * s2 - k * s1 * s2;
  594. };
  595. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  596. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  597. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  598. }
  599. TEST (pitch_shift_moves_frequency_keeps_length)
  600. {
  601. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  602. fx->prepare (48000.0);
  603. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  604. auto crossingsAfter = [&] (float semitoneParam) {
  605. fx->reset();
  606. slot.params[0] = semitoneParam;
  607. std::vector<float> l (48000), r (48000);
  608. for (int i = 0; i < 48000; ++i)
  609. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  610. for (int done = 0; done < 48000; done += 512)
  611. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  612. int crossings = 0;
  613. for (int i = 9600; i < 48000; ++i)
  614. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  615. for (auto v : l)
  616. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  617. return crossings / 0.8 / 2.0; // cycles per second
  618. };
  619. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  620. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  621. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  622. }
  623. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  624. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  625. namespace
  626. {
  627. struct GbPoint
  628. {
  629. double dx, y;
  630. std::uint8_t type;
  631. };
  632. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  633. const std::map<int, std::vector<GbPoint>>& slots)
  634. {
  635. std::vector<std::uint8_t> b;
  636. 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); };
  637. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  638. i32 (0x00080003);
  639. for (auto v : params)
  640. i32 (v);
  641. put (options.data(), options.size());
  642. for (int k = 0; k < 72; ++k)
  643. {
  644. b.push_back (0); // empty name
  645. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  646. b.push_back (0), b.push_back (0), b.push_back (0);
  647. const auto it = slots.find (k);
  648. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  649. const auto& pts = it != slots.end() ? it->second : flat;
  650. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  651. for (const auto& p : pts)
  652. {
  653. put (&p.dx, 8), put (&p.y, 8);
  654. const float tension = 0.0f;
  655. put (&tension, 4);
  656. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  657. }
  658. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  659. }
  660. return b;
  661. }
  662. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  663. {
  664. auto fx = lynx::makeEffect (slot);
  665. fx->prepare (rate);
  666. auto l = in, r = in;
  667. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  668. for (std::size_t done = 0; done < l.size(); done += 512)
  669. {
  670. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  671. fx->setSongPosition ((double) done * tps);
  672. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  673. }
  674. return l;
  675. }
  676. } // namespace
  677. TEST (gross_beat_state_container_and_parameters)
  678. {
  679. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  680. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  681. CHECK (parsed.has_value());
  682. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  683. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  684. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  685. std::vector<std::uint8_t> fl;
  686. int ppq = 0;
  687. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  688. // every raw FL value survives the 0..1 parameter
  689. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  690. bool roundTrip = true;
  691. for (int i = 0; i < 8; ++i)
  692. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  693. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  694. CHECK (roundTrip);
  695. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  696. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  697. }
  698. TEST (gross_beat_volume_slot_gates_the_bar)
  699. {
  700. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  701. 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 } };
  702. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  703. const auto slot = lynx::grossbeat::slotFromFl (state);
  704. CHECK (slot.has_value());
  705. if (! slot)
  706. return;
  707. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  708. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  709. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  710. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  711. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  712. }
  713. TEST (gross_beat_time_slot_reads_the_past_bar)
  714. {
  715. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  716. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  717. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  718. const auto slot = lynx::grossbeat::slotFromFl (state);
  719. CHECK (slot.has_value());
  720. if (! slot)
  721. return;
  722. std::vector<float> in (150000, 0.0f);
  723. in[1000] = 1.0f;
  724. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  725. std::size_t peak = 0;
  726. for (std::size_t i = 0; i < out.size(); ++i)
  727. if (std::abs (out[i]) > std::abs (out[peak]))
  728. peak = i;
  729. CHECK (peak == 1000 + 96000 + 15);
  730. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  731. float elsewhere = 0.0f;
  732. for (std::size_t i = 0; i < out.size(); ++i)
  733. if (i + 20 < peak || i > peak + 20)
  734. elsewhere = std::max (elsewhere, std::abs (out[i]));
  735. CHECK (elsewhere == 0.0f);
  736. }
  737. TEST (volume_gate_follows_bar_position)
  738. {
  739. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  740. fx->prepare (48000.0);
  741. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  742. for (int i = 0; i < 32; ++i)
  743. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  744. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  745. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  746. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  747. fx->setSongPosition (0.0);
  748. for (int done = 0; done < 96000; done += 512)
  749. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  750. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  751. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  752. // starting mid-bar (beat 3): already closed
  753. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  754. fx->reset();
  755. fx->setSongPosition (2.0 * lynx::ppq);
  756. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  757. CHECK (l2[4000] < 0.01f);
  758. // mix 0: transparent
  759. slot.params[32] = 0.0f;
  760. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  761. fx->setSongPosition (2.0 * lynx::ppq);
  762. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  763. CHECK (l3[4000] > 0.99f);
  764. }
  765. TEST (patcher_branch_heads_follow_rea)
  766. {
  767. auto words = [] (std::vector<std::int32_t> v) {
  768. std::vector<std::uint8_t> b (v.size() * 4);
  769. std::memcpy (b.data(), v.data(), b.size());
  770. return b;
  771. };
  772. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  773. std::vector<std::int32_t> fs (18, 0);
  774. fs[0] = 4;
  775. fs[1] = 32768;
  776. fs[3] = 3;
  777. fs[6] = 0;
  778. fs[11] = -600;
  779. fs[15] = 1;
  780. fs[16] = 2;
  781. fs[17] = 3;
  782. const auto split = words (fs);
  783. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  784. CHECK (low.valid && low.slot && low.note.empty());
  785. if (low.slot)
  786. {
  787. // the exact engine (REA_rodada62): the low band's send, 24 dB/oct
  788. CHECK (low.slot->kind == lynx::EffectKind::frequencySplitter && low.slot->params[26] == 1.0f && low.slot->params[27] == 0.0f);
  789. CHECK (low.slot->params[2] == 3.0f / 7.0f);
  790. }
  791. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  792. 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
  793. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  794. CHECK (high.valid && high.slot && high.slot->params[28] == 1.0f && high.slot->params[10] == (float) (1200.0 / 3600.0));
  795. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  796. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  797. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  798. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  799. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  800. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  801. }
  802. TEST (parallel_container_sums_branches)
  803. {
  804. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  805. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  806. lynx::EffectBranch dry;
  807. dry.gain = 0.5f;
  808. lynx::EffectBranch inverted;
  809. inverted.gain = 0.5f;
  810. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  811. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  812. inverted.chain.push_back (flip);
  813. slot.branches = { dry, inverted };
  814. auto fx = lynx::makeParallelEffect (slot);
  815. fx->prepare (48000.0);
  816. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  817. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  818. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  819. // only the dry branch with gain 2: doubles
  820. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  821. auto doubler = lynx::makeParallelEffect (slot);
  822. doubler->prepare (48000.0);
  823. std::fill (l.begin(), l.end(), 0.25f);
  824. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  825. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  826. }
  827. TEST (fl_controller_constant_outputs)
  828. {
  829. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  830. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  831. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  832. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  833. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  834. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  835. std::vector<std::uint8_t> cs (4, 0);
  836. cs[0] = 1;
  837. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  838. const auto size = (std::uint64_t) body.size();
  839. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  840. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  841. out.insert (out.end(), body.begin(), body.end());
  842. };
  843. std::vector<std::uint8_t> value (12, 0);
  844. const float v = 0.75f;
  845. const std::int32_t out = 2;
  846. std::memcpy (value.data(), &v, 4);
  847. std::memcpy (value.data() + 8, &out, 4);
  848. std::vector<std::uint8_t> group;
  849. chunk (group, 0x836, value);
  850. chunk (cs, 0, group);
  851. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  852. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  853. }
  854. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  855. {
  856. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  857. const auto size = (std::uint64_t) body.size();
  858. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  859. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  860. out.insert (out.end(), body.begin(), body.end());
  861. };
  862. auto i32s = [] (std::vector<std::int32_t> v) {
  863. std::vector<std::uint8_t> b (v.size() * 4);
  864. std::memcpy (b.data(), v.data(), b.size());
  865. return b;
  866. };
  867. flp::Project p;
  868. flp::Channel midiOut;
  869. midiOut.index = 0;
  870. midiOut.type = flp::ChannelType::native;
  871. midiOut.pluginName = "MIDI Out";
  872. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  873. flp::Channel wrapper;
  874. wrapper.index = 1;
  875. wrapper.type = flp::ChannelType::native;
  876. wrapper.pluginName = "Fruity Wrapper";
  877. wrapper.pluginState = i32s ({ 9 });
  878. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  879. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  880. p.channels = { midiOut, wrapper };
  881. flp::Pattern pattern;
  882. pattern.index = 1;
  883. flp::Note note;
  884. note.channel = 0;
  885. note.key = 64;
  886. note.length = 96;
  887. pattern.notes.push_back (note);
  888. p.patterns.push_back (pattern);
  889. const auto song = lynx::importFlp (p);
  890. CHECK (song.channels.size() == 2 && song.channels[0].muted);
  891. CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
  892. int toWrapper = 0;
  893. for (const auto& pat : song.patterns)
  894. for (const auto& n : pat.notes)
  895. toWrapper += n.channel == 1 && n.key == 64;
  896. CHECK (toWrapper == 1);
  897. }
  898. TEST (flp_effect_automation_follows_slots_and_params)
  899. {
  900. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  901. flp::Project p;
  902. flp::MixerInsert insert;
  903. insert.index = 1;
  904. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  905. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  906. p.mixer.push_back (insert);
  907. auto clip = [&] (int index, std::uint32_t target) {
  908. flp::Channel c;
  909. c.index = index;
  910. c.type = flp::ChannelType::automationClip;
  911. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  912. p.channels.push_back (c);
  913. p.automationLinks.push_back ({ index, { target } });
  914. };
  915. 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; };
  916. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  917. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  918. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  919. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  920. const auto song = lynx::importFlp (p);
  921. CHECK (song.automations.size() == 4);
  922. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  923. const auto& freq = song.automations[0];
  924. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  925. CHECK (freq.target.param == lynx::peq::param (0, 1));
  926. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  927. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  928. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  929. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  930. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  931. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  932. // the automated mix is read/written through the target
  933. auto copy = song;
  934. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  935. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  936. }
  937. TEST (flp_real_files)
  938. {
  939. if (realFlpDir.empty())
  940. {
  941. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  942. return;
  943. }
  944. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  945. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, wrappedVst2 = 0, wrappedVst2State = 0, markers = 0, signatures = 0, trackEqs = 0;
  946. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  947. sytrusSounding = 0;
  948. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  949. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  950. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  951. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  952. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  953. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  954. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  955. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  956. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  957. int threeOscKind = 0, threeOscSounding = 0;
  958. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  959. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  960. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  961. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0, dwExact = 0;
  962. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  963. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  964. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  965. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  966. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  967. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  968. int filterSlots = 0, filterExact = 0; // REA_rodada63: Fast LP, Free Filter, Fruity Filter
  969. int distructorSlots = 0, distructorExact = 0, distructorImpulses = 0; // REA_rodada71
  970. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  971. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  972. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  973. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  974. lynx::FlpImportOptions withFiles;
  975. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  976. withFiles.roots = { flRoot, realFlpDir, "" };
  977. withFiles.fileExists = [] (const std::string& path) {
  978. auto local = path;
  979. std::replace (local.begin(), local.end(), '\\', '/');
  980. return std::filesystem::exists (local);
  981. };
  982. withFiles.readFile = [] (const std::string& path) {
  983. auto local = path;
  984. std::replace (local.begin(), local.end(), '\\', '/');
  985. std::ifstream in (local, std::ios::binary);
  986. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  987. };
  988. int embeddedSaved = 0;
  989. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  990. ++embeddedSaved;
  991. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  992. };
  993. int slicedChannels = 0, flKeysChannels = 0;
  994. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  995. {
  996. if (entry.path().extension() != ".flp")
  997. continue;
  998. std::ifstream in (entry.path(), std::ios::binary);
  999. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1000. ++files;
  1001. try
  1002. {
  1003. const auto p = flp::parseProject (bytes);
  1004. CHECK (! p.version.empty());
  1005. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  1006. const auto song = lynx::importFlp (p);
  1007. CHECK (! song.patterns.empty());
  1008. {
  1009. const auto full = lynx::importFlp (p, withFiles);
  1010. bool hasFpc = false;
  1011. for (const auto& c : p.channels)
  1012. hasFpc = hasFpc || c.pluginName == "FPC";
  1013. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  1014. // installation) and sounds; two demo channels match golden samples of our round-39 models
  1015. for (const auto& c : p.channels)
  1016. {
  1017. if (c.pluginName == "GMS")
  1018. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  1019. if (c.pluginName == "Morphine")
  1020. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  1021. }
  1022. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  1023. for (const auto& c : p.channels)
  1024. if (c.pluginName == "Kepler")
  1025. {
  1026. ++kepler;
  1027. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  1028. }
  1029. for (const auto& c : full.channels)
  1030. if (c.instrument == lynx::InstrumentKind::kepler)
  1031. {
  1032. ++keplerKind;
  1033. 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 } },
  1034. 44100.0, 11025);
  1035. double e = 0.0;
  1036. bool finite = ! y.empty();
  1037. for (auto v : y)
  1038. finite = finite && std::isfinite (v), e += (double) v * v;
  1039. CHECK (finite);
  1040. keplerSounding += e > 1e-6 ? 1 : 0;
  1041. }
  1042. const auto file = entry.path().filename().string();
  1043. for (const auto& c : full.channels)
  1044. {
  1045. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  1046. if (! isGms && ! isMorphine)
  1047. continue;
  1048. (isGms ? gmsKind : morphineKind)++;
  1049. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1050. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  1051. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  1052. std::vector<float> y;
  1053. if (isGms)
  1054. y = lynx::gms::renderReference (c.pluginState,
  1055. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  1056. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  1057. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1058. else
  1059. 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 } },
  1060. 44100.0, golden ? 100000 : 22050, c.samplePath);
  1061. double e = 0.0;
  1062. bool finite = ! y.empty();
  1063. for (auto v : y)
  1064. finite = finite && std::isfinite (v), e += (double) v * v;
  1065. CHECK (finite);
  1066. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  1067. if (golden && ! y.empty())
  1068. {
  1069. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  1070. const auto rms = std::sqrt (e / (double) y.size());
  1071. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  1072. 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 } },
  1073. 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 } };
  1074. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  1075. 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);
  1076. ++demoGoldens;
  1077. }
  1078. }
  1079. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  1080. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  1081. // renders of the installation's DLL for these states)
  1082. for (const auto& c : p.channels)
  1083. if (c.pluginName == "MiniSynth")
  1084. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  1085. for (const auto& c : full.channels)
  1086. {
  1087. if (c.instrument != lynx::InstrumentKind::minisynth)
  1088. continue;
  1089. ++miniSynthKind;
  1090. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1091. const auto exo = file.find ("Exoplanet") != std::string::npos;
  1092. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  1093. const auto y = lynx::minisynth::renderReference (c.pluginState,
  1094. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1095. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1096. 44100.0, golden ? 88200 : 22050, c.samplePath);
  1097. double e = 0.0;
  1098. bool finite = ! y.empty();
  1099. for (auto v : y)
  1100. finite = finite && std::isfinite (v), e += (double) v * v;
  1101. CHECK (finite);
  1102. miniSynthSounding += e > 1e-6 ? 1 : 0;
  1103. if (golden && ! y.empty())
  1104. {
  1105. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1106. const auto lead = c.name == "Main Lead 1";
  1107. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1108. 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 } },
  1109. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1110. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1111. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1112. ++miniSynthGoldens;
  1113. }
  1114. }
  1115. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1116. // samples of our round-44 model
  1117. for (const auto& c : p.channels)
  1118. if (c.pluginName == "PoiZone")
  1119. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1120. for (const auto& c : full.channels)
  1121. {
  1122. if (c.instrument != lynx::InstrumentKind::poizone)
  1123. continue;
  1124. ++poizoneKind;
  1125. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1126. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1127. const auto y = lynx::poizone::renderReference (c.pluginState,
  1128. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1129. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1130. 44100.0, golden ? 88200 : 22050);
  1131. double e = 0.0;
  1132. bool finite = ! y.empty();
  1133. for (auto v : y)
  1134. finite = finite && std::isfinite (v), e += (double) v * v;
  1135. CHECK (finite);
  1136. poizoneSounding += e > 1e-6 ? 1 : 0;
  1137. if (golden && ! y.empty())
  1138. {
  1139. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1140. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1141. 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 } };
  1142. for (const auto& g : at)
  1143. 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);
  1144. ++poizoneGoldens;
  1145. }
  1146. }
  1147. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1148. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1149. for (const auto& c : p.channels)
  1150. if (c.pluginName == "Toxic Biohazard")
  1151. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1152. for (const auto& c : full.channels)
  1153. {
  1154. if (c.instrument != lynx::InstrumentKind::toxic)
  1155. continue;
  1156. ++toxicKind;
  1157. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1158. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1159. const auto y = lynx::toxic::renderReference (
  1160. c.pluginState,
  1161. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1162. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1163. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1164. double e = 0.0;
  1165. bool finite = ! y.empty();
  1166. for (auto v : y)
  1167. finite = finite && std::isfinite (v), e += (double) v * v;
  1168. CHECK (finite);
  1169. toxicSounding += e > 1e-6 ? 1 : 0;
  1170. if (golden && ! y.empty())
  1171. {
  1172. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1173. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1174. 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 } };
  1175. for (const auto& g : at)
  1176. 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);
  1177. ++toxicGoldens;
  1178. }
  1179. }
  1180. for (const auto& c : full.channels)
  1181. {
  1182. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1183. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1184. {
  1185. ++harmlessCompWanted;
  1186. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1187. }
  1188. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1189. if (c.sampler.slices.size() >= 4)
  1190. {
  1191. ++slicedChannels;
  1192. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1193. CHECK (c.sampler.slices.back() < 1.0f);
  1194. }
  1195. dwExact += c.instrument == lynx::InstrumentKind::directwave ? 1 : 0;
  1196. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1197. {
  1198. ++dwChannels;
  1199. dwZones += (int) c.zones.size();
  1200. }
  1201. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1202. }
  1203. fpcKits += hasFpc;
  1204. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1205. // carrier's Sytrus preset) read from the installation
  1206. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1207. for (const auto& insert : p.mixer)
  1208. for (const auto& slot : insert.slots)
  1209. {
  1210. maximusSlots += slot.plugin == "Maximus";
  1211. delay2Slots += slot.plugin == "Fruity Delay 2";
  1212. delay3Slots += slot.plugin == "Fruity Delay 3";
  1213. sgzSlots += slot.plugin == "Soundgoodizer";
  1214. vocodexSlots += slot.plugin == "Vocodex";
  1215. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1216. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1217. equoSlots += slot.plugin == "EQUO";
  1218. distructorSlots += slot.plugin == "Distructor";
  1219. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1220. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1221. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1222. filterSlots += slot.plugin == "Fruity Fast LP" || slot.plugin == "Fruity Free Filter" || slot.plugin == "Fruity Filter";
  1223. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1224. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1225. flangusSlots += slot.plugin == "Fruity Flangus";
  1226. flangerSlots += slot.plugin == "Fruity Flanger";
  1227. }
  1228. for (const auto& t : full.mixer)
  1229. for (const auto& fx : t.effects)
  1230. {
  1231. if (fx.kind == lynx::EffectKind::maximus)
  1232. {
  1233. const auto blob = lynx::maximusfx::unpack (fx.state);
  1234. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1235. if (blob)
  1236. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1237. }
  1238. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1239. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1240. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1241. if (fx.kind == lynx::EffectKind::vocodex)
  1242. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1243. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1244. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1245. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1246. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1247. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1248. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1249. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1250. if (fx.kind == lynx::EffectKind::distructor)
  1251. if (const auto d = lynx::distructor::parseBlob (fx.state))
  1252. {
  1253. ++distructorExact;
  1254. for (const auto& m : d->modules)
  1255. distructorImpulses += m.type == 3 && ! m.impulseFile.empty();
  1256. }
  1257. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1258. filterExact += fx.kind == lynx::EffectKind::fruityFastLP || fx.kind == lynx::EffectKind::fruityFreeFilter
  1259. || fx.kind == lynx::EffectKind::fruityFilter;
  1260. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1261. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1262. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1263. }
  1264. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1265. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1266. basicSlots - basicSlots0, basicExact - basicExact0);
  1267. for (const auto& pat : full.patterns)
  1268. for (const auto& n : pat.notes)
  1269. {
  1270. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1271. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1272. }
  1273. }
  1274. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1275. for (const auto& t : song.mixer)
  1276. for (const auto& fx : t.effects)
  1277. {
  1278. if (fx.kind == lynx::EffectKind::parametricEq)
  1279. {
  1280. ++peqSlots;
  1281. peqShaped += fx.params != peqDefaults;
  1282. for (double hz : { 50.0, 1000.0, 10000.0 })
  1283. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1284. }
  1285. if (fx.kind == lynx::EffectKind::reverb)
  1286. {
  1287. ++reverbs;
  1288. reverbsDryless += fx.params[4] < 0.01f;
  1289. reverbsLowCut += fx.params[5] > 0.0f;
  1290. }
  1291. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1292. {
  1293. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1294. ++reverbs;
  1295. reverbsDryless += r2.p[10] == 0;
  1296. reverbsLowCut += r2.p[0] >= 20;
  1297. }
  1298. }
  1299. for (const auto& t : song.mixer)
  1300. if (t.eq.active())
  1301. {
  1302. ++trackEqs;
  1303. for (std::size_t b = 0; b < 3; ++b)
  1304. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1305. }
  1306. for (const auto& a : song.arrangements)
  1307. for (const auto& m : a.markers)
  1308. {
  1309. ++markers;
  1310. signatures += m.isSignature();
  1311. CHECK (m.tick >= 0);
  1312. }
  1313. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1314. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1315. if (plugin != "Fruity Wrapper")
  1316. return;
  1317. ++wrapped;
  1318. lynx::ForeignPlugin f;
  1319. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1320. if (f.format == lynx::ForeignPlugin::Format::vst2)
  1321. {
  1322. // VST2 state (REA_rodada89 §2): every one decodes and writes back to the same bytes
  1323. ++wrappedVst2;
  1324. lynx::FlVst2State st;
  1325. if (lynx::parseFlVst2State (f.state, st) && st.version == -9 && lynx::writeFlVst2State (st) == f.state)
  1326. ++wrappedVst2State;
  1327. }
  1328. };
  1329. for (const auto& c : p.channels)
  1330. countWrapper (c.pluginName, c.pluginState);
  1331. for (const auto& insert : p.mixer)
  1332. for (const auto& slot : insert.slots)
  1333. countWrapper (slot.plugin, slot.state);
  1334. // Harmless: every state in the demos becomes a synth, with finite values
  1335. for (const auto& c : p.channels)
  1336. if (c.pluginName == "Harmless")
  1337. {
  1338. ++harmless;
  1339. lynx::SynthSettings hs;
  1340. float g = 1.0f;
  1341. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1342. {
  1343. ++harmlessDecoded;
  1344. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1345. }
  1346. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1347. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1348. }
  1349. for (const auto& c : song.channels)
  1350. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1351. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1352. for (const auto& c : p.channels)
  1353. if (c.pluginName == "Harmor")
  1354. {
  1355. ++harmor;
  1356. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1357. }
  1358. for (const auto& c : song.channels)
  1359. if (c.instrument == lynx::InstrumentKind::harmor)
  1360. {
  1361. ++harmorKind;
  1362. 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);
  1363. double e = 0.0;
  1364. bool finite = ! y.empty();
  1365. for (auto v : y)
  1366. finite = finite && std::isfinite (v), e += (double) v * v;
  1367. CHECK (finite);
  1368. harmorSounding += e > 1e-6 ? 1 : 0;
  1369. }
  1370. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1371. for (const auto& c : p.channels)
  1372. if (c.pluginName == "Ogun")
  1373. {
  1374. ++ogun;
  1375. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1376. }
  1377. for (const auto& c : song.channels)
  1378. if (c.instrument == lynx::InstrumentKind::ogun)
  1379. {
  1380. ++ogunKind;
  1381. 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 },
  1382. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1383. double e = 0.0;
  1384. bool finite = ! y.empty();
  1385. for (auto v : y)
  1386. finite = finite && std::isfinite (v), e += (double) v * v;
  1387. CHECK (finite);
  1388. ogunSounding += e > 1e-6 ? 1 : 0;
  1389. }
  1390. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1391. // Magic Dust stage gets Autogun\Data\SG.dat
  1392. {
  1393. bool any = false;
  1394. for (const auto& c : p.channels)
  1395. if (c.pluginName == "Autogun")
  1396. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1397. if (any)
  1398. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1399. if (c.instrument == lynx::InstrumentKind::autogun)
  1400. {
  1401. ++autogunKind;
  1402. lynx::ogun::RefOptions opt;
  1403. opt.autogun = true;
  1404. if (! c.samplePath.empty())
  1405. {
  1406. auto local = c.samplePath;
  1407. std::replace (local.begin(), local.end(), '\\', '/');
  1408. std::ifstream sg (local, std::ios::binary);
  1409. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1410. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1411. }
  1412. 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 } },
  1413. 44100.0, 11025, opt);
  1414. double e = 0.0;
  1415. bool finite = ! y.empty();
  1416. for (auto v : y)
  1417. finite = finite && std::isfinite (v), e += (double) v * v;
  1418. CHECK (finite);
  1419. autogunSounding += e > 1e-6 ? 1 : 0;
  1420. }
  1421. }
  1422. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1423. for (const auto& c : p.channels)
  1424. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1425. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1426. for (const auto& c : song.channels)
  1427. if (c.instrument == lynx::InstrumentKind::dx10)
  1428. {
  1429. ++dx10Kind;
  1430. 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 } },
  1431. 44100.0, 11025);
  1432. double e = 0.0;
  1433. bool finite = ! y.empty();
  1434. for (auto v : y)
  1435. finite = finite && std::isfinite (v), e += (double) v * v;
  1436. CHECK (finite);
  1437. dx10Sounding += e > 1e-6 ? 1 : 0;
  1438. }
  1439. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1440. for (const auto& c : song.channels)
  1441. if (c.instrument == lynx::InstrumentKind::threeosc)
  1442. {
  1443. ++threeOscKind;
  1444. 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 } },
  1445. 44100.0, 11025);
  1446. double e = 0.0;
  1447. bool finite = ! y.empty();
  1448. for (auto v : y)
  1449. finite = finite && std::isfinite (v), e += (double) v * v;
  1450. CHECK (finite);
  1451. threeOscSounding += e > 1e-6 ? 1 : 0;
  1452. }
  1453. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1454. for (const auto& c : p.channels)
  1455. if (c.pluginName == "Sytrus")
  1456. {
  1457. ++sytrus;
  1458. lynx::SynthSettings ss;
  1459. float g = 1.0f;
  1460. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1461. if (lynx::sytrus::stateUsable (c.pluginState))
  1462. {
  1463. ++sytrusEngine;
  1464. if (sytrusRendered < 24)
  1465. {
  1466. ++sytrusRendered;
  1467. lynx::sytrus::Engine e;
  1468. e.prepare (44100.0, true, 512);
  1469. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1470. lynx::sytrus::NoteParams note;
  1471. e.noteOn (note);
  1472. std::vector<float> l (8192), r (8192);
  1473. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1474. bool finite = true;
  1475. double energy = 0.0;
  1476. for (std::size_t k = 0; k < l.size(); ++k)
  1477. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1478. CHECK (finite);
  1479. sytrusSounding += energy > 1e-6;
  1480. }
  1481. }
  1482. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1483. {
  1484. ++sytrusDecoded;
  1485. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1486. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1487. for (const auto& o : ss.osc)
  1488. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1489. }
  1490. }
  1491. // round 12 generators
  1492. for (const auto& c : p.channels)
  1493. {
  1494. lynx::SynthSettings gs;
  1495. float g = 1.0f;
  1496. int decoded = -1;
  1497. if (c.pluginName == "Harmor")
  1498. {
  1499. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1500. if (decoded)
  1501. {
  1502. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1503. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1504. static int resonant = 0, strong = 0, total = 0;
  1505. resonant += gs.resonance > 0.0f;
  1506. strong += gs.resonance > 0.9f;
  1507. if (++total % 50 == 0)
  1508. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1509. }
  1510. static int saw = 0, square = 0, other = 0, total = 0;
  1511. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1512. {
  1513. auto isSaw = true, isSquare = true;
  1514. for (int n = 1; n <= 16; ++n)
  1515. {
  1516. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1517. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1518. }
  1519. ++(isSaw ? saw : isSquare ? square : other);
  1520. if (++total % 20 == 0 || total == 100)
  1521. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1522. }
  1523. }
  1524. else if (c.pluginName == "GMS")
  1525. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1526. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1527. {
  1528. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1529. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1530. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1531. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1532. {
  1533. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1534. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1535. auto match = true;
  1536. for (std::size_t k = 0; k < 3; ++k)
  1537. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1538. ogunTrumpet += match ? 1 : 0;
  1539. }
  1540. }
  1541. else if (c.pluginName == "Sakura")
  1542. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1543. else if (c.pluginName == "Kepler")
  1544. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1545. else if (c.pluginName == "Wave Traveller")
  1546. decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
  1547. else if (c.pluginName == "PoiZone")
  1548. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1549. else if (c.pluginName == "BassDrum")
  1550. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1551. else if (c.pluginName == "Toxic Biohazard")
  1552. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1553. else if (c.pluginName == "Morphine")
  1554. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1555. else if (c.pluginName == "BooBass")
  1556. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1557. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1558. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1559. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1560. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1561. else if (c.pluginName == "FLEX")
  1562. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1563. else if (c.pluginName == "Fruity DrumSynth Live")
  1564. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1565. if (decoded < 0)
  1566. continue;
  1567. auto& counter = generators[c.pluginName];
  1568. ++counter.first;
  1569. counter.second += decoded != 0;
  1570. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1571. {
  1572. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1573. 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));
  1574. for (const auto& o : gs.osc)
  1575. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1576. }
  1577. }
  1578. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1579. for (const auto& c : p.channels)
  1580. if (c.pluginName == "3x Osc")
  1581. {
  1582. ++threeOsc;
  1583. lynx::SynthSettings s;
  1584. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1585. {
  1586. ++threeOscDecoded;
  1587. // with AM, osc 3 is the modulation depth and is left out of the sum
  1588. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1589. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1590. for (const auto& o : s.osc)
  1591. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1592. }
  1593. }
  1594. for (auto& pat : p.patterns)
  1595. if (! pat.notes.empty())
  1596. {
  1597. ++withNotes;
  1598. break;
  1599. }
  1600. for (auto& a : p.arrangements)
  1601. for (auto& item : a.items)
  1602. CHECK (item.track >= 0 && item.track < 500);
  1603. for (auto& clip : song.playlist)
  1604. {
  1605. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1606. CHECK (clip.length > 0 && clip.offset >= 0);
  1607. if (clip.kind == lynx::ClipKind::automation)
  1608. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1609. }
  1610. for (auto& a : song.automations)
  1611. {
  1612. ++automations;
  1613. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1614. CHECK (a.isLive() || ! a.points.empty());
  1615. for (std::size_t i = 1; i < a.points.size(); ++i)
  1616. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1617. }
  1618. // imported routing has no cycles and valid destinations
  1619. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1620. for (auto& t : song.mixer)
  1621. for (auto& s : t.sends)
  1622. {
  1623. ++sends;
  1624. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1625. }
  1626. if (! song.playlist.empty())
  1627. ++withPlaylist;
  1628. }
  1629. catch (const flp::Error& e)
  1630. {
  1631. ++failures;
  1632. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1633. }
  1634. }
  1635. CHECK (threeOscDecoded == threeOsc);
  1636. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1637. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1638. sytrusSounding, sytrusRendered);
  1639. CHECK (sytrusEngine == sytrus);
  1640. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1641. for (const auto& [name, counter] : generators)
  1642. {
  1643. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1644. CHECK (counter.second == counter.first);
  1645. }
  1646. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1647. CHECK (ogunTrumpet >= 1);
  1648. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1649. {
  1650. int vocodexes = 0, vocodexSides = 0;
  1651. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1652. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1653. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1654. if (entry.path().extension() == ".flp")
  1655. {
  1656. std::ifstream in (entry.path(), std::ios::binary);
  1657. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1658. try
  1659. {
  1660. const auto project = flp::parseProject (file);
  1661. for (const auto& insert : project.mixer)
  1662. for (const auto& slot : insert.slots)
  1663. if (slot.plugin == "Gross Beat")
  1664. {
  1665. // FL's own engine (REA_rodada53): every demo state is read
  1666. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1667. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1668. ++gates;
  1669. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1670. ++shaped;
  1671. }
  1672. else if (slot.plugin == "Fruity Love Philter")
  1673. {
  1674. ++lovePhilters;
  1675. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1676. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1677. {
  1678. ++lovePhiltersConverted;
  1679. CHECK (! conv->slots.empty());
  1680. for (const auto& fx : conv->slots)
  1681. for (auto v : fx.params)
  1682. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1683. for (const auto& fx : conv->slots)
  1684. for (const auto& link : fx.links)
  1685. {
  1686. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1687. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1688. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1689. }
  1690. for (const auto& link : conv->links)
  1691. {
  1692. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1693. CHECK (link.curve.size() == 33);
  1694. for (auto v : link.curve)
  1695. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1696. }
  1697. }
  1698. }
  1699. else if (slot.plugin == "Vocodex")
  1700. {
  1701. const auto v = lynx::vocodexEffect (slot.state);
  1702. CHECK (v.has_value());
  1703. vocodexes += v.has_value();
  1704. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1705. }
  1706. else if (slot.plugin == "Effector")
  1707. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1708. else if (slot.plugin == "Patcher")
  1709. {
  1710. const auto graph = lynx::parsePatcher (slot.state);
  1711. CHECK (graph.has_value());
  1712. ++patchers;
  1713. if (graph && ! graph->serialChain.empty())
  1714. ++serial;
  1715. if (graph && graph->serialChain.empty() && graph->audio)
  1716. ++parallel;
  1717. }
  1718. for (const auto& c : project.channels)
  1719. if (c.pluginName == "DirectWave")
  1720. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1721. for (const auto& wav : program->embeddedWav)
  1722. if (! wav.empty())
  1723. {
  1724. ++embeddedZones;
  1725. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1726. }
  1727. for (const auto& c : project.channels)
  1728. if (c.pluginName == "Patcher")
  1729. {
  1730. const auto graph = lynx::parsePatcher (c.pluginState);
  1731. CHECK (graph.has_value());
  1732. generatorPatchers += graph && graph->mainGenerator >= 0;
  1733. }
  1734. }
  1735. catch (const flp::Error&) {}
  1736. }
  1737. CHECK (gates >= 30 && shaped == gates);
  1738. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1739. lovePhilters, lovePhiltersRead);
  1740. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1741. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1742. CHECK (vocodexes >= 9);
  1743. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1744. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1745. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1746. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1747. CHECK (embeddedZones == 12);
  1748. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1749. parallel, generatorPatchers);
  1750. CHECK (gates == 0 || shaped > 0);
  1751. }
  1752. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1753. {
  1754. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1755. if (std::filesystem::exists (path))
  1756. {
  1757. std::ifstream in (path, std::ios::binary);
  1758. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1759. const auto song = lynx::importFlp (flp::parseProject (file));
  1760. const lynx::ChannelDef* controller = nullptr;
  1761. for (const auto& c : song.channels)
  1762. if (c.instrument == lynx::InstrumentKind::controller)
  1763. controller = &c;
  1764. CHECK (controller != nullptr);
  1765. if (controller != nullptr)
  1766. {
  1767. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1768. inst->prepare (48000.0);
  1769. inst->configure (*controller, song.bpm);
  1770. std::vector<float> l (480), r (480);
  1771. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1772. inst->noteOn (60, 1.0f);
  1773. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1774. const auto ducked = inst->controlOutput (0);
  1775. for (int i = 0; i < 100; ++i) // +1 s
  1776. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1777. const auto recovered = inst->controlOutput (0);
  1778. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1779. CHECK (ducked < 0.3f && recovered > 0.9f);
  1780. }
  1781. }
  1782. }
  1783. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1784. {
  1785. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1786. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1787. {
  1788. if (entry.path().extension() != ".flp")
  1789. continue;
  1790. std::ifstream in (entry.path(), std::ios::binary);
  1791. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1792. try
  1793. {
  1794. const auto project = flp::parseProject (file);
  1795. for (const auto& c : project.channels)
  1796. if (! c.pluginState.empty())
  1797. samples.emplace (c.pluginName, c.pluginState);
  1798. for (const auto& insert : project.mixer)
  1799. for (const auto& slot : insert.slots)
  1800. if (! slot.state.empty())
  1801. samples.emplace (slot.plugin, slot.state);
  1802. }
  1803. catch (const flp::Error&) {}
  1804. }
  1805. std::uint32_t seed = 12345;
  1806. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  1807. int mutations = 0;
  1808. for (const auto& [name, original] : samples)
  1809. for (int round = 0; round < 40; ++round)
  1810. {
  1811. auto blob = original;
  1812. if (round % 4 == 3 && ! blob.empty())
  1813. blob.resize (rnd() % blob.size()); // truncated
  1814. else
  1815. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  1816. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  1817. ++mutations;
  1818. lynx::SynthSettings synth;
  1819. float gain = 1.0f;
  1820. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  1821. lynx::synthFromHarmor (blob, synth, gain);
  1822. lynx::synthFromGms (blob, 120.0, synth, gain);
  1823. lynx::synthFromOgun (blob, synth, gain);
  1824. lynx::synthFromMorphine (blob, synth, gain);
  1825. lynx::synthFromKepler (blob, synth, gain);
  1826. lynx::synthFromPoiZone (blob, synth, gain);
  1827. lynx::synthFromToxic (blob, synth, gain);
  1828. lynx::synthFromBassDrum (blob, synth, gain);
  1829. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  1830. lynx::harmlessParams (blob);
  1831. if (lynx::harmorStateUsable (blob))
  1832. lynx::harmorFallbacks (blob);
  1833. lynx::parseEnvelopeController (blob);
  1834. lynx::parseKeyboardController (blob);
  1835. lynx::parseLovePhilter (blob);
  1836. lynx::parsePatcher (blob);
  1837. lynx::effectFromFl (name, blob);
  1838. lynx::fpcPads (blob);
  1839. lynx::directWaveProgram (blob);
  1840. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  1841. }
  1842. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  1843. }
  1844. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  1845. {
  1846. int files = 0, decoded = 0, audible = 0;
  1847. double seconds = 0.0;
  1848. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  1849. if (entry.path().extension() == ".wv")
  1850. {
  1851. std::ifstream in (entry.path(), std::ios::binary);
  1852. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1853. ++files;
  1854. lynx::DecodedAudio audio;
  1855. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  1856. continue;
  1857. ++decoded;
  1858. float peak = 0.0f;
  1859. bool finite = true;
  1860. for (const auto& ch : audio.channels)
  1861. for (auto v : ch)
  1862. {
  1863. finite = finite && std::isfinite (v);
  1864. peak = std::max (peak, std::abs (v));
  1865. }
  1866. CHECK (finite && peak <= 4.0f);
  1867. audible += peak > 1e-4f;
  1868. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  1869. }
  1870. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  1871. CHECK (decoded == files);
  1872. }
  1873. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  1874. int gmsPresets = 0, gmsDecoded = 0;
  1875. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  1876. if (std::filesystem::exists (gmsFolder))
  1877. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  1878. if (entry.path().extension() == ".gmsynth")
  1879. {
  1880. std::ifstream in (entry.path(), std::ios::binary);
  1881. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1882. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  1883. const auto n = (std::uint32_t) file.size();
  1884. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  1885. blob.insert (blob.end(), file.begin(), file.end());
  1886. lynx::SynthSettings gs;
  1887. float g = 1.0f;
  1888. ++gmsPresets;
  1889. if (lynx::synthFromGms (blob, 120.0, gs, g))
  1890. {
  1891. ++gmsDecoded;
  1892. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  1893. }
  1894. }
  1895. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  1896. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  1897. int presetFiles = 0, presetsRead = 0;
  1898. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  1899. "PoiZone", "Toxic Biohazard", "BassDrum" })
  1900. {
  1901. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  1902. if (! std::filesystem::exists (folder))
  1903. continue;
  1904. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  1905. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  1906. {
  1907. std::ifstream in (entry.path(), std::ios::binary);
  1908. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1909. ++presetFiles;
  1910. presetsRead += lynx::synthFromFlPreset (file).has_value();
  1911. }
  1912. }
  1913. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  1914. CHECK (presetsRead >= presetFiles * 95 / 100);
  1915. CHECK (gmsDecoded == gmsPresets);
  1916. CHECK (sytrusDecoded == sytrus);
  1917. CHECK (harmlessDecoded == harmless && harmless > 100);
  1918. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  1919. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  1920. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  1921. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  1922. 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",
  1923. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  1924. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  1925. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  1926. miniSynthSounding, miniSynthGoldens);
  1927. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  1928. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  1929. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  1930. poizoneSounding, poizoneGoldens);
  1931. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  1932. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  1933. toxicSounding, toxicGoldens);
  1934. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  1935. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  1936. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  1937. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  1938. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  1939. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  1940. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  1941. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  1942. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  1943. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  1944. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  1945. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  1946. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  1947. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  1948. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  1949. // option; the other Data folders add states with options not decoded
  1950. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  1951. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  1952. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  1953. clipperExact, clipperSlots);
  1954. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  1955. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  1956. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  1957. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  1958. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  1959. // one PanOMatic of "Bombs Away" (insert 69, last slot) falls off LYNX's 10-slot limit after a serial Patcher expands
  1960. CHECK (basicExact + 1 >= basicSlots && basicSlots >= 270); // REA_rodada58
  1961. // (Patcher graphs can add Distructor nodes of their own to the imported mixer)
  1962. std::printf (" Distructor: %d of %d slots on the exact engine (%d cabinet impulses read from the installation)\n",
  1963. distructorExact, distructorSlots, distructorImpulses);
  1964. CHECK (distructorExact >= distructorSlots && distructorSlots >= 4 && distructorImpulses >= 1); // REA_rodada71
  1965. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  1966. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  1967. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  1968. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  1969. std::printf (" Fruity Fast LP / Free Filter / Filter: %d of %d slots on the exact engines\n", filterExact, filterSlots);
  1970. CHECK (filterExact >= filterSlots && filterSlots >= 53); // REA_rodada63
  1971. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  1972. delay3Exact, delay3Slots);
  1973. CHECK (delay2Exact == delay2Slots && delay2Slots >= 60 && delay3Exact == delay3Slots && delay3Slots >= 100); // REA_rodada52
  1974. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  1975. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  1976. 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,
  1977. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  1978. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  1979. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  1980. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  1981. int presets = 0, presetsDecoded = 0;
  1982. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  1983. if (std::filesystem::is_directory (presetDir))
  1984. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  1985. if (entry.path().extension() == ".fst")
  1986. {
  1987. ++presets;
  1988. std::ifstream in (entry.path(), std::ios::binary);
  1989. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1990. if (auto synth = lynx::synthFromFlPreset (bytes))
  1991. {
  1992. ++presetsDecoded;
  1993. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  1994. }
  1995. }
  1996. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  1997. CHECK (presetsDecoded == presets);
  1998. // FL effect presets that become built-in effects
  1999. int fxPresets = 0, fxDecoded = 0;
  2000. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  2001. lynx::EffectKind::utility })
  2002. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  2003. {
  2004. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  2005. if (! std::filesystem::is_directory (dir))
  2006. continue;
  2007. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  2008. if (entry.path().extension() == ".fst")
  2009. {
  2010. ++fxPresets;
  2011. std::ifstream in (entry.path(), std::ios::binary);
  2012. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  2013. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  2014. }
  2015. }
  2016. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  2017. CHECK (fxDecoded == fxPresets);
  2018. CHECK (wrappedParsed == wrapped);
  2019. CHECK (wrappedVst2State == wrappedVst2);
  2020. std::printf (" %d plugins in Fruity Wrapper, %d read; %d VST2 states, %d decoded\n", wrapped, wrappedParsed, wrappedVst2, wrappedVst2State);
  2021. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  2022. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  2023. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  2024. padChannels, padNotes);
  2025. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  2026. std::printf (" DirectWave on the exact engine (REA_rodada72): %d channels\n", dwExact);
  2027. CHECK (dwExact >= 57);
  2028. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  2029. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  2030. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  2031. CHECK (flKeysChannels >= 8);
  2032. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  2033. CHECK (markers > 300 && signatures > 60);
  2034. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  2035. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  2036. // with its saved settings, in FL too
  2037. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  2038. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  2039. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  2040. CHECK (files > 0);
  2041. CHECK (automations > 0 && sends > 0);
  2042. }
  2043. // ---------------------------------------------------------------------------
  2044. // Sequencer
  2045. TEST (pattern_toggle_step_and_length)
  2046. {
  2047. lynx::Pattern p;
  2048. CHECK (! p.hasStep (0, 4));
  2049. p.toggleStep (0, 4);
  2050. CHECK (p.hasStep (0, 4));
  2051. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  2052. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  2053. p.toggleStep (1, 20); // second bar
  2054. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  2055. p.toggleStep (0, 4);
  2056. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  2057. }
  2058. TEST (scheduler_places_events_at_exact_samples)
  2059. {
  2060. lynx::Pattern p;
  2061. p.toggleStep (0, 0);
  2062. p.toggleStep (0, 4); // tick 960 = 1 beat
  2063. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  2064. const auto tps = lynx::ticksPerSample (bpm, sr);
  2065. lynx::EventBuffer ev;
  2066. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  2067. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  2068. int ons = 0;
  2069. for (auto& e : ev)
  2070. if (e.type == lynx::NoteEvent::Type::on)
  2071. {
  2072. ++ons;
  2073. CHECK (e.sampleOffset == 100);
  2074. }
  2075. CHECK (ons == 1);
  2076. }
  2077. TEST (scheduler_wraps_the_loop)
  2078. {
  2079. lynx::Pattern p;
  2080. p.toggleStep (2, 0);
  2081. const auto len = p.effectiveLength (4); // 3840 ticks
  2082. const double sr = 48000.0;
  2083. const auto tps = lynx::ticksPerSample (120.0, sr);
  2084. lynx::EventBuffer ev;
  2085. // block that crosses the loop end: starts 10 samples before
  2086. const auto start = (double) len - 10.0 * tps;
  2087. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  2088. bool found = false;
  2089. for (auto& e : ev)
  2090. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  2091. {
  2092. found = true;
  2093. CHECK (e.sampleOffset == 10);
  2094. }
  2095. CHECK (found);
  2096. }
  2097. TEST (scheduler_off_before_on_at_same_sample)
  2098. {
  2099. lynx::Pattern p;
  2100. lynx::Note a;
  2101. a.start = 0; a.length = 240; a.channel = 0;
  2102. lynx::Note b = a;
  2103. b.start = 240;
  2104. p.notes = { a, b };
  2105. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  2106. lynx::EventBuffer ev;
  2107. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  2108. CHECK (ev.size() >= 2);
  2109. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  2110. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  2111. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  2112. }
  2113. TEST (snapshot_exchange_hands_over_and_recycles)
  2114. {
  2115. struct Counted
  2116. {
  2117. int value;
  2118. int* alive;
  2119. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  2120. ~Counted() { --*alive; }
  2121. };
  2122. int alive = 0;
  2123. {
  2124. lynx::SnapshotExchange<Counted> x;
  2125. CHECK (x.acquire() == nullptr);
  2126. x.publish (std::make_unique<Counted> (1, &alive));
  2127. CHECK (x.acquire()->value == 1);
  2128. x.publish (std::make_unique<Counted> (2, &alive));
  2129. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  2130. CHECK (alive == 2);
  2131. CHECK (x.acquire()->value == 3);
  2132. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2133. x.collectGarbage();
  2134. CHECK (alive == 1);
  2135. // without collection, the return slots fill up and the audio side holds on to the current one
  2136. for (int i = 0; i < 20; ++i)
  2137. {
  2138. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2139. CHECK (x.acquire() != nullptr);
  2140. }
  2141. }
  2142. CHECK (alive == 0);
  2143. }
  2144. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2145. {
  2146. auto song = lynx::Song::makeDefault();
  2147. song.bpm = 120.0;
  2148. const double sr = 48000.0;
  2149. auto state = lynx::RackState::build (song, nullptr, sr);
  2150. lynx::RackRenderer r;
  2151. r.prepare (sr);
  2152. std::vector<float> l (512), rr (512);
  2153. double tick = 0.0;
  2154. float peak = 0.0f;
  2155. int kicks = 0;
  2156. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2157. {
  2158. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2159. for (auto& e : r.lastEvents())
  2160. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2161. ++kicks;
  2162. for (int i = 0; i < 512; ++i)
  2163. {
  2164. CHECK (std::isfinite (l[(std::size_t) i]));
  2165. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2166. }
  2167. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2168. }
  2169. CHECK (peak > 0.1f && peak < 4.0f);
  2170. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2171. // editing the song keeps the instruments of channels whose type didn't change
  2172. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2173. auto next = lynx::RackState::build (song, state.get(), sr);
  2174. CHECK (next->instruments[0] == state->instruments[0]);
  2175. CHECK (next->instruments[4] != state->instruments[4]);
  2176. // stopping silences the synth and triggers no new notes
  2177. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2178. CHECK (r.lastEvents().size() == 0);
  2179. }
  2180. TEST (flp_import_maps_channels_and_rescales)
  2181. {
  2182. flp::Project p;
  2183. p.header.ppq = 96;
  2184. p.tempo = 150.0;
  2185. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2186. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2187. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2188. flp::Pattern pat;
  2189. pat.index = 1;
  2190. flp::Note n;
  2191. n.position = 96;
  2192. n.length = 0;
  2193. n.channel = 2;
  2194. n.key = 72;
  2195. n.velocity = 64;
  2196. pat.notes.push_back (n);
  2197. p.patterns.push_back (pat);
  2198. const auto s = lynx::importFlp (p);
  2199. CHECK (s.bpm == 150.0);
  2200. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2201. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2202. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2203. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2204. CHECK (s.patterns.size() == 1);
  2205. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2206. CHECK (s.patterns[0].notes[0].channel == 2);
  2207. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2208. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2209. }
  2210. TEST (live_queue_is_fifo_and_bounded)
  2211. {
  2212. lynx::LiveEventQueue q;
  2213. lynx::NoteEvent e;
  2214. CHECK (! q.pop (e));
  2215. for (int i = 0; i < 10; ++i)
  2216. {
  2217. lynx::NoteEvent in;
  2218. in.key = i;
  2219. CHECK (q.push (in));
  2220. }
  2221. for (int i = 0; i < 10; ++i)
  2222. CHECK (q.pop (e) && e.key == i);
  2223. int pushed = 0;
  2224. while (q.push (e))
  2225. ++pushed;
  2226. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2227. }
  2228. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2229. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2230. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2231. TEST (fl_sampler_interpolators_match_fl)
  2232. {
  2233. std::vector<float> l, r;
  2234. std::uint32_t x = 12345;
  2235. for (int k = 0; k < 2 * 144; ++k)
  2236. {
  2237. x = x * 1103515245u + 12345u;
  2238. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2239. (k % 2 == 0 ? l : r).push_back (v);
  2240. }
  2241. const int pad = 40;
  2242. 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 } };
  2243. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2244. const Case cases[] = {
  2245. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2246. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2247. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2248. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2249. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2250. };
  2251. const lynx::flvp::X87Extended precision;
  2252. for (const auto& c : cases)
  2253. {
  2254. lynx::flrs::Kernel k;
  2255. k.setQuality (c.quality);
  2256. k.setSpeed (c.speed);
  2257. int same = 0;
  2258. for (int p = 0; p < 8; ++p)
  2259. {
  2260. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2261. float a, b;
  2262. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2263. std::uint32_t ba, bb;
  2264. std::memcpy (&ba, &a, 4);
  2265. std::memcpy (&bb, &b, 4);
  2266. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2267. }
  2268. CHECK (same == 16);
  2269. }
  2270. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2271. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2272. }
  2273. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2274. // last frame
  2275. TEST (fl_sampler_copies_at_original_rate)
  2276. {
  2277. auto data = std::make_shared<lynx::SampleData>();
  2278. data->sampleRate = 48000.0;
  2279. for (int i = 0; i < 300; ++i)
  2280. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2281. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2282. sampler->prepare (48000.0);
  2283. sampler->noteOn (60, 1.0f);
  2284. std::vector<float> l (400), r (400);
  2285. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2286. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2287. int exact = 0;
  2288. for (int i = 0; i < 300; ++i)
  2289. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2290. CHECK (exact == 300);
  2291. CHECK (l[300] == 0.0f);
  2292. }
  2293. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2294. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2295. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2296. {
  2297. auto data = std::make_shared<lynx::SampleData>();
  2298. data->sampleRate = 48000.0;
  2299. data->left.assign (96000, 0.5f);
  2300. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2301. sampler->prepare (48000.0);
  2302. lynx::ChannelDef c;
  2303. c.instrument = lynx::InstrumentKind::sampler;
  2304. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2305. sampler->configure (c, 120.0);
  2306. sampler->noteOn (60, 1.0f);
  2307. std::vector<float> l (40000), r (40000);
  2308. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2309. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2310. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2311. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2312. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2313. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2314. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2315. sampler->noteOff (60);
  2316. std::fill (l.begin(), l.end(), 0.0f);
  2317. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2318. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2319. CHECK (l[39999] == 0.0f && sampler->idle());
  2320. }
  2321. // FL's native sampler voice (REA_rodada76): FL's own command-line renders of crafted projects (one sampler channel,
  2322. // 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
  2323. // note pan/velocity set; the 12 dB LP with volume, cutoff and pitch envelopes and their releases. FL's int16 codes at
  2324. // a few frames; LYNX within one code. Also the velocity law and the pan law with the channel's levels inside the voice.
  2325. TEST (fl_native_sampler_voice_matches_fl_renders)
  2326. {
  2327. auto noise = std::make_shared<lynx::SampleData>();
  2328. noise->sampleRate = 44100.0;
  2329. {
  2330. std::uint32_t seed = 1;
  2331. for (int i = 0; i < 26460; ++i)
  2332. {
  2333. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  2334. noise->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  2335. }
  2336. }
  2337. struct Ev { int on, off; float vel, pan; };
  2338. auto render = [&] (std::vector<std::int32_t> recs, std::array<int, 3> filter, const std::vector<Ev>& notes,
  2339. std::shared_ptr<lynx::SampleData> data, float chanGain) {
  2340. lynx::ChannelDef c;
  2341. c.instrument = lynx::InstrumentKind::sampler;
  2342. c.sampler.flChannel = recs;
  2343. c.sampler.flChannel.insert (c.sampler.flChannel.end(), { filter[0], filter[1], filter[2], 96 });
  2344. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2345. lynx::setOfflineRendering (true); // FL's render dialog: 32-point sinc (the pitch envelope interpolates)
  2346. inst->prepare (44100.0);
  2347. lynx::setOfflineRendering (false);
  2348. inst->configure (c, 120.0);
  2349. inst->setSongPosition (0.0);
  2350. std::vector<std::pair<int, int>> events; // (sample, index ≥ 0 on / < 0 off)
  2351. for (int i = 0; i < (int) notes.size(); ++i)
  2352. events.push_back ({ notes[(std::size_t) i].on, i }), events.push_back ({ notes[(std::size_t) i].off, -1 - i });
  2353. std::sort (events.begin(), events.end());
  2354. std::vector<float> l (120000), r (120000);
  2355. int at = 0;
  2356. for (const auto& [when, i] : events)
  2357. {
  2358. inst->render (l.data() + at, r.data() + at, when - at, chanGain, chanGain);
  2359. at = when;
  2360. if (i >= 0)
  2361. {
  2362. inst->setNextNoteExpression (notes[(std::size_t) i].pan, 0.0f, false);
  2363. inst->noteOn (60, notes[(std::size_t) i].vel);
  2364. }
  2365. else
  2366. inst->noteOff (60);
  2367. }
  2368. inst->render (l.data() + at, r.data() + at, 120000 - at, chanGain, chanGain);
  2369. return std::make_pair (l, r);
  2370. };
  2371. const std::vector<std::int32_t> plain = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  2372. auto records = [&] (std::vector<std::int32_t> vol, std::vector<std::int32_t> cut, std::vector<std::int32_t> pitch) {
  2373. std::vector<std::int32_t> all = plain;
  2374. for (const auto* r : std::initializer_list<const std::vector<std::int32_t>*> { &vol, &cut, &plain, &pitch })
  2375. all.insert (all.end(), r->begin(), r->end());
  2376. return all;
  2377. };
  2378. 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 };
  2379. const auto chan = lynx::channelGain (10000.0f / 12800.0f); // the rack's gain at pan 0 (panGains: gain·√2·cos(π/4))
  2380. const int at[] = { 5, 300, 2000, 9000, 11500, 15000, 22100, 26000 };
  2381. auto check = [&] (const std::pair<std::vector<float>, std::vector<float>>& out, const int* goldL, const int* goldR) {
  2382. int bad = 0;
  2383. for (int n = 0; n < 3; ++n)
  2384. for (int k = 0; k < 8; ++k)
  2385. {
  2386. const auto p = (std::size_t) (44100 * n + at[k]);
  2387. bad += std::abs ((int) std::lround (out.first[p] * 32767.0f) - goldL[8 * n + k]) > 1;
  2388. bad += std::abs ((int) std::lround (out.second[p] * 32767.0f) - goldR[8 * n + k]) > 1;
  2389. }
  2390. return bad;
  2391. };
  2392. {
  2393. // fast LP (Mod X 100, Mod Y 200); notes: len 96, (vel 80, pan 40) len 48, (vel 120, pan 90) len 144
  2394. const auto out = render (records (volPlain, plain, plain), { 100, 200, 0 },
  2395. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -24.0f / 64 }, { 88200, 121275, 120.0f / 128, 26.0f / 64 } },
  2396. noise, chan);
  2397. 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 };
  2398. 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 };
  2399. CHECK (check (out, L, R) == 0);
  2400. }
  2401. {
  2402. // 12 dB LP (Mod X 90, Mod Y 60), volume / cutoff / pitch envelopes; notes len 48, (vel 80, pan 30) len 48, len 30
  2403. const auto out = render (records ({ 4, 1, 100, 3000, 20000, 30000, 100, 6000, 0, 100, 20000, 0, 32950, 0, 0, 0, -101 },
  2404. { 0, 1, 100, 6000, 2000, 15000, 40, 8000, 100, 100, 20000, 0, 32950, 0, 0, 0, 0 },
  2405. { 0, 1, 100, 4000, 20000, 9000, 0, 20000, 600, 100, 20000, 0, 32950, 0, 0, 0, 0 }),
  2406. { 90, 60, 1 },
  2407. { { 0, 11025, 100.0f / 128, 0.0f }, { 44100, 55125, 80.0f / 128, -34.0f / 64 }, { 88200, 95091, 100.0f / 128, 0.0f } },
  2408. noise, chan);
  2409. 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 };
  2410. 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 };
  2411. CHECK (check (out, L, R) == 0);
  2412. }
  2413. {
  2414. // a global volume LFO (flag 0x20, amount 80, speed 24000): one phase for the channel, (k + 1)·increment at song
  2415. // tick k (notes on ticks 0, 100, 300)
  2416. auto vol = volPlain;
  2417. vol[0] = 4 | 0x20, vol[11] = 80, vol[12] = 24000;
  2418. const auto out = render (records (vol, plain, plain), { 256, 0, 0 },
  2419. { { 0, 22050, 100.0f / 128, 0.0f }, { 22969, 45019, 80.0f / 128, 0.0f }, { 68906, 90956, 100.0f / 128, 0.0f } },
  2420. noise, chan);
  2421. const int pos[] = { 500, 3000, 7000, 12000, 23469, 26000, 30000, 69406, 72000, 80000 };
  2422. const int gold[] = { 1149, 765, 374, 4530, 846, -911, -646, 1160, 2316, 727 };
  2423. int bad = 0;
  2424. for (int k = 0; k < 10; ++k)
  2425. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2426. CHECK (bad == 0);
  2427. }
  2428. {
  2429. // a sample with its own loop (WAV smpl 4410…8819): FL loops it while the note lasts and fades it out over 10 ms
  2430. // after the note-off (no volume envelope)
  2431. auto looped = std::make_shared<lynx::SampleData>();
  2432. looped->sampleRate = 44100.0;
  2433. for (int i = 0; i < 8820; ++i)
  2434. 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)));
  2435. looped->loopStart = 4410, looped->loopEnd = 8820;
  2436. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 },
  2437. { { 0, 22050, 100.0f / 128, 0.0f }, { 44100, 49612, 100.0f / 128, 0.0f } }, looped, chan);
  2438. const int pos[] = { 125, 4525, 8925, 13337, 17725, 22025, 22125, 22325, 22437, 49625, 49737, 50025 };
  2439. const int gold[] = { 6338, 4746, 3842, 4161, -1470, -989, -3577, -1174, -32, 4373, 2573, 42 };
  2440. int bad = 0;
  2441. for (int k = 0; k < 12; ++k)
  2442. bad += std::abs ((int) std::lround (out.first[(std::size_t) pos[k]] * 32767.0f) - gold[k]) > 1;
  2443. CHECK (bad == 0);
  2444. }
  2445. {
  2446. // a constant sample: velocity law (FL: vel 100 → 0.389608, 128 → 0.799753, 64 → 0.141805 at the default
  2447. // channel volume, pan centre) and a hard-left note (0.550997 / 0)
  2448. auto dc = std::make_shared<lynx::SampleData>();
  2449. dc->sampleRate = 44100.0;
  2450. dc->left.assign (20000, 0.5f);
  2451. auto level = [&] (float vel, float pan, bool right) {
  2452. const auto out = render (records (volPlain, plain, plain), { 256, 0, 0 }, { { 0, 10000, vel, pan } }, dc, chan);
  2453. return (right ? out.second : out.first)[5000] / 0.5f;
  2454. };
  2455. CHECK (std::abs (level (100.0f / 128, 0.0f, false) - 0.389608f) < 3e-5f);
  2456. CHECK (std::abs (level (1.0f, 0.0f, false) - 0.799753f) < 3e-5f);
  2457. CHECK (std::abs (level (64.0f / 128, 0.0f, false) - 0.141805f) < 3e-5f);
  2458. CHECK (std::abs (level (100.0f / 128, -1.0f, false) - 0.550997f) < 3e-5f);
  2459. CHECK (std::abs (level (100.0f / 128, -1.0f, true)) < 1e-6f);
  2460. }
  2461. }
  2462. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2463. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2464. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2465. {
  2466. auto level = [] (float v) {
  2467. auto d = std::make_shared<lynx::SampleData>();
  2468. d->sampleRate = 48000.0;
  2469. d->left.assign (4800, v);
  2470. return d;
  2471. };
  2472. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2473. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2474. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2475. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2476. inst->prepare (48000.0);
  2477. inst->configure (lynx::ChannelDef {}, 120.0);
  2478. auto play = [&] (float velocity) {
  2479. inst->allNotesOff();
  2480. std::vector<float> l (2000), r (2000);
  2481. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2482. std::fill (l.begin(), l.end(), 0.0f);
  2483. inst->noteOn (60, velocity);
  2484. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2485. return l[5] / velocity;
  2486. };
  2487. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2488. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2489. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2490. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2491. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2492. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2493. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2494. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2495. const std::int32_t version = 0x00060003, volume = 16000;
  2496. std::memcpy (state.data(), &version, 4);
  2497. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2498. state.push_back (1);
  2499. state.push_back (5);
  2500. for (char ch : std::string ("x.wav"))
  2501. state.push_back ((std::uint8_t) ch);
  2502. const auto sliced = lynx::slicex (state);
  2503. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2504. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2505. }
  2506. TEST (fl_keys_state_wti_and_loader)
  2507. {
  2508. using namespace lynx::flkeys;
  2509. // state: u32 1, the wavetable name, 15 int32 knobs
  2510. State st;
  2511. st.wavetable = "Grand Piano";
  2512. for (int i = 0; i < 15; ++i)
  2513. st.knobs[(std::size_t) i] = 10 * i - 50;
  2514. const auto bytes = writeState (st);
  2515. CHECK (bytes.size() == 4 + 1 + 11 + 60);
  2516. const auto back = parseState (bytes);
  2517. CHECK (back && back->wavetable == "Grand Piano" && back->knobs == st.knobs);
  2518. // one WAV per keygroup: the file's own loop gives the default end and loop; a keygroup without a file reuses
  2519. // the previous one; a repeated file name copies the earlier keygroup; each file is followed by a guard frame
  2520. // that repeats the frame `loop` before the end (REA_rodada69 §8.2)
  2521. std::vector<std::string> names;
  2522. auto riff = parseWti ("[info]\nsamples=3\nfiletype=riff\npath=W\nvelocityfactor=2\nsamplerate=22050\n"
  2523. "[sample0]\nname=a.wav\nroot=40\nhigh=50\n[sample1]\nroot=51\nhigh=999\npos=2\nend=5\nloop=3\n"
  2524. "[sample2]\nname=A.WAV\nroot=60\nhigh=70\n", names);
  2525. CHECK (riff && names.size() == 1 && names[0] == "W\\a.wav");
  2526. CHECK (riff && riff->keygroups.size() == 3 && riff->keygroups[2].duplicateOf == 0 && riff->thresholds == std::vector<int> { 0 });
  2527. if (riff)
  2528. {
  2529. riff->files[0].loopStart = 4, riff->files[0].loopEnd = 8;
  2530. FileSamples a;
  2531. for (int i = 0; i < 10; ++i)
  2532. a.samples.push_back ((std::int16_t) (100 * (i + 1)));
  2533. const auto decoded = FlKeysTable::decode (riff->encode());
  2534. CHECK (decoded.has_value());
  2535. const auto w = buildWavetable (*decoded, { a });
  2536. CHECK (w != nullptr);
  2537. if (w != nullptr)
  2538. {
  2539. 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; };
  2540. CHECK (same (w->keygroups[0], { 40, 50, 0, 8, 5 }));
  2541. CHECK (same (w->keygroups[1], { 51, 999, 2, 5, 3 }));
  2542. CHECK (same (w->keygroups[2], { 60, 70, 0, 8, 5 }));
  2543. CHECK (w->data[9] == 400 && w->data[10] == 0 && w->data[8] == 900);
  2544. CHECK (w->sampleRate == 22050.0f && w->velocityFactor == 2.0f && ! w->stereo);
  2545. }
  2546. }
  2547. // one raw file: every keygroup's part is copied one after another with a guard sample; "canloop=false" makes the
  2548. // loop as long as the keygroup's end, so the voice stops there
  2549. auto raw = parseWti ("[info]\nsamples=2\nsamplefile=x.dat\nfiletype=raw\n[sample0]\nroot=36\nhigh=47\npos=0\nend=3\nloop=2\n"
  2550. "[sample1]\nroot=48\nhigh=999\npos=4\nend=7\nloop=4\ncanloop=false\n", names);
  2551. CHECK (raw && raw->raw && names.size() == 1 && names[0] == "x.dat");
  2552. if (raw)
  2553. {
  2554. FileSamples x;
  2555. for (int i = 0; i < 10; ++i)
  2556. x.samples.push_back ((std::int16_t) (10 * (i + 1)));
  2557. const auto w = buildWavetable (*raw, { x });
  2558. CHECK (w != nullptr);
  2559. if (w != nullptr)
  2560. {
  2561. CHECK (w->keygroups[0].pos == 0 && w->keygroups[0].end == 3 && w->keygroups[0].loop == 2);
  2562. CHECK (w->keygroups[1].pos == 5 && w->keygroups[1].end == 8 && w->keygroups[1].loop == 8);
  2563. const std::vector<std::int16_t> expected { 10, 20, 30, 40, 30, 50, 60, 70, 80, 50, 0, 0 };
  2564. CHECK (std::equal (expected.begin(), expected.end(), w->data.begin()));
  2565. // a held note on the non-looping keygroup ends by itself; 33 notes leave 32 sounding (FL's MaxPoly)
  2566. State s2;
  2567. s2.knobs = { -1000, 0, 0, -1000, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2568. RefNote n;
  2569. n.key = 60;
  2570. const auto out = renderReference (*w, s2, { n }, 44100.0, 4096, 512, true);
  2571. double early = 0, late = 0;
  2572. for (int i = 0; i < 64; ++i)
  2573. early += std::fabs (out[(std::size_t) (2 * i)]);
  2574. for (int i = 2048; i < 4096; ++i)
  2575. late += std::fabs (out[(std::size_t) (2 * i)]);
  2576. CHECK (early > 0.0 && late == 0.0);
  2577. }
  2578. }
  2579. // instrument front-end without a wavetable: silent and idle
  2580. auto inst = makeFlKeysInstrument (nullptr);
  2581. inst->prepare (44100.0);
  2582. inst->noteOn (60, 0.78f);
  2583. std::vector<float> l (256), r (256);
  2584. inst->render (l.data(), r.data(), 256, 1.0f, 1.0f);
  2585. CHECK (inst->idle() && l[100] == 0.0f);
  2586. }
  2587. TEST (ogg_vorbis_decodes_like_fl)
  2588. {
  2589. using namespace lynx::oggvorbis;
  2590. 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 };
  2591. CHECK (isOggWav (wavHeader, sizeof wavHeader));
  2592. CHECK (! isOggStream (wavHeader, sizeof wavHeader));
  2593. Decoded none;
  2594. CHECK (! decode (wavHeader, sizeof wavHeader, none) && none.channels == 0);
  2595. // FNV-1a 64 of the planar float32 samples FL's oggio_x64.dll decodes from installation files (REA_rodada77 §3):
  2596. // a plain file, one with a second (PCM) stream multiplexed in, one where an empty stream's end-of-stream page
  2597. // ends the decode early (FL keeps 17 728 of 20 691 frames), and an FL Keys wavetable
  2598. if (realFlpDir.empty())
  2599. return;
  2600. const auto root = std::filesystem::path (realFlpDir).parent_path();
  2601. const struct { const char* file; std::uint64_t hash; std::size_t frames; int channels; } cases[] = {
  2602. { "Data/Patches/Packs/Drums/Percussion/FPC Wood Hit 2.wav", 0xe6137a6141cd5946ull, 29664, 1 },
  2603. { "Data/Patches/Packs/Instruments/Keyboard/Grand/FL Piano (11).wav", 0x3f60df388ac0c636ull, 264600, 2 },
  2604. { "Data/Patches/Packs/Drums/Percussion/Grv Perc 08.wav", 0x7f235f467e90efe0ull, 17728, 0 },
  2605. { "Plugins/Fruity/Generators/FL Keys/Wavtables/Grand Piano/CHANGPIANOHARD_A8.wav", 0x00bbf4cc42f5f72cull, 153280, 0 },
  2606. };
  2607. for (const auto& c : cases)
  2608. {
  2609. const auto path = root / c.file;
  2610. if (! std::filesystem::exists (path))
  2611. continue;
  2612. std::ifstream in (path, std::ios::binary);
  2613. const std::vector<std::uint8_t> bytes { std::istreambuf_iterator<char> (in), {} };
  2614. CHECK (isOggWav (bytes.data(), bytes.size()));
  2615. Decoded d;
  2616. CHECK (decode (bytes.data(), bytes.size(), d));
  2617. if (d.channels == 0)
  2618. continue;
  2619. std::uint64_t h = 1469598103934665603ULL;
  2620. for (const auto& ch : d.data)
  2621. {
  2622. const auto* p = reinterpret_cast<const std::uint8_t*> (ch.data());
  2623. for (std::size_t i = 0; i < ch.size() * sizeof (float); ++i)
  2624. h = (h ^ p[i]) * 1099511628211ULL;
  2625. }
  2626. CHECK (d.data[0].size() == c.frames && d.sampleRate == 44100);
  2627. CHECK (c.channels == 0 || d.channels == c.channels);
  2628. CHECK (h == c.hash);
  2629. }
  2630. }
  2631. TEST (directwave_crt_math_and_state)
  2632. {
  2633. using namespace lynx::dw;
  2634. auto f = [] (std::uint32_t b) { float x; std::memcpy (&x, &b, 4); return x; };
  2635. auto u = [] (float x) { std::uint32_t b; std::memcpy (&b, &x, 4); return b; };
  2636. // the plugin's own run-time (r72/run/mt.exe on DirectWave_x64.dll): values where the libm result differs
  2637. CHECK (u (sinfCrt (f (0x40b1a97fu))) == 0xbf2af4bbu);
  2638. CHECK (u (sinfCrt (f (0xc0ad8469u))) == 0x3f4222f6u);
  2639. CHECK (u (sinfCrt (f (0xc016e8c7u))) == 0xbf34b32du);
  2640. CHECK (u (cosfCrt (f (0xc0af1cc0u))) == 0x3f305702u);
  2641. CHECK (u (cosfCrt (f (0x3f15cba3u))) == 0x3f556924u);
  2642. CHECK (u (cosfCrt (f (0xc0aeb047u))) == 0x3f2dddfeu);
  2643. CHECK (u (expfCrt (f (0xbfb679c0u))) == 0x3e7622a1u);
  2644. CHECK (encodeFormats ({ true, false }) == std::vector<float> ({ 1.0f, 2.0f, 1.0f, 0.0f }));
  2645. CHECK (decodeFormats ({ 1.0f, 2.0f, 1.0f, 0.0f }) == std::vector<bool> ({ true, false }));
  2646. CHECK (! parseState ({ 1, 2, 3 }).has_value());
  2647. }
  2648. TEST (directwave_matches_fl_with_demo_programs)
  2649. {
  2650. // Demo programs (voices, mod matrix, filters, chorus, reverb, shaper) with our own 16-bit samples in every zone:
  2651. // FNV-1a 64 of DirectWave_x64.dll's output (r72/py/synth.py + cmp.py: 44.1 kHz, host blocks of 512, tempo 120)
  2652. if (realFlpDir.empty())
  2653. return;
  2654. using namespace lynx::dw;
  2655. auto readAll = [] (const std::filesystem::path& p) {
  2656. std::ifstream in (p, std::ios::binary);
  2657. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (in), {} };
  2658. };
  2659. auto stateOf = [&] (const std::string& file, const std::string& channel) {
  2660. const auto path = std::filesystem::path (realFlpDir) / file;
  2661. if (! std::filesystem::exists (path))
  2662. return std::vector<std::uint8_t>();
  2663. const auto p = flp::parseProject (readAll (path));
  2664. for (const auto& c : p.channels)
  2665. if (c.pluginName == "DirectWave" && c.name == channel)
  2666. return c.pluginState;
  2667. return std::vector<std::uint8_t>();
  2668. };
  2669. auto sampleValue = [] (int z, std::int64_t j) {
  2670. const auto v = (j * (13 + z * 7)) % 4096 - 2048;
  2671. const auto w = ((j * j + z * 31) % 997) - 498;
  2672. return (std::int16_t) std::clamp<std::int64_t> (v * 6 + w * 8, -32768, 32767);
  2673. };
  2674. std::vector<RefNote> notes;
  2675. auto note = [&] (int start, int key, double vel, double fv, double fp, double cents) {
  2676. RefNote n; n.start = start; n.key = key; n.velocity = vel; n.finalVol = fv; n.finalPan = fp; n.cents = cents;
  2677. notes.push_back (n);
  2678. };
  2679. 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);
  2680. note (44100, 36, 0.3, 0.8, 0.8, -40); note (80000, 72, 0.9, 0.6, -0.6, 0);
  2681. notes[0].release = 40000; notes[1].release = 70000; notes[2].release = 60000;
  2682. auto run = [&] (const std::string& file, const std::string& channel, std::uint64_t expected) {
  2683. const auto st = parseState (stateOf (file, channel));
  2684. if (! st)
  2685. return;
  2686. const auto* prog = playedProgram (*st);
  2687. CHECK (prog != nullptr);
  2688. if (prog == nullptr)
  2689. return;
  2690. std::vector<std::shared_ptr<const ZoneSamples>> zs;
  2691. for (int z = 0; z < (int) prog->zones.size(); ++z)
  2692. {
  2693. auto s = std::make_shared<ZoneSamples>();
  2694. s->bits = 16;
  2695. s->channels = 1;
  2696. s->frames = prog->zones[(std::size_t) z].frames;
  2697. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  2698. for (std::int64_t j = 0; j < s->frames; ++j)
  2699. v[(std::size_t) j + 256] = sampleValue (z, j);
  2700. s->i16.push_back (std::move (v));
  2701. zs.push_back (s);
  2702. }
  2703. EngineOptions o;
  2704. o.velocityAsInitVol = (st->flags & 2) != 0;
  2705. o.noGainClamp = (st->flags & 1) != 0;
  2706. const auto out = renderReference (*prog, zs, o, notes, 44100.0, 120000, 512, 1);
  2707. std::uint64_t h = 14695981039346656037ull;
  2708. for (const float x : out)
  2709. {
  2710. std::uint32_t b;
  2711. std::memcpy (&b, &x, 4);
  2712. for (int k = 0; k < 4; ++k)
  2713. h = (h ^ ((b >> (8 * k)) & 0xffu)) * 1099511628211ull;
  2714. }
  2715. CHECK (h == expected);
  2716. if (h != expected)
  2717. std::printf (" DirectWave %s: %016llx\n", channel.c_str(), (unsigned long long) h);
  2718. };
  2719. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Gtr", 0xa1de3dd6be53d665ull);
  2720. run ("Demo projects/Visual/ZGameEditor Visualizer/Treben - Umziky - All the same.flp", "Rhodes", 0xcd458ec6a857f9c7ull);
  2721. run ("System/Render tests/Directwave.flp", "DW", 0x95b4c2e29b664dd1ull);
  2722. }
  2723. TEST (fl_keys_matches_fl_with_the_installation)
  2724. {
  2725. // FL Keys' own output (r69/keys/kcmp3.py: the installation's DLL, FL's x87 host pan law, blocks of 512 at
  2726. // 44.1 kHz): FNV-1a 64 of the interleaved float bits of 24 000 frames
  2727. if (realFlpDir.empty())
  2728. return;
  2729. using namespace lynx::flkeys;
  2730. const auto folder = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Keys" / "Wavtables";
  2731. if (! std::filesystem::exists (folder / "mda Piano.wti"))
  2732. return;
  2733. auto readAll = [] (const std::filesystem::path& p) {
  2734. std::ifstream f (p, std::ios::binary);
  2735. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  2736. };
  2737. auto pcm = [] (const std::vector<std::uint8_t>& w) {
  2738. FileSamples f;
  2739. std::uint16_t channels = 1;
  2740. for (std::size_t pos = 12; pos + 8 <= w.size();)
  2741. {
  2742. std::uint32_t size = 0;
  2743. std::memcpy (&size, w.data() + pos + 4, 4);
  2744. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  2745. std::memcpy (&channels, w.data() + pos + 10, 2);
  2746. else if (std::memcmp (w.data() + pos, "data", 4) == 0)
  2747. {
  2748. f.samples.resize (std::min<std::size_t> (size, w.size() - pos - 8) / 2);
  2749. std::memcpy (f.samples.data(), w.data() + pos + 8, f.samples.size() * 2);
  2750. f.channels = channels;
  2751. }
  2752. pos += 8 + size + (size & 1);
  2753. }
  2754. return f;
  2755. };
  2756. auto run = [&] (const std::string& name, std::array<int, 15> knobs, std::uint64_t expected) {
  2757. const auto wti = readAll (folder / (name + ".wti"));
  2758. std::vector<std::string> names;
  2759. auto table = parseWti (std::string (wti.begin(), wti.end()), names);
  2760. CHECK (table.has_value());
  2761. if (! table)
  2762. return;
  2763. std::vector<FileSamples> files;
  2764. for (std::size_t i = 0; i < names.size(); ++i)
  2765. {
  2766. auto rel = names[i];
  2767. std::replace (rel.begin(), rel.end(), '\\', '/');
  2768. const auto bytes = readAll (folder / rel);
  2769. if (table->raw)
  2770. {
  2771. FileSamples f;
  2772. f.samples.resize (bytes.size() / 2);
  2773. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  2774. files.push_back (std::move (f));
  2775. }
  2776. else
  2777. {
  2778. table->files[i] = wavFileInfo (bytes);
  2779. files.push_back (pcm (bytes));
  2780. }
  2781. }
  2782. const auto w = buildWavetable (*table, files);
  2783. CHECK (w != nullptr);
  2784. if (w == nullptr)
  2785. return;
  2786. State s;
  2787. s.wavetable = name;
  2788. s.knobs = knobs;
  2789. auto note = [] (int start, int key, double iv, double fv, double fp, double cents) {
  2790. RefNote n;
  2791. n.start = start, n.key = key, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = cents;
  2792. return n;
  2793. };
  2794. 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),
  2795. 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) };
  2796. // (floats as the harness parses them)
  2797. for (auto& n : notes)
  2798. n.initVol = (float) n.initVol, n.finalVol = (float) n.finalVol, n.finalPan = (float) n.finalPan;
  2799. notes[0].release = 9000, notes[2].release = 12000, notes[3].release = 15000, notes[5].release = 20000;
  2800. const auto out = renderReference (*w, s, notes, 44100.0, 24000, 512, true);
  2801. std::uint64_t h = 0xcbf29ce484222325ull;
  2802. for (auto v : out)
  2803. {
  2804. std::uint32_t b;
  2805. std::memcpy (&b, &v, 4);
  2806. for (int k = 0; k < 4; ++k)
  2807. h = (h ^ ((b >> (8 * k)) & 0xff)) * 0x100000001b3ull;
  2808. }
  2809. CHECK (h == expected);
  2810. };
  2811. run ("mda Piano", { 1000, 138, 388, -1000, 716, 0, -1000, 256, 0, 0, 0, 0, 0, 0, 0 }, 0x78268ec007f31309ull);
  2812. run ("Rhodes", { 200, 300, 500, 800, 300, 1500, 300, 1500, 300, 400, 400, 800, 900, 30000, 700 }, 0xcc91c93d917f1d31ull);
  2813. run ("Roto Organ", { 1000, 1000, 1000, 1000, 0, 0, -1000, 0, 1000, 0, 500, 1000, 1000, 0, 300 }, 0x99eb13e87640a1c6ull);
  2814. }
  2815. TEST (sampler_plays_pitched_one_shot)
  2816. {
  2817. // 1000-frame ramp sample: the position can be measured from the amplitude
  2818. auto data = std::make_shared<lynx::SampleData>();
  2819. data->sampleRate = 48000.0;
  2820. for (int i = 0; i < 1000; ++i)
  2821. data->left.push_back (1.0f);
  2822. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2823. sampler->prepare (48000.0);
  2824. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  2825. sampler->noteOn (60, 1.0f);
  2826. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  2827. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2828. int sounding = 0;
  2829. for (auto s : l)
  2830. sounding += s > 0.5f;
  2831. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  2832. std::fill (l.begin(), l.end(), 0.0f);
  2833. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  2834. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2835. sounding = 0;
  2836. for (auto s : l)
  2837. sounding += s > 0.5f;
  2838. CHECK (sounding >= 495 && sounding <= 500);
  2839. CHECK (r[10] > 0.5f); // mono goes to both sides
  2840. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  2841. silent->prepare (48000.0);
  2842. silent->noteOn (60, 1.0f);
  2843. std::fill (l.begin(), l.end(), 0.0f);
  2844. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  2845. CHECK (l[0] == 0.0f);
  2846. }
  2847. TEST (rack_live_notes_and_sample_provider)
  2848. {
  2849. lynx::Song song;
  2850. lynx::ChannelDef c;
  2851. c.name = "Smp";
  2852. c.instrument = lynx::InstrumentKind::sampler;
  2853. c.samplePath = "a.wav";
  2854. song.channels.push_back (c);
  2855. song.patterns.emplace_back();
  2856. int loads = 0;
  2857. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  2858. ++loads;
  2859. CHECK (path == "a.wav");
  2860. auto d = std::make_shared<lynx::SampleData>();
  2861. d->left.assign (4800, 0.5f);
  2862. return d;
  2863. };
  2864. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  2865. CHECK (loads == 1);
  2866. lynx::RackRenderer renderer;
  2867. renderer.prepare (48000.0);
  2868. lynx::LiveEventQueue live;
  2869. lynx::NoteEvent on;
  2870. on.channel = 0;
  2871. on.velocity = 1.0f;
  2872. live.push (on);
  2873. std::vector<float> l (256), r (256);
  2874. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  2875. CHECK (l[0] > 0.1f);
  2876. // same sample: reused; different sample: reloaded
  2877. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  2878. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  2879. song.channels[0].samplePath = "b.wav";
  2880. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  2881. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  2882. }
  2883. TEST (flp_paths_resolve_and_samplers_import)
  2884. {
  2885. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  2886. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  2887. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  2888. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  2889. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  2890. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  2891. flp::Project p;
  2892. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  2893. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  2894. lynx::FlpImportOptions options;
  2895. options.roots = roots;
  2896. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  2897. const auto s = lynx::importFlp (p, options);
  2898. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  2899. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  2900. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  2901. }
  2902. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  2903. {
  2904. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  2905. lynx::softClip (s, 5);
  2906. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  2907. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  2908. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  2909. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  2910. }
  2911. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  2912. {
  2913. lynx::Song s;
  2914. s.channels.resize (1);
  2915. auto& p = s.patterns.emplace_back();
  2916. for (int step : { 0, 4, 8, 12 }) // one note per beat
  2917. p.toggleStep (0, step);
  2918. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  2919. lynx::Clip c;
  2920. c.pattern = 0;
  2921. c.start = 4 * lynx::ppq;
  2922. c.length = 2 * lynx::ppq;
  2923. c.offset = 2 * lynx::ppq;
  2924. s.playlist.push_back (c);
  2925. CHECK (s.songLength() == 8 * lynx::ppq);
  2926. const double sr = 48000.0;
  2927. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  2928. lynx::EventBuffer ev;
  2929. // whole song in a single block (8 beats = 192000 samples)
  2930. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2931. std::vector<int> ons;
  2932. for (auto& e : ev)
  2933. if (e.type == lynx::NoteEvent::Type::on)
  2934. ons.push_back (e.sampleOffset);
  2935. CHECK (ons.size() == 2);
  2936. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  2937. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  2938. ev.clear();
  2939. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  2940. CHECK (ev.size() == 0);
  2941. // a note longer than the clip is cut at the clip end
  2942. p.notes[3].length = 4 * lynx::ppq;
  2943. ev.clear();
  2944. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2945. int lastOff = -1;
  2946. for (auto& e : ev)
  2947. if (e.type == lynx::NoteEvent::Type::off)
  2948. lastOff = std::max (lastOff, e.sampleOffset);
  2949. CHECK (lastOff == 6 * 24000);
  2950. }
  2951. TEST (remove_pattern_fixes_clips)
  2952. {
  2953. lynx::Song s;
  2954. s.patterns.resize (3);
  2955. s.currentPattern = 2;
  2956. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  2957. s.removePattern (1);
  2958. CHECK (s.patterns.size() == 2);
  2959. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  2960. CHECK (s.currentPattern == 1);
  2961. }
  2962. TEST (flp_import_3x_osc_state_and_channel_filter)
  2963. {
  2964. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  2965. std::vector<std::uint8_t> blob (460, 0);
  2966. auto put = [&] (std::size_t off, std::int32_t v) {
  2967. for (int b = 0; b < 4; ++b)
  2968. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  2969. };
  2970. put (0x00, 14);
  2971. put (0x08, 3); // osc 1 saw
  2972. put (0x10, 7); // osc 1 fine +7
  2973. put (0x04, -32); // osc 1 pan -0.5
  2974. put (0x1C, 64); // osc 2 mix
  2975. put (0x28, -12); // osc 2 coarse
  2976. put (0x38, 32); // osc 3 mix
  2977. put (0x40, 2); // osc 3 square
  2978. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  2979. lynx::SynthSettings s;
  2980. CHECK (lynx::synthFrom3xOsc (blob, s));
  2981. 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);
  2982. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  2983. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  2984. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  2985. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  2986. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  2987. float hz = 0.0f, res = 1.0f;
  2988. int mode = -1;
  2989. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  2990. CHECK (hz >= 19999.0f && res == 0.0f);
  2991. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  2992. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  2993. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  2994. CHECK (hz > 240.0f && hz < 320.0f);
  2995. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  2996. CHECK (res > 0.8f);
  2997. CHECK (mode == 0);
  2998. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  2999. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  3000. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  3001. CHECK (mode == 1);
  3002. }
  3003. TEST (synth_pitch_and_filter_envelopes)
  3004. {
  3005. auto render = [] (const lynx::SynthSettings& s, int frames) {
  3006. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3007. inst->prepare (48000.0);
  3008. lynx::ChannelDef c;
  3009. c.synth = s;
  3010. inst->configure (c, 120.0);
  3011. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  3012. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3013. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  3014. return l;
  3015. };
  3016. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  3017. int n = 0;
  3018. for (int i = from + 1; i < to; ++i)
  3019. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  3020. return n;
  3021. };
  3022. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  3023. lynx::SynthSettings kick;
  3024. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3025. kick.cutoff = 20000.0f;
  3026. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  3027. const auto k = render (kick, 48000);
  3028. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  3029. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  3030. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  3031. CHECK (std::abs (late - 110) < 4);
  3032. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  3033. lynx::SynthSettings pluck;
  3034. pluck.cutoff = 200.0f;
  3035. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  3036. const auto p = render (pluck, 48000);
  3037. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  3038. double e = 0.0;
  3039. for (int i = from + 1; i < to; ++i)
  3040. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  3041. return e;
  3042. };
  3043. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  3044. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  3045. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  3046. lynx::SynthSettings toxic = pluck;
  3047. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  3048. const auto t = render (toxic, 48000);
  3049. auto thf = [&] (int from, int to) {
  3050. double e = 0.0;
  3051. for (int i = from + 1; i < to; ++i)
  3052. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  3053. return e;
  3054. };
  3055. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  3056. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  3057. }
  3058. TEST (synth_mono_reuses_the_sounding_voice)
  3059. {
  3060. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3061. inst->prepare (48000.0);
  3062. lynx::ChannelDef c;
  3063. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3064. c.synth.cutoff = 20000.0f;
  3065. c.synth.mono = true;
  3066. inst->configure (c, 120.0);
  3067. std::vector<float> l (48000), r (48000);
  3068. inst->noteOn (57, 1.0f);
  3069. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3070. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  3071. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  3072. int crossings = 0;
  3073. float peak = 0.0f;
  3074. for (std::size_t i = 24001; i < 48000; ++i)
  3075. {
  3076. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3077. peak = std::max (peak, std::abs (l[i]));
  3078. }
  3079. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  3080. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  3081. // no click on the handover: the jump between neighbouring samples stays sine-sized
  3082. float jump = 0.0f;
  3083. for (std::size_t i = 4790; i < 4810; ++i)
  3084. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  3085. CHECK (jump < 0.05f);
  3086. }
  3087. TEST (synth_porta_glides_and_am_modulates)
  3088. {
  3089. auto make = [] (const lynx::SynthSettings& s) {
  3090. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3091. inst->prepare (48000.0);
  3092. lynx::ChannelDef c;
  3093. c.synth = s;
  3094. inst->configure (c, 120.0);
  3095. return inst;
  3096. };
  3097. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  3098. int n = 0;
  3099. for (auto i = from + 1; i < to; ++i)
  3100. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  3101. return n;
  3102. };
  3103. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  3104. lynx::SynthSettings s;
  3105. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3106. s.cutoff = 20000.0f;
  3107. s.mono = true;
  3108. s.porta = true;
  3109. s.portaBeats = 1.0f;
  3110. auto inst = make (s);
  3111. std::vector<float> l (96000), r (96000);
  3112. inst->noteOn (57, 1.0f);
  3113. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3114. inst->noteOn (69, 1.0f);
  3115. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  3116. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  3117. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  3118. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  3119. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  3120. lynx::SynthSettings am;
  3121. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3122. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  3123. am.cutoff = 20000.0f;
  3124. am.am = true;
  3125. auto modulated = make (am);
  3126. modulated->noteOn (69, 1.0f);
  3127. std::fill (l.begin(), l.end(), 0.0f);
  3128. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3129. float peak = 0.0f;
  3130. for (std::size_t i = 4800; i < 48000; ++i)
  3131. peak = std::max (peak, std::abs (l[i]));
  3132. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  3133. }
  3134. TEST (channel_lfo_vibrato_and_tremolo)
  3135. {
  3136. auto render = [] (const lynx::SynthSettings& s) {
  3137. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3138. inst->prepare (48000.0);
  3139. lynx::ChannelDef c;
  3140. c.synth = s;
  3141. inst->configure (c, 120.0);
  3142. inst->noteOn (69, 1.0f);
  3143. std::vector<float> l (48000), r (48000);
  3144. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3145. return l;
  3146. };
  3147. lynx::SynthSettings s;
  3148. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3149. s.cutoff = 20000.0f;
  3150. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  3151. auto tremolo = s;
  3152. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  3153. const auto t = render (tremolo);
  3154. float lowest = 1.0f, highest = 0.0f;
  3155. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  3156. {
  3157. float peak = 0.0f;
  3158. for (auto i = w; i < w + 480; ++i)
  3159. peak = std::max (peak, std::abs (t[i]));
  3160. lowest = std::min (lowest, peak);
  3161. highest = std::max (highest, peak);
  3162. }
  3163. CHECK (lowest < 0.08f && highest > 0.5f);
  3164. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  3165. auto vibrato = s;
  3166. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  3167. const auto v = render (vibrato);
  3168. int crossings = 0;
  3169. for (std::size_t i = 1; i < 48000; ++i)
  3170. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3171. CHECK (std::abs (crossings - 880) < 12);
  3172. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  3173. for (std::size_t i = 1; i < 6000; ++i)
  3174. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  3175. CHECK (firstQuarter > 112);
  3176. }
  3177. TEST (sampler_porta_glides_from_previous_note)
  3178. {
  3179. auto data = std::make_shared<lynx::SampleData>();
  3180. data->sampleRate = 48000.0;
  3181. data->left.resize (96000);
  3182. for (std::size_t i = 0; i < data->left.size(); ++i)
  3183. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  3184. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3185. inst->prepare (48000.0);
  3186. lynx::ChannelDef c;
  3187. c.sampler.cut = true;
  3188. c.sampler.porta = true;
  3189. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  3190. inst->configure (c, 120.0);
  3191. std::vector<float> l (24000), r (24000);
  3192. inst->noteOn (60, 1.0f);
  3193. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  3194. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  3195. std::fill (l.begin(), l.end(), 0.0f);
  3196. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3197. auto crossings = [&] (std::size_t from, std::size_t to) {
  3198. int n = 0;
  3199. for (auto i = from + 1; i < to; ++i)
  3200. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3201. return n;
  3202. };
  3203. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  3204. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  3205. CHECK (start < 22);
  3206. CHECK (end > 36);
  3207. }
  3208. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  3209. {
  3210. auto sine = [] (double hz, float amp) {
  3211. auto d = std::make_shared<lynx::SampleData>();
  3212. d->sampleRate = 48000.0;
  3213. d->left.resize (48000);
  3214. for (std::size_t i = 0; i < d->left.size(); ++i)
  3215. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  3216. return d;
  3217. };
  3218. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  3219. // hard layer on the highs: same tuning, louder
  3220. std::vector<lynx::LoadedZone> zones {
  3221. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  3222. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  3223. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  3224. };
  3225. auto inst = lynx::makeZonedSampler (zones);
  3226. inst->prepare (48000.0);
  3227. inst->configure (lynx::ChannelDef {}, 120.0);
  3228. auto play = [&] (int key, float velocity) {
  3229. inst->allNotesOff();
  3230. std::vector<float> l (24000), r (24000);
  3231. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  3232. std::fill (l.begin(), l.end(), 0.0f);
  3233. inst->noteOn (key, velocity);
  3234. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3235. int crossings = 0;
  3236. float peak = 0.0f;
  3237. for (std::size_t i = 1; i < 24000; ++i)
  3238. {
  3239. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  3240. peak = std::max (peak, std::abs (l[i]));
  3241. }
  3242. return std::pair { crossings, peak };
  3243. };
  3244. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  3245. CHECK (std::abs (low.first - 440) < 4);
  3246. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  3247. CHECK (std::abs (soft.first - 880) < 6);
  3248. const auto hard = play (81, 0.95f); // high velocity: hard layer
  3249. CHECK (hard.second > soft.second * 2.0f);
  3250. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  3251. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  3252. rr->prepare (48000.0);
  3253. rr->configure (lynx::ChannelDef {}, 120.0);
  3254. float peaks[2] {};
  3255. for (int k = 0; k < 2; ++k)
  3256. {
  3257. rr->allNotesOff();
  3258. std::vector<float> l (4800), r (4800);
  3259. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  3260. std::fill (l.begin(), l.end(), 0.0f);
  3261. rr->noteOn (60, 1.0f);
  3262. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3263. for (auto v : l)
  3264. peaks[k] = std::max (peaks[k], std::abs (v));
  3265. }
  3266. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  3267. }
  3268. TEST (zoned_sampler_loops_while_held)
  3269. {
  3270. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  3271. // on release, it leaves the loop and ends
  3272. auto data = std::make_shared<lynx::SampleData>();
  3273. data->sampleRate = 48000.0;
  3274. data->left.resize (4800);
  3275. for (std::size_t i = 0; i < data->left.size(); ++i)
  3276. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  3277. lynx::SampleZone zone { "loop.wav", 60 };
  3278. zone.loopStart = 1000;
  3279. zone.loopEnd = 3000;
  3280. zone.loopMode = 2;
  3281. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  3282. inst->prepare (48000.0);
  3283. lynx::ChannelDef c;
  3284. c.sampler.release = 0.01f;
  3285. inst->configure (c, 120.0);
  3286. inst->noteOn (60, 1.0f);
  3287. std::vector<float> l (48000), r (48000);
  3288. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  3289. float held = 0.0f;
  3290. for (std::size_t i = 20000; i < 24000; ++i)
  3291. held = std::max (held, std::abs (l[i]));
  3292. CHECK (held > 0.3f);
  3293. inst->noteOff (60);
  3294. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  3295. float after = 0.0f;
  3296. for (std::size_t i = 40000; i < 48000; ++i)
  3297. after = std::max (after, std::abs (l[i]));
  3298. CHECK (after == 0.0f);
  3299. }
  3300. TEST (global_lfo_follows_song_position)
  3301. {
  3302. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  3303. // same note with a per-note LFO starts at phase 0
  3304. auto first = [] (bool global, double songTicks) {
  3305. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3306. inst->prepare (48000.0);
  3307. lynx::ChannelDef c;
  3308. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3309. c.synth.cutoff = 20000.0f;
  3310. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  3311. inst->configure (c, 120.0);
  3312. inst->setSongPosition (songTicks);
  3313. inst->noteOn (69, 1.0f);
  3314. std::vector<float> l (4800), r (4800);
  3315. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  3316. float peak = 0.0f;
  3317. for (std::size_t i = 2400; i < 4800; ++i)
  3318. peak = std::max (peak, std::abs (l[i]));
  3319. return peak;
  3320. };
  3321. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  3322. const auto globalLate = first (true, 0.6 * lynx::ppq);
  3323. const auto perNote = first (false, 0.6 * lynx::ppq);
  3324. CHECK (globalLate < 0.05f);
  3325. CHECK (perNote > 0.4f);
  3326. }
  3327. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  3328. {
  3329. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  3330. auto data = std::make_shared<lynx::SampleData>();
  3331. data->sampleRate = 48000.0;
  3332. data->left.resize (48000);
  3333. std::uint32_t seed = 7;
  3334. for (auto& v : data->left)
  3335. {
  3336. seed = seed * 1664525u + 1013904223u;
  3337. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  3338. }
  3339. data->right = data->left;
  3340. auto energy = [&] (float cutoff) {
  3341. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3342. inst->prepare (48000.0);
  3343. lynx::ChannelDef c;
  3344. c.sampler.cutoff = cutoff;
  3345. inst->configure (c, 120.0);
  3346. inst->noteOn (60, 1.0f);
  3347. std::vector<float> l (24000), r (24000);
  3348. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3349. double e = 0.0;
  3350. for (auto v : l)
  3351. e += (double) v * v;
  3352. return e;
  3353. };
  3354. const auto open = energy (20000.0f), dark = energy (300.0f);
  3355. {
  3356. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  3357. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3358. inst->prepare (48000.0);
  3359. lynx::ChannelDef hp;
  3360. hp.sampler.cutoff = 10000.0f;
  3361. hp.sampler.filterType = 2;
  3362. inst->configure (hp, 120.0);
  3363. inst->noteOn (60, 1.0f);
  3364. std::vector<float> l (24000), r (24000);
  3365. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3366. double e = 0.0, lowE = 0.0;
  3367. float lp = 0.0f;
  3368. for (auto v : l)
  3369. {
  3370. e += (double) v * v;
  3371. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  3372. lowE += (double) lp * lp;
  3373. }
  3374. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  3375. CHECK (lowE < e * 0.01);
  3376. }
  3377. CHECK (open > 100.0);
  3378. CHECK (dark < open * 0.05);
  3379. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  3380. // and ends in half the time (1 s of sample -> 0.5 s)
  3381. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  3382. inst->prepare (48000.0);
  3383. lynx::ChannelDef c;
  3384. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  3385. inst->configure (c, 120.0);
  3386. inst->noteOn (60, 1.0f);
  3387. std::vector<float> l (48000), r (48000);
  3388. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  3389. float tail = 0.0f, body = 0.0f;
  3390. for (std::size_t i = 20000; i < 23000; ++i)
  3391. body = std::max (body, std::abs (l[i]));
  3392. for (std::size_t i = 25000; i < 48000; ++i)
  3393. tail = std::max (tail, std::abs (l[i]));
  3394. CHECK (body > 0.1f);
  3395. CHECK (tail == 0.0f);
  3396. }
  3397. TEST (synth_oscillator_pan_and_semitones)
  3398. {
  3399. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  3400. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3401. synth->prepare (48000.0);
  3402. lynx::ChannelDef c;
  3403. c.synth = s;
  3404. synth->configure (c, 120.0);
  3405. synth->noteOn (69, 1.0f);
  3406. l.assign (24000, 0.0f);
  3407. r.assign (24000, 0.0f);
  3408. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  3409. };
  3410. lynx::SynthSettings s;
  3411. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  3412. s.cutoff = 20000.0f;
  3413. std::vector<float> l, r;
  3414. render (s, l, r);
  3415. float peakL = 0.0f, peakR = 0.0f;
  3416. int crossings = 0;
  3417. for (std::size_t i = 4800; i < 24000; ++i)
  3418. {
  3419. peakL = std::max (peakL, std::abs (l[i]));
  3420. peakR = std::max (peakR, std::abs (r[i]));
  3421. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  3422. }
  3423. CHECK (peakL < 1e-4f && peakR > 0.1f);
  3424. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  3425. CHECK (std::abs (crossings - 527) < 6);
  3426. }
  3427. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  3428. {
  3429. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  3430. std::vector<std::uint8_t> blob;
  3431. 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))); };
  3432. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  3433. u32 (b, id);
  3434. u32 (b, (std::uint32_t) payload.size());
  3435. u32 (b, 0);
  3436. b.insert (b.end(), payload.begin(), payload.end());
  3437. };
  3438. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  3439. std::vector<std::uint8_t> vst3State;
  3440. u32 (vst3State, 1);
  3441. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  3442. record (vst3State, 3, bytes ("COMPONENTE"));
  3443. record (vst3State, 2, bytes ("CONTROLADOR"));
  3444. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  3445. std::vector<std::uint8_t> slots;
  3446. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  3447. u32 (slots, v);
  3448. record (vst3State, 4, slots);
  3449. u32 (blob, 12);
  3450. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  3451. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  3452. std::vector<std::uint8_t> cid;
  3453. for (int i = 0; i < 16; ++i)
  3454. cid.push_back ((std::uint8_t) (0xA0 + i));
  3455. record (blob, 0x34, cid);
  3456. record (blob, 0x36, bytes ("Surge XT"));
  3457. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  3458. record (blob, 0x35, vst3State);
  3459. lynx::ForeignPlugin p;
  3460. CHECK (lynx::parseFlWrapper (blob, p));
  3461. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  3462. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  3463. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  3464. std::vector<std::uint8_t> component, controller;
  3465. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  3466. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  3467. const auto saved = lynx::flVst3ParamSlots (p.state);
  3468. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  3469. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  3470. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  3471. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  3472. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  3473. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  3474. // VST2 state (REA_rodada89 §2): chunk and raw-parameter layouts, current (−9) and older versions
  3475. {
  3476. lynx::FlVst2State st;
  3477. st.flags = 8;
  3478. st.currentProgram = 3;
  3479. st.chunk = bytes ("BANKCHUNK");
  3480. const auto withChunk = lynx::writeFlVst2State (st);
  3481. lynx::FlVst2State back;
  3482. CHECK (lynx::parseFlVst2State (withChunk, back));
  3483. CHECK (back.version == -9 && back.flags == 8 && back.currentProgram == 3 && back.chunk == bytes ("BANKCHUNK") && back.params.empty());
  3484. CHECK (! back.chunkIsProgram());
  3485. lynx::FlVst2State raw;
  3486. raw.flags = 1;
  3487. raw.params = { 0.25f, 0.5f, 1.0f };
  3488. raw.programNames = { "Init" };
  3489. const auto withParams = lynx::writeFlVst2State (raw);
  3490. CHECK (withParams.size() == 4 + 1 + 4 + 8 + 4 + 4 + 12 + 4 + 25);
  3491. CHECK (lynx::parseFlVst2State (withParams, back));
  3492. CHECK (back.chunk.empty() && back.params == raw.params && back.allPrograms() && back.programNames == std::vector<std::string> { "Init" });
  3493. // version −5: no flags, i32 size, then i32 program; −6 without "all programs": the chunk is one program
  3494. std::vector<std::uint8_t> v5;
  3495. for (auto v : { 0xFFFFFFFBu, 4u, 7u })
  3496. u32 (v5, v);
  3497. for (auto c : bytes ("ABCD"))
  3498. v5.push_back (c);
  3499. CHECK (lynx::parseFlVst2State (v5, back) && back.version == -5 && back.currentProgram == 7 && back.chunk == bytes ("ABCD"));
  3500. std::vector<std::uint8_t> v6;
  3501. u32 (v6, 0xFFFFFFFAu);
  3502. v6.push_back (0);
  3503. for (auto v : { 2u, 0u })
  3504. u32 (v6, v);
  3505. v6.push_back ('x');
  3506. v6.push_back ('y');
  3507. CHECK (lynx::parseFlVst2State (v6, back) && back.chunkIsProgram() && back.chunk == bytes ("xy"));
  3508. // nothing to restore (123456), unknown old layouts, truncated chunks
  3509. std::vector<std::uint8_t> marker;
  3510. u32 (marker, 123456u);
  3511. CHECK (! lynx::parseFlVst2State (marker, back));
  3512. auto cut = withChunk;
  3513. cut.pop_back();
  3514. CHECK (! lynx::parseFlVst2State (cut, back));
  3515. CHECK (! lynx::parseFlVst2State ({}, back));
  3516. }
  3517. // version out of range or no type: not a usable wrapper
  3518. blob[0] = 13;
  3519. CHECK (! lynx::parseFlWrapper (blob, p));
  3520. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  3521. }
  3522. TEST (schedule_index_matches_full_scan)
  3523. {
  3524. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  3525. // exactly the same events as the full scan, including in the block that wraps around
  3526. lynx::Song s = lynx::Song::makeDefault();
  3527. s.swing = 0.6f;
  3528. std::uint32_t seed = 12345;
  3529. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  3530. s.patterns.resize (3);
  3531. for (auto& p : s.patterns)
  3532. {
  3533. p.notes.clear();
  3534. for (int i = 0; i < 60; ++i)
  3535. {
  3536. lynx::Note n;
  3537. n.start = rnd (16 * lynx::ppq);
  3538. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  3539. n.channel = rnd (4);
  3540. n.key = 40 + rnd (40);
  3541. n.slide = rnd (20) == 0;
  3542. p.notes.push_back (n);
  3543. }
  3544. p.sortNotes();
  3545. }
  3546. s.playlist.clear();
  3547. for (int i = 0; i < 12; ++i)
  3548. {
  3549. lynx::Clip c;
  3550. c.pattern = rnd (3);
  3551. c.track = rnd (4);
  3552. c.start = rnd (40) * lynx::ppq;
  3553. c.length = (rnd (12) + 1) * lynx::ppq;
  3554. c.offset = rnd (4) * lynx::ppq;
  3555. s.playlist.push_back (c);
  3556. }
  3557. s.mode = lynx::PlayMode::song;
  3558. const auto index = lynx::makeScheduleIndex (s);
  3559. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  3560. const auto loop = (double) s.songLength();
  3561. int mismatches = 0, events = 0;
  3562. for (int block = 0; block < 400; ++block)
  3563. {
  3564. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  3565. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  3566. lynx::EventBuffer a, b;
  3567. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  3568. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  3569. events += a.size();
  3570. if (a.size() != b.size())
  3571. {
  3572. ++mismatches;
  3573. continue;
  3574. }
  3575. for (int i = 0; i < a.size(); ++i)
  3576. {
  3577. const auto& x = *(a.begin() + i);
  3578. const auto& y = *(b.begin() + i);
  3579. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  3580. }
  3581. }
  3582. CHECK (events > 500);
  3583. CHECK (mismatches == 0);
  3584. }
  3585. TEST (mixer_track_eq_defaults_and_boost)
  3586. {
  3587. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  3588. lynx::TrackEq eq;
  3589. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  3590. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  3591. CHECK (! eq.active());
  3592. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  3593. lynx::Song song = lynx::Song::makeDefault();
  3594. song.mixer[1].eq.gainDb[1] = 12.0f;
  3595. for (auto& c : song.channels)
  3596. c.mixerTrack = 1;
  3597. song.channels[4].instrument = lynx::InstrumentKind::synth;
  3598. song.channels[4].synth = {};
  3599. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  3600. song.channels[4].synth.cutoff = 20000.0f;
  3601. auto render = [] (const lynx::Song& s) {
  3602. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3603. lynx::RackRenderer renderer;
  3604. renderer.prepare (48000.0);
  3605. lynx::LiveEventQueue live;
  3606. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  3607. on.channel = 4;
  3608. on.key = 91;
  3609. on.velocity = 1.0f;
  3610. live.push (on);
  3611. std::vector<float> l (24000), r (24000);
  3612. for (int pos = 0; pos < 24000; pos += 480)
  3613. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  3614. float peak = 0.0f;
  3615. for (std::size_t i = 12000; i < 24000; ++i)
  3616. peak = std::max (peak, std::abs (l[i]));
  3617. return peak;
  3618. };
  3619. const auto boosted = render (song);
  3620. song.mixer[1].eq.gainDb[1] = 0.0f;
  3621. const auto flat = render (song);
  3622. CHECK (flat > 0.01f);
  3623. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  3624. }
  3625. TEST (bars_follow_time_signature_markers)
  3626. {
  3627. lynx::Song song;
  3628. song.beatsPerBar = 4;
  3629. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3630. auto bars = song.barsBetween (0, bar4 * 3);
  3631. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3632. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3633. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3634. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3635. const auto seven = (std::int64_t) lynx::ppq * 7;
  3636. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3637. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3638. CHECK (bars.size() >= 5);
  3639. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3640. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3641. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3642. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3643. // from far away: starts at the right bar without losing the numbering
  3644. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3645. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3646. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3647. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3648. }
  3649. TEST (split_drum_channel_moves_notes_to_pads)
  3650. {
  3651. lynx::Song song = lynx::Song::makeDefault();
  3652. const auto channels = (int) song.channels.size();
  3653. song.patterns[0].notes.clear();
  3654. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3655. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3656. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3657. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3658. lynx::Automation a;
  3659. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3660. song.automations.push_back (a);
  3661. std::vector<lynx::DrumPad> pads (2);
  3662. pads[0].name = "Kick";
  3663. pads[0].key = 36;
  3664. pads[0].layers = { { "kick.wav" } };
  3665. pads[1].name = "Snare";
  3666. pads[1].key = 38;
  3667. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3668. pads[1].cutGroup = 2;
  3669. pads[1].cutBy = 2;
  3670. lynx::splitDrumChannel (song, 1, pads);
  3671. CHECK ((int) song.channels.size() == channels + 1);
  3672. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  3673. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  3674. const auto& notes = song.patterns[0].notes;
  3675. CHECK (notes.size() == 3);
  3676. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  3677. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  3678. CHECK (notes[2].channel == 4); // the old channel 3
  3679. CHECK (song.automations[0].target.index == 4);
  3680. }
  3681. TEST (flp_import_brings_playlist)
  3682. {
  3683. flp::Project p;
  3684. p.header.ppq = 96;
  3685. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  3686. flp::Pattern empty;
  3687. empty.index = 1;
  3688. flp::Pattern full;
  3689. full.index = 2;
  3690. flp::Note n;
  3691. n.channel = 0;
  3692. full.notes.push_back (n);
  3693. p.patterns = { empty, full };
  3694. flp::Arrangement a;
  3695. flp::PlaylistItem item;
  3696. item.kind = flp::PlaylistItem::Kind::pattern;
  3697. item.index = 2;
  3698. item.position = 384;
  3699. item.length = 192;
  3700. item.track = 3;
  3701. item.patternStartTick = 96;
  3702. a.items.push_back (item);
  3703. item.index = 1; // empty pattern: discarded
  3704. a.items.push_back (item);
  3705. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  3706. a.items.push_back (item);
  3707. p.arrangements.push_back (a);
  3708. const auto s = lynx::importFlp (p);
  3709. CHECK (s.mode == lynx::PlayMode::song);
  3710. CHECK (s.playlist.size() == 1);
  3711. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  3712. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  3713. }
  3714. TEST (rack_releases_notes_on_position_jump)
  3715. {
  3716. lynx::Song s;
  3717. lynx::ChannelDef c;
  3718. c.instrument = lynx::InstrumentKind::synth;
  3719. s.channels.push_back (c);
  3720. auto& p = s.patterns.emplace_back();
  3721. lynx::Note n;
  3722. n.start = 0;
  3723. n.length = 4 * lynx::ppq; // long note
  3724. p.notes.push_back (n);
  3725. s.bpm = 120.0;
  3726. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3727. lynx::RackRenderer r;
  3728. r.prepare (48000.0);
  3729. std::vector<float> l (512), rr (512);
  3730. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3731. double tick = 0.0;
  3732. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  3733. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3734. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  3735. for (int i = 0; i < 40; ++i)
  3736. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  3737. float tail = 0.0f;
  3738. for (auto v : l)
  3739. tail = std::max (tail, std::abs (v));
  3740. CHECK (tail < 0.001f);
  3741. }
  3742. namespace
  3743. {
  3744. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  3745. float juce_db (float gain)
  3746. {
  3747. return 20.0f * std::log10 (gain);
  3748. }
  3749. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  3750. {
  3751. lynx::Song s;
  3752. s.mixer = lynx::Song::defaultMixer (4);
  3753. for (int i = 0; i < 2; ++i)
  3754. {
  3755. lynx::ChannelDef c;
  3756. c.instrument = lynx::InstrumentKind::sampler;
  3757. c.samplePath = "dc";
  3758. c.volume = 1.0f;
  3759. c.mixerTrack = i + 1;
  3760. s.channels.push_back (c);
  3761. }
  3762. s.patterns.emplace_back();
  3763. tweak (s);
  3764. auto provider = [] (const std::string&) {
  3765. auto d = std::make_shared<lynx::SampleData>();
  3766. d->left.assign (48000, 0.5f);
  3767. return std::shared_ptr<const lynx::SampleData> (d);
  3768. };
  3769. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3770. lynx::RackRenderer r;
  3771. r.prepare (48000.0, 256);
  3772. lynx::LiveEventQueue live;
  3773. for (int ch = 0; ch < 2; ++ch)
  3774. {
  3775. lynx::NoteEvent on;
  3776. on.channel = ch;
  3777. on.velocity = 1.0f;
  3778. live.push (on);
  3779. }
  3780. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  3781. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  3782. return std::max (std::abs (l[999]), std::abs (rr[999]));
  3783. }
  3784. } // namespace
  3785. TEST (mixer_routes_mutes_solos_and_meters)
  3786. {
  3787. // two 0.5 channels summed on the Master at 0 dB
  3788. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  3789. // insert 1 muted: only channel 2 remains
  3790. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  3791. // solo on insert 2: same
  3792. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  3793. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  3794. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  3795. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  3796. CHECK (lynx::faderGain (0.0f) == 0.0f);
  3797. // Master fader at max
  3798. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  3799. // both channels on the same insert, insert fader at 0.4
  3800. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  3801. s.channels[1].mixerTrack = 1;
  3802. s.mixer[1].volume = 0.4f;
  3803. }) - lynx::faderGain (0.4f)) < 1e-4f);
  3804. // a route outside the mixer falls to the last existing insert, without crashing
  3805. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  3806. // no mixer: everything straight to the output at 0 dB
  3807. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  3808. lynx::MeterBank meters;
  3809. renderMixerCase ([] (lynx::Song&) {}, &meters);
  3810. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
  3811. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
  3812. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  3813. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  3814. }
  3815. TEST (next_free_insert_and_flp_mixer_import)
  3816. {
  3817. auto s = lynx::Song::makeDefault();
  3818. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  3819. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  3820. flp::Project p;
  3821. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  3822. p.mixer = { { 0, "" }, { 30, "Drums" } };
  3823. const auto song = lynx::importFlp (p);
  3824. CHECK (song.channels[0].mixerTrack == 30);
  3825. CHECK (song.mixer.size() == 31);
  3826. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  3827. }
  3828. namespace
  3829. {
  3830. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  3831. int frames = 48000)
  3832. {
  3833. auto fx = lynx::makeEffect (kind);
  3834. fx->prepare (48000.0);
  3835. auto slot = lynx::makeEffectSlot (kind);
  3836. tweak (slot);
  3837. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3838. for (int i = 0; i < frames; ++i)
  3839. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  3840. for (int i = 0; i < frames; i += 480)
  3841. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  3842. float peak = 0.0f;
  3843. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  3844. {
  3845. CHECK (std::isfinite (l[(std::size_t) i]));
  3846. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3847. }
  3848. return peak;
  3849. }
  3850. } // namespace
  3851. TEST (parametric_eq_bands_and_response)
  3852. {
  3853. // default: transparent
  3854. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3855. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  3856. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  3857. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  3858. auto boost = [] (lynx::EffectSlot& s) {
  3859. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  3860. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  3861. };
  3862. boost (slot);
  3863. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  3864. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  3865. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  3866. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  3867. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  3868. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3869. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  3870. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  3871. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  3872. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  3873. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  3874. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  3875. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  3876. // round-trip conversions
  3877. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  3878. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  3879. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  3880. }
  3881. TEST (fl_parametric_eq2_state_maps_to_bands)
  3882. {
  3883. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  3884. std::vector<std::uint8_t> blob (354, 0);
  3885. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3886. put (0, 8);
  3887. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  3888. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  3889. for (int b = 0; b < 7; ++b)
  3890. param (21 + b, 0); // all off
  3891. param (21 + 0, 3); // high pass (= low cut)
  3892. param (7 + 0, hzRaw (100.0));
  3893. param (14 + 0, 40124); // default width -> Q ~0.68
  3894. param (28 + 0, -3); // Steep 8 -> 4 biquads
  3895. param (21 + 3, 6); // peaking
  3896. param (7 + 3, hzRaw (1000.0));
  3897. param (3, 600); // +6 dB
  3898. param (14 + 3, 25412);
  3899. param (35, -300); // main level −3 dB
  3900. put (0x13E, 0x7F);
  3901. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3902. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  3903. const auto& p = slot->params;
  3904. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  3905. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  3906. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  3907. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  3908. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  3909. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  3910. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  3911. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  3912. put (0x13E, 0x7F & ~(1 << 3));
  3913. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3914. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  3915. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  3916. }
  3917. TEST (reverb_low_cut_and_predelay)
  3918. {
  3919. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  3920. // 100 Hz sine barely enters the reverb
  3921. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  3922. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  3923. fx->prepare (48000.0);
  3924. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3925. slot.params[4] = 0.0f; // wet only
  3926. slot.params[3] = 1.0f;
  3927. tweak (slot);
  3928. std::vector<float> l (48000), r (48000);
  3929. for (int i = 0; i < 48000; ++i)
  3930. 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);
  3931. for (int i = 0; i < 48000; i += 480)
  3932. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  3933. return l;
  3934. };
  3935. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  3936. float early = 0.0f, later = 0.0f;
  3937. for (std::size_t i = 0; i < 4800; ++i)
  3938. early = std::max (early, std::abs (delayed[i]));
  3939. for (std::size_t i = 4800; i < 24000; ++i)
  3940. later = std::max (later, std::abs (delayed[i]));
  3941. CHECK (early == 0.0f && later > 0.0f);
  3942. 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; };
  3943. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  3944. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  3945. CHECK (cut < open * 0.01);
  3946. }
  3947. TEST (utility_effect_and_fl_stereo_tools)
  3948. {
  3949. // transparent by default; balance fully right silences the left
  3950. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  3951. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  3952. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  3953. std::vector<std::uint8_t> blob (36, 0);
  3954. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3955. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  3956. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  3957. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  3958. auto shaperFx = lynx::makeEffect (*slot);
  3959. shaperFx->prepare (44100.0);
  3960. float zl = 0.0f, zr = 0.0f;
  3961. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  3962. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  3963. float l = 1.0f, r = 0.0f;
  3964. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  3965. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  3966. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  3967. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  3968. // Balance: volume 256 = 0 dB, pan −128 = fully left
  3969. std::vector<std::uint8_t> bal (8, 0);
  3970. std::int32_t pan = -128, vol = 256;
  3971. std::memcpy (bal.data(), &pan, 4);
  3972. std::memcpy (bal.data() + 4, &vol, 4);
  3973. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  3974. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  3975. if (b)
  3976. {
  3977. auto balance = lynx::makeEffect (*b);
  3978. balance->prepare (44100.0);
  3979. l = 0.5f; r = 0.5f;
  3980. balance->process (&l, &r, 1, b->params, 120.0);
  3981. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  3982. }
  3983. }
  3984. TEST (effects_eq_compressor_behave)
  3985. {
  3986. // EQ at default is transparent
  3987. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  3988. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  3989. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  3990. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  3991. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  3992. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  3993. s.params[0] = 40.0f / 60.0f;
  3994. s.params[1] = 1.0f;
  3995. }, 200.0, 1.0f);
  3996. CHECK (squashed < 0.2f && squashed > 0.05f);
  3997. // a signal below the threshold passes intact
  3998. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  3999. }
  4000. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  4001. {
  4002. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  4003. delay->prepare (48000.0);
  4004. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4005. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  4006. slot.params[1] = 0.0f; // no feedback: a single echo
  4007. slot.params[2] = 1.0f;
  4008. slot.params[4] = 0.0f; // no damping
  4009. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  4010. l[0] = r[0] = 1.0f;
  4011. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4012. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  4013. CHECK (std::abs (l[12000]) < 1e-6f);
  4014. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  4015. reverb->prepare (48000.0);
  4016. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  4017. rs.params[3] = 1.0f;
  4018. std::fill (l.begin(), l.end(), 0.0f);
  4019. std::fill (r.begin(), r.end(), 0.0f);
  4020. l[0] = r[0] = 1.0f;
  4021. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  4022. float tail = 0.0f;
  4023. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  4024. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  4025. CHECK (tail > 1e-4f && tail < 1.0f);
  4026. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  4027. }
  4028. TEST (rack_runs_insert_effects_and_reuses_them)
  4029. {
  4030. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  4031. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  4032. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4033. eq.params[4] = 0.0f; // output -18 dB
  4034. s.mixer[1].effects.push_back (eq);
  4035. });
  4036. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  4037. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  4038. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  4039. eq.params[4] = 0.0f;
  4040. eq.enabled = false;
  4041. s.mixer[1].effects.push_back (eq);
  4042. });
  4043. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  4044. auto s = lynx::Song::makeDefault();
  4045. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4046. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  4047. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  4048. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  4049. CHECK (b->effects[2][0] == a->effects[2][0]);
  4050. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  4051. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  4052. CHECK (c->effects[2][0] != b->effects[2][0]);
  4053. }
  4054. TEST (audio_clips_trigger_cut_and_resume)
  4055. {
  4056. lynx::Song s;
  4057. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  4058. lynx::ChannelDef c;
  4059. c.instrument = lynx::InstrumentKind::audioClip;
  4060. c.samplePath = "ramp";
  4061. c.volume = 1.0f;
  4062. s.channels.push_back (c);
  4063. s.patterns.emplace_back();
  4064. lynx::Clip clip;
  4065. clip.kind = lynx::ClipKind::audio;
  4066. clip.channel = 0;
  4067. clip.start = lynx::ppq; // starts at beat 2
  4068. clip.length = 2 * lynx::ppq; // lasts 2 beats
  4069. clip.offset = lynx::ppq; // skips the first beat of the audio
  4070. s.playlist.push_back (clip);
  4071. s.mode = lynx::PlayMode::song;
  4072. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4073. lynx::EventBuffer ev;
  4074. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  4075. CHECK (ev.size() == 2);
  4076. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  4077. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  4078. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  4079. ev.clear();
  4080. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  4081. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  4082. ev.clear();
  4083. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  4084. CHECK (ev.size() == 0);
  4085. // sample whose value = position in seconds: lets us check where playback started
  4086. auto provider = [] (const std::string&) {
  4087. auto d = std::make_shared<lynx::SampleData>();
  4088. d->sampleRate = 48000.0;
  4089. for (int i = 0; i < 48000 * 3; ++i)
  4090. d->left.push_back ((float) i / 48000.0f);
  4091. return std::shared_ptr<const lynx::SampleData> (d);
  4092. };
  4093. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4094. lynx::RackRenderer r;
  4095. r.prepare (48000.0, 512);
  4096. std::vector<float> l (256), rr (256);
  4097. // first block already playing mid-clip
  4098. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  4099. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f); // clip level at 100% channel volume (REA_rodada89 §4.2)
  4100. CHECK (std::abs (l[0] - 0.75f * unity) < 0.002f);
  4101. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  4102. double tick = 3.0 * lynx::ppq - 10 * tps;
  4103. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  4104. for (int i = 0; i < 4; ++i)
  4105. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  4106. CHECK (std::abs (l[255]) < 1e-6f);
  4107. }
  4108. TEST (remove_channel_fixes_notes_and_audio_clips)
  4109. {
  4110. lynx::Song s;
  4111. s.channels.resize (3);
  4112. s.patterns.resize (1);
  4113. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  4114. lynx::Clip a;
  4115. a.kind = lynx::ClipKind::audio;
  4116. a.channel = 1;
  4117. a.length = 100;
  4118. lynx::Clip b = a;
  4119. b.channel = 2;
  4120. s.playlist = { a, b };
  4121. s.removeChannel (1);
  4122. CHECK (s.channels.size() == 2);
  4123. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  4124. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  4125. }
  4126. TEST (flp_import_audio_clips_with_offsets)
  4127. {
  4128. flp::Project p;
  4129. p.header.ppq = 96;
  4130. p.tempo = 120.0;
  4131. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  4132. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  4133. flp::Arrangement arr;
  4134. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  4135. beats.kind = flp::PlaylistItem::Kind::channelClip;
  4136. beats.index = 0;
  4137. beats.position = 192;
  4138. beats.length = 384; // 4 beats
  4139. beats.startOffset = 2.0f;
  4140. beats.endOffset = 6.0f;
  4141. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  4142. ms.index = 1;
  4143. ms.startOffset = 500.0f;
  4144. ms.endOffset = 2500.0f;
  4145. arr.items = { beats, ms };
  4146. p.arrangements.push_back (arr);
  4147. lynx::FlpImportOptions options;
  4148. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  4149. options.fileExists = [] (const std::string&) { return true; };
  4150. const auto s = lynx::importFlp (p, options);
  4151. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  4152. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  4153. CHECK (s.playlist.size() == 2);
  4154. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  4155. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  4156. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  4157. CHECK (s.mode == lynx::PlayMode::song);
  4158. }
  4159. TEST (plugin_factories_create_reuse_and_fallback)
  4160. {
  4161. lynx::Song s;
  4162. s.mixer = lynx::Song::defaultMixer (2);
  4163. lynx::ChannelDef c;
  4164. c.instrument = lynx::InstrumentKind::plugin;
  4165. c.plugin.id = "VST3-Synth-123";
  4166. c.mixerTrack = 1;
  4167. s.channels.push_back (c);
  4168. lynx::ChannelDef missing = c;
  4169. missing.plugin.id = "VST3-Ausente-0";
  4170. s.channels.push_back (missing);
  4171. lynx::EffectSlot fx;
  4172. fx.kind = lynx::EffectKind::plugin;
  4173. fx.plugin.id = "VST3-Reverb-9";
  4174. s.mixer[1].effects.push_back (fx);
  4175. s.patterns.emplace_back();
  4176. int instrumentsMade = 0, effectsMade = 0;
  4177. lynx::RackState::Factories f;
  4178. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  4179. if (ref.id != "VST3-Synth-123")
  4180. return nullptr;
  4181. ++instrumentsMade;
  4182. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4183. synth->prepare (48000.0);
  4184. return synth;
  4185. };
  4186. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  4187. ++effectsMade;
  4188. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  4189. eq->prepare (48000.0);
  4190. return eq;
  4191. };
  4192. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  4193. CHECK (instrumentsMade == 1 && effectsMade == 1);
  4194. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  4195. // changing the saved state doesn't recreate; swapping the plugin does
  4196. s.channels[0].plugin.state = "abc";
  4197. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  4198. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  4199. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  4200. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  4201. CHECK (effectsMade == 2);
  4202. // rendering with a missing plugin and a plugin slot breaks nothing
  4203. lynx::RackRenderer r;
  4204. r.prepare (48000.0, 256);
  4205. lynx::LiveEventQueue live;
  4206. lynx::NoteEvent on;
  4207. on.channel = 0;
  4208. on.velocity = 1.0f;
  4209. live.push (on);
  4210. on.channel = 1;
  4211. live.push (on);
  4212. std::vector<float> l (256), rr (256);
  4213. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4214. float peak = 0.0f;
  4215. for (auto v : l)
  4216. {
  4217. CHECK (std::isfinite (v));
  4218. peak = std::max (peak, std::abs (v));
  4219. }
  4220. CHECK (peak > 0.0f);
  4221. }
  4222. TEST (export_song_adds_silent_tail_and_never_restarts)
  4223. {
  4224. auto s = lynx::Song::makeDefault();
  4225. s.playlist.clear();
  4226. std::int64_t length = 0;
  4227. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  4228. CHECK (length == lynx::ppq * 4);
  4229. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  4230. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  4231. // no new notes after the song ends, only silence until the end of the tail
  4232. lynx::EventBuffer events;
  4233. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  4234. int onsAfterEnd = 0, onsBefore = 0;
  4235. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  4236. {
  4237. events.clear();
  4238. lynx::scheduleSong (pat, tick, 512, tps, events);
  4239. for (const auto& e : events)
  4240. if (e.type == lynx::NoteEvent::Type::on)
  4241. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  4242. }
  4243. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  4244. CHECK (onsAfterEnd == 0);
  4245. // with Playlist: the end is the last clip's, without rounding
  4246. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  4247. auto song = lynx::makeExportSong (s, true, 0, length);
  4248. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  4249. song = lynx::makeExportSong (s, false, 0, length);
  4250. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  4251. }
  4252. TEST (note_edit_selection_operations)
  4253. {
  4254. namespace ne = lynx::noteedit;
  4255. lynx::Pattern p;
  4256. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  4257. // sorting keeps the indices in step
  4258. p.notes[0].start = 1200;
  4259. std::vector<int> tracked { 0, 2 };
  4260. ne::sortTracking (p, tracked);
  4261. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  4262. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  4263. // group move respects the limits
  4264. std::int64_t dt = -5000;
  4265. int dk = 100;
  4266. ne::move (p, { 0, 1 }, dt, dk);
  4267. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  4268. // duplicate: right after the selection, rounded to the grid
  4269. lynx::Pattern q;
  4270. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  4271. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  4272. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  4273. // paste onto another channel with an offset
  4274. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  4275. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  4276. // quantize rounds to the nearest
  4277. q.notes[0].start = 130;
  4278. q.notes[1].start = 100;
  4279. ne::quantize (q, { 0, 1 }, 240);
  4280. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  4281. // remove several (order and duplicates don't matter)
  4282. const auto before = q.notes.size();
  4283. ne::remove (q, { 1, 0, 1 });
  4284. CHECK (q.notes.size() == before - 2);
  4285. }
  4286. TEST (note_edit_quick_operations)
  4287. {
  4288. namespace ne = lynx::noteedit;
  4289. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4290. // split: only strictly inside; the tail is appended and does not slide again
  4291. lynx::Pattern p;
  4292. p.notes = { { 0, 4 * step, 0, 60 } };
  4293. p.notes[0].slide = true;
  4294. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  4295. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  4296. && ! p.notes[1].slide);
  4297. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  4298. lynx::Pattern c;
  4299. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  4300. const auto pieces = ne::chop (c, { 0, 1 }, step);
  4301. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  4302. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  4303. && c.notes[3].length == step);
  4304. // join: touching and overlapping notes of the same key and channel merge; others stay
  4305. lynx::Pattern j;
  4306. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  4307. { step, step, 0, 62 }, { step, step, 1, 60 } };
  4308. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  4309. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  4310. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  4311. lynx::Pattern l;
  4312. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  4313. ne::legato (l, { 0, 1, 2, 3, 4 });
  4314. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  4315. && l.notes[4].length == 10);
  4316. // shrink to grid and full quantise (start and end)
  4317. ne::shrinkToGrid (l, { 3 }, step / 2);
  4318. CHECK (l.notes[3].length == step / 2);
  4319. lynx::Pattern q;
  4320. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  4321. ne::quantizeFull (q, { 0, 1 }, step);
  4322. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  4323. // rotate inside the bar, wrapping
  4324. lynx::Pattern r;
  4325. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  4326. ne::rotate (r, { 0, 1 }, step, bar, 1);
  4327. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  4328. ne::rotate (r, { 0, 1 }, step, bar, -2);
  4329. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  4330. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  4331. ne::rotate (r, { 2 }, step, bar, -2);
  4332. CHECK (r.notes[2].start == 2 * bar - step);
  4333. }
  4334. TEST (note_edit_grid_and_time_text)
  4335. {
  4336. namespace ne = lynx::noteedit;
  4337. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  4338. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  4339. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  4340. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  4341. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  4342. CHECK (ne::screenStep (24.0 / step, bar) == step);
  4343. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  4344. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  4345. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  4346. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  4347. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  4348. std::int64_t t = 0;
  4349. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  4350. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  4351. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  4352. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  4353. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  4354. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  4355. }
  4356. TEST (playlist_clip_edges_and_time_selection)
  4357. {
  4358. namespace ne = lynx::noteedit;
  4359. const std::int64_t beat = lynx::ppq;
  4360. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  4361. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  4362. ne::trimStart (c, 5 * beat, beat);
  4363. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  4364. ne::trimStart (c, 0, beat);
  4365. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  4366. ne::trimStart (c, 100 * beat, beat);
  4367. CHECK (c.length == beat && c.end() == 8 * beat);
  4368. // slip moves the content, not the clip
  4369. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  4370. ne::slip (s, beat / 2);
  4371. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  4372. ne::slip (s, beat);
  4373. CHECK (s.offset == 0);
  4374. ne::slip (s, -3 * beat);
  4375. CHECK (s.offset == 3 * beat);
  4376. // insert / delete time across all tracks, markers included
  4377. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  4378. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  4379. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  4380. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  4381. && markers[1].tick == beat / 2);
  4382. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  4383. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  4384. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  4385. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  4386. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  4387. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  4388. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  4389. CHECK (d.size() == 5);
  4390. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  4391. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  4392. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  4393. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  4394. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  4395. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  4396. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  4397. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  4398. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  4399. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  4400. }
  4401. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  4402. {
  4403. auto song = lynx::Song::makeDefault();
  4404. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  4405. lynx::NoteRecorder r;
  4406. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  4407. CHECK (r.takeFinished().empty());
  4408. r.begin (loop);
  4409. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  4410. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  4411. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  4412. r.end (song, 5240.0);
  4413. const auto notes = r.takeFinished();
  4414. CHECK (notes.size() == 2);
  4415. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  4416. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  4417. // song mode: position inside the current pattern's clip (with offset)
  4418. song.mode = lynx::PlayMode::song;
  4419. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  4420. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  4421. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  4422. }
  4423. TEST (automation_curve_and_playback)
  4424. {
  4425. lynx::Automation a;
  4426. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  4427. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  4428. CHECK (a.length() == 1920);
  4429. a.points[0].tension = 1.0f; // curve: starts slow
  4430. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  4431. // automated Master: silence in the first bar, sound in the second, and the last value
  4432. // keeps applying after the automation clip ends
  4433. auto song = lynx::Song::makeDefault();
  4434. song.bpm = 120.0;
  4435. song.mode = lynx::PlayMode::song;
  4436. const auto bar = (std::int64_t) lynx::ppq * 4;
  4437. for (int b = 0; b < 3; ++b)
  4438. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  4439. lynx::Automation master;
  4440. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  4441. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  4442. song.automations.push_back (master);
  4443. lynx::Clip ac;
  4444. ac.kind = lynx::ClipKind::automation;
  4445. ac.automation = 0;
  4446. ac.track = 1;
  4447. ac.length = bar + bar / 2;
  4448. song.playlist.push_back (ac);
  4449. const double sr = 48000.0;
  4450. auto state = lynx::RackState::build (song, nullptr, sr);
  4451. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  4452. lynx::RackRenderer r;
  4453. r.prepare (sr, 512);
  4454. std::vector<float> l (512), rr (512);
  4455. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  4456. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  4457. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  4458. {
  4459. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4460. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  4461. for (auto v : l)
  4462. peak = std::max (peak, std::abs (v));
  4463. }
  4464. CHECK (peakBar1 < 1.0e-6f);
  4465. CHECK (peakBar2 > 0.05f);
  4466. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  4467. // target removed: automation disappears from the tables
  4468. song.removeChannel (0);
  4469. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  4470. song.removeChannel (0);
  4471. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  4472. song.removeAutomation (0);
  4473. CHECK (song.automations.empty() && song.playlist.size() == 3);
  4474. }
  4475. TEST (mixer_sends_between_inserts)
  4476. {
  4477. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  4478. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  4479. // send at 50% and muted destination
  4480. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  4481. 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);
  4482. // going to the Master and to 3: the signal doubles
  4483. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  4484. // solo on 3 lets what reaches it sound (insert 1), but not 2
  4485. 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);
  4486. // solo on 1 lets the path to the Master (3) sound
  4487. 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);
  4488. // order and cycles
  4489. auto song = lynx::Song::makeDefault();
  4490. song.setRoute (4, 2, true);
  4491. song.setRoute (2, 1, true);
  4492. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  4493. song.setRoute (1, 4, true); // ignored: would create a cycle
  4494. CHECK (! song.routes (1, 4));
  4495. const auto order = song.mixerOrder();
  4496. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  4497. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  4498. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  4499. song.setRoute (4, 2, false);
  4500. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  4501. }
  4502. namespace
  4503. {
  4504. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  4505. class LatentEffect final : public lynx::Effect
  4506. {
  4507. public:
  4508. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  4509. void prepare (double) noexcept override {}
  4510. void reset() noexcept override {}
  4511. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  4512. int latencySamples() const noexcept override { return latency; }
  4513. private:
  4514. int latency;
  4515. lynx::DelayLine line;
  4516. };
  4517. } // namespace
  4518. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  4519. {
  4520. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  4521. lynx::DelayLine a (37), b (37);
  4522. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  4523. for (int i = 0; i < 300; ++i)
  4524. {
  4525. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  4526. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  4527. }
  4528. for (int start = 0; start < 300; start += 100)
  4529. {
  4530. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  4531. for (int i = start; i < start + 100; ++i)
  4532. {
  4533. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  4534. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  4535. }
  4536. a.process (xl.data() + start, xr.data() + start, 100);
  4537. }
  4538. float worst = 0.0f;
  4539. for (int i = 0; i < 300; ++i)
  4540. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  4541. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  4542. CHECK (worst < 1e-6f);
  4543. // silenced source: the tail still in the line comes out, then nothing remains
  4544. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  4545. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  4546. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  4547. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  4548. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  4549. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  4550. }
  4551. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  4552. {
  4553. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  4554. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  4555. fx->prepare (48000.0);
  4556. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4557. slot.params[2] = 0.0f; // 10 ms release
  4558. std::vector<float> l (48000), r (48000);
  4559. std::uint32_t seed = 1;
  4560. for (int i = 0; i < 48000; ++i)
  4561. {
  4562. seed = seed * 1664525u + 1013904223u;
  4563. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  4564. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  4565. r[(std::size_t) i] = -noise;
  4566. }
  4567. for (int i = 0; i < 48000; i += 333)
  4568. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  4569. float peak = 0.0f, quiet = 0.0f;
  4570. for (int i = 0; i < 48000; ++i)
  4571. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  4572. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  4573. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  4574. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  4575. CHECK (peak <= ceiling + 1e-5f);
  4576. CHECK (quiet > 0.03f);
  4577. }
  4578. TEST (pdc_aligns_paths_through_latent_effects)
  4579. {
  4580. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  4581. lynx::Song s;
  4582. s.mixer = lynx::Song::defaultMixer (4);
  4583. s.channels.resize (3);
  4584. s.channels[0].mixerTrack = 1;
  4585. s.channels[1].mixerTrack = 2;
  4586. s.channels[2].mixerTrack = 3;
  4587. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  4588. s.setRoute (1, 3, true);
  4589. s.setRoute (1, 0, false);
  4590. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  4591. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  4592. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  4593. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  4594. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  4595. // bypass zeroes the slot's latency
  4596. s.mixer[1].effects[0].enabled = false;
  4597. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  4598. // rendered: two impulses played together come out together
  4599. lynx::Song r;
  4600. r.mixer = lynx::Song::defaultMixer (2);
  4601. for (int i = 0; i < 2; ++i)
  4602. {
  4603. lynx::ChannelDef c;
  4604. c.instrument = lynx::InstrumentKind::sampler;
  4605. c.samplePath = "impulso";
  4606. c.volume = 1.0f;
  4607. c.mixerTrack = i + 1;
  4608. r.channels.push_back (c);
  4609. }
  4610. r.patterns.emplace_back();
  4611. lynx::EffectSlot fx;
  4612. fx.kind = lynx::EffectKind::plugin;
  4613. fx.plugin.id = "latente";
  4614. r.mixer[1].effects.push_back (fx);
  4615. lynx::RackState::Factories f;
  4616. f.samples = [] (const std::string&) {
  4617. auto d = std::make_shared<lynx::SampleData>();
  4618. d->left.assign (64, 0.0f);
  4619. d->left[0] = 1.0f;
  4620. return std::shared_ptr<const lynx::SampleData> (d);
  4621. };
  4622. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  4623. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  4624. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  4625. lynx::RackRenderer renderer;
  4626. renderer.prepare (48000.0, 256);
  4627. lynx::LiveEventQueue live;
  4628. for (int ch = 0; ch < 2; ++ch)
  4629. {
  4630. lynx::NoteEvent on;
  4631. on.channel = ch;
  4632. on.key = 60;
  4633. on.velocity = 1.0f;
  4634. live.push (on);
  4635. }
  4636. std::vector<float> l (256), rr (256);
  4637. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4638. int first = -1;
  4639. for (int i = 0; i < 256 && first < 0; ++i)
  4640. if (std::abs (l[(std::size_t) i]) > 0.01f)
  4641. first = i;
  4642. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  4643. // editing without changing latencies reuses the lines
  4644. r.channels[0].volume = 0.5f;
  4645. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  4646. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  4647. }
  4648. TEST (mixer_mute_fades_instead_of_clicking)
  4649. {
  4650. lynx::Song s;
  4651. s.mixer = lynx::Song::defaultMixer (2);
  4652. lynx::ChannelDef c;
  4653. c.instrument = lynx::InstrumentKind::sampler;
  4654. c.samplePath = "dc";
  4655. c.volume = 1.0f;
  4656. c.mixerTrack = 1;
  4657. s.channels.push_back (c);
  4658. s.patterns.emplace_back();
  4659. auto provider = [] (const std::string&) {
  4660. auto d = std::make_shared<lynx::SampleData>();
  4661. d->left.assign (48000, 0.5f);
  4662. return std::shared_ptr<const lynx::SampleData> (d);
  4663. };
  4664. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4665. s.mixer[1].muted = true;
  4666. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  4667. lynx::RackRenderer r;
  4668. r.prepare (48000.0, 256);
  4669. lynx::LiveEventQueue live;
  4670. lynx::NoteEvent note;
  4671. note.velocity = 1.0f;
  4672. live.push (note);
  4673. std::vector<float> l (256), rr (256);
  4674. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4675. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  4676. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4677. CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
  4678. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4679. CHECK (std::abs (l[0]) < 1e-9f);
  4680. }
  4681. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  4682. {
  4683. lynx::Dither d (16);
  4684. std::vector<float> x (4800);
  4685. for (std::size_t i = 0; i < x.size(); ++i)
  4686. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  4687. auto y = x;
  4688. d.process (y.data(), (int) y.size(), 0);
  4689. double errorSum = 0.0;
  4690. bool onGrid = true;
  4691. for (std::size_t i = 0; i < x.size(); ++i)
  4692. {
  4693. const auto steps = y[i] * 32768.0f;
  4694. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  4695. errorSum += std::abs (y[i] - x[i]);
  4696. }
  4697. CHECK (onGrid);
  4698. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  4699. }
  4700. TEST (parallel_rendering_matches_sequential)
  4701. {
  4702. // 32 channels (worth parallelizing), inserts with chained sends
  4703. auto song = lynx::Song::makeDefault();
  4704. song.mixer = lynx::Song::defaultMixer (16);
  4705. for (int i = 5; i < 32; ++i)
  4706. {
  4707. lynx::ChannelDef c;
  4708. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  4709. c.mixerTrack = 1 + i % 12;
  4710. c.volume = 0.3f;
  4711. song.channels.push_back (c);
  4712. for (int step = i % 4; step < 16; step += 3)
  4713. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  4714. }
  4715. song.patterns[0].sortNotes();
  4716. song.setRoute (3, 7, true, 0.6f);
  4717. song.setRoute (7, 9, true, 0.5f);
  4718. song.setRoute (2, 9, true);
  4719. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4720. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  4721. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  4722. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  4723. lynx::WorkerPool pool (3);
  4724. lynx::RackRenderer seq, par;
  4725. seq.prepare (48000.0, 512);
  4726. par.prepare (48000.0, 512);
  4727. par.setWorkerPool (&pool);
  4728. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  4729. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4730. bool same = true;
  4731. float peak = 0.0f;
  4732. for (int block = 0; block < 200; ++block)
  4733. {
  4734. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  4735. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  4736. same = same && l1 == l2 && r1 == r2;
  4737. for (auto v : l1)
  4738. peak = std::max (peak, std::abs (v));
  4739. }
  4740. CHECK (same);
  4741. CHECK (peak > 0.05f);
  4742. // the pool handles many small rounds in a row
  4743. std::atomic<int> sum { 0 };
  4744. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  4745. for (int round = 0; round < 2000; ++round)
  4746. pool.parallelFor (5, add);
  4747. CHECK (sum.load() == 2000 * 15);
  4748. }
  4749. TEST (arrangements_switch_and_keep_clip_indices)
  4750. {
  4751. auto s = lynx::Song::makeDefault();
  4752. s.patterns.emplace_back().name = "B";
  4753. s.playlist = { { 1, 0, 0, 960, 0 } };
  4754. s.loopEnd = 960;
  4755. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  4756. const auto second = s.addArrangement ("Versão curta", true);
  4757. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  4758. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  4759. s.loopEnd = 0;
  4760. s.selectArrangement (0);
  4761. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  4762. // removing pattern 0 fixes the clips stored in the other arrangement
  4763. s.removePattern (0);
  4764. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  4765. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  4766. s.removeArrangement (0);
  4767. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  4768. }
  4769. namespace
  4770. {
  4771. // Test effect that inverts the signal and counts how many times it processed.
  4772. class CountingInverter final : public lynx::Effect
  4773. {
  4774. public:
  4775. void prepare (double) noexcept override {}
  4776. void reset() noexcept override {}
  4777. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  4778. {
  4779. ++calls;
  4780. for (int i = 0; i < n; ++i)
  4781. {
  4782. l[i] = -l[i];
  4783. r[i] = -r[i];
  4784. }
  4785. }
  4786. int calls = 0;
  4787. };
  4788. } // namespace
  4789. TEST (keyboard_controller_outputs_key_value)
  4790. {
  4791. std::vector<std::uint8_t> blob (554, 0);
  4792. const std::uint32_t version = 3;
  4793. std::memcpy (blob.data(), &version, 4);
  4794. const float key60 = 0.7f, off = 0.2f;
  4795. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  4796. std::memcpy (blob.data() + 548, &off, 4);
  4797. blob[552] = 1; // auto release
  4798. lynx::ChannelDef c;
  4799. c.instrument = lynx::InstrumentKind::controller;
  4800. c.controllerState = blob;
  4801. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  4802. inst->prepare (48000.0);
  4803. inst->configure (c, 120.0);
  4804. std::vector<float> l (64), r (64);
  4805. inst->noteOn (60, 1.0f);
  4806. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  4807. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  4808. inst->noteOff (60);
  4809. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  4810. }
  4811. TEST (envelope_controller_ducks_on_each_note)
  4812. {
  4813. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  4814. lynx::ControllerArticulator a;
  4815. a.envOn = true;
  4816. a.base = 0.05f;
  4817. a.envLevel = 1.0f;
  4818. a.count = 3;
  4819. a.time = { 0.0f, 0.2f, 0.3f };
  4820. a.level = { 0.0f, 0.75f, 1.0f };
  4821. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  4822. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  4823. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  4824. a.sustain = 1;
  4825. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  4826. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  4827. a.bipolar = true;
  4828. a.base = 0.5f;
  4829. a.sustain = -1;
  4830. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  4831. }
  4832. TEST (peak_controller_drives_live_automation)
  4833. {
  4834. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  4835. auto render = [] (bool linked) {
  4836. lynx::Song s;
  4837. s.mixer = lynx::Song::defaultMixer (2);
  4838. for (int t = 1; t <= 2; ++t)
  4839. {
  4840. lynx::ChannelDef c;
  4841. c.instrument = lynx::InstrumentKind::sampler;
  4842. c.samplePath = "dc";
  4843. c.volume = 1.0f;
  4844. c.mixerTrack = t;
  4845. s.channels.push_back (c);
  4846. }
  4847. s.patterns.emplace_back();
  4848. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  4849. peak.params[0] = 1.0f; // base 1
  4850. peak.params[1] = 0.0f; // amount −1: 1 − peak
  4851. peak.params[2] = 0.5f; // no tension
  4852. s.mixer[1].effects.push_back (peak);
  4853. if (linked)
  4854. {
  4855. lynx::Automation a;
  4856. a.name = "Peak";
  4857. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  4858. a.source = { 1, 0, 0 };
  4859. s.automations.push_back (a);
  4860. }
  4861. lynx::RackState::Factories f;
  4862. f.samples = [] (const std::string&) {
  4863. auto d = std::make_shared<lynx::SampleData>();
  4864. d->left.assign (48000, 0.5f);
  4865. return std::shared_ptr<const lynx::SampleData> (d);
  4866. };
  4867. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4868. lynx::RackRenderer r;
  4869. r.prepare (48000.0, 512);
  4870. lynx::LiveEventQueue live;
  4871. for (int ch = 0; ch < 2; ++ch)
  4872. {
  4873. lynx::NoteEvent on;
  4874. on.channel = ch;
  4875. on.velocity = 1.0f;
  4876. live.push (on);
  4877. }
  4878. std::vector<float> l (4800), rr (4800);
  4879. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  4880. return l[4000];
  4881. };
  4882. const auto free = render (false), ducked = render (true);
  4883. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  4884. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  4885. }
  4886. TEST (effect_slot_dry_wet_and_smart_disable)
  4887. {
  4888. lynx::Song s;
  4889. s.mixer = lynx::Song::defaultMixer (1);
  4890. lynx::ChannelDef c;
  4891. c.instrument = lynx::InstrumentKind::sampler;
  4892. c.samplePath = "dc";
  4893. c.volume = 1.0f;
  4894. c.mixerTrack = 1;
  4895. s.channels.push_back (c);
  4896. s.patterns.emplace_back();
  4897. lynx::EffectSlot fx;
  4898. fx.kind = lynx::EffectKind::plugin;
  4899. fx.plugin.id = "inversor";
  4900. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  4901. s.mixer[1].effects.push_back (fx);
  4902. auto inverter = std::make_shared<CountingInverter>();
  4903. lynx::RackState::Factories f;
  4904. f.samples = [] (const std::string&) {
  4905. auto d = std::make_shared<lynx::SampleData>();
  4906. d->left.assign (4800, 0.5f);
  4907. return std::shared_ptr<const lynx::SampleData> (d);
  4908. };
  4909. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  4910. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4911. lynx::RackRenderer r;
  4912. r.prepare (48000.0, 512);
  4913. lynx::LiveEventQueue live;
  4914. lynx::NoteEvent on;
  4915. on.velocity = 1.0f;
  4916. live.push (on);
  4917. std::vector<float> l (480), rr (480);
  4918. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4919. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  4920. // after the sample ends: 4 s of silence and the plugin stops being called
  4921. for (int i = 0; i < 100 * 6; ++i) // 6 s
  4922. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4923. const auto callsAfterSilence = inverter->calls;
  4924. for (int i = 0; i < 100; ++i)
  4925. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4926. CHECK (inverter->calls == callsAfterSilence);
  4927. CHECK (callsAfterSilence < 100 * 6);
  4928. // sound again: processing resumes in the same block
  4929. live.push (on);
  4930. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4931. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  4932. }
  4933. TEST (swing_delays_offbeat_sixteenths)
  4934. {
  4935. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  4936. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  4937. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  4938. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  4939. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  4940. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  4941. lynx::Pattern p;
  4942. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  4943. lynx::EventBuffer ev;
  4944. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4945. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  4946. int onOffset = -1;
  4947. for (auto& e : ev)
  4948. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  4949. onOffset = e.sampleOffset;
  4950. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  4951. }
  4952. TEST (sampler_settings_pitch_reverse_envelope_cut)
  4953. {
  4954. auto ramp = std::make_shared<lynx::SampleData>();
  4955. for (int i = 0; i < 48000; ++i)
  4956. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  4957. ramp->sampleRate = 48000.0;
  4958. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  4959. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4960. inst->prepare (48000.0);
  4961. lynx::ChannelDef c;
  4962. c.sampler = s;
  4963. inst->configure (c, 120.0);
  4964. inst->noteOn (key, 1.0f);
  4965. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4966. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  4967. if (releaseAfterHalf)
  4968. inst->noteOff (key);
  4969. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  4970. return l;
  4971. };
  4972. // +12 semitones: reads twice as fast
  4973. lynx::SamplerSettings up;
  4974. up.pitch = 12.0f;
  4975. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  4976. // reversed: starts from the end
  4977. lynx::SamplerSettings back;
  4978. back.reverse = true;
  4979. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  4980. // 10 ms attack: starts silent and reaches full level
  4981. lynx::SamplerSettings slow;
  4982. slow.attack = 0.01f;
  4983. const auto a = play (slow, 60, 2000, false);
  4984. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  4985. // 5 ms release: gone right after release; without release it plays in full
  4986. lynx::SamplerSettings rel;
  4987. rel.release = 0.005f;
  4988. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  4989. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  4990. // cut itself: the second note cuts the first (one voice remains)
  4991. lynx::SamplerSettings cut;
  4992. cut.cut = true;
  4993. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4994. inst->prepare (48000.0);
  4995. lynx::ChannelDef cutChannel;
  4996. cutChannel.sampler = cut;
  4997. inst->configure (cutChannel, 120.0);
  4998. inst->noteOn (60, 1.0f);
  4999. std::vector<float> l (2000), r (2000);
  5000. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5001. inst->noteOn (60, 1.0f);
  5002. std::fill (l.begin(), l.end(), 0.0f);
  5003. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  5004. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  5005. }
  5006. TEST (playlist_split_and_track_mute)
  5007. {
  5008. lynx::Clip c { 0, 3, 960, 3840, 480 };
  5009. const auto rest = lynx::splitClip (c, 2000);
  5010. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  5011. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  5012. auto s = lynx::Song::makeDefault();
  5013. s.mode = lynx::PlayMode::song;
  5014. s.playlist = { { 0, 0, 0, 3840, 0 } };
  5015. lynx::EventBuffer ev;
  5016. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5017. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5018. CHECK (ev.size() > 0);
  5019. s.playlistTracks.resize (1);
  5020. s.playlistTracks[0].muted = true;
  5021. ev.clear();
  5022. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  5023. CHECK (ev.size() == 0);
  5024. }
  5025. TEST (note_pan_pitch_and_mute)
  5026. {
  5027. lynx::Pattern p;
  5028. lynx::Note a { 0, 240, 0, 60 };
  5029. a.pan = -1.0f;
  5030. a.finePitch = 50.0f;
  5031. lynx::Note b { 240, 240, 0, 62 };
  5032. b.muted = true;
  5033. p.notes = { a, b };
  5034. lynx::EventBuffer ev;
  5035. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5036. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  5037. int ons = 0;
  5038. for (auto& e : ev)
  5039. if (e.type == lynx::NoteEvent::Type::on)
  5040. {
  5041. ++ons;
  5042. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  5043. }
  5044. CHECK (ons == 1); // the muted one doesn't play
  5045. // note pan in the sampler: fully left
  5046. auto dc = std::make_shared<lynx::SampleData>();
  5047. dc->left.assign (4800, 0.5f);
  5048. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5049. inst->prepare (48000.0);
  5050. inst->setNextNoteExpression (-1.0f, 0.0f);
  5051. inst->noteOn (60, 1.0f);
  5052. std::vector<float> l (100), r (100);
  5053. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  5054. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  5055. }
  5056. TEST (tempo_automation_changes_speed_and_duration)
  5057. {
  5058. auto song = lynx::Song::makeDefault();
  5059. song.bpm = 120.0;
  5060. song.mode = lynx::PlayMode::song;
  5061. const auto bar = (std::int64_t) lynx::ppq * 4;
  5062. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  5063. lynx::Automation tempo;
  5064. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  5065. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  5066. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  5067. song.automations.push_back (tempo);
  5068. lynx::Clip clip;
  5069. clip.kind = lynx::ClipKind::automation;
  5070. clip.automation = 0;
  5071. clip.track = 1;
  5072. clip.length = bar * 2;
  5073. song.playlist.push_back (clip);
  5074. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  5075. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  5076. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  5077. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5078. CHECK (state->tempoAutomation == 0);
  5079. lynx::RackRenderer r;
  5080. r.prepare (48000.0, 512);
  5081. std::vector<float> l (512), rr (512);
  5082. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  5083. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  5084. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  5085. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  5086. }
  5087. TEST (bounce_insert_song_and_apply)
  5088. {
  5089. auto s = lynx::Song::makeDefault();
  5090. s.setRoute (3, 7, true, 0.5f);
  5091. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  5092. s.mixer[2].solo = true;
  5093. const auto b = lynx::makeBounceSong (s, 3);
  5094. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  5095. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  5096. const auto audible = b.soloAudible();
  5097. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  5098. const auto channels = s.channels.size();
  5099. const auto inserts = s.mixer.size();
  5100. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  5101. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  5102. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  5103. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  5104. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  5105. }
  5106. TEST (mixer_stereo_separation_phase_swap)
  5107. {
  5108. lynx::MixerTrack t;
  5109. std::vector<float> l { 1.0f }, r { 0.0f };
  5110. t.stereo = 1.0f; // mono
  5111. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  5112. CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
  5113. l = { 1.0f };
  5114. r = { 0.0f };
  5115. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  5116. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  5117. CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
  5118. l = { 1.0f };
  5119. r = { 0.25f };
  5120. t.stereo = 0.0f;
  5121. t.swapChannels = true;
  5122. t.invertPhase = true;
  5123. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  5124. CHECK (l[0] == -0.25f && r[0] == -1.0f);
  5125. }
  5126. TEST (effects_limiter_filter_distortion_chorus)
  5127. {
  5128. const double sr = 48000.0;
  5129. auto sine = [] (float hz, float amp, int n) {
  5130. std::vector<float> v ((std::size_t) n);
  5131. for (int i = 0; i < n; ++i)
  5132. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  5133. return v;
  5134. };
  5135. auto peak = [] (const std::vector<float>& v, int from) {
  5136. float p = 0.0f;
  5137. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  5138. p = std::max (p, std::abs (v[i]));
  5139. return p;
  5140. };
  5141. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  5142. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  5143. lim->prepare (sr);
  5144. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  5145. slot.params[0] = 11.0f / 12.0f; // -1 dB
  5146. slot.params[1] = 0.5f; // +12 dB
  5147. auto l = sine (100.0f, 0.9f, 48000), r = l;
  5148. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5149. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  5150. CHECK (peak (l, 4800) > 0.8f);
  5151. CHECK (lim->latencySamples() == 72);
  5152. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  5153. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  5154. lp->prepare (sr);
  5155. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  5156. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  5157. l = sine (8000.0f, 1.0f, 9600);
  5158. r = l;
  5159. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  5160. CHECK (peak (l, 4800) < 0.05f);
  5161. // distortion and chorus: finite, audible output
  5162. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  5163. {
  5164. auto fx = lynx::makeEffect (kind);
  5165. fx->prepare (sr);
  5166. auto ps = lynx::makeEffectSlot (kind);
  5167. l = sine (220.0f, 0.5f, 9600);
  5168. r = l;
  5169. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  5170. bool finite = true;
  5171. for (auto v : l)
  5172. finite = finite && std::isfinite (v);
  5173. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  5174. }
  5175. }
  5176. TEST (slide_notes_glide_without_retrigger)
  5177. {
  5178. // sampler with a ramp: the read speed doubles after a one-octave slide
  5179. auto ramp = std::make_shared<lynx::SampleData>();
  5180. for (int i = 0; i < 96000; ++i)
  5181. ramp->left.push_back ((float) i / 96000.0f);
  5182. ramp->sampleRate = 48000.0;
  5183. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5184. inst->prepare (48000.0);
  5185. inst->noteOn (60, 1.0f);
  5186. std::vector<float> l (2000), r (2000);
  5187. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5188. const auto stepBefore = l[999] - l[998];
  5189. inst->slideTo (72, 0.0f, 100.0f);
  5190. std::fill (l.begin(), l.end(), 0.0f);
  5191. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  5192. const auto stepAfter = l[999] - l[998];
  5193. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  5194. // the sequencer sends "slide" (no off) for the slide note
  5195. lynx::Pattern p;
  5196. lynx::Note base { 0, 960, 0, 48 };
  5197. lynx::Note glide { 480, 240, 0, 55 };
  5198. glide.slide = true;
  5199. p.notes = { base, glide };
  5200. lynx::EventBuffer ev;
  5201. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  5202. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  5203. int ons = 0, offs = 0, slides = 0;
  5204. for (auto& e : ev)
  5205. {
  5206. ons += e.type == lynx::NoteEvent::Type::on;
  5207. offs += e.type == lynx::NoteEvent::Type::off;
  5208. if (e.type == lynx::NoteEvent::Type::slide)
  5209. {
  5210. ++slides;
  5211. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  5212. }
  5213. }
  5214. CHECK (ons == 1 && offs == 1 && slides == 1);
  5215. }
  5216. TEST (synth_fm_phase_modulation)
  5217. {
  5218. auto render = [] (const lynx::SynthSettings& s) {
  5219. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5220. inst->prepare (48000.0);
  5221. lynx::ChannelDef c;
  5222. c.synth = s;
  5223. inst->configure (c, 120.0);
  5224. inst->noteOn (69, 1.0f); // 440 Hz
  5225. std::vector<float> l (48000), r (48000);
  5226. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  5227. return l;
  5228. };
  5229. // energy (Goertzel) at frequency f over frames [from, to)
  5230. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  5231. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5232. double s1 = 0.0, s2 = 0.0;
  5233. for (int i = from; i < to; ++i)
  5234. {
  5235. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5236. s2 = s1;
  5237. s1 = s0;
  5238. }
  5239. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5240. };
  5241. lynx::SynthSettings plain;
  5242. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5243. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5244. plain.cutoff = 20000.0f;
  5245. auto fm = plain;
  5246. fm.fm = 1.0f;
  5247. const auto a = render (plain), b = render (fm);
  5248. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  5249. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  5250. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  5251. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  5252. for (auto v : b)
  5253. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  5254. // index decaying to 0: the start is bright, the end is nearly a sine again
  5255. auto decaying = fm;
  5256. decaying.fmDecay = 0.1f;
  5257. decaying.fmSustain = 0.0f;
  5258. const auto c = render (decaying);
  5259. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  5260. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  5261. CHECK (early > 0.05 && late < 1e-3);
  5262. }
  5263. TEST (synth_three_oscillators_adsr_filter)
  5264. {
  5265. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  5266. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5267. inst->prepare (48000.0);
  5268. lynx::ChannelDef c;
  5269. c.synth = s;
  5270. inst->configure (c, 120.0);
  5271. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  5272. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5273. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  5274. inst->noteOff (69);
  5275. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  5276. return l;
  5277. };
  5278. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  5279. float p = 0.0f;
  5280. for (int i = from; i < to; ++i)
  5281. p = std::max (p, std::abs (v[(std::size_t) i]));
  5282. return p;
  5283. };
  5284. // default: sounds and fades after the release
  5285. const lynx::SynthSettings standard;
  5286. const auto a = render (standard, 48000, 24000);
  5287. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  5288. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  5289. lynx::SynthSettings sine;
  5290. sine.osc[0].wave = lynx::Waveform::sine;
  5291. sine.cutoff = 20000.0f;
  5292. const auto b = render (sine, 48000, 47000);
  5293. int crossings = 0;
  5294. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  5295. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  5296. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  5297. float sinePeak = 0.0f;
  5298. for (int i = 4800; i < 9600; ++i)
  5299. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  5300. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  5301. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  5302. lynx::SynthSettings two = sine;
  5303. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  5304. two.sustain = 0.0f;
  5305. two.decay = 0.1f;
  5306. const auto c = render (two, 48000, 47000);
  5307. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  5308. }
  5309. TEST (set_target_value_round_trips)
  5310. {
  5311. auto s = lynx::Song::makeDefault();
  5312. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  5313. const lynx::AutomationTarget targets[] = {
  5314. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  5315. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  5316. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  5317. };
  5318. for (const auto& t : targets)
  5319. {
  5320. CHECK (s.setTargetValue (t, 0.25f));
  5321. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  5322. }
  5323. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  5324. }
  5325. TEST (cut_groups_stretch_and_envelope_import)
  5326. {
  5327. // open hat (cut by 1) is cut when the closed one (group 1) plays
  5328. lynx::Song s;
  5329. s.mixer = lynx::Song::defaultMixer (2);
  5330. for (int i = 0; i < 2; ++i)
  5331. {
  5332. lynx::ChannelDef c;
  5333. c.instrument = lynx::InstrumentKind::sampler;
  5334. c.samplePath = "dc";
  5335. c.volume = 1.0f;
  5336. c.mixerTrack = i + 1;
  5337. s.channels.push_back (c);
  5338. }
  5339. s.channels[0].cutBy = 1; // open
  5340. s.channels[1].cutGroup = 1; // closed
  5341. auto& p = s.patterns.emplace_back();
  5342. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  5343. auto provider = [] (const std::string&) {
  5344. auto d = std::make_shared<lynx::SampleData>();
  5345. d->left.assign (96000, 0.5f);
  5346. return std::shared_ptr<const lynx::SampleData> (d);
  5347. };
  5348. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5349. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  5350. lynx::RackRenderer r;
  5351. r.prepare (48000.0, 512);
  5352. std::vector<float> l (512), rr (512);
  5353. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5354. float afterCut = 0.0f;
  5355. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  5356. {
  5357. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5358. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  5359. afterCut = std::max (afterCut, std::abs (l[256]));
  5360. }
  5361. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  5362. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  5363. auto ramp = std::make_shared<lynx::SampleData>();
  5364. for (int i = 0; i < 48000; ++i)
  5365. ramp->left.push_back ((float) i / 48000.0f);
  5366. ramp->sampleRate = 48000.0;
  5367. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5368. inst->prepare (48000.0);
  5369. lynx::ChannelDef c;
  5370. c.sampler.stretchBeats = 4.0f;
  5371. inst->configure (c, 120.0);
  5372. inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
  5373. std::vector<float> a (48000), b (48000);
  5374. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  5375. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  5376. // FL envelope time: 32950 = 2 s; < 101 = 0
  5377. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  5378. }
  5379. TEST (time_stretch_keeps_pitch)
  5380. {
  5381. lynx::SampleData sine;
  5382. sine.sampleRate = 48000.0;
  5383. for (int i = 0; i < 48000; ++i)
  5384. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  5385. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  5386. int c = 0;
  5387. const auto n = (int) d.frames();
  5388. for (int i = 4800; i < n - 4800; ++i)
  5389. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  5390. return (double) c / ((double) (n - 9600) / d.sampleRate);
  5391. };
  5392. for (double ratio : { 2.0, 0.5, 1.37 })
  5393. {
  5394. const auto out = lynx::timeStretch (sine, ratio);
  5395. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  5396. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  5397. float peak = 0.0f;
  5398. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  5399. peak = std::max (peak, std::abs (out->left[i]));
  5400. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  5401. }
  5402. }
  5403. TEST (sampler_region_start_and_end)
  5404. {
  5405. auto ramp = std::make_shared<lynx::SampleData>();
  5406. for (int i = 0; i < 10000; ++i)
  5407. ramp->left.push_back ((float) i / 10000.0f);
  5408. ramp->sampleRate = 48000.0;
  5409. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5410. inst->prepare (48000.0);
  5411. lynx::ChannelDef c;
  5412. c.sampler.regionStart = 0.25f;
  5413. c.sampler.regionEnd = 0.5f;
  5414. inst->configure (c, 120.0);
  5415. inst->noteOn (60, 1.0f);
  5416. std::vector<float> l (6000), r (6000);
  5417. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  5418. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  5419. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  5420. }
  5421. TEST (arpeggiator_plays_held_chord_in_steps)
  5422. {
  5423. auto song = lynx::Song::makeDefault();
  5424. song.bpm = 120.0;
  5425. song.patterns[0].notes.clear();
  5426. for (int key : { 60, 64, 67 })
  5427. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  5428. song.channels[4].arp.direction = 1; // up
  5429. song.channels[4].arp.stepBeats = 0.25f;
  5430. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5431. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  5432. // the arpeggiator generates the notes; we count what it hands to the instrument
  5433. auto& arp = *state->arpeggiators[4];
  5434. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5435. std::vector<int> keys;
  5436. lynx::EventBuffer ev;
  5437. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  5438. {
  5439. ev.clear();
  5440. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  5441. int in = 0, out = 0;
  5442. for (auto& e : ev)
  5443. if (e.channel == 4)
  5444. arp.input[(std::size_t) in++] = e;
  5445. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  5446. for (int i = 0; i < out; ++i)
  5447. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  5448. keys.push_back (arp.output[(std::size_t) i].key);
  5449. }
  5450. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  5451. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  5452. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  5453. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  5454. CHECK (keys[i] == expected[i]);
  5455. // actually rendering also makes sound
  5456. lynx::RackRenderer r;
  5457. r.prepare (48000.0, 512);
  5458. std::vector<float> l (512), rr (512);
  5459. float peak = 0.0f;
  5460. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  5461. {
  5462. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5463. for (auto v : l)
  5464. peak = std::max (peak, std::abs (v));
  5465. }
  5466. CHECK (peak > 0.01f);
  5467. }
  5468. TEST (audio_clip_fades_and_gain)
  5469. {
  5470. lynx::Song s;
  5471. s.bpm = 120.0;
  5472. s.mode = lynx::PlayMode::song;
  5473. s.mixer = lynx::Song::defaultMixer (1);
  5474. lynx::ChannelDef c;
  5475. c.instrument = lynx::InstrumentKind::audioClip;
  5476. c.samplePath = "dc";
  5477. c.volume = 1.0f;
  5478. c.mixerTrack = 1;
  5479. s.channels.push_back (c);
  5480. s.patterns.emplace_back();
  5481. lynx::Clip clip;
  5482. clip.kind = lynx::ClipKind::audio;
  5483. clip.channel = 0;
  5484. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  5485. clip.fadeIn = lynx::ppq; // 0.5 s
  5486. clip.fadeOut = lynx::ppq;
  5487. clip.gain = 0.5f;
  5488. s.playlist.push_back (clip);
  5489. auto provider = [] (const std::string&) {
  5490. auto d = std::make_shared<lynx::SampleData>();
  5491. d->left.assign (48000 * 4, 1.0f);
  5492. return std::shared_ptr<const lynx::SampleData> (d);
  5493. };
  5494. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  5495. lynx::RackRenderer r;
  5496. r.prepare (48000.0, 512);
  5497. std::vector<float> all;
  5498. std::vector<float> l (512), rr (512);
  5499. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  5500. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5501. {
  5502. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5503. all.insert (all.end(), l.begin(), l.end());
  5504. }
  5505. // FL plays a clip at unity at the default channel volume (78%): at 100% it is that much louder (REA_rodada89 §4.2)
  5506. const auto unity = 1.0f / lynx::channelGain (10000.0f / 12800.0f);
  5507. CHECK (all[100] < 0.01f); // fade-in start
  5508. CHECK (std::abs (all[12000] - 0.25f * unity) < 0.02f); // fade-in midpoint: half of 0.5
  5509. CHECK (std::abs (all[48000] - 0.5f * unity) < 0.01f); // body: the clip gain
  5510. CHECK (std::abs (all[96000 - 12000] - 0.25f * unity) < 0.02f); // fade-out midpoint
  5511. }
  5512. TEST (audio_clip_unity_at_fl_default_volume)
  5513. {
  5514. // a Playlist audio clip at FL's default channel volume (10000/12800) and centre pan plays its sample unchanged
  5515. auto dc = std::make_shared<lynx::SampleData>();
  5516. dc->left.assign (48000, 0.5f);
  5517. auto clip = lynx::makeInstrument (lynx::InstrumentKind::audioClip, dc);
  5518. clip->prepare (48000.0);
  5519. lynx::ChannelDef c;
  5520. c.instrument = lynx::InstrumentKind::audioClip;
  5521. c.volume = 10000.0f / 12800.0f;
  5522. clip->configure (c, 120.0);
  5523. clip->noteOn (60, 1.0f);
  5524. std::vector<float> l (4800, 0.0f), r (4800, 0.0f);
  5525. const auto g = lynx::channelGain (c.volume);
  5526. clip->render (l.data(), r.data(), 4800, g, g);
  5527. CHECK (std::abs (l[2400] - 0.5f) < 1.0e-3f && std::abs (r[2400] - 0.5f) < 1.0e-3f);
  5528. // a sampler channel (pattern notes) keeps the channel volume curve as it was
  5529. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  5530. sampler->prepare (48000.0);
  5531. c.instrument = lynx::InstrumentKind::sampler;
  5532. sampler->configure (c, 120.0);
  5533. sampler->noteOn (60, 1.0f);
  5534. std::fill (l.begin(), l.end(), 0.0f);
  5535. std::fill (r.begin(), r.end(), 0.0f);
  5536. sampler->render (l.data(), r.data(), 4800, g, g);
  5537. CHECK (std::abs (l[2400] - 0.5f * g) < 0.02f);
  5538. }
  5539. TEST (slicer_detects_hits_and_plays_slices_by_key)
  5540. {
  5541. // 4 hits (decaying noise) every 0.25 s
  5542. lynx::SampleData loop;
  5543. loop.sampleRate = 48000.0;
  5544. loop.left.assign (48000, 0.0f);
  5545. lynx::Noise noise;
  5546. for (int hit = 0; hit < 4; ++hit)
  5547. for (int i = 0; i < 6000; ++i)
  5548. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  5549. const auto slices = lynx::detectSlices (loop, 0.5f);
  5550. CHECK (slices.size() == 4);
  5551. for (std::size_t i = 0; i < slices.size(); ++i)
  5552. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  5553. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  5554. auto ramp = std::make_shared<lynx::SampleData>();
  5555. for (int i = 0; i < 48000; ++i)
  5556. ramp->left.push_back ((float) i / 48000.0f);
  5557. ramp->sampleRate = 48000.0;
  5558. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  5559. inst->prepare (48000.0);
  5560. lynx::ChannelDef c;
  5561. c.sampler.slices = slices;
  5562. inst->configure (c, 120.0);
  5563. inst->noteOn (61, 1.0f);
  5564. std::vector<float> l (20000), r (20000);
  5565. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  5566. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  5567. }
  5568. TEST (project_templates_render_sound)
  5569. {
  5570. const auto names = lynx::templateNames();
  5571. CHECK (names.size() == 4);
  5572. for (int t = 1; t < (int) names.size(); ++t)
  5573. {
  5574. auto song = lynx::makeTemplate (t);
  5575. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  5576. lynx::RackRenderer r;
  5577. r.prepare (48000.0, 512);
  5578. std::vector<float> l (512), rr (512);
  5579. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  5580. float peak = 0.0f;
  5581. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  5582. {
  5583. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  5584. for (auto v : l)
  5585. peak = std::max (peak, std::abs (v));
  5586. }
  5587. CHECK (peak > 0.05f && peak < 4.0f);
  5588. }
  5589. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  5590. }
  5591. // --- interface translations (resources/i18n) -------------------------------------------------------
  5592. namespace i18ntest
  5593. {
  5594. std::string readFile (const std::filesystem::path& path)
  5595. {
  5596. std::ifstream in (path, std::ios::binary);
  5597. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  5598. }
  5599. std::string unescape (const std::string& s)
  5600. {
  5601. std::string out;
  5602. for (std::size_t i = 0; i < s.size(); ++i)
  5603. {
  5604. if (s[i] == '\\' && i + 1 < s.size())
  5605. {
  5606. const auto n = s[++i];
  5607. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  5608. }
  5609. else
  5610. out += s[i];
  5611. }
  5612. return out;
  5613. }
  5614. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  5615. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  5616. {
  5617. if (pos >= s.size() || s[pos] != '"')
  5618. return false;
  5619. raw.clear();
  5620. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  5621. {
  5622. if (s[pos] == '\\' && pos + 1 < s.size())
  5623. raw += s[pos++];
  5624. raw += s[pos];
  5625. }
  5626. if (pos >= s.size() || s[pos] != '"')
  5627. return false;
  5628. ++pos;
  5629. return true;
  5630. }
  5631. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  5632. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  5633. {
  5634. std::map<std::string, std::string> table;
  5635. std::istringstream lines (text);
  5636. std::string line;
  5637. while (std::getline (lines, line))
  5638. {
  5639. std::size_t pos = line.find_first_not_of (" \t");
  5640. std::string key, value;
  5641. if (pos == std::string::npos || ! quoted (line, pos, key))
  5642. continue;
  5643. pos = line.find ('"', pos);
  5644. if (pos == std::string::npos || ! quoted (line, pos, value))
  5645. value.clear();
  5646. const auto k = unescape (key);
  5647. if (table.count (k) != 0)
  5648. duplicates.push_back (k);
  5649. table[k] = unescape (value);
  5650. }
  5651. return table;
  5652. }
  5653. // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
  5654. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  5655. {
  5656. std::map<std::string, std::string> keys;
  5657. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  5658. {
  5659. const auto ext = entry.path().extension().string();
  5660. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
  5661. continue;
  5662. // drop // comment lines (they may quote tr ("...") as an example)
  5663. std::string text, line;
  5664. std::istringstream in (readFile (entry.path()));
  5665. while (std::getline (in, line))
  5666. {
  5667. const auto first = line.find_first_not_of (" \t");
  5668. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  5669. text += '\n';
  5670. }
  5671. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  5672. for (const std::string name : { "tr", "TR_NOOP" })
  5673. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  5674. {
  5675. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  5676. continue;
  5677. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  5678. if (pos == std::string::npos || text[pos] != '(')
  5679. continue;
  5680. pos = text.find_first_not_of (" \t\n", pos + 1);
  5681. std::string key, raw;
  5682. bool any = false;
  5683. while (pos != std::string::npos && quoted (text, pos, raw))
  5684. {
  5685. key += raw;
  5686. any = true;
  5687. pos = text.find_first_not_of (" \t\n", pos);
  5688. }
  5689. if (any)
  5690. keys.emplace (unescape (key), entry.path().filename().string());
  5691. }
  5692. }
  5693. return keys;
  5694. }
  5695. std::set<std::string> placeholders (const std::string& s)
  5696. {
  5697. std::set<std::string> found;
  5698. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  5699. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  5700. found.insert (s.substr (open, close - open + 1));
  5701. return found;
  5702. }
  5703. } // namespace i18ntest
  5704. TEST (translations_cover_every_ui_string)
  5705. {
  5706. using namespace i18ntest;
  5707. const auto text = readFile ("resources/i18n/pt-BR.txt");
  5708. CHECK (! text.empty());
  5709. std::vector<std::string> duplicates;
  5710. const auto table = parseTable (text, duplicates);
  5711. CHECK (table.size() > 300);
  5712. CHECK (duplicates.empty());
  5713. for (const auto& d : duplicates)
  5714. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  5715. const auto keys = sourceKeys ("src");
  5716. CHECK (keys.size() > 300);
  5717. int missing = 0;
  5718. for (const auto& [key, file] : keys)
  5719. if (auto it = table.find (key); it == table.end() || it->second.empty())
  5720. {
  5721. ++missing;
  5722. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  5723. }
  5724. CHECK (missing == 0);
  5725. // no empty values, and every {n} placeholder survives the translation
  5726. int empty = 0, badPlaceholders = 0;
  5727. for (const auto& [english, portuguese] : table)
  5728. {
  5729. empty += portuguese.empty() ? 1 : 0;
  5730. if (placeholders (english) != placeholders (portuguese))
  5731. {
  5732. ++badPlaceholders;
  5733. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  5734. }
  5735. }
  5736. CHECK (empty == 0);
  5737. CHECK (badPlaceholders == 0);
  5738. }
  5739. TEST (translations_embedded_table_is_up_to_date)
  5740. {
  5741. // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
  5742. const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
  5743. CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
  5744. CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
  5745. }
  5746. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  5747. TEST (workflow_pattern_navigation_and_insert)
  5748. {
  5749. auto song = lynx::Song::makeDefault();
  5750. song.patterns.resize (3);
  5751. song.patterns[1].name = "B";
  5752. song.patterns[2].name = "C";
  5753. song.patterns[2].notes.push_back ({});
  5754. song.currentPattern = 0;
  5755. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  5756. // pattern 0 has the default notes, 1 is empty, 2 has a note
  5757. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  5758. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  5759. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  5760. song.patterns[1].notes.push_back ({});
  5761. const auto before = song.patterns.size();
  5762. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  5763. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  5764. // insert keeps Playlist clips on their patterns
  5765. lynx::Clip clip;
  5766. clip.pattern = 2;
  5767. song.playlist = { clip };
  5768. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  5769. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  5770. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  5771. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  5772. }
  5773. TEST (workflow_split_pattern_by_channel)
  5774. {
  5775. auto song = lynx::Song::makeDefault();
  5776. song.patterns.resize (1);
  5777. auto& p = song.patterns[0];
  5778. p.name = "Beat";
  5779. p.notes.clear();
  5780. for (int i = 0; i < 4; ++i)
  5781. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  5782. song.channels[2].colour = 0xff112233;
  5783. lynx::Clip clip;
  5784. clip.pattern = 0;
  5785. song.playlist = { clip };
  5786. const auto created = lynx::splitPatternByChannel (song, 0);
  5787. CHECK (created == 2 && song.patterns.size() == 3);
  5788. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  5789. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  5790. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  5791. CHECK (song.playlist[0].pattern == 0);
  5792. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  5793. }
  5794. TEST (workflow_channel_selection)
  5795. {
  5796. lynx::ChannelSelection s;
  5797. s.selectOnly (2);
  5798. s.toggle (5);
  5799. s.toggle (0);
  5800. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  5801. s.toggle (2);
  5802. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  5803. s.selectOnly (3);
  5804. s.extendTo (6);
  5805. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  5806. s.extendTo (1);
  5807. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  5808. s.selectOnly (4);
  5809. s.toggle (4); // the last one stays
  5810. CHECK ((s.rows == std::vector<int> { 4 }));
  5811. s.selectAll (3);
  5812. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  5813. s.rows = { 7, 9 };
  5814. s.clamp (5);
  5815. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  5816. }
  5817. TEST (workflow_move_clone_insert_channels)
  5818. {
  5819. auto song = lynx::Song::makeDefault();
  5820. const auto names = [&] {
  5821. std::vector<std::string> n;
  5822. for (const auto& c : song.channels)
  5823. n.push_back (c.name);
  5824. return n;
  5825. };
  5826. const auto original = names();
  5827. const auto count = (int) song.channels.size();
  5828. CHECK (count >= 4);
  5829. // references that must follow their channel
  5830. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  5831. lynx::Automation a;
  5832. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  5833. song.automations.push_back (a);
  5834. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  5835. lynx::Clip audio;
  5836. audio.kind = lynx::ClipKind::audio;
  5837. audio.channel = 3;
  5838. song.playlist.push_back (audio);
  5839. std::vector<int> selection { 1, 3 };
  5840. CHECK (lynx::moveChannels (song, selection, -1));
  5841. CHECK ((selection == std::vector<int> { 0, 2 }));
  5842. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  5843. && song.channels[3].name == original[2]);
  5844. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  5845. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  5846. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  5847. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  5848. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  5849. // clone: settings, not notes, right below, with a new name
  5850. song.channels[1].volume = 0.33f;
  5851. const auto notesBefore = song.patterns[0].notes.size();
  5852. const auto clone = lynx::cloneChannel (song, 1);
  5853. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  5854. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  5855. CHECK (song.patterns[0].notes.size() == notesBefore);
  5856. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  5857. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  5858. // remove several
  5859. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  5860. CHECK (names() == original);
  5861. }
  5862. TEST (workflow_send_channels_to_free_inserts)
  5863. {
  5864. auto song = lynx::Song::makeDefault();
  5865. for (auto& c : song.channels)
  5866. c.mixerTrack = 0;
  5867. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  5868. song.channels[0].mixerTrack = 2; // not free: used
  5869. song.setRoute (5, 3, true); // 3 is not free: receives a send
  5870. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  5871. song.channels[1].colour = 0xff445566;
  5872. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  5873. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  5874. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  5875. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  5876. // with no free insert left, a new one is added at the end
  5877. auto full = lynx::Song::makeDefault();
  5878. full.mixer = lynx::Song::defaultMixer (1);
  5879. full.channels[0].mixerTrack = 1;
  5880. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  5881. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  5882. }
  5883. TEST (workflow_palette_random_colour)
  5884. {
  5885. const auto& palette = lynx::lynxPalette();
  5886. for (std::uint32_t seed = 0; seed < 50; ++seed)
  5887. {
  5888. const auto current = palette[seed % palette.size()];
  5889. const auto c = lynx::randomPaletteColour (current, seed);
  5890. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  5891. }
  5892. }
  5893. TEST (workflow_key_map)
  5894. {
  5895. using namespace lynx::keys;
  5896. KeyMap map;
  5897. const auto global = Context::global, rack = Context::channelRack;
  5898. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  5899. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  5900. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  5901. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  5902. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  5903. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  5904. // panel context: only while focused
  5905. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  5906. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  5907. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  5908. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  5909. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  5910. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  5911. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  5912. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  5913. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  5914. CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
  5915. CHECK (map.conflicts().empty());
  5916. // chord text
  5917. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  5918. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  5919. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  5920. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  5921. for (const auto& a : actions())
  5922. for (const auto& c : a.defaults)
  5923. CHECK (parseChord (toString (c)) == c);
  5924. // JSON overrides
  5925. std::vector<std::string> errors;
  5926. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  5927. CHECK (errors.size() == 2);
  5928. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  5929. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  5930. map.setChords ("pattern.previous", { { right, ctrl } });
  5931. CHECK (map.conflicts().size() == 1);
  5932. KeyMap reloaded;
  5933. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  5934. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  5935. map.resetToDefaults();
  5936. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  5937. }
  5938. TEST (workflow_workspace_layout)
  5939. {
  5940. using namespace lynx::workspace;
  5941. auto l = Layout::makeDefault (1280, 620);
  5942. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  5943. for (const auto& p : l.panels)
  5944. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  5945. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  5946. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  5947. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  5948. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  5949. // toggle: show+focus, focus, hide
  5950. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  5951. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  5952. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  5953. l.toggleMaximised (Panel::playlist);
  5954. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  5955. // save and load at another size: positions scale
  5956. l[Panel::mixer].detached = true;
  5957. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  5958. const auto text = l.serialise();
  5959. const auto big = Layout::parse (text, 2560, 1240);
  5960. CHECK (big.has_value());
  5961. if (big)
  5962. {
  5963. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  5964. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  5965. CHECK (big->order == l.order && big->focused == l.focused);
  5966. }
  5967. 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));
  5968. // resize keeps panels inside and edges glued to the border
  5969. l.resize (1000, 500);
  5970. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  5971. for (const auto& p : l.panels)
  5972. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  5973. // off-screen panels come back
  5974. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  5975. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  5976. // snapping: to the desktop, to another panel, and resizing one edge
  5977. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  5978. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  5979. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  5980. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  5981. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  5982. }
  5983. TEST (workflow_control_math)
  5984. {
  5985. using namespace lynx::controls;
  5986. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  5987. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  5988. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  5989. 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);
  5990. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  5991. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  5992. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  5993. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  5994. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  5995. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  5996. TapTempo tap;
  5997. CHECK (! tap.tap (1000.0));
  5998. CHECK (tap.tap (1500.0) == 120.0);
  5999. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  6000. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  6001. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  6002. }
  6003. int main (int argc, char** argv)
  6004. {
  6005. if (argc > 1)
  6006. realFlpDir = argv[1];
  6007. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  6008. for (auto& [name, fn] : registry())
  6009. {
  6010. if (only != nullptr && std::strstr (name, only) == nullptr)
  6011. continue;
  6012. std::printf ("%s\n", name);
  6013. fn();
  6014. }
  6015. std::printf ("\n%d checks, %d failures\n", checks, failures);
  6016. return failures == 0 ? 0 : 1;
  6017. }
  6018. TEST (synth_cutoff_keytrack)
  6019. {
  6020. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  6021. auto rms = [] (float keyTrack, int key) {
  6022. lynx::SynthSettings s;
  6023. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6024. s.cutoff = 130.81f;
  6025. s.keyTrack = keyTrack;
  6026. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6027. inst->prepare (48000.0);
  6028. lynx::ChannelDef c;
  6029. c.synth = s;
  6030. inst->configure (c, 120.0);
  6031. inst->noteOn (key, 1.0f);
  6032. std::vector<float> l (24000), r (24000);
  6033. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  6034. double e = 0.0;
  6035. for (int i = 4800; i < 24000; ++i)
  6036. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6037. return std::sqrt (e / 19200.0);
  6038. };
  6039. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  6040. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  6041. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  6042. }
  6043. namespace
  6044. {
  6045. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  6046. {
  6047. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6048. inst->prepare (48000.0);
  6049. lynx::ChannelDef c;
  6050. c.synth = s;
  6051. inst->configure (c, 120.0);
  6052. inst->noteOn (key, 1.0f);
  6053. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  6054. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  6055. return l;
  6056. }
  6057. double goertzel (const std::vector<float>& x, double f, int from, int to)
  6058. {
  6059. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  6060. double s1 = 0.0, s2 = 0.0;
  6061. for (int i = from; i < to; ++i)
  6062. {
  6063. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  6064. s2 = s1;
  6065. s1 = s0;
  6066. }
  6067. return s1 * s1 + s2 * s2 - k * s1 * s2;
  6068. }
  6069. } // namespace
  6070. TEST (synth_pitch_sweep_ring_and_bands)
  6071. {
  6072. lynx::SynthSettings sine;
  6073. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6074. sine.cutoff = 20000.0f;
  6075. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  6076. auto sweep = sine;
  6077. sweep.sweep = 1.0f;
  6078. sweep.sweepTime = 0.01f;
  6079. const auto a = renderSynthNote (sweep, 69, 24000);
  6080. auto crossings = [] (const std::vector<float>& x, int to) {
  6081. int n = 0;
  6082. for (int i = 1; i < to; ++i)
  6083. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  6084. return n;
  6085. };
  6086. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  6087. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  6088. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  6089. auto ring = sine;
  6090. ring.am = ring.ring = true;
  6091. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  6092. const auto b = renderSynthNote (ring, 69, 24000);
  6093. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6094. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  6095. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  6096. auto bands = sine;
  6097. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  6098. auto rms = [] (const std::vector<float>& x) {
  6099. double e = 0.0;
  6100. for (std::size_t i = 4800; i < x.size(); ++i)
  6101. e += (double) x[i] * x[i];
  6102. return std::sqrt (e / (double) (x.size() - 4800));
  6103. };
  6104. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  6105. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  6106. }
  6107. TEST (love_philter_map_evaluation)
  6108. {
  6109. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  6110. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  6111. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  6112. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  6113. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  6114. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  6115. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  6116. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  6117. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  6118. }
  6119. TEST (lofi_holds_and_quantizes)
  6120. {
  6121. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  6122. fx->prepare (48000.0);
  6123. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  6124. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  6125. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  6126. std::vector<float> l (170), r (170);
  6127. for (std::size_t i = 0; i < l.size(); ++i)
  6128. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  6129. const auto original = l;
  6130. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  6131. for (int i = 0; i < 17; ++i)
  6132. CHECK (l[(std::size_t) i] == l[0]);
  6133. CHECK (l[17] != l[16]);
  6134. const auto step = 2.0f * 2049.0f / 65536.0f;
  6135. for (auto v : l)
  6136. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  6137. CHECK (std::abs (l[0] - original[0]) <= step);
  6138. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  6139. }
  6140. namespace
  6141. {
  6142. lynx::EffectParams effectorParams (int type, float x, float y)
  6143. {
  6144. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  6145. slot.params[0] = (float) type / 11.0f;
  6146. slot.params[3] = x;
  6147. slot.params[4] = y;
  6148. return slot.params;
  6149. }
  6150. } // namespace
  6151. TEST (effector_algorithms)
  6152. {
  6153. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  6154. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  6155. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  6156. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  6157. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  6158. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  6159. fx->prepare (48000.0);
  6160. fx->setSongPosition (0.0);
  6161. auto r = in;
  6162. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  6163. return in;
  6164. };
  6165. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  6166. {
  6167. auto p = effectorParams (0, 0.5f, 1.0f);
  6168. const auto out = run (p, std::vector<float> (4800, 0.5f));
  6169. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  6170. const auto small = run (p, std::vector<float> (4800, 0.05f));
  6171. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  6172. }
  6173. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  6174. {
  6175. auto p = effectorParams (11, 0.0f, 0.0f);
  6176. const auto out = run (p, std::vector<float> (48000, 1.0f));
  6177. int crossings = 0;
  6178. for (std::size_t i = 4800; i < out.size(); ++i)
  6179. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  6180. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  6181. }
  6182. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  6183. {
  6184. auto p = effectorParams (8, 1.0f, 0.0f);
  6185. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  6186. const auto out = run (p, std::vector<float> (48000, 1.0f));
  6187. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  6188. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  6189. }
  6190. // Reverb with Wet = 0: the dry passes intact
  6191. {
  6192. auto p = effectorParams (6, 0.5f, 0.5f);
  6193. p[2] = 0.0f;
  6194. std::vector<float> in (4800);
  6195. for (std::size_t i = 0; i < in.size(); ++i)
  6196. in[i] = (float) std::sin (0.01 * (double) i);
  6197. const auto out = run (p, in);
  6198. for (std::size_t i = 0; i < in.size(); ++i)
  6199. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  6200. }
  6201. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  6202. {
  6203. auto p = effectorParams (5, 0.7f, 0.5f);
  6204. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  6205. std::vector<float> in (12000, 0.0f);
  6206. in[0] = 1.0f;
  6207. const auto out = run (p, in);
  6208. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  6209. CHECK (std::abs (out[3000]) < 1e-6f);
  6210. }
  6211. // Stutter: repeats the recorded start every period
  6212. {
  6213. auto p = effectorParams (9, 0.0f, 0.0f);
  6214. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  6215. std::vector<float> in (9000);
  6216. for (std::size_t i = 0; i < in.size(); ++i)
  6217. in[i] = (float) i / 9000.0f;
  6218. const auto out = run (p, in);
  6219. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  6220. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  6221. }
  6222. }
  6223. TEST (crossover_linkwitz_riley_sums_flat)
  6224. {
  6225. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  6226. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  6227. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  6228. {
  6229. auto run = [&] (bool high) {
  6230. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  6231. fx->prepare (48000.0);
  6232. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  6233. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  6234. slot.params[high ? 0 : 3] = 1.0f;
  6235. slot.params[high ? 1 : 4] = value;
  6236. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  6237. std::vector<float> l (24000), r (24000);
  6238. for (std::size_t i = 0; i < l.size(); ++i)
  6239. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  6240. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6241. return l;
  6242. };
  6243. const auto low = run (false), high = run (true);
  6244. double peak = 0.0;
  6245. for (std::size_t i = 12000; i < low.size(); ++i)
  6246. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  6247. CHECK (std::abs (peak - 1.0) < 0.02);
  6248. }
  6249. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  6250. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  6251. fx->prepare (48000.0);
  6252. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  6253. slot.params[3] = 1.0f;
  6254. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  6255. slot.params[5] = 1.0f;
  6256. std::vector<float> l (24000), r (24000);
  6257. for (std::size_t i = 0; i < l.size(); ++i)
  6258. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  6259. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6260. float peak = 0.0f;
  6261. for (std::size_t i = 12000; i < l.size(); ++i)
  6262. peak = std::max (peak, std::abs (l[i]));
  6263. CHECK (peak < 1e-4f);
  6264. }
  6265. TEST (rhythm_gain_and_autopan)
  6266. {
  6267. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  6268. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  6269. slot.params[0] = 1.0f / 64.0f;
  6270. slot.params[1] = 3.0f / 12.0f;
  6271. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  6272. for (int k = 0; k < 3; ++k)
  6273. for (int j = 0; j < 3; ++j)
  6274. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  6275. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  6276. fx->prepare (48000.0);
  6277. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  6278. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  6279. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  6280. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  6281. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  6282. CHECK (l[12000] < 1e-3f);
  6283. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  6284. // positive tension: changes quickly at the start of the segment
  6285. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  6286. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  6287. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6288. pan.params[0] = 1.0f / 64.0f;
  6289. pan.params[1] = 1.0f;
  6290. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  6291. ap->prepare (48000.0);
  6292. ap->setSongPosition (0.0);
  6293. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  6294. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  6295. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  6296. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  6297. }
  6298. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  6299. {
  6300. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  6301. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  6302. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  6303. slot.params[16] = 1.0f;
  6304. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  6305. fx->prepare (48000.0);
  6306. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  6307. std::uint32_t seed = 1;
  6308. for (std::size_t i = 0; i < modL.size(); ++i)
  6309. {
  6310. seed = seed * 1664525u + 1013904223u;
  6311. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6312. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  6313. }
  6314. for (int done = 0; done < 48000; done += 480)
  6315. {
  6316. fx->setSidechain (modL.data() + done, modL.data() + done);
  6317. fx->setSidechain2 (car.data() + done, car.data() + done);
  6318. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  6319. }
  6320. auto rms = [&] (int from, int to) {
  6321. double e = 0.0;
  6322. for (int i = from; i < to; ++i)
  6323. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6324. return std::sqrt (e / (to - from));
  6325. };
  6326. CHECK (rms (6000, 20000) > 0.01);
  6327. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  6328. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  6329. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  6330. for (int done = 0; done < 4800; done += 480)
  6331. {
  6332. fx->setSidechain (nullptr, nullptr);
  6333. fx->setSidechain2 (car.data() + done, car.data() + done);
  6334. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  6335. }
  6336. CHECK (std::abs (a[4700]) < 0.05f);
  6337. }
  6338. TEST (vocoder_follows_modulator)
  6339. {
  6340. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  6341. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  6342. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  6343. fx->prepare (48000.0);
  6344. std::vector<float> l (48000), r (48000);
  6345. std::uint32_t seed = 1;
  6346. for (std::size_t i = 0; i < l.size(); ++i)
  6347. {
  6348. seed = seed * 1664525u + 1013904223u;
  6349. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6350. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  6351. }
  6352. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  6353. auto rms = [&] (int from, int to) {
  6354. double e = 0.0;
  6355. for (int i = from; i < to; ++i)
  6356. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6357. return std::sqrt (e / (to - from));
  6358. };
  6359. CHECK (rms (6000, 20000) > 0.01);
  6360. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  6361. for (std::size_t i = 0; i < l.size(); ++i)
  6362. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  6363. }
  6364. TEST (fruity_vocoder_engine)
  6365. {
  6366. // REA_rodada70: Fruity Vocoder, states of our own against the installation's DLL (goldens = the DLL's output for
  6367. // the same input, EXEC; all three bit-identical over the whole 4096 frames)
  6368. std::vector<float> inL (4096), inR (4096);
  6369. for (int i = 0; i < 4096; ++i)
  6370. {
  6371. inL[(std::size_t) i] = i < 3000 ? (float) (((i * 37) % 200) - 100) / 128.0f : 0.0f; // modulator, then silence
  6372. inR[(std::size_t) i] = (i * 13) % 64 < 32 ? 0.5f : -0.5f; // carrier
  6373. }
  6374. auto make = [] (int bands, int order, int listen, bool swap, std::vector<float> levels, std::array<int, 12> c) {
  6375. lynx::fvocoder::State s;
  6376. s.bands = bands;
  6377. s.order = order;
  6378. s.listen = listen;
  6379. s.swap = swap;
  6380. for (std::size_t k = 0; k < levels.size(); ++k)
  6381. s.levels[k] = levels[k];
  6382. s.c = c;
  6383. return s;
  6384. };
  6385. auto run = [&] (const lynx::fvocoder::State& s, double rate, int block, int holdAt) {
  6386. std::vector<float> l = inL, r = inR;
  6387. lynx::fvocoder::Engine e;
  6388. e.setState (s);
  6389. e.prepare (rate);
  6390. for (int i = 0; i < 4096;)
  6391. {
  6392. auto len = std::min (block, 4096 - i);
  6393. if (holdAt > i && holdAt < i + len)
  6394. len = holdAt - i;
  6395. if (i == holdAt)
  6396. {
  6397. auto t = e.state();
  6398. t.c[lynx::fvocoder::hold] = 1;
  6399. e.setState (t);
  6400. }
  6401. e.processStereo (l.data() + i, r.data() + i, len);
  6402. i += len;
  6403. }
  6404. return std::make_pair (l, r);
  6405. };
  6406. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::pair<int, float>>& golden) {
  6407. for (const auto& [i, g] : golden)
  6408. CHECK (out.first[(std::size_t) i] == g && out.second[(std::size_t) i] == g);
  6409. };
  6410. // 16 bands, order 2, edited band levels, modulator and carrier levels in the mix
  6411. 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 },
  6412. { 0, 65536, 64, 0, 0, 50, 20, 100, 0, 20, 10, 128 });
  6413. const auto outA = run (a, 44100.0, 512, -1);
  6414. check (outA, { { 100, -0.23145467042922974f }, { 1000, 0.2901436686515808f }, { 2047, 0.03407000005245209f }, { 2500, 0.8314149379730225f },
  6415. { 3000, 0.606562614440918f }, { 3500, 0.2637328505516052f }, { 4095, -0.001206810586154461f } });
  6416. // 64 bands, order 3, inverted synthesis bank shifted by −300 cents, 48 kHz in blocks of 300, Hold from frame 2048
  6417. const auto b = make (64, 3, 2, false, {}, { 2000, 60000, 90, 1, -300, 120, 15, 300, 0, 0, 5, 150 });
  6418. check (run (b, 48000.0, 300, 2048), { { 100, 0.005184958688914776f }, { 1000, -0.0008608861826360226f }, { 2047, -0.026466617360711098f },
  6419. { 2500, -0.009371745400130749f }, { 3000, -0.018949978053569794f }, { 3500, -0.02667868137359619f },
  6420. { 4095, -0.021947938948869705f } });
  6421. // 4 bands, order 1, swapped inputs, narrow 30000…50000 range with the linear scale, 22.05 kHz in blocks of 77
  6422. 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 });
  6423. check (run (c, 22050.0, 77, -1), { { 100, 0.31434211134910583f }, { 1000, -1.953513264656067f }, { 2047, 1.4135023355484009f },
  6424. { 2500, 1.539392352104187f }, { 3000, -0.27189597487449646f }, { 3500, -0.11583124846220016f },
  6425. { 4095, -0.11583124846220016f } });
  6426. // FL's state imports into the LYNX slot (controls as parameters, the levels in the state) and the slot's effect
  6427. // (one 4096-frame call, split into 512-frame engine blocks) gives the same output
  6428. const auto bytes = lynx::fvocoder::serialise (a);
  6429. CHECK (bytes.size() == 17 + 16 * 4 + 12 * 4);
  6430. const auto slot = lynx::effectFromFl ("Fruity Vocoder", bytes);
  6431. CHECK (slot && slot->kind == lynx::EffectKind::vocoder && slot->state == bytes);
  6432. CHECK (slot && slot->params[0] == 0.4f && slot->params[1] == 0.5f && slot->params[2] == 1.0f && slot->params[15] == 0.8f);
  6433. if (slot)
  6434. {
  6435. auto fx = lynx::makeEffect (*slot);
  6436. fx->prepare (44100.0);
  6437. std::vector<float> l = inL, r = inR;
  6438. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  6439. CHECK (l == outA.first && r == outA.second);
  6440. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 5, slot->params[5], 120.0) == "16000 Hz");
  6441. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 10, slot->params[10], 120.0) == "20 ms");
  6442. CHECK (lynx::formatEffectParam (lynx::EffectKind::vocoder, 15, slot->params[15], 120.0) == "1.00");
  6443. }
  6444. const auto parsed = lynx::fvocoder::parse (bytes);
  6445. CHECK (parsed && parsed->bands == 16 && parsed->levels[5] == 2.0f && parsed->c[lynx::fvocoder::release] == 100);
  6446. CHECK (! lynx::fvocoder::parse ({ 2, 0, 0, 0, 12, 0, 0, 0 })); // 12 bands is not a valid count
  6447. // a raw control outside the knob range (FL keeps it, REA_rodada78 §1) survives while the knob shows it, clamped
  6448. CHECK (lynx::fvocoder::rawFromKnob (0.0f, lynx::fvocoder::attack, 5) == 5);
  6449. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::attack, 5) == 505);
  6450. CHECK (lynx::fvocoder::rawFromKnob (1.0f, lynx::fvocoder::wetLevel, 250) == 250);
  6451. CHECK (lynx::fvocoder::rawFromKnob (0.5f, lynx::fvocoder::freqScale, 64) == 64);
  6452. }
  6453. // Delphi run-time math (REA_rodada70 §2): values the plugin's own exp / ln / SinCos return (EXEC, called in place)
  6454. TEST (delphi_math_matches_plugin_runtime)
  6455. {
  6456. // arguments where the C library differs from the Delphi run-time in the last bit
  6457. CHECK (lynx::dmath::exp (0x1.f6e5c5a26a7f8p+3) == 0x1.98170c3deea30p+22);
  6458. CHECK (lynx::dmath::exp (-0x1.12fc91a51d906p+4) == 0x1.27108f934a58cp-25);
  6459. CHECK (lynx::dmath::exp (-0x1.2e9e4c229e93ap+4) == 0x1.a3bf2e4ee3f1ep-28);
  6460. CHECK (lynx::dmath::ln (0x1.76a77e2a09abep-1) == -0x1.3fd111c5297f8p-2);
  6461. CHECK (lynx::dmath::ln (0x1.da1feaa8547dfp+0) == 0x1.3b8abd2756774p-1);
  6462. CHECK (lynx::dmath::ln (0x1.a1e28772a0d78p-1) == -0x1.9ffcaa3a11e30p-3);
  6463. double s = 0.0, c = 0.0;
  6464. lynx::dmath::sinCos (0x1.91b4a19e65a9bp-1, s, c);
  6465. CHECK (s == 0x1.69be278761916p-1 && c == 0x1.6a559572e1ac9p-1);
  6466. lynx::dmath::sinCos (0x1.3284a93d70a8ap-1, s, c);
  6467. CHECK (s == 0x1.2088b313ea643p-1 && c == 0x1.a6f4d889fd91ap-1);
  6468. lynx::dmath::sinCos (0x1.f9b2f53d6ce02p+0, s, c);
  6469. CHECK (s == 0x1.d6a9b8f170994p-1 && c == -0x1.931701be7b5f3p-2);
  6470. }
  6471. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  6472. // 13 map-axis bytes, tone, notes)
  6473. struct VocodexSpec
  6474. {
  6475. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  6476. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  6477. 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 };
  6478. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  6479. 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
  6480. std::vector<std::uint8_t> bytes() const
  6481. {
  6482. std::vector<std::uint8_t> d;
  6483. 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); };
  6484. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  6485. i32 (8 << 16);
  6486. for (int m = 0; m < 13; ++m)
  6487. {
  6488. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  6489. const double y1 = m == 8 ? 0.9918 : y0;
  6490. i32 (1), i32 (0), i32 (2);
  6491. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  6492. {
  6493. put (&dx, 8), put (&y, 8);
  6494. i32 (0), i32 (0); // tension (f32 0), flags
  6495. }
  6496. const std::array<std::uint8_t, 20> tail {};
  6497. put (tail.data(), tail.size());
  6498. }
  6499. for (auto v : params)
  6500. i32 (v);
  6501. for (auto v : block)
  6502. i32 (v);
  6503. put (flags.data(), flags.size());
  6504. const std::array<std::uint8_t, 13> axis {};
  6505. put (axis.data(), axis.size());
  6506. d.push_back (3), put ("PWM", 3);
  6507. i32 (1), i32 (60);
  6508. return d;
  6509. }
  6510. };
  6511. std::vector<float> vocodexSaw (int n, double hz, double fs)
  6512. {
  6513. std::vector<float> out ((std::size_t) n);
  6514. for (int i = 0; i < n; ++i)
  6515. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  6516. return out;
  6517. }
  6518. TEST (vocodex_state_parse_and_conversion)
  6519. {
  6520. VocodexSpec spec;
  6521. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  6522. spec.params[17] = -1200;
  6523. const auto raw = spec.bytes();
  6524. const auto s = lynx::vocodex::parseState (raw);
  6525. CHECK (s.has_value());
  6526. if (! s)
  6527. return;
  6528. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  6529. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  6530. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  6531. 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);
  6532. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  6533. const auto v = lynx::vocodexEffect (raw);
  6534. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  6535. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  6536. spec.params[1] = 150; // Soundgoodizer stage
  6537. const auto w = lynx::vocodexEffect (spec.bytes());
  6538. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  6539. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  6540. }
  6541. TEST (vocodex_vocodes_side_inputs)
  6542. {
  6543. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  6544. // output follows the modulator's envelope and fades after it stops
  6545. VocodexSpec spec;
  6546. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  6547. slot.state = spec.bytes();
  6548. auto fx = lynx::makeEffect (slot);
  6549. fx->prepare (48000.0);
  6550. CHECK (fx->latencySamples() == 0);
  6551. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  6552. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  6553. std::uint32_t seed = 7;
  6554. for (std::size_t i = 0; i < mod.size(); ++i)
  6555. {
  6556. seed = seed * 1664525u + 1013904223u;
  6557. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  6558. }
  6559. for (int done = 0; done < 48000; done += 512)
  6560. {
  6561. const int n = std::min (512, 48000 - done);
  6562. fx->setSidechain (mod.data() + done, mod.data() + done);
  6563. fx->setSidechain2 (car.data() + done, car.data() + done);
  6564. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  6565. }
  6566. auto rms = [&] (int from, int to) {
  6567. double e = 0.0;
  6568. for (int i = from; i < to; ++i)
  6569. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  6570. return std::sqrt (e / (2.0 * (to - from)));
  6571. };
  6572. CHECK (rms (6000, 20000) > 0.02);
  6573. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  6574. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  6575. }
  6576. TEST (vocodex_draft_latency_and_pass_through)
  6577. {
  6578. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  6579. // 16 samples late in Draft
  6580. VocodexSpec spec;
  6581. spec.params[0] = 0; // Wet
  6582. spec.params[4] = 12800; // CarVol = 1
  6583. for (const int draftField : { 1, 0 })
  6584. {
  6585. spec.block[4] = draftField;
  6586. const auto s = lynx::vocodex::parseState (spec.bytes());
  6587. CHECK (s.has_value());
  6588. lynx::vocodex::Engine engine;
  6589. engine.prepare (*s, 44100.0, 300, s->draft());
  6590. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  6591. engine.flush();
  6592. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  6593. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  6594. for (int i = 0; i < 3000; ++i)
  6595. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  6596. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  6597. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  6598. {
  6599. n = std::min (sizes[k % 7], 3000 - done);
  6600. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  6601. }
  6602. const int lag = engine.latencySamples();
  6603. auto maxError = 0.0f;
  6604. for (int i = lag; i < 3000; ++i)
  6605. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  6606. CHECK (maxError == 0.0f);
  6607. }
  6608. }
  6609. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  6610. {
  6611. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  6612. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  6613. VocodexSpec spec;
  6614. spec.params[0] = 2000;
  6615. spec.params[1] = 250;
  6616. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  6617. slot.state = spec.bytes();
  6618. auto fx = lynx::makeEffect (slot);
  6619. fx->prepare (48000.0);
  6620. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  6621. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  6622. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  6623. for (std::size_t i = 0; i < mod.size(); ++i)
  6624. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  6625. auto peak = 0.0f;
  6626. for (int done = 0; done < 48000; done += 512)
  6627. {
  6628. const int n = std::min (512, 48000 - done);
  6629. fx->setSidechain (mod.data() + done, mod.data() + done);
  6630. fx->setSidechain2 (car.data() + done, car.data() + done);
  6631. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  6632. }
  6633. for (std::size_t i = 0; i < l.size(); ++i)
  6634. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  6635. CHECK (peak > 0.5f && peak <= 0.9775f);
  6636. }
  6637. TEST (vocodex_tail_bypass_and_noise)
  6638. {
  6639. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  6640. VocodexSpec spec;
  6641. const auto s = lynx::vocodex::parseState (spec.bytes());
  6642. CHECK (s.has_value());
  6643. lynx::vocodex::Engine engine;
  6644. engine.prepare (*s, 44100.0, 512, false);
  6645. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  6646. for (std::size_t i = 0; i < own.size(); ++i)
  6647. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  6648. for (int b = 0; b < 80; ++b)
  6649. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  6650. CHECK (out == own);
  6651. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  6652. spec.params[6] = 9000;
  6653. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  6654. lynx::vocodex::Engine a;
  6655. a.prepare (*noisy, 44100.0, 512, false);
  6656. std::vector<float> mod (1024), first (1024), second (1024);
  6657. for (std::size_t i = 0; i < mod.size(); ++i)
  6658. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  6659. a.flush();
  6660. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  6661. a.flush();
  6662. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  6663. CHECK (first == second);
  6664. auto energy = 0.0;
  6665. for (auto v : first)
  6666. energy += (double) v * v;
  6667. CHECK (energy > 1e-6 && std::isfinite (energy));
  6668. }
  6669. TEST (synth_spectral_oscillator)
  6670. {
  6671. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  6672. lynx::SynthSettings saw;
  6673. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  6674. saw.cutoff = 20000.0f;
  6675. auto spectral = saw;
  6676. spectral.osc[0].wave = lynx::Waveform::spectral;
  6677. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  6678. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  6679. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  6680. for (const auto h : { 1, 2, 3, 5 })
  6681. {
  6682. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  6683. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  6684. }
  6685. // odd only: no 2nd harmonic
  6686. auto odd = spectral;
  6687. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  6688. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  6689. const auto c = renderSynthNote (odd, 57, 24000);
  6690. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  6691. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  6692. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  6693. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  6694. }
  6695. TEST (compressor_sidechain_ducks_and_orders_mixer)
  6696. {
  6697. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  6698. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  6699. slot.params[0] = 0.5f; // threshold
  6700. slot.params[1] = 1.0f; // high ratio
  6701. slot.params[4] = 0.0f;
  6702. auto run = [&] (bool withSidechain) {
  6703. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  6704. fx->prepare (48000.0);
  6705. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  6706. if (withSidechain)
  6707. fx->setSidechain (kick.data(), kick.data());
  6708. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  6709. return l.back();
  6710. };
  6711. const auto alone = run (false), ducked = run (true);
  6712. CHECK (ducked < 0.5f * alone);
  6713. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  6714. auto song = lynx::Song::makeDefault();
  6715. while (song.mixer.size() < 8)
  6716. song.mixer.push_back ({});
  6717. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  6718. comp.sidechain = 7;
  6719. song.mixer[5].effects.push_back (comp);
  6720. const auto order = song.mixerOrder();
  6721. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  6722. CHECK (at (7) < at (5));
  6723. }
  6724. TEST (pitch_correction_pulls_to_allowed_note)
  6725. {
  6726. auto run = [] (const lynx::EffectParams& p, double hz) {
  6727. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6728. fx->prepare (48000.0);
  6729. std::vector<float> l (48000 * 2), r;
  6730. for (std::size_t i = 0; i < l.size(); ++i)
  6731. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  6732. r = l;
  6733. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  6734. // frequency from zero crossings in the last second
  6735. int crossings = 0;
  6736. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  6737. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  6738. return crossings / 2.0;
  6739. };
  6740. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6741. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  6742. for (int k = 0; k < 12; ++k)
  6743. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  6744. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  6745. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  6746. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  6747. }
  6748. TEST (pitch_correction_bypass_is_a_pure_delay)
  6749. {
  6750. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  6751. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  6752. // (REA_rodada30 §2.4/2.5); both are reported as latency
  6753. std::vector<float> in (48000);
  6754. for (std::size_t i = 0; i < in.size(); ++i)
  6755. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  6756. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  6757. auto run = [&] (bool formant, float gender, int& latency) {
  6758. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6759. fx->prepare (48000.0);
  6760. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  6761. slot.params[2] = 1.0f;
  6762. slot.params[29] = formant ? 1.0f : 0.0f;
  6763. slot.params[30] = gender;
  6764. auto l = in, r = in;
  6765. for (std::size_t i = 0; i < l.size(); i += 480)
  6766. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  6767. latency = fx->latencySamples();
  6768. return l;
  6769. };
  6770. int latency = 0;
  6771. auto plain = run (false, 0.5f, latency);
  6772. CHECK (latency == 9);
  6773. auto worst = 0.0f;
  6774. for (std::size_t i = 4000; i < in.size(); ++i)
  6775. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  6776. CHECK (worst < 1e-5f);
  6777. auto formant = run (true, 0.8f, latency);
  6778. CHECK (latency == 257);
  6779. worst = 0.0f;
  6780. for (std::size_t i = 4000; i < in.size(); ++i)
  6781. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  6782. CHECK (worst < 1e-5f);
  6783. }
  6784. TEST (pitch_correction_formant_path)
  6785. {
  6786. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  6787. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  6788. auto run = [] (float gender) {
  6789. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6790. fx->prepare (48000.0);
  6791. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6792. slot.params[29] = 1.0f;
  6793. slot.params[30] = gender;
  6794. std::vector<float> l (48000 * 2), r;
  6795. for (std::size_t i = 0; i < l.size(); ++i)
  6796. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  6797. r = l;
  6798. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  6799. l.erase (l.begin(), l.begin() + 48000);
  6800. return l;
  6801. };
  6802. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  6803. auto pitch = [] (const std::vector<float>& x) {
  6804. auto best = 0.0, bestLag = 0.0;
  6805. for (int lag = 60; lag <= 160; ++lag)
  6806. {
  6807. auto sum = 0.0;
  6808. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  6809. sum += (double) x[i] * x[i + (std::size_t) lag];
  6810. if (sum > best)
  6811. {
  6812. best = sum;
  6813. bestLag = lag;
  6814. }
  6815. }
  6816. return 48000.0 / bestLag;
  6817. };
  6818. // brightness: energy of the first difference relative to the energy
  6819. auto brightness = [] (const std::vector<float>& x) {
  6820. auto e = 0.0, d = 0.0;
  6821. for (std::size_t i = 1; i < x.size(); ++i)
  6822. {
  6823. e += (double) x[i] * x[i];
  6824. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  6825. }
  6826. return d / e;
  6827. };
  6828. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  6829. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  6830. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  6831. CHECK (brightness (up) > 1.5 * brightness (neutral));
  6832. CHECK (brightness (down) < 0.7 * brightness (neutral));
  6833. }
  6834. TEST (parallel_container_inner_links_modulate)
  6835. {
  6836. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  6837. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  6838. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  6839. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  6840. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6841. lfo.params[0] = 1.0f / 64.0f;
  6842. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  6843. container.branches.push_back ({ { gain, lfo }, 1.0f });
  6844. lynx::EffectLink link;
  6845. link.source = 1;
  6846. link.param = 0;
  6847. for (int i = 0; i <= 32; ++i)
  6848. link.curve.push_back (0.5f * (float) i / 32.0f);
  6849. auto levels = [&] (bool linked) {
  6850. auto slot = container;
  6851. if (linked)
  6852. slot.links.push_back (link);
  6853. auto fx = lynx::makeParallelEffect (slot);
  6854. fx->prepare (48000.0);
  6855. float lo = 1e9f, hi = 0.0f;
  6856. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  6857. {
  6858. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  6859. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  6860. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  6861. lo = std::min (lo, std::abs (l[128]));
  6862. hi = std::max (hi, std::abs (l[128]));
  6863. }
  6864. return std::pair { lo, hi };
  6865. };
  6866. const auto [lo, hi] = levels (true);
  6867. CHECK (hi > 0.8f && lo < 0.12f);
  6868. const auto [lo0, hi0] = levels (false);
  6869. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  6870. }
  6871. TEST (synth_spectrum_decays_per_harmonic)
  6872. {
  6873. // 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
  6874. lynx::SynthSettings s;
  6875. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6876. s.cutoff = 20000.0f;
  6877. s.decay = 0.0f;
  6878. s.sustain = 1.0f;
  6879. s.spectrum[0] = 1.0f;
  6880. s.spectrum[2] = 1.0f;
  6881. s.spectrumDecay[2] = 0.2f;
  6882. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  6883. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  6884. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  6885. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  6886. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  6887. CHECK (late3 < 1e-4 * late1);
  6888. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  6889. // without per-harmonic decay the spectrum stays as it is
  6890. s.spectrumDecay[2] = 0.0f;
  6891. const auto y = renderSynthNote (s, 57, 48000);
  6892. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  6893. }
  6894. TEST (worker_pool_runs_each_index_once_per_round)
  6895. {
  6896. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  6897. // exactly once, and nothing running after parallelFor returns
  6898. lynx::WorkerPool pool (3);
  6899. std::vector<std::atomic<int>> hits (64);
  6900. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  6901. std::atomic<bool> open { false };
  6902. int failures = 0;
  6903. for (int r = 1; r <= 20000; ++r)
  6904. {
  6905. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  6906. for (int i = 0; i < n; ++i)
  6907. hits[(std::size_t) i].store (0);
  6908. round.store (r);
  6909. open.store (true);
  6910. auto fn = [&, r] (int i) {
  6911. if (! open.load())
  6912. outside.fetch_add (1);
  6913. if (round.load() != r)
  6914. wrongRound.fetch_add (1);
  6915. hits[(std::size_t) i].fetch_add (1);
  6916. };
  6917. pool.parallelFor (n, fn);
  6918. open.store (false);
  6919. for (int i = 0; i < n; ++i)
  6920. failures += hits[(std::size_t) i].load() != 1;
  6921. }
  6922. CHECK (failures == 0);
  6923. CHECK (outside.load() == 0);
  6924. CHECK (wrongRound.load() == 0);
  6925. }
  6926. TEST (worker_pool_wakes_sleeping_workers)
  6927. {
  6928. // rounds far apart (the workers have gone to sleep on the semaphore in between): every index still runs once,
  6929. // and the sleeping workers are woken (REA round 87: the Windows build used to poll with growing Sleep()s)
  6930. lynx::WorkerPool pool (3);
  6931. std::vector<std::atomic<int>> hits (16);
  6932. int failures = 0;
  6933. for (int r = 0; r < 12; ++r)
  6934. {
  6935. std::this_thread::sleep_for (std::chrono::microseconds (lynx::WorkerPool::spinMicros * 4));
  6936. for (auto& h : hits)
  6937. h.store (0);
  6938. auto fn = [&] (int i) {
  6939. std::this_thread::sleep_for (std::chrono::microseconds (200)); // long enough for woken workers to join
  6940. hits[(std::size_t) i].fetch_add (1);
  6941. };
  6942. pool.parallelFor (16, fn);
  6943. for (auto& h : hits)
  6944. failures += h.load() != 1;
  6945. }
  6946. CHECK (failures == 0);
  6947. CHECK (pool.wakeUps() > 0);
  6948. CHECK (pool.indicesRunByWorkers() > 0);
  6949. }
  6950. TEST (realtime_stats_count_deadline_misses_and_late_calls)
  6951. {
  6952. lynx::RtStats stats;
  6953. // 10 ms deadline: 3 ms, 9 ms (80-100 %), 12 ms (over), then a call 40 ms after the previous one (late)
  6954. stats.record (100.0, 3.0, 10.0);
  6955. stats.record (110.0, 9.0, 10.0);
  6956. stats.record (120.0, 12.0, 10.0);
  6957. stats.record (160.0, 2.0, 10.0);
  6958. const auto s = stats.snapshot();
  6959. CHECK (s.callbacks == 4);
  6960. CHECK (s.overBudget == 1);
  6961. CHECK (s.nearBudget == 1);
  6962. CHECK (s.lateCalls == 1);
  6963. CHECK (std::abs (s.maxLoad - 1.2) < 1e-9);
  6964. CHECK (std::abs (s.averageLoad - 26.0 / 40.0) < 1e-3);
  6965. CHECK (s.histogram[3] == 1 && s.histogram[9] == 1 && s.histogram[12] == 1 && s.histogram[2] == 1);
  6966. CHECK (std::abs (stats.takeWindowMaxLoad() - 1.2) < 1e-9 && stats.takeWindowMaxLoad() == 0.0);
  6967. // a device restart is not a late call
  6968. stats.deviceRestarted();
  6969. stats.record (5000.0, 1.0, 10.0);
  6970. CHECK (stats.snapshot().lateCalls == 1);
  6971. stats.reset();
  6972. CHECK (stats.snapshot().callbacks == 0 && stats.snapshot().maxLoad == 0.0);
  6973. }
  6974. TEST (safe_default_buffer_size)
  6975. {
  6976. const std::vector<int> wasapi { 144, 160, 192, 224, 256, 288, 320, 384, 448, 480, 512, 576, 640, 1024, 2048 };
  6977. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 48000.0) == 512); // the 10 ms default becomes 512
  6978. CHECK (lynx::prefs::safeBufferSize (wasapi, 1024, 48000.0) == 1024); // already larger: kept
  6979. CHECK (lynx::prefs::safeBufferSize (wasapi, 480, 96000.0) == 1024); // ~10.7 ms at 96 kHz
  6980. CHECK (lynx::prefs::safeBufferSize ({ 64, 128, 256 }, 128, 44100.0) == 256); // nothing reaches 512: the largest
  6981. CHECK (lynx::prefs::safeBufferSize ({}, 0, 44100.0) == 0);
  6982. }
  6983. TEST (linear_phase_convolver_matches_direct_convolution)
  6984. {
  6985. // the block FFT convolver (cached twiddles, REA round 87) equals the direct convolution with its kernel
  6986. lynx::peqfl::FftConvolver c;
  6987. c.init (256);
  6988. std::vector<float> table (129, 1.0f);
  6989. for (int k = 40; k < 129; ++k)
  6990. table[(std::size_t) k] = 0.25f; // a high shelf cut
  6991. c.setKernel (table);
  6992. // impulse response through step()
  6993. std::vector<float> ir (600, 0.0f);
  6994. for (int i = 0; i < 600; ++i)
  6995. {
  6996. float l = i == 0 ? 1.0f : 0.0f, r = 0.0f;
  6997. c.step (l, r);
  6998. ir[(std::size_t) i] = l;
  6999. }
  7000. // then a noise signal, compared with direct convolution by that impulse response
  7001. lynx::peqfl::FftConvolver d;
  7002. d.init (256);
  7003. d.setKernel (table);
  7004. std::vector<float> x (2000), y (2000);
  7005. std::uint32_t seed = 1;
  7006. for (auto& v : x)
  7007. v = (float) ((seed = seed * 1664525u + 1013904223u) >> 8) / 16777216.0f - 0.5f;
  7008. for (std::size_t i = 0; i < x.size(); ++i)
  7009. {
  7010. float l = x[i], r = 0.0f;
  7011. d.step (l, r);
  7012. y[i] = l;
  7013. }
  7014. double worst = 0.0;
  7015. for (std::size_t i = 0; i < x.size(); ++i)
  7016. {
  7017. double direct = 0.0;
  7018. for (std::size_t k = 0; k < ir.size() && k <= i; ++k)
  7019. direct += (double) ir[k] * (double) x[i - k];
  7020. worst = std::max (worst, std::abs (direct - (double) y[i]));
  7021. }
  7022. CHECK (worst < 1e-4);
  7023. // and it delays by 3n/4 with the kernel's centre there
  7024. 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());
  7025. CHECK (peak == 192);
  7026. }
  7027. TEST (patcher_container_is_reused_across_snapshots)
  7028. {
  7029. // an unchanged Patcher keeps its built effect (its children's tails, delay lines and PDC stay put), and a
  7030. // linear-phase Frequency Splitter reports its latency before its first block (REA round 87: it used to be
  7031. // rebuilt on every snapshot and its latency appeared one block late, re-planning PDC in a loop)
  7032. auto song = lynx::Song::makeDefault();
  7033. if (song.mixer.size() < 2)
  7034. song.mixer.resize (2);
  7035. lynx::EffectSlot splitter;
  7036. splitter.kind = lynx::EffectKind::frequencySplitter;
  7037. splitter.params = lynx::makeEffectSlot (lynx::EffectKind::frequencySplitter).params;
  7038. splitter.params[4] = 0.6f; // linear-phase mode
  7039. splitter.params[6] = 0.0f; // quality 0
  7040. lynx::EffectSlot patcher;
  7041. patcher.kind = lynx::EffectKind::parallel;
  7042. patcher.branches.push_back ({ { splitter }, 1.0f });
  7043. patcher.branches.push_back ({ {}, 0.5f });
  7044. song.mixer[1].effects = { patcher };
  7045. const auto a = lynx::RackState::build (song, nullptr, 44100.0);
  7046. const auto b = lynx::RackState::build (song, a.get(), 44100.0);
  7047. CHECK (a->effects[1][0] == b->effects[1][0]);
  7048. CHECK (a->effects[1][0]->latencySamples() == 384); // 3/4 of the 512-point FFT, before any block
  7049. CHECK (a->latencies == a->currentLatencies());
  7050. song.mixer[1].effects[0].branches[0].chain[0].params[0] = 0.25f; // a child's parameter changed: rebuilt
  7051. const auto c = lynx::RackState::build (song, b.get(), 44100.0);
  7052. CHECK (c->effects[1][0] != b->effects[1][0]);
  7053. song.mixer[1].effects[0].branches[1].gain = 0.75f;
  7054. const auto d = lynx::RackState::build (song, c.get(), 44100.0);
  7055. CHECK (d->effects[1][0] != c->effects[1][0]);
  7056. }
  7057. TEST (ladder_filter_follows_toxic_law)
  7058. {
  7059. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  7060. // the input saturation keeps it bounded even at full resonance
  7061. auto level = [] (bool ladder, float res, double hz) {
  7062. lynx::SynthSettings s;
  7063. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7064. s.cutoff = 500.0f;
  7065. s.resonance = res;
  7066. s.ladder = ladder;
  7067. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  7068. const auto x = renderSynthNote (s, key, 48000);
  7069. double e = 0.0, peak = 0.0;
  7070. for (std::size_t i = 24000; i < 48000; ++i)
  7071. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  7072. return std::pair { std::sqrt (e / 24000.0), peak };
  7073. };
  7074. 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
  7075. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  7076. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  7077. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  7078. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  7079. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  7080. const auto loud = level (true, 1.0f, 523.25);
  7081. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  7082. }
  7083. TEST (synth_spectrum_settles_on_target)
  7084. {
  7085. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  7086. lynx::SynthSettings s;
  7087. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  7088. s.cutoff = 20000.0f;
  7089. s.decay = 0.0f;
  7090. s.sustain = 1.0f;
  7091. s.spectrum[0] = 1.0f;
  7092. s.spectrum[2] = 1.0f;
  7093. s.spectrumDecay[2] = 0.1f;
  7094. s.spectrumTarget[2] = 0.25f;
  7095. const auto x = renderSynthNote (s, 57, 48000);
  7096. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  7097. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  7098. }
  7099. TEST (kepler_chorus_is_stereo_and_keeps_level)
  7100. {
  7101. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  7102. lynx::SynthSettings s;
  7103. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7104. s.cutoff = 20000.0f;
  7105. auto dry = s;
  7106. s.chorus = 1;
  7107. auto render = [] (const lynx::SynthSettings& set) {
  7108. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7109. inst->prepare (48000.0);
  7110. lynx::ChannelDef c;
  7111. c.synth = set;
  7112. inst->configure (c, 120.0);
  7113. inst->noteOn (69, 1.0f);
  7114. std::vector<float> l (96000), r (96000);
  7115. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  7116. return std::pair { l, r };
  7117. };
  7118. const auto [l, r] = render (s);
  7119. const auto [dl, dr] = render (dry);
  7120. double diff = 0.0, e = 0.0, ed = 0.0;
  7121. for (std::size_t i = 48000; i < 96000; ++i)
  7122. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  7123. CHECK (diff / 48000.0 > 1e-3);
  7124. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  7125. }
  7126. TEST (fl_pan_law_matches_love_philter_dll)
  7127. {
  7128. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  7129. struct Row { double pan, ll, rr, rl, lr; };
  7130. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  7131. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  7132. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  7133. for (const auto& row : rows)
  7134. {
  7135. float ll, rl, lr, rr;
  7136. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  7137. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  7138. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  7139. }
  7140. }
  7141. TEST (effector_units_match_fl_dll_impulse_responses)
  7142. {
  7143. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  7144. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  7145. struct Case { int kind; float x, y; double v[4]; };
  7146. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  7147. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  7148. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  7149. for (const auto& c : cases)
  7150. {
  7151. auto fx = lynx::effector::make();
  7152. fx->prepare (44100.0);
  7153. lynx::EffectParams p {};
  7154. p[lynx::effector::type] = c.kind / 11.0f;
  7155. p[lynx::effector::wetMix] = 1.0f;
  7156. p[lynx::effector::paramX] = c.x;
  7157. p[lynx::effector::paramY] = c.y;
  7158. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  7159. p[lynx::effector::modRate] = 0.5f;
  7160. p[lynx::effector::inputLevel] = 1.0f;
  7161. p[lynx::effector::outputGain] = 0.5f;
  7162. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  7163. l[0] = r[0] = 1.0f;
  7164. for (int d = 0; d < 1024; d += 256)
  7165. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  7166. const int at[4] { 1, 10, 100, 1000 };
  7167. for (int k = 0; k < 4; ++k)
  7168. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  7169. }
  7170. }
  7171. TEST (harmless_stereo_tremolo_and_vibrato)
  7172. {
  7173. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  7174. lynx::SynthSettings s;
  7175. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7176. s.cutoff = 20000.0f;
  7177. s.stereoTremolo = 1.0f;
  7178. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  7179. inst->prepare (48000.0);
  7180. lynx::ChannelDef c;
  7181. c.synth = s;
  7182. inst->configure (c, 120.0);
  7183. inst->noteOn (69, 1.0f);
  7184. std::vector<float> l (48000), r (48000);
  7185. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  7186. 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; };
  7187. // θ = 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
  7188. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  7189. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  7190. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  7191. auto crossingsSpread = [] (float cents) {
  7192. lynx::SynthSettings v;
  7193. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  7194. v.cutoff = 20000.0f;
  7195. v.vibratoCents = cents;
  7196. const auto x = renderSynthNote (v, 69, 48000);
  7197. std::vector<int> zc;
  7198. for (int i = 24000; i < 48000; ++i)
  7199. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  7200. zc.push_back (i);
  7201. int lo = 1 << 30, hi = 0;
  7202. for (std::size_t k = 1; k < zc.size(); ++k)
  7203. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  7204. return hi - lo;
  7205. };
  7206. CHECK (crossingsSpread (0.0f) <= 1);
  7207. CHECK (crossingsSpread (100.0f) >= 4);
  7208. }
  7209. TEST (physical_models_sound_and_end)
  7210. {
  7211. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  7212. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  7213. {
  7214. lynx::ChannelDef c;
  7215. c.instrument = kind;
  7216. if (kind == lynx::InstrumentKind::sakura)
  7217. {
  7218. c.modelParams.assign (146, 0.5f);
  7219. c.modelParams[111] = 3.0f; // both strings at the note
  7220. c.modelParams[113] = 0.5f; // octave ×1
  7221. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  7222. c.modelParams[31] = 0.0f; // noise exciter
  7223. }
  7224. else
  7225. {
  7226. c.modelParams.assign (38, 0.5f);
  7227. c.modelParams[0] = 1.0f; // retrigger noise
  7228. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  7229. }
  7230. auto inst = lynx::makeInstrument (kind);
  7231. inst->prepare (48000.0);
  7232. inst->configure (c, 120.0);
  7233. inst->noteOn (60, 0.8f);
  7234. std::vector<float> l (48000), r (48000);
  7235. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  7236. inst->noteOff (60);
  7237. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  7238. double early = 0.0;
  7239. for (int i = 0; i < 12000; ++i)
  7240. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7241. CHECK (early > 1e-6);
  7242. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  7243. std::vector<float> tail (480000), tailR (480000);
  7244. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  7245. CHECK (inst->idle());
  7246. }
  7247. }
  7248. TEST (squeeze_matches_fl_model)
  7249. {
  7250. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  7251. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  7252. slot.params[1] = 0.0f; // mix 0: dry only
  7253. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  7254. fx->prepare (44100.0);
  7255. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  7256. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  7257. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  7258. slot.params[1] = 1.0f; // wet only
  7259. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  7260. std::vector<float> a (512), b (512);
  7261. for (int i = 0; i < 512; ++i)
  7262. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  7263. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  7264. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  7265. for (int i = 0; i < 512; i += 37)
  7266. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  7267. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  7268. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  7269. CHECK (z[10] == 0.0f);
  7270. }
  7271. TEST (sakura_matrix_and_effects)
  7272. {
  7273. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  7274. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  7275. auto base = [] {
  7276. lynx::ChannelDef c;
  7277. c.instrument = lynx::InstrumentKind::sakura;
  7278. c.modelParams.assign (146, 0.0f);
  7279. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  7280. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  7281. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  7282. for (const auto& [i, v] : values)
  7283. c.modelParams[(std::size_t) i] = v;
  7284. return c;
  7285. };
  7286. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  7287. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  7288. inst->prepare (48000.0);
  7289. inst->configure (c, 120.0);
  7290. inst->noteOn (60, velocity);
  7291. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7292. for (int s = 0; s < total; s += 256)
  7293. {
  7294. if (s == held)
  7295. inst->noteOff (60);
  7296. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  7297. }
  7298. return std::make_pair (l, std::move (inst));
  7299. };
  7300. auto energy = [] (const std::vector<float>& x, int from, int to) {
  7301. double e = 0.0;
  7302. for (int i = from; i < to; ++i)
  7303. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  7304. return e;
  7305. };
  7306. auto c = base();
  7307. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  7308. c.modelParams[80] = 1.0f;
  7309. c.modelParams[82] = 1.0f;
  7310. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  7311. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  7312. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  7313. auto d = base();
  7314. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  7315. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  7316. d.modelParams[64] = 0.5f;
  7317. d.modelParams[69] = 1.0f;
  7318. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  7319. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  7320. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  7321. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  7322. }
  7323. TEST (drumsynth_live_engine)
  7324. {
  7325. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  7326. // sources only, noise from the fallback seed), voice modes, patch level/pan
  7327. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  7328. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  7329. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  7330. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  7331. std::vector<std::uint8_t> blob;
  7332. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  7333. put (3);
  7334. put (66);
  7335. for (int i = 0; i < 120; ++i)
  7336. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  7337. for (int i = 0; i < 120 * 38; ++i)
  7338. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  7339. put (0);
  7340. const auto parsed = lynx::drumSynthLiveParams (blob);
  7341. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  7342. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  7343. blob.resize (500);
  7344. CHECK (! lynx::drumSynthLiveParams (blob));
  7345. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  7346. 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,
  7347. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  7348. lynx::ChannelDef c;
  7349. c.instrument = lynx::InstrumentKind::drumsynth;
  7350. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  7351. for (int s = 0; s < 120; ++s)
  7352. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  7353. for (int i = 0; i < 38; ++i)
  7354. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  7355. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  7356. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  7357. inst->configure (def, 120.0);
  7358. inst->prepare (48000.0);
  7359. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7360. for (int s = 0; s < total; s += 512)
  7361. {
  7362. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  7363. if (keys[k + 1] == s)
  7364. inst->noteOn (keys[k], 100.0f / 128.0f);
  7365. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7366. }
  7367. return std::make_tuple (l, r, std::move (inst));
  7368. };
  7369. {
  7370. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  7371. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  7372. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  7373. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  7374. for (const auto& [i, v] : golden)
  7375. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  7376. std::vector<float> tail (48000 * 4), tailR (tail.size());
  7377. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7378. CHECK (inst->idle()); // the engine reported the voice finished
  7379. }
  7380. auto energy = [] (const std::vector<float>& x, int from, int to) {
  7381. double e = 0.0;
  7382. for (int i = from; i < to; ++i)
  7383. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  7384. return e;
  7385. };
  7386. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  7387. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  7388. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  7389. auto tone = c;
  7390. tone.modelParams[38 * 12 + 14] = 0.0f;
  7391. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  7392. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  7393. auto poly = tone;
  7394. poly.modelParams[38 * 12 + 24] = 1.0f;
  7395. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  7396. double monoDiff = 0.0, polyDiff = 0.0;
  7397. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  7398. 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]);
  7399. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  7400. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  7401. auto right = c;
  7402. right.modelParams[38 * 12 + 1] = 64.0f;
  7403. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  7404. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  7405. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  7406. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  7407. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  7408. }
  7409. TEST (kepler_lfo_bpm_sync)
  7410. {
  7411. // 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)
  7412. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  7413. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  7414. putU32 (12, 9);
  7415. std::memcpy (state.data() + 16, "Kepler-1", 9);
  7416. putU32 (0x19, 4);
  7417. putU32 (0x1D, 34);
  7418. float k[34] {};
  7419. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  7420. k[6] = 1.0f;
  7421. k[10] = 1.0f; // vibrato on
  7422. k[33] = 1.0f;
  7423. std::memcpy (state.data() + 0x21, k, sizeof k);
  7424. lynx::SynthSettings s;
  7425. float gain = 0.0f;
  7426. CHECK (lynx::synthFromKepler (state, s, gain));
  7427. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  7428. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  7429. k[5] = 0.0f; // slowest: 32 beats
  7430. std::memcpy (state.data() + 0x21, k, sizeof k);
  7431. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  7432. k[6] = 0.0f; // free rate in seconds
  7433. std::memcpy (state.data() + 0x21, k, sizeof k);
  7434. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  7435. }
  7436. namespace
  7437. {
  7438. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  7439. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  7440. // velocity map — and the global block. Every other curve is neutral.
  7441. // features (REA_rodada75): 1 arpeggio markers on the volume envelope, 2 noise resonance on filter 1, 4 "pluck replaces
  7442. // blur decay", 8 pluck blend, 16 harmonic randomness, 32 phaser + harmonizer masks
  7443. std::vector<std::uint8_t> harmorTestState (bool panLfo = true, bool image = false, int features = 0)
  7444. {
  7445. std::vector<std::uint8_t> b;
  7446. 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))); };
  7447. 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))); };
  7448. struct Pt { double dx, y, tension; int mode; int arp = 0; };
  7449. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  7450. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  7451. for (const auto& p : pts)
  7452. {
  7453. f64 (p.dx), f64 (p.y);
  7454. const auto t = (float) p.tension;
  7455. std::uint32_t u;
  7456. std::memcpy (&u, &t, 4);
  7457. i32 ((std::int32_t) u);
  7458. b.push_back ((std::uint8_t) p.mode), b.push_back ((std::uint8_t) p.arp), b.push_back (0), b.push_back (0);
  7459. }
  7460. for (auto h : hdr)
  7461. i32 (h);
  7462. };
  7463. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  7464. 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); };
  7465. i32 (18);
  7466. i32 (1 | 4);
  7467. std::vector<std::uint8_t> part;
  7468. std::swap (b, part);
  7469. std::array<std::int32_t, 386> p {};
  7470. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  7471. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  7472. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  7473. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  7474. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  7475. for (const auto& [i, v] : values)
  7476. p[(std::size_t) i] = v;
  7477. if ((features & 2) != 0)
  7478. p[57] = 400, p[59] = 300; // filter-1 resonance amount and width (noise type set in the combo block)
  7479. if ((features & 4) != 0)
  7480. p[75] = 700, p[21] = 300; // pluck value, time-blur attack
  7481. if ((features & 8) != 0)
  7482. p[75] = 650, p[4] = 600; // pluck, timbre mix
  7483. if ((features & 32) != 0)
  7484. p[76] = 700, p[27] = 600; // phaser mix, harmonizer mix
  7485. if (image) // image synthesis (REA_rodada75): speed, cubic gain / linear freq interpolation, Hz freq mode, formant
  7486. for (const auto& [i, v] : std::initializer_list<std::pair<int, int>> { { 82, 200 }, { 83, 100000 }, { 84, 10000 }, { 85, 20000 },
  7487. { 86, 750 }, { 87, 4 }, { 88, 600 }, { 89, 900 }, { 90, 1 }, { 91, 3 }, { 92, 1500 }, { 93, -300 }, { 94, 800 } })
  7488. p[(std::size_t) i] = v;
  7489. for (auto v : p)
  7490. i32 (v);
  7491. for (int i = 0; i < 12; ++i)
  7492. i32 (i); // module order
  7493. std::array<std::uint8_t, 0x4b> misc {};
  7494. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  7495. const float one = 1.0f;
  7496. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  7497. put (0x34, 0), put (0x38, 4), put (0x3c, (features & 2) != 0 ? 109 : 100), put (0x40, 108);
  7498. if ((features & 8) != 0)
  7499. put (0x00, 5);
  7500. misc[0x44] = 1;
  7501. if ((features & 4) != 0)
  7502. misc[0x46] = 1;
  7503. b.insert (b.end(), misc.begin(), misc.end());
  7504. 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 };
  7505. for (int k = 0; k < 43; ++k)
  7506. {
  7507. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  7508. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  7509. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  7510. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  7511. i32 (3);
  7512. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  7513. if (k == 1 && (features & 1) != 0) // arpeggio volume envelope: a looping pattern with chord markers
  7514. 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 },
  7515. { 0.1, 0, 0.2, 0 } },
  7516. { 0, 1, -1, 0, 4 });
  7517. else if (k == 1) // volume envelope: attack, decay to the sustain point, release
  7518. 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 });
  7519. else if (k == 4) // filter-1 envelope
  7520. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  7521. else
  7522. flat (neutral);
  7523. for (int a : { 128, 128, 0, 128 })
  7524. i32 (a);
  7525. if (k == 0 && panLfo) // pan LFO
  7526. flat (1.0, on);
  7527. else
  7528. flat (0.5);
  7529. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  7530. i32 (a);
  7531. i32 (0);
  7532. for (int m = 2; m < 11; ++m)
  7533. if (k == 1 && m == 3)
  7534. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  7535. else
  7536. flat (neutral);
  7537. }
  7538. for (int c = 0; c < 26; ++c)
  7539. if ((features & 16) != 0 && c == 4)
  7540. curve ({ { 0, 0.5, 0, 0 }, { 0.2, 0.8, 0.3, 0 }, { 0.8, 0.3, 0, 0 } }, off); // harmonic randomness
  7541. else if ((features & 32) != 0 && (c == 22 || c == 23))
  7542. curve ({ { 0, 0.3, 0, 0 }, { 0.6, 1.0, 0.2, 0 }, { 0.4, 0.5, 0, 0 } }, off); // phaser / harmonizer masks
  7543. else if (c == 3 && (features & 8) != 0)
  7544. curve ({ { 0, 0.2, 0, 0 }, { 1, 0.05, 0.4, 0 } }, off); // timbre 2 for the pluck blend
  7545. else if (c == 2 || c == 3)
  7546. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  7547. else if (c == 14)
  7548. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  7549. else
  7550. 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)));
  7551. if (image)
  7552. {
  7553. // the image object (FUN_00793300 layout): version 0, image mode, looping, 6 columns × 40 rows, gain + freq planes
  7554. const int cols = 6, rows = 40;
  7555. i32 (0), i32 (0), i32 (1), i32 (0);
  7556. f64 (-1.0), f64 (-1.0), f64 (0.0);
  7557. i32 (cols), i32 (rows);
  7558. i32 (cols * rows);
  7559. for (int c2 = 0; c2 < cols; ++c2)
  7560. for (int r2 = 0; r2 < rows; ++r2)
  7561. {
  7562. const auto v = (float) (255.0 * std::pow (0.93, r2) * (0.6 + 0.4 * std::cos (0.9 * c2 + 0.37 * r2)));
  7563. std::uint32_t u;
  7564. std::memcpy (&u, &v, 4);
  7565. i32 ((std::int32_t) u);
  7566. }
  7567. i32 (cols * rows);
  7568. for (int c2 = 0; c2 < cols; ++c2)
  7569. for (int r2 = 0; r2 < rows; ++r2)
  7570. {
  7571. const auto v = (float) (0.3 * std::sin (1.3 * c2 + 0.5 * r2));
  7572. std::uint32_t u;
  7573. std::memcpy (&u, &v, 4);
  7574. i32 ((std::int32_t) u);
  7575. }
  7576. i32 (0);
  7577. b.push_back (0); // (file name: empty)
  7578. }
  7579. std::swap (b, part);
  7580. i32 ((std::int32_t) part.size());
  7581. b.insert (b.end(), part.begin(), part.end());
  7582. std::array<std::int32_t, 53> g {};
  7583. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  7584. for (auto v : g)
  7585. i32 (v);
  7586. for (int i = 0; i < 6; ++i)
  7587. i32 (i);
  7588. std::array<std::uint8_t, 0x3d> gm {};
  7589. const std::int32_t cr = 200, vr = 1;
  7590. std::memcpy (gm.data() + 12, &vr, 4);
  7591. std::memcpy (gm.data() + 24, &cr, 4);
  7592. b.insert (b.end(), gm.begin(), gm.end());
  7593. return b;
  7594. }
  7595. } // namespace
  7596. TEST (harmor_engine)
  7597. {
  7598. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  7599. const auto blob = harmorTestState();
  7600. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  7601. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  7602. CHECK (lynx::harmorStateUsable (blob));
  7603. CHECK (lynx::harmorFallbacks (blob).empty());
  7604. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  7605. std::int32_t cutoff = 0;
  7606. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  7607. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  7608. auto broken = blob;
  7609. broken.resize (broken.size() / 2);
  7610. CHECK (! lynx::harmorStateUsable (broken));
  7611. auto old = blob;
  7612. old[0] = 15; // articulators and curves are stored from version 16
  7613. CHECK (! lynx::harmorStateUsable (old));
  7614. lynx::ChannelDef c;
  7615. c.instrument = lynx::InstrumentKind::harmor;
  7616. c.pluginState = blob;
  7617. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  7618. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  7619. inst->configure (def, 120.0);
  7620. inst->prepare (44100.0);
  7621. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7622. inst->noteOn (60, 0.78125f);
  7623. for (int s = 0; s < total; s += 512)
  7624. {
  7625. if (s == offAt)
  7626. inst->noteOff (60);
  7627. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7628. }
  7629. return std::make_tuple (l, r, std::move (inst));
  7630. };
  7631. {
  7632. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  7633. // Mersenne Twister seeded like LYNX's)
  7634. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7635. const std::tuple<int, float, float> golden[] {
  7636. { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
  7637. { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
  7638. for (const auto& [i, gl, gr] : golden)
  7639. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7640. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  7641. std::vector<float> tail (44100), tailR (tail.size());
  7642. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7643. CHECK (inst->idle());
  7644. CHECK (std::abs (tail.back()) == 0.0f);
  7645. }
  7646. // the reference renderer gives the same samples as the instrument
  7647. {
  7648. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  7649. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  7650. double worst = 0.0;
  7651. for (int i = 0; i < 8192; ++i)
  7652. 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]) });
  7653. CHECK (worst == 0.0);
  7654. }
  7655. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  7656. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  7657. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  7658. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  7659. auto mono = c;
  7660. mono.pluginState = harmorTestState (false); // no pan LFO ...
  7661. const std::int32_t one = 1;
  7662. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  7663. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  7664. double el = 0.0, er = 0.0;
  7665. for (int i = 0; i < 2048; ++i)
  7666. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7667. CHECK (el < 1e-10 * er && er > 1e-3);
  7668. }
  7669. namespace
  7670. {
  7671. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  7672. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  7673. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  7674. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  7675. std::vector<std::uint8_t> ogunTestState()
  7676. {
  7677. std::vector<std::uint8_t> b;
  7678. 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))); };
  7679. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  7680. 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))); };
  7681. struct Pt { double dx, y, tension; int mode; };
  7682. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  7683. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  7684. for (const auto& p : pts)
  7685. {
  7686. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  7687. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  7688. }
  7689. for (auto h : hdr)
  7690. i32 (h);
  7691. };
  7692. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  7693. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  7694. std::array<std::int32_t, 64> P {};
  7695. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  7696. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  7697. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  7698. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  7699. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  7700. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  7701. { 56, 0x00010000 }, // +0x20e: declick
  7702. { 62, 0x00000100 } }; // +0x225: normalise
  7703. for (const auto& [i, v] : values)
  7704. P[(std::size_t) i] = v;
  7705. for (auto v : P)
  7706. i32 (v);
  7707. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  7708. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  7709. b.push_back (0); // comment: 0 characters
  7710. for (int k = 0; k < 13; ++k)
  7711. {
  7712. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  7713. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  7714. i32 (1);
  7715. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  7716. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  7717. 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 });
  7718. else
  7719. flat (1, neutral);
  7720. for (int a : { 128, 128, 128, 128 })
  7721. i32 (a);
  7722. if (k == 1) // cutoff LFO
  7723. curve (2, { { 0, 0.8, 0, 0 } }, on);
  7724. else
  7725. flat (2, 0.5);
  7726. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  7727. i32 (a);
  7728. i32 (0);
  7729. for (int m = 0; m < 4; ++m)
  7730. if (k == 0 && m == 1)
  7731. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  7732. else
  7733. flat (3, neutral);
  7734. }
  7735. flat (3, 0.0); // Forced: none
  7736. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  7737. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  7738. flat (3, 0.5); // Release EQ
  7739. // u32 version + zlib (stored blocks)
  7740. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  7741. for (std::size_t i = 0; i < b.size();)
  7742. {
  7743. const auto len = std::min<std::size_t> (65535, b.size() - i);
  7744. s.push_back (i + len >= b.size() ? 1 : 0);
  7745. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  7746. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  7747. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  7748. i += len;
  7749. }
  7750. std::uint32_t a1 = 1, a2 = 0;
  7751. for (auto c : b)
  7752. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  7753. for (int sh = 24; sh >= 0; sh -= 8)
  7754. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  7755. return s;
  7756. }
  7757. } // namespace
  7758. TEST (ogun_engine)
  7759. {
  7760. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  7761. const auto blob = ogunTestState();
  7762. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  7763. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  7764. CHECK (lynx::ogunStateUsable (blob));
  7765. CHECK (lynx::ogunFallbacks (blob).empty());
  7766. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
  7767. auto broken = blob;
  7768. broken.resize (broken.size() - 300);
  7769. CHECK (! lynx::ogunStateUsable (broken));
  7770. auto old = blob;
  7771. old[0] = 13; // the articulator layout is from version 14
  7772. CHECK (! lynx::ogunStateUsable (old));
  7773. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  7774. // released at 8192; host block 512)
  7775. 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 } };
  7776. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  7777. CHECK (ref.size() == 2 * 16384);
  7778. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  7779. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  7780. const std::tuple<int, float, float> golden[] {
  7781. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  7782. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  7783. { 16383, -0.00465311f, -0.00119627f } };
  7784. for (const auto& [i, gl, gr] : golden)
  7785. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  7786. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  7787. lynx::ChannelDef c;
  7788. c.instrument = lynx::InstrumentKind::ogun;
  7789. c.pluginState = blob;
  7790. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  7791. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  7792. inst->configure (def, 120.0);
  7793. inst->prepare (44100.0);
  7794. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  7795. for (int s = 0; s < total; s += 512)
  7796. {
  7797. if (s == 0)
  7798. inst->noteOn (60, 0.75f);
  7799. if (s == 2048)
  7800. inst->noteOn (67, 0.625f);
  7801. if (s == 6144 && ! held)
  7802. inst->noteOff (60);
  7803. if (s == 8192 && ! held)
  7804. inst->noteOff (67);
  7805. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  7806. }
  7807. return std::make_tuple (l, r, std::move (inst));
  7808. };
  7809. {
  7810. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  7811. double worst = 0.0;
  7812. for (int i = 0; i < 16384; ++i)
  7813. 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]) });
  7814. CHECK (worst == 0.0);
  7815. // the voices end and the effect tails die out: the instrument goes idle
  7816. std::vector<float> tail (44100), tailR (tail.size());
  7817. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  7818. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7819. CHECK (inst->idle());
  7820. }
  7821. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  7822. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  7823. {
  7824. auto dry = c;
  7825. for (int i : { 19, 29, 40, 53 })
  7826. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  7827. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  7828. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  7829. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  7830. double worst = 0.0, peak = 0.0;
  7831. for (int i = 30000; i < 32768; ++i)
  7832. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  7833. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  7834. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  7835. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  7836. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  7837. const auto V = lynx::channelGain (0.78125f);
  7838. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  7839. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  7840. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  7841. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  7842. double ei = 0.0, er0 = 0.0;
  7843. for (int i = 30000; i < 32768; ++i)
  7844. 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];
  7845. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  7846. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  7847. double el = 0.0, er = 0.0;
  7848. for (int i = 0; i < 4096; ++i)
  7849. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  7850. CHECK (el < 1e-10 * er && er > 1e-3);
  7851. }
  7852. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  7853. {
  7854. lynx::setOfflineRendering (true);
  7855. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  7856. lynx::setOfflineRendering (false);
  7857. lynx::ogun::RefOptions q4;
  7858. q4.quality = 4;
  7859. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  7860. double worst = 0.0, diff = 0.0;
  7861. for (int i = 0; i < 8192; ++i)
  7862. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  7863. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  7864. CHECK (worst == 0.0 && diff > 1e-3);
  7865. const std::tuple<int, float, float> golden4[] {
  7866. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  7867. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  7868. for (const auto& [i, gl, gr] : golden4)
  7869. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  7870. }
  7871. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  7872. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  7873. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  7874. {
  7875. struct Sample { int at; float l, r; };
  7876. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  7877. const Variant variants[] {
  7878. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  7879. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  7880. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  7881. { "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 } } },
  7882. { "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 } } },
  7883. { "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 } } },
  7884. { "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 } } },
  7885. { "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 } } },
  7886. { "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 } } },
  7887. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  7888. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  7889. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  7890. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  7891. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  7892. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  7893. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  7894. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  7895. };
  7896. for (const auto& v : variants)
  7897. {
  7898. auto st = blob;
  7899. for (const auto& [i, x] : v.params)
  7900. CHECK (lynx::ogunSetParam (st, i, x));
  7901. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  7902. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  7903. double el = 0.0, er = 0.0, peak = 0.0;
  7904. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7905. 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]) });
  7906. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  7907. for (const auto& smp : v.s)
  7908. 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;
  7909. if (! ok)
  7910. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  7911. CHECK (ok);
  7912. }
  7913. }
  7914. }
  7915. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  7916. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  7917. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  7918. {
  7919. std::vector<std::uint8_t> d;
  7920. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  7921. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  7922. i32 (7);
  7923. for (int copy = 0; copy < 2; ++copy)
  7924. {
  7925. for (auto v : g)
  7926. i32 (v);
  7927. for (const auto& b : bands)
  7928. for (auto v : b)
  7929. i32 (v);
  7930. }
  7931. d.resize (0x1fe, 0);
  7932. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  7933. for (const auto& e : envs)
  7934. {
  7935. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  7936. for (const auto& pt : e)
  7937. {
  7938. const double dx = pt[0], y = pt[1];
  7939. const float t = (float) pt[2];
  7940. put (&dx, 8), put (&y, 8), put (&t, 4);
  7941. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  7942. put (flags, 4);
  7943. }
  7944. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  7945. }
  7946. return d;
  7947. }
  7948. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  7949. std::vector<std::uint8_t> keplerTestState()
  7950. {
  7951. 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 };
  7952. std::vector<std::uint8_t> st;
  7953. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  7954. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  7955. u32 (0), u32 (0), u32 (0), u32 (9);
  7956. for (char c : std::string ("Kepler-1"))
  7957. st.push_back ((std::uint8_t) c);
  7958. st.push_back (0);
  7959. u32 (2), u32 (34);
  7960. for (auto f : panel)
  7961. f32 (f);
  7962. st.push_back (0);
  7963. f32 (1.0f), f32 (0.5f);
  7964. const auto size = (std::uint32_t) st.size() - 12;
  7965. std::memcpy (st.data() + 4, &size, 4);
  7966. return st;
  7967. }
  7968. std::string keplerDll()
  7969. {
  7970. if (realFlpDir.empty())
  7971. return {};
  7972. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  7973. }
  7974. TEST (kepler_engine)
  7975. {
  7976. const auto st = keplerTestState();
  7977. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  7978. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  7979. auto broken = st;
  7980. broken[17] = 'X'; // not "Kepler-1"
  7981. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  7982. auto v3 = st;
  7983. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  7984. CHECK (! lynx::keplerStateUsable (v3));
  7985. const auto dll = keplerDll();
  7986. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  7987. return; // no FL installation (the engine needs the user's own DLL tables)
  7988. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  7989. 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 } };
  7990. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  7991. CHECK (y.size() == 2 * 39690);
  7992. if (y.size() != 2 * 39690)
  7993. return;
  7994. double el = 0.0, er = 0.0;
  7995. for (std::size_t i = 0; i < y.size(); i += 2)
  7996. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7997. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  7998. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  7999. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  8000. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  8001. for (const auto& [i, l, r] : golden)
  8002. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  8003. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  8004. lynx::ChannelDef c;
  8005. c.instrument = lynx::InstrumentKind::kepler;
  8006. c.pluginState = st;
  8007. c.samplePath = dll;
  8008. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  8009. inst->configure (c, 120.0);
  8010. inst->prepare (44100.0);
  8011. std::vector<float> l (4096), r (4096);
  8012. inst->noteOn (60, 0.8f);
  8013. double e = 0.0;
  8014. bool finite = true;
  8015. for (int b = 0; b < 8; ++b)
  8016. {
  8017. std::fill (l.begin(), l.end(), 0.0f);
  8018. std::fill (r.begin(), r.end(), 0.0f);
  8019. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8020. for (int i = 0; i < 4096; ++i)
  8021. 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];
  8022. if (b == 3)
  8023. inst->noteOff (60);
  8024. }
  8025. CHECK (finite && e > 1.0);
  8026. for (int b = 0; b < 40; ++b)
  8027. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8028. CHECK (inst->idle());
  8029. }
  8030. // REA round 57 §2: Transistor Bass
  8031. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  8032. std::vector<std::uint8_t> tbassPresetState (const char* name)
  8033. {
  8034. if (realFlpDir.empty())
  8035. return {};
  8036. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  8037. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8038. std::size_t i = 0;
  8039. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8040. ++i;
  8041. i += 8;
  8042. while (i < b.size())
  8043. {
  8044. const auto id = b[i++];
  8045. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8046. if (id >= 192)
  8047. for (int shift = 0; i < b.size(); shift += 7)
  8048. {
  8049. const auto c = b[i++];
  8050. n |= (std::size_t) (c & 0x7F) << shift;
  8051. if (c < 0x80)
  8052. break;
  8053. }
  8054. if (id == 0xD5 && i + n <= b.size())
  8055. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8056. i += n;
  8057. }
  8058. return {};
  8059. }
  8060. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  8061. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  8062. {
  8063. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  8064. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  8065. for (std::size_t at = 4; at + 12 <= s.size();)
  8066. {
  8067. std::uint32_t id;
  8068. std::uint64_t n;
  8069. std::memcpy (&id, s.data() + at, 4);
  8070. std::memcpy (&n, s.data() + at + 4, 8);
  8071. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  8072. at += 12 + (std::size_t) n;
  8073. if (id == 2)
  8074. {
  8075. std::uint64_t m;
  8076. std::memcpy (&m, d.data() + 4, 8);
  8077. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  8078. std::int16_t v;
  8079. std::memcpy (&v, e.data(), 2);
  8080. if (v < 5)
  8081. {
  8082. v = 5;
  8083. std::memcpy (e.data(), &v, 2);
  8084. e.insert (e.end(), 4, 0);
  8085. }
  8086. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  8087. m = e.size();
  8088. d.resize (12);
  8089. std::memcpy (d.data() + 4, &m, 8);
  8090. d.insert (d.end(), e.begin(), e.end());
  8091. }
  8092. const std::uint64_t size = d.size();
  8093. put (&id, 4);
  8094. put (&size, 8);
  8095. put (d.data(), d.size());
  8096. }
  8097. return out;
  8098. }
  8099. std::string tbassDll()
  8100. {
  8101. if (realFlpDir.empty())
  8102. return {};
  8103. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  8104. }
  8105. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  8106. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  8107. TEST (plucked_engine)
  8108. {
  8109. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  8110. std::vector<std::uint8_t> st (20);
  8111. std::memcpy (st.data(), std::data (k), 20);
  8112. return st;
  8113. };
  8114. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  8115. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  8116. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  8117. using N = lynx::plucked::RefNote;
  8118. struct Sample { int at; float l, r; };
  8119. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  8120. const std::vector<std::vector<N>> sets {
  8121. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  8122. { { 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 } },
  8123. { { 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 } },
  8124. { { 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 } },
  8125. { { 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 } },
  8126. };
  8127. const Golden goldens[] {
  8128. { 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 } } },
  8129. { 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 } } },
  8130. { 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 } } },
  8131. { 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 } } },
  8132. { 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 } } },
  8133. };
  8134. for (const auto& g : goldens)
  8135. {
  8136. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  8137. const auto length = (int) g.fs * 2;
  8138. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  8139. g.law != 0, g.maxPoly);
  8140. double el = 0.0, er = 0.0;
  8141. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8142. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8143. 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;
  8144. for (const auto& smp : g.s)
  8145. 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;
  8146. if (! ok)
  8147. 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);
  8148. CHECK (ok);
  8149. }
  8150. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  8151. lynx::ChannelDef c;
  8152. c.instrument = lynx::InstrumentKind::plucked;
  8153. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  8154. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  8155. inst->configure (c, 120.0);
  8156. inst->prepare (44100.0);
  8157. std::vector<float> l (1024), r (1024);
  8158. inst->noteOn (57, 0.8f);
  8159. double e = 0.0;
  8160. for (int b = 0; b < 8; ++b)
  8161. {
  8162. std::fill (l.begin(), l.end(), 0.0f);
  8163. std::fill (r.begin(), r.end(), 0.0f);
  8164. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8165. for (int i = 0; i < 1024; ++i)
  8166. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  8167. }
  8168. CHECK (e > 1.0 && ! inst->idle());
  8169. inst->noteOff (57);
  8170. for (int b = 0; b < 4; ++b)
  8171. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8172. CHECK (inst->idle());
  8173. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  8174. inst->configure (c, 120.0);
  8175. inst->noteOn (60, 1.0f);
  8176. inst->noteOff (60); // (no gate: the string rings on)
  8177. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  8178. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8179. CHECK (inst->idle());
  8180. }
  8181. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  8182. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  8183. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  8184. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  8185. {
  8186. constexpr int W = 9, H = 6;
  8187. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  8188. 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)); };
  8189. img[0] = 'B', img[1] = 'M';
  8190. 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);
  8191. put (34, 4 * W * H, 4);
  8192. for (int y = 0; y < H; ++y) // bottom-up rows
  8193. for (int x = 0; x < W; ++x)
  8194. {
  8195. const auto yy = H - 1 - y;
  8196. auto* px = img.data() + 54 + 4 * (y * W + x);
  8197. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  8198. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  8199. }
  8200. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  8201. std::size_t k = 0;
  8202. for (auto v : settings)
  8203. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  8204. const auto len = (std::int32_t) img.size();
  8205. std::memcpy (st.data() + 64, &len, 4);
  8206. st.insert (st.end(), img.begin(), img.end());
  8207. return st;
  8208. }
  8209. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  8210. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  8211. {
  8212. if (realFlpDir.empty())
  8213. return {};
  8214. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  8215. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8216. std::size_t i = 0;
  8217. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8218. ++i;
  8219. i += 8;
  8220. while (i < b.size())
  8221. {
  8222. const auto id = b[i++];
  8223. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8224. if (id >= 192)
  8225. for (int shift = 0; i < b.size(); shift += 7)
  8226. {
  8227. const auto c = b[i++];
  8228. n |= (std::size_t) (c & 0x7F) << shift;
  8229. if (c < 0x80)
  8230. break;
  8231. }
  8232. if (id == 0xD5 && i + n <= b.size())
  8233. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8234. i += n;
  8235. }
  8236. return {};
  8237. }
  8238. TEST (beepmap_engine)
  8239. {
  8240. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  8241. CHECK (lynx::beepMapStateUsable (synthA));
  8242. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  8243. auto broken = synthA;
  8244. broken[0] = 2; // versions above 1 are refused
  8245. CHECK (! lynx::beepMapStateUsable (broken));
  8246. broken = synthA;
  8247. broken.resize (broken.size() - 20); // truncated bitmap
  8248. CHECK (! lynx::beepMapStateUsable (broken));
  8249. using N = lynx::beepmap::RefNote;
  8250. struct Sample { int at; float l, r; };
  8251. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  8252. std::vector<std::vector<std::uint8_t>> states {
  8253. synthA,
  8254. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  8255. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  8256. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  8257. beepMapPresetState ("Default.fst"),
  8258. beepMapPresetState ("Voice.fst"),
  8259. };
  8260. const std::vector<std::vector<N>> sets {
  8261. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  8262. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  8263. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  8264. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  8265. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  8266. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  8267. };
  8268. const Golden goldens[] {
  8269. { 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 } } },
  8270. { 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 } } },
  8271. { 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 } } },
  8272. { 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 } } },
  8273. { 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 } } },
  8274. { 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 } } },
  8275. };
  8276. for (const auto& g : goldens)
  8277. {
  8278. const auto& st = states[(std::size_t) g.set];
  8279. if (st.empty())
  8280. continue; // (no FL installation: the presets are skipped)
  8281. const auto length = (int) g.fs * 2;
  8282. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  8283. double el = 0.0, er = 0.0;
  8284. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8285. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  8286. 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;
  8287. for (const auto& smp : g.s)
  8288. 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;
  8289. if (! ok)
  8290. 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);
  8291. CHECK (ok);
  8292. }
  8293. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  8294. lynx::ChannelDef c;
  8295. c.instrument = lynx::InstrumentKind::beepmap;
  8296. c.pluginState = states[1];
  8297. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  8298. inst->configure (c, 120.0);
  8299. inst->prepare (44100.0);
  8300. std::vector<float> l (1024), r (1024);
  8301. inst->noteOn (60, 0.8f);
  8302. double e = 0.0;
  8303. for (int b = 0; b < 8; ++b)
  8304. {
  8305. std::fill (l.begin(), l.end(), 0.0f);
  8306. std::fill (r.begin(), r.end(), 0.0f);
  8307. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8308. for (int i = 0; i < 1024; ++i)
  8309. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  8310. }
  8311. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  8312. inst->noteOff (60);
  8313. for (int b = 0; b < 4; ++b)
  8314. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8315. CHECK (inst->idle());
  8316. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  8317. inst->configure (c, 120.0);
  8318. inst->noteOn (60, 1.0f);
  8319. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  8320. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  8321. CHECK (inst->idle());
  8322. }
  8323. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  8324. namespace
  8325. {
  8326. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  8327. {
  8328. if (realFlpDir.empty())
  8329. return {};
  8330. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  8331. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8332. std::size_t i = 0;
  8333. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  8334. ++i;
  8335. i += 8;
  8336. while (i < b.size())
  8337. {
  8338. const auto id = b[i++];
  8339. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  8340. if (id >= 192)
  8341. for (int shift = 0; i < b.size(); shift += 7)
  8342. {
  8343. const auto c = b[i++];
  8344. n |= (std::size_t) (c & 0x7F) << shift;
  8345. if (c < 0x80)
  8346. break;
  8347. }
  8348. if (id == 0xD5 && i + n <= b.size())
  8349. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  8350. i += n;
  8351. }
  8352. return {};
  8353. }
  8354. } // namespace
  8355. TEST (sawer_engine)
  8356. {
  8357. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  8358. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  8359. std::memcpy (synth.data(), "SSLF", 4);
  8360. const std::uint32_t size = 4 + 4 * 96;
  8361. std::memcpy (synth.data() + 4, &size, 4);
  8362. std::memcpy (synth.data() + 8, "1rwS", 4);
  8363. const float p27 = 0.75f;
  8364. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  8365. CHECK (lynx::sawerStateUsable (synth));
  8366. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  8367. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  8368. auto broken = synth;
  8369. broken[8] = 'X';
  8370. CHECK (! lynx::sawerStateUsable (broken));
  8371. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  8372. const auto dflt = sawerPresetState ("Default.fst");
  8373. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  8374. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  8375. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  8376. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  8377. {
  8378. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  8379. return;
  8380. }
  8381. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  8382. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  8383. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  8384. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  8385. auto syn = dflt;
  8386. 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 },
  8387. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  8388. syn = setP (syn, kv.first, kv.second);
  8389. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  8390. using N = lynx::sawer::RefNote;
  8391. struct Sample { int at; float l, r; };
  8392. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  8393. const Golden goldens[] {
  8394. { &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 } },
  8395. 44100, 512, 1172.343107, 1169.673445,
  8396. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  8397. { &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,
  8398. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  8399. { &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,
  8400. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  8401. };
  8402. for (const auto& g : goldens)
  8403. {
  8404. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  8405. CHECK (y.size() == 2 * 66150u);
  8406. if (y.size() != 2 * 66150u)
  8407. continue;
  8408. double el = 0.0, er = 0.0;
  8409. for (std::size_t i = 0; i < 66150; ++i)
  8410. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  8411. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  8412. for (const auto& s : g.s)
  8413. 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);
  8414. }
  8415. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  8416. lynx::ChannelDef c;
  8417. c.instrument = lynx::InstrumentKind::sawer;
  8418. c.pluginState = dflt;
  8419. c.samplePath = tablePath;
  8420. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  8421. inst->configure (c, 120.0);
  8422. inst->prepare (44100.0);
  8423. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  8424. for (int b = 0; b < 40; ++b)
  8425. {
  8426. if (b == 0)
  8427. inst->noteOn (60, 0.78125f);
  8428. if (b == 10)
  8429. inst->noteOff (60);
  8430. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  8431. }
  8432. 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,
  8433. 8192, 512, 120.0, 0, true);
  8434. double diff = 0.0, energy = 0.0;
  8435. for (int i = 0; i < 8192; ++i)
  8436. {
  8437. 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]) });
  8438. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  8439. }
  8440. CHECK (diff == 0.0 && energy > 1e-3);
  8441. CHECK (inst->idle());
  8442. }
  8443. TEST (transistorbass_engine)
  8444. {
  8445. const auto factory = tbassPresetState ("Factory programs.fst");
  8446. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  8447. if (factory.empty() || seqOff.empty())
  8448. return; // no FL installation
  8449. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  8450. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  8451. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  8452. auto broken = factory;
  8453. broken[0] = 3; // the wrapper refuses versions above 2
  8454. CHECK (! lynx::transistorBassStateUsable (broken));
  8455. auto truncated = factory;
  8456. truncated.resize (truncated.size() - 1000);
  8457. CHECK (! lynx::transistorBassStateUsable (truncated));
  8458. const auto dll = tbassDll();
  8459. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  8460. return;
  8461. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  8462. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  8463. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  8464. struct Sample { int at; float l, r; };
  8465. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  8466. const Golden goldens[] {
  8467. { "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 } } },
  8468. { "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 } } },
  8469. { "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 } } },
  8470. { "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 } } },
  8471. { "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 } } },
  8472. { "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 } } },
  8473. { "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 } } },
  8474. { "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 } } },
  8475. { "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 } } },
  8476. { "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 } } },
  8477. };
  8478. constexpr int length = 88200;
  8479. for (const auto& g : goldens)
  8480. {
  8481. const bool factoryBank = std::string (g.preset) == "Factory";
  8482. auto st = factoryBank ? factory : seqOff;
  8483. if (g.engine >= 0)
  8484. st = tbassWithEngine (st, g.engine);
  8485. std::vector<lynx::tbass::RefNote> notes;
  8486. if (factoryBank)
  8487. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  8488. else
  8489. {
  8490. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  8491. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  8492. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  8493. }
  8494. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  8495. double el = 0.0, er = 0.0, peak = 0.0;
  8496. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8497. 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]) });
  8498. 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;
  8499. for (const auto& smp : g.s)
  8500. 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;
  8501. if (! ok)
  8502. 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);
  8503. CHECK (ok);
  8504. }
  8505. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  8506. lynx::ChannelDef c;
  8507. c.instrument = lynx::InstrumentKind::transistorbass;
  8508. c.pluginState = factory;
  8509. c.samplePath = dll;
  8510. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  8511. inst->configure (c, 128.0);
  8512. inst->prepare (44100.0);
  8513. std::vector<float> l (4096), r (4096);
  8514. inst->noteOn (65, 0.8f);
  8515. double e = 0.0;
  8516. bool finite = true;
  8517. for (int b = 0; b < 24; ++b)
  8518. {
  8519. std::fill (l.begin(), l.end(), 0.0f);
  8520. std::fill (r.begin(), r.end(), 0.0f);
  8521. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8522. for (int i = 0; i < 4096; ++i)
  8523. 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];
  8524. if (b == 20)
  8525. inst->noteOff (65);
  8526. }
  8527. CHECK (finite && e > 1.0 && ! inst->idle());
  8528. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  8529. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  8530. CHECK (inst->idle());
  8531. }
  8532. TEST (kepler_demo_channels)
  8533. {
  8534. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  8535. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  8536. const auto dll = keplerDll();
  8537. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  8538. return;
  8539. struct Sample { int at; float l, r; };
  8540. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  8541. const Golden goldens[] {
  8542. { "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 } } },
  8543. { "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 } } },
  8544. { "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 } } },
  8545. { "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 } } },
  8546. };
  8547. int checked = 0;
  8548. for (const auto& g : goldens)
  8549. {
  8550. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  8551. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8552. if (bytes.empty())
  8553. continue;
  8554. const flp::Channel* ch = nullptr;
  8555. const auto project = flp::parseProject (bytes);
  8556. for (const auto& c : project.channels)
  8557. if (c.pluginName == "Kepler" && c.name == g.channel)
  8558. ch = &c;
  8559. CHECK (ch != nullptr);
  8560. if (ch == nullptr)
  8561. continue;
  8562. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  8563. 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 },
  8564. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  8565. double el = 0.0, er = 0.0, peak = 0.0;
  8566. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8567. 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]) });
  8568. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8569. for (const auto& smp : g.s)
  8570. 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;
  8571. if (! ok)
  8572. 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);
  8573. CHECK (ok);
  8574. ++checked;
  8575. }
  8576. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  8577. CHECK (checked == (int) std::size (goldens));
  8578. }
  8579. TEST (ogun_demo_channels)
  8580. {
  8581. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  8582. // 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),
  8583. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  8584. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  8585. if (realFlpDir.empty())
  8586. return;
  8587. struct Sample { int at; float l, r; };
  8588. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  8589. const Golden goldens[] {
  8590. { "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
  8591. { "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
  8592. { "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
  8593. { "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
  8594. { "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
  8595. { "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
  8596. { "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
  8597. { "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
  8598. { "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
  8599. { "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
  8600. { "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
  8601. { "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
  8602. { "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
  8603. { "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
  8604. { "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
  8605. { "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
  8606. { "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
  8607. { "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
  8608. { "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
  8609. { "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
  8610. { "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
  8611. { "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
  8612. { "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
  8613. { "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
  8614. { "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
  8615. { "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
  8616. { "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
  8617. { "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
  8618. { "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
  8619. { "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
  8620. { "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
  8621. { "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
  8622. { "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
  8623. { "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
  8624. { "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
  8625. { "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
  8626. { "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
  8627. { "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
  8628. { "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
  8629. { "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
  8630. };
  8631. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  8632. int checked = 0;
  8633. for (const auto& g : goldens)
  8634. {
  8635. if (! states.contains (g.file))
  8636. {
  8637. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  8638. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8639. auto& list = states[g.file];
  8640. if (! bytes.empty())
  8641. for (const auto& c : flp::parseProject (bytes).channels)
  8642. if (c.pluginName == "Ogun")
  8643. list.push_back (c.pluginState);
  8644. }
  8645. const auto& list = states[g.file];
  8646. if (list.empty())
  8647. continue; // not this installation
  8648. CHECK ((int) list.size() > g.ordinal);
  8649. if ((int) list.size() <= g.ordinal)
  8650. continue;
  8651. const auto& st = list[(std::size_t) g.ordinal];
  8652. CHECK (st.size() == g.stateSize);
  8653. lynx::ogun::RefOptions opt;
  8654. opt.quality = g.export4 ? 4 : -1;
  8655. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  8656. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  8657. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  8658. double el = 0.0, er = 0.0, peak = 0.0;
  8659. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8660. 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]) });
  8661. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8662. for (const auto& smp : g.s)
  8663. 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;
  8664. if (! ok)
  8665. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  8666. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  8667. CHECK (ok);
  8668. ++checked;
  8669. }
  8670. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  8671. CHECK (checked == (int) std::size (goldens));
  8672. }
  8673. TEST (autogun_template_channels)
  8674. {
  8675. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  8676. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  8677. // 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
  8678. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  8679. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  8680. if (realFlpDir.empty())
  8681. return;
  8682. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  8683. std::ifstream sgIn (sgPath, std::ios::binary);
  8684. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  8685. if (sgState.size() != 1342)
  8686. return; // not this installation
  8687. struct Sample { int at; float l, r; };
  8688. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  8689. const Golden goldens[] {
  8690. { "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)
  8691. { "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)
  8692. { "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)
  8693. { "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)
  8694. { "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)
  8695. { "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)
  8696. { "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)
  8697. { "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)
  8698. { "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)
  8699. { "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)
  8700. };
  8701. int checked = 0;
  8702. for (const auto& g : goldens)
  8703. {
  8704. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  8705. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8706. if (bytes.empty())
  8707. continue;
  8708. std::vector<std::uint8_t> st;
  8709. for (const auto& c : flp::parseProject (bytes).channels)
  8710. if (c.pluginName == "Autogun" && st.empty())
  8711. st = c.pluginState;
  8712. CHECK (st.size() == g.stateSize);
  8713. CHECK (lynx::autogunStateUsable (st));
  8714. lynx::ogun::RefOptions opt;
  8715. opt.autogun = true;
  8716. opt.sgState = sgState;
  8717. opt.quality = g.export4 ? 4 : -1;
  8718. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  8719. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  8720. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  8721. double el = 0.0, er = 0.0, peak = 0.0;
  8722. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  8723. 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]) });
  8724. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  8725. for (const auto& smp : g.s)
  8726. 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;
  8727. if (! ok)
  8728. 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);
  8729. CHECK (ok);
  8730. ++checked;
  8731. }
  8732. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  8733. CHECK (checked == (int) std::size (goldens));
  8734. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  8735. {
  8736. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  8737. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  8738. lynx::ChannelDef c;
  8739. c.instrument = lynx::InstrumentKind::autogun;
  8740. for (const auto& ch : flp::parseProject (bytes).channels)
  8741. if (ch.pluginName == "Autogun")
  8742. c.pluginState = ch.pluginState;
  8743. c.samplePath = sgPath.string();
  8744. lynx::ogun::RefOptions opt;
  8745. opt.autogun = true;
  8746. opt.sgState = sgState;
  8747. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  8748. 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);
  8749. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  8750. inst->configure (c, 120.0);
  8751. inst->prepare (44100.0);
  8752. std::vector<float> l (16384), r (16384);
  8753. for (int s = 0; s < 16384; s += 512)
  8754. {
  8755. if (s == 0)
  8756. inst->noteOn (60, 0.75f);
  8757. if (s == 6144)
  8758. inst->noteOff (60);
  8759. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  8760. }
  8761. double worst = 0.0, peak = 0.0;
  8762. for (int i = 0; i < 16384; ++i)
  8763. 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]) }),
  8764. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  8765. if (worst != 0.0)
  8766. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  8767. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  8768. std::vector<float> tail (44100), tailR (tail.size());
  8769. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  8770. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  8771. CHECK (inst->idle());
  8772. }
  8773. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  8774. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  8775. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  8776. for (const auto& c : flp::parseProject (rtBytes).channels)
  8777. if (c.pluginName == "Autogun")
  8778. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  8779. }
  8780. TEST (harmless_rack)
  8781. {
  8782. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  8783. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  8784. std::vector<float> inL (8192), inR (8192);
  8785. for (int i = 0; i < 4096; ++i)
  8786. {
  8787. 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));
  8788. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8789. }
  8790. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  8791. lynx::hlfx::Rack rack;
  8792. std::array<int, 7> t {};
  8793. t[3] = compType;
  8794. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  8795. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  8796. for (int s = 0; s < total; s += 512)
  8797. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  8798. return std::make_pair (l, r);
  8799. };
  8800. {
  8801. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  8802. std::array<int, 100> p {};
  8803. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  8804. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  8805. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  8806. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  8807. { 8191, 0.50599128f, -0.43637198f } };
  8808. for (const auto& [i, gl, gr] : golden)
  8809. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8810. }
  8811. {
  8812. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  8813. std::array<int, 100> p {};
  8814. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  8815. lynx::hlfx::Rack rack;
  8816. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  8817. std::vector<float> l (16384), r (16384);
  8818. l[0] = 1.0f;
  8819. for (int s = 0; s < 16384; s += 512)
  8820. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  8821. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  8822. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  8823. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  8824. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  8825. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  8826. }
  8827. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  8828. using Env = std::vector<std::array<double, 4>>;
  8829. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8830. 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,
  8831. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  8832. {
  8833. // master brickwall at −0.22 dB only (bands off / compression off)
  8834. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  8835. { def, def, def, def, def, def, def, def }, 0);
  8836. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  8837. CHECK (preset != nullptr);
  8838. std::array<int, 100> p {};
  8839. p[98] = 1000;
  8840. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  8841. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  8842. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  8843. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  8844. { 4095, -0.12743522f, -0.15237485f } };
  8845. for (const auto& [i, gl, gr] : golden)
  8846. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8847. float peak = 0.0f;
  8848. for (int i = 2048; i < 4096; ++i)
  8849. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  8850. CHECK (peak < 0.9751f);
  8851. }
  8852. {
  8853. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  8854. // exponential tensions (type 0), release 2 on the master, band mix 600
  8855. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  8856. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8857. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  8858. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  8859. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  8860. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  8861. { def, def, knee, def, t0, def, m0, def }, 1);
  8862. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  8863. CHECK (preset != nullptr);
  8864. std::array<int, 100> p {};
  8865. p[98] = 600;
  8866. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  8867. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  8868. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  8869. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  8870. { 4095, 0.10172445f, 0.050564945f } };
  8871. for (const auto& [i, gl, gr] : golden)
  8872. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8873. }
  8874. {
  8875. // an .fst wrapper around a state is found; unsupported states are rejected
  8876. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  8877. { def, def, def, def, def, def, def, def }, 0);
  8878. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  8879. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  8880. for (auto n = st.size(); ; n >>= 7)
  8881. {
  8882. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  8883. if (n < 0x80)
  8884. break;
  8885. }
  8886. body.insert (body.end(), st.begin(), st.end());
  8887. const auto len = (std::uint32_t) body.size();
  8888. for (int k = 0; k < 4; ++k)
  8889. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  8890. fst.insert (fst.end(), body.begin(), body.end());
  8891. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  8892. CHECK (found && *found == st);
  8893. auto rms = st;
  8894. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  8895. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  8896. auto masterMid = st;
  8897. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  8898. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  8899. }
  8900. }
  8901. TEST (fruity_delay_bank_engine)
  8902. {
  8903. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  8904. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  8905. lynx::ScopedFlushDenormals ftz;
  8906. struct Bank
  8907. {
  8908. std::array<int, 16> ctl;
  8909. lynx::fldbank::BankOptions opt;
  8910. };
  8911. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  8912. auto s = lynx::fldbank::defaultState();
  8913. for (int i = 0; i < 4; ++i)
  8914. s.p[(std::size_t) i] = globals[(std::size_t) i];
  8915. s.p[132] = oversampling;
  8916. for (auto& [b, k] : banks)
  8917. {
  8918. for (int c = 0; c < 16; ++c)
  8919. if (16 * b + 4 + c < lynx::fldbank::automatable)
  8920. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  8921. s.bank[(std::size_t) b] = k.opt;
  8922. s.bank[(std::size_t) b].enabled = true;
  8923. }
  8924. return s;
  8925. };
  8926. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  8927. lynx::fldbank::BankOptions o;
  8928. o.filterType[0] = f1;
  8929. o.filterOrder[0] = o1;
  8930. o.filterType[1] = f2;
  8931. o.filterOrder[1] = o2;
  8932. o.filterAfter = after;
  8933. o.mode = mode;
  8934. o.steps = steps;
  8935. o.amount = amount;
  8936. o.tapBeforeFeedback = tap;
  8937. o.sync = sync;
  8938. return o;
  8939. };
  8940. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  8941. std::vector<float> l (4096), r (4096);
  8942. for (int i = 0; i < 4096; ++i)
  8943. {
  8944. 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));
  8945. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  8946. }
  8947. lynx::fldbank::Engine e (s, rate, bpm, block);
  8948. for (int i = 0; i < 4096; i += block)
  8949. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  8950. return std::make_pair (l, r);
  8951. };
  8952. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8953. for (const auto& [i, gl, gr] : golden)
  8954. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  8955. };
  8956. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  8957. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  8958. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  8959. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  8960. const auto a = build ({ 100, 128, 120, 110 },
  8961. { { 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) } },
  8962. { 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) } },
  8963. { 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) } } },
  8964. 0);
  8965. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  8966. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  8967. { 4095, 0.0093755126f, 0.241272509f } });
  8968. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  8969. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  8970. const auto b = build ({ 0, 128, 128, 128 },
  8971. { { 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) } },
  8972. { 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) } } },
  8973. 1);
  8974. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  8975. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  8976. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  8977. auto bytes = lynx::fldbank::serialise (a);
  8978. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  8979. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  8980. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  8981. bytes[0] = 15;
  8982. const auto v15 = lynx::fldbank::parse (bytes);
  8983. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  8984. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  8985. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  8986. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  8987. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  8988. auto fx = lynx::makeEffect (*slot);
  8989. fx->prepare (44100.0);
  8990. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  8991. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8992. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  8993. }
  8994. TEST (fruity_multiband_engine)
  8995. {
  8996. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  8997. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  8998. std::vector<float> inL (4096), inR (4096);
  8999. for (int i = 0; i < 4096; ++i)
  9000. {
  9001. 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)
  9002. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  9003. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  9004. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  9005. }
  9006. auto multiband = [] (std::array<int, 28> c) {
  9007. std::vector<std::uint8_t> b;
  9008. auto put = [&b] (std::int32_t v) {
  9009. std::uint8_t q[4];
  9010. std::memcpy (q, &v, 4);
  9011. b.insert (b.end(), q, q + 4);
  9012. };
  9013. put (0);
  9014. for (auto v : c)
  9015. put (v);
  9016. put (0); // UI words of the state (not used by the engine)
  9017. put (50);
  9018. return b;
  9019. };
  9020. struct Out { std::vector<float> l, r; int latency = 0; };
  9021. auto run = [&] (const std::vector<std::uint8_t>& st) {
  9022. const auto s = lynx::flmb::parse (st);
  9023. CHECK (s.has_value());
  9024. lynx::EffectParams p {};
  9025. lynx::flmb::toParams (*s, p.data());
  9026. auto fx = lynx::flmb::make();
  9027. fx->prepare (44100.0);
  9028. Out o { inL, inR };
  9029. for (int s0 = 0; s0 < 4096; s0 += 512)
  9030. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  9031. o.latency = fx->latencySamples();
  9032. return o;
  9033. };
  9034. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9035. for (const auto& [i, gl, gr] : golden)
  9036. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  9037. };
  9038. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  9039. 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 }));
  9040. CHECK (oa.latency == 0);
  9041. check (oa,
  9042. { { 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 } });
  9043. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  9044. 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 }));
  9045. CHECK (ob.latency == 52);
  9046. check (ob,
  9047. { { 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 } });
  9048. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  9049. 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 }));
  9050. CHECK (oc.latency == 52);
  9051. check (oc,
  9052. { { 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 } });
  9053. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  9054. // effect built with FL's state still uses the exact controls
  9055. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  9056. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  9057. const auto parsed = lynx::flmb::parse (st);
  9058. CHECK (parsed && parsed->c[4] == 15604);
  9059. lynx::EffectParams p {};
  9060. lynx::flmb::toParams (*parsed, p.data());
  9061. for (int i = 0; i < lynx::flmb::controls; ++i)
  9062. 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]);
  9063. lynx::EffectParams rounded = p;
  9064. for (auto& v : rounded)
  9065. v = std::round (v * 10000.0f) / 10000.0f;
  9066. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  9067. auto exact = lynx::flmb::make();
  9068. auto fromState = lynx::flmb::make (st);
  9069. exact->prepare (44100.0);
  9070. fromState->prepare (44100.0);
  9071. for (int s0 = 0; s0 < 4096; s0 += 512)
  9072. {
  9073. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  9074. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  9075. }
  9076. CHECK (l1 == l2 && r1 == r2);
  9077. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  9078. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  9079. // the FL import builds the exact effect with FL's state kept
  9080. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  9081. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  9082. // unknown versions are rejected
  9083. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  9084. }
  9085. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  9086. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  9087. {
  9088. std::vector<float> l (4096), r (4096);
  9089. for (int i = 0; i < 4096; ++i)
  9090. {
  9091. 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));
  9092. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9093. }
  9094. return { l, r };
  9095. }
  9096. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  9097. {
  9098. std::vector<std::uint8_t> d;
  9099. for (const auto w : words)
  9100. for (int b = 0; b < 4; ++b)
  9101. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  9102. return d;
  9103. }
  9104. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  9105. {
  9106. auto [l, r] = stereoFxInput();
  9107. auto fx = lynx::makeEffect (slot);
  9108. fx->prepare (44100.0);
  9109. for (int s = 0; s < 4096; s += 512)
  9110. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  9111. for (const auto& [i, gl, gr] : golden)
  9112. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9113. }
  9114. TEST (stereo_shaper_engine)
  9115. {
  9116. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  9117. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  9118. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  9119. if (slot)
  9120. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  9121. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  9122. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  9123. CHECK (slot.has_value());
  9124. if (slot)
  9125. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  9126. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  9127. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  9128. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  9129. CHECK (st.has_value());
  9130. if (st)
  9131. {
  9132. lynx::EffectParams p {};
  9133. lynx::stereofx::stereoShaperToParams (*st, p.data());
  9134. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  9135. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  9136. && back.phase == -2496 && back.post == 1);
  9137. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  9138. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  9139. }
  9140. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  9141. }
  9142. TEST (frequency_splitter_engine)
  9143. {
  9144. // Frequency Splitter (REA_rodada62): our own states through Frequency Splitter_x64.dll from its creation (gains
  9145. // ramping up from 0, FFT response faded in), blocks of 512 at 44.1 kHz; every band sent to its own destination
  9146. // (1 low, 2 mid, 3 high). IIR modes are bit-exact; the FFT mode's residual is the plugin's single-precision FFT.
  9147. using Gold = std::vector<std::tuple<int, float, float>>;
  9148. struct Case
  9149. {
  9150. std::vector<int> words;
  9151. int mute, latency;
  9152. std::array<Gold, 4> gold; // low, mid, high sends, summed output
  9153. };
  9154. const std::vector<Case> cases {
  9155. { { 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
  9156. { { 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
  9157. { { 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
  9158. { { 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
  9159. { { 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
  9160. { { 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
  9161. { { 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
  9162. };
  9163. auto [l, r] = stereoFxInput();
  9164. for (const auto& c : cases)
  9165. {
  9166. auto words = c.words;
  9167. words.insert (words.begin(), 4);
  9168. for (const int w : { 0, 0, 0, c.mute })
  9169. words.push_back (w);
  9170. std::vector<std::uint8_t> st;
  9171. for (const auto w : words)
  9172. for (int b = 0; b < 4; ++b)
  9173. st.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  9174. st.push_back (0);
  9175. st.resize (st.size() + 24, 0);
  9176. const auto s = lynx::fsplit::parseState (st);
  9177. CHECK (s.has_value());
  9178. if (! s)
  9179. continue;
  9180. lynx::fsplit::Engine e;
  9181. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  9182. e.setParameter (i, s->params[(std::size_t) i]);
  9183. e.prepare (44100.0);
  9184. e.reset (false);
  9185. std::array<std::vector<float>, 8> o;
  9186. for (auto& v : o)
  9187. v.assign (4096, 0.0f);
  9188. for (int b = 0; b < 4096; b += 512)
  9189. {
  9190. float* out[8];
  9191. for (int k = 0; k < 8; ++k)
  9192. out[k] = o[(std::size_t) k].data() + b;
  9193. e.process (l.data() + b, r.data() + b, out, 512);
  9194. }
  9195. CHECK (e.latencySamples() == c.latency);
  9196. const auto tol = c.latency > 0 ? 2e-5f : 2e-6f;
  9197. for (int k = 0; k < 4; ++k)
  9198. for (const auto& [i, gl, gr] : c.gold[(std::size_t) k])
  9199. CHECK (std::abs (o[(std::size_t) (2 * k)][(std::size_t) i] - gl) <= tol * std::max (1.0f, std::abs (gl))
  9200. && std::abs (o[(std::size_t) (2 * k + 1)][(std::size_t) i] - gr) <= tol * std::max (1.0f, std::abs (gr)));
  9201. // the mixer effect: the summed output (FL's main output) of an instance that has been running, and a
  9202. // Patcher bus = the bands sent to it
  9203. auto slot = lynx::effectFromFl ("Frequency Splitter", st);
  9204. CHECK (slot && slot->kind == lynx::EffectKind::frequencySplitter);
  9205. const auto head = lynx::patcherBranchHead ("Frequency Splitter", st, 3);
  9206. CHECK (head.valid && head.slot && head.slot->params[lynx::fsplit::outputHigh] == 1.0f
  9207. && head.slot->params[lynx::fsplit::outputLow] == 0.0f);
  9208. if (slot && head.slot)
  9209. {
  9210. lynx::fsplit::Engine settled;
  9211. for (int i = 0; i < lynx::fsplit::engineParams; ++i)
  9212. settled.setParameter (i, s->params[(std::size_t) i]);
  9213. settled.prepare (44100.0);
  9214. settled.reset (true);
  9215. std::vector<float> sl (4096), sr (4096), hl (4096), hr (4096);
  9216. float* out[8] { nullptr, nullptr, nullptr, nullptr, hl.data(), hr.data(), sl.data(), sr.data() };
  9217. settled.process (l.data(), r.data(), out, 4096);
  9218. const lynx::EffectSlot* both[2] { &*slot, &*head.slot };
  9219. for (const auto* p : both)
  9220. {
  9221. auto fx = lynx::makeEffect (*p);
  9222. fx->prepare (44100.0);
  9223. auto ml = l, mr = r;
  9224. for (int b = 0; b < 4096; b += 300)
  9225. fx->process (ml.data() + b, mr.data() + b, std::min (300, 4096 - b), p->params, 120.0);
  9226. const auto& wl = p == &*slot ? sl : hl;
  9227. const auto& wr = p == &*slot ? sr : hr;
  9228. CHECK (ml == wl && mr == wr);
  9229. CHECK (fx->latencySamples() == c.latency);
  9230. }
  9231. }
  9232. }
  9233. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", std::vector<std::uint8_t> (0x58, 0), 2).valid); // no band sent to bus 2
  9234. CHECK (lynx::fsplit::paramText (2, 3.0f / 7.0f) == "24 dB/oct" && lynx::fsplit::paramText (11, 0.5f) == "0.0 dB");
  9235. CHECK (lynx::fsplit::paramText (0, 0.0f) == "30.0 Hz" && lynx::fsplit::paramText (4, 1.0f) == "FFT");
  9236. }
  9237. TEST (stereo_enhancer_transient_spreader_engines)
  9238. {
  9239. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  9240. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  9241. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  9242. if (se)
  9243. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  9244. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  9245. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  9246. tpState.push_back (5);
  9247. for (const auto w : i32State ({ 0, 50 }))
  9248. tpState.push_back (w);
  9249. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  9250. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  9251. if (tp)
  9252. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  9253. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  9254. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  9255. spState.push_back (1);
  9256. spState.push_back (1);
  9257. auto sp = lynx::effectFromFl ("Spreader", spState);
  9258. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  9259. if (sp)
  9260. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  9261. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  9262. // unknown layouts keep the earlier fallback; FL automation maps one to one
  9263. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  9264. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  9265. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  9266. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  9267. }
  9268. TEST (fruity_dynamics_engines)
  9269. {
  9270. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  9271. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  9272. std::vector<float> inL (4096), inR (4096), side (4096);
  9273. for (int i = 0; i < 4096; ++i)
  9274. {
  9275. 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));
  9276. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9277. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  9278. }
  9279. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  9280. std::uint8_t q[4];
  9281. std::memcpy (q, &v, 4);
  9282. b.insert (b.end(), q, q + 4);
  9283. };
  9284. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  9285. std::vector<std::uint8_t> b;
  9286. put (b, 7);
  9287. for (auto v : p)
  9288. put (b, v);
  9289. b.resize (0xA4, 0);
  9290. put (b, k);
  9291. b.push_back ((std::uint8_t) exact);
  9292. return b;
  9293. };
  9294. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  9295. std::vector<std::uint8_t> b;
  9296. put (b, version);
  9297. for (auto v : p)
  9298. put (b, v);
  9299. put (b, flags);
  9300. if (version > 2)
  9301. put (b, k);
  9302. return b;
  9303. };
  9304. struct Out { std::vector<float> l, r; int latency = 0; };
  9305. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  9306. auto fx = lynx::makeEffect (slot);
  9307. fx->prepare (44100.0);
  9308. Out o { inL, inR };
  9309. for (int s = 0; s < 4096; s += 512)
  9310. {
  9311. if (sidechain)
  9312. fx->setSidechain (side.data() + s, side.data() + s);
  9313. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  9314. }
  9315. o.latency = fx->latencySamples();
  9316. return o;
  9317. };
  9318. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9319. for (const auto& [i, gl, gr] : golden)
  9320. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  9321. };
  9322. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  9323. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  9324. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  9325. return *slot;
  9326. };
  9327. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  9328. 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);
  9329. CHECK (a.latency == 88);
  9330. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  9331. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  9332. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  9333. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  9334. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  9335. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  9336. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  9337. 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),
  9338. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  9339. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  9340. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  9341. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  9342. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  9343. return *slot;
  9344. };
  9345. // Vintage type, look-ahead flag, side input 1: bit-identical
  9346. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  9347. CHECK (ca.latency == 64);
  9348. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  9349. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  9350. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  9351. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  9352. CHECK (cb.latency == 0);
  9353. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  9354. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  9355. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  9356. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  9357. const auto parsed = lynx::fldyn::parseLimiter (st);
  9358. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  9359. const auto slot = limiter (st);
  9360. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  9361. {
  9362. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  9363. 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]);
  9364. }
  9365. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  9366. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  9367. // unreadable states keep the old approximation
  9368. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  9369. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  9370. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  9371. }
  9372. TEST (maximus_engine)
  9373. {
  9374. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  9375. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  9376. std::vector<float> inL (4096), inR (4096);
  9377. for (int i = 0; i < 4096; ++i)
  9378. {
  9379. 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));
  9380. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  9381. }
  9382. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  9383. lynx::hlfx::MaximusProcessor m;
  9384. m.prepare (std::move (preset), 44100.0, mix);
  9385. std::vector<float> l (inL), r (inR);
  9386. for (int s = 0; s < 4096; s += 512)
  9387. m.process (l.data() + s, r.data() + s, 512);
  9388. return std::make_pair (l, r);
  9389. };
  9390. using Env = std::vector<std::array<double, 4>>;
  9391. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9392. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  9393. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  9394. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  9395. 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,
  9396. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  9397. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  9398. for (const auto& [i, gl, gr] : golden)
  9399. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  9400. };
  9401. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  9402. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  9403. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  9404. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  9405. { def, def, knee, def, t0, def, m0, def }, 0);
  9406. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  9407. CHECK (p3 != nullptr);
  9408. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  9409. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  9410. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  9411. auto solo = st3;
  9412. solo[0x1ef] = 1;
  9413. std::memset (solo.data() + 0x1e0, 0, 4);
  9414. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  9415. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  9416. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  9417. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  9418. auto fir = st3;
  9419. fir[0x1fd] = 1;
  9420. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  9421. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  9422. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  9423. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  9424. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  9425. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  9426. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  9427. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  9428. { def, def, knee, def, t0, def, m0, def }, 0);
  9429. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  9430. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  9431. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  9432. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  9433. CHECK (p4 != nullptr);
  9434. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  9435. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  9436. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  9437. // the mixer effect's container and the effect itself
  9438. lynx::maximusfx::Blob blob;
  9439. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  9440. blob.mix = 600;
  9441. blob.state = v4;
  9442. const auto packed = lynx::maximusfx::pack (blob);
  9443. const auto back = lynx::maximusfx::unpack (packed);
  9444. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  9445. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  9446. slot.state = packed;
  9447. auto fx = lynx::makeEffect (slot);
  9448. fx->prepare (44100.0);
  9449. {
  9450. std::vector<float> l (inL), r (inR);
  9451. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  9452. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  9453. const auto ref = run (p4, 600);
  9454. CHECK (l == ref.first && r == ref.second);
  9455. }
  9456. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  9457. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  9458. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  9459. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  9460. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  9461. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  9462. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  9463. lynx::vocodex::InstallData data;
  9464. data.sgDat = st2;
  9465. data.carrierState = { 9, 9, 9 };
  9466. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  9467. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  9468. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  9469. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  9470. }
  9471. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  9472. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  9473. // on our own states and input; LYNX's engine is bit-identical to it.
  9474. TEST (reeverb2_engine)
  9475. {
  9476. auto hex = [] (const char* h) {
  9477. std::vector<std::uint8_t> v;
  9478. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9479. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9480. return v;
  9481. };
  9482. constexpr int N = 8192;
  9483. std::vector<float> inL (N), inR (N);
  9484. std::uint32_t seed = 12345;
  9485. for (int i = 0; i < N; ++i)
  9486. {
  9487. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9488. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9489. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9490. const float g = i < 4000 ? 1.0f : 0.0f;
  9491. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9492. inR[(std::size_t) i] = 0.25f * tri * g;
  9493. }
  9494. struct Golden
  9495. {
  9496. int i;
  9497. float l, r;
  9498. };
  9499. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  9500. const auto slot = lynx::reeverb2FromFl (state);
  9501. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  9502. if (! slot)
  9503. return;
  9504. auto fx = lynx::makeEffect (*slot);
  9505. fx->prepare (44100.0);
  9506. fx->reset();
  9507. std::vector<float> l (inL), r (inR);
  9508. for (int s = 0; s < N; s += 512)
  9509. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  9510. for (const auto& g : gold)
  9511. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9512. };
  9513. // modulation, early reflections, negative separation, filters, predelay
  9514. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  9515. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  9516. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  9517. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  9518. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  9519. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  9520. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  9521. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  9522. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  9523. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  9524. { 8191, 0.144686908f, 0.144686908f } });
  9525. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  9526. lynx::reeverb2::Settings s;
  9527. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  9528. s.tempoPredelay = true;
  9529. lynx::EffectParams p {};
  9530. lynx::reeverb2::toParams (s, p);
  9531. CHECK (lynx::reeverb2::fromParams (p) == s);
  9532. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  9533. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  9534. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  9535. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  9536. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  9537. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  9538. // plugin runs and as LYNX renders)
  9539. lynx::ScopedFlushDenormals ftz;
  9540. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9541. const auto slot = lynx::reeverb1FromFl (state);
  9542. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  9543. if (! slot)
  9544. return;
  9545. auto fx = lynx::makeEffect (*slot);
  9546. fx->prepare (44100.0);
  9547. std::vector<float> l (inL), r (inR);
  9548. for (int s = 0; s < N; s += 512)
  9549. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9550. for (const auto& g : gold)
  9551. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9552. };
  9553. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  9554. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  9555. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  9556. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  9557. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  9558. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  9559. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  9560. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  9561. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  9562. }
  9563. // ---- Fruity Love Philter (REA_rodada66) ---------------------------------------------------------------------------------
  9564. TEST (love_philter_engine)
  9565. {
  9566. // goldens: Fruity Love Philter_x64.dll on our own synthetic states (44.1 kHz, blocks of 512, 120 BPM, live mode),
  9567. // bit-identical on the test machine (denormals flushed, as the plugin runs and as LYNX renders)
  9568. lynx::ScopedFlushDenormals ftz;
  9569. auto hex = [] (const char* h) {
  9570. std::vector<std::uint8_t> v;
  9571. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9572. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9573. return v;
  9574. };
  9575. constexpr int N = 8192;
  9576. std::vector<float> inL (N), inR (N);
  9577. std::uint32_t seed = 12345;
  9578. for (int i = 0; i < N; ++i)
  9579. {
  9580. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9581. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9582. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9583. const float g = i < 4000 ? 1.0f : 0.0f;
  9584. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9585. inR[(std::size_t) i] = 0.25f * tri * g;
  9586. }
  9587. struct Golden
  9588. {
  9589. int i;
  9590. float l, r;
  9591. };
  9592. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9593. const auto slot = lynx::effectFromFl ("Fruity Love Philter", state, 120.0);
  9594. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::lovePhilter);
  9595. if (! slot || slot->kind != lynx::EffectKind::lovePhilter)
  9596. return;
  9597. auto fx = lynx::makeEffect (*slot);
  9598. fx->prepare (44100.0);
  9599. std::vector<float> l (inL), r (inR);
  9600. for (int s = 0; s < N; s += 512)
  9601. {
  9602. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  9603. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9604. }
  9605. for (const auto& g : gold)
  9606. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9607. };
  9608. // SVF triple with Low/Band/High, a song-locked volume pattern with tension, a cutoff LFO; Lime HP fed by NEXT, panned
  9609. run (hex (
  9610. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  9611. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  9612. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  9613. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  9614. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  9615. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  9616. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  9617. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  9618. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  9619. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  9620. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  9621. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  9622. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  9623. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  9624. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  9625. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  9626. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90"),
  9627. { { 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 } });
  9628. // Mango alternate triple with drive, waveshaper, Mod X/Y maps (smoothed), follower map on the volume; ×4 oversampling
  9629. run (hex (
  9630. "03000c00789cedda4d8ed33018c671a788151b8ec0013803993dd7e00e6c7d0a24100bae81c40590e00e5d82c46238c1e0c879db7752b9"
  9631. "8b470c1f4ffe963c759af6371e4b79e675945a4aa96b2f4f5b7f56cad45e3e1d4a79be0c5a7bf9f3f57cd7da326e6fb7f3d3b49e5abef6"
  9632. "a1f537adbf2ae7968f4f7c6b8f5b7fb20ca6e951fb19fd7a3bceefde2eedfbf2fa22bfdf5f6fe7fce9bb4d2bd3e9549d973eadbf742adb"
  9633. "7686e25c86f2df51d7cfe0e0fc6de7203a37ef2fbe70ba200f17678ef3fde3b08f572fbe7fddebebd273816cc07173fe4c36dc0eaebdeb"
  9634. "ff981fcabb2c28cedfeb85c4c70befeb97a5fd580b8dcfcbfba773dbdf433d81e3ec3c7466a46bec9e1d457e7e777bedfd1ff5c4d2c806"
  9635. "1c3f87bd86eeb1d7c07176f6b8d7f85d1ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715ae3f1544653"
  9636. "fbd65e6fe650b65349363985e3e7705f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9"
  9637. "211b748f6cc07176c806dd231b709c1db241f76ae96b183d5a3ecee3b4c6e3a98ca6c6b316383b76b82faa7bd43038ce0e358cee5137e0"
  9638. "383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bb5f4358c1e2d1fe7715a"
  9639. "e3f1544653e3590b9c1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd"
  9640. "231b709c1db241f7c8061c67876cd0bd5afa1a468f968ff338adf1782aa3a9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e75"
  9641. "83ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e2d7d0da347cbc7799cd6783c95"
  9642. "d1d478d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c"
  9643. "67876cd03db201c7d9211b74af96be86d1a3e5e33c4e6b3c9eca686a3c6b81b36387fba2ba470d83e3ec50c3e81e75038eb343dda07b64"
  9644. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba574b5fc3e8d1f2711ea7351e4f6534359e"
  9645. "b5c0d9b1c37d51dda386c17176a861748fba01c7d9a16ed03db201c7d9211b748f3d058eb3c39e42f7c8061c67876cd03db201c7d9211b"
  9646. "748f6cc07176c806dd2bbf0040771f82"),
  9647. { { 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 } });
  9648. // band-pass alternate dual with FLAT, low shelf, all-pass with a pan LFO and inverted input, CENTER, no oversampling
  9649. run (hex (
  9650. "03000c00789ceddd418ad44018c5f12f3303baf404e2d243083d7b6fe0dab55b5dd64106bc8427f024aea497ed09da8a95225ff75883bc"
  9651. "01092fff82a22b19fb97a2248faf62184b4494a5c7abdadf444cf3f866e9b5bdfff5f970ae2d9636cdad0de7af7da9fd6327623d5ffe32"
  9652. "be5b7afdfe6dfdb85d2ff2443b1ddae7b17edecfe33f173f5fb598d6ab1fe63e2d179886e0fab30c5dcf1d07670bce8de8dc3f3cfac213"
  9653. "37df8fc3e571b72fcf5fdf7c5bf7dabab45c201b70dc9cff930da7c1bd7712efe56d78d40d38ce0e7583ee910d38ce0ed9a07bec29709c"
  9654. "1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89b686bdf7968ff338adf1782aa3a9f1ae05ce8e1d9e8bea1e350c"
  9655. "8eb3430da37bd40d38ce0e7583ee910d38ce0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85ea9"
  9656. "e73ed4feaef64fe9cf95a5bf8ef67b2fca727ebeeecb79c0efb5c0c1d9604e6de33926cf457170fedda186d13dea061c6787ba41f7c806"
  9657. "1c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876cd03db201c7d9211b74afd473df6aff19eb9ac6322e69fc706eff05"
  9658. "c9bc8e2fda603c95d1d478d70267c74ecfa9c737cc7170ef1e2ffce9cafffeb5fdec6db477a222785ebab5bf731c9ce738d436ba473d81"
  9659. "e3ecf0efacba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3"
  9660. "b4c6e3a98ca6c63b18383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900d"
  9661. "ba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383b"
  9662. "d40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3"
  9663. "a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736"
  9664. "e0383b6483ee910d38ce0ed9a07b25da1af6de5b3ecee3b4c6e3a98ca6c6bb16383b76782eaa7bd43038ce0e358cee5137e0383bd40dba"
  9665. "4736e0383b6483eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07bf11b286e0820"),
  9666. { { 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 } });
  9667. // the slot exposes FL's controls; FL indices map both ways; unreadable states keep the old conversion
  9668. CHECK (lynx::philter::flIndexOf (6) == 6 && lynx::philter::flIndexOf (8) == 9 && lynx::philter::flIndexOf (8 + 5 * 7 + 4) == 8 + 44 * 7 + 9);
  9669. for (int i = 0; i < lynx::philter::paramCount; ++i)
  9670. CHECK (lynx::philter::paramOfFlIndex (lynx::philter::flIndexOf (i)) == i);
  9671. CHECK (lynx::philter::paramOfFlIndex (8 + 3) == -1 && lynx::philter::paramOfFlIndex (8 + 12) == -1);
  9672. CHECK (lynx::philter::parse ({ 3, 0, 12, 0, 1, 2, 3, 4 }) == nullptr);
  9673. CHECK (lynx::effectInfo (lynx::EffectKind::lovePhilter).params.size() == (std::size_t) lynx::philter::paramCount);
  9674. CHECK (lynx::philter::paramText (5, 0.0f) == "pattern 1" && lynx::philter::paramText (8 + 3, 0.5f) == "centred");
  9675. // a control edited in LYNX is applied when the effect is built from the slot (the state keeps FL's value)
  9676. {
  9677. const auto stateA = hex (
  9678. "03000c00789cedda316ed44018c5f1cfd90b5050234a0e81702a1a6e4045c11d68e7005c00c40928e96810254590380225e552538471ec"
  9679. "d97c314c841e0259cfff91463bf66e7e3b9ac42fdf582e1151961e776abf1f314ce3b3a5d7f6e4fb8bf1b2b6693c5cf56118e6b79ed5fe"
  9680. "b2f6b7b53fadfd41ed8f1379378d9fd77e88a50d57c3c3f597dcd28ee3fcfaadbe9e8fcb24e272d56238fd4019a73e2c5f3074c1ebf732"
  9681. "745a8ee51507670bce99e89cbff9e5076eb9f8be8e378f9b7df3fcfae2dbba37afcb9c0b391b0eb16ec795ff657cfd6a6aefc7cf17178f"
  9682. "f2f9f6f9fafe8769342c7dfabe58e65e62db7f5338fb74fe4f96acafa5e3f8bbf3eb6b75eb1e75068eb3d3b241b95ef2ffc0d67ebc8bb8"
  9683. "b792b65e2ffc9bfa636a64068e9fc3de44f77a7b93adfc6e7170fec661afa17b64038eb34336e81ed980e3ec900dba4736e0383b6483ee"
  9684. "957aee61ed9fe27a4d63194ffd63fdccf44cc7f972fef42408cf6ce0e06c30a7b6711f93fba238387fee50c3e81e75038eb343dda07b64"
  9685. "038eb34336e81e7b0a1c67873d85ee910d38ce0ed9a07b64038eb34336e81ed980e3ec900dba57625ec3d65bcbc7799cd6b83f95ded478"
  9686. "d60267c70ef745758f1a06c7d9a186d13dea061c6787ba41f7c8061c67876cd03df61438ce0e7b0add231b709c1db241f7c8061c67876c"
  9687. "d03db201c7d9211b74afc4bc86adb7968ff338ad717f2abda9f1ac05ce8e1dee8bea1e350c8eb3430da37bd40d38ce0e7583ee910d38ce"
  9688. "0ed9a07bec29709c1df614ba4736e0383b6483ee910d38ce0ed9a07b64038eb34336e85e89790d5b6f2d1fe7715ae3fe547a53e3590b9c"
  9689. "1d3bdc17d53d6a181c67871a46f7a81b709c1dea06dd231b709c1db241f7d853e0383bec29748f6cc07176c806dd231b709c1db241f7c8"
  9690. "061c67876cd0bd12f31ab6de5a3ecee3b4c6fda9f4a6c6b316383b76b82faa7bd43038ce0e358cee5137e0383bd40dba4736e0383b6483"
  9691. "eeb1a7c07176d853e81ed980e3ec900dba4736e0383b6483ee910d38ce0ed9a07b25e6356cbdb57c9cc7698dfb53e94d8d672d7076ec70"
  9692. "5f54f7a861709c1d6a18dda36ec07176a81b748f6cc07176c806dd634f81e3ecb0a7d03db201c7d9211b748f6cc07176c806dd231b709c"
  9693. "1db241f74acc6bd87a6bf9388fd31af7a7d29b1acf5ae0ecd8e1bea8ee51c3e0383bd430ba47dd80e3ec5037e81ed980e3ec900dbac79e"
  9694. "02c7d9614fa17b64038eb34336e81ed980e3ec900dba4736e0383b6483eec54ff7970e90");
  9695. auto slot = lynx::philter::slotFromFl (stateA);
  9696. CHECK (slot.has_value() && ! lynx::philter::slotFromFl (hex ("0300000078")).has_value());
  9697. if (slot)
  9698. {
  9699. auto render = [&] (const lynx::EffectSlot& sl) {
  9700. auto fx = lynx::makeEffect (sl);
  9701. fx->prepare (44100.0);
  9702. std::vector<float> l (inL), r (inR);
  9703. for (int s = 0; s < 4096; s += 512)
  9704. {
  9705. fx->setSongPosition (s * 2.0 * 960.0 / 44100.0);
  9706. fx->process (l.data() + s, r.data() + s, 512, sl.params, 120.0);
  9707. }
  9708. return l[3333];
  9709. };
  9710. const auto base = render (*slot);
  9711. auto edited = *slot;
  9712. edited.params[8] = 0.1f; // unit 1 cutoff
  9713. CHECK (base == 0.0f || base != render (edited));
  9714. CHECK (base == render (*slot));
  9715. }
  9716. }
  9717. }
  9718. TEST (fruity_vocoder_round66_goldens)
  9719. {
  9720. // REA_rodada66 §6 goldens against the round-70 engine (core/FruityVocoder), through FL's state import and the mixer
  9721. // effect: Fruity Vocoder_x64.dll on our own states (44.1 kHz, blocks of 512): left = modulator, right = carrier.
  9722. // The first state's attack (5 ms) is below the knob range (10…1000): the plugin uses it as it is (REA_rodada78 §1)
  9723. lynx::ScopedFlushDenormals ftz;
  9724. auto hex = [] (const char* h) {
  9725. std::vector<std::uint8_t> v;
  9726. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  9727. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  9728. return v;
  9729. };
  9730. constexpr int N = 8192;
  9731. std::vector<float> inL (N), inR (N);
  9732. std::uint32_t seed = 12345;
  9733. for (int i = 0; i < N; ++i)
  9734. {
  9735. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  9736. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  9737. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  9738. const float g = i < 4000 ? 1.0f : 0.0f;
  9739. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  9740. inR[(std::size_t) i] = 0.25f * tri * g;
  9741. }
  9742. struct Golden
  9743. {
  9744. int i;
  9745. float v;
  9746. };
  9747. auto run = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  9748. const auto slot = lynx::effectFromFl ("Fruity Vocoder", state, 120.0);
  9749. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::vocoder);
  9750. if (! slot || slot->kind != lynx::EffectKind::vocoder)
  9751. return;
  9752. auto fx = lynx::makeEffect (*slot);
  9753. fx->prepare (44100.0);
  9754. std::vector<float> l (inL), r (inR);
  9755. for (int s = 0; s < N; s += 512)
  9756. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  9757. for (const auto& g : gold)
  9758. CHECK (l[(std::size_t) g.i] == g.v && r[(std::size_t) g.i] == g.v);
  9759. };
  9760. // 32 bands, order 3, inverted carrier bank with a formant shift, fast attack (5 ms, outside the knob range)
  9761. run (hex (
  9762. "02000000200000000300000002000000000000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000"
  9763. "803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f00"
  9764. "00803f0000803f0000803f0000803f0000803f0000803f0000803f0000803f0000803fb80b000050c30000640000000100000044fdffff"
  9765. "78000000050000002c010000000000008c0000003c000000a0000000"),
  9766. { { 777, 0.47870129346847534f }, { 1555, 0.2731437087059021f }, { 2222, -0.41599133610725403f }, { 3333, 0.083399198949337f }, { 3999, 0.7295553088188171f }, { 4100, 0.0871216207742691f } });
  9767. // 8 bands, swapped inputs, per-band levels, modulator level 0
  9768. run (hex (
  9769. "02000000080000000200000002000000010000003f9a99193f3333333fcdcc4c3f6666663f0000803fcdcc8c3f9a99993f1027000060ea"
  9770. "00001e000000000000002c010000500000000f0000003c00000000000000000000001400000080000000"),
  9771. { { 777, 0.07233863323926926f }, { 1555, 0.1183714047074318f }, { 2222, -0.6131120920181274f }, { 3333, 0.18050849437713623f }, { 3999, 0.056527279317379f }, { 4100, 0.05711518973112106f } });
  9772. }
  9773. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  9774. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  9775. namespace
  9776. {
  9777. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  9778. {
  9779. std::vector<std::uint8_t> b;
  9780. 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))); };
  9781. 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))); };
  9782. 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))); };
  9783. i32 (0x40003);
  9784. i32 (pre), i32 (mix), i32 (post);
  9785. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  9786. i32 (os);
  9787. 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 },
  9788. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  9789. i32 (3), i32 (3), i32 (5);
  9790. for (const auto& p : pts)
  9791. {
  9792. f64 (p.dt), f64 (p.y), f32 (p.t);
  9793. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  9794. }
  9795. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  9796. i32 (1);
  9797. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  9798. return b;
  9799. }
  9800. } // namespace
  9801. TEST (fruity_waveshaper_and_soft_clipper)
  9802. {
  9803. std::vector<float> inL (4096), inR (4096);
  9804. for (int n = 0; n < 4096; ++n)
  9805. {
  9806. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  9807. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  9808. }
  9809. struct Gold { int i; float l, r; };
  9810. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  9811. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  9812. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  9813. std::vector<float> l (inL), r (inR);
  9814. for (int s = 0; s < 4096; s += 512)
  9815. {
  9816. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  9817. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  9818. }
  9819. for (const auto& g : gold)
  9820. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  9821. };
  9822. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  9823. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  9824. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  9825. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  9826. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  9827. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  9828. {
  9829. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  9830. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  9831. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  9832. if (slot)
  9833. {
  9834. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  9835. CHECK (s.threshold == 64 && s.post == 140);
  9836. auto fx = lynx::makeEffect (*slot);
  9837. fx->prepare (44100.0);
  9838. std::vector<float> l (inL), r (inR);
  9839. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  9840. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  9841. }
  9842. }
  9843. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  9844. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  9845. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  9846. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  9847. const auto pA = lynx::flshape::parseWaveShaper (stA);
  9848. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  9849. if (pA)
  9850. {
  9851. const auto T = lynx::flshape::shaperTable (pA->points, false);
  9852. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  9853. && T[16384] == 32767 && T[0] == T[1]);
  9854. const auto U = lynx::flshape::shaperTable (pA->points, true);
  9855. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  9856. }
  9857. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  9858. const auto s = lynx::flshape::parseWaveShaper (st);
  9859. CHECK (s.has_value());
  9860. if (! s)
  9861. return;
  9862. lynx::flshape::WaveShaperEngine e;
  9863. e.setCurve (s->points);
  9864. e.prepare (44100.0);
  9865. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  9866. std::vector<float> l (inL), r (inR);
  9867. for (int i = 0; i < 4096; i += 512)
  9868. e.process (l.data() + i, r.data() + i, 512);
  9869. for (const auto& g : gold)
  9870. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  9871. };
  9872. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  9873. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  9874. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  9875. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  9876. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  9877. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  9878. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  9879. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  9880. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  9881. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  9882. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  9883. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  9884. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  9885. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  9886. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  9887. if (slot)
  9888. {
  9889. auto fx = lynx::makeEffect (*slot);
  9890. fx->prepare (44100.0);
  9891. std::vector<float> l (inL), r (inR);
  9892. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9893. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9894. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9895. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  9896. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  9897. }
  9898. // an unreadable state keeps the old approximation
  9899. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9900. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9901. }
  9902. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  9903. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  9904. namespace
  9905. {
  9906. struct PeqTestBand { int type, freq, width, gain, order; };
  9907. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  9908. {
  9909. std::vector<std::uint8_t> b;
  9910. 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))); };
  9911. i32 (8);
  9912. for (int copy = 0; copy < 2; ++copy)
  9913. {
  9914. for (const auto& x : bands) i32 (x.gain);
  9915. for (const auto& x : bands) i32 (x.freq);
  9916. for (const auto& x : bands) i32 (x.width);
  9917. for (const auto& x : bands) i32 (x.type);
  9918. for (const auto& x : bands) i32 (x.order);
  9919. i32 (main);
  9920. }
  9921. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  9922. b.push_back ((std::uint8_t) options);
  9923. i32 (mask), i32 (1), i32 (0);
  9924. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  9925. i32 (0);
  9926. return b;
  9927. }
  9928. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  9929. {
  9930. std::vector<std::uint8_t> b;
  9931. 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))); };
  9932. for (const auto& x : bands) i32 (x.gain);
  9933. for (const auto& x : bands) i32 (x.freq);
  9934. for (const auto& x : bands) i32 (x.width);
  9935. for (const auto& x : bands) i32 (x.type);
  9936. i32 (main);
  9937. return b;
  9938. }
  9939. } // namespace
  9940. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  9941. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  9942. {
  9943. std::vector<std::uint8_t> b;
  9944. 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); };
  9945. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  9946. auto putF = [&put] (float v) { put (&v, 4); };
  9947. std::uint32_t seed = 12345;
  9948. auto rnd = [&seed] {
  9949. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  9950. return (float) ((double) (seed % 20001) / 10000.0);
  9951. };
  9952. putI (4);
  9953. for (int k = 0; k < 93; ++k)
  9954. {
  9955. putF (rnd());
  9956. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  9957. }
  9958. for (int bank = 0; bank < 9; ++bank)
  9959. for (int k = 0; k < 93; ++k)
  9960. {
  9961. const float v = rnd();
  9962. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  9963. }
  9964. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  9965. putI (v);
  9966. for (int v : { 0, add ? 1 : 0, 0 })
  9967. b.push_back ((std::uint8_t) v);
  9968. putI (1);
  9969. return b;
  9970. }
  9971. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  9972. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  9973. {
  9974. std::vector<std::uint8_t> b;
  9975. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  9976. putI (7);
  9977. for (int v : p)
  9978. putI (v);
  9979. putI (p[11]);
  9980. b.push_back (0);
  9981. putI (engine);
  9982. putI (detector);
  9983. putI (1000);
  9984. b.push_back (1);
  9985. b.push_back (0);
  9986. return b;
  9987. }
  9988. TEST (pitch_shifter_exact)
  9989. {
  9990. std::vector<float> inL (8192), inR (8192);
  9991. for (int n = 0; n < 8192; ++n)
  9992. {
  9993. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  9994. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  9995. }
  9996. struct Gold { int i; float l, r; };
  9997. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  9998. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  9999. lynx::psfl::State s;
  10000. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  10001. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  10002. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  10003. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  10004. if (! slot)
  10005. return;
  10006. auto fx = lynx::makeEffect (*slot);
  10007. fx->prepare (44100.0);
  10008. std::vector<float> l (inL), r (inR);
  10009. for (int i = 0, k = 0; i < 8192; ++k)
  10010. {
  10011. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  10012. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  10013. i += n;
  10014. }
  10015. CHECK (fx->latencySamples() == latency);
  10016. for (const auto& g : gold)
  10017. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10018. };
  10019. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  10020. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  10021. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  10022. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  10023. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  10024. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  10025. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  10026. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  10027. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  10028. }
  10029. TEST (equo_exact)
  10030. {
  10031. std::vector<float> inL (4096), inR (4096);
  10032. for (int n = 0; n < 4096; ++n)
  10033. {
  10034. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10035. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10036. }
  10037. struct Gold { int i; float l, r; };
  10038. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  10039. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  10040. const auto s = lynx::equofx::parse (st);
  10041. CHECK (s.has_value());
  10042. if (! s)
  10043. return;
  10044. lynx::equofx::Engine e;
  10045. e.prepare (44100.0, hq);
  10046. e.set (*s);
  10047. std::vector<float> l (inL), r (inR);
  10048. if (! ticks)
  10049. e.process (l.data(), r.data(), 4096);
  10050. else
  10051. {
  10052. auto k = *s;
  10053. e.startFromConstruction();
  10054. for (int i = 0; i < 4096; i += 256)
  10055. {
  10056. if (i == 2048)
  10057. k.knob[2] = 5300, e.setKnobs (k);
  10058. e.tick();
  10059. e.process (l.data() + i, r.data() + i, 256);
  10060. }
  10061. }
  10062. for (const auto& g : gold)
  10063. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10064. };
  10065. const auto plain = equoTestState (0, 150, false);
  10066. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  10067. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  10068. run (equoTestState (1, 300, true), false, false,
  10069. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  10070. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  10071. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  10072. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  10073. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  10074. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  10075. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  10076. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  10077. if (slot)
  10078. {
  10079. auto back = *lynx::equofx::parse (plain);
  10080. lynx::equofx::applyParams (slot->params, back);
  10081. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  10082. auto fx = lynx::makeEffect (*slot);
  10083. fx->prepare (44100.0);
  10084. std::vector<float> l (inL), r (inR);
  10085. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  10086. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  10087. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  10088. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  10089. lynx::equofx::Engine ref;
  10090. ref.prepare (44100.0, false);
  10091. ref.set (*lynx::equofx::parse (plain));
  10092. std::vector<float> rl (inL), rr (inR);
  10093. ref.process (rl.data(), rr.data(), 700);
  10094. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  10095. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  10096. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  10097. }
  10098. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  10099. }
  10100. // Multiband Delay test state (version 7, our own LCG cells, no FL data; REA_rodada71 mbd/py/teststate.py builds the same bytes)
  10101. static std::vector<std::uint8_t> multibandDelayTestState (std::array<int, 12> globals, std::uint32_t seed)
  10102. {
  10103. std::vector<std::uint8_t> b;
  10104. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  10105. putI (7);
  10106. putI (396);
  10107. std::uint64_t s = seed;
  10108. for (int i = 0; i < 384; ++i)
  10109. {
  10110. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  10111. putI ((std::int32_t) ((s % 100001u) * (1u << 30) / 100000u));
  10112. }
  10113. for (int v : globals)
  10114. putI (v);
  10115. putI (0);
  10116. putI (3);
  10117. for (int v : { 1, 1, 0 })
  10118. b.push_back ((std::uint8_t) v);
  10119. b.insert (b.end(), 192, 0);
  10120. return b;
  10121. }
  10122. TEST (multiband_delay_exact)
  10123. {
  10124. std::vector<float> inL (8192), inR (8192);
  10125. for (int n = 0; n < 8192; ++n)
  10126. {
  10127. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0 * 0.3);
  10128. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0 * 0.3);
  10129. }
  10130. struct Gold { int i; float l, r; };
  10131. // goldens: Multiband Delay_x64.dll (+ its engine_x64.dll) on the same states and signal, 512-frame blocks (REA_rodada71)
  10132. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, float tol, std::initializer_list<Gold> gold) {
  10133. CHECK (st.size() == 1795 && lynx::mbdelay::parse (st).has_value());
  10134. const auto slot = lynx::effectFromFl ("Multiband Delay", st, 120.0);
  10135. CHECK (slot && slot->kind == lynx::EffectKind::multibandDelay && slot->state == st);
  10136. if (! slot)
  10137. return;
  10138. auto fx = lynx::makeEffect (*slot);
  10139. fx->prepare (44100.0);
  10140. std::vector<float> l (inL), r (inR);
  10141. for (int i = 0, k = 0; i < 8192; ++k)
  10142. {
  10143. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  10144. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  10145. i += n;
  10146. }
  10147. CHECK (fx->latencySamples() == latency);
  10148. for (const auto& g : gold)
  10149. CHECK (std::fabs (l[(std::size_t) g.i] - g.l) <= tol && std::fabs (r[(std::size_t) g.i] - g.r) <= tol);
  10150. };
  10151. // steep crossover, keep pitch, weighted feedback, compressor
  10152. run (multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777), 0, 0.0f,
  10153. { { 1000, -0.125012308f, 0.0675066486f }, { 3000, -0.125002861f, -0.0525011979f }, { 5000, -0.110439189f, 0.0597563162f },
  10154. { 7000, -0.146480903f, 0.0139919929f }, { 8191, -0.0454496294f, 0.0398242511f } });
  10155. // gentle crossover, harmonic scale, delay ×0.1, saturation
  10156. run (multibandDelayTestState ({ -2500, 1, 4500, 0, 0, 0, 0, 128, 1, 0, 1000, 1 }, 4242), 0, 0.0f,
  10157. { { 1000, 0.0679653957f, 0.0137896547f }, { 3000, 0.0620588325f, 0.0558352768f }, { 5000, 0.0558495969f, -0.0220566839f },
  10158. { 7000, 0.057009533f, 0.00332171144f }, { 8191, 0.0673644915f, 0.00163267844f } });
  10159. // linear-phase crossover (FL's IPP FFTs are approximated: about −120 dB), clipper
  10160. run (multibandDelayTestState ({ 1500, 0, 2000, 1500, 2, 0, 1000, 100, 0, 0, 5000, 2 }, 99), 1536, 2e-6f,
  10161. { { 1000, 0.0f, 0.0f }, { 3000, 0.103910387f, -0.0521105044f }, { 5000, 0.0516818762f, -0.0134486742f },
  10162. { 7000, 0.065563485f, -0.00765838847f }, { 8191, -0.0376662463f, -0.0434602089f } });
  10163. const auto st = multibandDelayTestState ({ 2000, 0, 3000, 600, 1, 1, 3500, 64, 0, 1, 30000, 0 }, 777);
  10164. const auto s = *lynx::mbdelay::parse (st);
  10165. CHECK (lynx::mbdelay::parse (lynx::mbdelay::serialize (s))->raw == s.raw);
  10166. lynx::EffectParams p {};
  10167. lynx::mbdelay::toParams (s, p);
  10168. auto back = s;
  10169. lynx::mbdelay::applyParams (p, back);
  10170. CHECK (back.raw == s.raw);
  10171. CHECK (lynx::mbdelay::engineValue (s, 384 + 7) == (float) (192.0 / 256.0));
  10172. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 4, p[4], 120.0) == "steep");
  10173. CHECK (lynx::formatEffectParam (lynx::EffectKind::multibandDelay, 7, 1.0f, 120.0) == "100% wet");
  10174. CHECK (! lynx::mbdelay::parse (std::vector<std::uint8_t> (16, 0)).has_value());
  10175. }
  10176. TEST (fruity_parametric_eq_exact)
  10177. {
  10178. std::vector<float> inL (4096), inR (4096);
  10179. for (int n = 0; n < 4096; ++n)
  10180. {
  10181. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10182. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10183. }
  10184. struct Gold { int i; float l, r; };
  10185. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  10186. // low pass Gentle 6
  10187. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  10188. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  10189. { 1, 60000, 45000, 0, 2 } };
  10190. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  10191. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  10192. { 1, 60000, 45000, 0, 0 } };
  10193. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  10194. CHECK (s.has_value());
  10195. if (! s)
  10196. return;
  10197. lynx::peqfl::Engine e;
  10198. e.set (*s);
  10199. e.prepare (44100.0);
  10200. std::vector<float> l (inL), r (inR);
  10201. for (int i = 0; i < 4096; i += 512)
  10202. e.process (l.data() + i, r.data() + i, 512);
  10203. for (const auto& g : gold)
  10204. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10205. };
  10206. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  10207. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  10208. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  10209. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  10210. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  10211. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  10212. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  10213. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  10214. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  10215. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  10216. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  10217. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  10218. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  10219. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  10220. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  10221. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  10222. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  10223. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  10224. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  10225. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  10226. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  10227. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  10228. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  10229. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  10230. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  10231. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  10232. // is not replicated, so within 1e-6 of the DLL
  10233. for (const bool hq : { false, true })
  10234. {
  10235. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  10236. CHECK (s && s->linearPhase);
  10237. if (! s)
  10238. continue;
  10239. std::vector<float> l (12288), r (12288);
  10240. for (int n = 0; n < 12288; ++n)
  10241. {
  10242. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10243. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10244. }
  10245. lynx::peqfl::Engine e;
  10246. e.set (*s);
  10247. e.prepare (44100.0);
  10248. CHECK (e.latencySamples() == 1536);
  10249. for (int i = 0; i < 12288; i += 512)
  10250. e.process (l.data() + i, r.data() + i, 512);
  10251. const std::initializer_list<Gold> gold = hq ? std::initializer_list<Gold> {
  10252. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  10253. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  10254. { 12287, 0.40586185455322266f, -0.014698396436870098f } } : std::initializer_list<Gold> {
  10255. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  10256. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  10257. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  10258. for (const auto& g : gold)
  10259. 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);
  10260. }
  10261. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  10262. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  10263. {
  10264. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  10265. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  10266. const auto slot = lynx::effectFromFl (name, st, 120.0);
  10267. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  10268. if (! slot)
  10269. continue;
  10270. const auto back = lynx::peqfl::fromParams (slot->params);
  10271. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  10272. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  10273. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  10274. auto fx = lynx::makeEffect (*slot);
  10275. fx->prepare (44100.0);
  10276. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  10277. std::vector<float> l (inL), r (inR);
  10278. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  10279. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  10280. if (v1)
  10281. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  10282. else
  10283. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  10284. // editor texts and graph
  10285. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  10286. == (v1 ? "12 dB/oct" : "Steep 8"));
  10287. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  10288. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  10289. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  10290. }
  10291. // a native LYNX EQ is untouched by the FL law
  10292. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  10293. }
  10294. TEST (fruity_panomatic_balance_blood_overdrive)
  10295. {
  10296. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  10297. const lynx::ScopedFlushDenormals ftz;
  10298. std::vector<float> inL (4096), inR (4096);
  10299. for (int n = 0; n < 4096; ++n)
  10300. {
  10301. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10302. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10303. }
  10304. struct Gold { int i; float l, r; };
  10305. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  10306. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  10307. for (const auto& g : gold)
  10308. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10309. };
  10310. auto st32 = [] (std::initializer_list<int> v) {
  10311. std::vector<std::uint8_t> b;
  10312. for (int x : v)
  10313. for (int k = 0; k < 4; ++k)
  10314. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  10315. return b;
  10316. };
  10317. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  10318. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  10319. lynx::flbasic::PanOMaticEngine e;
  10320. e.prepare (44100);
  10321. e.set (s);
  10322. if (prime)
  10323. e.prime();
  10324. std::vector<float> l (inL), r (inR);
  10325. for (int i = 0; i < 4096; i += 512)
  10326. e.process (l.data() + i, r.data() + i, 512);
  10327. check (l, r, gold);
  10328. };
  10329. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  10330. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  10331. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  10332. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  10333. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  10334. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  10335. // Balance: flushed and from silence
  10336. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  10337. lynx::flbasic::BalanceEngine e;
  10338. e.prepare (44100);
  10339. e.set (s);
  10340. if (flush)
  10341. e.flush();
  10342. std::vector<float> l (inL), r (inR);
  10343. for (int i = 0; i < 4096; i += 512)
  10344. e.process (l.data() + i, r.data() + i, 512);
  10345. check (l, r, gold);
  10346. };
  10347. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  10348. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  10349. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  10350. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  10351. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  10352. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  10353. lynx::flbasic::BloodOverdriveEngine e;
  10354. e.start (44100, { raw });
  10355. std::vector<float> l (inL), r (inR);
  10356. for (int i = 0; i < 4096; i += 512)
  10357. e.process (l.data() + i, r.data() + i, 512);
  10358. check (l, r, gold);
  10359. };
  10360. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  10361. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  10362. { 4095, 0.0402069278f, 0.0499244779f } });
  10363. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  10364. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  10365. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  10366. CHECK (slot && slot->kind == kind);
  10367. if (! slot)
  10368. return;
  10369. auto fx = lynx::makeEffect (*slot);
  10370. fx->prepare (44100.0);
  10371. std::vector<float> l (inL), r (inR);
  10372. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10373. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  10374. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  10375. check (l, r, gold);
  10376. };
  10377. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  10378. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  10379. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  10380. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  10381. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  10382. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  10383. {
  10384. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  10385. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  10386. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  10387. if (slot)
  10388. {
  10389. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  10390. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  10391. }
  10392. }
  10393. // unreadable states keep the old approximations
  10394. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  10395. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  10396. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  10397. }
  10398. TEST (fruity_fast_lp_free_filter_filter)
  10399. {
  10400. // REA_rodada63: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512 (the wrapper
  10401. // splits the host blocks below into calls of 512, as the DLL ran)
  10402. const lynx::ScopedFlushDenormals ftz;
  10403. std::vector<float> inL (4096), inR (4096);
  10404. for (int n = 0; n < 4096; ++n)
  10405. {
  10406. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10407. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10408. }
  10409. struct Gold { int i; float l, r; };
  10410. auto state = [] (const char* hex) {
  10411. std::vector<std::uint8_t> b;
  10412. for (const char* p = hex; p[0] && p[1]; p += 2)
  10413. b.push_back ((std::uint8_t) std::strtol (std::string (p, 2).c_str(), nullptr, 16));
  10414. return b;
  10415. };
  10416. auto viaImport = [&] (const char* plugin, const char* hex, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  10417. const auto slot = lynx::effectFromFl (plugin, state (hex), 120.0);
  10418. CHECK (slot && slot->kind == kind);
  10419. if (! slot)
  10420. return;
  10421. auto fx = lynx::makeEffect (*slot);
  10422. fx->prepare (44100.0);
  10423. std::vector<float> l (inL), r (inR);
  10424. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10425. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  10426. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  10427. for (const auto& g : gold)
  10428. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  10429. };
  10430. using K = lynx::EffectKind;
  10431. viaImport ("Fruity Fast LP", "01000000e0010000bc020000", K::fruityFastLP,
  10432. { { 100, -2.876206159591675f, 1.5413906574249268f }, { 1000, 0.12972253561019897f, 0.02936629205942154f },
  10433. { 2047, -0.2262123078107834f, -0.15169914066791534f }, { 3000, 0.12972258031368256f, -0.0798468142747879f },
  10434. { 4095, 0.0011161785805597901f, -0.08101501315832138f } });
  10435. viaImport ("Fruity Fast LP", "01000000890000008a000000", K::fruityFastLP,
  10436. { { 100, -0.7748895883560181f, -0.2310182899236679f }, { 1000, -0.01256702933460474f, -0.0086913350969553f },
  10437. { 2047, -0.010225297883152962f, -0.014700239524245262f }, { 3000, -0.012712989933788776f, -0.008916046470403671f },
  10438. { 4095, -0.008918046951293945f, -0.020280027762055397f } });
  10439. viaImport ("Fruity Free Filter", "02000000c2010000200300002c0100008403000001", K::fruityFreeFilter, // low shelf, CENTER
  10440. { { 100, -0.6178444623947144f, -0.13762974739074707f }, { 1000, -1.6557703018188477f, 0.8969771862030029f },
  10441. { 2047, 0.6539966464042664f, -0.21659433841705322f }, { 3000, -1.6404345035552979f, 0.4286947548389435f },
  10442. { 4095, -1.2272151708602905f, -0.5219485759735107f } });
  10443. viaImport ("Fruity Free Filter", "020000008a02000058020000bc020000c800000000", K::fruityFreeFilter, // high shelf
  10444. { { 100, -0.06996861100196838f, -0.11112016439437866f }, { 1000, -0.47636136412620544f, 0.31258222460746765f },
  10445. { 2047, 0.1628853678703308f, -0.3169582486152649f }, { 3000, -0.47636136412620544f, -0.22028093039989471f },
  10446. { 4095, -0.0975298061966896f, 0.05710025131702423f } });
  10447. viaImport ("Fruity Free Filter", "020000006400000000040000a9000000a402000001", K::fruityFreeFilter, // low pass 16 kHz
  10448. { { 100, 1.3032524585723877f, -0.007953938096761703f }, { 1000, 0.18662987649440765f, 2.155099391937256f },
  10449. { 2047, 0.5824014544487f, 0.05704556778073311f }, { 3000, 0.18981599807739258f, -0.6362011432647705f },
  10450. { 4095, 1.2624475955963135f, 2.497816324234009f } });
  10451. viaImport ("Fruity Filter", "020000004b01000076000000000400002c010000580200000100000001", K::fruityFilter, // x2, CENTER
  10452. { { 100, 0.2685631811618805f, 0.16885951161384583f }, { 1000, -0.20637311041355133f, 0.13557639718055725f },
  10453. { 2047, 0.07038643211126328f, -0.15532609820365906f }, { 3000, -0.20311756432056427f, -0.09958365559577942f },
  10454. { 4095, 0.08275680989027023f, 0.010792487300932407f } });
  10455. viaImport ("Fruity Filter", "01000000bc020000f401000000000000000400000000000000000000", K::fruityFilter, // band pass
  10456. { { 100, -0.4368843734264374f, -0.12492841482162476f }, { 1000, -0.3362128436565399f, -0.09686881303787231f },
  10457. { 2047, 0.21920710802078247f, 0.3213832974433899f }, { 3000, -0.3362128436565399f, 0.4016203284263611f },
  10458. { 4095, -0.14230486750602722f, -0.22842440009117126f } });
  10459. // an old Free Filter layout (type·10) is converted to the current scale; parameter texts
  10460. {
  10461. const auto slot = lynx::effectFromFl ("Fruity Free Filter", state ("010000003c00000000020000a9000000a4020000"), 120.0);
  10462. CHECK (slot && slot->kind == K::fruityFreeFilter);
  10463. if (slot)
  10464. {
  10465. CHECK (lynx::flfilter::freeFilterFromParams (slot->params.data()).type == 100 + (int) std::nearbyint (5.5 / 7.0 * 600.0));
  10466. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 0, slot->params[0], 120.0) == "Peaking");
  10467. }
  10468. CHECK (lynx::formatEffectParam (K::fruityFreeFilter, 1, 1.0f, 120.0) == "16000 Hz");
  10469. CHECK (lynx::formatEffectParam (K::fruityFilter, 0, 1.0f, 120.0) == "7350 Hz");
  10470. }
  10471. // unreadable states keep the old approximations
  10472. CHECK (lynx::effectFromFl ("Fruity Fast LP", { 1, 2, 3 }, 120.0)->kind == K::filter);
  10473. CHECK (lynx::effectFromFl ("Fruity Free Filter", { 1, 2, 3 }, 120.0)->kind == K::filter);
  10474. CHECK (lynx::effectFromFl ("Fruity Filter", { 9, 0, 0, 0 }, 120.0)->kind == K::filter);
  10475. }
  10476. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  10477. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  10478. namespace
  10479. {
  10480. struct SytrusTestPatch
  10481. {
  10482. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  10483. struct Op
  10484. {
  10485. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  10486. int ratio = 20000, offset = 0, noise = 0;
  10487. std::vector<float> amps { 1.0f };
  10488. std::uint8_t flags[8] {};
  10489. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  10490. std::vector<std::pair<double, double>> env1pts;
  10491. } ops[6];
  10492. struct Filt
  10493. {
  10494. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  10495. int type = 3;
  10496. } filts[3];
  10497. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  10498. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  10499. bool reverbOn = false;
  10500. int eqmode = 0;
  10501. SytrusTestPatch()
  10502. {
  10503. main[0] = 128, main[1] = 128;
  10504. for (int i = 0; i < 8; ++i)
  10505. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  10506. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  10507. }
  10508. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  10509. std::vector<std::uint8_t> blob() const
  10510. {
  10511. std::vector<std::uint8_t> b;
  10512. 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))); };
  10513. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  10514. 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))); };
  10515. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  10516. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  10517. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  10518. for (const auto& [dt, y] : pts)
  10519. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  10520. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  10521. };
  10522. for (int i = 0; i < 240; ++i)
  10523. if (4 * i + 4 <= 0x320)
  10524. i32 (main[(std::size_t) i]);
  10525. b.resize (962, 0); // mute bytes: none muted
  10526. i32 (0); // global declick amount
  10527. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  10528. for (int k = 0; k < 9; ++k)
  10529. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  10530. for (const auto& d : delays)
  10531. b.insert (b.end(), d.begin(), d.end());
  10532. for (auto v : reverb)
  10533. i32 (v);
  10534. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  10535. i32 (eqmode);
  10536. for (int k = 0; k < 3; ++k)
  10537. i32 (0), i32 (30000), i32 (20000), i32 (k);
  10538. b.push_back (0); // no info text
  10539. for (int u = 0; u < 10; ++u)
  10540. {
  10541. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  10542. for (int j = 0; j < 10; ++j)
  10543. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  10544. if (u < 6)
  10545. {
  10546. const auto& o = ops[u];
  10547. const auto at = b.size();
  10548. b.resize (at + 0x828, 0);
  10549. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  10550. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  10551. put (0xc, o.amps.data(), 4 * o.amps.size());
  10552. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  10553. put (0x80c, shape, sizeof shape);
  10554. put (0x820, o.flags, 8);
  10555. }
  10556. else if (u < 9)
  10557. {
  10558. const auto at = b.size();
  10559. b.resize (at + 0x30, 0);
  10560. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  10561. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10562. b.push_back (0);
  10563. }
  10564. for (int k = 0; k < narts; ++k)
  10565. {
  10566. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  10567. const double neutral = add ? 0.5 : 1.0;
  10568. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  10569. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  10570. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  10571. else
  10572. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10573. i32 (128), i32 (128), i32 (0), i32 (128);
  10574. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  10575. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  10576. for (int m = 0; m < 6; ++m)
  10577. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  10578. }
  10579. }
  10580. b.push_back (0), b.push_back (0);
  10581. // zlib with stored blocks
  10582. std::vector<std::uint8_t> z { 0x78, 0x01 };
  10583. for (std::size_t at = 0; at < b.size(); at += 65535)
  10584. {
  10585. const auto n = std::min<std::size_t> (65535, b.size() - at);
  10586. z.push_back (at + n >= b.size() ? 1 : 0);
  10587. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  10588. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  10589. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  10590. }
  10591. std::uint32_t a = 1, c = 0;
  10592. for (auto x : b)
  10593. a = (a + x) % 65521, c = (c + a) % 65521;
  10594. for (int k = 3; k >= 0; --k)
  10595. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  10596. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  10597. out.insert (out.end(), z.begin(), z.end());
  10598. return out;
  10599. }
  10600. };
  10601. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  10602. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  10603. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  10604. {
  10605. lynx::sytrus::Engine e;
  10606. e.setTempo (120.0);
  10607. e.prepare (fs, renderMode, 512);
  10608. e.setPatch (lynx::sytrus::parsePatch (blob));
  10609. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  10610. for (int n0 = 0; n0 < n; n0 += 512)
  10611. {
  10612. for (std::size_t k = 0; k < notes.size(); ++k)
  10613. {
  10614. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  10615. {
  10616. lynx::sytrus::NoteParams p;
  10617. p.key = (float) notes[k][0];
  10618. p.id = (std::int64_t) k + 1;
  10619. e.noteOn (p);
  10620. }
  10621. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  10622. e.release ((std::int64_t) k + 1);
  10623. }
  10624. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  10625. }
  10626. return { l, r };
  10627. }
  10628. } // namespace
  10629. TEST (sytrus_engine_sine)
  10630. {
  10631. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  10632. SytrusTestPatch t;
  10633. t.cell (0, 0, 8, 256);
  10634. const auto blob = t.blob();
  10635. CHECK (lynx::sytrus::stateUsable (blob));
  10636. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  10637. auto broken = blob;
  10638. broken.resize (broken.size() / 2);
  10639. CHECK (! lynx::sytrus::stateUsable (broken));
  10640. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  10641. double worst = 0.0;
  10642. for (int k = 0; k < 4000; ++k)
  10643. 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)));
  10644. CHECK (worst < 2e-5);
  10645. CHECK (l == r);
  10646. // 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
  10647. // 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)
  10648. int last = 0;
  10649. for (int k = 0; k < 8192; ++k)
  10650. if (l[(std::size_t) k] != 0.0f)
  10651. last = k;
  10652. CHECK (last == 4895);
  10653. // key 69 = 880 Hz; 48 kHz
  10654. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  10655. worst = 0.0;
  10656. for (int k = 0; k < 4000; ++k)
  10657. 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)));
  10658. CHECK (worst < 5e-5);
  10659. (void) r2;
  10660. }
  10661. TEST (sytrus_engine_reference)
  10662. {
  10663. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  10664. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  10665. SytrusTestPatch t;
  10666. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  10667. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  10668. t.ops[1].ratio = 40000;
  10669. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  10670. t.cell (0, 0, 8, 128);
  10671. t.cell (0, 6, 0, 256); // op 1 → filter 1
  10672. t.cell (0, 6, 8, 256);
  10673. t.cell (0, 0, 7, 200); // FX send
  10674. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  10675. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  10676. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  10677. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  10678. std::memcpy (t.delays[0].data(), d0, 20);
  10679. t.delays[0][22] = 1;
  10680. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  10681. t.reverbOn = true;
  10682. const auto blob = t.blob();
  10683. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  10684. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  10685. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  10686. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  10687. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  10688. for (const auto& [i, v] : golden)
  10689. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  10690. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  10691. for (const auto& [i, v] : goldenR)
  10692. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  10693. }
  10694. TEST (sytrus_instrument)
  10695. {
  10696. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  10697. SytrusTestPatch t;
  10698. t.cell (0, 0, 8, 256);
  10699. lynx::ChannelDef c;
  10700. c.instrument = lynx::InstrumentKind::sytrus;
  10701. c.pluginState = t.blob();
  10702. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  10703. inst->prepare (44100.0);
  10704. inst->configure (c, 120.0);
  10705. CHECK (inst->idle());
  10706. std::vector<float> l (4096), r (4096);
  10707. inst->noteOn (60, 100.0f / 128.0f);
  10708. // 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
  10709. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  10710. double peak = 0.0;
  10711. for (int k = 1000; k < 4096; ++k)
  10712. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  10713. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  10714. CHECK (! inst->idle());
  10715. inst->noteOff (60);
  10716. std::vector<float> tail (44100 * 3), tailR (tail.size());
  10717. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  10718. CHECK (inst->idle());
  10719. CHECK (tail.back() == 0.0f);
  10720. }
  10721. TEST (harmor_image)
  10722. {
  10723. // REA_rodada75: image synthesis — a looping 6-column image (gain + frequency planes) read at a speed, cubic gain and
  10724. // linear frequency interpolation, Hz frequency mode, formant shift — against our reference model (r75 imgm.py)
  10725. const auto blob = harmorTestState (false, true);
  10726. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_IMAGE"))
  10727. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  10728. CHECK (lynx::harmorStateUsable (blob));
  10729. CHECK (lynx::harmorFallbacks (blob).empty()); // (the image is modelled)
  10730. 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);
  10731. CHECK (ref.size() == 2 * 8192);
  10732. if (std::getenv ("LYNX_PRINT_HARMOR_IMAGE") != nullptr)
  10733. for (int i : { 100, 1000, 2500, 3000, 5000, 6500, 8191 })
  10734. std::printf ("harmor image %d %.8g %.8g\n", i, (double) ref[2 * (std::size_t) i], (double) ref[2 * (std::size_t) i + 1]);
  10735. const std::tuple<int, float, float> golden[] {
  10736. { 100, 0.0022269291f, 0.018017211f }, { 1000, 0.013962966f, -0.028602127f }, { 2500, 0.016781429f, -0.028931717f },
  10737. { 3000, -0.010532441f, -0.0061010128f }, { 5000, -0.017426063f, 0.016474312f }, { 6500, 0.1689423f, 0.078417062f },
  10738. { 8191, -0.045403875f, -0.049045475f } };
  10739. for (const auto& [i, gl, gr] : golden)
  10740. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  10741. // the image changes the sound: without it (gain mix 0, frequency scale 0) the same notes render differently
  10742. auto plain = blob;
  10743. const std::int32_t zero = 0;
  10744. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 89), &zero, 4);
  10745. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 92), &zero, 4);
  10746. std::memcpy (plain.data() + lynx::harmorParamOffset (plain, 0, 94), &zero, 4);
  10747. 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);
  10748. double diff = 0.0, energy = 0.0;
  10749. for (std::size_t i = 0; i < ref.size(); ++i)
  10750. diff += (double) (ref[i] - ref0[i]) * (ref[i] - ref0[i]), energy += (double) ref[i] * ref[i];
  10751. CHECK (energy > 1.0 && diff > 0.01 * energy);
  10752. // a resynthesis-mode image (a sample analysed at load time) is not modelled: reported, the part plays its timbres
  10753. auto resyn = blob;
  10754. {
  10755. // the image object follows the 26 curves: find its header (version 0, mode 0, loop 1) from the end of part A
  10756. 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 };
  10757. const auto it = std::search (resyn.begin(), resyn.end(), std::begin (hdr), std::end (hdr));
  10758. CHECK (it != resyn.end());
  10759. if (it != resyn.end())
  10760. *(it + 4) = 1;
  10761. }
  10762. const auto fb = lynx::harmorFallbacks (resyn);
  10763. CHECK (std::any_of (fb.begin(), fb.end(), [] (const std::string& f) { return f.find ("resynthesis") != std::string::npos; }));
  10764. }
  10765. TEST (harmor_round75_features)
  10766. {
  10767. // REA_rodada75: arpeggio markers, noise resonance, pluck blur mode, pluck blend, harmonic randomness, phaser and
  10768. // harmonizer masks — LYNX's engine against our reference model (r75 hmodel.py, the Mersenne Twister seeded like LYNX's)
  10769. 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 } };
  10770. struct Case { int features; std::array<std::tuple<int, float, float>, 4> golden; };
  10771. const Case cases[] {
  10772. { 1, { { { 2000, 0.00326227601f, -0.00143631078f }, { 4500, 0.104633873f, 0.0262026641f }, { 7000, -0.00242280135f, 0.00364600495f }, { 11000, -0.0029983636f, -0.00308720154f } } } },
  10773. { 2, { { { 2000, 0.294110536f, -0.229805974f }, { 4500, -0.00492941859f, -0.00132748827f }, { 7000, 0.0132401136f, 0.00544014934f }, { 11000, -0.00105760543f, -0.00210882883f } } } },
  10774. { 4, { { { 2000, 0.0720629904f, -0.0296807697f }, { 4500, -0.10764533f, 0.113311439f }, { 7000, -0.229827746f, -0.101087983f }, { 11000, -0.0519010639f, -0.0665841137f } } } },
  10775. { 8, { { { 2000, 0.0720039901f, -0.0297230242f }, { 4500, -0.107470558f, 0.113653905f }, { 7000, -0.228478677f, -0.152556434f }, { 11000, -0.0412466244f, -0.0504480202f } } } },
  10776. { 16, { { { 2000, 0.0634379551f, -0.0309438618f }, { 4500, -0.112099687f, 0.112784195f }, { 7000, -0.231666087f, -0.167064955f }, { 11000, -0.0411841346f, -0.0503804788f } } } },
  10777. { 32, { { { 2000, 0.0862953135f, -0.0358822417f }, { 4500, 0.0694009664f, 0.141198453f }, { 7000, -0.158969622f, -0.103794064f }, { 11000, -0.0294273162f, -0.0417808629f } } } },
  10778. { 63, { { { 2000, 0.0172010472f, -0.0106831535f }, { 4500, 0.556600683f, 0.0811084217f }, { 7000, -0.00255421684f, 0.020610704f }, { 11000, -0.00318711501f, 0.000247763831f } } } } };
  10779. for (const auto& c : cases)
  10780. {
  10781. const auto blob = harmorTestState (false, false, c.features);
  10782. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR_R75"))
  10783. std::ofstream (std::string (dump) + std::to_string (c.features) + ".bin", std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  10784. CHECK (lynx::harmorStateUsable (blob));
  10785. CHECK (lynx::harmorFallbacks (blob).empty());
  10786. const auto ref = lynx::harmor::renderReference (blob, chord, 44100.0, 12000);
  10787. if (std::getenv ("LYNX_PRINT_HARMOR_R75") != nullptr)
  10788. for (int i : { 2000, 4500, 7000, 11000 })
  10789. 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]);
  10790. for (const auto& [i, gl, gr] : c.golden)
  10791. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  10792. }
  10793. }
  10794. TEST (sytrus_main_settings)
  10795. {
  10796. // the channel settings panel's controls (REA_rodada74 §3): FL's "Main settings" ints inside the zlib state
  10797. SytrusTestPatch t;
  10798. t.cell (0, 0, 8, 256);
  10799. t.main[173] = 3;
  10800. const auto blob = t.blob();
  10801. const auto& S = lynx::sytrus::settings();
  10802. auto find = [&] (const char* name) {
  10803. for (int i = 0; i < (int) S.size(); ++i)
  10804. if (std::string (S[(std::size_t) i].name) == name)
  10805. return i;
  10806. return -1;
  10807. };
  10808. const int master = find ("Master level"), unison = find ("Unison"), porta = find ("Porta"), live = find ("Live oversampling");
  10809. CHECK (master >= 0 && unison >= 0 && porta >= 0 && live >= 0 && S.size() == 25u);
  10810. CHECK (lynx::sytrus::settingValue (blob, master) == 128 && lynx::sytrus::settingValue (blob, unison) == 3);
  10811. CHECK (lynx::sytrus::settingValue (blob, porta) == 0 && lynx::sytrus::settingValue (blob, find ("Mod X")) == 256);
  10812. CHECK (! lynx::sytrus::settingValue (blob, -1) && ! lynx::sytrus::settingValue (blob, (int) S.size()));
  10813. auto broken = blob;
  10814. broken.resize (broken.size() / 2);
  10815. CHECK (! lynx::sytrus::settingValue (broken, master) && ! lynx::sytrus::setSetting (broken, master, 64));
  10816. // master level 64: gain (11^(64/128) − 1)/10 instead of 1 (REA_rodada11 §1.3); the state stays readable
  10817. auto quiet = blob;
  10818. CHECK (lynx::sytrus::setSetting (quiet, master, 64) && lynx::sytrus::stateUsable (quiet));
  10819. CHECK (lynx::sytrus::settingValue (quiet, master) == 64 && lynx::sytrus::settingValue (quiet, unison) == 3);
  10820. CHECK (quiet[0] == blob[0] && quiet[1] == blob[1]); // version word kept
  10821. const auto [l1, r1] = sytrusRender (blob, false, 4096, { { 60, 0, -1 } });
  10822. const auto [l2, r2] = sytrusRender (quiet, false, 4096, { { 60, 0, -1 } });
  10823. const auto g = (std::sqrt (11.0) - 1.0) / 10.0;
  10824. double worst = 0.0;
  10825. for (int k = 0; k < 4096; ++k)
  10826. worst = std::max (worst, std::abs ((double) l2[(std::size_t) k] - g * l1[(std::size_t) k]));
  10827. CHECK (worst < 1e-5);
  10828. (void) r1, (void) r2;
  10829. // clamped to the control's range; PORTA is the flag byte the engine reads
  10830. auto edited = blob;
  10831. CHECK (lynx::sytrus::setSetting (edited, unison, 42) && lynx::sytrus::settingValue (edited, unison) == 9);
  10832. CHECK (lynx::sytrus::setSetting (edited, porta, 1) && lynx::sytrus::settingValue (edited, porta) == 1);
  10833. CHECK (lynx::sytrus::setSetting (edited, live, 2) && lynx::sytrus::settingValue (edited, live) == 2);
  10834. CHECK (lynx::sytrus::parsePatch (edited) != nullptr);
  10835. }
  10836. TEST (harmor_effects_rack)
  10837. {
  10838. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  10839. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  10840. auto blob = harmorTestState (false);
  10841. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  10842. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  10843. };
  10844. set (blob, 0, 10, 1000); // all to the FX send
  10845. auto render = [] (const std::vector<std::uint8_t>& st) {
  10846. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  10847. };
  10848. const auto dry = render (blob);
  10849. auto rev = blob;
  10850. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  10851. 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);
  10852. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  10853. const auto bypassed = render (rev);
  10854. double eb = 0.0, db = 0.0;
  10855. for (std::size_t i = 0; i < dry.size(); ++i)
  10856. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  10857. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  10858. auto after = rev;
  10859. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  10860. for (int k = 0; k < 6; ++k)
  10861. set (after, -1, 53 + k, order[k]);
  10862. const auto wet = render (after);
  10863. double e = 0.0, d = 0.0;
  10864. bool finite = true;
  10865. for (std::size_t i = 0; i < wet.size(); ++i)
  10866. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  10867. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  10868. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  10869. auto dist = harmorTestState (false);
  10870. {
  10871. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  10872. const std::int32_t six = 6;
  10873. std::memcpy (dist.data() + at, &six, 4);
  10874. }
  10875. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  10876. 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);
  10877. set (dist, -1, 47, 1050);
  10878. lynx::ChannelDef c;
  10879. c.instrument = lynx::InstrumentKind::harmor;
  10880. c.pluginState = dist;
  10881. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  10882. inst->configure (c, 120.0);
  10883. inst->prepare (44100.0);
  10884. std::vector<float> l (44100 * 4), r (l.size());
  10885. inst->noteOn (60, 0.8f);
  10886. double energy = 0.0;
  10887. for (int s = 0; s < (int) l.size(); s += 512)
  10888. {
  10889. if (s == 8192)
  10890. inst->noteOff (60);
  10891. inst->render (l.data() + s, r.data() + s, std::min (512, (int) l.size() - s), 1.0f, 1.0f);
  10892. }
  10893. for (std::size_t i = 0; i < l.size(); ++i)
  10894. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  10895. CHECK (finite && energy > 1.0);
  10896. std::vector<float> tail (44100 * 6), tailR (tail.size());
  10897. for (int s = 0; s < (int) tail.size(); s += 512)
  10898. inst->render (tail.data() + s, tailR.data() + s, std::min (512, (int) tail.size() - s), 1.0f, 1.0f);
  10899. CHECK (inst->idle());
  10900. }
  10901. TEST (harmless_engine)
  10902. {
  10903. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  10904. std::vector<std::uint8_t> blob;
  10905. 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))); };
  10906. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  10907. for (int i = 11; i < 23; ++i)
  10908. p[(std::size_t) i] = 1000;
  10909. 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;
  10910. 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;
  10911. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  10912. 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;
  10913. put (12);
  10914. for (auto v : p)
  10915. put (v);
  10916. for (int i = 0; i < 7; ++i)
  10917. put (0);
  10918. put (524); // Custom colour table: 524 × (amplitude, phase)
  10919. for (int i = 0; i < 1048; ++i)
  10920. {
  10921. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  10922. std::uint32_t bits = 0;
  10923. std::memcpy (&bits, &f, 4);
  10924. put ((std::int32_t) bits);
  10925. }
  10926. const auto parsed = lynx::harmlessParams (blob);
  10927. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  10928. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  10929. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  10930. auto old = blob;
  10931. old[0] = 3; // versions below 4 are not Harmless states we know
  10932. CHECK (! lynx::harmlessParams (old));
  10933. old = blob;
  10934. old.resize (300);
  10935. CHECK (! lynx::harmlessParams (old));
  10936. lynx::ChannelDef c;
  10937. c.instrument = lynx::InstrumentKind::harmless;
  10938. c.modelParams = *parsed;
  10939. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  10940. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  10941. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  10942. inst->configure (def, 120.0);
  10943. inst->prepare (44100.0);
  10944. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  10945. inst->noteOn (60, 0.78125f);
  10946. for (int s = 0; s < total; s += 512)
  10947. {
  10948. if (s == offAt)
  10949. inst->noteOff (60);
  10950. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  10951. }
  10952. return std::make_tuple (l, r, std::move (inst));
  10953. };
  10954. {
  10955. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  10956. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  10957. const std::tuple<int, float, float> golden[] {
  10958. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  10959. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  10960. for (const auto& [i, gl, gr] : golden)
  10961. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10962. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  10963. std::vector<float> tail (44100), tailR (tail.size());
  10964. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  10965. CHECK (inst->idle());
  10966. }
  10967. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  10968. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  10969. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  10970. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  10971. auto mono = c;
  10972. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  10973. mono.modelParams[65] = 1.0f;
  10974. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  10975. double el = 0.0, er = 0.0;
  10976. for (int i = 0; i < 2048; ++i)
  10977. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  10978. CHECK (el < 1e-10 * er && er > 1.0);
  10979. }
  10980. TEST (startup_sound)
  10981. {
  10982. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  10983. // a landing "pop" at impactTime and a chime ringing after it
  10984. using namespace lynx::startup;
  10985. const auto sound = makeSound (48000.0);
  10986. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  10987. float peak = 0.0f;
  10988. bool finite = true;
  10989. for (std::size_t i = 0; i < sound.left.size(); ++i)
  10990. {
  10991. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  10992. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  10993. }
  10994. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  10995. auto rms = [&sound] (double from, double to) {
  10996. double e = 0.0;
  10997. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  10998. for (auto i = a; i < b; ++i)
  10999. e += (double) sound.left[i] * sound.left[i];
  11000. return std::sqrt (e / (double) (b - a));
  11001. };
  11002. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  11003. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  11004. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  11005. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  11006. }
  11007. namespace
  11008. {
  11009. // little-endian byte writer for the state builders below
  11010. struct Bytes
  11011. {
  11012. std::vector<std::uint8_t> b;
  11013. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  11014. 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); }
  11015. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  11016. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  11017. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  11018. };
  11019. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  11020. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  11021. // unison, slide, distortion, delay, reverb and the output EQ.
  11022. std::vector<std::uint8_t> gmsTestState()
  11023. {
  11024. std::array<float, 212> P {};
  11025. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  11026. P[(std::size_t) i] = 0.5f;
  11027. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  11028. 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;
  11029. 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;
  11030. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  11031. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  11032. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  11033. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  11034. Bytes preset;
  11035. preset.raw ("4nyS", 4);
  11036. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  11037. {
  11038. const auto n = std::strlen (path);
  11039. preset.raw (path, n);
  11040. preset.zeros (512 - n);
  11041. }
  11042. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  11043. for (auto v : P)
  11044. preset.f32 (v);
  11045. preset.raw ("LYNX test", 9);
  11046. preset.zeros (512 - 9);
  11047. Bytes s;
  11048. s.raw ("SSLF", 4);
  11049. s.i32 ((std::int32_t) preset.b.size());
  11050. s.bytes (preset.b);
  11051. return s.b;
  11052. }
  11053. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  11054. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  11055. // amplitude through the matrix), the master ADSR and every plugin effect on.
  11056. std::vector<std::uint8_t> morphineTestState()
  11057. {
  11058. auto mbp = [] (int seed) {
  11059. Bytes b;
  11060. b.raw ("0PBM", 4);
  11061. for (int a = 0; a < 3; ++a)
  11062. for (int byte = 0; byte < 16; ++byte)
  11063. {
  11064. b.b.push_back (0xff);
  11065. for (int bit = 0; bit < 8; ++bit)
  11066. {
  11067. const auto k = byte * 8 + bit;
  11068. const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
  11069. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  11070. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  11071. b.f32 (v);
  11072. }
  11073. }
  11074. return b.b;
  11075. };
  11076. auto msp = [&] (int frames, int seed, int loopMode) {
  11077. Bytes b;
  11078. b.raw ("0PSM", 4);
  11079. b.i32 (frames);
  11080. for (int f = 0; f < frames; ++f)
  11081. {
  11082. const auto m = mbp (seed + f);
  11083. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  11084. b.bytes (m);
  11085. }
  11086. b.i32 (0x424);
  11087. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  11088. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  11089. for (int k = 0; k < 128; ++k)
  11090. b.f32 (0.3f);
  11091. for (int k = 0; k < 128; ++k)
  11092. b.f32 (1.0f);
  11093. return b.b;
  11094. };
  11095. auto mvc = [&] (int frames, int seed, int pwmMode) {
  11096. Bytes z;
  11097. z.raw ("0HZM", 4);
  11098. z.i32 (1);
  11099. const auto s = msp (frames, seed, 1);
  11100. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  11101. z.bytes (s);
  11102. Bytes b;
  11103. b.raw ("0CVM", 4);
  11104. b.i32 ((std::int32_t) z.b.size());
  11105. b.bytes (z.b);
  11106. 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);
  11107. return b.b;
  11108. };
  11109. Bytes p;
  11110. p.raw ("0roM", 4);
  11111. p.raw ("0RPM", 4);
  11112. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  11113. {
  11114. p.i32 ((std::int32_t) g.size());
  11115. p.bytes (g);
  11116. }
  11117. {
  11118. Bytes m;
  11119. m.raw ("0LMM", 4);
  11120. m.i32 (2), m.i32 (12);
  11121. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  11122. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  11123. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  11124. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  11125. m.f32 (v);
  11126. for (int k = 0; k < 4; ++k)
  11127. m.i32 (0);
  11128. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  11129. p.i32 ((std::int32_t) m.b.size());
  11130. p.bytes (m.b);
  11131. }
  11132. {
  11133. Bytes d;
  11134. d.raw ("0DMM", 4);
  11135. d.i32 (4);
  11136. for (int e = 0; e < 4; ++e)
  11137. {
  11138. Bytes g;
  11139. g.raw ("0GEM", 4);
  11140. g.i32 (3), g.i32 (8);
  11141. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  11142. 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),
  11143. g.i32 (e == 0 ? 1 : 0);
  11144. d.i32 ((std::int32_t) g.b.size());
  11145. d.bytes (g.b);
  11146. }
  11147. d.i32 (0x94);
  11148. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  11149. const float amt[12] = { 0.56f, 0.7f };
  11150. for (int k = 0; k < 12; ++k)
  11151. d.i32 (src[k]);
  11152. for (int k = 0; k < 12; ++k)
  11153. d.i32 (dst[k]);
  11154. for (int k = 0; k < 12; ++k)
  11155. d.f32 (amt[k]);
  11156. p.i32 ((std::int32_t) d.b.size());
  11157. p.bytes (d.b);
  11158. }
  11159. {
  11160. 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
  11161. 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 …
  11162. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  11163. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  11164. Bytes g;
  11165. g.i32 (0x5c4);
  11166. for (auto v : A)
  11167. g.f32 (v);
  11168. g.zeros (0x5c4 - g.b.size());
  11169. p.bytes (g.b);
  11170. }
  11171. Bytes s;
  11172. s.raw ("SSLF", 4);
  11173. s.i32 ((std::int32_t) p.b.size());
  11174. s.bytes (p.b);
  11175. return s.b;
  11176. }
  11177. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  11178. {
  11179. if (const char* path = std::getenv (var))
  11180. {
  11181. std::ofstream out (path, std::ios::binary);
  11182. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  11183. }
  11184. }
  11185. } // namespace
  11186. TEST (dx10_engine)
  11187. {
  11188. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  11189. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  11190. // instrument front-end
  11191. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  11192. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  11193. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  11194. blob[0] = 1;
  11195. std::memcpy (blob.data() + 4, P, sizeof (P));
  11196. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  11197. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  11198. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  11199. {
  11200. auto bad = blob;
  11201. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  11202. CHECK (! lynx::dx10StateUsable (bad));
  11203. }
  11204. using N = lynx::dx10::RefNote;
  11205. 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 } };
  11206. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  11207. auto check = [&] (const Golden& g) {
  11208. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  11209. CHECK (y.size() == 2 * 16384);
  11210. if (y.size() != 2 * 16384)
  11211. return y;
  11212. double el = 0.0, er = 0.0;
  11213. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  11214. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  11215. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  11216. for (const auto& [i, l, r] : g.at)
  11217. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  11218. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  11219. return y;
  11220. };
  11221. lynx::dx10::RefOptions plain, odd;
  11222. odd.block = 333, odd.maxPoly = 1;
  11223. check ({ blob, plain, 44100, 672.293115, 778.393071,
  11224. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  11225. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  11226. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  11227. check ({ blob, odd, 48000, 122.232761, 493.320297,
  11228. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  11229. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  11230. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  11231. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  11232. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  11233. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11234. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  11235. for (const auto& [i, v] : fields)
  11236. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  11237. };
  11238. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  11239. std::memcpy (s.data() + 0x15c, &modX, 4);
  11240. std::memcpy (s.data() + 0x160, &modY, 4);
  11241. std::memcpy (s.data() + 0x168, &type, 4);
  11242. };
  11243. std::vector<std::uint8_t> snap (364, 0);
  11244. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  11245. 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 } });
  11246. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  11247. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  11248. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  11249. settings (snap, 140, 120, 7);
  11250. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  11251. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  11252. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  11253. lynx::dx10::RefOptions ticks;
  11254. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  11255. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  11256. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  11257. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  11258. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  11259. std::vector<std::uint8_t> decay (364, 0);
  11260. for (int k = 0; k < 5; ++k)
  11261. record (decay, k, {});
  11262. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  11263. settings (decay, 256, 0, 0);
  11264. auto ticks48 = ticks;
  11265. ticks48.block = 333;
  11266. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  11267. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  11268. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  11269. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  11270. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  11271. silentTail = silentTail && y[i] == 0.0f;
  11272. CHECK (silentTail);
  11273. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  11274. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  11275. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  11276. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  11277. auto noVib = blob;
  11278. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  11279. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  11280. lynx::ChannelDef c;
  11281. c.instrument = lynx::InstrumentKind::dx10;
  11282. c.pluginState = noVib;
  11283. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  11284. inst->configure (c, 120.0);
  11285. inst->prepare (44100.0);
  11286. const float V = 0.6f, pan = 0.25f;
  11287. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  11288. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  11289. std::vector<float> l (16384), r (16384);
  11290. for (int s = 0; s < 16384; s += 512)
  11291. {
  11292. if (s == 0)
  11293. inst->noteOn (60, 0.78125f);
  11294. if (s == 2048)
  11295. {
  11296. inst->setNextNoteExpression (-0.5f, 25.0f);
  11297. inst->noteOn (67, 0.5f);
  11298. }
  11299. if (s == 6144)
  11300. inst->noteOff (60);
  11301. if (s == 8192)
  11302. inst->noteOff (67);
  11303. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  11304. }
  11305. // the channel level reaches the engine as FL's (recovered to float precision)
  11306. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  11307. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  11308. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  11309. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  11310. 44100.0, 16384);
  11311. double worst = 0.0, peak = 0.0;
  11312. for (int i = 0; i < 16384; ++i)
  11313. 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]) }),
  11314. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  11315. CHECK (worst == 0.0 && peak > 0.05);
  11316. // the released voices end (DX10's own release) and the instrument goes idle
  11317. std::vector<float> tail (44100), tailR (tail.size());
  11318. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  11319. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  11320. CHECK (inst->idle());
  11321. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  11322. {
  11323. lynx::ChannelDef cs = c;
  11324. cs.pluginState = withSnap;
  11325. cs.modelParams = { 96.0f };
  11326. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  11327. si->configure (cs, 120.0);
  11328. si->prepare (44100.0);
  11329. std::vector<float> sl (32768), sr (32768);
  11330. si->noteOn (48, 0.78125f);
  11331. for (int s = 0; s < 32768; s += 512)
  11332. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  11333. double e1 = 0.0, e2 = 0.0;
  11334. bool finite = true;
  11335. for (int i = 0; i < 32768; ++i)
  11336. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  11337. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  11338. }
  11339. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  11340. auto edited = blob;
  11341. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  11342. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  11343. }
  11344. TEST (threeosc_engine)
  11345. {
  11346. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  11347. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  11348. 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 };
  11349. 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) {
  11350. std::vector<std::uint8_t> st (0x60, 0);
  11351. const std::int32_t version = 14;
  11352. std::memcpy (st.data(), &version, 4);
  11353. std::memcpy (st.data() + 4, p, 21 * 4);
  11354. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  11355. st.insert (st.end(), snap.begin(), snap.end());
  11356. return st;
  11357. };
  11358. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  11359. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11360. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  11361. for (const auto& [i, v] : fields)
  11362. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  11363. };
  11364. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  11365. std::memcpy (s.data() + 0x15c, &modX, 4);
  11366. std::memcpy (s.data() + 0x160, &modY, 4);
  11367. std::memcpy (s.data() + 0x168, &type, 4);
  11368. };
  11369. std::vector<std::uint8_t> neutral (364, 0);
  11370. for (int k = 0; k < 5; ++k)
  11371. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  11372. settings (neutral, 256, 0, 0);
  11373. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  11374. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  11375. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  11376. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  11377. // version 10 (20 parameters, no invert/AM bytes)
  11378. {
  11379. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  11380. v12[0] = 12;
  11381. v12[0x58] = 1;
  11382. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  11383. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  11384. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  11385. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  11386. v10[0] = 10;
  11387. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  11388. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  11389. }
  11390. using N = lynx::threeosc::RefNote;
  11391. 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 } };
  11392. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  11393. auto check = [&] (const Golden& g) {
  11394. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  11395. CHECK (y.size() == 2 * 16384);
  11396. if (y.size() != 2 * 16384)
  11397. return y;
  11398. double el = 0.0, er = 0.0;
  11399. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  11400. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  11401. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  11402. for (const auto& [i, l, r] : g.at)
  11403. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  11404. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  11405. return y;
  11406. };
  11407. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  11408. lynx::threeosc::RefOptions plain;
  11409. plain.seed = 12345;
  11410. check ({ blob, plain, 44100, 681.6491972612481, 1509.7245664533937,
  11411. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  11412. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  11413. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  11414. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  11415. 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 };
  11416. lynx::threeosc::RefOptions odd;
  11417. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  11418. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.27241060751771, 1996.4548848690094,
  11419. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  11420. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  11421. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  11422. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  11423. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  11424. std::vector<std::uint8_t> snap (364, 0);
  11425. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  11426. 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 } });
  11427. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  11428. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  11429. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  11430. settings (snap, 140, 120, 7);
  11431. lynx::threeosc::RefOptions ticks;
  11432. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  11433. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  11434. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  11435. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  11436. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  11437. auto ticks48 = ticks;
  11438. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  11439. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  11440. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  11441. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  11442. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  11443. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  11444. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  11445. // one agree to float rounding, so the golden values hold to 1e-5)
  11446. {
  11447. 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 };
  11448. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  11449. hq[0x5c] = 1;
  11450. CHECK (lynx::threeOscParam (hq, 25) == 1);
  11451. lynx::threeosc::RefOptions o;
  11452. o.seed = 3;
  11453. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  11454. CHECK (y.size() == 2 * 16384);
  11455. if (y.size() == 2 * 16384)
  11456. {
  11457. double el = 0.0, er = 0.0;
  11458. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  11459. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  11460. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  11461. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  11462. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  11463. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  11464. for (const auto& [i, l, r] : at)
  11465. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  11466. }
  11467. }
  11468. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  11469. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  11470. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  11471. lynx::ChannelDef c;
  11472. c.instrument = lynx::InstrumentKind::threeosc;
  11473. c.pluginState = blob;
  11474. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  11475. inst->configure (c, 120.0);
  11476. inst->prepare (44100.0);
  11477. const float V = 0.6f, pan = 0.25f;
  11478. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  11479. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  11480. std::vector<float> l (16384), r (16384);
  11481. for (int s = 0; s < 16384; s += 512)
  11482. {
  11483. if (s == 0)
  11484. inst->noteOn (60, 0.78125f);
  11485. if (s == 2048)
  11486. {
  11487. inst->setNextNoteExpression (-0.5f, 25.0f);
  11488. inst->noteOn (67, 0.5f);
  11489. }
  11490. if (s == 6144)
  11491. inst->noteOff (60);
  11492. if (s == 8192)
  11493. inst->noteOff (67);
  11494. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  11495. }
  11496. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  11497. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  11498. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  11499. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  11500. 44100.0, 16384);
  11501. double worst = 0.0, peak = 0.0;
  11502. for (int i = 0; i < 16384; ++i)
  11503. 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]) }),
  11504. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  11505. CHECK (worst == 0.0 && peak > 0.05);
  11506. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  11507. CHECK (inst->idle());
  11508. // parameter edits write the raw integers, clamped to the knob ranges
  11509. auto edited = blob;
  11510. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  11511. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  11512. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  11513. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  11514. }
  11515. TEST (gms_engine)
  11516. {
  11517. const auto blob = gmsTestState();
  11518. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  11519. CHECK (lynx::gmsStateUsable (blob));
  11520. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  11521. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  11522. auto broken = blob;
  11523. broken[8] = 'X';
  11524. CHECK (! lynx::gmsStateUsable (broken));
  11525. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  11526. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  11527. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  11528. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11529. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  11530. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  11531. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  11532. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  11533. CHECK (y.size() == 2 * 66150u);
  11534. 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 } };
  11535. for (const auto& g : golden)
  11536. 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);
  11537. double e = 0.0;
  11538. for (auto v : y)
  11539. e += (double) v * v;
  11540. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  11541. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11542. lynx::ChannelDef c;
  11543. c.instrument = lynx::InstrumentKind::gms;
  11544. c.pluginState = blob;
  11545. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  11546. inst->configure (c, 140.0);
  11547. inst->prepare (44100.0);
  11548. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11549. for (int b = 0; b < 16; ++b)
  11550. {
  11551. if (b == 0)
  11552. inst->noteOn (60, 0.78125f);
  11553. if (b == 10)
  11554. inst->noteOff (60);
  11555. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11556. }
  11557. 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, "");
  11558. double diff = 0.0, energy = 0.0;
  11559. for (int i = 0; i < 8192; ++i)
  11560. {
  11561. 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]) });
  11562. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11563. }
  11564. CHECK (diff == 0.0 && energy > 1e-3);
  11565. }
  11566. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  11567. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  11568. std::vector<std::uint8_t> toxicTestState()
  11569. {
  11570. auto s = lynx::toxicDefaultState();
  11571. static const std::pair<int, double> set[] = {
  11572. { 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 },
  11573. { 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 },
  11574. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  11575. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  11576. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  11577. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  11578. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  11579. for (const auto& [index, value] : set)
  11580. {
  11581. const auto v = (float) value;
  11582. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  11583. }
  11584. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  11585. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  11586. std::memcpy (s.data() + at, &amp, 4);
  11587. std::memcpy (s.data() + at + 512, &phase, 4);
  11588. };
  11589. for (int h = 0; h < 24; ++h)
  11590. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  11591. for (int k = 0; k < 16; ++k)
  11592. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  11593. return s;
  11594. }
  11595. TEST (toxic_engine)
  11596. {
  11597. const auto blob = toxicTestState();
  11598. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  11599. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  11600. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  11601. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  11602. auto broken = blob;
  11603. broken[8] = 'X';
  11604. CHECK (! lynx::toxicStateUsable (broken));
  11605. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  11606. auto needs = blob;
  11607. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  11608. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  11609. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  11610. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  11611. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  11612. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11613. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11614. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11615. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11616. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  11617. CHECK (y.size() == 2 * 66150u);
  11618. 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 } };
  11619. for (const auto& g : golden)
  11620. 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);
  11621. double e = 0.0;
  11622. for (auto v : y)
  11623. e += (double) v * v;
  11624. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  11625. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11626. lynx::ChannelDef c;
  11627. c.instrument = lynx::InstrumentKind::toxic;
  11628. c.pluginState = blob;
  11629. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  11630. inst->configure (c, 140.0);
  11631. inst->prepare (44100.0);
  11632. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11633. for (int b = 0; b < 16; ++b)
  11634. {
  11635. if (b == 0)
  11636. inst->noteOn (60, 0.78125f);
  11637. if (b == 10)
  11638. inst->noteOff (60);
  11639. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11640. }
  11641. 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 } },
  11642. 44100.0, 8192, "", 512, 140.0);
  11643. double diff = 0.0, energy = 0.0;
  11644. for (int i = 0; i < 8192; ++i)
  11645. {
  11646. 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]) });
  11647. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11648. }
  11649. CHECK (diff == 0.0 && energy > 1e-3);
  11650. }
  11651. TEST (morphine_engine)
  11652. {
  11653. const auto blob = morphineTestState();
  11654. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  11655. CHECK (lynx::morphineStateUsable (blob));
  11656. CHECK (lynx::morphineNoiseName (blob).empty());
  11657. const auto at = lynx::morphineParamOffset (blob, 9);
  11658. CHECK (at > 0);
  11659. float a9 = 0.0f;
  11660. std::memcpy (&a9, blob.data() + at, 4);
  11661. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  11662. auto broken = blob;
  11663. broken[12] = 'X'; // the MPR0 tag
  11664. CHECK (! lynx::morphineStateUsable (broken));
  11665. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  11666. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  11667. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  11668. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  11669. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  11670. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  11671. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  11672. CHECK (y.size() == 2 * 80000u);
  11673. 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 } };
  11674. for (const auto& g : golden)
  11675. 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);
  11676. double e = 0.0;
  11677. for (auto v : y)
  11678. e += (double) v * v;
  11679. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  11680. // the instrument: same engine behind LYNX's channel gains
  11681. lynx::ChannelDef c;
  11682. c.instrument = lynx::InstrumentKind::morphine;
  11683. c.pluginState = blob;
  11684. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  11685. inst->configure (c, 120.0);
  11686. inst->prepare (44100.0);
  11687. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11688. for (int b = 0; b < 16; ++b)
  11689. {
  11690. if (b == 0)
  11691. inst->noteOn (60, 0.78125f);
  11692. if (b == 10)
  11693. inst->noteOff (60);
  11694. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11695. }
  11696. 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, "");
  11697. double diff = 0.0, energy = 0.0;
  11698. for (int i = 0; i < 8192; ++i)
  11699. {
  11700. 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]) });
  11701. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11702. }
  11703. CHECK (diff == 0.0 && energy > 1e-3);
  11704. }
  11705. namespace
  11706. {
  11707. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  11708. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  11709. {
  11710. const std::string name = "LYNX test";
  11711. Bytes body;
  11712. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  11713. body.raw ("HEAD", 4);
  11714. body.i32 (6 + 2 * (std::int32_t) name.size());
  11715. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  11716. body.raw (reinterpret_cast<const char*> (head), 6);
  11717. for (char ch : name)
  11718. body.raw (&ch, 1), body.zeros (1);
  11719. body.raw ("DATA", 4);
  11720. body.i32 (176);
  11721. body.raw ("0rev", 4);
  11722. for (auto v : P)
  11723. body.f32 (v);
  11724. Bytes s;
  11725. s.raw ("SSLF", 4);
  11726. s.i32 ((std::int32_t) body.b.size());
  11727. s.bytes (body.b);
  11728. return s.b;
  11729. }
  11730. std::array<float, 43> miniSynthDefaults()
  11731. {
  11732. 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,
  11733. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  11734. }
  11735. // per-process suffix for scratch files: several test binaries (one per worktree) may run at the same time, and a
  11736. // fixed name let one process read a file another one was rewriting
  11737. std::string processTempTag()
  11738. {
  11739. static const std::string tag = std::to_string (std::random_device {}()) + "_"
  11740. + std::to_string (std::chrono::steady_clock::now().time_since_epoch().count());
  11741. static const struct Cleanup
  11742. {
  11743. ~Cleanup()
  11744. {
  11745. std::error_code ec;
  11746. std::filesystem::remove (std::filesystem::temp_directory_path (ec) / ("lynx_test_oscdata_" + tag + ".dat"), ec);
  11747. std::filesystem::remove_all (std::filesystem::temp_directory_path (ec) / ("lynx_bassdrum_test_" + tag), ec);
  11748. }
  11749. } cleanup;
  11750. (void) cleanup;
  11751. return tag;
  11752. }
  11753. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  11754. std::string miniSynthTestOscdata()
  11755. {
  11756. const auto path = (std::filesystem::temp_directory_path() / ("lynx_test_oscdata_" + processTempTag() + ".dat")).string();
  11757. std::vector<float> d (2048);
  11758. for (int h = 0; h < 1024; ++h)
  11759. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  11760. std::ofstream out (path, std::ios::binary);
  11761. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  11762. return path;
  11763. }
  11764. } // namespace
  11765. TEST (minisynth_engine)
  11766. {
  11767. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  11768. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  11769. 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 },
  11770. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  11771. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  11772. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  11773. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  11774. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  11775. // B: lead (legato glide), BELL, LFO → pitch
  11776. 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 },
  11777. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  11778. for (const auto& [i, v] : a)
  11779. A[(std::size_t) i] = v;
  11780. for (const auto& [i, v] : b)
  11781. B[(std::size_t) i] = v;
  11782. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  11783. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  11784. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  11785. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  11786. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  11787. auto shortData = blobA;
  11788. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  11789. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  11790. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  11791. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  11792. const auto osc = miniSynthTestOscdata();
  11793. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11794. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11795. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11796. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11797. struct Golden { int i; float l, r; };
  11798. 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 } };
  11799. 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 } };
  11800. for (int which = 0; which < 2; ++which)
  11801. {
  11802. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  11803. CHECK (y.size() == 2 * 66150u);
  11804. if (y.size() != 2 * 66150u)
  11805. continue;
  11806. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  11807. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  11808. double e = 0.0;
  11809. for (auto v : y)
  11810. e += (double) v * v;
  11811. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  11812. }
  11813. // missing oscdata.dat: silent tables, still finite
  11814. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  11815. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  11816. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11817. lynx::ChannelDef c;
  11818. c.instrument = lynx::InstrumentKind::minisynth;
  11819. c.pluginState = blobA;
  11820. c.samplePath = osc;
  11821. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  11822. inst->configure (c, 120.0);
  11823. inst->prepare (44100.0);
  11824. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11825. for (int k = 0; k < 16; ++k)
  11826. {
  11827. if (k == 0)
  11828. inst->noteOn (60, 0.78125f);
  11829. if (k == 10)
  11830. inst->noteOff (60);
  11831. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  11832. }
  11833. 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 } },
  11834. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  11835. double diff = 0.0, energy = 0.0;
  11836. for (int i = 0; i < 8192; ++i)
  11837. {
  11838. 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]) });
  11839. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11840. }
  11841. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  11842. }
  11843. namespace
  11844. {
  11845. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  11846. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  11847. std::vector<std::uint8_t> poizoneTestState()
  11848. {
  11849. std::array<float, 156> P {};
  11850. 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 },
  11851. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  11852. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  11853. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  11854. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  11855. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  11856. { 79, 1.0f } };
  11857. for (const auto& [i, v] : values)
  11858. P[(std::size_t) i] = v;
  11859. for (int i = 49; i < 65; ++i)
  11860. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  11861. for (int i = 81; i < 155; ++i)
  11862. P[(std::size_t) i] = -1.0f;
  11863. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  11864. blob.resize (12 + 4 * 156 + 64, 0);
  11865. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  11866. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  11867. return blob;
  11868. }
  11869. } // namespace
  11870. TEST (poizone_engine)
  11871. {
  11872. const auto blob = poizoneTestState();
  11873. CHECK (lynx::poizoneStateUsable (blob));
  11874. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  11875. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  11876. auto broken = blob;
  11877. broken[8] = 'X'; // the "Ver4" tag
  11878. CHECK (! lynx::poizoneStateUsable (broken));
  11879. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  11880. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  11881. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  11882. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  11883. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  11884. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  11885. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  11886. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  11887. CHECK (y.size() == 2 * 66150u);
  11888. 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 } };
  11889. for (const auto& g : golden)
  11890. 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);
  11891. double e = 0.0;
  11892. for (auto v : y)
  11893. e += (double) v * v;
  11894. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  11895. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  11896. lynx::ChannelDef c;
  11897. c.instrument = lynx::InstrumentKind::poizone;
  11898. c.pluginState = blob;
  11899. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  11900. inst->configure (c, 120.0);
  11901. inst->prepare (44100.0);
  11902. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11903. for (int b = 0; b < 16; ++b)
  11904. {
  11905. if (b == 0)
  11906. inst->noteOn (60, 0.78125f);
  11907. if (b == 10)
  11908. inst->noteOff (60);
  11909. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  11910. }
  11911. 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,
  11912. 512, 120.0, 0, true);
  11913. double diff = 0.0, energy = 0.0;
  11914. for (int i = 0; i < 8192; ++i)
  11915. {
  11916. 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]) });
  11917. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11918. }
  11919. CHECK (diff == 0.0 && energy > 1e-3);
  11920. }
  11921. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  11922. namespace
  11923. {
  11924. std::vector<std::uint8_t> fruitKickStateA()
  11925. {
  11926. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  11927. std::vector<std::uint8_t> b (32);
  11928. std::memcpy (b.data(), w, 32);
  11929. return b;
  11930. }
  11931. std::vector<std::uint8_t> fruitKickStateB()
  11932. {
  11933. std::vector<std::uint8_t> b (400, 0);
  11934. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  11935. std::memcpy (b.data(), head, sizeof (head));
  11936. std::int32_t recs[5][17];
  11937. for (auto& r : recs)
  11938. {
  11939. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  11940. std::memcpy (r, d, sizeof (d));
  11941. }
  11942. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  11943. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  11944. std::memcpy (recs[0], pan, sizeof (pan));
  11945. std::memcpy (recs[1], vol, sizeof (vol));
  11946. for (int k = 0; k < 5; ++k)
  11947. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  11948. const std::int32_t cut = 256;
  11949. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  11950. return b;
  11951. }
  11952. std::string bassDrumTestFolder()
  11953. {
  11954. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  11955. const auto dir = std::filesystem::temp_directory_path() / ("lynx_bassdrum_test_" + processTempTag());
  11956. std::filesystem::create_directories (dir / "Noise Samples");
  11957. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  11958. std::ofstream o (path, std::ios::binary);
  11959. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  11960. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  11961. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  11962. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  11963. o.write ((const char*) data, bytes);
  11964. };
  11965. std::vector<float> table (1024);
  11966. for (int i = 0; i < 1024; ++i)
  11967. 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));
  11968. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  11969. std::vector<std::int16_t> noise (3000);
  11970. std::uint32_t s = 12345;
  11971. for (auto& v : noise)
  11972. {
  11973. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  11974. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  11975. }
  11976. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  11977. return dir.string();
  11978. }
  11979. std::vector<std::uint8_t> bassDrumTestState()
  11980. {
  11981. std::vector<std::uint8_t> b (0x50C + 92, 0);
  11982. std::memcpy (b.data(), "SSLF", 4);
  11983. const std::uint32_t size = 0x560;
  11984. std::memcpy (b.data() + 4, &size, 4);
  11985. std::memcpy (b.data() + 8, "2kiK", 4);
  11986. std::memcpy (b.data() + 12, "Test kick", 9);
  11987. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  11988. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  11989. 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,
  11990. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  11991. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  11992. return b;
  11993. }
  11994. struct Golden
  11995. {
  11996. int i;
  11997. float l, r;
  11998. };
  11999. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  12000. {
  12001. bool ok = true;
  12002. for (const auto& g : golden)
  12003. 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;
  12004. double e = 0.0;
  12005. for (auto v : y)
  12006. e += (double) v * v;
  12007. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  12008. }
  12009. } // namespace
  12010. TEST (fruitkick_engine)
  12011. {
  12012. const auto A = fruitKickStateA(), B = fruitKickStateB();
  12013. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  12014. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  12015. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  12016. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  12017. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  12018. std::vector<std::vector<std::uint8_t>> recs;
  12019. for (int k = 0; k < 5; ++k)
  12020. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  12021. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  12022. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  12023. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  12024. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  12025. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  12026. 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 } };
  12027. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  12028. CHECK (ya.size() == 40000u);
  12029. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own;
  12030. // the release fade since REA_rodada74 with IPP's cosine, checked against the DLL)
  12031. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  12032. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124622017f, 0.0544089526f },
  12033. { 9400, -0.146487325f, -0.144867823f }, { 15000, 0.009439799f, 0.009439799f } },
  12034. 0.284267665614));
  12035. std::vector<int> ticks;
  12036. for (int t = 115; t < 20000; t += 230)
  12037. ticks.push_back (t);
  12038. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  12039. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  12040. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  12041. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  12042. 0.117964927585));
  12043. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  12044. lynx::ChannelDef c;
  12045. c.instrument = lynx::InstrumentKind::fruitkick;
  12046. c.pluginState = A;
  12047. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  12048. inst->configure (c, 140.0);
  12049. inst->prepare (44100.0);
  12050. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12051. for (int b = 0; b < 16; ++b)
  12052. {
  12053. if (b == 0)
  12054. inst->noteOn (60, 0.78125f);
  12055. if (b == 10)
  12056. inst->noteOff (60);
  12057. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12058. }
  12059. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  12060. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  12061. double diff = 0.0, energy = 0.0;
  12062. for (int i = 0; i < 8192; ++i)
  12063. {
  12064. 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]) });
  12065. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12066. }
  12067. CHECK (diff < 1e-6 && energy > 1e-2);
  12068. }
  12069. TEST (ipp_cos_and_end_fade)
  12070. {
  12071. // ippsCos_32f_A11 of the installation's dsp_ippv2_x64.dll (REA_rodada74 §1): outputs the DLL gave on its two paths
  12072. const auto bits = [] (float v) { return ippcos::floatToBits (v); };
  12073. const auto f = ippcos::bitsToFloat;
  12074. CHECK (bits (ippcos::cosFma (0.1f)) == 0x3f7eaf8bu && bits (ippcos::cosFma (1.0f)) == 0x3f0a534cu);
  12075. CHECK (bits (ippcos::cosFma (2.0f)) == 0xbed51118u && bits (ippcos::cosFma (-3.0f)) == 0xbf7d6646u);
  12076. CHECK (bits (ippcos::cosFma (f (0x3e000007))) == 0x3f7df6f9u && bits (ippcos::cosNoFma (f (0x3e000007))) == 0x3f7df6f8u);
  12077. CHECK (bits (ippcos::cosFma (f (0x3e00007c))) == 0x3f7df6f4u && bits (ippcos::cosNoFma (f (0x3e00007c))) == 0x3f7df6f5u);
  12078. CHECK (ippcos::cosFma (1e-9f) == 1.0f && ippcos::cosNoFma (0.0f) == 1.0f);
  12079. // the portable fused multiply-add rounds once: (1 + 2^-12)^2 = 1 + 2^-11 + 2^-24 is a float halfway case that a
  12080. // tiny addend (lost in a plain double sum) must decide
  12081. const float a = 1.0f + 0x1p-12f;
  12082. CHECK (ippcos::fusedMulAdd (a, a, 0x1p-60f) == 1.0f + 0x1p-11f + 0x1p-23f);
  12083. CHECK (ippcos::fusedMulAdd (a, a, -0x1p-60f) == 1.0f + 0x1p-11f && ippcos::fusedMulAdd (a, a, 0.0f) == 1.0f + 0x1p-11f);
  12084. CHECK (ippcos::fusedMulAdd (3.0f, 5.0f, -1.0f) == 14.0f);
  12085. // the end-fade table: within 1.5e-4 of the exact cosine, ends near zero
  12086. std::vector<float> t (441);
  12087. ippcos::endFadeTable (t.data(), 441);
  12088. double worst = 0.0;
  12089. for (int i = 0; i < 441; ++i)
  12090. worst = std::max (worst, std::abs ((double) t[(std::size_t) i] - 0.5 * (1.0 + std::cos ((i + 1) * 3.141592653589793 / 442.0))));
  12091. CHECK (worst < 1.5e-4 && t[0] > 0.9999f && t[440] < 1e-4f);
  12092. }
  12093. TEST (bassdrum_engine)
  12094. {
  12095. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  12096. const float x = 0.87011606f;
  12097. std::uint32_t bits = 0;
  12098. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  12099. std::memcpy (&bits, &p, 4);
  12100. CHECK (bits == 1065063964u);
  12101. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  12102. const auto st = bassDrumTestState();
  12103. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  12104. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  12105. auto broken = st;
  12106. broken[8] = 'X';
  12107. CHECK (! lynx::bassDrumStateUsable (broken));
  12108. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  12109. const auto dir = bassDrumTestFolder();
  12110. 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 },
  12111. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  12112. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  12113. CHECK (y.size() == 60000u);
  12114. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  12115. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  12116. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  12117. 0.230007588169));
  12118. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  12119. lynx::ChannelDef c;
  12120. c.instrument = lynx::InstrumentKind::bassdrum;
  12121. c.pluginState = st;
  12122. c.samplePath = dir;
  12123. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  12124. inst->configure (c, 120.0);
  12125. inst->prepare (44100.0);
  12126. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12127. for (int b = 0; b < 16; ++b)
  12128. {
  12129. if (b == 0)
  12130. inst->noteOn (60, 0.78125f);
  12131. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  12132. }
  12133. 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);
  12134. double diff = 0.0, energy = 0.0;
  12135. for (int i = 0; i < 8192; ++i)
  12136. {
  12137. 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]) });
  12138. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12139. }
  12140. CHECK (diff == 0.0 && energy > 1e-2);
  12141. }
  12142. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  12143. TEST (fruity_fast_dist_flangus_flanger)
  12144. {
  12145. std::vector<float> inL (4096), inR (4096);
  12146. for (int n = 0; n < 4096; ++n)
  12147. {
  12148. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  12149. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  12150. }
  12151. struct Gold { int i; float l, r; };
  12152. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  12153. // Fast Dist (+ FL's host waveshaper): live and render kernels
  12154. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  12155. std::vector<float> l (inL), r (inR);
  12156. lynx::fastdistfx::process (l.data(), 4096, s, render);
  12157. lynx::fastdistfx::process (r.data(), 4096, s, render);
  12158. for (const auto& g : gold)
  12159. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  12160. };
  12161. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  12162. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  12163. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  12164. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  12165. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  12166. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  12167. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  12168. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  12169. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  12170. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  12171. {
  12172. const auto* T = lynx::fastdistfx::curve (0, 10);
  12173. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  12174. }
  12175. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  12176. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  12177. lynx::flangusfx::Engine e;
  12178. e.prepare (fs);
  12179. e.load (s);
  12180. e.setRate (fs);
  12181. e.setBlockSize (512);
  12182. std::vector<float> io (8192);
  12183. for (int n = 0; n < 4096; ++n)
  12184. {
  12185. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  12186. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12187. }
  12188. for (int o = 0; o < 4096; o += 512)
  12189. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  12190. for (const auto& g : gold)
  12191. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12192. };
  12193. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  12194. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  12195. { 4095, 0.201686561f, 0.607052445f } });
  12196. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  12197. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  12198. { 4095, -0.354708612f, 0.503573656f } });
  12199. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  12200. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  12201. { 4095, 1.49345946f, -0.537866116f } });
  12202. // Flanger: state, then the sample rate (FL's call order)
  12203. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  12204. lynx::flangerfx::Engine e;
  12205. e.load (s);
  12206. e.prepare (fs);
  12207. std::vector<float> io (8192);
  12208. for (int n = 0; n < 4096; ++n)
  12209. {
  12210. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  12211. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12212. }
  12213. for (int o = 0; o < 4096; o += 512)
  12214. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  12215. for (const auto& g : gold)
  12216. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12217. };
  12218. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  12219. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  12220. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  12221. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  12222. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  12223. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  12224. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  12225. {
  12226. 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 };
  12227. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  12228. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  12229. if (slot)
  12230. {
  12231. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  12232. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  12233. auto fx = lynx::makeEffect (*slot);
  12234. fx->prepare (44100.0);
  12235. std::vector<float> l (inL), r (inR);
  12236. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  12237. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  12238. }
  12239. std::vector<std::uint8_t> fg (32);
  12240. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  12241. std::memcpy (fg.data(), v, 32);
  12242. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  12243. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  12244. if (f2)
  12245. {
  12246. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  12247. for (int i = 0; i < 8; ++i)
  12248. CHECK (s[(std::size_t) i] == v[i]);
  12249. auto fx = lynx::makeEffect (*f2);
  12250. fx->prepare (48000.0);
  12251. std::vector<float> l (inL), r (inR);
  12252. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  12253. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  12254. }
  12255. std::vector<std::uint8_t> fl (52);
  12256. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  12257. std::memcpy (fl.data(), w, 52);
  12258. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  12259. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  12260. if (f3)
  12261. {
  12262. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  12263. for (int i = 0; i < 12; ++i)
  12264. CHECK (s[(std::size_t) i] == w[i + 1]);
  12265. auto fx = lynx::makeEffect (*f3);
  12266. fx->prepare (48000.0);
  12267. std::vector<float> l (inL), r (inR);
  12268. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  12269. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  12270. }
  12271. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  12272. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  12273. }
  12274. }
  12275. // Workflow P2 (docs/WORKFLOW_FL.md)
  12276. #include "WorkflowP2Tests.inc"
  12277. // Editor mouse grammar (Piano Roll / Playlist)
  12278. #include "EditorGestureTests.inc"
  12279. TEST (chorus_family_engines)
  12280. {
  12281. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  12282. std::vector<float> inL (4096), inR (4096);
  12283. for (int n = 0; n < 4096; ++n)
  12284. {
  12285. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  12286. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  12287. }
  12288. struct Gold { int i; float l, r; };
  12289. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  12290. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  12291. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  12292. CHECK (slot && slot->kind == kind);
  12293. if (! slot)
  12294. return;
  12295. auto fx = lynx::makeEffect (*slot);
  12296. fx->prepare (44100.0);
  12297. std::vector<float> l (inL), r (inR);
  12298. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  12299. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  12300. for (const auto& g : gold)
  12301. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  12302. };
  12303. auto ints = [] (std::initializer_list<int> v) {
  12304. std::vector<std::uint8_t> b;
  12305. for (int x : v)
  12306. for (int k = 0; k < 4; ++k)
  12307. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  12308. return b;
  12309. };
  12310. // Fruity Chorus (start phases 0, which FL draws at random)
  12311. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  12312. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  12313. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  12314. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  12315. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  12316. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  12317. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  12318. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  12319. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  12320. {
  12321. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  12322. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  12323. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  12324. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  12325. }
  12326. // a state FL does not read (version 2) or too short keeps the generic chorus
  12327. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  12328. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  12329. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  12330. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  12331. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  12332. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  12333. namespace hc = lynx::hyperchorusfx;
  12334. hc::Engine e (instance);
  12335. const std::vector<int> r (raws);
  12336. 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); };
  12337. for (int i = 0; i < hc::params; ++i)
  12338. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  12339. for (int i = 0; i < hc::params; ++i)
  12340. e.setParam (i, val (i, r[(std::size_t) i]));
  12341. e.setRate (44100.0);
  12342. std::vector<float> io (8192);
  12343. for (int n = 0; n < 4096; ++n)
  12344. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12345. for (int s = 0; s < 4096; s += 512)
  12346. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  12347. for (const auto& g : gold)
  12348. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12349. };
  12350. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  12351. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  12352. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  12353. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  12354. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  12355. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  12356. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  12357. {
  12358. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  12359. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  12360. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  12361. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  12362. lynx::Song song;
  12363. song.mixer.resize (3);
  12364. song.mixer[1].effects = { *slot, *slot };
  12365. song.mixer[2].effects = { *slot };
  12366. lynx::numberChorusInstances (song);
  12367. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  12368. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  12369. fx->prepare (48000.0);
  12370. std::vector<float> l (inL), r (inR);
  12371. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  12372. bool finite = true, wet = false;
  12373. for (int n = 0; n < 4096; ++n)
  12374. {
  12375. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  12376. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  12377. }
  12378. CHECK (finite && wet);
  12379. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  12380. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  12381. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  12382. }
  12383. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  12384. // start from the engine's initial ones)
  12385. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  12386. namespace vc = lynx::vintagechorusfx;
  12387. vc::Engine e;
  12388. e.setRate (44100.0);
  12389. int i = 0;
  12390. for (int raw : raws)
  12391. {
  12392. 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));
  12393. ++i;
  12394. }
  12395. std::vector<float> io (8192);
  12396. for (int n = 0; n < 4096; ++n)
  12397. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  12398. for (int s = 0; s < 4096; s += 512)
  12399. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  12400. for (const auto& g : gold)
  12401. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  12402. };
  12403. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  12404. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  12405. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  12406. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  12407. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  12408. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  12409. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  12410. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  12411. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  12412. {
  12413. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  12414. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  12415. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  12416. auto fx = lynx::makeEffect (*slot);
  12417. fx->prepare (44100.0);
  12418. std::vector<float> l (inL), r (inR);
  12419. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  12420. bool finite = true;
  12421. for (int n = 0; n < 4096; ++n)
  12422. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  12423. CHECK (finite && l[3000] != inL[3000]);
  12424. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  12425. 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()
  12426. || 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);
  12427. }
  12428. }
  12429. #include "UiChromeTests.inc"
  12430. TEST (fruity_delay_engines)
  12431. {
  12432. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  12433. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  12434. const int n = 16384;
  12435. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  12436. std::uint32_t seed = 1;
  12437. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  12438. {
  12439. seed = seed * 1664525u + 1013904223u;
  12440. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  12441. seed = seed * 1664525u + 1013904223u;
  12442. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  12443. }
  12444. using Golden = std::vector<std::tuple<int, float, float>>;
  12445. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  12446. for (const auto& [i, gl, gr] : golden)
  12447. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  12448. };
  12449. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  12450. lynx::fldelay::Delay3Engine e (44100.0);
  12451. e.setTempo (bpm);
  12452. for (int i = 0; i < 26; ++i)
  12453. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  12454. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  12455. e.setSampleRate (44100.0);
  12456. std::vector<float> l (inL), r (inR);
  12457. for (int s = 0; s < n; s += 512)
  12458. e.process (l.data() + s, r.data() + s, 512);
  12459. return std::make_pair (l, r);
  12460. };
  12461. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  12462. lynx::fldelay::Delay2State st;
  12463. st.v = raw;
  12464. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  12465. std::vector<float> l (inL), r (inR);
  12466. for (int s = 0; s < n; s += 512)
  12467. e.process (l.data() + s, r.data() + s, 512);
  12468. return std::make_pair (l, r);
  12469. };
  12470. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  12471. 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 };
  12472. auto o = run3 (a, 120.0);
  12473. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  12474. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  12475. o = run3 (a, 100.0);
  12476. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  12477. { 16000, 0.0806550607f, -0.0991330743f } });
  12478. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  12479. 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);
  12480. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  12481. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  12482. // fake stereo, unsynced, high-pass, limit distortion, no dry
  12483. 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);
  12484. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  12485. { 16000, -0.000312746735f, 0.00101647642f } });
  12486. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  12487. 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);
  12488. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  12489. { 11000, 0.0f, 0.0f } });
  12490. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  12491. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  12492. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  12493. { 16000, 0.0182533264f, 0.105384827f } });
  12494. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  12495. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  12496. { 16000, -0.029524127f, 0.0128910914f } });
  12497. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  12498. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  12499. { 13000, 0.145023212f, -0.104788139f } });
  12500. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  12501. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  12502. 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 };
  12503. state3[0] = 1;
  12504. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  12505. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  12506. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  12507. auto state2 = std::vector<std::uint8_t> (32, 0);
  12508. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  12509. std::memcpy (state2.data(), def2.data(), 32);
  12510. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  12511. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  12512. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  12513. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  12514. auto fx = lynx::makeEffect (slot);
  12515. fx->prepare (44100.0);
  12516. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  12517. l[100] = 1.0f;
  12518. for (int s = 0; s < 48000; s += 1024)
  12519. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  12520. int best = 200;
  12521. for (int i = 200; i < 48000; ++i)
  12522. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  12523. best = i;
  12524. return best - 100;
  12525. };
  12526. if (slot3 && slot2)
  12527. {
  12528. CHECK (echoAt (*slot3, 120.0) == 22050);
  12529. CHECK (echoAt (*slot3, 150.0) == 17640);
  12530. CHECK (echoAt (*slot2, 120.0) == 16538);
  12531. CHECK (echoAt (*slot2, 140.0) == 14175);
  12532. }
  12533. }
  12534. #include "UiRackMixerTests.inc"
  12535. // ---------------------------------------------------------------------------
  12536. // FL Studio integration (core/FlPluginDb.h): synthetic plugin-database fixtures, no Image-Line files
  12537. namespace
  12538. {
  12539. std::vector<std::uint8_t> testWrapperBlob (std::uint32_t type, const std::string& name, const std::string& path,
  12540. const std::vector<std::uint8_t>& classId = {}, const std::string& clapId = {})
  12541. {
  12542. std::vector<std::uint8_t> b;
  12543. auto u32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  12544. auto record = [&] (std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  12545. u32 (id);
  12546. u32 ((std::uint32_t) payload.size());
  12547. u32 (0);
  12548. b.insert (b.end(), payload.begin(), payload.end());
  12549. };
  12550. u32 (12);
  12551. record (0x32, { (std::uint8_t) type, 0, 0, 0 });
  12552. if (! classId.empty())
  12553. record (0x34, classId);
  12554. if (! clapId.empty())
  12555. record (0x3A, std::vector<std::uint8_t> (clapId.begin(), clapId.end()));
  12556. record (0x36, std::vector<std::uint8_t> (name.begin(), name.end()));
  12557. record (0x37, std::vector<std::uint8_t> (path.begin(), path.end()));
  12558. return b;
  12559. }
  12560. } // namespace
  12561. TEST (fl_plugin_db_nfo_parsing)
  12562. {
  12563. const std::string nfo = "ps_name=Vital\r\n"
  12564. "ps_file_name_0=C:\\Program Files\\Common Files\\VST3\\Vital.vst3\r\n"
  12565. "ps_file_vendorname_0=Vital Audio\r\n"
  12566. "ps_file_type_0=2\r\n"
  12567. "ps_file_name_1=C:\\VSTPlugins\\Vital_x64.dll\r\n"
  12568. "ps_file_type_1=2\r\n"
  12569. "ps_file_magic_1=Vita\r\n"
  12570. "ps_file_name_2=not a plugin\r\n"
  12571. "garbage line without equals\r\n";
  12572. const auto entries = lynx::parseFlPluginNfo (nfo, "D:\\FL\\Presets\\Plugin database\\Installed\\Generators\\VST3\\Vital.nfo");
  12573. CHECK (entries.size() == 2);
  12574. if (entries.size() == 2)
  12575. {
  12576. CHECK (entries[0].name == "Vital" && entries[0].vendor == "Vital Audio" && entries[0].role == 2);
  12577. CHECK (entries[0].format == lynx::ForeignPlugin::Format::vst3);
  12578. CHECK (entries[1].format == lynx::ForeignPlugin::Format::vst2 && entries[1].vst2Id == 0x56697461u);
  12579. }
  12580. // no type field: the role comes from the Effects / Generators folder; an ID in GUID form is read
  12581. const auto fx = lynx::parseFlPluginNfo ("ps_name=Fancy Comp\nps_file_filename_0=C:/CLAP/Fancy Comp.clap\n"
  12582. "ps_file_guid_0={01234567-89AB-CDEF-0123-456789ABCDEF}\n",
  12583. "C:\\x\\Installed\\Effects\\CLAP\\Fancy Comp.nfo");
  12584. CHECK (fx.size() == 1 && fx[0].role == 1 && fx[0].format == lynx::ForeignPlugin::Format::clap && fx[0].hasClassId
  12585. && fx[0].classId[0] == 0x01 && fx[0].classId[15] == 0xEF);
  12586. CHECK (lynx::parseFlPluginNfo ("Tip=All of the plugins found in your system.").empty());
  12587. CHECK (lynx::pluginNameKey ("Serum_x64") == "serum");
  12588. CHECK (lynx::pluginNameKey ("Vital.VST3") == "vital");
  12589. CHECK (lynx::pluginNameKey ("Pro-Q 3") == "proq3");
  12590. CHECK (lynx::pluginFormatFromPath ("a\\b.CLAP") == lynx::ForeignPlugin::Format::clap);
  12591. }
  12592. TEST (fl_plugin_db_fst_parsing_and_matching)
  12593. {
  12594. std::vector<std::uint8_t> cid;
  12595. for (int i = 0; i < 16; ++i)
  12596. cid.push_back ((std::uint8_t) (0x10 + i));
  12597. // an .fst of the database: an FL preset whose 0xD5 holds the wrapper record
  12598. FlpBuilder fst;
  12599. fst.ascii (0xC9, "Fruity Wrapper");
  12600. fst.data (0xD5, testWrapperBlob (8, "Surge XT", "E:\\Plugins\\VST3\\Surge XT.vst3", cid));
  12601. lynx::FlPluginDbEntry e;
  12602. CHECK (lynx::parseFlPluginFst (fst.bytes, e));
  12603. CHECK (e.name == "Surge XT" && e.format == lynx::ForeignPlugin::Format::vst3 && e.hasClassId && e.classId[0] == 0x10);
  12604. lynx::FlPluginDbEntry bad;
  12605. CHECK (! lynx::parseFlPluginFst (std::vector<std::uint8_t> { 'F', 'L', 'h', 'd' }, bad));
  12606. lynx::FlPluginDb db;
  12607. db.add (e);
  12608. // the .nfo of the same plugin fills in vendor and role
  12609. for (const auto& n : lynx::parseFlPluginNfo ("ps_name=Surge XT\nps_file_name_0=E:/Plugins/VST3/Surge XT.vst3\n"
  12610. "ps_file_vendorname_0=Surge Synth Team\nps_file_type_0=2\n"))
  12611. db.add (n);
  12612. db.add ({ "Clapper", "", "E:\\Plugins\\CLAP\\Clapper.clap", "", lynx::ForeignPlugin::Format::clap, 1, {}, false, "org.clapper", 0 });
  12613. db.add ({ "Big Verb", "", "E:\\Old VST\\BigVerb_x64.dll", "", lynx::ForeignPlugin::Format::vst2, 1, {}, false, "", 0x42566262u });
  12614. db.add ({ "Bundle", "", "F:\\VST3\\Bundle.vst3\\Contents\\x86_64-win\\Bundle.vst3", "", lynx::ForeignPlugin::Format::vst3, 2, {}, false, "", 0 });
  12615. CHECK (db.entries().size() == 4);
  12616. CHECK (db.entries()[0].vendor == "Surge Synth Team" && db.entries()[0].role == 2);
  12617. // by VST3 class ID even when the project saved another path and name
  12618. lynx::ForeignPlugin p;
  12619. p.format = lynx::ForeignPlugin::Format::vst3;
  12620. p.hasClassId = true;
  12621. std::copy (cid.begin(), cid.end(), p.classId.begin());
  12622. p.name = "Renamed";
  12623. p.path = "C:\\Other PC\\Surge.vst3";
  12624. CHECK (db.match (p, true) == &db.entries()[0]);
  12625. // by CLAP id, by VST2 id
  12626. lynx::ForeignPlugin c;
  12627. c.format = lynx::ForeignPlugin::Format::clap;
  12628. c.clapId = "org.clapper";
  12629. CHECK (db.match (c, false) == &db.entries()[1]);
  12630. lynx::ForeignPlugin v;
  12631. v.format = lynx::ForeignPlugin::Format::vst2;
  12632. v.vst2Id = 0x42566262u;
  12633. CHECK (db.match (v, false) == &db.entries()[2]);
  12634. // by name (architecture suffix ignored), only in the same role
  12635. lynx::ForeignPlugin byName;
  12636. byName.format = lynx::ForeignPlugin::Format::vst2;
  12637. byName.name = "BigVerb x64";
  12638. CHECK (db.match (byName, false) == &db.entries()[2]);
  12639. CHECK (db.match (byName, true) == nullptr);
  12640. lynx::ForeignPlugin unknown;
  12641. unknown.format = lynx::ForeignPlugin::Format::vst3;
  12642. unknown.name = "Nothing Like It";
  12643. CHECK (db.match (unknown, true) == nullptr);
  12644. // folders: the bundle's parent, deduplicated
  12645. const auto folders = db.pluginFolders();
  12646. CHECK (folders.size() == 4);
  12647. CHECK (std::find (folders.begin(), folders.end(), "F:\\VST3") != folders.end());
  12648. CHECK (std::find (folders.begin(), folders.end(), "E:\\Plugins\\VST3") != folders.end());
  12649. // targeted scan picks: same file, or same name key
  12650. CHECK (lynx::pluginFileMatches ("E:/Plugins/VST3/Surge XT.vst3", p, &db.entries()[0]));
  12651. CHECK (lynx::pluginFileMatches ("C:\\Program Files\\Common Files\\VST3\\BigVerb.vst3", byName, nullptr));
  12652. CHECK (! lynx::pluginFileMatches ("C:\\VST3\\Other.vst3", byName, nullptr));
  12653. // the plugins an .flp uses: wrapper channels and mixer slots
  12654. flp::Project project;
  12655. flp::Channel ch;
  12656. ch.pluginName = "Fruity Wrapper";
  12657. ch.pluginState = testWrapperBlob (8, "Surge XT", "C:\\VST3\\Surge XT.vst3", cid);
  12658. project.channels.push_back (ch);
  12659. ch.pluginName = "3x Osc";
  12660. project.channels.push_back (ch);
  12661. flp::MixerInsert insert;
  12662. insert.slots.push_back ({ 0, "Fruity Wrapper", testWrapperBlob (12, "Clapper", "C:\\CLAP\\Clapper.clap", {}, "org.clapper") });
  12663. insert.slots.push_back ({ 1, "Fruity Wrapper", testWrapperBlob (3, "DX thing", "C:\\dx.dll") }); // DirectX: not hosted
  12664. project.mixer.push_back (insert);
  12665. const auto uses = lynx::flpForeignPlugins (project);
  12666. CHECK (uses.size() == 2);
  12667. CHECK (uses.size() == 2 && uses[0].instrument && ! uses[1].instrument && uses[1].plugin.clapId == "org.clapper");
  12668. }
  12669. TEST (fl_sample_relocation)
  12670. {
  12671. std::set<std::string> files { "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav",
  12672. "C:\\Users\\me\\Music\\loop.wav", "E:\\Songs\\Track\\Samples\\snare.wav", "E:\\Songs\\Track\\hat.wav" };
  12673. auto exists = [&] (const std::string& p) { return files.count (p) > 0; };
  12674. lynx::FlPathRoots roots;
  12675. roots.userData = "D:\\Docs\\Image-Line\\FL Studio";
  12676. roots.factoryData = "C:\\Program Files\\FL 2026";
  12677. roots.home = "C:\\Users\\me";
  12678. const std::string project = "E:\\Songs\\Track";
  12679. // variable resolved, file in place
  12680. CHECK (lynx::locateFlSample ("%FLStudioFactoryData%\\Data\\Patches\\kick.wav", roots, project, exists) == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  12681. // another PC's FL user data / FL install / user profile
  12682. CHECK (lynx::locateFlSample ("C:\\Users\\bob\\Documents\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav", roots, project, exists)
  12683. == "D:\\Docs\\Image-Line\\FL Studio\\Audio\\Recorded\\take.wav");
  12684. CHECK (lynx::locateFlSample ("C:\\Program Files (x86)\\Image-Line\\FL Studio 20\\Data\\Patches\\kick.wav", roots, project, exists)
  12685. == "C:\\Program Files\\FL 2026\\Data\\Patches\\kick.wav");
  12686. CHECK (lynx::locateFlSample ("C:/Users/bob/Music/loop.wav", roots, project, exists) == "C:\\Users\\me\\Music\\loop.wav");
  12687. // next to the project, also in a sub-folder named in the original path
  12688. CHECK (lynx::locateFlSample ("Z:\\Old\\Samples\\snare.wav", roots, project, exists) == "E:\\Songs\\Track\\Samples\\snare.wav");
  12689. CHECK (lynx::locateFlSample ("Z:\\Old\\Stuff\\hat.wav", roots, project, exists) == "E:\\Songs\\Track\\hat.wav");
  12690. CHECK (lynx::locateFlSample ("Z:\\nowhere.wav", roots, project, exists).empty());
  12691. CHECK (lynx::locateFlSample ("", roots, project, exists).empty());
  12692. }
  12693. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  12694. namespace
  12695. {
  12696. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  12697. // ints, version)
  12698. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  12699. {
  12700. std::vector<std::uint8_t> b (564, 0);
  12701. std::memcpy (b.data(), P.data(), 360);
  12702. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  12703. std::memcpy (b.data() + 560, &version, 4);
  12704. return b;
  12705. }
  12706. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  12707. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  12708. {
  12709. std::vector<std::vector<std::uint8_t>> out;
  12710. std::ifstream in (file, std::ios::binary);
  12711. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  12712. std::size_t i = 0;
  12713. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  12714. ++i;
  12715. i += 8;
  12716. std::string name;
  12717. while (i < b.size())
  12718. {
  12719. const auto id = b[i++];
  12720. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  12721. if (id >= 192)
  12722. for (int shift = 0; i < b.size(); shift += 7)
  12723. {
  12724. const auto c = b[i++];
  12725. n |= (std::size_t) (c & 0x7F) << shift;
  12726. if (c < 0x80)
  12727. break;
  12728. }
  12729. if (i + n > b.size())
  12730. break;
  12731. if (id == 0xC9)
  12732. {
  12733. name.clear();
  12734. for (std::size_t k = 0; k < n; ++k)
  12735. if (b[i + k] != 0)
  12736. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  12737. }
  12738. if (id == 0xD5 && name == plugin)
  12739. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  12740. i += n;
  12741. }
  12742. return out;
  12743. }
  12744. } // namespace
  12745. TEST (simsynth_engine)
  12746. {
  12747. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  12748. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  12749. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  12750. 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,
  12751. 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 };
  12752. 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 };
  12753. 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,
  12754. 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 };
  12755. 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 };
  12756. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  12757. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  12758. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  12759. const auto hp = lynx::simSynthHostParams (blobA);
  12760. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  12761. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  12762. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  12763. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  12764. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  12765. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  12766. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  12767. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  12768. struct Golden { int i; float l, r; };
  12769. 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 } };
  12770. 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 } };
  12771. for (int which = 0; which < 2; ++which)
  12772. {
  12773. std::vector<std::int64_t> counters;
  12774. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  12775. CHECK (y.size() == 2 * 66150u);
  12776. if (y.size() != 2 * 66150u)
  12777. continue;
  12778. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  12779. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  12780. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  12781. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  12782. double e = 0.0;
  12783. for (auto v : y)
  12784. e += (double) v * v;
  12785. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  12786. }
  12787. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  12788. lynx::ChannelDef c;
  12789. c.instrument = lynx::InstrumentKind::simsynth;
  12790. c.pluginState = blobA;
  12791. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  12792. inst->prepare (44100.0);
  12793. inst->configure (c, 120.0);
  12794. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  12795. for (int k = 0; k < 16; ++k)
  12796. {
  12797. if (k == 0)
  12798. inst->noteOn (60, 0.8f);
  12799. if (k == 10)
  12800. inst->noteOff (60);
  12801. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  12802. }
  12803. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  12804. double diff = 0.0, energy = 0.0;
  12805. for (int i = 0; i < 8192; ++i)
  12806. {
  12807. 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]) });
  12808. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  12809. }
  12810. CHECK (diff == 0.0 && energy > 1e-3);
  12811. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  12812. if (! realFlpDir.empty())
  12813. {
  12814. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  12815. int presets = 0, usable = 0, sounding = 0;
  12816. if (std::filesystem::exists (folder))
  12817. for (const auto& entry : std::filesystem::directory_iterator (folder))
  12818. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  12819. {
  12820. ++presets;
  12821. if (! lynx::simSynthStateUsable (st))
  12822. continue;
  12823. ++usable;
  12824. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  12825. double e = 0.0;
  12826. bool finite = true;
  12827. for (auto v : y)
  12828. finite = finite && std::isfinite (v), e += (double) v * v;
  12829. CHECK (finite);
  12830. sounding += e > 1e-9 ? 1 : 0;
  12831. }
  12832. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  12833. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  12834. }
  12835. }
  12836. TEST (wasp_is_a_silent_placeholder)
  12837. {
  12838. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  12839. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  12840. inst->prepare (44100.0);
  12841. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  12842. inst->noteOn (60, 1.0f);
  12843. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  12844. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12845. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  12846. // their state kept
  12847. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  12848. const int version = 14;
  12849. std::memcpy (ss.data() + 560, &version, 4);
  12850. FlpBuilder b;
  12851. b.ascii (0xC7, "25.2.5.5319");
  12852. int index = 0;
  12853. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  12854. {
  12855. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  12856. b.u8 (0x15); b.u8 (2);
  12857. b.utf16 (0xC9, name);
  12858. b.data (0xD5, state);
  12859. }
  12860. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  12861. CHECK (song.channels.size() == 3u);
  12862. if (song.channels.size() == 3u)
  12863. {
  12864. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  12865. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  12866. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  12867. }
  12868. }
  12869. // --- REA round 67: Fruity Granulizer (exact engine) --------------------------------------------------------------
  12870. namespace
  12871. {
  12872. // our own deterministic test sample (a fading 220 Hz sine plus LCG noise per channel), as r67/py/gold67.py builds it
  12873. lynx::SampleData granulizerTestSample (int n, int ch)
  12874. {
  12875. lynx::SampleData d;
  12876. std::uint32_t s = 12345;
  12877. for (int i = 0; i < n; ++i)
  12878. {
  12879. const auto base = 0.5 * std::sin (2.0 * 3.141592653589793 * 220.0 * i / 44100.0) * (1.0 - 0.7 * i / n);
  12880. for (int c = 0; c < ch; ++c)
  12881. {
  12882. s = s * 1664525u + 1013904223u;
  12883. const auto v = (float) (base + 0.2 * ((double) (std::int32_t) s * (1.0 / 2147483648.0)));
  12884. (c == 0 ? d.left : d.right).push_back (v);
  12885. }
  12886. }
  12887. return d;
  12888. }
  12889. std::vector<std::uint8_t> granulizerTestState (const std::array<int, 12>& P, int keyMode = 0)
  12890. {
  12891. std::vector<std::uint8_t> b (60, 0);
  12892. const std::int32_t v = 5;
  12893. std::memcpy (b.data(), &v, 4);
  12894. std::memcpy (b.data() + 4, P.data(), 48);
  12895. std::memcpy (b.data() + 56, &keyMode, 4);
  12896. return b;
  12897. }
  12898. } // namespace
  12899. TEST (granulizer_engine)
  12900. {
  12901. // REA_rodada67: states written from scratch against our reference model (granmodel.py, which renders like the
  12902. // installation's DLL within −128 dB: the DLL's rcpps and IPP sines are not replicated)
  12903. const auto v5 = granulizerTestState ({ 204, 50, 611, 611, 0, 180, 0, 0, 0, 0, 0, 228 });
  12904. CHECK (lynx::granulizerStateUsable (v5));
  12905. const auto n5 = lynx::granulizerNormalisedState (v5, {});
  12906. CHECK (n5.size() == 428 && n5[0] == 7 && lynx::granulizerParam (n5, 0) == 204 && lynx::granulizerParam (n5, 11) == 228
  12907. && ! lynx::granulizerParam (n5, 12));
  12908. // version 1 (10 parameters): parameter 11 takes the plugin's 0xf8
  12909. {
  12910. std::vector<std::uint8_t> v1 (v5.begin(), v5.begin() + 44);
  12911. v1[0] = 1;
  12912. const auto n1 = lynx::granulizerNormalisedState (v1, {});
  12913. CHECK (lynx::granulizerParam (n1, 9) == 0 && lynx::granulizerParam (n1, 11) == 0xf8 && lynx::granulizerParam (n1, 3) == 611);
  12914. std::vector<std::uint8_t> bad (v5);
  12915. bad[0] = 9;
  12916. CHECK (! lynx::granulizerStateUsable (bad) && lynx::granulizerNormalisedState (bad, {}).empty());
  12917. auto edited = n5;
  12918. CHECK (lynx::granulizerSetParam (edited, 3, -5000) && lynx::granulizerParam (edited, 3) == -1000);
  12919. }
  12920. using N = lynx::granulizer::RefNote;
  12921. struct Golden { std::vector<std::uint8_t> state; int ch, fs, rate; lynx::granulizer::RefOptions opt; std::vector<N> notes;
  12922. double el, er; std::tuple<int, float, float> at[6]; };
  12923. auto check = [] (const Golden& g) {
  12924. auto smp = granulizerTestSample (20000, g.ch);
  12925. smp.sampleRate = g.rate;
  12926. const auto y = lynx::granulizer::renderReference (lynx::granulizerNormalisedState (g.state, {}), smp, g.notes, (double) g.fs, 16384, g.opt);
  12927. CHECK (y.size() == 2 * 16384);
  12928. if (y.size() != 2 * 16384)
  12929. return;
  12930. double el = 0.0, er = 0.0;
  12931. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  12932. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  12933. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  12934. for (const auto& [i, l, r] : g.at)
  12935. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  12936. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  12937. };
  12938. lynx::granulizer::RefOptions o1, o2, o3;
  12939. o2.block = 333, o2.seed = 777;
  12940. o3.block = 64, o3.seed = 5;
  12941. // the plugin's defaults, two notes (one pitched by +23 cents and released: the processor's end fade)
  12942. 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,
  12943. { { 100, 0.057010360062122345f, 0.08960580080747604f }, { 1000, 0.07210570573806763f, -0.048257164657115936f },
  12944. { 2500, 0.14769265055656433f, -0.0194698516279459f }, { 4000, 0.11169229447841644f, 0.17487481236457825f },
  12945. { 7777, -0.4309714436531067f, -0.17027156054973602f }, { 12000, -0.11129666864871979f, -0.19622492790222168f } } });
  12946. // backwards, randomness, depth/speed wobble, stereo separation, loop, sample start; a mono 44.1 kHz sample at 48 kHz
  12947. check ({ granulizerTestState ({ 150, 120, 300, -480, 200, 90, 120, 40, 1, 0, 900, 300 }), 1, 48000, 44100, o2,
  12948. { { 0, 55, 1.0, 0.2, -37.0, 12000 }, { 5000, 67, 0.5, 0.0, 0.0, -1 } }, 959.5011214226065, 1294.4823684530272,
  12949. { { 100, 0.02567935362458229f, 0.010721667669713497f }, { 1000, 0.3007732629776001f, 0.08214527368545532f },
  12950. { 2500, 0.435413658618927f, 0.09068503230810165f }, { 4000, -0.24611327052116394f, -0.20149046182632446f },
  12951. { 7777, -0.008478119969367981f, 0.31221628189086914f }, { 12000, 0.203709632f, 0.324944794f } } });
  12952. // key mode 1 (the sample spread over two octaves), freeze, hard stereo separation; 48 kHz sample at 96 kHz, 64-sample blocks
  12953. check ({ granulizerTestState ({ 300, 0, 800, 700, 0, 180, 0, 64, 0, 1, 2000, 100 }, 1), 2, 96000, 48000, o3,
  12954. { { 0, 62, 0.9, 0.0, 0.0, -1 }, { 4000, 50, 0.7, 0.0, 11.0, -1 } }, 366.79556669810813, 0.00825590391720353,
  12955. { { 100, 0.004204070195555687f, 7.680895437786243e-20f }, { 1000, 0.008516596630215645f, 3.0263170437258543e-19f },
  12956. { 2500, 0.06948182731866837f, 4.5391312775082004e-18f }, { 4000, -0.01754589192569256f, -4.895453905423713e-19f },
  12957. { 7777, 0.10338862985372543f, 2.344363859234514e-18f }, { 12000, -0.09385982155799866f, -1.3493021437162966e-17f } } });
  12958. // the instrument: silent without a sample (FL's plugin refuses the note), sounding with one
  12959. {
  12960. auto silent = lynx::makeInstrument (lynx::InstrumentKind::granulizer);
  12961. silent->prepare (44100.0);
  12962. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  12963. silent->noteOn (60, 1.0f);
  12964. silent->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  12965. CHECK (silent->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  12966. auto smp = std::make_shared<lynx::SampleData> (granulizerTestSample (20000, 2));
  12967. auto inst = lynx::makeInstrument (lynx::InstrumentKind::granulizer, smp);
  12968. lynx::ChannelDef def;
  12969. def.instrument = lynx::InstrumentKind::granulizer;
  12970. def.pluginState = n5;
  12971. inst->configure (def, 140.0);
  12972. inst->prepare (44100.0);
  12973. inst->noteOn (60, 0.8f);
  12974. inst->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  12975. double e = 0.0;
  12976. for (auto v : l)
  12977. e += (double) v * v;
  12978. CHECK (! inst->idle() && e > 1.0);
  12979. inst->noteOff (60);
  12980. for (int k = 0; k < 4; ++k)
  12981. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  12982. CHECK (inst->idle()); // the 10 ms end fade of the plugin's voice processor
  12983. }
  12984. // import: a Fruity Granulizer channel plays on the exact engine with its sample
  12985. {
  12986. FlpBuilder b;
  12987. b.ascii (0xC7, "25.2.5.5319");
  12988. b.u8 (0x40); b.u16 (0);
  12989. b.u8 (0x15); b.u8 (2);
  12990. b.utf16 (0xC9, u"Fruity Granulizer");
  12991. b.data (0xD5, v5);
  12992. b.utf16 (0xC4, u"%FLStudioFactoryData%\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  12993. lynx::FlpImportOptions options;
  12994. options.roots.factoryData = "C:\\FL";
  12995. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav"; };
  12996. lynx::FlpImportReport report;
  12997. options.report = &report;
  12998. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  12999. CHECK (song.channels.size() == 1u);
  13000. if (song.channels.size() == 1u)
  13001. {
  13002. CHECK (song.channels[0].instrument == lynx::InstrumentKind::granulizer && song.channels[0].pluginState == n5);
  13003. CHECK (song.channels[0].samplePath == "C:\\FL\\Data\\Patches\\Packs\\Drums\\Kicks\\808 Kick.wav");
  13004. }
  13005. CHECK (report.converted["Fruity Granulizer"] == 1);
  13006. }
  13007. }
  13008. // --- REA round 67: Drumaxx (exact engine) -------------------------------------------------------------------------
  13009. namespace
  13010. {
  13011. // our own deterministic 16-pad kit (LCG-chosen parameters), as r67/py/gold67d.py builds it for the DLL
  13012. std::vector<std::uint8_t> drumaxxTestState (bool eq)
  13013. {
  13014. std::uint32_t s = 20261015u;
  13015. auto u = [&s] {
  13016. s = s * 1664525u + 1013904223u;
  13017. return (float) ((double) (s >> 8) * (1.0 / 16777216.0));
  13018. };
  13019. // (a product rounded on its own: no fused multiply-add)
  13020. auto mul = [] (float a, float b) {
  13021. volatile float p = a * b;
  13022. return (float) p;
  13023. };
  13024. static const int targets[] = { 0, 9, 10, 16, 21, 23, 26 };
  13025. std::array<float, 720> P {};
  13026. for (int k = 0; k < 16; ++k)
  13027. for (int i = 0; i < 44; ++i)
  13028. {
  13029. auto v = u();
  13030. if (i == 0)
  13031. v = 0.5f + mul (v, 0.5f);
  13032. else if ((i >= 7 && i <= 18) || (i >= 20 && i <= 34))
  13033. v = 0.2f + mul (v, 0.6f);
  13034. else if (i == 2 || i == 3)
  13035. v = 0.0f;
  13036. else if (i == 4)
  13037. v = (float) (36 + k);
  13038. else if (i == 5)
  13039. v = v < 0.5f ? 0.0f : 1.0f;
  13040. else if (i == 6)
  13041. v = (float) (int) (v * 4.0f);
  13042. else if (i >= 35 && i <= 38)
  13043. v = (float) targets[(int) (v * 7.0f)];
  13044. P[(std::size_t) (44 * k + i)] = v;
  13045. }
  13046. for (int i = 0; i < 16; ++i)
  13047. {
  13048. auto v = u();
  13049. if (i == 1)
  13050. v = 0.3f + mul (v, 0.4f);
  13051. else if (i == 3)
  13052. v = eq ? 1.0f : 0.0f;
  13053. else if (i >= 4 && i <= 11)
  13054. v = 0.35f + mul (v, 0.3f);
  13055. else if (i == 12)
  13056. v = 1.0f;
  13057. else if (i == 13)
  13058. v = v * 0.1f;
  13059. else if (i == 0 || i == 15)
  13060. v = 0.0f;
  13061. P[(std::size_t) (704 + i)] = v;
  13062. }
  13063. std::vector<std::uint8_t> b;
  13064. auto put32 = [&b] (std::uint32_t v) { for (int i = 0; i < 4; ++i) b.push_back ((std::uint8_t) (v >> (8 * i))); };
  13065. b.insert (b.end(), { 'S', 'S', 'L', 'F' });
  13066. put32 (21636u);
  13067. put32 (0x444d5333u); // engine stream version 3
  13068. const std::string name = "LYNX test kit";
  13069. b.insert (b.end(), name.begin(), name.end());
  13070. b.resize (b.size() + 256 - name.size(), 0);
  13071. const auto at = b.size();
  13072. b.resize (at + 720 * 4);
  13073. std::memcpy (b.data() + at, P.data(), 720 * 4);
  13074. for (int k = 0; k < 16; ++k)
  13075. {
  13076. b.resize (b.size() + 0x400, 0);
  13077. b.push_back (100), b.push_back (100); // pattern length, trigger key
  13078. }
  13079. b.resize (b.size() + 0x800, 0);
  13080. for (int k = 0; k < 16; ++k)
  13081. b.push_back (k >= 2 && k <= 4 ? 1 : 0); // pads 3…5 in cut group 1
  13082. b.resize (b.size() + 16, 0); // every pad on the master
  13083. return b;
  13084. }
  13085. } // namespace
  13086. TEST (drumaxx_engine)
  13087. {
  13088. const auto st = drumaxxTestState (true);
  13089. CHECK (st.size() == 21644u && lynx::drumaxxStateUsable (st) && lynx::drumaxxKitName (st) == "LYNX test kit");
  13090. CHECK (lynx::drumaxxParam (st, 4) == 36.0f && lynx::drumaxxParam (st, 44 * 15 + 4) == 51.0f && lynx::drumaxxParam (st, 0x2c3) == 1.0f);
  13091. {
  13092. auto edited = st;
  13093. lynx::drumaxxSetParam (edited, 0x2c1, 0.25f);
  13094. CHECK (lynx::drumaxxParam (edited, 0x2c1) == 0.25f && std::isnan (lynx::drumaxxParam (edited, 720)));
  13095. auto bad = st;
  13096. bad[8] = 'X';
  13097. CHECK (! lynx::drumaxxStateUsable (bad) && std::isnan (lynx::drumaxxParam (bad, 0)));
  13098. }
  13099. // goldens: LYNX's renders, bit-identical to Drumaxx_x64.dll + engine_x64.dll driven by the round-67 harness
  13100. using N = lynx::drumaxx::RefNote;
  13101. struct Golden { bool eq; int fs, block; std::vector<N> notes; double el, er; std::tuple<int, float, float> at[6]; };
  13102. auto check = [] (const Golden& g) {
  13103. const auto y = lynx::drumaxx::renderReference (drumaxxTestState (g.eq), g.notes, (double) g.fs, 20000, g.block);
  13104. CHECK (y.size() == 2 * 20000);
  13105. if (y.size() != 2 * 20000)
  13106. return;
  13107. double el = 0.0, er = 0.0;
  13108. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  13109. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  13110. CHECK (std::abs (el - g.el) <= 1e-6 * g.el && std::abs (er - g.er) <= 1e-6 * g.er);
  13111. for (const auto& [i, l, r] : g.at)
  13112. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-6f * std::max (1e-16f, std::abs (l))
  13113. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-6f * std::max (1e-16f, std::abs (r)));
  13114. };
  13115. // master EQ and limiter on: velocities, Final.Vol, pitch offsets, the cut group (pads 3…5)
  13116. check ({ true, 44100, 512,
  13117. { { 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 },
  13118. { 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 },
  13119. { 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 } },
  13120. 2698.372377185949, 3678.285731410973,
  13121. { { 100, 0.0f, 0.0f }, { 1000, -0.0034233136102557182f, -0.005708245560526848f }, { 2500, -0.41297006607055664f, -0.0276018138974905f },
  13122. { 4000, -0.04906991869211197f, -0.7206904292106628f }, { 7777, 0.7544509172439575f, 0.7584841847419739f },
  13123. { 15000, -0.13438990712165833f, -0.6798369884490967f } } });
  13124. // EQ off, 48 kHz in 333-sample blocks, two pads on the same sample
  13125. check ({ false, 48000, 333,
  13126. { { 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 },
  13127. { 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 } },
  13128. 2241.008810618806, 1814.5219804412964,
  13129. { { 100, 0.0f, 0.0f }, { 1000, -0.32539352774620056f, -0.7439078688621521f }, { 2500, -0.1711389720439911f, -0.39125433564186096f },
  13130. { 4000, 0.15524788200855255f, 0.3445436358451843f }, { 7777, 0.5472834706306458f, 0.08737402409315109f },
  13131. { 15000, -0.03079034946858883f, -0.06140122935175896f } } });
  13132. // 96 kHz in 64-sample blocks, an octave down
  13133. check ({ true, 96000, 64,
  13134. { { 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 },
  13135. { 10000, 51, 1.0, 1.0, -1200.0 } },
  13136. 3522.8420617665834, 5746.555795626526,
  13137. { { 100, 0.0f, 0.0f }, { 1000, -0.04133236035704613f, -0.09954667836427689f }, { 2500, 0.2800408899784088f, -0.9350972175598145f },
  13138. { 4000, 0.9537044167518616f, 0.8892703056335449f }, { 7777, 0.8376388549804688f, -0.14166420698165894f },
  13139. { 15000, -0.05926012247800827f, -0.5840796828269958f } } });
  13140. // the instrument: a pad sounds on its key and dies away; keys without a pad stay silent
  13141. {
  13142. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumaxx);
  13143. lynx::ChannelDef def;
  13144. def.instrument = lynx::InstrumentKind::drumaxx;
  13145. def.pluginState = st;
  13146. inst->configure (def, 140.0);
  13147. inst->prepare (44100.0);
  13148. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  13149. inst->noteOn (90, 1.0f);
  13150. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  13151. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  13152. inst->noteOn (36, 0.8f);
  13153. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  13154. double e = 0.0;
  13155. for (auto v : l)
  13156. e += (double) v * v;
  13157. CHECK (! inst->idle() && e > 1e-3);
  13158. for (int k = 0; k < 200 && ! inst->idle(); ++k)
  13159. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  13160. CHECK (inst->idle());
  13161. }
  13162. // import: a Drumaxx channel plays on the exact engine with its raw state
  13163. {
  13164. FlpBuilder b;
  13165. b.ascii (0xC7, "25.2.5.5319");
  13166. b.u8 (0x40); b.u16 (0);
  13167. b.u8 (0x15); b.u8 (2);
  13168. b.utf16 (0xC9, u"Drumaxx");
  13169. b.data (0xD5, st);
  13170. lynx::FlpImportOptions options;
  13171. lynx::FlpImportReport report;
  13172. options.report = &report;
  13173. const auto song = lynx::importFlp (flp::parseProject (b.bytes), options);
  13174. CHECK (song.channels.size() == 1u);
  13175. if (song.channels.size() == 1u)
  13176. CHECK (song.channels[0].instrument == lynx::InstrumentKind::drumaxx && song.channels[0].pluginState == st);
  13177. CHECK (report.converted["Drumaxx"] == 1);
  13178. }
  13179. }
  13180. // --- REA round 68: FL Slayer (exact engine) and Fruity Dance (visual only) ------------------------------------------
  13181. namespace
  13182. {
  13183. std::string slayerDll()
  13184. {
  13185. if (realFlpDir.empty())
  13186. return {};
  13187. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "FL Slayer" / "Slayer_Engine.dll").string();
  13188. }
  13189. std::vector<std::uint8_t> slayerPreset (const char* name)
  13190. {
  13191. if (realFlpDir.empty())
  13192. return {};
  13193. const auto file = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer" / (std::string (name) + ".fst");
  13194. const auto states = pluginStatesIn (file, "FL Slayer");
  13195. return states.empty() ? std::vector<std::uint8_t> {} : states.front();
  13196. }
  13197. } // namespace
  13198. TEST (slayer_state)
  13199. {
  13200. // the saved state: int version, then the parameter integers (version 0: 28, version 1: 29 with the mod wheel)
  13201. std::vector<std::uint8_t> v0 (4 + 28 * 4, 0), v1 (4 + 29 * 4, 0);
  13202. const std::int32_t one = 1, wheel = 77, mode = 5;
  13203. std::memcpy (v1.data(), &one, 4);
  13204. std::memcpy (v1.data() + 4 + 28 * 4, &wheel, 4);
  13205. std::memcpy (v0.data() + 4 + 24 * 4, &mode, 4);
  13206. CHECK (lynx::slayerStateUsable (v0) && lynx::slayerStateUsable (v1));
  13207. CHECK (! lynx::slayerStateUsable (std::vector<std::uint8_t> (v0.begin(), v0.end() - 1)));
  13208. auto v2 = v1;
  13209. v2[0] = 2;
  13210. CHECK (! lynx::slayerStateUsable (v2));
  13211. const auto p0 = lynx::slayerStateParams (v0), p1 = lynx::slayerStateParams (v1);
  13212. CHECK (p0.size() == 29u && p0[24] == 5 && p0[28] == 0 && p1.size() == 29u && p1[28] == 77);
  13213. // the controls' maxima (the front-end sends value / max)
  13214. CHECK (lynx::slayerParamMax (0) == 6 && lynx::slayerParamMax (20) == 15 && lynx::slayerParamMax (24) == 5 && lynx::slayerParamMax (28) == 127
  13215. && lynx::slayerParamMax (3) == 65535);
  13216. // MIDI velocity from Init.Vol (0 → 0, which the engine takes as a note-off; FL's full velocity → 127)
  13217. CHECK (lynx::slayer::midiVelocity (0.0) == 0 && lynx::slayer::midiVelocity (1.0) == 99 && lynx::slayer::midiVelocity (2.0) == 126
  13218. && lynx::slayer::midiVelocity ((std::pow (11.0, 1.28) - 1.0) / 10.0) == 127);
  13219. }
  13220. TEST (slayer_engine)
  13221. {
  13222. const auto dll = slayerDll();
  13223. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  13224. return; // no FL installation
  13225. // against the installation's Slayer_Engine.dll (EXEC, REA_rodada68 §7: the DLL driven like FL's front-end drives it,
  13226. // host blocks of 512 at 44.1 kHz, tempo 120): bit-identical output. C3 vel 90 1024–20000, G3 vel 120 8000–30000,
  13227. // E4 vel 40 16000–40000, C5 vel 127 from 30000.
  13228. struct Sample { int at; float l, r; };
  13229. struct Golden { const char* preset; double el, er; Sample s[4]; };
  13230. const Golden goldens[] {
  13231. { "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 } } },
  13232. { "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 } } },
  13233. { "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 } } },
  13234. { "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 } } },
  13235. { "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 } } },
  13236. { "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 } } },
  13237. { "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 } } },
  13238. { "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 } } },
  13239. { "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 } } },
  13240. { "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 } } },
  13241. { "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 } } },
  13242. { "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 } } },
  13243. };
  13244. const std::vector<lynx::slayer::RefNote> notes = { { 1024, 48, 90, 20000 }, { 8000, 55, 120, 30000 }, { 16000, 64, 40, 40000 }, { 30000, 72, 127, -1 } };
  13245. constexpr int length = 44100;
  13246. int checked = 0;
  13247. for (const auto& g : goldens)
  13248. {
  13249. const auto st = slayerPreset (g.preset);
  13250. CHECK (lynx::slayerStateUsable (st));
  13251. if (! lynx::slayerStateUsable (st))
  13252. continue;
  13253. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, notes, 44100.0, length);
  13254. double el = 0.0, er = 0.0;
  13255. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  13256. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  13257. 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;
  13258. for (const auto& smp : g.s)
  13259. ok = ok && y[2 * (std::size_t) smp.at] == smp.l && y[2 * (std::size_t) smp.at + 1] == smp.r;
  13260. if (! ok)
  13261. 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);
  13262. CHECK (ok);
  13263. ++checked;
  13264. }
  13265. CHECK (checked == 12);
  13266. // the instrument: the engine primed with one sample, FL's velocity curve, the front-end's gains from the channel's
  13267. // MIDI pan / volume (REA_rodada79 §2: twice the channel gain through the host's integer-pan law); a note with
  13268. // velocity 0.8 is the reference note one sample later
  13269. const auto st = slayerPreset ("BASS Ebass 2");
  13270. lynx::ChannelDef c;
  13271. c.instrument = lynx::InstrumentKind::slayer;
  13272. c.pluginState = st;
  13273. c.samplePath = dll;
  13274. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  13275. inst->configure (c, 120.0);
  13276. inst->prepare (44100.0);
  13277. std::vector<float> l (16384, 0.0f), r (16384, 0.0f);
  13278. for (int b = 0; b < 32; ++b)
  13279. {
  13280. if (b == 0)
  13281. inst->noteOn (60, 0.8f);
  13282. if (b == 20)
  13283. inst->noteOff (60);
  13284. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 0.5f, 0.25f);
  13285. }
  13286. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  13287. const auto ref = lynx::slayer::renderReference (lynx::slayerStateParams (st), dll, { { 1, 60, vel, 1 + 20 * 512 } }, 44100.0, 16385);
  13288. // LYNX's channel gains 0.5 / 0.25 = gain 0.395…, pan −0.4097 → MIDI pan −26
  13289. const auto chanVol = (float) std::sqrt ((0.25 + 0.0625) * 0.5);
  13290. float hl = 0.0f, hr = 0.0f;
  13291. lynx::slayer::hostGains (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0), chanVol, hl, hr);
  13292. CHECK (lynx::slayer::midiPan (std::atan2 (0.25, 0.5) * (4.0 / 3.141592653589793) - 1.0) == -26);
  13293. double diff = 0.0, energy = 0.0;
  13294. for (int i = 0; i < 16384; ++i)
  13295. {
  13296. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  13297. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  13298. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13299. }
  13300. CHECK (diff == 0.0 && energy > 1.0 && vel == 102);
  13301. // every preset of the installation: usable and sounding
  13302. int presets = 0, sounding = 0;
  13303. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "FL Slayer";
  13304. if (std::filesystem::exists (folder))
  13305. for (const auto& entry : std::filesystem::directory_iterator (folder))
  13306. for (const auto& s : pluginStatesIn (entry.path(), "FL Slayer"))
  13307. {
  13308. ++presets;
  13309. const auto y = lynx::slayer::renderReference (lynx::slayerStateParams (s), dll, { { 1024, 52, 100, 20000 } }, 44100.0, 22050);
  13310. double e = 0.0;
  13311. bool finite = true;
  13312. for (auto v : y)
  13313. finite = finite && std::isfinite (v), e += (double) v * v;
  13314. CHECK (finite);
  13315. sounding += e > 1.0 ? 1 : 0;
  13316. }
  13317. std::printf (" FL Slayer presets: %d, %d sounding\n", presets, sounding);
  13318. CHECK (presets == 64 && sounding == 64);
  13319. }
  13320. // FL Slayer's host levels and live parameters (REA_rodada79): channel pan / volume / pitch arrive as MIDI events
  13321. // (ProcessEvent 2/3/4), automation and settings edits as setParameter calls on the running engine
  13322. TEST (slayer_host_levels_and_automation)
  13323. {
  13324. using namespace lynx::slayer;
  13325. CHECK (pitchBend (0) == 8192 && pitchBend (100) == 8874 && pitchBend (-100) == 7510 && pitchBend (1200) == 0x3fff
  13326. && pitchBend (-1200) == 0 && pitchBend (-1199) == 7 && pitchBend (5000) == 0x3fff);
  13327. CHECK (midiPan (0.0) == 0 && midiPan (1.0) == 64 && midiPan (-1.0) == -64 && midiPan (-0.5) == -32
  13328. && midiPan (99.0 / 6400.0) == 0 && midiPan (-199.0 / 6400.0) == -1 && midiPan (2.0) == 64);
  13329. float l0 = 0.0f, r0 = 0.0f, l1 = 0.0f, r1 = 0.0f;
  13330. hostGains (0, 0.5f, l0, r0); // the front-end's own start values (volume 1 after the ×2, centre)
  13331. hostGains (64, 1.0f, l1, r1);
  13332. CHECK (l0 == 0.70710677f && r0 == 0.70710677f && std::abs (l1) < 1e-15f && r1 == 2.0f);
  13333. const auto dll = slayerDll();
  13334. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  13335. return;
  13336. const auto st = slayerPreset ("DIST Dire Straits");
  13337. const auto params = lynx::slayerStateParams (st);
  13338. lynx::ChannelDef c;
  13339. c.instrument = lynx::InstrumentKind::slayer;
  13340. c.pluginState = st;
  13341. c.samplePath = dll;
  13342. c.sampler.pitch = 1.5f; // channel pitch +150 cents → bend 9216; no root shift
  13343. c.modelParams = { 0.0f };
  13344. auto inst = lynx::makeInstrument (lynx::InstrumentKind::slayer);
  13345. inst->configure (c, 120.0);
  13346. inst->prepare (44100.0);
  13347. const int blocks = 40, n = 512;
  13348. std::vector<float> l ((std::size_t) (blocks * n), 0.0f), r ((std::size_t) (blocks * n), 0.0f);
  13349. auto edited = st;
  13350. const std::int32_t newDrive = 60000; // parameter 13 (amp drive), edited in the settings at block 25
  13351. std::memcpy (edited.data() + 4 + 4 * 13, &newDrive, 4);
  13352. for (int b = 0; b < blocks; ++b)
  13353. {
  13354. if (b == 0)
  13355. inst->noteOn (52, 0.8f);
  13356. if (b == 10)
  13357. inst->setParameter (20, 9.0f / 15.0f); // automation: effect 9 (chorus)
  13358. if (b == 18)
  13359. inst->setParameter (28, 0.5f); // automation: the mod wheel → CC 1 64
  13360. if (b == 25)
  13361. {
  13362. c.pluginState = edited;
  13363. inst->configure (c, 120.0);
  13364. }
  13365. inst->render (l.data() + n * b, r.data() + n * b, n, 1.0f, 1.0f);
  13366. }
  13367. lynx::slayer::RefOptions opt;
  13368. opt.midi.push_back ({ 1, 0xe0, 9216 & 0x7f, 9216 >> 7 });
  13369. opt.params = { { 1 + 10 * n, 20, 9 }, { 1 + 18 * n, 28, 64 }, { 1 + 25 * n, 13, newDrive } };
  13370. const int vel = lynx::slayer::midiVelocity ((std::pow (11.0, 1.28 * 0.8) - 1.0) / 10.0);
  13371. const auto ref = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1, opt);
  13372. float hl = 0.0f, hr = 0.0f;
  13373. hostGains (0, 1.0f, hl, hr);
  13374. double diff = 0.0, energy = 0.0;
  13375. for (int i = 0; i < blocks * n; ++i)
  13376. {
  13377. diff = std::max ({ diff, (double) std::abs (l[(std::size_t) i] - ref[2 * (std::size_t) (i + 1)] * hl),
  13378. (double) std::abs (r[(std::size_t) i] - ref[2 * (std::size_t) (i + 1) + 1] * hr) });
  13379. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  13380. }
  13381. CHECK (diff == 0.0 && energy > 1.0);
  13382. // the changes really change the sound: the same render without them differs
  13383. const auto plain = lynx::slayer::renderReference (params, dll, { { 1, 52, vel, -1 } }, 44100.0, blocks * n + 1);
  13384. CHECK (plain != ref);
  13385. }
  13386. TEST (slayer_and_fruity_dance_import)
  13387. {
  13388. // FL Slayer plays on the exact engine when the installation's engine DLL is there (otherwise it stays unsupported);
  13389. // Fruity Dance (a visual plugin) becomes a silent channel that keeps its state
  13390. std::vector<std::uint8_t> slayer (4 + 28 * 4, 0), dance (40, 1);
  13391. FlpBuilder b;
  13392. b.ascii (0xC7, "25.2.5.5319");
  13393. int index = 0;
  13394. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"FL Slayer", slayer }, { u"Fruity Dance", dance } })
  13395. {
  13396. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  13397. b.u8 (0x15); b.u8 (2);
  13398. b.utf16 (0xC9, name);
  13399. b.data (0xD5, state);
  13400. }
  13401. const auto project = flp::parseProject (b.bytes);
  13402. const auto plain = lynx::importFlp (project);
  13403. CHECK (plain.channels.size() == 2u);
  13404. if (plain.channels.size() == 2u)
  13405. {
  13406. CHECK (plain.channels[0].instrument != lynx::InstrumentKind::slayer);
  13407. CHECK (plain.channels[1].instrument == lynx::InstrumentKind::fruitydance && plain.channels[1].pluginState == dance);
  13408. }
  13409. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitydance);
  13410. inst->prepare (44100.0);
  13411. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  13412. inst->noteOn (60, 1.0f);
  13413. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  13414. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  13415. const auto dll = slayerDll();
  13416. if (dll.empty() || ! lynx::slayerDllUsable (dll))
  13417. return;
  13418. lynx::FlpImportOptions withFiles;
  13419. withFiles.roots = { std::filesystem::path (realFlpDir).parent_path().string(), realFlpDir, "" };
  13420. withFiles.fileExists = [] (const std::string& path) {
  13421. auto local = path;
  13422. std::replace (local.begin(), local.end(), '\\', '/');
  13423. return std::filesystem::exists (local);
  13424. };
  13425. const auto song = lynx::importFlp (project, withFiles);
  13426. CHECK (song.channels.size() == 2u && song.channels[0].instrument == lynx::InstrumentKind::slayer && song.channels[0].pluginState == slayer
  13427. && ! song.channels[0].samplePath.empty());
  13428. }
  13429. #include "WaveAnalysisTests.inc"
  13430. #include "UiPolish2Tests.inc"
  13431. #include "UiToolbarTests.inc"
  13432. #include "LuxeVerbTests.inc"
  13433. #include "ConvolverTests.inc"
  13434. // --- Newtone / Newtime (REA_rodada73) -----------------------------------------------------------------------------
  13435. namespace newtonetest
  13436. {
  13437. 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))); }
  13438. 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))); }
  13439. void record (std::vector<std::uint8_t>& b, int id, const std::vector<std::uint8_t>& d)
  13440. {
  13441. put32 (b, id);
  13442. put64 (b, (std::int64_t) d.size());
  13443. b.insert (b.end(), d.begin(), d.end());
  13444. }
  13445. // a state with our own layout reading (REA_rodada73 §2): options, the wrapper record around the engine XML, the source path
  13446. std::vector<std::uint8_t> state (int version, std::uint32_t flags, const std::string& xml, const std::string& source)
  13447. {
  13448. std::vector<std::uint8_t> b, r1, r2;
  13449. put32 (b, version);
  13450. put32 (r1, 727);
  13451. put32 (r1, 582);
  13452. put32 (r1, (std::int32_t) flags);
  13453. r1.resize (0x28, 0);
  13454. record (b, 1, r1);
  13455. std::vector<std::uint8_t> chunk { 'V', 'C', '2', '!' };
  13456. put32 (chunk, (std::int32_t) xml.size() + 1);
  13457. chunk.insert (chunk.end(), xml.begin(), xml.end());
  13458. chunk.push_back (0);
  13459. put32 (r2, -9);
  13460. r2.push_back (0x0c);
  13461. put32 (r2, -2);
  13462. put64 (r2, (std::int64_t) chunk.size());
  13463. put32 (r2, 0);
  13464. r2.insert (r2.end(), chunk.begin(), chunk.end());
  13465. record (b, 2, r2);
  13466. record (b, 3, std::vector<std::uint8_t> (source.begin(), source.end()));
  13467. record (b, 4, std::vector<std::uint8_t> (12, 0));
  13468. record (b, 5, std::vector<std::uint8_t> (16, 0));
  13469. record (b, 6, std::vector<std::uint8_t> (32, 0));
  13470. return b;
  13471. }
  13472. const char* xmlWithTrack (bool edited)
  13473. {
  13474. return edited ? "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJPositionablePSOLASource\" "
  13475. "FilePath=\"C:\\tmp\\a&amp;b.wav\"><AudioObject SelfBPM=\"120.0\" OriginalFileLength=\"1000\" GlobalTune=\"0.0\">"
  13476. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" iOrigLength=\"500\" iCurLength=\"500\"/>"
  13477. "<Pitch_1 fOrigPitch=\"62.0\" fCurPitch=\"64.0\" iOrigLength=\"500\" iCurLength=\"500\"/></AudioObject></Trk_0></Tracks></NewToneProject>"
  13478. : "<NewToneProject NumOfTracks=\"1\"><Tracks><Trk_0 TrackNum=\"0\" Type=\"class CzplJTimelineWarpSource\" "
  13479. "FilePath=\"x.wav\"><AudioObject SelfBPM=\"80.0\" OriginalFileLength=\"1000\">"
  13480. "<Pitch_0 fOrigPitch=\"60.0\" fCurPitch=\"60.0\" fCurVolume=\"1.0\"/></AudioObject></Trk_0></Tracks></NewToneProject>";
  13481. }
  13482. } // namespace newtonetest
  13483. TEST (newtone_state)
  13484. {
  13485. using namespace newtonetest;
  13486. const auto a = lynx::newtonefx::parse (state (0, 5, xmlWithTrack (true), "%FLStudioUserData%\\vox.wav"));
  13487. CHECK (a.has_value());
  13488. CHECK (a->version == 0 && a->editorWidth == 727 && a->editorHeight == 582 && a->slave());
  13489. CHECK (a->sourcePath == "%FLStudioUserData%\\vox.wav");
  13490. CHECK (a->tracks.size() == 1 && a->tracks[0].notes == 2 && a->tracks[0].frames == 1000 && ! a->tracks[0].timeWarp);
  13491. CHECK (a->tracks[0].filePath == "C:\\tmp\\a&b.wav" && a->tracks[0].selfBpm == 120.0);
  13492. CHECK (a->tracks[0].edited && a->edited()); // note 1 moved from 62 to 64
  13493. const auto b = lynx::newtonefx::parse (state (1, 0x41, xmlWithTrack (false), ""));
  13494. CHECK (b.has_value() && ! b->slave() && b->sourcePath.empty());
  13495. CHECK (b->tracks.size() == 1 && b->tracks[0].timeWarp && ! b->edited());
  13496. // an empty project, as the installation's presets
  13497. const auto c = lynx::newtonefx::parse (state (0, 1, "<NewToneProject NumOfTracks=\"0\"><Tracks/></NewToneProject>", ""));
  13498. CHECK (c.has_value() && c->tracks.empty());
  13499. // broken: other version, truncated record, no options record
  13500. CHECK (! lynx::newtonefx::parse (state (2, 1, "", "")));
  13501. auto cut = state (0, 1, xmlWithTrack (true), "");
  13502. cut.resize (cut.size() - 3);
  13503. CHECK (! lynx::newtonefx::parse (cut));
  13504. CHECK (! lynx::newtonefx::parse ({ 0, 0, 0, 0 }));
  13505. // the FL import keeps the state and takes the slave switch as the editable parameter
  13506. const auto raw = state (0, 5, xmlWithTrack (false), "");
  13507. const auto slot = lynx::effectFromFl ("Newtone", raw, 120.0);
  13508. CHECK (slot && slot->kind == lynx::EffectKind::newtone && slot->state == raw && slot->params[0] == 1.0f);
  13509. const auto slot2 = lynx::effectFromFl ("Newtime", state (1, 0x41, xmlWithTrack (false), ""), 120.0);
  13510. CHECK (slot2 && slot2->kind == lynx::EffectKind::newtime && slot2->params[0] == 0.0f);
  13511. CHECK (lynx::formatEffectParam (lynx::EffectKind::newtone, 0, 1.0f, 120.0) == "on");
  13512. // the installation's own states (plugin database and the playlist's pitch/warp correction tracks) [DATA]
  13513. if (! realFlpDir.empty())
  13514. {
  13515. int found = 0, read = 0;
  13516. for (const char* rel : { "System/PitchCorrectionTrack.fst", "System/WarpCorrectionTrack.fst",
  13517. "Patches/Plugin database/Effects/Misc/Newtone.fst", "Patches/Plugin database/Effects/Misc/Newtime.fst" })
  13518. {
  13519. const auto path = std::filesystem::path (realFlpDir) / rel;
  13520. if (! std::filesystem::exists (path))
  13521. continue;
  13522. ++found;
  13523. std::ifstream f (path, std::ios::binary);
  13524. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  13525. const auto fx = lynx::effectFromFlPreset (bytes, 120.0);
  13526. read += fx && (fx->kind == lynx::EffectKind::newtone || fx->kind == lynx::EffectKind::newtime) && ! fx->state.empty()
  13527. && fx->params[0] == 0.0f && lynx::newtonefx::parse (fx->state)->tracks.empty();
  13528. }
  13529. std::printf (" %d Newtone/Newtime states, %d read\n", found, read);
  13530. CHECK (read == found);
  13531. }
  13532. }
  13533. TEST (newtone_engine)
  13534. {
  13535. // REA_rodada73 §3-4 [EXEC]: the input passes unchanged unless slave mode is on and the song plays; then the source
  13536. // sample is added from the song time on (at the source's own rate)
  13537. auto sample = std::make_shared<lynx::SampleData>();
  13538. sample->sampleRate = 44100.0;
  13539. for (int i = 0; i < 3000; ++i)
  13540. sample->left.push_back (0.001f * (float) (i % 97) - 0.04f);
  13541. const double sr = 44100.0, bpm = 120.0;
  13542. const auto tps = lynx::ticksPerSample (bpm, sr);
  13543. auto fx = lynx::newtonefx::make (sample);
  13544. fx->prepare (sr);
  13545. lynx::EffectParams on {}, off {};
  13546. on[0] = 1.0f;
  13547. std::vector<float> l (256), r (256), in (256);
  13548. auto block = [&] (double tick, bool playing, const lynx::EffectParams& p) {
  13549. for (int i = 0; i < 256; ++i)
  13550. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.25f * std::sin (0.01f * (float) (i + tick));
  13551. fx->setSongPosition (tick);
  13552. fx->setPlaying (playing);
  13553. fx->process (l.data(), r.data(), 256, p, bpm);
  13554. };
  13555. auto same = [&] { return std::equal (l.begin(), l.end(), in.begin()) && std::equal (r.begin(), r.end(), in.begin()); };
  13556. block (0.0, true, off);
  13557. CHECK (same()); // slave off
  13558. block (0.0, false, on);
  13559. CHECK (same()); // not playing
  13560. // playing from the start: in + source, sample by sample, over consecutive blocks
  13561. bool exact = true;
  13562. for (int b = 0; b < 4; ++b)
  13563. {
  13564. block (b * 256 * tps, true, on);
  13565. for (int i = 0; i < 256; ++i)
  13566. exact = exact && l[(std::size_t) i] == in[(std::size_t) i] + sample->left[(std::size_t) (b * 256 + i)]
  13567. && r[(std::size_t) i] == l[(std::size_t) i];
  13568. }
  13569. CHECK (exact);
  13570. // a jump re-seeks to the song time; past the end of the sample only the input remains
  13571. block (2000 * tps, true, on);
  13572. CHECK (l[0] == in[0] + sample->left[2000] && l[255] == in[255] + sample->left[2255]);
  13573. block (2900 * tps, true, on);
  13574. CHECK (l[99] == in[99] + sample->left[2999] && l[100] == in[100]);
  13575. // stopping and playing again: from the new song time
  13576. block (0.0, false, on);
  13577. CHECK (same());
  13578. block (10 * tps, true, on);
  13579. CHECK (l[0] == in[0] + sample->left[10]);
  13580. // without its sample: a pass-through
  13581. auto none = lynx::makeEffect (lynx::EffectKind::newtime);
  13582. none->prepare (sr);
  13583. for (int i = 0; i < 256; ++i)
  13584. l[(std::size_t) i] = r[(std::size_t) i] = in[(std::size_t) i] = 0.1f * (float) i / 256.0f;
  13585. none->setPlaying (true);
  13586. none->process (l.data(), r.data(), 256, on, bpm);
  13587. CHECK (same());
  13588. }
  13589. TEST (newtone_rack_follows_transport)
  13590. {
  13591. // the mixer gives the effect the transport, and keeps it running on a silent insert (no smart disable)
  13592. auto song = lynx::Song::makeDefault();
  13593. for (auto& p : song.patterns)
  13594. p.notes.clear();
  13595. song.bpm = 120.0;
  13596. auto slot = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  13597. slot.params[0] = 1.0f;
  13598. slot.samplePath = "newtone-test.wav";
  13599. song.mixer[1].effects.push_back (slot);
  13600. auto sample = std::make_shared<lynx::SampleData>();
  13601. sample->sampleRate = 48000.0;
  13602. sample->left.assign (48000, 0.5f);
  13603. int loads = 0;
  13604. lynx::RackState::SampleProvider provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  13605. ++loads;
  13606. return path == "newtone-test.wav" ? sample : nullptr;
  13607. };
  13608. const double sr = 48000.0;
  13609. auto state = lynx::RackState::build (song, nullptr, sr, provider);
  13610. CHECK (loads == 1);
  13611. lynx::RackRenderer rr;
  13612. rr.prepare (sr);
  13613. std::vector<float> l (512), r (512);
  13614. auto peakOf = [&] (bool playing, double tick) {
  13615. float peak = 0.0f;
  13616. for (int b = 0; b < 8; ++b)
  13617. {
  13618. rr.process (state.get(), playing, tick + b * 512 * lynx::ticksPerSample (song.bpm, sr), 512, l.data(), r.data());
  13619. for (auto v : l)
  13620. peak = std::max (peak, std::abs (v));
  13621. }
  13622. return peak;
  13623. };
  13624. CHECK (peakOf (false, 0.0) == 0.0f);
  13625. CHECK (peakOf (true, 0.0) > 0.1f);
  13626. }
  13627. #include "HardcoreTests.inc"
  13628. #include "DistructorTests.inc"
  13629. // --- missing files (core/MissingFiles.h) ------------------------------------------------------------------------
  13630. TEST (missing_files_names_and_scores)
  13631. {
  13632. CHECK (lynx::pathFileName ("C:\\Samples\\Drums\\Kick 01.wav") == "Kick 01.wav");
  13633. CHECK (lynx::pathFileName ("/home/u/loops/beat.flac") == "beat.flac");
  13634. CHECK (lynx::pathFileName ("D:/mixed\\sep/x.wav/") == "x.wav");
  13635. CHECK (lynx::pathFileName ("plain.wav") == "plain.wav");
  13636. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/Drums/Kicks/KICK.WAV") == 3);
  13637. CHECK (lynx::relocationScore ("C:\\Old\\Drums\\Kicks\\kick.wav", "/new/other/kick.wav") == 1);
  13638. CHECK (lynx::relocationScore ("C:\\Old\\kick.wav", "/new/kick2.wav") == 0);
  13639. }
  13640. TEST (missing_files_find_in_song)
  13641. {
  13642. lynx::Song song;
  13643. lynx::ChannelDef a, b, c, d, e;
  13644. a.name = "Kick", a.instrument = lynx::InstrumentKind::sampler, a.samplePath = "C:\\gone\\kick.wav";
  13645. b.name = "Vox", b.instrument = lynx::InstrumentKind::audioClip, b.samplePath = "C:\\gone\\vox.wav";
  13646. c.name = "Here", c.instrument = lynx::InstrumentKind::sampler, c.samplePath = "C:\\here\\snare.wav";
  13647. d.name = "GMS", d.instrument = lynx::InstrumentKind::gms, d.samplePath = "C:\\FL\\gms waves"; // installation data: not listed
  13648. e.name = "Kick 2", e.instrument = lynx::InstrumentKind::sampler, e.samplePath = "c:/GONE/KICK.wav"; // same file, other spelling
  13649. lynx::SampleZone z;
  13650. z.path = "C:\\gone\\piano_c4.wav";
  13651. a.zones.push_back (z);
  13652. song.channels = { a, b, c, d, e };
  13653. song.mixer.resize (3);
  13654. lynx::EffectSlot fx;
  13655. fx.samplePath = "C:\\gone\\tune.wav";
  13656. lynx::EffectSlot parallel;
  13657. parallel.branches.push_back ({ { fx }, 1.0f });
  13658. song.mixer[2].effects.push_back (parallel);
  13659. song.mixer[2].name = "Lead";
  13660. const auto exists = [] (const std::string& p) { return p.find ("here") != std::string::npos || p.find ("FL") != std::string::npos; };
  13661. auto missing = lynx::findMissingSamples (song, exists);
  13662. CHECK (missing.size() == 4);
  13663. CHECK (missing.size() == 4 && missing[0].path == "C:\\gone\\kick.wav" && missing[0].usedBy.size() == 2 && missing[0].usedBy[1] == "Kick 2");
  13664. CHECK (missing.size() == 4 && missing[1].path == "C:\\gone\\piano_c4.wav" && missing[1].usedBy[0] == "Kick");
  13665. CHECK (missing.size() == 4 && missing[2].usedBy[0] == "Audio clip: Vox");
  13666. CHECK (missing.size() == 4 && missing[3].usedBy[0] == "Lead: effect");
  13667. // candidates from a search: the deeper path match wins; one stays missing
  13668. const std::vector<std::string> hits { "/lib/other/kick.wav", "/lib/gone/kick.wav", "/lib/x/vox.wav", "/lib/y/tune.wav" };
  13669. CHECK (lynx::relocateByName (missing, hits) == 3);
  13670. CHECK (missing[0].found == "/lib/gone/kick.wav");
  13671. CHECK (missing[1].found.empty());
  13672. missing[3].skipped = true; // skipped: not applied
  13673. const auto map = lynx::relocationMap (missing);
  13674. CHECK (map.size() == 2);
  13675. CHECK (lynx::applyRelocations (song, map) == 3); // both spellings of kick.wav + vox
  13676. CHECK (song.channels[0].samplePath == "/lib/gone/kick.wav" && song.channels[4].samplePath == "/lib/gone/kick.wav");
  13677. CHECK (song.channels[1].samplePath == "/lib/x/vox.wav");
  13678. CHECK (song.channels[2].samplePath == "C:\\here\\snare.wav");
  13679. CHECK (song.mixer[2].effects[0].branches[0].chain[0].samplePath == "C:\\gone\\tune.wav");
  13680. lynx::Song copy = song;
  13681. CHECK (lynx::applyRelocations (copy, { { "C:\\gone\\tune.wav", "/t.wav" } }) == 1 && copy.mixer[2].effects[0].branches[0].chain[0].samplePath == "/t.wav");
  13682. }
  13683. TEST (missing_files_folder_search)
  13684. {
  13685. namespace fs = std::filesystem;
  13686. const auto root = fs::temp_directory_path() / "lynx-missing-test";
  13687. fs::remove_all (root);
  13688. fs::create_directories (root / "a" / "deep" / "Kicks");
  13689. fs::create_directories (root / "b");
  13690. for (const auto& f : { root / "a" / "deep" / "Kicks" / "Kick.WAV", root / "b" / "kick.wav", root / "b" / "hat.wav", root / "b" / "unrelated.wav" })
  13691. std::ofstream (f) << "x";
  13692. std::vector<lynx::MissingFile> missing { { "C:\\Old\\Kicks\\kick.wav", { "Kick" }, {}, false }, { "C:\\Old\\hat.wav", { "Hat" }, {}, false },
  13693. { "C:\\Old\\ride.wav", { "Ride" }, {}, false } };
  13694. int listed = 0;
  13695. auto list = [&] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  13696. for (const auto& entry : fs::directory_iterator (folder))
  13697. {
  13698. ++listed;
  13699. if (! onEntry (entry.path().string(), entry.is_directory()))
  13700. return;
  13701. }
  13702. };
  13703. const auto hits = lynx::searchFolderForMissing (missing, root.string(), list);
  13704. CHECK (hits.size() == 3);
  13705. CHECK (lynx::relocateByName (missing, hits) == 2);
  13706. CHECK (missing[0].found.find ("Kicks") != std::string::npos); // the copy in a "Kicks" folder matches better
  13707. CHECK (missing[1].found.find ("hat.wav") != std::string::npos);
  13708. CHECK (missing[2].found.empty());
  13709. // a bounded walk stops early; a cancelled one finds nothing
  13710. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 2).size() <= 1);
  13711. CHECK (lynx::searchFolderForMissing (missing, root.string(), list, 0, [] { return true; }).empty());
  13712. // nothing left to find: no walk at all
  13713. missing[2].skipped = true;
  13714. listed = 0;
  13715. CHECK (lynx::searchFolderForMissing (missing, root.string(), list).empty() && listed == 0);
  13716. fs::remove_all (root);
  13717. }
  13718. TEST (missing_files_folder_list)
  13719. {
  13720. const auto folders = lynx::parseFolderList ("C:\\A\r\n C:\\B \n\nc:\\a\nD:/x\n");
  13721. CHECK (folders.size() == 3 && folders[0] == "C:\\A" && folders[1] == "C:\\B" && folders[2] == "D:/x");
  13722. auto list = folders;
  13723. lynx::rememberFolder (list, "D:\\x");
  13724. CHECK (list.size() == 3 && list[0] == "D:\\x" && list[1] == "C:\\A");
  13725. lynx::rememberFolder (list, "E:\\new", 2);
  13726. CHECK (list.size() == 2 && list[0] == "E:\\new" && list[1] == "D:\\x");
  13727. CHECK (lynx::parseFolderList (lynx::folderListText (list)) == list);
  13728. }
  13729. TEST (missing_files_flp_report_and_relocation)
  13730. {
  13731. // an .flp sampler whose sample is gone: listed with its channel; a relocation map makes the import use the copy
  13732. flp::Project project;
  13733. flp::Channel ch;
  13734. ch.index = 0;
  13735. ch.type = flp::ChannelType::sampler;
  13736. ch.name = "Snare";
  13737. ch.samplePath = "C:\\Producer\\Packs\\snare 7.wav";
  13738. project.channels.push_back (ch);
  13739. lynx::FlpImportOptions options;
  13740. options.fileExists = [] (const std::string& p) { return p == "/found/snare 7.wav"; };
  13741. lynx::FlpImportReport report;
  13742. options.report = &report;
  13743. auto song = lynx::importFlp (project, options);
  13744. CHECK (report.missingFiles.size() == 1);
  13745. CHECK (report.missingFiles.size() == 1 && report.missingFiles[0].path == "C:\\Producer\\Packs\\snare 7.wav" && report.missingFiles[0].usedBy[0] == "Snare");
  13746. CHECK (song.channels.size() == 1 && song.channels[0].samplePath.empty());
  13747. lynx::FlpImportReport again;
  13748. options.report = &again;
  13749. options.relocations = { { "C:\\Producer\\Packs\\snare 7.wav", "/found/snare 7.wav" } };
  13750. song = lynx::importFlp (project, options);
  13751. CHECK (again.missingFiles.empty());
  13752. CHECK (song.channels.size() == 1 && song.channels[0].samplePath == "/found/snare 7.wav" && song.channels[0].instrument == lynx::InstrumentKind::sampler);
  13753. }
  13754. TEST (project_info_history_and_flp_import)
  13755. {
  13756. lynx::Song a;
  13757. auto b = a;
  13758. b.info.title = "Night Drive";
  13759. CHECK (lynx::describeChangeText (a, b) == "Project info");
  13760. auto c = a;
  13761. c.beatsPerBar = 3;
  13762. CHECK (lynx::describeChangeText (a, c) == "Time signature");
  13763. flp::Project project;
  13764. project.title = "Song", project.artist = "Me", project.genre = "House", project.url = "https://x", project.comments = "line 1\rline 2";
  13765. const auto song = lynx::importFlp (project);
  13766. CHECK (song.info.title == "Song" && song.info.author == "Me" && song.info.genre == "House" && song.info.url == "https://x");
  13767. CHECK (song.info.comments == "line 1\nline 2");
  13768. }
  13769. #include "UiFixTests.inc"
  13770. // ---------------------------------------------------------------------------
  13771. // Piano Roll tools of round 83 (expected values worked out from FL's formulas, docs/analise/REA_rodada83.md)
  13772. namespace
  13773. {
  13774. lynx::Note prNote (std::int64_t start, std::int64_t length, int key = 60, float velocity = 100.0f / 128.0f)
  13775. {
  13776. lynx::Note n;
  13777. n.start = start;
  13778. n.length = length;
  13779. n.key = key;
  13780. n.velocity = velocity;
  13781. return n;
  13782. }
  13783. std::vector<int> allOf (const lynx::Pattern& p)
  13784. {
  13785. std::vector<int> v;
  13786. for (int i = 0; i < (int) p.notes.size(); ++i)
  13787. v.push_back (i);
  13788. return v;
  13789. }
  13790. } // namespace
  13791. TEST (prtools_score_file)
  13792. {
  13793. using namespace lynx::prtools;
  13794. FlpBuilder b (96);
  13795. b.ascii (0xC7, "20.9.0");
  13796. b.u8 (0x41); b.u16 (0);
  13797. auto notes = noteRecord (0, 0, 24, 60, 100);
  13798. auto n2 = noteRecord (48, 0, 24, 67, 64);
  13799. n2[19] = 4; // colour 4
  13800. notes.insert (notes.end(), n2.begin(), n2.end());
  13801. b.data (0xE0, notes);
  13802. const auto score = parseScore (b.bytes);
  13803. CHECK (score && score->notes.size() == 2);
  13804. if (score && score->notes.size() == 2)
  13805. {
  13806. CHECK (score->notes[0].start == 0 && score->notes[0].length == 240 && score->notes[0].key == 60);
  13807. CHECK (score->notes[1].start == 480 && score->notes[1].colour == 4 && score->notes[1].velocity == 0.5f);
  13808. CHECK (score->length() == 720);
  13809. }
  13810. CHECK (! parseScore (std::vector<std::uint8_t> { 1, 2, 3 }));
  13811. const auto grid = gridScore (240);
  13812. CHECK (grid.notes.size() == 4 && grid.length() == 960 && grid.notes[3].start == 720);
  13813. CHECK (gridScore (240, 1.0f).notes[1].start == 240 + 120);
  13814. if (! realFlpDir.empty()) // the installation's templates
  13815. {
  13816. std::ifstream f (realFlpDir + "/Patches/Scores/Quantization/Default.fsc", std::ios::binary);
  13817. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  13818. const auto fsc = parseScore (bytes);
  13819. CHECK (fsc && fsc->notes.size() == 16 && fsc->length() == 3840 && fsc->notes[1].start == 240);
  13820. // an old (FL 3.5, 20-byte records) chopping pattern with colour-3 notes, read as colour 12
  13821. std::ifstream g (realFlpDir + "/Patches/Scores/Chopping/Default.fsc", std::ios::binary);
  13822. std::vector<std::uint8_t> chopBytes ((std::istreambuf_iterator<char> (g)), {});
  13823. const auto chop = parseScore (chopBytes);
  13824. CHECK (chop && ! chop->notes.empty());
  13825. }
  13826. }
  13827. TEST (prtools_quantize)
  13828. {
  13829. using namespace lynx;
  13830. using namespace lynx::prtools;
  13831. const auto grid = gridScore (240);
  13832. Pattern p;
  13833. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  13834. QuantizeOptions o; // start strength 64/128, sensitivity 36 (180 ticks)
  13835. quantize (p, allOf (p), grid, o);
  13836. CHECK (p.notes[0].start == 985 && p.notes[0].length == 500); // 1010 - (50 * 64) / 128
  13837. CHECK (p.notes[1].start == 1165); // toward 1200: 1130 + (70 * 64) / 128
  13838. p.notes = { prNote (1010, 500), prNote (1130, 100) };
  13839. o.start = 1.0f;
  13840. o.sensitivity = 12; // 60 ticks: 1130 is 70 away from 1200
  13841. quantize (p, allOf (p), grid, o);
  13842. CHECK (p.notes[0].start == 960 && p.notes[1].start == 1130);
  13843. p.notes = { prNote (1010, 500) };
  13844. o.sensitivity = 36;
  13845. o.durationMode = QuantizeOptions::leaveEnd;
  13846. quantize (p, allOf (p), grid, o);
  13847. CHECK (p.notes[0].start == 960 && p.notes[0].length == 550); // the end (1509) stays
  13848. p.notes = { prNote (1010, 500) };
  13849. o.durationMode = QuantizeOptions::quantizeEnd;
  13850. o.duration = 1.0f;
  13851. quantize (p, allOf (p), grid, o);
  13852. CHECK (p.notes[0].start == 960 && p.notes[0].length == 1439 - 960 + 1); // end 1509 -> the nearest end 1439
  13853. // levels: a template velocity of 50/100 halves the velocity; 0 removes the note
  13854. Score half = grid;
  13855. for (auto& n : half.notes)
  13856. n.velocity = 50.0f / 128.0f;
  13857. p.notes = { prNote (960, 100, 60, 0.8f) };
  13858. o = {};
  13859. quantize (p, allOf (p), half, o);
  13860. CHECK (std::abs (p.notes[0].velocity - 0.4f) < 1e-5f);
  13861. for (auto& n : half.notes)
  13862. n.velocity = 0.0f;
  13863. quantize (p, allOf (p), half, o);
  13864. CHECK (p.notes.empty());
  13865. }
  13866. TEST (prtools_chop)
  13867. {
  13868. using namespace lynx;
  13869. using namespace lynx::prtools;
  13870. Score pat;
  13871. pat.notes = { ScoreNote { 0, 240, 60 }, ScoreNote { 480, 240, 67 } }; // period 720
  13872. Pattern p;
  13873. p.notes = { prNote (0, 1920, 50) };
  13874. ChopOptions o;
  13875. chop (p, allOf (p), pat, o);
  13876. CHECK (p.notes.size() == 5);
  13877. if (p.notes.size() == 5)
  13878. {
  13879. CHECK (p.notes[0].start == 0 && p.notes[0].key == 50 && p.notes[1].start == 480 && p.notes[1].key == 57);
  13880. CHECK (p.notes[2].start == 720 && p.notes[3].start == 1200 && p.notes[4].start == 1440 && p.notes[4].length == 240);
  13881. }
  13882. // absolute: the pattern stays on the song grid; a piece before the note start is cut
  13883. p.notes = { prNote (100, 600, 50) };
  13884. chop (p, allOf (p), pat, o);
  13885. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 140 && p.notes[1].start == 480);
  13886. o.absolute = false;
  13887. p.notes = { prNote (100, 600, 50) };
  13888. chop (p, allOf (p), pat, o);
  13889. CHECK (p.notes.size() == 2 && p.notes[0].start == 100 && p.notes[0].length == 240 && p.notes[1].start == 580
  13890. && p.notes[1].length == 120);
  13891. // time mul x2 (wheel 5); colour 4 holds to the end in the first round only
  13892. o = {};
  13893. o.timeMul = 5;
  13894. p.notes = { prNote (0, 1920, 60) };
  13895. chop (p, allOf (p), pat, o);
  13896. CHECK (p.notes.size() == 3 && p.notes[1].start == 960 && p.notes[1].length == 480 && p.notes[2].start == 1440);
  13897. pat.notes[1].colour = 4;
  13898. o.timeMul = 4;
  13899. p.notes = { prNote (0, 1920, 60) };
  13900. chop (p, allOf (p), pat, o);
  13901. CHECK (p.notes.size() == 4 && p.notes[1].length == 1920 - 480 && p.notes[3].start == 1440);
  13902. }
  13903. TEST (prtools_flam)
  13904. {
  13905. using namespace lynx;
  13906. using namespace lynx::prtools;
  13907. Pattern p;
  13908. p.notes = { prNote (960, 480, 60, 0.75f) };
  13909. flam (p, allOf (p), flamPreset (FlamPreset::doubleStroke), 120.0); // +1 step after
  13910. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[1].start == 1200 && p.notes[1].length == 480);
  13911. CHECK (std::abs (p.notes[1].velocity - 0.75f * 85.0f / 128.0f) < 1e-6f);
  13912. p.notes = { prNote (960, 480) };
  13913. flam (p, allOf (p), flamPreset (FlamPreset::earlyDoubleStroke), 120.0); // the flam one step before, shortened
  13914. CHECK (p.notes.size() == 2 && p.notes[0].start == 720 && p.notes[0].length == 240 && p.notes[1].start == 960);
  13915. // absolute: -64 = -33.3 ms = -64 ticks at 120 BPM (960 PPQ); the original moves, the flam stays on the grid
  13916. CHECK (flamOffset (flamPreset (FlamPreset::flam), 120.0) == -64);
  13917. p.notes = { prNote (960, 480) };
  13918. flam (p, allOf (p), flamPreset (FlamPreset::flam), 120.0);
  13919. CHECK (p.notes.size() == 2 && p.notes[0].start == 960 && p.notes[0].length == 64 && p.notes[1].start == 1024);
  13920. p.notes = { prNote (960, 480) };
  13921. flam (p, allOf (p), flamPreset (FlamPreset::earlyFlam), 120.0);
  13922. CHECK (p.notes.size() == 2 && p.notes[0].start == 896 && p.notes[1].start == 960 && p.notes[1].length == 480);
  13923. }
  13924. TEST (prtools_lfo)
  13925. {
  13926. using namespace lynx;
  13927. using namespace lynx::prtools;
  13928. CHECK (lfoPeriod (20480) == 3840.0 && lfoPeriod (0) == 960.0 * 128 && lfoPeriod (40960) == 120.0);
  13929. LfoOptions o; // sine, phase 1/2: starts at the bottom (value 1/2, range 1/4)
  13930. const auto c = lfoCurve (o, 3840);
  13931. 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);
  13932. o.shape = LfoOptions::square;
  13933. o.phase = 0; // phase 0.5: Round (0.5) = 0 (half to even) -> +1, then -1
  13934. const auto sq = lfoCurve (o, 3840);
  13935. CHECK (sq[0] == 0.75f && sq[10] == 0.25f && sq[2000] == 0.75f);
  13936. o = {};
  13937. o.useEnd = true;
  13938. o.endRange = 0; // the range fades out
  13939. const auto fade = lfoCurve (o, 3840);
  13940. CHECK (std::abs (fade[3839] - 0.5f) < 0.01f);
  13941. Pattern p;
  13942. p.notes = { prNote (0, 100), prNote (960, 100), prNote (1920, 100) };
  13943. lfo (p, allOf (p), notetools::Level::velocity, LfoOptions {});
  13944. CHECK (std::abs (p.notes[0].velocity - 0.25f) < 1e-4f && std::abs (p.notes[1].velocity - 0.5f) < 1e-3f
  13945. && std::abs (p.notes[2].velocity - 0.75f) < 1e-3f);
  13946. }
  13947. TEST (prtools_claw)
  13948. {
  13949. using namespace lynx;
  13950. using namespace lynx::prtools;
  13951. ClawOptions o; // beat, trash the 4th quarter, stretch, remove short notes
  13952. Pattern p;
  13953. p.notes = { prNote (0, 240), prNote (960, 240), prNote (750, 100), prNote (1440, 240) };
  13954. claw (p, allOf (p), o);
  13955. CHECK (p.notes.size() == 3); // the note inside the trashed slice is gone
  13956. if (p.notes.size() == 3)
  13957. {
  13958. CHECK (p.notes[0].start == 0 && p.notes[0].length == 320);
  13959. CHECK (p.notes[1].start == 960 && p.notes[1].length == 320);
  13960. CHECK (p.notes[2].start == 1600 && p.notes[2].length == 320); // 1440 - 240 = 1200 -> x 4/3
  13961. }
  13962. o.trash = 9; // none: only the warp, which is neutral
  13963. p.notes = { prNote (100, 300) };
  13964. claw (p, allOf (p), o);
  13965. CHECK (p.notes.size() == 1 && p.notes[0].start == 100 && p.notes[0].length == 300);
  13966. o.timeDistortion = 1024; // strong bend: the period's ends stay, the middle moves
  13967. p.notes = { prNote (0, 480), prNote (960, 480) };
  13968. claw (p, allOf (p), o);
  13969. CHECK (p.notes[0].start == 0 && p.notes[1].start == 960 && p.notes[0].length > 480 && p.notes[1].length > 480);
  13970. o.trash = 0; // the first quarter
  13971. o.timeDistortion = 0;
  13972. o.stretch = false;
  13973. p.notes = { prNote (0, 240), prNote (240, 240) };
  13974. claw (p, allOf (p), o);
  13975. CHECK (p.notes.size() == 1 && p.notes[0].start == 0 && p.notes[0].length == 240);
  13976. }
  13977. TEST (prtools_articulate)
  13978. {
  13979. using namespace lynx;
  13980. using namespace lynx::prtools;
  13981. Pattern p;
  13982. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  13983. articulate (p, allOf (p), articulatePreset (ArticulatePreset::legato), 4);
  13984. CHECK (p.notes[0].length == 480 && p.notes[1].length == 480 && p.notes[2].length == 3840 - 960); // to the bar line
  13985. p.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 2880) };
  13986. articulate (p, allOf (p), articulatePreset (ArticulatePreset::staccato), 4);
  13987. CHECK (p.notes[0].length == 96 && p.notes[1].length == 96 && p.notes[2].length == 576); // ends on a bar: own length
  13988. auto o = articulatePreset (ArticulatePreset::legato);
  13989. o.gap = 20;
  13990. p.notes = { prNote (0, 100), prNote (480, 100) };
  13991. articulate (p, allOf (p), o, 4);
  13992. CHECK (p.notes[0].length == 460);
  13993. // chop chords: a held note is cut where the next one starts
  13994. p.notes = { prNote (0, 960, 48), prNote (480, 480, 60) };
  13995. articulate (p, allOf (p), articulatePreset (ArticulatePreset::chopChordsOnly), 4);
  13996. CHECK (p.notes.size() == 3 && p.notes[0].length == 480 && p.notes[1].start == 480 && p.notes[2].start == 480);
  13997. // variation is seeded (MT19937): same seed, same result; lengths only get shorter
  13998. o = articulatePreset (ArticulatePreset::legato);
  13999. o.variation = 50;
  14000. o.seed = 7;
  14001. Pattern a, b2;
  14002. a.notes = b2.notes = { prNote (0, 100), prNote (480, 100), prNote (960, 100) };
  14003. articulate (a, allOf (a), o, 4);
  14004. articulate (b2, allOf (b2), o, 4);
  14005. CHECK (a.notes[0].length == b2.notes[0].length && a.notes[0].length <= 480 && a.notes[0].length >= 240);
  14006. }
  14007. TEST (prtools_chords)
  14008. {
  14009. using namespace lynx::prtools;
  14010. auto c = parseProgression ("I-V-vi-IV", 0, false);
  14011. CHECK (c && c->size() == 4);
  14012. if (c && c->size() == 4)
  14013. {
  14014. CHECK ((*c)[0].root == 0 && (*c)[1].root == 7 && (*c)[2].root == 9 && (*c)[3].root == 5);
  14015. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 3, 7 }) && chordName ((*c)[2]) == "Am");
  14016. }
  14017. c = parseProgression ("ii 7-V 7-I \xE2\x88\x86-IV/\xE2\x99\xADVII", 0, false);
  14018. CHECK (c && c->size() == 4);
  14019. if (c && c->size() == 4)
  14020. {
  14021. CHECK ((*c)[0].intervals == std::vector<int> ({ 0, 3, 7, 10 }) && chordName ((*c)[0]) == "Dm7");
  14022. CHECK ((*c)[2].intervals == std::vector<int> ({ 0, 4, 7, 11 }) && chordName ((*c)[2]) == "Cmaj7");
  14023. CHECK ((*c)[3].root == 5 && (*c)[3].bass == 10 && chordName ((*c)[3]) == "F/A#");
  14024. }
  14025. c = parseProgression ("i-v-\xE2\x99\xADVI-iv", 9, true); // A minor: Am Em F Dm
  14026. CHECK (c && c->size() == 4 && (*c)[2].root == 5 && (*c)[3].root == 2 && (*c)[1].intervals[1] == 3);
  14027. CHECK (parseProgression ("Isus2 add9", 0, false) && parseProgression ("Isus2 add9", 0, false)->front().intervals
  14028. == std::vector<int> ({ 0, 2, 7, 14 }));
  14029. CHECK (! parseProgression ("I-X-V", 0, false));
  14030. 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");
  14031. CHECK (presets.size() == 3 && presets[0].numerals == "I-V-vi-IV" && ! presets[0].minor && presets[2].minor);
  14032. const auto rhythms = parseRhythmPresets ("Default,4,>,-,-,-,>,-,-,-,>,-,-,-,>,-,-,-\nRandom 1,4,r,16,2,1.5\n");
  14033. CHECK (rhythms.size() == 1 && rhythms[0].cells == ">--->--->--->---");
  14034. const auto g = generateProgression (0, false, 8, 0.3f, 5);
  14035. CHECK (g.size() == 8 && g[0].root == 0 && g == generateProgression (0, false, 8, 0.3f, 5));
  14036. for (std::uint32_t seed = 1; seed < 40; ++seed) // numerals read back as the same chords
  14037. {
  14038. const auto gen = generateProgression (2, seed % 2 == 0, 8, 1.0f, seed);
  14039. const auto back = parseProgression (toNumerals (gen, 2), 2, seed % 2 == 0);
  14040. CHECK (back && *back == gen);
  14041. }
  14042. CHECK (toNumerals (*parseProgression ("I-V-vi-IV/bVII", 0, false), 0) == "I-V-vi-IV/bVII");
  14043. lynx::Pattern p;
  14044. const auto chords = *parseProgression ("I-IV", 0, false);
  14045. ChordWriteOptions w;
  14046. const auto added = writeProgression (p, 2, 0, chords, w);
  14047. CHECK (added.size() == 8 && p.notes.size() == 8 && p.notes[0].channel == 2);
  14048. CHECK (std::any_of (p.notes.begin(), p.notes.end(), [] (auto& n) { return n.key == 48; })); // bass C3
  14049. w.rhythm = ">-`->-`->-`->-`-";
  14050. lynx::Pattern r;
  14051. writeProgression (r, 0, 0, chords, w);
  14052. CHECK (r.notes.size() == 8 && r.notes[0].length == 1920); // '>' + '-' of 960-tick cells
  14053. }
  14054. TEST (prtools_riff_machine)
  14055. {
  14056. using namespace lynx;
  14057. using namespace lynx::prtools;
  14058. Pattern p;
  14059. p.notes = { prNote (0, 100, 70) };
  14060. p.notes[0].channel = 1;
  14061. RiffOptions o;
  14062. o.channel = 1;
  14063. o.bars = 2;
  14064. const auto notes = riffMachine (p, { 0 }, o, 4);
  14065. CHECK (! notes.empty() && notes.size() == p.notes.size());
  14066. CHECK (std::all_of (p.notes.begin(), p.notes.end(), [] (auto& n) {
  14067. return n.channel == 1 && n.start >= 0 && n.start < 2 * 3840 && n.key >= 36 && n.key <= 84;
  14068. }));
  14069. // only the first step: the progression shape, chopped from one note at key 48
  14070. o.stepsUpTo = 0;
  14071. Pattern q;
  14072. const auto first = riffMachine (q, {}, o, 4);
  14073. CHECK (first.size() == builtInRiffProgression (0).notes.size() * 2 && q.notes[0].key == 48);
  14074. }
  14075. TEST (event_editor_lanes)
  14076. {
  14077. using namespace lynx;
  14078. Pattern p;
  14079. const AutomationTarget vol { AutomationTargetKind::channelVolume, 2 };
  14080. auto& lane = eventedit::laneFor (p, vol);
  14081. CHECK (p.events.size() == 1 && &eventedit::laneFor (p, vol) == &lane && eventedit::findLane (p, { AutomationTargetKind::channelPan, 2 }) == nullptr);
  14082. eventedit::fill (lane, 0, 960, 0.25f, 240);
  14083. CHECK (lane.events.size() == 4 && lane.events[3].tick == 720 && lane.events[3].value == 0.25f);
  14084. CHECK (lane.valueAt (-5.0) == 0.25f && lane.valueAt (5000.0) == 0.25f);
  14085. eventedit::line (lane, 960, 1920, 0.0f, 1.0f, 240); // 4 cells: 0, 1/3, 2/3, 1
  14086. CHECK (lane.events.size() == 8 && lane.events[4].value == 0.0f && lane.events[7].value == 1.0f);
  14087. CHECK (std::abs (lane.valueAt (1500.0) - 2.0f / 3.0f) < 1e-6f); // held from 1440
  14088. eventedit::setEvent (lane, 1440, 0.1f);
  14089. CHECK (lane.events.size() == 8 && lane.valueAt (1440.0) == 0.1f);
  14090. eventedit::setEvent (lane, 100, 0.9f);
  14091. CHECK (lane.events.size() == 9 && lane.events[1].tick == 100);
  14092. eventedit::scale (lane, 0, 960, 2.0f, 0.0f, false);
  14093. CHECK (lane.events[0].value == 0.5f && lane.events[1].value == 1.0f); // clamped
  14094. eventedit::scale (lane, 0, 960, 0.0f, 0.0f, true); // bipolar: toward the centre
  14095. CHECK (lane.events[0].value == 0.5f && lane.events[2].value == 0.5f);
  14096. eventedit::erase (lane, 0, 960);
  14097. CHECK (lane.events.size() == 4 && lane.events.front().tick == 960);
  14098. auto copy = lane;
  14099. eventedit::randomize (lane, 0, 4000, 0.2f, 3);
  14100. eventedit::randomize (copy, 0, 4000, 0.2f, 3);
  14101. CHECK (lane.events[1].value == copy.events[1].value && std::abs (lane.events[1].value - 1.0f / 3.0f) <= 0.2f + 1e-6f);
  14102. prtools::LfoOptions o;
  14103. eventedit::applyLfo (lane, 0, 3840, o, 960); // FL's LFO: bottom, middle, top, middle
  14104. 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);
  14105. p.events.push_back ({ { AutomationTargetKind::channelPan, 1 }, {} });
  14106. eventedit::removeEmptyLanes (p);
  14107. CHECK (p.events.size() == 1);
  14108. // undo label
  14109. auto before = Song::makeDefault();
  14110. auto after = before;
  14111. after.patterns[0].events.push_back ({ vol, { { 0, 0.5f } } });
  14112. CHECK (describeChangeText (before, after) == "Edit events: " + after.patterns[0].name);
  14113. // channel removal re-targets the lanes like automations
  14114. auto song = Song::makeDefault();
  14115. song.patterns[0].events.push_back ({ { AutomationTargetKind::channelVolume, 3 }, { { 0, 0.5f } } });
  14116. song.removeChannel (1);
  14117. CHECK (song.patterns[0].events[0].target.index == 2);
  14118. }
  14119. TEST (event_editor_playback)
  14120. {
  14121. // pattern events on the Master volume: silent first half of each pattern, sound in the second half
  14122. auto song = lynx::Song::makeDefault();
  14123. song.bpm = 120.0;
  14124. const auto bar = (std::int64_t) lynx::ppq * 4;
  14125. song.patterns[0].lengthTicks = bar;
  14126. song.patterns[0].notes.clear();
  14127. for (int s = 0; s < 16; ++s)
  14128. song.patterns[0].notes.push_back ({ s * 240, 200, 0, 60 });
  14129. song.patterns[0].events.push_back ({ { lynx::AutomationTargetKind::mixerVolume, 0 }, { { 0, 0.0f }, { bar / 2, 0.8f } } });
  14130. const auto peaks = [&] (lynx::PlayMode mode) {
  14131. song.mode = mode;
  14132. song.playlist.clear();
  14133. song.playlist.push_back ({ 0, 0, bar, bar, 0 }); // one clip, in the second bar
  14134. const double sr = 48000.0;
  14135. auto state = lynx::RackState::build (song, nullptr, sr);
  14136. lynx::RackRenderer r;
  14137. r.prepare (sr, 512);
  14138. std::vector<float> l (512), rr (512);
  14139. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  14140. std::array<float, 6> peak {}; // half bars
  14141. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  14142. {
  14143. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  14144. // blocks inside a half bar, away from its edges (the gain glides over one chunk at a change)
  14145. const auto blockEnd = tick + 512 * tps;
  14146. const auto half = (std::size_t) (tick / (double) (bar / 2));
  14147. const auto edge = (double) half * (double) (bar / 2);
  14148. if (half > 5 || tick - edge < 2 * 512 * tps || blockEnd > edge + (double) (bar / 2))
  14149. continue;
  14150. for (auto v : l)
  14151. peak[half] = std::max (peak[half], std::abs (v));
  14152. }
  14153. return std::make_pair (state->eventLanes.size(), peak);
  14154. };
  14155. const auto [lanesSong, song3] = peaks (lynx::PlayMode::song);
  14156. CHECK (lanesSong == 1);
  14157. CHECK (song3[2] < 1.0e-4f && song3[3] > 0.01f); // inside the clip: silent, then sound
  14158. const auto [lanesPattern, loop] = peaks (lynx::PlayMode::pattern);
  14159. CHECK (lanesPattern == 1);
  14160. CHECK (loop[0] < 1.0e-4f && loop[1] > 0.01f && loop[2] < 1.0e-4f && loop[3] > 0.01f);
  14161. }
  14162. #include "PrefsTests.inc"
  14163. TEST (level_audit_demo_imports_round89)
  14164. {
  14165. // REA_rodada89 §4: two demo projects whose FL renders were much louder than LYNX's
  14166. if (realFlpDir.empty())
  14167. return;
  14168. auto load = [] (const std::filesystem::path& path) -> std::optional<lynx::Song> {
  14169. if (! std::filesystem::exists (path))
  14170. return std::nullopt;
  14171. std::ifstream in (path, std::ios::binary);
  14172. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  14173. return lynx::importFlp (flp::parseProject (file));
  14174. };
  14175. const auto demos = std::filesystem::path (realFlpDir) / "Demo projects";
  14176. // FL 12+: a route to insert 100+ without a level record plays at 100% (the percussion bus of "Lightning")
  14177. if (const auto song = load (demos / "Product demos" / "Square a Saw - Lightning (VEELA Vocals Resynthesized).flp"))
  14178. {
  14179. int highRoutes = 0, highAtZero = 0;
  14180. for (const auto& t : song->mixer)
  14181. for (const auto& send : t.sends)
  14182. if (send.target >= 100)
  14183. {
  14184. ++highRoutes;
  14185. highAtZero += send.level <= 0.0f;
  14186. }
  14187. std::printf (" Lightning: %d routes to inserts 100+, %d at level 0\n", highRoutes, highAtZero);
  14188. CHECK (highRoutes > 0 && highAtZero == 0);
  14189. }
  14190. // Fruity Compressors FL saved as VST2 inside Fruity Wrapper (RawFL): the exact engine with the saved controls
  14191. if (const auto song = load (demos / "Visual" / "ZGameEditor Visualizer" / "Treben - Gimbal and Sinan - RawFL.flp"))
  14192. {
  14193. int compressors = 0, withSettings = 0;
  14194. for (const auto& t : song->mixer)
  14195. for (const auto& fx : t.effects)
  14196. if (fx.kind == lynx::EffectKind::fruityCompressor)
  14197. {
  14198. ++compressors;
  14199. const auto defaults = lynx::makeEffectSlot (lynx::EffectKind::fruityCompressor);
  14200. withSettings += fx.params != defaults.params;
  14201. }
  14202. std::printf (" RawFL: %d wrapped Fruity Compressors on the exact engine, %d with their own settings\n", compressors, withSettings);
  14203. CHECK (compressors >= 10 && withSettings >= 8);
  14204. }
  14205. }
  14206. #include "DialogsTests84.inc"
  14207. // --- native plugin editors (round 93): FL's arrangements ---------------------------------------------------------
  14208. TEST (plugin_layouts_name_real_parameters)
  14209. {
  14210. using namespace lynx;
  14211. // every value control edits a parameter of its effect, inside the body; FL arrangements place (or list in a menu)
  14212. // every parameter of the effect
  14213. for (const auto kind : plugui::flLayoutKinds())
  14214. {
  14215. const auto& l = plugui::layoutFor (kind);
  14216. const auto& info = effectInfo (kind);
  14217. CHECK (plugui::hasFlLayout (kind));
  14218. CHECK (l.width > 100 && l.height > 60);
  14219. std::vector<bool> placed (info.params.size(), false);
  14220. for (const auto& item : l.items)
  14221. {
  14222. 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);
  14223. if (item.param != nullptr)
  14224. {
  14225. const auto p = plugui::paramIndex (kind, item.param);
  14226. CHECK (p >= 0);
  14227. if (p >= 0)
  14228. placed[(std::size_t) p] = true;
  14229. }
  14230. if (item.type == plugui::Item::radio || item.type == plugui::Item::tabs || item.type == plugui::Item::menu)
  14231. {
  14232. const auto labels = plugui::optionLabels (item);
  14233. CHECK (labels.size() >= 2);
  14234. CHECK (item.values.empty() || item.values.size() == labels.size());
  14235. }
  14236. if (item.type == plugui::Item::knob && item.h + 0.5f < item.w) // (a caption above its knob would hide it)
  14237. {
  14238. std::printf (" %s: knob '%s' is shorter than it is wide\n", info.name, item.param != nullptr ? item.param : "");
  14239. CHECK (false);
  14240. }
  14241. CHECK (item.param2 == nullptr || plugui::paramIndex (kind, item.param2) >= 0);
  14242. if (item.param2 != nullptr && plugui::paramIndex (kind, item.param2) >= 0)
  14243. placed[(std::size_t) plugui::paramIndex (kind, item.param2)] = true;
  14244. for (const auto* when : { item.whenParam, item.when2Param })
  14245. CHECK (when == nullptr || plugui::paramIndex (kind, when) >= 0);
  14246. }
  14247. for (const auto* name : l.menuSwitches)
  14248. {
  14249. const auto p = plugui::paramIndex (kind, name);
  14250. CHECK (p >= 0);
  14251. if (p >= 0)
  14252. placed[(std::size_t) p] = true;
  14253. }
  14254. for (const auto* name : l.editedElsewhere)
  14255. {
  14256. const auto p = plugui::paramIndex (kind, name);
  14257. CHECK (p >= 0);
  14258. if (p >= 0)
  14259. placed[(std::size_t) p] = true;
  14260. }
  14261. for (std::size_t p = 0; p < placed.size(); ++p)
  14262. if (! placed[p])
  14263. {
  14264. std::printf (" %s: parameter '%s' has no control\n", info.name, info.params[p].name);
  14265. CHECK (false);
  14266. }
  14267. }
  14268. }
  14269. TEST (plugin_layout_options_match_the_engine_values)
  14270. {
  14271. using namespace lynx;
  14272. // the switches show FL's captions for the engine's own values
  14273. const auto text = [] (EffectKind kind, const char* param, int option) {
  14274. for (const auto& item : plugui::layoutFor (kind).items)
  14275. if (item.param != nullptr && std::strcmp (item.param, param) == 0 && item.options != nullptr)
  14276. return formatEffectParam (kind, plugui::paramIndex (kind, param), plugui::optionValue (item, option), 120.0);
  14277. return std::string ("?");
  14278. };
  14279. CHECK (text (EffectKind::fruityDelay3, "Delay type", 0) == "Fake stereo");
  14280. CHECK (text (EffectKind::fruityDelay3, "Delay type", 2) == "Ping pong");
  14281. CHECK (text (EffectKind::fruityDelay3, "Delay type", 3) == "Off");
  14282. CHECK (text (EffectKind::fruityDelay3, "Filter mode", 1) == "High pass");
  14283. CHECK (text (EffectKind::fruityDelay3, "Saturation type", 1) == "Saturate");
  14284. CHECK (text (EffectKind::fruityMultiband, "Low mode", 1) == "off");
  14285. CHECK (text (EffectKind::fruityMultiband, "High mode", 2) == "bypass");
  14286. CHECK (text (EffectKind::luxeVerb, "Pitch type", 1) == "feed-forward");
  14287. CHECK (text (EffectKind::luxeVerb, "Freeze mode", 2) == "sustain");
  14288. CHECK (text (EffectKind::stereoEnhancer, "Invert", 2) == "invert R");
  14289. CHECK (text (EffectKind::parametricEq, "B3 type", 4) == formatEffectParam (EffectKind::parametricEq, peq::param (2, 0), peq::typeValue (peq::BandType::lowCut), 120.0));
  14290. // the option nearest to a value; pages and conditions
  14291. plugui::LayoutItem radio;
  14292. radio.type = plugui::Item::radio;
  14293. radio.options = "A|B|C|D";
  14294. CHECK (plugui::optionLabels (radio).size() == 4);
  14295. CHECK (plugui::optionAt (radio, 0.30f) == 1);
  14296. CHECK (plugui::optionAt (radio, 0.95f) == 3);
  14297. CHECK (std::abs (plugui::optionValue (radio, 2) - 2.0f / 3.0f) < 1e-6f);
  14298. const auto& lux = plugui::layoutFor (EffectKind::luxeVerb);
  14299. auto params = makeEffectSlot (EffectKind::luxeVerb).params;
  14300. const auto pitchType = plugui::paramIndex (EffectKind::luxeVerb, "Pitch type");
  14301. int feedForward = 0, feedback = 0;
  14302. for (float v : { 0.0f, 1.0f })
  14303. {
  14304. params[(std::size_t) pitchType] = v;
  14305. for (const auto& item : lux.items)
  14306. if (item.param != nullptr && std::strcmp (item.param, "Feed-forward pitch 1") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  14307. ++feedForward;
  14308. else if (item.param != nullptr && std::strcmp (item.param, "Feedback pitch") == 0 && plugui::isShown (EffectKind::luxeVerb, item, 0, params))
  14309. ++feedback;
  14310. }
  14311. CHECK (feedForward == 1); // only in feed-forward mode
  14312. CHECK (feedback == 2); // value plate + fader, only in feedback mode
  14313. const auto& limiter = plugui::layoutFor (EffectKind::fruityLimiter);
  14314. CHECK (limiter.pages.size() == 2);
  14315. int ceilingShown = 0;
  14316. for (int page = 0; page < 2; ++page)
  14317. for (const auto& item : limiter.items)
  14318. if (item.param != nullptr && std::strcmp (item.param, "Ceiling") == 0 && plugui::isShown (EffectKind::fruityLimiter, item, page, params))
  14319. ++ceilingShown;
  14320. CHECK (ceilingShown == 1); // LIMIT page only
  14321. // every other effect still gets a (generated) window
  14322. CHECK (! plugui::hasFlLayout (EffectKind::distortion));
  14323. CHECK (! plugui::layoutFor (EffectKind::distortion).items.empty());
  14324. }