tests.cpp 602 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815
  1. // Core tests (no JUCE). Usage: lynx_tests [folder-with-flp-files]
  2. #include <atomic>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <filesystem>
  8. #include <map>
  9. #include <set>
  10. #include <sstream>
  11. #include <fstream>
  12. #include <functional>
  13. #include <iterator>
  14. #include <string>
  15. #include <tuple>
  16. #include <vector>
  17. #include "core/Dither.h"
  18. #include "core/DrumSynthLive.h"
  19. #include "core/Effects.h"
  20. #include "core/Effector.h"
  21. #include "core/FruityDelayBank.h"
  22. #include "core/FruityMultiband.h"
  23. #include "core/Denormals.h"
  24. #include "core/FlpImport.h"
  25. #include "core/FlEffects.h"
  26. #include "core/FlPatcher.h"
  27. #include "core/FlSamplers.h"
  28. #include "core/FlSynths.h"
  29. #include "core/Inflate.h"
  30. #include "core/WavPack.h"
  31. #include "core/FlWrapper.h"
  32. #include "core/Gms.h"
  33. #include "core/PoiZone.h"
  34. #include "core/MiniSynth.h"
  35. #include "core/SimSynth.h"
  36. #include "core/Wasp.h"
  37. #include "core/Plucked.h"
  38. #include "core/BeepMap.h"
  39. #include "core/Sawer.h"
  40. #include "core/Toxic.h"
  41. #include "core/Harmless.h"
  42. #include "core/Harmor.h"
  43. #include "core/Morphine.h"
  44. #include "core/Ogun.h"
  45. #include "core/BassDrum.h"
  46. #include "core/FruitKick.h"
  47. #include "core/Dx10.h"
  48. #include "core/Kepler.h"
  49. #include "core/ThreeOsc.h"
  50. #include "core/FlResampler.h"
  51. #include "core/FlVoiceProcessor.h"
  52. #include "core/TransistorBass.h"
  53. #include "core/HarmlessFx.h"
  54. #include "core/MaximusFx.h"
  55. #include "core/Reeverb2.h"
  56. #include "core/LuxeVerb.h"
  57. #include "core/Denormals.h"
  58. #include "core/FruityDynamics.h"
  59. #include "core/FruityShapers.h"
  60. #include "core/ParametricEqFl.h"
  61. #include "core/FruityBasicFx.h"
  62. #include "core/EquoFx.h"
  63. #include "core/PitchShifterFl.h"
  64. #include "core/FruityDelay.h"
  65. #include "core/StereoFx.h"
  66. #include "core/ChorusFx.h"
  67. #include "core/GrossBeat.h"
  68. #include "core/FlangusFastDist.h"
  69. #include "core/LiveEventQueue.h"
  70. #include "core/RackRenderer.h"
  71. #include "core/Scheduler.h"
  72. #include "core/SnapshotExchange.h"
  73. #include "core/SoftClip.h"
  74. #include "core/Slicer.h"
  75. #include "core/SytrusEngine.h"
  76. #include "core/Templates.h"
  77. #include "core/Vocodex.h"
  78. #include "core/TimeStretch.h"
  79. #include "core/WorkerPool.h"
  80. #include "core/Song.h"
  81. #include "core/NoteEdit.h"
  82. #include "core/Recorder.h"
  83. #include "core/KeyMap.h"
  84. #include "core/RackOps.h"
  85. #include "core/ValueMath.h"
  86. #include "core/Workspace.h"
  87. #include "core/StartupSound.h"
  88. #include "flp/Flp.h"
  89. #include "ui/I18nData.h"
  90. namespace
  91. {
  92. int failures = 0;
  93. int checks = 0;
  94. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  95. {
  96. static std::vector<std::pair<const char*, std::function<void()>>> r;
  97. return r;
  98. }
  99. struct Register
  100. {
  101. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  102. };
  103. #define TEST(name) \
  104. static void name(); \
  105. static Register reg_##name (#name, name); \
  106. static void name()
  107. #define CHECK(cond) \
  108. do \
  109. { \
  110. ++checks; \
  111. if (! (cond)) \
  112. { \
  113. ++failures; \
  114. std::printf (" FAILED %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  115. } \
  116. } while (false)
  117. std::string realFlpDir;
  118. // --- helpers for building synthetic .flp files -----------------------------
  119. struct FlpBuilder
  120. {
  121. std::vector<std::uint8_t> bytes;
  122. FlpBuilder (std::uint16_t ppq = 96)
  123. {
  124. put ("FLhd");
  125. u32 (6);
  126. u16 (0);
  127. u16 (1);
  128. u16 (ppq);
  129. put ("FLdt");
  130. u32 (0); // size is not used by the reader
  131. }
  132. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  133. void u8 (std::uint8_t v) { bytes.push_back (v); }
  134. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  135. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  136. void varint (std::size_t n)
  137. {
  138. do
  139. {
  140. auto b = (std::uint8_t) (n & 0x7F);
  141. n >>= 7;
  142. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  143. } while (n);
  144. }
  145. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  146. {
  147. u8 (id);
  148. varint (payload.size());
  149. bytes.insert (bytes.end(), payload.begin(), payload.end());
  150. }
  151. void ascii (std::uint8_t id, const std::string& s)
  152. {
  153. std::vector<std::uint8_t> p (s.begin(), s.end());
  154. p.push_back (0);
  155. data (id, p);
  156. }
  157. void utf16 (std::uint8_t id, const std::u16string& s)
  158. {
  159. std::vector<std::uint8_t> p;
  160. for (auto c : s)
  161. {
  162. p.push_back ((std::uint8_t) c);
  163. p.push_back ((std::uint8_t) (c >> 8));
  164. }
  165. p.push_back (0);
  166. p.push_back (0);
  167. data (id, p);
  168. }
  169. };
  170. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  171. std::uint8_t velocity)
  172. {
  173. std::vector<std::uint8_t> r (24, 0);
  174. std::memcpy (&r[0], &pos, 4);
  175. std::memcpy (&r[6], &channel, 2);
  176. std::memcpy (&r[8], &len, 4);
  177. std::memcpy (&r[12], &key, 2);
  178. r[16] = 120;
  179. r[20] = 64;
  180. r[21] = velocity;
  181. return r;
  182. }
  183. } // namespace
  184. // ---------------------------------------------------------------------------
  185. // FLP
  186. TEST (flp_rejects_garbage)
  187. {
  188. std::vector<std::uint8_t> junk (64, 0x41);
  189. bool threw = false;
  190. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  191. CHECK (threw);
  192. }
  193. TEST (flp_reads_size_classes_varint_and_extended_ids)
  194. {
  195. FlpBuilder b;
  196. b.u8 (0x15); b.u8 (2); // u8
  197. b.u8 (0x40); b.u16 (513); // u16
  198. b.u8 (0x9C); b.u32 (128500); // u32
  199. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // 2-byte varint
  200. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // extended ID
  201. flp::EventReader r (b.bytes);
  202. flp::Event e;
  203. CHECK (r.header().ppq == 96);
  204. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  205. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  206. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  207. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  208. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  209. CHECK (! r.next (e));
  210. }
  211. TEST (flp_truncated_file_throws)
  212. {
  213. FlpBuilder b;
  214. b.u8 (0x9C); b.u8 (1); // u32 with only 1 byte
  215. flp::EventReader r (b.bytes);
  216. flp::Event e;
  217. bool threw = false;
  218. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  219. CHECK (threw);
  220. }
  221. TEST (flp_version_numbers)
  222. {
  223. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  224. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  225. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  226. }
  227. TEST (flp_playlist_record_size_by_version)
  228. {
  229. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  230. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  231. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  232. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // size doesn't match the version -> tries the others
  233. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  234. }
  235. TEST (flp_parses_project_model)
  236. {
  237. FlpBuilder b (96);
  238. b.ascii (0xC7, "25.2.5.5319");
  239. b.u8 (0x9C); b.u32 (174500);
  240. b.utf16 (0xC2, u"Título");
  241. b.u8 (0x41); b.u16 (3);
  242. b.u8 (0x96); b.u32 (0x00A14547); // BGR in the file
  243. auto notes = noteRecord (96, 1, 48, 64, 100);
  244. auto n2 = noteRecord (0, 0, 0, 60, 128);
  245. notes.insert (notes.end(), n2.begin(), n2.end());
  246. b.data (0xE0, notes);
  247. b.u8 (0x40); b.u16 (0);
  248. b.u8 (0x15); b.u8 (0);
  249. b.utf16 (0xCB, u"Kick");
  250. {
  251. std::vector<std::uint8_t> levels (24, 0);
  252. const std::int32_t lv[] = { 3200, 12800, -150 };
  253. std::memcpy (levels.data(), lv, sizeof (lv));
  254. b.data (0xDB, levels);
  255. }
  256. b.u8 (0x68); b.u16 (5);
  257. b.u8 (0x40); b.u16 (1);
  258. b.u8 (0x15); b.u8 (2);
  259. b.utf16 (0xC9, u"FLEX");
  260. b.utf16 (0xCB, u"Lead");
  261. b.u8 (0x63); b.u16 (0);
  262. std::vector<std::uint8_t> item (80, 0);
  263. std::uint32_t pos = 384, len = 768;
  264. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  265. std::memcpy (&item[0], &pos, 4);
  266. std::memcpy (&item[4], &base, 2);
  267. std::memcpy (&item[6], &idx, 2);
  268. std::memcpy (&item[8], &len, 4);
  269. std::memcpy (&item[12], &rv, 2);
  270. b.data (0xE9, item);
  271. b.utf16 (0xCC, u"Drums");
  272. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  273. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  274. const auto p = flp::parseProject (b.bytes);
  275. CHECK (p.version == "25.2.5.5319");
  276. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  277. CHECK (p.title == "Título");
  278. CHECK (p.channels.size() == 2);
  279. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  280. CHECK (p.channels[0].hasLevels && p.channels[0].pan == 3200 && p.channels[0].volume == 12800 && p.channels[0].pitchCents == -150);
  281. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  282. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  283. CHECK (p.patterns[0].colour && *p.patterns[0].colour == 0x4745A1);
  284. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // sorted
  285. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  286. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  287. const auto& it = p.arrangements[0].items[0];
  288. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  289. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  290. CHECK (p.warnings.empty());
  291. }
  292. TEST (flp_fuzz_corrupted_files_do_not_crash)
  293. {
  294. if (realFlpDir.empty())
  295. return;
  296. // some real .flp files, corrupted in various ways: the parser may reject them (flp::Error), but
  297. // must never hang, read past the buffer or produce a Song with invalid indices
  298. std::vector<std::vector<std::uint8_t>> sources;
  299. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  300. if (entry.path().extension() == ".flp" && sources.size() < 6 && std::filesystem::file_size (entry.path()) < 3'000'000)
  301. {
  302. std::ifstream in (entry.path(), std::ios::binary);
  303. sources.emplace_back ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  304. }
  305. std::uint32_t seed = 2024;
  306. auto rnd = [&] (std::uint32_t n) { seed = seed * 1664525u + 1013904223u; return n == 0 ? 0u : (seed >> 8) % n; };
  307. int parsed = 0, rejected = 0;
  308. for (int round = 0; round < 240 && ! sources.empty(); ++round)
  309. {
  310. auto bytes = sources[(std::size_t) rnd ((std::uint32_t) sources.size())];
  311. switch (round % 4)
  312. {
  313. case 0: bytes.resize (rnd ((std::uint32_t) bytes.size())); break; // truncated
  314. 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;
  315. case 2: // absurd event size
  316. for (int k = 0; k < 5; ++k)
  317. bytes[(std::size_t) rnd ((std::uint32_t) bytes.size())] = 0xFF;
  318. break;
  319. default: // shuffled block
  320. {
  321. const auto a = rnd ((std::uint32_t) bytes.size()), b = rnd ((std::uint32_t) bytes.size());
  322. std::reverse (bytes.begin() + std::min (a, b), bytes.begin() + std::max (a, b));
  323. break;
  324. }
  325. }
  326. try
  327. {
  328. const auto project = flp::parseProject (bytes);
  329. const auto song = lynx::importFlp (project);
  330. ++parsed;
  331. for (const auto& p : song.patterns)
  332. for (const auto& n : p.notes)
  333. CHECK (n.channel >= 0 && n.channel < (int) song.channels.size());
  334. for (const auto& c : song.playlist)
  335. CHECK (c.kind != lynx::ClipKind::pattern || (c.pattern >= 0 && c.pattern < (int) song.patterns.size()));
  336. }
  337. catch (const flp::Error&)
  338. {
  339. ++rejected;
  340. }
  341. }
  342. std::printf (" fuzz: %d imported, %d rejected\n", parsed, rejected);
  343. CHECK (parsed + rejected == 240);
  344. }
  345. TEST (plugin_state_decoders_survive_garbage)
  346. {
  347. // random blobs (varied sizes, including valid headers followed by garbage)
  348. std::uint32_t seed = 77;
  349. auto rnd = [&] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  350. const char* plugins[] = { "Fruity Parametric EQ 2", "Fruity Limiter", "Fruity Reeverb 2", "Fruity Delay 2", "Fruity Delay 3",
  351. "Fruity Compressor", "Fruity Balance", "Fruity PanOMatic", "Fruity Stereo Shaper", "Fruity Phase Inverter" };
  352. int decoded = 0;
  353. for (int round = 0; round < 3000; ++round)
  354. {
  355. std::vector<std::uint8_t> blob ((std::size_t) (rnd() % 600));
  356. for (auto& b : blob)
  357. b = (std::uint8_t) rnd();
  358. if (blob.size() >= 4 && round % 3 == 0) // versions the readers accept
  359. {
  360. const std::uint32_t versions[] = { 8, 10001, 10000, 12, 14, 6, 1 };
  361. const auto v = versions[rnd() % 7];
  362. std::memcpy (blob.data(), &v, 4);
  363. }
  364. if (auto fx = lynx::effectFromFl (plugins[rnd() % 10], blob))
  365. {
  366. ++decoded;
  367. for (auto v : fx->params)
  368. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  369. }
  370. lynx::SynthSettings synth;
  371. if (lynx::synthFrom3xOsc (blob, synth))
  372. for (const auto& o : synth.osc)
  373. CHECK (std::isfinite (o.level) && o.level >= 0.0f && o.level <= 1.0f);
  374. float harmlessGain = 1.0f;
  375. if (lynx::synthFromHarmless (blob, 120.0, synth, harmlessGain))
  376. CHECK (std::isfinite (harmlessGain) && std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  377. if (lynx::synthFromFruitKick (blob, synth))
  378. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.pitchEnv.amount));
  379. if (lynx::synthFromGms (blob, 120.0, synth, harmlessGain) || lynx::synthFromHarmor (blob, synth, harmlessGain) || lynx::synthFromOgun (blob, synth, harmlessGain)
  380. || lynx::synthFromSakura (blob, synth) || lynx::synthFromKepler (blob, synth, harmlessGain))
  381. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack) && std::isfinite (harmlessGain));
  382. if (auto sound = lynx::drumSynthLiveSound (blob))
  383. CHECK (sound->size() < 256);
  384. if (auto wave = lynx::waveTravellerSample (blob))
  385. CHECK (! wave->first.empty() && wave->second >= 0 && wave->second <= 127);
  386. lynx::synthFromBooBass (blob, synth);
  387. lynx::gmsStateUsable (blob); // (the exact engines' parsers reject garbage without crashing)
  388. lynx::morphineStateUsable (blob);
  389. lynx::poizoneStateUsable (blob);
  390. if (lynx::synthFromPoiZone (blob, synth, harmlessGain) || lynx::synthFromBassDrum (blob, synth, harmlessGain) || lynx::synthFromToxic (blob, synth, harmlessGain)
  391. || lynx::synthFromMorphine (blob, synth, harmlessGain))
  392. CHECK (std::isfinite (synth.cutoff) && std::isfinite (harmlessGain));
  393. if (auto patch = lynx::drumpadPatchName (blob))
  394. CHECK (patch->size() <= 0x30 && std::isfinite (lynx::drumToneForName (*patch).decay));
  395. if (auto flex = lynx::flexPresetName (blob))
  396. CHECK ((! flex->first.empty() || ! flex->second.empty()) && flex->second.size() <= 1024);
  397. if (lynx::synthFromSytrus (blob, 120.0, synth, harmlessGain))
  398. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.attack));
  399. if (lynx::synthFromDX10 (blob, synth))
  400. CHECK (std::isfinite (synth.decay) && std::isfinite (synth.release) && std::isfinite (synth.osc[1].level));
  401. if (blob.size() >= 4 && round % 5 == 0) // MiniSynth header followed by garbage
  402. {
  403. std::memcpy (blob.data(), "SSLF", 4);
  404. if (blob.size() > 220)
  405. {
  406. std::memcpy (blob.data() + 40, "DATA", 4);
  407. std::memcpy (blob.data() + 48, "0rev", 4);
  408. }
  409. }
  410. if (lynx::synthFromMiniSynth (blob, 120.0, synth))
  411. CHECK (std::isfinite (synth.cutoff) && std::isfinite (synth.decay) && std::isfinite (synth.attack));
  412. if (lynx::miniSynthStateUsable (blob)) // the exact engine's parser and a short render with silent tables
  413. {
  414. const auto params = lynx::miniSynthParams (blob);
  415. CHECK (params.size() == 43u && std::all_of (params.begin(), params.end(), [] (float v) { return v >= 0.0f && v <= 1.0f; }));
  416. const auto y = lynx::minisynth::renderReference (blob, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 64 } }, 44100.0, 128, "");
  417. CHECK (y.size() == 256u && std::all_of (y.begin(), y.end(), [] (float v) { return std::isfinite (v); }));
  418. }
  419. lynx::ForeignPlugin foreign;
  420. lynx::parseFlWrapper (blob, foreign);
  421. std::vector<std::uint8_t> component, controller;
  422. lynx::splitFlVst3State (blob, component, controller);
  423. }
  424. CHECK (decoded == 3000); // every known name yields an effect (default when the blob is unusable)
  425. }
  426. TEST (inflate_zlib)
  427. {
  428. // fixed Huffman with back-references
  429. const std::uint8_t fixed[] = { 0x78, 0xda, 0x4b, 0x4c, 0x4a, 0x4e, 0xc4, 0x86, 0x22, 0x00, 0x7c, 0x69, 0x09, 0x89 };
  430. const auto a = lynx::inflateZlib (fixed, sizeof (fixed));
  431. CHECK (a && std::string (a->begin(), a->end()) == "abcabcabcabcabcabcabcabcX");
  432. // stored (uncompressed) block
  433. const std::uint8_t stored[] = { 0x78, 0x01, 0x01, 0x03, 0x00, 0xfc, 0xff, 'L', 'Y', 'N', 0x00, 0x00, 0x00, 0x00 };
  434. const auto b = lynx::inflateZlib (stored, sizeof (stored));
  435. CHECK (b && std::string (b->begin(), b->end()) == "LYN");
  436. // truncated, bad header and size limit
  437. CHECK (! lynx::inflateZlib (fixed, 6));
  438. const std::uint8_t bad[] = { 0x12, 0x34, 0x00 };
  439. CHECK (! lynx::inflateZlib (bad, sizeof (bad)));
  440. CHECK (! lynx::inflateZlib (fixed, sizeof (fixed), false, 10));
  441. }
  442. TEST (fl_round15_effects_convert_known_values)
  443. {
  444. auto blob = [] (std::vector<std::int32_t> words) {
  445. std::vector<std::uint8_t> b (words.size() * 4);
  446. std::memcpy (b.data(), words.data(), b.size());
  447. return b;
  448. };
  449. // Frequency Shifter "+800 Hz": frequency 51542 in the 20 kHz range, 100% wet
  450. const auto fs = lynx::effectFromFl ("Frequency Shifter", blob ({ 4, 10000, 0, 51542, 0, 10000, 10000, 5000, 10000, 0, 5000, 0, 0 }));
  451. CHECK (fs && fs->kind == lynx::EffectKind::freqShift);
  452. if (fs)
  453. {
  454. const auto x = (fs->params[0] - 0.5) * 2.0;
  455. CHECK (std::abs (2000.0 * x * std::abs (x) - 800.0) < 10.0);
  456. CHECK (std::abs (fs->params[1] - 1.0f) < 1e-4f);
  457. }
  458. // Emphasis: input 0.0625 (= +1.5 dB) and ceiling 0.9308 (≈ −1.6 dB) -> limiter
  459. const auto em = lynx::effectFromFl ("Emphasis", blob ({ 8, 0, 0, 2, 1, 0, 0, 625, 10000, 10000, 9308, 0, 2, 7, 0 }));
  460. CHECK (em && em->kind == lynx::EffectKind::limiter && em->enabled);
  461. if (em)
  462. {
  463. CHECK (std::abs (em->params[1] * 24.0f - 1.5f) < 0.05f);
  464. CHECK (std::abs (em->params[0] * 12.0f - 12.0f + 1.6f) < 0.1f);
  465. }
  466. // Pitch Shifter +12 st, mix 100%
  467. 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 }));
  468. CHECK (ps && ps->kind == lynx::EffectKind::flPitchShifter && ps->params[0] == 1.0f && ps->params[3] == 1.0f); // REA_rodada61
  469. }
  470. TEST (frequency_shifter_moves_spectrum_by_hz)
  471. {
  472. auto fx = lynx::makeEffect (lynx::EffectKind::freqShift);
  473. fx->prepare (48000.0);
  474. auto slot = lynx::makeEffectSlot (lynx::EffectKind::freqShift);
  475. // +200 Hz: x·|x|·2000 = 200 -> x = sqrt(0.1)
  476. slot.params[0] = (float) (0.5 + std::sqrt (0.1) / 2.0);
  477. std::vector<float> l (48000), r (48000);
  478. for (int i = 0; i < 48000; ++i)
  479. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 1000.0 * i / 48000.0);
  480. for (int done = 0; done < 48000; done += 480)
  481. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  482. auto energy = [&] (double f) {
  483. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  484. double s1 = 0.0, s2 = 0.0;
  485. for (int i = 9600; i < 48000; ++i)
  486. {
  487. const auto s0 = l[(std::size_t) i] + k * s1 - s2;
  488. s2 = s1;
  489. s1 = s0;
  490. }
  491. return s1 * s1 + s2 * s2 - k * s1 * s2;
  492. };
  493. // the energy moves to 1200 Hz; little remains at 1000 and almost nothing at the image (800)
  494. CHECK (energy (1200.0) > 50.0 * energy (1000.0));
  495. CHECK (energy (1200.0) > 50.0 * energy (800.0));
  496. }
  497. TEST (pitch_shift_moves_frequency_keeps_length)
  498. {
  499. auto fx = lynx::makeEffect (lynx::EffectKind::pitchShift);
  500. fx->prepare (48000.0);
  501. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchShift);
  502. auto crossingsAfter = [&] (float semitoneParam) {
  503. fx->reset();
  504. slot.params[0] = semitoneParam;
  505. std::vector<float> l (48000), r (48000);
  506. for (int i = 0; i < 48000; ++i)
  507. l[(std::size_t) i] = r[(std::size_t) i] = (float) std::sin (2.0 * 3.141592653589793 * 220.0 * i / 48000.0);
  508. for (int done = 0; done < 48000; done += 512)
  509. fx->process (l.data() + done, r.data() + done, std::min (512, 48000 - done), slot.params, 120.0);
  510. int crossings = 0;
  511. for (int i = 9600; i < 48000; ++i)
  512. crossings += (l[(std::size_t) i - 1] < 0.0f) != (l[(std::size_t) i] < 0.0f);
  513. for (auto v : l)
  514. CHECK (std::isfinite (v) && std::abs (v) < 1.6f);
  515. return crossings / 0.8 / 2.0; // cycles per second
  516. };
  517. CHECK (std::abs (crossingsAfter (0.5f) - 220.0) < 8.0); // 0 st
  518. CHECK (std::abs (crossingsAfter (0.5f + 12.0f / 48.0f) - 440.0) < 25.0); // +12 st
  519. CHECK (std::abs (crossingsAfter (0.5f - 12.0f / 48.0f) - 110.0) < 12.0); // −12 st
  520. }
  521. // A Gross Beat state in FL's layout (REA_rodada53 §1), built here: version 8, parameters, options, 72 slots with a name,
  522. // sync bytes and a map envelope (kind 3, version 3, points: Δx in beats, level, tension, curve type)
  523. namespace
  524. {
  525. struct GbPoint
  526. {
  527. double dx, y;
  528. std::uint8_t type;
  529. };
  530. std::vector<std::uint8_t> grossBeatState (const std::array<int, 9>& params, const std::array<std::uint8_t, 9>& options,
  531. const std::map<int, std::vector<GbPoint>>& slots)
  532. {
  533. std::vector<std::uint8_t> b;
  534. 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); };
  535. auto i32 = [&put] (std::int32_t v) { put (&v, 4); };
  536. i32 (0x00080003);
  537. for (auto v : params)
  538. i32 (v);
  539. put (options.data(), options.size());
  540. for (int k = 0; k < 72; ++k)
  541. {
  542. b.push_back (0); // empty name
  543. i32 (0), i32 (5), i32 (0); // trigger sync off, position sync 4 beats, no hold
  544. b.push_back (0), b.push_back (0), b.push_back (0);
  545. const auto it = slots.find (k);
  546. const std::vector<GbPoint> flat { { 0.0, 1.0, 0 }, { 4.0, 1.0, 0 } };
  547. const auto& pts = it != slots.end() ? it->second : flat;
  548. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  549. for (const auto& p : pts)
  550. {
  551. put (&p.dx, 8), put (&p.y, 8);
  552. const float tension = 0.0f;
  553. put (&tension, 4);
  554. b.push_back (p.type), b.push_back (0), b.push_back (0), b.push_back (2);
  555. }
  556. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  557. }
  558. return b;
  559. }
  560. std::vector<float> renderGrossBeat (const lynx::EffectSlot& slot, const std::vector<float>& in, double rate, double bpm)
  561. {
  562. auto fx = lynx::makeEffect (slot);
  563. fx->prepare (rate);
  564. auto l = in, r = in;
  565. const auto tps = bpm / 60.0 * lynx::ppq / rate;
  566. for (std::size_t done = 0; done < l.size(); done += 512)
  567. {
  568. const auto n = (int) std::min<std::size_t> (512, l.size() - done);
  569. fx->setSongPosition ((double) done * tps);
  570. fx->process (l.data() + done, r.data() + done, n, slot.params, bpm);
  571. }
  572. return l;
  573. }
  574. } // namespace
  575. TEST (gross_beat_state_container_and_parameters)
  576. {
  577. const auto state = grossBeatState ({ 3, 40, 128, 256, 1765, 5000, -77, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, {});
  578. const auto parsed = lynx::grossbeat::parseState (state.data(), state.size());
  579. CHECK (parsed.has_value());
  580. CHECK (parsed && parsed->version == 8 && parsed->params[6] == -77 && parsed->slots[0].flat && parsed->slots[71].flat);
  581. const auto slot = lynx::grossbeat::slotFromFl (state, 960);
  582. CHECK (slot && slot->kind == lynx::EffectKind::grossBeat);
  583. std::vector<std::uint8_t> fl;
  584. int ppq = 0;
  585. CHECK (slot && lynx::grossbeat::unpack (slot->state, fl, ppq) && fl == state && ppq == 960);
  586. // every raw FL value survives the 0..1 parameter
  587. const int lo[8] { 0, 0, 0, 0, 0, 0, -256, 0 }, hi[8] { 35, 35, 256, 256, 10000, 10000, 256, 1 };
  588. bool roundTrip = true;
  589. for (int i = 0; i < 8; ++i)
  590. for (int raw = lo[i]; raw <= hi[i]; ++raw)
  591. roundTrip = roundTrip && lynx::grossbeat::rawFromParam (i, lynx::grossbeat::paramFromRaw (i, raw)) == raw;
  592. CHECK (roundTrip);
  593. CHECK (slot && slot->params[1] == lynx::grossbeat::paramFromRaw (1, 35)); // the volume slot 40 is clamped to 35
  594. CHECK (! lynx::grossbeat::parseState (state.data(), 100).has_value());
  595. }
  596. TEST (gross_beat_volume_slot_gates_the_bar)
  597. {
  598. // volume slot 1: open for two beats, closed for two (hold segments); no smoothing; dry path 15 samples late
  599. 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 } };
  600. const auto state = grossBeatState ({ 0, 1, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 37, gate } });
  601. const auto slot = lynx::grossbeat::slotFromFl (state);
  602. CHECK (slot.has_value());
  603. if (! slot)
  604. return;
  605. const auto out = renderGrossBeat (*slot, std::vector<float> (200000, 1.0f), 48000.0, 120.0); // a beat = 24000 samples
  606. CHECK (out[14] == 0.0f && out[15] == 1.0f);
  607. CHECK (out[30000] == 1.0f && out[47990] == 1.0f);
  608. CHECK (out[48010] == 0.0f && out[95990] == 0.0f);
  609. CHECK (out[96010] == 1.0f && out[150000] == 0.0f);
  610. }
  611. TEST (gross_beat_time_slot_reads_the_past_bar)
  612. {
  613. // time slot 1 at level 0.5: the buffer is read one bar back (two bars = level 0), plus the 15-sample latency
  614. const std::vector<GbPoint> half { { 0.0, 0.5, 0 }, { 4.0, 0.5, 2 } };
  615. const auto state = grossBeatState ({ 1, 0, 256, 256, 0, 0, 0, 0, 0x10000 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1 }, { { 1, half } });
  616. const auto slot = lynx::grossbeat::slotFromFl (state);
  617. CHECK (slot.has_value());
  618. if (! slot)
  619. return;
  620. std::vector<float> in (150000, 0.0f);
  621. in[1000] = 1.0f;
  622. const auto out = renderGrossBeat (*slot, in, 48000.0, 120.0); // a bar = 96000 samples
  623. std::size_t peak = 0;
  624. for (std::size_t i = 0; i < out.size(); ++i)
  625. if (std::abs (out[i]) > std::abs (out[peak]))
  626. peak = i;
  627. CHECK (peak == 1000 + 96000 + 15);
  628. CHECK (std::abs (out[peak] - 1.0f) < 1e-6f);
  629. float elsewhere = 0.0f;
  630. for (std::size_t i = 0; i < out.size(); ++i)
  631. if (i + 20 < peak || i > peak + 20)
  632. elsewhere = std::max (elsewhere, std::abs (out[i]));
  633. CHECK (elsewhere == 0.0f);
  634. }
  635. TEST (volume_gate_follows_bar_position)
  636. {
  637. auto fx = lynx::makeEffect (lynx::EffectKind::volumeGate);
  638. fx->prepare (48000.0);
  639. auto slot = lynx::makeEffectSlot (lynx::EffectKind::volumeGate);
  640. for (int i = 0; i < 32; ++i)
  641. slot.params[(std::size_t) i] = i < 16 ? 1.0f : 0.0f; // first two beats open, two closed
  642. slot.params[33] = slot.params[34] = 0.0f; // no smoothing
  643. // 120 BPM: one beat = 0.5 s = 24000 samples; the 4-beat bar starts at tick 0
  644. std::vector<float> l (96000, 1.0f), r (96000, 1.0f);
  645. fx->setSongPosition (0.0);
  646. for (int done = 0; done < 96000; done += 512)
  647. fx->process (l.data() + done, r.data() + done, std::min (512, 96000 - done), slot.params, 120.0);
  648. CHECK (l[10000] > 0.99f && l[40000] > 0.99f); // beats 1 and 2
  649. CHECK (l[60000] < 0.01f && l[90000] < 0.01f); // beats 3 and 4
  650. // starting mid-bar (beat 3): already closed
  651. std::vector<float> l2 (4800, 1.0f), r2 (4800, 1.0f);
  652. fx->reset();
  653. fx->setSongPosition (2.0 * lynx::ppq);
  654. fx->process (l2.data(), r2.data(), 4800, slot.params, 120.0);
  655. CHECK (l2[4000] < 0.01f);
  656. // mix 0: transparent
  657. slot.params[32] = 0.0f;
  658. std::vector<float> l3 (4800, 1.0f), r3 (4800, 1.0f);
  659. fx->setSongPosition (2.0 * lynx::ppq);
  660. fx->process (l3.data(), r3.data(), 4800, slot.params, 120.0);
  661. CHECK (l3[4000] > 0.99f);
  662. }
  663. TEST (patcher_branch_heads_follow_rea)
  664. {
  665. auto words = [] (std::vector<std::int32_t> v) {
  666. std::vector<std::uint8_t> b (v.size() * 4);
  667. std::memcpy (b.data(), v.data(), b.size());
  668. return b;
  669. };
  670. // Frequency Splitter, 2 bands, destinations 1/2/3, slope combo 3 (24 dB/oct), high send −6 dB
  671. std::vector<std::int32_t> fs (18, 0);
  672. fs[0] = 4;
  673. fs[1] = 32768;
  674. fs[3] = 3;
  675. fs[6] = 0;
  676. fs[11] = -600;
  677. fs[15] = 1;
  678. fs[16] = 2;
  679. fs[17] = 3;
  680. const auto split = words (fs);
  681. const auto low = lynx::patcherBranchHead ("Frequency Splitter", split, 1);
  682. CHECK (low.valid && low.slot && low.note.empty());
  683. if (low.slot)
  684. {
  685. // low band = just the Crossover low-pass, combo 3 (24 dB/oct)
  686. CHECK (low.slot->kind == lynx::EffectKind::crossover && low.slot->params[0] == 0.0f && low.slot->params[3] == 1.0f);
  687. CHECK (std::lround (low.slot->params[5] * 7.0f) == 3);
  688. }
  689. const auto mid = lynx::patcherBranchHead ("Frequency Splitter", split, 2);
  690. CHECK (mid.valid && mid.gain == 0.0f); // with 2 bands the mid is silent
  691. const auto high = lynx::patcherBranchHead ("Frequency Splitter", split, 3);
  692. CHECK (high.valid && std::abs (high.gain - std::pow (10.0f, -6.0f / 20.0f)) < 1e-4f);
  693. CHECK (! lynx::patcherBranchHead ("Frequency Splitter", split, 7).valid);
  694. // Stereo Shaper: the difference destination (4) is the exact engine with its difference output (REA_rodada54)
  695. const auto shaper = words ({ 3, 0, 12800, 12800, 0, 0, 0, 4, 0 });
  696. const auto diff = lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 4);
  697. CHECK (diff.valid && diff.slot && diff.slot->kind == lynx::EffectKind::stereoShaper && diff.slot->params[7] == 1.0f);
  698. CHECK (! lynx::patcherBranchHead ("Fruity Stereo Shaper", shaper, 2).valid);
  699. }
  700. TEST (parallel_container_sums_branches)
  701. {
  702. // two branches: dry with gain 0.5 and a utility that flips phase (matrix −1) with gain 0.5 -> silence
  703. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  704. lynx::EffectBranch dry;
  705. dry.gain = 0.5f;
  706. lynx::EffectBranch inverted;
  707. inverted.gain = 0.5f;
  708. auto flip = lynx::makeEffectSlot (lynx::EffectKind::utility);
  709. flip.params[2] = flip.params[5] = 0.25f; // (−1 + 2)/4
  710. inverted.chain.push_back (flip);
  711. slot.branches = { dry, inverted };
  712. auto fx = lynx::makeParallelEffect (slot);
  713. fx->prepare (48000.0);
  714. std::vector<float> l (9000, 0.8f), r (9000, -0.4f);
  715. fx->process (l.data(), r.data(), 9000, slot.params, 120.0);
  716. CHECK (std::abs (l[100]) < 1e-5f && std::abs (r[8999]) < 1e-5f);
  717. // only the dry branch with gain 2: doubles
  718. slot.branches = { lynx::EffectBranch { {}, 2.0f } };
  719. auto doubler = lynx::makeParallelEffect (slot);
  720. doubler->prepare (48000.0);
  721. std::fill (l.begin(), l.end(), 0.25f);
  722. doubler->process (l.data(), r.data(), 9000, slot.params, 120.0);
  723. CHECK (std::abs (l[5000] - 0.5f) < 1e-6f);
  724. }
  725. TEST (fl_controller_constant_outputs)
  726. {
  727. std::vector<std::int32_t> xy { 2, 32768, 0, 65536, 0, 16384, 0, 65536, 0, 1024, 256 };
  728. std::vector<std::uint8_t> blob (xy.size() * 4 + 8, 0);
  729. std::memcpy (blob.data(), xy.data(), xy.size() * 4);
  730. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 0).value_or (-1.0f) - 0.5f) < 1e-4f);
  731. CHECK (std::abs (lynx::controllerConstantOutput ("Fruity X-Y Controller", blob, 1).value_or (-1.0f) - 0.25f) < 1e-4f);
  732. // Control Surface: version + group { 0x836: value 0.75, -, output 2 }
  733. std::vector<std::uint8_t> cs (4, 0);
  734. cs[0] = 1;
  735. auto chunk = [] (std::vector<std::uint8_t>& out, std::uint32_t id, const std::vector<std::uint8_t>& body) {
  736. const auto size = (std::uint64_t) body.size();
  737. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  738. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  739. out.insert (out.end(), body.begin(), body.end());
  740. };
  741. std::vector<std::uint8_t> value (12, 0);
  742. const float v = 0.75f;
  743. const std::int32_t out = 2;
  744. std::memcpy (value.data(), &v, 4);
  745. std::memcpy (value.data() + 8, &out, 4);
  746. std::vector<std::uint8_t> group;
  747. chunk (group, 0x836, value);
  748. chunk (cs, 0, group);
  749. CHECK (std::abs (lynx::controllerConstantOutput ("Control Surface", cs, 2).value_or (-1.0f) - 0.75f) < 1e-6f);
  750. CHECK (! lynx::controllerConstantOutput ("Control Surface", cs, 0).has_value());
  751. }
  752. TEST (flp_midi_out_notes_reach_wrapper_on_same_port)
  753. {
  754. auto record = [] (std::vector<std::uint8_t>& out, std::uint32_t id, std::vector<std::uint8_t> body) {
  755. const auto size = (std::uint64_t) body.size();
  756. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&id), reinterpret_cast<const std::uint8_t*> (&id) + 4);
  757. out.insert (out.end(), reinterpret_cast<const std::uint8_t*> (&size), reinterpret_cast<const std::uint8_t*> (&size) + 8);
  758. out.insert (out.end(), body.begin(), body.end());
  759. };
  760. auto i32s = [] (std::vector<std::int32_t> v) {
  761. std::vector<std::uint8_t> b (v.size() * 4);
  762. std::memcpy (b.data(), v.data(), b.size());
  763. return b;
  764. };
  765. flp::Project p;
  766. flp::Channel midiOut;
  767. midiOut.index = 0;
  768. midiOut.type = flp::ChannelType::native;
  769. midiOut.pluginName = "MIDI Out";
  770. midiOut.pluginState = i32s ({ 7, 0, -1, -1, -1, 0, 3, 0 }); // channel 1, port 3 (@24)
  771. flp::Channel wrapper;
  772. wrapper.index = 1;
  773. wrapper.type = flp::ChannelType::native;
  774. wrapper.pluginName = "Fruity Wrapper";
  775. wrapper.pluginState = i32s ({ 9 });
  776. record (wrapper.pluginState, 0x32, i32s ({ 7 })); // VST3
  777. record (wrapper.pluginState, 0x01, i32s ({ 3, -1, 0, 0, 0 })); // input port 3
  778. p.channels = { midiOut, wrapper };
  779. flp::Pattern pattern;
  780. pattern.index = 1;
  781. flp::Note note;
  782. note.channel = 0;
  783. note.key = 64;
  784. note.length = 96;
  785. pattern.notes.push_back (note);
  786. p.patterns.push_back (pattern);
  787. const auto song = lynx::importFlp (p);
  788. CHECK (song.channels.size() == 2 && song.channels[0].muted);
  789. CHECK (song.channels[1].plugin.foreign.midiInPort == 3);
  790. int toWrapper = 0;
  791. for (const auto& pat : song.patterns)
  792. for (const auto& n : pat.notes)
  793. toWrapper += n.channel == 1 && n.key == 64;
  794. CHECK (toWrapper == 1);
  795. }
  796. TEST (flp_effect_automation_follows_slots_and_params)
  797. {
  798. // track 1: slot 0 with an effect that has no equivalent (dropped), slot 2 with Parametric EQ 2
  799. flp::Project p;
  800. flp::MixerInsert insert;
  801. insert.index = 1;
  802. insert.slots.push_back ({ 0, "Some Unknown FX", {} });
  803. insert.slots.push_back ({ 2, "Fruity Parametric EQ 2", {} });
  804. p.mixer.push_back (insert);
  805. auto clip = [&] (int index, std::uint32_t target) {
  806. flp::Channel c;
  807. c.index = index;
  808. c.type = flp::ChannelType::automationClip;
  809. c.automation = { { 0.0, 0.0 }, { 96.0, 1.0 } };
  810. p.channels.push_back (c);
  811. p.automationLinks.push_back ({ index, { target } });
  812. };
  813. 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; };
  814. clip (0, mixer (1, 2, 0x8000 + 7)); // EQ band 1 frequency
  815. clip (1, mixer (1, 2, 0x1F01)); // EQ slot mix
  816. clip (2, mixer (1, 0, 0x8000)); // parameter of the dropped effect
  817. clip (3, mixer (1, 2, 0x8000 + 21)); // band type: no equivalent
  818. const auto song = lynx::importFlp (p);
  819. CHECK (song.automations.size() == 4);
  820. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::parametricEq);
  821. const auto& freq = song.automations[0];
  822. CHECK (freq.target.kind == lynx::AutomationTargetKind::effectParam && freq.target.index == 1 && freq.target.slot == 0);
  823. CHECK (freq.target.param == lynx::peq::param (0, 1));
  824. // FL's 0..1 (20 Hz..20 kHz) becomes our frequency scale
  825. CHECK (std::abs (freq.points.front().value - lynx::peq::hzValue (20.0)) < 1e-4f);
  826. CHECK (std::abs (freq.points.back().value - lynx::peq::hzValue (20000.0)) < 1e-4f);
  827. CHECK (song.automations[1].target.kind == lynx::AutomationTargetKind::effectMix && song.automations[1].target.slot == 0);
  828. CHECK (song.automations[2].target.kind == lynx::AutomationTargetKind::none);
  829. CHECK (song.automations[3].target.kind == lynx::AutomationTargetKind::none);
  830. // the automated mix is read/written through the target
  831. auto copy = song;
  832. CHECK (copy.setTargetValue (song.automations[1].target, 0.25f) && std::abs (copy.mixer[1].effects[0].mix - 0.25f) < 1e-6f);
  833. CHECK (std::abs (copy.targetValue (song.automations[1].target) - 0.25f) < 1e-6f);
  834. }
  835. TEST (flp_real_files)
  836. {
  837. if (realFlpDir.empty())
  838. {
  839. std::printf (" (skipped: pass a folder with .flp files as argument)\n");
  840. return;
  841. }
  842. int files = 0, withNotes = 0, withPlaylist = 0, automations = 0, automationsWithTarget = 0, sends = 0;
  843. int threeOsc = 0, threeOscDecoded = 0, wrapped = 0, wrappedParsed = 0, markers = 0, signatures = 0, trackEqs = 0;
  844. int harmless = 0, harmlessDecoded = 0, harmlessParsed = 0, harmlessKind = 0, sytrus = 0, sytrusDecoded = 0, sytrusEngine = 0, sytrusRendered = 0,
  845. sytrusSounding = 0;
  846. int harmor = 0, harmorUsable = 0, harmorKind = 0, harmorSounding = 0;
  847. int gms = 0, gmsUsable = 0, gmsKind = 0, gmsSounding = 0, morphine = 0, morphineUsable = 0, morphineKind = 0, morphineSounding = 0, demoGoldens = 0;
  848. int ogun = 0, ogunUsable = 0, ogunKind = 0, ogunSounding = 0;
  849. int autogun = 0, autogunUsable = 0, autogunKind = 0, autogunWithSg = 0, autogunSounding = 0; // REA_rodada57
  850. int poizone = 0, poizoneUsable = 0, poizoneKind = 0, poizoneSounding = 0, poizoneGoldens = 0;
  851. int miniSynth = 0, miniSynthUsable = 0, miniSynthKind = 0, miniSynthSounding = 0, miniSynthGoldens = 0;
  852. int dx10 = 0, dx10Usable = 0, dx10Kind = 0, dx10Sounding = 0;
  853. int kepler = 0, keplerUsable = 0, keplerKind = 0, keplerSounding = 0;
  854. int toxic = 0, toxicUsable = 0, toxicKind = 0, toxicSounding = 0, toxicGoldens = 0; // REA_rodada45
  855. int threeOscKind = 0, threeOscSounding = 0;
  856. std::map<std::string, std::pair<int, int>> generators; // GMS/Ogun/Autogun/Sakura/DrumSynth: channels, decoded
  857. int ogunTrumpet = 0; // Ogun channels whose spectrum matches the REA "Trumpet" reference
  858. int peqSlots = 0, peqShaped = 0, reverbs = 0, reverbsDryless = 0, reverbsLowCut = 0;
  859. int dwChannels = 0, dwZones = 0, fpcKits = 0, padChannels = 0, padNotes = 0;
  860. int harmlessCompWanted = 0, harmlessCompLoaded = 0; // Harmless rack compressors / presets read from the installation
  861. int maximusSlots = 0, maximusExact = 0, sgzSlots = 0, sgzExact = 0, vocodexSlots = 0, vocodexWithData = 0; // REA_rodada40
  862. int shaperSlots = 0, shaperExact = 0, clipperSlots = 0, clipperExact = 0; // REA_rodada51
  863. int flPeqSlots = 0, flPeqExact = 0; // REA_rodada50
  864. int basicSlots = 0, basicExact = 0; // REA_rodada58: PanOMatic, Balance, Blood Overdrive
  865. int equoSlots = 0, equoExact = 0, pitchShifterSlots = 0, pitchShifterExact = 0; // REA_rodada61
  866. int delay2Slots = 0, delay2Exact = 0, delay3Slots = 0, delay3Exact = 0; // REA_rodada52
  867. int chorusSlots = 0, chorusExact = 0; // REA_rodada59: the exact choruses
  868. int fastDistSlots = 0, fastDistExact = 0, flangusSlots = 0, flangusExact = 0, flangerSlots = 0, flangerExact = 0; // REA_rodada55
  869. // import with the installation's samples (realFlpDir is ...\Data): for DirectWave/FPC
  870. lynx::FlpImportOptions withFiles;
  871. const auto flRoot = std::filesystem::path (realFlpDir).parent_path().string();
  872. withFiles.roots = { flRoot, realFlpDir, "" };
  873. withFiles.fileExists = [] (const std::string& path) {
  874. auto local = path;
  875. std::replace (local.begin(), local.end(), '\\', '/');
  876. return std::filesystem::exists (local);
  877. };
  878. withFiles.readFile = [] (const std::string& path) {
  879. auto local = path;
  880. std::replace (local.begin(), local.end(), '\\', '/');
  881. std::ifstream in (local, std::ios::binary);
  882. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  883. };
  884. int embeddedSaved = 0;
  885. withFiles.saveEmbedded = [&embeddedSaved] (const std::vector<std::uint8_t>& wav, const std::string&) {
  886. ++embeddedSaved;
  887. return wav.size() > 44 ? std::string ("embutido.wav") : std::string();
  888. };
  889. int slicedChannels = 0, flKeysChannels = 0;
  890. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  891. {
  892. if (entry.path().extension() != ".flp")
  893. continue;
  894. std::ifstream in (entry.path(), std::ios::binary);
  895. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  896. ++files;
  897. try
  898. {
  899. const auto p = flp::parseProject (bytes);
  900. CHECK (! p.version.empty());
  901. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  902. const auto song = lynx::importFlp (p);
  903. CHECK (! song.patterns.empty());
  904. {
  905. const auto full = lynx::importFlp (p, withFiles);
  906. bool hasFpc = false;
  907. for (const auto& c : p.channels)
  908. hasFpc = hasFpc || c.pluginName == "FPC";
  909. // GMS and Morphine (REA_rodada39): every state plays on the exact engine (GMS with its waves from the
  910. // installation) and sounds; two demo channels match golden samples of our round-39 models
  911. for (const auto& c : p.channels)
  912. {
  913. if (c.pluginName == "GMS")
  914. ++gms, gmsUsable += lynx::gmsStateUsable (c.pluginState) ? 1 : 0;
  915. if (c.pluginName == "Morphine")
  916. ++morphine, morphineUsable += lynx::morphineStateUsable (c.pluginState) ? 1 : 0;
  917. }
  918. // Kepler: every state in the demos plays on the exact engine (REA_rodada46) when the engine DLL is installed
  919. for (const auto& c : p.channels)
  920. if (c.pluginName == "Kepler")
  921. {
  922. ++kepler;
  923. keplerUsable += lynx::keplerStateUsable (c.pluginState) ? 1 : 0;
  924. }
  925. for (const auto& c : full.channels)
  926. if (c.instrument == lynx::InstrumentKind::kepler)
  927. {
  928. ++keplerKind;
  929. 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 } },
  930. 44100.0, 11025);
  931. double e = 0.0;
  932. bool finite = ! y.empty();
  933. for (auto v : y)
  934. finite = finite && std::isfinite (v), e += (double) v * v;
  935. CHECK (finite);
  936. keplerSounding += e > 1e-6 ? 1 : 0;
  937. }
  938. const auto file = entry.path().filename().string();
  939. for (const auto& c : full.channels)
  940. {
  941. const auto isGms = c.instrument == lynx::InstrumentKind::gms, isMorphine = c.instrument == lynx::InstrumentKind::morphine;
  942. if (! isGms && ! isMorphine)
  943. continue;
  944. (isGms ? gmsKind : morphineKind)++;
  945. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  946. const auto golden = (isGms && c.name == "GMS" && file.find ("Song For You") != std::string::npos)
  947. || (isMorphine && c.name == "Morphine" && file.find ("SHINE") != std::string::npos);
  948. std::vector<float> y;
  949. if (isGms)
  950. y = lynx::gms::renderReference (c.pluginState,
  951. { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  952. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, (float) iv (127), 1.0, 0.0, 0.0, 70000 } },
  953. 44100.0, golden ? 88200 : 22050, c.samplePath);
  954. else
  955. 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 } },
  956. 44100.0, golden ? 100000 : 22050, c.samplePath);
  957. double e = 0.0;
  958. bool finite = ! y.empty();
  959. for (auto v : y)
  960. finite = finite && std::isfinite (v), e += (double) v * v;
  961. CHECK (finite);
  962. (isGms ? gmsSounding : morphineSounding) += e > 1e-6 ? 1 : 0;
  963. if (golden && ! y.empty())
  964. {
  965. // our model, same events (GMS: tempo 140; Morphine: tempo 120), state as saved, 44.1 kHz
  966. const auto rms = std::sqrt (e / (double) y.size());
  967. CHECK (std::abs (rms - (isGms ? 0.213602668574 : 0.0673606448152)) < 1e-5);
  968. 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 } },
  969. 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 } };
  970. for (const auto& g : isGms ? std::vector (std::begin (gmsAt), std::end (gmsAt)) : std::vector (std::begin (morAt), std::end (morAt)))
  971. 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);
  972. ++demoGoldens;
  973. }
  974. }
  975. // MiniSynth (REA_rodada41): every state plays on the exact engine (tables from the installation's
  976. // oscdata.dat) and sounds; two demo channels match golden samples of our round-41 model (bit-identical to
  977. // renders of the installation's DLL for these states)
  978. for (const auto& c : p.channels)
  979. if (c.pluginName == "MiniSynth")
  980. ++miniSynth, miniSynthUsable += lynx::miniSynthStateUsable (c.pluginState) ? 1 : 0;
  981. for (const auto& c : full.channels)
  982. {
  983. if (c.instrument != lynx::InstrumentKind::minisynth)
  984. continue;
  985. ++miniSynthKind;
  986. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  987. const auto exo = file.find ("Exoplanet") != std::string::npos;
  988. const auto golden = exo && (c.name == "Main Lead 1" || c.name == "Riser");
  989. const auto y = lynx::minisynth::renderReference (c.pluginState,
  990. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  991. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  992. 44100.0, golden ? 88200 : 22050, c.samplePath);
  993. double e = 0.0;
  994. bool finite = ! y.empty();
  995. for (auto v : y)
  996. finite = finite && std::isfinite (v), e += (double) v * v;
  997. CHECK (finite);
  998. miniSynthSounding += e > 1e-6 ? 1 : 0;
  999. if (golden && ! y.empty())
  1000. {
  1001. // our model, same events, tempo 120, state as saved, 44.1 kHz, host blocks of 512, L = R = Final.Vol
  1002. const auto lead = c.name == "Main Lead 1";
  1003. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (lead ? 0.177495801753 : 0.19006567413)) < 1e-9);
  1004. 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 } },
  1005. riserAt[] = { { 2000, -0.0603227727f }, { 15000, 0.112200871f }, { 30000, 0.0453962795f }, { 50000, 0.234380022f }, { 65000, -0.164545432f }, { 80000, -0.000485120923f } };
  1006. for (const auto& g : lead ? std::vector (std::begin (leadAt), std::end (leadAt)) : std::vector (std::begin (riserAt), std::end (riserAt)))
  1007. CHECK (y[2 * (std::size_t) g.i] == g.v && y[2 * (std::size_t) g.i + 1] == g.v);
  1008. ++miniSynthGoldens;
  1009. }
  1010. }
  1011. // PoiZone (REA_rodada44): every state plays on the exact engine and sounds; the demo channel matches golden
  1012. // samples of our round-44 model
  1013. for (const auto& c : p.channels)
  1014. if (c.pluginName == "PoiZone")
  1015. ++poizone, poizoneUsable += lynx::poizoneStateUsable (c.pluginState) ? 1 : 0;
  1016. for (const auto& c : full.channels)
  1017. {
  1018. if (c.instrument != lynx::InstrumentKind::poizone)
  1019. continue;
  1020. ++poizoneKind;
  1021. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1022. const auto golden = c.name == "PoiZone" && file.find ("NIGHT TERROR") != std::string::npos;
  1023. const auto y = lynx::poizone::renderReference (c.pluginState,
  1024. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1025. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1026. 44100.0, golden ? 88200 : 22050);
  1027. double e = 0.0;
  1028. bool finite = ! y.empty();
  1029. for (auto v : y)
  1030. finite = finite && std::isfinite (v), e += (double) v * v;
  1031. CHECK (finite);
  1032. poizoneSounding += e > 1e-6 ? 1 : 0;
  1033. if (golden && ! y.empty())
  1034. {
  1035. // our model, same events, tempo 120, state as saved, 44.1 kHz, L = R = Final.Vol
  1036. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.018975758213507415) < 1e-7);
  1037. 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 } };
  1038. for (const auto& g : at)
  1039. 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);
  1040. ++poizoneGoldens;
  1041. }
  1042. }
  1043. // Toxic Biohazard (REA_rodada45): every demo state plays on the exact engine with the waves read from the
  1044. // installation's DLL and sounds; the "Bombs Away" channel matches golden samples of our round-45 model
  1045. for (const auto& c : p.channels)
  1046. if (c.pluginName == "Toxic Biohazard")
  1047. ++toxic, toxicUsable += lynx::toxicStateUsable (c.pluginState) ? 1 : 0;
  1048. for (const auto& c : full.channels)
  1049. {
  1050. if (c.instrument != lynx::InstrumentKind::toxic)
  1051. continue;
  1052. ++toxicKind;
  1053. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  1054. const auto golden = file.find ("Bombs Away") != std::string::npos;
  1055. const auto y = lynx::toxic::renderReference (
  1056. c.pluginState,
  1057. { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 }, { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  1058. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 }, { 60000, 48, iv (127), 1.0, 0.0, 0.0, 70000 } },
  1059. 44100.0, golden ? 88200 : 22050, c.samplePath, 512, 140.0);
  1060. double e = 0.0;
  1061. bool finite = ! y.empty();
  1062. for (auto v : y)
  1063. finite = finite && std::isfinite (v), e += (double) v * v;
  1064. CHECK (finite);
  1065. toxicSounding += e > 1e-6 ? 1 : 0;
  1066. if (golden && ! y.empty())
  1067. {
  1068. // our model (r45/py/toxmodel.py, which equals the DLL bit for bit), state as saved, tempo 140, 44.1 kHz
  1069. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0832500924945) < 1e-6);
  1070. 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 } };
  1071. for (const auto& g : at)
  1072. 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);
  1073. ++toxicGoldens;
  1074. }
  1075. }
  1076. for (const auto& c : full.channels)
  1077. {
  1078. if (c.instrument == lynx::InstrumentKind::harmless && c.modelParams.size() >= (std::size_t) lynx::harmlessParamCount
  1079. && c.modelParams[(std::size_t) lynx::harmlessComboIndex + 3] > 0.5f)
  1080. {
  1081. ++harmlessCompWanted;
  1082. harmlessCompLoaded += c.modelParams.size() > (std::size_t) lynx::harmlessCompIndex + 1000 ? 1 : 0;
  1083. }
  1084. flKeysChannels += c.zones.size() == 30 && c.zones.front().path.find ("CHANGPIANO") != std::string::npos ? 1 : 0;
  1085. if (c.sampler.slices.size() >= 4)
  1086. {
  1087. ++slicedChannels;
  1088. CHECK (std::is_sorted (c.sampler.slices.begin(), c.sampler.slices.end()));
  1089. CHECK (c.sampler.slices.back() < 1.0f);
  1090. }
  1091. if (! c.zones.empty() && c.name.find (" - ") == std::string::npos)
  1092. {
  1093. ++dwChannels;
  1094. dwZones += (int) c.zones.size();
  1095. }
  1096. padChannels += c.name.find (" - ") != std::string::npos && c.instrument == lynx::InstrumentKind::sampler ? 1 : 0;
  1097. }
  1098. fpcKits += hasFpc;
  1099. // Maximus / Soundgoodizer become the exact engine (REA_rodada40); Vocodex carries SG.dat (and its
  1100. // carrier's Sytrus preset) read from the installation
  1101. const int basicSlots0 = basicSlots, basicExact0 = basicExact;
  1102. for (const auto& insert : p.mixer)
  1103. for (const auto& slot : insert.slots)
  1104. {
  1105. maximusSlots += slot.plugin == "Maximus";
  1106. delay2Slots += slot.plugin == "Fruity Delay 2";
  1107. delay3Slots += slot.plugin == "Fruity Delay 3";
  1108. sgzSlots += slot.plugin == "Soundgoodizer";
  1109. vocodexSlots += slot.plugin == "Vocodex";
  1110. shaperSlots += slot.plugin == "Fruity WaveShaper";
  1111. flPeqSlots += slot.plugin == "Fruity Parametric EQ 2" || slot.plugin == "Fruity Parametric EQ";
  1112. equoSlots += slot.plugin == "EQUO";
  1113. pitchShifterSlots += slot.plugin == "Pitch Shifter";
  1114. clipperSlots += slot.plugin == "Fruity Soft Clipper";
  1115. basicSlots += slot.plugin == "Fruity PanOMatic" || slot.plugin == "Fruity Balance" || slot.plugin == "Fruity Blood Overdrive";
  1116. chorusSlots += lynx::chorusControls (slot.plugin) > 0 && slot.state.size() >= 4 ? 1 : 0;
  1117. fastDistSlots += slot.plugin == "Fruity Fast Dist";
  1118. flangusSlots += slot.plugin == "Fruity Flangus";
  1119. flangerSlots += slot.plugin == "Fruity Flanger";
  1120. }
  1121. for (const auto& t : full.mixer)
  1122. for (const auto& fx : t.effects)
  1123. {
  1124. if (fx.kind == lynx::EffectKind::maximus)
  1125. {
  1126. const auto blob = lynx::maximusfx::unpack (fx.state);
  1127. CHECK (blob && lynx::maximusfx::presetOf (*blob) != nullptr);
  1128. if (blob)
  1129. (blob->build == lynx::hlfx::MaximusBuild::soundgoodizer ? sgzExact : maximusExact) += 1;
  1130. }
  1131. fastDistExact += fx.kind == lynx::EffectKind::fruityFastDist;
  1132. flangusExact += fx.kind == lynx::EffectKind::fruityFlangus;
  1133. flangerExact += fx.kind == lynx::EffectKind::fruityFlanger;
  1134. if (fx.kind == lynx::EffectKind::vocodex)
  1135. vocodexWithData += ! lynx::vocodex::splitInstallData (fx.state).second.sgDat.empty();
  1136. if (fx.kind == lynx::EffectKind::fruityWaveShaper)
  1137. shaperExact += lynx::flshape::parseWaveShaper (fx.state).has_value() ? 1 : 0;
  1138. clipperExact += fx.kind == lynx::EffectKind::fruitySoftClipper;
  1139. flPeqExact += fx.kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (fx.params);
  1140. basicExact += fx.kind == lynx::EffectKind::fruityPanOMatic || fx.kind == lynx::EffectKind::fruityBalance
  1141. || fx.kind == lynx::EffectKind::fruityBloodOverdrive;
  1142. equoExact += fx.kind == lynx::EffectKind::equo && lynx::equofx::parse (fx.state).has_value();
  1143. pitchShifterExact += fx.kind == lynx::EffectKind::flPitchShifter;
  1144. delay2Exact += fx.kind == lynx::EffectKind::fruityDelay2;
  1145. delay3Exact += fx.kind == lynx::EffectKind::fruityDelay3;
  1146. chorusExact += fx.kind == lynx::EffectKind::fruityChorus || fx.kind == lynx::EffectKind::hyperChorus || fx.kind == lynx::EffectKind::vintageChorus ? 1 : 0;
  1147. }
  1148. if (basicSlots - basicSlots0 != basicExact - basicExact0)
  1149. std::printf (" %s: %d PanOMatic/Balance/Blood Overdrive slots, %d exact effects\n", file.c_str(),
  1150. basicSlots - basicSlots0, basicExact - basicExact0);
  1151. for (const auto& pat : full.patterns)
  1152. for (const auto& n : pat.notes)
  1153. {
  1154. CHECK (n.channel >= 0 && n.channel < (int) full.channels.size());
  1155. padNotes += full.channels[(std::size_t) n.channel].name.find (" - ") != std::string::npos ? 1 : 0;
  1156. }
  1157. }
  1158. const auto peqDefaults = lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params;
  1159. for (const auto& t : song.mixer)
  1160. for (const auto& fx : t.effects)
  1161. {
  1162. if (fx.kind == lynx::EffectKind::parametricEq)
  1163. {
  1164. ++peqSlots;
  1165. peqShaped += fx.params != peqDefaults;
  1166. for (double hz : { 50.0, 1000.0, 10000.0 })
  1167. CHECK (std::isfinite (lynx::peq::responseDb (fx.params, 48000.0, hz)));
  1168. }
  1169. if (fx.kind == lynx::EffectKind::reverb)
  1170. {
  1171. ++reverbs;
  1172. reverbsDryless += fx.params[4] < 0.01f;
  1173. reverbsLowCut += fx.params[5] > 0.0f;
  1174. }
  1175. if (fx.kind == lynx::EffectKind::reeverb2) // FL's exact Reeverb 2 (REA_rodada49): FL's own controls
  1176. {
  1177. const auto r2 = lynx::reeverb2::fromParams (fx.params);
  1178. ++reverbs;
  1179. reverbsDryless += r2.p[10] == 0;
  1180. reverbsLowCut += r2.p[0] >= 20;
  1181. }
  1182. }
  1183. for (const auto& t : song.mixer)
  1184. if (t.eq.active())
  1185. {
  1186. ++trackEqs;
  1187. for (std::size_t b = 0; b < 3; ++b)
  1188. CHECK (t.eq.hz[b] >= 10.0f && t.eq.hz[b] <= 16000.0f && std::abs (t.eq.gainDb[b]) <= 18.0f);
  1189. }
  1190. for (const auto& a : song.arrangements)
  1191. for (const auto& m : a.markers)
  1192. {
  1193. ++markers;
  1194. signatures += m.isSignature();
  1195. CHECK (m.tick >= 0);
  1196. }
  1197. // Fruity Wrapper (channels and mixer slots): every blob in the demos is read
  1198. auto countWrapper = [&] (const std::string& plugin, const std::vector<std::uint8_t>& state) {
  1199. if (plugin != "Fruity Wrapper")
  1200. return;
  1201. ++wrapped;
  1202. lynx::ForeignPlugin f;
  1203. wrappedParsed += lynx::parseFlWrapper (state, f) && f.format != lynx::ForeignPlugin::Format::none && ! f.name.empty();
  1204. };
  1205. for (const auto& c : p.channels)
  1206. countWrapper (c.pluginName, c.pluginState);
  1207. for (const auto& insert : p.mixer)
  1208. for (const auto& slot : insert.slots)
  1209. countWrapper (slot.plugin, slot.state);
  1210. // Harmless: every state in the demos becomes a synth, with finite values
  1211. for (const auto& c : p.channels)
  1212. if (c.pluginName == "Harmless")
  1213. {
  1214. ++harmless;
  1215. lynx::SynthSettings hs;
  1216. float g = 1.0f;
  1217. if (lynx::synthFromHarmless (c.pluginState, 128.0, hs, g))
  1218. {
  1219. ++harmlessDecoded;
  1220. CHECK (std::isfinite (g) && hs.cutoff >= 20.0f && hs.cutoff <= 20000.0f && std::isfinite (hs.decay) && std::isfinite (hs.release));
  1221. }
  1222. if (const auto hp = lynx::harmlessParams (c.pluginState))
  1223. harmlessParsed += (int) hp->size() == lynx::harmlessParamCount ? 1 : 0;
  1224. }
  1225. for (const auto& c : song.channels)
  1226. harmlessKind += c.instrument == lynx::InstrumentKind::harmless ? 1 : 0;
  1227. // Harmor: every state in the demos plays on the exact engine (REA_rodada34) and sounds, with finite samples
  1228. for (const auto& c : p.channels)
  1229. if (c.pluginName == "Harmor")
  1230. {
  1231. ++harmor;
  1232. harmorUsable += lynx::harmorStateUsable (c.pluginState) ? 1 : 0;
  1233. }
  1234. for (const auto& c : song.channels)
  1235. if (c.instrument == lynx::InstrumentKind::harmor)
  1236. {
  1237. ++harmorKind;
  1238. 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);
  1239. double e = 0.0;
  1240. bool finite = ! y.empty();
  1241. for (auto v : y)
  1242. finite = finite && std::isfinite (v), e += (double) v * v;
  1243. CHECK (finite);
  1244. harmorSounding += e > 1e-6 ? 1 : 0;
  1245. }
  1246. // Ogun: every state in the demos plays on the exact engine (REA_rodada37) and sounds, with finite samples
  1247. for (const auto& c : p.channels)
  1248. if (c.pluginName == "Ogun")
  1249. {
  1250. ++ogun;
  1251. ogunUsable += lynx::ogunStateUsable (c.pluginState) ? 1 : 0;
  1252. }
  1253. for (const auto& c : song.channels)
  1254. if (c.instrument == lynx::InstrumentKind::ogun)
  1255. {
  1256. ++ogunKind;
  1257. 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 },
  1258. { 2000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 8000 } }, 44100.0, 11025);
  1259. double e = 0.0;
  1260. bool finite = ! y.empty();
  1261. for (auto v : y)
  1262. finite = finite && std::isfinite (v), e += (double) v * v;
  1263. CHECK (finite);
  1264. ogunSounding += e > 1e-6 ? 1 : 0;
  1265. }
  1266. // Autogun (REA_rodada57): every state plays on Ogun's engine in its Autogun variant; with the installation, the
  1267. // Magic Dust stage gets Autogun\Data\SG.dat
  1268. {
  1269. bool any = false;
  1270. for (const auto& c : p.channels)
  1271. if (c.pluginName == "Autogun")
  1272. ++autogun, autogunUsable += lynx::autogunStateUsable (c.pluginState) ? 1 : 0, any = true;
  1273. if (any)
  1274. for (const auto& c : lynx::importFlp (p, withFiles).channels)
  1275. if (c.instrument == lynx::InstrumentKind::autogun)
  1276. {
  1277. ++autogunKind;
  1278. lynx::ogun::RefOptions opt;
  1279. opt.autogun = true;
  1280. if (! c.samplePath.empty())
  1281. {
  1282. auto local = c.samplePath;
  1283. std::replace (local.begin(), local.end(), '\\', '/');
  1284. std::ifstream sg (local, std::ios::binary);
  1285. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  1286. autogunWithSg += opt.sgState.size() == 1342 ? 1 : 0;
  1287. }
  1288. 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 } },
  1289. 44100.0, 11025, opt);
  1290. double e = 0.0;
  1291. bool finite = ! y.empty();
  1292. for (auto v : y)
  1293. finite = finite && std::isfinite (v), e += (double) v * v;
  1294. CHECK (finite);
  1295. autogunSounding += e > 1e-6 ? 1 : 0;
  1296. }
  1297. }
  1298. // Fruity DX10: every state plays on the exact engine when its channel processor is neutral (REA_rodada42)
  1299. for (const auto& c : p.channels)
  1300. if (c.pluginName == "Fruity DX10" || c.pluginName == "DX10")
  1301. ++dx10, dx10Usable += lynx::dx10StateUsable (c.pluginState) ? 1 : 0;
  1302. for (const auto& c : song.channels)
  1303. if (c.instrument == lynx::InstrumentKind::dx10)
  1304. {
  1305. ++dx10Kind;
  1306. 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 } },
  1307. 44100.0, 11025);
  1308. double e = 0.0;
  1309. bool finite = ! y.empty();
  1310. for (auto v : y)
  1311. finite = finite && std::isfinite (v), e += (double) v * v;
  1312. CHECK (finite);
  1313. dx10Sounding += e > 1e-6 ? 1 : 0;
  1314. }
  1315. // 3x Osc: every channel plays on the exact engine (REA_rodada47)
  1316. for (const auto& c : song.channels)
  1317. if (c.instrument == lynx::InstrumentKind::threeosc)
  1318. {
  1319. ++threeOscKind;
  1320. 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 } },
  1321. 44100.0, 11025);
  1322. double e = 0.0;
  1323. bool finite = ! y.empty();
  1324. for (auto v : y)
  1325. finite = finite && std::isfinite (v), e += (double) v * v;
  1326. CHECK (finite);
  1327. threeOscSounding += e > 1e-6 ? 1 : 0;
  1328. }
  1329. // Sytrus: every state in the demos (zlib + 10 units) decodes, with finite values
  1330. for (const auto& c : p.channels)
  1331. if (c.pluginName == "Sytrus")
  1332. {
  1333. ++sytrus;
  1334. lynx::SynthSettings ss;
  1335. float g = 1.0f;
  1336. // the Sytrus engine (REA_rodada31) reads every demo state; the first 24 play a short note (render settings)
  1337. if (lynx::sytrus::stateUsable (c.pluginState))
  1338. {
  1339. ++sytrusEngine;
  1340. if (sytrusRendered < 24)
  1341. {
  1342. ++sytrusRendered;
  1343. lynx::sytrus::Engine e;
  1344. e.prepare (44100.0, true, 512);
  1345. e.setPatch (lynx::sytrus::parsePatch (c.pluginState));
  1346. lynx::sytrus::NoteParams note;
  1347. e.noteOn (note);
  1348. std::vector<float> l (8192), r (8192);
  1349. e.render (l.data(), r.data(), 8192, 0.551f, 0.0f);
  1350. bool finite = true;
  1351. double energy = 0.0;
  1352. for (std::size_t k = 0; k < l.size(); ++k)
  1353. finite = finite && std::isfinite (l[k]) && std::isfinite (r[k]), energy += (double) l[k] * l[k];
  1354. CHECK (finite);
  1355. sytrusSounding += energy > 1e-6;
  1356. }
  1357. }
  1358. if (lynx::synthFromSytrus (c.pluginState, 128.0, ss, g))
  1359. {
  1360. ++sytrusDecoded;
  1361. CHECK (std::isfinite (g) && ss.cutoff >= 20.0f && ss.cutoff <= 20000.0f && std::isfinite (ss.attack) && std::isfinite (ss.decay)
  1362. && std::isfinite (ss.release) && ss.sustain >= 0.0f && ss.sustain <= 1.0f && std::isfinite (ss.filterEnv.amount));
  1363. for (const auto& o : ss.osc)
  1364. CHECK (o.level >= 0.0f && o.level <= 1.0f && o.octave >= -3 && o.octave <= 3);
  1365. }
  1366. }
  1367. // round 12 generators
  1368. for (const auto& c : p.channels)
  1369. {
  1370. lynx::SynthSettings gs;
  1371. float g = 1.0f;
  1372. int decoded = -1;
  1373. if (c.pluginName == "Harmor")
  1374. {
  1375. decoded = lynx::synthFromHarmor (c.pluginState, gs, g);
  1376. if (decoded)
  1377. {
  1378. // REA_rodada25 §4: most resonant settings stay below Q ≈ 3, a few saturate
  1379. CHECK (gs.resonance >= 0.0f && gs.resonance <= 1.0f);
  1380. static int resonant = 0, strong = 0, total = 0;
  1381. resonant += gs.resonance > 0.0f;
  1382. strong += gs.resonance > 0.9f;
  1383. if (++total % 50 == 0)
  1384. std::printf (" Harmor resonance: %d with a peak, %d with Q > ~5 (of %d)\n", resonant, strong, total);
  1385. }
  1386. static int saw = 0, square = 0, other = 0, total = 0;
  1387. if (decoded && std::any_of (gs.osc.begin(), gs.osc.end(), [] (const lynx::SynthOscillator& o) { return o.wave == lynx::Waveform::spectral; }))
  1388. {
  1389. auto isSaw = true, isSquare = true;
  1390. for (int n = 1; n <= 16; ++n)
  1391. {
  1392. isSaw = isSaw && std::abs (gs.spectrum[(std::size_t) n - 1] - 1.0f / n) < 1e-3f;
  1393. isSquare = isSquare && std::abs (gs.spectrum[(std::size_t) n - 1] - (n % 2 == 1 ? 1.1535f / n : 0.0f)) < 2e-3f;
  1394. }
  1395. ++(isSaw ? saw : isSquare ? square : other);
  1396. if (++total % 20 == 0 || total == 100)
  1397. std::printf (" Harmor spectrum: %d saw, %d square, %d other (of %d)\n", saw, square, other, total);
  1398. }
  1399. }
  1400. else if (c.pluginName == "GMS")
  1401. decoded = lynx::synthFromGms (c.pluginState, 128.0, gs, g);
  1402. else if (c.pluginName == "Ogun" || c.pluginName == "Autogun")
  1403. {
  1404. decoded = lynx::synthFromOgun (c.pluginState, gs, g);
  1405. // REA_rodada24 §1.9: the "Trumpet" preset (richness 7, no seeds) has partials 0.9087, 0.8610,
  1406. // 0.8151… at the fixed seed-3 phases 0.919, 0.380, 0.130
  1407. if (decoded && gs.osc[0].wave == lynx::Waveform::spectral)
  1408. {
  1409. CHECK (std::all_of (gs.spectrum.begin(), gs.spectrum.end(), [] (float a) { return std::isfinite (a) && a >= 0.0f; }));
  1410. static constexpr float trumpet[3] { 0.9087f, 0.8610f, 0.8151f }, phases[3] { 0.919f, 0.380f, 0.130f };
  1411. auto match = true;
  1412. for (std::size_t k = 0; k < 3; ++k)
  1413. match = match && std::abs (gs.spectrum[k] * 2.0f / 3.14159265f - trumpet[k]) < 1e-3f && std::abs (gs.spectrumPhase[k] - phases[k]) < 1e-3f;
  1414. ogunTrumpet += match ? 1 : 0;
  1415. }
  1416. }
  1417. else if (c.pluginName == "Sakura")
  1418. decoded = lynx::synthFromSakura (c.pluginState, gs);
  1419. else if (c.pluginName == "Kepler")
  1420. decoded = lynx::synthFromKepler (c.pluginState, gs, g);
  1421. else if (c.pluginName == "Wave Traveller")
  1422. decoded = lynx::waveTravellerSample (c.pluginState).has_value() ? 2 : 0;
  1423. else if (c.pluginName == "PoiZone")
  1424. decoded = lynx::synthFromPoiZone (c.pluginState, gs, g);
  1425. else if (c.pluginName == "BassDrum")
  1426. decoded = lynx::synthFromBassDrum (c.pluginState, gs, g);
  1427. else if (c.pluginName == "Toxic Biohazard")
  1428. decoded = lynx::synthFromToxic (c.pluginState, gs, g);
  1429. else if (c.pluginName == "Morphine")
  1430. decoded = lynx::synthFromMorphine (c.pluginState, gs, g);
  1431. else if (c.pluginName == "BooBass")
  1432. decoded = lynx::synthFromBooBass (c.pluginState, gs);
  1433. else if (c.pluginName == "Drumpad" || c.pluginName == "DrumPad")
  1434. decoded = lynx::drumpadPatchName (c.pluginState).has_value() ? 2 : 0;
  1435. else if (c.pluginName == "Fruity Envelope Controller" && c.pluginState.size() >= 4 && c.pluginState[2] >= 6) // v >= 6
  1436. decoded = lynx::parseEnvelopeController (c.pluginState).has_value() ? 2 : 0;
  1437. else if (c.pluginName == "FLEX")
  1438. decoded = lynx::flexPresetName (c.pluginState).has_value() ? 2 : 0;
  1439. else if (c.pluginName == "Fruity DrumSynth Live")
  1440. decoded = lynx::drumSynthLiveParams (c.pluginState).has_value() ? 2 : 0;
  1441. if (decoded < 0)
  1442. continue;
  1443. auto& counter = generators[c.pluginName];
  1444. ++counter.first;
  1445. counter.second += decoded != 0;
  1446. if (decoded == 1 && c.pluginName != "Fruity DrumSynth Live")
  1447. {
  1448. CHECK (std::isfinite (g) && g >= 0.0f && gs.cutoff >= 20.0f && gs.cutoff <= 20000.0f && gs.sustain >= 0.0f && gs.sustain <= 1.0f);
  1449. 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));
  1450. for (const auto& o : gs.osc)
  1451. CHECK (o.level >= 0.0f && o.level <= 4.0f && std::isfinite (o.detune) && std::isfinite (o.pan));
  1452. }
  1453. }
  1454. // 3x Osc: every state in the demos decodes, with levels summing to at most 1
  1455. for (const auto& c : p.channels)
  1456. if (c.pluginName == "3x Osc")
  1457. {
  1458. ++threeOsc;
  1459. lynx::SynthSettings s;
  1460. if (lynx::synthFrom3xOsc (c.pluginState, s))
  1461. {
  1462. ++threeOscDecoded;
  1463. // with AM, osc 3 is the modulation depth and is left out of the sum
  1464. const auto sum = s.osc[0].level + s.osc[1].level + (s.am ? 0.0f : s.osc[2].level);
  1465. CHECK (sum <= 1.001f && s.osc[2].level <= 1.0f);
  1466. for (const auto& o : s.osc)
  1467. CHECK (o.octave >= -2 && o.octave <= 2 && std::abs (o.semitones) <= 11 && std::abs (o.pan) <= 1.0f);
  1468. }
  1469. }
  1470. for (auto& pat : p.patterns)
  1471. if (! pat.notes.empty())
  1472. {
  1473. ++withNotes;
  1474. break;
  1475. }
  1476. for (auto& a : p.arrangements)
  1477. for (auto& item : a.items)
  1478. CHECK (item.track >= 0 && item.track < 500);
  1479. for (auto& clip : song.playlist)
  1480. {
  1481. CHECK (clip.pattern >= 0 && clip.pattern < (int) song.patterns.size());
  1482. CHECK (clip.length > 0 && clip.offset >= 0);
  1483. if (clip.kind == lynx::ClipKind::automation)
  1484. CHECK (clip.automation >= 0 && clip.automation < (int) song.automations.size());
  1485. }
  1486. for (auto& a : song.automations)
  1487. {
  1488. ++automations;
  1489. automationsWithTarget += a.target.kind != lynx::AutomationTargetKind::none;
  1490. CHECK (a.isLive() || ! a.points.empty());
  1491. for (std::size_t i = 1; i < a.points.size(); ++i)
  1492. CHECK (a.points[i].tick >= a.points[i - 1].tick);
  1493. }
  1494. // imported routing has no cycles and valid destinations
  1495. CHECK (song.mixerOrder().size() + 1 == song.mixer.size());
  1496. for (auto& t : song.mixer)
  1497. for (auto& s : t.sends)
  1498. {
  1499. ++sends;
  1500. CHECK (s.target > 0 && s.target < (int) song.mixer.size());
  1501. }
  1502. if (! song.playlist.empty())
  1503. ++withPlaylist;
  1504. }
  1505. catch (const flp::Error& e)
  1506. {
  1507. ++failures;
  1508. std::printf (" FAILED %s: %s\n", entry.path().string().c_str(), e.what());
  1509. }
  1510. }
  1511. CHECK (threeOscDecoded == threeOsc);
  1512. std::printf (" Harmless: %d channels, %d decoded\n", harmless, harmlessDecoded);
  1513. std::printf (" Sytrus: %d channels, %d decoded, %d by the engine (%d of %d rendered sound)\n", sytrus, sytrusDecoded, sytrusEngine,
  1514. sytrusSounding, sytrusRendered);
  1515. CHECK (sytrusEngine == sytrus);
  1516. CHECK (sytrusSounding >= sytrusRendered * 3 / 4);
  1517. for (const auto& [name, counter] : generators)
  1518. {
  1519. std::printf (" %s: %d channels, %d decoded\n", name.c_str(), counter.first, counter.second);
  1520. CHECK (counter.second == counter.first);
  1521. }
  1522. std::printf (" Ogun spectra matching the Trumpet reference: %d\n", ogunTrumpet);
  1523. CHECK (ogunTrumpet >= 1);
  1524. // demo Gross Beat: the volume slot becomes the gate (those using an envelope slot get steps < 1)
  1525. {
  1526. int vocodexes = 0, vocodexSides = 0;
  1527. int lovePhilterLinks[4] {}; // live links: cutoff, resonance; inside containers: cutoff, resonance
  1528. int embeddedZones = 0, lovePhilters = 0, lovePhiltersRead = 0, lovePhiltersConverted = 0, gates = 0, shaped = 0, effectors = 0, patchers = 0, serial = 0, parallel = 0, generatorPatchers = 0;
  1529. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1530. if (entry.path().extension() == ".flp")
  1531. {
  1532. std::ifstream in (entry.path(), std::ios::binary);
  1533. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1534. try
  1535. {
  1536. const auto project = flp::parseProject (file);
  1537. for (const auto& insert : project.mixer)
  1538. for (const auto& slot : insert.slots)
  1539. if (slot.plugin == "Gross Beat")
  1540. {
  1541. // FL's own engine (REA_rodada53): every demo state is read
  1542. const auto fx = lynx::effectFromFl (slot.plugin, slot.state);
  1543. CHECK (fx && fx->kind == lynx::EffectKind::grossBeat);
  1544. ++gates;
  1545. if (fx && fx->kind == lynx::EffectKind::grossBeat && lynx::grossbeat::slotNames (fx->state, true).size() == 36)
  1546. ++shaped;
  1547. }
  1548. else if (slot.plugin == "Fruity Love Philter")
  1549. {
  1550. ++lovePhilters;
  1551. lovePhiltersRead += lynx::parseLovePhilter (slot.state).has_value();
  1552. if (const auto conv = lynx::lovePhilterEffects (slot.state))
  1553. {
  1554. ++lovePhiltersConverted;
  1555. CHECK (! conv->slots.empty());
  1556. for (const auto& fx : conv->slots)
  1557. for (auto v : fx.params)
  1558. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1559. for (const auto& fx : conv->slots)
  1560. for (const auto& link : fx.links)
  1561. {
  1562. const auto& chain = fx.branches[(std::size_t) link.branch].chain;
  1563. CHECK (link.source < (int) chain.size() && chain[(std::size_t) link.target].kind == lynx::EffectKind::parametricEq);
  1564. lovePhilterLinks[2 + (link.param == lynx::peq::param (0, 3) ? 1 : 0)]++;
  1565. }
  1566. for (const auto& link : conv->links)
  1567. {
  1568. lovePhilterLinks[link.param == lynx::peq::param (0, 3) ? 1 : 0]++;
  1569. CHECK (link.curve.size() == 33);
  1570. for (auto v : link.curve)
  1571. CHECK (std::isfinite (v) && v >= 0.0f && v <= 1.0f);
  1572. }
  1573. }
  1574. }
  1575. else if (slot.plugin == "Vocodex")
  1576. {
  1577. const auto v = lynx::vocodexEffect (slot.state);
  1578. CHECK (v.has_value());
  1579. vocodexes += v.has_value();
  1580. vocodexSides += v && (v->modIn > 0 || v->carIn > 0);
  1581. }
  1582. else if (slot.plugin == "Effector")
  1583. effectors += lynx::effectFromFl (slot.plugin, slot.state).has_value();
  1584. else if (slot.plugin == "Patcher")
  1585. {
  1586. const auto graph = lynx::parsePatcher (slot.state);
  1587. CHECK (graph.has_value());
  1588. ++patchers;
  1589. if (graph && ! graph->serialChain.empty())
  1590. ++serial;
  1591. if (graph && graph->serialChain.empty() && graph->audio)
  1592. ++parallel;
  1593. }
  1594. for (const auto& c : project.channels)
  1595. if (c.pluginName == "DirectWave")
  1596. if (const auto program = lynx::directWaveProgram (c.pluginState))
  1597. for (const auto& wav : program->embeddedWav)
  1598. if (! wav.empty())
  1599. {
  1600. ++embeddedZones;
  1601. CHECK (wav.size() > 44 && std::memcmp (wav.data(), "RIFF", 4) == 0);
  1602. }
  1603. for (const auto& c : project.channels)
  1604. if (c.pluginName == "Patcher")
  1605. {
  1606. const auto graph = lynx::parsePatcher (c.pluginState);
  1607. CHECK (graph.has_value());
  1608. generatorPatchers += graph && graph->mainGenerator >= 0;
  1609. }
  1610. }
  1611. catch (const flp::Error&) {}
  1612. }
  1613. CHECK (gates >= 30 && shaped == gates);
  1614. std::printf (" Gross Beat: %d slots, %d read exactly; Effector: %d converted; Love Philter: %d, %d read\n", gates, shaped, effectors,
  1615. lovePhilters, lovePhiltersRead);
  1616. CHECK (lovePhiltersRead == lovePhilters && lovePhiltersConverted == lovePhilters);
  1617. std::printf (" Vocodex: %d converted, %d with side inputs\n", vocodexes, vocodexSides);
  1618. CHECK (vocodexes >= 9);
  1619. std::printf (" Love Philter live links: %d cutoff, %d resonance; in multi-unit containers: %d cutoff, %d resonance\n",
  1620. lovePhilterLinks[0], lovePhilterLinks[1], lovePhilterLinks[2], lovePhilterLinks[3]);
  1621. CHECK (lovePhilterLinks[2] + lovePhilterLinks[3] > 0);
  1622. std::printf (" DirectWave: %d zones with embedded audio\n", embeddedZones);
  1623. CHECK (embeddedZones == 12);
  1624. std::printf (" Patcher: %d in the mixer (%d serial, %d reduced to serial/parallel), %d as instrument with generator\n", patchers, serial,
  1625. parallel, generatorPatchers);
  1626. CHECK (gates == 0 || shaped > 0);
  1627. }
  1628. // Envelope Controller "Basic Sidechain" (Future House): drops on the note and returns to ~1 within ~1/3 s
  1629. {
  1630. const auto path = std::filesystem::path (realFlpDir) / "Demo projects" / "Demo songs" / "Asher Postman - Future House.flp";
  1631. if (std::filesystem::exists (path))
  1632. {
  1633. std::ifstream in (path, std::ios::binary);
  1634. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1635. const auto song = lynx::importFlp (flp::parseProject (file));
  1636. const lynx::ChannelDef* controller = nullptr;
  1637. for (const auto& c : song.channels)
  1638. if (c.instrument == lynx::InstrumentKind::controller)
  1639. controller = &c;
  1640. CHECK (controller != nullptr);
  1641. if (controller != nullptr)
  1642. {
  1643. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  1644. inst->prepare (48000.0);
  1645. inst->configure (*controller, song.bpm);
  1646. std::vector<float> l (480), r (480);
  1647. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1648. inst->noteOn (60, 1.0f);
  1649. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f); // 10 ms
  1650. const auto ducked = inst->controlOutput (0);
  1651. for (int i = 0; i < 100; ++i) // +1 s
  1652. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  1653. const auto recovered = inst->controlOutput (0);
  1654. std::printf (" Envelope Controller (Basic Sidechain): %.2f on the note, %.2f after 1 s\n", ducked, recovered);
  1655. CHECK (ducked < 0.3f && recovered > 0.9f);
  1656. }
  1657. }
  1658. }
  1659. // mutation fuzz: real states with flipped and truncated bytes must not crash the readers
  1660. {
  1661. std::map<std::string, std::vector<std::uint8_t>> samples; // one real blob per plugin
  1662. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Demo projects"))
  1663. {
  1664. if (entry.path().extension() != ".flp")
  1665. continue;
  1666. std::ifstream in (entry.path(), std::ios::binary);
  1667. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1668. try
  1669. {
  1670. const auto project = flp::parseProject (file);
  1671. for (const auto& c : project.channels)
  1672. if (! c.pluginState.empty())
  1673. samples.emplace (c.pluginName, c.pluginState);
  1674. for (const auto& insert : project.mixer)
  1675. for (const auto& slot : insert.slots)
  1676. if (! slot.state.empty())
  1677. samples.emplace (slot.plugin, slot.state);
  1678. }
  1679. catch (const flp::Error&) {}
  1680. }
  1681. std::uint32_t seed = 12345;
  1682. auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
  1683. int mutations = 0;
  1684. for (const auto& [name, original] : samples)
  1685. for (int round = 0; round < 40; ++round)
  1686. {
  1687. auto blob = original;
  1688. if (round % 4 == 3 && ! blob.empty())
  1689. blob.resize (rnd() % blob.size()); // truncated
  1690. else
  1691. for (int k = 0; k < 1 + (int) (rnd() % 8) && ! blob.empty(); ++k)
  1692. blob[rnd() % blob.size()] = (std::uint8_t) rnd();
  1693. ++mutations;
  1694. lynx::SynthSettings synth;
  1695. float gain = 1.0f;
  1696. lynx::synthFromSytrus (blob, 120.0, synth, gain);
  1697. lynx::synthFromHarmor (blob, synth, gain);
  1698. lynx::synthFromGms (blob, 120.0, synth, gain);
  1699. lynx::synthFromOgun (blob, synth, gain);
  1700. lynx::synthFromMorphine (blob, synth, gain);
  1701. lynx::synthFromKepler (blob, synth, gain);
  1702. lynx::synthFromPoiZone (blob, synth, gain);
  1703. lynx::synthFromToxic (blob, synth, gain);
  1704. lynx::synthFromBassDrum (blob, synth, gain);
  1705. lynx::synthFromHarmless (blob, 120.0, synth, gain);
  1706. lynx::harmlessParams (blob);
  1707. if (lynx::harmorStateUsable (blob))
  1708. lynx::harmorFallbacks (blob);
  1709. lynx::parseEnvelopeController (blob);
  1710. lynx::parseKeyboardController (blob);
  1711. lynx::parseLovePhilter (blob);
  1712. lynx::parsePatcher (blob);
  1713. lynx::effectFromFl (name, blob);
  1714. lynx::fpcPads (blob);
  1715. lynx::directWaveProgram (blob);
  1716. CHECK (std::isfinite (gain) && std::isfinite (synth.cutoff));
  1717. }
  1718. std::printf (" mutation fuzz: %d blobs from %d plugins\n", mutations, (int) samples.size());
  1719. }
  1720. // WavPack (.wv) samples from FL packs: all decode, with finite, non-silent audio
  1721. {
  1722. int files = 0, decoded = 0, audible = 0;
  1723. double seconds = 0.0;
  1724. for (const auto& entry : std::filesystem::recursive_directory_iterator (std::filesystem::path (realFlpDir) / "Patches" / "Packs"))
  1725. if (entry.path().extension() == ".wv")
  1726. {
  1727. std::ifstream in (entry.path(), std::ios::binary);
  1728. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1729. ++files;
  1730. lynx::DecodedAudio audio;
  1731. if (! lynx::decodeWavPack (file.data(), file.size(), audio))
  1732. continue;
  1733. ++decoded;
  1734. float peak = 0.0f;
  1735. bool finite = true;
  1736. for (const auto& ch : audio.channels)
  1737. for (auto v : ch)
  1738. {
  1739. finite = finite && std::isfinite (v);
  1740. peak = std::max (peak, std::abs (v));
  1741. }
  1742. CHECK (finite && peak <= 4.0f);
  1743. audible += peak > 1e-4f;
  1744. seconds += (double) audio.channels[0].size() / audio.sampleRate;
  1745. }
  1746. std::printf (" WavPack: %d .wv files, %d decoded, %d audible, %.0f s of audio\n", files, decoded, audible, seconds);
  1747. CHECK (decoded == files);
  1748. }
  1749. // native GMS presets (.gmsynth = the contents of 0xD5 without the "SSLF")
  1750. int gmsPresets = 0, gmsDecoded = 0;
  1751. const auto gmsFolder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "GMS";
  1752. if (std::filesystem::exists (gmsFolder))
  1753. for (const auto& entry : std::filesystem::recursive_directory_iterator (gmsFolder))
  1754. if (entry.path().extension() == ".gmsynth")
  1755. {
  1756. std::ifstream in (entry.path(), std::ios::binary);
  1757. std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1758. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F' };
  1759. const auto n = (std::uint32_t) file.size();
  1760. blob.insert (blob.end(), reinterpret_cast<const std::uint8_t*> (&n), reinterpret_cast<const std::uint8_t*> (&n) + 4);
  1761. blob.insert (blob.end(), file.begin(), file.end());
  1762. lynx::SynthSettings gs;
  1763. float g = 1.0f;
  1764. ++gmsPresets;
  1765. if (lynx::synthFromGms (blob, 120.0, gs, g))
  1766. {
  1767. ++gmsDecoded;
  1768. CHECK (std::isfinite (g) && gs.cutoff >= 20.0f && std::isfinite (gs.pitchEnv.amount) && std::isfinite (gs.lfo[lynx::lfoPitch].period));
  1769. }
  1770. }
  1771. std::printf (" GMS presets: %d, %d decoded\n", gmsPresets, gmsDecoded);
  1772. // the synth's "FL presets" button: Sytrus/Harmor .fst and .gmsynth become synth settings
  1773. int presetFiles = 0, presetsRead = 0;
  1774. for (const auto* name : { "Sytrus", "Harmor", "GMS", "Harmless", "Ogun", "Sakura", "Fruit Kick", "Fruity DX10", "MiniSynth", "Kepler",
  1775. "PoiZone", "Toxic Biohazard", "BassDrum" })
  1776. {
  1777. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / name;
  1778. if (! std::filesystem::exists (folder))
  1779. continue;
  1780. for (const auto& entry : std::filesystem::recursive_directory_iterator (folder))
  1781. if (entry.path().extension() == ".fst" || entry.path().extension() == ".gmsynth")
  1782. {
  1783. std::ifstream in (entry.path(), std::ios::binary);
  1784. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  1785. ++presetFiles;
  1786. presetsRead += lynx::synthFromFlPreset (file).has_value();
  1787. }
  1788. }
  1789. std::printf (" FL synth presets: %d files, %d read\n", presetFiles, presetsRead);
  1790. CHECK (presetsRead >= presetFiles * 95 / 100);
  1791. CHECK (gmsDecoded == gmsPresets);
  1792. CHECK (sytrusDecoded == sytrus);
  1793. CHECK (harmlessDecoded == harmless && harmless > 100);
  1794. std::printf (" Harmless: %d states parsed for the engine, %d channels imported as Harmless\n", harmlessParsed, harmlessKind);
  1795. CHECK (harmlessParsed == harmless && harmlessKind >= harmless);
  1796. std::printf (" Harmor: %d channels, %d states usable by the engine, %d imported as Harmor (%d sounding)\n", harmor, harmorUsable, harmorKind, harmorSounding);
  1797. CHECK (harmorUsable == harmor && harmor > 90 && harmorKind >= harmor && harmorSounding >= harmorKind * 9 / 10);
  1798. 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",
  1799. gms, gmsUsable, gmsKind, gmsSounding, morphine, morphineUsable, morphineKind, morphineSounding, demoGoldens);
  1800. CHECK (gmsUsable == gms && gms >= 9 && gmsKind == gms && gmsSounding == gmsKind);
  1801. std::printf (" MiniSynth channels: %d, %d usable, %d on the exact engine, %d sounding, %d golden\n", miniSynth, miniSynthUsable, miniSynthKind,
  1802. miniSynthSounding, miniSynthGoldens);
  1803. CHECK (miniSynthUsable == miniSynth && miniSynth >= 12 && miniSynthKind == miniSynth && miniSynthSounding == miniSynthKind && miniSynthGoldens == 2);
  1804. CHECK (morphineUsable == morphine && morphine >= 7 && morphineKind == morphine && morphineSounding == morphineKind && demoGoldens == 2);
  1805. std::printf (" PoiZone: %d channels, %d usable, %d imported as PoiZone (%d sounding), %d demo golden\n", poizone, poizoneUsable, poizoneKind,
  1806. poizoneSounding, poizoneGoldens);
  1807. CHECK (poizoneUsable == poizone && poizone >= 2 && poizoneKind == poizone && poizoneSounding == poizoneKind && poizoneGoldens == 1);
  1808. std::printf (" Toxic Biohazard: %d channels, %d usable, %d imported as Toxic (%d sounding), %d golden\n", toxic, toxicUsable, toxicKind,
  1809. toxicSounding, toxicGoldens);
  1810. CHECK (toxicUsable == toxic && toxic >= 3 && toxicKind == toxic && toxicSounding == toxicKind && toxicGoldens == 1);
  1811. std::printf (" Ogun: %d channels, %d states usable by the engine, %d imported as Ogun (%d sounding)\n", ogun, ogunUsable, ogunKind, ogunSounding);
  1812. CHECK (ogunUsable == ogun && ogun >= 20 && ogunKind >= ogun && ogunSounding >= ogunKind * 9 / 10);
  1813. std::printf (" Autogun: %d channels, %d states usable by the engine, %d imported as Autogun (%d with SG.dat, %d sounding)\n", autogun,
  1814. autogunUsable, autogunKind, autogunWithSg, autogunSounding);
  1815. CHECK (autogunUsable == autogun && autogun >= 9 && autogunKind == autogun && autogunWithSg == autogun && autogunSounding == autogun);
  1816. std::printf (" Fruity DX10: %d channels, %d states usable by the engine, %d imported as DX10 (%d sounding)\n", dx10, dx10Usable, dx10Kind, dx10Sounding);
  1817. CHECK (dx10Usable == dx10 && dx10 >= 12 && dx10Kind == dx10 && dx10Sounding == dx10Kind);
  1818. std::printf (" Kepler: %d channels, %d states usable by the engine, %d imported as Kepler (%d sounding)\n", kepler, keplerUsable, keplerKind, keplerSounding);
  1819. CHECK (keplerUsable == kepler && kepler >= 4 && keplerSounding == keplerKind && (keplerKind == 0 || keplerKind == kepler));
  1820. std::printf (" Harmless rack compressor: %d of %d presets read from the installation\n", harmlessCompLoaded, harmlessCompWanted);
  1821. CHECK (harmlessCompLoaded == harmlessCompWanted && harmlessCompWanted > 100);
  1822. std::printf (" Maximus: %d of %d slots on the exact engine; Soundgoodizer: %d of %d; Vocodex with SG.dat: %d of %d\n", maximusExact,
  1823. maximusSlots, sgzExact, sgzSlots, vocodexWithData, vocodexSlots);
  1824. // REA_rodada40 §6: in Demo projects the 4 states left on the approximation use the Frequency Splitter linear-phase
  1825. // option; the other Data folders add states with options not decoded
  1826. CHECK (maximusSlots - maximusExact <= 16 && maximusExact >= 97 && sgzExact == sgzSlots && sgzSlots > 90);
  1827. CHECK (vocodexWithData == vocodexSlots && vocodexSlots >= 10);
  1828. std::printf (" Fruity WaveShaper: %d of %d slots on the exact engine; Fruity Soft Clipper: %d of %d\n", shaperExact, shaperSlots,
  1829. clipperExact, clipperSlots);
  1830. // (Patcher graphs add WaveShaper nodes of their own to the imported mixer)
  1831. CHECK (shaperExact >= shaperSlots && shaperSlots >= 70 && clipperExact >= clipperSlots && clipperSlots >= 25); // REA_rodada51
  1832. std::printf (" Fruity Parametric EQ 2 / Parametric EQ: %d of %d slots on the exact engine\n", flPeqExact, flPeqSlots);
  1833. CHECK (flPeqExact >= flPeqSlots && flPeqSlots >= 1100); // REA_rodada50
  1834. std::printf (" Fruity PanOMatic / Balance / Blood Overdrive: %d of %d slots on the exact engines\n", basicExact, basicSlots);
  1835. // one PanOMatic of "Bombs Away" (insert 69, last slot) falls off LYNX's 10-slot limit after a serial Patcher expands
  1836. CHECK (basicExact + 1 >= basicSlots && basicSlots >= 270); // REA_rodada58
  1837. std::printf (" EQUO: %d of %d slots on the exact engine\n", equoExact, equoSlots);
  1838. CHECK (equoExact == equoSlots && equoSlots >= 10); // REA_rodada61
  1839. std::printf (" Pitch Shifter: %d of %d slots on the exact engine\n", pitchShifterExact, pitchShifterSlots);
  1840. CHECK (pitchShifterExact == pitchShifterSlots && pitchShifterSlots >= 15); // REA_rodada61
  1841. std::printf (" Fruity Delay 2: %d of %d slots on the exact engine; Fruity Delay 3: %d of %d\n", delay2Exact, delay2Slots,
  1842. delay3Exact, delay3Slots);
  1843. CHECK (delay2Exact == delay2Slots && delay2Slots >= 60 && delay3Exact == delay3Slots && delay3Slots >= 100); // REA_rodada52
  1844. std::printf (" Chorus family: %d of %d slots on the exact engines\n", chorusExact, chorusSlots);
  1845. CHECK (chorusExact >= chorusSlots && chorusSlots >= 47); // REA_rodada59
  1846. 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,
  1847. fastDistSlots, flangusExact, flangusSlots, flangerExact, flangerSlots);
  1848. CHECK (fastDistExact >= fastDistSlots && fastDistSlots >= 30 && flangusExact >= flangusSlots && flangusSlots >= 30
  1849. && flangerExact >= flangerSlots && flangerSlots >= 3); // REA_rodada55
  1850. // 3x Osc channel presets shipped with FL (.fst): all become synth settings
  1851. int presets = 0, presetsDecoded = 0;
  1852. const auto presetDir = std::filesystem::path (realFlpDir) / "Patches" / "Channel presets" / "3x Osc";
  1853. if (std::filesystem::is_directory (presetDir))
  1854. for (auto& entry : std::filesystem::directory_iterator (presetDir))
  1855. if (entry.path().extension() == ".fst")
  1856. {
  1857. ++presets;
  1858. std::ifstream in (entry.path(), std::ios::binary);
  1859. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1860. if (auto synth = lynx::synthFromFlPreset (bytes))
  1861. {
  1862. ++presetsDecoded;
  1863. CHECK (synth->osc[0].level + synth->osc[1].level + (synth->am ? 0.0f : synth->osc[2].level) <= 1.001f);
  1864. }
  1865. }
  1866. std::printf (" %d 3x Osc presets, %d read\n", presets, presetsDecoded);
  1867. CHECK (presetsDecoded == presets);
  1868. // FL effect presets that become built-in effects
  1869. int fxPresets = 0, fxDecoded = 0;
  1870. for (auto kind : { lynx::EffectKind::parametricEq, lynx::EffectKind::limiter, lynx::EffectKind::reverb, lynx::EffectKind::delay,
  1871. lynx::EffectKind::utility })
  1872. for (const auto& plugin : lynx::flPluginsForEffect (kind))
  1873. {
  1874. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / plugin;
  1875. if (! std::filesystem::is_directory (dir))
  1876. continue;
  1877. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  1878. if (entry.path().extension() == ".fst")
  1879. {
  1880. ++fxPresets;
  1881. std::ifstream in (entry.path(), std::ios::binary);
  1882. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  1883. fxDecoded += lynx::effectFromFlPreset (bytes).has_value();
  1884. }
  1885. }
  1886. std::printf (" %d FL effect presets, %d read\n", fxPresets, fxDecoded);
  1887. CHECK (fxDecoded == fxPresets);
  1888. CHECK (wrappedParsed == wrapped);
  1889. std::printf (" %d plugins in Fruity Wrapper, %d read\n", wrapped, wrappedParsed);
  1890. std::printf (" %d markers, %d time signature changes, %d tracks with EQ\n", markers, signatures, trackEqs);
  1891. std::printf (" %d parametric EQs (%d with FL settings), %d reverbs (%d without dry)\n", peqSlots, peqShaped, reverbs, reverbsDryless);
  1892. std::printf (" DirectWave: %d channels, %d zones; FPC: %d projects, %d pad channels, %d pad notes\n", dwChannels, dwZones, fpcKits,
  1893. padChannels, padNotes);
  1894. CHECK (dwZones > 1000 && padChannels > 250 && padNotes > 500);
  1895. std::printf (" sliced channels (Slicer/Slicex): %d; embedded WAVs saved: %d\n", slicedChannels, embeddedSaved);
  1896. CHECK (slicedChannels >= 25 && embeddedSaved >= 10); // without decoding Ogg here, some loops have no duration
  1897. std::printf (" FL Keys (Grand Piano, 30 zones): %d channels\n", flKeysChannels);
  1898. CHECK (flKeysChannels >= 8);
  1899. CHECK (peqSlots > 1000 && peqShaped > 900 && reverbsDryless > 20 && reverbsLowCut > 250);
  1900. CHECK (markers > 300 && signatures > 60);
  1901. std::printf (" %d 3x Osc channels, %d decoded, %d on the exact engine (%d sounding)\n", threeOsc, threeOscDecoded, threeOscKind, threeOscSounding);
  1902. // one demo channel (a noise layer of "Right Night feeling") has the channel filter closed (fast LP, Mod X 0): silent
  1903. // with its saved settings, in FL too
  1904. CHECK (threeOscKind == threeOsc && threeOscSounding == threeOscKind - 1);
  1905. std::printf (" %d .flp files read, %d with notes, %d with playlist, %d automations (%d with target), %d sends\n",
  1906. files, withNotes, withPlaylist, automations, automationsWithTarget, sends);
  1907. CHECK (files > 0);
  1908. CHECK (automations > 0 && sends > 0);
  1909. }
  1910. // ---------------------------------------------------------------------------
  1911. // Sequencer
  1912. TEST (pattern_toggle_step_and_length)
  1913. {
  1914. lynx::Pattern p;
  1915. CHECK (! p.hasStep (0, 4));
  1916. p.toggleStep (0, 4);
  1917. CHECK (p.hasStep (0, 4));
  1918. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * lynx::ticksPerStep);
  1919. CHECK (p.effectiveLength (4) == 4 * lynx::ppq); // one bar
  1920. p.toggleStep (1, 20); // second bar
  1921. CHECK (p.effectiveLength (4) == 8 * lynx::ppq);
  1922. p.toggleStep (0, 4);
  1923. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  1924. }
  1925. TEST (scheduler_places_events_at_exact_samples)
  1926. {
  1927. lynx::Pattern p;
  1928. p.toggleStep (0, 0);
  1929. p.toggleStep (0, 4); // tick 960 = 1 beat
  1930. const double sr = 48000.0, bpm = 120.0; // 1 beat = 0.5 s = 24000 samples
  1931. const auto tps = lynx::ticksPerSample (bpm, sr);
  1932. lynx::EventBuffer ev;
  1933. lynx::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  1934. // the beat-2 note starts at sample 24000 -> offset 100 in this block
  1935. int ons = 0;
  1936. for (auto& e : ev)
  1937. if (e.type == lynx::NoteEvent::Type::on)
  1938. {
  1939. ++ons;
  1940. CHECK (e.sampleOffset == 100);
  1941. }
  1942. CHECK (ons == 1);
  1943. }
  1944. TEST (scheduler_wraps_the_loop)
  1945. {
  1946. lynx::Pattern p;
  1947. p.toggleStep (2, 0);
  1948. const auto len = p.effectiveLength (4); // 3840 ticks
  1949. const double sr = 48000.0;
  1950. const auto tps = lynx::ticksPerSample (120.0, sr);
  1951. lynx::EventBuffer ev;
  1952. // block that crosses the loop end: starts 10 samples before
  1953. const auto start = (double) len - 10.0 * tps;
  1954. lynx::schedulePatternLoop (p, len, start, 64, tps, ev);
  1955. bool found = false;
  1956. for (auto& e : ev)
  1957. if (e.type == lynx::NoteEvent::Type::on && e.channel == 2)
  1958. {
  1959. found = true;
  1960. CHECK (e.sampleOffset == 10);
  1961. }
  1962. CHECK (found);
  1963. }
  1964. TEST (scheduler_off_before_on_at_same_sample)
  1965. {
  1966. lynx::Pattern p;
  1967. lynx::Note a;
  1968. a.start = 0; a.length = 240; a.channel = 0;
  1969. lynx::Note b = a;
  1970. b.start = 240;
  1971. p.notes = { a, b };
  1972. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  1973. lynx::EventBuffer ev;
  1974. lynx::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  1975. CHECK (ev.size() >= 2);
  1976. CHECK (ev[0].type == lynx::NoteEvent::Type::off);
  1977. CHECK (ev[1].type == lynx::NoteEvent::Type::on);
  1978. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  1979. }
  1980. TEST (snapshot_exchange_hands_over_and_recycles)
  1981. {
  1982. struct Counted
  1983. {
  1984. int value;
  1985. int* alive;
  1986. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  1987. ~Counted() { --*alive; }
  1988. };
  1989. int alive = 0;
  1990. {
  1991. lynx::SnapshotExchange<Counted> x;
  1992. CHECK (x.acquire() == nullptr);
  1993. x.publish (std::make_unique<Counted> (1, &alive));
  1994. CHECK (x.acquire()->value == 1);
  1995. x.publish (std::make_unique<Counted> (2, &alive));
  1996. x.publish (std::make_unique<Counted> (3, &alive)); // 2 was never read: freed immediately
  1997. CHECK (alive == 2);
  1998. CHECK (x.acquire()->value == 3);
  1999. CHECK (alive == 2); // 1 was returned, but only the UI frees it
  2000. x.collectGarbage();
  2001. CHECK (alive == 1);
  2002. // without collection, the return slots fill up and the audio side holds on to the current one
  2003. for (int i = 0; i < 20; ++i)
  2004. {
  2005. x.publish (std::make_unique<Counted> (10 + i, &alive));
  2006. CHECK (x.acquire() != nullptr);
  2007. }
  2008. }
  2009. CHECK (alive == 0);
  2010. }
  2011. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  2012. {
  2013. auto song = lynx::Song::makeDefault();
  2014. song.bpm = 120.0;
  2015. const double sr = 48000.0;
  2016. auto state = lynx::RackState::build (song, nullptr, sr);
  2017. lynx::RackRenderer r;
  2018. r.prepare (sr);
  2019. std::vector<float> l (512), rr (512);
  2020. double tick = 0.0;
  2021. float peak = 0.0f;
  2022. int kicks = 0;
  2023. for (int block = 0; block < 93; ++block) // 47616 samples: just before 1 s
  2024. {
  2025. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  2026. for (auto& e : r.lastEvents())
  2027. if (e.type == lynx::NoteEvent::Type::on && e.channel == 0)
  2028. ++kicks;
  2029. for (int i = 0; i < 512; ++i)
  2030. {
  2031. CHECK (std::isfinite (l[(std::size_t) i]));
  2032. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  2033. }
  2034. tick += 512 * lynx::ticksPerSample (song.bpm, sr);
  2035. }
  2036. CHECK (peak > 0.1f && peak < 4.0f);
  2037. CHECK (kicks == 2); // 120 BPM: beats at 0 and 0.5 s
  2038. // editing the song keeps the instruments of channels whose type didn't change
  2039. song.channels[4].instrument = lynx::InstrumentKind::kick;
  2040. auto next = lynx::RackState::build (song, state.get(), sr);
  2041. CHECK (next->instruments[0] == state->instruments[0]);
  2042. CHECK (next->instruments[4] != state->instruments[4]);
  2043. // stopping silences the synth and triggers no new notes
  2044. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  2045. CHECK (r.lastEvents().size() == 0);
  2046. }
  2047. TEST (flp_import_maps_channels_and_rescales)
  2048. {
  2049. flp::Project p;
  2050. p.header.ppq = 96;
  2051. p.tempo = 150.0;
  2052. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  2053. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  2054. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  2055. flp::Pattern pat;
  2056. pat.index = 1;
  2057. flp::Note n;
  2058. n.position = 96;
  2059. n.length = 0;
  2060. n.channel = 2;
  2061. n.key = 72;
  2062. n.velocity = 64;
  2063. pat.notes.push_back (n);
  2064. p.patterns.push_back (pat);
  2065. const auto s = lynx::importFlp (p);
  2066. CHECK (s.bpm == 150.0);
  2067. CHECK (s.channels.size() == 3); // audio clip channels stay in the rack, as in FL
  2068. CHECK (s.channels[0].instrument == lynx::InstrumentKind::kick);
  2069. CHECK (s.channels[1].instrument == lynx::InstrumentKind::audioClip);
  2070. CHECK (s.channels[2].instrument == lynx::InstrumentKind::synth);
  2071. CHECK (s.patterns.size() == 1);
  2072. CHECK (s.patterns[0].notes[0].start == lynx::ppq); // 96 @ PPQ 96 = 1 beat
  2073. CHECK (s.patterns[0].notes[0].channel == 2);
  2074. CHECK (s.patterns[0].notes[0].length == lynx::ticksPerStep); // step note
  2075. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  2076. }
  2077. TEST (live_queue_is_fifo_and_bounded)
  2078. {
  2079. lynx::LiveEventQueue q;
  2080. lynx::NoteEvent e;
  2081. CHECK (! q.pop (e));
  2082. for (int i = 0; i < 10; ++i)
  2083. {
  2084. lynx::NoteEvent in;
  2085. in.key = i;
  2086. CHECK (q.push (in));
  2087. }
  2088. for (int i = 0; i < 10; ++i)
  2089. CHECK (q.pop (e) && e.key == i);
  2090. int pushed = 0;
  2091. while (q.push (e))
  2092. ++pushed;
  2093. CHECK (pushed == (int) lynx::LiveEventQueue::capacity - 1); // one slot stays free to tell full from empty
  2094. }
  2095. // FL's sampler interpolators (REA_rodada69 §1): outputs of FLEngine's own kernels (EXEC, r69/py/gold.py) on a synthetic
  2096. // 16-bit stereo signal; the port must give the same bits (linear, 6-point, 24-point sinc, 32-point sinc at speed 1.37,
  2097. // 16-point sinc widened to 24 points with the cutoff at π/9.5 at speed 9.5)
  2098. TEST (fl_sampler_interpolators_match_fl)
  2099. {
  2100. std::vector<float> l, r;
  2101. std::uint32_t x = 12345;
  2102. for (int k = 0; k < 2 * 144; ++k)
  2103. {
  2104. x = x * 1103515245u + 12345u;
  2105. const auto v = (float) (std::int16_t) (std::uint16_t) (x >> 16) / 32768.0f;
  2106. (k % 2 == 0 ? l : r).push_back (v);
  2107. }
  2108. const int pad = 40;
  2109. 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 } };
  2110. struct Case { int quality; double speed; std::uint32_t bits[16]; };
  2111. const Case cases[] = {
  2112. { 1, 1.0, { 0x3ec1a000, 0x3d774000, 0x3ca1e000, 0x3ec39900, 0xbf2d4600, 0xbe096800, 0xbea0cd17, 0x3f433097, 0x3f2796da, 0xbd9fbd17, 0x3f27a3ff, 0xbda03ff2, 0x3dace746, 0x3eba19c4, 0x3e477a00, 0x3f0f2a80 } },
  2113. { 2, 1.0, { 0x3ec1a000, 0x3d773fff, 0x3d34885d, 0x3eedeaa0, 0xbf49e298, 0xbea52fc4, 0xbeba55fc, 0x3f7c58da, 0x3f27c5c2, 0xbda139af, 0x3f27a3ff, 0xbda03ffa, 0x3dd8e7b1, 0x3ee872c1, 0x3e05b2d5, 0x3f144c4f } },
  2114. { 4, 1.0, { 0x3ec19fff, 0x3d773fff, 0x3d889c32, 0x3ef6b184, 0xbf57cf0d, 0xbf0abecd, 0xbea14a13, 0x3f94f11b, 0x3f27edc4, 0xbda10d8c, 0x3f27a3fd, 0xbda03ffa, 0x3e5baf41, 0x3edb298c, 0x3dd76cbf, 0x3ef9d212 } },
  2115. { 5, 1.37, { 0x3f19d373, 0x3ea9b6a3, 0xbdcbe56e, 0x3e2af6c6, 0xbf476f7a, 0xbe566a13, 0xbe9f166a, 0x3f52b5ad, 0x3f27ad16, 0xbce48792, 0x3f27960a, 0xbce01ebe, 0x3e1b96be, 0x3e163fd8, 0x3f042e42, 0x3ec4be83 } },
  2116. { 3, 9.5, { 0xbd97308e, 0x3dd8783c, 0xbd911374, 0x3e162a41, 0xbd8a959e, 0x3e38b220, 0xbd7e6785, 0x3e58895c, 0xbd380c4f, 0x3e7e3008, 0xbd380e4e, 0x3e7e2696, 0x3d00c6d0, 0xbd627d87, 0x3e308cb8, 0xbcece918 } },
  2117. };
  2118. const lynx::flvp::X87Extended precision;
  2119. for (const auto& c : cases)
  2120. {
  2121. lynx::flrs::Kernel k;
  2122. k.setQuality (c.quality);
  2123. k.setSpeed (c.speed);
  2124. int same = 0;
  2125. for (int p = 0; p < 8; ++p)
  2126. {
  2127. const auto lo = pad + positions[p].first - k.halfWidth + 1;
  2128. float a, b;
  2129. lynx::flrs::interpolate (k, l.data() + lo, r.data() + lo, positions[p].second, a, b);
  2130. std::uint32_t ba, bb;
  2131. std::memcpy (&ba, &a, 4);
  2132. std::memcpy (&bb, &b, 4);
  2133. same += (ba == c.bits[2 * p]) + (bb == c.bits[2 * p + 1]);
  2134. }
  2135. CHECK (same == 16);
  2136. }
  2137. CHECK (lynx::flrs::halfWidthFor (4) == 12 && lynx::flrs::halfWidthFor (5) == 16 && lynx::flrs::halfWidthFor (9) == 256);
  2138. CHECK (lynx::flrs::speedFor (0.0f, 1.0) == 1.0 && std::abs (lynx::flrs::speedFor (1200.0f, 0.5) - 1.0) < 1e-12);
  2139. }
  2140. // The sampler at the original rate copies the sample (FL's copy path), and plays until the read position passes the
  2141. // last frame
  2142. TEST (fl_sampler_copies_at_original_rate)
  2143. {
  2144. auto data = std::make_shared<lynx::SampleData>();
  2145. data->sampleRate = 48000.0;
  2146. for (int i = 0; i < 300; ++i)
  2147. data->left.push_back (std::sin (0.05f * (float) i) * 0.5f);
  2148. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2149. sampler->prepare (48000.0);
  2150. sampler->noteOn (60, 1.0f);
  2151. std::vector<float> l (400), r (400);
  2152. sampler->render (l.data(), r.data(), 400, 1.0f, 1.0f);
  2153. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f; // the sampler's centre pan gain
  2154. int exact = 0;
  2155. for (int i = 0; i < 300; ++i)
  2156. exact += l[(std::size_t) i] == data->left[(std::size_t) i] * panL ? 1 : 0;
  2157. CHECK (exact == 300);
  2158. CHECK (l[300] == 0.0f);
  2159. }
  2160. // FL's volume envelope on the sampler (REA_rodada69 §3): the articulator is stepped once per sequencer tick and the level
  2161. // ramps linearly to each tick's value; 96 PPQ at 120 BPM and 48 kHz = 250 samples per tick, attack 20000 = 89 ticks
  2162. TEST (fl_sampler_volume_envelope_steps_per_tick)
  2163. {
  2164. auto data = std::make_shared<lynx::SampleData>();
  2165. data->sampleRate = 48000.0;
  2166. data->left.assign (96000, 0.5f);
  2167. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2168. sampler->prepare (48000.0);
  2169. lynx::ChannelDef c;
  2170. c.instrument = lynx::InstrumentKind::sampler;
  2171. c.sampler.flVolumeEnvelope = { 0, 1, 100, 20000, 0, 30000, 128, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0, 96 };
  2172. sampler->configure (c, 120.0);
  2173. sampler->noteOn (60, 1.0f);
  2174. std::vector<float> l (40000), r (40000);
  2175. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2176. const auto panL = std::cos ((0.0f + 1.0f) * 0.25f * 3.14159265f) * 1.41421356f;
  2177. auto level = [&] (int n) { return l[(std::size_t) n] / (0.5f * panL); };
  2178. CHECK (std::abs (level (1000) - 1000.0f / 22500.0f) < 2e-5f);
  2179. CHECK (std::abs (level (10125) - 10125.0f / 22500.0f) < 2e-5f);
  2180. CHECK (std::abs (level (39000) - 1.0f) < 1e-6f); // the attack ended after 90 ticks; sustain 128 = 1.0
  2181. // release: 20000 = 89 ticks, linear (tension 0), from the current level
  2182. sampler->noteOff (60);
  2183. std::fill (l.begin(), l.end(), 0.0f);
  2184. sampler->render (l.data(), r.data(), 40000, 1.0f, 1.0f);
  2185. CHECK (std::abs (level (2500) - (1.0f - 10.0f / 90.0f)) < 1e-3f);
  2186. CHECK (l[39999] == 0.0f && sampler->idle());
  2187. }
  2188. // FPC's "cycle" layers (REA_rodada69 §4): among the layers covering the velocity, in order, the one after the last
  2189. // chosen, wrapping; FL's velocity index is round(v·128). Slicex's master volume uses FL's L11 law (§5).
  2190. TEST (fl_fpc_cycle_layers_and_slicex_master_volume)
  2191. {
  2192. auto level = [] (float v) {
  2193. auto d = std::make_shared<lynx::SampleData>();
  2194. d->sampleRate = 48000.0;
  2195. d->left.assign (4800, v);
  2196. return d;
  2197. };
  2198. // A covers 0…63, B 32…95, C 64…127 (all keys)
  2199. auto inst = lynx::makeZonedSampler ({ { { "a.wav", 60, 0, 127, 0, 63 }, level (0.1f) },
  2200. { { "b.wav", 60, 0, 127, 32, 95 }, level (0.2f) },
  2201. { { "c.wav", 60, 0, 127, 64, 127 }, level (0.3f) } });
  2202. inst->prepare (48000.0);
  2203. inst->configure (lynx::ChannelDef {}, 120.0);
  2204. auto play = [&] (float velocity) {
  2205. inst->allNotesOff();
  2206. std::vector<float> l (2000), r (2000);
  2207. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2208. std::fill (l.begin(), l.end(), 0.0f);
  2209. inst->noteOn (60, velocity);
  2210. inst->render (l.data(), r.data(), 10, 1.0f, 1.0f);
  2211. return l[5] / velocity;
  2212. };
  2213. auto which = [] (float x) { return (int) std::lround (x / 0.1f / std::cos (0.25f * 3.14159265f) / 1.41421356f); };
  2214. CHECK (which (play (64.0f / 128.0f)) == 2); // candidates B, C: first B
  2215. CHECK (which (play (64.0f / 128.0f)) == 3); // then C
  2216. CHECK (which (play (40.0f / 128.0f)) == 1); // candidates A, B: C is not among them → the first, A
  2217. CHECK (which (play (40.0f / 128.0f)) == 2); // then B
  2218. CHECK (which (play (64.0f / 128.0f)) == 3); // B was last → C
  2219. // Slicex state: version, 8 master values (volume 16000), options and UI state, deck A referencing a file
  2220. std::vector<std::uint8_t> state (4 + 32 + 47 + 12, 0);
  2221. const std::int32_t version = 0x00060003, volume = 16000;
  2222. std::memcpy (state.data(), &version, 4);
  2223. std::memcpy (state.data() + 4 + 8, &volume, 4);
  2224. state.push_back (1);
  2225. state.push_back (5);
  2226. for (char ch : std::string ("x.wav"))
  2227. state.push_back ((std::uint8_t) ch);
  2228. const auto sliced = lynx::slicex (state);
  2229. CHECK (sliced.has_value() && sliced->path == "x.wav");
  2230. CHECK (sliced && std::abs (sliced->gain - (float) ((std::pow (11.0, 1.25) - 1.0) / 10.0)) < 1e-5f);
  2231. }
  2232. TEST (sampler_plays_pitched_one_shot)
  2233. {
  2234. // 1000-frame ramp sample: the position can be measured from the amplitude
  2235. auto data = std::make_shared<lynx::SampleData>();
  2236. data->sampleRate = 48000.0;
  2237. for (int i = 0; i < 1000; ++i)
  2238. data->left.push_back (1.0f);
  2239. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2240. sampler->prepare (48000.0);
  2241. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  2242. sampler->noteOn (60, 1.0f);
  2243. sampler->noteOff (60); // one-shot: noteOff doesn't cut
  2244. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2245. int sounding = 0;
  2246. for (auto s : l)
  2247. sounding += s > 0.5f;
  2248. CHECK (sounding >= 995 && sounding <= 1000); // plays the whole sample at the original pitch
  2249. std::fill (l.begin(), l.end(), 0.0f);
  2250. sampler->noteOn (72, 1.0f); // one octave up: half the duration
  2251. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  2252. sounding = 0;
  2253. for (auto s : l)
  2254. sounding += s > 0.5f;
  2255. CHECK (sounding >= 495 && sounding <= 500);
  2256. CHECK (r[10] > 0.5f); // mono goes to both sides
  2257. auto silent = lynx::makeInstrument (lynx::InstrumentKind::sampler, nullptr);
  2258. silent->prepare (48000.0);
  2259. silent->noteOn (60, 1.0f);
  2260. std::fill (l.begin(), l.end(), 0.0f);
  2261. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  2262. CHECK (l[0] == 0.0f);
  2263. }
  2264. TEST (rack_live_notes_and_sample_provider)
  2265. {
  2266. lynx::Song song;
  2267. lynx::ChannelDef c;
  2268. c.name = "Smp";
  2269. c.instrument = lynx::InstrumentKind::sampler;
  2270. c.samplePath = "a.wav";
  2271. song.channels.push_back (c);
  2272. song.patterns.emplace_back();
  2273. int loads = 0;
  2274. auto provider = [&] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  2275. ++loads;
  2276. CHECK (path == "a.wav");
  2277. auto d = std::make_shared<lynx::SampleData>();
  2278. d->left.assign (4800, 0.5f);
  2279. return d;
  2280. };
  2281. auto state = lynx::RackState::build (song, nullptr, 48000.0, provider);
  2282. CHECK (loads == 1);
  2283. lynx::RackRenderer renderer;
  2284. renderer.prepare (48000.0);
  2285. lynx::LiveEventQueue live;
  2286. lynx::NoteEvent on;
  2287. on.channel = 0;
  2288. on.velocity = 1.0f;
  2289. live.push (on);
  2290. std::vector<float> l (256), r (256);
  2291. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // stopped, but the live note plays
  2292. CHECK (l[0] > 0.1f);
  2293. // same sample: reused; different sample: reloaded
  2294. auto same = lynx::RackState::build (song, state.get(), 48000.0, provider);
  2295. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  2296. song.channels[0].samplePath = "b.wav";
  2297. auto other = lynx::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  2298. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  2299. }
  2300. TEST (flp_paths_resolve_and_samplers_import)
  2301. {
  2302. lynx::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  2303. CHECK (lynx::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  2304. CHECK (lynx::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  2305. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  2306. CHECK (lynx::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  2307. CHECK (lynx::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  2308. flp::Project p;
  2309. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  2310. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  2311. lynx::FlpImportOptions options;
  2312. options.roots = roots;
  2313. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  2314. const auto s = lynx::importFlp (p, options);
  2315. CHECK (s.channels[0].instrument == lynx::InstrumentKind::sampler);
  2316. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  2317. CHECK (s.channels[1].instrument == lynx::InstrumentKind::hat); // no file: built-in instrument
  2318. }
  2319. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  2320. {
  2321. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  2322. lynx::softClip (s, 5);
  2323. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  2324. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  2325. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  2326. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  2327. }
  2328. TEST (song_scheduler_plays_clips_with_offset_and_cut)
  2329. {
  2330. lynx::Song s;
  2331. s.channels.resize (1);
  2332. auto& p = s.patterns.emplace_back();
  2333. for (int step : { 0, 4, 8, 12 }) // one note per beat
  2334. p.toggleStep (0, step);
  2335. // clip at bar 2 playing only the second half of the pattern (beats 3 and 4)
  2336. lynx::Clip c;
  2337. c.pattern = 0;
  2338. c.start = 4 * lynx::ppq;
  2339. c.length = 2 * lynx::ppq;
  2340. c.offset = 2 * lynx::ppq;
  2341. s.playlist.push_back (c);
  2342. CHECK (s.songLength() == 8 * lynx::ppq);
  2343. const double sr = 48000.0;
  2344. const auto tps = lynx::ticksPerSample (120.0, sr); // 1 beat = 24000 samples
  2345. lynx::EventBuffer ev;
  2346. // whole song in a single block (8 beats = 192000 samples)
  2347. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2348. std::vector<int> ons;
  2349. for (auto& e : ev)
  2350. if (e.type == lynx::NoteEvent::Type::on)
  2351. ons.push_back (e.sampleOffset);
  2352. CHECK (ons.size() == 2);
  2353. CHECK (ons.size() == 2 && ons[0] == 4 * 24000 && ons[1] == 5 * 24000);
  2354. // crossing the loop: bar 1 is empty, so nothing at the start of the second pass
  2355. ev.clear();
  2356. lynx::scheduleSong (s, 8.0 * lynx::ppq - 100 * tps, 200, tps, ev);
  2357. CHECK (ev.size() == 0);
  2358. // a note longer than the clip is cut at the clip end
  2359. p.notes[3].length = 4 * lynx::ppq;
  2360. ev.clear();
  2361. lynx::scheduleSong (s, 0.0, 192000, tps, ev);
  2362. int lastOff = -1;
  2363. for (auto& e : ev)
  2364. if (e.type == lynx::NoteEvent::Type::off)
  2365. lastOff = std::max (lastOff, e.sampleOffset);
  2366. CHECK (lastOff == 6 * 24000);
  2367. }
  2368. TEST (remove_pattern_fixes_clips)
  2369. {
  2370. lynx::Song s;
  2371. s.patterns.resize (3);
  2372. s.currentPattern = 2;
  2373. s.playlist = { { 0, 0, 0, 100, 0 }, { 1, 0, 100, 100, 0 }, { 2, 1, 0, 100, 0 } };
  2374. s.removePattern (1);
  2375. CHECK (s.patterns.size() == 2);
  2376. CHECK (s.playlist.size() == 2 && s.playlist[1].pattern == 1);
  2377. CHECK (s.currentPattern == 1);
  2378. }
  2379. TEST (flp_import_3x_osc_state_and_channel_filter)
  2380. {
  2381. // new 3x Osc patch (version 14): osc 2 mix 64 coarse -12, osc 3 mix 32 coarse -24
  2382. std::vector<std::uint8_t> blob (460, 0);
  2383. auto put = [&] (std::size_t off, std::int32_t v) {
  2384. for (int b = 0; b < 4; ++b)
  2385. blob[off + (std::size_t) b] = (std::uint8_t) ((std::uint32_t) v >> (8 * b));
  2386. };
  2387. put (0x00, 14);
  2388. put (0x08, 3); // osc 1 saw
  2389. put (0x10, 7); // osc 1 fine +7
  2390. put (0x04, -32); // osc 1 pan -0.5
  2391. put (0x1C, 64); // osc 2 mix
  2392. put (0x28, -12); // osc 2 coarse
  2393. put (0x38, 32); // osc 3 mix
  2394. put (0x40, 2); // osc 3 square
  2395. put (0x44, -19); // osc 3 coarse: -1 octave and -7 semitones
  2396. lynx::SynthSettings s;
  2397. CHECK (lynx::synthFrom3xOsc (blob, s));
  2398. 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);
  2399. CHECK (s.osc[0].wave == lynx::Waveform::saw && s.osc[0].detune == 7.0f && s.osc[0].pan == -0.5f);
  2400. CHECK (s.osc[1].wave == lynx::Waveform::sine && s.osc[1].octave == -1 && s.osc[1].semitones == 0);
  2401. CHECK (s.osc[2].wave == lynx::Waveform::square && s.osc[2].octave == -1 && s.osc[2].semitones == -7);
  2402. CHECK (! lynx::synthFrom3xOsc (std::vector<std::uint8_t> (40, 0), s));
  2403. // channel filter: open by default; LP with Mod X 128 ~ 2.71 kHz; Fast LP with Mod X 128 ~ 280 Hz
  2404. float hz = 0.0f, res = 1.0f;
  2405. int mode = -1;
  2406. lynx::synthFilterFromChannel (256, 0, 0, hz, res, mode);
  2407. CHECK (hz >= 19999.0f && res == 0.0f);
  2408. lynx::synthFilterFromChannel (128, 0, 1, hz, res, mode);
  2409. CHECK (std::abs (hz - 2710.0f) < 30.0f && res == 0.0f);
  2410. lynx::synthFilterFromChannel (128, 0, 0, hz, res, mode);
  2411. CHECK (hz > 240.0f && hz < 320.0f);
  2412. lynx::synthFilterFromChannel (128, 256, 1, hz, res, mode);
  2413. CHECK (res > 0.8f);
  2414. CHECK (mode == 0);
  2415. lynx::synthFilterFromChannel (64, 0, 3, hz, res, mode); // high-pass at ~840 Hz
  2416. CHECK (mode == 2 && std::abs (hz - 840.0f) < 40.0f);
  2417. lynx::synthFilterFromChannel (128, 0, 2, hz, res, mode); // band-pass
  2418. CHECK (mode == 1);
  2419. }
  2420. TEST (synth_pitch_and_filter_envelopes)
  2421. {
  2422. auto render = [] (const lynx::SynthSettings& s, int frames) {
  2423. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2424. inst->prepare (48000.0);
  2425. lynx::ChannelDef c;
  2426. c.synth = s;
  2427. inst->configure (c, 120.0);
  2428. inst->noteOn (45, 1.0f); // A2 = 110 Hz
  2429. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  2430. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  2431. return l;
  2432. };
  2433. auto crossings = [] (const std::vector<float>& b, int from, int to) {
  2434. int n = 0;
  2435. for (int i = from + 1; i < to; ++i)
  2436. n += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  2437. return n;
  2438. };
  2439. // 3x Osc "kick": sine with a +24 st pitch envelope falling fast
  2440. lynx::SynthSettings kick;
  2441. kick.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2442. kick.cutoff = 20000.0f;
  2443. kick.pitchEnv = { 24.0f, 0.0f, 0.0f, 0.05f, 0.0f, 0.05f };
  2444. const auto k = render (kick, 48000);
  2445. const auto early = crossings (k, 0, 2400); // first 50 ms: well above 110 Hz
  2446. const auto late = crossings (k, 24000, 48000); // then back to 110 Hz (~110 crossings in 0.5 s)
  2447. CHECK (early > 20); // +24 st ramp over 50 ms: ~2.2x the average frequency (11 crossings without envelope)
  2448. CHECK (std::abs (late - 110) < 4);
  2449. // cutoff envelope: saw closed at 200 Hz opens +5 octaves on attack and closes again
  2450. lynx::SynthSettings pluck;
  2451. pluck.cutoff = 200.0f;
  2452. pluck.filterEnv = { 5.0f, 0.0f, 0.0f, 0.1f, 0.0f, 0.1f };
  2453. const auto p = render (pluck, 48000);
  2454. auto hf = [&] (int from, int to) { // energy of the difference (highs)
  2455. double e = 0.0;
  2456. for (int i = from + 1; i < to; ++i)
  2457. e += std::pow ((double) p[(std::size_t) i] - p[(std::size_t) i - 1], 2.0);
  2458. return e;
  2459. };
  2460. CHECK (hf (0, 2400) > 4.0 * hf (40000, 42400));
  2461. // Toxic Biohazard's envelope (REA_rodada33 §2): exponential towards 5 clipped at 1, so the cutoff is still near
  2462. // its base early in the attack, fully open at A = 0.2 s and back down after the exponential decay
  2463. lynx::SynthSettings toxic = pluck;
  2464. toxic.filterEnv = { 5.0f, 0.2f, 0.0f, 0.1f, 0.0f, 0.1f, 1 };
  2465. const auto t = render (toxic, 48000);
  2466. auto thf = [&] (int from, int to) {
  2467. double e = 0.0;
  2468. for (int i = from + 1; i < to; ++i)
  2469. e += std::pow ((double) t[(std::size_t) i] - t[(std::size_t) i - 1], 2.0);
  2470. return e;
  2471. };
  2472. CHECK (thf (9000, 9600) > 4.0 * thf (300, 900));
  2473. CHECK (thf (9000, 9600) > 4.0 * thf (40000, 40600));
  2474. }
  2475. TEST (synth_mono_reuses_the_sounding_voice)
  2476. {
  2477. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2478. inst->prepare (48000.0);
  2479. lynx::ChannelDef c;
  2480. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2481. c.synth.cutoff = 20000.0f;
  2482. c.synth.mono = true;
  2483. inst->configure (c, 120.0);
  2484. std::vector<float> l (48000), r (48000);
  2485. inst->noteOn (57, 1.0f);
  2486. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2487. inst->noteOn (69, 1.0f); // A4 takes over the A3 voice: only one note sounding
  2488. inst->render (l.data() + 4800, r.data() + 4800, 43200, 1.0f, 1.0f);
  2489. int crossings = 0;
  2490. float peak = 0.0f;
  2491. for (std::size_t i = 24001; i < 48000; ++i)
  2492. {
  2493. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2494. peak = std::max (peak, std::abs (l[i]));
  2495. }
  2496. CHECK (std::abs (crossings - 440) < 4); // 0.5 s of 440 Hz, without the A3 underneath
  2497. CHECK (peak < 0.35f); // one voice (0.3 level), not two summed
  2498. // no click on the handover: the jump between neighbouring samples stays sine-sized
  2499. float jump = 0.0f;
  2500. for (std::size_t i = 4790; i < 4810; ++i)
  2501. jump = std::max (jump, std::abs (l[i] - l[i - 1]));
  2502. CHECK (jump < 0.05f);
  2503. }
  2504. TEST (synth_porta_glides_and_am_modulates)
  2505. {
  2506. auto make = [] (const lynx::SynthSettings& s) {
  2507. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2508. inst->prepare (48000.0);
  2509. lynx::ChannelDef c;
  2510. c.synth = s;
  2511. inst->configure (c, 120.0);
  2512. return inst;
  2513. };
  2514. auto crossings = [] (const std::vector<float>& b, std::size_t from, std::size_t to) {
  2515. int n = 0;
  2516. for (auto i = from + 1; i < to; ++i)
  2517. n += (b[i - 1] < 0.0f) != (b[i] < 0.0f);
  2518. return n;
  2519. };
  2520. // mono + 1-beat porta (0.5 s at 120 BPM): from A3 to A4, halfway ~E4 (329.6 Hz)
  2521. lynx::SynthSettings s;
  2522. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2523. s.cutoff = 20000.0f;
  2524. s.mono = true;
  2525. s.porta = true;
  2526. s.portaBeats = 1.0f;
  2527. auto inst = make (s);
  2528. std::vector<float> l (96000), r (96000);
  2529. inst->noteOn (57, 1.0f);
  2530. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2531. inst->noteOn (69, 1.0f);
  2532. inst->render (l.data() + 4800, r.data() + 4800, 91200, 1.0f, 1.0f);
  2533. const auto middle = crossings (l, 4800 + 12000 - 2400, 4800 + 12000 + 2400); // 0.1 s around 0.25 s
  2534. CHECK (std::abs (middle - 66) < 6); // ~330 Hz * 0.1 s * 2
  2535. CHECK (std::abs (crossings (l, 48000, 96000) - 880) < 4); // reached A4 (1 s = 880 crossings)
  2536. // AM: osc 3 at level 1.0, slow sine, modulates osc 1 (the average level drops, the sum doesn't gain osc 3)
  2537. lynx::SynthSettings am;
  2538. am.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2539. am.osc[2] = { lynx::Waveform::sine, -3, 0.0f, 1.0f };
  2540. am.cutoff = 20000.0f;
  2541. am.am = true;
  2542. auto modulated = make (am);
  2543. modulated->noteOn (69, 1.0f);
  2544. std::fill (l.begin(), l.end(), 0.0f);
  2545. modulated->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2546. float peak = 0.0f;
  2547. for (std::size_t i = 4800; i < 48000; ++i)
  2548. peak = std::max (peak, std::abs (l[i]));
  2549. CHECK (peak > 0.45f && peak < 0.65f); // 0.3 · (1 + 1) at most: (1 − o3) goes up to 2
  2550. }
  2551. TEST (channel_lfo_vibrato_and_tremolo)
  2552. {
  2553. auto render = [] (const lynx::SynthSettings& s) {
  2554. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2555. inst->prepare (48000.0);
  2556. lynx::ChannelDef c;
  2557. c.synth = s;
  2558. inst->configure (c, 120.0);
  2559. inst->noteOn (69, 1.0f);
  2560. std::vector<float> l (48000), r (48000);
  2561. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2562. return l;
  2563. };
  2564. lynx::SynthSettings s;
  2565. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2566. s.cutoff = 20000.0f;
  2567. // tremolo: 100% volume LFO at 4 Hz (0.25 s period) -> the amplitude envelope goes from ~0 to ~2x
  2568. auto tremolo = s;
  2569. tremolo.lfo[lynx::lfoVolume] = { 1.0f, 0.25f, false, 0, 0.0f, 0.0f };
  2570. const auto t = render (tremolo);
  2571. float lowest = 1.0f, highest = 0.0f;
  2572. for (std::size_t w = 4800; w + 480 <= 48000; w += 480) // 10 ms windows (4.4 cycles of 440 Hz)
  2573. {
  2574. float peak = 0.0f;
  2575. for (auto i = w; i < w + 480; ++i)
  2576. peak = std::max (peak, std::abs (t[i]));
  2577. lowest = std::min (lowest, peak);
  2578. highest = std::max (highest, peak);
  2579. }
  2580. CHECK (lowest < 0.08f && highest > 0.5f);
  2581. // synced vibrato: ±1 semitone, 1-beat period (0.5 s at 120 BPM); the average stays at 440 Hz
  2582. auto vibrato = s;
  2583. vibrato.lfo[lynx::lfoPitch] = { 1.0f, 1.0f, true, 0, 0.0f, 0.0f };
  2584. const auto v = render (vibrato);
  2585. int crossings = 0;
  2586. for (std::size_t i = 1; i < 48000; ++i)
  2587. crossings += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2588. CHECK (std::abs (crossings - 880) < 12);
  2589. int firstQuarter = 0; // first quarter of the cycle: rising sine -> higher than 440
  2590. for (std::size_t i = 1; i < 6000; ++i)
  2591. firstQuarter += (v[i - 1] < 0.0f) != (v[i] < 0.0f);
  2592. CHECK (firstQuarter > 112);
  2593. }
  2594. TEST (sampler_porta_glides_from_previous_note)
  2595. {
  2596. auto data = std::make_shared<lynx::SampleData>();
  2597. data->sampleRate = 48000.0;
  2598. data->left.resize (96000);
  2599. for (std::size_t i = 0; i < data->left.size(); ++i)
  2600. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 220.0 * (double) i / 48000.0);
  2601. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2602. inst->prepare (48000.0);
  2603. lynx::ChannelDef c;
  2604. c.sampler.cut = true;
  2605. c.sampler.porta = true;
  2606. c.sampler.portaBeats = 1.0f; // 0.5 s at 120 BPM
  2607. inst->configure (c, 120.0);
  2608. std::vector<float> l (24000), r (24000);
  2609. inst->noteOn (60, 1.0f);
  2610. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  2611. inst->noteOn (72, 1.0f); // one octave up: starts at 220 Hz and rises
  2612. std::fill (l.begin(), l.end(), 0.0f);
  2613. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2614. auto crossings = [&] (std::size_t from, std::size_t to) {
  2615. int n = 0;
  2616. for (auto i = from + 1; i < to; ++i)
  2617. n += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2618. return n;
  2619. };
  2620. const auto start = crossings (600, 2400); // 37.5 ms still near 220 Hz: ~17 crossings (440 Hz would give 33)
  2621. const auto end = crossings (21600, 24000); // 50 ms at the end of the glide: near 440 Hz (~44; 220 Hz would give 22)
  2622. CHECK (start < 22);
  2623. CHECK (end > 36);
  2624. }
  2625. TEST (zoned_sampler_picks_zone_by_key_and_velocity)
  2626. {
  2627. auto sine = [] (double hz, float amp) {
  2628. auto d = std::make_shared<lynx::SampleData>();
  2629. d->sampleRate = 48000.0;
  2630. d->left.resize (48000);
  2631. for (std::size_t i = 0; i < d->left.size(); ++i)
  2632. d->left[i] = amp * (float) std::sin (6.283185307 * hz * (double) i / 48000.0);
  2633. return d;
  2634. };
  2635. // lows: 220 Hz sine recorded as A3 (57), up to key 69; highs: 880 Hz as A5 (81)
  2636. // hard layer on the highs: same tuning, louder
  2637. std::vector<lynx::LoadedZone> zones {
  2638. { { "graves.wav", 57, 0, 69, 0, 127, 0.0f, 1.0f }, sine (220.0, 0.5f) },
  2639. { { "agudos.wav", 81, 70, 127, 0, 99, 0.0f, 1.0f }, sine (880.0, 0.25f) },
  2640. { { "agudos forte.wav", 81, 70, 127, 100, 127, 0.0f, 1.0f }, sine (880.0, 0.9f) },
  2641. };
  2642. auto inst = lynx::makeZonedSampler (zones);
  2643. inst->prepare (48000.0);
  2644. inst->configure (lynx::ChannelDef {}, 120.0);
  2645. auto play = [&] (int key, float velocity) {
  2646. inst->allNotesOff();
  2647. std::vector<float> l (24000), r (24000);
  2648. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f); // let the old voices die out
  2649. std::fill (l.begin(), l.end(), 0.0f);
  2650. inst->noteOn (key, velocity);
  2651. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2652. int crossings = 0;
  2653. float peak = 0.0f;
  2654. for (std::size_t i = 1; i < 24000; ++i)
  2655. {
  2656. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  2657. peak = std::max (peak, std::abs (l[i]));
  2658. }
  2659. return std::pair { crossings, peak };
  2660. };
  2661. const auto low = play (69, 0.5f); // A4 from root A3: 440 Hz -> 440 crossings in 0.5 s
  2662. CHECK (std::abs (low.first - 440) < 4);
  2663. const auto soft = play (81, 0.5f); // high zone at the root: 880 Hz
  2664. CHECK (std::abs (soft.first - 880) < 6);
  2665. const auto hard = play (81, 0.95f); // high velocity: hard layer
  2666. CHECK (hard.second > soft.second * 2.0f);
  2667. // overlapping layers round-robin: two identical zones at different levels, notes alternate
  2668. auto rr = lynx::makeZonedSampler ({ { { "a.wav", 60 }, sine (440.0, 0.2f) }, { { "b.wav", 60 }, sine (440.0, 0.8f) } });
  2669. rr->prepare (48000.0);
  2670. rr->configure (lynx::ChannelDef {}, 120.0);
  2671. float peaks[2] {};
  2672. for (int k = 0; k < 2; ++k)
  2673. {
  2674. rr->allNotesOff();
  2675. std::vector<float> l (4800), r (4800);
  2676. rr->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  2677. std::fill (l.begin(), l.end(), 0.0f);
  2678. rr->noteOn (60, 1.0f);
  2679. rr->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2680. for (auto v : l)
  2681. peaks[k] = std::max (peaks[k], std::abs (v));
  2682. }
  2683. CHECK (std::abs (peaks[0] - peaks[1]) > 0.3f);
  2684. }
  2685. TEST (zoned_sampler_loops_while_held)
  2686. {
  2687. // 0.1 s sample with a loop in the middle: holding the note it keeps sounding well past the end of the file;
  2688. // on release, it leaves the loop and ends
  2689. auto data = std::make_shared<lynx::SampleData>();
  2690. data->sampleRate = 48000.0;
  2691. data->left.resize (4800);
  2692. for (std::size_t i = 0; i < data->left.size(); ++i)
  2693. data->left[i] = 0.5f * (float) std::sin (6.283185307 * 480.0 * (double) i / 48000.0); // 100 frames per cycle
  2694. lynx::SampleZone zone { "loop.wav", 60 };
  2695. zone.loopStart = 1000;
  2696. zone.loopEnd = 3000;
  2697. zone.loopMode = 2;
  2698. auto inst = lynx::makeZonedSampler ({ { zone, data } });
  2699. inst->prepare (48000.0);
  2700. lynx::ChannelDef c;
  2701. c.sampler.release = 0.01f;
  2702. inst->configure (c, 120.0);
  2703. inst->noteOn (60, 1.0f);
  2704. std::vector<float> l (48000), r (48000);
  2705. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f); // 0.5 s held
  2706. float held = 0.0f;
  2707. for (std::size_t i = 20000; i < 24000; ++i)
  2708. held = std::max (held, std::abs (l[i]));
  2709. CHECK (held > 0.3f);
  2710. inst->noteOff (60);
  2711. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  2712. float after = 0.0f;
  2713. for (std::size_t i = 40000; i < 48000; ++i)
  2714. after = std::max (after, std::abs (l[i]));
  2715. CHECK (after == 0.0f);
  2716. }
  2717. TEST (global_lfo_follows_song_position)
  2718. {
  2719. // global volume LFO (1-beat period): a note mid-cycle starts mid-wave; the
  2720. // same note with a per-note LFO starts at phase 0
  2721. auto first = [] (bool global, double songTicks) {
  2722. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2723. inst->prepare (48000.0);
  2724. lynx::ChannelDef c;
  2725. c.synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2726. c.synth.cutoff = 20000.0f;
  2727. c.synth.lfo[lynx::lfoVolume] = { 1.0f, 1.0f, true, 2, 0.0f, 0.0f, global }; // pulse: +1 in the first half
  2728. inst->configure (c, 120.0);
  2729. inst->setSongPosition (songTicks);
  2730. inst->noteOn (69, 1.0f);
  2731. std::vector<float> l (4800), r (4800);
  2732. inst->render (l.data(), r.data(), 4800, 1.0f, 1.0f);
  2733. float peak = 0.0f;
  2734. for (std::size_t i = 2400; i < 4800; ++i)
  2735. peak = std::max (peak, std::abs (l[i]));
  2736. return peak;
  2737. };
  2738. // mid-cycle (phase 0.6) the pulse is at −1: volume ~0; per note (phase 0) it is at +1: ~2x
  2739. const auto globalLate = first (true, 0.6 * lynx::ppq);
  2740. const auto perNote = first (false, 0.6 * lynx::ppq);
  2741. CHECK (globalLate < 0.05f);
  2742. CHECK (perNote > 0.4f);
  2743. }
  2744. TEST (sampler_channel_filter_darkens_and_bypasses_when_open)
  2745. {
  2746. // white noise in the sampler: with a 300 Hz cutoff the energy drops a lot; open passes unchanged
  2747. auto data = std::make_shared<lynx::SampleData>();
  2748. data->sampleRate = 48000.0;
  2749. data->left.resize (48000);
  2750. std::uint32_t seed = 7;
  2751. for (auto& v : data->left)
  2752. {
  2753. seed = seed * 1664525u + 1013904223u;
  2754. v = ((float) (seed >> 8) / 16777216.0f - 0.5f);
  2755. }
  2756. data->right = data->left;
  2757. auto energy = [&] (float cutoff) {
  2758. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2759. inst->prepare (48000.0);
  2760. lynx::ChannelDef c;
  2761. c.sampler.cutoff = cutoff;
  2762. inst->configure (c, 120.0);
  2763. inst->noteOn (60, 1.0f);
  2764. std::vector<float> l (24000), r (24000);
  2765. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2766. double e = 0.0;
  2767. for (auto v : l)
  2768. e += (double) v * v;
  2769. return e;
  2770. };
  2771. const auto open = energy (20000.0f), dark = energy (300.0f);
  2772. {
  2773. // high-pass at 10 kHz: also removes most of the noise energy, but from the other side
  2774. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2775. inst->prepare (48000.0);
  2776. lynx::ChannelDef hp;
  2777. hp.sampler.cutoff = 10000.0f;
  2778. hp.sampler.filterType = 2;
  2779. inst->configure (hp, 120.0);
  2780. inst->noteOn (60, 1.0f);
  2781. std::vector<float> l (24000), r (24000);
  2782. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2783. double e = 0.0, lowE = 0.0;
  2784. float lp = 0.0f;
  2785. for (auto v : l)
  2786. {
  2787. e += (double) v * v;
  2788. lp += 0.01f * (v - lp); // crude low-pass: almost nothing should remain below
  2789. lowE += (double) lp * lp;
  2790. }
  2791. CHECK (e > 10.0 && e < 0.7 * energy (20000.0f));
  2792. CHECK (lowE < e * 0.01);
  2793. }
  2794. CHECK (open > 100.0);
  2795. CHECK (dark < open * 0.05);
  2796. // +12 st pitch envelope with sustain 1: the sample runs at double speed
  2797. // and ends in half the time (1 s of sample -> 0.5 s)
  2798. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  2799. inst->prepare (48000.0);
  2800. lynx::ChannelDef c;
  2801. c.sampler.pitchEnv = { 12.0f, 0.0f, 0.0f, 0.1f, 1.0f, 0.1f };
  2802. inst->configure (c, 120.0);
  2803. inst->noteOn (60, 1.0f);
  2804. std::vector<float> l (48000), r (48000);
  2805. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  2806. float tail = 0.0f, body = 0.0f;
  2807. for (std::size_t i = 20000; i < 23000; ++i)
  2808. body = std::max (body, std::abs (l[i]));
  2809. for (std::size_t i = 25000; i < 48000; ++i)
  2810. tail = std::max (tail, std::abs (l[i]));
  2811. CHECK (body > 0.1f);
  2812. CHECK (tail == 0.0f);
  2813. }
  2814. TEST (synth_oscillator_pan_and_semitones)
  2815. {
  2816. auto render = [] (lynx::SynthSettings s, std::vector<float>& l, std::vector<float>& r) {
  2817. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  2818. synth->prepare (48000.0);
  2819. lynx::ChannelDef c;
  2820. c.synth = s;
  2821. synth->configure (c, 120.0);
  2822. synth->noteOn (69, 1.0f);
  2823. l.assign (24000, 0.0f);
  2824. r.assign (24000, 0.0f);
  2825. synth->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  2826. };
  2827. lynx::SynthSettings s;
  2828. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f, 7, 1.0f }; // a fifth up, fully right
  2829. s.cutoff = 20000.0f;
  2830. std::vector<float> l, r;
  2831. render (s, l, r);
  2832. float peakL = 0.0f, peakR = 0.0f;
  2833. int crossings = 0;
  2834. for (std::size_t i = 4800; i < 24000; ++i)
  2835. {
  2836. peakL = std::max (peakL, std::abs (l[i]));
  2837. peakR = std::max (peakR, std::abs (r[i]));
  2838. crossings += (r[i - 1] < 0.0f) != (r[i] < 0.0f);
  2839. }
  2840. CHECK (peakL < 1e-4f && peakR > 0.1f);
  2841. // 440 * 2^(7/12) = 659.3 Hz; 0.4 s = 263.7 cycles = ~527 crossings
  2842. CHECK (std::abs (crossings - 527) < 6);
  2843. }
  2844. TEST (fl_wrapper_state_parses_records_and_vst3_state)
  2845. {
  2846. // Fruity Wrapper blob (version 12): VST3 generator type, class ID, name, path and state
  2847. std::vector<std::uint8_t> blob;
  2848. 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))); };
  2849. auto record = [&] (std::vector<std::uint8_t>& b, std::uint32_t id, const std::vector<std::uint8_t>& payload) {
  2850. u32 (b, id);
  2851. u32 (b, (std::uint32_t) payload.size());
  2852. u32 (b, 0);
  2853. b.insert (b.end(), payload.begin(), payload.end());
  2854. };
  2855. auto bytes = [] (const std::string& s) { return std::vector<std::uint8_t> (s.begin(), s.end()); };
  2856. std::vector<std::uint8_t> vst3State;
  2857. u32 (vst3State, 1);
  2858. record (vst3State, 1, std::vector<std::uint8_t> (64, 0));
  2859. record (vst3State, 3, bytes ("COMPONENTE"));
  2860. record (vst3State, 2, bytes ("CONTROLADOR"));
  2861. // record 4: 3 slots → ParamIDs 700, −1 (empty), 0xFFFFFF01
  2862. std::vector<std::uint8_t> slots;
  2863. for (auto v : { 3u, 700u, 0xFFFFFFFFu, 0xFFFFFF01u })
  2864. u32 (slots, v);
  2865. record (vst3State, 4, slots);
  2866. u32 (blob, 12);
  2867. record (blob, 0x01, std::vector<std::uint8_t> (20, 0xFF));
  2868. record (blob, 0x32, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  2869. std::vector<std::uint8_t> cid;
  2870. for (int i = 0; i < 16; ++i)
  2871. cid.push_back ((std::uint8_t) (0xA0 + i));
  2872. record (blob, 0x34, cid);
  2873. record (blob, 0x36, bytes ("Surge XT"));
  2874. record (blob, 0x37, bytes ("C:/Program Files/Common Files/VST3/Surge XT.vst3"));
  2875. record (blob, 0x35, vst3State);
  2876. lynx::ForeignPlugin p;
  2877. CHECK (lynx::parseFlWrapper (blob, p));
  2878. CHECK (p.format == lynx::ForeignPlugin::Format::vst3);
  2879. CHECK (p.hasClassId && p.classId[0] == 0xA0 && p.classId[15] == 0xAF);
  2880. CHECK (p.name == "Surge XT" && p.path.find ("Surge XT.vst3") != std::string::npos);
  2881. std::vector<std::uint8_t> component, controller;
  2882. CHECK (lynx::splitFlVst3State (p.state, component, controller));
  2883. CHECK (component == bytes ("COMPONENTE") && controller == bytes ("CONTROLADOR"));
  2884. const auto saved = lynx::flVst3ParamSlots (p.state);
  2885. CHECK (saved == (std::vector<std::int32_t> { 700, -1, (std::int32_t) 0xFFFFFF01u }));
  2886. // FL's slot assignment: saved slots keep their ParamID wherever it now sits; the empty slot stays empty;
  2887. // a vanished ParamID (0xFFFFFF01) leaves its slot dead; parameters without a slot get slots 3, 4… in list order
  2888. const auto positions = lynx::flVst3SlotPositions (saved, { 5u, 700u, 9u });
  2889. CHECK (positions == (std::vector<int> { 1, -1, -1, 0, 2 }));
  2890. CHECK (lynx::flVst3SlotPositions ({}, { 5u, 6u }) == (std::vector<int> { 0, 1 })); // new instance: slot = position
  2891. // version out of range or no type: not a usable wrapper
  2892. blob[0] = 13;
  2893. CHECK (! lynx::parseFlWrapper (blob, p));
  2894. CHECK (! lynx::parseFlWrapper ({ 12, 0, 0, 0 }, p));
  2895. }
  2896. TEST (schedule_index_matches_full_scan)
  2897. {
  2898. // random song (long notes, swing, clips with offset); indexed scheduling must yield
  2899. // exactly the same events as the full scan, including in the block that wraps around
  2900. lynx::Song s = lynx::Song::makeDefault();
  2901. s.swing = 0.6f;
  2902. std::uint32_t seed = 12345;
  2903. auto rnd = [&] (int n) { seed = seed * 1664525u + 1013904223u; return (int) ((seed >> 8) % (std::uint32_t) n); };
  2904. s.patterns.resize (3);
  2905. for (auto& p : s.patterns)
  2906. {
  2907. p.notes.clear();
  2908. for (int i = 0; i < 60; ++i)
  2909. {
  2910. lynx::Note n;
  2911. n.start = rnd (16 * lynx::ppq);
  2912. n.length = rnd (5) == 0 ? rnd (12 * lynx::ppq) + 1 : rnd (lynx::ppq) + 1;
  2913. n.channel = rnd (4);
  2914. n.key = 40 + rnd (40);
  2915. n.slide = rnd (20) == 0;
  2916. p.notes.push_back (n);
  2917. }
  2918. p.sortNotes();
  2919. }
  2920. s.playlist.clear();
  2921. for (int i = 0; i < 12; ++i)
  2922. {
  2923. lynx::Clip c;
  2924. c.pattern = rnd (3);
  2925. c.track = rnd (4);
  2926. c.start = rnd (40) * lynx::ppq;
  2927. c.length = (rnd (12) + 1) * lynx::ppq;
  2928. c.offset = rnd (4) * lynx::ppq;
  2929. s.playlist.push_back (c);
  2930. }
  2931. s.mode = lynx::PlayMode::song;
  2932. const auto index = lynx::makeScheduleIndex (s);
  2933. const auto tps = lynx::ticksPerSample (140.0, 48000.0);
  2934. const auto loop = (double) s.songLength();
  2935. int mismatches = 0, events = 0;
  2936. for (int block = 0; block < 400; ++block)
  2937. {
  2938. const auto start = block == 0 ? loop - 100.0 * tps : (double) rnd ((int) loop * 2);
  2939. const auto samples = 64 + rnd (24000); // up to ~1 beat per block
  2940. lynx::EventBuffer a, b;
  2941. lynx::scheduleSong (s, start, samples, tps, a, false, nullptr);
  2942. lynx::scheduleSong (s, start, samples, tps, b, false, &index);
  2943. events += a.size();
  2944. if (a.size() != b.size())
  2945. {
  2946. ++mismatches;
  2947. continue;
  2948. }
  2949. for (int i = 0; i < a.size(); ++i)
  2950. {
  2951. const auto& x = *(a.begin() + i);
  2952. const auto& y = *(b.begin() + i);
  2953. mismatches += x.sampleOffset != y.sampleOffset || x.type != y.type || x.key != y.key || x.channel != y.channel;
  2954. }
  2955. }
  2956. CHECK (events > 500);
  2957. CHECK (mismatches == 0);
  2958. }
  2959. TEST (mixer_track_eq_defaults_and_boost)
  2960. {
  2961. // FL defaults: 90 Hz, 1.5 kHz and 8 kHz; peak Q 0.632; shelf slope 0.607
  2962. lynx::TrackEq eq;
  2963. CHECK (std::abs (lynx::TrackEq::peakQ (eq.width[1]) - 0.632) < 0.01);
  2964. CHECK (std::abs (lynx::TrackEq::shelfSlope (eq.width[0]) - 0.607) < 0.01);
  2965. CHECK (! eq.active());
  2966. // +12 dB peak at 1.5 kHz on a track: a 1.5 kHz sine rises ~4x
  2967. lynx::Song song = lynx::Song::makeDefault();
  2968. song.mixer[1].eq.gainDb[1] = 12.0f;
  2969. for (auto& c : song.channels)
  2970. c.mixerTrack = 1;
  2971. song.channels[4].instrument = lynx::InstrumentKind::synth;
  2972. song.channels[4].synth = {};
  2973. song.channels[4].synth.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  2974. song.channels[4].synth.cutoff = 20000.0f;
  2975. auto render = [] (const lynx::Song& s) {
  2976. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  2977. lynx::RackRenderer renderer;
  2978. renderer.prepare (48000.0);
  2979. lynx::LiveEventQueue live;
  2980. lynx::NoteEvent on; // 1.5 kHz ~ key 90.6: use 91 (1568 Hz), still within the wide peak
  2981. on.channel = 4;
  2982. on.key = 91;
  2983. on.velocity = 1.0f;
  2984. live.push (on);
  2985. std::vector<float> l (24000), r (24000);
  2986. for (int pos = 0; pos < 24000; pos += 480)
  2987. renderer.process (state.get(), false, 0.0, 480, l.data() + pos, r.data() + pos, pos == 0 ? &live : nullptr);
  2988. float peak = 0.0f;
  2989. for (std::size_t i = 12000; i < 24000; ++i)
  2990. peak = std::max (peak, std::abs (l[i]));
  2991. return peak;
  2992. };
  2993. const auto boosted = render (song);
  2994. song.mixer[1].eq.gainDb[1] = 0.0f;
  2995. const auto flat = render (song);
  2996. CHECK (flat > 0.01f);
  2997. CHECK (boosted / flat > 3.0f && boosted / flat < 4.2f);
  2998. }
  2999. TEST (bars_follow_time_signature_markers)
  3000. {
  3001. lynx::Song song;
  3002. song.beatsPerBar = 4;
  3003. const auto bar4 = (std::int64_t) lynx::ppq * 4;
  3004. auto bars = song.barsBetween (0, bar4 * 3);
  3005. CHECK (bars.size() == 3 && bars[2].tick == bar4 * 2 && bars[2].number == 3);
  3006. // 7/4 from bar 3, and 3/4 in the middle of a 7 bar (cuts and restarts)
  3007. song.addMarker ({ bar4 * 2, "Verso", 7, 4 });
  3008. song.addMarker ({ bar4, "Nome só", 0, 4 });
  3009. const auto seven = (std::int64_t) lynx::ppq * 7;
  3010. song.addMarker ({ bar4 * 2 + seven + lynx::ppq * 2, "", 3, 4 });
  3011. bars = song.barsBetween (0, bar4 * 2 + seven * 2);
  3012. CHECK (bars.size() >= 5);
  3013. CHECK (bars[1].length == bar4 && bars[2].tick == bar4 * 2 && bars[2].length == seven);
  3014. CHECK (bars[3].length == lynx::ppq * 2); // cut short by the change
  3015. CHECK (bars[4].length == lynx::ppq * 3 && bars[4].number == 5);
  3016. CHECK (song.markers()[0].name == "Nome só"); // sorted by tick
  3017. // from far away: starts at the right bar without losing the numbering
  3018. const auto far = song.barsBetween (lynx::ppq * 3000, lynx::ppq * 3001);
  3019. CHECK (far.size() == 1 && far[0].length == lynx::ppq * 3);
  3020. CHECK (song.barAt (lynx::ppq * 3000).number == far[0].number);
  3021. CHECK ((lynx::ppq * 3000 - far[0].tick) < far[0].length);
  3022. }
  3023. TEST (split_drum_channel_moves_notes_to_pads)
  3024. {
  3025. lynx::Song song = lynx::Song::makeDefault();
  3026. const auto channels = (int) song.channels.size();
  3027. song.patterns[0].notes.clear();
  3028. song.patterns[0].notes.push_back ({ 0, 240, 1, 36, 0.9f }); // kit kick (channel 1)
  3029. song.patterns[0].notes.push_back ({ 240, 240, 1, 38, 0.8f }); // snare
  3030. song.patterns[0].notes.push_back ({ 480, 240, 1, 50, 0.8f }); // key without a pad: dropped
  3031. song.patterns[0].notes.push_back ({ 480, 240, 3, 60, 0.8f }); // other channel: shifts by 1 position
  3032. lynx::Automation a;
  3033. a.target = { lynx::AutomationTargetKind::channelVolume, 3, 0, 0 };
  3034. song.automations.push_back (a);
  3035. std::vector<lynx::DrumPad> pads (2);
  3036. pads[0].name = "Kick";
  3037. pads[0].key = 36;
  3038. pads[0].layers = { { "kick.wav" } };
  3039. pads[1].name = "Snare";
  3040. pads[1].key = 38;
  3041. pads[1].layers = { { "snare soft.wav", 60, 0, 127, 0, 90 }, { "snare hard.wav", 60, 0, 127, 91, 127 } };
  3042. pads[1].cutGroup = 2;
  3043. pads[1].cutBy = 2;
  3044. lynx::splitDrumChannel (song, 1, pads);
  3045. CHECK ((int) song.channels.size() == channels + 1);
  3046. CHECK (song.channels[1].samplePath == "kick.wav" && song.channels[1].instrument == lynx::InstrumentKind::sampler);
  3047. CHECK (song.channels[2].zones.size() == 2 && song.channels[2].cutGroup == song.channels[2].cutBy && song.channels[2].cutGroup != 0);
  3048. const auto& notes = song.patterns[0].notes;
  3049. CHECK (notes.size() == 3);
  3050. CHECK (notes[0].channel == 1 && notes[0].key == 60);
  3051. CHECK (notes[1].channel == 2 && notes[1].key == 60);
  3052. CHECK (notes[2].channel == 4); // the old channel 3
  3053. CHECK (song.automations[0].target.index == 4);
  3054. }
  3055. TEST (flp_import_brings_playlist)
  3056. {
  3057. flp::Project p;
  3058. p.header.ppq = 96;
  3059. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 1 } };
  3060. flp::Pattern empty;
  3061. empty.index = 1;
  3062. flp::Pattern full;
  3063. full.index = 2;
  3064. flp::Note n;
  3065. n.channel = 0;
  3066. full.notes.push_back (n);
  3067. p.patterns = { empty, full };
  3068. flp::Arrangement a;
  3069. flp::PlaylistItem item;
  3070. item.kind = flp::PlaylistItem::Kind::pattern;
  3071. item.index = 2;
  3072. item.position = 384;
  3073. item.length = 192;
  3074. item.track = 3;
  3075. item.patternStartTick = 96;
  3076. a.items.push_back (item);
  3077. item.index = 1; // empty pattern: discarded
  3078. a.items.push_back (item);
  3079. item.kind = flp::PlaylistItem::Kind::channelClip; // audio: not supported yet
  3080. a.items.push_back (item);
  3081. p.arrangements.push_back (a);
  3082. const auto s = lynx::importFlp (p);
  3083. CHECK (s.mode == lynx::PlayMode::song);
  3084. CHECK (s.playlist.size() == 1);
  3085. CHECK (s.playlist[0].pattern == 0 && s.playlist[0].track == 3);
  3086. CHECK (s.playlist[0].start == 4 * lynx::ppq && s.playlist[0].length == 2 * lynx::ppq && s.playlist[0].offset == lynx::ppq);
  3087. }
  3088. TEST (rack_releases_notes_on_position_jump)
  3089. {
  3090. lynx::Song s;
  3091. lynx::ChannelDef c;
  3092. c.instrument = lynx::InstrumentKind::synth;
  3093. s.channels.push_back (c);
  3094. auto& p = s.patterns.emplace_back();
  3095. lynx::Note n;
  3096. n.start = 0;
  3097. n.length = 4 * lynx::ppq; // long note
  3098. p.notes.push_back (n);
  3099. s.bpm = 120.0;
  3100. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  3101. lynx::RackRenderer r;
  3102. r.prepare (48000.0);
  3103. std::vector<float> l (512), rr (512);
  3104. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3105. double tick = 0.0;
  3106. for (int i = 0; i < 20; ++i, tick += 512 * tps)
  3107. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3108. // jump to tick 2000 (mid-note, no new "on"): the synth must go silent
  3109. for (int i = 0; i < 40; ++i)
  3110. r.process (state.get(), true, 2000.0 + i * 512 * tps, 512, l.data(), rr.data());
  3111. float tail = 0.0f;
  3112. for (auto v : l)
  3113. tail = std::max (tail, std::abs (v));
  3114. CHECK (tail < 0.001f);
  3115. }
  3116. namespace
  3117. {
  3118. // Two samplers with a constant 0.5 signal on different inserts; returns the output peak.
  3119. float juce_db (float gain)
  3120. {
  3121. return 20.0f * std::log10 (gain);
  3122. }
  3123. float renderMixerCase (const std::function<void (lynx::Song&)>& tweak, lynx::MeterBank* meters = nullptr)
  3124. {
  3125. lynx::Song s;
  3126. s.mixer = lynx::Song::defaultMixer (4);
  3127. for (int i = 0; i < 2; ++i)
  3128. {
  3129. lynx::ChannelDef c;
  3130. c.instrument = lynx::InstrumentKind::sampler;
  3131. c.samplePath = "dc";
  3132. c.volume = 1.0f;
  3133. c.mixerTrack = i + 1;
  3134. s.channels.push_back (c);
  3135. }
  3136. s.patterns.emplace_back();
  3137. tweak (s);
  3138. auto provider = [] (const std::string&) {
  3139. auto d = std::make_shared<lynx::SampleData>();
  3140. d->left.assign (48000, 0.5f);
  3141. return std::shared_ptr<const lynx::SampleData> (d);
  3142. };
  3143. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3144. lynx::RackRenderer r;
  3145. r.prepare (48000.0, 256);
  3146. lynx::LiveEventQueue live;
  3147. for (int ch = 0; ch < 2; ++ch)
  3148. {
  3149. lynx::NoteEvent on;
  3150. on.channel = ch;
  3151. on.velocity = 1.0f;
  3152. live.push (on);
  3153. }
  3154. std::vector<float> l (1000), rr (1000); // larger than the mixer block: tests the chunking
  3155. r.process (state.get(), false, 0.0, 1000, l.data(), rr.data(), &live, meters);
  3156. return std::max (std::abs (l[999]), std::abs (rr[999]));
  3157. }
  3158. } // namespace
  3159. TEST (mixer_routes_mutes_solos_and_meters)
  3160. {
  3161. // two 0.5 channels summed on the Master at 0 dB
  3162. CHECK (std::abs (renderMixerCase ([] (lynx::Song&) {}) - 1.0f) < 1e-4f);
  3163. // insert 1 muted: only channel 2 remains
  3164. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[1].muted = true; }) - 0.5f) < 1e-4f);
  3165. // solo on insert 2: same
  3166. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[2].solo = true; }) - 0.5f) < 1e-4f);
  3167. // FL fader curve: 0.8 = 0 dB, max ≈ +5.6 dB, half travel ≈ -12.7 dB
  3168. CHECK (std::abs (lynx::faderGain (0.8f) - 1.0f) < 1e-5f);
  3169. CHECK (std::abs (juce_db (lynx::faderGain (1.0f)) - 5.59f) < 0.05f);
  3170. CHECK (lynx::faderGain (0.0f) == 0.0f);
  3171. // Master fader at max
  3172. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer[0].volume = 1.0f; }) - lynx::faderGain (1.0f)) < 1e-4f);
  3173. // both channels on the same insert, insert fader at 0.4
  3174. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) {
  3175. s.channels[1].mixerTrack = 1;
  3176. s.mixer[1].volume = 0.4f;
  3177. }) - lynx::faderGain (0.4f)) < 1e-4f);
  3178. // a route outside the mixer falls to the last existing insert, without crashing
  3179. CHECK (renderMixerCase ([] (lynx::Song& s) { s.channels[0].mixerTrack = 100; }) > 0.9f);
  3180. // no mixer: everything straight to the output at 0 dB
  3181. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.mixer.clear(); }) - 1.0f) < 1e-4f);
  3182. lynx::MeterBank meters;
  3183. renderMixerCase ([] (lynx::Song&) {}, &meters);
  3184. CHECK (std::abs (meters.takePeak (1, 0) - 0.5f) < 1e-4f);
  3185. CHECK (std::abs (meters.takePeak (0, 1) - 1.0f) < 1e-4f);
  3186. CHECK (meters.takePeak (1, 0) == 0.0f); // reading resets
  3187. CHECK (meters.takePeak (3, 0) == 0.0f); // insert with no channel
  3188. }
  3189. TEST (next_free_insert_and_flp_mixer_import)
  3190. {
  3191. auto s = lynx::Song::makeDefault();
  3192. CHECK (s.mixer.size() == 17 && s.mixer[0].name == "Master");
  3193. CHECK (s.nextFreeInsert() == 6); // the 5 default channels take 1..5
  3194. flp::Project p;
  3195. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "", 30 } };
  3196. p.mixer = { { 0, "" }, { 30, "Drums" } };
  3197. const auto song = lynx::importFlp (p);
  3198. CHECK (song.channels[0].mixerTrack == 30);
  3199. CHECK (song.mixer.size() == 31);
  3200. CHECK (song.mixer[30].name == "Drums" && song.mixer[0].name == "Master");
  3201. }
  3202. namespace
  3203. {
  3204. float runEffectOnSine (lynx::EffectKind kind, const std::function<void (lynx::EffectSlot&)>& tweak, double hz, float amp,
  3205. int frames = 48000)
  3206. {
  3207. auto fx = lynx::makeEffect (kind);
  3208. fx->prepare (48000.0);
  3209. auto slot = lynx::makeEffectSlot (kind);
  3210. tweak (slot);
  3211. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  3212. for (int i = 0; i < frames; ++i)
  3213. l[(std::size_t) i] = r[(std::size_t) i] = amp * (float) std::sin (6.283185307 * hz * i / 48000.0);
  3214. for (int i = 0; i < frames; i += 480)
  3215. fx->process (l.data() + i, r.data() + i, std::min (480, frames - i), slot.params, 120.0);
  3216. float peak = 0.0f;
  3217. for (int i = frames / 2; i < frames; ++i) // second half: already settled
  3218. {
  3219. CHECK (std::isfinite (l[(std::size_t) i]));
  3220. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3221. }
  3222. return peak;
  3223. }
  3224. } // namespace
  3225. TEST (parametric_eq_bands_and_response)
  3226. {
  3227. // default: transparent
  3228. auto slot = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3229. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0)) < 0.01);
  3230. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::parametricEq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.005f);
  3231. // +12 dB peak at 1 kHz on band 4: the response and the signal agree
  3232. auto boost = [] (lynx::EffectSlot& s) {
  3233. s.params[(std::size_t) lynx::peq::param (3, 1)] = lynx::peq::hzValue (1000.0);
  3234. s.params[(std::size_t) lynx::peq::param (3, 2)] = lynx::peq::gainValue (12.0);
  3235. };
  3236. boost (slot);
  3237. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 1000.0) - 12.0) < 0.2);
  3238. CHECK (std::abs (lynx::peq::responseDb (slot.params, 48000.0, 100.0)) < 1.0);
  3239. const auto peak = runEffectOnSine (lynx::EffectKind::parametricEq, boost, 1000.0, 0.1f);
  3240. CHECK (std::abs (peak - 0.1f * std::pow (10.0f, 12.0f / 20.0f)) < 0.01f);
  3241. // low cut at 48 dB/oct at 200 Hz: 50 Hz (2 octaves below) drops ~96 dB; 2 kHz passes
  3242. auto cut = lynx::makeEffectSlot (lynx::EffectKind::parametricEq);
  3243. cut.params[(std::size_t) lynx::peq::param (0, 0)] = lynx::peq::typeValue (lynx::peq::BandType::lowCut);
  3244. cut.params[(std::size_t) lynx::peq::param (0, 1)] = lynx::peq::hzValue (200.0);
  3245. cut.params[(std::size_t) lynx::peq::param (0, 3)] = lynx::peq::qValue (0.70710678); // Butterworth
  3246. cut.params[(std::size_t) (lynx::peq::slopeParam + 0)] = 1.0f;
  3247. CHECK (lynx::peq::responseDb (cut.params, 48000.0, 50.0) < -80.0);
  3248. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 200.0) + 3.0) < 0.5); // -3 dB at the cutoff (Butterworth)
  3249. CHECK (std::abs (lynx::peq::responseDb (cut.params, 48000.0, 2000.0)) < 0.1);
  3250. // round-trip conversions
  3251. CHECK (std::abs (lynx::peq::hz (lynx::peq::hzValue (440.0)) - 440.0) < 0.5);
  3252. CHECK (std::abs (lynx::peq::q (lynx::peq::qValue (2.5)) - 2.5) < 0.01);
  3253. CHECK (lynx::peq::type (lynx::peq::typeValue (lynx::peq::BandType::notch)) == lynx::peq::BandType::notch);
  3254. }
  3255. TEST (fl_parametric_eq2_state_maps_to_bands)
  3256. {
  3257. // v8 blob: band 1 low cut at 100 Hz "Steep 8", band 4 peak +6 dB at 1 kHz, output −3 dB
  3258. std::vector<std::uint8_t> blob (354, 0);
  3259. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3260. put (0, 8);
  3261. auto param = [&] (int i, std::int32_t v) { put (4 + 4 * (std::size_t) i, v); };
  3262. auto hzRaw = [] (double hz) { return (std::int32_t) std::lround (std::log (hz / 20.0) / std::log (1000.0) * 65536.0); };
  3263. for (int b = 0; b < 7; ++b)
  3264. param (21 + b, 0); // all off
  3265. param (21 + 0, 3); // high pass (= low cut)
  3266. param (7 + 0, hzRaw (100.0));
  3267. param (14 + 0, 40124); // default width -> Q ~0.68
  3268. param (28 + 0, -3); // Steep 8 -> 4 biquads
  3269. param (21 + 3, 6); // peaking
  3270. param (7 + 3, hzRaw (1000.0));
  3271. param (3, 600); // +6 dB
  3272. param (14 + 3, 25412);
  3273. param (35, -300); // main level −3 dB
  3274. put (0x13E, 0x7F);
  3275. const auto slot = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3276. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::parametricEq);
  3277. const auto& p = slot->params;
  3278. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (0, 0)]) == lynx::peq::BandType::lowCut);
  3279. CHECK (lynx::peq::flLaw (p) && lynx::peqfl::fromParams (p).order[0] == -3); // FL's own order (REA_rodada50)
  3280. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (3, 0)]) == lynx::peq::BandType::peak);
  3281. CHECK (lynx::peq::type (p[(std::size_t) lynx::peq::param (1, 0)]) == lynx::peq::BandType::off);
  3282. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 1000.0) - 3.0) < 0.3); // +6 from the peak −3 from the output
  3283. CHECK (lynx::peq::responseDb (p, 48000.0, 30.0) < -40.0); // 48 dB/oct below 100 Hz
  3284. CHECK (std::abs (lynx::peq::responseDb (p, 48000.0, 8000.0) + 3.0) < 0.3);
  3285. // a band muted by the mask doesn't sound (FL's mask travels with the slot, REA_rodada50)
  3286. put (0x13E, 0x7F & ~(1 << 3));
  3287. const auto muted = lynx::effectFromFl ("Fruity Parametric EQ 2", blob);
  3288. CHECK ((lynx::peqfl::fromParams (muted->params).mask & (1u << 3)) == 0);
  3289. CHECK (std::abs (lynx::peq::responseDb (muted->params, 48000.0, 1000.0) + 3.0) < 0.3);
  3290. }
  3291. TEST (reverb_low_cut_and_predelay)
  3292. {
  3293. // impulse: with 100 ms predelay the wet only starts later; with a 3 kHz low cut a
  3294. // 100 Hz sine barely enters the reverb
  3295. auto run = [] (const std::function<void (lynx::EffectSlot&)>& tweak, double hz, bool impulse) {
  3296. auto fx = lynx::makeEffect (lynx::EffectKind::reverb);
  3297. fx->prepare (48000.0);
  3298. auto slot = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3299. slot.params[4] = 0.0f; // wet only
  3300. slot.params[3] = 1.0f;
  3301. tweak (slot);
  3302. std::vector<float> l (48000), r (48000);
  3303. for (int i = 0; i < 48000; ++i)
  3304. 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);
  3305. for (int i = 0; i < 48000; i += 480)
  3306. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  3307. return l;
  3308. };
  3309. const auto delayed = run ([] (auto& s) { s.params[7] = 0.1f; }, 0.0, true);
  3310. float early = 0.0f, later = 0.0f;
  3311. for (std::size_t i = 0; i < 4800; ++i)
  3312. early = std::max (early, std::abs (delayed[i]));
  3313. for (std::size_t i = 4800; i < 24000; ++i)
  3314. later = std::max (later, std::abs (delayed[i]));
  3315. CHECK (early == 0.0f && later > 0.0f);
  3316. 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; };
  3317. const auto open = energy (run ([] (auto&) {}, 100.0, false));
  3318. const auto cut = energy (run ([] (auto& s) { s.params[5] = 1.0f; }, 100.0, false));
  3319. CHECK (cut < open * 0.01);
  3320. }
  3321. TEST (utility_effect_and_fl_stereo_tools)
  3322. {
  3323. // transparent by default; balance fully right silences the left
  3324. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::utility, [] (auto&) {}, 440.0, 0.5f) - 0.5f) < 0.001f);
  3325. CHECK (runEffectOnSine (lynx::EffectKind::utility, [] (auto& s) { s.params[1] = 1.0f; }, 440.0, 0.5f) < 0.001f);
  3326. // Stereo Shaper at half the knob range (6400): each gain = (3^0.5 − 1)/2 (FL's knob law, REA_rodada54 §2.2)
  3327. std::vector<std::uint8_t> blob (36, 0);
  3328. auto put = [&] (std::size_t off, std::int32_t v) { std::memcpy (blob.data() + off, &v, 4); };
  3329. put (4, 6400); put (8, 6400); put (12, 6400); put (16, 6400);
  3330. const auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", blob);
  3331. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  3332. auto shaperFx = lynx::makeEffect (*slot);
  3333. shaperFx->prepare (44100.0);
  3334. float zl = 0.0f, zr = 0.0f;
  3335. shaperFx->process (&zl, &zr, 0, slot->params, 120.0); // takes the controls
  3336. shaperFx->reset(); // FL's Flush: the gains jump to their targets
  3337. float l = 1.0f, r = 0.0f;
  3338. shaperFx->process (&l, &r, 1, slot->params, 120.0);
  3339. const auto half = (float) ((std::sqrt (3.0) - 1.0) / 2.0);
  3340. CHECK (std::abs (l - half) < 1e-4f && std::abs (r - half) < 1e-4f);
  3341. auto fx = lynx::makeEffect (lynx::EffectKind::utility);
  3342. // Balance: volume 256 = 0 dB, pan −128 = fully left
  3343. std::vector<std::uint8_t> bal (8, 0);
  3344. std::int32_t pan = -128, vol = 256;
  3345. std::memcpy (bal.data(), &pan, 4);
  3346. std::memcpy (bal.data() + 4, &vol, 4);
  3347. const auto b = lynx::effectFromFl ("Fruity Balance", bal);
  3348. CHECK (b && b->kind == lynx::EffectKind::fruityBalance); // FL's exact law (REA_rodada58 §2): fully left sums R into L
  3349. if (b)
  3350. {
  3351. auto balance = lynx::makeEffect (*b);
  3352. balance->prepare (44100.0);
  3353. l = 0.5f; r = 0.5f;
  3354. balance->process (&l, &r, 1, b->params, 120.0);
  3355. CHECK (std::abs (l - 0.70710678f) < 1e-6f && r == 0.0f);
  3356. }
  3357. }
  3358. TEST (effects_eq_compressor_behave)
  3359. {
  3360. // EQ at default is transparent
  3361. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::eq, [] (auto&) {}, 1000.0, 0.5f) - 0.5f) < 0.01f);
  3362. // +18 dB on the lows: 60 Hz rises a lot, 5 kHz barely
  3363. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 60.0, 0.05f) > 0.3f);
  3364. CHECK (runEffectOnSine (lynx::EffectKind::eq, [] (auto& s) { s.params[0] = 1.0f; }, 5000.0, 0.05f) < 0.06f);
  3365. // compressor: threshold -20 dB, ratio 20:1 flattens a 0 dBFS signal to near the threshold
  3366. const auto squashed = runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) {
  3367. s.params[0] = 40.0f / 60.0f;
  3368. s.params[1] = 1.0f;
  3369. }, 200.0, 1.0f);
  3370. CHECK (squashed < 0.2f && squashed > 0.05f);
  3371. // a signal below the threshold passes intact
  3372. CHECK (std::abs (runEffectOnSine (lynx::EffectKind::compressor, [] (auto& s) { s.params[0] = 1.0f; }, 200.0, 0.3f) - 0.3f) < 0.01f);
  3373. }
  3374. TEST (effects_delay_echoes_on_tempo_and_reverb_has_tail)
  3375. {
  3376. auto delay = lynx::makeEffect (lynx::EffectKind::delay);
  3377. delay->prepare (48000.0);
  3378. auto slot = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3379. slot.params[0] = 3.5f / 8.0f; // index 3 = 1/4 = 0.5 s at 120 BPM
  3380. slot.params[1] = 0.0f; // no feedback: a single echo
  3381. slot.params[2] = 1.0f;
  3382. slot.params[4] = 0.0f; // no damping
  3383. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  3384. l[0] = r[0] = 1.0f;
  3385. delay->process (l.data(), r.data(), 48000, slot.params, 120.0);
  3386. CHECK (std::abs (l[24000] - 1.0f) < 1e-4f);
  3387. CHECK (std::abs (l[12000]) < 1e-6f);
  3388. auto reverb = lynx::makeEffect (lynx::EffectKind::reverb);
  3389. reverb->prepare (48000.0);
  3390. auto rs = lynx::makeEffectSlot (lynx::EffectKind::reverb);
  3391. rs.params[3] = 1.0f;
  3392. std::fill (l.begin(), l.end(), 0.0f);
  3393. std::fill (r.begin(), r.end(), 0.0f);
  3394. l[0] = r[0] = 1.0f;
  3395. reverb->process (l.data(), r.data(), 48000, rs.params, 120.0);
  3396. float tail = 0.0f;
  3397. for (int i = 4800; i < 9600; ++i) // between 100 and 200 ms after the impulse
  3398. tail = std::max (tail, std::abs (l[(std::size_t) i]));
  3399. CHECK (tail > 1e-4f && tail < 1.0f);
  3400. CHECK (lynx::formatEffectParam (lynx::EffectKind::delay, 0, slot.params[0], 120.0) == "1/4 (500 ms)");
  3401. }
  3402. TEST (rack_runs_insert_effects_and_reuses_them)
  3403. {
  3404. // an insert with EQ at -inf doesn't exist; we use the EQ output at -18 dB to see the effect act
  3405. const auto quiet = renderMixerCase ([] (lynx::Song& s) {
  3406. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3407. eq.params[4] = 0.0f; // output -18 dB
  3408. s.mixer[1].effects.push_back (eq);
  3409. });
  3410. CHECK (quiet > 0.55f && quiet < 0.6f); // 0.5 from channel 2 + 0.5 * 0.126 from channel 1
  3411. const auto bypassed = renderMixerCase ([] (lynx::Song& s) {
  3412. auto eq = lynx::makeEffectSlot (lynx::EffectKind::eq);
  3413. eq.params[4] = 0.0f;
  3414. eq.enabled = false;
  3415. s.mixer[1].effects.push_back (eq);
  3416. });
  3417. CHECK (std::abs (bypassed - 1.0f) < 1e-4f);
  3418. auto s = lynx::Song::makeDefault();
  3419. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  3420. auto a = lynx::RackState::build (s, nullptr, 48000.0);
  3421. s.mixer[2].effects[0].params[0] = 0.9f; // turning a knob doesn't recreate the effect
  3422. auto b = lynx::RackState::build (s, a.get(), 48000.0);
  3423. CHECK (b->effects[2][0] == a->effects[2][0]);
  3424. s.mixer[2].effects[0] = lynx::makeEffectSlot (lynx::EffectKind::delay);
  3425. auto c = lynx::RackState::build (s, b.get(), 48000.0);
  3426. CHECK (c->effects[2][0] != b->effects[2][0]);
  3427. }
  3428. TEST (audio_clips_trigger_cut_and_resume)
  3429. {
  3430. lynx::Song s;
  3431. s.bpm = 120.0; // 1 beat = 0.5 s = 24000 samples at 48 kHz
  3432. lynx::ChannelDef c;
  3433. c.instrument = lynx::InstrumentKind::audioClip;
  3434. c.samplePath = "ramp";
  3435. c.volume = 1.0f;
  3436. s.channels.push_back (c);
  3437. s.patterns.emplace_back();
  3438. lynx::Clip clip;
  3439. clip.kind = lynx::ClipKind::audio;
  3440. clip.channel = 0;
  3441. clip.start = lynx::ppq; // starts at beat 2
  3442. clip.length = 2 * lynx::ppq; // lasts 2 beats
  3443. clip.offset = lynx::ppq; // skips the first beat of the audio
  3444. s.playlist.push_back (clip);
  3445. s.mode = lynx::PlayMode::song;
  3446. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  3447. lynx::EventBuffer ev;
  3448. lynx::scheduleSong (s, 0.0, 4 * 24000, tps, ev);
  3449. CHECK (ev.size() == 2);
  3450. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ev[0].sampleOffset == 24000 && std::abs (ev[0].startSeconds - 0.5) < 1e-9);
  3451. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 3 * 24000);
  3452. // seek to the middle of the clip (beat 2.5): with resume, starts at the block's first sample
  3453. ev.clear();
  3454. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, true);
  3455. CHECK (ev.size() == 1 && ev[0].sampleOffset == 0 && std::abs (ev[0].startSeconds - 0.75) < 1e-9);
  3456. ev.clear();
  3457. lynx::scheduleSong (s, 1.5 * lynx::ppq, 256, tps, ev, false);
  3458. CHECK (ev.size() == 0);
  3459. // sample whose value = position in seconds: lets us check where playback started
  3460. auto provider = [] (const std::string&) {
  3461. auto d = std::make_shared<lynx::SampleData>();
  3462. d->sampleRate = 48000.0;
  3463. for (int i = 0; i < 48000 * 3; ++i)
  3464. d->left.push_back ((float) i / 48000.0f);
  3465. return std::shared_ptr<const lynx::SampleData> (d);
  3466. };
  3467. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  3468. lynx::RackRenderer r;
  3469. r.prepare (48000.0, 512);
  3470. std::vector<float> l (256), rr (256);
  3471. // first block already playing mid-clip
  3472. r.process (state.get(), true, 1.5 * lynx::ppq, 256, l.data(), rr.data());
  3473. CHECK (std::abs (l[0] - 0.75f) < 0.001f);
  3474. // the clip ends at beat 3: after the short fade, silence (the sample would still have audio)
  3475. double tick = 3.0 * lynx::ppq - 10 * tps;
  3476. r.process (state.get(), false, tick, 256, l.data(), rr.data()); // stops (fade)
  3477. for (int i = 0; i < 4; ++i)
  3478. r.process (state.get(), false, tick, 256, l.data(), rr.data());
  3479. CHECK (std::abs (l[255]) < 1e-6f);
  3480. }
  3481. TEST (remove_channel_fixes_notes_and_audio_clips)
  3482. {
  3483. lynx::Song s;
  3484. s.channels.resize (3);
  3485. s.patterns.resize (1);
  3486. s.patterns[0].notes = { { 0, 240, 0, 60, 1.0f }, { 0, 240, 1, 60, 1.0f }, { 0, 240, 2, 60, 1.0f } };
  3487. lynx::Clip a;
  3488. a.kind = lynx::ClipKind::audio;
  3489. a.channel = 1;
  3490. a.length = 100;
  3491. lynx::Clip b = a;
  3492. b.channel = 2;
  3493. s.playlist = { a, b };
  3494. s.removeChannel (1);
  3495. CHECK (s.channels.size() == 2);
  3496. CHECK (s.patterns[0].notes.size() == 2 && s.patterns[0].notes[1].channel == 1);
  3497. CHECK (s.playlist.size() == 1 && s.playlist[0].channel == 1);
  3498. }
  3499. TEST (flp_import_audio_clips_with_offsets)
  3500. {
  3501. flp::Project p;
  3502. p.header.ppq = 96;
  3503. p.tempo = 120.0;
  3504. p.channels = { { 0, flp::ChannelType::audioClip, "Vocal", "", "%FLStudioData%\\v.wav", 2 },
  3505. { 1, flp::ChannelType::audioClip, "Loop", "", "%FLStudioData%\\l.wav", 3 } };
  3506. flp::Arrangement arr;
  3507. flp::PlaylistItem beats; // offset in beats: end - start = duration in beats
  3508. beats.kind = flp::PlaylistItem::Kind::channelClip;
  3509. beats.index = 0;
  3510. beats.position = 192;
  3511. beats.length = 384; // 4 beats
  3512. beats.startOffset = 2.0f;
  3513. beats.endOffset = 6.0f;
  3514. flp::PlaylistItem ms = beats; // offset in ms: 4 beats at 120 BPM = 2000 ms
  3515. ms.index = 1;
  3516. ms.startOffset = 500.0f;
  3517. ms.endOffset = 2500.0f;
  3518. arr.items = { beats, ms };
  3519. p.arrangements.push_back (arr);
  3520. lynx::FlpImportOptions options;
  3521. options.roots = { "C:\\FL", "C:\\FL\\Data", "" };
  3522. options.fileExists = [] (const std::string&) { return true; };
  3523. const auto s = lynx::importFlp (p, options);
  3524. CHECK (s.channels.size() == 2 && s.channels[0].instrument == lynx::InstrumentKind::audioClip);
  3525. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\v.wav");
  3526. CHECK (s.playlist.size() == 2);
  3527. CHECK (s.playlist[0].kind == lynx::ClipKind::audio && s.playlist[0].start == 2 * lynx::ppq);
  3528. CHECK (s.playlist[0].offset == 2 * lynx::ppq);
  3529. CHECK (s.playlist[1].offset == lynx::ppq); // 500 ms at 120 BPM = 1 beat
  3530. CHECK (s.mode == lynx::PlayMode::song);
  3531. }
  3532. TEST (plugin_factories_create_reuse_and_fallback)
  3533. {
  3534. lynx::Song s;
  3535. s.mixer = lynx::Song::defaultMixer (2);
  3536. lynx::ChannelDef c;
  3537. c.instrument = lynx::InstrumentKind::plugin;
  3538. c.plugin.id = "VST3-Synth-123";
  3539. c.mixerTrack = 1;
  3540. s.channels.push_back (c);
  3541. lynx::ChannelDef missing = c;
  3542. missing.plugin.id = "VST3-Ausente-0";
  3543. s.channels.push_back (missing);
  3544. lynx::EffectSlot fx;
  3545. fx.kind = lynx::EffectKind::plugin;
  3546. fx.plugin.id = "VST3-Reverb-9";
  3547. s.mixer[1].effects.push_back (fx);
  3548. s.patterns.emplace_back();
  3549. int instrumentsMade = 0, effectsMade = 0;
  3550. lynx::RackState::Factories f;
  3551. f.pluginInstruments = [&] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> {
  3552. if (ref.id != "VST3-Synth-123")
  3553. return nullptr;
  3554. ++instrumentsMade;
  3555. auto synth = lynx::makeInstrument (lynx::InstrumentKind::synth);
  3556. synth->prepare (48000.0);
  3557. return synth;
  3558. };
  3559. f.pluginEffects = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> {
  3560. ++effectsMade;
  3561. auto eq = lynx::makeEffect (lynx::EffectKind::eq);
  3562. eq->prepare (48000.0);
  3563. return eq;
  3564. };
  3565. auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  3566. CHECK (instrumentsMade == 1 && effectsMade == 1);
  3567. CHECK (a->instruments.size() == 2 && a->instruments[1] != nullptr); // missing: muted, but it exists
  3568. // changing the saved state doesn't recreate; swapping the plugin does
  3569. s.channels[0].plugin.state = "abc";
  3570. auto b = lynx::RackState::build (s, a.get(), 48000.0, f);
  3571. CHECK (instrumentsMade == 1 && b->instruments[0] == a->instruments[0] && b->effects[1][0] == a->effects[1][0]);
  3572. s.mixer[1].effects[0].plugin.id = "VST3-Outro-1";
  3573. auto c2 = lynx::RackState::build (s, b.get(), 48000.0, f);
  3574. CHECK (effectsMade == 2);
  3575. // rendering with a missing plugin and a plugin slot breaks nothing
  3576. lynx::RackRenderer r;
  3577. r.prepare (48000.0, 256);
  3578. lynx::LiveEventQueue live;
  3579. lynx::NoteEvent on;
  3580. on.channel = 0;
  3581. on.velocity = 1.0f;
  3582. live.push (on);
  3583. on.channel = 1;
  3584. live.push (on);
  3585. std::vector<float> l (256), rr (256);
  3586. r.process (c2.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  3587. float peak = 0.0f;
  3588. for (auto v : l)
  3589. {
  3590. CHECK (std::isfinite (v));
  3591. peak = std::max (peak, std::abs (v));
  3592. }
  3593. CHECK (peak > 0.0f);
  3594. }
  3595. TEST (export_song_adds_silent_tail_and_never_restarts)
  3596. {
  3597. auto s = lynx::Song::makeDefault();
  3598. s.playlist.clear();
  3599. std::int64_t length = 0;
  3600. auto pat = lynx::makeExportSong (s, true, lynx::ppq * 8, length); // empty Playlist: uses the pattern
  3601. CHECK (length == lynx::ppq * 4);
  3602. CHECK (pat.mode == lynx::PlayMode::song && pat.playlist.size() == 2);
  3603. CHECK (pat.songLength() >= length + lynx::ppq * 8);
  3604. // no new notes after the song ends, only silence until the end of the tail
  3605. lynx::EventBuffer events;
  3606. const auto tps = lynx::ticksPerSample (pat.bpm, 48000.0);
  3607. int onsAfterEnd = 0, onsBefore = 0;
  3608. for (double tick = 0.0; tick < (double) (length + lynx::ppq * 8); tick += 512 * tps)
  3609. {
  3610. events.clear();
  3611. lynx::scheduleSong (pat, tick, 512, tps, events);
  3612. for (const auto& e : events)
  3613. if (e.type == lynx::NoteEvent::Type::on)
  3614. (tick + e.sampleOffset * tps >= (double) length ? onsAfterEnd : onsBefore)++;
  3615. }
  3616. CHECK (onsBefore == (int) s.patterns[0].notes.size());
  3617. CHECK (onsAfterEnd == 0);
  3618. // with Playlist: the end is the last clip's, without rounding
  3619. s.playlist.push_back ({ 0, 0, lynx::ppq * 2, lynx::ppq * 3, 0 });
  3620. auto song = lynx::makeExportSong (s, true, 0, length);
  3621. CHECK (length == lynx::ppq * 5 && song.playlist.size() == 1);
  3622. song = lynx::makeExportSong (s, false, 0, length);
  3623. CHECK (length == lynx::ppq * 4 && song.playlist[0].start == 0);
  3624. }
  3625. TEST (note_edit_selection_operations)
  3626. {
  3627. namespace ne = lynx::noteedit;
  3628. lynx::Pattern p;
  3629. p.notes = { { 0, 240, 0, 60 }, { 480, 240, 0, 64 }, { 960, 480, 1, 67 } };
  3630. // sorting keeps the indices in step
  3631. p.notes[0].start = 1200;
  3632. std::vector<int> tracked { 0, 2 };
  3633. ne::sortTracking (p, tracked);
  3634. CHECK (p.notes[2].start == 1200 && p.notes[(std::size_t) tracked[0]].start == 1200);
  3635. CHECK (p.notes[(std::size_t) tracked[1]].key == 67);
  3636. // group move respects the limits
  3637. std::int64_t dt = -5000;
  3638. int dk = 100;
  3639. ne::move (p, { 0, 1 }, dt, dk);
  3640. CHECK (dt == -480 && dk == 127 - 67 && p.notes[0].start == 0 && p.notes[1].key == 127);
  3641. // duplicate: right after the selection, rounded to the grid
  3642. lynx::Pattern q;
  3643. q.notes = { { 0, 240, 0, 60 }, { 240, 300, 0, 62 } }; // 540-tick selection
  3644. const auto added = ne::duplicate (q, { 0, 1 }, 960);
  3645. CHECK (added.size() == 2 && q.notes[(std::size_t) added[0]].start == 960 && q.notes[(std::size_t) added[1]].start == 1200);
  3646. // paste onto another channel with an offset
  3647. const auto pasted = ne::paste (q, ne::copy (q, { 1 }), 100, 3);
  3648. CHECK (pasted.size() == 1 && q.notes.back().channel == 3 && q.notes.back().start == 340);
  3649. // quantize rounds to the nearest
  3650. q.notes[0].start = 130;
  3651. q.notes[1].start = 100;
  3652. ne::quantize (q, { 0, 1 }, 240);
  3653. CHECK (q.notes[0].start == 240 && q.notes[1].start == 0);
  3654. // remove several (order and duplicates don't matter)
  3655. const auto before = q.notes.size();
  3656. ne::remove (q, { 1, 0, 1 });
  3657. CHECK (q.notes.size() == before - 2);
  3658. }
  3659. TEST (note_edit_quick_operations)
  3660. {
  3661. namespace ne = lynx::noteedit;
  3662. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3663. // split: only strictly inside; the tail is appended and does not slide again
  3664. lynx::Pattern p;
  3665. p.notes = { { 0, 4 * step, 0, 60 } };
  3666. p.notes[0].slide = true;
  3667. CHECK (ne::split (p, 0, 0) == -1 && ne::split (p, 0, 4 * step) == -1);
  3668. CHECK (ne::split (p, 0, step) == 1 && p.notes[0].length == step && p.notes[1].start == step && p.notes[1].length == 3 * step
  3669. && ! p.notes[1].slide);
  3670. // chop to grid: a 2.5-step note from half a step becomes 3 pieces cut on the step lines
  3671. lynx::Pattern c;
  3672. c.notes = { { step / 2, step * 5 / 2, 0, 60 }, { 0, step, 1, 50 } };
  3673. const auto pieces = ne::chop (c, { 0, 1 }, step);
  3674. CHECK (pieces.size() == 4 && c.notes.size() == 4);
  3675. CHECK (c.notes[0].length == step / 2 && c.notes[2].start == step && c.notes[2].length == step && c.notes[3].start == 2 * step
  3676. && c.notes[3].length == step);
  3677. // join: touching and overlapping notes of the same key and channel merge; others stay
  3678. lynx::Pattern j;
  3679. j.notes = { { 0, step, 0, 60 }, { step, step, 0, 60 }, { step * 3 / 2, step * 2, 0, 60 }, { 5 * step, step, 0, 60 },
  3680. { step, step, 0, 62 }, { step, step, 1, 60 } };
  3681. CHECK (ne::join (j, { 0, 1, 2, 3, 4, 5 }) == 2 && j.notes.size() == 4);
  3682. CHECK (j.notes[0].start == 0 && j.notes[0].length == step * 7 / 2);
  3683. // legato: each note reaches the next start (a chord ends together); the last keeps its length
  3684. lynx::Pattern l;
  3685. l.notes = { { 0, 10, 0, 60 }, { 0, 10, 0, 64 }, { step, 10, 0, 62 }, { 3 * step, 10, 0, 65 }, { step, 10, 1, 40 } };
  3686. ne::legato (l, { 0, 1, 2, 3, 4 });
  3687. CHECK (l.notes[0].length == step && l.notes[1].length == step && l.notes[2].length == 2 * step && l.notes[3].length == 10
  3688. && l.notes[4].length == 10);
  3689. // shrink to grid and full quantise (start and end)
  3690. ne::shrinkToGrid (l, { 3 }, step / 2);
  3691. CHECK (l.notes[3].length == step / 2);
  3692. lynx::Pattern q;
  3693. q.notes = { { 10, step - 30, 0, 60 }, { step + 100, 20, 0, 60 } };
  3694. ne::quantizeFull (q, { 0, 1 }, step);
  3695. CHECK (q.notes[0].start == 0 && q.notes[0].length == step && q.notes[1].start == step && q.notes[1].length == step);
  3696. // rotate inside the bar, wrapping
  3697. lynx::Pattern r;
  3698. r.notes = { { 0, step, 0, 60 }, { bar - step, step, 0, 62 } };
  3699. ne::rotate (r, { 0, 1 }, step, bar, 1);
  3700. CHECK (r.notes[0].start == step && r.notes[1].start == 0);
  3701. ne::rotate (r, { 0, 1 }, step, bar, -2);
  3702. CHECK (r.notes[0].start == bar - step && r.notes[1].start == bar - 2 * step);
  3703. r.notes.push_back ({ bar + step, step, 0, 64 }); // alone in the second bar: wraps inside that bar
  3704. ne::rotate (r, { 2 }, step, bar, -2);
  3705. CHECK (r.notes[2].start == 2 * bar - step);
  3706. }
  3707. TEST (note_edit_grid_and_time_text)
  3708. {
  3709. namespace ne = lynx::noteedit;
  3710. const std::int64_t step = lynx::ticksPerStep, bar = lynx::ppq * 4;
  3711. CHECK (ne::snapDown (250.0, step) == step && ne::snapDown (-1.0, step) == -step && ne::snapDown (17.9, 1) == 17);
  3712. CHECK (ne::snapNearest (1000.0, { 400, 1100, 980 }, 50.0, step) == 980);
  3713. CHECK (ne::snapNearest (1000.0, { 400 }, 50.0, step) == 960); // too far: falls back to the grid
  3714. // screen grid: 24 px per step shows steps; zoomed out it climbs to beats, bars and bar multiples
  3715. CHECK (ne::screenStep (24.0 / step, bar) == step);
  3716. CHECK (ne::screenStep (96.0 / bar, bar) == lynx::ppq);
  3717. CHECK (ne::screenStep (8.0 / bar, bar) == 2 * bar);
  3718. CHECK (ne::screenStep (200.0 / step, bar) == step / 4);
  3719. CHECK (ne::formatTime (0, 4) == "1:1:0" && ne::formatTime (bar + 5 * step + 7, 4) == "2:6:7");
  3720. CHECK (ne::formatTime (step * 2, 4, true) == "0:2:0");
  3721. std::int64_t t = 0;
  3722. CHECK (ne::parseTime ("2:6:7", 4, t) && t == bar + 5 * step + 7);
  3723. CHECK (ne::parseTime ("3", 4, t) && t == 2 * bar);
  3724. CHECK (ne::parseTime ("0:2:0", 4, t, true) && t == 2 * step);
  3725. CHECK (! ne::parseTime ("0:1:0", 4, t) && ! ne::parseTime ("1:17:0", 4, t) && ! ne::parseTime ("1:1:240", 4, t)
  3726. && ! ne::parseTime ("a", 4, t) && ! ne::parseTime ("", 4, t));
  3727. CHECK (ne::parseTime ("1:12:0", 3, t) && ! ne::parseTime ("1:13:0", 3, t)); // 3/4: 12 steps per bar
  3728. }
  3729. TEST (playlist_clip_edges_and_time_selection)
  3730. {
  3731. namespace ne = lynx::noteedit;
  3732. const std::int64_t beat = lynx::ppq;
  3733. // left-edge trim: content stays in place, cannot reveal what is before the content's start
  3734. lynx::Clip c { 0, 0, 4 * beat, 4 * beat, beat };
  3735. ne::trimStart (c, 5 * beat, beat);
  3736. CHECK (c.start == 5 * beat && c.offset == 2 * beat && c.length == 3 * beat);
  3737. ne::trimStart (c, 0, beat);
  3738. CHECK (c.start == 3 * beat && c.offset == 0 && c.length == 5 * beat);
  3739. ne::trimStart (c, 100 * beat, beat);
  3740. CHECK (c.length == beat && c.end() == 8 * beat);
  3741. // slip moves the content, not the clip
  3742. lynx::Clip s { 0, 0, 0, 2 * beat, beat };
  3743. ne::slip (s, beat / 2);
  3744. CHECK (s.offset == beat / 2 && s.start == 0 && s.length == 2 * beat);
  3745. ne::slip (s, beat);
  3746. CHECK (s.offset == 0);
  3747. ne::slip (s, -3 * beat);
  3748. CHECK (s.offset == 3 * beat);
  3749. // insert / delete time across all tracks, markers included
  3750. std::vector<lynx::Clip> clips { { 0, 0, 0, 2 * beat, 0 }, { 0, 1, 4 * beat, 2 * beat, 0 }, { 0, 2, beat, 6 * beat, 0 } };
  3751. std::vector<lynx::Marker> markers { { 5 * beat, "A" }, { beat / 2, "B" } };
  3752. ne::insertTime (clips, markers, 2 * beat, 4 * beat);
  3753. CHECK (clips[0].start == 0 && clips[1].start == 8 * beat && clips[2].start == beat && markers[0].tick == 9 * beat
  3754. && markers[1].tick == beat / 2);
  3755. std::vector<lynx::Clip> d { { 0, 0, 0, 2 * beat, 0 }, // before: kept
  3756. { 0, 1, 3 * beat, beat, 0 }, // inside: deleted
  3757. { 0, 2, beat, 6 * beat, 0 }, // spans the cut: head + tail
  3758. { 0, 3, 3 * beat, 4 * beat, 0 }, // tail outside: trimmed and moved
  3759. { 0, 4, 8 * beat, beat, 0 } }; // after: moved left
  3760. std::vector<lynx::Marker> dm { { 3 * beat, "in" }, { 9 * beat, "after" } };
  3761. ne::deleteTime (d, dm, 2 * beat, 5 * beat);
  3762. CHECK (d.size() == 5);
  3763. CHECK (d[1].track == 2 && d[1].start == beat && d[1].length == beat);
  3764. CHECK (d[2].track == 2 && d[2].start == 2 * beat && d[2].offset == 4 * beat && d[2].length == 2 * beat);
  3765. CHECK (d[3].track == 3 && d[3].start == 2 * beat && d[3].offset == 2 * beat && d[3].length == 2 * beat);
  3766. CHECK (d[4].start == 5 * beat && dm.size() == 1 && dm[0].tick == 6 * beat);
  3767. // time selection: overlapping clips; Ctrl+B repeats what starts inside it after itself
  3768. std::vector<lynx::Clip> r { { 0, 0, 0, beat, 0 }, { 0, 1, beat, 2 * beat, 0 }, { 0, 0, 4 * beat, beat, 0 } };
  3769. CHECK ((ne::clipsInTime (r, beat / 2, 2 * beat) == std::vector<int> { 0, 1 }));
  3770. const auto added = ne::repeatTime (r, 0, 4 * beat, {});
  3771. CHECK (added.size() == 2 && r[3].start == 4 * beat && r[4].start == 5 * beat && r[4].track == 1);
  3772. CHECK (ne::repeatTime (r, 0, 2 * beat, { 2 }).size() == 1 && r.back().start == 6 * beat);
  3773. }
  3774. TEST (recorder_builds_notes_in_pattern_and_song_mode)
  3775. {
  3776. auto song = lynx::Song::makeDefault();
  3777. const auto loop = song.patterns[0].effectiveLength (4); // 1 bar = 3840
  3778. lynx::NoteRecorder r;
  3779. r.add (song, { 100.0, 4, 60, 0.5f, true }); // no take: ignored
  3780. CHECK (r.takeFinished().empty());
  3781. r.begin (loop);
  3782. r.add (song, { 4000.0, 4, 60, 0.5f, true }); // second loop pass
  3783. r.add (song, { 4480.0, 4, 60, 0.0f, false });
  3784. r.add (song, { 5000.0, 4, 64, 0.9f, true }); // held until stop
  3785. r.end (song, 5240.0);
  3786. const auto notes = r.takeFinished();
  3787. CHECK (notes.size() == 2);
  3788. CHECK (notes[0].start == 4000 - 3840 && notes[0].length == 480 && notes[0].velocity == 0.5f && notes[0].channel == 4);
  3789. CHECK (notes[1].key == 64 && notes[1].start == 5000 - 3840 && notes[1].length == 240);
  3790. // song mode: position inside the current pattern's clip (with offset)
  3791. song.mode = lynx::PlayMode::song;
  3792. song.playlist = { { 0, 0, 7680, 3840, 960 } };
  3793. CHECK (lynx::patternPositionForTick (song, 7680.0 + 100.0, loop) == 1060);
  3794. CHECK (lynx::patternPositionForTick (song, 100.0, loop) == 100); // outside the clip: like loop
  3795. }
  3796. TEST (automation_curve_and_playback)
  3797. {
  3798. lynx::Automation a;
  3799. a.points = { { 0, 0.0f }, { 960, 1.0f }, { 1920, 0.5f, 0.0f } };
  3800. CHECK (a.valueAt (-10.0) == 0.0f && a.valueAt (480.0) == 0.5f && a.valueAt (5000.0) == 0.5f);
  3801. CHECK (a.length() == 1920);
  3802. a.points[0].tension = 1.0f; // curve: starts slow
  3803. CHECK (a.valueAt (480.0) < 0.2f && a.valueAt (480.0) > 0.0f);
  3804. // automated Master: silence in the first bar, sound in the second, and the last value
  3805. // keeps applying after the automation clip ends
  3806. auto song = lynx::Song::makeDefault();
  3807. song.bpm = 120.0;
  3808. song.mode = lynx::PlayMode::song;
  3809. const auto bar = (std::int64_t) lynx::ppq * 4;
  3810. for (int b = 0; b < 3; ++b)
  3811. song.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  3812. lynx::Automation master;
  3813. master.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  3814. master.points = { { 0, 0.0f }, { bar - 1, 0.0f }, { bar, 0.8f } };
  3815. song.automations.push_back (master);
  3816. lynx::Clip ac;
  3817. ac.kind = lynx::ClipKind::automation;
  3818. ac.automation = 0;
  3819. ac.track = 1;
  3820. ac.length = bar + bar / 2;
  3821. song.playlist.push_back (ac);
  3822. const double sr = 48000.0;
  3823. auto state = lynx::RackState::build (song, nullptr, sr);
  3824. CHECK (state->hasAutomation && state->mixerVolumeAuto[0] == 0 && state->automationClips[0].size() == 1);
  3825. lynx::RackRenderer r;
  3826. r.prepare (sr, 512);
  3827. std::vector<float> l (512), rr (512);
  3828. const auto tps = lynx::ticksPerSample (song.bpm, sr);
  3829. float peakBar1 = 0.0f, peakBar2 = 0.0f, peakBar3 = 0.0f;
  3830. for (double tick = 0.0; tick < (double) (bar * 3); tick += 512 * tps)
  3831. {
  3832. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  3833. auto& peak = tick < (double) bar - 512 * tps ? peakBar1 : tick < (double) bar * 2 ? peakBar2 : peakBar3;
  3834. for (auto v : l)
  3835. peak = std::max (peak, std::abs (v));
  3836. }
  3837. CHECK (peakBar1 < 1.0e-6f);
  3838. CHECK (peakBar2 > 0.05f);
  3839. CHECK (peakBar3 > 0.05f); // after the clip: keeps the last value
  3840. // target removed: automation disappears from the tables
  3841. song.removeChannel (0);
  3842. song.automations[0].target = { lynx::AutomationTargetKind::channelVolume, 0 };
  3843. song.removeChannel (0);
  3844. CHECK (song.automations[0].target.kind == lynx::AutomationTargetKind::none);
  3845. song.removeAutomation (0);
  3846. CHECK (song.automations.empty() && song.playlist.size() == 3);
  3847. }
  3848. TEST (mixer_sends_between_inserts)
  3849. {
  3850. // insert 1 goes only to 3; 3 goes to the Master: same sum (0.5 + 0.5)
  3851. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); s.setRoute (1, 0, false); }) - 1.0f) < 1e-4f);
  3852. // send at 50% and muted destination
  3853. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true, 0.5f); s.setRoute (1, 0, false); }) - 0.75f) < 1e-4f);
  3854. 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);
  3855. // going to the Master and to 3: the signal doubles
  3856. CHECK (std::abs (renderMixerCase ([] (lynx::Song& s) { s.setRoute (1, 3, true); }) - 1.5f) < 1e-4f);
  3857. // solo on 3 lets what reaches it sound (insert 1), but not 2
  3858. 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);
  3859. // solo on 1 lets the path to the Master (3) sound
  3860. 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);
  3861. // order and cycles
  3862. auto song = lynx::Song::makeDefault();
  3863. song.setRoute (4, 2, true);
  3864. song.setRoute (2, 1, true);
  3865. CHECK (! song.canRoute (1, 4) && ! song.canRoute (1, 2) && song.canRoute (1, 5));
  3866. song.setRoute (1, 4, true); // ignored: would create a cycle
  3867. CHECK (! song.routes (1, 4));
  3868. const auto order = song.mixerOrder();
  3869. const auto pos = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  3870. CHECK ((int) order.size() == (int) song.mixer.size() - 1);
  3871. CHECK (pos (4) < pos (2) && pos (2) < pos (1));
  3872. song.setRoute (4, 2, false);
  3873. CHECK (! song.routes (4, 2) && song.routes (4, 0));
  3874. }
  3875. namespace
  3876. {
  3877. // Test effect: delays the signal by `latency` samples and reports that latency (like a lookahead plugin).
  3878. class LatentEffect final : public lynx::Effect
  3879. {
  3880. public:
  3881. explicit LatentEffect (int samples) : latency (samples), line (samples) {}
  3882. void prepare (double) noexcept override {}
  3883. void reset() noexcept override {}
  3884. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override { line.process (l, r, n); }
  3885. int latencySamples() const noexcept override { return latency; }
  3886. private:
  3887. int latency;
  3888. lynx::DelayLine line;
  3889. };
  3890. } // namespace
  3891. TEST (delay_line_mix_into_matches_process_and_drains_tail)
  3892. {
  3893. // mixInto = process over source*level, added to the destination; in stretches that cross the ring
  3894. lynx::DelayLine a (37), b (37);
  3895. std::vector<float> sl (300), sr (300), dl (300, 0.5f), dr (300, -0.5f), xl (300), xr (300);
  3896. for (int i = 0; i < 300; ++i)
  3897. {
  3898. sl[(std::size_t) i] = (float) std::sin (i * 0.1);
  3899. sr[(std::size_t) i] = (float) std::cos (i * 0.07);
  3900. }
  3901. for (int start = 0; start < 300; start += 100)
  3902. {
  3903. b.mixInto (sl.data() + start, sr.data() + start, 0.25f, dl.data() + start, dr.data() + start, 100);
  3904. for (int i = start; i < start + 100; ++i)
  3905. {
  3906. xl[(std::size_t) i] = sl[(std::size_t) i] * 0.25f;
  3907. xr[(std::size_t) i] = sr[(std::size_t) i] * 0.25f;
  3908. }
  3909. a.process (xl.data() + start, xr.data() + start, 100);
  3910. }
  3911. float worst = 0.0f;
  3912. for (int i = 0; i < 300; ++i)
  3913. worst = std::max ({ worst, std::abs (dl[(std::size_t) i] - 0.5f - xl[(std::size_t) i]),
  3914. std::abs (dr[(std::size_t) i] + 0.5f - xr[(std::size_t) i]) });
  3915. CHECK (worst < 1e-6f);
  3916. // silenced source: the tail still in the line comes out, then nothing remains
  3917. std::vector<float> tl (64, 0.0f), tr (64, 0.0f);
  3918. b.mixInto (nullptr, nullptr, 1.0f, tl.data(), tr.data(), 64);
  3919. CHECK (std::abs (tl[0] - sl[263] * 0.25f) < 1e-6f);
  3920. std::vector<float> zl (64, 0.0f), zr (64, 0.0f);
  3921. b.mixInto (nullptr, nullptr, 1.0f, zl.data(), zr.data(), 64);
  3922. CHECK (*std::max_element (zl.begin(), zl.end()) == 0.0f && *std::min_element (zl.begin(), zl.end()) == 0.0f);
  3923. }
  3924. TEST (limiter_lookahead_holds_ceiling_on_bursts)
  3925. {
  3926. // short bursts over a low floor: the sliding maximum must see each peak before it leaves
  3927. auto fx = lynx::makeEffect (lynx::EffectKind::limiter);
  3928. fx->prepare (48000.0);
  3929. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  3930. slot.params[2] = 0.0f; // 10 ms release
  3931. std::vector<float> l (48000), r (48000);
  3932. std::uint32_t seed = 1;
  3933. for (int i = 0; i < 48000; ++i)
  3934. {
  3935. seed = seed * 1664525u + 1013904223u;
  3936. const auto noise = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.1f;
  3937. l[(std::size_t) i] = noise + ((i % 9973) < 3 ? 3.0f : 0.0f);
  3938. r[(std::size_t) i] = -noise;
  3939. }
  3940. for (int i = 0; i < 48000; i += 333)
  3941. fx->process (l.data() + i, r.data() + i, std::min (333, 48000 - i), slot.params, 120.0);
  3942. float peak = 0.0f, quiet = 0.0f;
  3943. for (int i = 0; i < 48000; ++i)
  3944. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  3945. for (int i = 9973 * 2 + 2000; i < 9973 * 2 + 2400; ++i) // 40 ms after the burst the gain has already recovered
  3946. quiet = std::max (quiet, std::abs (r[(std::size_t) i]));
  3947. const auto ceiling = 1.0f; // the default ceiling is at 0 dBFS or below
  3948. CHECK (peak <= ceiling + 1e-5f);
  3949. CHECK (quiet > 0.03f);
  3950. }
  3951. TEST (pdc_aligns_paths_through_latent_effects)
  3952. {
  3953. // plan: channel 0 -> insert 1 (effect of 100) -> insert 3; channel 1 -> insert 2 -> Master
  3954. lynx::Song s;
  3955. s.mixer = lynx::Song::defaultMixer (4);
  3956. s.channels.resize (3);
  3957. s.channels[0].mixerTrack = 1;
  3958. s.channels[1].mixerTrack = 2;
  3959. s.channels[2].mixerTrack = 3;
  3960. s.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::eq));
  3961. s.setRoute (1, 3, true);
  3962. s.setRoute (1, 0, false);
  3963. auto plan = lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } });
  3964. CHECK (plan.inAlign[1] == 0 && plan.outLat[1] == 100);
  3965. CHECK (plan.inAlign[3] == 100 && plan.channelDelay[2] == 70); // instrument of 30 on insert 3
  3966. CHECK (plan.sendDelay[1][1] == 0); // the slowest path isn't delayed
  3967. CHECK (plan.sendDelay[2][0] == 100 && plan.sendDelay[3][0] == 0 && plan.total == 100);
  3968. // bypass zeroes the slot's latency
  3969. s.mixer[1].effects[0].enabled = false;
  3970. CHECK (lynx::computePdc (s, { 0, 0, 30 }, { {}, { 100 } }).total == 30);
  3971. // rendered: two impulses played together come out together
  3972. lynx::Song r;
  3973. r.mixer = lynx::Song::defaultMixer (2);
  3974. for (int i = 0; i < 2; ++i)
  3975. {
  3976. lynx::ChannelDef c;
  3977. c.instrument = lynx::InstrumentKind::sampler;
  3978. c.samplePath = "impulso";
  3979. c.volume = 1.0f;
  3980. c.mixerTrack = i + 1;
  3981. r.channels.push_back (c);
  3982. }
  3983. r.patterns.emplace_back();
  3984. lynx::EffectSlot fx;
  3985. fx.kind = lynx::EffectKind::plugin;
  3986. fx.plugin.id = "latente";
  3987. r.mixer[1].effects.push_back (fx);
  3988. lynx::RackState::Factories f;
  3989. f.samples = [] (const std::string&) {
  3990. auto d = std::make_shared<lynx::SampleData>();
  3991. d->left.assign (64, 0.0f);
  3992. d->left[0] = 1.0f;
  3993. return std::shared_ptr<const lynx::SampleData> (d);
  3994. };
  3995. f.pluginEffects = [] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<LatentEffect> (100); };
  3996. auto state = lynx::RackState::build (r, nullptr, 48000.0, f);
  3997. CHECK (state->pdc.total == 100 && state->sendDelays[2][0] != nullptr && state->sendDelays[2][0]->length() == 100);
  3998. lynx::RackRenderer renderer;
  3999. renderer.prepare (48000.0, 256);
  4000. lynx::LiveEventQueue live;
  4001. for (int ch = 0; ch < 2; ++ch)
  4002. {
  4003. lynx::NoteEvent on;
  4004. on.channel = ch;
  4005. on.key = 60;
  4006. on.velocity = 1.0f;
  4007. live.push (on);
  4008. }
  4009. std::vector<float> l (256), rr (256);
  4010. renderer.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4011. int first = -1;
  4012. for (int i = 0; i < 256 && first < 0; ++i)
  4013. if (std::abs (l[(std::size_t) i]) > 0.01f)
  4014. first = i;
  4015. CHECK (first == 100 && std::abs (l[100] - 2.0f) < 1e-3f); // both impulses arrive together, summed
  4016. // editing without changing latencies reuses the lines
  4017. r.channels[0].volume = 0.5f;
  4018. auto next = lynx::RackState::build (r, state.get(), 48000.0, f);
  4019. CHECK (next->sendDelays[2][0] == state->sendDelays[2][0]);
  4020. }
  4021. TEST (mixer_mute_fades_instead_of_clicking)
  4022. {
  4023. lynx::Song s;
  4024. s.mixer = lynx::Song::defaultMixer (2);
  4025. lynx::ChannelDef c;
  4026. c.instrument = lynx::InstrumentKind::sampler;
  4027. c.samplePath = "dc";
  4028. c.volume = 1.0f;
  4029. c.mixerTrack = 1;
  4030. s.channels.push_back (c);
  4031. s.patterns.emplace_back();
  4032. auto provider = [] (const std::string&) {
  4033. auto d = std::make_shared<lynx::SampleData>();
  4034. d->left.assign (48000, 0.5f);
  4035. return std::shared_ptr<const lynx::SampleData> (d);
  4036. };
  4037. auto on = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4038. s.mixer[1].muted = true;
  4039. auto off = lynx::RackState::build (s, on.get(), 48000.0, provider);
  4040. lynx::RackRenderer r;
  4041. r.prepare (48000.0, 256);
  4042. lynx::LiveEventQueue live;
  4043. lynx::NoteEvent note;
  4044. note.velocity = 1.0f;
  4045. live.push (note);
  4046. std::vector<float> l (256), rr (256);
  4047. r.process (on.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  4048. CHECK (std::abs (l[255] - 0.5f) < 1e-3f);
  4049. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4050. CHECK (l[0] > 0.45f && l[128] > 0.2f && l[128] < 0.3f && std::abs (l[255]) < 1e-6f); // ramp down to zero
  4051. r.process (off.get(), false, 0.0, 256, l.data(), rr.data());
  4052. CHECK (std::abs (l[0]) < 1e-9f);
  4053. }
  4054. TEST (dither_quantizes_to_lsb_grid_with_small_error)
  4055. {
  4056. lynx::Dither d (16);
  4057. std::vector<float> x (4800);
  4058. for (std::size_t i = 0; i < x.size(); ++i)
  4059. x[i] = 0.25f * std::sin ((float) i * 0.05f);
  4060. auto y = x;
  4061. d.process (y.data(), (int) y.size(), 0);
  4062. double errorSum = 0.0;
  4063. bool onGrid = true;
  4064. for (std::size_t i = 0; i < x.size(); ++i)
  4065. {
  4066. const auto steps = y[i] * 32768.0f;
  4067. onGrid = onGrid && std::abs (steps - std::round (steps)) < 1e-3f;
  4068. errorSum += std::abs (y[i] - x[i]);
  4069. }
  4070. CHECK (onGrid);
  4071. CHECK (errorSum / (double) x.size() < 4.0 / 32768.0); // a few LSBs of noise on average
  4072. }
  4073. TEST (parallel_rendering_matches_sequential)
  4074. {
  4075. // 32 channels (worth parallelizing), inserts with chained sends
  4076. auto song = lynx::Song::makeDefault();
  4077. song.mixer = lynx::Song::defaultMixer (16);
  4078. for (int i = 5; i < 32; ++i)
  4079. {
  4080. lynx::ChannelDef c;
  4081. c.instrument = i % 2 ? lynx::InstrumentKind::synth : lynx::InstrumentKind::hat;
  4082. c.mixerTrack = 1 + i % 12;
  4083. c.volume = 0.3f;
  4084. song.channels.push_back (c);
  4085. for (int step = i % 4; step < 16; step += 3)
  4086. song.patterns[0].notes.push_back ({ (std::int64_t) step * lynx::ticksPerStep, 200, i, 48 + i, 0.7f });
  4087. }
  4088. song.patterns[0].sortNotes();
  4089. song.setRoute (3, 7, true, 0.6f);
  4090. song.setRoute (7, 9, true, 0.5f);
  4091. song.setRoute (2, 9, true);
  4092. song.mixer[9].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4093. auto seqState = lynx::RackState::build (song, nullptr, 48000.0);
  4094. auto parState = lynx::RackState::build (song, nullptr, 48000.0);
  4095. CHECK (parState->parallel && parState->mixerLayers.size() == 3);
  4096. lynx::WorkerPool pool (3);
  4097. lynx::RackRenderer seq, par;
  4098. seq.prepare (48000.0, 512);
  4099. par.prepare (48000.0, 512);
  4100. par.setWorkerPool (&pool);
  4101. std::vector<float> l1 (512), r1 (512), l2 (512), r2 (512);
  4102. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4103. bool same = true;
  4104. float peak = 0.0f;
  4105. for (int block = 0; block < 200; ++block)
  4106. {
  4107. seq.process (seqState.get(), true, block * 512 * tps, 512, l1.data(), r1.data());
  4108. par.process (parState.get(), true, block * 512 * tps, 512, l2.data(), r2.data());
  4109. same = same && l1 == l2 && r1 == r2;
  4110. for (auto v : l1)
  4111. peak = std::max (peak, std::abs (v));
  4112. }
  4113. CHECK (same);
  4114. CHECK (peak > 0.05f);
  4115. // the pool handles many small rounds in a row
  4116. std::atomic<int> sum { 0 };
  4117. auto add = [&sum] (int i) { sum.fetch_add (i + 1); };
  4118. for (int round = 0; round < 2000; ++round)
  4119. pool.parallelFor (5, add);
  4120. CHECK (sum.load() == 2000 * 15);
  4121. }
  4122. TEST (arrangements_switch_and_keep_clip_indices)
  4123. {
  4124. auto s = lynx::Song::makeDefault();
  4125. s.patterns.emplace_back().name = "B";
  4126. s.playlist = { { 1, 0, 0, 960, 0 } };
  4127. s.loopEnd = 960;
  4128. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0);
  4129. const auto second = s.addArrangement ("Versão curta", true);
  4130. CHECK (second == 1 && s.currentArrangement == 1 && s.playlist.size() == 1 && s.loopEnd == 960);
  4131. s.playlist.push_back ({ 0, 1, 960, 960, 0 });
  4132. s.loopEnd = 0;
  4133. s.selectArrangement (0);
  4134. CHECK (s.playlist.size() == 1 && s.loopEnd == 960 && s.arrangements[1].clips.size() == 2);
  4135. // removing pattern 0 fixes the clips stored in the other arrangement
  4136. s.removePattern (0);
  4137. CHECK (s.playlist.size() == 1 && s.playlist[0].pattern == 0);
  4138. CHECK (s.arrangements[1].clips.size() == 1 && s.arrangements[1].clips[0].pattern == 0);
  4139. s.removeArrangement (0);
  4140. CHECK (s.arrangements.size() == 1 && s.currentArrangement == 0 && s.playlist.size() == 1 && s.loopEnd == 0);
  4141. }
  4142. namespace
  4143. {
  4144. // Test effect that inverts the signal and counts how many times it processed.
  4145. class CountingInverter final : public lynx::Effect
  4146. {
  4147. public:
  4148. void prepare (double) noexcept override {}
  4149. void reset() noexcept override {}
  4150. void process (float* l, float* r, int n, const lynx::EffectParams&, double) noexcept override
  4151. {
  4152. ++calls;
  4153. for (int i = 0; i < n; ++i)
  4154. {
  4155. l[i] = -l[i];
  4156. r[i] = -r[i];
  4157. }
  4158. }
  4159. int calls = 0;
  4160. };
  4161. } // namespace
  4162. TEST (keyboard_controller_outputs_key_value)
  4163. {
  4164. std::vector<std::uint8_t> blob (554, 0);
  4165. const std::uint32_t version = 3;
  4166. std::memcpy (blob.data(), &version, 4);
  4167. const float key60 = 0.7f, off = 0.2f;
  4168. std::memcpy (blob.data() + 12 + 60 * 4, &key60, 4);
  4169. std::memcpy (blob.data() + 548, &off, 4);
  4170. blob[552] = 1; // auto release
  4171. lynx::ChannelDef c;
  4172. c.instrument = lynx::InstrumentKind::controller;
  4173. c.controllerState = blob;
  4174. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  4175. inst->prepare (48000.0);
  4176. inst->configure (c, 120.0);
  4177. std::vector<float> l (64), r (64);
  4178. inst->noteOn (60, 1.0f);
  4179. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  4180. CHECK (std::abs (inst->controlOutput (0) - 0.7f) < 1e-6f);
  4181. inst->noteOff (60);
  4182. CHECK (std::abs (inst->controlOutput (0) - 0.2f) < 1e-6f);
  4183. }
  4184. TEST (envelope_controller_ducks_on_each_note)
  4185. {
  4186. // a unipolar articulator like "Basic Sidechain": base 0.05, drops to 0 on the note and rises to 1 in 0.3 s
  4187. lynx::ControllerArticulator a;
  4188. a.envOn = true;
  4189. a.base = 0.05f;
  4190. a.envLevel = 1.0f;
  4191. a.count = 3;
  4192. a.time = { 0.0f, 0.2f, 0.3f };
  4193. a.level = { 0.0f, 0.75f, 1.0f };
  4194. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.05f) < 1e-4f);
  4195. CHECK (std::abs (lynx::controllerValue (a, 0.1, -1.0) - 0.425f) < 1e-3f);
  4196. CHECK (lynx::controllerValue (a, 1.0, -1.0) > 0.99f); // no sustain: runs to the end and stays
  4197. a.sustain = 1;
  4198. CHECK (std::abs (lynx::controllerValue (a, 1.0, -1.0) - 0.8f) < 1e-3f); // held at sustain
  4199. CHECK (lynx::controllerValue (a, 1.0, 0.5) > 0.99f); // released: continues to the end
  4200. a.bipolar = true;
  4201. a.base = 0.5f;
  4202. a.sustain = -1;
  4203. CHECK (std::abs (lynx::controllerValue (a, 0.0, -1.0) - 0.0f) < 1e-4f); // bipolar: 0.5 + (0 − 0.5)
  4204. }
  4205. TEST (peak_controller_drives_live_automation)
  4206. {
  4207. // track 1: source (DC 0.5) with Peak Controller; track 2: DC 0.5 with volume linked to the controller
  4208. auto render = [] (bool linked) {
  4209. lynx::Song s;
  4210. s.mixer = lynx::Song::defaultMixer (2);
  4211. for (int t = 1; t <= 2; ++t)
  4212. {
  4213. lynx::ChannelDef c;
  4214. c.instrument = lynx::InstrumentKind::sampler;
  4215. c.samplePath = "dc";
  4216. c.volume = 1.0f;
  4217. c.mixerTrack = t;
  4218. s.channels.push_back (c);
  4219. }
  4220. s.patterns.emplace_back();
  4221. auto peak = lynx::makeEffectSlot (lynx::EffectKind::peakController);
  4222. peak.params[0] = 1.0f; // base 1
  4223. peak.params[1] = 0.0f; // amount −1: 1 − peak
  4224. peak.params[2] = 0.5f; // no tension
  4225. s.mixer[1].effects.push_back (peak);
  4226. if (linked)
  4227. {
  4228. lynx::Automation a;
  4229. a.name = "Peak";
  4230. a.target = { lynx::AutomationTargetKind::mixerVolume, 2 };
  4231. a.source = { 1, 0, 0 };
  4232. s.automations.push_back (a);
  4233. }
  4234. lynx::RackState::Factories f;
  4235. f.samples = [] (const std::string&) {
  4236. auto d = std::make_shared<lynx::SampleData>();
  4237. d->left.assign (48000, 0.5f);
  4238. return std::shared_ptr<const lynx::SampleData> (d);
  4239. };
  4240. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4241. lynx::RackRenderer r;
  4242. r.prepare (48000.0, 512);
  4243. lynx::LiveEventQueue live;
  4244. for (int ch = 0; ch < 2; ++ch)
  4245. {
  4246. lynx::NoteEvent on;
  4247. on.channel = ch;
  4248. on.velocity = 1.0f;
  4249. live.push (on);
  4250. }
  4251. std::vector<float> l (4800), rr (4800);
  4252. r.process (state.get(), false, 0.0, 4800, l.data(), rr.data(), &live);
  4253. return l[4000];
  4254. };
  4255. const auto free = render (false), ducked = render (true);
  4256. CHECK (std::isfinite (ducked) && ducked > 0.0f);
  4257. CHECK (ducked < free - 0.05f); // the 0.5 peak on track 1 lowers track 2's volume
  4258. }
  4259. TEST (effect_slot_dry_wet_and_smart_disable)
  4260. {
  4261. lynx::Song s;
  4262. s.mixer = lynx::Song::defaultMixer (1);
  4263. lynx::ChannelDef c;
  4264. c.instrument = lynx::InstrumentKind::sampler;
  4265. c.samplePath = "dc";
  4266. c.volume = 1.0f;
  4267. c.mixerTrack = 1;
  4268. s.channels.push_back (c);
  4269. s.patterns.emplace_back();
  4270. lynx::EffectSlot fx;
  4271. fx.kind = lynx::EffectKind::plugin;
  4272. fx.plugin.id = "inversor";
  4273. fx.mix = 0.25f; // 75% dry (+0.5) and 25% wet (-0.5) = +0.25
  4274. s.mixer[1].effects.push_back (fx);
  4275. auto inverter = std::make_shared<CountingInverter>();
  4276. lynx::RackState::Factories f;
  4277. f.samples = [] (const std::string&) {
  4278. auto d = std::make_shared<lynx::SampleData>();
  4279. d->left.assign (4800, 0.5f);
  4280. return std::shared_ptr<const lynx::SampleData> (d);
  4281. };
  4282. f.pluginEffects = [inverter] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Effect> { return inverter; };
  4283. auto state = lynx::RackState::build (s, nullptr, 48000.0, f);
  4284. lynx::RackRenderer r;
  4285. r.prepare (48000.0, 512);
  4286. lynx::LiveEventQueue live;
  4287. lynx::NoteEvent on;
  4288. on.velocity = 1.0f;
  4289. live.push (on);
  4290. std::vector<float> l (480), rr (480);
  4291. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4292. CHECK (std::abs (l[100] - 0.25f) < 1e-4f);
  4293. // after the sample ends: 4 s of silence and the plugin stops being called
  4294. for (int i = 0; i < 100 * 6; ++i) // 6 s
  4295. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4296. const auto callsAfterSilence = inverter->calls;
  4297. for (int i = 0; i < 100; ++i)
  4298. r.process (state.get(), false, 0.0, 480, l.data(), rr.data());
  4299. CHECK (inverter->calls == callsAfterSilence);
  4300. CHECK (callsAfterSilence < 100 * 6);
  4301. // sound again: processing resumes in the same block
  4302. live.push (on);
  4303. r.process (state.get(), false, 0.0, 480, l.data(), rr.data(), &live);
  4304. CHECK (inverter->calls == callsAfterSilence + 1 && std::abs (l[100] - 0.25f) < 1e-4f);
  4305. }
  4306. TEST (swing_delays_offbeat_sixteenths)
  4307. {
  4308. CHECK (lynx::swingDelay (0, 1.0f) == 0);
  4309. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f) == lynx::ticksPerStep / 2);
  4310. CHECK (lynx::swingDelay (lynx::ticksPerStep * 2, 1.0f) == 0);
  4311. CHECK (lynx::swingDelay (lynx::ticksPerStep + 10, 1.0f) == (lynx::ticksPerStep - 20) / 2); // off the grid: triangular weight (FL)
  4312. CHECK (lynx::swingDelay (lynx::ticksPerStep, 1.0f, 0.5f) == lynx::ticksPerStep / 4); // channel mix
  4313. CHECK (lynx::swingDelay (lynx::ticksPerStep * 3, 0.5f) == lynx::ticksPerStep / 4);
  4314. lynx::Pattern p;
  4315. p.notes = { { 0, 100, 0, 60 }, { lynx::ticksPerStep, 100, 0, 62 } };
  4316. lynx::EventBuffer ev;
  4317. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4318. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 12000, tps, ev, 1.0f);
  4319. int onOffset = -1;
  4320. for (auto& e : ev)
  4321. if (e.type == lynx::NoteEvent::Type::on && e.key == 62)
  4322. onOffset = e.sampleOffset;
  4323. CHECK (onOffset == (int) std::floor ((lynx::ticksPerStep * 1.5) / tps));
  4324. }
  4325. TEST (sampler_settings_pitch_reverse_envelope_cut)
  4326. {
  4327. auto ramp = std::make_shared<lynx::SampleData>();
  4328. for (int i = 0; i < 48000; ++i)
  4329. ramp->left.push_back ((float) i / 48000.0f); // ramp: the value tells where the read position is
  4330. ramp->sampleRate = 48000.0;
  4331. auto play = [&] (const lynx::SamplerSettings& s, int key, int frames, bool releaseAfterHalf) {
  4332. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4333. inst->prepare (48000.0);
  4334. lynx::ChannelDef c;
  4335. c.sampler = s;
  4336. inst->configure (c, 120.0);
  4337. inst->noteOn (key, 1.0f);
  4338. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4339. inst->render (l.data(), r.data(), frames / 2, 1.0f, 1.0f);
  4340. if (releaseAfterHalf)
  4341. inst->noteOff (key);
  4342. inst->render (l.data() + frames / 2, r.data() + frames / 2, frames - frames / 2, 1.0f, 1.0f);
  4343. return l;
  4344. };
  4345. // +12 semitones: reads twice as fast
  4346. lynx::SamplerSettings up;
  4347. up.pitch = 12.0f;
  4348. CHECK (std::abs (play (up, 60, 1000, false)[999] - 2.0f * 999.0f / 48000.0f) < 1e-3f);
  4349. // reversed: starts from the end
  4350. lynx::SamplerSettings back;
  4351. back.reverse = true;
  4352. CHECK (play (back, 60, 100, false)[10] > 0.99f);
  4353. // 10 ms attack: starts silent and reaches full level
  4354. lynx::SamplerSettings slow;
  4355. slow.attack = 0.01f;
  4356. const auto a = play (slow, 60, 2000, false);
  4357. CHECK (a[10] < 0.2f * (10.0f / 48000.0f) + 1e-6f && std::abs (a[1500] - 1500.0f / 48000.0f) < 1e-4f);
  4358. // 5 ms release: gone right after release; without release it plays in full
  4359. lynx::SamplerSettings rel;
  4360. rel.release = 0.005f;
  4361. CHECK (play (rel, 60, 2000, true)[1999] == 0.0f);
  4362. CHECK (play (lynx::SamplerSettings {}, 60, 2000, true)[1999] > 0.0f);
  4363. // cut itself: the second note cuts the first (one voice remains)
  4364. lynx::SamplerSettings cut;
  4365. cut.cut = true;
  4366. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4367. inst->prepare (48000.0);
  4368. lynx::ChannelDef cutChannel;
  4369. cutChannel.sampler = cut;
  4370. inst->configure (cutChannel, 120.0);
  4371. inst->noteOn (60, 1.0f);
  4372. std::vector<float> l (2000), r (2000);
  4373. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4374. inst->noteOn (60, 1.0f);
  4375. std::fill (l.begin(), l.end(), 0.0f);
  4376. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  4377. CHECK (std::abs (l[1999] - 1999.0f / 48000.0f) < 1e-4f); // only the new note
  4378. }
  4379. TEST (playlist_split_and_track_mute)
  4380. {
  4381. lynx::Clip c { 0, 3, 960, 3840, 480 };
  4382. const auto rest = lynx::splitClip (c, 2000);
  4383. CHECK (c.start == 960 && c.length == 1040 && c.offset == 480);
  4384. CHECK (rest.start == 2000 && rest.length == 2800 && rest.offset == 480 + 1040 && rest.track == 3);
  4385. auto s = lynx::Song::makeDefault();
  4386. s.mode = lynx::PlayMode::song;
  4387. s.playlist = { { 0, 0, 0, 3840, 0 } };
  4388. lynx::EventBuffer ev;
  4389. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4390. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4391. CHECK (ev.size() > 0);
  4392. s.playlistTracks.resize (1);
  4393. s.playlistTracks[0].muted = true;
  4394. ev.clear();
  4395. lynx::scheduleSong (s, 0.0, 48000, tps, ev);
  4396. CHECK (ev.size() == 0);
  4397. }
  4398. TEST (note_pan_pitch_and_mute)
  4399. {
  4400. lynx::Pattern p;
  4401. lynx::Note a { 0, 240, 0, 60 };
  4402. a.pan = -1.0f;
  4403. a.finePitch = 50.0f;
  4404. lynx::Note b { 240, 240, 0, 62 };
  4405. b.muted = true;
  4406. p.notes = { a, b };
  4407. lynx::EventBuffer ev;
  4408. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4409. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 48000, tps, ev);
  4410. int ons = 0;
  4411. for (auto& e : ev)
  4412. if (e.type == lynx::NoteEvent::Type::on)
  4413. {
  4414. ++ons;
  4415. CHECK (e.key == 60 && e.pan == -1.0f && e.cents == 50.0f);
  4416. }
  4417. CHECK (ons == 1); // the muted one doesn't play
  4418. // note pan in the sampler: fully left
  4419. auto dc = std::make_shared<lynx::SampleData>();
  4420. dc->left.assign (4800, 0.5f);
  4421. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, dc);
  4422. inst->prepare (48000.0);
  4423. inst->setNextNoteExpression (-1.0f, 0.0f);
  4424. inst->noteOn (60, 1.0f);
  4425. std::vector<float> l (100), r (100);
  4426. inst->render (l.data(), r.data(), 100, 1.0f, 1.0f);
  4427. CHECK (l[50] > 0.6f && std::abs (r[50]) < 1e-6f);
  4428. }
  4429. TEST (tempo_automation_changes_speed_and_duration)
  4430. {
  4431. auto song = lynx::Song::makeDefault();
  4432. song.bpm = 120.0;
  4433. song.mode = lynx::PlayMode::song;
  4434. const auto bar = (std::int64_t) lynx::ppq * 4;
  4435. song.playlist = { { 0, 0, 0, bar, 0 }, { 0, 0, bar, bar, 0 } };
  4436. lynx::Automation tempo;
  4437. tempo.target = { lynx::AutomationTargetKind::tempo, 0 };
  4438. const auto v120 = lynx::valueFromTempo (120.0), v240 = lynx::valueFromTempo (240.0);
  4439. tempo.points = { { 0, v120 }, { bar - 1, v120 }, { bar, v240 } }; // step: 120 -> 240 at bar 2
  4440. song.automations.push_back (tempo);
  4441. lynx::Clip clip;
  4442. clip.kind = lynx::ClipKind::automation;
  4443. clip.automation = 0;
  4444. clip.track = 1;
  4445. clip.length = bar * 2;
  4446. song.playlist.push_back (clip);
  4447. CHECK (std::abs (song.tempoAt (100.0) - 120.0) < 0.01 && std::abs (song.tempoAt ((double) bar + 100.0) - 240.0) < 0.01);
  4448. // 1 bar at 120 (2 s) + 1 at 240 (1 s)
  4449. CHECK (std::abs (song.secondsForTicks (bar * 2) - 3.0) < 0.01);
  4450. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4451. CHECK (state->tempoAutomation == 0);
  4452. lynx::RackRenderer r;
  4453. r.prepare (48000.0, 512);
  4454. std::vector<float> l (512), rr (512);
  4455. r.process (state.get(), true, 10.0, 512, l.data(), rr.data());
  4456. CHECK (std::abs (r.currentBpm() - 120.0) < 0.01);
  4457. r.process (state.get(), true, (double) bar + 10.0, 512, l.data(), rr.data());
  4458. CHECK (std::abs (r.currentBpm() - 240.0) < 0.01);
  4459. }
  4460. TEST (bounce_insert_song_and_apply)
  4461. {
  4462. auto s = lynx::Song::makeDefault();
  4463. s.setRoute (3, 7, true, 0.5f);
  4464. s.mixer[0].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::reverb));
  4465. s.mixer[2].solo = true;
  4466. const auto b = lynx::makeBounceSong (s, 3);
  4467. CHECK (b.mixer[3].solo && ! b.mixer[2].solo && b.mixer[3].toMaster && b.mixer[3].sends.empty());
  4468. CHECK (b.mixer[0].effects.empty() && b.mode == lynx::PlayMode::song);
  4469. const auto audible = b.soloAudible();
  4470. CHECK (audible[3] && ! audible[1] && ! audible[7]);
  4471. const auto channels = s.channels.size();
  4472. const auto inserts = s.mixer.size();
  4473. const auto created = lynx::applyBounce (s, 3, "C:/x.wav", 3840);
  4474. CHECK (created == (int) inserts && s.mixer.size() == inserts + 1 && s.mixer[3].muted);
  4475. CHECK (s.mixer[(std::size_t) created].sends.size() == 1 && s.mixer[(std::size_t) created].sends[0].target == 7);
  4476. CHECK (s.channels.size() == channels + 1 && s.channels.back().mixerTrack == created);
  4477. CHECK (s.playlist.back().kind == lynx::ClipKind::audio && s.playlist.back().length == 3840);
  4478. }
  4479. TEST (mixer_stereo_separation_phase_swap)
  4480. {
  4481. lynx::MixerTrack t;
  4482. std::vector<float> l { 1.0f }, r { 0.0f };
  4483. t.stereo = 1.0f; // mono
  4484. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4485. CHECK (std::abs (l[0] - 0.5f) < 1e-6f && std::abs (r[0] - 0.5f) < 1e-6f);
  4486. l = { 1.0f };
  4487. r = { 0.0f };
  4488. t.stereo = -1.0f; // doubled side: 1.5 L - 0.5 R
  4489. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4490. CHECK (std::abs (l[0] - 1.5f) < 1e-6f && std::abs (r[0] + 0.5f) < 1e-6f);
  4491. l = { 1.0f };
  4492. r = { 0.25f };
  4493. t.stereo = 0.0f;
  4494. t.swapChannels = true;
  4495. t.invertPhase = true;
  4496. lynx::RackRenderer::applyStereoForTest (t, l.data(), r.data(), 1);
  4497. CHECK (l[0] == -0.25f && r[0] == -1.0f);
  4498. }
  4499. TEST (effects_limiter_filter_distortion_chorus)
  4500. {
  4501. const double sr = 48000.0;
  4502. auto sine = [] (float hz, float amp, int n) {
  4503. std::vector<float> v ((std::size_t) n);
  4504. for (int i = 0; i < n; ++i)
  4505. v[(std::size_t) i] = amp * std::sin (2.0f * 3.14159265f * hz * (float) i / 48000.0f);
  4506. return v;
  4507. };
  4508. auto peak = [] (const std::vector<float>& v, int from) {
  4509. float p = 0.0f;
  4510. for (std::size_t i = (std::size_t) from; i < v.size(); ++i)
  4511. p = std::max (p, std::abs (v[i]));
  4512. return p;
  4513. };
  4514. // limiter: +12 dB on a 0.9 sine never exceeds the ceiling (-1 dB) and reports the latency
  4515. auto lim = lynx::makeEffect (lynx::EffectKind::limiter);
  4516. lim->prepare (sr);
  4517. auto slot = lynx::makeEffectSlot (lynx::EffectKind::limiter);
  4518. slot.params[0] = 11.0f / 12.0f; // -1 dB
  4519. slot.params[1] = 0.5f; // +12 dB
  4520. auto l = sine (100.0f, 0.9f, 48000), r = l;
  4521. lim->process (l.data(), r.data(), 48000, slot.params, 120.0);
  4522. CHECK (peak (l, 0) <= std::pow (10.0f, -1.0f / 20.0f) + 1e-4f);
  4523. CHECK (peak (l, 4800) > 0.8f);
  4524. CHECK (lim->latencySamples() == 72);
  4525. // low-pass filter at 500 Hz knocks down an 8 kHz sine
  4526. auto lp = lynx::makeEffect (lynx::EffectKind::filter);
  4527. lp->prepare (sr);
  4528. auto fs = lynx::makeEffectSlot (lynx::EffectKind::filter);
  4529. fs.params[1] = std::log (500.0f / 20.0f) / std::log (1000.0f);
  4530. l = sine (8000.0f, 1.0f, 9600);
  4531. r = l;
  4532. lp->process (l.data(), r.data(), 9600, fs.params, 120.0);
  4533. CHECK (peak (l, 4800) < 0.05f);
  4534. // distortion and chorus: finite, audible output
  4535. for (auto kind : { lynx::EffectKind::distortion, lynx::EffectKind::chorus })
  4536. {
  4537. auto fx = lynx::makeEffect (kind);
  4538. fx->prepare (sr);
  4539. auto ps = lynx::makeEffectSlot (kind);
  4540. l = sine (220.0f, 0.5f, 9600);
  4541. r = l;
  4542. fx->process (l.data(), r.data(), 9600, ps.params, 120.0);
  4543. bool finite = true;
  4544. for (auto v : l)
  4545. finite = finite && std::isfinite (v);
  4546. CHECK (finite && peak (l, 2400) > 0.1f && peak (l, 0) < 2.0f);
  4547. }
  4548. }
  4549. TEST (slide_notes_glide_without_retrigger)
  4550. {
  4551. // sampler with a ramp: the read speed doubles after a one-octave slide
  4552. auto ramp = std::make_shared<lynx::SampleData>();
  4553. for (int i = 0; i < 96000; ++i)
  4554. ramp->left.push_back ((float) i / 96000.0f);
  4555. ramp->sampleRate = 48000.0;
  4556. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4557. inst->prepare (48000.0);
  4558. inst->noteOn (60, 1.0f);
  4559. std::vector<float> l (2000), r (2000);
  4560. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4561. const auto stepBefore = l[999] - l[998];
  4562. inst->slideTo (72, 0.0f, 100.0f);
  4563. std::fill (l.begin(), l.end(), 0.0f);
  4564. inst->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  4565. const auto stepAfter = l[999] - l[998];
  4566. CHECK (std::abs (stepAfter / stepBefore - 2.0f) < 0.01f);
  4567. // the sequencer sends "slide" (no off) for the slide note
  4568. lynx::Pattern p;
  4569. lynx::Note base { 0, 960, 0, 48 };
  4570. lynx::Note glide { 480, 240, 0, 55 };
  4571. glide.slide = true;
  4572. p.notes = { base, glide };
  4573. lynx::EventBuffer ev;
  4574. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  4575. lynx::schedulePatternLoop (p, p.effectiveLength (4), 0.0, 30000, tps, ev);
  4576. int ons = 0, offs = 0, slides = 0;
  4577. for (auto& e : ev)
  4578. {
  4579. ons += e.type == lynx::NoteEvent::Type::on;
  4580. offs += e.type == lynx::NoteEvent::Type::off;
  4581. if (e.type == lynx::NoteEvent::Type::slide)
  4582. {
  4583. ++slides;
  4584. CHECK (e.key == 55 && std::abs (e.slideSamples - 240.0f / (float) tps) < 1.0f);
  4585. }
  4586. }
  4587. CHECK (ons == 1 && offs == 1 && slides == 1);
  4588. }
  4589. TEST (synth_fm_phase_modulation)
  4590. {
  4591. auto render = [] (const lynx::SynthSettings& s) {
  4592. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4593. inst->prepare (48000.0);
  4594. lynx::ChannelDef c;
  4595. c.synth = s;
  4596. inst->configure (c, 120.0);
  4597. inst->noteOn (69, 1.0f); // 440 Hz
  4598. std::vector<float> l (48000), r (48000);
  4599. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  4600. return l;
  4601. };
  4602. // energy (Goertzel) at frequency f over frames [from, to)
  4603. auto energy = [] (const std::vector<float>& x, double f, int from, int to) {
  4604. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  4605. double s1 = 0.0, s2 = 0.0;
  4606. for (int i = from; i < to; ++i)
  4607. {
  4608. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  4609. s2 = s1;
  4610. s1 = s0;
  4611. }
  4612. return s1 * s1 + s2 * s2 - k * s1 * s2;
  4613. };
  4614. lynx::SynthSettings plain;
  4615. plain.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4616. plain.osc[2] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  4617. plain.cutoff = 20000.0f;
  4618. auto fm = plain;
  4619. fm.fm = 1.0f;
  4620. const auto a = render (plain), b = render (fm);
  4621. // no FM: osc 3 adds (same pitch) and there's no 2nd harmonic; with FM the sidebands appear
  4622. CHECK (energy (a, 880.0, 4800, 24000) < 1e-3 * energy (a, 440.0, 4800, 24000));
  4623. CHECK (energy (b, 880.0, 4800, 24000) > 0.1 * energy (b, 440.0, 4800, 24000));
  4624. CHECK (energy (b, 1320.0, 4800, 24000) > 0.01 * energy (b, 440.0, 4800, 24000));
  4625. for (auto v : b)
  4626. CHECK (std::isfinite (v) && std::abs (v) < 1.0f);
  4627. // index decaying to 0: the start is bright, the end is nearly a sine again
  4628. auto decaying = fm;
  4629. decaying.fmDecay = 0.1f;
  4630. decaying.fmSustain = 0.0f;
  4631. const auto c = render (decaying);
  4632. const auto early = energy (c, 880.0, 0, 2400) / energy (c, 440.0, 0, 2400);
  4633. const auto late = energy (c, 880.0, 36000, 48000) / energy (c, 440.0, 36000, 48000);
  4634. CHECK (early > 0.05 && late < 1e-3);
  4635. }
  4636. TEST (synth_three_oscillators_adsr_filter)
  4637. {
  4638. auto render = [] (const lynx::SynthSettings& s, int frames, int releaseAt) {
  4639. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  4640. inst->prepare (48000.0);
  4641. lynx::ChannelDef c;
  4642. c.synth = s;
  4643. inst->configure (c, 120.0);
  4644. inst->noteOn (69, 1.0f); // A4 = 440 Hz
  4645. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  4646. inst->render (l.data(), r.data(), releaseAt, 1.0f, 1.0f);
  4647. inst->noteOff (69);
  4648. inst->render (l.data() + releaseAt, r.data() + releaseAt, frames - releaseAt, 1.0f, 1.0f);
  4649. return l;
  4650. };
  4651. auto peakIn = [] (const std::vector<float>& v, int from, int to) {
  4652. float p = 0.0f;
  4653. for (int i = from; i < to; ++i)
  4654. p = std::max (p, std::abs (v[(std::size_t) i]));
  4655. return p;
  4656. };
  4657. // default: sounds and fades after the release
  4658. const lynx::SynthSettings standard;
  4659. const auto a = render (standard, 48000, 24000);
  4660. CHECK (peakIn (a, 1000, 20000) > 0.05f && peakIn (a, 40000, 48000) < 1e-3f);
  4661. // pure sine with an open filter: zero-crossing count gives ~440 Hz
  4662. lynx::SynthSettings sine;
  4663. sine.osc[0].wave = lynx::Waveform::sine;
  4664. sine.cutoff = 20000.0f;
  4665. const auto b = render (sine, 48000, 47000);
  4666. int crossings = 0;
  4667. for (int i = 4800; i < 4800 + 48000 / 2; ++i)
  4668. crossings += (b[(std::size_t) i - 1] < 0.0f) != (b[(std::size_t) i] < 0.0f);
  4669. CHECK (std::abs (crossings - 440) < 6); // half a second = 220 cycles = 440 crossings
  4670. float sinePeak = 0.0f;
  4671. for (int i = 4800; i < 9600; ++i)
  4672. sinePeak = std::max (sinePeak, std::abs (b[(std::size_t) i]));
  4673. CHECK (sinePeak > 0.2f && sinePeak < 0.35f); // 0.3 level, approximated sine without clipping
  4674. // second oscillator an octave up adds energy; sustain 0 makes the note decay on its own
  4675. lynx::SynthSettings two = sine;
  4676. two.osc[1] = { lynx::Waveform::sine, 1, 0.0f, 1.0f };
  4677. two.sustain = 0.0f;
  4678. two.decay = 0.1f;
  4679. const auto c = render (two, 48000, 47000);
  4680. CHECK (peakIn (c, 100, 2000) > peakIn (b, 100, 2000) && peakIn (c, 30000, 40000) < 1e-3f);
  4681. }
  4682. TEST (set_target_value_round_trips)
  4683. {
  4684. auto s = lynx::Song::makeDefault();
  4685. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  4686. const lynx::AutomationTarget targets[] = {
  4687. { lynx::AutomationTargetKind::channelVolume, 1 }, { lynx::AutomationTargetKind::channelPan, 2 },
  4688. { lynx::AutomationTargetKind::mixerVolume, 3 }, { lynx::AutomationTargetKind::mixerPan, 0 },
  4689. { lynx::AutomationTargetKind::effectParam, 2, 0, 1 }, { lynx::AutomationTargetKind::tempo, 0 },
  4690. };
  4691. for (const auto& t : targets)
  4692. {
  4693. CHECK (s.setTargetValue (t, 0.25f));
  4694. CHECK (std::abs (s.targetValue (t) - 0.25f) < 1e-5f);
  4695. }
  4696. CHECK (! s.setTargetValue ({ lynx::AutomationTargetKind::channelParam, 0, 0, 3 }, 0.5f)); // lives in the plugin
  4697. }
  4698. TEST (cut_groups_stretch_and_envelope_import)
  4699. {
  4700. // open hat (cut by 1) is cut when the closed one (group 1) plays
  4701. lynx::Song s;
  4702. s.mixer = lynx::Song::defaultMixer (2);
  4703. for (int i = 0; i < 2; ++i)
  4704. {
  4705. lynx::ChannelDef c;
  4706. c.instrument = lynx::InstrumentKind::sampler;
  4707. c.samplePath = "dc";
  4708. c.volume = 1.0f;
  4709. c.mixerTrack = i + 1;
  4710. s.channels.push_back (c);
  4711. }
  4712. s.channels[0].cutBy = 1; // open
  4713. s.channels[1].cutGroup = 1; // closed
  4714. auto& p = s.patterns.emplace_back();
  4715. p.notes = { { 0, 100, 0, 60 }, { 960, 100, 1, 60 } };
  4716. auto provider = [] (const std::string&) {
  4717. auto d = std::make_shared<lynx::SampleData>();
  4718. d->left.assign (96000, 0.5f);
  4719. return std::shared_ptr<const lynx::SampleData> (d);
  4720. };
  4721. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4722. CHECK (state->hasCutGroups && state->cutTargets[1].size() == 1 && state->cutTargets[1][0] == 0);
  4723. lynx::RackRenderer r;
  4724. r.prepare (48000.0, 512);
  4725. std::vector<float> l (512), rr (512);
  4726. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  4727. float afterCut = 0.0f;
  4728. for (double tick = 0.0; tick < 960.0 * 3; tick += 512 * tps)
  4729. {
  4730. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4731. if (tick > 960.0 * 2) // the closed one continues; the open one has stopped
  4732. afterCut = std::max (afterCut, std::abs (l[256]));
  4733. }
  4734. CHECK (std::abs (afterCut - 0.5f * 0.78f) < 0.01f); // only the closed one (default velocity 0.78)
  4735. // resampling stretch: 1 s of sample stretched to 4 beats at 120 BPM = 2 s
  4736. auto ramp = std::make_shared<lynx::SampleData>();
  4737. for (int i = 0; i < 48000; ++i)
  4738. ramp->left.push_back ((float) i / 48000.0f);
  4739. ramp->sampleRate = 48000.0;
  4740. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4741. inst->prepare (48000.0);
  4742. lynx::ChannelDef c;
  4743. c.sampler.stretchBeats = 4.0f;
  4744. inst->configure (c, 120.0);
  4745. inst->noteOn (72, 1.0f); // the note pitch doesn't change the speed
  4746. std::vector<float> a (48000), b (48000);
  4747. inst->render (a.data(), b.data(), 48000, 1.0f, 1.0f);
  4748. CHECK (std::abs (a[47999] - 0.5f) < 0.01f); // after 1 s it has read half
  4749. // FL envelope time: 32950 = 2 s; < 101 = 0
  4750. CHECK (std::abs (flp::Channel::envelopeTime (32950) - 2.0) < 0.01 && flp::Channel::envelopeTime (50) == 0.0);
  4751. }
  4752. TEST (time_stretch_keeps_pitch)
  4753. {
  4754. lynx::SampleData sine;
  4755. sine.sampleRate = 48000.0;
  4756. for (int i = 0; i < 48000; ++i)
  4757. sine.left.push_back (0.5f * std::sin (2.0f * 3.14159265f * 440.0f * (float) i / 48000.0f));
  4758. auto crossingsPerSecond = [] (const lynx::SampleData& d) {
  4759. int c = 0;
  4760. const auto n = (int) d.frames();
  4761. for (int i = 4800; i < n - 4800; ++i)
  4762. c += (d.left[(std::size_t) i - 1] < 0.0f) != (d.left[(std::size_t) i] < 0.0f);
  4763. return (double) c / ((double) (n - 9600) / d.sampleRate);
  4764. };
  4765. for (double ratio : { 2.0, 0.5, 1.37 })
  4766. {
  4767. const auto out = lynx::timeStretch (sine, ratio);
  4768. CHECK (std::abs ((double) out->frames() - 48000.0 * ratio) < 2.0);
  4769. CHECK (std::abs (crossingsPerSecond (*out) - 880.0) < 15.0); // 440 Hz stays 440 Hz
  4770. float peak = 0.0f;
  4771. for (std::size_t i = 4800; i + 4800 < out->frames(); ++i)
  4772. peak = std::max (peak, std::abs (out->left[i]));
  4773. CHECK (peak > 0.4f && peak < 0.6f); // no gaps or clipping in the overlap
  4774. }
  4775. }
  4776. TEST (sampler_region_start_and_end)
  4777. {
  4778. auto ramp = std::make_shared<lynx::SampleData>();
  4779. for (int i = 0; i < 10000; ++i)
  4780. ramp->left.push_back ((float) i / 10000.0f);
  4781. ramp->sampleRate = 48000.0;
  4782. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4783. inst->prepare (48000.0);
  4784. lynx::ChannelDef c;
  4785. c.sampler.regionStart = 0.25f;
  4786. c.sampler.regionEnd = 0.5f;
  4787. inst->configure (c, 120.0);
  4788. inst->noteOn (60, 1.0f);
  4789. std::vector<float> l (6000), r (6000);
  4790. inst->render (l.data(), r.data(), 6000, 1.0f, 1.0f);
  4791. CHECK (std::abs (l[0] - 0.25f) < 0.001f); // starts at the beginning of the slice
  4792. CHECK (l[2400] > 0.48f && l[2600] == 0.0f); // and stops at its end
  4793. }
  4794. TEST (arpeggiator_plays_held_chord_in_steps)
  4795. {
  4796. auto song = lynx::Song::makeDefault();
  4797. song.bpm = 120.0;
  4798. song.patterns[0].notes.clear();
  4799. for (int key : { 60, 64, 67 })
  4800. song.patterns[0].notes.push_back ({ 0, lynx::ppq * 4, 4, key, 0.8f }); // chord held for 1 bar
  4801. song.channels[4].arp.direction = 1; // up
  4802. song.channels[4].arp.stepBeats = 0.25f;
  4803. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4804. CHECK (state->arpeggiators[4] != nullptr && state->arpeggiators[0] == nullptr);
  4805. // the arpeggiator generates the notes; we count what it hands to the instrument
  4806. auto& arp = *state->arpeggiators[4];
  4807. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4808. std::vector<int> keys;
  4809. lynx::EventBuffer ev;
  4810. for (double tick = 0.0; tick < lynx::ppq * 2.0; tick += 512 * tps)
  4811. {
  4812. ev.clear();
  4813. lynx::schedulePatternLoop (song.patterns[0], song.patterns[0].effectiveLength (4), tick, 512, tps, ev);
  4814. int in = 0, out = 0;
  4815. for (auto& e : ev)
  4816. if (e.channel == 4)
  4817. arp.input[(std::size_t) in++] = e;
  4818. arp.process (4, arp.input.data(), in, tick, tps, 512, song.channels[4].arp, arp.output.data(), out);
  4819. for (int i = 0; i < out; ++i)
  4820. if (arp.output[(std::size_t) i].type == lynx::NoteEvent::Type::on)
  4821. keys.push_back (arp.output[(std::size_t) i].key);
  4822. }
  4823. // 2 beats = 8 sixteenth steps: 60 64 67 60 64 67 60 64
  4824. CHECK (keys.size() >= 8); // the last block goes slightly past 2 beats
  4825. const int expected[] = { 60, 64, 67, 60, 64, 67, 60, 64 };
  4826. for (std::size_t i = 0; i < keys.size() && i < 8; ++i)
  4827. CHECK (keys[i] == expected[i]);
  4828. // actually rendering also makes sound
  4829. lynx::RackRenderer r;
  4830. r.prepare (48000.0, 512);
  4831. std::vector<float> l (512), rr (512);
  4832. float peak = 0.0f;
  4833. for (double tick = 0.0; tick < lynx::ppq; tick += 512 * tps)
  4834. {
  4835. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4836. for (auto v : l)
  4837. peak = std::max (peak, std::abs (v));
  4838. }
  4839. CHECK (peak > 0.01f);
  4840. }
  4841. TEST (audio_clip_fades_and_gain)
  4842. {
  4843. lynx::Song s;
  4844. s.bpm = 120.0;
  4845. s.mode = lynx::PlayMode::song;
  4846. s.mixer = lynx::Song::defaultMixer (1);
  4847. lynx::ChannelDef c;
  4848. c.instrument = lynx::InstrumentKind::audioClip;
  4849. c.samplePath = "dc";
  4850. c.volume = 1.0f;
  4851. c.mixerTrack = 1;
  4852. s.channels.push_back (c);
  4853. s.patterns.emplace_back();
  4854. lynx::Clip clip;
  4855. clip.kind = lynx::ClipKind::audio;
  4856. clip.channel = 0;
  4857. clip.length = lynx::ppq * 4; // 2 s at 120 BPM
  4858. clip.fadeIn = lynx::ppq; // 0.5 s
  4859. clip.fadeOut = lynx::ppq;
  4860. clip.gain = 0.5f;
  4861. s.playlist.push_back (clip);
  4862. auto provider = [] (const std::string&) {
  4863. auto d = std::make_shared<lynx::SampleData>();
  4864. d->left.assign (48000 * 4, 1.0f);
  4865. return std::shared_ptr<const lynx::SampleData> (d);
  4866. };
  4867. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  4868. lynx::RackRenderer r;
  4869. r.prepare (48000.0, 512);
  4870. std::vector<float> all;
  4871. std::vector<float> l (512), rr (512);
  4872. const auto tps = lynx::ticksPerSample (s.bpm, 48000.0);
  4873. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  4874. {
  4875. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4876. all.insert (all.end(), l.begin(), l.end());
  4877. }
  4878. CHECK (all[100] < 0.01f); // fade-in start
  4879. CHECK (std::abs (all[12000] - 0.25f) < 0.02f); // fade-in midpoint: half of 0.5
  4880. CHECK (std::abs (all[48000] - 0.5f) < 0.01f); // body: the clip gain
  4881. CHECK (std::abs (all[96000 - 12000] - 0.25f) < 0.02f); // fade-out midpoint
  4882. }
  4883. TEST (slicer_detects_hits_and_plays_slices_by_key)
  4884. {
  4885. // 4 hits (decaying noise) every 0.25 s
  4886. lynx::SampleData loop;
  4887. loop.sampleRate = 48000.0;
  4888. loop.left.assign (48000, 0.0f);
  4889. lynx::Noise noise;
  4890. for (int hit = 0; hit < 4; ++hit)
  4891. for (int i = 0; i < 6000; ++i)
  4892. loop.left[(std::size_t) (hit * 12000 + i)] = noise.next() * 0.8f * std::exp (-(float) i / 1500.0f);
  4893. const auto slices = lynx::detectSlices (loop, 0.5f);
  4894. CHECK (slices.size() == 4);
  4895. for (std::size_t i = 0; i < slices.size(); ++i)
  4896. CHECK (std::abs (slices[i] - 0.25f * (float) i) < 0.01f);
  4897. // key 61 plays the second slice, which starts at 0.25 and ends before 0.5
  4898. auto ramp = std::make_shared<lynx::SampleData>();
  4899. for (int i = 0; i < 48000; ++i)
  4900. ramp->left.push_back ((float) i / 48000.0f);
  4901. ramp->sampleRate = 48000.0;
  4902. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, ramp);
  4903. inst->prepare (48000.0);
  4904. lynx::ChannelDef c;
  4905. c.sampler.slices = slices;
  4906. inst->configure (c, 120.0);
  4907. inst->noteOn (61, 1.0f);
  4908. std::vector<float> l (20000), r (20000);
  4909. inst->render (l.data(), r.data(), 20000, 1.0f, 1.0f);
  4910. CHECK (std::abs (l[0] - 0.25f) < 0.01f && l[11000] > 0.45f && l[13000] == 0.0f);
  4911. }
  4912. TEST (project_templates_render_sound)
  4913. {
  4914. const auto names = lynx::templateNames();
  4915. CHECK (names.size() == 4);
  4916. for (int t = 1; t < (int) names.size(); ++t)
  4917. {
  4918. auto song = lynx::makeTemplate (t);
  4919. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  4920. lynx::RackRenderer r;
  4921. r.prepare (48000.0, 512);
  4922. std::vector<float> l (512), rr (512);
  4923. const auto tps = lynx::ticksPerSample (song.bpm, 48000.0);
  4924. float peak = 0.0f;
  4925. for (double tick = 0.0; tick < lynx::ppq * 4.0; tick += 512 * tps)
  4926. {
  4927. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  4928. for (auto v : l)
  4929. peak = std::max (peak, std::abs (v));
  4930. }
  4931. CHECK (peak > 0.05f && peak < 4.0f);
  4932. }
  4933. CHECK (lynx::makeTemplate (0).patterns[0].notes.empty());
  4934. }
  4935. // --- interface translations (resources/i18n) -------------------------------------------------------
  4936. namespace i18ntest
  4937. {
  4938. std::string readFile (const std::filesystem::path& path)
  4939. {
  4940. std::ifstream in (path, std::ios::binary);
  4941. return { std::istreambuf_iterator<char> (in), std::istreambuf_iterator<char>() };
  4942. }
  4943. std::string unescape (const std::string& s)
  4944. {
  4945. std::string out;
  4946. for (std::size_t i = 0; i < s.size(); ++i)
  4947. {
  4948. if (s[i] == '\\' && i + 1 < s.size())
  4949. {
  4950. const auto n = s[++i];
  4951. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  4952. }
  4953. else
  4954. out += s[i];
  4955. }
  4956. return out;
  4957. }
  4958. // reads a quoted string starting at s[pos] == '"'; returns the raw (escaped) contents and moves pos past the closing quote
  4959. bool quoted (const std::string& s, std::size_t& pos, std::string& raw)
  4960. {
  4961. if (pos >= s.size() || s[pos] != '"')
  4962. return false;
  4963. raw.clear();
  4964. for (++pos; pos < s.size() && s[pos] != '"' && s[pos] != '\n'; ++pos)
  4965. {
  4966. if (s[pos] == '\\' && pos + 1 < s.size())
  4967. raw += s[pos++];
  4968. raw += s[pos];
  4969. }
  4970. if (pos >= s.size() || s[pos] != '"')
  4971. return false;
  4972. ++pos;
  4973. return true;
  4974. }
  4975. // JUCE LocalisedStrings format: "english" = "translation"; duplicates are reported in `duplicates`
  4976. std::map<std::string, std::string> parseTable (const std::string& text, std::vector<std::string>& duplicates)
  4977. {
  4978. std::map<std::string, std::string> table;
  4979. std::istringstream lines (text);
  4980. std::string line;
  4981. while (std::getline (lines, line))
  4982. {
  4983. std::size_t pos = line.find_first_not_of (" \t");
  4984. std::string key, value;
  4985. if (pos == std::string::npos || ! quoted (line, pos, key))
  4986. continue;
  4987. pos = line.find ('"', pos);
  4988. if (pos == std::string::npos || ! quoted (line, pos, value))
  4989. value.clear();
  4990. const auto k = unescape (key);
  4991. if (table.count (k) != 0)
  4992. duplicates.push_back (k);
  4993. table[k] = unescape (value);
  4994. }
  4995. return table;
  4996. }
  4997. // string literals passed to tr() / TR_NOOP() in the sources (adjacent literals are concatenated)
  4998. std::map<std::string, std::string> sourceKeys (const std::filesystem::path& srcDir)
  4999. {
  5000. std::map<std::string, std::string> keys;
  5001. for (const auto& entry : std::filesystem::recursive_directory_iterator (srcDir))
  5002. {
  5003. const auto ext = entry.path().extension().string();
  5004. if (! entry.is_regular_file() || (ext != ".cpp" && ext != ".h"))
  5005. continue;
  5006. // drop // comment lines (they may quote tr ("...") as an example)
  5007. std::string text, line;
  5008. std::istringstream in (readFile (entry.path()));
  5009. while (std::getline (in, line))
  5010. {
  5011. const auto first = line.find_first_not_of (" \t");
  5012. text += (first != std::string::npos && line.compare (first, 2, "//") == 0) ? std::string() : line;
  5013. text += '\n';
  5014. }
  5015. auto isIdent = [] (char c) { return std::isalnum ((unsigned char) c) || c == '_'; };
  5016. for (const std::string name : { "tr", "TR_NOOP" })
  5017. for (auto at = text.find (name); at != std::string::npos; at = text.find (name, at + 1))
  5018. {
  5019. if ((at > 0 && isIdent (text[at - 1])) || isIdent (text[at + name.size()]))
  5020. continue;
  5021. auto pos = text.find_first_not_of (" \t\n", at + name.size());
  5022. if (pos == std::string::npos || text[pos] != '(')
  5023. continue;
  5024. pos = text.find_first_not_of (" \t\n", pos + 1);
  5025. std::string key, raw;
  5026. bool any = false;
  5027. while (pos != std::string::npos && quoted (text, pos, raw))
  5028. {
  5029. key += raw;
  5030. any = true;
  5031. pos = text.find_first_not_of (" \t\n", pos);
  5032. }
  5033. if (any)
  5034. keys.emplace (unescape (key), entry.path().filename().string());
  5035. }
  5036. }
  5037. return keys;
  5038. }
  5039. std::set<std::string> placeholders (const std::string& s)
  5040. {
  5041. std::set<std::string> found;
  5042. for (auto open = s.find ('{'); open != std::string::npos; open = s.find ('{', open + 1))
  5043. if (const auto close = s.find ('}', open); close != std::string::npos && close - open <= 3)
  5044. found.insert (s.substr (open, close - open + 1));
  5045. return found;
  5046. }
  5047. } // namespace i18ntest
  5048. TEST (translations_cover_every_ui_string)
  5049. {
  5050. using namespace i18ntest;
  5051. const auto text = readFile ("resources/i18n/pt-BR.txt");
  5052. CHECK (! text.empty());
  5053. std::vector<std::string> duplicates;
  5054. const auto table = parseTable (text, duplicates);
  5055. CHECK (table.size() > 300);
  5056. CHECK (duplicates.empty());
  5057. for (const auto& d : duplicates)
  5058. std::printf (" duplicate pt-BR entry: \"%s\"\n", d.c_str());
  5059. const auto keys = sourceKeys ("src");
  5060. CHECK (keys.size() > 300);
  5061. int missing = 0;
  5062. for (const auto& [key, file] : keys)
  5063. if (auto it = table.find (key); it == table.end() || it->second.empty())
  5064. {
  5065. ++missing;
  5066. std::printf (" no pt-BR translation for \"%s\" (%s)\n", key.c_str(), file.c_str());
  5067. }
  5068. CHECK (missing == 0);
  5069. // no empty values, and every {n} placeholder survives the translation
  5070. int empty = 0, badPlaceholders = 0;
  5071. for (const auto& [english, portuguese] : table)
  5072. {
  5073. empty += portuguese.empty() ? 1 : 0;
  5074. if (placeholders (english) != placeholders (portuguese))
  5075. {
  5076. ++badPlaceholders;
  5077. std::printf (" placeholders differ: \"%s\"\n", english.c_str());
  5078. }
  5079. }
  5080. CHECK (empty == 0);
  5081. CHECK (badPlaceholders == 0);
  5082. }
  5083. TEST (translations_embedded_table_is_up_to_date)
  5084. {
  5085. // src/ui/I18nData.h must be regenerated (tools/make_i18n.py) whenever the .txt changes
  5086. const auto text = i18ntest::readFile ("resources/i18n/pt-BR.txt");
  5087. CHECK (text.size() == sizeof (lynx::i18n::data::ptBR));
  5088. CHECK (std::memcmp (text.data(), lynx::i18n::data::ptBR, std::min (text.size(), sizeof (lynx::i18n::data::ptBR))) == 0);
  5089. }
  5090. // --- workflow shell: pattern/channel operations, key map, workspace layout, control math -------------
  5091. TEST (workflow_pattern_navigation_and_insert)
  5092. {
  5093. auto song = lynx::Song::makeDefault();
  5094. song.patterns.resize (3);
  5095. song.patterns[1].name = "B";
  5096. song.patterns[2].name = "C";
  5097. song.patterns[2].notes.push_back ({});
  5098. song.currentPattern = 0;
  5099. CHECK (lynx::stepPattern (song, 1) == 1 && lynx::stepPattern (song, -1) == 0 && lynx::stepPattern (song, 10) == 2);
  5100. // pattern 0 has the default notes, 1 is empty, 2 has a note
  5101. CHECK (! lynx::isPatternEmpty (song.patterns[0]) && lynx::isPatternEmpty (song.patterns[1]));
  5102. CHECK (lynx::findNextEmptyPattern (song, 0) == 1 && lynx::findNextEmptyPattern (song, 1) == -1);
  5103. CHECK (lynx::gotoNextEmptyPattern (song) == 1 && song.currentPattern == 1);
  5104. song.patterns[1].notes.push_back ({});
  5105. const auto before = song.patterns.size();
  5106. CHECK (lynx::gotoNextEmptyPattern (song) == (int) before && song.patterns.size() == before + 1);
  5107. CHECK (song.patterns.back().name == "Pattern 1" || song.patterns.back().name.rfind ("Pattern ", 0) == 0);
  5108. // insert keeps Playlist clips on their patterns
  5109. lynx::Clip clip;
  5110. clip.pattern = 2;
  5111. song.playlist = { clip };
  5112. song.arrangements.push_back ({ "Other", { clip }, 0, 0, {} });
  5113. CHECK (lynx::insertPattern (song, 1, "New") == 1 && song.currentPattern == 1);
  5114. CHECK (song.patterns[1].name == "New" && song.patterns[1].notes.empty() && song.patterns[3].name == "C");
  5115. CHECK (song.playlist[0].pattern == 3 && song.arrangements[1].clips[0].pattern == 3);
  5116. }
  5117. TEST (workflow_split_pattern_by_channel)
  5118. {
  5119. auto song = lynx::Song::makeDefault();
  5120. song.patterns.resize (1);
  5121. auto& p = song.patterns[0];
  5122. p.name = "Beat";
  5123. p.notes.clear();
  5124. for (int i = 0; i < 4; ++i)
  5125. p.notes.push_back ({ (std::int64_t) i * lynx::ticksPerStep, lynx::ticksPerStep, i % 2 == 0 ? 0 : 2, 60 });
  5126. song.channels[2].colour = 0xff112233;
  5127. lynx::Clip clip;
  5128. clip.pattern = 0;
  5129. song.playlist = { clip };
  5130. const auto created = lynx::splitPatternByChannel (song, 0);
  5131. CHECK (created == 2 && song.patterns.size() == 3);
  5132. CHECK (song.patterns[0].notes.size() == 4); // the original is unchanged
  5133. CHECK (song.patterns[1].name == "Beat - " + song.channels[0].name && song.patterns[1].notes.size() == 2);
  5134. CHECK (song.patterns[2].colour == 0xff112233u && song.patterns[2].notes.size() == 2 && song.patterns[2].notes[0].channel == 2);
  5135. CHECK (song.playlist[0].pattern == 0);
  5136. CHECK (lynx::splitPatternByChannel (song, 99) == 0);
  5137. }
  5138. TEST (workflow_channel_selection)
  5139. {
  5140. lynx::ChannelSelection s;
  5141. s.selectOnly (2);
  5142. s.toggle (5);
  5143. s.toggle (0);
  5144. CHECK ((s.rows == std::vector<int> { 0, 2, 5 }) && s.contains (5) && ! s.contains (3));
  5145. s.toggle (2);
  5146. CHECK ((s.rows == std::vector<int> { 0, 5 }));
  5147. s.selectOnly (3);
  5148. s.extendTo (6);
  5149. CHECK ((s.rows == std::vector<int> { 3, 4, 5, 6 }));
  5150. s.extendTo (1);
  5151. CHECK ((s.rows == std::vector<int> { 1, 2, 3 }));
  5152. s.selectOnly (4);
  5153. s.toggle (4); // the last one stays
  5154. CHECK ((s.rows == std::vector<int> { 4 }));
  5155. s.selectAll (3);
  5156. CHECK ((s.rows == std::vector<int> { 0, 1, 2 }));
  5157. s.rows = { 7, 9 };
  5158. s.clamp (5);
  5159. CHECK (s.rows.size() == 1 && s.rows[0] < 5);
  5160. }
  5161. TEST (workflow_move_clone_insert_channels)
  5162. {
  5163. auto song = lynx::Song::makeDefault();
  5164. const auto names = [&] {
  5165. std::vector<std::string> n;
  5166. for (const auto& c : song.channels)
  5167. n.push_back (c.name);
  5168. return n;
  5169. };
  5170. const auto original = names();
  5171. const auto count = (int) song.channels.size();
  5172. CHECK (count >= 4);
  5173. // references that must follow their channel
  5174. song.patterns[0].notes.push_back ({ 0, 240, 1, 64 });
  5175. lynx::Automation a;
  5176. a.target = { lynx::AutomationTargetKind::channelVolume, 1 };
  5177. song.automations.push_back (a);
  5178. song.midiLinks.push_back ({ 7, -1, { lynx::AutomationTargetKind::channelPan, 3 } });
  5179. lynx::Clip audio;
  5180. audio.kind = lynx::ClipKind::audio;
  5181. audio.channel = 3;
  5182. song.playlist.push_back (audio);
  5183. std::vector<int> selection { 1, 3 };
  5184. CHECK (lynx::moveChannels (song, selection, -1));
  5185. CHECK ((selection == std::vector<int> { 0, 2 }));
  5186. CHECK (song.channels[0].name == original[1] && song.channels[1].name == original[0] && song.channels[2].name == original[3]
  5187. && song.channels[3].name == original[2]);
  5188. CHECK (song.automations[0].target.index == 0 && song.midiLinks[0].target.index == 2 && song.playlist.back().channel == 2);
  5189. CHECK (std::any_of (song.patterns[0].notes.begin(), song.patterns[0].notes.end(),
  5190. [] (const lynx::Note& n) { return n.channel == 0 && n.key == 64; }));
  5191. CHECK (! lynx::moveChannels (song, selection, -1)); // the block is at the top
  5192. CHECK (lynx::moveChannels (song, selection, 1) && (selection == std::vector<int> { 1, 3 }) && names() == original);
  5193. // clone: settings, not notes, right below, with a new name
  5194. song.channels[1].volume = 0.33f;
  5195. const auto notesBefore = song.patterns[0].notes.size();
  5196. const auto clone = lynx::cloneChannel (song, 1);
  5197. CHECK (clone == 2 && (int) song.channels.size() == count + 1);
  5198. CHECK (song.channels[2].name == original[1] + " #2" && song.channels[2].volume == 0.33f);
  5199. CHECK (song.patterns[0].notes.size() == notesBefore);
  5200. CHECK (song.channels[3].name == original[2] && song.automations[0].target.index == 1);
  5201. CHECK (lynx::cloneChannel (song, 1) == 2 && song.channels[2].name == original[1] + " #3");
  5202. // remove several
  5203. CHECK (lynx::removeChannels (song, { 2, 3, 2 }) == 2 && (int) song.channels.size() == count);
  5204. CHECK (names() == original);
  5205. }
  5206. TEST (workflow_send_channels_to_free_inserts)
  5207. {
  5208. auto song = lynx::Song::makeDefault();
  5209. for (auto& c : song.channels)
  5210. c.mixerTrack = 0;
  5211. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay)); // not free: has effects
  5212. song.channels[0].mixerTrack = 2; // not free: used
  5213. song.setRoute (5, 3, true); // 3 is not free: receives a send
  5214. CHECK (! lynx::isInsertFree (song, 1) && ! lynx::isInsertFree (song, 2) && ! lynx::isInsertFree (song, 3) && lynx::isInsertFree (song, 4));
  5215. song.channels[1].colour = 0xff445566;
  5216. const auto tracks = lynx::sendChannelsToFreeInserts (song, { 1, 2 });
  5217. CHECK ((tracks == std::vector<int> { 4, 5 }) || (tracks == std::vector<int> { 4, 6 }));
  5218. CHECK (song.channels[1].mixerTrack == 4 && song.mixer[4].name == song.channels[1].name && song.mixer[4].colour == 0xff445566u);
  5219. CHECK (song.mixer[(std::size_t) tracks[1]].name == song.channels[2].name);
  5220. // with no free insert left, a new one is added at the end
  5221. auto full = lynx::Song::makeDefault();
  5222. full.mixer = lynx::Song::defaultMixer (1);
  5223. full.channels[0].mixerTrack = 1;
  5224. const auto added = lynx::sendChannelsToFreeInserts (full, { 1 });
  5225. CHECK (added.size() == 1 && added[0] == 2 && full.mixer.size() == 3 && full.channels[1].mixerTrack == 2);
  5226. }
  5227. TEST (workflow_palette_random_colour)
  5228. {
  5229. const auto& palette = lynx::lynxPalette();
  5230. for (std::uint32_t seed = 0; seed < 50; ++seed)
  5231. {
  5232. const auto current = palette[seed % palette.size()];
  5233. const auto c = lynx::randomPaletteColour (current, seed);
  5234. CHECK (c != current && std::find (palette.begin(), palette.end(), c) != palette.end());
  5235. }
  5236. }
  5237. TEST (workflow_key_map)
  5238. {
  5239. using namespace lynx::keys;
  5240. KeyMap map;
  5241. const auto global = Context::global, rack = Context::channelRack;
  5242. CHECK (map.find ({ space, none }, global, true) == "transport.playStop");
  5243. CHECK (map.find ({ space, ctrl }, global, true) == "transport.pause");
  5244. CHECK (map.find ({ f6, none }, global, true) == "panel.channelRack");
  5245. CHECK (map.find ({ f8, alt }, global, false) == "panel.browser");
  5246. CHECK (map.find ({ insert, ctrl | shift }, global, false) == "pattern.insert");
  5247. CHECK (map.find ({ numpadPlus, none }, rack, true) == "pattern.next");
  5248. // panel context: only while focused
  5249. CHECK (map.find ({ 'L', ctrl }, rack, true) == "rack.sendToInserts");
  5250. CHECK (map.find ({ 'L', ctrl }, global, true).empty());
  5251. CHECK (map.find ({ up, alt }, rack, false) == "rack.moveUp");
  5252. // typing piano: note keys play, other plain keys only for whilePiano actions; chords always work
  5253. CHECK (KeyMap::playsNote ({ 'Z', none }, true) && ! KeyMap::playsNote ({ 'Z', none }, false) && ! KeyMap::playsNote ({ 'Z', ctrl }, true));
  5254. CHECK (map.find ({ 'L', none }, global, true) == "transport.toggleMode");
  5255. CHECK (map.find ({ '1', none }, rack, false) == "rack.mute1" && map.find ({ '0', none }, rack, false) == "rack.mute10");
  5256. CHECK (map.find ({ '1', none }, rack, true).empty()); // a plain digit is not a shortcut while the piano is on
  5257. CHECK (map.find ({ '2', ctrl }, rack, true) == "rack.solo2");
  5258. CHECK (typingPianoNote ('Z') == 60 && typingPianoNote ('q') == 72 && typingPianoNote ('P') == 88 && typingPianoNote ('L') == -1);
  5259. CHECK (map.conflicts().empty());
  5260. // chord text
  5261. CHECK (toString ({ f4, ctrl | shift }) == "Ctrl+Shift+F4" && toString ({ numpadMinus, none }) == "Numpad -");
  5262. CHECK (parseChord ("Shift+Ctrl+Ins") == (Chord { insert, ctrl | shift }));
  5263. CHECK (parseChord ("alt+up") == (Chord { up, alt }) && parseChord ("k") == (Chord { 'K', none }));
  5264. CHECK (parseChord ("Numpad +") == (Chord { numpadPlus, none }) && ! parseChord ("F13") && ! parseChord ("Ctrl+"));
  5265. for (const auto& a : actions())
  5266. for (const auto& c : a.defaults)
  5267. CHECK (parseChord (toString (c)) == c);
  5268. // JSON overrides
  5269. std::vector<std::string> errors;
  5270. CHECK (map.loadJson (R"({ "pattern.next": "Ctrl+Right", "rack.clone": ["Alt+D", "Ctrl+D"], "help.shortcuts": "", "nope": "F1", "file.open": "Hyper+Q" })", &errors));
  5271. CHECK (errors.size() == 2);
  5272. CHECK (map.find ({ right, ctrl }, global, false) == "pattern.next" && map.find ({ numpadPlus, none }, global, false).empty());
  5273. CHECK (map.find ({ 'D', ctrl }, rack, false) == "rack.clone" && map.find ({ f1, none }, global, false).empty());
  5274. map.setChords ("pattern.previous", { { right, ctrl } });
  5275. CHECK (map.conflicts().size() == 1);
  5276. KeyMap reloaded;
  5277. CHECK (reloaded.loadJson (map.toJson()) && reloaded.chordsFor ("rack.clone") == map.chordsFor ("rack.clone"));
  5278. CHECK (! map.loadJson ("[1, 2]") && ! map.loadJson ("{\"a\": }"));
  5279. map.resetToDefaults();
  5280. CHECK (map.find ({ numpadPlus, none }, global, false) == "pattern.next");
  5281. }
  5282. TEST (workflow_workspace_layout)
  5283. {
  5284. using namespace lynx::workspace;
  5285. auto l = Layout::makeDefault (1280, 620);
  5286. // every visible panel is inside the desktop; the default shows Browser, Playlist and Channel Rack
  5287. for (const auto& p : l.panels)
  5288. CHECK (p.bounds.x >= 0 && p.bounds.y >= 0 && p.bounds.right() <= 1280 && p.bounds.bottom() <= 620 && p.bounds.w >= minWidth);
  5289. CHECK (l[Panel::browser].visible && l[Panel::playlist].visible && l[Panel::channelRack].visible);
  5290. CHECK (! l[Panel::pianoRoll].visible && ! l[Panel::mixer].visible);
  5291. CHECK (l[Panel::playlist].bounds.x == l[Panel::browser].bounds.right() && l[Panel::playlist].bounds.right() == 1280);
  5292. CHECK (l.isFrontmost (Panel::channelRack) && l.focused == Panel::channelRack);
  5293. // toggle: show+focus, focus, hide
  5294. CHECK (l.toggle (Panel::mixer) && l.focused == Panel::mixer && l.isFrontmost (Panel::mixer));
  5295. CHECK (l.toggle (Panel::channelRack) && l.focused == Panel::channelRack && l.isFrontmost (Panel::channelRack));
  5296. CHECK (! l.toggle (Panel::channelRack) && ! l[Panel::channelRack].visible && l.focused == Panel::mixer);
  5297. l.toggleMaximised (Panel::playlist);
  5298. CHECK (l[Panel::playlist].maximised && l.focused == Panel::playlist);
  5299. // save and load at another size: positions scale
  5300. l[Panel::mixer].detached = true;
  5301. l[Panel::mixer].windowBounds = { 100, 50, 900, 500 };
  5302. const auto text = l.serialise();
  5303. const auto big = Layout::parse (text, 2560, 1240);
  5304. CHECK (big.has_value());
  5305. if (big)
  5306. {
  5307. CHECK ((*big)[Panel::playlist].maximised && (*big)[Panel::mixer].detached && (*big)[Panel::mixer].windowBounds.w == 900);
  5308. CHECK (std::abs ((*big)[Panel::playlist].bounds.x - 2 * l[Panel::playlist].bounds.x) <= 1);
  5309. CHECK (big->order == l.order && big->focused == l.focused);
  5310. }
  5311. 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));
  5312. // resize keeps panels inside and edges glued to the border
  5313. l.resize (1000, 500);
  5314. CHECK (l[Panel::playlist].bounds.right() == 1000 && l[Panel::browser].bounds.bottom() == 500);
  5315. for (const auto& p : l.panels)
  5316. CHECK (p.bounds.right() <= 1000 && p.bounds.bottom() <= 500);
  5317. // off-screen panels come back
  5318. l[Panel::pianoRoll].bounds = { 5000, -300, 400, 300 };
  5319. CHECK (l.bringBackOffscreen() == 1 && l[Panel::pianoRoll].bounds.x + 400 <= 1000 && l[Panel::pianoRoll].bounds.y == 0);
  5320. // snapping: to the desktop, to another panel, and resizing one edge
  5321. CHECK ((snap ({ 5, 6, 200, 100 }, {}, 1000, 500) == Rect { 0, 0, 200, 100 }));
  5322. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500) == Rect { 300, 50, 200, 100 }));
  5323. CHECK ((snap ({ 304, 50, 200, 100 }, { { 0, 0, 300, 400 } }, 1000, 500, 2) == Rect { 304, 50, 200, 100 })); // out of range
  5324. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 0, 300, 400 } }, 1000, 500, 8, right) == Rect { 100, 100, 200, 100 }));
  5325. CHECK ((snap ({ 100, 100, 195, 100 }, { { 300, 1000, 300, 400 } }, 2000, 2000, 8, right) == Rect { 100, 100, 195, 100 })); // not facing
  5326. }
  5327. TEST (workflow_control_math)
  5328. {
  5329. using namespace lynx::controls;
  5330. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, false) - 1.0) < 1e-9);
  5331. CHECK (std::abs (dragValue (0.5, 75, 0, 1, 150, true) - 0.55) < 1e-9);
  5332. CHECK (dragValue (0.5, -1000, -1, 1, 150, false) == -1.0);
  5333. 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);
  5334. CHECK (wheelValue (3, 1, 0, 10, true, 1) == 4.0); // integer controls move at least one step
  5335. CHECK (std::abs (snapToInterval (0.234, 0, 0.1) - 0.2) < 1e-9);
  5336. CHECK (tempoDrag (130.0, 8, false, false) == 132.0 && tempoDrag (130.0, -3, false, false) == 130.0);
  5337. CHECK (std::abs (tempoDrag (130.0, 25, true, false) - 130.25) < 1e-9);
  5338. CHECK (std::abs (tempoDrag (130.0, 8, false, true) - 130.2) < 1e-9);
  5339. CHECK (tempoDrag (520.0, 400, false, false) == lynx::maxTempo && halfTempo (15.0) == lynx::minTempo && doubleTempo (130.0) == 260.0);
  5340. TapTempo tap;
  5341. CHECK (! tap.tap (1000.0));
  5342. CHECK (tap.tap (1500.0) == 120.0);
  5343. CHECK (tap.tap (2000.0) == 120.0 && tap.count() == 3);
  5344. CHECK (std::abs (*tap.tap (2400.0) - 128.57) < 0.01); // average of 500, 500, 400 ms
  5345. CHECK (! tap.tap (9000.0) && tap.count() == 1); // a long pause starts over
  5346. }
  5347. int main (int argc, char** argv)
  5348. {
  5349. if (argc > 1)
  5350. realFlpDir = argv[1];
  5351. const char* only = std::getenv ("LYNX_TEST"); // run only the tests whose name contains this
  5352. for (auto& [name, fn] : registry())
  5353. {
  5354. if (only != nullptr && std::strstr (name, only) == nullptr)
  5355. continue;
  5356. std::printf ("%s\n", name);
  5357. fn();
  5358. }
  5359. std::printf ("\n%d checks, %d failures\n", checks, failures);
  5360. return failures == 0 ? 0 : 1;
  5361. }
  5362. TEST (synth_cutoff_keytrack)
  5363. {
  5364. // the cutoff follows the note from key 48 (130.81 Hz), like Harmor's "Key to filter frequency"
  5365. auto rms = [] (float keyTrack, int key) {
  5366. lynx::SynthSettings s;
  5367. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5368. s.cutoff = 130.81f;
  5369. s.keyTrack = keyTrack;
  5370. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5371. inst->prepare (48000.0);
  5372. lynx::ChannelDef c;
  5373. c.synth = s;
  5374. inst->configure (c, 120.0);
  5375. inst->noteOn (key, 1.0f);
  5376. std::vector<float> l (24000), r (24000);
  5377. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  5378. double e = 0.0;
  5379. for (int i = 4800; i < 24000; ++i)
  5380. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5381. return std::sqrt (e / 19200.0);
  5382. };
  5383. const auto fixed = rms (0.0f, 72), tracked = rms (1.0f, 72);
  5384. CHECK (tracked > 4.0 * fixed); // 2 octaves up: without keytrack the low-pass cuts
  5385. CHECK (std::abs (rms (1.0f, 48) - rms (0.0f, 48)) < 0.05 * rms (0.0f, 48)); // unchanged at the reference key
  5386. }
  5387. namespace
  5388. {
  5389. std::vector<float> renderSynthNote (const lynx::SynthSettings& s, int key, int frames)
  5390. {
  5391. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  5392. inst->prepare (48000.0);
  5393. lynx::ChannelDef c;
  5394. c.synth = s;
  5395. inst->configure (c, 120.0);
  5396. inst->noteOn (key, 1.0f);
  5397. std::vector<float> l ((std::size_t) frames), r ((std::size_t) frames);
  5398. inst->render (l.data(), r.data(), frames, 1.0f, 1.0f);
  5399. return l;
  5400. }
  5401. double goertzel (const std::vector<float>& x, double f, int from, int to)
  5402. {
  5403. const auto w = 2.0 * 3.141592653589793 * f / 48000.0, k = 2.0 * std::cos (w);
  5404. double s1 = 0.0, s2 = 0.0;
  5405. for (int i = from; i < to; ++i)
  5406. {
  5407. const auto s0 = x[(std::size_t) i] + k * s1 - s2;
  5408. s2 = s1;
  5409. s1 = s0;
  5410. }
  5411. return s1 * s1 + s2 * s2 - k * s1 * s2;
  5412. }
  5413. } // namespace
  5414. TEST (synth_pitch_sweep_ring_and_bands)
  5415. {
  5416. lynx::SynthSettings sine;
  5417. sine.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  5418. sine.cutoff = 20000.0f;
  5419. // sweep: starts at f·(1 + sweep) and returns to f (time constant s = 10 ms; s⁴ falls 4× faster)
  5420. auto sweep = sine;
  5421. sweep.sweep = 1.0f;
  5422. sweep.sweepTime = 0.01f;
  5423. const auto a = renderSynthNote (sweep, 69, 24000);
  5424. auto crossings = [] (const std::vector<float>& x, int to) {
  5425. int n = 0;
  5426. for (int i = 1; i < to; ++i)
  5427. n += (x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f);
  5428. return n;
  5429. };
  5430. CHECK (crossings (a, 480) > 1.3 * crossings (renderSynthNote (sine, 69, 480), 480)); // 10 ms: much higher
  5431. CHECK (goertzel (a, 440.0, 12000, 24000) > 100.0 * goertzel (a, 880.0, 12000, 24000));
  5432. // ring with |a| = 1: carrier 440 × modulator 110 -> only 330 and 550, no 440
  5433. auto ring = sine;
  5434. ring.am = ring.ring = true;
  5435. ring.osc[2] = { lynx::Waveform::sine, -2, 0.0f, 1.0f };
  5436. const auto b = renderSynthNote (ring, 69, 24000);
  5437. CHECK (goertzel (b, 330.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5438. CHECK (goertzel (b, 550.0, 4800, 24000) > 100.0 * goertzel (b, 440.0, 4800, 24000));
  5439. // 3 bands: muting the high knocks down a 4 kHz note and barely touches an 80 Hz one
  5440. auto bands = sine;
  5441. bands.bandGain = { 1.0f, 1.0f, 0.0f };
  5442. auto rms = [] (const std::vector<float>& x) {
  5443. double e = 0.0;
  5444. for (std::size_t i = 4800; i < x.size(); ++i)
  5445. e += (double) x[i] * x[i];
  5446. return std::sqrt (e / (double) (x.size() - 4800));
  5447. };
  5448. CHECK (rms (renderSynthNote (bands, 107, 24000)) < 0.3 * rms (renderSynthNote (sine, 107, 24000)));
  5449. CHECK (rms (renderSynthNote (bands, 28, 24000)) > 0.85 * rms (renderSynthNote (sine, 28, 24000)));
  5450. }
  5451. TEST (love_philter_map_evaluation)
  5452. {
  5453. lynx::LovePhilterMap line { { { 0.0, 0.0 }, { 1.0, 1.0 } } };
  5454. CHECK (std::abs (lynx::lovePhilterMapAt (line, 0.5) - 0.5) < 1e-9);
  5455. CHECK (std::abs (lynx::lovePhilterMapAt (line, 1.0) - 1.0) < 1e-9);
  5456. // steps (Δx = 0): 1 until 0.4375, ramp to 0 at 0.5, then 0
  5457. lynx::LovePhilterMap steps { { { 0.0, 1.0 }, { 0.4375, 1.0 }, { 0.0625, 0.0 }, { 0.5, 0.0 } } };
  5458. CHECK (lynx::lovePhilterMapAt (steps, 0.27) == 1.0);
  5459. CHECK (lynx::lovePhilterMapAt (steps, 0.75) == 0.0);
  5460. CHECK (std::abs (lynx::lovePhilterMapAt (steps, 0.46875) - 0.5) < 1e-9);
  5461. CHECK (lynx::lovePhilterMapAt ({}, 0.3) == 1.0);
  5462. }
  5463. TEST (lofi_holds_and_quantizes)
  5464. {
  5465. auto fx = lynx::makeEffect (lynx::EffectKind::lofi);
  5466. fx->prepare (48000.0);
  5467. auto slot = lynx::makeEffectSlot (lynx::EffectKind::lofi);
  5468. slot.params[0] = 0.5f; // 128·0.125 + 1 = 17 samples
  5469. slot.params[1] = 1.0f; // 65536/2049 ≈ 32 levels
  5470. std::vector<float> l (170), r (170);
  5471. for (std::size_t i = 0; i < l.size(); ++i)
  5472. l[i] = r[i] = 0.9f * (float) std::sin (0.05 * (double) i);
  5473. const auto original = l;
  5474. fx->process (l.data(), r.data(), 170, slot.params, 120.0);
  5475. for (int i = 0; i < 17; ++i)
  5476. CHECK (l[(std::size_t) i] == l[0]);
  5477. CHECK (l[17] != l[16]);
  5478. const auto step = 2.0f * 2049.0f / 65536.0f;
  5479. for (auto v : l)
  5480. CHECK (std::abs (v / step - std::round (v / step)) < 1e-3f);
  5481. CHECK (std::abs (l[0] - original[0]) <= step);
  5482. CHECK (lynx::formatEffectParam (lynx::EffectKind::lofi, 1, 0.0f, 120.0) == "16.0 bits");
  5483. }
  5484. namespace
  5485. {
  5486. lynx::EffectParams effectorParams (int type, float x, float y)
  5487. {
  5488. auto slot = lynx::makeEffectSlot (lynx::EffectKind::effector);
  5489. slot.params[0] = (float) type / 11.0f;
  5490. slot.params[3] = x;
  5491. slot.params[4] = y;
  5492. return slot.params;
  5493. }
  5494. } // namespace
  5495. TEST (effector_algorithms)
  5496. {
  5497. CHECK (lynx::effector::typeOf (8.0f / 11.0f) == 8);
  5498. CHECK (std::string (lynx::effector::typeName (11)) == "Ring");
  5499. CHECK (std::abs (lynx::effector::rateBeats (0.5f, 0.0f) - 0.75) < 1e-9); // trunc(8·0.5 + 0.5) = 4 -> 3/4
  5500. CHECK (std::abs (lynx::effector::rateBeats (1.0f, 0.5f) - 2.0) < 1e-9); // 8 × (1 − 0.5)²
  5501. auto run = [] (const lynx::EffectParams& p, std::vector<float> in, double bpm = 120.0) {
  5502. auto fx = lynx::makeEffect (lynx::EffectKind::effector);
  5503. fx->prepare (48000.0);
  5504. fx->setSongPosition (0.0);
  5505. auto r = in;
  5506. fx->process (in.data(), r.data(), (int) in.size(), p, bpm);
  5507. return in;
  5508. };
  5509. // Distort with Y = 1 (everything goes to the cascade): 0.5 comes out 0.5 (compensation G = 0.5/F(0.5))
  5510. {
  5511. auto p = effectorParams (0, 0.5f, 1.0f);
  5512. const auto out = run (p, std::vector<float> (4800, 0.5f));
  5513. CHECK (std::abs (out.back() - 0.5f) < 1e-3f);
  5514. const auto small = run (p, std::vector<float> (4800, 0.05f));
  5515. CHECK (small.back() > 0.45f && small.back() < 0.5f); // saturates near ±0.5
  5516. }
  5517. // Ring: DC 1 becomes the carrier sine, 2^(8.7·0 + 5) = 32 Hz -> 32 cycles per second
  5518. {
  5519. auto p = effectorParams (11, 0.0f, 0.0f);
  5520. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5521. int crossings = 0;
  5522. for (std::size_t i = 4800; i < out.size(); ++i)
  5523. crossings += (out[i - 1] < 0.0f) != (out[i] < 0.0f);
  5524. CHECK (crossings >= 56 && crossings <= 60); // 0.9 s × 32 Hz × 2
  5525. }
  5526. // Trancer, Y = 0: at the start of the period the gain drops to zero; X = 1 returns only at the end of the period
  5527. {
  5528. auto p = effectorParams (8, 1.0f, 0.0f);
  5529. p[5] = 1.0f; // 8/1 = 8 beats = 4 s at 120 BPM
  5530. const auto out = run (p, std::vector<float> (48000, 1.0f));
  5531. CHECK (out[100] > 0.3f); // still rising on the "and" (1% of the period = 40 ms)
  5532. CHECK (out[24000] < 0.05f); // 0.5 s: trough
  5533. }
  5534. // Reverb with Wet = 0: the dry passes intact
  5535. {
  5536. auto p = effectorParams (6, 0.5f, 0.5f);
  5537. p[2] = 0.0f;
  5538. std::vector<float> in (4800);
  5539. for (std::size_t i = 0; i < in.size(); ++i)
  5540. in[i] = (float) std::sin (0.01 * (double) i);
  5541. const auto out = run (p, in);
  5542. for (std::size_t i = 0; i < in.size(); ++i)
  5543. CHECK (std::abs (out[i] - in[i]) < 1e-6f);
  5544. }
  5545. // Delay: the echo arrives after the Mod Rate period (1/4 beat at 120 BPM = 125 ms), at level X²
  5546. {
  5547. auto p = effectorParams (5, 0.7f, 0.5f);
  5548. p[5] = 1.0f / 8.0f; // index 1 = 1/4
  5549. std::vector<float> in (12000, 0.0f);
  5550. in[0] = 1.0f;
  5551. const auto out = run (p, in);
  5552. CHECK (std::abs (out[6000] - 0.49f) < 0.02f);
  5553. CHECK (std::abs (out[3000]) < 1e-6f);
  5554. }
  5555. // Stutter: repeats the recorded start every period
  5556. {
  5557. auto p = effectorParams (9, 0.0f, 0.0f);
  5558. p[5] = 0.0f; // 1/8 beat = 62.5 ms = 3000 samples
  5559. std::vector<float> in (9000);
  5560. for (std::size_t i = 0; i < in.size(); ++i)
  5561. in[i] = (float) i / 9000.0f;
  5562. const auto out = run (p, in);
  5563. CHECK (std::abs (out[3000 + 500] - in[500]) < 0.01f); // after the crossfade, it's the start again
  5564. CHECK (std::abs (out[1000] - in[1000]) < 1e-6f);
  5565. }
  5566. }
  5567. TEST (crossover_linkwitz_riley_sums_flat)
  5568. {
  5569. // low band + high band of the same crossover: sum with magnitude 1 (Linkwitz-Riley; for 12 and 36 dB the HP is inverted)
  5570. for (const auto combo : { 1, 3, 4, 5, 6, 7 })
  5571. for (const auto testHz : { 200.0, 1000.0, 5000.0 })
  5572. {
  5573. auto run = [&] (bool high) {
  5574. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5575. fx->prepare (48000.0);
  5576. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5577. const auto value = (float) (std::log2 (1000.0 / 30.0) / 9.0589);
  5578. slot.params[high ? 0 : 3] = 1.0f;
  5579. slot.params[high ? 1 : 4] = value;
  5580. slot.params[high ? 2 : 5] = (float) combo / 7.0f;
  5581. std::vector<float> l (24000), r (24000);
  5582. for (std::size_t i = 0; i < l.size(); ++i)
  5583. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * testHz * (double) i / 48000.0);
  5584. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5585. return l;
  5586. };
  5587. const auto low = run (false), high = run (true);
  5588. double peak = 0.0;
  5589. for (std::size_t i = 12000; i < low.size(); ++i)
  5590. peak = std::max (peak, (double) std::abs (low[i] + high[i]));
  5591. CHECK (std::abs (peak - 1.0) < 0.02);
  5592. }
  5593. // 96 dB/oct: two octaves above the cutoff the low-pass is already below −150 dB... in practice, very low
  5594. auto fx = lynx::makeEffect (lynx::EffectKind::crossover);
  5595. fx->prepare (48000.0);
  5596. auto slot = lynx::makeEffectSlot (lynx::EffectKind::crossover);
  5597. slot.params[3] = 1.0f;
  5598. slot.params[4] = (float) (std::log2 (500.0 / 30.0) / 9.0589);
  5599. slot.params[5] = 1.0f;
  5600. std::vector<float> l (24000), r (24000);
  5601. for (std::size_t i = 0; i < l.size(); ++i)
  5602. l[i] = r[i] = (float) std::sin (2.0 * 3.141592653589793 * 2000.0 * (double) i / 48000.0);
  5603. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5604. float peak = 0.0f;
  5605. for (std::size_t i = 12000; i < l.size(); ++i)
  5606. peak = std::max (peak, std::abs (l[i]));
  5607. CHECK (peak < 1e-4f);
  5608. }
  5609. TEST (rhythm_gain_and_autopan)
  5610. {
  5611. // 1-beat gate: 1 → 0 at 0.5 beat (linear) → 1 at the end; at 120 BPM 1 beat = 24000 samples at 48 kHz
  5612. auto slot = lynx::makeEffectSlot (lynx::EffectKind::rhythmGain);
  5613. slot.params[0] = 1.0f / 64.0f;
  5614. slot.params[1] = 3.0f / 12.0f;
  5615. const float pts[3][3] = { { 0.0f, 1.0f, 0.5f }, { 0.5f, 0.0f, 0.5f }, { 1.0f, 1.0f, 0.5f } };
  5616. for (int k = 0; k < 3; ++k)
  5617. for (int j = 0; j < 3; ++j)
  5618. slot.params[(std::size_t) (2 + 3 * k + j)] = pts[k][j];
  5619. auto fx = lynx::makeEffect (lynx::EffectKind::rhythmGain);
  5620. fx->prepare (48000.0);
  5621. fx->setSongPosition (960.0 * 2.0); // beat 2: phase 0
  5622. std::vector<float> l (24000, 1.0f), r (24000, 1.0f);
  5623. fx->process (l.data(), r.data(), 24000, slot.params, 120.0);
  5624. CHECK (std::abs (l[0] - 1.0f) < 1e-3f);
  5625. CHECK (std::abs (l[6000] - 0.5f) < 1e-3f);
  5626. CHECK (l[12000] < 1e-3f);
  5627. CHECK (std::abs (l[18000] - 0.5f) < 1e-3f);
  5628. // positive tension: changes quickly at the start of the segment
  5629. CHECK (lynx::effectInfo (lynx::EffectKind::rhythmGain).params.size() == 39);
  5630. // auto-pan: 1-beat period, full depth; start phase 0.75 -> W = 0 rising (pan centered)
  5631. auto pan = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  5632. pan.params[0] = 1.0f / 64.0f;
  5633. pan.params[1] = 1.0f;
  5634. auto ap = lynx::makeEffect (lynx::EffectKind::autoPan);
  5635. ap->prepare (48000.0);
  5636. ap->setSongPosition (0.0);
  5637. std::vector<float> a (24000, 1.0f), b (24000, 1.0f);
  5638. ap->process (a.data(), b.data(), 24000, pan.params, 120.0);
  5639. CHECK (std::abs (a[0] - 1.0f) < 1e-3f && std::abs (b[0] - 1.0f) < 1e-3f);
  5640. CHECK (a[6000] < 0.01f && std::abs (b[6000] - 1.0f) < 1e-3f); // ¼ cycle later: fully right
  5641. }
  5642. TEST (vocoder_side_inputs_split_modulator_and_carrier)
  5643. {
  5644. // "Side inputs" mode (Vocodex): modulator from side input 1 (noise in the first half), carrier from side input 2
  5645. // (saw), own input silent → the output follows the side modulator; with input 1 silent, the output is silent
  5646. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5647. slot.params[16] = 1.0f;
  5648. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5649. fx->prepare (48000.0);
  5650. std::vector<float> modL (48000), car (48000), l (48000, 0.0f), r (48000, 0.0f);
  5651. std::uint32_t seed = 1;
  5652. for (std::size_t i = 0; i < modL.size(); ++i)
  5653. {
  5654. seed = seed * 1664525u + 1013904223u;
  5655. modL[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5656. car[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5657. }
  5658. for (int done = 0; done < 48000; done += 480)
  5659. {
  5660. fx->setSidechain (modL.data() + done, modL.data() + done);
  5661. fx->setSidechain2 (car.data() + done, car.data() + done);
  5662. fx->process (l.data() + done, r.data() + done, 480, slot.params, 120.0);
  5663. }
  5664. auto rms = [&] (int from, int to) {
  5665. double e = 0.0;
  5666. for (int i = from; i < to; ++i)
  5667. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5668. return std::sqrt (e / (to - from));
  5669. };
  5670. CHECK (rms (6000, 20000) > 0.01);
  5671. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000));
  5672. // the side modulator configured but silent: nothing comes out (the own input is not used instead)
  5673. std::vector<float> a (4800, 0.3f), b (4800, 0.3f);
  5674. for (int done = 0; done < 4800; done += 480)
  5675. {
  5676. fx->setSidechain (nullptr, nullptr);
  5677. fx->setSidechain2 (car.data() + done, car.data() + done);
  5678. fx->process (a.data() + done, b.data() + done, 480, slot.params, 120.0);
  5679. }
  5680. CHECK (std::abs (a[4700]) < 0.05f);
  5681. }
  5682. TEST (vocoder_follows_modulator)
  5683. {
  5684. // modulator (L) plays only in the first half; carrier (R) is a continuous saw: the wet follows the modulator
  5685. auto slot = lynx::makeEffectSlot (lynx::EffectKind::vocoder);
  5686. auto fx = lynx::makeEffect (lynx::EffectKind::vocoder);
  5687. fx->prepare (48000.0);
  5688. std::vector<float> l (48000), r (48000);
  5689. std::uint32_t seed = 1;
  5690. for (std::size_t i = 0; i < l.size(); ++i)
  5691. {
  5692. seed = seed * 1664525u + 1013904223u;
  5693. l[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5694. r[i] = (float) (2.0 * std::fmod ((double) i * 220.0 / 48000.0, 1.0) - 1.0) * 0.5f;
  5695. }
  5696. fx->process (l.data(), r.data(), 48000, slot.params, 120.0);
  5697. auto rms = [&] (int from, int to) {
  5698. double e = 0.0;
  5699. for (int i = from; i < to; ++i)
  5700. e += (double) l[(std::size_t) i] * l[(std::size_t) i];
  5701. return std::sqrt (e / (to - from));
  5702. };
  5703. CHECK (rms (6000, 20000) > 0.01);
  5704. CHECK (rms (40000, 48000) < 0.1 * rms (6000, 20000)); // modulator silent: the vocoder fades out (100 ms release)
  5705. for (std::size_t i = 0; i < l.size(); ++i)
  5706. CHECK (l[i] == r[i] && std::isfinite (l[i])); // mono output
  5707. }
  5708. // Vocodex (REA_rodada35): a synthetic state in FL's layout (version 8, 13 maps, 20 parameters, the 0x24 block,
  5709. // 13 map-axis bytes, tone, notes)
  5710. struct VocodexSpec
  5711. {
  5712. // Wet, SG, ModVol, ModHP, CarVol, CarLP, CarNoise, CarNoiseHP, Hold, Att, AttCurve, Rel, RelCurve, Freeze, BW,
  5713. // Flatness, ModBW, ModPitch, FatPitch, FatPan
  5714. 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 };
  5715. std::array<std::int32_t, 7> block { 0, 2, 0, 24, 1, 1, 2 }; // hold mode, order, unison, bands, draft field, ModIn, CarIn
  5716. 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
  5717. std::vector<std::uint8_t> bytes() const
  5718. {
  5719. std::vector<std::uint8_t> d;
  5720. 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); };
  5721. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  5722. i32 (8 << 16);
  5723. for (int m = 0; m < 13; ++m)
  5724. {
  5725. const double y0 = m == 8 ? 0.1429 : (m == 3 || m == 4 || m == 11) ? 0.0 : 0.5;
  5726. const double y1 = m == 8 ? 0.9918 : y0;
  5727. i32 (1), i32 (0), i32 (2);
  5728. for (const auto& [dx, y] : { std::pair { 0.0, y0 }, std::pair { 1.0, y1 } })
  5729. {
  5730. put (&dx, 8), put (&y, 8);
  5731. i32 (0), i32 (0); // tension (f32 0), flags
  5732. }
  5733. const std::array<std::uint8_t, 20> tail {};
  5734. put (tail.data(), tail.size());
  5735. }
  5736. for (auto v : params)
  5737. i32 (v);
  5738. for (auto v : block)
  5739. i32 (v);
  5740. put (flags.data(), flags.size());
  5741. const std::array<std::uint8_t, 13> axis {};
  5742. put (axis.data(), axis.size());
  5743. d.push_back (3), put ("PWM", 3);
  5744. i32 (1), i32 (60);
  5745. return d;
  5746. }
  5747. };
  5748. std::vector<float> vocodexSaw (int n, double hz, double fs)
  5749. {
  5750. std::vector<float> out ((std::size_t) n);
  5751. for (int i = 0; i < n; ++i)
  5752. out[(std::size_t) i] = (float) (2.0 * std::fmod (i * hz / fs, 1.0) - 1.0) * 0.4f;
  5753. return out;
  5754. }
  5755. TEST (vocodex_state_parse_and_conversion)
  5756. {
  5757. VocodexSpec spec;
  5758. spec.block = { 1, 3, 2, 40, 0, 2, 1 };
  5759. spec.params[17] = -1200;
  5760. const auto raw = spec.bytes();
  5761. const auto s = lynx::vocodex::parseState (raw);
  5762. CHECK (s.has_value());
  5763. if (! s)
  5764. return;
  5765. CHECK (s->version == 8 && s->holdMode == 1 && s->order == 3 && s->unison == 2 && s->bands == 40);
  5766. CHECK (s->draft() && s->modIn == 2 && s->carIn == 1 && s->minTimes && s->subtractiveGate && ! s->lrEncoding);
  5767. CHECK (s->params[lynx::vocodex::modPitch] == -1200 && s->params[lynx::vocodex::carLp] == 65536);
  5768. 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);
  5769. CHECK (s->tone == "PWM" && s->notes.size() == 1 && s->notes[0] == 60);
  5770. const auto v = lynx::vocodexEffect (raw);
  5771. CHECK (v && v->slot.kind == lynx::EffectKind::vocodex && v->slot.state == raw && v->modIn == 2 && v->carIn == 1 && v->notes.empty());
  5772. spec.block[6] = -1; // internal synth carrier: not available yet → reported
  5773. spec.params[1] = 150; // Soundgoodizer stage
  5774. const auto w = lynx::vocodexEffect (spec.bytes());
  5775. CHECK (w && w->carIn == -1 && w->notes.size() == 2);
  5776. CHECK (! lynx::vocodex::parseState ({ 1, 2, 3 }).has_value());
  5777. }
  5778. TEST (vocodex_vocodes_side_inputs)
  5779. {
  5780. // modulator on side input 1 (noise in the first half), carrier on side input 2 (saw), own input silent: the
  5781. // output follows the modulator's envelope and fades after it stops
  5782. VocodexSpec spec;
  5783. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  5784. slot.state = spec.bytes();
  5785. auto fx = lynx::makeEffect (slot);
  5786. fx->prepare (48000.0);
  5787. CHECK (fx->latencySamples() == 0);
  5788. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  5789. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  5790. std::uint32_t seed = 7;
  5791. for (std::size_t i = 0; i < mod.size(); ++i)
  5792. {
  5793. seed = seed * 1664525u + 1013904223u;
  5794. mod[i] = i < 24000 ? ((float) (seed >> 8) / 16777216.0f - 0.5f) : 0.0f;
  5795. }
  5796. for (int done = 0; done < 48000; done += 512)
  5797. {
  5798. const int n = std::min (512, 48000 - done);
  5799. fx->setSidechain (mod.data() + done, mod.data() + done);
  5800. fx->setSidechain2 (car.data() + done, car.data() + done);
  5801. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  5802. }
  5803. auto rms = [&] (int from, int to) {
  5804. double e = 0.0;
  5805. for (int i = from; i < to; ++i)
  5806. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  5807. return std::sqrt (e / (2.0 * (to - from)));
  5808. };
  5809. CHECK (rms (6000, 20000) > 0.02);
  5810. CHECK (rms (40000, 48000) < 0.01 * rms (6000, 20000));
  5811. CHECK (std::abs (l[0]) < 1e-6f); // the gains ramp up from 0 when the plugin is created
  5812. }
  5813. TEST (vocodex_draft_latency_and_pass_through)
  5814. {
  5815. // carrier pass-through at unit gain only (Wet 0): after a Flush the gains snap, so the output is the carrier,
  5816. // 16 samples late in Draft
  5817. VocodexSpec spec;
  5818. spec.params[0] = 0; // Wet
  5819. spec.params[4] = 12800; // CarVol = 1
  5820. for (const int draftField : { 1, 0 })
  5821. {
  5822. spec.block[4] = draftField;
  5823. const auto s = lynx::vocodex::parseState (spec.bytes());
  5824. CHECK (s.has_value());
  5825. lynx::vocodex::Engine engine;
  5826. engine.prepare (*s, 44100.0, 300, s->draft());
  5827. CHECK (engine.latencySamples() == (draftField == 0 ? 16 : 0));
  5828. engine.flush();
  5829. const auto saw = vocodexSaw (3000, 220.0, 44100.0);
  5830. std::vector<float> car (6000), mod (6000, 0.0f), out (6000);
  5831. for (int i = 0; i < 3000; ++i)
  5832. car[(std::size_t) (2 * i)] = car[(std::size_t) (2 * i + 1)] = saw[(std::size_t) i];
  5833. static constexpr int sizes[] { 300, 37, 128, 1, 255, 16, 299 }; // uneven blocks exercise the FIFOs
  5834. for (int done = 0, k = 0, n = 0; done < 3000; done += n, ++k)
  5835. {
  5836. n = std::min (sizes[k % 7], 3000 - done);
  5837. engine.process (mod.data() + 2 * done, mod.data() + 2 * done, car.data() + 2 * done, out.data() + 2 * done, n);
  5838. }
  5839. const int lag = engine.latencySamples();
  5840. auto maxError = 0.0f;
  5841. for (int i = lag; i < 3000; ++i)
  5842. maxError = std::max (maxError, std::abs (out[(std::size_t) (2 * i)] - saw[(std::size_t) (i - lag)]));
  5843. CHECK (maxError == 0.0f);
  5844. }
  5845. }
  5846. TEST (vocodex_soundgoodizer_stage_limits_the_output)
  5847. {
  5848. // SG > 0: the vocoder's output (loud here: Wet 2000) goes through the Soundgoodizer approximation, whose
  5849. // master limiter keeps the peaks under −0.2 dBFS; its look-ahead is reported as latency
  5850. VocodexSpec spec;
  5851. spec.params[0] = 2000;
  5852. spec.params[1] = 250;
  5853. lynx::EffectSlot slot = lynx::makeEffectSlot (lynx::EffectKind::vocodex);
  5854. slot.state = spec.bytes();
  5855. auto fx = lynx::makeEffect (slot);
  5856. fx->prepare (48000.0);
  5857. CHECK (fx->latencySamples() == 72); // 1.5 ms at 48 kHz
  5858. const auto car = vocodexSaw (48000, 110.0, 48000.0);
  5859. std::vector<float> mod (48000), l (48000, 0.0f), r (48000, 0.0f);
  5860. for (std::size_t i = 0; i < mod.size(); ++i)
  5861. mod[i] = 0.9f * (float) std::sin (0.07 * (double) i) * (float) std::sin (0.0003 * (double) i);
  5862. auto peak = 0.0f;
  5863. for (int done = 0; done < 48000; done += 512)
  5864. {
  5865. const int n = std::min (512, 48000 - done);
  5866. fx->setSidechain (mod.data() + done, mod.data() + done);
  5867. fx->setSidechain2 (car.data() + done, car.data() + done);
  5868. fx->process (l.data() + done, r.data() + done, n, slot.params, 120.0);
  5869. }
  5870. for (std::size_t i = 0; i < l.size(); ++i)
  5871. peak = std::max ({ peak, std::abs (l[i]), std::abs (r[i]) });
  5872. CHECK (peak > 0.5f && peak <= 0.9775f);
  5873. }
  5874. TEST (vocodex_tail_bypass_and_noise)
  5875. {
  5876. // both inputs silent for longer than 0.5 s + hold + release: the own input passes unchanged
  5877. VocodexSpec spec;
  5878. const auto s = lynx::vocodex::parseState (spec.bytes());
  5879. CHECK (s.has_value());
  5880. lynx::vocodex::Engine engine;
  5881. engine.prepare (*s, 44100.0, 512, false);
  5882. std::vector<float> zero (1024, 0.0f), own (1024), out (1024);
  5883. for (std::size_t i = 0; i < own.size(); ++i)
  5884. own[i] = 0.25f * (float) std::sin (0.01 * (double) i);
  5885. for (int b = 0; b < 80; ++b)
  5886. engine.process (own.data(), zero.data(), zero.data(), out.data(), 512);
  5887. CHECK (out == own);
  5888. // carrier noise: the IPP-compatible generator restarts from the same seed after a Flush
  5889. spec.params[6] = 9000;
  5890. const auto noisy = lynx::vocodex::parseState (spec.bytes());
  5891. lynx::vocodex::Engine a;
  5892. a.prepare (*noisy, 44100.0, 512, false);
  5893. std::vector<float> mod (1024), first (1024), second (1024);
  5894. for (std::size_t i = 0; i < mod.size(); ++i)
  5895. mod[i] = 0.3f * (float) std::sin (0.05 * (double) i);
  5896. a.flush();
  5897. a.process (own.data(), mod.data(), zero.data(), first.data(), 512);
  5898. a.flush();
  5899. a.process (own.data(), mod.data(), zero.data(), second.data(), 512);
  5900. CHECK (first == second);
  5901. auto energy = 0.0;
  5902. for (auto v : first)
  5903. energy += (double) v * v;
  5904. CHECK (energy > 1e-6 && std::isfinite (energy));
  5905. }
  5906. TEST (synth_spectral_oscillator)
  5907. {
  5908. // 1/n spectrum = saw: same harmonic proportions and same level as LYNX's saw
  5909. lynx::SynthSettings saw;
  5910. saw.osc[0] = { lynx::Waveform::saw, 0, 0.0f, 1.0f };
  5911. saw.cutoff = 20000.0f;
  5912. auto spectral = saw;
  5913. spectral.osc[0].wave = lynx::Waveform::spectral;
  5914. for (int n = 1; n <= lynx::SynthSettings::spectrumSize; ++n)
  5915. spectral.spectrum[(std::size_t) n - 1] = 1.0f / (float) n;
  5916. const auto a = renderSynthNote (saw, 57, 24000), b = renderSynthNote (spectral, 57, 24000); // 220 Hz
  5917. for (const auto h : { 1, 2, 3, 5 })
  5918. {
  5919. const auto ea = goertzel (a, 220.0 * h, 4800, 24000), eb = goertzel (b, 220.0 * h, 4800, 24000);
  5920. CHECK (std::abs (10.0 * std::log10 (eb / ea)) < 0.5); // within 0.5 dB
  5921. }
  5922. // odd only: no 2nd harmonic
  5923. auto odd = spectral;
  5924. for (int n = 2; n <= lynx::SynthSettings::spectrumSize; n += 2)
  5925. odd.spectrum[(std::size_t) n - 1] = 0.0f;
  5926. const auto c = renderSynthNote (odd, 57, 24000);
  5927. CHECK (goertzel (c, 440.0, 4800, 24000) < 1e-4 * goertzel (c, 220.0, 4800, 24000));
  5928. // high note: the band-limited tables don't pass Nyquist (no aliasing below the fundamental)
  5929. const auto d = renderSynthNote (spectral, 105, 24000); // 3520 Hz
  5930. CHECK (goertzel (d, 1000.0, 4800, 24000) < 1e-3 * goertzel (d, 3520.0, 4800, 24000));
  5931. }
  5932. TEST (compressor_sidechain_ducks_and_orders_mixer)
  5933. {
  5934. // the detector listens to the side input: quiet signal on the track, loud kick on the sidechain -> reduction
  5935. auto slot = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5936. slot.params[0] = 0.5f; // threshold
  5937. slot.params[1] = 1.0f; // high ratio
  5938. slot.params[4] = 0.0f;
  5939. auto run = [&] (bool withSidechain) {
  5940. auto fx = lynx::makeEffect (lynx::EffectKind::compressor);
  5941. fx->prepare (48000.0);
  5942. std::vector<float> l (4800, 0.1f), r (4800, 0.1f), kick (4800, 1.0f);
  5943. if (withSidechain)
  5944. fx->setSidechain (kick.data(), kick.data());
  5945. fx->process (l.data(), r.data(), 4800, slot.params, 120.0);
  5946. return l.back();
  5947. };
  5948. const auto alone = run (false), ducked = run (true);
  5949. CHECK (ducked < 0.5f * alone);
  5950. // order: track 5 listens to 7 via sidechain -> 7 comes before 5
  5951. auto song = lynx::Song::makeDefault();
  5952. while (song.mixer.size() < 8)
  5953. song.mixer.push_back ({});
  5954. auto comp = lynx::makeEffectSlot (lynx::EffectKind::compressor);
  5955. comp.sidechain = 7;
  5956. song.mixer[5].effects.push_back (comp);
  5957. const auto order = song.mixerOrder();
  5958. const auto at = [&] (int t) { return std::find (order.begin(), order.end(), t) - order.begin(); };
  5959. CHECK (at (7) < at (5));
  5960. }
  5961. TEST (pitch_correction_pulls_to_allowed_note)
  5962. {
  5963. auto run = [] (const lynx::EffectParams& p, double hz) {
  5964. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5965. fx->prepare (48000.0);
  5966. std::vector<float> l (48000 * 2), r;
  5967. for (std::size_t i = 0; i < l.size(); ++i)
  5968. l[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * hz * (double) i / 48000.0);
  5969. r = l;
  5970. fx->process (l.data(), r.data(), (int) l.size(), p, 120.0);
  5971. // frequency from zero crossings in the last second
  5972. int crossings = 0;
  5973. for (std::size_t i = 48000 + 1; i < l.size(); ++i)
  5974. crossings += (l[i - 1] < 0.0f) != (l[i] < 0.0f);
  5975. return crossings / 2.0;
  5976. };
  5977. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  5978. CHECK (std::abs (run (slot.params, 452.0) - 440.0) < 4.0);
  5979. for (int k = 0; k < 12; ++k)
  5980. slot.params[(std::size_t) (5 + k)] = k == 0 ? 1.0f : 0.0f; // only C
  5981. CHECK (std::abs (run (slot.params, 452.0) - 523.25) < 5.0); // C5 (523 Hz) is closer than C4 (262)
  5982. slot.params[2] = 1.0f; // turn everything off: passes unchanged
  5983. CHECK (std::abs (run (slot.params, 452.0) - 452.0) < 4.0);
  5984. }
  5985. TEST (pitch_correction_bypass_is_a_pure_delay)
  5986. {
  5987. // FL's Pitcher with Bypass All still runs its shifter: the plain path is an exact 9-sample delay, the formant
  5988. // path (256-Hann overlap-add) an exact 257-sample delay, and Gender changes nothing without a shift
  5989. // (REA_rodada30 §2.4/2.5); both are reported as latency
  5990. std::vector<float> in (48000);
  5991. for (std::size_t i = 0; i < in.size(); ++i)
  5992. in[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 440.0 * (double) i / 48000.0)
  5993. + 0.2f * (float) std::sin (2.0 * 3.141592653589793 * 1234.5 * (double) i / 48000.0);
  5994. auto run = [&] (bool formant, float gender, int& latency) {
  5995. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  5996. fx->prepare (48000.0);
  5997. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect);
  5998. slot.params[2] = 1.0f;
  5999. slot.params[29] = formant ? 1.0f : 0.0f;
  6000. slot.params[30] = gender;
  6001. auto l = in, r = in;
  6002. for (std::size_t i = 0; i < l.size(); i += 480)
  6003. fx->process (l.data() + i, r.data() + i, 480, slot.params, 120.0);
  6004. latency = fx->latencySamples();
  6005. return l;
  6006. };
  6007. int latency = 0;
  6008. auto plain = run (false, 0.5f, latency);
  6009. CHECK (latency == 9);
  6010. auto worst = 0.0f;
  6011. for (std::size_t i = 4000; i < in.size(); ++i)
  6012. worst = std::max (worst, std::abs (plain[i] - in[i - 9]));
  6013. CHECK (worst < 1e-5f);
  6014. auto formant = run (true, 0.8f, latency);
  6015. CHECK (latency == 257);
  6016. worst = 0.0f;
  6017. for (std::size_t i = 4000; i < in.size(); ++i)
  6018. worst = std::max (worst, std::abs (formant[i] - in[i - 257]));
  6019. CHECK (worst < 1e-5f);
  6020. }
  6021. TEST (pitch_correction_formant_path)
  6022. {
  6023. // Formant on: PSOLA keeps the correction (output pitch = grain spacing) while Gender resamples the grains:
  6024. // the spectrum of a saw moves up with Gender > 0.5 and down with Gender < 0.5
  6025. auto run = [] (float gender) {
  6026. auto fx = lynx::makeEffect (lynx::EffectKind::pitchCorrect);
  6027. fx->prepare (48000.0);
  6028. auto slot = lynx::makeEffectSlot (lynx::EffectKind::pitchCorrect); // chromatic, instant, A 440
  6029. slot.params[29] = 1.0f;
  6030. slot.params[30] = gender;
  6031. std::vector<float> l (48000 * 2), r;
  6032. for (std::size_t i = 0; i < l.size(); ++i)
  6033. l[i] = 0.5f * (float) (2.0 * std::fmod (452.0 * (double) i / 48000.0, 1.0) - 1.0); // saw
  6034. r = l;
  6035. fx->process (l.data(), r.data(), (int) l.size(), slot.params, 120.0);
  6036. l.erase (l.begin(), l.begin() + 48000);
  6037. return l;
  6038. };
  6039. // period by autocorrelation (lags 60…160 = 300…800 Hz)
  6040. auto pitch = [] (const std::vector<float>& x) {
  6041. auto best = 0.0, bestLag = 0.0;
  6042. for (int lag = 60; lag <= 160; ++lag)
  6043. {
  6044. auto sum = 0.0;
  6045. for (std::size_t i = 0; i + (std::size_t) lag < x.size(); ++i)
  6046. sum += (double) x[i] * x[i + (std::size_t) lag];
  6047. if (sum > best)
  6048. {
  6049. best = sum;
  6050. bestLag = lag;
  6051. }
  6052. }
  6053. return 48000.0 / bestLag;
  6054. };
  6055. // brightness: energy of the first difference relative to the energy
  6056. auto brightness = [] (const std::vector<float>& x) {
  6057. auto e = 0.0, d = 0.0;
  6058. for (std::size_t i = 1; i < x.size(); ++i)
  6059. {
  6060. e += (double) x[i] * x[i];
  6061. d += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  6062. }
  6063. return d / e;
  6064. };
  6065. const auto neutral = run (0.5f), up = run (0.8f), down = run (0.3f);
  6066. CHECK (std::abs (pitch (neutral) - 440.0) < 5.0); // 452 Hz pulled to A
  6067. CHECK (std::abs (pitch (up) - 440.0) < 5.0); // Gender does not move the pitch
  6068. CHECK (brightness (up) > 1.5 * brightness (neutral));
  6069. CHECK (brightness (down) < 0.7 * brightness (neutral));
  6070. }
  6071. TEST (parallel_container_inner_links_modulate)
  6072. {
  6073. // one branch: utility (gain) + a control-only auto-pan LFO (1 beat); the link maps the LFO 0..1 onto the
  6074. // utility gain −24 dB..0 dB, so the level must swing; without the link it stays put
  6075. auto container = lynx::makeEffectSlot (lynx::EffectKind::parallel);
  6076. auto gain = lynx::makeEffectSlot (lynx::EffectKind::utility);
  6077. auto lfo = lynx::makeEffectSlot (lynx::EffectKind::autoPan);
  6078. lfo.params[0] = 1.0f / 64.0f;
  6079. lfo.params[4] = 1.0f; // control only: audio passes unchanged
  6080. container.branches.push_back ({ { gain, lfo }, 1.0f });
  6081. lynx::EffectLink link;
  6082. link.source = 1;
  6083. link.param = 0;
  6084. for (int i = 0; i <= 32; ++i)
  6085. link.curve.push_back (0.5f * (float) i / 32.0f);
  6086. auto levels = [&] (bool linked) {
  6087. auto slot = container;
  6088. if (linked)
  6089. slot.links.push_back (link);
  6090. auto fx = lynx::makeParallelEffect (slot);
  6091. fx->prepare (48000.0);
  6092. float lo = 1e9f, hi = 0.0f;
  6093. for (int block = 0; block < 94; ++block) // ~1 beat at 120 BPM in 256-sample blocks
  6094. {
  6095. std::vector<float> l (256, 1.0f), r (256, 1.0f);
  6096. fx->setSongPosition (block * 256.0 / 48000.0 * 2.0 * 960.0); // the engine positions every block
  6097. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  6098. lo = std::min (lo, std::abs (l[128]));
  6099. hi = std::max (hi, std::abs (l[128]));
  6100. }
  6101. return std::pair { lo, hi };
  6102. };
  6103. const auto [lo, hi] = levels (true);
  6104. CHECK (hi > 0.8f && lo < 0.12f);
  6105. const auto [lo0, hi0] = levels (false);
  6106. CHECK (std::abs (hi0 - lo0) < 1e-4f);
  6107. }
  6108. TEST (synth_spectrum_decays_per_harmonic)
  6109. {
  6110. // 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
  6111. lynx::SynthSettings s;
  6112. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6113. s.cutoff = 20000.0f;
  6114. s.decay = 0.0f;
  6115. s.sustain = 1.0f;
  6116. s.spectrum[0] = 1.0f;
  6117. s.spectrum[2] = 1.0f;
  6118. s.spectrumDecay[2] = 0.2f;
  6119. const auto x = renderSynthNote (s, 57, 48000); // 220 Hz, 48 kHz, 1 s
  6120. // equal 2400-sample windows (Goertzel power grows with the window): 10…60 ms and 500…550 ms
  6121. const auto early1 = goertzel (x, 220.0, 480, 2880), early3 = goertzel (x, 660.0, 480, 2880);
  6122. const auto late1 = goertzel (x, 220.0, 24000, 26400), late3 = goertzel (x, 660.0, 24000, 26400);
  6123. CHECK (early3 > 0.05 * early1); // ≈ −9 dB on average over 10…60 ms
  6124. CHECK (late3 < 1e-4 * late1);
  6125. CHECK (std::abs (10.0 * std::log10 (late1 / early1)) < 1.0); // the 1st holds
  6126. // without per-harmonic decay the spectrum stays as it is
  6127. s.spectrumDecay[2] = 0.0f;
  6128. const auto y = renderSynthNote (s, 57, 48000);
  6129. CHECK (std::abs (10.0 * std::log10 (goertzel (y, 660.0, 24000, 26400) / goertzel (y, 660.0, 480, 2880))) < 1.0);
  6130. }
  6131. TEST (worker_pool_runs_each_index_once_per_round)
  6132. {
  6133. // rounds of alternating sizes (a stalled worker must never run an index of a later round), each index
  6134. // exactly once, and nothing running after parallelFor returns
  6135. lynx::WorkerPool pool (3);
  6136. std::vector<std::atomic<int>> hits (64);
  6137. std::atomic<int> round { 0 }, outside { 0 }, wrongRound { 0 };
  6138. std::atomic<bool> open { false };
  6139. int failures = 0;
  6140. for (int r = 1; r <= 20000; ++r)
  6141. {
  6142. const auto n = (r % 3 == 0) ? 64 : (r % 3 == 1 ? 2 : 9);
  6143. for (int i = 0; i < n; ++i)
  6144. hits[(std::size_t) i].store (0);
  6145. round.store (r);
  6146. open.store (true);
  6147. auto fn = [&, r] (int i) {
  6148. if (! open.load())
  6149. outside.fetch_add (1);
  6150. if (round.load() != r)
  6151. wrongRound.fetch_add (1);
  6152. hits[(std::size_t) i].fetch_add (1);
  6153. };
  6154. pool.parallelFor (n, fn);
  6155. open.store (false);
  6156. for (int i = 0; i < n; ++i)
  6157. failures += hits[(std::size_t) i].load() != 1;
  6158. }
  6159. CHECK (failures == 0);
  6160. CHECK (outside.load() == 0);
  6161. CHECK (wrongRound.load() == 0);
  6162. }
  6163. TEST (ladder_filter_follows_toxic_law)
  6164. {
  6165. // the 4-pole ladder: −24 dB/oct far above the cutoff (vs −12 for the SVF), resonance raises the corner, and
  6166. // the input saturation keeps it bounded even at full resonance
  6167. auto level = [] (bool ladder, float res, double hz) {
  6168. lynx::SynthSettings s;
  6169. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6170. s.cutoff = 500.0f;
  6171. s.resonance = res;
  6172. s.ladder = ladder;
  6173. const auto key = (int) std::lround (69.0 + 12.0 * std::log2 (hz / 440.0));
  6174. const auto x = renderSynthNote (s, key, 48000);
  6175. double e = 0.0, peak = 0.0;
  6176. for (std::size_t i = 24000; i < 48000; ++i)
  6177. e += (double) x[i] * x[i], peak = std::max (peak, (double) std::abs (x[i]));
  6178. return std::pair { std::sqrt (e / 24000.0), peak };
  6179. };
  6180. 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
  6181. const auto svfHigh = level (false, 0.0f, 3520.0).first / level (false, 0.0f, 110.0).first;
  6182. CHECK (20.0 * std::log10 (high / low) < -55.0); // ≈ 24 dB/oct over ~2.8 octaves
  6183. CHECK (20.0 * std::log10 (svfHigh) > -45.0); // the 2-pole SVF falls much less
  6184. const auto corner0 = level (true, 0.0f, 523.25).first, corner1 = level (true, 0.6f, 523.25).first;
  6185. CHECK (corner1 > 1.5 * corner0); // resonance boosts near the cutoff
  6186. const auto loud = level (true, 1.0f, 523.25);
  6187. CHECK (std::isfinite (loud.first) && loud.second < 20.0); // self-oscillation stays bounded
  6188. }
  6189. TEST (synth_spectrum_settles_on_target)
  6190. {
  6191. // ALT DECAY-like morph: harmonic 3 starts at 1 and settles on 0.25 (−12 dB) instead of fading out
  6192. lynx::SynthSettings s;
  6193. s.osc[0] = { lynx::Waveform::spectral, 0, 0.0f, 1.0f };
  6194. s.cutoff = 20000.0f;
  6195. s.decay = 0.0f;
  6196. s.sustain = 1.0f;
  6197. s.spectrum[0] = 1.0f;
  6198. s.spectrum[2] = 1.0f;
  6199. s.spectrumDecay[2] = 0.1f;
  6200. s.spectrumTarget[2] = 0.25f;
  6201. const auto x = renderSynthNote (s, 57, 48000);
  6202. const auto ratio = goertzel (x, 660.0, 24000, 26400) / goertzel (x, 220.0, 24000, 26400);
  6203. CHECK (std::abs (10.0 * std::log10 (ratio) - 20.0 * std::log10 (0.25)) < 1.0);
  6204. }
  6205. TEST (kepler_chorus_is_stereo_and_keeps_level)
  6206. {
  6207. // chorus I on a sine: L and R differ (opposite LFO phases), the level stays close to the dry one
  6208. lynx::SynthSettings s;
  6209. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6210. s.cutoff = 20000.0f;
  6211. auto dry = s;
  6212. s.chorus = 1;
  6213. auto render = [] (const lynx::SynthSettings& set) {
  6214. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6215. inst->prepare (48000.0);
  6216. lynx::ChannelDef c;
  6217. c.synth = set;
  6218. inst->configure (c, 120.0);
  6219. inst->noteOn (69, 1.0f);
  6220. std::vector<float> l (96000), r (96000);
  6221. inst->render (l.data(), r.data(), 96000, 1.0f, 1.0f);
  6222. return std::pair { l, r };
  6223. };
  6224. const auto [l, r] = render (s);
  6225. const auto [dl, dr] = render (dry);
  6226. double diff = 0.0, e = 0.0, ed = 0.0;
  6227. for (std::size_t i = 48000; i < 96000; ++i)
  6228. diff += std::abs (l[i] - r[i]), e += (double) l[i] * l[i], ed += (double) dl[i] * dl[i];
  6229. CHECK (diff / 48000.0 > 1e-3);
  6230. CHECK (std::abs (10.0 * std::log10 (e / ed)) < 3.0);
  6231. }
  6232. TEST (fl_pan_law_matches_love_philter_dll)
  6233. {
  6234. // values printed by the installation's Love Philter pan routine for vol 0.8 (REA_rodada27 §3.1, lppan.txt)
  6235. struct Row { double pan, ll, rr, rl, lr; };
  6236. const Row rows[] = { { -1.5, 0.239782482, -0.432956904, 0.805467963, 0.0 }, { -0.5, 0.805467963, 0.432956904, 0.239782482, 0.0 },
  6237. { 0.0, 0.8, 0.8, 0.0, 0.0 }, { 0.75, 0.220719516, 0.707889318, 0.0, 0.401742548 },
  6238. { 1.0, 0.0, 0.565685451, 0.0, 0.565685451 } };
  6239. for (const auto& row : rows)
  6240. {
  6241. float ll, rl, lr, rr;
  6242. lynx::flPanMatrix (row.pan, ll, rl, lr, rr);
  6243. CHECK (std::abs (0.8 * ll - row.ll) < 1e-6 && std::abs (0.8 * rr - row.rr) < 1e-6);
  6244. CHECK (std::abs (0.8 * rl - row.rl) < 1e-6 && std::abs (0.8 * lr - row.lr) < 1e-6);
  6245. }
  6246. }
  6247. TEST (effector_units_match_fl_dll_impulse_responses)
  6248. {
  6249. // impulse responses of the installation's Effector engine (REA_rodada27 §2, eu_<kind>_<X>_<Y>.txt; LFO off,
  6250. // wet 1, blocks of 256): samples 1, 10, 100 and 1000
  6251. struct Case { int kind; float x, y; double v[4]; };
  6252. const Case cases[] = { { 3, 0.5f, 0.5f, { -0.603055418, 0.159798339, -0.0055043716, -1.06241259e-06 } },
  6253. { 4, 0.2f, 0.5f, { 4.68885949e-26, 4.20032622e-19, 0.000487731799, 2.27715846e-09 } },
  6254. { 4, 0.8f, 0.3f, { -0.293783396, -0.0306414738, -0.00068128563, 1.358577e-15 } } };
  6255. for (const auto& c : cases)
  6256. {
  6257. auto fx = lynx::effector::make();
  6258. fx->prepare (44100.0);
  6259. lynx::EffectParams p {};
  6260. p[lynx::effector::type] = c.kind / 11.0f;
  6261. p[lynx::effector::wetMix] = 1.0f;
  6262. p[lynx::effector::paramX] = c.x;
  6263. p[lynx::effector::paramY] = c.y;
  6264. p[lynx::effector::modX] = p[lynx::effector::modY] = 0.5f;
  6265. p[lynx::effector::modRate] = 0.5f;
  6266. p[lynx::effector::inputLevel] = 1.0f;
  6267. p[lynx::effector::outputGain] = 0.5f;
  6268. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  6269. l[0] = r[0] = 1.0f;
  6270. for (int d = 0; d < 1024; d += 256)
  6271. fx->process (l.data() + d, r.data() + d, 256, p, 120.0);
  6272. const int at[4] { 1, 10, 100, 1000 };
  6273. for (int k = 0; k < 4; ++k)
  6274. CHECK (std::abs (l[(std::size_t) at[k]] - c.v[k]) < 2e-7 + 1e-5 * std::abs (c.v[k]));
  6275. }
  6276. }
  6277. TEST (harmless_stereo_tremolo_and_vibrato)
  6278. {
  6279. // tremolo d = 1: each side swings between 0 and 1.25 at 5 Hz, L and R in anti-phase; vibrato modulates the pitch
  6280. lynx::SynthSettings s;
  6281. s.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6282. s.cutoff = 20000.0f;
  6283. s.stereoTremolo = 1.0f;
  6284. auto inst = lynx::makeInstrument (lynx::InstrumentKind::synth);
  6285. inst->prepare (48000.0);
  6286. lynx::ChannelDef c;
  6287. c.synth = s;
  6288. inst->configure (c, 120.0);
  6289. inst->noteOn (69, 1.0f);
  6290. std::vector<float> l (48000), r (48000);
  6291. inst->render (l.data(), r.data(), 48000, 1.0f, 1.0f);
  6292. 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; };
  6293. // θ = 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
  6294. CHECK (envelope (l, 19200) > 4.0f * envelope (r, 19200));
  6295. CHECK (envelope (r, 24000) > 4.0f * envelope (l, 24000));
  6296. // vibrato 100 cents: zero crossings vary in spacing; without it they are regular
  6297. auto crossingsSpread = [] (float cents) {
  6298. lynx::SynthSettings v;
  6299. v.osc[0] = { lynx::Waveform::sine, 0, 0.0f, 1.0f };
  6300. v.cutoff = 20000.0f;
  6301. v.vibratoCents = cents;
  6302. const auto x = renderSynthNote (v, 69, 48000);
  6303. std::vector<int> zc;
  6304. for (int i = 24000; i < 48000; ++i)
  6305. if (x[(std::size_t) i - 1] < 0.0f && x[(std::size_t) i] >= 0.0f)
  6306. zc.push_back (i);
  6307. int lo = 1 << 30, hi = 0;
  6308. for (std::size_t k = 1; k < zc.size(); ++k)
  6309. lo = std::min (lo, zc[k] - zc[k - 1]), hi = std::max (hi, zc[k] - zc[k - 1]);
  6310. return hi - lo;
  6311. };
  6312. CHECK (crossingsSpread (0.0f) <= 1);
  6313. CHECK (crossingsSpread (100.0f) >= 4);
  6314. }
  6315. TEST (physical_models_sound_and_end)
  6316. {
  6317. // Sakura: a plucked string with default-ish parameters rings and fades after the release; Drumpad: a hit decays
  6318. for (const auto kind : { lynx::InstrumentKind::sakura, lynx::InstrumentKind::drumpad })
  6319. {
  6320. lynx::ChannelDef c;
  6321. c.instrument = kind;
  6322. if (kind == lynx::InstrumentKind::sakura)
  6323. {
  6324. c.modelParams.assign (146, 0.5f);
  6325. c.modelParams[111] = 3.0f; // both strings at the note
  6326. c.modelParams[113] = 0.5f; // octave ×1
  6327. c.modelParams[6] = c.modelParams[11] = 0.99f; // long decay
  6328. c.modelParams[31] = 0.0f; // noise exciter
  6329. }
  6330. else
  6331. {
  6332. c.modelParams.assign (38, 0.5f);
  6333. c.modelParams[0] = 1.0f; // retrigger noise
  6334. c.modelParams[30] = c.modelParams[31] = c.modelParams[32] = c.modelParams[33] = 0.0f; // no velocity morph
  6335. }
  6336. auto inst = lynx::makeInstrument (kind);
  6337. inst->prepare (48000.0);
  6338. inst->configure (c, 120.0);
  6339. inst->noteOn (60, 0.8f);
  6340. std::vector<float> l (48000), r (48000);
  6341. inst->render (l.data(), r.data(), 24000, 1.0f, 1.0f);
  6342. inst->noteOff (60);
  6343. inst->render (l.data() + 24000, r.data() + 24000, 24000, 1.0f, 1.0f);
  6344. double early = 0.0;
  6345. for (int i = 0; i < 12000; ++i)
  6346. early += (double) l[(std::size_t) i] * l[(std::size_t) i];
  6347. CHECK (early > 1e-6);
  6348. CHECK (std::all_of (l.begin(), l.end(), [] (float v) { return std::isfinite (v); }));
  6349. std::vector<float> tail (480000), tailR (480000);
  6350. inst->render (tail.data(), tailR.data(), 480000, 1.0f, 1.0f); // 10 s later the voice has ended
  6351. CHECK (inst->idle());
  6352. }
  6353. }
  6354. TEST (squeeze_matches_fl_model)
  6355. {
  6356. // Fruity Squeeze: Steps quantises, Mix 0 passes the input dry ×Volume/127; a silent block is left untouched
  6357. auto slot = lynx::makeEffectSlot (lynx::EffectKind::squeeze);
  6358. slot.params[1] = 0.0f; // mix 0: dry only
  6359. auto fx = lynx::makeEffect (lynx::EffectKind::squeeze);
  6360. fx->prepare (44100.0);
  6361. std::vector<float> l (512, 0.3f), r (512, -0.2f);
  6362. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  6363. CHECK (std::abs (l[100] - 0.3f) < 1e-6f && std::abs (r[100] + 0.2f) < 1e-6f); // Volume 127/127
  6364. slot.params[1] = 1.0f; // wet only
  6365. slot.params[2] = 32000.0f / 32768.0f; // 32768^(1 − 32000/32768) ≈ 1.27 levels per unit → heavy quantisation
  6366. std::vector<float> a (512), b (512);
  6367. for (int i = 0; i < 512; ++i)
  6368. a[(std::size_t) i] = b[(std::size_t) i] = 0.4f * (float) std::sin (i * 0.05);
  6369. fx->process (a.data(), b.data(), 512, slot.params, 120.0);
  6370. const auto levels = std::pow (32768.0f, 1.0f - 32000.0f / 32768.0f);
  6371. for (int i = 0; i < 512; i += 37)
  6372. CHECK (std::abs (a[(std::size_t) i] * levels - std::nearbyint (a[(std::size_t) i] * levels)) < 1e-3f);
  6373. std::vector<float> z (512, 0.0f), z2 (512, 0.0f);
  6374. fx->process (z.data(), z2.data(), 512, slot.params, 120.0);
  6375. CHECK (z[10] == 0.0f);
  6376. }
  6377. TEST (sakura_matrix_and_effects)
  6378. {
  6379. // REA_rodada29 §1: velocity → level follows 10^(2m)·0.05 (vel 1 vs 0.5 → ×10 in amplitude); the delay leaves
  6380. // an echo after the note has ended and keeps the instrument from going idle until its tail is silent
  6381. auto base = [] {
  6382. lynx::ChannelDef c;
  6383. c.instrument = lynx::InstrumentKind::sakura;
  6384. c.modelParams.assign (146, 0.0f);
  6385. const std::pair<int, float> values[] { { 0, 0.375f }, { 1, 0.5f }, { 3, 0.5f }, { 4, 1.0f }, { 6, 0.95f }, { 9, 0.3f },
  6386. { 11, 0.95f }, { 14, 0.3f }, { 18, 0.5f }, { 19, 0.5f }, { 27, 0.5f }, { 28, 1.0f },
  6387. { 29, 0.7f }, { 31, 1.0f }, { 32, 0.5f }, { 111, 3.0f }, { 113, 0.5f } };
  6388. for (const auto& [i, v] : values)
  6389. c.modelParams[(std::size_t) i] = v;
  6390. return c;
  6391. };
  6392. auto render = [] (const lynx::ChannelDef& c, float velocity, int held, int total) {
  6393. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sakura);
  6394. inst->prepare (48000.0);
  6395. inst->configure (c, 120.0);
  6396. inst->noteOn (60, velocity);
  6397. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6398. for (int s = 0; s < total; s += 256)
  6399. {
  6400. if (s == held)
  6401. inst->noteOff (60);
  6402. inst->render (l.data() + s, r.data() + s, std::min (256, total - s), 1.0f, 1.0f);
  6403. }
  6404. return std::make_pair (l, std::move (inst));
  6405. };
  6406. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6407. double e = 0.0;
  6408. for (int i = from; i < to; ++i)
  6409. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6410. return e;
  6411. };
  6412. auto c = base();
  6413. c.modelParams[79] = 6.0f; // slot 1: velocity → level, amount 1
  6414. c.modelParams[80] = 1.0f;
  6415. c.modelParams[82] = 1.0f;
  6416. const auto loud = energy (render (c, 1.0f, 24064, 24064).first, 0, 24064);
  6417. const auto soft = energy (render (c, 0.5f, 24064, 24064).first, 0, 24064);
  6418. CHECK (loud > 1e-6 && std::abs (loud / soft - 100.0) < 1.0);
  6419. auto d = base();
  6420. auto [dry, dryInst] = render (d, 0.8f, 4864, 72000);
  6421. d.modelParams[63] = 1.0f; // delay on: time 0.5 → ≈ 0.52 s, wet 1, no feedback: one echo
  6422. d.modelParams[64] = 0.5f;
  6423. d.modelParams[69] = 1.0f;
  6424. auto [wet, wetInst] = render (d, 0.8f, 4864, 72000);
  6425. CHECK (energy (wet, 26000, 40000) > 100.0 * energy (dry, 26000, 40000) + 1e-9); // the note ends near 0.2 s
  6426. CHECK (dryInst->idle() || energy (dry, 70000, 72000) < 1e-12);
  6427. CHECK (! wetInst->idle()); // the echo tail has not been silent for 3 s yet
  6428. }
  6429. TEST (drumsynth_live_engine)
  6430. {
  6431. // REA_rodada30 §1: key → patch slot, state parsing, the engine against our reference model (no DLL: synthesised
  6432. // sources only, noise from the fallback seed), voice modes, patch level/pan
  6433. CHECK (lynx::drumSynthKeyToSlot (48) == 0 && lynx::drumSynthKeyToSlot (71) == 23 && lynx::drumSynthKeyToSlot (0) == 24);
  6434. CHECK (lynx::drumSynthKeyToSlot (47) == 71 && lynx::drumSynthKeyToSlot (72) == 72 && lynx::drumSynthKeyToSlot (119) == 119);
  6435. CHECK (lynx::drumSynthKeyToSlot (120) == -1);
  6436. // a version 3 state: 120 names, 120 × 38 ints (send track −1 becomes 0 below version 4), flags
  6437. std::vector<std::uint8_t> blob;
  6438. auto put = [&] (std::uint32_t v) { for (int k = 0; k < 4; ++k) blob.push_back ((std::uint8_t) (v >> (8 * k))); };
  6439. put (3);
  6440. put (66);
  6441. for (int i = 0; i < 120; ++i)
  6442. blob.push_back (2), blob.push_back ('a'), blob.push_back ((std::uint8_t) ('0' + i % 10));
  6443. for (int i = 0; i < 120 * 38; ++i)
  6444. put ((std::uint32_t) (i % 38 == 36 ? -1 : i % 38 == 4 ? 300 : 0));
  6445. put (0);
  6446. const auto parsed = lynx::drumSynthLiveParams (blob);
  6447. CHECK (parsed && (int) parsed->size() == lynx::drumSynthParamCount);
  6448. CHECK (parsed && (*parsed)[36] == 0.0f && (*parsed)[38 * 7 + 4] == 300.0f && (*parsed)[(std::size_t) lynx::drumSynthEditorKeyIndex] == 66.0f);
  6449. blob.resize (500);
  6450. CHECK (! lynx::drumSynthLiveParams (blob));
  6451. // patch on key 60 (slot 12): tone-mode Osc1 with sweep and click, noise-band Osc2, ring, 2 retriggers
  6452. 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,
  6453. 0, 0, 0, 0, 0, 0, 30, -1, 0, 0, 64 };
  6454. lynx::ChannelDef c;
  6455. c.instrument = lynx::InstrumentKind::drumsynth;
  6456. c.modelParams.assign ((std::size_t) lynx::drumSynthParamCount, 0.0f);
  6457. for (int s = 0; s < 120; ++s)
  6458. c.modelParams[(std::size_t) (38 * s + 15)] = c.modelParams[(std::size_t) (38 * s + 34)] = -1.0f;
  6459. for (int i = 0; i < 38; ++i)
  6460. c.modelParams[(std::size_t) (38 * 12 + i)] = (float) patch[i];
  6461. auto render = [] (const lynx::ChannelDef& def, std::vector<int> keys, int total, float gl = 1.0f, float gr = 1.0f) {
  6462. auto inst = lynx::makeInstrument (lynx::InstrumentKind::drumsynth);
  6463. inst->configure (def, 120.0);
  6464. inst->prepare (48000.0);
  6465. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6466. for (int s = 0; s < total; s += 512)
  6467. {
  6468. for (std::size_t k = 0; k + 1 < keys.size(); k += 2)
  6469. if (keys[k + 1] == s)
  6470. inst->noteOn (keys[k], 100.0f / 128.0f);
  6471. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6472. }
  6473. return std::make_tuple (l, r, std::move (inst));
  6474. };
  6475. {
  6476. auto [l, r, inst] = render (c, { 60, 0 }, 9600);
  6477. // reference model (REA round 30 dsl_model.py, seed 0x56CE, FL velocity 100, V = 1, centre: × g(100)·cos(π/4))
  6478. const std::pair<int, float> golden[] { { 0, -0.0074092713f }, { 10, -0.13961405f }, { 100, -0.47290668f }, { 1000, -0.29188696f },
  6479. { 3000, -0.22130175f }, { 6000, -0.049537566f }, { 9599, 0.010392102f } };
  6480. for (const auto& [i, v] : golden)
  6481. CHECK (std::abs (l[(std::size_t) i] - v) < 2e-6f && std::abs (r[(std::size_t) i] - v) < 2e-6f);
  6482. std::vector<float> tail (48000 * 4), tailR (tail.size());
  6483. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6484. CHECK (inst->idle()); // the engine reported the voice finished
  6485. }
  6486. auto energy = [] (const std::vector<float>& x, int from, int to) {
  6487. double e = 0.0;
  6488. for (int i = from; i < to; ++i)
  6489. e += (double) x[(std::size_t) i] * x[(std::size_t) i];
  6490. return e;
  6491. };
  6492. // a key without a patch level (slot 0, level 0) and a key outside the 120 patches are silent
  6493. CHECK (energy (std::get<0> (render (c, { 48, 0, 125, 0 }, 4096)), 0, 4096) == 0.0);
  6494. // mono: a second hit on the same key cuts the first; poly: both ring together (Osc2 off: no noise, deterministic)
  6495. auto tone = c;
  6496. tone.modelParams[38 * 12 + 14] = 0.0f;
  6497. const auto single = std::get<0> (render (tone, { 60, 0 }, 8192));
  6498. const auto mono = std::get<0> (render (tone, { 60, 0, 60, 2048 }, 8192));
  6499. auto poly = tone;
  6500. poly.modelParams[38 * 12 + 24] = 1.0f;
  6501. const auto both = std::get<0> (render (poly, { 60, 0, 60, 2048 }, 8192));
  6502. double monoDiff = 0.0, polyDiff = 0.0;
  6503. for (int i = 2048 + 300; i < 8192; ++i) // after the cut fade: mono = only the second hit, a copy of the first
  6504. 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]);
  6505. CHECK (monoDiff < 1e-3 && polyDiff > 1.0);
  6506. // patch pan +64 → right only; channel volume counts twice (FL applies Final.Vol in the engine and at the host)
  6507. auto right = c;
  6508. right.modelParams[38 * 12 + 1] = 64.0f;
  6509. const auto [pl, pr, pi] = render (right, { 60, 0 }, 4096);
  6510. CHECK (energy (pl, 0, 4096) < 1e-12 * energy (pr, 0, 4096) && energy (pr, 0, 4096) > 1.0);
  6511. const auto full = std::get<0> (render (tone, { 60, 0 }, 4096));
  6512. const auto half = std::get<0> (render (tone, { 60, 0 }, 4096, 0.5f, 0.5f));
  6513. CHECK (std::abs (half[100] - 0.25f * full[100]) < 1e-6f && std::abs (full[100]) > 0.01f);
  6514. }
  6515. TEST (kepler_lfo_bpm_sync)
  6516. {
  6517. // 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)
  6518. std::vector<std::uint8_t> state (0x21 + 34 * 4, 0);
  6519. auto putU32 = [&] (std::size_t o, std::uint32_t v) { std::memcpy (state.data() + o, &v, 4); };
  6520. putU32 (12, 9);
  6521. std::memcpy (state.data() + 16, "Kepler-1", 9);
  6522. putU32 (0x19, 4);
  6523. putU32 (0x1D, 34);
  6524. float k[34] {};
  6525. k[5] = 0.602085769f; // "Pads - Kepler" demo: n = 286
  6526. k[6] = 1.0f;
  6527. k[10] = 1.0f; // vibrato on
  6528. k[33] = 1.0f;
  6529. std::memcpy (state.data() + 0x21, k, sizeof k);
  6530. lynx::SynthSettings s;
  6531. float gain = 0.0f;
  6532. CHECK (lynx::synthFromKepler (state, s, gain));
  6533. CHECK (s.lfo[lynx::lfoPitch].sync && s.lfo[lynx::lfoPitch].secondsTiming);
  6534. CHECK (std::abs (s.lfo[lynx::lfoPitch].period - 286.0f / 192.0f) < 1e-6f);
  6535. k[5] = 0.0f; // slowest: 32 beats
  6536. std::memcpy (state.data() + 0x21, k, sizeof k);
  6537. CHECK (lynx::synthFromKepler (state, s, gain) && std::abs (s.lfo[lynx::lfoPitch].period - 32.0f) < 1e-6f);
  6538. k[6] = 0.0f; // free rate in seconds
  6539. std::memcpy (state.data() + 0x21, k, sizeof k);
  6540. CHECK (lynx::synthFromKepler (state, s, gain) && ! s.lfo[lynx::lfoPitch].sync);
  6541. }
  6542. namespace
  6543. {
  6544. // A Harmor state written from scratch (REA_rodada11 §4.1 layout, version 18): part A only — a decaying-harmonics
  6545. // timbre, low-pass + resonance, filter envelope, 3-voice unison, vibrato, pluck, tremolo, volume envelope with a
  6546. // velocity map — and the global block. Every other curve is neutral.
  6547. std::vector<std::uint8_t> harmorTestState (bool panLfo = true)
  6548. {
  6549. std::vector<std::uint8_t> b;
  6550. 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))); };
  6551. 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))); };
  6552. struct Pt { double dx, y, tension; int mode; };
  6553. auto curve = [&] (const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6554. i32 (3), i32 (3), i32 ((std::int32_t) pts.size());
  6555. for (const auto& p : pts)
  6556. {
  6557. f64 (p.dx), f64 (p.y);
  6558. const auto t = (float) p.tension;
  6559. std::uint32_t u;
  6560. std::memcpy (&u, &t, 4);
  6561. i32 ((std::int32_t) u);
  6562. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  6563. }
  6564. for (auto h : hdr)
  6565. i32 (h);
  6566. };
  6567. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6568. 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); };
  6569. i32 (18);
  6570. i32 (1 | 4);
  6571. std::vector<std::uint8_t> part;
  6572. std::swap (b, part);
  6573. std::array<std::int32_t, 386> p {};
  6574. const std::pair<int, int> values[] = { { 0, 500 }, { 3, 1000 }, { 9, 1024 }, { 11, 1000 }, { 12, 1000 }, { 17, 300 }, { 18, 4000 },
  6575. { 19, 500 }, { 20, 1000 }, { 24, 500 }, { 25, 500 }, { 28, 500 }, { 29, 256 }, { 30, -1 }, { 31, -1 }, { 33, -1 }, { 34, 3 },
  6576. { 35, 1 }, { 37, 192 }, { 38, 128 }, { 39, 500 }, { 40, 10000 }, { 41, 1 }, { 42, 10000 }, { 43, 1 }, { 45, 200 }, { 46, 500 },
  6577. { 47, 1 }, { 49, -140 }, { 50, 1200 }, { 51, 0 }, { 52, 5000 }, { 54, 500 }, { 56, 600 }, { 57, 300 }, { 59, 500 }, { 60, 600 },
  6578. { 64, 8192 }, { 66, 500 }, { 72, 600 }, { 74, 1000 }, { 75, 600 }, { 78, 5000 }, { 79, 500 }, { 80, 3000 } };
  6579. for (const auto& [i, v] : values)
  6580. p[(std::size_t) i] = v;
  6581. for (auto v : p)
  6582. i32 (v);
  6583. for (int i = 0; i < 12; ++i)
  6584. i32 (i); // module order
  6585. std::array<std::uint8_t, 0x4b> misc {};
  6586. auto put = [&] (int o, std::int32_t v) { std::memcpy (misc.data() + o, &v, 4); };
  6587. const float one = 1.0f;
  6588. put (0x08, 107), put (0x1c, 1), put (0x28, 200), std::memcpy (misc.data() + 0x30, &one, 4);
  6589. put (0x34, 0), put (0x38, 4), put (0x3c, 100), put (0x40, 108);
  6590. misc[0x44] = 1;
  6591. b.insert (b.end(), misc.begin(), misc.end());
  6592. 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 };
  6593. for (int k = 0; k < 43; ++k)
  6594. {
  6595. static const int mode2[] = { 2, 14, 23, 28, 30, 38, 39, 41, 42 }; // ×2 around 0.5: 1.0 is neutral
  6596. const bool centred = std::find (std::begin (mode1), std::end (mode1), k) != std::end (mode1);
  6597. const bool doubled = std::find (std::begin (mode2), std::end (mode2), k) != std::end (mode2);
  6598. const double neutral = centred && ! doubled ? 0.5 : 1.0;
  6599. i32 (3);
  6600. i32 (1065353216), i32 (1065353216), i32 (1065353216);
  6601. if (k == 1) // volume envelope: attack, decay to the sustain point, release
  6602. 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 });
  6603. else if (k == 4) // filter-1 envelope
  6604. curve ({ { 0, 1, 0, 0 }, { 0.25, 0.2, -0.5, 0 } }, { 0, 1, -1, -1, -1 });
  6605. else
  6606. flat (neutral);
  6607. for (int a : { 128, 128, 0, 128 })
  6608. i32 (a);
  6609. if (k == 0 && panLfo) // pan LFO
  6610. flat (1.0, on);
  6611. else
  6612. flat (0.5);
  6613. for (int a : { k == 0 ? 40000 : 0, 0, 0, 0 })
  6614. i32 (a);
  6615. i32 (0);
  6616. for (int m = 2; m < 11; ++m)
  6617. if (k == 1 && m == 3)
  6618. curve ({ { 0, 0.2, 0, 0 }, { 1, 1, -0.3, 0 } }, off); // velocity → volume
  6619. else
  6620. flat (neutral);
  6621. }
  6622. for (int c = 0; c < 26; ++c)
  6623. if (c == 2 || c == 3)
  6624. curve ({ { 0, 0.25, 0, 0 }, { 0.006, 0.12, -0.46, 0 }, { 0.994, 0, 0, 0 } }, off); // timbres
  6625. else if (c == 14)
  6626. curve ({ { 0, 1, 0, 0 }, { 1, 0, 0, 0 } }, off); // pluck curve
  6627. else
  6628. flat (c == 0 || c == 17 ? 0.25 : (c == 5 ? 0.0 : (c == 6 || c == 10 || c == 11 || (c >= 18 && c <= 24) ? 1.0 : 0.5)));
  6629. std::swap (b, part);
  6630. i32 ((std::int32_t) part.size());
  6631. b.insert (b.end(), part.begin(), part.end());
  6632. std::array<std::int32_t, 53> g {};
  6633. g[0] = g[1] = g[2] = 256, g[10] = 500, g[11] = 1000, g[12] = 1000, g[15] = 1000;
  6634. for (auto v : g)
  6635. i32 (v);
  6636. for (int i = 0; i < 6; ++i)
  6637. i32 (i);
  6638. std::array<std::uint8_t, 0x3d> gm {};
  6639. const std::int32_t cr = 200, vr = 1;
  6640. std::memcpy (gm.data() + 12, &vr, 4);
  6641. std::memcpy (gm.data() + 24, &cr, 4);
  6642. b.insert (b.end(), gm.begin(), gm.end());
  6643. return b;
  6644. }
  6645. } // namespace
  6646. TEST (harmor_engine)
  6647. {
  6648. // REA_rodada34: state parsing, the additive engine against our reference model (hmodel.py), host levels, voice end
  6649. const auto blob = harmorTestState();
  6650. if (const char* dump = std::getenv ("LYNX_DUMP_HARMOR"))
  6651. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  6652. CHECK (lynx::harmorStateUsable (blob));
  6653. CHECK (lynx::harmorFallbacks (blob).size() == 1); // (only the harmonic-randomness note)
  6654. const auto at = lynx::harmorParamOffset (blob, 0, 52);
  6655. std::int32_t cutoff = 0;
  6656. CHECK (at > 0 && (std::memcpy (&cutoff, blob.data() + at, 4), cutoff == 5000));
  6657. CHECK (lynx::harmorParamOffset (blob, 1, 52) < 0 && lynx::harmorParamOffset (blob, -1, 12) > at);
  6658. auto broken = blob;
  6659. broken.resize (broken.size() / 2);
  6660. CHECK (! lynx::harmorStateUsable (broken));
  6661. auto old = blob;
  6662. old[0] = 15; // articulators and curves are stored from version 16
  6663. CHECK (! lynx::harmorStateUsable (old));
  6664. lynx::ChannelDef c;
  6665. c.instrument = lynx::InstrumentKind::harmor;
  6666. c.pluginState = blob;
  6667. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  6668. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  6669. inst->configure (def, 120.0);
  6670. inst->prepare (44100.0);
  6671. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6672. inst->noteOn (60, 0.78125f);
  6673. for (int s = 0; s < total; s += 512)
  6674. {
  6675. if (s == offAt)
  6676. inst->noteOff (60);
  6677. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6678. }
  6679. return std::make_tuple (l, r, std::move (inst));
  6680. };
  6681. {
  6682. // reference model (REA round 34 hmodel.py; C5 at FL velocity 100, Final.Vol 1, centre, released at 6144, the
  6683. // Mersenne Twister seeded like LYNX's)
  6684. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  6685. const std::tuple<int, float, float> golden[] {
  6686. { 0, 0.0f, 0.0f }, { 100, 0.00947693f, 0.04249846f }, { 1000, 0.08140575f, -0.03251422f }, { 3000, 0.05928446f, -0.00247572f },
  6687. { 6000, 0.02997839f, 0.14549091f }, { 7000, -0.01121871f, 0.04197952f }, { 8191, 0.00314545f, 0.03857505f } };
  6688. for (const auto& [i, gl, gr] : golden)
  6689. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  6690. // after the release the volume envelope ends, the record declicks and the instrument goes idle
  6691. std::vector<float> tail (44100), tailR (tail.size());
  6692. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6693. CHECK (inst->idle());
  6694. CHECK (std::abs (tail.back()) == 0.0f);
  6695. }
  6696. // the reference renderer gives the same samples as the instrument
  6697. {
  6698. const auto ref = lynx::harmor::renderReference (blob, { { 0, 60, 0.78125, 1.0, 0.0, 6144 } }, 44100.0, 8192);
  6699. const auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  6700. double worst = 0.0;
  6701. for (int i = 0; i < 8192; ++i)
  6702. 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]) });
  6703. CHECK (worst == 0.0);
  6704. }
  6705. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  6706. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  6707. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  6708. CHECK (std::abs (half[1000] - 0.5f * full[1000]) < 1e-6f && std::abs (full[1000]) > 0.01f);
  6709. auto mono = c;
  6710. mono.pluginState = harmorTestState (false); // no pan LFO ...
  6711. const std::int32_t one = 1;
  6712. std::memcpy (mono.pluginState.data() + lynx::harmorParamOffset (mono.pluginState, 0, 34), &one, 4); // ... nor unison spread
  6713. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  6714. double el = 0.0, er = 0.0;
  6715. for (int i = 0; i < 2048; ++i)
  6716. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  6717. CHECK (el < 1e-10 * er && er > 1e-3);
  6718. }
  6719. namespace
  6720. {
  6721. // An Ogun state written from scratch (REA_rodada37 §1.3 layout, version 18; zlib with stored blocks): richness 4
  6722. // (255 partials), pre-decay, decay/release, fullness, Seed 1 + fixed Seed 2 with morph, 3-voice unison, a 2-stage
  6723. // low-pass with an LFO on its cutoff, a volume envelope with a sustain point and a velocity map, declick, normalisation,
  6724. // Impulse/Decay maps, and every plugin effect (EQ band, chorus, ping-pong delay, reverb). Everything else is neutral.
  6725. std::vector<std::uint8_t> ogunTestState()
  6726. {
  6727. std::vector<std::uint8_t> b;
  6728. 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))); };
  6729. auto f32v = [&] (float v) { std::uint32_t u; std::memcpy (&u, &v, 4); i32 ((std::int32_t) u); };
  6730. 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))); };
  6731. struct Pt { double dx, y, tension; int mode; };
  6732. auto curve = [&] (int type, const std::vector<Pt>& pts, std::array<std::int32_t, 5> hdr) {
  6733. i32 (type), i32 (3), i32 ((std::int32_t) pts.size());
  6734. for (const auto& p : pts)
  6735. {
  6736. f64 (p.dx), f64 (p.y), f32v ((float) p.tension);
  6737. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  6738. }
  6739. for (auto h : hdr)
  6740. i32 (h);
  6741. };
  6742. const std::array<std::int32_t, 5> off { 0, 0, -1, -1, -1 }, on { 0, 1, -1, -1, -1 };
  6743. auto flat = [&] (int type, double y) { curve (type, { { 0, y, 0, 0 }, { 1, y, 0, 0 } }, off); };
  6744. std::array<std::int32_t, 64> P {};
  6745. const std::pair<int, int> values[] = { { 0, 128 }, { 5, 300 }, { 6, 3000 }, { 7, 2000 }, { 8, 200 }, { 9, 500 }, { 10, 12 },
  6746. { 11, 34 }, { 12, 3 }, { 13, 64 }, { 14, 64 }, { 15, 40 }, { 16, 64 }, { 17, 180 }, { 18, 60 }, { 19, 2 }, { 20, 300 },
  6747. { 21, 400 }, { 22, 40 }, { 23, 30 }, { 24, 20 }, { 25, 64 }, { 26, 40 }, { 27, 96 }, { 28, 100 }, { 29, 64 }, { 30, 2 },
  6748. { 31, 19 }, { 32, 221 }, { 33, 0 }, { 34, 50 }, { 35, 100 }, { 36, 20 }, { 37, 60 }, { 38, 2 }, { 39, 64 }, { 40, 300 },
  6749. { 43, 30000 }, { 46, 20000 }, { 49, 0 }, { 50, 2 }, { 51, 0 }, { 52, 9 }, { 53, 1 }, { 54, 0 },
  6750. { 55, 0x01000000 }, // +0x20b: pitch follows the key
  6751. { 56, 0x00010000 }, // +0x20e: declick
  6752. { 62, 0x00000100 } }; // +0x225: normalise
  6753. for (const auto& [i, v] : values)
  6754. P[(std::size_t) i] = v;
  6755. for (auto v : P)
  6756. i32 (v);
  6757. b.push_back (0), b.push_back (1), b.push_back (0); // delay: not tempo-based, on
  6758. i32 (1), i32 (1), i32 (1); // EQ band types: peaking
  6759. b.push_back (0); // comment: 0 characters
  6760. for (int k = 0; k < 13; ++k)
  6761. {
  6762. static constexpr int sum[] = { 1, 2, 3, 9, 11, 12 };
  6763. const double neutral = std::find (std::begin (sum), std::end (sum), k) != std::end (sum) ? 0.5 : 1.0;
  6764. i32 (1);
  6765. f32v (0.0f), f32v (1.0f), f32v (k == 1 ? 0.3f : 0.0f);
  6766. if (k == 0) // volume envelope: attack, decay to the sustain point, release
  6767. 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 });
  6768. else
  6769. flat (1, neutral);
  6770. for (int a : { 128, 128, 128, 128 })
  6771. i32 (a);
  6772. if (k == 1) // cutoff LFO
  6773. curve (2, { { 0, 0.8, 0, 0 } }, on);
  6774. else
  6775. flat (2, 0.5);
  6776. for (int a : { k == 1 ? 40000 : 0, 0, 0, 0 })
  6777. i32 (a);
  6778. i32 (0);
  6779. for (int m = 0; m < 4; ++m)
  6780. if (k == 0 && m == 1)
  6781. curve (3, { { 0, 0.3, 0, 0 }, { 1, 1, -0.2, 0 } }, off); // velocity → volume
  6782. else
  6783. flat (3, neutral);
  6784. }
  6785. flat (3, 0.0); // Forced: none
  6786. curve (3, { { 0, 0.6, 0, 0 }, { 1, 0.4, 0.3, 0 } }, off); // Impulse EQ
  6787. curve (3, { { 0, 0.5, 0, 0 }, { 1, 0.3, 0, 0 } }, off); // Decay EQ
  6788. flat (3, 0.5); // Release EQ
  6789. // u32 version + zlib (stored blocks)
  6790. std::vector<std::uint8_t> s { 18, 0, 0, 0, 0x78, 0x01 };
  6791. for (std::size_t i = 0; i < b.size();)
  6792. {
  6793. const auto len = std::min<std::size_t> (65535, b.size() - i);
  6794. s.push_back (i + len >= b.size() ? 1 : 0);
  6795. s.push_back ((std::uint8_t) len), s.push_back ((std::uint8_t) (len >> 8));
  6796. s.push_back ((std::uint8_t) ~len), s.push_back ((std::uint8_t) (~len >> 8));
  6797. s.insert (s.end(), b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + len));
  6798. i += len;
  6799. }
  6800. std::uint32_t a1 = 1, a2 = 0;
  6801. for (auto c : b)
  6802. a1 = (a1 + c) % 65521u, a2 = (a2 + a1) % 65521u;
  6803. for (int sh = 24; sh >= 0; sh -= 8)
  6804. s.push_back ((std::uint8_t) (((a2 << 16) | a1) >> sh));
  6805. return s;
  6806. }
  6807. } // namespace
  6808. TEST (ogun_engine)
  6809. {
  6810. // REA_rodada37: state parsing, the engine against our reference model (ogmodel.py), host levels, export quality
  6811. const auto blob = ogunTestState();
  6812. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN"))
  6813. std::ofstream (dump, std::ios::binary).write ((const char*) blob.data(), (std::streamsize) blob.size());
  6814. CHECK (lynx::ogunStateUsable (blob));
  6815. CHECK (lynx::ogunFallbacks (blob).empty());
  6816. CHECK (lynx::ogunParam (blob, 17) == 180 && lynx::ogunParam (blob, 52) == 9 && ! lynx::ogunParam (blob, 64));
  6817. auto broken = blob;
  6818. broken.resize (broken.size() - 300);
  6819. CHECK (! lynx::ogunStateUsable (broken));
  6820. auto old = blob;
  6821. old[0] = 13; // the articulator layout is from version 14
  6822. CHECK (! lynx::ogunStateUsable (old));
  6823. // reference model (REA round 37 ogmodel.Plugin(state).run: C5 velocity 0.75 released at 6144, G5 velocity 0.625 at 2048
  6824. // released at 8192; host block 512)
  6825. 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 } };
  6826. const auto ref = lynx::ogun::renderReference (blob, notes, 44100.0, 16384);
  6827. CHECK (ref.size() == 2 * 16384);
  6828. if (const char* dump = std::getenv ("LYNX_DUMP_OGUN_OUT"))
  6829. std::ofstream (dump, std::ios::binary).write ((const char*) ref.data(), (std::streamsize) (ref.size() * 4));
  6830. const std::tuple<int, float, float> golden[] {
  6831. { 100, -7.763202e-05f, -8.919523e-05f }, { 1000, 0.06195267f, 0.03858230f }, { 3000, 0.01363476f, -0.00378433f },
  6832. { 6000, -0.06601223f, -0.03084030f }, { 9000, 0.04204506f, 0.02452896f }, { 12000, 0.03420147f, -0.01837870f },
  6833. { 16383, -0.00465311f, -0.00119627f } };
  6834. for (const auto& [i, gl, gr] : golden)
  6835. CHECK (std::abs (ref[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  6836. // the instrument plays like the reference renderer (Final.Vol 1, centre)
  6837. lynx::ChannelDef c;
  6838. c.instrument = lynx::InstrumentKind::ogun;
  6839. c.pluginState = blob;
  6840. auto render = [&] (const lynx::ChannelDef& def, int total, float gl, float gr, bool held = false) {
  6841. auto inst = lynx::makeInstrument (lynx::InstrumentKind::ogun);
  6842. inst->configure (def, 120.0);
  6843. inst->prepare (44100.0);
  6844. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  6845. for (int s = 0; s < total; s += 512)
  6846. {
  6847. if (s == 0)
  6848. inst->noteOn (60, 0.75f);
  6849. if (s == 2048)
  6850. inst->noteOn (67, 0.625f);
  6851. if (s == 6144 && ! held)
  6852. inst->noteOff (60);
  6853. if (s == 8192 && ! held)
  6854. inst->noteOff (67);
  6855. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  6856. }
  6857. return std::make_tuple (l, r, std::move (inst));
  6858. };
  6859. {
  6860. auto [l, r, inst] = render (c, 16384, 1.0f, 1.0f);
  6861. double worst = 0.0;
  6862. for (int i = 0; i < 16384; ++i)
  6863. 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]) });
  6864. CHECK (worst == 0.0);
  6865. // the voices end and the effect tails die out: the instrument goes idle
  6866. std::vector<float> tail (44100), tailR (tail.size());
  6867. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  6868. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  6869. CHECK (inst->idle());
  6870. }
  6871. // FL's level law applied once (effects off): half the channel volume halves the output once the gain ramps
  6872. // (limited to 0.001 per sample) have settled on the sustain level; full right pan silences the left side
  6873. {
  6874. auto dry = c;
  6875. for (int i : { 19, 29, 40, 53 })
  6876. CHECK (lynx::ogunSetParam (dry.pluginState, i, 0)); // chorus, delay wet, EQ band and reverb off
  6877. CHECK (lynx::ogunStateUsable (dry.pluginState) && lynx::ogunParam (dry.pluginState, 19) == 0);
  6878. const auto full = std::get<0> (render (dry, 32768, 1.0f, 1.0f, true));
  6879. const auto half = std::get<0> (render (dry, 32768, 0.5f, 0.5f, true));
  6880. double worst = 0.0, peak = 0.0;
  6881. for (int i = 30000; i < 32768; ++i)
  6882. worst = std::max (worst, (double) std::abs (half[(std::size_t) i] - 0.5f * full[(std::size_t) i])),
  6883. peak = std::max (peak, (double) std::abs (full[(std::size_t) i]));
  6884. CHECK (worst <= 1e-7 * peak && peak > 1e-3);
  6885. // FL's default channel volume (100/128): FL hands Ogun Final.Vol = (11^v − 1)/10 (REA_rodada28 §4.1); LYNX's
  6886. // centred channel gains (√2·V·cos(π/4) each) give the reference model's level at that Final.Vol
  6887. const auto V = lynx::channelGain (0.78125f);
  6888. const auto g = V * std::cos (0.25f * 3.14159265f) * 1.41421356f;
  6889. const auto atDefault = std::get<0> (render (dry, 32768, g, g, true));
  6890. const auto refDefault = lynx::ogun::renderReference (dry.pluginState, { { 0, 60, 0.75, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 },
  6891. { 2048, 67, 0.625, V, 0.0, 0.0, 0.0, 0.0, 0.5, -1 } }, 44100.0, 32768);
  6892. double ei = 0.0, er0 = 0.0;
  6893. for (int i = 30000; i < 32768; ++i)
  6894. 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];
  6895. CHECK (er0 > 0.0 && std::abs (10.0 * std::log10 (ei / er0)) < 0.001);
  6896. const auto [pl, pr, pi] = render (dry, 4096, 0.0f, 1.41421356f);
  6897. double el = 0.0, er = 0.0;
  6898. for (int i = 0; i < 4096; ++i)
  6899. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  6900. CHECK (el < 1e-10 * er && er > 1e-3);
  6901. }
  6902. // export: FL renders Ogun at quality 4 (×2 rate, 32-point reader), LYNX too while it renders offline
  6903. {
  6904. lynx::setOfflineRendering (true);
  6905. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f);
  6906. lynx::setOfflineRendering (false);
  6907. lynx::ogun::RefOptions q4;
  6908. q4.quality = 4;
  6909. const auto ref4 = lynx::ogun::renderReference (blob, notes, 44100.0, 8192, q4);
  6910. double worst = 0.0, diff = 0.0;
  6911. for (int i = 0; i < 8192; ++i)
  6912. worst = std::max (worst, (double) std::abs (ref4[2 * (std::size_t) i] - l[(std::size_t) i])),
  6913. diff = std::max (diff, (double) std::abs (ref[2 * (std::size_t) i] - l[(std::size_t) i]));
  6914. CHECK (worst == 0.0 && diff > 1e-3);
  6915. const std::tuple<int, float, float> golden4[] {
  6916. { 100, 0.00605961f, 0.00628969f }, { 1000, 0.03729841f, 0.01727954f }, { 3000, -0.09010383f, -0.02551294f },
  6917. { 6000, 0.02167798f, -0.01115632f }, { 8191, 0.00320288f, -0.00615792f } };
  6918. for (const auto& [i, gl, gr] : golden4)
  6919. CHECK (std::abs (ref4[2 * (std::size_t) i] - gl) < 2e-6f && std::abs (ref4[2 * (std::size_t) i + 1] - gr) < 2e-6f);
  6920. }
  6921. // feature variants of the same state against the reference model (energy per side over 8192 samples, three
  6922. // samples; C1 released at 6144): qualities, richness, unison, the filter types, ALT DECAY, 1/√h, Seed 2 Random,
  6923. // infinite decay, logarithmic axis, velocity → position, other rates. Goldens from ogmodel.py (REA round 37).
  6924. {
  6925. struct Sample { int at; float l, r; };
  6926. struct Variant { const char* name; std::vector<std::pair<int, int>> params; int rate; double el, er; Sample s[3]; };
  6927. const Variant variants[] {
  6928. { "q1", { { 54, 1 } }, 44100, 11.0970402, 6.87820714, { { 1500, 0.0240363069f, -0.0148524037f }, { 4000, 0.0873190612f, -0.000715861097f }, { 7000, -0.0378603041f, -0.00338819111f } } },
  6929. { "q2", { { 54, 2 } }, 44100, 11.2405446, 6.68045082, { { 1500, 0.0448660068f, -0.00560745923f }, { 4000, -0.0122657465f, 0.0104285963f }, { 7000, -0.0216034129f, -0.0134632047f } } },
  6930. { "q3", { { 54, 3 } }, 44100, 11.3200608, 6.86196449, { { 1500, -0.0031537537f, -0.0242648814f }, { 4000, 0.0426301025f, 0.0364697985f }, { 7000, 0.00843803957f, 0.0302239582f } } },
  6931. { "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 } } },
  6932. { "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 } } },
  6933. { "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 } } },
  6934. { "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 } } },
  6935. { "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 } } },
  6936. { "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 } } },
  6937. { "alt", { { 62, 257 } }, 44100, 17.2485711, 9.99223979, { { 1500, -0.0319156498f, -0.0186850764f }, { 4000, -0.0769012421f, -0.0375530645f }, { 7000, 0.0310976505f, 0.0445244014f } } },
  6938. { "isqrt", { { 62, 65792 } }, 44100, 45.7309307, 27.1224593, { { 1500, -0.04376591f, -0.00845711026f }, { 4000, 0.0351300463f, -0.129557073f }, { 7000, 0.0096148327f, 0.00123740174f } } },
  6939. { "seed2rnd", { { 11, 0 } }, 44100, 16.8228819, 11.5433061, { { 1500, -0.0117614279f, -0.026497297f }, { 4000, 0.00265733898f, -0.07069581f }, { 7000, 0.00576663949f, 0.0565217957f } } },
  6940. { "decinf", { { 6, 10000 } }, 44100, 19.1879226, 11.0487311, { { 1500, -0.0324105881f, -0.0190765262f }, { 4000, -0.0809780285f, -0.0395730771f }, { 7000, 0.034197174f, 0.048585102f } } },
  6941. { "logx", { { 57, 16777216 } }, 44100, 12.3064326, 7.31519289, { { 1500, -0.0438990779f, 0.0096983267f }, { 4000, -0.0175748877f, 0.0313935578f }, { 7000, 0.0639200211f, 0.024419222f } } },
  6942. { "velpos", { { 56, 66304 } }, 44100, 4.9966925, 3.16540662, { { 1500, 0.0083851777f, -0.0165472236f }, { 4000, 0.0630451962f, 0.0156494901f }, { 7000, -0.0293043777f, -0.00182877108f } } },
  6943. { "fs48", { }, 48000, 10.8813888, 6.53592373, { { 1500, -0.0485184453f, -0.0319588967f }, { 4000, 0.0230421089f, -0.0286604818f }, { 7000, -0.0117630213f, -0.017459048f } } },
  6944. { "fs96", { }, 96000, 11.0189683, 6.39821883, { { 1500, -0.0267860368f, -0.0227564126f }, { 4000, 0.0238709711f, -0.0100849615f }, { 7000, -0.0034619756f, 0.0451687202f } } },
  6945. };
  6946. for (const auto& v : variants)
  6947. {
  6948. auto st = blob;
  6949. for (const auto& [i, x] : v.params)
  6950. CHECK (lynx::ogunSetParam (st, i, x));
  6951. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 0.75, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6144 },
  6952. { 2048, 67, 0.625, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1 } }, (double) v.rate, 8192);
  6953. double el = 0.0, er = 0.0, peak = 0.0;
  6954. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  6955. 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]) });
  6956. bool ok = y.size() == 2 * 8192 && std::abs (el - v.el) <= 1e-5 * v.el && std::abs (er - v.er) <= 1e-5 * v.er;
  6957. for (const auto& smp : v.s)
  6958. 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;
  6959. if (! ok)
  6960. std::printf (" Ogun variant %s differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", v.name, el, er, v.el, v.er);
  6961. CHECK (ok);
  6962. }
  6963. }
  6964. }
  6965. // our own Maximus v7 state (layout REA_rodada8 §1.1): 7 globals, 4 × 13 band parameters, 8 envelopes (dx, y, tension, type)
  6966. static std::vector<std::uint8_t> maximusTestState (const std::array<int, 7>& g, const std::array<std::array<int, 13>, 4>& bands,
  6967. const std::array<std::vector<std::array<double, 4>>, 8>& envs, int oversampling)
  6968. {
  6969. std::vector<std::uint8_t> d;
  6970. auto put = [&] (const void* v, std::size_t n) { d.insert (d.end(), (const std::uint8_t*) v, (const std::uint8_t*) v + n); };
  6971. auto i32 = [&] (std::int32_t v) { put (&v, 4); };
  6972. i32 (7);
  6973. for (int copy = 0; copy < 2; ++copy)
  6974. {
  6975. for (auto v : g)
  6976. i32 (v);
  6977. for (const auto& b : bands)
  6978. for (auto v : b)
  6979. i32 (v);
  6980. }
  6981. d.resize (0x1fe, 0);
  6982. std::memcpy (d.data() + 0x1f4, &oversampling, 4);
  6983. for (const auto& e : envs)
  6984. {
  6985. i32 (3), i32 (2), i32 ((std::int32_t) e.size());
  6986. for (const auto& pt : e)
  6987. {
  6988. const double dx = pt[0], y = pt[1];
  6989. const float t = (float) pt[2];
  6990. put (&dx, 8), put (&y, 8), put (&t, 4);
  6991. const std::uint8_t flags[4] { (std::uint8_t) pt[3], 0, 0, 0 };
  6992. put (flags, 4);
  6993. }
  6994. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  6995. }
  6996. return d;
  6997. }
  6998. // A "Kepler-1" state written from scratch (REA_rodada46 §1.1 layout, version 2): the default panel values
  6999. std::vector<std::uint8_t> keplerTestState()
  7000. {
  7001. 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 };
  7002. std::vector<std::uint8_t> st;
  7003. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) st.push_back ((std::uint8_t) (v >> (8 * i))); };
  7004. auto f32 = [&] (float f) { std::uint32_t v; std::memcpy (&v, &f, 4); u32 (v); };
  7005. u32 (0), u32 (0), u32 (0), u32 (9);
  7006. for (char c : std::string ("Kepler-1"))
  7007. st.push_back ((std::uint8_t) c);
  7008. st.push_back (0);
  7009. u32 (2), u32 (34);
  7010. for (auto f : panel)
  7011. f32 (f);
  7012. st.push_back (0);
  7013. f32 (1.0f), f32 (0.5f);
  7014. const auto size = (std::uint32_t) st.size() - 12;
  7015. std::memcpy (st.data() + 4, &size, 4);
  7016. return st;
  7017. }
  7018. std::string keplerDll()
  7019. {
  7020. if (realFlpDir.empty())
  7021. return {};
  7022. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Kepler" / "engine_x64.dll").string();
  7023. }
  7024. TEST (kepler_engine)
  7025. {
  7026. const auto st = keplerTestState();
  7027. CHECK (st.size() == 178 && lynx::keplerStateUsable (st));
  7028. CHECK (lynx::keplerPanelOffset (st, 19) == 0x21 + 4 * 19 && lynx::keplerPanelOffset (st, 34) == -1);
  7029. auto broken = st;
  7030. broken[17] = 'X'; // not "Kepler-1"
  7031. CHECK (! lynx::keplerStateUsable (broken) && lynx::keplerPanelOffset (broken, 0) == -1);
  7032. auto v3 = st;
  7033. v3[0x19] = 3; // version 3 needs the gate / noise / oversampling bytes
  7034. CHECK (! lynx::keplerStateUsable (v3));
  7035. const auto dll = keplerDll();
  7036. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  7037. return; // no FL installation (the engine needs the user's own DLL tables)
  7038. // against FL's engine DLL (EXEC, REA_rodada46 §6): three notes, 44.1 kHz, host blocks of 512 — bit-exact
  7039. 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 } };
  7040. const auto y = lynx::kepler::renderReference (st, dll, notes, 44100.0, 39690);
  7041. CHECK (y.size() == 2 * 39690);
  7042. if (y.size() != 2 * 39690)
  7043. return;
  7044. double el = 0.0, er = 0.0;
  7045. for (std::size_t i = 0; i < y.size(); i += 2)
  7046. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7047. std::printf (" Kepler default patch: energy %.9g %.9g (FL's DLL 250.641268)\n", el, er);
  7048. CHECK (std::abs (el - 250.641268) < 1e-6 && std::abs (er - 250.641268) < 1e-6);
  7049. const std::tuple<int, float, float> golden[] { { 5000, 0.0467273332f, 0.0467273332f }, { 12000, -0.119412214f, -0.119412214f },
  7050. { 25000, 0.0855825394f, 0.0855825394f }, { 35000, -3.34166579e-08f, -3.34166579e-08f } };
  7051. for (const auto& [i, l, r] : golden)
  7052. CHECK (y[2 * (std::size_t) i] == l && y[2 * (std::size_t) i + 1] == r);
  7053. // the instrument: FL's wrapper velocity, channel volume as an output gain, idle after the release
  7054. lynx::ChannelDef c;
  7055. c.instrument = lynx::InstrumentKind::kepler;
  7056. c.pluginState = st;
  7057. c.samplePath = dll;
  7058. auto inst = lynx::makeInstrument (lynx::InstrumentKind::kepler);
  7059. inst->configure (c, 120.0);
  7060. inst->prepare (44100.0);
  7061. std::vector<float> l (4096), r (4096);
  7062. inst->noteOn (60, 0.8f);
  7063. double e = 0.0;
  7064. bool finite = true;
  7065. for (int b = 0; b < 8; ++b)
  7066. {
  7067. std::fill (l.begin(), l.end(), 0.0f);
  7068. std::fill (r.begin(), r.end(), 0.0f);
  7069. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7070. for (int i = 0; i < 4096; ++i)
  7071. 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];
  7072. if (b == 3)
  7073. inst->noteOff (60);
  7074. }
  7075. CHECK (finite && e > 1.0);
  7076. for (int b = 0; b < 40; ++b)
  7077. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7078. CHECK (inst->idle());
  7079. }
  7080. // REA round 57 §2: Transistor Bass
  7081. // the plugin state (event 0xD5) of an FL preset file (.fst: the FLhd/FLdt event stream)
  7082. std::vector<std::uint8_t> tbassPresetState (const char* name)
  7083. {
  7084. if (realFlpDir.empty())
  7085. return {};
  7086. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Transistor Bass" / name, std::ios::binary);
  7087. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7088. std::size_t i = 0;
  7089. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7090. ++i;
  7091. i += 8;
  7092. while (i < b.size())
  7093. {
  7094. const auto id = b[i++];
  7095. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7096. if (id >= 192)
  7097. for (int shift = 0; i < b.size(); shift += 7)
  7098. {
  7099. const auto c = b[i++];
  7100. n |= (std::size_t) (c & 0x7F) << shift;
  7101. if (c < 0x80)
  7102. break;
  7103. }
  7104. if (id == 0xD5 && i + n <= b.size())
  7105. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7106. i += n;
  7107. }
  7108. return {};
  7109. }
  7110. // the same state re-saved as engine-stream version 5 with an engine selector (REA_rodada57 §2.2)
  7111. std::vector<std::uint8_t> tbassWithEngine (const std::vector<std::uint8_t>& s, std::int32_t engine)
  7112. {
  7113. std::vector<std::uint8_t> out (s.begin(), s.begin() + 4);
  7114. auto put = [&] (const void* p, std::size_t n) { out.insert (out.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  7115. for (std::size_t at = 4; at + 12 <= s.size();)
  7116. {
  7117. std::uint32_t id;
  7118. std::uint64_t n;
  7119. std::memcpy (&id, s.data() + at, 4);
  7120. std::memcpy (&n, s.data() + at + 4, 8);
  7121. std::vector<std::uint8_t> d (s.begin() + (std::ptrdiff_t) (at + 12), s.begin() + (std::ptrdiff_t) (at + 12 + n));
  7122. at += 12 + (std::size_t) n;
  7123. if (id == 2)
  7124. {
  7125. std::uint64_t m;
  7126. std::memcpy (&m, d.data() + 4, 8);
  7127. std::vector<std::uint8_t> e (d.begin() + 12, d.begin() + 12 + (std::ptrdiff_t) m);
  7128. std::int16_t v;
  7129. std::memcpy (&v, e.data(), 2);
  7130. if (v < 5)
  7131. {
  7132. v = 5;
  7133. std::memcpy (e.data(), &v, 2);
  7134. e.insert (e.end(), 4, 0);
  7135. }
  7136. std::memcpy (e.data() + e.size() - 4, &engine, 4);
  7137. m = e.size();
  7138. d.resize (12);
  7139. std::memcpy (d.data() + 4, &m, 8);
  7140. d.insert (d.end(), e.begin(), e.end());
  7141. }
  7142. const std::uint64_t size = d.size();
  7143. put (&id, 4);
  7144. put (&size, 8);
  7145. put (d.data(), d.size());
  7146. }
  7147. return out;
  7148. }
  7149. std::string tbassDll()
  7150. {
  7151. if (realFlpDir.empty())
  7152. return {};
  7153. return (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Transistor Bass" / "engine_x64.dll").string();
  7154. }
  7155. // Plucked! (REA_rodada65 §1): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7156. // note, the host killing voices when the plugin asks). LYNX renders these bit-identically to the DLL.
  7157. TEST (plucked_engine)
  7158. {
  7159. auto stateOf = [] (std::initializer_list<std::int32_t> k) {
  7160. std::vector<std::uint8_t> st (20);
  7161. std::memcpy (st.data(), std::data (k), 20);
  7162. return st;
  7163. };
  7164. CHECK (! lynx::pluckedStateUsable (std::vector<std::uint8_t> (19)));
  7165. CHECK (lynx::pluckedStateUsable (stateOf ({ 128, 64, 1, 0, 1 })));
  7166. CHECK (lynx::pluckedParams (stateOf ({ 7, 9, 0, 1, 0 })) == std::vector<int> ({ 7, 9, 0, 1, 0 }));
  7167. using N = lynx::plucked::RefNote;
  7168. struct Sample { int at; float l, r; };
  7169. struct Golden { int set; std::int32_t P[5]; double fs; int block, law, maxPoly; double el, er; Sample s[4]; };
  7170. const std::vector<std::vector<N>> sets {
  7171. { { 0, 60, 1.0, 0.0, 0.0, 0.0, 0.0, -1 } },
  7172. { { 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 } },
  7173. { { 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 } },
  7174. { { 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 } },
  7175. { { 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 } },
  7176. };
  7177. const Golden goldens[] {
  7178. { 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 } } },
  7179. { 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 } } },
  7180. { 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 } } },
  7181. { 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 } } },
  7182. { 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 } } },
  7183. };
  7184. for (const auto& g : goldens)
  7185. {
  7186. const auto st = stateOf ({ g.P[0], g.P[1], g.P[2], g.P[3], g.P[4] });
  7187. const auto length = (int) g.fs * 2;
  7188. const auto y = lynx::plucked::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 777u + (std::uint32_t) g.set,
  7189. g.law != 0, g.maxPoly);
  7190. double el = 0.0, er = 0.0;
  7191. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7192. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7193. 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;
  7194. for (const auto& smp : g.s)
  7195. 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;
  7196. if (! ok)
  7197. 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);
  7198. CHECK (ok);
  7199. }
  7200. // the instrument: plays, the gate makes a note-off silence the string, a held string dies out by itself
  7201. lynx::ChannelDef c;
  7202. c.instrument = lynx::InstrumentKind::plucked;
  7203. c.pluginState = stateOf ({ 200, 40, 1, 1, 1 });
  7204. auto inst = lynx::makeInstrument (lynx::InstrumentKind::plucked);
  7205. inst->configure (c, 120.0);
  7206. inst->prepare (44100.0);
  7207. std::vector<float> l (1024), r (1024);
  7208. inst->noteOn (57, 0.8f);
  7209. double e = 0.0;
  7210. for (int b = 0; b < 8; ++b)
  7211. {
  7212. std::fill (l.begin(), l.end(), 0.0f);
  7213. std::fill (r.begin(), r.end(), 0.0f);
  7214. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7215. for (int i = 0; i < 1024; ++i)
  7216. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7217. }
  7218. CHECK (e > 1.0 && ! inst->idle());
  7219. inst->noteOff (57);
  7220. for (int b = 0; b < 4; ++b)
  7221. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7222. CHECK (inst->idle());
  7223. c.pluginState = stateOf ({ 0, 64, 0, 0, 0 });
  7224. inst->configure (c, 120.0);
  7225. inst->noteOn (60, 1.0f);
  7226. inst->noteOff (60); // (no gate: the string rings on)
  7227. for (int b = 0; b < 2000 && ! inst->idle(); ++b)
  7228. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7229. CHECK (inst->idle());
  7230. }
  7231. // BeepMap (REA_rodada65 §2): the C++ engine against FL's DLL (EXEC: same notes, the DLL's RandSeed set before the first
  7232. // note, the host killing voices when the plugin asks): a synthetic 9×6 bitmap with every Scale mode and switch, and two
  7233. // of the installation's presets. LYNX renders these bit-identically to the DLL.
  7234. std::vector<std::uint8_t> beepMapTestState (std::initializer_list<std::int32_t> settings)
  7235. {
  7236. constexpr int W = 9, H = 6;
  7237. std::vector<std::uint8_t> img (54 + 4 * W * H, 0);
  7238. 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)); };
  7239. img[0] = 'B', img[1] = 'M';
  7240. 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);
  7241. put (34, 4 * W * H, 4);
  7242. for (int y = 0; y < H; ++y) // bottom-up rows
  7243. for (int x = 0; x < W; ++x)
  7244. {
  7245. const auto yy = H - 1 - y;
  7246. auto* px = img.data() + 54 + 4 * (y * W + x);
  7247. px[0] = (std::uint8_t) ((x * 91 + yy * 29) & 255), px[1] = (std::uint8_t) ((x * 13 + yy * 71) & 255);
  7248. px[2] = (std::uint8_t) ((x * 37 + yy * 11) & 255), px[3] = 255;
  7249. }
  7250. std::vector<std::uint8_t> st (4 + 60 + 4, 0);
  7251. std::size_t k = 0;
  7252. for (auto v : settings)
  7253. std::memcpy (st.data() + 4 + 4 * k++, &v, 4);
  7254. const auto len = (std::int32_t) img.size();
  7255. std::memcpy (st.data() + 64, &len, 4);
  7256. st.insert (st.end(), img.begin(), img.end());
  7257. return st;
  7258. }
  7259. // the 0xD5 state of one of the installation's BeepMap presets (empty without an installation)
  7260. std::vector<std::uint8_t> beepMapPresetState (const char* name)
  7261. {
  7262. if (realFlpDir.empty())
  7263. return {};
  7264. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "BeepMap" / name, std::ios::binary);
  7265. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7266. std::size_t i = 0;
  7267. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7268. ++i;
  7269. i += 8;
  7270. while (i < b.size())
  7271. {
  7272. const auto id = b[i++];
  7273. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7274. if (id >= 192)
  7275. for (int shift = 0; i < b.size(); shift += 7)
  7276. {
  7277. const auto c = b[i++];
  7278. n |= (std::size_t) (c & 0x7F) << shift;
  7279. if (c < 0x80)
  7280. break;
  7281. }
  7282. if (id == 0xD5 && i + n <= b.size())
  7283. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7284. i += n;
  7285. }
  7286. return {};
  7287. }
  7288. TEST (beepmap_engine)
  7289. {
  7290. const auto synthA = beepMapTestState ({ 3000, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 });
  7291. CHECK (lynx::beepMapStateUsable (synthA));
  7292. CHECK (lynx::beepMapParams (synthA)->at (1) == 200 && lynx::beepMapParamOffset (synthA, 2) == 12);
  7293. auto broken = synthA;
  7294. broken[0] = 2; // versions above 1 are refused
  7295. CHECK (! lynx::beepMapStateUsable (broken));
  7296. broken = synthA;
  7297. broken.resize (broken.size() - 20); // truncated bitmap
  7298. CHECK (! lynx::beepMapStateUsable (broken));
  7299. using N = lynx::beepmap::RefNote;
  7300. struct Sample { int at; float l, r; };
  7301. struct Golden { int set; double fs; int block; double el, er; Sample s[4]; };
  7302. std::vector<std::vector<std::uint8_t>> states {
  7303. synthA,
  7304. beepMapTestState ({ 5000, 600, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7305. beepMapTestState ({ 8000, 100, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7306. beepMapTestState ({ 1000, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 64 }),
  7307. beepMapPresetState ("Default.fst"),
  7308. beepMapPresetState ("Voice.fst"),
  7309. };
  7310. const std::vector<std::vector<N>> sets {
  7311. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  7312. { { 0, 57, 0.8, 0.3, 25.0, -1 }, { 7000, 64, 0.6, -0.5, 0.0, -1 } },
  7313. { { 0, 48, 1.0, 0.0, 0.0, -1 }, { 300, 72, 0.5, 0.0, -40.0, -1 } },
  7314. { { 0, 67, 1.0, -0.2, 0.0, -1 } },
  7315. { { 0, 60, 1.0, 0.0, 0.0, -1 } },
  7316. { { 0, 55, 0.8, 0.0, 0.0, -1 }, { 20000, 62, 0.8, 0.4, 0.0, -1 } },
  7317. };
  7318. const Golden goldens[] {
  7319. { 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 } } },
  7320. { 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 } } },
  7321. { 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 } } },
  7322. { 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 } } },
  7323. { 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 } } },
  7324. { 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 } } },
  7325. };
  7326. for (const auto& g : goldens)
  7327. {
  7328. const auto& st = states[(std::size_t) g.set];
  7329. if (st.empty())
  7330. continue; // (no FL installation: the presets are skipped)
  7331. const auto length = (int) g.fs * 2;
  7332. const auto y = lynx::beepmap::renderReference (st, sets[(std::size_t) g.set], g.fs, length, g.block, 4321u + (std::uint32_t) g.set);
  7333. double el = 0.0, er = 0.0;
  7334. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7335. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  7336. 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;
  7337. for (const auto& smp : g.s)
  7338. 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;
  7339. if (! ok)
  7340. 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);
  7341. CHECK (ok);
  7342. }
  7343. // the instrument: plays the image, ends after the last column, a note-off fades the voice out
  7344. lynx::ChannelDef c;
  7345. c.instrument = lynx::InstrumentKind::beepmap;
  7346. c.pluginState = states[1];
  7347. auto inst = lynx::makeInstrument (lynx::InstrumentKind::beepmap);
  7348. inst->configure (c, 120.0);
  7349. inst->prepare (44100.0);
  7350. std::vector<float> l (1024), r (1024);
  7351. inst->noteOn (60, 0.8f);
  7352. double e = 0.0;
  7353. for (int b = 0; b < 8; ++b)
  7354. {
  7355. std::fill (l.begin(), l.end(), 0.0f);
  7356. std::fill (r.begin(), r.end(), 0.0f);
  7357. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7358. for (int i = 0; i < 1024; ++i)
  7359. e += (double) l[(std::size_t) i] * l[(std::size_t) i] + (double) r[(std::size_t) i] * r[(std::size_t) i];
  7360. }
  7361. CHECK (e > 0.01 && ! inst->idle()); // (Loop on)
  7362. inst->noteOff (60);
  7363. for (int b = 0; b < 4; ++b)
  7364. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7365. CHECK (inst->idle());
  7366. c.pluginState = synthA; // (no loop: 9 columns of 64·(e^(200/4096·ln 33) − 1)… samples, then the voice ends)
  7367. inst->configure (c, 120.0);
  7368. inst->noteOn (60, 1.0f);
  7369. for (int b = 0; b < 200 && ! inst->idle(); ++b)
  7370. inst->render (l.data(), r.data(), 1024, 0.7f, 0.7f);
  7371. CHECK (inst->idle());
  7372. }
  7373. // ---- Sawer (REA_rodada65 §3): installation presets and data.saw read at run time, fingerprints of engine_x64.dll ----
  7374. namespace
  7375. {
  7376. std::vector<std::uint8_t> sawerPresetState (const char* rel)
  7377. {
  7378. if (realFlpDir.empty())
  7379. return {};
  7380. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Sawer" / rel, std::ios::binary);
  7381. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7382. std::size_t i = 0;
  7383. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  7384. ++i;
  7385. i += 8;
  7386. while (i < b.size())
  7387. {
  7388. const auto id = b[i++];
  7389. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  7390. if (id >= 192)
  7391. for (int shift = 0; i < b.size(); shift += 7)
  7392. {
  7393. const auto c = b[i++];
  7394. n |= (std::size_t) (c & 0x7F) << shift;
  7395. if (c < 0x80)
  7396. break;
  7397. }
  7398. if (id == 0xD5 && i + n <= b.size())
  7399. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  7400. i += n;
  7401. }
  7402. return {};
  7403. }
  7404. } // namespace
  7405. TEST (sawer_engine)
  7406. {
  7407. // a synthetic header: "SSLF" + size + "1rwS" + 96 floats
  7408. std::vector<std::uint8_t> synth (12 + 4 * 96, 0);
  7409. std::memcpy (synth.data(), "SSLF", 4);
  7410. const std::uint32_t size = 4 + 4 * 96;
  7411. std::memcpy (synth.data() + 4, &size, 4);
  7412. std::memcpy (synth.data() + 8, "1rwS", 4);
  7413. const float p27 = 0.75f;
  7414. std::memcpy (synth.data() + 12 + 4 * 27, &p27, 4);
  7415. CHECK (lynx::sawerStateUsable (synth));
  7416. CHECK (lynx::sawerParam (synth, 27) == 0.75f && lynx::sawerParamOffset (synth, 27) == 12 + 4 * 27);
  7417. CHECK (! lynx::sawerParam (synth, 96) && lynx::sawerParamOffset (synth, -1) < 0);
  7418. auto broken = synth;
  7419. broken[8] = 'X';
  7420. CHECK (! lynx::sawerStateUsable (broken));
  7421. CHECK (! lynx::sawerStateUsable (std::vector<std::uint8_t> (synth.begin(), synth.end() - 4)));
  7422. const auto dflt = sawerPresetState ("Default.fst");
  7423. const auto slap = sawerPresetState ("Arpeggios/FG Velocity Slapper.fst");
  7424. const auto tablePath = realFlpDir.empty() ? std::string() : (std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Sawer" / "data.saw").string();
  7425. const auto table = tablePath.empty() ? std::vector<float> {} : lynx::sawerReadTable (tablePath);
  7426. if (dflt.empty() || slap.empty() || table.size() != 8192u)
  7427. {
  7428. std::printf (" (sawer_engine: FL Studio installation not found, DLL fingerprints skipped)\n");
  7429. return;
  7430. }
  7431. CHECK (lynx::sawerStateUsable (dflt) && lynx::sawerStateUsable (slap));
  7432. // DLL fingerprints (py/gold_sw.py: the EXEC harness, L = R = Final.Vol); "syn" = Default with our own settings
  7433. // (unison 4, sub pulse at 0.6, sync, band-pass, noise, resonance, chorus)
  7434. auto setP = [] (std::vector<std::uint8_t> s, int k, float v) { std::memcpy (s.data() + 12 + 4 * k, &v, 4); return s; };
  7435. auto syn = dflt;
  7436. 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 },
  7437. { 26, 0.3f }, { 28, 0.7f }, { 45, 0.5f }, { 46, 0.0f } })
  7438. syn = setP (syn, kv.first, kv.second);
  7439. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  7440. using N = lynx::sawer::RefNote;
  7441. struct Sample { int at; float l, r; };
  7442. struct Golden { const std::vector<std::uint8_t>* state; std::vector<N> notes; double fs; int block; double el, er; Sample s[4]; };
  7443. const Golden goldens[] {
  7444. { &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 } },
  7445. 44100, 512, 1172.343107, 1169.673445,
  7446. { { 1000, 0.0349505879f, 0.0349650607f }, { 10000, -0.0326882228f, -0.0326587185f }, { 30000, 0.139422998f, 0.139268339f }, { 60000, -1.01108653e-05f, -1.01210762e-05f } } },
  7447. { &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,
  7448. { { 1000, 0.00919434987f, 0.0769683793f }, { 10000, 0.0363935083f, 0.0227626693f }, { 30000, -0.00462511741f, -0.0105419969f }, { 60000, -0.00727002183f, -0.00159690797f } } },
  7449. { &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,
  7450. { { 1000, -0.162745371f, -0.172861978f }, { 10000, 0.0468756184f, 0.112415045f }, { 30000, -0.123887017f, -0.132816017f }, { 60000, 0.0f, 0.0f } } },
  7451. };
  7452. for (const auto& g : goldens)
  7453. {
  7454. const auto y = lynx::sawer::renderReference (*g.state, table, g.notes, g.fs, 66150, g.block);
  7455. CHECK (y.size() == 2 * 66150u);
  7456. if (y.size() != 2 * 66150u)
  7457. continue;
  7458. double el = 0.0, er = 0.0;
  7459. for (std::size_t i = 0; i < 66150; ++i)
  7460. el += (double) y[2 * i] * y[2 * i], er += (double) y[2 * i + 1] * y[2 * i + 1];
  7461. CHECK (std::abs (el - g.el) < 1e-5 * g.el && std::abs (er - g.er) < 1e-5 * g.er);
  7462. for (const auto& s : g.s)
  7463. 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);
  7464. }
  7465. // the instrument: the same engine behind LYNX's channel gains (centre → law A), velocity → Init.Vol; it goes idle
  7466. lynx::ChannelDef c;
  7467. c.instrument = lynx::InstrumentKind::sawer;
  7468. c.pluginState = dflt;
  7469. c.samplePath = tablePath;
  7470. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sawer);
  7471. inst->configure (c, 120.0);
  7472. inst->prepare (44100.0);
  7473. std::vector<float> l (512 * 40, 0.0f), r (512 * 40, 0.0f);
  7474. for (int b = 0; b < 40; ++b)
  7475. {
  7476. if (b == 0)
  7477. inst->noteOn (60, 0.78125f);
  7478. if (b == 10)
  7479. inst->noteOff (60);
  7480. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  7481. }
  7482. 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,
  7483. 8192, 512, 120.0, 0, true);
  7484. double diff = 0.0, energy = 0.0;
  7485. for (int i = 0; i < 8192; ++i)
  7486. {
  7487. 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]) });
  7488. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  7489. }
  7490. CHECK (diff == 0.0 && energy > 1e-3);
  7491. CHECK (inst->idle());
  7492. }
  7493. TEST (transistorbass_engine)
  7494. {
  7495. const auto factory = tbassPresetState ("Factory programs.fst");
  7496. const auto seqOff = tbassPresetState ("Sequencer off.fst");
  7497. if (factory.empty() || seqOff.empty())
  7498. return; // no FL installation
  7499. CHECK (factory.size() == 197887 && seqOff.size() == 196051);
  7500. CHECK (lynx::transistorBassStateUsable (factory) && lynx::transistorBassStateUsable (seqOff));
  7501. CHECK (lynx::transistorBassStateUsable (tbassWithEngine (factory, 2)));
  7502. auto broken = factory;
  7503. broken[0] = 3; // the wrapper refuses versions above 2
  7504. CHECK (! lynx::transistorBassStateUsable (broken));
  7505. auto truncated = factory;
  7506. truncated.resize (truncated.size() - 1000);
  7507. CHECK (! lynx::transistorBassStateUsable (truncated));
  7508. const auto dll = tbassDll();
  7509. if (dll.empty() || ! lynx::transistorBassDllUsable (dll))
  7510. return;
  7511. // against FL's engine DLL (EXEC, REA_rodada57 §2.11): programs of the factory bank (sequencer, accents, slides,
  7512. // distortion, delay, reverb) on the three engines and the sequencer-off preset with legato and accent notes; host
  7513. // blocks of 512 at 44.1 kHz, tempo 125. LYNX differs from the DLL by −121 dB or less here (library pow rounding).
  7514. struct Sample { int at; float l, r; };
  7515. struct Golden { const char* preset; int engine, key; double el, er; Sample s[4]; };
  7516. const Golden goldens[] {
  7517. { "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 } } },
  7518. { "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 } } },
  7519. { "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 } } },
  7520. { "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 } } },
  7521. { "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 } } },
  7522. { "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 } } },
  7523. { "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 } } },
  7524. { "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 } } },
  7525. { "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 } } },
  7526. { "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 } } },
  7527. };
  7528. constexpr int length = 88200;
  7529. for (const auto& g : goldens)
  7530. {
  7531. const bool factoryBank = std::string (g.preset) == "Factory";
  7532. auto st = factoryBank ? factory : seqOff;
  7533. if (g.engine >= 0)
  7534. st = tbassWithEngine (st, g.engine);
  7535. std::vector<lynx::tbass::RefNote> notes;
  7536. if (factoryBank)
  7537. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 70560 });
  7538. else
  7539. {
  7540. notes.push_back ({ 0, g.key, 0.5, 0.0, 0.0, 0.0, 0.0, 30000 });
  7541. notes.push_back ({ 20000, g.key + 7, 1.6, 0.0, 0.0, 0.0, 0.0, 50000 });
  7542. notes.push_back ({ 45000, g.key - 5, 0.3, 0.0, 0.0, 0.0, 0.0, 70560 });
  7543. }
  7544. const auto y = lynx::tbass::renderReference (st, dll, notes, 44100.0, length);
  7545. double el = 0.0, er = 0.0, peak = 0.0;
  7546. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7547. 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]) });
  7548. 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;
  7549. for (const auto& smp : g.s)
  7550. 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;
  7551. if (! ok)
  7552. 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);
  7553. CHECK (ok);
  7554. }
  7555. // the instrument: FL's wrapper velocity, the channel volume as an output gain, the sequencer plays while the key is held
  7556. lynx::ChannelDef c;
  7557. c.instrument = lynx::InstrumentKind::transistorbass;
  7558. c.pluginState = factory;
  7559. c.samplePath = dll;
  7560. auto inst = lynx::makeInstrument (lynx::InstrumentKind::transistorbass);
  7561. inst->configure (c, 128.0);
  7562. inst->prepare (44100.0);
  7563. std::vector<float> l (4096), r (4096);
  7564. inst->noteOn (65, 0.8f);
  7565. double e = 0.0;
  7566. bool finite = true;
  7567. for (int b = 0; b < 24; ++b)
  7568. {
  7569. std::fill (l.begin(), l.end(), 0.0f);
  7570. std::fill (r.begin(), r.end(), 0.0f);
  7571. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7572. for (int i = 0; i < 4096; ++i)
  7573. 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];
  7574. if (b == 20)
  7575. inst->noteOff (65);
  7576. }
  7577. CHECK (finite && e > 1.0 && ! inst->idle());
  7578. for (int b = 0; b < 400 && ! inst->idle(); ++b)
  7579. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  7580. CHECK (inst->idle());
  7581. }
  7582. TEST (kepler_demo_channels)
  7583. {
  7584. // every Kepler channel of FL's demo projects on LYNX's engine against FL's engine DLL (EXEC fingerprints, REA_rodada46 §6;
  7585. // the engine differs from the DLL by −127 dB or less on these, from the DLL library's pow rounding)
  7586. const auto dll = keplerDll();
  7587. if (dll.empty() || ! lynx::keplerDllUsable (dll))
  7588. return;
  7589. struct Sample { int at; float l, r; };
  7590. struct Golden { const char* file; const char* channel; std::size_t stateSize; double el, er; Sample s[4]; };
  7591. const Golden goldens[] {
  7592. { "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 } } },
  7593. { "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 } } },
  7594. { "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 } } },
  7595. { "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 } } },
  7596. };
  7597. int checked = 0;
  7598. for (const auto& g : goldens)
  7599. {
  7600. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  7601. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7602. if (bytes.empty())
  7603. continue;
  7604. const flp::Channel* ch = nullptr;
  7605. const auto project = flp::parseProject (bytes);
  7606. for (const auto& c : project.channels)
  7607. if (c.pluginName == "Kepler" && c.name == g.channel)
  7608. ch = &c;
  7609. CHECK (ch != nullptr);
  7610. if (ch == nullptr)
  7611. continue;
  7612. CHECK (ch->pluginState.size() == g.stateSize && lynx::keplerStateUsable (ch->pluginState));
  7613. 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 },
  7614. { 16000, 55, 0.95, 0.0, 0.0, 30000 } }, 44100.0, 39690);
  7615. double el = 0.0, er = 0.0, peak = 0.0;
  7616. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7617. 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]) });
  7618. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7619. for (const auto& smp : g.s)
  7620. 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;
  7621. if (! ok)
  7622. 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);
  7623. CHECK (ok);
  7624. ++checked;
  7625. }
  7626. std::printf (" Kepler: %d demo channels checked against FL's engine DLL\n", checked);
  7627. CHECK (checked == (int) std::size (goldens));
  7628. }
  7629. TEST (ogun_demo_channels)
  7630. {
  7631. // every Ogun channel of FL's demo projects (with its plugin effects) against the round-37 reference model, which is
  7632. // 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),
  7633. // 39 690 samples at 44.1 kHz, at the state's quality and at the export quality 4. Fingerprint: energy per side and
  7634. // four samples (the engine matches the model within float rounding, −125 dB or better on the whole signal).
  7635. if (realFlpDir.empty())
  7636. return;
  7637. struct Sample { int at; float l, r; };
  7638. struct Golden { const char* file; int ordinal; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  7639. const Golden goldens[] {
  7640. { "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
  7641. { "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
  7642. { "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
  7643. { "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
  7644. { "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
  7645. { "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
  7646. { "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
  7647. { "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
  7648. { "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
  7649. { "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
  7650. { "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
  7651. { "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
  7652. { "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
  7653. { "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
  7654. { "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
  7655. { "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
  7656. { "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
  7657. { "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
  7658. { "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
  7659. { "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
  7660. { "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
  7661. { "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
  7662. { "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
  7663. { "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
  7664. { "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
  7665. { "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
  7666. { "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
  7667. { "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
  7668. { "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
  7669. { "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
  7670. { "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
  7671. { "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
  7672. { "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
  7673. { "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
  7674. { "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
  7675. { "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
  7676. { "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
  7677. { "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
  7678. { "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
  7679. { "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
  7680. };
  7681. std::map<std::string, std::vector<std::vector<std::uint8_t>>> states;
  7682. int checked = 0;
  7683. for (const auto& g : goldens)
  7684. {
  7685. if (! states.contains (g.file))
  7686. {
  7687. std::ifstream in (std::filesystem::path (realFlpDir) / "Demo projects" / g.file, std::ios::binary);
  7688. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7689. auto& list = states[g.file];
  7690. if (! bytes.empty())
  7691. for (const auto& c : flp::parseProject (bytes).channels)
  7692. if (c.pluginName == "Ogun")
  7693. list.push_back (c.pluginState);
  7694. }
  7695. const auto& list = states[g.file];
  7696. if (list.empty())
  7697. continue; // not this installation
  7698. CHECK ((int) list.size() > g.ordinal);
  7699. if ((int) list.size() <= g.ordinal)
  7700. continue;
  7701. const auto& st = list[(std::size_t) g.ordinal];
  7702. CHECK (st.size() == g.stateSize);
  7703. lynx::ogun::RefOptions opt;
  7704. opt.quality = g.export4 ? 4 : -1;
  7705. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  7706. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  7707. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  7708. double el = 0.0, er = 0.0, peak = 0.0;
  7709. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7710. 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]) });
  7711. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7712. for (const auto& smp : g.s)
  7713. 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;
  7714. if (! ok)
  7715. std::printf (" Ogun %s #%d (q%s) differs from the model: energy %.9g %.9g (model %.9g %.9g)\n", g.file, g.ordinal,
  7716. g.export4 ? "4" : "-state", el, er, g.el, g.er);
  7717. CHECK (ok);
  7718. ++checked;
  7719. }
  7720. std::printf (" Ogun: %d demo renders checked against the reference model\n", checked);
  7721. CHECK (checked == (int) std::size (goldens));
  7722. }
  7723. TEST (autogun_template_channels)
  7724. {
  7725. // REA_rodada57: every distinct Autogun state of the installation's projects (templates and the render test), with its
  7726. // effects and the Magic Dust stage (SG.dat from the installation), three notes (C5 1.0 0–20000, G5 0.6 8000–26000,
  7727. // 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
  7728. // goldens are renders of the installation's Autogun_x64.dll (LYNX is within −127 dB of them); the render-test state
  7729. // uses Seed 2 "Random", so its golden is LYNX's own render with the counter at 0 (a fingerprint of the engine).
  7730. if (realFlpDir.empty())
  7731. return;
  7732. const auto sgPath = std::filesystem::path (realFlpDir).parent_path() / "Plugins" / "Fruity" / "Generators" / "Autogun" / "Data" / "SG.dat";
  7733. std::ifstream sgIn (sgPath, std::ios::binary);
  7734. const std::vector<std::uint8_t> sgState ((std::istreambuf_iterator<char> (sgIn)), std::istreambuf_iterator<char>());
  7735. if (sgState.size() != 1342)
  7736. return; // not this installation
  7737. struct Sample { int at; float l, r; };
  7738. struct Golden { const char* file; std::size_t stateSize; bool export4; double el, er; Sample s[4]; };
  7739. const Golden goldens[] {
  7740. { "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)
  7741. { "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)
  7742. { "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)
  7743. { "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)
  7744. { "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)
  7745. { "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)
  7746. { "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)
  7747. { "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)
  7748. { "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)
  7749. { "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)
  7750. };
  7751. int checked = 0;
  7752. for (const auto& g : goldens)
  7753. {
  7754. std::ifstream in (std::filesystem::path (realFlpDir) / g.file, std::ios::binary);
  7755. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7756. if (bytes.empty())
  7757. continue;
  7758. std::vector<std::uint8_t> st;
  7759. for (const auto& c : flp::parseProject (bytes).channels)
  7760. if (c.pluginName == "Autogun" && st.empty())
  7761. st = c.pluginState;
  7762. CHECK (st.size() == g.stateSize);
  7763. CHECK (lynx::autogunStateUsable (st));
  7764. lynx::ogun::RefOptions opt;
  7765. opt.autogun = true;
  7766. opt.sgState = sgState;
  7767. opt.quality = g.export4 ? 4 : -1;
  7768. const auto y = lynx::ogun::renderReference (st, { { 0, 60, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 20000 },
  7769. { 8000, 67, 0.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 26000 },
  7770. { 16000, 55, 0.95, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30000 } }, 44100.0, 39690, opt);
  7771. double el = 0.0, er = 0.0, peak = 0.0;
  7772. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  7773. 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]) });
  7774. bool ok = y.size() == 2 * 39690 && std::abs (el - g.el) <= 1e-5 * g.el && std::abs (er - g.er) <= 1e-5 * g.er;
  7775. for (const auto& smp : g.s)
  7776. 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;
  7777. if (! ok)
  7778. 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);
  7779. CHECK (ok);
  7780. ++checked;
  7781. }
  7782. std::printf (" Autogun: %d template/render-test renders checked (DLL goldens for the fixed-spectrum states)\n", checked);
  7783. CHECK (checked == (int) std::size (goldens));
  7784. // the instrument (SG.dat read from ChannelDef::samplePath) plays like the reference renderer, then goes idle
  7785. {
  7786. std::ifstream in (std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp", std::ios::binary);
  7787. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  7788. lynx::ChannelDef c;
  7789. c.instrument = lynx::InstrumentKind::autogun;
  7790. for (const auto& ch : flp::parseProject (bytes).channels)
  7791. if (ch.pluginName == "Autogun")
  7792. c.pluginState = ch.pluginState;
  7793. c.samplePath = sgPath.string();
  7794. lynx::ogun::RefOptions opt;
  7795. opt.autogun = true;
  7796. opt.sgState = sgState;
  7797. opt.tempo = 120.0; // the instrument is configured at 120 BPM
  7798. 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);
  7799. auto inst = lynx::makeInstrument (lynx::InstrumentKind::autogun);
  7800. inst->configure (c, 120.0);
  7801. inst->prepare (44100.0);
  7802. std::vector<float> l (16384), r (16384);
  7803. for (int s = 0; s < 16384; s += 512)
  7804. {
  7805. if (s == 0)
  7806. inst->noteOn (60, 0.75f);
  7807. if (s == 6144)
  7808. inst->noteOff (60);
  7809. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  7810. }
  7811. double worst = 0.0, peak = 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. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  7815. if (worst != 0.0)
  7816. std::printf (" Autogun instrument vs reference: worst %.3g (peak %.3g)\n", worst, peak);
  7817. CHECK (ref.size() == 2 * 16384 && peak > 0.01 && worst == 0.0);
  7818. std::vector<float> tail (44100), tailR (tail.size());
  7819. for (int k = 0; k < 40 && ! inst->idle(); ++k)
  7820. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  7821. CHECK (inst->idle());
  7822. }
  7823. // the state variants: Ogun's parser rejects the version-19 Autogun layout, the comment length is one byte or 0xff + i32
  7824. std::ifstream rt (std::filesystem::path (realFlpDir) / "System" / "Render tests" / "Autogun.flp", std::ios::binary);
  7825. const std::vector<std::uint8_t> rtBytes ((std::istreambuf_iterator<char> (rt)), std::istreambuf_iterator<char>());
  7826. for (const auto& c : flp::parseProject (rtBytes).channels)
  7827. if (c.pluginName == "Autogun")
  7828. CHECK (lynx::autogunStateUsable (c.pluginState) && lynx::ogunParam (c.pluginState, 63) == 430);
  7829. }
  7830. TEST (harmless_rack)
  7831. {
  7832. // REA_rodada36: Harmless's effects rack against our reference model (rack.py / maxmodel.py, which match the
  7833. // installation's Harmless_x64.dll and SG_x64.dll), 44.1 kHz, host blocks of 512
  7834. std::vector<float> inL (8192), inR (8192);
  7835. for (int i = 0; i < 4096; ++i)
  7836. {
  7837. 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));
  7838. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  7839. }
  7840. auto run = [&] (const std::array<int, 100>& p, int compType, std::shared_ptr<const lynx::hlfx::MaximusPreset> comp, int total, int voicesUntil) {
  7841. lynx::hlfx::Rack rack;
  7842. std::array<int, 7> t {};
  7843. t[3] = compType;
  7844. rack.prepare (p, t, 44100.0, 120.0, std::move (comp));
  7845. std::vector<float> l (inL.begin(), inL.begin() + total), r (inR.begin(), inR.begin() + total);
  7846. for (int s = 0; s < total; s += 512)
  7847. rack.process (l.data() + s, r.data() + s, std::min (512, total - s), s < voicesUntil);
  7848. return std::make_pair (l, r);
  7849. };
  7850. {
  7851. // chorus type 3 (mix 400), ping-pong delay type 2 (vol 600, feedback 500, 1/4 beat), reverb type 4 (vol 700)
  7852. std::array<int, 100> p {};
  7853. p[90] = 3, p[91] = 400, p[92] = 2, p[93] = 600, p[94] = 500, p[95] = 48, p[96] = 4, p[97] = 700;
  7854. const auto [l, r] = run (p, 0, nullptr, 8192, 4096);
  7855. const std::tuple<int, float, float> golden[] { { 0, 0.0f, 0.0f }, { 500, -0.25248247f, 0.15141529f }, { 3000, -0.65128994f, -0.64854866f },
  7856. { 5000, -0.54308629f, -0.36827448f }, { 6000, -0.98378551f, -0.72790027f },
  7857. { 8191, 0.50599128f, -0.43637198f } };
  7858. for (const auto& [i, gl, gr] : golden)
  7859. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7860. }
  7861. {
  7862. // the delay alone: an impulse comes back after round(time·fs·0.3125/bpm) samples × (11^0.6 − 1)/10, then × feedback
  7863. std::array<int, 100> p {};
  7864. p[92] = 1, p[93] = 600, p[94] = 500, p[95] = 48;
  7865. lynx::hlfx::Rack rack;
  7866. rack.prepare (p, {}, 44100.0, 120.0, nullptr);
  7867. std::vector<float> l (16384), r (16384);
  7868. l[0] = 1.0f;
  7869. for (int s = 0; s < 16384; s += 512)
  7870. rack.process (l.data() + s, r.data() + s, 512, s == 0);
  7871. const auto D = 5512; // 48·44100·0.3125/120 = 5512.5 → 5512 (round half to even)
  7872. const auto vol = (float) ((std::exp ((double) (600 * 0.001f) * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  7873. CHECK (std::abs (l[(std::size_t) D] - vol) < 1e-7f && std::abs (l[(std::size_t) D - 1]) < 1e-12f);
  7874. // the feedback ramps from 0.5 toward 0.5: the second echo is vol·0.5
  7875. CHECK (std::abs (l[(std::size_t) (2 * D)] - vol * 0.5f) < 1e-7f && std::abs (r[(std::size_t) D]) < 1e-12f);
  7876. }
  7877. // the compressor: Maximus states of our own (no preset data), checked against SG_x64.dll by REA round 36
  7878. using Env = std::vector<std::array<double, 4>>;
  7879. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  7880. 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,
  7881. int sus = 2882) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, 0 }; };
  7882. {
  7883. // master brickwall at −0.22 dB only (bands off / compression off)
  7884. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  7885. { def, def, def, def, def, def, def, def }, 0);
  7886. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  7887. CHECK (preset != nullptr);
  7888. std::array<int, 100> p {};
  7889. p[98] = 1000;
  7890. const auto [l, r] = run (p, 1, preset, 4096, 4096);
  7891. // SG_x64.dll's own output (EXEC, REA_rodada40: bit-identical to the port)
  7892. const std::tuple<int, float, float> golden[] { { 100, 0.024997942f, 0.88166064f }, { 1000, 0.56971401f, -0.62808102f },
  7893. { 2047, -0.27541628f, 0.709135f }, { 3000, 0.32431623f, -0.37985197f },
  7894. { 4095, -0.12743522f, -0.15237485f } };
  7895. for (const auto& [i, gl, gr] : golden)
  7896. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7897. float peak = 0.0f;
  7898. for (int i = 2048; i < 4096; ++i)
  7899. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  7900. CHECK (peak < 0.9751f);
  7901. }
  7902. {
  7903. // 24 dB crossovers, ×2 oversampling, low cut, stereo separation, saturation A, a knee with tension (type 7) and
  7904. // exponential tensions (type 0), release 2 on the master, band mix 600
  7905. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  7906. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  7907. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  7908. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  7909. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  7910. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  7911. { def, def, knee, def, t0, def, m0, def }, 1);
  7912. const auto preset = lynx::hlfx::makeMaximusPreset (st);
  7913. CHECK (preset != nullptr);
  7914. std::array<int, 100> p {};
  7915. p[98] = 600;
  7916. const auto [l, r] = run (p, 2, preset, 4096, 4096);
  7917. // SG_x64.dll's own output (EXEC, REA_rodada40; the port is within −142 dB of it)
  7918. const std::tuple<int, float, float> golden[] { { 100, 0.0f, 0.0f }, { 1000, -0.46856543f, -0.63262129f },
  7919. { 2047, 0.20268451f, 0.54145539f }, { 3000, -0.25613472f, -0.49985155f },
  7920. { 4095, 0.10172445f, 0.050564945f } };
  7921. for (const auto& [i, gl, gr] : golden)
  7922. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  7923. }
  7924. {
  7925. // an .fst wrapper around a state is found; unsupported states are rejected
  7926. const auto st = maximusTestState ({ 19800, 1, 48065, 1, 1115, 1000, -1 }, { band (3), band (1), band (3), band (0) },
  7927. { def, def, def, def, def, def, def, def }, 0);
  7928. std::vector<std::uint8_t> fst { 'F', 'L', 'h', 'd', 6, 0, 0, 0, 0, 0, 1, 0, 0x60, 0, 'F', 'L', 'd', 't' };
  7929. std::vector<std::uint8_t> body { 0xC9, 8, 'M', 'a', 'x', 'i', 'm', 'u', 's', 0, 0xD5 };
  7930. for (auto n = st.size(); ; n >>= 7)
  7931. {
  7932. body.push_back ((std::uint8_t) ((n & 0x7f) | (n >= 0x80 ? 0x80 : 0)));
  7933. if (n < 0x80)
  7934. break;
  7935. }
  7936. body.insert (body.end(), st.begin(), st.end());
  7937. const auto len = (std::uint32_t) body.size();
  7938. for (int k = 0; k < 4; ++k)
  7939. fst.push_back ((std::uint8_t) (len >> (8 * k)));
  7940. fst.insert (fst.end(), body.begin(), body.end());
  7941. const auto found = lynx::hlfx::maximusStateFromFst (fst);
  7942. CHECK (found && *found == st);
  7943. auto rms = st;
  7944. rms[4 + 4 * (7 + 13 * 3 + 12)] = 1; // RMS detector on the master (decoded in REA_rodada40)
  7945. CHECK (lynx::hlfx::makeMaximusPreset (rms) != nullptr);
  7946. auto masterMid = st;
  7947. masterMid[0x1fc] = 1; // "Master Mid mode": not decoded
  7948. CHECK (lynx::hlfx::makeMaximusPreset (masterMid) == nullptr);
  7949. }
  7950. }
  7951. TEST (fruity_delay_bank_engine)
  7952. {
  7953. // REA_rodada56: Fruity Delay Bank, two states of our own against the installation's DLL (goldens = the DLL's output
  7954. // for the same input, EXEC; a new instance, FTZ/DAZ as the plugin's thread). Both are bit-identical.
  7955. lynx::ScopedFlushDenormals ftz;
  7956. struct Bank
  7957. {
  7958. std::array<int, 16> ctl;
  7959. lynx::fldbank::BankOptions opt;
  7960. };
  7961. auto build = [] (std::array<int, 4> globals, std::map<int, Bank> banks, int oversampling) {
  7962. auto s = lynx::fldbank::defaultState();
  7963. for (int i = 0; i < 4; ++i)
  7964. s.p[(std::size_t) i] = globals[(std::size_t) i];
  7965. s.p[132] = oversampling;
  7966. for (auto& [b, k] : banks)
  7967. {
  7968. for (int c = 0; c < 16; ++c)
  7969. if (16 * b + 4 + c < lynx::fldbank::automatable)
  7970. s.p[(std::size_t) (16 * b + 4 + c)] = k.ctl[(std::size_t) c];
  7971. s.bank[(std::size_t) b] = k.opt;
  7972. s.bank[(std::size_t) b].enabled = true;
  7973. }
  7974. return s;
  7975. };
  7976. auto opt = [] (int f1, int o1, int f2, int o2, bool after, int mode, int steps, int amount, bool tap, bool sync) {
  7977. lynx::fldbank::BankOptions o;
  7978. o.filterType[0] = f1;
  7979. o.filterOrder[0] = o1;
  7980. o.filterType[1] = f2;
  7981. o.filterOrder[1] = o2;
  7982. o.filterAfter = after;
  7983. o.mode = mode;
  7984. o.steps = steps;
  7985. o.amount = amount;
  7986. o.tapBeforeFeedback = tap;
  7987. o.sync = sync;
  7988. return o;
  7989. };
  7990. auto run = [] (const lynx::fldbank::State& s, double rate, int block, double bpm) {
  7991. std::vector<float> l (4096), r (4096);
  7992. for (int i = 0; i < 4096; ++i)
  7993. {
  7994. 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));
  7995. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / rate));
  7996. }
  7997. lynx::fldbank::Engine e (s, rate, bpm, block);
  7998. for (int i = 0; i < 4096; i += block)
  7999. e.process (l.data() + i, r.data() + i, std::min (block, 4096 - i));
  8000. return std::make_pair (l, r);
  8001. };
  8002. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8003. for (const auto& [i, gl, gr] : golden)
  8004. CHECK (out.first[(std::size_t) i] == gl && out.second[(std::size_t) i] == gr);
  8005. };
  8006. // controls: in pan, in vol, f1 cutoff, f1 res, f1 gain, fb pan, fb level, fb cross, time, offset, f2 cutoff, f2 res,
  8007. // f2 gain, out pan, out vol, send. Three banks: ping-pong with low-pass ×2 / peaking feedback filter, pattern and send;
  8008. // free time with a high shelf after the delay, band-pass feedback, negative feedback/cross, tap before the feedback;
  8009. // swapped outputs with a 3-section high-pass in the loop and a negative pattern shape.
  8010. const auto a = build ({ 100, 128, 120, 110 },
  8011. { { 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) } },
  8012. { 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) } },
  8013. { 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) } } },
  8014. 0);
  8015. check (run (a, 44100.0, 512, 120.0), { { 100, -0.00294386433f, 0.0141299525f }, { 1000, -0.117360562f, -0.140237048f },
  8016. { 2047, 0.172598198f, -0.401046395f }, { 3000, 0.303020209f, -0.127763838f },
  8017. { 4095, 0.0093755126f, 0.241272509f } });
  8018. // 2× oversampling at 48 kHz in blocks of 300, low shelf before the delay, 2-section notch in the loop, a send into a
  8019. // ping-pong bank 3 (bank 1 and 2 off: no send) with a 2-step pattern
  8020. const auto b = build ({ 0, 128, 128, 128 },
  8021. { { 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) } },
  8022. { 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) } } },
  8023. 1);
  8024. check (run (b, 48000.0, 300, 93.0), { { 100, 0.0f, 0.0f }, { 1000, 0.0f, 0.0f }, { 2047, 0.201803684f, -0.327834547f },
  8025. { 3000, 0.412344277f, -0.101866335f }, { 4095, 0.204572573f, 0.71171385f } });
  8026. // FL's state round-trips through the import (version 15 times are converted), the globals are the slot's parameters
  8027. auto bytes = lynx::fldbank::serialise (a);
  8028. const auto slot = lynx::effectFromFl ("Fruity Delay Bank", bytes);
  8029. CHECK (slot && slot->kind == lynx::EffectKind::fruityDelayBank && slot->state == bytes);
  8030. CHECK (slot && slot->params[0] == 100.0f / 128.0f && slot->params[3] == 110.0f / 128.0f);
  8031. bytes[0] = 15;
  8032. const auto v15 = lynx::fldbank::parse (bytes);
  8033. CHECK (v15 && v15->p[12] == 2400 && v15->p[28] == 1500);
  8034. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 3, 0.5f, 120.0) == "50%");
  8035. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelayBank, 0, 1.0f, 120.0) == "0.0 dB");
  8036. CHECK (! lynx::fldbank::parse ({ 16, 0, 0, 0 }));
  8037. // the LYNX effect (settled ramps, 512-frame engine blocks) runs from the slot
  8038. auto fx = lynx::makeEffect (*slot);
  8039. fx->prepare (44100.0);
  8040. std::vector<float> l (1024, 0.5f), r (1024, -0.25f);
  8041. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8042. CHECK (std::isfinite (l[1023]) && std::isfinite (r[1023]));
  8043. }
  8044. TEST (fruity_multiband_engine)
  8045. {
  8046. // REA_rodada56: Fruity Multiband Compressor, states of our own against the installation's DLL
  8047. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz; all three bit-identical)
  8048. std::vector<float> inL (4096), inR (4096);
  8049. for (int i = 0; i < 4096; ++i)
  8050. {
  8051. 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)
  8052. + 0.5 * std::sin (2.0 * 3.141592653589793 * 90.0 * i / 44100.0));
  8053. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0)
  8054. + 0.4 * std::sin (2.0 * 3.141592653589793 * 7000.0 * i / 44100.0));
  8055. }
  8056. auto multiband = [] (std::array<int, 28> c) {
  8057. std::vector<std::uint8_t> b;
  8058. auto put = [&b] (std::int32_t v) {
  8059. std::uint8_t q[4];
  8060. std::memcpy (q, &v, 4);
  8061. b.insert (b.end(), q, q + 4);
  8062. };
  8063. put (0);
  8064. for (auto v : c)
  8065. put (v);
  8066. put (0); // UI words of the state (not used by the engine)
  8067. put (50);
  8068. return b;
  8069. };
  8070. struct Out { std::vector<float> l, r; int latency = 0; };
  8071. auto run = [&] (const std::vector<std::uint8_t>& st) {
  8072. const auto s = lynx::flmb::parse (st);
  8073. CHECK (s.has_value());
  8074. lynx::EffectParams p {};
  8075. lynx::flmb::toParams (*s, p.data());
  8076. auto fx = lynx::flmb::make();
  8077. fx->prepare (44100.0);
  8078. Out o { inL, inR };
  8079. for (int s0 = 0; s0 < 4096; s0 += 512)
  8080. fx->process (o.l.data() + s0, o.r.data() + s0, 512, p, 120.0);
  8081. o.latency = fx->latencySamples();
  8082. return o;
  8083. };
  8084. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8085. for (const auto& [i, gl, gr] : golden)
  8086. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  8087. };
  8088. // IIR crossovers, limiter on, high band compressing, mid bypassed, low compressing, +2 dB output
  8089. 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 }));
  8090. CHECK (oa.latency == 0);
  8091. check (oa,
  8092. { { 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 } });
  8093. // linear phase (FIR, 52 frames of latency), overlapping mid transitions, low band off, limiter off
  8094. 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 }));
  8095. CHECK (ob.latency == 52);
  8096. check (ob,
  8097. { { 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 } });
  8098. // linear phase, hard compression everywhere, limiter on, mid crossovers crossed (the FIR mid band is silent)
  8099. 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 }));
  8100. CHECK (oc.latency == 52);
  8101. check (oc,
  8102. { { 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 } });
  8103. // the slot's parameters carry FL's raw controls exactly; after the project file's 4-decimal rounding the
  8104. // effect built with FL's state still uses the exact controls
  8105. const auto st = multiband ({ 42598, 1, 1, 0, 15604, 25782, 53524, 32768, 11, 298, 51118, 0, 14994, 5037, 24736, 55755, 26870, 221,
  8106. 1090, 49807, 0, 5037, 21845, 47074, 32768, 550, 1090, 40632 });
  8107. const auto parsed = lynx::flmb::parse (st);
  8108. CHECK (parsed && parsed->c[4] == 15604);
  8109. lynx::EffectParams p {};
  8110. lynx::flmb::toParams (*parsed, p.data());
  8111. for (int i = 0; i < lynx::flmb::controls; ++i)
  8112. 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]);
  8113. lynx::EffectParams rounded = p;
  8114. for (auto& v : rounded)
  8115. v = std::round (v * 10000.0f) / 10000.0f;
  8116. std::vector<float> l1 (inL), r1 (inR), l2 (inL), r2 (inR);
  8117. auto exact = lynx::flmb::make();
  8118. auto fromState = lynx::flmb::make (st);
  8119. exact->prepare (44100.0);
  8120. fromState->prepare (44100.0);
  8121. for (int s0 = 0; s0 < 4096; s0 += 512)
  8122. {
  8123. exact->process (l1.data() + s0, r1.data() + s0, 512, p, 120.0);
  8124. fromState->process (l2.data() + s0, r2.data() + s0, 512, rounded, 120.0);
  8125. }
  8126. CHECK (l1 == l2 && r1 == r2);
  8127. CHECK (lynx::flmb::paramText (4, p[4]) == "6302 Hz");
  8128. CHECK (lynx::flmb::paramText (3, 0.5f) == "off");
  8129. // the FL import builds the exact effect with FL's state kept
  8130. const auto slot = lynx::effectFromFl ("Fruity Multiband Compressor", st);
  8131. CHECK (slot && slot->kind == lynx::EffectKind::fruityMultiband && slot->state == st && slot->params[4] == p[4]);
  8132. // unknown versions are rejected
  8133. CHECK (! lynx::flmb::parse ({ 1, 0, 0, 0 }));
  8134. }
  8135. // REA_rodada54: input of the stereo-effect goldens (the installation DLLs' output for the same input, EXEC)
  8136. static std::pair<std::vector<float>, std::vector<float>> stereoFxInput()
  8137. {
  8138. std::vector<float> l (4096), r (4096);
  8139. for (int i = 0; i < 4096; ++i)
  8140. {
  8141. 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));
  8142. r[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8143. }
  8144. return { l, r };
  8145. }
  8146. static std::vector<std::uint8_t> i32State (std::initializer_list<int> words)
  8147. {
  8148. std::vector<std::uint8_t> d;
  8149. for (const auto w : words)
  8150. for (int b = 0; b < 4; ++b)
  8151. d.push_back ((std::uint8_t) ((std::uint32_t) w >> (8 * b)));
  8152. return d;
  8153. }
  8154. static void checkStereoFx (lynx::EffectSlot slot, const std::vector<std::tuple<int, float, float>>& golden)
  8155. {
  8156. auto [l, r] = stereoFxInput();
  8157. auto fx = lynx::makeEffect (slot);
  8158. fx->prepare (44100.0);
  8159. for (int s = 0; s < 4096; s += 512)
  8160. fx->process (l.data() + s, r.data() + s, 512, slot.params, 120.0);
  8161. for (const auto& [i, gl, gr] : golden)
  8162. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  8163. }
  8164. TEST (stereo_shaper_engine)
  8165. {
  8166. // Fruity Stereo Shaper (REA_rodada54 §2): matrix with cross terms, delay on R, all-pass on L, effects post / pre
  8167. auto slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 0, 1 }));
  8168. CHECK (slot && slot->kind == lynx::EffectKind::stereoShaper);
  8169. if (slot)
  8170. checkStereoFx (*slot, { { 100, 0.0438031591f, 0.0f }, { 1000, 1.11109531f, -0.781641483f }, { 2047, -0.73191303f, 0.483852893f },
  8171. { 3000, 0.950346172f, -0.769737065f }, { 4095, 0.251555145f, 0.153306276f } });
  8172. slot = lynx::effectFromFl ("Fruity Stereo Shaper", i32State ({ 0, 0, 12800, 12800, 0, -2656, 2184, 0, 0 }));
  8173. CHECK (slot.has_value());
  8174. if (slot)
  8175. checkStereoFx (*slot, { { 100, 0.0f, -0.104282036f }, { 1000, 0.0f, 1.08441091f }, { 2047, 0.463494211f, -0.93754214f },
  8176. { 3000, -1.49942112f, 0.820726812f }, { 4095, -1.31784308f, -0.057449434f } });
  8177. // controls round-trip through the slot parameters; the Patcher's difference bus is the same engine flagged
  8178. const auto st = lynx::stereofx::parseStereoShaper (i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }));
  8179. CHECK (st.has_value());
  8180. if (st)
  8181. {
  8182. lynx::EffectParams p {};
  8183. lynx::stereofx::stereoShaperToParams (*st, p.data());
  8184. const auto back = lynx::stereofx::stereoShaperFromParams (p.data());
  8185. CHECK (back.rightToLeft == -3050 && back.left == 12800 && back.right == 9000 && back.leftToRight == 2000 && back.delay == 1408
  8186. && back.phase == -2496 && back.post == 1);
  8187. const auto head = lynx::patcherBranchHead ("Fruity Stereo Shaper", i32State ({ 0, -3050, 12800, 9000, 2000, 1408, -2496, 2, 1 }), 2);
  8188. CHECK (head.valid && head.slot && head.slot->kind == lynx::EffectKind::stereoShaper && head.slot->params[7] == 1.0f);
  8189. }
  8190. CHECK (! lynx::stereofx::parseStereoShaper ({ 1, 2, 3 }));
  8191. }
  8192. TEST (stereo_enhancer_transient_spreader_engines)
  8193. {
  8194. // REA_rodada54 §3–5: own states against the installation's DLLs (goldens = the DLL's output, EXEC)
  8195. auto se = lynx::effectFromFl ("Fruity Stereo Enhancer", i32State ({ -18, 300, -54, -148, 1, 1 }));
  8196. CHECK (se && se->kind == lynx::EffectKind::stereoEnhancer);
  8197. if (se)
  8198. checkStereoFx (*se, { { 100, 0.0f, 0.00309863733f }, { 1000, 0.0101111531f, -0.110266283f }, { 2047, -0.688752949f, -1.93998075f },
  8199. { 3000, -0.283846498f, -0.739818454f }, { 4095, 0.191147864f, 2.70425582f } });
  8200. auto tpState = i32State ({ 1, 138, 116, 64, 90, 2, 0, 70, 100, 1 });
  8201. tpState.push_back (5);
  8202. for (const auto w : i32State ({ 0, 50 }))
  8203. tpState.push_back (w);
  8204. auto tp = lynx::effectFromFl ("Transient Processor", tpState);
  8205. CHECK (tp && tp->kind == lynx::EffectKind::transientProcessor);
  8206. if (tp)
  8207. checkStereoFx (*tp, { { 100, 0.0568947345f, 0.124077775f }, { 1000, -0.326598823f, -0.193128616f }, { 2047, 0.612462878f, -0.0517415144f },
  8208. { 3000, -0.535302937f, -0.446303189f }, { 4095, -0.634670973f, 0.316044629f } });
  8209. auto spState = i32State ({ 0x10003, 7861, 3215, 106000, 1, 1, 0 });
  8210. spState.push_back (1);
  8211. spState.push_back (1);
  8212. auto sp = lynx::effectFromFl ("Spreader", spState);
  8213. CHECK (sp && sp->kind == lynx::EffectKind::spreader);
  8214. if (sp)
  8215. checkStereoFx (*sp, { { 100, 0.11841587f, -0.0958916694f }, { 1000, 1.40852344f, 0.087452054f }, { 2047, 1.07267976f, -2.02493715f },
  8216. { 3000, -0.781082511f, 0.811192274f }, { 4095, -1.80673707f, 0.89967829f } });
  8217. // unknown layouts keep the earlier fallback; FL automation maps one to one
  8218. CHECK (! lynx::stereofx::parseSpreader (i32State ({ 0x10001, 1, 2, 3, 4, 5 })));
  8219. CHECK (! lynx::stereofx::parseTransientProcessor (i32State ({ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
  8220. const auto map = lynx::effectParamFromFl ("Transient Processor", 6);
  8221. CHECK (map && map->param == 6 && map->convert (0.25f) == 0.25f);
  8222. }
  8223. TEST (fruity_dynamics_engines)
  8224. {
  8225. // REA_rodada48: Fruity Limiter / Fruity Compressor, states of our own against the installation's DLLs
  8226. // (goldens = the DLL's output for the same input, EXEC; blocks of 512 at 44.1 kHz)
  8227. std::vector<float> inL (4096), inR (4096), side (4096);
  8228. for (int i = 0; i < 4096; ++i)
  8229. {
  8230. 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));
  8231. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8232. side[(std::size_t) i] = (float) (0.9 * std::sin (2.0 * 3.141592653589793 * 55.0 * i / 44100.0));
  8233. }
  8234. auto put = [] (std::vector<std::uint8_t>& b, std::int32_t v) {
  8235. std::uint8_t q[4];
  8236. std::memcpy (q, &v, 4);
  8237. b.insert (b.end(), q, q + 4);
  8238. };
  8239. auto limiterState = [&] (std::array<int, 18> p, int k = 0, int exact = 0) {
  8240. std::vector<std::uint8_t> b;
  8241. put (b, 7);
  8242. for (auto v : p)
  8243. put (b, v);
  8244. b.resize (0xA4, 0);
  8245. put (b, k);
  8246. b.push_back ((std::uint8_t) exact);
  8247. return b;
  8248. };
  8249. auto compressorState = [&] (int version, std::array<int, 6> p, int flags, int k = 0) {
  8250. std::vector<std::uint8_t> b;
  8251. put (b, version);
  8252. for (auto v : p)
  8253. put (b, v);
  8254. put (b, flags);
  8255. if (version > 2)
  8256. put (b, k);
  8257. return b;
  8258. };
  8259. struct Out { std::vector<float> l, r; int latency = 0; };
  8260. auto run = [&] (const lynx::EffectSlot& slot, bool sidechain) {
  8261. auto fx = lynx::makeEffect (slot);
  8262. fx->prepare (44100.0);
  8263. Out o { inL, inR };
  8264. for (int s = 0; s < 4096; s += 512)
  8265. {
  8266. if (sidechain)
  8267. fx->setSidechain (side.data() + s, side.data() + s);
  8268. fx->process (o.l.data() + s, o.r.data() + s, 512, slot.params, 120.0);
  8269. }
  8270. o.latency = fx->latencySamples();
  8271. return o;
  8272. };
  8273. auto check = [] (const Out& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8274. for (const auto& [i, gl, gr] : golden)
  8275. CHECK (std::abs (out.l[(std::size_t) i] - gl) < 2e-6f && std::abs (out.r[(std::size_t) i] - gr) < 2e-6f);
  8276. };
  8277. auto limiter = [] (const std::vector<std::uint8_t>& st) {
  8278. auto slot = lynx::effectFromFl ("Fruity Limiter", st);
  8279. CHECK (slot && slot->kind == lynx::EffectKind::fruityLimiter);
  8280. return *slot;
  8281. };
  8282. // look-ahead limiter: +4 dB gain into a −2.5 dB ceiling, 2 ms attack (88 samples of latency); bit-identical
  8283. 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);
  8284. CHECK (a.latency == 88);
  8285. check (a, { { 100, 0.0176312141f, 0.621841073f }, { 1000, 0.401142687f, -0.442239583f }, { 2047, -0.19392401f, 0.499310732f },
  8286. { 3000, 0.22835508f, -0.267458469f }, { 4095, -0.089728713f, -0.107289009f } });
  8287. // compressor section with a positive knee, 2 compressor curve stages and soft saturation (port within −143 dB)
  8288. check (run (limiter (limiterState ({ 1000, 900, 1000, 0, 3, 5000, 3, 2882, 400, 600, 400, 1115, 6666, 2, 650, 1000, 0, 4187 })), false),
  8289. { { 100, -0.00208989787f, 0.0100310864f }, { 1000, -0.0545219742f, -0.0651496574f }, { 2047, 0.177322432f, -0.230846375f },
  8290. { 3000, -0.159136176f, -0.183688983f }, { 4095, -0.300143838f, 0.299418598f } });
  8291. // sidechained compressor (side input 1) with a negative knee, noise gate and the exact-ceiling option
  8292. 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),
  8293. { { 100, -0.00186592329f, 0.00895605329f }, { 1000, -0.0375732854f, -0.0448972471f }, { 2047, 0.124697573f, -0.162336946f },
  8294. { 3000, -0.111069582f, -0.128206298f }, { 4095, -0.218446419f, 0.21791859f } });
  8295. auto compressor = [] (const std::vector<std::uint8_t>& st) {
  8296. auto slot = lynx::effectFromFl ("Fruity Compressor", st);
  8297. CHECK (slot && slot->kind == lynx::EffectKind::fruityCompressor);
  8298. return *slot;
  8299. };
  8300. // Vintage type, look-ahead flag, side input 1: bit-identical
  8301. const auto ca = run (compressor (compressorState (3, { -200, 40, 30, 50, 300, 2 }, 1, 1)), true);
  8302. CHECK (ca.latency == 64);
  8303. check (ca, { { 100, 0.0795554966f, -0.230549783f }, { 1000, 0.483903229f, 0.0241768137f }, { 2047, -0.596430957f, -0.441769004f },
  8304. { 3000, 0.597724319f, -0.18129006f }, { 4095, 0.607042372f, 0.435067475f } });
  8305. // Soft/R, version 2 without the flag (the output is still 64 frames late, but not reported): bit-identical
  8306. const auto cb = run (compressor (compressorState (2, { -150, 80, 0, 150, 100, 7 }, 0)), false);
  8307. CHECK (cb.latency == 0);
  8308. check (cb, { { 100, 0.0813739002f, -0.235819489f }, { 1000, 0.264188439f, 0.0131994039f }, { 2047, -0.260424435f, -0.192893147f },
  8309. { 3000, 0.251500547f, -0.0762802213f }, { 4095, 0.252972007f, 0.181305125f } });
  8310. // the slot's parameters carry FL's raw controls exactly (and survive the 4-decimal project file)
  8311. const auto st = limiterState ({ 1261, 995, 1303, 1115, 3, 5000, 3, 2882, 264, 244, -590, 1115, 6666, 1, 650, 1000, 0, 4187 }, 1);
  8312. const auto parsed = lynx::fldyn::parseLimiter (st);
  8313. CHECK (parsed && parsed->sidechain == 1 && ! parsed->exactCeiling);
  8314. const auto slot = limiter (st);
  8315. for (int i = 0; i < lynx::fldyn::limiterControls; ++i)
  8316. {
  8317. const auto rounded = std::round (slot.params[(std::size_t) i] * 10000.0f) / 10000.0f;
  8318. 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]);
  8319. }
  8320. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityLimiter, 3, slot.params[3], 120.0) == "2.0 ms");
  8321. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityCompressor, 5, 2.0f / 7.0f, 120.0) == "Vintage");
  8322. // unreadable states keep the old approximation
  8323. CHECK (lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 }).has_value() == false
  8324. || lynx::effectFromFl ("Fruity Limiter", { 7, 0, 0, 0 })->kind != lynx::EffectKind::fruityLimiter);
  8325. CHECK (! lynx::fldyn::parseCompressor ({ 4, 0, 0, 0 }));
  8326. }
  8327. TEST (maximus_engine)
  8328. {
  8329. // REA_rodada40: the engine on its own (MaximusProcessor), states of our own against the installation's DLLs
  8330. // (goldens = the DLL's output for the same input, EXEC), and the containers of the mixer effect and Vocodex
  8331. std::vector<float> inL (4096), inR (4096);
  8332. for (int i = 0; i < 4096; ++i)
  8333. {
  8334. 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));
  8335. inR[(std::size_t) i] = (float) (1.2 * std::sin (2.0 * 3.141592653589793 * 660.0 * i / 44100.0));
  8336. }
  8337. auto run = [&] (std::shared_ptr<const lynx::hlfx::MaximusPreset> preset, int mix) {
  8338. lynx::hlfx::MaximusProcessor m;
  8339. m.prepare (std::move (preset), 44100.0, mix);
  8340. std::vector<float> l (inL), r (inR);
  8341. for (int s = 0; s < 4096; s += 512)
  8342. m.process (l.data() + s, r.data() + s, 512);
  8343. return std::make_pair (l, r);
  8344. };
  8345. using Env = std::vector<std::array<double, 4>>;
  8346. const Env def { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8347. const Env knee { { 0.0, 0.0, 0.0, 0 }, { 0.35, 0.35, 0.0, 0 }, { 0.65, 0.6, 0.36, 7 } };
  8348. const Env t0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.14, 0 }, { 0.5, 0.5, 0.0, 0 } };
  8349. const Env m0 { { 0.0, 0.0, 0.0, 0 }, { 0.5, 0.5, 0.0, 0 }, { 0.5, 0.6, 0.085, 0 } };
  8350. 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,
  8351. int sus = 2882, int rms = 0) { return std::array<int, 13> { mode, pre, post, sep, sat, ceil, att, rel2, 2, 5000, 3, sus, rms }; };
  8352. auto check = [] (const std::pair<std::vector<float>, std::vector<float>>& out, const std::vector<std::tuple<int, float, float>>& golden) {
  8353. for (const auto& [i, gl, gr] : golden)
  8354. CHECK (std::abs (out.first[(std::size_t) i] - gl) < 2e-6f && std::abs (out.second[(std::size_t) i] - gr) < 2e-6f);
  8355. };
  8356. // RMS detectors, saturation mode B, a 5 Hz low cut, 12 dB high split (SG_x64.dll: port within −152 dB)
  8357. auto st3 = maximusTestState ({ 19800, 1, 48065, 0, 1115, 1000, 0 },
  8358. { band (0, 1500, 1000, 0, 400, 900, 1115, 0, 2882, 1), band (0, 1340, 1000, 0, 0, 1000, 1115, 0, 1570, 1),
  8359. band (1, 1000, 1200, 0, 1000, 1000), band (0, 1000, 1000, 0, 250, 1000, 1115, 4464, 2882, 1) },
  8360. { def, def, knee, def, t0, def, m0, def }, 0);
  8361. const auto p3 = lynx::hlfx::makeMaximusPreset (st3);
  8362. CHECK (p3 != nullptr);
  8363. check (run (p3, 1000), { { 100, 0.0f, 0.0f }, { 1000, 0.87061065f, 0.64004499f }, { 2047, -0.97266865f, 0.60940075f },
  8364. { 3000, 0.96658099f, 0.9250561f }, { 4095, 0.99094611f, -0.964059f } });
  8365. // the same with solo on (UI byte 0x1EF) and the low band selected (0x1E0): bit-identical to SG_x64.dll
  8366. auto solo = st3;
  8367. solo[0x1ef] = 1;
  8368. std::memset (solo.data() + 0x1e0, 0, 4);
  8369. check (run (lynx::hlfx::makeMaximusPreset (solo), 1000),
  8370. { { 100, 3.2922967e-06f, 0.00036916698f }, { 1000, 0.15653539f, -0.047913823f }, { 2047, -0.17336531f, 0.019099623f },
  8371. { 3000, 0.17185682f, -0.017173048f }, { 4095, 0.16714551f, 0.019473195f } });
  8372. // the legacy linear-phase FIR band split (UI byte 0x1FD; SG_x64.dll: port within −148 dB)
  8373. auto fir = st3;
  8374. fir[0x1fd] = 1;
  8375. check (run (lynx::hlfx::makeMaximusPreset (fir), 1000),
  8376. { { 100, 0.0f, 0.0f }, { 1000, 0.97986746f, 0.18555209f }, { 2047, -0.98930448f, -0.95970845f },
  8377. { 3000, 0.98856646f, -0.44592777f }, { 4095, 0.99069679f, 0.94382554f } });
  8378. // Soundgoodizer_x64.dll's own build with a v4 file (its presets' layout): a copy of the round-36 state without
  8379. // oversampling, through a byte-identical copy of the installation's DLL with this file as its preset (EXEC)
  8380. const auto st2 = maximusTestState ({ 19800, 1, 48065, 1, 891, 600, 32768 },
  8381. { band (1, 1264, 1296, 500, -500, 1200), band (0, 1340, 1000, -348, 0, 1000, 1115, 0, 1570),
  8382. band (0, 1364, 1152, 0, 0, 1000, 1115, 0, 1186), band (0, 1000, 1000, 0, -196, 1000, 1115, 4464, 1538) },
  8383. { def, def, knee, def, t0, def, m0, def }, 0);
  8384. std::vector<std::uint8_t> v4 { 4, 0, 0, 0 };
  8385. v4.insert (v4.end(), st2.begin() + 4, st2.begin() + 0x1f5);
  8386. v4.insert (v4.end(), st2.begin() + 0x1fe, st2.end());
  8387. const auto p4 = lynx::hlfx::makeMaximusPreset (v4, lynx::hlfx::MaximusBuild::soundgoodizer);
  8388. CHECK (p4 != nullptr);
  8389. check (run (p4, 600), { { 100, 0.0f, 0.0f }, { 1000, -0.60926896f, -0.6652832f }, { 2047, 0.40872875f, 0.36375371f },
  8390. { 3000, -0.41941205f, -0.61312866f }, { 4095, -0.12878418f, 0.27944946f } });
  8391. CHECK (lynx::hlfx::makeMaximusPreset (st2, lynx::hlfx::MaximusBuild::soundgoodizer) != nullptr); // v7: also read by that build
  8392. // the mixer effect's container and the effect itself
  8393. lynx::maximusfx::Blob blob;
  8394. blob.build = lynx::hlfx::MaximusBuild::soundgoodizer;
  8395. blob.mix = 600;
  8396. blob.state = v4;
  8397. const auto packed = lynx::maximusfx::pack (blob);
  8398. const auto back = lynx::maximusfx::unpack (packed);
  8399. CHECK (back && back->build == blob.build && back->mix == 600 && back->state == v4);
  8400. auto slot = lynx::makeEffectSlot (lynx::EffectKind::maximus);
  8401. slot.state = packed;
  8402. auto fx = lynx::makeEffect (slot);
  8403. fx->prepare (44100.0);
  8404. {
  8405. std::vector<float> l (inL), r (inR);
  8406. for (int s = 0; s < 4096; s += 1024) // longer host blocks are split into the engine's 512-frame calls
  8407. fx->process (l.data() + s, r.data() + s, 1024, slot.params, 120.0);
  8408. const auto ref = run (p4, 600);
  8409. CHECK (l == ref.first && r == ref.second);
  8410. }
  8411. const std::vector<std::uint8_t> sgState { 3, 0, 0, 0, 2, 0, 0, 0, 0x78, 2, 0, 0 };
  8412. const auto sg = lynx::maximusfx::parseSoundgoodizer (sgState);
  8413. CHECK (sg && sg->preset == 2 && sg->amount == 632 && std::string (lynx::maximusfx::soundgoodizerPresetFile (2)) == "c.dat");
  8414. CHECK (lynx::soundgoodizerPresetPath (sgState) == "Plugins\\Fruity\\Effects\\Soundgoodizer\\Data\\Presets\\c.dat");
  8415. CHECK (lynx::soundgoodizerExact (sgState, v4).has_value() && ! lynx::soundgoodizerExact (sgState, {}).has_value());
  8416. // Vocodex's appended installation data survives a round trip and leaves FL's state untouched
  8417. const std::vector<std::uint8_t> flState { 8, 0, 0, 0, 1, 2, 3 };
  8418. lynx::vocodex::InstallData data;
  8419. data.sgDat = st2;
  8420. data.carrierState = { 9, 9, 9 };
  8421. const auto joined = lynx::vocodex::appendInstallData (flState, data);
  8422. const auto [raw, files] = lynx::vocodex::splitInstallData (joined);
  8423. CHECK (raw == flState && files.sgDat == st2 && files.carrierState == data.carrierState);
  8424. CHECK (lynx::vocodex::splitInstallData (flState).first == flState);
  8425. }
  8426. // ---- Fruity Reeverb 2 (REA_rodada49) --------------------------------------------------------------------------------
  8427. // Goldens: the installation's Fruity Reeverb 2_x64.dll (unmodified, rate set before the state, flushed, blocks of 512)
  8428. // on our own states and input; LYNX's engine is bit-identical to it.
  8429. TEST (reeverb2_engine)
  8430. {
  8431. auto hex = [] (const char* h) {
  8432. std::vector<std::uint8_t> v;
  8433. for (std::size_t i = 0; h[i] != 0 && h[i + 1] != 0; i += 2)
  8434. v.push_back ((std::uint8_t) std::stoi (std::string (h + i, 2), nullptr, 16));
  8435. return v;
  8436. };
  8437. constexpr int N = 8192;
  8438. std::vector<float> inL (N), inR (N);
  8439. std::uint32_t seed = 12345;
  8440. for (int i = 0; i < N; ++i)
  8441. {
  8442. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  8443. const auto nz = (float) ((double) seed / 2147483648.0 - 0.5);
  8444. const auto tri = (float) ((i % 200) - 100) / 100.0f;
  8445. const float g = i < 4000 ? 1.0f : 0.0f;
  8446. inL[(std::size_t) i] = 0.5f * tri * g + (i >= 100 && i < 300 ? nz * 0.6f : 0.0f);
  8447. inR[(std::size_t) i] = 0.25f * tri * g;
  8448. }
  8449. struct Golden
  8450. {
  8451. int i;
  8452. float l, r;
  8453. };
  8454. auto run = [&] (const std::vector<std::uint8_t>& state, double bpm, const std::vector<Golden>& gold) {
  8455. const auto slot = lynx::reeverb2FromFl (state);
  8456. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb2);
  8457. if (! slot)
  8458. return;
  8459. auto fx = lynx::makeEffect (*slot);
  8460. fx->prepare (44100.0);
  8461. fx->reset();
  8462. std::vector<float> l (inL), r (inR);
  8463. for (int s = 0; s < N; s += 512)
  8464. fx->process (l.data() + s, r.data() + s, 512, slot->params, bpm);
  8465. for (const auto& g : gold)
  8466. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8467. };
  8468. // modulation, early reflections, negative separation, filters, predelay
  8469. run (hex ("112700004b000000780000001e0000004600000055000000190000003c0000009600000090010000e2ffffff6e000000640000008c00000084030000b00400000000"),
  8470. 120.0, { { 777, 0.263778746f, 0.131889373f }, { 1555, 0.187972173f, 0.0587911084f }, { 3333, 0.10398978f, 0.0566705838f },
  8471. { 5000, 0.087177977f, -0.0197417438f }, { 8191, 0.0769432634f, -0.282023013f } });
  8472. // Mid/Side input, tempo-synced predelay (27 ticks at 128 BPM), positive separation
  8473. run (hex ("112700004b000000280000001b00000032000000640000000f0000002800000064000000f401000028000000800000000000000040000000ac020000000000000101"),
  8474. 128.0, { { 777, 0.38499999f, 0.192499995f }, { 1555, 0.275000006f, 0.137500003f }, { 3333, 0.164420277f, 0.0826604739f },
  8475. { 5000, -0.00861243438f, -0.00140450173f }, { 8191, -0.00873835851f, 0.00888271444f } });
  8476. // version 10000 (no modulation controls), everything off or at its extreme, dry 0
  8477. run (hex ("1027000013000000dd000000000000006400000000000000c8000000dd00000014000000d00700004000000000000000a0000000a00000000000"),
  8478. 120.0, { { 777, 0.0f, 0.0f }, { 1555, 0.0f, 0.0f }, { 3333, 0.0632086769f, 0.0632086769f }, { 5000, -0.108072646f, -0.108072646f },
  8479. { 8191, 0.144686908f, 0.144686908f } });
  8480. // the parameters map back to FL's integers; unsupported states fall back to the approximation
  8481. lynx::reeverb2::Settings s;
  8482. s.p = { 3000, 5, 384, 1, 0, 200, 221, 20, 2000, -64, 160, 0, 160, 2048, 1 };
  8483. s.tempoPredelay = true;
  8484. lynx::EffectParams p {};
  8485. lynx::reeverb2::toParams (s, p);
  8486. CHECK (lynx::reeverb2::fromParams (p) == s);
  8487. CHECK (! lynx::reeverb2FromFl ({ 0x11, 0x27, 0, 0, 1, 0 }).has_value());
  8488. CHECK (! lynx::reeverb2FromFl ({ 0x12, 0x27, 0, 0 }).has_value());
  8489. const auto approx = lynx::effectFromFl ("Fruity Reeverb 2", { 0x11, 0x27, 0, 0 }, 120.0);
  8490. CHECK (! approx || approx->kind != lynx::EffectKind::reeverb2);
  8491. CHECK (lynx::reeverb2ParamText (0, 0.0f) == "off" && lynx::reeverb2ParamText (5, 1.0f) == "20.0 s");
  8492. // Fruity Reeverb (the first version): Fruity Reeverb_x64.dll goldens on our own states (denormals flushed, as the
  8493. // plugin runs and as LYNX renders)
  8494. lynx::ScopedFlushDenormals ftz;
  8495. auto run1 = [&] (const std::vector<std::uint8_t>& state, const std::vector<Golden>& gold) {
  8496. const auto slot = lynx::reeverb1FromFl (state);
  8497. CHECK (slot.has_value() && slot->kind == lynx::EffectKind::reeverb1);
  8498. if (! slot)
  8499. return;
  8500. auto fx = lynx::makeEffect (*slot);
  8501. fx->prepare (44100.0);
  8502. std::vector<float> l (inL), r (inR);
  8503. for (int s = 0; s < N; s += 512)
  8504. fx->process (l.data() + s, r.data() + s, 512, slot->params, 120.0);
  8505. for (const auto& g : gold)
  8506. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8507. };
  8508. run1 (hex ("01000000204e0000204e000025000000409c000050c300000a00000030750000a861000050c3000060ea0000"),
  8509. { { 777, 0.293731689f, 0.146865845f }, { 1555, 0.20980835f, 0.104904175f }, { 3333, 0.0998796821f, 0.0754205957f },
  8510. { 5000, -0.0994476378f, 0.0116700521f }, { 8191, -0.141061321f, 0.0601170398f } });
  8511. run1 (hex ("0100000000000000000001000000000000000100000001002800000000000100000001000000000000000100"),
  8512. { { 777, -0.016678432f, -4.58686594e-31f }, { 1555, 0.0221031085f, 0.0230635051f }, { 3333, 0.0875115693f, 0.1695389f },
  8513. { 5000, -0.111421227f, -0.295867324f }, { 8191, 0.503498077f, 0.190456569f } });
  8514. CHECK (lynx::reeverb1FromFl (std::vector<std::uint8_t> (44, 0)).has_value());
  8515. CHECK (! lynx::reeverb1FromFl (std::vector<std::uint8_t> (40, 0)).has_value());
  8516. CHECK (lynx::reeverb1ParamText (5, 0.0f) == "Brighter" && lynx::reeverb1ParamText (0, 0.0f) == "off");
  8517. }
  8518. // ---- Fruity WaveShaper / Fruity Soft Clipper (REA_rodada51) ------------------------------------------------------------
  8519. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  8520. namespace
  8521. {
  8522. std::vector<std::uint8_t> waveShaperTestState (int pre, int mix, int post, bool uni, bool center, int os, bool hq, bool smooth)
  8523. {
  8524. std::vector<std::uint8_t> b;
  8525. 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))); };
  8526. 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))); };
  8527. 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))); };
  8528. i32 (0x40003);
  8529. i32 (pre), i32 (mix), i32 (post);
  8530. b.push_back (uni ? 1 : 0), b.push_back (center ? 1 : 0);
  8531. i32 (os);
  8532. 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 },
  8533. { 0.25, 0.9, -0.3f, 3 }, { 0.2, 1.0, 0.5f, 9 } };
  8534. i32 (3), i32 (3), i32 (5);
  8535. for (const auto& p : pts)
  8536. {
  8537. f64 (p.dt), f64 (p.y), f32 (p.t);
  8538. b.push_back ((std::uint8_t) p.mode), b.push_back (0), b.push_back (0), b.push_back (0);
  8539. }
  8540. i32 (0), i32 (-1), i32 (-1), i32 (-1), i32 (-1);
  8541. i32 (1);
  8542. b.push_back (hq ? 1 : 0), b.push_back (smooth ? 1 : 0);
  8543. return b;
  8544. }
  8545. } // namespace
  8546. TEST (fruity_waveshaper_and_soft_clipper)
  8547. {
  8548. std::vector<float> inL (4096), inR (4096);
  8549. for (int n = 0; n < 4096; ++n)
  8550. {
  8551. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8552. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8553. }
  8554. struct Gold { int i; float l, r; };
  8555. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  8556. // Soft Clipper: bit-exact against Fruity Soft Clipper_x64.dll
  8557. auto clip = [&] (int thr, int post, std::initializer_list<Gold> gold) {
  8558. std::vector<float> l (inL), r (inR);
  8559. for (int s = 0; s < 4096; s += 512)
  8560. {
  8561. lynx::flshape::softClip (l.data() + s, 512, thr, post);
  8562. lynx::flshape::softClip (r.data() + s, 512, thr, post);
  8563. }
  8564. for (const auto& g : gold)
  8565. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8566. };
  8567. clip (100, 128, { { 100, 0.0f, -0.949052989f }, { 1000, -0.99617976f, 0.872887492f }, { 2047, 0.649999976f, -0.944175363f },
  8568. { 3000, -0.99617976f, -0.699999988f }, { 4095, 0.25f, 0.600000024f } });
  8569. clip (1, 150, { { 100, 0.0f, -1.04588532f }, { 1000, -1.27003884f, 0.930835366f }, { 2047, 0.750244498f, -1.03539562f },
  8570. { 3000, -1.27003884f, -0.790091455f }, { 4095, 0.347655922f, 0.708338201f } });
  8571. clip (64, 140, { { 100, 0.0f, -1.08492827f }, { 1000, -1.21535313f, 0.990323186f }, { 2047, 0.804165959f, -1.0770781f },
  8572. { 3000, -1.21535313f, -0.849189103f }, { 4095, 0.319320858f, 0.754407823f } });
  8573. {
  8574. const std::vector<std::uint8_t> st { 64, 0, 0, 0, 140, 0, 0, 0 };
  8575. const auto slot = lynx::effectFromFl ("Fruity Soft Clipper", st, 120.0);
  8576. CHECK (slot && slot->kind == lynx::EffectKind::fruitySoftClipper);
  8577. if (slot)
  8578. {
  8579. const auto s = lynx::flshape::softClipperFromParams (slot->params.data());
  8580. CHECK (s.threshold == 64 && s.post == 140);
  8581. auto fx = lynx::makeEffect (*slot);
  8582. fx->prepare (44100.0);
  8583. std::vector<float> l (inL), r (inR);
  8584. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  8585. CHECK (l[3000] == -1.21535313f && r[4095] == 0.754407823f);
  8586. }
  8587. }
  8588. // WaveShaper: tables (bipolar / unipolar) and the engine on three of our states, live and as FL renders
  8589. const auto stA = waveShaperTestState (150, 100, 120, false, true, 1, true, true); // CENTER, ×2, HQ, smooth
  8590. const auto stB = waveShaperTestState (100, 128, 128, true, false, 0, false, false); // unipolar
  8591. const auto stC = waveShaperTestState (192, 90, 100, false, false, 2, false, false); // ×4
  8592. const auto pA = lynx::flshape::parseWaveShaper (stA);
  8593. CHECK (pA && pA->points.size() == 5 && pA->pre == 150 && pA->center && pA->hq && pA->smooth && pA->oversampling == 1);
  8594. if (pA)
  8595. {
  8596. const auto T = lynx::flshape::shaperTable (pA->points, false);
  8597. CHECK (T.size() == 16386 && T[1] == 3277 && T[1001] == 18824 && T[5001] == 26195 && T[9001] == 11469 && T[12001] == 24614
  8598. && T[16384] == 32767 && T[0] == T[1]);
  8599. const auto U = lynx::flshape::shaperTable (pA->points, true);
  8600. CHECK (U.size() == 32770 && U[1] == -26214 && U[1001] == -6408 && U[5001] == 17073 && U[9001] == 19551 && U[12001] == 14549);
  8601. }
  8602. auto shape = [&] (const std::vector<std::uint8_t>& st, bool render, std::initializer_list<Gold> gold) {
  8603. const auto s = lynx::flshape::parseWaveShaper (st);
  8604. CHECK (s.has_value());
  8605. if (! s)
  8606. return;
  8607. lynx::flshape::WaveShaperEngine e;
  8608. e.setCurve (s->points);
  8609. e.prepare (44100.0);
  8610. e.setControls (s->pre, s->mix, s->post, s->oversampling, s->hq, s->smooth, s->center, s->unipolar, render);
  8611. std::vector<float> l (inL), r (inR);
  8612. for (int i = 0; i < 4096; i += 512)
  8613. e.process (l.data() + i, r.data() + i, 512);
  8614. for (const auto& g : gold)
  8615. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  8616. };
  8617. shape (stA, false, { { 100, 0.59108448f, 0.469315082f }, { 1000, 0.284353405f, 1.02621937f }, { 2047, -0.533613563f, 0.117038473f },
  8618. { 3000, 0.22673659f, -1.13572407f }, { 4095, -0.267153352f, 0.822866499f } });
  8619. shape (stA, true, { { 100, 0.794869065f, -0.127601087f }, { 1000, -0.00841622613f, 0.900729597f }, { 2047, 0.450603426f, -0.325943232f },
  8620. { 3000, -0.0530530401f, -0.32303831f }, { 4095, 1.0959363f, 0.946009099f } });
  8621. shape (stB, false, { { 100, -0.206451416f, 0.502471924f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514160156f },
  8622. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  8623. shape (stB, true, { { 100, -0.206390381f, 0.502493739f }, { 1000, -0.799987793f, 0.761169434f }, { 2047, 0.411743164f, 0.514144361f },
  8624. { 3000, -0.799987793f, 0.596221924f }, { 4095, -0.157653809f, 0.334106445f } });
  8625. shape (stC, false, { { 100, 0.317797363f, 0.0483714938f }, { 1000, -0.445489585f, -0.533142388f }, { 2047, 0.634525597f, 0.942175806f },
  8626. { 3000, -0.445489585f, 0.514707327f }, { 4095, 0.200391173f, -0.937523127f } });
  8627. shape (stC, true, { { 100, 0.61165309f, -0.0993360579f }, { 1000, 0.0375693105f, 0.667849183f }, { 2047, 0.353716731f, -0.223758236f },
  8628. { 3000, 0.0375693105f, -0.223787636f }, { 4095, 0.843308687f, 0.688474894f } });
  8629. // the mixer effect from the FL import: controls as parameters, the curve in the slot's state; host blocks of any size
  8630. const auto slot = lynx::effectFromFl ("Fruity WaveShaper", stC, 120.0);
  8631. CHECK (slot && slot->kind == lynx::EffectKind::fruityWaveShaper && slot->state == stC);
  8632. if (slot)
  8633. {
  8634. auto fx = lynx::makeEffect (*slot);
  8635. fx->prepare (44100.0);
  8636. std::vector<float> l (inL), r (inR);
  8637. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  8638. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  8639. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  8640. CHECK (close (l[1000], -0.445489585f) && close (r[2047], 0.942175806f) && close (l[4095], 0.200391173f));
  8641. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityWaveShaper, 3, slot->params[3], 120.0) == "x4");
  8642. }
  8643. // an unreadable state keeps the old approximation
  8644. CHECK (lynx::effectFromFl ("Fruity WaveShaper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  8645. CHECK (lynx::effectFromFl ("Fruity Soft Clipper", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  8646. }
  8647. // ---- Fruity Parametric EQ 2 / Fruity Parametric EQ (REA_rodada50) -----------------------------------------------------
  8648. // States written here from our own values (no FL preset data); goldens from the installation's DLLs (EXEC, blocks of 512)
  8649. namespace
  8650. {
  8651. struct PeqTestBand { int type, freq, width, gain, order; };
  8652. std::vector<std::uint8_t> peq2TestState (const PeqTestBand (&bands)[7], int main, int uiFlags, int options, int mask)
  8653. {
  8654. std::vector<std::uint8_t> b;
  8655. 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))); };
  8656. i32 (8);
  8657. for (int copy = 0; copy < 2; ++copy)
  8658. {
  8659. for (const auto& x : bands) i32 (x.gain);
  8660. for (const auto& x : bands) i32 (x.freq);
  8661. for (const auto& x : bands) i32 (x.width);
  8662. for (const auto& x : bands) i32 (x.type);
  8663. for (const auto& x : bands) i32 (x.order);
  8664. i32 (main);
  8665. }
  8666. i32 (uiFlags), i32 (0), b.push_back (0), i32 (2), i32 (0), i32 (2), i32 (0);
  8667. b.push_back ((std::uint8_t) options);
  8668. i32 (mask), i32 (1), i32 (0);
  8669. for (int v : { 3, 1, 2, 2, 2 }) i32 (v);
  8670. i32 (0);
  8671. return b;
  8672. }
  8673. std::vector<std::uint8_t> peq1TestState (const PeqTestBand (&bands)[7], int main)
  8674. {
  8675. std::vector<std::uint8_t> b;
  8676. 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))); };
  8677. for (const auto& x : bands) i32 (x.gain);
  8678. for (const auto& x : bands) i32 (x.freq);
  8679. for (const auto& x : bands) i32 (x.width);
  8680. for (const auto& x : bands) i32 (x.type);
  8681. i32 (main);
  8682. return b;
  8683. }
  8684. } // namespace
  8685. // EQUO test state (our own LCG curves, no FL data; REA_rodada61 equo/teststate.py builds the same bytes)
  8686. static std::vector<std::uint8_t> equoTestState (int ovs, int bw, bool add)
  8687. {
  8688. std::vector<std::uint8_t> b;
  8689. 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); };
  8690. auto putI = [&put] (std::int32_t v) { put (&v, 4); };
  8691. auto putF = [&put] (float v) { put (&v, 4); };
  8692. std::uint32_t seed = 12345;
  8693. auto rnd = [&seed] {
  8694. seed = (std::uint32_t) (((std::uint64_t) seed * 1103515245u + 12345u) & 0x7fffffffu);
  8695. return (float) ((double) (seed % 20001) / 10000.0);
  8696. };
  8697. putI (4);
  8698. for (int k = 0; k < 93; ++k)
  8699. {
  8700. putF (rnd());
  8701. putI (k % 11 == 0 ? 1 : (k % 13 == 0 ? 2 : 0));
  8702. }
  8703. for (int bank = 0; bank < 9; ++bank)
  8704. for (int k = 0; k < 93; ++k)
  8705. {
  8706. const float v = rnd();
  8707. putF (bank == 8 ? 0.75f + v * 0.25f : v);
  8708. }
  8709. for (int v : { 1500, 90, 2600, 170, 150, ovs, bw, -1, 0, 0 })
  8710. putI (v);
  8711. for (int v : { 0, add ? 1 : 0, 0 })
  8712. b.push_back ((std::uint8_t) v);
  8713. putI (1);
  8714. return b;
  8715. }
  8716. // Pitch Shifter state (version 7, our own settings; REA_rodada61 ps/teststate.py builds the same bytes)
  8717. static std::vector<std::uint8_t> pitchShifterTestState (std::array<int, 13> p, int engine, int detector)
  8718. {
  8719. std::vector<std::uint8_t> b;
  8720. auto putI = [&b] (std::int32_t v) { const auto* q = (const std::uint8_t*) &v; b.insert (b.end(), q, q + 4); };
  8721. putI (7);
  8722. for (int v : p)
  8723. putI (v);
  8724. putI (p[11]);
  8725. b.push_back (0);
  8726. putI (engine);
  8727. putI (detector);
  8728. putI (1000);
  8729. b.push_back (1);
  8730. b.push_back (0);
  8731. return b;
  8732. }
  8733. TEST (pitch_shifter_exact)
  8734. {
  8735. std::vector<float> inL (8192), inR (8192);
  8736. for (int n = 0; n < 8192; ++n)
  8737. {
  8738. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.3f;
  8739. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.3f;
  8740. }
  8741. struct Gold { int i; float l, r; };
  8742. // goldens: Pitch Shifter_x64.dll on the same states and signal (REA_rodada61)
  8743. auto run = [&] (const std::vector<std::uint8_t>& st, int latency, std::initializer_list<Gold> gold) {
  8744. lynx::psfl::State s;
  8745. CHECK (st.size() == 75 && lynx::psfl::parsePitchShifter (st.data(), st.size(), s));
  8746. // through the mixer effect: FL import → slot parameters → effect, odd host blocks
  8747. const auto slot = lynx::effectFromFl ("Pitch Shifter", st, 120.0);
  8748. CHECK (slot && slot->kind == lynx::EffectKind::flPitchShifter);
  8749. if (! slot)
  8750. return;
  8751. auto fx = lynx::makeEffect (*slot);
  8752. fx->prepare (44100.0);
  8753. std::vector<float> l (inL), r (inR);
  8754. for (int i = 0, k = 0; i < 8192; ++k)
  8755. {
  8756. const int n = std::min (8192 - i, k % 2 == 0 ? 700 : 333);
  8757. fx->process (l.data() + i, r.data() + i, n, slot->params, 120.0);
  8758. i += n;
  8759. }
  8760. CHECK (fx->latencySamples() == latency);
  8761. for (const auto& g : gold)
  8762. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8763. };
  8764. // "FFT" (pitch-synchronous) engine: +5 st, formant −3 st, correction 50 %, mix 79 %, accurate detector
  8765. run (pitchShifterTestState ({ 500, -300, 2, 1900, 8500, 200, 0, 0, 768, 0, 0, 32768, 0 }, 0, 1), 2457,
  8766. { { 1000, 0.0f, 0.0f }, { 3000, 0.142123833f, 0.0878119618f }, { 5000, 0.139498621f, -0.365035176f },
  8767. { 7000, 0.139498621f, 0.161137447f }, { 8191, 0.449655473f, 0.453882188f } });
  8768. // granular engine: −7 st, 60 ms grains, density 300 %, jitter 30 %, random 20 %, post delay with feedback 40 %
  8769. run (pitchShifterTestState ({ -700, 0, 2, 2400, 6000, 300, 30, 20, 200, 40, 1, 65536, 0 }, 1, 0), 0,
  8770. { { 1000, -0.0736689344f, -0.349420458f }, { 3000, 0.086801365f, 0.0471927896f }, { 5000, -0.0939825475f, 0.31712544f },
  8771. { 7000, -0.28462702f, -0.437854528f }, { 8191, 0.138916701f, -0.0426892526f } });
  8772. CHECK (lynx::formatEffectParam (lynx::EffectKind::flPitchShifter, 0, 0.75f, 120.0) == "+6.00 st");
  8773. }
  8774. TEST (equo_exact)
  8775. {
  8776. std::vector<float> inL (4096), inR (4096);
  8777. for (int n = 0; n < 4096; ++n)
  8778. {
  8779. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8780. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8781. }
  8782. struct Gold { int i; float l, r; };
  8783. // goldens: EQUO_x64.dll run on the same states and signal (REA_rodada61 §5)
  8784. auto run = [&] (const std::vector<std::uint8_t>& st, bool hq, bool ticks, std::initializer_list<Gold> gold) {
  8785. const auto s = lynx::equofx::parse (st);
  8786. CHECK (s.has_value());
  8787. if (! s)
  8788. return;
  8789. lynx::equofx::Engine e;
  8790. e.prepare (44100.0, hq);
  8791. e.set (*s);
  8792. std::vector<float> l (inL), r (inR);
  8793. if (! ticks)
  8794. e.process (l.data(), r.data(), 4096);
  8795. else
  8796. {
  8797. auto k = *s;
  8798. e.startFromConstruction();
  8799. for (int i = 0; i < 4096; i += 256)
  8800. {
  8801. if (i == 2048)
  8802. k.knob[2] = 5300, e.setKnobs (k);
  8803. e.tick();
  8804. e.process (l.data() + i, r.data() + i, 256);
  8805. }
  8806. }
  8807. for (const auto& g : gold)
  8808. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8809. };
  8810. const auto plain = equoTestState (0, 150, false);
  8811. run (plain, false, false, { { 100, -0.00154058414f, -0.00550224911f }, { 1000, -0.908149242f, 0.619277894f }, { 2047, 0.411961317f, -0.364051819f },
  8812. { 3000, -1.42823684f, -0.0602950938f }, { 4095, -0.0661701784f, 0.300809383f } });
  8813. run (equoTestState (1, 300, true), false, false,
  8814. { { 100, 0.0312003642f, 0.0184187777f }, { 1000, 0.0379521511f, 0.224126667f }, { 2047, -0.135689721f, 0.0444660783f },
  8815. { 3000, 0.113949843f, -1.00256324f }, { 4095, -0.176232055f, 0.79159987f } });
  8816. run (plain, true, false, { { 100, -0.00104017637f, 0.000872685807f }, { 1000, -0.0426855832f, 0.0402083024f }, { 2047, 0.191993102f, 0.174184501f },
  8817. { 3000, -0.114048228f, 0.419817507f }, { 4095, 0.0376945846f, -0.274040252f } });
  8818. run (plain, false, true, { { 100, -0.0019897793f, -0.00981196482f }, { 1000, -1.09574401f, 0.83203429f }, { 2047, 0.839113712f, -0.691361725f } });
  8819. // the mixer effect from the FL import: FL's state in the slot, the knobs in its parameters, any host block size
  8820. const auto slot = lynx::effectFromFl ("EQUO", plain, 140.0);
  8821. CHECK (slot && slot->kind == lynx::EffectKind::equo && slot->state == plain);
  8822. if (slot)
  8823. {
  8824. auto back = *lynx::equofx::parse (plain);
  8825. lynx::equofx::applyParams (slot->params, back);
  8826. CHECK (back.knob == lynx::equofx::parse (plain)->knob && back.bandwidth == 150 && back.oversample == 0);
  8827. auto fx = lynx::makeEffect (*slot);
  8828. fx->prepare (44100.0);
  8829. std::vector<float> l (inL), r (inR);
  8830. fx->process (l.data(), r.data(), 700, slot->params, 0.0);
  8831. fx->process (l.data() + 700, r.data() + 700, 3396, slot->params, 0.0);
  8832. // the engine glides its bands per processing call, as FL does per host buffer: compare with the engine run on
  8833. // the same 700 + 3396 split (sample 1000 differs from the single-call golden), and with the golden once settled
  8834. lynx::equofx::Engine ref;
  8835. ref.prepare (44100.0, false);
  8836. ref.set (*lynx::equofx::parse (plain));
  8837. std::vector<float> rl (inL), rr (inR);
  8838. ref.process (rl.data(), rr.data(), 700);
  8839. ref.process (rl.data() + 700, rr.data() + 700, 3396);
  8840. CHECK (l == rl && rr == r && r[3000] == -0.0602950938f);
  8841. CHECK (lynx::formatEffectParam (lynx::EffectKind::equo, 4, slot->params[4], 120.0, slot->params).find ("dB") != std::string::npos);
  8842. }
  8843. CHECK (! lynx::equofx::parse (std::vector<std::uint8_t> (100, 0)).has_value());
  8844. }
  8845. TEST (fruity_parametric_eq_exact)
  8846. {
  8847. std::vector<float> inL (4096), inR (4096);
  8848. for (int n = 0; n < 4096; ++n)
  8849. {
  8850. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8851. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8852. }
  8853. struct Gold { int i; float l, r; };
  8854. // high pass Steep 8, low shelf, steep peaking, a 0 dB peaking (0.2 s countdown), notch Gentle 4, high shelf Gentle 8,
  8855. // low pass Gentle 6
  8856. const PeqTestBand bands[7] = { { 3, 8000, 30000, 0, -3 }, { 5, 15000, 40124, 600, 0 }, { 6, 30000, 20000, -900, -2 },
  8857. { 6, 35000, 25412, 0, 0 }, { 4, 40000, 10000, 0, 1 }, { 7, 50000, 30000, 450, 3 },
  8858. { 1, 60000, 45000, 0, 2 } };
  8859. const PeqTestBand bands1[7] = { { 3, 6000, 20000, 0, 0 }, { 5, 15000, 30000, 600, 0 }, { 6, 30000, 20000, -900, 0 },
  8860. { 6, 35000, 25000, 0, 0 }, { 4, 40000, 10000, 0, 0 }, { 7, 50000, 30000, 450, 0 },
  8861. { 1, 60000, 45000, 0, 0 } };
  8862. auto run = [&] (const std::optional<lynx::peqfl::Settings>& s, std::initializer_list<Gold> gold) {
  8863. CHECK (s.has_value());
  8864. if (! s)
  8865. return;
  8866. lynx::peqfl::Engine e;
  8867. e.set (*s);
  8868. e.prepare (44100.0);
  8869. std::vector<float> l (inL), r (inR);
  8870. for (int i = 0; i < 4096; i += 512)
  8871. e.process (l.data() + i, r.data() + i, 512);
  8872. for (const auto& g : gold)
  8873. CHECK (l[(std::size_t) g.i] == g.l && r[(std::size_t) g.i] == g.r);
  8874. };
  8875. const auto plain = peq2TestState (bands, 300, 0, 0x60, 0x7F);
  8876. const auto hq = peq2TestState (bands, 300, 1, 0x60, 0x7F);
  8877. const auto ext = peq2TestState (bands, 300, 1, 0x62, 0x7F);
  8878. run (lynx::peqfl::parsePeq2 (plain), { { 100, 0.1340574324131012f, -0.3040016293525696f }, { 1000, 0.28402701020240784f, -0.23813000321388245f },
  8879. { 2047, -0.9301530122756958f, 0.13881002366542816f }, { 3000, 0.2846762537956238f, 0.48893973231315613f },
  8880. { 4095, -0.8481860756874084f, -0.2641225755214691f } });
  8881. run (lynx::peqfl::parsePeq2 (hq), { { 100, 0.19229719042778015f, 0.29938483238220215f }, { 1000, 0.12543821334838867f, -0.055617764592170715f },
  8882. { 2047, 1.0342068672180176f, 0.10901166498661041f }, { 3000, 0.12598788738250732f, -0.12920552492141724f },
  8883. { 4095, 0.1619236022233963f, -0.23385579884052277f } });
  8884. run (lynx::peqfl::parsePeq2 (ext), { { 100, 1.2178642749786377f, -0.21160493791103363f }, { 1000, -0.8292768001556396f, -0.23916664719581604f },
  8885. { 2047, -0.7030635476112366f, -0.1978478729724884f }, { 3000, -0.8299900889396667f, 0.9836107492446899f },
  8886. { 4095, 0.06754066050052643f, -0.34364908933639526f } });
  8887. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, -250, 1, 0x60, 0x5B)), // bands 3 and 6 muted
  8888. { { 100, -0.02959616854786873f, 0.227821946144104f }, { 1000, 0.053224340081214905f, 0.06922465562820435f },
  8889. { 2047, 0.3365536034107208f, -0.05899651721119881f }, { 3000, 0.053670305758714676f, -0.07452142238616943f },
  8890. { 4095, -0.04406970739364624f, -0.12635089457035065f } });
  8891. run (lynx::peqfl::parsePeq1 (peq1TestState (bands1, 150)),
  8892. { { 100, -0.6533108353614807f, -1.6903921365737915f }, { 1000, -4.313726902008057f, 5.973916053771973f },
  8893. { 2047, 1.0222549438476562f, 4.427900314331055f }, { 3000, -4.310323238372803f, 7.825916767120361f },
  8894. { 4095, 8.344571113586426f, -0.49086111783981323f } });
  8895. // "alternative design" (Frequency Splitter's matched biquads), bit-exact
  8896. run (lynx::peqfl::parsePeq2 (peq2TestState (bands, 300, 3, 0x60, 0x7F)),
  8897. { { 100, -0.790181577205658f, -0.08691816031932831f }, { 1000, 0.4960691034793854f, -0.24172282218933105f },
  8898. { 2047, -0.42601829767227173f, 0.027932533994317055f }, { 3000, 0.49674150347709656f, 1.0799059867858887f },
  8899. { 4095, -0.35040369629859924f, -0.28149813413619995f } });
  8900. // linear phase (plain and HQ response): FFT convolution, 1536-sample latency, 0.1 s fade-in; FL's IPP FFT rounding
  8901. // is not replicated, so within 1e-6 of the DLL
  8902. for (const bool hq : { false, true })
  8903. {
  8904. const auto s = lynx::peqfl::parsePeq2 (peq2TestState (bands, hq ? -200 : 300, hq ? 1 : 0, 0x61, 0x7F));
  8905. CHECK (s && s->linearPhase);
  8906. if (! s)
  8907. continue;
  8908. std::vector<float> l (12288), r (12288);
  8909. for (int n = 0; n < 12288; ++n)
  8910. {
  8911. l[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8912. r[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8913. }
  8914. lynx::peqfl::Engine e;
  8915. e.set (*s);
  8916. e.prepare (44100.0);
  8917. CHECK (e.latencySamples() == 1536);
  8918. for (int i = 0; i < 12288; i += 512)
  8919. e.process (l.data() + i, r.data() + i, 512);
  8920. const std::initializer_list<Gold> gold = hq ? std::initializer_list<Gold> {
  8921. { 2000, 0.08618364483118057f, 0.11248858273029327f }, { 5000, 0.18998730182647705f, 0.428600937128067f },
  8922. { 8191, -0.1412065178155899f, -0.002996620489284396f }, { 10000, 0.18998737633228302f, -0.012570115737617016f },
  8923. { 12287, 0.40586185455322266f, -0.014698396436870098f } } : std::initializer_list<Gold> {
  8924. { 2000, 0.18538671731948853f, 0.206522598862648f }, { 5000, 0.4086928963661194f, 0.8167467713356018f },
  8925. { 8191, -0.3218550980091095f, -0.06812914460897446f }, { 10000, 0.40869277715682983f, -0.08537150919437408f },
  8926. { 12287, 0.775394082069397f, -0.08931504935026169f } };
  8927. for (const auto& g : gold)
  8928. 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);
  8929. }
  8930. // the mixer effect from the FL import: FL's values in the slot's parameters (exact round trip), any host block size
  8931. for (const auto* name : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  8932. {
  8933. const bool v1 = std::string (name) == "Fruity Parametric EQ";
  8934. const auto st = v1 ? peq1TestState (bands1, 150) : ext;
  8935. const auto slot = lynx::effectFromFl (name, st, 120.0);
  8936. CHECK (slot && slot->kind == lynx::EffectKind::parametricEq && lynx::peq::flLaw (slot->params));
  8937. if (! slot)
  8938. continue;
  8939. const auto back = lynx::peqfl::fromParams (slot->params);
  8940. const auto orig = v1 ? lynx::peqfl::parsePeq1 (st) : lynx::peqfl::parsePeq2 (st);
  8941. CHECK (back.freq == orig->freq && back.gain == orig->gain && back.width == orig->width && back.type == orig->type
  8942. && back.order == orig->order && back.main == orig->main && back.processing == orig->processing && back.mask == orig->mask);
  8943. auto fx = lynx::makeEffect (*slot);
  8944. fx->prepare (44100.0);
  8945. CHECK (fx->latencySamples() == (v1 ? 0 : 44));
  8946. std::vector<float> l (inL), r (inR);
  8947. fx->process (l.data(), r.data(), 1000, slot->params, 120.0);
  8948. fx->process (l.data() + 1000, r.data() + 1000, 3096, slot->params, 120.0);
  8949. if (v1)
  8950. CHECK (l[3000] == -4.310323238372803f && r[4095] == -0.49086111783981323f);
  8951. else
  8952. CHECK (l[1000] == -0.8292768001556396f && r[3000] == 0.9836107492446899f && l[4095] == 0.06754066050052643f);
  8953. // editor texts and graph
  8954. CHECK (lynx::formatEffectParam (lynx::EffectKind::parametricEq, lynx::peq::slopeParam, slot->params[lynx::peq::slopeParam], 120.0, slot->params)
  8955. == (v1 ? "12 dB/oct" : "Steep 8"));
  8956. CHECK (lynx::peq::widthText (slot->params, 0).rfind ("BW ", 0) == 0);
  8957. const auto lowCutDb = lynx::peq::responseDb (slot->params, 44100.0, 25.0), midDb = lynx::peq::responseDb (slot->params, 44100.0, 1000.0);
  8958. CHECK (lowCutDb < midDb - 5.0); // high pass below ~100 Hz (Steep 8 in the EQ 2 state, 12 dB/oct in v1)
  8959. }
  8960. // a native LYNX EQ is untouched by the FL law
  8961. CHECK (! lynx::peq::flLaw (lynx::makeEffectSlot (lynx::EffectKind::parametricEq).params));
  8962. }
  8963. TEST (fruity_panomatic_balance_blood_overdrive)
  8964. {
  8965. // REA_rodada58: goldens from the installation's DLLs (EXEC) on our own states, 44.1 kHz, blocks of 512
  8966. const lynx::ScopedFlushDenormals ftz;
  8967. std::vector<float> inL (4096), inR (4096);
  8968. for (int n = 0; n < 4096; ++n)
  8969. {
  8970. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  8971. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  8972. }
  8973. struct Gold { int i; float l, r; };
  8974. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  8975. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, std::initializer_list<Gold> gold) {
  8976. for (const auto& g : gold)
  8977. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  8978. };
  8979. auto st32 = [] (std::initializer_list<int> v) {
  8980. std::vector<std::uint8_t> b;
  8981. for (int x : v)
  8982. for (int k = 0; k < 4; ++k)
  8983. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  8984. return b;
  8985. };
  8986. // PanOMatic: the engine started as LYNX starts it (one silent frame, then FL's flush), and from silence (fade-in)
  8987. auto pom = [&] (lynx::flbasic::PanOMaticState s, bool prime, std::initializer_list<Gold> gold) {
  8988. lynx::flbasic::PanOMaticEngine e;
  8989. e.prepare (44100);
  8990. e.set (s);
  8991. if (prime)
  8992. e.prime();
  8993. std::vector<float> l (inL), r (inR);
  8994. for (int i = 0; i < 4096; i += 512)
  8995. e.process (l.data() + i, r.data() + i, 512);
  8996. check (l, r, gold);
  8997. };
  8998. pom ({ 40, 300, 2, 1, 128, 50000 }, true, { { 100, 0.0f, -1.44884729f }, { 1000, -1.78870034f, 0.778897762f }, { 2047, 0.697593153f, -1.26396084f },
  8999. { 3000, -1.78870034f, -1.32851648f }, { 4095, 0.268305063f, 0.851258814f } });
  9000. pom ({ 200, 180, 1, 2, 100, 30000 }, true, { { 100, 0.0f, -0.347919852f }, { 1000, -0.322590947f, 0.0788071603f }, { 2047, 0.126614109f, -0.26421836f },
  9001. { 3000, -0.326532423f, -0.435213327f }, { 4095, 0.0493027233f, 0.226084977f } });
  9002. pom ({ 90, 256, 0, 1, 77, 40000 }, false, { { 100, -0.0292895138f, -0.110000029f }, { 1000, -1.31911683f, 0.666457176f }, { 2047, 0.477211952f, -0.864247262f },
  9003. { 3000, -1.59167564f, -0.591367304f }, { 4095, 0.268362761f, 0.537029684f } });
  9004. // Balance: flushed and from silence
  9005. auto bal = [&] (lynx::flbasic::BalanceState s, bool flush, std::initializer_list<Gold> gold) {
  9006. lynx::flbasic::BalanceEngine e;
  9007. e.prepare (44100);
  9008. e.set (s);
  9009. if (flush)
  9010. e.flush();
  9011. std::vector<float> l (inL), r (inR);
  9012. for (int i = 0; i < 4096; i += 512)
  9013. e.process (l.data() + i, r.data() + i, 512);
  9014. check (l, r, gold);
  9015. };
  9016. bal ({ 77, 100 }, true, { { 100, 0.0f, -0.136579692f }, { 1000, -0.0669578835f, -0.0282340273f }, { 2047, 0.0261135735f, -0.0795038193f },
  9017. { 3000, -0.0669578835f, -0.226895392f }, { 4095, 0.0100436825f, 0.0954951793f } });
  9018. bal ({ -128, 320 }, false, { { 100, -0.28914085f, 0.0f }, { 1000, -1.03179514f, 0.0f }, { 2047, -0.578702569f, 0.0f },
  9019. { 3000, -3.18510628f, 0.0f }, { 4095, 1.14394665f, 0.0f } });
  9020. // Blood Overdrive: rate first, then the state (its smoothers settle for one second of samples)
  9021. auto od = [&] (std::array<int, 6> raw, std::initializer_list<Gold> gold) {
  9022. lynx::flbasic::BloodOverdriveEngine e;
  9023. e.start (44100, { raw });
  9024. std::vector<float> l (inL), r (inR);
  9025. for (int i = 0; i < 4096; i += 512)
  9026. e.process (l.data() + i, r.data() + i, 512);
  9027. check (l, r, gold);
  9028. };
  9029. od ({ 3000, 7000, 5000, 1, 9000, 5000 }, { { 100, -0.0442250744f, 0.170355186f }, { 1000, 0.0470311977f, 0.0765217245f },
  9030. { 2047, 0.0261161141f, -0.0341798998f }, { 3000, 0.0470311977f, 0.0213619489f },
  9031. { 4095, 0.0402069278f, 0.0499244779f } });
  9032. // the mixer effects from the FL import: controls as parameters; host blocks of any size
  9033. auto viaImport = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  9034. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  9035. CHECK (slot && slot->kind == kind);
  9036. if (! slot)
  9037. return;
  9038. auto fx = lynx::makeEffect (*slot);
  9039. fx->prepare (44100.0);
  9040. std::vector<float> l (inL), r (inR);
  9041. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  9042. fx->process (l.data() + 1024, r.data() + 1024, 1024, slot->params, 120.0);
  9043. fx->process (l.data() + 2048, r.data() + 2048, 2048, slot->params, 120.0);
  9044. check (l, r, gold);
  9045. };
  9046. viaImport ("Fruity PanOMatic", st32 ({ 40, 300, 2, 1, 128, 50000 }), lynx::EffectKind::fruityPanOMatic,
  9047. { { 1000, -1.78870034f, 0.778897762f }, { 4095, 0.268305063f, 0.851258814f } });
  9048. viaImport ("Fruity Balance", st32 ({ 0, 256 }), lynx::EffectKind::fruityBalance,
  9049. { { 100, 0.0f, -1.10000002f }, { 1000, -1.66666663f, 0.899999976f }, { 4095, 0.25f, 0.600000024f } });
  9050. viaImport ("Fruity Blood Overdrive", st32 ({ 3, 0, 5000, 2578, 1, 0, 2911, 0, 0 }), lynx::EffectKind::fruityBloodOverdrive,
  9051. { { 100, -0.122698173f, 0.0206716377f }, { 2047, 0.0128381588f, -0.0274834726f }, { 4095, -0.0899387598f, -0.121109024f } });
  9052. {
  9053. const auto slot = lynx::effectFromFl ("Fruity PanOMatic", st32 ({ 119, 256, 1, 2, 64, 27500 }), 120.0);
  9054. CHECK (slot && lynx::flbasic::panOMaticFromParams (slot->params.data()).pan == 119
  9055. && lynx::flbasic::panOMaticFromParams (slot->params.data()).target == 2);
  9056. if (slot)
  9057. {
  9058. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 2, slot->params[2], 120.0) == "Triangle");
  9059. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityPanOMatic, 1, slot->params[1], 120.0) == "+0.0 dB");
  9060. }
  9061. }
  9062. // unreadable states keep the old approximations
  9063. CHECK (lynx::effectFromFl ("Fruity PanOMatic", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  9064. CHECK (lynx::effectFromFl ("Fruity Balance", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::utility);
  9065. CHECK (lynx::effectFromFl ("Fruity Blood Overdrive", { 1, 2, 3 }, 120.0)->kind == lynx::EffectKind::distortion);
  9066. }
  9067. // ---- Sytrus (REA_rodada31) -------------------------------------------------------------------------------------------
  9068. // A Sytrus state built from scratch (version 25 layout, our own values — no FL preset data), zlib "stored" blocks.
  9069. namespace
  9070. {
  9071. struct SytrusTestPatch
  9072. {
  9073. std::vector<std::int32_t> main = std::vector<std::int32_t> (240, 0); // 962-byte main block as ints (+ mute bytes)
  9074. struct Op
  9075. {
  9076. int p[10] { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0 };
  9077. int ratio = 20000, offset = 0, noise = 0;
  9078. std::vector<float> amps { 1.0f };
  9079. std::uint8_t flags[8] {};
  9080. int env1[3] { -1, -1, -1 }; // volume envelope: on (≠ −1 → on), decay point, sustain point
  9081. std::vector<std::pair<double, double>> env1pts;
  9082. } ops[6];
  9083. struct Filt
  9084. {
  9085. int p[10] { 0, 0, 128, 0, 128, 0, 0, 128, 128, 0 };
  9086. int type = 3;
  9087. } filts[3];
  9088. std::array<std::array<std::uint8_t, 23>, 3> delays {};
  9089. std::array<std::int32_t, 9> reverb { 19, 221, 0, 50, 80, 30, 221, 2, 0 };
  9090. bool reverbOn = false;
  9091. int eqmode = 0;
  9092. SytrusTestPatch()
  9093. {
  9094. main[0] = 128, main[1] = 128;
  9095. for (int i = 0; i < 8; ++i)
  9096. main[(std::size_t) (165 + i)] = (i % 4 == 2) ? 0 : 128;
  9097. main[173] = 1, main[175] = 128, main[188] = 128, main[189] = -1, main[190] = main[191] = 256;
  9098. }
  9099. void cell (int layer, int row, int col, int v) { main[(std::size_t) (3 + layer * 81 + row * 9 + col)] = v; }
  9100. std::vector<std::uint8_t> blob() const
  9101. {
  9102. std::vector<std::uint8_t> b;
  9103. 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))); };
  9104. auto f32 = [&] (float v) { std::int32_t x; std::memcpy (&x, &v, 4); i32 (x); };
  9105. 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))); };
  9106. // point lists: kind 1 envelope, 2 LFO, 3 map / shaper curve; format version 3
  9107. auto env = [&] (int kind, const std::vector<std::pair<double, double>>& pts, int on, int decay, int sus) {
  9108. i32 (kind), i32 (3), i32 ((std::int32_t) pts.size());
  9109. for (const auto& [dt, y] : pts)
  9110. f64 (dt), f64 (y), f32 (0.0f), i32 (0);
  9111. i32 (0), i32 (on), i32 (decay), i32 (-1), i32 (sus);
  9112. };
  9113. for (int i = 0; i < 240; ++i)
  9114. if (4 * i + 4 <= 0x320)
  9115. i32 (main[(std::size_t) i]);
  9116. b.resize (962, 0); // mute bytes: none muted
  9117. i32 (0); // global declick amount
  9118. b.push_back (0), b.push_back (0), b.push_back (0); // OSC grouping, DC stage, GIBBS OFF
  9119. for (int k = 0; k < 9; ++k)
  9120. b.push_back (k == 7 ? 1 : 0); // flag group (release multiplier on, as Default.fst)
  9121. for (const auto& d : delays)
  9122. b.insert (b.end(), d.begin(), d.end());
  9123. for (auto v : reverb)
  9124. i32 (v);
  9125. b.push_back (reverbOn ? 1 : 0), b.push_back (0);
  9126. i32 (eqmode);
  9127. for (int k = 0; k < 3; ++k)
  9128. i32 (0), i32 (30000), i32 (20000), i32 (k);
  9129. b.push_back (0); // no info text
  9130. for (int u = 0; u < 10; ++u)
  9131. {
  9132. const int narts = u < 6 ? 6 : (u < 9 ? 8 : 2);
  9133. for (int j = 0; j < 10; ++j)
  9134. i32 (u < 6 ? ops[u].p[j] : (u < 9 ? filts[u - 6].p[j] : 0));
  9135. if (u < 6)
  9136. {
  9137. const auto& o = ops[u];
  9138. const auto at = b.size();
  9139. b.resize (at + 0x828, 0);
  9140. auto put = [&] (std::size_t off, const void* src, std::size_t n) { std::memcpy (b.data() + at + off, src, n); };
  9141. put (0, &o.ratio, 4), put (4, &o.offset, 4), put (8, &o.noise, 4);
  9142. put (0xc, o.amps.data(), 4 * o.amps.size());
  9143. const std::int32_t shape[5] { 0, 0, 0, 0, 128 };
  9144. put (0x80c, shape, sizeof shape);
  9145. put (0x820, o.flags, 8);
  9146. }
  9147. else if (u < 9)
  9148. {
  9149. const auto at = b.size();
  9150. b.resize (at + 0x30, 0);
  9151. std::memcpy (b.data() + at + 0x2c, &filts[u - 6].type, 4);
  9152. env (3, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  9153. b.push_back (0);
  9154. }
  9155. for (int k = 0; k < narts; ++k)
  9156. {
  9157. const bool add = k == 0 || k == 3 || (u >= 6 && u <= 8 && (k == 2 || k == 3)) || (u < 6 && k == 4);
  9158. const double neutral = add ? 0.5 : 1.0;
  9159. i32 (3), i32 (0), f32 (1.0f), f32 (1.0f);
  9160. if (u < 6 && k == 1 && ops[u].env1[0] != -1)
  9161. env (1, ops[u].env1pts, -1, ops[u].env1[1], ops[u].env1[2]);
  9162. else
  9163. env (1, { { 0.0, 0.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  9164. i32 (128), i32 (128), i32 (0), i32 (128);
  9165. env (2, { { 0.0, 1.0 }, { 1.0, 1.0 } }, 0, -1, -1);
  9166. i32 (40000), i32 (0), i32 (0), i32 (0), i32 (0);
  9167. for (int m = 0; m < 6; ++m)
  9168. env (3, { { 0.0, neutral }, { 1.0, neutral } }, 0, -1, -1);
  9169. }
  9170. }
  9171. b.push_back (0), b.push_back (0);
  9172. // zlib with stored blocks
  9173. std::vector<std::uint8_t> z { 0x78, 0x01 };
  9174. for (std::size_t at = 0; at < b.size(); at += 65535)
  9175. {
  9176. const auto n = std::min<std::size_t> (65535, b.size() - at);
  9177. z.push_back (at + n >= b.size() ? 1 : 0);
  9178. z.push_back ((std::uint8_t) n), z.push_back ((std::uint8_t) (n >> 8));
  9179. z.push_back ((std::uint8_t) ~n), z.push_back ((std::uint8_t) (~n >> 8));
  9180. z.insert (z.end(), b.begin() + (std::ptrdiff_t) at, b.begin() + (std::ptrdiff_t) (at + n));
  9181. }
  9182. std::uint32_t a = 1, c = 0;
  9183. for (auto x : b)
  9184. a = (a + x) % 65521, c = (c + a) % 65521;
  9185. for (int k = 3; k >= 0; --k)
  9186. z.push_back ((std::uint8_t) (((c << 16) | a) >> (8 * k)));
  9187. std::vector<std::uint8_t> out { 25, 0, 0, 0 };
  9188. out.insert (out.end(), z.begin(), z.end());
  9189. return out;
  9190. }
  9191. };
  9192. // renders like FL's Gen_Render calls: notes start/release at the block that contains them
  9193. std::pair<std::vector<float>, std::vector<float>> sytrusRender (const std::vector<std::uint8_t>& blob, bool renderMode, int n,
  9194. std::vector<std::array<int, 3>> notes, double fs = 44100.0)
  9195. {
  9196. lynx::sytrus::Engine e;
  9197. e.setTempo (120.0);
  9198. e.prepare (fs, renderMode, 512);
  9199. e.setPatch (lynx::sytrus::parsePatch (blob));
  9200. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  9201. for (int n0 = 0; n0 < n; n0 += 512)
  9202. {
  9203. for (std::size_t k = 0; k < notes.size(); ++k)
  9204. {
  9205. if (notes[k][1] >= n0 && notes[k][1] < n0 + 512)
  9206. {
  9207. lynx::sytrus::NoteParams p;
  9208. p.key = (float) notes[k][0];
  9209. p.id = (std::int64_t) k + 1;
  9210. e.noteOn (p);
  9211. }
  9212. if (notes[k][2] >= n0 && notes[k][2] < n0 + 512)
  9213. e.release ((std::int64_t) k + 1);
  9214. }
  9215. e.render (l.data() + n0, r.data() + n0, std::min (512, n - n0), 1.0f, 0.0f);
  9216. }
  9217. return { l, r };
  9218. }
  9219. } // namespace
  9220. TEST (sytrus_engine_sine)
  9221. {
  9222. // one sine operator, ratio 1, OUT 256, no envelopes: C5 = 523.2511 Hz at 0.7071 (centre of the pan law, §6.1)
  9223. SytrusTestPatch t;
  9224. t.cell (0, 0, 8, 256);
  9225. const auto blob = t.blob();
  9226. CHECK (lynx::sytrus::stateUsable (blob));
  9227. CHECK (lynx::sytrus::parsePatch (blob) != nullptr);
  9228. auto broken = blob;
  9229. broken.resize (broken.size() / 2);
  9230. CHECK (! lynx::sytrus::stateUsable (broken));
  9231. const auto [l, r] = sytrusRender (blob, false, 8192, { { 60, 0, 4096 } });
  9232. double worst = 0.0;
  9233. for (int k = 0; k < 4000; ++k)
  9234. 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)));
  9235. CHECK (worst < 2e-5);
  9236. CHECK (l == r);
  9237. // 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
  9238. // 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)
  9239. int last = 0;
  9240. for (int k = 0; k < 8192; ++k)
  9241. if (l[(std::size_t) k] != 0.0f)
  9242. last = k;
  9243. CHECK (last == 4895);
  9244. // key 69 = 880 Hz; 48 kHz
  9245. const auto [l2, r2] = sytrusRender (blob, false, 4096, { { 69, 0, -1 } }, 48000.0);
  9246. worst = 0.0;
  9247. for (int k = 0; k < 4000; ++k)
  9248. 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)));
  9249. CHECK (worst < 5e-5);
  9250. (void) r2;
  9251. }
  9252. TEST (sytrus_engine_reference)
  9253. {
  9254. // FM 2→1, Vanilla LP filter, volume envelope, unison 3, delay and reverb, 2× oversampling with the 8-sample
  9255. // envelope tick (render settings): against our Python reference model of REA_rodada31 (sytmodel.render)
  9256. SytrusTestPatch t;
  9257. t.main[195] = 0, t.main[196] = 1, t.main[193] = 0, t.main[194] = 1;
  9258. t.main[173] = 3, t.main[174] = 64, t.main[176] = 100;
  9259. t.ops[1].ratio = 40000;
  9260. t.cell (0, 0, 1, 128); // op 2 → op 1 FM
  9261. t.cell (0, 0, 8, 128);
  9262. t.cell (0, 6, 0, 256); // op 1 → filter 1
  9263. t.cell (0, 6, 8, 256);
  9264. t.cell (0, 0, 7, 200); // FX send
  9265. t.filts[0].p[0] = 1, t.filts[0].p[2] = 180, t.filts[0].p[3] = 60;
  9266. t.ops[0].env1[0] = 0, t.ops[0].env1[1] = 1, t.ops[0].env1[2] = 2;
  9267. t.ops[0].env1pts = { { 0.0, 0.0 }, { 0.05, 1.0 }, { 0.1, 0.6 }, { 0.2, 0.0 } };
  9268. const std::int32_t d0[5] { 40, 100, 50, 100, 0 };
  9269. std::memcpy (t.delays[0].data(), d0, 20);
  9270. t.delays[0][22] = 1;
  9271. t.reverb = { 19, 221, 10, 50, 80, 30, 100, 2, 100 };
  9272. t.reverbOn = true;
  9273. const auto blob = t.blob();
  9274. const auto [l, r] = sytrusRender (blob, true, 12000, { { 60, 0, 6000 } });
  9275. // reference values (model render_mode=True, 44.1 kHz, block 512, key 60, velocity 0.78125, release at 6000); the same
  9276. // state rendered by FL's own Sytrus DLL in render mode differs from this by −136 dB (EXEC, REA round 31 harness)
  9277. const std::array<std::pair<int, float>, 8> golden { { { 100, -0.00235529384f }, { 777, -0.175126165f }, { 2000, 0.30976975f }, { 4321, -0.0213534199f },
  9278. { 6100, 0.202372357f }, { 7000, 0.249530748f }, { 9000, 0.0307101421f }, { 11999, 0.176390275f } } };
  9279. for (const auto& [i, v] : golden)
  9280. CHECK (std::abs (l[(std::size_t) i] - v) < 1e-6f);
  9281. const std::array<std::pair<int, float>, 3> goldenR { { { 777, -0.166890964f }, { 4321, -0.212358952f }, { 9000, -0.0251909085f } } };
  9282. for (const auto& [i, v] : goldenR)
  9283. CHECK (std::abs (r[(std::size_t) i] - v) < 1e-6f);
  9284. }
  9285. TEST (sytrus_instrument)
  9286. {
  9287. // the LYNX instrument: state in ChannelDef::pluginState, channel volume/pan recovered from the rack gains
  9288. SytrusTestPatch t;
  9289. t.cell (0, 0, 8, 256);
  9290. lynx::ChannelDef c;
  9291. c.instrument = lynx::InstrumentKind::sytrus;
  9292. c.pluginState = t.blob();
  9293. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sytrus);
  9294. inst->prepare (44100.0);
  9295. inst->configure (c, 120.0);
  9296. CHECK (inst->idle());
  9297. std::vector<float> l (4096), r (4096);
  9298. inst->noteOn (60, 100.0f / 128.0f);
  9299. // 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
  9300. inst->render (l.data(), r.data(), 4096, 0.5f, 0.5f);
  9301. double peak = 0.0;
  9302. for (int k = 1000; k < 4096; ++k)
  9303. peak = std::max (peak, (double) std::abs (l[(std::size_t) k]));
  9304. CHECK (std::abs (peak - 0.5 * 0.70710678) < 1e-3); // Final.Vol 0.5 × the plugin's centre pan law
  9305. CHECK (! inst->idle());
  9306. inst->noteOff (60);
  9307. std::vector<float> tail (44100 * 3), tailR (tail.size());
  9308. inst->render (tail.data(), tailR.data(), (int) tail.size(), 0.5f, 0.5f);
  9309. CHECK (inst->idle());
  9310. CHECK (tail.back() == 0.0f);
  9311. }
  9312. TEST (harmor_effects_rack)
  9313. {
  9314. // REA_rodada38: the rack runs in the state's unit order and the records' FX send joins it at unit 4 — with the FX
  9315. // amount at 1000 every record output bypasses a reverb placed before unit 4 (EXEC: −600 dB vs the rack off)
  9316. auto blob = harmorTestState (false);
  9317. auto set = [&] (std::vector<std::uint8_t>& st, int part, int index, std::int32_t v) {
  9318. std::memcpy (st.data() + lynx::harmorParamOffset (st, part, index), &v, 4);
  9319. };
  9320. set (blob, 0, 10, 1000); // all to the FX send
  9321. auto render = [] (const std::vector<std::uint8_t>& st) {
  9322. return lynx::harmor::renderReference (st, { { 0, 60, 0.8, 1.0, 0.0, 6000 } }, 44100.0, 12000);
  9323. };
  9324. const auto dry = render (blob);
  9325. auto rev = blob;
  9326. set (rev, -1, 22, 1); // reverb on: room 50, diffusion 100, decay 2^3·0.1 s, high cut / damping off, wet 0.8
  9327. 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);
  9328. set (rev, -1, 30, 2), set (rev, -1, 31, 800);
  9329. const auto bypassed = render (rev);
  9330. double eb = 0.0, db = 0.0;
  9331. for (std::size_t i = 0; i < dry.size(); ++i)
  9332. eb += (double) (bypassed[i] - dry[i]) * (bypassed[i] - dry[i]), db += (double) dry[i] * dry[i];
  9333. CHECK (eb < 1e-12 * db && db > 1e-3); // (float rounding of the rack's buffers only)
  9334. auto after = rev;
  9335. const std::int32_t order[6] { 0, 1, 2, 4, 3, 5 }; // the join before the reverb
  9336. for (int k = 0; k < 6; ++k)
  9337. set (after, -1, 53 + k, order[k]);
  9338. const auto wet = render (after);
  9339. double e = 0.0, d = 0.0;
  9340. bool finite = true;
  9341. for (std::size_t i = 0; i < wet.size(); ++i)
  9342. e += (double) (wet[i] - dry[i]) * (wet[i] - dry[i]), d += (double) dry[i] * dry[i], finite = finite && std::isfinite (wet[i]);
  9343. CHECK (finite && e > 1e-4 * d && d > 1e-3);
  9344. // a distortion (type 6) and a delay on the dry path change the sound, stay finite, and the instrument ends quiet
  9345. auto dist = harmorTestState (false);
  9346. {
  9347. const auto at = lynx::harmorParamOffset (dist, -1, 0) + 0xd4 + 0x18 + 48; // global misc +48: distortion type
  9348. const std::int32_t six = 6;
  9349. std::memcpy (dist.data() + at, &six, 4);
  9350. }
  9351. set (dist, -1, 17, 600), set (dist, -1, 19, 1000), set (dist, -1, 21, 1100);
  9352. 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);
  9353. set (dist, -1, 47, 1050);
  9354. lynx::ChannelDef c;
  9355. c.instrument = lynx::InstrumentKind::harmor;
  9356. c.pluginState = dist;
  9357. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmor);
  9358. inst->configure (c, 120.0);
  9359. inst->prepare (44100.0);
  9360. std::vector<float> l (44100 * 4), r (l.size());
  9361. inst->noteOn (60, 0.8f);
  9362. double energy = 0.0;
  9363. for (int s = 0; s < (int) l.size(); s += 512)
  9364. {
  9365. if (s == 8192)
  9366. inst->noteOff (60);
  9367. inst->render (l.data() + s, r.data() + s, 512, 1.0f, 1.0f);
  9368. }
  9369. for (std::size_t i = 0; i < l.size(); ++i)
  9370. finite = finite && std::isfinite (l[i]) && std::isfinite (r[i]), energy += (double) l[i] * l[i];
  9371. CHECK (finite && energy > 1.0);
  9372. std::vector<float> tail (44100 * 6), tailR (tail.size());
  9373. for (int s = 0; s < (int) tail.size(); s += 512)
  9374. inst->render (tail.data() + s, tailR.data() + s, 512, 1.0f, 1.0f);
  9375. CHECK (inst->idle());
  9376. }
  9377. TEST (harmless_engine)
  9378. {
  9379. // REA_rodada32: state parsing, the additive engine against our reference model (hlmodel.py), host levels, voice end
  9380. std::vector<std::uint8_t> blob;
  9381. 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))); };
  9382. std::array<std::int32_t, 100> p {}; // the reference's clean patch: Saw, open filter, no modulation
  9383. for (int i = 11; i < 23; ++i)
  9384. p[(std::size_t) i] = 1000;
  9385. 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;
  9386. 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;
  9387. // + resonance, pluck, 3-voice unison, tremolo, LFO on pitch
  9388. 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;
  9389. put (12);
  9390. for (auto v : p)
  9391. put (v);
  9392. for (int i = 0; i < 7; ++i)
  9393. put (0);
  9394. put (524); // Custom colour table: 524 × (amplitude, phase)
  9395. for (int i = 0; i < 1048; ++i)
  9396. {
  9397. const auto f = i < 524 ? 1.0f / (float) (i + 1) : 0.25f;
  9398. std::uint32_t bits = 0;
  9399. std::memcpy (&bits, &f, 4);
  9400. put ((std::int32_t) bits);
  9401. }
  9402. const auto parsed = lynx::harmlessParams (blob);
  9403. CHECK (parsed && (int) parsed->size() == lynx::harmlessParamCount);
  9404. CHECK (parsed && (*parsed)[54] == 3500.0f && (*parsed)[99] == 0.0f && (*parsed)[lynx::harmlessCustomFlagIndex] == 1.0f);
  9405. CHECK (parsed && (*parsed)[lynx::harmlessCustomIndex + 1] == 0.5f && (*parsed)[lynx::harmlessCustomIndex + 524] == 0.25f);
  9406. auto old = blob;
  9407. old[0] = 3; // versions below 4 are not Harmless states we know
  9408. CHECK (! lynx::harmlessParams (old));
  9409. old = blob;
  9410. old.resize (300);
  9411. CHECK (! lynx::harmlessParams (old));
  9412. lynx::ChannelDef c;
  9413. c.instrument = lynx::InstrumentKind::harmless;
  9414. c.modelParams = *parsed;
  9415. c.modelParams[lynx::harmlessCustomFlagIndex] = 0.0f; // colour 0 (Saw): the custom table is not read
  9416. auto render = [] (const lynx::ChannelDef& def, int total, float gl, float gr, int offAt) {
  9417. auto inst = lynx::makeInstrument (lynx::InstrumentKind::harmless);
  9418. inst->configure (def, 120.0);
  9419. inst->prepare (44100.0);
  9420. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  9421. inst->noteOn (60, 0.78125f);
  9422. for (int s = 0; s < total; s += 512)
  9423. {
  9424. if (s == offAt)
  9425. inst->noteOff (60);
  9426. inst->render (l.data() + s, r.data() + s, std::min (512, total - s), gl, gr);
  9427. }
  9428. return std::make_tuple (l, r, std::move (inst));
  9429. };
  9430. {
  9431. // reference model (REA round 32 hlmodel.py, C5 at FL velocity 100, channel volume 1, centre, released at 6144)
  9432. auto [l, r, inst] = render (c, 8192, 1.0f, 1.0f, 6144);
  9433. const std::tuple<int, float, float> golden[] {
  9434. { 0, 0.0f, 0.0f }, { 10, -0.02220994f, -0.06232700f }, { 100, 0.40623373f, 0.17308202f }, { 1000, -0.04091963f, -0.26821208f },
  9435. { 3000, 0.14519004f, 0.11027778f }, { 6000, 0.50411958f, 0.27901855f }, { 8191, 0.02117692f, -0.01466602f } };
  9436. for (const auto& [i, gl, gr] : golden)
  9437. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  9438. // after the release the record fades out (below 1e-4, then 30 ms) and the instrument goes idle
  9439. std::vector<float> tail (44100), tailR (tail.size());
  9440. inst->render (tail.data(), tailR.data(), (int) tail.size(), 1.0f, 1.0f);
  9441. CHECK (inst->idle());
  9442. }
  9443. // FL's level law applied once: half the channel volume halves the output; full right pan silences the left side
  9444. const auto full = std::get<0> (render (c, 2048, 1.0f, 1.0f, -1));
  9445. const auto half = std::get<0> (render (c, 2048, 0.5f, 0.5f, -1));
  9446. CHECK (std::abs (half[100] - 0.5f * full[100]) < 1e-6f && std::abs (full[100]) > 0.1f);
  9447. auto mono = c;
  9448. mono.modelParams[25] = 0.0f; // no tremolo, no unison pan spread
  9449. mono.modelParams[65] = 1.0f;
  9450. const auto [pl, pr, pi] = render (mono, 2048, 0.0f, 1.41421356f, -1);
  9451. double el = 0.0, er = 0.0;
  9452. for (int i = 0; i < 2048; ++i)
  9453. el += (double) pl[(std::size_t) i] * pl[(std::size_t) i], er += (double) pr[(std::size_t) i] * pr[(std::size_t) i];
  9454. CHECK (el < 1e-10 * er && er > 1.0);
  9455. }
  9456. TEST (startup_sound)
  9457. {
  9458. // the splash's sound: the right length, finite, at its peak level, quiet before the whoosh swells,
  9459. // a landing "pop" at impactTime and a chime ringing after it
  9460. using namespace lynx::startup;
  9461. const auto sound = makeSound (48000.0);
  9462. CHECK (sound.left.size() == (std::size_t) (soundLength * 48000.0) && sound.right.size() == sound.left.size());
  9463. float peak = 0.0f;
  9464. bool finite = true;
  9465. for (std::size_t i = 0; i < sound.left.size(); ++i)
  9466. {
  9467. finite = finite && std::isfinite (sound.left[i]) && std::isfinite (sound.right[i]);
  9468. peak = std::max ({ peak, std::abs (sound.left[i]), std::abs (sound.right[i]) });
  9469. }
  9470. CHECK (finite && std::abs (peak - peakLevel) < 1.0e-4f);
  9471. auto rms = [&sound] (double from, double to) {
  9472. double e = 0.0;
  9473. const auto a = (std::size_t) (from * 48000.0), b = (std::size_t) (to * 48000.0);
  9474. for (auto i = a; i < b; ++i)
  9475. e += (double) sound.left[i] * sound.left[i];
  9476. return std::sqrt (e / (double) (b - a));
  9477. };
  9478. CHECK (rms (0.0, 0.05) < 0.2 * rms (impactTime - 0.05, impactTime)); // the whoosh swells
  9479. CHECK (rms (impactTime, impactTime + 0.05) > rms (impactTime - 0.05, impactTime)); // the landing
  9480. CHECK (rms (0.8, 1.0) > 0.005 && rms (soundLength - 0.02, soundLength) < rms (0.8, 1.0)); // chime rings, tail fades
  9481. CHECK (makeSound (48000.0).left == sound.left); // the same sound on every start
  9482. }
  9483. namespace
  9484. {
  9485. // little-endian byte writer for the state builders below
  9486. struct Bytes
  9487. {
  9488. std::vector<std::uint8_t> b;
  9489. void raw (const char* s, std::size_t n) { b.insert (b.end(), s, s + n); }
  9490. 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); }
  9491. void f32 (float v) { b.insert (b.end(), reinterpret_cast<const std::uint8_t*> (&v), reinterpret_cast<const std::uint8_t*> (&v) + 4); }
  9492. void zeros (std::size_t n) { b.insert (b.end(), n, 0); }
  9493. void bytes (const std::vector<std::uint8_t>& o) { b.insert (b.end(), o.begin(), o.end()); }
  9494. };
  9495. // A GMS state written from scratch (REA_rodada12 §1.1 layout, "Syn4"): oscillator waves that don't exist (silent
  9496. // tables, so the result does not depend on an FL installation), noise through the ladder with envelope and LFO,
  9497. // unison, slide, distortion, delay, reverb and the output EQ.
  9498. std::vector<std::uint8_t> gmsTestState()
  9499. {
  9500. std::array<float, 212> P {};
  9501. for (int i : { 1, 2, 44, 55, 56, 57, 58, 59, 60, 207, 208, 209, 210, 211 })
  9502. P[(std::size_t) i] = 0.5f;
  9503. P[0] = 0.75f, P[4] = 0.1f, P[5] = 0.4f, P[6] = 0.6f, P[7] = 0.3f, P[8] = 0.8f;
  9504. 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;
  9505. 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;
  9506. P[147] = 1.0f, P[117] = 0.3f, P[127] = 0.6f; // distortion
  9507. P[153] = 1.0f, P[123] = 0.5f, P[133] = 0.4f, P[143] = 0.4f; // delay (1/2 beat)
  9508. P[154] = 1.0f, P[124] = 0.6f, P[134] = 0.5f; // reverb
  9509. P[206] = 1.0f, P[207] = 0.7f, P[211] = 0.3f; // output EQ
  9510. Bytes preset;
  9511. preset.raw ("4nyS", 4);
  9512. for (const char* path : { "NoSuchDir\\NoWave A.wav", "NoSuchDir\\NoWave B.wav", "NoSuchDir\\NoWave C.wav", "", "", "", "" })
  9513. {
  9514. const auto n = std::strlen (path);
  9515. preset.raw (path, n);
  9516. preset.zeros (512 - n);
  9517. }
  9518. preset.i32 (0), preset.i32 (0), preset.i32 (0);
  9519. for (auto v : P)
  9520. preset.f32 (v);
  9521. preset.raw ("LYNX test", 9);
  9522. preset.zeros (512 - 9);
  9523. Bytes s;
  9524. s.raw ("SSLF", 4);
  9525. s.i32 ((std::int32_t) preset.b.size());
  9526. s.bytes (preset.b);
  9527. return s.b;
  9528. }
  9529. // A Morphine state written from scratch (REA_rodada16 §3 / REA_rodada39 §2.2 layout): generators A and B with a few
  9530. // frames each (A loops, PWM on A), C and D silent, a morph line A→B, four envelopes (Eg1 → pitch and velocity →
  9531. // amplitude through the matrix), the master ADSR and every plugin effect on.
  9532. std::vector<std::uint8_t> morphineTestState()
  9533. {
  9534. auto mbp = [] (int seed) {
  9535. Bytes b;
  9536. b.raw ("0PBM", 4);
  9537. for (int a = 0; a < 3; ++a)
  9538. for (int byte = 0; byte < 16; ++byte)
  9539. {
  9540. b.b.push_back (0xff);
  9541. for (int bit = 0; bit < 8; ++bit)
  9542. {
  9543. const auto k = byte * 8 + bit;
  9544. const float v = a == 0 ? std::cbrt (1.0f / (float) (k + 1 + seed * (k % 3))) * (k < 40 ? 1.0f : 0.2f)
  9545. : a == 1 ? 0.1f * (float) ((k * 7 + seed) % 11 - 5) / 5.0f
  9546. : 0.02f * (float) ((k * 5 + seed) % 7 - 3);
  9547. b.f32 (v);
  9548. }
  9549. }
  9550. return b.b;
  9551. };
  9552. auto msp = [&] (int frames, int seed, int loopMode) {
  9553. Bytes b;
  9554. b.raw ("0PSM", 4);
  9555. b.i32 (frames);
  9556. for (int f = 0; f < frames; ++f)
  9557. {
  9558. const auto m = mbp (seed + f);
  9559. b.i32 (20 + 7 * f), b.f32 (1.0f - 0.2f * (float) f), b.i32 ((std::int32_t) m.size());
  9560. b.bytes (m);
  9561. }
  9562. b.i32 (0x424);
  9563. b.i32 (10), b.i32 (40), b.i32 (0), b.i32 (loopMode);
  9564. b.f32 (0.45f), b.f32 (0.6f), b.f32 (0.5f), b.i32 (0);
  9565. for (int k = 0; k < 128; ++k)
  9566. b.f32 (0.3f);
  9567. for (int k = 0; k < 128; ++k)
  9568. b.f32 (1.0f);
  9569. return b.b;
  9570. };
  9571. auto mvc = [&] (int frames, int seed, int pwmMode) {
  9572. Bytes z;
  9573. z.raw ("0HZM", 4);
  9574. z.i32 (1);
  9575. const auto s = msp (frames, seed, 1);
  9576. z.i32 (128), z.f32 (frames > 1 ? 0.9f : 0.0f), z.i32 ((std::int32_t) s.size());
  9577. z.bytes (s);
  9578. Bytes b;
  9579. b.raw ("0CVM", 4);
  9580. b.i32 ((std::int32_t) z.b.size());
  9581. b.bytes (z.b);
  9582. 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);
  9583. return b.b;
  9584. };
  9585. Bytes p;
  9586. p.raw ("0roM", 4);
  9587. p.raw ("0RPM", 4);
  9588. for (const auto& g : { mvc (3, 1, 2), mvc (2, 7, 0), mvc (1, 3, 0), mvc (1, 5, 0) })
  9589. {
  9590. p.i32 ((std::int32_t) g.size());
  9591. p.bytes (g);
  9592. }
  9593. {
  9594. Bytes m;
  9595. m.raw ("0LMM", 4);
  9596. m.i32 (2), m.i32 (12);
  9597. m.f32 (0.0f), m.f32 (1.0f), m.i32 (60);
  9598. m.f32 (1.0f), m.f32 (1.0f), m.i32 (60);
  9599. m.i32 (0x54), m.i32 (0), m.i32 (0), m.i32 (0), m.i32 (0);
  9600. for (float v : { 1.0f, 0.8f, 0.0f, 0.0f, 0.5f, 0.4f, 0.5f, 0.5f })
  9601. m.f32 (v);
  9602. for (int k = 0; k < 4; ++k)
  9603. m.i32 (0);
  9604. m.f32 (0.5f), m.f32 (0.5f), m.f32 (0.5f), m.i32 (0);
  9605. p.i32 ((std::int32_t) m.b.size());
  9606. p.bytes (m.b);
  9607. }
  9608. {
  9609. Bytes d;
  9610. d.raw ("0DMM", 4);
  9611. d.i32 (4);
  9612. for (int e = 0; e < 4; ++e)
  9613. {
  9614. Bytes g;
  9615. g.raw ("0GEM", 4);
  9616. g.i32 (3), g.i32 (8);
  9617. g.f32 (0.0f), g.i32 (10), g.f32 (1.0f), g.i32 (40 + 10 * e), g.f32 (0.3f), g.i32 (50);
  9618. 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),
  9619. g.i32 (e == 0 ? 1 : 0);
  9620. d.i32 ((std::int32_t) g.b.size());
  9621. d.bytes (g.b);
  9622. }
  9623. d.i32 (0x94);
  9624. const int src[12] = { 5, 2 }, dst[12] = { 3, 1 };
  9625. const float amt[12] = { 0.56f, 0.7f };
  9626. for (int k = 0; k < 12; ++k)
  9627. d.i32 (src[k]);
  9628. for (int k = 0; k < 12; ++k)
  9629. d.i32 (dst[k]);
  9630. for (int k = 0; k < 12; ++k)
  9631. d.f32 (amt[k]);
  9632. p.i32 ((std::int32_t) d.b.size());
  9633. p.bytes (d.b);
  9634. }
  9635. {
  9636. 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
  9637. 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 …
  9638. 0.4f, 0.3f, 1.0f, 0.5f, 0.5f, 0.4f, 1.0f, 0.6f, 0.4f, 0.5f, // … delay, reverb, EQ …
  9639. 0.5f, 0.5f, 0.55f, 0.5f, 0.45f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f }; // EQ on, noise (muted)
  9640. Bytes g;
  9641. g.i32 (0x5c4);
  9642. for (auto v : A)
  9643. g.f32 (v);
  9644. g.zeros (0x5c4 - g.b.size());
  9645. p.bytes (g.b);
  9646. }
  9647. Bytes s;
  9648. s.raw ("SSLF", 4);
  9649. s.i32 ((std::int32_t) p.b.size());
  9650. s.bytes (p.b);
  9651. return s.b;
  9652. }
  9653. void dumpIfAsked (const char* var, const std::vector<std::uint8_t>& blob)
  9654. {
  9655. if (const char* path = std::getenv (var))
  9656. {
  9657. std::ofstream out (path, std::ios::binary);
  9658. out.write (reinterpret_cast<const char*> (blob.data()), (std::streamsize) blob.size());
  9659. }
  9660. }
  9661. } // namespace
  9662. TEST (dx10_engine)
  9663. {
  9664. // REA_rodada42: a Fruity DX10 state written from scratch (version 1: 22 raw parameters) against our reference model
  9665. // (dxmodel.py + dxproc.py, bit-identical to the installation's DLL), the channel processor, host levels and the
  9666. // instrument front-end
  9667. const std::int32_t P[22] = { 1000, 40000, 30000, 30000, 40000, 30000, 35000, 8000, 40000, 50000, 20000,
  9668. 25000, 15000, 30000, 20000, 10000, 20000, 30000, 5000, 30000, 20000, -1 };
  9669. std::vector<std::uint8_t> blob (4 + 22 * 4, 0);
  9670. blob[0] = 1;
  9671. std::memcpy (blob.data() + 4, P, sizeof (P));
  9672. CHECK (lynx::dx10StateUsable (blob) && lynx::dx10ProcessorNeutral (blob));
  9673. CHECK (lynx::dx10Param (blob, 21) == -1 && lynx::dx10Param (blob, 3) == 30000 && ! lynx::dx10Param (blob, 22));
  9674. CHECK (! lynx::dx10StateUsable (std::vector<std::uint8_t> (40, 0)));
  9675. {
  9676. auto bad = blob;
  9677. bad[0] = 3; // versions ≥ 3 are ignored by the plugin
  9678. CHECK (! lynx::dx10StateUsable (bad));
  9679. }
  9680. using N = lynx::dx10::RefNote;
  9681. 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 } };
  9682. struct Golden { std::vector<std::uint8_t> state; lynx::dx10::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  9683. auto check = [&] (const Golden& g) {
  9684. const auto y = lynx::dx10::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  9685. CHECK (y.size() == 2 * 16384);
  9686. if (y.size() != 2 * 16384)
  9687. return y;
  9688. double el = 0.0, er = 0.0;
  9689. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9690. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9691. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  9692. for (const auto& [i, l, r] : g.at)
  9693. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  9694. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  9695. return y;
  9696. };
  9697. lynx::dx10::RefOptions plain, odd;
  9698. odd.block = 333, odd.maxPoly = 1;
  9699. check ({ blob, plain, 44100, 672.293115, 778.393071,
  9700. { { 100, -0.0943799689f, -0.154014125f }, { 1000, -0.0814286768f, -0.132879525f }, { 2500, 0.499493301f, 0.312498778f },
  9701. { 5000, -0.184045464f, 0.171641544f }, { 9000, -0.153369367f, -0.20805186f }, { 16383, -0.00441304967f, 0.200126961f } } });
  9702. // 48 kHz, odd host blocks, max polyphony 1 (the last note steals); the hard-right note keeps FL's x87 left gain
  9703. check ({ blob, odd, 48000, 122.232761, 493.320297,
  9704. { { 100, 0.0236493405f, 0.0385922194f }, { 1000, 0.189806491f, 0.309736073f }, { 2500, -0.425576478f, -0.176279545f },
  9705. { 5000, -3.54030076e-18f, -0.0578194074f }, { 9000, 1.37052479e-17f, 0.223831087f }, { 16383, 9.248742e-18f, 0.151048407f } } });
  9706. // the channel processor (§3): our own settings snapshot — pan LFO (triangle, delayed, faded in), volume envelope
  9707. // with tensions and LFO, cutoff envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  9708. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  9709. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  9710. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  9711. for (const auto& [i, v] : fields)
  9712. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  9713. };
  9714. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  9715. std::memcpy (s.data() + 0x15c, &modX, 4);
  9716. std::memcpy (s.data() + 0x160, &modY, 4);
  9717. std::memcpy (s.data() + 0x168, &type, 4);
  9718. };
  9719. std::vector<std::uint8_t> snap (364, 0);
  9720. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  9721. 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 } });
  9722. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  9723. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  9724. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 8, -700 } });
  9725. settings (snap, 140, 120, 7);
  9726. const auto withSnap = lynx::dx10StateWithSnapshot (blob, snap);
  9727. CHECK (withSnap.size() == 460 && withSnap[0] == 2 && ! lynx::dx10ProcessorNeutral (withSnap));
  9728. CHECK (lynx::dx10StateWithSnapshot (withSnap, std::vector<std::uint8_t> (364, 0)) == withSnap); // a v2 state keeps its own
  9729. lynx::dx10::RefOptions ticks;
  9730. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0;
  9731. check ({ withSnap, ticks, 44100, 346.852062, 554.699375,
  9732. { { 100, -0.00162654708f, -0.00265428401f }, { 1000, 0.0310285408f, 0.0506339073f }, { 2500, -0.118068494f, -0.0915558636f },
  9733. { 5000, -0.297166437f, -0.256465197f }, { 9000, 0.199509233f, 0.166108996f }, { 16383, -0.00392446062f, 0.0410436988f } } });
  9734. // a volume envelope decaying to zero: the 10 ms end fade, then every voice is gone (silence from 14950 on)
  9735. std::vector<std::uint8_t> decay (364, 0);
  9736. for (int k = 0; k < 5; ++k)
  9737. record (decay, k, {});
  9738. record (decay, 1, { { 0, 4 }, { 1, 1 }, { 3, 9000 }, { 4, 100 }, { 5, 12000 }, { 6, 0 }, { 15, 50 }, { 16, -101 } });
  9739. settings (decay, 256, 0, 0);
  9740. auto ticks48 = ticks;
  9741. ticks48.block = 333;
  9742. const auto y = check ({ lynx::dx10StateWithSnapshot (blob, decay), ticks48, 48000, 338.361707, 350.520208,
  9743. { { 100, 0.000514116196f, 0.000838961045f }, { 1000, 0.0412623473f, 0.0673340634f }, { 2500, -0.141532451f, -0.156581759f },
  9744. { 5000, 0.382550895f, 0.23462519f }, { 7000, -0.189829409f, 0.144132584f }, { 9000, -0.13638559f, 0.142912149f } } });
  9745. bool silentTail = y.size() == 2 * 16384 && (y[2 * 14949] != 0.0f || y[2 * 14949 + 1] != 0.0f);
  9746. for (std::size_t i = 2 * 14950; i < y.size(); ++i)
  9747. silentTail = silentTail && y[i] == 0.0f;
  9748. CHECK (silentTail);
  9749. // the instrument: LYNX velocity v → Init.Vol = (11^(1.28·v) − 1)/10 and Final.Vol = channel volume × Init.Vol; channel
  9750. // gains √2·V·(cos, sin)((1 + p)π/4) → V, p; notes on block boundaries play like the reference renderer (neutral
  9751. // channel settings: the instrument's sequencer ticks change nothing; vibrato off, since the plugin-wide vibrato
  9752. // oscillator is stepped by each voice in turn, so its interleaving follows the host block layout, as in FL)
  9753. auto noVib = blob;
  9754. CHECK (lynx::dx10SetParam (noVib, 10, 0));
  9755. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); }; // Init.Vol is a float
  9756. lynx::ChannelDef c;
  9757. c.instrument = lynx::InstrumentKind::dx10;
  9758. c.pluginState = noVib;
  9759. auto inst = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  9760. inst->configure (c, 120.0);
  9761. inst->prepare (44100.0);
  9762. const float V = 0.6f, pan = 0.25f;
  9763. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  9764. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  9765. std::vector<float> l (16384), r (16384);
  9766. for (int s = 0; s < 16384; s += 512)
  9767. {
  9768. if (s == 0)
  9769. inst->noteOn (60, 0.78125f);
  9770. if (s == 2048)
  9771. {
  9772. inst->setNextNoteExpression (-0.5f, 25.0f);
  9773. inst->noteOn (67, 0.5f);
  9774. }
  9775. if (s == 6144)
  9776. inst->noteOff (60);
  9777. if (s == 8192)
  9778. inst->noteOff (67);
  9779. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  9780. }
  9781. // the channel level reaches the engine as FL's (recovered to float precision)
  9782. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  9783. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  9784. const auto ref = lynx::dx10::renderReference (noVib, { { 0, 60, initVol (0.78125), Vr * initVol (0.78125), pr, 0.0, 6144 },
  9785. { 2048, 67, initVol (0.5), Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  9786. 44100.0, 16384);
  9787. double worst = 0.0, peak = 0.0;
  9788. for (int i = 0; i < 16384; ++i)
  9789. 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]) }),
  9790. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9791. CHECK (worst == 0.0 && peak > 0.05);
  9792. // the released voices end (DX10's own release) and the instrument goes idle
  9793. std::vector<float> tail (44100), tailR (tail.size());
  9794. for (int k = 0; k < 20 && ! inst->idle(); ++k)
  9795. inst->render (tail.data(), tailR.data(), (int) tail.size(), gl, gr);
  9796. CHECK (inst->idle());
  9797. // with the channel settings above the instrument follows FL's ticks: the cutoff envelope darkens the note
  9798. {
  9799. lynx::ChannelDef cs = c;
  9800. cs.pluginState = withSnap;
  9801. cs.modelParams = { 96.0f };
  9802. auto si = lynx::makeInstrument (lynx::InstrumentKind::dx10);
  9803. si->configure (cs, 120.0);
  9804. si->prepare (44100.0);
  9805. std::vector<float> sl (32768), sr (32768);
  9806. si->noteOn (48, 0.78125f);
  9807. for (int s = 0; s < 32768; s += 512)
  9808. si->render (sl.data() + s, sr.data() + s, 512, 1.0f, 1.0f);
  9809. double e1 = 0.0, e2 = 0.0;
  9810. bool finite = true;
  9811. for (int i = 0; i < 32768; ++i)
  9812. finite = finite && std::isfinite (sl[(std::size_t) i]), (i < 4096 ? e1 : e2) += (double) sl[(std::size_t) i] * sl[(std::size_t) i];
  9813. CHECK (finite && e1 > 1e-3 && e2 > 1e-3);
  9814. }
  9815. // parameter edits (the channel settings panel) write the raw integers, clamped to the knob ranges
  9816. auto edited = blob;
  9817. CHECK (lynx::dx10SetParam (edited, 21, 7) && lynx::dx10Param (edited, 21) == 3);
  9818. CHECK (lynx::dx10SetParam (edited, 12, 70000) && lynx::dx10Param (edited, 12) == 65536);
  9819. }
  9820. TEST (threeosc_engine)
  9821. {
  9822. // REA_rodada47: a 3x Osc state written from scratch (version 14) against our reference model (osc3model.py,
  9823. // bit-identical to the installation's DLL), the plugin voice processor with its release path, the instrument
  9824. 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 };
  9825. 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) {
  9826. std::vector<std::uint8_t> st (0x60, 0);
  9827. const std::int32_t version = 14;
  9828. std::memcpy (st.data(), &version, 4);
  9829. std::memcpy (st.data() + 4, p, 21 * 4);
  9830. st[0x58] = inv[0], st[0x59] = inv[1], st[0x5a] = inv[2], st[0x5b] = am;
  9831. st.insert (st.end(), snap.begin(), snap.end());
  9832. return st;
  9833. };
  9834. auto record = [] (std::vector<std::uint8_t>& s, int k, std::initializer_list<std::pair<int, int>> fields) {
  9835. const std::int32_t neutral[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  9836. std::memcpy (s.data() + 0x44 * k, neutral, sizeof (neutral));
  9837. for (const auto& [i, v] : fields)
  9838. std::memcpy (s.data() + 0x44 * k + 4 * i, &v, 4);
  9839. };
  9840. auto settings = [] (std::vector<std::uint8_t>& s, std::int32_t modX, std::int32_t modY, std::int32_t type) {
  9841. std::memcpy (s.data() + 0x15c, &modX, 4);
  9842. std::memcpy (s.data() + 0x160, &modY, 4);
  9843. std::memcpy (s.data() + 0x168, &type, 4);
  9844. };
  9845. std::vector<std::uint8_t> neutral (364, 0);
  9846. for (int k = 0; k < 5; ++k)
  9847. record (neutral, k, k == 1 ? std::initializer_list<std::pair<int, int>> { { 0, 4 }, { 16, -101 } } : std::initializer_list<std::pair<int, int>> {});
  9848. settings (neutral, 256, 0, 0);
  9849. const auto blob = make (P, { 0, 0, 0 }, 0, neutral);
  9850. CHECK (lynx::threeOscStateUsable (blob) && lynx::threeOscNormalisedState (blob, {}) == blob);
  9851. CHECK (lynx::threeOscParam (blob, 9) == -12 && lynx::threeOscParam (blob, 20) == 16 && lynx::threeOscParam (blob, 24) == 0 && ! lynx::threeOscParam (blob, 26));
  9852. // older versions are normalised like the plugin reads them: version 12 (no snapshot → the plugin's defaults),
  9853. // version 10 (20 parameters, no invert/AM bytes)
  9854. {
  9855. std::vector<std::uint8_t> v12 (blob.begin(), blob.begin() + 92);
  9856. v12[0] = 12;
  9857. v12[0x58] = 1;
  9858. const auto n12 = lynx::threeOscNormalisedState (v12, {});
  9859. CHECK (n12.size() == 460 && n12[0] == 14 && lynx::threeOscParam (n12, 21) == 1 && lynx::threeOscParam (n12, 20) == 16
  9860. && std::equal (n12.begin() + 0x60, n12.end(), neutral.begin()));
  9861. std::vector<std::uint8_t> v10 (blob.begin(), blob.begin() + 84);
  9862. v10[0] = 10;
  9863. const auto n10 = lynx::threeOscNormalisedState (v10, {});
  9864. CHECK (lynx::threeOscParam (n10, 20) == 0 && lynx::threeOscParam (n10, 13) == 40 && lynx::threeOscParam (n10, 21) == 0);
  9865. }
  9866. using N = lynx::threeosc::RefNote;
  9867. 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 } };
  9868. struct Golden { std::vector<std::uint8_t> state; lynx::threeosc::RefOptions opt; int rate; double el, er; std::tuple<int, float, float> at[6]; };
  9869. auto check = [&] (const Golden& g) {
  9870. const auto y = lynx::threeosc::renderReference (g.state, notes, (double) g.rate, 16384, g.opt);
  9871. CHECK (y.size() == 2 * 16384);
  9872. if (y.size() != 2 * 16384)
  9873. return y;
  9874. double el = 0.0, er = 0.0;
  9875. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9876. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9877. CHECK (std::abs (el - g.el) <= 1e-7 * g.el && std::abs (er - g.er) <= 1e-7 * g.er);
  9878. for (const auto& [i, l, r] : g.at)
  9879. CHECK (std::abs (y[2 * (std::size_t) i] - l) <= 1e-7f * std::max (1e-16f, std::abs (l))
  9880. && std::abs (y[2 * (std::size_t) i + 1] - r) <= 1e-7f * std::max (1e-16f, std::abs (r)));
  9881. return y;
  9882. };
  9883. // saw / square / triangle-ish mix with pans, stereo detune and phase, random start phases (seed 12345)
  9884. lynx::threeosc::RefOptions plain;
  9885. plain.seed = 12345;
  9886. check ({ blob, plain, 44100, 681.6493225566775, 1509.7243534586487,
  9887. { { 100, 0.0674247071146965f, -0.13440212607383728f }, { 1000, 0.12890009582042694f, 0.446039617061615f },
  9888. { 2500, -0.007686644792556763f, 0.3574123680591583f }, { 5000, 0.08792756497859955f, 0.4948147237300873f },
  9889. { 9000, 1.2136793475613885e-17f, 0.3116455078125f }, { 16383, 5.7305530145831346e-18f, 0.4134995639324188f } } });
  9890. // AM (osc 3 noise modulating osc 1 + 2), inverted oscillators, 48 kHz, odd blocks, max polyphony 1
  9891. 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 };
  9892. lynx::threeosc::RefOptions odd;
  9893. odd.block = 333, odd.maxPoly = 1, odd.seed = 777;
  9894. check ({ make (Pam, { 1, 0, 1 }, 1, neutral), odd, 48000, 453.2728551610344, 1996.4554357771258,
  9895. { { 100, -0.2589033246040344f, -0.310184121131897f }, { 1000, 0.22021546959877014f, 0.3591070771217346f },
  9896. { 2500, -0.5378497242927551f, -0.15602676570415497f }, { 5000, 1.2423200626807308e-17f, 0.3293072581291199f },
  9897. { 9000, 2.5857780099332177e-17f, 0.42474493384361267f }, { 16383, 2.941168384618868e-17f, 0.3569665551185608f } } });
  9898. // the channel processor with its release path: pan LFO, volume envelope released from its current value, cutoff
  9899. // envelope + square LFO, resonance LFO, pitch envelope, SVF LP ×2 (type 7)
  9900. std::vector<std::uint8_t> snap (364, 0);
  9901. record (snap, 0, { { 9, 5000 }, { 10, 9000 }, { 11, -40 }, { 12, 15000 }, { 13, 1 } });
  9902. 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 } });
  9903. record (snap, 2, { { 1, 1 }, { 3, 6000 }, { 4, 100 }, { 5, 16000 }, { 6, 20 }, { 7, 9000 }, { 8, -60 }, { 11, 20 }, { 12, 18000 }, { 13, 2 } });
  9904. record (snap, 3, { { 11, 40 }, { 12, 17000 } });
  9905. record (snap, 4, { { 1, 1 }, { 3, 5000 }, { 4, 100 }, { 5, 14000 }, { 6, 30 }, { 7, 12000 }, { 8, -700 } });
  9906. settings (snap, 140, 120, 7);
  9907. lynx::threeosc::RefOptions ticks;
  9908. ticks.tick = 230, ticks.ppq = 96, ticks.tempo = 120.0, ticks.seed = 99;
  9909. check ({ make (P, { 0, 0, 0 }, 0, snap), ticks, 44100, 977.9928196811321, 2072.965349921462,
  9910. { { 100, -0.001351095037534833f, 0.0029758031014353037f }, { 1000, 0.002833257894963026f, 0.10575854033231735f },
  9911. { 2500, 0.020360738039016724f, 0.3181821405887604f }, { 5000, 0.3340669274330139f, 0.4912331700325012f },
  9912. { 9000, 0.26940545439720154f, 0.27708977460861206f }, { 16383, 0.16570518910884857f, -0.10794822871685028f } } });
  9913. auto ticks48 = ticks;
  9914. ticks48.block = 333, ticks48.tick = 250, ticks48.seed = 5;
  9915. // (at 48 kHz the library's rate ratio 44100/fs scales the filter coefficients and the ramp limit)
  9916. check ({ make (P, { 0, 1, 0 }, 0, snap), ticks48, 48000, 864.9859578029492, 1895.6281818565287,
  9917. { { 100, 0.0031638722866773605f, -0.001987025374546647f }, { 1000, 0.049239177256822586f, 0.08794696629047394f },
  9918. { 2500, 0.13807585835456848f, 0.3554498553276062f }, { 5000, -0.08793184161186218f, -0.1596519947052002f },
  9919. { 9000, 0.08728396147489548f, 0.540290117263794f }, { 16383, 0.039887525141239166f, 0.21270982921123505f } } });
  9920. // HQ oscillators (engine_x64.dll: band-limited tables built with an FFT; FL's single-precision FFT and LYNX's double
  9921. // one agree to float rounding, so the golden values hold to 1e-5)
  9922. {
  9923. 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 };
  9924. auto hq = make (Phq, { 0, 0, 0 }, 0, neutral);
  9925. hq[0x5c] = 1;
  9926. CHECK (lynx::threeOscParam (hq, 25) == 1);
  9927. lynx::threeosc::RefOptions o;
  9928. o.seed = 3;
  9929. const auto y = lynx::threeosc::renderReference (hq, notes, 44100.0, 16384, o);
  9930. CHECK (y.size() == 2 * 16384);
  9931. if (y.size() == 2 * 16384)
  9932. {
  9933. double el = 0.0, er = 0.0;
  9934. for (std::size_t i = 0; i + 1 < y.size(); i += 2)
  9935. el += (double) y[i] * y[i], er += (double) y[i + 1] * y[i + 1];
  9936. CHECK (std::abs (el - 862.7912087773848) < 1e-5 * 862.8 && std::abs (er - 1232.8046929286115) < 1e-5 * 1232.8);
  9937. const std::tuple<int, float, float> at[] = { { 100, -0.019788391888141632f, -0.19209949672222137f }, { 1000, 0.12019827216863632f, 0.03852222114801407f },
  9938. { 2500, 0.3239150643348694f, 0.07325324416160583f }, { 5000, -0.2809634208679199f, -0.5693862438201904f },
  9939. { 16383, -1.1937656360586644e-17f, -0.061853960156440735f } };
  9940. for (const auto& [i, l, r] : at)
  9941. CHECK (std::abs (y[2 * (std::size_t) i] - l) < 1e-5f && std::abs (y[2 * (std::size_t) i + 1] - r) < 1e-5f);
  9942. }
  9943. }
  9944. // the instrument: Final.Vol = channel volume × Init.Vol, channel gains → FL's volume/pan; notes on block boundaries
  9945. // play like the reference renderer (seed 0, neutral channel settings: ticks change nothing)
  9946. auto initVol = [] (double v) { return (double) (float) ((std::pow (11.0, 1.28 * v) - 1.0) / 10.0); };
  9947. lynx::ChannelDef c;
  9948. c.instrument = lynx::InstrumentKind::threeosc;
  9949. c.pluginState = blob;
  9950. auto inst = lynx::makeInstrument (lynx::InstrumentKind::threeosc);
  9951. inst->configure (c, 120.0);
  9952. inst->prepare (44100.0);
  9953. const float V = 0.6f, pan = 0.25f;
  9954. const auto gl = (float) (std::sqrt (2.0) * V * std::cos ((1.0 + pan) * 3.141592653589793 / 4.0));
  9955. const auto gr = (float) (std::sqrt (2.0) * V * std::sin ((1.0 + pan) * 3.141592653589793 / 4.0));
  9956. std::vector<float> l (16384), r (16384);
  9957. for (int s = 0; s < 16384; s += 512)
  9958. {
  9959. if (s == 0)
  9960. inst->noteOn (60, 0.78125f);
  9961. if (s == 2048)
  9962. {
  9963. inst->setNextNoteExpression (-0.5f, 25.0f);
  9964. inst->noteOn (67, 0.5f);
  9965. }
  9966. if (s == 6144)
  9967. inst->noteOff (60);
  9968. if (s == 8192)
  9969. inst->noteOff (67);
  9970. inst->render (l.data() + s, r.data() + s, 512, gl, gr);
  9971. }
  9972. const auto Vr = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  9973. const auto pr = std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0;
  9974. const auto ref = lynx::threeosc::renderReference (blob, { { 0, 60, Vr * initVol (0.78125), pr, 0.0, 6144 },
  9975. { 2048, 67, Vr * initVol (0.5), std::clamp (-0.5 + pr, -1.0, 1.0), 25.0, 8192 } },
  9976. 44100.0, 16384);
  9977. double worst = 0.0, peak = 0.0;
  9978. for (int i = 0; i < 16384; ++i)
  9979. 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]) }),
  9980. peak = std::max (peak, (double) std::abs (l[(std::size_t) i]));
  9981. CHECK (worst == 0.0 && peak > 0.05);
  9982. // released notes end with the processor's 10 ms fade (volume envelope off) and the instrument goes idle
  9983. CHECK (inst->idle());
  9984. // parameter edits write the raw integers, clamped to the knob ranges
  9985. auto edited = blob;
  9986. CHECK (lynx::threeOscSetParam (edited, 9, -40) && lynx::threeOscParam (edited, 9) == -24);
  9987. CHECK (lynx::threeOscSetParam (edited, 13, 500) && lynx::threeOscParam (edited, 13) == 128);
  9988. CHECK (lynx::threeOscSetParam (edited, 24, 1) && edited[0x5b] == 1);
  9989. CHECK (! lynx::threeOscStateUsable (std::vector<std::uint8_t> (2, 0)));
  9990. }
  9991. TEST (gms_engine)
  9992. {
  9993. const auto blob = gmsTestState();
  9994. dumpIfAsked ("LYNX_DUMP_GMS", blob);
  9995. CHECK (lynx::gmsStateUsable (blob));
  9996. CHECK (lynx::gmsWaveNames (blob) == std::vector<std::string> ({ "NoWave A", "NoWave B", "NoWave C" }));
  9997. CHECK (lynx::gmsParamOffset (blob, 12) == 8 + 4 + 7 * 512 + 12 + 4 * 12 && lynx::gmsParamOffset (blob, 212) < 0);
  9998. auto broken = blob;
  9999. broken[8] = 'X';
  10000. CHECK (! lynx::gmsStateUsable (broken));
  10001. CHECK (! lynx::gmsStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 2000)));
  10002. // golden samples of our round-39 model (gmsmodel.GmsPlugin, waves missing → silent tables, tempo 140, 44.1 kHz,
  10003. // host blocks of 512): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  10004. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10005. const std::vector<lynx::gms::RefNote> notes = { { 0, 60, (float) iv (100), 1.0, 0.0, 0.0, 30000 },
  10006. { 11025, 64, (float) iv (80), 1.0, 0.0, 0.0, 40000 },
  10007. { 22050, 67, (float) iv (110), 1.0, 0.0, 0.0, 52000 } };
  10008. const auto y = lynx::gms::renderReference (blob, notes, 44100.0, 66150, "");
  10009. CHECK (y.size() == 2 * 66150u);
  10010. 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 } };
  10011. for (const auto& g : golden)
  10012. 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);
  10013. double e = 0.0;
  10014. for (auto v : y)
  10015. e += (double) v * v;
  10016. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.0790270452795) < 1e-6);
  10017. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10018. lynx::ChannelDef c;
  10019. c.instrument = lynx::InstrumentKind::gms;
  10020. c.pluginState = blob;
  10021. auto inst = lynx::makeInstrument (lynx::InstrumentKind::gms);
  10022. inst->configure (c, 140.0);
  10023. inst->prepare (44100.0);
  10024. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10025. for (int b = 0; b < 16; ++b)
  10026. {
  10027. if (b == 0)
  10028. inst->noteOn (60, 0.78125f);
  10029. if (b == 10)
  10030. inst->noteOff (60);
  10031. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10032. }
  10033. 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, "");
  10034. double diff = 0.0, energy = 0.0;
  10035. for (int i = 0; i < 8192; ++i)
  10036. {
  10037. 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]) });
  10038. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10039. }
  10040. CHECK (diff == 0.0 && energy > 1e-3);
  10041. }
  10042. // A Toxic Biohazard state written from scratch (REA_rodada45; r45/py/mktest.py builds the same bytes): our own drawn
  10043. // spectra on operators 1 and 3 (wave 0) and the sine on operator 2, so no wave of the installation is needed
  10044. std::vector<std::uint8_t> toxicTestState()
  10045. {
  10046. auto s = lynx::toxicDefaultState();
  10047. static const std::pair<int, double> set[] = {
  10048. { 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 },
  10049. { 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 },
  10050. { 24, 0.6 }, { 25, 1 }, { 26, 1 }, { 27, 1 }, { 32, 0.5606 }, { 33, 0.4394 }, { 38, 0.53 }, { 49, 0 }, { 55, 0.25 },
  10051. { 62, 0.5 }, { 75, 0.08 }, { 81, 0.5 }, { 87, 0.3 }, { 104, 0.8 }, { 105, 0.6 }, { 110, 0.35 }, { 115, 0.22 },
  10052. { 127, 0.18 }, { 147, 0.7 }, { 227, 1 }, { 229, 0.65 }, { 233, 0.4 }, { 236, 1 }, { 237, 1 }, { 252, 0.5 }, { 254, 0.3 },
  10053. { 256, 0.4 }, { 267, 0.4 }, { 269, 0.5 }, { 277, 0.3 }, { 300, 1 }, { 301, 8 }, { 302, 2 }, { 303, 0 }, { 305, 3 },
  10054. { 306, 0 }, { 307, 1 }, { 308, 0 }, { 314, 1 }, { 315, 0 }, { 316, 1 }, { 324, 106 }, { 332, 0.7 } };
  10055. for (const auto& [index, value] : set)
  10056. {
  10057. const auto v = (float) value;
  10058. std::memcpy (s.data() + lynx::toxicParamOffset (s, index), &v, 4);
  10059. }
  10060. auto spectrum = [&] (int slot, int h, float amp, float phase) {
  10061. const auto at = 12 + 0x40 + 1024 * (std::size_t) slot + 4 * (std::size_t) h;
  10062. std::memcpy (s.data() + at, &amp, 4);
  10063. std::memcpy (s.data() + at + 512, &phase, 4);
  10064. };
  10065. for (int h = 0; h < 24; ++h)
  10066. spectrum (0, h, (float) (1.0 / (h + 1)), 0.0f);
  10067. for (int k = 0; k < 16; ++k)
  10068. spectrum (2, 2 * k, (float) (1.0 / (2 * k + 1)), 0.25f);
  10069. return s;
  10070. }
  10071. TEST (toxic_engine)
  10072. {
  10073. const auto blob = toxicTestState();
  10074. dumpIfAsked ("LYNX_DUMP_TOXIC", blob);
  10075. CHECK (blob.size() == 10050u && lynx::toxicStateUsable (blob) && ! lynx::toxicNeedsDll (blob));
  10076. CHECK (lynx::toxicParamOffset (blob, 15) == 12 + 0x40 + 8192 + 4 * 15 && lynx::toxicParamOffset (blob, 340) < 0);
  10077. CHECK (! lynx::toxicDllUsable ("") && ! lynx::toxicDllUsable ("/nonexistent/Toxic Biohazard_x64.dll"));
  10078. auto broken = blob;
  10079. broken[8] = 'X';
  10080. CHECK (! lynx::toxicStateUsable (broken));
  10081. CHECK (! lynx::toxicStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 5000)));
  10082. auto needs = blob;
  10083. const auto w = 15.0f; // a built-in wave of the installation: without the DLL the engine refuses the state
  10084. std::memcpy (needs.data() + lynx::toxicParamOffset (needs, 307), &w, 4);
  10085. CHECK (lynx::toxicNeedsDll (needs) && lynx::toxic::renderReference (needs, { { 0, 60 } }, 44100.0, 64, "").empty());
  10086. // golden samples of our round-45 model (toxmodel.run, which equals the DLL bit for bit on this state), effects on,
  10087. // tempo 140, 44.1 kHz, host blocks of 512: C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000
  10088. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10089. const std::vector<lynx::toxic::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  10090. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  10091. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  10092. const auto y = lynx::toxic::renderReference (blob, notes, 44100.0, 66150, "", 512, 140.0);
  10093. CHECK (y.size() == 2 * 66150u);
  10094. 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 } };
  10095. for (const auto& g : golden)
  10096. 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);
  10097. double e = 0.0;
  10098. for (auto v : y)
  10099. e += (double) v * v;
  10100. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.307227390984) < 1e-6);
  10101. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10102. lynx::ChannelDef c;
  10103. c.instrument = lynx::InstrumentKind::toxic;
  10104. c.pluginState = blob;
  10105. auto inst = lynx::makeInstrument (lynx::InstrumentKind::toxic);
  10106. inst->configure (c, 140.0);
  10107. inst->prepare (44100.0);
  10108. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10109. for (int b = 0; b < 16; ++b)
  10110. {
  10111. if (b == 0)
  10112. inst->noteOn (60, 0.78125f);
  10113. if (b == 10)
  10114. inst->noteOff (60);
  10115. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10116. }
  10117. 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 } },
  10118. 44100.0, 8192, "", 512, 140.0);
  10119. double diff = 0.0, energy = 0.0;
  10120. for (int i = 0; i < 8192; ++i)
  10121. {
  10122. 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]) });
  10123. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10124. }
  10125. CHECK (diff == 0.0 && energy > 1e-3);
  10126. }
  10127. TEST (morphine_engine)
  10128. {
  10129. const auto blob = morphineTestState();
  10130. dumpIfAsked ("LYNX_DUMP_MORPHINE", blob);
  10131. CHECK (lynx::morphineStateUsable (blob));
  10132. CHECK (lynx::morphineNoiseName (blob).empty());
  10133. const auto at = lynx::morphineParamOffset (blob, 9);
  10134. CHECK (at > 0);
  10135. float a9 = 0.0f;
  10136. std::memcpy (&a9, blob.data() + at, 4);
  10137. CHECK (a9 == 0.2f && lynx::morphineParamOffset (blob, 40) < 0);
  10138. auto broken = blob;
  10139. broken[12] = 'X'; // the MPR0 tag
  10140. CHECK (! lynx::morphineStateUsable (broken));
  10141. CHECK (! lynx::morphineStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.end() - 10)));
  10142. // golden samples of our round-39 model (mormodel.Synth, tempo 120, 44.1 kHz, host blocks of 512): keys 60/64/67,
  10143. // Init.Vol 1.0/0.5/2.0, note-offs at 30000/50000/70000
  10144. const std::vector<lynx::morphine::RefNote> notes = { { 0, 60, 1.0, 1.0, 0.0, 0.0, 30000 },
  10145. { 11025, 64, 0.5, 1.0, 0.0, 0.0, 50000 },
  10146. { 22050, 67, 2.0, 1.0, 0.0, 0.0, 70000 } };
  10147. const auto y = lynx::morphine::renderReference (blob, notes, 44100.0, 80000, "");
  10148. CHECK (y.size() == 2 * 80000u);
  10149. 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 } };
  10150. for (const auto& g : golden)
  10151. 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);
  10152. double e = 0.0;
  10153. for (auto v : y)
  10154. e += (double) v * v;
  10155. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.18467099041) < 1e-6);
  10156. // the instrument: same engine behind LYNX's channel gains
  10157. lynx::ChannelDef c;
  10158. c.instrument = lynx::InstrumentKind::morphine;
  10159. c.pluginState = blob;
  10160. auto inst = lynx::makeInstrument (lynx::InstrumentKind::morphine);
  10161. inst->configure (c, 120.0);
  10162. inst->prepare (44100.0);
  10163. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10164. for (int b = 0; b < 16; ++b)
  10165. {
  10166. if (b == 0)
  10167. inst->noteOn (60, 0.78125f);
  10168. if (b == 10)
  10169. inst->noteOff (60);
  10170. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10171. }
  10172. 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, "");
  10173. double diff = 0.0, energy = 0.0;
  10174. for (int i = 0; i < 8192; ++i)
  10175. {
  10176. 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]) });
  10177. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10178. }
  10179. CHECK (diff == 0.0 && energy > 1e-3);
  10180. }
  10181. namespace
  10182. {
  10183. // A MiniSynth state written from scratch (REA_rodada41 §1.1 layout: "SSLF" + the .flms preset)
  10184. std::vector<std::uint8_t> miniSynthTestState (const std::array<float, 43>& P)
  10185. {
  10186. const std::string name = "LYNX test";
  10187. Bytes body;
  10188. body.raw ("\x99" "FLS\r\n\x1a\n", 8);
  10189. body.raw ("HEAD", 4);
  10190. body.i32 (6 + 2 * (std::int32_t) name.size());
  10191. const std::uint16_t head[3] = { 1, 1, (std::uint16_t) name.size() };
  10192. body.raw (reinterpret_cast<const char*> (head), 6);
  10193. for (char ch : name)
  10194. body.raw (&ch, 1), body.zeros (1);
  10195. body.raw ("DATA", 4);
  10196. body.i32 (176);
  10197. body.raw ("0rev", 4);
  10198. for (auto v : P)
  10199. body.f32 (v);
  10200. Bytes s;
  10201. s.raw ("SSLF", 4);
  10202. s.i32 ((std::int32_t) body.b.size());
  10203. s.bytes (body.b);
  10204. return s.b;
  10205. }
  10206. std::array<float, 43> miniSynthDefaults()
  10207. {
  10208. 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,
  10209. 0.5f, 0, 0.25f, 0.5f, 0.5f, 0, 0.11222222f, 0.4f, 0.1f, 1, 0.5f };
  10210. }
  10211. // a synthetic oscdata.dat (amplitudes 0.3/k, phases −π/2 + 0.001·(k − 1)): the test does not need an FL installation
  10212. std::string miniSynthTestOscdata()
  10213. {
  10214. const auto path = (std::filesystem::temp_directory_path() / "lynx_test_oscdata.dat").string();
  10215. std::vector<float> d (2048);
  10216. for (int h = 0; h < 1024; ++h)
  10217. d[(std::size_t) h] = (float) (0.3 / (h + 1)), d[1024 + (std::size_t) h] = (float) (-1.5707963267948966 + 0.001 * h);
  10218. std::ofstream out (path, std::ios::binary);
  10219. out.write (reinterpret_cast<const char*> (d.data()), 8192);
  10220. return path;
  10221. }
  10222. } // namespace
  10223. TEST (minisynth_engine)
  10224. {
  10225. auto A = miniSynthDefaults(), B = miniSynthDefaults();
  10226. // A: poly, SQR+SQR, noise, filter env/velocity/keytracking, LFO → cutoff, distortion + decimator, delay, chorus, phaser
  10227. 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 },
  10228. { 12, 0.1f }, { 13, 0.4f }, { 14, 0.3f }, { 15, 0.5f }, { 16, 0.8f }, { 17, 0.6f }, { 18, 0.1f },
  10229. { 19, 0.5f }, { 20, 0.85f }, { 21, 0.05f }, { 22, 0.5f }, { 23, 0.6f }, { 24, 0.4f }, { 25, 1 },
  10230. { 26, 0.3f }, { 27, 0.1f }, { 28, 1 }, { 29, 0.4f }, { 30, 0.5f }, { 31, 0.6f }, { 32, 0.6f },
  10231. { 33, 1 }, { 34, 0.4f }, { 35, 0.6f }, { 36, 0.5f }, { 37, 1 }, { 38, 0.3f }, { 39, 0.6f },
  10232. { 40, 0.5f }, { 41, 0.7f }, { 42, 0.4f } };
  10233. // B: lead (legato glide), BELL, LFO → pitch
  10234. 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 },
  10235. { 17, 0.1f }, { 18, 0.5f }, { 19, 0.6f }, { 20, 0.7f }, { 22, 0.6f }, { 23, 0.5f }, { 24, 0.3f } };
  10236. for (const auto& [i, v] : a)
  10237. A[(std::size_t) i] = v;
  10238. for (const auto& [i, v] : b)
  10239. B[(std::size_t) i] = v;
  10240. const auto blobA = miniSynthTestState (A), blobB = miniSynthTestState (B);
  10241. dumpIfAsked ("LYNX_DUMP_MINISYNTH", blobA);
  10242. CHECK (lynx::miniSynthStateUsable (blobA) && lynx::miniSynthParams (blobA).size() == 43u && lynx::miniSynthParams (blobA)[26] == 0.3f);
  10243. CHECK (lynx::miniSynthParamOffset (blobA, 9) == 8 + 8 + 8 + 6 + 18 + 12 + 4 * 9 && lynx::miniSynthParamOffset (blobA, 43) < 0);
  10244. CHECK (! lynx::miniSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 20)));
  10245. auto shortData = blobA;
  10246. shortData.resize (shortData.size() - 4 * 40); // only 3 parameters present: the rest keeps the defaults
  10247. CHECK (lynx::miniSynthParams (shortData)[20] == 0.5f && lynx::miniSynthParams (shortData)[1] == 0.5f);
  10248. // golden samples of our round-41 model (msmodel.Plugin on the same synthetic oscdata, 44.1 kHz, host blocks of 512,
  10249. // L = R = Final.Vol): C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol law A)
  10250. const auto osc = miniSynthTestOscdata();
  10251. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10252. const std::vector<lynx::minisynth::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  10253. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  10254. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  10255. struct Golden { int i; float l, r; };
  10256. 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 } };
  10257. 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 } };
  10258. for (int which = 0; which < 2; ++which)
  10259. {
  10260. const auto y = lynx::minisynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, osc);
  10261. CHECK (y.size() == 2 * 66150u);
  10262. if (y.size() != 2 * 66150u)
  10263. continue;
  10264. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  10265. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  10266. double e = 0.0;
  10267. for (auto v : y)
  10268. e += (double) v * v;
  10269. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.315552434392 : 0.03926116287)) < 1e-9);
  10270. }
  10271. // missing oscdata.dat: silent tables, still finite
  10272. const auto silent = lynx::minisynth::renderReference (blobB, notes, 44100.0, 4096, "");
  10273. CHECK (silent.size() == 2 * 4096u && std::all_of (silent.begin(), silent.end(), [] (float v) { return std::isfinite (v) && std::abs (v) < 1.0f; }));
  10274. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10275. lynx::ChannelDef c;
  10276. c.instrument = lynx::InstrumentKind::minisynth;
  10277. c.pluginState = blobA;
  10278. c.samplePath = osc;
  10279. auto inst = lynx::makeInstrument (lynx::InstrumentKind::minisynth);
  10280. inst->configure (c, 120.0);
  10281. inst->prepare (44100.0);
  10282. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10283. for (int k = 0; k < 16; ++k)
  10284. {
  10285. if (k == 0)
  10286. inst->noteOn (60, 0.78125f);
  10287. if (k == 10)
  10288. inst->noteOff (60);
  10289. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  10290. }
  10291. 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 } },
  10292. 44100.0, 8192, osc, 512, 120.0, 0, -1.0, true);
  10293. double diff = 0.0, energy = 0.0;
  10294. for (int i = 0; i < 8192; ++i)
  10295. {
  10296. 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]) });
  10297. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10298. }
  10299. CHECK (diff == 0.0 && energy > 1e-3 && ! inst->idle());
  10300. }
  10301. namespace
  10302. {
  10303. // A PoiZone state written from scratch (REA_rodada44 §1.3 layout, "Ver4" + 156 floats + name): pulse A synced to saw B,
  10304. // noise, envelope to the cutoff, triangle LFO to oscillator A's pitch, 3-voice unison, gate, chorus and tape delay.
  10305. std::vector<std::uint8_t> poizoneTestState()
  10306. {
  10307. std::array<float, 156> P {};
  10308. 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 },
  10309. { 10, 0.8f }, { 11, 0.3f }, { 12, 0.5f }, { 13, 0.4f }, { 14, 0.5f }, { 15, 0.5f }, { 16, 0.5f }, { 17, 0.5f },
  10310. { 18, 0.35f }, { 19, 0.6f }, { 20, 0.5f }, { 21, 0.3f }, { 22, 0.2f }, { 23, 0.6f }, { 24, 0.7f }, { 25, 0.5f },
  10311. { 26, 0.7f }, { 28, 0.6f }, { 29, 0.3f }, { 30, 0.8f }, { 31, 1.0f }, { 32, 0.3f }, { 33, 0.5f }, { 34, 0.5f },
  10312. { 35, 0.3f }, { 37, 1.0f }, { 38, 0.6f }, { 39, 1.0f }, { 40, 0.4f }, { 41, 0.5f }, { 42, 0.5f }, { 43, 0.5f },
  10313. { 47, 0.5f }, { 48, 0.3f }, { 67, 0.2f }, { 68, 0.5f }, { 75, 2.0f }, { 76, 15.0f }, { 77, 1.0f }, { 78, 1.0f },
  10314. { 79, 1.0f } };
  10315. for (const auto& [i, v] : values)
  10316. P[(std::size_t) i] = v;
  10317. for (int i = 49; i < 65; ++i)
  10318. P[(std::size_t) i] = (i - 49) % 3 != 2 ? 1.0f : 0.0f;
  10319. for (int i = 81; i < 155; ++i)
  10320. P[(std::size_t) i] = -1.0f;
  10321. std::vector<std::uint8_t> blob { 'S', 'S', 'L', 'F', 0xb4, 0x02, 0x00, 0x00, '4', 'r', 'e', 'V' };
  10322. blob.resize (12 + 4 * 156 + 64, 0);
  10323. std::memcpy (blob.data() + 12, P.data(), 4 * 156);
  10324. std::memcpy (blob.data() + 12 + 4 * 156, "LYNX test", 9);
  10325. return blob;
  10326. }
  10327. } // namespace
  10328. TEST (poizone_engine)
  10329. {
  10330. const auto blob = poizoneTestState();
  10331. CHECK (lynx::poizoneStateUsable (blob));
  10332. CHECK (lynx::poizoneParamOffset (blob, 18) == 12 + 4 * 18 && lynx::poizoneParamOffset (blob, 156) < 0);
  10333. CHECK (lynx::poizoneParam (blob, 75) == 2.0f && ! lynx::poizoneParam (blob, -1));
  10334. auto broken = blob;
  10335. broken[8] = 'X'; // the "Ver4" tag
  10336. CHECK (! lynx::poizoneStateUsable (broken));
  10337. CHECK (! lynx::poizoneStateUsable (std::vector<std::uint8_t> (blob.begin(), blob.begin() + 10)));
  10338. // golden samples of our round-44 model (pzmodel.Plugin, tempo 120, 44.1 kHz, host blocks of 512, L = R = Final.Vol):
  10339. // C5 vel 100 0–30000, E5 vel 80 11025–40000, G5 vel 110 22050–52000 (Init.Vol = (11^(vel/100) − 1)/10)
  10340. auto iv = [] (double vel) { return (std::pow (11.0, vel / 100.0) - 1.0) / 10.0; };
  10341. const std::vector<lynx::poizone::RefNote> notes = { { 0, 60, iv (100), 1.0, 0.0, 0.0, 30000 },
  10342. { 11025, 64, iv (80), 1.0, 0.0, 0.0, 40000 },
  10343. { 22050, 67, iv (110), 1.0, 0.0, 0.0, 52000 } };
  10344. const auto y = lynx::poizone::renderReference (blob, notes, 44100.0, 66150);
  10345. CHECK (y.size() == 2 * 66150u);
  10346. 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 } };
  10347. for (const auto& g : golden)
  10348. 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);
  10349. double e = 0.0;
  10350. for (auto v : y)
  10351. e += (double) v * v;
  10352. CHECK (std::abs (std::sqrt (e / (double) y.size()) - 0.10990940878400073) < 1e-7);
  10353. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → law A), velocity → Init.Vol
  10354. lynx::ChannelDef c;
  10355. c.instrument = lynx::InstrumentKind::poizone;
  10356. c.pluginState = blob;
  10357. auto inst = lynx::makeInstrument (lynx::InstrumentKind::poizone);
  10358. inst->configure (c, 120.0);
  10359. inst->prepare (44100.0);
  10360. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10361. for (int b = 0; b < 16; ++b)
  10362. {
  10363. if (b == 0)
  10364. inst->noteOn (60, 0.78125f);
  10365. if (b == 10)
  10366. inst->noteOff (60);
  10367. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10368. }
  10369. 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,
  10370. 512, 120.0, 0, true);
  10371. double diff = 0.0, energy = 0.0;
  10372. for (int i = 0; i < 8192; ++i)
  10373. {
  10374. 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]) });
  10375. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10376. }
  10377. CHECK (diff == 0.0 && energy > 1e-3);
  10378. }
  10379. // ---- Fruit Kick / BassDrum (REA_rodada43): synthetic states and data, golden values of our round-43 models ----
  10380. namespace
  10381. {
  10382. std::vector<std::uint8_t> fruitKickStateA()
  10383. {
  10384. const std::int32_t w[8] = { 0, 0, -600, 64, 128, 32, 20, 0 };
  10385. std::vector<std::uint8_t> b (32);
  10386. std::memcpy (b.data(), w, 32);
  10387. return b;
  10388. }
  10389. std::vector<std::uint8_t> fruitKickStateB()
  10390. {
  10391. std::vector<std::uint8_t> b (400, 0);
  10392. const std::int32_t head[7] = { 1, 624, -8, 42, 86, 64, 0 };
  10393. std::memcpy (b.data(), head, sizeof (head));
  10394. std::int32_t recs[5][17];
  10395. for (auto& r : recs)
  10396. {
  10397. const std::int32_t d[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 20000, 0, 32950, 0, 0, 0, 0 };
  10398. std::memcpy (r, d, sizeof (d));
  10399. }
  10400. const std::int32_t pan[17] = { 0, 0, 100, 20000, 20000, 30000, 50, 20000, 0, 100, 3000, 64, 20000, 1, 0, 0, 0 };
  10401. const std::int32_t vol[17] = { 4, -1, 100, 3000, 100, 25000, 64, 15000, 0, 100, 20000, 0, 32950, 0, 0, 40, -101 };
  10402. std::memcpy (recs[0], pan, sizeof (pan));
  10403. std::memcpy (recs[1], vol, sizeof (vol));
  10404. for (int k = 0; k < 5; ++k)
  10405. std::memcpy (b.data() + 32 + 68 * k, recs[k], 68);
  10406. const std::int32_t cut = 256;
  10407. std::memcpy (b.data() + 32 + 0x15c, &cut, 4);
  10408. return b;
  10409. }
  10410. std::string bassDrumTestFolder()
  10411. {
  10412. // our own oscillator table and a pseudo-random 16-bit noise sample, laid out like FL's BassDrum folder
  10413. const auto dir = std::filesystem::temp_directory_path() / "lynx_bassdrum_test";
  10414. std::filesystem::create_directories (dir / "Noise Samples");
  10415. auto wav = [] (const std::filesystem::path& path, const void* data, std::uint32_t bytes, std::uint16_t fmt, std::uint16_t bits) {
  10416. std::ofstream o (path, std::ios::binary);
  10417. auto u32 = [&] (std::uint32_t v) { o.write ((const char*) &v, 4); };
  10418. auto u16 = [&] (std::uint16_t v) { o.write ((const char*) &v, 2); };
  10419. o.write ("RIFF", 4), u32 (36 + bytes), o.write ("WAVEfmt ", 8), u32 (16), u16 (fmt), u16 (1), u32 (44100),
  10420. u32 (44100u * bits / 8), u16 ((std::uint16_t) (bits / 8)), u16 (bits), o.write ("data", 4), u32 (bytes);
  10421. o.write ((const char*) data, bytes);
  10422. };
  10423. std::vector<float> table (1024);
  10424. for (int i = 0; i < 1024; ++i)
  10425. 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));
  10426. wav (dir / "sounddata", table.data(), 4096, 3, 32);
  10427. std::vector<std::int16_t> noise (3000);
  10428. std::uint32_t s = 12345;
  10429. for (auto& v : noise)
  10430. {
  10431. s = (s * 1103515245u + 12345u) & 0x7fffffffu;
  10432. v = (std::int16_t) ((int) ((s >> 8) % 20001u) - 10000);
  10433. }
  10434. wav (dir / "Noise Samples" / "test noise.wav", noise.data(), 6000, 1, 16);
  10435. return dir.string();
  10436. }
  10437. std::vector<std::uint8_t> bassDrumTestState()
  10438. {
  10439. std::vector<std::uint8_t> b (0x50C + 92, 0);
  10440. std::memcpy (b.data(), "SSLF", 4);
  10441. const std::uint32_t size = 0x560;
  10442. std::memcpy (b.data() + 4, &size, 4);
  10443. std::memcpy (b.data() + 8, "2kiK", 4);
  10444. std::memcpy (b.data() + 12, "Test kick", 9);
  10445. const char path[] = "C:/Somewhere/Noise Samples/test noise.wav";
  10446. std::memcpy (b.data() + 268, path, sizeof (path) - 1);
  10447. 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,
  10448. 0.5f, 0.8f, 0.15f, 0.9f, 0.3f, 0.5f, 0.6f, 0.62f, 0.4f, 0.5f, 0.85f };
  10449. std::memcpy (b.data() + 0x50C, P, sizeof (P));
  10450. return b;
  10451. }
  10452. struct Golden
  10453. {
  10454. int i;
  10455. float l, r;
  10456. };
  10457. bool matchesGolden (const std::vector<float>& y, std::initializer_list<Golden> golden, double rms)
  10458. {
  10459. bool ok = true;
  10460. for (const auto& g : golden)
  10461. 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;
  10462. double e = 0.0;
  10463. for (auto v : y)
  10464. e += (double) v * v;
  10465. return ok && std::abs (std::sqrt (e / (double) y.size()) - rms) < 1e-6;
  10466. }
  10467. } // namespace
  10468. TEST (fruitkick_engine)
  10469. {
  10470. const auto A = fruitKickStateA(), B = fruitKickStateB();
  10471. CHECK (lynx::fruitKickStateUsable (A) && lynx::fruitKickStateUsable (B));
  10472. CHECK (! lynx::fruitKickStateUsable (std::vector<std::uint8_t> (A.begin(), A.begin() + 20)));
  10473. CHECK (lynx::fruitKickParam (A, 1) == -600 && lynx::fruitKickParam (B, 0) == 624);
  10474. CHECK (lynx::fruitKickFallbacks (A).empty() && lynx::fruitKickFallbacks (B).empty());
  10475. // a version-0 state takes the channel's envelope records as its snapshot (and becomes version 1)
  10476. std::vector<std::vector<std::uint8_t>> recs;
  10477. for (int k = 0; k < 5; ++k)
  10478. recs.emplace_back (B.begin() + 32 + 68 * k, B.begin() + 32 + 68 * (k + 1));
  10479. const auto withEnv = lynx::fruitKickWithChannelEnvelope (A, recs, 256, 0, 0);
  10480. CHECK (withEnv.size() == 400 && lynx::fruitKickParam (withEnv, 5) == 20 && std::memcmp (withEnv.data() + 32, B.data() + 32, 364) == 0);
  10481. CHECK (lynx::fruitKickWithChannelEnvelope (B, recs, 256, 0, 0) == B);
  10482. // golden samples of our round-43 model (fkmodel.run, 44.1 kHz, host blocks of 512): C5 at 0 held, C6 at 3000
  10483. // (Final.Vol 0.5, pan 0.3) released at 9000; B with the plugin's tick every 230 samples from 115
  10484. 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 } };
  10485. const auto ya = lynx::fruitkick::renderReference (A, notes, 44100.0, 20000);
  10486. CHECK (ya.size() == 40000u);
  10487. // (A uses the distortion: values since REA_rodada55 with FL's x87 live host waveshaper, checked against FLEngine's own)
  10488. CHECK (matchesGolden (ya, { { 10, 0.803601742f, 0.803601742f }, { 700, 0.185467482f, 0.185467482f }, { 2900, -0.63492614f, -0.63492614f },
  10489. { 3100, 0.339092523f, 0.544403195f }, { 5000, -0.0974622667f, -0.150664076f }, { 9100, 0.124627575f, 0.0544180274f },
  10490. { 9400, -0.146492869f, -0.144876853f }, { 15000, 0.009439799f, 0.009439799f } },
  10491. 0.284267678986));
  10492. std::vector<int> ticks;
  10493. for (int t = 115; t < 20000; t += 230)
  10494. ticks.push_back (t);
  10495. const auto yb = lynx::fruitkick::renderReference (B, notes, 44100.0, 20000, 512, ticks);
  10496. CHECK (matchesGolden (yb, { { 10, -0.00760388654f, -0.00760388654f }, { 700, 0.386696398f, 0.442232877f }, { 2900, -0.123097703f, -0.302172035f },
  10497. { 3100, 0.0523624197f, 0.0877688825f }, { 5000, 0.045177035f, 0.100505695f }, { 9100, -0.00423920294f, 0.00810315367f },
  10498. { 9400, 0.00482427329f, -0.010199016f }, { 15000, -0.000171193431f, 1.01026744e-05f } },
  10499. 0.117964927585));
  10500. // the instrument: same engine behind LYNX's channel gains (V = 1, centre), velocity → Init.Vol (link velocity on)
  10501. lynx::ChannelDef c;
  10502. c.instrument = lynx::InstrumentKind::fruitkick;
  10503. c.pluginState = A;
  10504. auto inst = lynx::makeInstrument (lynx::InstrumentKind::fruitkick);
  10505. inst->configure (c, 140.0);
  10506. inst->prepare (44100.0);
  10507. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10508. for (int b = 0; b < 16; ++b)
  10509. {
  10510. if (b == 0)
  10511. inst->noteOn (60, 0.78125f);
  10512. if (b == 10)
  10513. inst->noteOff (60);
  10514. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10515. }
  10516. const auto iv = (std::pow (11.0, 1.28 * 0.78125) - 1.0) / 10.0;
  10517. const auto ref = lynx::fruitkick::renderReference (A, { { 0, 60, iv, 0.0, 0.0, 5120 } }, 44100.0, 8192);
  10518. double diff = 0.0, energy = 0.0;
  10519. for (int i = 0; i < 8192; ++i)
  10520. {
  10521. 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]) });
  10522. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10523. }
  10524. CHECK (diff < 1e-6 && energy > 1e-2);
  10525. }
  10526. TEST (bassdrum_engine)
  10527. {
  10528. // FL's CRT powf as the engine computes it: a case where its last bit differs from the correctly rounded power
  10529. const float x = 0.87011606f;
  10530. std::uint32_t bits = 0;
  10531. const auto p = lynx::bassdrum::powfMsvc (x, 0.125f);
  10532. std::memcpy (&bits, &p, 4);
  10533. CHECK (bits == 1065063964u);
  10534. CHECK (lynx::bassdrum::powfMsvc (2.0f, 1.0f) == 2.0f && lynx::bassdrum::powfMsvc (10.0f, 0.0f) == 1.0f);
  10535. const auto st = bassDrumTestState();
  10536. CHECK (lynx::bassDrumStateUsable (st) && lynx::bassDrumNoiseName (st) == "test noise.wav");
  10537. CHECK (lynx::bassDrumParamOffset (st, 6) == 0x50C + 24 && lynx::bassDrumParamOffset (st, 23) < 0);
  10538. auto broken = st;
  10539. broken[8] = 'X';
  10540. CHECK (! lynx::bassDrumStateUsable (broken));
  10541. // golden samples of our round-43 model (bdmodel.run with its powf port, 44.1 kHz, host blocks of 512)
  10542. const auto dir = bassDrumTestFolder();
  10543. 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 },
  10544. { 6100, 55, 1.5, 1.0, -0.3, 0.0, -1 } };
  10545. const auto y = lynx::bassdrum::renderReference (st, notes, 44100.0, 30000, dir);
  10546. CHECK (y.size() == 60000u);
  10547. CHECK (matchesGolden (y, { { 50, 0.0122119058f, 0.0122119058f }, { 400, -0.0886279643f, -0.0886279643f }, { 3000, -0.0428197235f, -0.0428197235f },
  10548. { 6050, 0.0199260078f, 0.0240648985f }, { 6300, -0.257363439f, -0.185516387f }, { 9000, 0.268954486f, 0.10506025f },
  10549. { 15000, -0.0102827176f, -0.0380715057f }, { 25000, 0.0f, 0.0f } },
  10550. 0.230007588169));
  10551. CHECK (lynx::bassdrum::renderReference (st, notes, 44100.0, 100, dir + "/nowhere").empty()); // no table: no engine
  10552. lynx::ChannelDef c;
  10553. c.instrument = lynx::InstrumentKind::bassdrum;
  10554. c.pluginState = st;
  10555. c.samplePath = dir;
  10556. auto inst = lynx::makeInstrument (lynx::InstrumentKind::bassdrum);
  10557. inst->configure (c, 120.0);
  10558. inst->prepare (44100.0);
  10559. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  10560. for (int b = 0; b < 16; ++b)
  10561. {
  10562. if (b == 0)
  10563. inst->noteOn (60, 0.78125f);
  10564. inst->render (l.data() + 512 * b, r.data() + 512 * b, 512, 1.0f, 1.0f);
  10565. }
  10566. 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);
  10567. double diff = 0.0, energy = 0.0;
  10568. for (int i = 0; i < 8192; ++i)
  10569. {
  10570. 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]) });
  10571. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  10572. }
  10573. CHECK (diff == 0.0 && energy > 1e-2);
  10574. }
  10575. // REA_rodada55: Fruity Fast Dist, Fruity Flangus and Fruity Flanger, goldens from the installation's DLLs (EXEC, our own states)
  10576. TEST (fruity_fast_dist_flangus_flanger)
  10577. {
  10578. std::vector<float> inL (4096), inR (4096);
  10579. for (int n = 0; n < 4096; ++n)
  10580. {
  10581. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0);
  10582. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0);
  10583. }
  10584. struct Gold { int i; float l, r; };
  10585. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  10586. // Fast Dist (+ FL's host waveshaper): live and render kernels
  10587. auto dist = [&] (lynx::fastdistfx::State s, bool render, std::initializer_list<Gold> gold) {
  10588. std::vector<float> l (inL), r (inR);
  10589. lynx::fastdistfx::process (l.data(), 4096, s, render);
  10590. lynx::fastdistfx::process (r.data(), 4096, s, render);
  10591. for (const auto& g : gold)
  10592. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10593. };
  10594. dist ({ 128, 10, 0, 128, 128 }, false, { { 100, 0.0f, -0.39352417f }, { 1000, -0.39352417f, 0.389404297f }, { 2047, 0.376953125f, -0.39352417f },
  10595. { 3000, -0.39352417f, -0.379760742f }, { 4095, 0.342590332f, 0.373931885f } });
  10596. dist ({ 128, 10, 0, 128, 128 }, true, { { 3000, -0.39352417f, -0.379751563f }, { 4095, 0.342590332f, 0.373931885f } });
  10597. dist ({ 192, 4, 1, 77, 97 }, false, { { 100, 0.0f, -0.868428111f }, { 1000, -1.23000622f, 0.740812242f }, { 2047, 0.58129251f, -0.855666518f },
  10598. { 3000, -1.23000622f, -0.613196433f }, { 4095, 0.307311207f, 0.549388587f } });
  10599. dist ({ 192, 4, 1, 77, 97 }, true, { { 2047, 0.58129245f, -0.855666518f }, { 4095, 0.307311267f, 0.549388528f } });
  10600. dist ({ 90, 7, 0, 40, 120 }, false, { { 100, 0.0f, -0.346225947f }, { 1000, -0.511893928f, 0.287246376f }, { 2047, 0.212910682f, -0.340344131f },
  10601. { 3000, -0.511893928f, -0.227848753f }, { 4095, 0.0910475403f, 0.197926477f } });
  10602. dist ({ 90, 7, 0, 40, 120 }, true, { { 100, 0.0f, -0.346223623f }, { 1000, -0.511893749f, 0.287246734f }, { 2047, 0.212909341f, -0.340341568f },
  10603. { 3000, -0.511893749f, -0.227846757f }, { 4095, 0.0910428166f, 0.197928891f } });
  10604. {
  10605. const auto* T = lynx::fastdistfx::curve (0, 10);
  10606. CHECK (T[0] == 0 && T[8191] == T[8192] && lynx::fastdistfx::curve (1, 1)[8191] == 32767);
  10607. }
  10608. // Flangus: FL's call order (state, sample rate, block size of 512), no flush
  10609. auto flange = [&] (lynx::flangusfx::State s, int fs, std::initializer_list<Gold> gold) {
  10610. lynx::flangusfx::Engine e;
  10611. e.prepare (fs);
  10612. e.load (s);
  10613. e.setRate (fs);
  10614. e.setBlockSize (512);
  10615. std::vector<float> io (8192);
  10616. for (int n = 0; n < 4096; ++n)
  10617. {
  10618. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  10619. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10620. }
  10621. for (int o = 0; o < 4096; o += 512)
  10622. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  10623. for (const auto& g : gold)
  10624. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10625. };
  10626. flange ({ 4, 512, 768, 8, 128, -32, 96, 96 }, 44100, { { 100, 0.0249633491f, -0.119510099f }, { 1000, -1.26988697f, 0.687134743f },
  10627. { 2047, 1.03176403f, -0.278752804f }, { 3000, -0.940228283f, -0.853936791f },
  10628. { 4095, 0.201686561f, 0.607052445f } });
  10629. flange ({ 7, 300, 900, 40, 30, 100, -60, 128 }, 44100, { { 100, 0.0f, 0.0446268953f }, { 1000, 1.68992829f, 2.06982398f },
  10630. { 2047, 0.400214791f, 0.838502884f }, { 3000, 1.20018816f, 1.18875635f },
  10631. { 4095, -0.354708612f, 0.503573656f } });
  10632. flange ({ 2, 1024, 200, 0, 0, -128, 128, -90 }, 48000, { { 100, 0.172592998f, -0.230506495f }, { 1000, -1.56807125f, 0.854493499f },
  10633. { 2047, 1.02325416f, -1.17055321f }, { 3000, -1.74136555f, -0.535462856f },
  10634. { 4095, 1.49345946f, -0.537866116f } });
  10635. // Flanger: state, then the sample rate (FL's call order)
  10636. auto flanger = [&] (lynx::flangerfx::State s, int fs, std::initializer_list<Gold> gold) {
  10637. lynx::flangerfx::Engine e;
  10638. e.load (s);
  10639. e.prepare (fs);
  10640. std::vector<float> io (8192);
  10641. for (int n = 0; n < 4096; ++n)
  10642. {
  10643. io[2 * (std::size_t) n] = inL[(std::size_t) n];
  10644. io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10645. }
  10646. for (int o = 0; o < 4096; o += 512)
  10647. e.render (io.data() + 2 * o, io.data() + 2 * o, 512);
  10648. for (const auto& g : gold)
  10649. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10650. };
  10651. flanger ({ 0, 2000, 2000, 118, 0, 0, 23, 0, 1024, 3500, 3000, 2500 }, 44100,
  10652. { { 100, -1.22955823f, -1.98144293f }, { 1000, -0.975861192f, 0.646231353f }, { 2047, 0.518505156f, -0.857361794f },
  10653. { 3000, -0.546559215f, -0.0157452822f }, { 4095, 0.104127884f, 0.206255436f } });
  10654. flanger ({ 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 }, 48000,
  10655. { { 100, 0.0f, -0.440000027f }, { 1000, 0.24619779f, 0.775473535f }, { 2047, 1.11609459f, -0.0639692694f },
  10656. { 3000, -1.22629714f, -1.41118503f }, { 4095, -0.263562649f, 0.80683732f } });
  10657. // import: FL's states become the exact effects with the controls as parameters; short states keep the approximations
  10658. {
  10659. 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 };
  10660. const auto slot = lynx::effectFromFl ("Fruity Fast Dist", fd, 120.0);
  10661. CHECK (slot && slot->kind == lynx::EffectKind::fruityFastDist);
  10662. if (slot)
  10663. {
  10664. const auto s = lynx::fastdistfx::fromParams (slot->params.data());
  10665. CHECK (s.pre == 192 && s.threshold == 4 && s.type == 1 && s.mix == 77 && s.post == 97);
  10666. auto fx = lynx::makeEffect (*slot);
  10667. fx->prepare (44100.0);
  10668. std::vector<float> l (inL), r (inR);
  10669. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10670. CHECK (l[1000] == -1.23000622f && r[4095] == 0.549388587f);
  10671. }
  10672. std::vector<std::uint8_t> fg (32);
  10673. const int v[8] { 7, 300, 900, 40, 30, 100, -60, 128 };
  10674. std::memcpy (fg.data(), v, 32);
  10675. const auto f2 = lynx::effectFromFl ("Fruity Flangus", fg, 120.0);
  10676. CHECK (f2 && f2->kind == lynx::EffectKind::fruityFlangus);
  10677. if (f2)
  10678. {
  10679. const auto s = lynx::flangusfx::fromParams (f2->params.data());
  10680. for (int i = 0; i < 8; ++i)
  10681. CHECK (s[(std::size_t) i] == v[i]);
  10682. auto fx = lynx::makeEffect (*f2);
  10683. fx->prepare (48000.0);
  10684. std::vector<float> l (inL), r (inR);
  10685. fx->process (l.data(), r.data(), 4096, f2->params, 120.0);
  10686. CHECK (std::isfinite (l[4095]) && std::isfinite (r[4095]) && std::abs (l[2000]) > 0.0f);
  10687. }
  10688. std::vector<std::uint8_t> fl (52);
  10689. const int w[13] { 1, 300, 4000, 4500, 90, 1000, 300, 99, 1024, 1024, 2000, 4000, 1000 };
  10690. std::memcpy (fl.data(), w, 52);
  10691. const auto f3 = lynx::effectFromFl ("Fruity Flanger", fl, 120.0);
  10692. CHECK (f3 && f3->kind == lynx::EffectKind::fruityFlanger);
  10693. if (f3)
  10694. {
  10695. const auto s = lynx::flangerfx::fromParams (f3->params.data());
  10696. for (int i = 0; i < 12; ++i)
  10697. CHECK (s[(std::size_t) i] == w[i + 1]);
  10698. auto fx = lynx::makeEffect (*f3);
  10699. fx->prepare (48000.0);
  10700. std::vector<float> l (inL), r (inR);
  10701. fx->process (l.data(), r.data(), 4096, f3->params, 120.0);
  10702. CHECK (l[1000] == 0.24619779f && r[4095] == 0.80683732f);
  10703. }
  10704. const auto shortFd = lynx::effectFromFl ("Fruity Fast Dist", std::vector<std::uint8_t> (8, 0), 120.0);
  10705. CHECK (! shortFd || shortFd->kind != lynx::EffectKind::fruityFastDist);
  10706. }
  10707. }
  10708. // Workflow P2 (docs/WORKFLOW_FL.md)
  10709. #include "WorkflowP2Tests.inc"
  10710. // Editor mouse grammar (Piano Roll / Playlist)
  10711. #include "EditorGestureTests.inc"
  10712. TEST (chorus_family_engines)
  10713. {
  10714. // REA_rodada59: goldens from the installation's DLLs (44.1 kHz, blocks of 512) on this signal
  10715. std::vector<float> inL (4096), inR (4096);
  10716. for (int n = 0; n < 4096; ++n)
  10717. {
  10718. inL[(std::size_t) n] = (float) (((n * 37) % 200 - 100) / 60.0) * 0.5f;
  10719. inR[(std::size_t) n] = (float) (((n * 53) % 170 - 85) / 50.0) * 0.5f;
  10720. }
  10721. struct Gold { int i; float l, r; };
  10722. auto close = [] (float a, float b) { return std::abs (a - b) <= 2.0e-7f * std::max (1.0f, std::abs (b)); };
  10723. auto run = [&] (const char* plugin, const std::vector<std::uint8_t>& st, lynx::EffectKind kind, std::initializer_list<Gold> gold) {
  10724. const auto slot = lynx::effectFromFl (plugin, st, 120.0);
  10725. CHECK (slot && slot->kind == kind);
  10726. if (! slot)
  10727. return;
  10728. auto fx = lynx::makeEffect (*slot);
  10729. fx->prepare (44100.0);
  10730. std::vector<float> l (inL), r (inR);
  10731. fx->process (l.data(), r.data(), 1024, slot->params, 120.0);
  10732. fx->process (l.data() + 1024, r.data() + 1024, 3072, slot->params, 120.0);
  10733. for (const auto& g : gold)
  10734. CHECK (close (l[(std::size_t) g.i], g.l) && close (r[(std::size_t) g.i], g.r));
  10735. };
  10736. auto ints = [] (std::initializer_list<int> v) {
  10737. std::vector<std::uint8_t> b;
  10738. for (int x : v)
  10739. for (int k = 0; k < 4; ++k)
  10740. b.push_back ((std::uint8_t) ((unsigned) x >> (8 * k)));
  10741. return b;
  10742. };
  10743. // Fruity Chorus (start phases 0, which FL draws at random)
  10744. run ("Fruity Chorus", ints ({ 1, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), lynx::EffectKind::fruityChorus,
  10745. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.6181581020355225f, 0.2707735002040863f }, { 2047, -0.01424609124660492f, 0.24228274822235107f },
  10746. { 3000, -1.1650776863098145f, -0.1809898018836975f }, { 4095, -0.012790083885192871f, 0.060571759939193726f } });
  10747. run ("Fruity Chorus", ints ({ 1, 700, 4000, 900, 4000, 3000, 2000, 400, 700, 1000, 30, 4999, 0 }), lynx::EffectKind::fruityChorus,
  10748. { { 100, 0.0f, -0.3850000202655792f }, { 1000, -0.5833333134651184f, 0.36345374584198f }, { 2047, -0.0336097776889801f, -0.3360772430896759f },
  10749. { 3000, -0.40196430683135986f, -0.5073210597038269f }, { 4095, -0.1176539734005928f, 0.444706529378891f } });
  10750. run ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), lynx::EffectKind::fruityChorus,
  10751. { { 100, 0.2975943684577942f, 0.3747217655181885f }, { 1000, -0.39463338255882263f, 0.18099871277809143f }, { 2047, 0.3894335627555847f, 0.35111793875694275f },
  10752. { 3000, -0.1450725942850113f, 0.2847272455692291f }, { 4095, 0.33678582310676575f, -0.13365517556667328f } });
  10753. {
  10754. const auto slot = lynx::effectFromFl ("Fruity Chorus", ints ({ 1, 36, 469, 1004, 2832, 293, 3500, 0, 428, 0, 0, 3887, 1024 }), 120.0);
  10755. CHECK (slot && lynx::fruitychorusfx::fromParams (slot->params.data())[2] == 1004);
  10756. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityChorus, 11, 1.0f, 120.0) == "on");
  10757. CHECK (lynx::effectParamFromFl ("Fruity Chorus", 11).has_value() && ! lynx::effectParamFromFl ("Fruity Chorus", 12).has_value());
  10758. }
  10759. // a state FL does not read (version 2) or too short keeps the generic chorus
  10760. CHECK (! lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0).has_value()
  10761. || lynx::effectFromFl ("Fruity Chorus", ints ({ 2, 512, 2250, 341, 1500, 2500, 3500, 23, 0, 0, 0, 2500, 0 }), 120.0)->kind != lynx::EffectKind::fruityChorus);
  10762. CHECK (! lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0).has_value()
  10763. || lynx::effectFromFl ("Fruity Chorus", { 1, 0, 0, 0 }, 120.0)->kind != lynx::EffectKind::fruityChorus);
  10764. // Hyper Chorus engine in FL's load sequence (defaults, then the state: the parameters glide), instance k's phases
  10765. auto hyper = [&] (std::initializer_list<int> raws, int instance, std::initializer_list<Gold> gold) {
  10766. namespace hc = lynx::hyperchorusfx;
  10767. hc::Engine e (instance);
  10768. const std::vector<int> r (raws);
  10769. 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); };
  10770. for (int i = 0; i < hc::params; ++i)
  10771. e.setParam (i, val (i, hc::defaults[(std::size_t) i]));
  10772. for (int i = 0; i < hc::params; ++i)
  10773. e.setParam (i, val (i, r[(std::size_t) i]));
  10774. e.setRate (44100.0);
  10775. std::vector<float> io (8192);
  10776. for (int n = 0; n < 4096; ++n)
  10777. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10778. for (int s = 0; s < 4096; s += 512)
  10779. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  10780. for (const auto& g : gold)
  10781. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10782. };
  10783. hyper ({ 562, 919, 499, 505, 709, 716, 1, 19247, 10900, 191, 1000 }, 0,
  10784. { { 100, 0.0f, -0.4115639328956604f }, { 1000, -0.658551037311554f, 0.28647756576538086f }, { 2047, 0.24743860960006714f, -0.5088979005813599f },
  10785. { 3000, -0.5171564221382141f, -0.3630276620388031f }, { 4095, 0.08128821104764938f, 0.18811003863811493f } });
  10786. hyper ({ 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750 }, 2,
  10787. { { 100, 0.0f, -0.398922324180603f }, { 1000, -1.1528236865997314f, 0.2839684784412384f }, { 2047, 0.5115746855735779f, -0.3330395221710205f },
  10788. { 3000, -0.6094785928726196f, 0.17040175199508667f }, { 4095, -0.11666109412908554f, 0.21931695938110352f } });
  10789. CHECK (lynx::hyperchorusfx::randState (1) == 1u * 214013u + 2531011u);
  10790. {
  10791. // the import: controls as parameters; a song's Hyper Chorus slots are numbered in mixer order
  10792. const auto st = ints ({ 2, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 });
  10793. const auto slot = lynx::effectFromFl ("Hyper Chorus", st, 120.0);
  10794. CHECK (slot && slot->kind == lynx::EffectKind::hyperChorus && lynx::hyperchorusfx::fromParams (slot->params.data())[9] == 368);
  10795. lynx::Song song;
  10796. song.mixer.resize (3);
  10797. song.mixer[1].effects = { *slot, *slot };
  10798. song.mixer[2].effects = { *slot };
  10799. lynx::numberChorusInstances (song);
  10800. CHECK (lynx::hyperchorusfx::instanceOf (song.mixer[2].effects[0].state) == 2);
  10801. auto fx = lynx::makeEffect (song.mixer[2].effects[0]);
  10802. fx->prepare (48000.0);
  10803. std::vector<float> l (inL), r (inR);
  10804. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10805. bool finite = true, wet = false;
  10806. for (int n = 0; n < 4096; ++n)
  10807. {
  10808. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  10809. wet = wet || l[(std::size_t) n] != inL[(std::size_t) n];
  10810. }
  10811. CHECK (finite && wet);
  10812. CHECK (lynx::formatEffectParam (lynx::EffectKind::hyperChorus, 8, 0.0f, 120.0) == "off");
  10813. CHECK (! lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0).has_value()
  10814. || lynx::effectFromFl ("Hyper Chorus", ints ({ 3, 1000, 555, 0, 400, 300, 300, 0, 22000, 120, 368, 750, 0 }), 120.0)->kind != lynx::EffectKind::hyperChorus);
  10815. }
  10816. // Vintage Chorus engine as FL drives it (created, sample rate, each control), fresh instance (its smoothed values
  10817. // start from the engine's initial ones)
  10818. auto vintage = [&] (std::initializer_list<int> raws, std::initializer_list<Gold> gold) {
  10819. namespace vc = lynx::vintagechorusfx;
  10820. vc::Engine e;
  10821. e.setRate (44100.0);
  10822. int i = 0;
  10823. for (int raw : raws)
  10824. {
  10825. 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));
  10826. ++i;
  10827. }
  10828. std::vector<float> io (8192);
  10829. for (int n = 0; n < 4096; ++n)
  10830. io[2 * (std::size_t) n] = inL[(std::size_t) n], io[2 * (std::size_t) n + 1] = inR[(std::size_t) n];
  10831. for (int s = 0; s < 4096; s += 512)
  10832. e.render (io.data() + 2 * s, io.data() + 2 * s, 512);
  10833. for (const auto& g : gold)
  10834. CHECK (close (io[2 * (std::size_t) g.i], g.l) && close (io[2 * (std::size_t) g.i + 1], g.r));
  10835. };
  10836. vintage ({ 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 },
  10837. { { 100, 0.0f, -0.05606573447585106f }, { 1000, -0.3413679003715515f, 0.20469821989536285f }, { 2047, 0.1487489938735962f, -0.28199973702430725f },
  10838. { 3000, -0.31699514389038086f, -0.43230289220809937f }, { 4095, 0.1255197525024414f, 0.0810704305768013f } });
  10839. vintage ({ 2, 0, 80, 0, 0, 60, 3000, 3000, 0, 0, 0, 3000, 0, 3000, 0, 0, 0 }, // hiss
  10840. { { 100, -1.2527654007499223e-06f, -0.053655341267585754f }, { 1000, -0.44306400418281555f, 0.25606146454811096f }, { 2047, 0.23425261676311493f, -0.21375307440757751f },
  10841. { 3000, -0.5793180465698242f, -0.13371579349040985f }, { 4095, 0.05595953390002251f, 0.11362095922231674f } });
  10842. vintage ({ 0, 1, 49152, 0, 3000, 60, 3000, 3000, 0, 0, 0, 3000, 0, 0, 0, 0, 0 }, // Chorus 1 + 2
  10843. { { 100, 0.0f, -0.05365874245762825f }, { 1000, -0.5095958113670349f, 0.2809423506259918f }, { 2047, 0.17256303131580353f, -0.3563305139541626f },
  10844. { 3000, -0.471851646900177f, -0.30123069882392883f }, { 4095, 0.0959278866648674f, 0.13436323404312134f } });
  10845. {
  10846. const auto st = ints ({ 1, 2, 0, 211046, 1, 2411, 960, 1985, 5121, 0, 1, 4420, 6000, 273, 0, 0, 0, 0 });
  10847. const auto slot = lynx::effectFromFl ("Vintage Chorus", st, 120.0);
  10848. CHECK (slot && slot->kind == lynx::EffectKind::vintageChorus && lynx::vintagechorusfx::fromParams (slot->params.data())[2] == 211046);
  10849. auto fx = lynx::makeEffect (*slot);
  10850. fx->prepare (44100.0);
  10851. std::vector<float> l (inL), r (inR);
  10852. fx->process (l.data(), r.data(), 4096, slot->params, 120.0);
  10853. bool finite = true;
  10854. for (int n = 0; n < 4096; ++n)
  10855. finite = finite && std::isfinite (l[(std::size_t) n]) && std::isfinite (r[(std::size_t) n]);
  10856. CHECK (finite && l[3000] != inL[3000]);
  10857. CHECK (lynx::formatEffectParam (lynx::EffectKind::vintageChorus, 0, 0.0f, 120.0) == "Chorus 1");
  10858. 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()
  10859. || 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);
  10860. }
  10861. }
  10862. #include "UiChromeTests.inc"
  10863. TEST (fruity_delay_engines)
  10864. {
  10865. // REA_rodada52: the engines on their own against the installation's DLLs (goldens = the DLL's output for the same
  10866. // input with FL's set-up order, EXEC: bit-identical), then the mixer effects built from FL states
  10867. const int n = 16384;
  10868. std::vector<float> inL ((std::size_t) n, 0.0f), inR ((std::size_t) n, 0.0f);
  10869. std::uint32_t seed = 1;
  10870. for (int i = 0; i < 4096; ++i) // a noise burst, then the echoes
  10871. {
  10872. seed = seed * 1664525u + 1013904223u;
  10873. inL[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  10874. seed = seed * 1664525u + 1013904223u;
  10875. inR[(std::size_t) i] = (float) ((int) (seed >> 16) - 32768) / 32768.0f * 0.5f;
  10876. }
  10877. using Golden = std::vector<std::tuple<int, float, float>>;
  10878. auto check = [&] (const std::vector<float>& l, const std::vector<float>& r, const Golden& golden) {
  10879. for (const auto& [i, gl, gr] : golden)
  10880. CHECK (std::abs (l[(std::size_t) i] - gl) < 2e-6f && std::abs (r[(std::size_t) i] - gr) < 2e-6f);
  10881. };
  10882. auto run3 = [&] (const std::array<int, 26>& raw, double bpm) {
  10883. lynx::fldelay::Delay3Engine e (44100.0);
  10884. e.setTempo (bpm);
  10885. for (int i = 0; i < 26; ++i)
  10886. e.setParameter (i, lynx::fldelay::delay3EngineValue (i, raw[(std::size_t) i]));
  10887. e.setSampleRate (44100.0); // FL's wrapper sets the rate up again after the state
  10888. e.setSampleRate (44100.0);
  10889. std::vector<float> l (inL), r (inR);
  10890. for (int s = 0; s < n; s += 512)
  10891. e.process (l.data() + s, r.data() + s, 512);
  10892. return std::make_pair (l, r);
  10893. };
  10894. auto run2 = [&] (const std::array<int, 8>& raw, double bpm) {
  10895. lynx::fldelay::Delay2State st;
  10896. st.v = raw;
  10897. lynx::fldelay::Delay2Engine e (st, 44100.0, bpm, 512);
  10898. std::vector<float> l (inL), r (inR);
  10899. for (int s = 0; s < n; s += 512)
  10900. e.process (l.data() + s, r.data() + s, 512);
  10901. return std::make_pair (l, r);
  10902. };
  10903. // Delay 3, true stereo, synced 1/8 beat, cutoff + resonance (Fruity Delay 3_x64.dll at 120 and 100 BPM)
  10904. 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 };
  10905. auto o = run3 (a, 120.0);
  10906. check (o.first, o.second, { { 3000, 0.399949551f, 0.598533869f }, { 6000, 0.116703831f, -0.315510482f }, { 9000, -0.150520369f, -0.250620633f },
  10907. { 13000, 0.193608373f, 0.0598564595f }, { 16000, -0.0524701327f, 0.022601109f } });
  10908. o = run3 (a, 100.0);
  10909. check (o.first, o.second, { { 5000, 0.377422422f, 0.346570343f }, { 6000, -0.182619616f, 0.0488029197f }, { 11000, -0.00120426295f, 0.0253051519f },
  10910. { 16000, 0.0806550607f, -0.0991330743f } });
  10911. // ping pong, keep pitch, modulation, cutoff LFO, diffusion, saturation, rate and bit reduction, colour
  10912. 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);
  10913. check (o.first, o.second, { { 1500, 0.288540125f, -0.148862943f }, { 5000, 5.90502168e-06f, 0.111382999f }, { 8000, -0.0736631379f, -0.0305093247f },
  10914. { 11000, -0.00831047911f, 0.0102323527f }, { 16000, 0.0175791476f, 0.0273917634f } });
  10915. // fake stereo, unsynced, high-pass, limit distortion, no dry
  10916. 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);
  10917. check (o.first, o.second, { { 1500, 0.0825810209f, 0.0228505582f }, { 6000, -0.0427959524f, -0.0408911295f }, { 9000, 0.0114790015f, 0.019227922f },
  10918. { 16000, -0.000312746735f, 0.00101647642f } });
  10919. // delay type "Off": filter, distortion and the bit/rate reduction on the dry path
  10920. 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);
  10921. check (o.first, o.second, { { 1500, 0.238686681f, -0.106820084f }, { 3000, 0.131528616f, 0.0665975735f }, { 5000, 9.94473215e-09f, -8.22819057e-09f },
  10922. { 11000, 0.0f, 0.0f } });
  10923. // Delay 2: default, ping pong with a stereo offset, swapped without dry (Fruity Delay 2_x64.dll)
  10924. o = run2 ({ 0, 128, 128, 64, 36, 0, 0, 128 }, 120.0);
  10925. check (o.first, o.second, { { 3000, 0.211380005f, 0.277908325f }, { 5000, -0.223615795f, -0.0189359933f }, { 9000, 0.120054141f, -0.00297877868f },
  10926. { 16000, 0.0182533264f, 0.105384827f } });
  10927. o = run2 ({ -40, 110, 90, 90, 24, -300, 2, 90 }, 100.0);
  10928. check (o.first, o.second, { { 3000, 0.0929639712f, 0.122222826f }, { 5000, -0.0884733275f, -0.0507739782f }, { 11000, 0.0589251742f, 0.0f },
  10929. { 16000, -0.029524127f, 0.0128910914f } });
  10930. o = run2 ({ 60, 140, 0, 100, 30, 250, 1, 110 }, 120.0);
  10931. check (o.first, o.second, { { 1500, 0.0f, 0.0f }, { 5000, 0.176379532f, -0.212506086f }, { 8000, -0.324207425f, 0.0675852001f },
  10932. { 13000, 0.145023212f, -0.104788139f } });
  10933. // the mixer effects from FL states: time law (Delay 3 synced 192 = 1 beat; Delay 2 144 = 3/4 beat), settled start
  10934. auto state3 = std::vector<std::uint8_t> (4 + 26 * 4, 0);
  10935. 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 };
  10936. state3[0] = 1;
  10937. std::memcpy (state3.data() + 4, def3.data(), 26 * 4);
  10938. const auto slot3 = lynx::effectFromFl ("Fruity Delay 3", state3, 120.0);
  10939. CHECK (slot3 && slot3->kind == lynx::EffectKind::fruityDelay3);
  10940. auto state2 = std::vector<std::uint8_t> (32, 0);
  10941. const std::array<int, 8> def2 { 0, 128, 128, 0, 144, 0, 0, 128 };
  10942. std::memcpy (state2.data(), def2.data(), 32);
  10943. const auto slot2 = lynx::effectFromFl ("Fruity delay 2", state2, 120.0);
  10944. CHECK (slot2 && slot2->kind == lynx::EffectKind::fruityDelay2);
  10945. CHECK (lynx::formatEffectParam (lynx::EffectKind::fruityDelay2, 4, slot2->params[4], 120.0) == "3:00");
  10946. auto echoAt = [] (const lynx::EffectSlot& slot, double bpm) {
  10947. auto fx = lynx::makeEffect (slot);
  10948. fx->prepare (44100.0);
  10949. std::vector<float> l (48000, 0.0f), r (48000, 0.0f);
  10950. l[100] = 1.0f;
  10951. for (int s = 0; s < 48000; s += 1024)
  10952. fx->process (l.data() + s, r.data() + s, std::min (1024, 48000 - s), slot.params, bpm);
  10953. int best = 200;
  10954. for (int i = 200; i < 48000; ++i)
  10955. if (std::abs (l[(std::size_t) i]) > std::abs (l[(std::size_t) best]))
  10956. best = i;
  10957. return best - 100;
  10958. };
  10959. if (slot3 && slot2)
  10960. {
  10961. CHECK (echoAt (*slot3, 120.0) == 22050);
  10962. CHECK (echoAt (*slot3, 150.0) == 17640);
  10963. CHECK (echoAt (*slot2, 120.0) == 16538);
  10964. CHECK (echoAt (*slot2, 140.0) == 14175);
  10965. }
  10966. }
  10967. #include "UiRackMixerTests.inc"
  10968. // --- REA round 64: SimSynth (exact engine) and Wasp / Wasp XT (FL 2026's silent placeholder) ---------------------
  10969. namespace
  10970. {
  10971. // a SimSynth state written from scratch (REA_rodada64 §1.1 layout: 45 doubles, 19 unused ints, 19 switches, 12 unused
  10972. // ints, version)
  10973. std::vector<std::uint8_t> simSynthTestState (const std::array<double, 45>& P, const std::array<int, 19>& S, int version = 14)
  10974. {
  10975. std::vector<std::uint8_t> b (564, 0);
  10976. std::memcpy (b.data(), P.data(), 360);
  10977. std::memcpy (b.data() + 360 + 76, S.data(), 76);
  10978. std::memcpy (b.data() + 560, &version, 4);
  10979. return b;
  10980. }
  10981. // the 0xD5 records of an FL preset file (.fst: the FLhd/FLdt event stream) that follow a plugin name `plugin`
  10982. std::vector<std::vector<std::uint8_t>> pluginStatesIn (const std::filesystem::path& file, const std::string& plugin)
  10983. {
  10984. std::vector<std::vector<std::uint8_t>> out;
  10985. std::ifstream in (file, std::ios::binary);
  10986. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  10987. std::size_t i = 0;
  10988. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  10989. ++i;
  10990. i += 8;
  10991. std::string name;
  10992. while (i < b.size())
  10993. {
  10994. const auto id = b[i++];
  10995. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  10996. if (id >= 192)
  10997. for (int shift = 0; i < b.size(); shift += 7)
  10998. {
  10999. const auto c = b[i++];
  11000. n |= (std::size_t) (c & 0x7F) << shift;
  11001. if (c < 0x80)
  11002. break;
  11003. }
  11004. if (i + n > b.size())
  11005. break;
  11006. if (id == 0xC9)
  11007. {
  11008. name.clear();
  11009. for (std::size_t k = 0; k < n; ++k)
  11010. if (b[i + k] != 0)
  11011. name += (char) b[i + k]; // ASCII or UTF-16LE (zero bytes skipped)
  11012. }
  11013. if (id == 0xD5 && name == plugin)
  11014. out.emplace_back (b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n));
  11015. i += n;
  11016. }
  11017. return out;
  11018. }
  11019. } // namespace
  11020. TEST (simsynth_engine)
  11021. {
  11022. // A: pulse + octave layer, half-cosine with unison and two octave layers, sine/sine⁴ mix, square LFO → pitch with
  11023. // delay, filter envelope, LP/BP mix, chorus. B (version 7): triangle ring-modulated by filtered noise, sample &
  11024. // hold LFO → cutoff, retrigger, own filter envelope, chorus off
  11025. 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,
  11026. 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 };
  11027. 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 };
  11028. 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,
  11029. 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 };
  11030. 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 };
  11031. const auto blobA = simSynthTestState (A, SA), blobB = simSynthTestState (B, SB, 7);
  11032. dumpIfAsked ("LYNX_DUMP_SIMSYNTH", blobA);
  11033. CHECK (lynx::simSynthStateUsable (blobA) && ! lynx::simSynthStateUsable (std::vector<std::uint8_t> (blobA.begin(), blobA.begin() + 563)));
  11034. const auto hp = lynx::simSynthHostParams (blobA);
  11035. // knobs go through ⌊65535·v⌋, the coarse pitches through 48 steps, the switches and wave selectors as they are
  11036. CHECK (hp.size() == 58u && hp[0] == 52428 && hp[27] == 10080 && hp[41] == 1 && hp[54] == 0 && hp[56] == 4);
  11037. // golden samples of the installation's SimSynth_x64.dll (round-64 EXEC harness: created, state restored, 44.1 kHz,
  11038. // host blocks of 512, old voice record) — LYNX's engine renders bit-identically: C5 0–30000, E5 Final.Vol 0.8 pan
  11039. // 16 11025–40000, G5 pan −20 +25 cents 22050–52000
  11040. const std::vector<lynx::simsynth::RefNote> notes = { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 30000 },
  11041. { 11025, 64, 0.8f, 16, 0, 0.0f, 0.0f, 40000 },
  11042. { 22050, 67, 1.0f, -20, 25, 0.0f, 0.0f, 52000 } };
  11043. struct Golden { int i; float l, r; };
  11044. 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 } };
  11045. 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 } };
  11046. for (int which = 0; which < 2; ++which)
  11047. {
  11048. std::vector<std::int64_t> counters;
  11049. const auto y = lynx::simsynth::renderReference (which == 0 ? blobA : blobB, notes, 44100.0, 66150, 512, &counters);
  11050. CHECK (y.size() == 2 * 66150u);
  11051. if (y.size() != 2 * 66150u)
  11052. continue;
  11053. // the random-table index after creation / state / rate set equals the DLL's (276 / 696 / 756 and 276 / 492 / 528)
  11054. CHECK (counters.size() == 4u && counters[2] == 276 && counters[3] == (which == 0 ? 696 : 492) && counters[0] == (which == 0 ? 756 : 528));
  11055. for (const auto& g : which == 0 ? std::vector (std::begin (goldenA), std::end (goldenA)) : std::vector (std::begin (goldenB), std::end (goldenB)))
  11056. CHECK (y[2 * (std::size_t) g.i] == g.l && y[2 * (std::size_t) g.i + 1] == g.r);
  11057. double e = 0.0;
  11058. for (auto v : y)
  11059. e += (double) v * v;
  11060. CHECK (std::abs (std::sqrt (e / (double) y.size()) - (which == 0 ? 0.290665331870 : 0.992116840643)) < 1e-9);
  11061. }
  11062. // the instrument: same engine behind LYNX's channel gains (V = 1, centre → Final.Vol 1, Final.Pan 0)
  11063. lynx::ChannelDef c;
  11064. c.instrument = lynx::InstrumentKind::simsynth;
  11065. c.pluginState = blobA;
  11066. auto inst = lynx::makeInstrument (lynx::InstrumentKind::simsynth);
  11067. inst->prepare (44100.0);
  11068. inst->configure (c, 120.0);
  11069. std::vector<float> l (8192, 0.0f), r (8192, 0.0f);
  11070. for (int k = 0; k < 16; ++k)
  11071. {
  11072. if (k == 0)
  11073. inst->noteOn (60, 0.8f);
  11074. if (k == 10)
  11075. inst->noteOff (60);
  11076. inst->render (l.data() + 512 * k, r.data() + 512 * k, 512, 1.0f, 1.0f);
  11077. }
  11078. const auto ref = lynx::simsynth::renderReference (blobA, { { 0, 60, 1.0f, 0, 0, 0.0f, 0.0f, 5120 } }, 44100.0, 8192);
  11079. double diff = 0.0, energy = 0.0;
  11080. for (int i = 0; i < 8192; ++i)
  11081. {
  11082. 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]) });
  11083. energy += (double) l[(std::size_t) i] * l[(std::size_t) i];
  11084. }
  11085. CHECK (diff == 0.0 && energy > 1e-3);
  11086. // the installation's SimSynth presets (when the FL data folder is given): every state usable and sounding
  11087. if (! realFlpDir.empty())
  11088. {
  11089. const auto folder = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "SimSynth";
  11090. int presets = 0, usable = 0, sounding = 0;
  11091. if (std::filesystem::exists (folder))
  11092. for (const auto& entry : std::filesystem::directory_iterator (folder))
  11093. for (const auto& st : pluginStatesIn (entry.path(), "SimSynth"))
  11094. {
  11095. ++presets;
  11096. if (! lynx::simSynthStateUsable (st))
  11097. continue;
  11098. ++usable;
  11099. const auto y = lynx::simsynth::renderReference (st, { { 0, 48, 1.0f, 0, 0, 0.0f, 0.0f, 11025 } }, 44100.0, 22050);
  11100. double e = 0.0;
  11101. bool finite = true;
  11102. for (auto v : y)
  11103. finite = finite && std::isfinite (v), e += (double) v * v;
  11104. CHECK (finite);
  11105. sounding += e > 1e-9 ? 1 : 0;
  11106. }
  11107. std::printf (" SimSynth presets: %d, %d usable, %d sounding\n", presets, usable, sounding);
  11108. CHECK (presets == 0 || (usable == presets && presets >= 70 && sounding >= presets - 2));
  11109. }
  11110. }
  11111. TEST (wasp_is_a_silent_placeholder)
  11112. {
  11113. // FL 2026's Wasp / Wasp XT DLL keeps the state and plays nothing: so does LYNX (REA_rodada64 §5)
  11114. auto inst = lynx::makeInstrument (lynx::InstrumentKind::wasp);
  11115. inst->prepare (44100.0);
  11116. std::vector<float> l (1024, 0.0f), r (1024, 0.0f);
  11117. inst->noteOn (60, 1.0f);
  11118. inst->render (l.data(), r.data(), 1024, 1.0f, 1.0f);
  11119. CHECK (inst->idle() && std::all_of (l.begin(), l.end(), [] (float v) { return v == 0.0f; }));
  11120. // import: a SimSynth channel plays on the exact engine, Wasp and Wasp XT channels become the silent placeholder with
  11121. // their state kept
  11122. std::vector<std::uint8_t> ss (564, 0), wasp (169, 1);
  11123. const int version = 14;
  11124. std::memcpy (ss.data() + 560, &version, 4);
  11125. FlpBuilder b;
  11126. b.ascii (0xC7, "25.2.5.5319");
  11127. int index = 0;
  11128. for (const auto& [name, state] : { std::pair<std::u16string, std::vector<std::uint8_t>> { u"SimSynth", ss }, { u"Wasp", wasp }, { u"Wasp XT", wasp } })
  11129. {
  11130. b.u8 (0x40); b.u16 ((std::uint16_t) index++);
  11131. b.u8 (0x15); b.u8 (2);
  11132. b.utf16 (0xC9, name);
  11133. b.data (0xD5, state);
  11134. }
  11135. const auto song = lynx::importFlp (flp::parseProject (b.bytes));
  11136. CHECK (song.channels.size() == 3u);
  11137. if (song.channels.size() == 3u)
  11138. {
  11139. CHECK (song.channels[0].instrument == lynx::InstrumentKind::simsynth && song.channels[0].pluginState == ss);
  11140. CHECK (song.channels[1].instrument == lynx::InstrumentKind::wasp && song.channels[1].pluginState == wasp);
  11141. CHECK (song.channels[2].instrument == lynx::InstrumentKind::wasp);
  11142. }
  11143. }
  11144. #include "WaveAnalysisTests.inc"
  11145. #include "UiPolish2Tests.inc"
  11146. #include "LuxeVerbTests.inc"
  11147. #include "ConvolverTests.inc"