MixSuite.cpp 196 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245
  1. #include "core/MixSuite.h"
  2. #include <algorithm>
  3. #include <atomic>
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include "core/Biquad.h"
  8. #include "core/DynamicsSuite.h"
  9. #include "core/Effects.h"
  10. #include "core/EmberClassic.h"
  11. #include "core/LrCrossover.h"
  12. #include "core/ParametricEqLinear.h"
  13. #include "core/StrikeShaper.h"
  14. #include "core/AnvilBus.h"
  15. #include "core/VoiceMatch.h"
  16. #include "core/VoxStrip.h"
  17. // REA_rodada96. Every formula below is textbook material, cited where it matters:
  18. // - biquads: R. Bristow-Johnson, "Cookbook formulae for audio EQ biquad filter coefficients";
  19. // - half-band FIR oversampling with a Kaiser window: Oppenheim & Schafer, "Discrete-Time Signal Processing", 7.6
  20. // and 4.7 (multirate), Kaiser's design formula for beta;
  21. // - compressor / gate gain computer with a soft knee and the "smooth branching" peak detector in the log domain:
  22. // D. Giannoulis, M. Massberg, J. D. Reiss, "Digital Dynamic Range Compressor Design — A Tutorial and Analysis",
  23. // JAES 60(6), 2012;
  24. // - the electro-optical cell (an electroluminescent panel lighting a photoresistor in a feedback leveler): the
  25. // published behaviour of such cells — about 10 ms attack, a release in two stages (roughly half the reduction
  26. // within ~60 ms, the rest over 0.5 to several seconds, longer after long or heavy reduction) — as described in
  27. // the public service literature and circuit analyses of opto levelers; the model (a fast and a slow conductance
  28. // and a memory that lengthens the slow release) is our own;
  29. // - waveshaping and its harmonics: U. Zölzer (ed.), "DAFX: Digital Audio Effects", 2nd ed., ch. 4 (nonlinear
  30. // processing: tube / tape / distortion curves, exciters and enhancers), and J. Pakarinen, D. T. Yeh, "A Review of
  31. // Digital Techniques for Modeling Vacuum-Tube Guitar Amplifiers", CMJ 33(2), 2009 (asymmetric curves → even
  32. // harmonics).
  33. namespace lynx::mixsuite
  34. {
  35. namespace
  36. {
  37. constexpr double pi = 3.141592653589793;
  38. inline double dbToGain (double db) noexcept { return std::pow (10.0, db / 20.0); }
  39. inline double gainToDb (double g) noexcept { return 20.0 * std::log10 (std::max (g, 1e-12)); }
  40. inline double logMap (float v, double lo, double hi) noexcept { return lo * std::pow (hi / lo, (double) std::clamp (v, 0.0f, 1.0f)); }
  41. inline double lin (float v, double lo, double hi) noexcept { return lo + (hi - lo) * (double) std::clamp (v, 0.0f, 1.0f); }
  42. inline bool on (float v) noexcept { return v >= 0.5f; }
  43. // one-pole smoothing coefficient for a time constant in seconds
  44. inline double coef (double seconds, double sr) noexcept { return seconds <= 0.0 ? 0.0 : std::exp (-1.0 / (seconds * sr)); }
  45. // |H(e^jw)| of a biquad
  46. double biquadMag (const Biquad& b, double w)
  47. {
  48. const double c1 = std::cos (w), s1 = std::sin (w), c2 = std::cos (2 * w), s2 = std::sin (2 * w);
  49. const double nr = b.b0 + b.b1 * c1 + b.b2 * c2, ni = -(b.b1 * s1 + b.b2 * s2);
  50. const double dr = 1.0 + b.a1 * c1 + b.a2 * c2, di = -(b.a1 * s1 + b.a2 * s2);
  51. return std::sqrt ((nr * nr + ni * ni) / std::max (dr * dr + di * di, 1e-300));
  52. }
  53. // first-order high-pass (bilinear), stereo
  54. struct OnePoleHp
  55. {
  56. double b0 = 1, b1 = 0, a1 = 0;
  57. double x1[2] {}, y1[2] {};
  58. void set (double sr, double hz) noexcept
  59. {
  60. const auto k = std::tan (pi * std::min (hz, sr * 0.45) / sr);
  61. b0 = 1.0 / (1.0 + k);
  62. b1 = -b0;
  63. a1 = (k - 1.0) / (k + 1.0);
  64. }
  65. void reset() noexcept { x1[0] = x1[1] = y1[0] = y1[1] = 0.0; }
  66. float process (float x, int ch) noexcept
  67. {
  68. const auto y = b0 * x + b1 * x1[ch] - a1 * y1[ch];
  69. x1[ch] = x;
  70. y1[ch] = y;
  71. return (float) y;
  72. }
  73. double mag (double w) const
  74. {
  75. const double nr = b0 + b1 * std::cos (w), ni = -b1 * std::sin (w);
  76. const double dr = 1.0 + a1 * std::cos (w), di = -a1 * std::sin (w);
  77. return std::sqrt ((nr * nr + ni * ni) / (dr * dr + di * di));
  78. }
  79. };
  80. // Stereo delay line of fixed capacity (no allocation).
  81. template <int N>
  82. struct Delay2
  83. {
  84. std::array<float, N> l {}, r {};
  85. int pos = 0, len = 0;
  86. void setLength (int n) noexcept { len = std::clamp (n, 0, N - 1); }
  87. void reset() noexcept { l.fill (0.0f); r.fill (0.0f); pos = 0; }
  88. void process (float& a, float& b) noexcept
  89. {
  90. if (len == 0)
  91. return;
  92. l[(std::size_t) pos] = a;
  93. r[(std::size_t) pos] = b;
  94. auto rd = pos - len;
  95. if (rd < 0)
  96. rd += N;
  97. a = l[(std::size_t) rd];
  98. b = r[(std::size_t) rd];
  99. if (++pos == N)
  100. pos = 0;
  101. }
  102. };
  103. struct Meters
  104. {
  105. std::atomic<float> gr0 { 0.0f }, gr1 { 0.0f }, in { 0.0f }, out { 0.0f };
  106. float read (int k) const noexcept
  107. {
  108. switch (k)
  109. {
  110. case 0: return gr0.load (std::memory_order_relaxed);
  111. case 1: return gr1.load (std::memory_order_relaxed);
  112. case 2: return in.load (std::memory_order_relaxed);
  113. case 3: return out.load (std::memory_order_relaxed);
  114. default: return 0.0f;
  115. }
  116. }
  117. static float peak (const float* l, const float* r, int n) noexcept
  118. {
  119. float p = 0.0f;
  120. for (int i = 0; i < n; ++i)
  121. p = std::max (p, std::max (std::abs (l[i]), std::abs (r[i])));
  122. return std::min (p, 1.0f);
  123. }
  124. void set (float grDb0, float grDb1, float inPeak, float outPeak) noexcept
  125. {
  126. gr0.store (std::clamp (grDb0 / 30.0f, 0.0f, 1.0f), std::memory_order_relaxed);
  127. gr1.store (std::clamp (grDb1 / 30.0f, 0.0f, 1.0f), std::memory_order_relaxed);
  128. in.store (inPeak, std::memory_order_relaxed);
  129. out.store (outPeak, std::memory_order_relaxed);
  130. }
  131. // round 147: a level for the second meter slot when an engine has no second reduction (Hush: the detector's
  132. // energy, −60 … 0 dB as 0 … 1)
  133. void setDetector (float db) noexcept { gr1.store (std::clamp ((db + 60.0f) / 60.0f, 0.0f, 1.0f), std::memory_order_relaxed); }
  134. };
  135. // ---- oversampler design ----
  136. constexpr int stageTaps[Oversampler::maxStages] = { 95, 23, 15 }; // N = 4k + 3: the centre index c = (N − 1) / 2 is odd
  137. constexpr double stageBeta[Oversampler::maxStages] = { 10.0, 9.0, 8.5 };
  138. double besselI0 (double x)
  139. {
  140. double sum = 1.0, term = 1.0;
  141. for (int k = 1; k < 50; ++k)
  142. {
  143. term *= (x / (2.0 * k)) * (x / (2.0 * k));
  144. sum += term;
  145. if (term < 1e-12 * sum)
  146. break;
  147. }
  148. return sum;
  149. }
  150. // latency of a factor's path in units of the highest rate, before the pad
  151. int rawUnits (int stages)
  152. {
  153. const int f = 1 << stages;
  154. int u = 0;
  155. for (int s = 0; s < stages; ++s)
  156. u += ((stageTaps[s] - 1) / 2) * f / (1 << s);
  157. return u;
  158. }
  159. int pathLatency (int stages)
  160. {
  161. const int f = 1 << stages;
  162. const int u = rawUnits (stages);
  163. return (u + (f - u % f) % f) / f;
  164. }
  165. } // namespace
  166. // ================================================================== Oversampler
  167. Oversampler::Oversampler()
  168. {
  169. for (int s = 0; s < maxStages; ++s)
  170. {
  171. auto& stage = st[(std::size_t) s];
  172. const int n = stageTaps[s];
  173. const int c = (n - 1) / 2;
  174. const auto i0b = besselI0 (stageBeta[s]);
  175. double sum = 0.0;
  176. std::array<double, 64> h {};
  177. for (int j = 0; 2 * j < n; ++j)
  178. {
  179. const int t = 2 * j - c; // odd offset from the centre
  180. const auto r = (double) (2 * j - c) / c;
  181. const auto w = besselI0 (stageBeta[s] * std::sqrt (std::max (0.0, 1.0 - r * r))) / i0b;
  182. h[(std::size_t) j] = std::sin (0.5 * pi * t) / (0.5 * pi * t) * w; // sinc(t/2), scaled below
  183. sum += h[(std::size_t) j];
  184. }
  185. stage.taps = (n + 1) / 2;
  186. for (int j = 0; j < stage.taps; ++j)
  187. stage.coef[(std::size_t) j] = (float) (h[(std::size_t) j] * 0.5 / sum); // the side taps sum to 0.5
  188. stage.centreDelay = (c - 1) / 2;
  189. }
  190. setFactor (4);
  191. }
  192. int Oversampler::latency() noexcept { return pathLatency (maxStages); }
  193. void Oversampler::setFactor (int factor) noexcept
  194. {
  195. const int s = factor >= 8 ? 3 : (factor >= 4 ? 2 : 1);
  196. if (s == stages && (hiPadLen != 0 || loPadLen != 0 || s == maxStages))
  197. return;
  198. stages = s;
  199. const int f = 1 << s;
  200. const int u = rawUnits (s);
  201. hiPadLen = (f - u % f) % f;
  202. loPadLen = latency() - pathLatency (s);
  203. reset();
  204. }
  205. void Oversampler::reset() noexcept
  206. {
  207. for (auto& s : st)
  208. {
  209. s.upHist.fill (0.0f);
  210. s.evenHist.fill (0.0f);
  211. s.oddHist.fill (0.0f);
  212. s.upPos = s.downPos = 0;
  213. }
  214. hiPad.fill (0.0f);
  215. loPad.fill (0.0f);
  216. hiPadPos = loPadPos = 0;
  217. }
  218. void Oversampler::up (Stage& s, float x, float& a, float& b) noexcept
  219. {
  220. const int L = s.taps;
  221. s.upPos = s.upPos == 0 ? L - 1 : s.upPos - 1;
  222. s.upHist[(std::size_t) s.upPos] = s.upHist[(std::size_t) (s.upPos + L)] = x;
  223. const float* hist = s.upHist.data() + s.upPos; // hist[j] = x[m − j]
  224. float acc = 0.0f;
  225. for (int j = 0; j < L; ++j)
  226. acc += s.coef[(std::size_t) j] * hist[j];
  227. a = 2.0f * acc; // y[2m]
  228. b = hist[s.centreDelay]; // y[2m + 1] = x[m − (c − 1) / 2]
  229. }
  230. float Oversampler::down (Stage& s, float a, float b) noexcept
  231. {
  232. const int L = s.taps;
  233. s.downPos = s.downPos == 0 ? L - 1 : s.downPos - 1;
  234. s.evenHist[(std::size_t) s.downPos] = s.evenHist[(std::size_t) (s.downPos + L)] = a;
  235. s.oddHist[(std::size_t) s.downPos] = s.oddHist[(std::size_t) (s.downPos + L)] = b;
  236. const float* even = s.evenHist.data() + s.downPos;
  237. float acc = 0.0f;
  238. for (int j = 0; j < L; ++j)
  239. acc += s.coef[(std::size_t) j] * even[j];
  240. return acc + 0.5f * s.oddHist[(std::size_t) (s.downPos + s.centreDelay + 1)]; // v[2m − c]
  241. }
  242. float Oversampler::padIn (float x) noexcept
  243. {
  244. if (hiPadLen == 0)
  245. return x;
  246. const auto y = hiPad[(std::size_t) hiPadPos];
  247. hiPad[(std::size_t) hiPadPos] = x;
  248. hiPadPos = (hiPadPos + 1) % hiPadLen;
  249. return y;
  250. }
  251. float Oversampler::padOut (float x) noexcept
  252. {
  253. if (loPadLen == 0)
  254. return x;
  255. const auto y = loPad[(std::size_t) loPadPos];
  256. loPad[(std::size_t) loPadPos] = x;
  257. loPadPos = (loPadPos + 1) % loPadLen;
  258. return y;
  259. }
  260. // ================================================================== Ridge Strip
  261. namespace ridge
  262. {
  263. double hpfHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 16.0, 350.0); }
  264. double lpfHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 22000.0, 3000.0); }
  265. double bandGainDb (float v, bool black) noexcept { return (2.0 * std::clamp (v, 0.0f, 1.0f) - 1.0) * (black ? 18.0 : 15.0); }
  266. double lfHz (float v) noexcept { return logMap (v, 30.0, 450.0); }
  267. double lmfHz (float v) noexcept { return logMap (v, 200.0, 2500.0); }
  268. double hmfHz (float v) noexcept { return logMap (v, 600.0, 7000.0); }
  269. double hfHz (float v) noexcept { return logMap (v, 1500.0, 16000.0); }
  270. double bandQ (float v) noexcept { return logMap (v, 0.5, 2.5); }
  271. double ratio (float v) noexcept { return v >= 0.999f ? 1e9 : 1.0 + 19.0 * std::pow ((double) std::clamp (v, 0.0f, 1.0f), 2.0); }
  272. double compThresholdDb (float v) noexcept { return lin (v, -40.0, 0.0); }
  273. double releaseSeconds (float v) noexcept { return logMap (v, 0.1, 4.0); }
  274. double gateRangeDb (float v) noexcept { return lin (v, 0.0, 60.0); }
  275. double gateThresholdDb (float v) noexcept { return lin (v, -80.0, 0.0); }
  276. double trimDb (float v) noexcept { return lin (v, -20.0, 20.0); }
  277. double eQ (float v) noexcept { return vmatch::echannel::qFromDial (1.0 - (double) std::clamp (v, 0.0f, 1.0f)); }
  278. double eCompThresholdDb (float v) noexcept { return lin (v, -20.0, 10.0); }
  279. double eGateRangeDb (float v) noexcept { return lin (v, 0.0, 40.0); }
  280. double eGateThresholdDb (float v) noexcept { return lin (v, -30.0, 10.0); }
  281. bool eVoicing (const EffectParams& p) noexcept { return on (p[voicing]); }
  282. bool vocalVoicing (const EffectParams& p) noexcept { return on (p[vocal]); }
  283. bool k4Voicing (const EffectParams& p) noexcept { return on (p[console4k]); }
  284. double k4Q (float v) noexcept { return logMap (v, 0.5, 3.0); }
  285. float k4QValue (double q) noexcept { return (float) std::clamp (std::log (std::clamp (q, 0.5, 3.0) / 0.5) / std::log (6.0), 0.0, 1.0); }
  286. int k4ColourIndex (float v) noexcept { return std::clamp ((int) std::lround (v * 2.0f), 0, 2); }
  287. float k4ColourValue (int colour) noexcept { return (float) std::clamp (colour, 0, 2) * 0.5f; }
  288. double k4MicDb (float v) noexcept { return lin (v, 0.0, 50.0); }
  289. double k4OffsetDb (float v) noexcept { return lin (v, -12.0, 12.0); }
  290. // round tp172c: the Modern voicing's scales (the reference's ranges; its ratio dial through its own read-outs)
  291. bool modernVoicing (const EffectParams& p) noexcept { return on (p[modern]); }
  292. double mHpfHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 20.9, 500.0); }
  293. double mLpfKHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 27.5, 3.0); }
  294. double mLfHz (float v) noexcept { return logMap (v, 40.0, 600.0); }
  295. double mLmfKHz (float v) noexcept { return logMap (v, 0.2, 2.0); }
  296. double mHmfKHz (float v) noexcept { return logMap (v, 0.6, 7.0); }
  297. double mHfKHz (float v) noexcept { return logMap (v, 1.5, 22.0); }
  298. double mLfGainDb (float v) noexcept { return lin (v, -16.5, 16.5); }
  299. double mGainDb (float v) noexcept { return lin (v, -20.0, 20.0); }
  300. double mQ (float v) noexcept { return lin (v, 0.5, 2.5); }
  301. double mHoldSeconds (float v) noexcept { return lin (v, 0.0, 4.0); }
  302. namespace
  303. {
  304. // the reference's ratio dial (its read-outs at eighths of the travel); 63 .. infinity on the last eighth
  305. constexpr double kModernRatio[8] { 1.0, 1.1, 1.8, 4.3, 10.0, 21.0, 38.0, 63.0 };
  306. }
  307. double mRatio (float v) noexcept
  308. {
  309. const double x = std::clamp ((double) v, 0.0, 1.0) * 8.0;
  310. if (v >= 0.999f)
  311. return 1e9;
  312. if (x >= 7.0) // 63 : 1 to "infinity" (2000 : 1 just under the top)
  313. return 63.0 * std::pow (2000.0 / 63.0, (x - 7.0) / (7.992 - 7.0));
  314. const int i = (int) x;
  315. return kModernRatio[i] * std::pow (kModernRatio[i + 1] / kModernRatio[i], x - i);
  316. }
  317. float mRatioValue (double ratio) noexcept
  318. {
  319. if (ratio <= 1.0) return 0.0f;
  320. if (ratio >= 99.5) return 1.0f; // [EXEC] the reference stores its "INF" as 100
  321. if (ratio >= 63.0)
  322. return (float) std::min (0.998, (7.0 + std::log (ratio / 63.0) / std::log (2000.0 / 63.0) * 0.992) / 8.0);
  323. int i = 0;
  324. while (i < 6 && ratio > kModernRatio[i + 1]) ++i;
  325. return (float) ((i + std::log (ratio / kModernRatio[i]) / std::log (kModernRatio[i + 1] / kModernRatio[i])) / 8.0);
  326. }
  327. double autoMakeupDb (const EffectParams& p) noexcept
  328. {
  329. if (p[compRatio] <= 0.0005f || ! on (p[dynOn]))
  330. return 0.0;
  331. vmatch::echannel::Settings e;
  332. e.compRatio = ratio (p[compRatio]);
  333. e.compThreshold = std::clamp (compThresholdDb (p[compThreshold]) + 18.0, -20.0, 10.0);
  334. return vmatch::echannel::compMakeupDb (e);
  335. }
  336. // round 145: the Vocal strip voicing's settings (the reference's own laws, core/VoxStrip.h)
  337. voxstrip::Settings vocalSettings (const EffectParams& p) noexcept
  338. {
  339. namespace vs = voxstrip;
  340. vs::Settings s;
  341. s.inputDb = vs::gainDb (p[vInput]);
  342. s.outputDb = vs::gainDb (p[vOutput]);
  343. s.order = vs::orderOf (p[vOrder]);
  344. s.dsOn = on (p[vDsOn]); s.dsSplit = on (p[vDsSplit]); s.dsLookahead = on (p[vDsLookahead]); s.dsListen = on (p[vDsListen]);
  345. s.dsHz = vs::dsFreqHz (p[vDsFreq]); s.dsThresholdDb = vs::thresholdDb (p[vDsThreshold]);
  346. s.dpOn = on (p[vDpOn]); s.dpSplit = on (p[vDpSplit]); s.dpLookahead = on (p[vDpLookahead]); s.dpListen = on (p[vDpListen]);
  347. s.dpHz = vs::dpFreqHz (p[vDpFreq]); s.dpThresholdDb = vs::thresholdDb (p[vDpThreshold]);
  348. s.eqOn = on (p[vEqOn]); s.hpfOn = on (p[vHpfOn]); s.notchOn = on (p[vNotchOn]); s.highOn = on (p[vHighOn]);
  349. s.hpfHz = vs::hpfHz (p[vHpfFreq]); s.notchHz = vs::notchHz (p[vNotchFreq]); s.notchDb = vs::notchGainDb (p[vNotchGain]);
  350. s.highHz = vs::highHz (p[vHighFreq]); s.highDb = vs::highGainDb (p[vHighGain]);
  351. s.compOn = on (p[vCompOn]); s.softKnee = on (p[vKnee]); s.drive = on (p[vDrive]);
  352. s.expThresholdDb = vs::expThresholdDb (p[vExpThreshold]); s.compThresholdDb = vs::thresholdDb (p[vCompThreshold]);
  353. s.ratio = vs::ratio (p[vRatio]); s.attackMs = vs::attackMs (p[vAttack]); s.releaseMs = vs::releaseMs (p[vRelease]);
  354. s.makeupDb = vs::makeupDb (p[vMakeup]);
  355. return s;
  356. }
  357. namespace
  358. {
  359. constexpr double kneeDb = 6.0;
  360. // Giannoulis et al. 2012, eq. (4): soft-knee gain computer, output level for input level x (dB)
  361. double gainComputer (double x, double threshold, double r)
  362. {
  363. const auto over = x - threshold;
  364. if (2.0 * over < -kneeDb)
  365. return x;
  366. if (2.0 * std::abs (over) <= kneeDb)
  367. return x + (1.0 / r - 1.0) * (over + kneeDb / 2.0) * (over + kneeDb / 2.0) / (2.0 * kneeDb);
  368. return threshold + over / r;
  369. }
  370. // the EQ section's filters (shared by the processor and responseDb)
  371. struct EqDesign
  372. {
  373. OnePoleHp hp1;
  374. Biquad hp2, lp, lf, lmf, hmf, hf;
  375. bool hpOn = false, lpOn = false;
  376. void design (const EffectParams& p, double sr)
  377. {
  378. const bool black = on (p[curve]);
  379. hpOn = p[hpf] > 0.0f;
  380. lpOn = p[lpf] > 0.0f;
  381. if (hpOn)
  382. {
  383. // 3rd-order Butterworth high-pass: a first-order section and a second-order one with Q = 1
  384. hp1.set (sr, hpfHz (p[hpf]));
  385. hp2.filter (sr, 1, hpfHz (p[hpf]), 1.0);
  386. }
  387. if (lpOn)
  388. lp.filter (sr, 0, std::min (lpfHz (p[lpf]), sr * 0.45), 0.70710678);
  389. const auto nyq = sr * 0.49;
  390. // shelves: "brown" curves are plain (S = 1, no overshoot); "black" ones have the resonant shelf with the small
  391. // dip / bump before the shelf that many console EQs show (S > 1)
  392. const auto shelfS = black ? 1.6 : 1.0;
  393. const auto lg = bandGainDb (p[lfGain], black), hg = bandGainDb (p[hfGain], black);
  394. if (on (p[lfBell]))
  395. lf.peaking (sr, lfHz (p[lfFreq]), black ? propQ (0.9, lg) : 0.9, lg);
  396. else
  397. lf.shelf (sr, lfHz (p[lfFreq]), lg, false, shelfS);
  398. if (on (p[hfBell]))
  399. hf.peaking (sr, std::min (hfHz (p[hfFreq]), nyq), black ? propQ (0.9, hg) : 0.9, hg);
  400. else
  401. hf.shelf (sr, std::min (hfHz (p[hfFreq]), nyq), hg, true, shelfS);
  402. const auto mg = bandGainDb (p[lmfGain], black), ng = bandGainDb (p[hmfGain], black);
  403. lmf.peaking (sr, lmfHz (p[lmfFreq]), black ? propQ (bandQ (p[lmfQ]), mg) : bandQ (p[lmfQ]), mg);
  404. hmf.peaking (sr, std::min (hmfHz (p[hmfFreq]), nyq), black ? propQ (bandQ (p[hmfQ]), ng) : bandQ (p[hmfQ]), ng);
  405. }
  406. // "black" bells: proportional Q — wide for small boosts / cuts, narrower as the gain grows
  407. static double propQ (double q, double gainDb) { return std::max (0.2, q * (0.5 + std::abs (gainDb) / 12.0)); }
  408. };
  409. } // namespace
  410. // E voicing: Ridge's controls in the console's units (the shared ranges use Ridge's own laws)
  411. vmatch::echannel::Settings consoleSettingsOf (const EffectParams& p) noexcept
  412. {
  413. vmatch::echannel::Settings e;
  414. e.inputDb = trimDb (p[input]);
  415. e.outputDb = trimDb (p[output]);
  416. e.phase = on (p[phase]);
  417. e.hpHz = p[hpf] > 0.0f ? hpfHz (p[hpf]) : 0.0;
  418. e.lpKHz = p[lpf] > 0.0f ? lpfHz (p[lpf]) / 1000.0 : 0.0;
  419. e.lfGain = bandGainDb (p[lfGain], false);
  420. e.lfHz = lfHz (p[lfFreq]);
  421. e.lfBell = on (p[lfBell]);
  422. e.lmfGain = bandGainDb (p[lmfGain], false);
  423. e.lmfKHz = lmfHz (p[lmfFreq]) / 1000.0;
  424. e.lmfQ = eQ (p[lmfQ]);
  425. e.hmfGain = bandGainDb (p[hmfGain], false);
  426. e.hmfKHz = hmfHz (p[hmfFreq]) / 1000.0;
  427. e.hmfQ = eQ (p[hmfQ]);
  428. e.hfGain = bandGainDb (p[hfGain], false);
  429. e.hfKHz = hfHz (p[hfFreq]) / 1000.0;
  430. e.hfBell = on (p[hfBell]);
  431. e.eqOn = on (p[eqOn]);
  432. e.compRatio = p[compRatio] <= 0.0005f ? 1.0 : ratio (p[compRatio]);
  433. e.compThreshold = eCompThresholdDb (p[compThreshold]);
  434. e.compRelease = releaseSeconds (p[ridge::compRelease]);
  435. e.compFast = on (p[compFastAttack]);
  436. e.expRange = eGateRangeDb (p[gateRange]);
  437. e.expThreshold = eGateThresholdDb (p[gateThreshold]);
  438. e.expRelease = releaseSeconds (p[ridge::gateRelease]);
  439. e.expFast = on (p[gateFastAttack]);
  440. e.gate = ! on (p[expander]);
  441. e.dynOn = on (p[dynOn]);
  442. e.dynamicsAfterEq = ! on (p[order]);
  443. e.filtersBeforeDynamics = on (p[filterSplit]);
  444. e.eqToSidechain = on (p[eqToSidechain]); // round 165
  445. if (k4Voicing (p)) // round 148: the 4K console (its own Q scale, colour, mic pre, VCA, make-up, mix)
  446. {
  447. e.model = 1;
  448. e.colour = k4ColourIndex (p[k4Colour]);
  449. e.lmfQ = k4Q (p[lmfQ]);
  450. e.hmfQ = k4Q (p[hmfQ]);
  451. e.lpKHz = e.lpKHz >= 15.9 ? 0.0 : e.lpKHz;
  452. e.filtersBeforeDynamics = true;
  453. e.pre = on (p[k4Pre]);
  454. e.micDb = k4MicDb (p[k4Mic]);
  455. e.vca = on (p[analog]);
  456. e.autoMakeup = on (p[autoMakeup]);
  457. e.makeupOffsetDb = k4OffsetDb (p[k4MakeupOffset]);
  458. e.compMix = std::clamp ((double) p[compMix], 0.0, 1.0);
  459. }
  460. return e;
  461. }
  462. // round tp172c: the Modern voicing's settings (the shared controls in the reference's own units)
  463. vmatch::strip2::Settings modernSettingsOf (const EffectParams& p) noexcept
  464. {
  465. vmatch::strip2::Settings m;
  466. m.inputDb = trimDb (p[input]);
  467. m.outputDb = trimDb (p[output]);
  468. m.phase = on (p[phase]);
  469. m.hpHz = mHpfHz (p[hpf]);
  470. m.lpKHz = mLpfKHz (p[lpf]);
  471. m.typeG = on (p[curve]);
  472. m.lfGain = mLfGainDb (p[lfGain]);
  473. m.lfHz = mLfHz (p[lfFreq]);
  474. m.lfBell = on (p[lfBell]);
  475. m.lmfGain = mGainDb (p[lmfGain]);
  476. m.lmfKHz = mLmfKHz (p[lmfFreq]);
  477. m.lmfQ = mQ (p[lmfQ]);
  478. m.hmfGain = mGainDb (p[hmfGain]);
  479. m.hmfKHz = mHmfKHz (p[hmfFreq]);
  480. m.hmfQ = mQ (p[hmfQ]);
  481. m.hfGain = mGainDb (p[hfGain]);
  482. m.hfKHz = mHfKHz (p[hfFreq]);
  483. m.hfBell = on (p[hfBell]);
  484. m.eqOn = on (p[eqOn]);
  485. m.compRatio = mRatio (p[compRatio]);
  486. m.compThreshold = eCompThresholdDb (p[compThreshold]);
  487. m.compRelease = releaseSeconds (p[ridge::compRelease]);
  488. m.compFast = on (p[compFastAttack]);
  489. m.compPeak = on (p[compPeak]);
  490. m.compMix = std::clamp ((double) p[compMix], 0.0, 1.0);
  491. m.autoMakeup = on (p[autoMakeup]);
  492. m.makeupOffsetDb = k4OffsetDb (p[k4MakeupOffset]);
  493. m.expander = on (p[expander]);
  494. m.gateFast = on (p[gateFastAttack]);
  495. m.gateThreshold = eGateThresholdDb (p[gateThreshold]);
  496. m.gateRange = eGateRangeDb (p[gateRange]);
  497. m.gateRelease = releaseSeconds (p[ridge::gateRelease]);
  498. m.gateHold = mHoldSeconds (p[gateHold]);
  499. m.dynOn = on (p[dynOn]);
  500. m.dynamicsPreEq = on (p[order]);
  501. return m;
  502. }
  503. double responseDb (const EffectParams& p, double sampleRate, double hz)
  504. {
  505. if (modernVoicing (p)) // round tp172c
  506. return vmatch::strip2::responseDb (modernSettingsOf (p), sampleRate, hz);
  507. if (k4Voicing (p) || eVoicing (p)) // round 148: the console's own curve
  508. return vmatch::echannel::responseDb (consoleSettingsOf (p), sampleRate, hz);
  509. EqDesign d;
  510. d.design (p, sampleRate);
  511. const auto w = 2.0 * pi * hz / sampleRate;
  512. double m = 1.0;
  513. if (d.hpOn)
  514. m *= d.hp1.mag (w) * biquadMag (d.hp2, w);
  515. if (d.lpOn)
  516. m *= biquadMag (d.lp, w);
  517. if (on (p[eqOn]))
  518. m *= biquadMag (d.lf, w) * biquadMag (d.lmf, w) * biquadMag (d.hmf, w) * biquadMag (d.hf, w);
  519. return gainToDb (m);
  520. }
  521. double compGrCapDb (float v) noexcept { return v > 1e-6f ? 60.0 * std::min (1.0f, v) : 1e9; }
  522. double compAttackFineSeconds (float v) noexcept { return 0.25 * std::clamp (v, 0.0f, 1.0f); }
  523. double compressorCurveDb (const EffectParams& p, double inputDb)
  524. {
  525. if (modernVoicing (p)) // round tp172c: the Modern voicing's measured law
  526. return vmatch::strip2::compStaticDb (modernSettingsOf (p), inputDb);
  527. const auto out = gainComputer (inputDb, compThresholdDb (p[compThreshold]), ratio (p[compRatio]));
  528. return std::max (out, inputDb - compGrCapDb (p[compGrCap]));
  529. }
  530. // ---- round tp172c6: the DRIVE section ----
  531. // Each model is a static curve per drive position, fitted black-box on the trials' stairs (a 1 kHz sine, -51 .. -9 dBFS
  532. // RMS: fundamental, H2, H3, H5; tp172c data), and its linear tone (the impulse renders): x -> o * T f(x / T) with
  533. // f(u) = u / (1 + |u|^p)^(1/p) (the negative half clips a dB later), the negative half scaled by k (a level-independent
  534. // H2), + c2 y^2 (a level-dependent H2); then a low bell and a high shelf (and the amps' cabinet low-pass). The positions
  535. // between the measured ones interpolate the fitted values. Saturate's curve moves with its dial (-24 .. +24 dB into
  536. // the clipper, the level compensated: +0.9 dB below it, as measured).
  537. namespace
  538. {
  539. struct DriveNode
  540. {
  541. float tDb, p, k, oDb, aDb, c2, bellDb, shelfDb;
  542. };
  543. struct DriveModelDef
  544. {
  545. double lo, hi; // the dial's range in the model's units
  546. int nodes; // measured positions, equally spaced over the dial
  547. const DriveNode* node;
  548. double bellHz, bellQ, shelfHz, lpHz, lpQ;
  549. int lpSections;
  550. double hpHz, hpQ, bell2Hz, bell2Q, bell2Db;
  551. double apHz; // first-order all-pass (0 = none)
  552. // round tp172c9: the parts of the tone that sit before the curve (the amps': their sweeps compress the highs less /
  553. // more than the 1 kHz stair at drive): preMask = the elements run ahead of it (1 bell, 2 shelf, 8 high-pass,
  554. // 16 second bell); preLpHz / preLpQ = a low-pass ahead of it whose inverse the cabinet low-pass takes after it
  555. // (that one becomes LP(lpHz) / LP(preLpHz): the same linear tone); preShelfDb = a pre-emphasis shelf. Both halves
  556. // stay 0 dB at 1 kHz.
  557. int preMask;
  558. double preLpHz, preLpQ;
  559. double preShelfDb; // a high shelf at shelfHz ahead of the curve (the node's own shelf stays after it)
  560. };
  561. #include "core/RidgeDriveTables.inc"
  562. DriveNode driveNodeAt (int model, float v) noexcept
  563. {
  564. const auto& m = kDriveModels[std::clamp (model, 0, (int) driveModels - 1)];
  565. if (model == driveSaturate)
  566. {
  567. auto n = m.node[0];
  568. n.tDb = (float) (n.tDb - lin (v, m.lo, m.hi));
  569. return n;
  570. }
  571. const auto x = (double) std::clamp (v, 0.0f, 1.0f) * (m.nodes - 1);
  572. const auto i = std::min ((int) x, m.nodes - 2);
  573. const auto t = (float) (x - i);
  574. const auto& a = m.node[i];
  575. const auto& b = m.node[i + 1];
  576. auto mix = [t] (float u, float w) { return u + (w - u) * t; };
  577. return { mix (a.tDb, b.tDb), mix (a.p, b.p), mix (a.k, b.k), mix (a.oDb, b.oDb), mix (a.aDb, b.aDb), mix (a.c2, b.c2),
  578. mix (a.bellDb, b.bellDb), mix (a.shelfDb, b.shelfDb) };
  579. }
  580. struct DriveShape
  581. {
  582. double t = 1, p = 2, k = 1, o = 1, r = 1, c2 = 0, ip = 0.5;
  583. void set (const DriveNode& n) noexcept
  584. {
  585. t = std::pow (10.0, n.tDb / 20.0);
  586. p = std::max (0.3, (double) n.p);
  587. ip = 1.0 / p;
  588. k = n.k;
  589. o = std::pow (10.0, n.oDb / 20.0);
  590. r = std::pow (10.0, n.aDb / 20.0);
  591. c2 = n.c2;
  592. }
  593. double operator() (double x) const noexcept
  594. {
  595. const auto tt = x >= 0.0 ? t : t * r;
  596. const auto u = x / tt;
  597. auto y = o * tt * u / std::pow (1.0 + std::pow (std::abs (u), p), ip);
  598. if (y < 0.0)
  599. y *= k;
  600. return y + c2 * y * y;
  601. }
  602. };
  603. } // namespace
  604. int driveModelIndex (float v) noexcept { return std::clamp ((int) std::lround (v * (float) (driveModels - 1)), 0, (int) driveModels - 1); }
  605. float driveModelValue (int model) noexcept { return (float) std::clamp (model, 0, (int) driveModels - 1) / (float) (driveModels - 1); }
  606. const char* driveModelName (int model) noexcept
  607. {
  608. static const char* names[driveModels] { "Saturate", "Console E", "Console G", "Console A", "Console N", "Console Y", "Tube",
  609. "Guitar amp", "Bass amp" };
  610. return names[std::clamp (model, 0, (int) driveModels - 1)];
  611. }
  612. double driveAmountUnits (int model, float v) noexcept
  613. {
  614. const auto& m = kDriveModels[std::clamp (model, 0, (int) driveModels - 1)];
  615. return lin (v, m.lo, m.hi);
  616. }
  617. float driveAmountValue (int model, double units) noexcept
  618. {
  619. const auto& m = kDriveModels[std::clamp (model, 0, (int) driveModels - 1)];
  620. return (float) std::clamp ((units - m.lo) / (m.hi - m.lo), 0.0, 1.0);
  621. }
  622. double driveInputDb (float v) noexcept { return lin (v, -24.0, 24.0); }
  623. double driveTrimDb (float v) noexcept { return lin (v, -24.0, 24.0); }
  624. double driveCurve (const EffectParams& p, double x) noexcept
  625. {
  626. DriveShape s;
  627. const auto model = driveModelIndex (p[driveModel]);
  628. s.set (driveNodeAt (model, p[driveAmount]));
  629. return s (x * dbToGain (driveInputDb (p[driveInput]))) * dbToGain (driveTrimDb (p[driveTrim]));
  630. }
  631. namespace
  632. {
  633. // the stage: input gain -> curve (4x oversampled, a lookup table rebuilt when the settings move) -> 2 Hz DC blocker ->
  634. // tone -> trim, mixed with the dry signal (delayed by the oversampler's latency)
  635. class DriveStage
  636. {
  637. public:
  638. static int latency() noexcept { return Oversampler::latency(); }
  639. void prepare (double rate) noexcept
  640. {
  641. sr = rate;
  642. dcCoef = std::exp (-2.0 * 3.141592653589793 * 2.0 / sr);
  643. for (auto& o : os)
  644. o.setFactor (4);
  645. reset();
  646. }
  647. void reset() noexcept
  648. {
  649. for (auto& o : os)
  650. o.reset();
  651. for (auto* b : { &bell, &bell2, &shelf, &lp, &lp2, &hp, &ap, &preLp, &emphShelf })
  652. b->reset();
  653. dc[0] = dc[1] = dcIn[0] = dcIn[1] = 0.0;
  654. for (auto& ring : dry)
  655. ring.fill (0.0f);
  656. dryPos = 0;
  657. built = false;
  658. }
  659. void process (float* l, float* r, int n, const EffectParams& p) noexcept
  660. {
  661. process (l, r, n, driveModelIndex (p[driveModel]), p[driveAmount], (float) dbToGain (driveInputDb (p[driveInput])),
  662. (float) dbToGain (driveTrimDb (p[driveTrim])), std::clamp (p[driveMix], 0.0f, 1.0f));
  663. }
  664. // round tp172c7: the same with explicit settings (Kiln Channel drives the models with its own controls)
  665. void process (float* l, float* r, int n, int model, float amount, float inGain, float trim, float mix) noexcept
  666. {
  667. if (! built || model != lastModel || std::abs (amount - lastAmount) > 2e-4f)
  668. rebuild (model, amount);
  669. for (int start = 0; start < n; start += chunk)
  670. run (l + start, r + start, std::min (chunk, n - start), inGain, trim, mix);
  671. }
  672. private:
  673. void rebuild (int model, float amount) noexcept
  674. {
  675. lastModel = model;
  676. lastAmount = amount;
  677. built = true;
  678. const auto node = driveNodeAt (model, amount);
  679. DriveShape s;
  680. s.set (node);
  681. for (int i = 0; i < tableSize; ++i)
  682. table[(std::size_t) i] = (float) s (-tableRange + 2.0 * tableRange * i / (tableSize - 1));
  683. shape = s;
  684. const auto& m = kDriveModels[(std::size_t) model];
  685. bell.peaking (sr, std::min (m.bellHz, sr * 0.45), m.bellQ, node.bellDb);
  686. shelf.shelf (sr, std::min (m.shelfHz, sr * 0.45), node.shelfDb, true);
  687. bell2On = std::abs (m.bell2Db) > 1e-6;
  688. bell2.peaking (sr, std::min (m.bell2Hz, sr * 0.45), m.bell2Q, m.bell2Db);
  689. lpSections = m.lpHz > 0.0 ? m.lpSections : 0;
  690. lp.filter (sr, 0, std::min (m.lpHz > 0.0 ? m.lpHz : 20000.0, sr * 0.45), m.lpQ);
  691. lp2 = lp;
  692. preLpOn = m.preLpHz > 0.0 && lpSections == 1;
  693. if (preLpOn)
  694. {
  695. // LP(lpHz) / LP(preLpHz): both share the (1 + z^-1)^2 numerator, so the quotient is a stable biquad
  696. preLp.filter (sr, 0, std::min (m.preLpHz, sr * 0.45), m.preLpQ);
  697. const auto gain = (lp.b0) / preLp.b0;
  698. lp.b0 = gain;
  699. lp.b1 = gain * preLp.a1;
  700. lp.b2 = gain * preLp.a2;
  701. }
  702. pre = m.preMask;
  703. preShelfOn = std::abs (m.preShelfDb) > 1e-6;
  704. emphShelf.shelf (sr, std::min (m.shelfHz, sr * 0.45), m.preShelfDb, true);
  705. hpOn = m.hpHz > 0.0;
  706. hp.filter (sr, 1, std::max (m.hpHz, 1.0), m.hpQ);
  707. // first-order all-pass (s - a) / (s + a) by the bilinear transform: unit gain, so the tone's 1 kHz norm ignores it
  708. apOn = m.apHz > 0.0;
  709. if (apOn)
  710. {
  711. const auto k = std::tan (3.141592653589793 * std::min (m.apHz, sr * 0.45) / sr);
  712. ap.b0 = (1.0 - k) / (1.0 + k);
  713. ap.b1 = -1.0;
  714. ap.b2 = 0.0;
  715. ap.a1 = (k - 1.0) / (k + 1.0);
  716. ap.a2 = 0.0;
  717. }
  718. // the curve's gain was measured at 1 kHz: the tone (each half) is 0 dB there
  719. const auto w = 2.0 * 3.141592653589793 * 1000.0 / sr;
  720. double g = 1.0, gp = 1.0;
  721. auto part = [&] (const Biquad& b, int bit) { (pre & bit ? gp : g) *= biquadMag (b, w); };
  722. part (bell, preBell);
  723. part (shelf, preShelf);
  724. if (bell2On) part (bell2, preBell2);
  725. for (int k = 0; k < lpSections; ++k) g *= biquadMag (lp, w);
  726. if (preLpOn) gp *= biquadMag (preLp, w);
  727. if (preShelfOn) gp *= biquadMag (emphShelf, w);
  728. if (hpOn) part (hp, preHp);
  729. toneNorm = (float) (1.0 / std::max (1e-6, g));
  730. preNorm = (float) (1.0 / std::max (1e-6, gp));
  731. }
  732. void run (float* l, float* r, int n, float inGain, float trim, float mix) noexcept
  733. {
  734. const auto lat = latency();
  735. float* ch[2] = { l, r };
  736. float dryChunk[2][chunk];
  737. for (int c = 0; c < 2; ++c)
  738. for (int i = 0; i < n; ++i)
  739. {
  740. // the dry signal, delayed by the oversampler's latency (aligned with the driven path)
  741. auto& ring = dry[(std::size_t) c];
  742. ring[(std::size_t) ((dryPos + i) & (ringSize - 1))] = ch[c][i];
  743. dryChunk[c][i] = ring[(std::size_t) ((dryPos + i - lat) & (ringSize - 1))];
  744. ch[c][i] *= inGain;
  745. }
  746. dryPos = (dryPos + n) & (ringSize - 1);
  747. if (pre != 0 || preLpOn || preShelfOn)
  748. {
  749. if (preShelfOn) emphShelf.processBlock (l, r, n);
  750. if (pre & preBell) bell.processBlock (l, r, n);
  751. if (pre & preShelf) shelf.processBlock (l, r, n);
  752. if (bell2On && (pre & preBell2)) bell2.processBlock (l, r, n);
  753. if (preLpOn) preLp.processBlock (l, r, n);
  754. if (hpOn && (pre & preHp)) hp.processBlock (l, r, n);
  755. for (int c = 0; c < 2; ++c)
  756. for (int i = 0; i < n; ++i)
  757. ch[c][i] *= preNorm;
  758. }
  759. for (int c = 0; c < 2; ++c)
  760. os[(std::size_t) c].process (ch[c], n, [this] (float x) noexcept { return lookup (x); });
  761. for (int c = 0; c < 2; ++c)
  762. for (int i = 0; i < n; ++i)
  763. {
  764. // 2 Hz DC blocker (the asymmetric halves and c2 leave an offset): y[n] = x[n] - x[n-1] + R y[n-1]
  765. const double x = ch[c][i];
  766. const auto y = x - dcIn[c] + dcCoef * dc[c];
  767. dcIn[c] = x;
  768. dc[c] = y;
  769. ch[c][i] = (float) y;
  770. }
  771. if (! (pre & preBell))
  772. bell.processBlock (l, r, n);
  773. if (! (pre & preShelf))
  774. shelf.processBlock (l, r, n);
  775. if (bell2On && ! (pre & preBell2))
  776. bell2.processBlock (l, r, n);
  777. if (lpSections > 0)
  778. lp.processBlock (l, r, n);
  779. if (lpSections > 1)
  780. lp2.processBlock (l, r, n);
  781. if (hpOn && ! (pre & preHp))
  782. hp.processBlock (l, r, n);
  783. if (apOn)
  784. ap.processBlock (l, r, n);
  785. const auto wet = trim * toneNorm * mix;
  786. for (int c = 0; c < 2; ++c)
  787. for (int i = 0; i < n; ++i)
  788. ch[c][i] = ch[c][i] * wet + (1.0f - mix) * dryChunk[c][i];
  789. }
  790. float lookup (float x) const noexcept
  791. {
  792. const auto pos = ((double) x + tableRange) * ((tableSize - 1) / (2.0 * tableRange));
  793. if (pos <= 0.0 || pos >= tableSize - 1)
  794. return (float) shape (x); // beyond +-4 (+12 dBFS): the curve itself
  795. const auto i = (int) pos;
  796. const auto t = (float) (pos - i);
  797. return table[(std::size_t) i] + (table[(std::size_t) i + 1] - table[(std::size_t) i]) * t;
  798. }
  799. static constexpr int chunk = 256, ringSize = 1024;
  800. static constexpr int tableSize = 4097;
  801. static constexpr double tableRange = 4.0;
  802. double sr = 44100.0;
  803. std::array<Oversampler, 2> os;
  804. std::array<float, tableSize> table {};
  805. DriveShape shape;
  806. Biquad bell, bell2, shelf, lp, lp2, hp, ap, preLp, emphShelf;
  807. bool bell2On = false, hpOn = false, apOn = false, preLpOn = false, preShelfOn = false, built = false;
  808. int lpSections = 0, pre = 0;
  809. float toneNorm = 1.0f, preNorm = 1.0f;
  810. static constexpr int preBell = 1, preShelf = 2, preHp = 8, preBell2 = 16;
  811. int lastModel = -1;
  812. float lastAmount = -1.0f;
  813. double dc[2] {}, dcIn[2] {}, dcCoef = 0.9997;
  814. std::array<std::array<float, ringSize>, 2> dry {};
  815. int dryPos = 0;
  816. };
  817. } // namespace
  818. namespace
  819. {
  820. // ---- round tp172c7: Kiln Channel — a single console channel (input, console model, drive, output, the console's
  821. // noise) on the DRIVE section's measured console models; the role: Slate Digital's VCC Channel, measured black-box on
  822. // its trial (VCC_bank / VCC_usa / VCC_x / VCC_nr): its Group had no effect on one channel's sound, its noise reduction
  823. // off adds white, uncorrelated noise at a level per console (measured at 9 dB drive)
  824. std::atomic<int> kilnGroupBypassVotes[9] {};
  825. class KilnChannel final : public Effect
  826. {
  827. public:
  828. ~KilnChannel() override { vote (0, false); }
  829. void prepare (double rate) noexcept override
  830. {
  831. drive.prepare (rate);
  832. reset();
  833. }
  834. void reset() noexcept override { drive.reset(); }
  835. int latencySamples() const noexcept override { return DriveStage::latency(); }
  836. // 0..3 = the suite's meters (2 / 3: input / output peaks), 4..7 = input L / R and output L / R (the side columns)
  837. float controlOutput (int k) const noexcept override { return k >= 4 && k < 8 ? sides[k - 4].load (std::memory_order_relaxed) : meters.read (k); }
  838. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  839. {
  840. namespace k = kiln;
  841. const auto group = k::groupOf (p[k::group]);
  842. const bool ownBypass = p[k::groupBypass] >= 0.5f;
  843. vote (group, ownBypass);
  844. const bool bypassed = group > 0 ? kilnGroupBypassVotes[group].load (std::memory_order_relaxed) > 0 : ownBypass;
  845. const auto mainIn = (float) dbToGain (k::mainDb (p[k::mainIn])), mainOut = (float) dbToGain (k::mainDb (p[k::mainOut]));
  846. for (int i = 0; i < n; ++i)
  847. {
  848. l[i] *= mainIn;
  849. r[i] *= mainIn;
  850. }
  851. const auto inPeak = Meters::peak (l, r, n);
  852. sides[0].store (Meters::peak (l, l, n), std::memory_order_relaxed);
  853. sides[1].store (Meters::peak (r, r, n), std::memory_order_relaxed);
  854. const auto step = k::consoleStep (p[k::console]);
  855. const auto model = driveConsoleE + step;
  856. // the console in or out (out: the same latency, so a group bypass never moves the timing)
  857. drive.process (l, r, n, model, bypassed ? 0.0f : p[k::drive], bypassed ? 1.0f : (float) dbToGain (k::trimDb (p[k::input])),
  858. bypassed ? 1.0f : (float) dbToGain (k::trimDb (p[k::output])), bypassed ? 0.0f : 1.0f);
  859. if (! bypassed && p[k::noiseReduction] < 0.5f)
  860. {
  861. static constexpr double noiseDb[6] { -113.9, -113.9, -110.0, -109.8, -101.6, -101.9 };
  862. const auto sigma = (float) dbToGain (noiseDb[step] + k::trimDb (p[k::output]));
  863. for (int i = 0; i < n; ++i)
  864. {
  865. l[i] += sigma * gauss (noiseL);
  866. r[i] += sigma * gauss (noiseR);
  867. }
  868. }
  869. for (int i = 0; i < n; ++i)
  870. {
  871. l[i] *= mainOut;
  872. r[i] *= mainOut;
  873. }
  874. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  875. sides[2].store (Meters::peak (l, l, n), std::memory_order_relaxed);
  876. sides[3].store (Meters::peak (r, r, n), std::memory_order_relaxed);
  877. }
  878. private:
  879. static float gauss (std::uint32_t& st) noexcept
  880. {
  881. float sum = 0.0f;
  882. for (int k = 0; k < 4; ++k)
  883. {
  884. st ^= st << 13; st ^= st >> 17; st ^= st << 5;
  885. sum += (float) (st >> 8) * (1.0f / 16777216.0f);
  886. }
  887. return (sum - 2.0f) * 1.7320508f; // variance 4/12 -> 1
  888. }
  889. // a group's bypass: any channel of the group with its Group bypass on bypasses them all (the original's buttons
  890. // move together); this channel's vote follows its group and switch
  891. void vote (int group, bool on) noexcept
  892. {
  893. if (group == votedGroup && on == votedOn)
  894. return;
  895. if (votedOn && votedGroup > 0)
  896. kilnGroupBypassVotes[votedGroup].fetch_sub (1, std::memory_order_relaxed);
  897. if (on && group > 0)
  898. kilnGroupBypassVotes[group].fetch_add (1, std::memory_order_relaxed);
  899. votedGroup = group;
  900. votedOn = on;
  901. }
  902. DriveStage drive;
  903. Meters meters;
  904. std::array<std::atomic<float>, 4> sides {};
  905. std::uint32_t noiseL = 0x9e3779b9u, noiseR = 0x7f4a7c15u;
  906. int votedGroup = 0;
  907. bool votedOn = false;
  908. };
  909. } // namespace
  910. std::unique_ptr<Effect> makeKiln() { return std::make_unique<KilnChannel>(); }
  911. namespace
  912. {
  913. class Strip final : public Effect
  914. {
  915. public:
  916. // `initial` (may be null): the slot's parameters, so the latency (the Vocal voicing's look-ahead, the DRIVE
  917. // section) is known after prepare(), before the first block (issue #113)
  918. explicit Strip (const EffectParams* initial = nullptr)
  919. {
  920. if (initial != nullptr)
  921. params0 = *initial;
  922. hasParams0 = initial != nullptr;
  923. }
  924. void prepare (double rate) noexcept override
  925. {
  926. sr = rate;
  927. console.prepare (rate);
  928. modernEngine.prepare (rate);
  929. vocalEngine.prepare (rate);
  930. drive.prepare (rate);
  931. reset();
  932. if (hasParams0)
  933. {
  934. vocalLatency = 0;
  935. if (vocalVoicing (params0))
  936. {
  937. vocalEngine.set (vocalSettings (params0));
  938. vocalLatency = vocalEngine.latencySamples();
  939. }
  940. driveLatency = on (params0[driveOn]) ? DriveStage::latency() : 0;
  941. }
  942. }
  943. // round 145: only the Vocal strip voicing's look-ahead adds latency (known from the last block's settings);
  944. // round tp172c6: + the DRIVE section's oversampler while it is in
  945. int latencySamples() const noexcept override { return vocalLatency + driveLatency; }
  946. void reset() noexcept override
  947. {
  948. console.reset();
  949. modernEngine.reset();
  950. vocalEngine.reset();
  951. drive.reset();
  952. for (auto* d : { &audio, &side })
  953. {
  954. d->hp1.reset(); d->hp2.reset(); d->lp.reset(); d->lf.reset(); d->lmf.reset(); d->hmf.reset(); d->hf.reset();
  955. }
  956. compGr = 0.0;
  957. gateGr = 0.0;
  958. levelEnv = 0.0;
  959. gateOpen = false;
  960. }
  961. float controlOutput (int k) const noexcept override { return meters.read (k); }
  962. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  963. {
  964. if (vocalVoicing (p))
  965. {
  966. const auto inPeak = Meters::peak (l, r, n);
  967. vocalEngine.set (vocalSettings (p));
  968. vocalLatency = vocalEngine.latencySamples();
  969. vocalEngine.process (l, r, n);
  970. driveAfterVoicing (l, r, n, p);
  971. meters.set (vocalEngine.compReductionDb(), std::max (vocalEngine.deEssReductionDb(), vocalEngine.dePlosReductionDb()),
  972. inPeak, Meters::peak (l, r, n));
  973. return;
  974. }
  975. vocalLatency = 0;
  976. if (modernVoicing (p)) // round tp172c
  977. {
  978. const auto inPeak = Meters::peak (l, r, n);
  979. modernEngine.process (l, r, n, modernSettingsOf (p));
  980. driveAfterVoicing (l, r, n, p);
  981. meters.set (modernEngine.compReductionDb(), modernEngine.gateReductionDb(), inPeak, Meters::peak (l, r, n));
  982. return;
  983. }
  984. if (k4Voicing (p)) // round 148
  985. {
  986. const auto inPeak = Meters::peak (l, r, n);
  987. console.process (l, r, n, consoleSettingsOf (p));
  988. driveAfterVoicing (l, r, n, p);
  989. meters.set (console.compReductionDb(), console.gateReductionDb(), inPeak, Meters::peak (l, r, n));
  990. return;
  991. }
  992. if (eVoicing (p))
  993. {
  994. const auto inPeak = Meters::peak (l, r, n);
  995. console.process (l, r, n, consoleSettingsOf (p));
  996. driveAfterVoicing (l, r, n, p);
  997. if (on (p[analog]))
  998. addAnalogNoise (l, r, n);
  999. meters.set (console.compReductionDb(), console.gateReductionDb(), inPeak, Meters::peak (l, r, n));
  1000. return;
  1001. }
  1002. audio.design (p, sr);
  1003. side.design (p, sr);
  1004. const auto inPeak = Meters::peak (l, r, n);
  1005. const auto inGain = (float) dbToGain (trimDb (p[input]));
  1006. // round 147b: + the console's automatic make-up when switched on
  1007. const auto outGain = (float) (dbToGain (trimDb (p[output]) + (on (p[autoMakeup]) ? autoMakeupDb (p) : 0.0)) * (on (p[phase]) ? -1.0 : 1.0));
  1008. const bool eq = on (p[eqOn]), dyn = on (p[dynOn]), eqFirst = ! on (p[order]), fts = on (p[filtersToSidechain]);
  1009. const bool eqSc = on (p[eqToSidechain]); // round 165: the filters and the EQ in the side chain only
  1010. // round tp172c6: with the DRIVE section in, the output gain waits until after it
  1011. const bool driveIn = on (p[driveOn]);
  1012. driveLatency = driveIn ? DriveStage::latency() : 0;
  1013. if (driveIn && ! driveWasIn)
  1014. drive.reset();
  1015. driveWasIn = driveIn;
  1016. const auto driveOutGain = driveIn ? 1.0f : outGain;
  1017. // compressor
  1018. const auto thr = compThresholdDb (p[compThreshold]), rat = ratio (p[compRatio]);
  1019. const auto grCap = compGrCapDb (p[compGrCap]);
  1020. const auto compAttack = coef (p[compAttackFine] > 1e-6f ? compAttackFineSeconds (p[compAttackFine]) : (on (p[compFastAttack]) ? 0.001 : 0.010), sr);
  1021. const auto compRel = coef (releaseSeconds (p[ridge::compRelease]), sr);
  1022. const auto cmix = (float) std::clamp (p[compMix], 0.0f, 1.0f);
  1023. const bool compOn = p[compRatio] > 0.0005f;
  1024. // gate / expander
  1025. const auto range = gateRangeDb (p[gateRange]), gthr = gateThresholdDb (p[gateThreshold]);
  1026. const bool gateOn = range > 0.05, expand = on (p[expander]);
  1027. const auto gateAttack = coef (on (p[gateFastAttack]) ? 0.0001 : 0.0015, sr);
  1028. const auto gateRel = coef (releaseSeconds (p[ridge::gateRelease]), sr);
  1029. const auto levelRelease = coef (0.030, sr);
  1030. float maxComp = 0.0f, maxGate = 0.0f;
  1031. for (int i = 0; i < n; ++i)
  1032. {
  1033. float a = l[i] * inGain, b = r[i] * inGain;
  1034. if (! fts && ! eqSc)
  1035. filters (audio, a, b);
  1036. if (eq && eqFirst && ! eqSc)
  1037. bands (audio, a, b);
  1038. if (dyn && (compOn || gateOn))
  1039. {
  1040. float sa = a, sb = b;
  1041. if (fts || eqSc)
  1042. filters (side, sa, sb);
  1043. if (eqSc && eq)
  1044. bands (side, sa, sb);
  1045. const auto rect = (double) std::max (std::abs (sa), std::abs (sb));
  1046. // level detector: instant attack, 30 ms release (keeps the ripple of low notes out of the gain)
  1047. levelEnv = rect > levelEnv ? rect : levelRelease * levelEnv + (1.0 - levelRelease) * rect;
  1048. const auto peak = levelEnv;
  1049. // gate / expander first (as in console channel dynamics), then the compressor
  1050. if (gateOn)
  1051. {
  1052. const auto lvl = gainToDb (peak);
  1053. double target;
  1054. if (expand) // 1:2 downward expansion below the threshold, limited to the range
  1055. target = lvl < gthr ? std::min (range, (gthr - lvl)) : 0.0;
  1056. else
  1057. {
  1058. // hysteresis: opens at the threshold, closes 4 dB below it
  1059. if (lvl >= gthr)
  1060. gateOpen = true;
  1061. else if (lvl < gthr - 4.0)
  1062. gateOpen = false;
  1063. target = gateOpen ? 0.0 : range;
  1064. }
  1065. gateGr = target < gateGr ? gateAttack * gateGr + (1.0 - gateAttack) * target
  1066. : gateRel * gateGr + (1.0 - gateRel) * target;
  1067. }
  1068. else
  1069. gateGr = 0.0;
  1070. if (compOn)
  1071. {
  1072. const auto x = gainToDb (peak) - gateGr;
  1073. const auto want = std::min (x - gainComputer (x, thr, rat), grCap);
  1074. compGr = want > compGr ? compAttack * compGr + (1.0 - compAttack) * want
  1075. : compRel * compGr + (1.0 - compRel) * want;
  1076. }
  1077. else
  1078. compGr = 0.0;
  1079. const auto g = (float) dbToGain (-gateGr);
  1080. const auto c = (float) dbToGain (-compGr);
  1081. const auto wet = g * (cmix * c + (1.0f - cmix));
  1082. a *= wet;
  1083. b *= wet;
  1084. maxComp = std::max (maxComp, (float) compGr);
  1085. maxGate = std::max (maxGate, (float) gateGr);
  1086. }
  1087. if (eq && ! eqFirst && ! eqSc)
  1088. bands (audio, a, b);
  1089. l[i] = a * driveOutGain;
  1090. r[i] = b * driveOutGain;
  1091. }
  1092. // round tp172c6: the DRIVE section sits before the output trim (the strip's own make-up and phase)
  1093. if (driveIn)
  1094. {
  1095. drive.process (l, r, n, p);
  1096. for (int i = 0; i < n; ++i)
  1097. {
  1098. l[i] *= outGain;
  1099. r[i] *= outGain;
  1100. }
  1101. }
  1102. if (on (p[analog]))
  1103. addAnalogNoise (l, r, n);
  1104. meters.set (maxComp, maxGate, inPeak, Meters::peak (l, r, n));
  1105. }
  1106. private:
  1107. // round 147b: ANALOG — [EXEC] the reference console with Analog on adds, at its very output (whatever the input,
  1108. // output or compressor settings), white noise of −110.7 dBFS RMS per channel, the two channels uncorrelated; its
  1109. // harmonics are unchanged (H2 … H5 of a 1 kHz / −20 dBFS and a 100 Hz / −6 dBFS sine within 0.4 dB). Gaussian
  1110. // (Irwin–Hall, four uniforms) from a small xorshift generator, one per channel.
  1111. void addAnalogNoise (float* l, float* r, int n) noexcept
  1112. {
  1113. constexpr float sigma = 2.91e-6f; // 10^(−110.73 / 20)
  1114. const auto gauss = [] (std::uint32_t& st) noexcept {
  1115. float sum = 0.0f;
  1116. for (int k = 0; k < 4; ++k)
  1117. {
  1118. st ^= st << 13; st ^= st >> 17; st ^= st << 5;
  1119. sum += (float) (st >> 8) * (1.0f / 16777216.0f);
  1120. }
  1121. return (sum - 2.0f) * 1.7320508f; // variance 4/12 → 1
  1122. };
  1123. for (int i = 0; i < n; ++i)
  1124. {
  1125. l[i] += sigma * gauss (noiseL);
  1126. r[i] += sigma * gauss (noiseR);
  1127. }
  1128. }
  1129. std::uint32_t noiseL = 0x9e3779b9u, noiseR = 0x85ebca6bu;
  1130. // round tp172c6: the DRIVE section after a voicing (their output trims are inside them)
  1131. void driveAfterVoicing (float* l, float* r, int n, const EffectParams& p) noexcept
  1132. {
  1133. const bool driveIn = on (p[driveOn]);
  1134. driveLatency = driveIn ? DriveStage::latency() : 0;
  1135. if (driveIn && ! driveWasIn)
  1136. drive.reset();
  1137. driveWasIn = driveIn;
  1138. if (driveIn)
  1139. drive.process (l, r, n, p);
  1140. }
  1141. vmatch::echannel::Channel console;
  1142. vmatch::strip2::Strip modernEngine; // round tp172c
  1143. voxstrip::Engine vocalEngine;
  1144. int vocalLatency = 0;
  1145. DriveStage drive; // round tp172c6
  1146. int driveLatency = 0;
  1147. bool driveWasIn = false;
  1148. EffectParams params0 {};
  1149. bool hasParams0 = false;
  1150. static void filters (EqDesign& d, float& a, float& b) noexcept
  1151. {
  1152. if (d.hpOn)
  1153. {
  1154. a = d.hp2.process (d.hp1.process (a, 0), 0);
  1155. b = d.hp2.process (d.hp1.process (b, 1), 1);
  1156. }
  1157. if (d.lpOn)
  1158. {
  1159. a = d.lp.process (a, 0);
  1160. b = d.lp.process (b, 1);
  1161. }
  1162. }
  1163. static void bands (EqDesign& d, float& a, float& b) noexcept
  1164. {
  1165. a = d.hf.process (d.hmf.process (d.lmf.process (d.lf.process (a, 0), 0), 0), 0);
  1166. b = d.hf.process (d.hmf.process (d.lmf.process (d.lf.process (b, 1), 1), 1), 1);
  1167. }
  1168. double sr = 44100.0;
  1169. EqDesign audio, side;
  1170. double compGr = 0.0, gateGr = 0.0, levelEnv = 0.0;
  1171. bool gateOpen = false;
  1172. Meters meters;
  1173. };
  1174. } // namespace
  1175. } // namespace ridge
  1176. // ================================================================== Glow Leveler
  1177. namespace glow
  1178. {
  1179. double gainDb (float v) noexcept { return lin (v, 0.0, 30.0); }
  1180. double sidechainDb (float v) noexcept { return lin (v, -20.0, 40.0); }
  1181. bool classic (const EffectParams& p) noexcept { return on (p[voicing]); }
  1182. double trimDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  1183. std::vector<std::pair<int, float>> linkedEdit (const EffectParams& p, int param, float value)
  1184. {
  1185. std::vector<std::pair<int, float>> edits { { param, value } };
  1186. if (param != autoMakeup || ! classic (p) || ! on (p[optoLaw]) || on (value) == on (p[autoMakeup]))
  1187. return edits;
  1188. const double pr = 100.0 * std::clamp ((double) p[peakReduction], 0.0, 1.0);
  1189. if (on (value)) // switched on: the dial as it is now is the reference
  1190. edits.push_back ({ makeupRef, p[peakReduction] });
  1191. else // switched off: the trial keeps the make-up (its Gain knob moved); LYNX moves the Gain dial by as much
  1192. {
  1193. const double ref = 100.0 * std::clamp ((double) p[makeupRef], 0.0, 1.0);
  1194. const double db = vmatch::opto::makeupDb (100.0 * p[gain]) + vmatch::opto::autoMakeupDb (pr) - vmatch::opto::autoMakeupDb (ref);
  1195. edits.push_back ({ gain, (float) (vmatch::opto::gainForMakeupDb (db) / 100.0) });
  1196. }
  1197. return edits;
  1198. }
  1199. int analogHz (float v) noexcept { return v < 0.25f ? 0 : v < 0.75f ? 50 : 60; }
  1200. namespace
  1201. {
  1202. class Leveler final : public Effect
  1203. {
  1204. public:
  1205. void prepare (double rate) noexcept override
  1206. {
  1207. sr = rate;
  1208. panelAttack = coef (0.001, sr);
  1209. panelRelease = coef (0.030, sr);
  1210. fastAttack = coef (0.010, sr);
  1211. fastRelease = coef (0.050, sr);
  1212. slowAttack = coef (0.400, sr);
  1213. memoryUp = coef (2.0, sr);
  1214. memoryDown = coef (8.0, sr);
  1215. autoCoef = coef (0.5, sr);
  1216. classicEngine.prepare (rate);
  1217. reset();
  1218. }
  1219. void reset() noexcept override
  1220. {
  1221. emph.reset();
  1222. classicEngine.reset();
  1223. panel = cellFast = cellSlow = memory = autoGr = 0.0;
  1224. lastL = lastR = 0.0f;
  1225. }
  1226. float controlOutput (int k) const noexcept override { return meters.read (k); }
  1227. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  1228. {
  1229. const auto inPeak = Meters::peak (l, r, n);
  1230. if (classic (p))
  1231. {
  1232. vmatch::opto::Settings c;
  1233. c.peakReduction = 100.0 * std::clamp ((double) p[peakReduction], 0.0, 1.0);
  1234. c.gain = 100.0 * std::clamp ((double) p[gain], 0.0, 1.0);
  1235. c.hiFreq = 100.0 * std::clamp ((double) p[emphasis], 0.0, 1.0);
  1236. c.limit = on (p[mode]);
  1237. c.measured = on (p[optoLaw]);
  1238. c.autoMakeup = on (p[autoMakeup]);
  1239. c.measuredRelease = on (p[releaseLaw]);
  1240. c.makeupRef = 100.0 * std::clamp ((double) p[makeupRef], 0.0, 1.0);
  1241. c.memory = on (p[optoMemory]);
  1242. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1243. const int hz = analogHz (p[analog]);
  1244. const auto trimGain = (float) dbToGain (trimDb (p[trim]));
  1245. for (int off = 0; off < n; off += maxBlock)
  1246. {
  1247. const int m = std::min (maxBlock, n - off);
  1248. std::copy (l + off, l + off + m, dryL.begin());
  1249. std::copy (r + off, r + off + m, dryR.begin());
  1250. classicEngine.process (l + off, r + off, m, c);
  1251. if (hz > 0)
  1252. addAnalog (l + off, r + off, m, hz);
  1253. for (int i = 0; i < m; ++i)
  1254. {
  1255. l[off + i] = (l[off + i] * mx + dryL[(std::size_t) i] * (1.0f - mx)) * trimGain;
  1256. r[off + i] = (r[off + i] * mx + dryR[(std::size_t) i] * (1.0f - mx)) * trimGain;
  1257. }
  1258. }
  1259. meters.set (classicEngine.reductionDb(), 0.0f, inPeak, Meters::peak (l, r, n));
  1260. return;
  1261. }
  1262. const auto sc = p[peakReduction] <= 0.0f ? 0.0 : dbToGain (sidechainDb (p[peakReduction]));
  1263. const auto gamma = on (p[mode]) ? 9.0 : 2.0; // asymptotic ratio 1 + gamma: about 3:1 (compress), 10:1 (limit)
  1264. emph.shelf (sr, 2500.0, 10.0 * std::clamp ((double) p[emphasis], 0.0, 1.0), true, 0.7);
  1265. const auto makeup = (float) dbToGain (gainDb (p[gain]));
  1266. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1267. const auto trimGain = (float) dbToGain (trimDb (p[trim]));
  1268. const auto autoUp = on (p[autoMakeup]);
  1269. const int hz = analogHz (p[analog]);
  1270. double maxGr = 0.0;
  1271. for (int i = 0; i < n; ++i)
  1272. {
  1273. // feedback detection: the cell sees the leveler's own output (the previous sample)
  1274. const auto s = std::max (std::abs (emph.process (lastL, 0)), std::abs (emph.process (lastR, 1)));
  1275. panel = s > panel ? panelAttack * panel + (1.0 - panelAttack) * s : panelRelease * panel + (1.0 - panelRelease) * s;
  1276. const auto light = std::pow (sc * panel, gamma);
  1277. // the cell: a fast conductance (attack ~10 ms, release ~50 ms) and a slow one whose release lengthens
  1278. // with the memory of past reduction (0.4 s fresh, up to 5 s after long, heavy leveling)
  1279. cellFast = light > cellFast ? fastAttack * cellFast + (1.0 - fastAttack) * light
  1280. : fastRelease * cellFast + (1.0 - fastRelease) * light;
  1281. const auto slowRelease = coef (0.4 + 4.6 * memory, sr);
  1282. cellSlow = light > cellSlow ? slowAttack * cellSlow + (1.0 - slowAttack) * light
  1283. : slowRelease * cellSlow + (1.0 - slowRelease) * light;
  1284. const auto busy = light > 0.1 ? 1.0 : 0.0;
  1285. memory = busy > memory ? memoryUp * memory + (1.0 - memoryUp) * busy : memoryDown * memory + (1.0 - memoryDown) * busy;
  1286. const auto conductance = 0.7 * cellFast + 0.3 * cellSlow;
  1287. const auto g = (float) (1.0 / (1.0 + conductance));
  1288. lastL = l[i] * g;
  1289. lastR = r[i] * g;
  1290. // auto make-up (round 129) [model]: gives back ~2/3 of the slow average reduction, so turning PEAK REDUCTION
  1291. // up keeps the level roughly where it was (the reference's AUTO MAKEUP; its law is not measured)
  1292. auto wet = makeup;
  1293. if (autoUp)
  1294. {
  1295. const auto grDb = -gainToDb (g);
  1296. autoGr = autoCoef * autoGr + (1.0 - autoCoef) * grDb;
  1297. wet *= (float) dbToGain (0.66 * autoGr);
  1298. }
  1299. auto wl = lastL * wet, wr = lastR * wet;
  1300. if (hz > 0)
  1301. addAnalog (&wl, &wr, 1, hz);
  1302. l[i] = (wl * mx + l[i] * (1.0f - mx)) * trimGain;
  1303. r[i] = (wr * mx + r[i] * (1.0f - mx)) * trimGain;
  1304. maxGr = std::max (maxGr, -gainToDb (g));
  1305. }
  1306. meters.set ((float) maxGr, 0.0f, inPeak, Meters::peak (l, r, n));
  1307. }
  1308. private:
  1309. // round 152: ANALOG [EXEC] (REA_rodada152a §2) — the reference adds, in its wet path after the make-up (Trim moves it,
  1310. // Mix 0 % removes it, Gain and Peak reduction do not), pink-like noise of −89.5 dBFS RMS per channel (the two
  1311. // uncorrelated, flat per third octave within 3 dB) and mains hum on both channels alike: harmonics of 50 or 60 Hz,
  1312. // the 2nd strongest (−98.7 dBFS peak), as measured on silence. Pink: Paul Kellet's economy filter over Gaussian
  1313. // (Irwin–Hall) noise from a small xorshift per channel.
  1314. void addAnalog (float* l, float* r, int n, int hz) noexcept
  1315. {
  1316. static constexpr double hum50[12] { -117.2, -98.7, -118.2, -105.2, -122.5, -120.6, -126.6, -126.4, -127.7, -123.8, -125.5, -125.5 };
  1317. static constexpr double hum60[12] { -117.8, -98.5, -118.7, -104.9, -121.8, -120.1, -121.8, -123.1, -128.7, -121.4, -127.7, -132.1 };
  1318. constexpr float sigma = 1.1322e-5f; // 10^(−89.5 / 20) / the filter's RMS gain (2.9586)
  1319. if (humHz != hz)
  1320. {
  1321. humHz = hz;
  1322. for (int k = 0; k < 12; ++k)
  1323. humAmp[k] = dbToGain ((hz == 50 ? hum50 : hum60)[k]);
  1324. }
  1325. const auto gauss = [] (std::uint32_t& st) noexcept {
  1326. float sum = 0.0f;
  1327. for (int k = 0; k < 4; ++k)
  1328. {
  1329. st ^= st << 13; st ^= st >> 17; st ^= st << 5;
  1330. sum += (float) (st >> 8) * (1.0f / 16777216.0f);
  1331. }
  1332. return (sum - 2.0f) * 1.7320508f;
  1333. };
  1334. const double dphi = (double) hz / sr;
  1335. for (int i = 0; i < n; ++i)
  1336. {
  1337. float* ch[2] { &l[i], &r[i] };
  1338. for (int c = 0; c < 2; ++c)
  1339. {
  1340. const auto w = gauss (seed[c]);
  1341. auto* b = pink[c];
  1342. b[0] = 0.99765f * b[0] + w * 0.0990460f;
  1343. b[1] = 0.96300f * b[1] + w * 0.2965164f;
  1344. b[2] = 0.57000f * b[2] + w * 1.0526913f;
  1345. *ch[c] += sigma * (b[0] + b[1] + b[2] + w * 0.1848f);
  1346. }
  1347. double hum = 0.0;
  1348. for (int k = 0; k < 12; ++k)
  1349. hum += humAmp[k] * std::sin (2.0 * pi * (double) (k + 1) * humPhase);
  1350. humPhase += dphi;
  1351. if (humPhase >= 1.0) humPhase -= 1.0;
  1352. l[i] += (float) hum;
  1353. r[i] += (float) hum;
  1354. }
  1355. }
  1356. std::uint32_t seed[2] { 0x9e3779b9u, 0x85ebca6bu };
  1357. float pink[2][3] {};
  1358. double humPhase = 0.0, humAmp[12] {};
  1359. int humHz = 0;
  1360. static constexpr int maxBlock = 1024;
  1361. double sr = 44100.0;
  1362. vmatch::opto::Leveler classicEngine;
  1363. std::array<float, maxBlock> dryL {}, dryR {};
  1364. Biquad emph;
  1365. double panel = 0, cellFast = 0, cellSlow = 0, memory = 0;
  1366. double panelAttack = 0, panelRelease = 0, fastAttack = 0, fastRelease = 0, slowAttack = 0, memoryUp = 0, memoryDown = 0;
  1367. double autoGr = 0, autoCoef = 0;
  1368. float lastL = 0, lastR = 0;
  1369. Meters meters;
  1370. };
  1371. } // namespace
  1372. } // namespace glow
  1373. // ================================================================== Hush
  1374. namespace hush
  1375. {
  1376. double hz (float v) noexcept { return logMap (v, 2000.0, 16000.0); }
  1377. double lowPassHz (float v) noexcept { return logMap (v, 2000.0, 20000.0); }
  1378. double thresholdDb (float v) noexcept { return lin (v, -60.0, 0.0); }
  1379. double rangeDb (float v) noexcept { return lin (v, 0.0, 24.0); }
  1380. double releaseMs (float v) noexcept { return logMap (v, 10.0, 250.0); }
  1381. double lookaheadMs() noexcept { return 2.0; }
  1382. double lookaheadTimeMs (float v) noexcept { return std::round (lin (v, 0.0, 15.0) * 100.0) / 100.0; } // (0.01 ms steps: the default is exactly 2 ms)
  1383. float lookaheadTimeValue (double ms) noexcept { return (float) std::clamp (ms / 15.0, 0.0, 1.0); }
  1384. double classicThresholdDb (float v) noexcept { return lin (v, -80.0, 0.0); }
  1385. double panValue (float v) noexcept { return lin (v, -1.0, 1.0); }
  1386. // round 165x [EXEC] (REA_rodada165x §2, the trial's value texts at 1000 steps): the reference's Input / Output Level reads
  1387. // 72 dB per unit above 0.2 (0.5 = 0 dB, 1 = +36 dB) and 40 log10 (v) + 6.36 dB below (−21.6 dB at 0.2, −∞ at 0); its
  1388. // high-pass 2000 · 10^v Hz (2 … 20 kHz)
  1389. double levelDb (float v, bool ext) noexcept
  1390. {
  1391. if (! ext)
  1392. return lin (v, -12.0, 12.0);
  1393. const double x = std::clamp ((double) v, 0.0, 1.0);
  1394. return x >= 0.2 ? 72.0 * x - 36.0 : x <= 0.0 ? -200.0 : 40.0 * std::log10 (x) + 6.3588;
  1395. }
  1396. float levelValue (double db, bool ext) noexcept
  1397. {
  1398. if (! ext)
  1399. return (float) std::clamp ((db + 12.0) / 24.0, 0.0, 1.0);
  1400. if (db >= -21.6)
  1401. return (float) std::clamp ((db + 36.0) / 72.0, 0.0, 1.0);
  1402. return db <= -199.0 ? 0.0f : (float) std::clamp (std::pow (10.0, (db - 6.3588) / 40.0), 0.0, 0.2);
  1403. }
  1404. double hpHz (float v, bool ext) noexcept { return ext ? 2000.0 * std::pow (10.0, std::clamp ((double) v, 0.0, 1.0)) : hz (v); }
  1405. float hpValue (double f, bool ext) noexcept
  1406. {
  1407. const double x = std::max (f, 1.0) / 2000.0;
  1408. return (float) std::clamp (ext ? std::log10 (x) : std::log (x) / std::log (8.0), 0.0, 1.0);
  1409. }
  1410. bool extended (const EffectParams& p) noexcept { return on (p[rangeLaw]) && ! on (p[voicing]); }
  1411. // round 165x [EXEC] (REA_rodada165x §2; L / R test tones, sibilance in one channel, both in phase, in anti-phase):
  1412. // the reference's Stereo Link k (0 … 1 = 0 … 100 %) detects each channel on (1 − k/2) · own + k/2 · other — 100 % is
  1413. // the mid signal, so side-only sibilance does not trigger; above 100 % a share m of the output is processed on the mid
  1414. // signal only (Mid-only: the side passes) with the same gain. Side-only (the same test material, 125 / 150 / 200 %):
  1415. // each channel detects on (1 − m) · mid ∓ m · side (L −, R +) and processes (1 − m) · itself ± m · side (L +, R −).
  1416. struct ProLink
  1417. {
  1418. double k = 1.0, m = 0.0;
  1419. bool side = false;
  1420. bool twoLanes() const noexcept { return (m <= 0.0 && k < 1.0) || (m > 0.0 && side); }
  1421. // the detector's input(s) for the channels' samples a / b
  1422. void lanes (double a, double b, double& d0, double& d1) const noexcept
  1423. {
  1424. if (m <= 0.0 && k < 1.0) { d0 = (1.0 - 0.5 * k) * a + 0.5 * k * b; d1 = 0.5 * k * a + (1.0 - 0.5 * k) * b; }
  1425. else if (! side) d0 = d1 = 0.5 * (a + b);
  1426. else { const double mid = 0.5 * (a + b), sd = 0.5 * (a - b); d0 = (1.0 - m) * mid - m * sd; d1 = (1.0 - m) * mid + m * sd; }
  1427. }
  1428. // the change to add to the channels given their band signals ba / bb and the lanes' gains
  1429. void delta (double g0, double g1, double ba, double bb, double& oa, double& ob) const noexcept
  1430. {
  1431. if (m <= 0.0) { oa = (g0 - 1.0) * ba; ob = ((k < 1.0 ? g1 : g0) - 1.0) * bb; return; }
  1432. if (! side)
  1433. {
  1434. const double mid = 0.5 * (ba + bb);
  1435. oa = (g0 - 1.0) * ((1.0 - m) * ba + m * mid);
  1436. ob = (g0 - 1.0) * ((1.0 - m) * bb + m * mid);
  1437. return;
  1438. }
  1439. const double sd = 0.5 * (ba - bb);
  1440. oa = (g0 - 1.0) * ((1.0 - m) * ba + m * sd);
  1441. ob = (g1 - 1.0) * ((1.0 - m) * bb - m * sd);
  1442. }
  1443. };
  1444. static ProLink proLinkOf (const EffectParams& p) noexcept
  1445. {
  1446. ProLink l;
  1447. l.k = std::clamp ((double) p[stereoLink], 0.0, 1.0);
  1448. l.m = l.k >= 1.0 ? std::clamp ((double) p[linkOnly], 0.0, 1.0) : 0.0;
  1449. l.side = on (p[linkSide]);
  1450. return l;
  1451. }
  1452. // round 165w [EXEC] (REA_rodada165w §5): the reference's input / output pan is a balance — the far channel × (1 − |pan|),
  1453. // the near one untouched (both 1 kHz / 1.5 kHz test tones, every quarter step)
  1454. static void balance (float* l, float* r, int n, double pan) noexcept
  1455. {
  1456. if (pan == 0.0)
  1457. return;
  1458. const auto g = (float) (1.0 - std::min (1.0, std::abs (pan)));
  1459. auto* far = pan > 0.0 ? l : r;
  1460. for (int i = 0; i < n; ++i)
  1461. far[i] *= g;
  1462. }
  1463. bool classic (const EffectParams& p) noexcept { return on (p[voicing]); }
  1464. bool vocal (const EffectParams& p) noexcept { return on (p[vocalVoicing]) && ! on (p[voicing]); }
  1465. double airDb (float v) noexcept { return lin (std::clamp (v, 0.0f, 1.0f), 0.0, 12.0); }
  1466. namespace
  1467. {
  1468. class DeEsser final : public Effect
  1469. {
  1470. public:
  1471. // `p` (may be null): the slot's parameters, so the latency (voicing, look-ahead, oversampling) is known before the
  1472. // first block (issue #113: a Patcher plans its delay compensation right after building its nodes)
  1473. explicit DeEsser (const EffectParams* p = nullptr)
  1474. : primedMs (p != nullptr ? lookaheadTimeMs ((*p)[lookaheadTime]) : lookaheadMs()),
  1475. vocalOn (p != nullptr && (vocal (*p) || (extended (*p) && on ((*p)[allround]) && ! classic (*p)))),
  1476. classicOn (p != nullptr && classic (*p))
  1477. {
  1478. // (the same as processCore does when the factor moves)
  1479. if (const auto factor = p != nullptr && ! classic (*p) ? oversamplingOf ((*p)[oversampling]) : 1; factor != osFactor)
  1480. {
  1481. osFactor = factor;
  1482. for (auto* bank : { &os, &osBand, &osGain })
  1483. for (auto& o : *bank) { o.setFactor (std::max (2, factor)); o.reset(); }
  1484. }
  1485. }
  1486. void prepare (double rate) noexcept override
  1487. {
  1488. sr = rate;
  1489. setDelay (primedMs);
  1490. classicEngine.prepare (rate);
  1491. vocalEngine.prepare (rate);
  1492. reset();
  1493. }
  1494. void reset() noexcept override
  1495. {
  1496. delay.reset();
  1497. scDelay.reset();
  1498. classicEngine.reset();
  1499. vocalEngine.reset();
  1500. detect.reset();
  1501. detectLp.reset();
  1502. listenF.reset();
  1503. listenLp.reset();
  1504. split.reset();
  1505. bandLp.reset();
  1506. bandLp2.reset();
  1507. topHp.reset();
  1508. topHp2.reset();
  1509. env = 0.0;
  1510. gr = 0.0;
  1511. envCh[0] = envCh[1] = grCh[0] = grCh[1] = 0.0;
  1512. fullEnv = 0.0;
  1513. airShelf.reset();
  1514. for (auto& d : dryLine) d.fill (0.0f);
  1515. dryPos = 0;
  1516. for (auto& o : os) o.reset();
  1517. for (auto& o : osBand) o.reset();
  1518. for (auto& o : osGain) o.reset();
  1519. }
  1520. int latencySamples() const noexcept override
  1521. {
  1522. if (vocalOn)
  1523. return vocalEngine.latencySamples();
  1524. if (classicOn) // round 165 [EXEC]: the reference reports 0 samples, so the classic voicing has no latency
  1525. return 0;
  1526. return delay.len + (osFactor > 1 ? Oversampler::latency() : 0);
  1527. }
  1528. float controlOutput (int k) const noexcept override { return meters.read (k); }
  1529. void setSidechain (const float* l, const float* r) noexcept override { scL = l; scR = r; }
  1530. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  1531. {
  1532. if (n > maxBlock) // the oversampled path works on blocks of at most maxBlock samples
  1533. {
  1534. const auto* sl = scL;
  1535. const auto* sr2 = scR;
  1536. for (int i = 0; i < n; i += maxBlock)
  1537. {
  1538. if (sl != nullptr && sr2 != nullptr) { scL = sl + i; scR = sr2 + i; }
  1539. processBlock (l + i, r + i, std::min (maxBlock, n - i), p, bpm);
  1540. }
  1541. scL = scR = nullptr;
  1542. return;
  1543. }
  1544. processBlock (l, r, n, p, bpm);
  1545. }
  1546. private:
  1547. void processBlock (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept
  1548. {
  1549. // round 190dm: Air and Mix (off and 100 %: nothing below runs, the sound before)
  1550. const auto aDb = airDb (p[air]);
  1551. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1552. const bool post = aDb > 0.0 || mx < 1.0f;
  1553. if (post)
  1554. {
  1555. std::copy (l, l + n, dryBuf[0].begin());
  1556. std::copy (r, r + n, dryBuf[1].begin());
  1557. }
  1558. processCore (l, r, n, p, bpm);
  1559. if (post)
  1560. airAndMix (l, r, n, aDb, mx);
  1561. balance (l, r, n, panValue (p[outputPan])); // round 165w: after the output level, every voicing
  1562. }
  1563. // round 190dm: the air shelf on the processed signal, then the dry signal (delayed by the latency) mixed back
  1564. void airAndMix (float* l, float* r, int n, double aDb, float mx) noexcept
  1565. {
  1566. if (aDb != airDesignDb || sr != airDesignRate)
  1567. {
  1568. airDesignDb = aDb;
  1569. airDesignRate = sr;
  1570. airShelf.shelf (sr, std::min (airHz, 0.45 * sr), aDb, true, airSlope);
  1571. }
  1572. if (aDb > 0.0)
  1573. for (int i = 0; i < n; ++i)
  1574. {
  1575. l[i] = airShelf.process (l[i], 0);
  1576. r[i] = airShelf.process (r[i], 1);
  1577. }
  1578. else
  1579. airShelf.reset();
  1580. const int lat = std::clamp (latencySamples(), 0, dryLen - 1);
  1581. for (int i = 0; i < n; ++i)
  1582. {
  1583. dryLine[0][(std::size_t) dryPos] = dryBuf[0][(std::size_t) i];
  1584. dryLine[1][(std::size_t) dryPos] = dryBuf[1][(std::size_t) i];
  1585. const auto rd = (std::size_t) ((dryPos - lat + dryLen) % dryLen);
  1586. dryPos = (dryPos + 1) % dryLen;
  1587. if (mx < 1.0f)
  1588. {
  1589. l[i] = l[i] * mx + dryLine[0][rd] * (1.0f - mx);
  1590. r[i] = r[i] * mx + dryLine[1][rd] * (1.0f - mx);
  1591. }
  1592. }
  1593. }
  1594. void processCore (float* l, float* r, int n, const EffectParams& p, double) noexcept
  1595. {
  1596. // round 128: the look-ahead time is the delay (and the reported latency; the engine re-plans the delay
  1597. // compensation when it moves). The default 2 ms is the old fixed value.
  1598. if (const auto ms = lookaheadTimeMs (p[lookaheadTime]); std::abs (ms - delayMs) > 1e-9)
  1599. setDelay (ms);
  1600. const bool extLaw = extended (p); // round 165x: the reference's ranges and stereo link
  1601. const auto inGainDb = classic (p) ? lin (p[input], -12.0, 12.0) : levelDb (p[input], extLaw);
  1602. if (inGainDb != 0.0)
  1603. {
  1604. const auto gIn = (float) dbToGain (inGainDb);
  1605. for (int i = 0; i < n; ++i) { l[i] *= gIn; r[i] *= gIn; }
  1606. }
  1607. balance (l, r, n, panValue (p[inputPan])); // round 165w: after the input level, before the detector
  1608. const auto inPeak = Meters::peak (l, r, n);
  1609. // round 135: oversampling (Hush's own voicing): the gain and the band it acts on are upsampled with the audio
  1610. const auto factor = classic (p) ? 1 : oversamplingOf (p[oversampling]);
  1611. if (factor != osFactor)
  1612. {
  1613. osFactor = factor;
  1614. for (auto* bank : { &os, &osBand, &osGain })
  1615. for (auto& o : *bank) { o.setFactor (std::max (2, factor)); o.reset(); }
  1616. }
  1617. const bool over = osFactor > 1;
  1618. // round tp172b [EXEC] (Pro-DS trial, 1 / 7 kHz sine and pink / white noise stairs, threshold −40, range 8): the
  1619. // reference's Allround follows the single-vocal level law (0.2 dB per dB here, range × over / −threshold) without
  1620. // the vocal gate, where the old law reached the full range ~6 dB over the threshold
  1621. const bool proAllround = extLaw && on (p[allround]) && ! classic (p);
  1622. vocalOn = vocal (p) || proAllround;
  1623. if (vocalOn) // round 148: the measured single-vocal de-esser
  1624. {
  1625. vmatch::vocalds::Settings v;
  1626. v.gate = ! proAllround || vocal (p);
  1627. v.thresholdDb = thresholdDb (p[threshold]);
  1628. v.rangeDb = rangeDb (p[hush::range]);
  1629. v.hpHz = hpHz (p[frequency], extLaw);
  1630. v.lpHz = p[lowPass] < 0.999f ? lowPassHz (p[lowPass]) : 20000.0;
  1631. if (extLaw)
  1632. {
  1633. const auto pl = proLinkOf (p);
  1634. v.proLink = true;
  1635. v.link = pl.k;
  1636. v.linkOnly = pl.m;
  1637. v.linkSide = pl.side;
  1638. }
  1639. v.lookahead = on (p[lookahead]);
  1640. v.lookaheadMs = lookaheadTimeMs (p[lookaheadTime]);
  1641. v.wideband = on (p[mode]);
  1642. v.listen = on (p[listen]);
  1643. vocalEngine.process (l, r, n, v);
  1644. const auto out = (float) dbToGain (levelDb (p[output], extLaw));
  1645. for (int i = 0; i < n; ++i) { l[i] *= out; r[i] *= out; }
  1646. meters.set (vocalEngine.reductionDb(), 0.0f, inPeak, Meters::peak (l, r, n));
  1647. meters.setDetector (vocalEngine.detectorDb());
  1648. scL = scR = nullptr;
  1649. return;
  1650. }
  1651. classicOn = classic (p);
  1652. if (classicOn)
  1653. {
  1654. // the measured classic de-esser has no look-ahead and (round 165) no latency, as the reference
  1655. vmatch::deesser::Settings c;
  1656. c.hz = hz (p[frequency]);
  1657. c.thresholdDb = classicThresholdDb (p[threshold]);
  1658. c.bandPass = on (p[detector]);
  1659. c.wideband = on (p[mode]);
  1660. c.listen = on (p[listen]);
  1661. c.outputDb = lin (p[output], -12.0, 12.0);
  1662. classicEngine.process (l, r, n, c);
  1663. meters.set (classicEngine.reductionDb(), 0.0f, inPeak, Meters::peak (l, r, n));
  1664. meters.setDetector (classicEngine.detectorDb());
  1665. return;
  1666. }
  1667. const auto f = std::min (hpHz (p[frequency], extLaw), sr * 0.45);
  1668. const bool band = on (p[detector]);
  1669. if (band)
  1670. detect.filter (sr, 2, f, 1.4);
  1671. else
  1672. detect.filter (sr, 1, f, 0.70710678);
  1673. listenF = detect;
  1674. split.filter (sr, 0, f, 0.70710678);
  1675. const bool lpOn = p[lowPass] < 0.999f;
  1676. if (lpOn)
  1677. {
  1678. const auto lpHz = std::min (std::max (lowPassHz (p[lowPass]), f * 1.05), sr * 0.45);
  1679. detectLp.filter (sr, 0, lpHz, 0.70710678);
  1680. listenLp.filter (sr, 0, lpHz, 0.70710678);
  1681. bandLp.filter (sr, 0, lpHz, 0.70710678); // Linkwitz-Riley 4th order at the low-pass: band = LP², above = HP²
  1682. bandLp2 = bandLp;
  1683. topHp.filter (sr, 1, lpHz, 0.70710678);
  1684. topHp2 = topHp;
  1685. }
  1686. const auto thr = thresholdDb (p[threshold]), range = rangeDb (p[hush::range]);
  1687. const bool wide = on (p[mode]), listening = on (p[listen]), ahead = on (p[lookahead]);
  1688. const bool allroundMode = on (p[allround]), mid = on (p[linkMid]);
  1689. const bool ext = on (p[external]) && scL != nullptr && scR != nullptr;
  1690. const auto link = std::clamp ((double) p[stereoLink], 0.0, 1.0);
  1691. const bool linked = link >= 1.0 || mid;
  1692. const auto release = coef (releaseMs (p[hush::release]) * 0.001, sr);
  1693. const auto attack = coef (ahead ? std::max (delayMs, 0.03) * 0.001 / 3.0 : 0.0005, sr);
  1694. const auto out = (float) dbToGain (levelDb (p[output], extLaw));
  1695. const auto pl = proLinkOf (p);
  1696. const bool pro = extLaw; // round 165x: the reference's link (detection on the blended signals)
  1697. constexpr double knee = 3.0;
  1698. double maxGr = 0.0, maxDet = 0.0;
  1699. // gain reduction (dB) wanted for a detected level `s` (and the full-band level in allround mode)
  1700. auto wanted = [&] (double s, double full) {
  1701. auto over = gainToDb (s) - thr;
  1702. if (allroundMode) // the sibilance must also stand out from the whole signal (steady highs in a mix trigger less)
  1703. over -= 0.5 * std::max (0.0, gainToDb (full) - gainToDb (s) - 6.0);
  1704. double want = 0.0;
  1705. if (over > knee)
  1706. want = over;
  1707. else if (over > -knee)
  1708. want = (over + knee) * (over + knee) / (4.0 * knee);
  1709. return std::min (want, range);
  1710. };
  1711. for (int i = 0; i < n; ++i)
  1712. {
  1713. float a = l[i], b = r[i];
  1714. float da = a, db = b;
  1715. delay.process (da, db);
  1716. // with lookahead the detector hears the signal before it is played; without, both see the delayed one
  1717. auto xa = ahead ? a : da, xb = ahead ? b : db;
  1718. if (ext)
  1719. {
  1720. float sa = scL[i], sb = scR[i];
  1721. float dsa = sa, dsb = sb;
  1722. scDelay.process (dsa, dsb);
  1723. xa = ahead ? sa : dsa;
  1724. xb = ahead ? sb : dsb;
  1725. }
  1726. double sA, sB;
  1727. if (pro)
  1728. {
  1729. double d0, d1;
  1730. pl.lanes (xa, xb, d0, d1);
  1731. auto ya = detect.process ((float) d0, 0), yb = detect.process ((float) d1, 1);
  1732. if (lpOn) { ya = detectLp.process (ya, 0); yb = detectLp.process (yb, 1); }
  1733. sA = std::abs (ya);
  1734. sB = std::abs (yb);
  1735. }
  1736. else if (mid)
  1737. {
  1738. auto m = detect.process (0.5f * (xa + xb), 0);
  1739. if (lpOn) m = detectLp.process (m, 0);
  1740. sA = sB = std::abs (m);
  1741. }
  1742. else
  1743. {
  1744. auto ya = detect.process (xa, 0), yb = detect.process (xb, 1);
  1745. if (lpOn) { ya = detectLp.process (ya, 0); yb = detectLp.process (yb, 1); }
  1746. sA = std::abs (ya);
  1747. sB = std::abs (yb);
  1748. }
  1749. double full = 0.0;
  1750. if (allroundMode)
  1751. {
  1752. const double fa = std::max (std::abs (xa), std::abs (xb));
  1753. fullEnv = fa > fullEnv ? fa : release * fullEnv + (1.0 - release) * fa;
  1754. full = fullEnv;
  1755. }
  1756. float gA, gB;
  1757. if (pro)
  1758. {
  1759. // each lane its own envelope and gain (one lane when both hear the same signal)
  1760. const int lanes = pl.twoLanes() ? 2 : 1;
  1761. const double ss[2] { sA, sB };
  1762. for (int c = 0; c < lanes; ++c)
  1763. {
  1764. envCh[c] = ss[c] > envCh[c] ? ss[c] : release * envCh[c] + (1.0 - release) * ss[c];
  1765. maxDet = std::max (maxDet, envCh[c]);
  1766. const auto want = wanted (envCh[c], full);
  1767. grCh[c] = want > grCh[c] ? attack * grCh[c] + (1.0 - attack) * want : release * grCh[c] + (1.0 - release) * want;
  1768. maxGr = std::max (maxGr, grCh[c]);
  1769. }
  1770. gA = (float) dbToGain (-grCh[0]);
  1771. gB = (float) dbToGain (-grCh[lanes - 1]);
  1772. }
  1773. else if (linked)
  1774. {
  1775. const auto s = std::max (sA, sB);
  1776. env = s > env ? s : release * env + (1.0 - release) * s;
  1777. maxDet = std::max (maxDet, env);
  1778. const auto want = wanted (env, full);
  1779. gr = want > gr ? attack * gr + (1.0 - attack) * want : release * gr + (1.0 - release) * want;
  1780. gA = gB = (float) dbToGain (-gr);
  1781. maxGr = std::max (maxGr, gr);
  1782. }
  1783. else
  1784. {
  1785. const auto s = std::max (sA, sB);
  1786. const double ss[2] { link * s + (1.0 - link) * sA, link * s + (1.0 - link) * sB };
  1787. for (int c = 0; c < 2; ++c)
  1788. {
  1789. envCh[c] = ss[c] > envCh[c] ? ss[c] : release * envCh[c] + (1.0 - release) * ss[c];
  1790. maxDet = std::max (maxDet, envCh[c]);
  1791. const auto want = wanted (envCh[c], full);
  1792. grCh[c] = want > grCh[c] ? attack * grCh[c] + (1.0 - attack) * want : release * grCh[c] + (1.0 - release) * want;
  1793. maxGr = std::max (maxGr, grCh[c]);
  1794. }
  1795. gA = (float) dbToGain (-grCh[0]);
  1796. gB = (float) dbToGain (-grCh[1]);
  1797. }
  1798. if (listening)
  1799. {
  1800. a = listenF.process (da, 0);
  1801. b = listenF.process (db, 1);
  1802. if (lpOn) { a = listenLp.process (a, 0); b = listenLp.process (b, 1); }
  1803. }
  1804. else if (pro)
  1805. {
  1806. // round 165x: output = the pass-through + the link's change of the band (whole signal, above the
  1807. // split, or between the two handles)
  1808. double pa = da, pb = db, ba = da, bb = db;
  1809. if (! wide)
  1810. {
  1811. const auto la = split.process (da, 0), lb = split.process (db, 1);
  1812. ba = da - la;
  1813. bb = db - lb;
  1814. if (lpOn)
  1815. {
  1816. const auto ma = bandLp2.process (bandLp.process ((float) ba, 0), 0), mb = bandLp2.process (bandLp.process ((float) bb, 1), 1);
  1817. const auto ta = topHp2.process (topHp.process ((float) ba, 0), 0), tb = topHp2.process (topHp.process ((float) bb, 1), 1);
  1818. pa = la + ma + ta;
  1819. pb = lb + mb + tb;
  1820. ba = ma;
  1821. bb = mb;
  1822. }
  1823. }
  1824. double oa, ob;
  1825. pl.delta (gA, gB, ba, bb, oa, ob);
  1826. if (over)
  1827. {
  1828. // the change goes to the high rate as a band with a unit gain (the gain moves at the base rate)
  1829. bandBuf[0][(std::size_t) i] = (float) oa;
  1830. bandBuf[1][(std::size_t) i] = (float) ob;
  1831. gainBuf[0][(std::size_t) i] = gainBuf[1][(std::size_t) i] = 1.0f;
  1832. a = (float) pa; // (the pass-through; the oversampler adds the change)
  1833. b = (float) pb;
  1834. }
  1835. else
  1836. {
  1837. a = (float) (pa + oa);
  1838. b = (float) (pb + ob);
  1839. }
  1840. }
  1841. else if (over)
  1842. {
  1843. // output = input + band × (gain − 1): the band (whole signal, above the split, or between the two
  1844. // handles) and the gain go to the high rate below
  1845. float ba = da, bb = db;
  1846. if (! wide)
  1847. {
  1848. const auto la = split.process (da, 0), lb = split.process (db, 1);
  1849. ba = da - la;
  1850. bb = db - lb;
  1851. if (lpOn)
  1852. {
  1853. ba = bandLp2.process (bandLp.process (ba, 0), 0);
  1854. bb = bandLp2.process (bandLp.process (bb, 1), 1);
  1855. topHp2.process (topHp.process (da - la, 0), 0); // (kept running: switching back stays smooth)
  1856. topHp2.process (topHp.process (db - lb, 1), 1);
  1857. }
  1858. }
  1859. bandBuf[0][(std::size_t) i] = ba;
  1860. bandBuf[1][(std::size_t) i] = bb;
  1861. gainBuf[0][(std::size_t) i] = gA - 1.0f;
  1862. gainBuf[1][(std::size_t) i] = gB - 1.0f;
  1863. a = da;
  1864. b = db;
  1865. }
  1866. else if (wide)
  1867. {
  1868. a = da * gA;
  1869. b = db * gB;
  1870. }
  1871. else if (! lpOn)
  1872. {
  1873. // complementary split: low = 2nd-order low-pass, high = input − low (sums back exactly)
  1874. const auto la = split.process (da, 0), lb = split.process (db, 1);
  1875. a = la + (da - la) * gA;
  1876. b = lb + (db - lb) * gB;
  1877. }
  1878. else
  1879. {
  1880. // round 128: only the band between the two handles is turned down; above the low-pass it passes
  1881. // (the high part is split again at the low-pass by a Linkwitz-Riley pair: flat sum, no notch)
  1882. const auto la = split.process (da, 0), lb = split.process (db, 1);
  1883. const auto ha = da - la, hb = db - lb;
  1884. const auto ma = bandLp2.process (bandLp.process (ha, 0), 0), mb = bandLp2.process (bandLp.process (hb, 1), 1);
  1885. const auto ta = topHp2.process (topHp.process (ha, 0), 0), tb = topHp2.process (topHp.process (hb, 1), 1);
  1886. a = la + ma * gA + ta;
  1887. b = lb + mb * gB + tb;
  1888. }
  1889. l[i] = a * out;
  1890. r[i] = b * out;
  1891. }
  1892. if (over)
  1893. {
  1894. const auto m = n * osFactor;
  1895. for (int c = 0; c < 2; ++c)
  1896. {
  1897. auto* x = c == 0 ? l : r;
  1898. if (listening)
  1899. {
  1900. os[(std::size_t) c].process (x, n, [] (float v) noexcept { return v; });
  1901. continue;
  1902. }
  1903. auto& ub = upBand[(std::size_t) c];
  1904. auto& ug = upGain[(std::size_t) c];
  1905. int k = 0, j = 0, q = 0;
  1906. osBand[(std::size_t) c].process (bandBuf[(std::size_t) c].data(), n, [&] (float v) noexcept { ub[(std::size_t) std::min (k++, m - 1)] = v; return 0.0f; });
  1907. osGain[(std::size_t) c].process (gainBuf[(std::size_t) c].data(), n, [&] (float v) noexcept { ug[(std::size_t) std::min (j++, m - 1)] = v; return 0.0f; });
  1908. os[(std::size_t) c].process (x, n, [&] (float v) noexcept {
  1909. const auto at = (std::size_t) std::min (q++, m - 1);
  1910. return v + ub[at] * ug[at] * out;
  1911. });
  1912. }
  1913. }
  1914. meters.set ((float) maxGr, 0.0f, inPeak, Meters::peak (l, r, n));
  1915. meters.setDetector ((float) gainToDb (maxDet)); // round 147: the energy the threshold is compared with
  1916. scL = scR = nullptr;
  1917. }
  1918. void setDelay (double ms) noexcept
  1919. {
  1920. delayMs = ms;
  1921. const auto len = (int) std::lround (ms * 0.001 * sr);
  1922. delay.setLength (len);
  1923. scDelay.setLength (len);
  1924. }
  1925. static constexpr int maxBlock = 1024, maxFactor = 8;
  1926. int osFactor = 1;
  1927. std::array<Oversampler, 2> os, osBand, osGain;
  1928. std::array<std::array<float, maxBlock>, 2> bandBuf {}, gainBuf {};
  1929. std::array<std::array<float, maxBlock * maxFactor>, 2> upBand {}, upGain {};
  1930. double sr = 44100.0;
  1931. double primedMs = 2.0, delayMs = 2.0;
  1932. Delay2<4096> delay, scDelay;
  1933. vmatch::deesser::DeEsser classicEngine;
  1934. vmatch::vocalds::DeEsser vocalEngine;
  1935. bool vocalOn = false;
  1936. bool classicOn = false; // round 165: the classic voicing has no latency
  1937. Biquad detect, detectLp, listenF, listenLp, split, bandLp, bandLp2, topHp, topHp2;
  1938. // round 190dm: Air and Mix — the shelf, the block's dry input and the dry line (latency ≤ 8191 samples)
  1939. static constexpr int dryLen = 8192;
  1940. Biquad airShelf;
  1941. double airDesignDb = -1.0, airDesignRate = 0.0;
  1942. std::array<std::array<float, maxBlock>, 2> dryBuf {};
  1943. std::array<std::array<float, dryLen>, 2> dryLine {};
  1944. int dryPos = 0;
  1945. double env = 0.0, gr = 0.0, fullEnv = 0.0;
  1946. double envCh[2] {}, grCh[2] {};
  1947. const float* scL = nullptr;
  1948. const float* scR = nullptr;
  1949. Meters meters;
  1950. };
  1951. } // namespace
  1952. } // namespace hush
  1953. // ================================================================== Aether
  1954. namespace kiln
  1955. {
  1956. double trimDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  1957. double mainDb (float v) noexcept { return lin (v, -24.0, 24.0); }
  1958. double driveDb (float v) noexcept { return lin (v, 0.0, 18.0); }
  1959. int consoleStep (float v) noexcept { return std::clamp ((int) std::lround (v * 5.0f), 0, 5); }
  1960. float consoleValue (int step) noexcept { return (float) std::clamp (step, 0, 5) / 5.0f; }
  1961. int groupOf (float v) noexcept { return std::clamp ((int) std::lround (v * 8.0f), 0, 8); }
  1962. const char* consoleName (int step) noexcept
  1963. {
  1964. static const char* names[6] { "British E", "British G", "American A", "British N", "Y", "Tube" };
  1965. return names[std::clamp (step, 0, 5)];
  1966. }
  1967. std::vector<std::pair<int, float>> linkedEdit (const EffectParams& p, int param, float value)
  1968. {
  1969. std::vector<std::pair<int, float>> edits { { param, value } };
  1970. if (param == input && p[ioLink] >= 0.5f) // both trims are -18 .. +18 dB, linear in the value: the same step down
  1971. edits.push_back ({ output, std::clamp (p[output] - (value - p[input]), 0.0f, 1.0f) });
  1972. return edits;
  1973. }
  1974. } // namespace kiln
  1975. namespace aether
  1976. {
  1977. double airDb (float v) noexcept { return lin (v, 0.0, 15.0); }
  1978. double airHz (float v) noexcept { return logMap (v, 5000.0, 20000.0); }
  1979. double exciterHz (float v) noexcept { return logMap (v, 1000.0, 12000.0); }
  1980. namespace
  1981. {
  1982. // ---- the dynamic air model (round 124) ------------------------------------------------------------------------------
  1983. // Black-box measurements of the official Fresh Air trial (docs/analise/REA_rodada124.md §3), our own code:
  1984. // y = s(mid, high) * x + kMid(mid) * gMid(t) * HPmid(x) + kHigh(high) * gHigh(t) * HPhigh(x)
  1985. // HPmid / HPhigh: RBJ 2nd-order high-passes (2993 Hz, Q 0.629 / 9000 Hz, Q 0.707; the same analog corners at 44.1
  1986. // and 48 kHz); s, kMid, kHigh: measured on a 0.025 grid (small-signal transfer functions, residual < 0.4 %).
  1987. // gMid: a linked peak detector (instant attack, 5 ms hold, 96 ms release) of the band, 4:1 above -30.1 dBFS,
  1988. // released through a 7.7 ms one-pole. gHigh: two 4:1 stages — a peak detector with 2-pole gain smoothing, then a
  1989. // feedback stage on the band after the first one, whose threshold rises as High air goes down (T + c (1 - high)^2).
  1990. namespace dyn
  1991. {
  1992. constexpr double gridMidS[41] { 1.00237, 1.02960, 1.04368, 1.05483, 1.06431, 1.07263, 1.08006, 1.08676, 1.09284, 1.09838, 1.10343,
  1993. 1.10804, 1.11224, 1.11606, 1.11952, 1.12264, 1.12542, 1.12788, 1.13004, 1.13189, 1.13345, 1.13472, 1.13571, 1.13642, 1.13685,
  1994. 1.13700, 1.13689, 1.13650, 1.13584, 1.13492, 1.13373, 1.13227, 1.13055, 1.12855, 1.12629, 1.12376, 1.12097, 1.11790, 1.11456,
  1995. 1.11095, 1.10707 };
  1996. constexpr double gridMidK[41] { 0.00000, 0.13781, 0.22533, 0.30473, 0.38070, 0.45506, 0.52874, 0.60230, 0.67608, 0.75035, 0.82529,
  1997. 0.90102, 0.97767, 1.05531, 1.13403, 1.21388, 1.29491, 1.37717, 1.46071, 1.54556, 1.63175, 1.71932, 1.80829, 1.89870, 1.99056,
  1998. 2.08391, 2.17877, 2.27515, 2.37309, 2.47260, 2.57371, 2.67643, 2.78079, 2.88681, 2.99451, 3.10390, 3.21501, 3.32786, 3.44246,
  1999. 3.55884, 3.67701 };
  2000. constexpr double gridHighS[41] { 1.00237, 1.03339, 1.04999, 1.06349, 1.07527, 1.08587, 1.09559, 1.10462, 1.11306, 1.12100, 1.12851,
  2001. 1.13562, 1.14238, 1.14881, 1.15493, 1.16077, 1.16634, 1.17166, 1.17673, 1.18156, 1.18616, 1.19055, 1.19472, 1.19867, 1.20242,
  2002. 1.20597, 1.20932, 1.21247, 1.21543, 1.21820, 1.22077, 1.22316, 1.22535, 1.22736, 1.22918, 1.23082, 1.23227, 1.23353, 1.23461,
  2003. 1.23550, 1.23620 };
  2004. constexpr double gridHighK[41] { 0.00000, 0.26995, 0.45609, 0.63358, 0.81042, 0.98969, 1.17298, 1.36128, 1.55524, 1.75537, 1.96206,
  2005. 2.17564, 2.39639, 2.62458, 2.86045, 3.10422, 3.35611, 3.61634, 3.88510, 4.16260, 4.44906, 4.74466, 5.04961, 5.36411, 5.68837,
  2006. 6.02260, 6.36699, 6.72176, 7.08712, 7.46329, 7.85048, 8.24890, 8.65879, 9.08036, 9.51384, 9.95946, 10.41747, 10.88807, 11.37154,
  2007. 11.86810, 12.37801 };
  2008. inline double grid (const double (&t)[41], double v) noexcept
  2009. {
  2010. const auto x = std::clamp (v, 0.0, 1.0) * 40.0;
  2011. const auto i = std::min (39, (int) x);
  2012. return t[i] + (t[i + 1] - t[i]) * (x - i);
  2013. }
  2014. // the dry gain with both bands at 0
  2015. constexpr double baseGain = 1.00237;
  2016. // the fitted dynamics (REA_rodada124 §3)
  2017. struct Tuning
  2018. {
  2019. double midThreshold = -30.13, midHoldMs = 5.0, midReleaseMs = 96.03, midGainReleaseMs = 7.706;
  2020. double highThreshold = -38.03, highReleaseMs = 0.5354, highGainAttackMs = 2.1428, highGainReleaseMs = 55.52;
  2021. double high2ReleaseMs = 0.0190, high2GainAttackMs = 56.73, high2GainReleaseMs = 53.45, high2Slope = 8.257;
  2022. double high2Threshold = -41.13, high2Curve = 15.585;
  2023. };
  2024. class Engine
  2025. {
  2026. public:
  2027. void prepare (double rate) noexcept
  2028. {
  2029. sr = rate;
  2030. mid.hp (rate, 2993.0, 0.6291);
  2031. high.hp (rate, 9000.0, 0.7069);
  2032. reset();
  2033. }
  2034. void reset() noexcept
  2035. {
  2036. mid.reset();
  2037. high.reset();
  2038. envM = holdM = gM = envH = g1a = g1b = env2 = g2a = g2b = 0.0;
  2039. }
  2040. // processes in place; `out` is the output gain (linear)
  2041. void process (float* l, float* r, int n, double midAmount, double highAmount, double out) noexcept
  2042. {
  2043. const Tuning t;
  2044. const auto c = [this] (double ms) { return ms <= 0.0 ? 0.0 : std::exp (-1000.0 / (ms * sr)); };
  2045. const auto s = (grid (gridMidS, midAmount) + grid (gridHighS, highAmount) - baseGain) * out;
  2046. const auto km = grid (gridMidK, midAmount) * out, kh = grid (gridHighK, highAmount) * out;
  2047. const auto mRel = c (t.midReleaseMs), mGr = c (t.midGainReleaseMs), mHold = t.midHoldMs * sr / 1000.0;
  2048. const auto hRel = c (t.highReleaseMs), h1a = c (t.highGainAttackMs), h1r = c (t.highGainReleaseMs);
  2049. const auto h2Rel = c (t.high2ReleaseMs), h2a = c (t.high2GainAttackMs), h2r = c (t.high2GainReleaseMs);
  2050. const auto t2 = t.high2Threshold + t.high2Curve * (1.0 - highAmount) * (1.0 - highAmount);
  2051. for (int i = 0; i < n; ++i)
  2052. {
  2053. const double xl = l[i], xr = r[i];
  2054. const double ml = mid.process ((float) xl, 0), mr = mid.process ((float) xr, 1);
  2055. const double hl = high.process ((float) xl, 0), hr = high.process ((float) xr, 1);
  2056. // mid: peak with hold, 4:1
  2057. const auto pm = std::max (std::fabs (ml), std::fabs (mr));
  2058. if (pm >= envM) { envM = pm; holdM = mHold; }
  2059. else if (holdM > 0.0) holdM -= 1.0;
  2060. else envM = pm + (envM - pm) * mRel;
  2061. const auto gmTarget = -0.75 * std::max (0.0, 20.0 * std::log10 (envM + 1e-30) - t.midThreshold);
  2062. gM = gmTarget < gM ? gmTarget : gmTarget + (gM - gmTarget) * mGr;
  2063. // high, stage 1: peak, 4:1, 2-pole gain smoothing
  2064. const auto ph = std::max (std::fabs (hl), std::fabs (hr));
  2065. envH = ph > envH ? ph : ph + (envH - ph) * hRel;
  2066. const auto g1Target = -0.75 * std::max (0.0, 20.0 * std::log10 (envH + 1e-30) - t.highThreshold);
  2067. const auto k1 = g1Target < g1b ? h1a : h1r;
  2068. g1a = g1Target + (g1a - g1Target) * k1;
  2069. g1b = g1a + (g1b - g1a) * k1;
  2070. const auto g1 = std::pow (10.0, g1b / 20.0);
  2071. // high, stage 2: feedback (detector after both stages), peak, 2-pole gain smoothing
  2072. const auto g2 = std::pow (10.0, g2b / 20.0);
  2073. const auto p2 = ph * g1 * g2;
  2074. env2 = p2 >= env2 ? p2 : p2 + (env2 - p2) * h2Rel;
  2075. const auto g2Target = -t.high2Slope * std::max (0.0, 20.0 * std::log10 (env2 + 1e-30) - t2);
  2076. const auto k2 = g2Target < g2b ? h2a : h2r;
  2077. g2a = g2Target + (g2a - g2Target) * k2;
  2078. g2b = g2a + (g2b - g2a) * k2;
  2079. const auto gm = km * std::pow (10.0, gM / 20.0), gh = kh * g1 * g2;
  2080. l[i] = (float) (s * xl + gm * ml + gh * hl);
  2081. r[i] = (float) (s * xr + gm * mr + gh * hr);
  2082. }
  2083. }
  2084. float reductionDb() const noexcept { return (float) std::min (gM, g1b + g2b); }
  2085. private:
  2086. struct Hp2 : Biquad
  2087. {
  2088. void hp (double rate, double fc, double q) noexcept
  2089. {
  2090. const auto w = 2.0 * 3.14159265358979323846 * fc / rate, a = std::sin (w) / (2.0 * q), cw = std::cos (w);
  2091. set ((1.0 + cw) / 2.0, -(1.0 + cw), (1.0 + cw) / 2.0, 1.0 + a, -2.0 * cw, 1.0 - a);
  2092. }
  2093. double process (float x, int ch) noexcept
  2094. {
  2095. const auto y = b0 * x + z1[ch];
  2096. z1[ch] = b1 * x - a1 * y + z2[ch];
  2097. z2[ch] = b2 * x - a2 * y;
  2098. return y;
  2099. }
  2100. };
  2101. double sr = 48000.0;
  2102. Hp2 mid, high;
  2103. double envM = 0, holdM = 0, gM = 0, envH = 0, g1a = 0, g1b = 0, env2 = 0, g2a = 0, g2b = 0;
  2104. };
  2105. } // namespace dyn
  2106. class Air final : public Effect
  2107. {
  2108. public:
  2109. // `initial` (may be null): the slot's parameters, so the model (the exciter path's latency) is known before the
  2110. // first block (issue #113)
  2111. explicit Air (const EffectParams* initial = nullptr)
  2112. {
  2113. for (auto& o : os)
  2114. o.setFactor (4);
  2115. if (initial != nullptr)
  2116. dynamicMode = (*initial)[dynamic] >= 0.5f;
  2117. }
  2118. void prepare (double rate) noexcept override
  2119. {
  2120. sr = rate;
  2121. dry.setLength (Oversampler::latency());
  2122. dynEngine.prepare (rate);
  2123. reset();
  2124. }
  2125. void reset() noexcept override
  2126. {
  2127. for (auto& o : os)
  2128. o.reset();
  2129. dry.reset();
  2130. shelf.reset();
  2131. pre.reset();
  2132. post.reset();
  2133. dynEngine.reset();
  2134. }
  2135. // the dynamic model has no latency (as the measured reference); the exciter path is oversampled
  2136. int latencySamples() const noexcept override { return dynamicMode ? 0 : Oversampler::latency(); }
  2137. float controlOutput (int k) const noexcept override { return meters.read (k); }
  2138. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  2139. {
  2140. const bool dynamicNow = p[dynamic] >= 0.5f;
  2141. if (dynamicNow != dynamicMode)
  2142. {
  2143. dynamicMode = dynamicNow;
  2144. reset();
  2145. }
  2146. for (int off = 0; off < n; off += maxBlock)
  2147. if (dynamicMode)
  2148. dynamicChunk (l + off, r + off, std::min (maxBlock, n - off), p);
  2149. else
  2150. chunk (l + off, r + off, std::min (maxBlock, n - off), p);
  2151. }
  2152. private:
  2153. void dynamicChunk (float* l, float* r, int n, const EffectParams& p) noexcept
  2154. {
  2155. const auto inPeak = Meters::peak (l, r, n);
  2156. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  2157. if (mx < 1.0f)
  2158. for (int i = 0; i < n; ++i)
  2159. hl[(std::size_t) i] = l[i], hr[(std::size_t) i] = r[i];
  2160. dynEngine.process (l, r, n, std::clamp (p[midAir], 0.0f, 1.0f), std::clamp (p[highAir], 0.0f, 1.0f), dbToGain (lin (p[output], -12.0, 12.0)));
  2161. if (mx < 1.0f)
  2162. for (int i = 0; i < n; ++i)
  2163. {
  2164. l[i] = hl[(std::size_t) i] + (l[i] - hl[(std::size_t) i]) * mx;
  2165. r[i] = hr[(std::size_t) i] + (r[i] - hr[(std::size_t) i]) * mx;
  2166. }
  2167. meters.set (0.0f, -dynEngine.reductionDb(), inPeak, Meters::peak (l, r, n));
  2168. }
  2169. void chunk (float* l, float* r, int n, const EffectParams& p) noexcept
  2170. {
  2171. const auto inPeak = Meters::peak (l, r, n);
  2172. shelf.shelf (sr, std::min (airHz (p[airFreq]), sr * 0.45), airDb (p[air]), true, 0.6);
  2173. const auto ex = std::min (exciterHz (p[exciterFreq]), sr * 0.45);
  2174. pre.filter (sr, 1, ex, 0.70710678);
  2175. post.filter (sr, 1, ex, 0.70710678);
  2176. const auto amount = 2.0f * std::clamp (p[harmonics], 0.0f, 1.0f);
  2177. const auto odd = std::clamp (p[character], 0.0f, 1.0f);
  2178. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  2179. const auto out = (float) dbToGain (lin (p[output], -12.0, 12.0));
  2180. // harmonic generator (DAFX ch. 4, exciters): only the band above the exciter frequency is shaped, at 4x;
  2181. // an even part (x² -like, 2nd harmonic) and an odd part (tanh − x, 3rd harmonic), blended by `character`
  2182. auto gen = [odd] (float x) noexcept
  2183. {
  2184. const auto k = 4.0f * x;
  2185. const auto even = (std::sqrt (1.0f + k * k) - 1.0f) * 0.25f;
  2186. const auto oddPart = std::tanh (3.0f * x) / 3.0f - x;
  2187. return (1.0f - odd) * even - odd * oddPart * 3.0f;
  2188. };
  2189. for (int i = 0; i < n; ++i)
  2190. {
  2191. hl[(std::size_t) i] = pre.process (l[i], 0);
  2192. hr[(std::size_t) i] = pre.process (r[i], 1);
  2193. }
  2194. if (amount > 0.0f)
  2195. {
  2196. os[0].process (hl.data(), n, gen);
  2197. os[1].process (hr.data(), n, gen);
  2198. }
  2199. else
  2200. {
  2201. // keep the oversamplers' state moving so turning the harmonics up does not click with stale history
  2202. auto zero = [] (float) noexcept { return 0.0f; };
  2203. os[0].process (hl.data(), n, zero);
  2204. os[1].process (hr.data(), n, zero);
  2205. }
  2206. for (int i = 0; i < n; ++i)
  2207. {
  2208. float a = l[i], b = r[i];
  2209. dry.process (a, b);
  2210. const auto wa = shelf.process (a, 0) + amount * post.process (hl[(std::size_t) i], 0);
  2211. const auto wb = shelf.process (b, 1) + amount * post.process (hr[(std::size_t) i], 1);
  2212. l[i] = (a + (wa - a) * mx) * out;
  2213. r[i] = (b + (wb - b) * mx) * out;
  2214. }
  2215. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  2216. }
  2217. static constexpr int maxBlock = 2048;
  2218. double sr = 44100.0;
  2219. std::array<Oversampler, 2> os;
  2220. Delay2<256> dry;
  2221. Biquad shelf, pre, post;
  2222. dyn::Engine dynEngine;
  2223. bool dynamicMode = true; // issue #77: the default model (its latency is known before the first block)
  2224. std::array<float, maxBlock> hl {}, hr {};
  2225. Meters meters;
  2226. };
  2227. } // namespace
  2228. } // namespace aether
  2229. // ================================================================== Ember
  2230. namespace ember
  2231. {
  2232. Style styleOf (float v) noexcept { return (Style) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * (styles - 1)), 0, styles - 1); }
  2233. double driveDb (float v) noexcept { return lin (v, 0.0, 36.0); }
  2234. double toneTilt (float v) noexcept { return lin (v, -1.0, 1.0); }
  2235. double lowCutHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 20.0, 500.0); }
  2236. double highCutHz (float v) noexcept { return v >= 1.0f ? 0.0 : logMap (v, 2000.0, 20000.0); }
  2237. int oversampling (float v) noexcept { return v < 0.25f ? 2 : (v < 0.75f ? 4 : 8); }
  2238. int voiceIndex (float v) noexcept { return std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * voices), 0, voices); }
  2239. const char* voiceName (int k) noexcept
  2240. {
  2241. static const char* const names[voices + 1] { "Off", "Tape pre", "Console", "Grit", "Triode", "Pentode" };
  2242. return names[std::clamp (k, 0, voices)];
  2243. }
  2244. Style voiceStyle (int k) noexcept
  2245. {
  2246. static const Style st[voices + 1] { Style::tube, Style::tubeBiased, Style::console, Style::transistor, Style::tubeBiased, Style::tape };
  2247. return st[std::clamp (k, 0, voices)];
  2248. }
  2249. double lowCutWideHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 20.0, 1000.0); }
  2250. double highCutWideHz (float v) noexcept { return v >= 1.0f ? 0.0 : logMap (v, 1000.0, 20000.0); }
  2251. double driveSteps (float v) noexcept { return driveDb (v) / 3.6; }
  2252. int bandCount (float v) noexcept { return std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 3.0f), 0, 3) + 1; }
  2253. double crossoverHz (float v) noexcept { return logMap (v, 30.0, 18000.0); }
  2254. float crossoverValue (double hz) noexcept { return (float) std::clamp (std::log (hz / 30.0) / std::log (600.0), 0.0, 1.0); }
  2255. int slopeDbOct (float v) noexcept { return v < 0.25f ? 12 : (v < 0.75f ? 24 : 48); }
  2256. double bandLevelDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  2257. BandState bandState (float v) noexcept { return (BandState) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 2.0f), 0, 2); }
  2258. double inputDb (float v) noexcept { return lin (v, -24.0, 24.0); }
  2259. double toneDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  2260. double feedbackAmount (float v) noexcept { return lin (v, 0.0, 1.0); }
  2261. double feedbackHz (float v) noexcept { return logMap (v, 10.0, 1000.0); }
  2262. double dynamicsAmount (float v) noexcept { return lin (v, -1.0, 1.0); }
  2263. double panOf (float v) noexcept { return std::clamp ((double) v * 2.0 - 1.0, -1.0, 1.0); }
  2264. std::pair<float, float> panScales (double pan) noexcept
  2265. {
  2266. pan = std::clamp (pan, -1.0, 1.0);
  2267. return { (float) (pan > 0.0 ? 1.0 - pan : 1.0), (float) (pan < 0.0 ? 1.0 + pan : 1.0) };
  2268. }
  2269. bool midSide (const EffectParams& p) noexcept { return on (p[channelMode]); }
  2270. int bandParam (int band, int field) noexcept
  2271. {
  2272. if (band <= 0)
  2273. {
  2274. static constexpr int first[] { style, drive, band1Mix, band1Level };
  2275. return first[std::clamp (field, 0, 3)];
  2276. }
  2277. if (band < firstBands)
  2278. return band2Style + (band - 1) * 4 + std::clamp (field, 0, 3);
  2279. return band5Style + (std::min (band, maxBands - 1) - firstBands) * 4 + std::clamp (field, 0, 3); // round 150
  2280. }
  2281. int activeBands (const EffectParams& p) noexcept
  2282. {
  2283. const auto more = std::clamp ((int) std::lround (std::clamp (p[bandsMore], 0.0f, 1.0f) * 2.0f), 0, 2);
  2284. return more > 0 ? firstBands + more : bandCount (p[bands]);
  2285. }
  2286. std::pair<float, float> bandCountValues (int n) noexcept
  2287. {
  2288. n = std::clamp (n, 1, maxBands);
  2289. return { (float) (std::min (n, firstBands) - 1) / 3.0f, n <= firstBands ? 0.0f : (float) (n - firstBands) / 2.0f };
  2290. }
  2291. int characterIndex (float v) noexcept { return std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * characters), 0, characters); }
  2292. // ---- round 152 (REA_rodada152b): own crossover slopes, linear phase, modulation, the effect characters' law ----
  2293. namespace
  2294. {
  2295. // a knob law given as a table at equal steps of the value (the reference's value texts, read black-box)
  2296. template <std::size_t N>
  2297. double tableLaw (const double (&t)[N], float v) noexcept
  2298. {
  2299. const auto x = std::clamp ((double) v, 0.0, 1.0) * (double) (N - 1);
  2300. const auto i = std::min ((std::size_t) x, N - 2);
  2301. return t[i] + (t[i + 1] - t[i]) * (x - (double) i);
  2302. }
  2303. template <std::size_t N>
  2304. float tableInverse (const double (&t)[N], double y) noexcept
  2305. {
  2306. if (y <= t[0]) return 0.0f;
  2307. for (std::size_t i = 0; i + 1 < N; ++i)
  2308. if (y <= t[i + 1])
  2309. return (float) (((double) i + (y - t[i]) / std::max (t[i + 1] - t[i], 1e-12)) / (double) (N - 1));
  2310. return 1.0f;
  2311. }
  2312. // XLFO Frequency (21 texts, 0.05 apart) [EXEC]
  2313. constexpr double rateTable[21] { 0.02, 0.145, 0.27, 0.395, 0.52, 1.02, 1.52, 2.02, 2.52, 3.02, 3.52, 6.395, 9.27, 12.145,
  2314. 15.019, 32.519, 50.018, 100.02, 150.01, 250.01, 500.0 };
  2315. // Slot Level (21 texts) [EXEC]
  2316. constexpr double levelTable[21] { 0.0, 0.001, 0.002, 0.018, 0.034, 0.05, 0.072, 0.094, 0.116, 0.138, 0.16, 0.21, 0.26, 0.31,
  2317. 0.36, 0.498, 0.635, 0.773, 0.91, 0.955, 1.0 };
  2318. // EF Attack / Release (41 texts, 0.025 apart) [EXEC]
  2319. constexpr double attackTable[41] { 1, 4.33, 7.66, 10.99, 14.32, 17.65, 20.98, 25.97, 30.97, 34.81, 38.65, 42.5, 46.34, 50.18,
  2320. 56.66, 63.79, 70.93, 78.42, 85.92, 93.41, 100.9, 108.4, 115.9, 123.4, 130.9, 143.4, 155.8,
  2321. 168.3, 185.1, 202.9, 220.8, 253.2, 285.7, 318.2, 350.6, 405.6, 460.5, 560.4, 660.3, 793.5, 1000 };
  2322. constexpr double releaseTable[41] { 50, 66.5, 83, 99.5, 116, 132.5, 149, 173.7, 198.5, 217.5, 236.6, 255.6, 274.7, 293.7,
  2323. 325.8, 361.1, 396.5, 433.6, 470.7, 507.9, 545, 582.1, 619.2, 656.4, 693.5, 755.4, 817.2,
  2324. 879.1, 962.2, 1051, 1139, 1300, 1461, 1622, 1782, 2055, 2327, 2822, 3317, 3977, 5000 };
  2325. } // namespace
  2326. int ownSlopeDbOct (float v) noexcept
  2327. {
  2328. static constexpr int s[ownSlopes + 1] { 0, 6, 12, 24, 36, 48 };
  2329. return s[std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * ownSlopes), 0, ownSlopes)];
  2330. }
  2331. float ownSlopeValue (int dbOct) noexcept
  2332. {
  2333. static constexpr int s[ownSlopes] { 6, 12, 24, 36, 48 };
  2334. for (int k = 0; k < ownSlopes; ++k)
  2335. if (dbOct <= s[k]) return (float) (k + 1) / (float) ownSlopes;
  2336. return 1.0f;
  2337. }
  2338. int crossoverSlopeDbOct (const EffectParams& p, int k) noexcept
  2339. {
  2340. const auto own = ownSlopeDbOct (p[(std::size_t) (crossSlope1 + std::clamp (k, 0, maxBands - 2))]);
  2341. return own > 0 ? own : slopeDbOct (p[xoverSlope]);
  2342. }
  2343. bool linearPhase (const EffectParams& p) noexcept { return on (p[processing]); }
  2344. int linearLatency (double sampleRate) noexcept { return dyn::prism::linearLatency (sampleRate); }
  2345. double linearLow (double hz, double crossoverHz, int slopeDbOct) noexcept
  2346. {
  2347. const auto x = std::max (hz, 0.0) / std::max (crossoverHz, 1.0);
  2348. return 1.0 / (1.0 + std::pow (x, (double) std::max (slopeDbOct, 6) / 6.0));
  2349. }
  2350. ModSource modSourceOf (float v) noexcept { return (ModSource) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * (modSources - 1)), 0, modSources - 1); }
  2351. int modTargetOf (float v) noexcept { return std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * (modTargets - 1)), 0, modTargets - 1); }
  2352. const char* modTargetName (int t) noexcept
  2353. {
  2354. static const char* const names[modTargets] { "None", "Input gain", "Output gain", "Mix",
  2355. "B1 drive", "B2 drive", "B3 drive", "B4 drive", "B5 drive", "B6 drive",
  2356. "B1 level", "B2 level", "B3 level", "B4 level", "B5 level", "B6 level",
  2357. "B1 mix", "B2 mix", "B3 mix", "B4 mix", "B5 mix", "B6 mix",
  2358. "Crossover 1", "Crossover 2", "Crossover 3", "Crossover 4", "Crossover 5" };
  2359. return names[std::clamp (t, 0, modTargets - 1)];
  2360. }
  2361. int modTargetParam (int t) noexcept
  2362. {
  2363. if (t <= 0 || t >= modTargets) return -1;
  2364. if (t == 1) return inputGain;
  2365. if (t == 2) return output;
  2366. if (t == 3) return mix;
  2367. if (t < 10) return bandParam (t - 4, 1);
  2368. if (t < 16) return bandParam (t - 10, 3);
  2369. if (t < 22) return bandParam (t - 16, 2);
  2370. return crossoverParam (t - 22);
  2371. }
  2372. double modDelta (int t, double units) noexcept
  2373. {
  2374. // the reference's target ranges (its parameters' value texts) in Ember's parameter units [EXEC: a slot of level L
  2375. // moves its target by L × the source in the target's own 0 … 1 range — e.g. B1 Output Level: 72 dB × L]
  2376. if (t <= 0 || t >= modTargets) return 0.0;
  2377. if (t == 1) return units * 72.0 / 48.0; // ±36 dB → Ember's input ±24 dB
  2378. if (t == 2) return units * 72.0 / 36.0; // ±36 dB → Ember's output ±18 dB
  2379. if (t == 3 || (t >= 16 && t < 22)) return units; // 0 … 100 %
  2380. if (t < 10) return units * 30.0 / 36.0; // 0 … 100 % = 0 … 30 dB of Ember's 0 … 36 dB drive
  2381. if (t < 16) return units * 72.0 / 36.0; // −∞ … +36 dB (0.5 = 0 dB) → Ember's ±18 dB
  2382. return units * std::log (450.0) / std::log (600.0); // 40 Hz … 18 kHz → Ember's 30 Hz … 18 kHz (log)
  2383. }
  2384. double modAmount (float v) noexcept
  2385. {
  2386. const auto d = 2.0 * (double) std::clamp (v, 0.0f, 1.0f) - 1.0;
  2387. const auto m = tableLaw (levelTable, (float) std::abs (d));
  2388. return d < 0.0 ? -m : m;
  2389. }
  2390. float modAmountValue (double level) noexcept
  2391. {
  2392. const auto m = tableInverse (levelTable, std::min (std::abs (level), 1.0));
  2393. return (float) (0.5 + (level < 0.0 ? -0.5 : 0.5) * (double) m);
  2394. }
  2395. double lfoRateHz (float v) noexcept { return tableLaw (rateTable, v); }
  2396. float lfoRateValue (double hz) noexcept { return tableInverse (rateTable, hz); }
  2397. LfoShape lfoShapeOf (float v) noexcept { return (LfoShape) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * (lfoShapes - 1)), 0, lfoShapes - 1); }
  2398. double lfoValue (LfoShape s, double phase) noexcept
  2399. {
  2400. // the reference's step LFO with two steps (+1, −1): each step glides from the other value over the whole step with
  2401. // its glide function (linear, squared, square root, sine), or jumps (square) [EXEC: the four functions measured]
  2402. phase -= std::floor (phase);
  2403. const bool first = phase < 0.5;
  2404. const auto u = (first ? phase : phase - 0.5) * 2.0;
  2405. if (s == LfoShape::square) return first ? 1.0 : -1.0;
  2406. double f = u;
  2407. switch (s)
  2408. {
  2409. case LfoShape::sine: f = 0.5 - 0.5 * std::cos (pi * u); break;
  2410. case LfoShape::easeIn: f = u * u; break;
  2411. case LfoShape::easeOut: f = std::sqrt (u); break;
  2412. default: break;
  2413. }
  2414. const auto from = first ? -1.0 : 1.0;
  2415. return from + (-2.0 * from) * f;
  2416. }
  2417. double envAttackMs (float v) noexcept { return tableLaw (attackTable, v); }
  2418. double envReleaseMs (float v) noexcept { return tableLaw (releaseTable, v); }
  2419. double envSource (double followerDb) noexcept { return std::max (0.0, (followerDb + 60.7) / 30.0); }
  2420. bool modulated (const EffectParams& p) noexcept
  2421. {
  2422. for (int k = 0; k < modSlots; ++k)
  2423. if (modSourceOf (p[(std::size_t) (mod1Source + 3 * k)]) != ModSource::off
  2424. && modTargetOf (p[(std::size_t) (mod1Target + 3 * k)]) != 0 && std::abs (modAmount (p[(std::size_t) (mod1Amount + 3 * k)])) > 1e-9)
  2425. return true;
  2426. return false;
  2427. }
  2428. bool measuredEffects (const EffectParams& p) noexcept { return on (p[effectLaw]); }
  2429. std::vector<std::pair<int, float>> linkedEdit (const EffectParams& p, int param, float value)
  2430. {
  2431. std::vector<std::pair<int, float>> edits { { param, value } };
  2432. if (param == classicDrive && on (p[classicAuto]))
  2433. edits.push_back ({ classicOutput, emberclassic::autoOutput (p[classicOutput], p[classicDrive], value) });
  2434. return edits;
  2435. }
  2436. double collapseRatio (double referenceDrive) noexcept { return std::exp2 (-2.0 * std::clamp (referenceDrive, 0.0, 1.0)); }
  2437. float shape (Style s, float x) noexcept
  2438. {
  2439. switch (s)
  2440. {
  2441. case Style::tube:
  2442. {
  2443. // round 170c: a biased tanh with its knee at 2 (6 dB more headroom) and a milder bias (0.15): at +10 dB of
  2444. // drive a −12 dBFS tone gets 3 % THD with the 2nd harmonic leading (was 12 %), the 3 kHz / 50 Hz
  2445. // intermodulation drops from −6 to −15 dB (the "garbly" sound of a beat through it), unit slope at 0
  2446. constexpr float bias = 0.15f, knee = 2.0f;
  2447. const float tb = std::tanh (bias);
  2448. return knee * (std::tanh (x / knee + bias) - tb) / (1.0f - tb * tb);
  2449. }
  2450. case Style::tubeBiased:
  2451. {
  2452. // asymmetric: a biased tanh (even and odd harmonics, like a single-ended triode stage), unit slope at 0
  2453. constexpr float bias = 0.35f;
  2454. const float tb = std::tanh (bias);
  2455. return (std::tanh (x + bias) - tb) / (1.0f - tb * tb);
  2456. }
  2457. case Style::tape: return x / std::sqrt (1.0f + x * x); // gentle symmetric (odd) saturation
  2458. case Style::transistor:
  2459. {
  2460. const auto a = std::abs (x);
  2461. const auto a2 = a * a;
  2462. return x / std::sqrt (std::sqrt (1.0f + a2 * a2)); // harder knee: x / (1 + x⁴)^¼
  2463. }
  2464. case Style::console:
  2465. {
  2466. // round 135: a softer, mostly symmetric knee with a little even content (x / (1 + |x|^2.5)^0.4 + a small bias)
  2467. constexpr float bias = 0.12f;
  2468. auto f = [] (float u) { const auto a = std::abs (u); return u / std::pow (1.0f + std::pow (a, 2.5f), 0.4f); };
  2469. const auto fb = f (bias);
  2470. return f (x + bias) - fb;
  2471. }
  2472. case Style::fuzz:
  2473. default:
  2474. if (x >= 0.0f)
  2475. {
  2476. const auto y = x / 0.6f, y2 = y * y, y4 = y2 * y2;
  2477. return 0.6f * y / std::sqrt (std::sqrt (std::sqrt (1.0f + y4 * y4))); // near-hard positive clip at 0.6
  2478. }
  2479. return std::tanh (1.5f * x) / 1.5f;
  2480. }
  2481. }
  2482. // ---- round 150: the measured characters (REA_rodada150b) ----
  2483. namespace
  2484. {
  2485. struct CharacterData
  2486. {
  2487. const char* name;
  2488. int family; // 0 soft knee u / (1 + |u|^p)^(1/p) around a bias, 1 sine fold, 2 full-wave rectifier
  2489. float p, bias;
  2490. float driveDb[6], gainDb[6]; // at the reference's drive 0, 20, 40, 60, 80, 100 %: the curve's input / output gain
  2491. int highPasses, lowPasses; // identical cookbook sections (n × the same filter)
  2492. float hpHz, hpQ, lpHz, lpQ;
  2493. float bellHz[3], bellDb[3], bellQ[3];
  2494. float eqDb; // the EQ's gain (0 dB at 1 kHz overall)
  2495. };
  2496. constexpr CharacterData characterTable[characters] {
  2497. #include "core/EmberCharacters.inc"
  2498. };
  2499. float softKnee (float u, float p) noexcept
  2500. {
  2501. const auto a = std::abs (u);
  2502. if (p == 2.0f) return u / std::sqrt (1.0f + a * a);
  2503. if (p == 1.0f) return u / (1.0f + a);
  2504. return u / std::pow (1.0f + std::pow (a, p), 1.0f / p);
  2505. }
  2506. double interpDrive (const float* table, double drive) noexcept
  2507. {
  2508. const auto x = std::clamp (drive, 0.0, 1.0) * 5.0;
  2509. const auto i = std::min ((int) x, 4);
  2510. return table[i] + (table[i + 1] - table[i]) * (x - i);
  2511. }
  2512. } // namespace
  2513. const char* characterName (int k) noexcept
  2514. {
  2515. if (k <= 0 || k > characters) return "Off";
  2516. return characterTable[k - 1].name;
  2517. }
  2518. float characterShape (int k, double drive, float x) noexcept
  2519. {
  2520. if (k <= 0 || k > characters) return x;
  2521. const auto& c = characterTable[k - 1];
  2522. const auto d = (float) dbToGain (interpDrive (c.driveDb, drive));
  2523. const auto a = (float) dbToGain (interpDrive (c.gainDb, drive)) / d; // A is the small-signal gain: A · g (D x) / D
  2524. const auto u = d * x;
  2525. if (c.family == 1) return a * std::sin (u);
  2526. if (c.family == 2) return a * std::abs (u);
  2527. const auto f0 = softKnee (c.bias, c.p);
  2528. const auto slope = std::pow (1.0f + std::pow (std::abs (c.bias), c.p), -1.0f / c.p - 1.0f);
  2529. return a * (softKnee (u + c.bias, c.p) - f0) / slope;
  2530. }
  2531. namespace
  2532. {
  2533. // a band's character: the curve (run oversampled by the caller), its input / output gain from the drive and the EQ after
  2534. // it (at the base rate, after the band's DC blocker)
  2535. struct CharacterStage
  2536. {
  2537. int k = 0, lastK = -1, sections = 0;
  2538. double lastSr = 0.0;
  2539. int family = 0;
  2540. float p = 2.0f, bias = 0.0f, f0 = 0.0f, slope = 1.0f, eqGain = 1.0f;
  2541. std::array<Biquad, 9> eq;
  2542. void reset() noexcept { for (auto& q : eq) q.reset(); }
  2543. void configure (int kk, double sr) noexcept
  2544. {
  2545. k = std::clamp (kk, 0, characters);
  2546. if (k == 0 || (k == lastK && sr == lastSr)) return;
  2547. const auto& c = characterTable[k - 1];
  2548. family = c.family;
  2549. p = c.p;
  2550. bias = c.bias;
  2551. f0 = softKnee (bias, p);
  2552. slope = std::pow (1.0f + std::pow (std::abs (bias), p), -1.0f / p - 1.0f);
  2553. sections = 0;
  2554. for (int i = 0; i < c.highPasses && sections < 9; ++i) eq[(std::size_t) sections++].filter (sr, 1, c.hpHz, c.hpQ);
  2555. for (int i = 0; i < c.lowPasses && sections < 9; ++i) eq[(std::size_t) sections++].filter (sr, 0, c.lpHz, c.lpQ);
  2556. for (int i = 0; i < 3 && sections < 9; ++i)
  2557. if (std::abs (c.bellDb[i]) > 0.01f)
  2558. eq[(std::size_t) sections++].peaking (sr, std::min ((double) c.bellHz[i], sr * 0.45), c.bellQ[i], c.bellDb[i]);
  2559. eqGain = (float) dbToGain (c.eqDb);
  2560. if (k != lastK) reset();
  2561. lastK = k;
  2562. lastSr = sr;
  2563. }
  2564. // the input (drive) and output gains for Ember's drive in dB on one channel (+ PUNISH's extra input gain)
  2565. std::pair<float, float> gains (double driveDb, double extraDb) const noexcept
  2566. {
  2567. const auto& c = characterTable[std::max (k, 1) - 1];
  2568. const auto d = referenceDrive (driveDb);
  2569. // round 152: Collapse with the measured law — the pitch drop's own level curve (the output's rms at seven input
  2570. // levels per drive step, fitted with the same knee: 0.02 … 0.37 dB rms) [EXEC]
  2571. static constexpr float collapseD[6] { -1.5f, 1.5f, 11.0f, 22.0f, 35.25f, 47.0f };
  2572. static constexpr float collapseA[6] { 0.82f, 1.5f, 1.25f, 3.21f, 12.76f, 24.96f };
  2573. const bool measured = law && k == collapseCharacter;
  2574. const auto dDb = interpDrive (measured ? collapseD : c.driveDb, d);
  2575. return { (float) dbToGain (dDb + extraDb), (float) dbToGain (interpDrive (measured ? collapseA : c.gainDb, d) - dDb) }; // A · g (D x) / D
  2576. }
  2577. bool law = false; // round 152: the effect characters' measured law (set by the engine per block)
  2578. float curve (float u) const noexcept
  2579. {
  2580. if (family == 1) return std::sin (u);
  2581. if (family == 2) return std::abs (u);
  2582. return (softKnee (u + bias, p) - f0) / slope;
  2583. }
  2584. void post (float& a, float& b) noexcept
  2585. {
  2586. for (int i = 0; i < sections; ++i)
  2587. {
  2588. a = eq[(std::size_t) i].process (a, 0);
  2589. b = eq[(std::size_t) i].process (b, 1);
  2590. }
  2591. a *= eqGain;
  2592. b *= eqGain;
  2593. }
  2594. };
  2595. } // namespace
  2596. namespace
  2597. {
  2598. // A band's round-128 stages (Saturn parity): tone after the shaper, a feedback loop around it, dynamics on its level.
  2599. // Each is skipped while it is at its neutral setting, so the old sound is untouched.
  2600. struct BandExtras
  2601. {
  2602. std::array<Biquad, 4> tone; // bass, mid, treble, presence
  2603. bool toneOn = false;
  2604. Biquad fbBand; // the feedback band (at the oversampled rate)
  2605. double fbGain = 0.0;
  2606. float fbLast[2] {};
  2607. double dyn = 0.0, env = 0.0, envAtk = 0.0, envRel = 0.0;
  2608. std::array<float, 4> lastTone { -1.0f, -1.0f, -1.0f, -1.0f };
  2609. float lastFbHz = -1.0f;
  2610. int lastFactor = 0;
  2611. void reset() noexcept
  2612. {
  2613. for (auto& t : tone) t.reset();
  2614. fbBand.reset();
  2615. fbLast[0] = fbLast[1] = 0.0f;
  2616. env = 0.0;
  2617. }
  2618. void configure (const EffectParams& p, int band, double sr, int factor) noexcept
  2619. {
  2620. std::array<float, 4> tv;
  2621. for (int k = 0; k < 4; ++k) tv[(std::size_t) k] = p[(std::size_t) bandExtra (band, 2 + k)];
  2622. if (tv != lastTone)
  2623. {
  2624. tone[0].shelf (sr, 120.0, toneDb (tv[0]), false, 0.7);
  2625. tone[1].peaking (sr, 800.0, 0.8, toneDb (tv[1]));
  2626. tone[2].shelf (sr, 3500.0, toneDb (tv[2]), true, 0.7);
  2627. tone[3].peaking (sr, 6000.0, 1.0, toneDb (tv[3]));
  2628. lastTone = tv;
  2629. }
  2630. toneOn = false;
  2631. for (const auto v : tv) toneOn = toneOn || std::abs (toneDb (v)) > 1e-6;
  2632. fbGain = 0.9 * feedbackAmount (p[(std::size_t) bandExtra (band, 6)]);
  2633. const auto fhz = p[(std::size_t) bandExtra (band, 7)];
  2634. if (fbGain > 0.0 && (fhz != lastFbHz || factor != lastFactor))
  2635. {
  2636. fbBand.filter (sr * factor, 2, feedbackHz (fhz), 1.4);
  2637. lastFbHz = fhz;
  2638. lastFactor = factor;
  2639. }
  2640. dyn = dynamicsAmount (p[(std::size_t) bandExtra (band, 8)]);
  2641. envAtk = coef (0.005, sr);
  2642. envRel = coef (0.080, sr);
  2643. }
  2644. // the shaper with the feedback loop: the band-passed last output added to the input (channel ch)
  2645. float shapeWithFeedback (Style s, float x, int ch) noexcept
  2646. {
  2647. return feedbackShape ([s] (float u) noexcept { return shape (s, u); }, x, ch);
  2648. }
  2649. template <typename Curve>
  2650. float feedbackShape (Curve&& curve, float x, int ch) noexcept
  2651. {
  2652. const auto y = curve (x + (float) fbGain * fbLast[ch]);
  2653. fbLast[ch] = fbBand.process (y, ch);
  2654. return y;
  2655. }
  2656. // after the shaper: the level follows the band's input level (dynamics), then the tone
  2657. void post (float& a, float& b, float inA, float inB) noexcept
  2658. {
  2659. if (dyn != 0.0)
  2660. {
  2661. const double x = std::max (std::abs (inA), std::abs (inB));
  2662. env = x > env ? envAtk * env + (1.0 - envAtk) * x : envRel * env + (1.0 - envRel) * x;
  2663. const double over = gainToDb (env + 1e-9) + 18.0; // dB above −18 dBFS
  2664. const auto g = (float) dbToGain (std::clamp (dyn * 0.5 * over, -18.0, 18.0));
  2665. a *= g;
  2666. b *= g;
  2667. }
  2668. if (toneOn)
  2669. for (auto& t : tone)
  2670. {
  2671. a = t.process (a, 0);
  2672. b = t.process (b, 1);
  2673. }
  2674. }
  2675. };
  2676. // round 152: the measured "breakdown" effect's pitch drop (Collapse with the measured law): a two-tap delay-line pitch
  2677. // shifter (taps half a window apart, sin² cross-fade) whose delay grows at 1 − ratio per sample [EXEC: the output
  2678. // frequency is the input's × 2^(−2 · drive) at every level and input frequency; the window length is LYNX's]
  2679. struct PitchDown
  2680. {
  2681. std::vector<float> buf[2];
  2682. int size = 0, w = 0;
  2683. double phase = 0.0, window = 2400.0;
  2684. void prepare (double sr)
  2685. {
  2686. window = std::max (64.0, 0.05 * sr);
  2687. size = 1;
  2688. while (size < (int) window + 8) size <<= 1;
  2689. for (auto& b : buf) b.assign ((std::size_t) size, 0.0f);
  2690. reset();
  2691. }
  2692. void reset() noexcept
  2693. {
  2694. for (auto& b : buf) std::fill (b.begin(), b.end(), 0.0f);
  2695. w = 0;
  2696. phase = 0.0;
  2697. }
  2698. float tap (int c, double d) const noexcept
  2699. {
  2700. const auto pos = (double) w - d;
  2701. const auto i0 = (int) std::floor (pos);
  2702. const auto f = (float) (pos - (double) i0);
  2703. const auto& b = buf[c];
  2704. const auto a = b[(std::size_t) (i0 & (size - 1))], bb = b[(std::size_t) ((i0 + 1) & (size - 1))];
  2705. return a + (bb - a) * f;
  2706. }
  2707. void run (float& l, float& r, double ratio) noexcept
  2708. {
  2709. if (size == 0) return;
  2710. buf[0][(std::size_t) w] = l;
  2711. buf[1][(std::size_t) w] = r;
  2712. if (ratio < 0.9999)
  2713. {
  2714. const auto ph2 = phase + 0.5 - std::floor (phase + 0.5);
  2715. const auto g1 = std::sin (pi * phase), g2 = std::sin (pi * ph2);
  2716. const auto d1 = phase * window, d2 = ph2 * window;
  2717. l = (float) (g1 * g1) * tap (0, d1) + (float) (g2 * g2) * tap (0, d2);
  2718. r = (float) (g1 * g1) * tap (1, d1) + (float) (g2 * g2) * tap (1, d2);
  2719. phase += (1.0 - ratio) / window;
  2720. phase -= std::floor (phase);
  2721. }
  2722. else
  2723. phase = 0.0;
  2724. w = (w + 1) & (size - 1);
  2725. }
  2726. };
  2727. // round 152: a plain stereo delay of a fixed length (the linear-phase mode's dry paths), allocated in prepare()
  2728. struct LongDelay
  2729. {
  2730. std::vector<float> buf[2];
  2731. int n = 0, pos = 0;
  2732. void allocate (int length)
  2733. {
  2734. n = std::max (length, 0);
  2735. for (auto& b : buf) b.assign ((std::size_t) std::max (n, 1), 0.0f);
  2736. pos = 0;
  2737. }
  2738. void reset() noexcept
  2739. {
  2740. for (auto& b : buf) std::fill (b.begin(), b.end(), 0.0f);
  2741. pos = 0;
  2742. }
  2743. void process (float& l, float& r) noexcept
  2744. {
  2745. if (n <= 0) return;
  2746. std::swap (buf[0][(std::size_t) pos], l);
  2747. std::swap (buf[1][(std::size_t) pos], r);
  2748. pos = pos + 1 == n ? 0 : pos + 1;
  2749. }
  2750. };
  2751. class Saturator final : public Effect
  2752. {
  2753. public:
  2754. explicit Saturator (const EffectParams* initial = nullptr) noexcept
  2755. {
  2756. if (initial != nullptr) linOn = linearPhase (*initial);
  2757. }
  2758. void prepare (double rate) noexcept override
  2759. {
  2760. sr = rate;
  2761. // round 152: the linear-phase crossovers (FFT convolvers) and their dry delays, the pitch drop, allocated here
  2762. linLatency = linearLatency (rate);
  2763. const auto size = dyn::prism::linearFftSize (rate);
  2764. if (size != linSize)
  2765. {
  2766. linSize = size;
  2767. for (auto& c : linConv)
  2768. c.init (size);
  2769. linTable.assign ((std::size_t) (size / 2 + 1), 0.0f);
  2770. }
  2771. linDry.allocate (linLatency);
  2772. linTop.allocate (linLatency);
  2773. linDesigned = false;
  2774. for (auto& pd : pitch)
  2775. pd.prepare (rate);
  2776. dry.setLength (Oversampler::latency());
  2777. dc.set (sr, 8.0);
  2778. classic.prepare (sr); // round 136: the Classic voicing (allocates here, never in process)
  2779. classicOn = false;
  2780. for (auto& d : bandDry)
  2781. d.setLength (Oversampler::latency());
  2782. for (auto& d : bandDc)
  2783. d.set (sr, 8.0);
  2784. splitBands = 0; // redesign the crossovers at the new rate
  2785. lastVoice = -1;
  2786. reset();
  2787. }
  2788. void reset() noexcept override
  2789. {
  2790. classic.reset();
  2791. for (auto& o : os)
  2792. o.reset();
  2793. dry.reset();
  2794. lowcut.reset();
  2795. emphasis.reset();
  2796. deemphasis.reset();
  2797. tiltLo.reset();
  2798. tiltHi.reset();
  2799. highcut.reset();
  2800. highcut2.reset();
  2801. highcut1.reset();
  2802. voicePre.reset();
  2803. voicePost.reset();
  2804. dc.reset();
  2805. for (auto& b : bandOs)
  2806. for (auto& o : b)
  2807. o.reset();
  2808. for (auto& d : bandDry)
  2809. d.reset();
  2810. for (auto& d : bandDc)
  2811. d.reset();
  2812. for (auto& x : split) x.reset();
  2813. for (auto& row : ap)
  2814. for (auto& x : row) x.reset();
  2815. for (auto& x : extras)
  2816. x.reset();
  2817. for (auto& x : chars)
  2818. x.reset();
  2819. // round 152
  2820. for (auto& c : linConv)
  2821. if (c.n > 0) c.clear();
  2822. linDry.reset();
  2823. linTop.reset();
  2824. for (auto& pd : pitch)
  2825. pd.reset();
  2826. lfoPhase = { 0.0, 0.0 };
  2827. envPower = 0.0;
  2828. }
  2829. int latencySamples() const noexcept override { return Oversampler::latency() + (linOn ? linLatency : 0); }
  2830. float controlOutput (int k) const noexcept override { return meters.read (k); }
  2831. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  2832. {
  2833. linOn = linearPhase (p); // round 152 (its latency follows)
  2834. if (! modulated (p))
  2835. {
  2836. processBlock (l, r, n, p);
  2837. return;
  2838. }
  2839. // round 152: the modulation, at a control rate of 32 samples — the sources at the start of each block, the
  2840. // targets moved from the slot's own values
  2841. constexpr int step = 32;
  2842. for (int off = 0; off < n; off += step)
  2843. {
  2844. const int m = std::min (step, n - off);
  2845. modulate (l + off, r + off, m, p);
  2846. processBlock (l + off, r + off, m, pm);
  2847. }
  2848. }
  2849. private:
  2850. void modulate (const float* l, const float* r, int n, const EffectParams& p) noexcept
  2851. {
  2852. // the follower: a mean-square one-pole (attack while rising, release / 10 while falling) [EXEC fit: the
  2853. // trial's default 1 ms / 50 ms within 0.025 source units rms, REA_rodada152b §4]
  2854. const auto ta = envAttackMs (p[envAttack]) * 1e-3 * sr, tr = envReleaseMs (p[envRelease]) * 1e-4 * sr;
  2855. const auto aa = 1.0 - std::exp (-1.0 / std::max (ta, 1e-3)), ar = 1.0 - std::exp (-1.0 / std::max (tr, 1e-3));
  2856. const auto envNow = envSource (10.0 * std::log10 (envPower + 1e-30));
  2857. for (int i = 0; i < n; ++i)
  2858. {
  2859. const double v = 0.5 * ((double) l[i] * l[i] + (double) r[i] * r[i]);
  2860. envPower += (v > envPower ? aa : ar) * (v - envPower);
  2861. }
  2862. std::array<double, 2> lfo {};
  2863. for (int k = 0; k < 2; ++k)
  2864. {
  2865. lfo[(std::size_t) k] = lfoValue (lfoShapeOf (p[(std::size_t) (k == 0 ? lfo1Shape : lfo2Shape)]), lfoPhase[(std::size_t) k]);
  2866. lfoPhase[(std::size_t) k] += lfoRateHz (p[(std::size_t) (k == 0 ? lfo1Rate : lfo2Rate)]) * (double) n / sr;
  2867. lfoPhase[(std::size_t) k] -= std::floor (lfoPhase[(std::size_t) k]);
  2868. }
  2869. pm = p;
  2870. const bool lin = linearPhase (p);
  2871. for (int k = 0; k < modSlots; ++k)
  2872. {
  2873. const auto src = modSourceOf (p[(std::size_t) (mod1Source + 3 * k)]);
  2874. const auto t = modTargetOf (p[(std::size_t) (mod1Target + 3 * k)]);
  2875. const auto prm = modTargetParam (t);
  2876. if (src == ModSource::off || prm < 0 || (lin && t >= 22)) continue; // linear phase: crossovers not modulated
  2877. const auto value = src == ModSource::lfo1 ? lfo[0] : (src == ModSource::lfo2 ? lfo[1] : envNow);
  2878. pm[(std::size_t) prm] += (float) modDelta (t, modAmount (p[(std::size_t) (mod1Amount + 3 * k)]) * value);
  2879. }
  2880. for (int k = 0; k < modSlots; ++k)
  2881. if (const auto prm = modTargetParam (modTargetOf (p[(std::size_t) (mod1Target + 3 * k)])); prm >= 0)
  2882. pm[(std::size_t) prm] = std::clamp (pm[(std::size_t) prm], 0.0f, 1.0f);
  2883. }
  2884. void processBlock (float* l, float* r, int n, const EffectParams& p) noexcept
  2885. {
  2886. // round 147: mid / side — M = (L + R) / 2 on channel 0, S = (R − L) / 2 on channel 1 (the trial's sign: a
  2887. // left-only tone's even harmonics land on the right), decoded L = M − S, R = M + S
  2888. const bool ms = midSide (p);
  2889. if (ms)
  2890. for (int i = 0; i < n; ++i)
  2891. {
  2892. const auto m = 0.5f * (l[i] + r[i]), sd = 0.5f * (r[i] - l[i]);
  2893. l[i] = m;
  2894. r[i] = sd;
  2895. }
  2896. // round 150 (measured): the global input pan before everything and the output pan after the dry / wet mix,
  2897. // the far channel × (1 − |pan|), on left / right or (in mid / side) on mid / side like the band pan
  2898. const auto [inA, inB] = panScales (panOf (p[inputPan]));
  2899. if (inA != 1.0f || inB != 1.0f)
  2900. for (int i = 0; i < n; ++i) { l[i] *= inA; r[i] *= inB; }
  2901. for (int off = 0; off < n; off += maxBlock)
  2902. chunk (l + off, r + off, std::min (maxBlock, n - off), p);
  2903. const auto [outA, outB] = panScales (panOf (p[outputPan]));
  2904. if (outA != 1.0f || outB != 1.0f)
  2905. for (int i = 0; i < n; ++i) { l[i] *= outA; r[i] *= outB; }
  2906. if (ms)
  2907. for (int i = 0; i < n; ++i)
  2908. {
  2909. const auto m = l[i], sd = r[i];
  2910. l[i] = m - sd;
  2911. r[i] = m + sd;
  2912. }
  2913. }
  2914. void chunk (float* l, float* r, int n, const EffectParams& p) noexcept
  2915. {
  2916. // round 152: linear phase — the single band and the Classic voicing are delayed like the FIR bands
  2917. if (linearPhase (p) && (on (p[voicing]) || activeBands (p) <= 1))
  2918. for (int i = 0; i < n; ++i)
  2919. linDry.process (l[i], r[i]);
  2920. if (on (p[voicing])) // round 136: the Classic voicing (core/EmberClassic.h) replaces the whole engine
  2921. {
  2922. chunkClassic (l, r, n, p);
  2923. return;
  2924. }
  2925. classicOn = false;
  2926. if (const auto inDb = inputDb (p[inputGain]); std::abs (inDb) > 1e-9) // round 128: the input gain
  2927. {
  2928. const auto g = (float) dbToGain (inDb);
  2929. for (int i = 0; i < n; ++i) { l[i] *= g; r[i] *= g; }
  2930. }
  2931. if (activeBands (p) > 1)
  2932. {
  2933. chunkBands (l, r, n, p);
  2934. return;
  2935. }
  2936. const auto inPeak = Meters::peak (l, r, n);
  2937. const auto factor = oversampling (p[quality]);
  2938. for (auto& o : os)
  2939. o.setFactor (factor);
  2940. // round 135: a voicing replaces the Style menu's curve and adds its own EQ; PUNISH adds 20 dB before the drive
  2941. // round 150: band 1's character (when set) replaces the Style menu and the voicing
  2942. auto& chr = chars[0];
  2943. chr.configure (characterIndex (p[characterParam (0)]), sr);
  2944. chr.law = measuredEffects (p); // round 152
  2945. const auto vk = chr.k > 0 ? 0 : voiceIndex (p[voice]);
  2946. const auto s = vk > 0 ? voiceStyle (vk) : styleOf (p[style]);
  2947. const auto drv = (float) dbToGain (driveDb (p[drive]) + (on (p[punish]) ? punishDb : 0.0));
  2948. // round 147: the drive pan — the far channel's drive × (1 − |pan|) (in dB, the trial's 0 … 100 % scale)
  2949. const auto [dsA, dsB] = panScales (panOf (p[bandDrivePanParam (0)]));
  2950. const bool drivePanned = dsA != 1.0f || dsB != 1.0f;
  2951. const auto drvA = drivePanned ? (float) dbToGain (driveDb (p[drive]) * dsA + (on (p[punish]) ? punishDb : 0.0)) : drv;
  2952. const auto drvB = drivePanned ? (float) dbToGain (driveDb (p[drive]) * dsB + (on (p[punish]) ? punishDb : 0.0)) : drv;
  2953. const auto [panA, panB] = panScales (panOf (p[bandPanParam (0)]));
  2954. const bool tape = s == Style::tape && vk == 0 && chr.k == 0;
  2955. const auto [lc, hc] = configureCuts (p);
  2956. configureVoice (vk);
  2957. // tape: pre-emphasis before the drive and the inverse after it, so the highs saturate first (DAFX 4.3)
  2958. emphasis.shelf (sr, 3000.0, 6.0, true, 0.7);
  2959. deemphasis.shelf (sr, 3000.0, -6.0, true, 0.7);
  2960. const auto tilt = toneTilt (p[tone]);
  2961. tiltLo.shelf (sr, 1000.0, -6.0 * tilt, false, 0.5);
  2962. tiltHi.shelf (sr, 1000.0, 6.0 * tilt, true, 0.5);
  2963. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  2964. const auto out = (float) dbToGain (lin (p[output], -18.0, 18.0));
  2965. if (on (p[autoGain]) && (drv != lastDrive || s != lastStyle))
  2966. {
  2967. makeup = autoMakeup (s, drv);
  2968. lastDrive = drv;
  2969. lastStyle = s;
  2970. }
  2971. const auto comp = on (p[autoGain]) ? makeup : 1.0f;
  2972. const auto compA = drivePanned && on (p[autoGain]) ? autoMakeup (s, drvA) : comp;
  2973. auto compB = drivePanned && on (p[autoGain]) ? autoMakeup (s, drvB) : comp;
  2974. auto drvA2 = drvA, drvB2 = drvB, compA2 = compA;
  2975. if (chr.k > 0)
  2976. {
  2977. const auto extra = on (p[punish]) ? punishDb : 0.0;
  2978. const auto ga = chr.gains (driveDb (p[drive]) * dsA, extra), gb = chr.gains (driveDb (p[drive]) * dsB, extra);
  2979. drvA2 = ga.first;
  2980. compA2 = ga.second;
  2981. drvB2 = gb.first;
  2982. compB = gb.second;
  2983. }
  2984. for (int i = 0; i < n; ++i)
  2985. {
  2986. float a = l[i], b = r[i];
  2987. if (lc > 0.0)
  2988. {
  2989. a = lowcut.process (a, 0);
  2990. b = lowcut.process (b, 1);
  2991. }
  2992. if (vk > 0)
  2993. {
  2994. a = voicePre.process (a, 0);
  2995. b = voicePre.process (b, 1);
  2996. }
  2997. if (tape)
  2998. {
  2999. a = emphasis.process (a, 0);
  3000. b = emphasis.process (b, 1);
  3001. }
  3002. wl[(std::size_t) i] = a * drvA2;
  3003. wr[(std::size_t) i] = b * drvB2;
  3004. }
  3005. // round 128: band 1's power, mute, feedback, dynamics and tone (single-band mode)
  3006. auto& ex = extras[0];
  3007. ex.configure (p, 0, sr, factor);
  3008. const bool powered = on (p[bandExtra (0, 0)]);
  3009. const bool muted = bandState (p[bandExtra (0, 1)]) == BandState::mute;
  3010. if (chr.k > 0)
  3011. {
  3012. if (ex.fbGain > 0.0)
  3013. {
  3014. auto left = [&ex, &chr] (float x) noexcept { return ex.feedbackShape ([&chr] (float u) noexcept { return chr.curve (u); }, x, 0); };
  3015. auto right = [&ex, &chr] (float x) noexcept { return ex.feedbackShape ([&chr] (float u) noexcept { return chr.curve (u); }, x, 1); };
  3016. os[0].process (wl.data(), n, left);
  3017. os[1].process (wr.data(), n, right);
  3018. }
  3019. else
  3020. {
  3021. auto curve = [&chr] (float x) noexcept { return chr.curve (x); };
  3022. os[0].process (wl.data(), n, curve);
  3023. os[1].process (wr.data(), n, curve);
  3024. }
  3025. }
  3026. else if (ex.fbGain > 0.0)
  3027. {
  3028. auto left = [&ex, s] (float x) noexcept { return ex.shapeWithFeedback (s, x, 0); };
  3029. auto right = [&ex, s] (float x) noexcept { return ex.shapeWithFeedback (s, x, 1); };
  3030. os[0].process (wl.data(), n, left);
  3031. os[1].process (wr.data(), n, right);
  3032. }
  3033. else
  3034. {
  3035. auto curve = [s] (float x) noexcept { return shape (s, x); };
  3036. os[0].process (wl.data(), n, curve);
  3037. os[1].process (wr.data(), n, curve);
  3038. }
  3039. const bool post = ex.toneOn || ex.dyn != 0.0;
  3040. const bool collapse = chr.k == collapseCharacter && measuredEffects (p); // round 152
  3041. const auto cRatio = collapse ? collapseRatio (referenceDrive (driveDb (p[drive]))) : 1.0;
  3042. // round 152 (with the measured law): the single band applies band 1's mix and level like the reference (before,
  3043. // they only acted in the multiband mode)
  3044. const bool bandLaw = measuredEffects (p);
  3045. const auto bandMix = std::clamp (p[band1Mix], 0.0f, 1.0f);
  3046. const auto bandGain = (float) dbToGain (bandLevelDb (p[band1Level]));
  3047. for (int i = 0; i < n; ++i)
  3048. {
  3049. auto a = dc.process (wl[(std::size_t) i], 0) * compA2, b = dc.process (wr[(std::size_t) i], 1) * compB;
  3050. if (chr.k > 0) chr.post (a, b);
  3051. if (collapse) pitch[0].run (a, b, cRatio); // round 152
  3052. if (tape)
  3053. {
  3054. a = deemphasis.process (a, 0);
  3055. b = deemphasis.process (b, 1);
  3056. }
  3057. if (post) ex.post (a, b, l[i], r[i]);
  3058. if (vk > 0)
  3059. {
  3060. a = voicePost.process (a, 0);
  3061. b = voicePost.process (b, 1);
  3062. }
  3063. a = tiltHi.process (tiltLo.process (a, 0), 0);
  3064. b = tiltHi.process (tiltLo.process (b, 1), 1);
  3065. if (hc > 0.0)
  3066. {
  3067. a = applyHighCut (a, 0);
  3068. b = applyHighCut (b, 1);
  3069. }
  3070. float da = l[i], db = r[i];
  3071. dry.process (da, db);
  3072. if (bandLaw) // round 152: band 1's own mix and level, as the reference's single band
  3073. {
  3074. a = (da + (a - da) * bandMix) * bandGain;
  3075. b = (db + (b - db) * bandMix) * bandGain;
  3076. }
  3077. if (! powered) { a = da; b = db; } // switched off: the band passes clean
  3078. if (muted) { a = 0.0f; b = 0.0f; }
  3079. a *= panA; // round 147: the band's pan
  3080. b *= panB;
  3081. l[i] = (da + (a - da) * mx) * out;
  3082. r[i] = (db + (b - db) * mx) * out;
  3083. }
  3084. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  3085. }
  3086. // Multiband mode (REA_rodada108): Linkwitz-Riley bands (the lower ones through the higher splits' all-passes, so
  3087. // the bands sum flat), each with its own style, drive, mix and level, oversampled separately; the cuts, tone,
  3088. // global mix and output as in the single-band mode.
  3089. void chunkBands (float* l, float* r, int n, const EffectParams& p) noexcept
  3090. {
  3091. const auto inPeak = Meters::peak (l, r, n);
  3092. const int nb = activeBands (p);
  3093. const auto factor = oversampling (p[quality]);
  3094. // round 150: up to five crossovers (each at least 5 % above the one below)
  3095. std::array<double, maxBands - 1> f {};
  3096. for (int k = 0; k < maxBands - 1; ++k)
  3097. {
  3098. f[(std::size_t) k] = crossoverHz (p[(std::size_t) crossoverParam (k)]);
  3099. if (k > 0) f[(std::size_t) k] = std::max (f[(std::size_t) k], f[(std::size_t) k - 1] * 1.05);
  3100. }
  3101. // round 152: each crossover its own slope (or the global one)
  3102. std::array<int, maxBands - 1> slope {};
  3103. for (int k = 0; k < maxBands - 1; ++k)
  3104. slope[(std::size_t) k] = crossoverSlopeDbOct (p, k);
  3105. const bool linear = linearPhase (p);
  3106. if (linear && (f != linHz || slope != linSlopes || nb != linBands || ! linDesigned))
  3107. designLinear (f, slope, nb);
  3108. if (f != splitHz || slope != splitSlope || nb != splitBands)
  3109. {
  3110. for (int k = 0; k < maxBands - 1; ++k)
  3111. {
  3112. split[(std::size_t) k].design (sr, f[(std::size_t) k], slope[(std::size_t) k]);
  3113. for (int j = 0; j < k; ++j) // the bands below split k pass its all-pass copy, so the sum stays flat
  3114. ap[(std::size_t) k][(std::size_t) j].design (sr, f[(std::size_t) k], slope[(std::size_t) k]);
  3115. }
  3116. if (nb != splitBands)
  3117. {
  3118. for (auto& x : split) x.reset();
  3119. for (auto& row : ap)
  3120. for (auto& x : row) x.reset();
  3121. for (auto& d : bandDry)
  3122. d.reset();
  3123. }
  3124. splitHz = f;
  3125. splitSlope = slope;
  3126. splitBands = nb;
  3127. }
  3128. const auto [lc, hc] = configureCuts (p);
  3129. const auto tilt = toneTilt (p[tone]);
  3130. tiltLo.shelf (sr, 1000.0, -6.0 * tilt, false, 0.5);
  3131. tiltHi.shelf (sr, 1000.0, 6.0 * tilt, true, 0.5);
  3132. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  3133. const auto out = (float) dbToGain (lin (p[output], -18.0, 18.0));
  3134. const bool autoOn = on (p[autoGain]);
  3135. const auto punishGain = (float) (on (p[punish]) ? dbToGain (punishDb) : 1.0); // round 135
  3136. std::array<Style, maxBands> st {};
  3137. std::array<float, maxBands> drv {}, bmix {}, lvl {}, comp {};
  3138. std::array<float, maxBands> drvB {}, compB {}, panA {}, panB {}; // round 147: channel 1's drive, the band pan
  3139. bool anySolo = false;
  3140. for (int b = 0; b < nb; ++b) anySolo = anySolo || bandState (p[(std::size_t) bandExtra (b, 1)]) == BandState::solo;
  3141. for (int b = 0; b < nb; ++b)
  3142. {
  3143. st[(std::size_t) b] = styleOf (p[(std::size_t) bandParam (b, 0)]);
  3144. drv[(std::size_t) b] = (float) dbToGain (driveDb (p[(std::size_t) bandParam (b, 1)])) * punishGain;
  3145. bmix[(std::size_t) b] = std::clamp (p[(std::size_t) bandParam (b, 2)], 0.0f, 1.0f);
  3146. lvl[(std::size_t) b] = (float) dbToGain (bandLevelDb (p[(std::size_t) bandParam (b, 3)]));
  3147. comp[(std::size_t) b] = autoOn ? autoMakeup (st[(std::size_t) b], drv[(std::size_t) b]) : 1.0f;
  3148. // round 147: drive pan (channel 0's drive stays in drv) and band pan
  3149. const auto [dsA, dsB] = panScales (panOf (p[(std::size_t) bandDrivePanParam (b)]));
  3150. const auto dDb = driveDb (p[(std::size_t) bandParam (b, 1)]);
  3151. drvB[(std::size_t) b] = drv[(std::size_t) b];
  3152. compB[(std::size_t) b] = comp[(std::size_t) b];
  3153. if (dsA != 1.0f || dsB != 1.0f)
  3154. {
  3155. drv[(std::size_t) b] = (float) dbToGain (dDb * dsA) * punishGain;
  3156. drvB[(std::size_t) b] = (float) dbToGain (dDb * dsB) * punishGain;
  3157. comp[(std::size_t) b] = autoOn ? autoMakeup (st[(std::size_t) b], drv[(std::size_t) b]) : 1.0f;
  3158. compB[(std::size_t) b] = autoOn ? autoMakeup (st[(std::size_t) b], drvB[(std::size_t) b]) : 1.0f;
  3159. }
  3160. // round 150: a character replaces the style's curve, its drive law and the level (no auto gain on top)
  3161. chars[(std::size_t) b].configure (characterIndex (p[(std::size_t) characterParam (b)]), sr);
  3162. chars[(std::size_t) b].law = measuredEffects (p); // round 152
  3163. if (chars[(std::size_t) b].k > 0)
  3164. {
  3165. const auto extra = on (p[punish]) ? punishDb : 0.0;
  3166. const auto ga = chars[(std::size_t) b].gains (dDb * dsA, extra), gb = chars[(std::size_t) b].gains (dDb * dsB, extra);
  3167. drv[(std::size_t) b] = ga.first;
  3168. comp[(std::size_t) b] = ga.second;
  3169. drvB[(std::size_t) b] = gb.first;
  3170. compB[(std::size_t) b] = gb.second;
  3171. }
  3172. const auto [pa, pb] = panScales (panOf (p[(std::size_t) bandPanParam (b)]));
  3173. panA[(std::size_t) b] = pa;
  3174. panB[(std::size_t) b] = pb;
  3175. bandOs[(std::size_t) b][0].setFactor (factor);
  3176. bandOs[(std::size_t) b][1].setFactor (factor);
  3177. }
  3178. for (int i = 0; i < n && linear; ++i)
  3179. {
  3180. // round 152: linear phase — band k < top = the zero-phase FIR of its share, the top band = the delayed
  3181. // input minus the others (so the bands sum to the delayed input exactly); the dry input is delayed alike
  3182. float a = l[i], b = r[i];
  3183. if (lc > 0.0)
  3184. {
  3185. a = lowcut.process (a, 0);
  3186. b = lowcut.process (b, 1);
  3187. }
  3188. float ta = a, tb = b;
  3189. linTop.process (ta, tb);
  3190. for (int k = 0; k < nb - 1; ++k)
  3191. {
  3192. float ca = a, cb = b;
  3193. linConv[(std::size_t) k].step (ca, cb);
  3194. band (k, 0, i) = ca;
  3195. band (k, 1, i) = cb;
  3196. ta -= ca;
  3197. tb -= cb;
  3198. }
  3199. band (nb - 1, 0, i) = ta;
  3200. band (nb - 1, 1, i) = tb;
  3201. linDry.process (l[i], r[i]);
  3202. }
  3203. for (int i = 0; i < n && ! linear; ++i)
  3204. {
  3205. float x[2] { l[i], r[i] };
  3206. for (int c = 0; c < 2; ++c)
  3207. {
  3208. auto v = x[c];
  3209. if (lc > 0.0)
  3210. v = lowcut.process (v, c);
  3211. // the bands from the bottom up: split k cuts the rest in two, the bands already cut pass its all-pass
  3212. // (the same filters in the same order as the four-band version before round 150)
  3213. auto rest = v;
  3214. for (int k = 0; k < nb - 1; ++k)
  3215. {
  3216. float lo, hi;
  3217. split[(std::size_t) k].run (rest, c, lo, hi);
  3218. for (int j = 0; j < k; ++j)
  3219. band (j, c, i) = ap[(std::size_t) k][(std::size_t) j].allpass (band (j, c, i), c);
  3220. band (k, c, i) = lo;
  3221. rest = hi;
  3222. }
  3223. band (nb - 1, c, i) = rest;
  3224. }
  3225. }
  3226. // each band: keep its dry copy (delayed like the oversampled path), drive and shape it
  3227. for (int b = 0; b < nb; ++b)
  3228. {
  3229. auto* bl = bandBuf[(std::size_t) b][0].data();
  3230. auto* br = bandBuf[(std::size_t) b][1].data();
  3231. for (int i = 0; i < n; ++i)
  3232. {
  3233. float da = bl[i], db = br[i];
  3234. bandDry[(std::size_t) b].process (da, db);
  3235. dryBand[0][(std::size_t) i] = da;
  3236. dryBand[1][(std::size_t) i] = db;
  3237. bl[i] *= drv[(std::size_t) b];
  3238. br[i] *= drvB[(std::size_t) b];
  3239. }
  3240. const auto s = st[(std::size_t) b];
  3241. // round 128: the band's feedback, dynamics, tone, power and solo / mute
  3242. auto& ex = extras[(std::size_t) b];
  3243. ex.configure (p, b, sr, factor);
  3244. auto& chr = chars[(std::size_t) b];
  3245. const bool collapse = chr.k == collapseCharacter && measuredEffects (p);
  3246. const auto ratio = collapse ? collapseRatio (referenceDrive (driveDb (p[(std::size_t) bandParam (b, 1)]))) : 1.0;
  3247. if (chr.k > 0)
  3248. {
  3249. if (ex.fbGain > 0.0)
  3250. {
  3251. auto left = [&ex, &chr] (float v) noexcept { return ex.feedbackShape ([&chr] (float u) noexcept { return chr.curve (u); }, v, 0); };
  3252. auto right = [&ex, &chr] (float v) noexcept { return ex.feedbackShape ([&chr] (float u) noexcept { return chr.curve (u); }, v, 1); };
  3253. bandOs[(std::size_t) b][0].process (bl, n, left);
  3254. bandOs[(std::size_t) b][1].process (br, n, right);
  3255. }
  3256. else
  3257. {
  3258. auto curve = [&chr] (float v) noexcept { return chr.curve (v); };
  3259. bandOs[(std::size_t) b][0].process (bl, n, curve);
  3260. bandOs[(std::size_t) b][1].process (br, n, curve);
  3261. }
  3262. }
  3263. else if (ex.fbGain > 0.0)
  3264. {
  3265. auto left = [&ex, s] (float v) noexcept { return ex.shapeWithFeedback (s, v, 0); };
  3266. auto right = [&ex, s] (float v) noexcept { return ex.shapeWithFeedback (s, v, 1); };
  3267. bandOs[(std::size_t) b][0].process (bl, n, left);
  3268. bandOs[(std::size_t) b][1].process (br, n, right);
  3269. }
  3270. else
  3271. {
  3272. auto curve = [s] (float v) noexcept { return shape (s, v); };
  3273. bandOs[(std::size_t) b][0].process (bl, n, curve);
  3274. bandOs[(std::size_t) b][1].process (br, n, curve);
  3275. }
  3276. const auto m = bmix[(std::size_t) b], g = lvl[(std::size_t) b], k = comp[(std::size_t) b], kB = compB[(std::size_t) b];
  3277. const auto gA = g * panA[(std::size_t) b], gB = g * panB[(std::size_t) b];
  3278. const bool post = ex.toneOn || ex.dyn != 0.0;
  3279. const bool powered = on (p[(std::size_t) bandExtra (b, 0)]);
  3280. const auto state = bandState (p[(std::size_t) bandExtra (b, 1)]);
  3281. const bool silent = state == BandState::mute || (anySolo && state != BandState::solo);
  3282. for (int i = 0; i < n; ++i)
  3283. {
  3284. auto wa = bandDc[(std::size_t) b].process (bl[i], 0) * k, wb = bandDc[(std::size_t) b].process (br[i], 1) * kB;
  3285. if (chr.k > 0) chr.post (wa, wb);
  3286. if (collapse) pitch[(std::size_t) b].run (wa, wb, ratio); // round 152
  3287. if (post) ex.post (wa, wb, dryBand[0][(std::size_t) i], dryBand[1][(std::size_t) i]);
  3288. auto ya = (dryBand[0][(std::size_t) i] + (wa - dryBand[0][(std::size_t) i]) * m) * gA;
  3289. auto yb = (dryBand[1][(std::size_t) i] + (wb - dryBand[1][(std::size_t) i]) * m) * gB;
  3290. if (! powered) { ya = dryBand[0][(std::size_t) i]; yb = dryBand[1][(std::size_t) i]; }
  3291. if (silent) { ya = 0.0f; yb = 0.0f; }
  3292. if (b == 0)
  3293. {
  3294. wl[(std::size_t) i] = ya;
  3295. wr[(std::size_t) i] = yb;
  3296. }
  3297. else
  3298. {
  3299. wl[(std::size_t) i] += ya;
  3300. wr[(std::size_t) i] += yb;
  3301. }
  3302. }
  3303. }
  3304. for (int i = 0; i < n; ++i)
  3305. {
  3306. auto a = tiltHi.process (tiltLo.process (wl[(std::size_t) i], 0), 0);
  3307. auto b = tiltHi.process (tiltLo.process (wr[(std::size_t) i], 1), 1);
  3308. if (hc > 0.0)
  3309. {
  3310. a = applyHighCut (a, 0);
  3311. b = applyHighCut (b, 1);
  3312. }
  3313. float da = l[i], db = r[i];
  3314. dry.process (da, db);
  3315. l[i] = (da + (a - da) * mx) * out;
  3316. r[i] = (db + (b - db) * mx) * out;
  3317. }
  3318. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  3319. }
  3320. float& band (int b, int c, int i) noexcept { return bandBuf[(std::size_t) b][(std::size_t) c][(std::size_t) i]; }
  3321. // round 135: the low / high cut of either range. Old range: 12 dB/oct both; THUMP gives the low cut a resonance,
  3322. // STEEP makes the high cut 30 dB/oct (5th-order Butterworth), the wide range's gentle high cut is 6 dB/oct
  3323. // [EXEC trial: gentle ≈ 6 dB/oct, steep ≈ 30 dB/oct, low cut 12 dB/oct, Thump +3.5 dB at the cutoff]
  3324. std::pair<double, double> configureCuts (const EffectParams& p) noexcept
  3325. {
  3326. const bool wide = on (p[wideCuts]);
  3327. const auto lc = wide ? lowCutWideHz (p[lowCutWide]) : lowCutHz (p[lowCut]);
  3328. const auto hc = wide ? highCutWideHz (p[highCutWide]) : highCutHz (p[highCut]);
  3329. if (lc > 0.0)
  3330. lowcut.filter (sr, 1, lc, on (p[thump]) ? thumpQ : 0.70710678);
  3331. hcMode = on (p[steep]) ? 2 : (wide ? 1 : 0);
  3332. if (hc > 0.0)
  3333. {
  3334. const auto f = std::min (hc, sr * 0.45);
  3335. if (hcMode == 2)
  3336. {
  3337. highcut.filter (sr, 0, f, 1.0 / (2.0 * std::cos (3.14159265358979323846 / 5.0))); // Q 0.618
  3338. highcut2.filter (sr, 0, f, 1.0 / (2.0 * std::cos (2.0 * 3.14159265358979323846 / 5.0))); // Q 1.618
  3339. }
  3340. else if (hcMode == 0)
  3341. highcut.filter (sr, 0, f, 0.70710678);
  3342. const auto k = std::tan (3.14159265358979323846 * f / sr); // first-order section (bilinear)
  3343. highcut1.set (k, k, 0.0, 1.0 + k, k - 1.0, 0.0);
  3344. }
  3345. return { lc, hc };
  3346. }
  3347. float applyHighCut (float x, int c) noexcept
  3348. {
  3349. if (hcMode == 1) return highcut1.process (x, c);
  3350. x = highcut.process (x, c);
  3351. if (hcMode == 2) x = highcut1.process (highcut2.process (x, c), c);
  3352. return x;
  3353. }
  3354. // a voicing's EQ before and after its curve (small, broad shelves: the five references differ mostly there)
  3355. void configureVoice (int k) noexcept
  3356. {
  3357. if (k <= 0 || k == lastVoice) return;
  3358. lastVoice = k;
  3359. switch (k)
  3360. {
  3361. case 1: voicePre.shelf (sr, 8000.0, 1.0, true, 0.7); voicePost.shelf (sr, 60.0, 0.0, false, 0.7); break; // tape pre: open top
  3362. case 2: voicePre.shelf (sr, 7000.0, 2.5, true, 0.7); voicePost.shelf (sr, 50.0, 1.0, false, 0.7); break; // console: airy, firm bass
  3363. case 3: voicePre.shelf (sr, 6000.0, -2.5, true, 0.7); voicePost.shelf (sr, 80.0, 0.0, false, 0.7); break; // grit: darker top
  3364. case 4: voicePre.shelf (sr, 60.0, -3.0, false, 0.7); voicePost.shelf (sr, 10000.0, 0.5, true, 0.7); break; // triode: thinner bass
  3365. default: voicePre.peaking (sr, 100.0, 0.8, 2.5); voicePost.shelf (sr, 1000.0, 0.0, true, 0.7); break; // pentode: bass bump
  3366. }
  3367. }
  3368. static constexpr int maxBlock = 2048;
  3369. // level compensation: the RMS gain of the curve on a −12 dBFS sine at this drive, inverted
  3370. static float autoMakeup (Style s, float drv) noexcept
  3371. {
  3372. double in = 0.0, out = 0.0, mean = 0.0;
  3373. constexpr int points = 64;
  3374. std::array<double, points> ys {};
  3375. for (int k = 0; k < points; ++k)
  3376. {
  3377. const auto x = 0.25 * std::sin (2.0 * pi * (k + 0.5) / points);
  3378. ys[(std::size_t) k] = shape (s, (float) (x * drv));
  3379. mean += ys[(std::size_t) k];
  3380. in += x * x;
  3381. }
  3382. mean /= points;
  3383. for (auto y : ys)
  3384. out += (y - mean) * (y - mean);
  3385. return out > 1e-12 ? (float) std::sqrt (in / out) : 1.0f;
  3386. }
  3387. double sr = 44100.0;
  3388. std::array<Oversampler, 2> os;
  3389. Delay2<256> dry;
  3390. Biquad lowcut, emphasis, deemphasis, tiltLo, tiltHi, highcut;
  3391. Biquad highcut2, highcut1, voicePre, voicePost; // round 135
  3392. int hcMode = 0, lastVoice = -1;
  3393. OnePoleHp dc;
  3394. std::array<float, maxBlock> wl {}, wr {};
  3395. float makeup = 1.0f, lastDrive = -1.0f;
  3396. Style lastStyle = Style::tube;
  3397. // round 136: the Classic voicing
  3398. emberclassic::Engine classic;
  3399. emberclassic::Settings classicSet;
  3400. bool classicOn = false;
  3401. void chunkClassic (float* l, float* r, int n, const EffectParams& p) noexcept
  3402. {
  3403. emberclassic::Settings c;
  3404. c.style = emberclassic::styleOf (p[classicStyle]);
  3405. c.drive = emberclassic::driveUnits (p[classicDrive]);
  3406. c.punish = on (p[classicPunish]);
  3407. c.lowCutHz = emberclassic::lowCutHz (p[classicLowCut]);
  3408. c.thump = on (p[classicThump]);
  3409. c.toneDb = emberclassic::toneDb (p[classicTone]);
  3410. c.highCutHz = emberclassic::highCutHz (p[classicHighCut]);
  3411. c.steep = on (p[classicSteep]);
  3412. c.mix = std::clamp ((double) p[classicMix], 0.0, 1.0);
  3413. c.outputDb = emberclassic::outputDb (p[classicOutput]);
  3414. c.measuredPunish = on (p[classicLaw]);
  3415. c.hotCoupling = on (p[classicHot]);
  3416. if (! classicOn || ! (c == classicSet))
  3417. {
  3418. if (! classicOn)
  3419. classic.reset();
  3420. classic.set (c);
  3421. classicSet = c;
  3422. classicOn = true;
  3423. }
  3424. const auto inPeak = Meters::peak (l, r, n);
  3425. classic.process (l, r, n);
  3426. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  3427. }
  3428. // multiband mode
  3429. std::array<LrSplit, maxBands - 1> split;
  3430. std::array<std::array<LrSplit, maxBands - 2>, maxBands - 1> ap; // ap[k][j]: split k's all-pass for band j < k
  3431. std::array<double, maxBands - 1> splitHz {};
  3432. std::array<int, maxBands - 1> splitSlope {};
  3433. int splitBands = 0;
  3434. std::array<std::array<Oversampler, 2>, maxBands> bandOs;
  3435. std::array<Delay2<256>, maxBands> bandDry;
  3436. std::array<OnePoleHp, maxBands> bandDc;
  3437. std::array<std::array<std::array<float, maxBlock>, 2>, maxBands> bandBuf {};
  3438. std::array<std::array<float, maxBlock>, 2> dryBand {};
  3439. std::array<BandExtras, maxBands> extras;
  3440. std::array<CharacterStage, maxBands> chars; // round 150
  3441. // round 152: linear phase, the modulation, the pitch drop
  3442. std::array<lynx::peqfl::FftConvolver, maxBands - 1> linConv;
  3443. std::vector<float> linTable;
  3444. LongDelay linDry, linTop;
  3445. int linSize = 0, linLatency = 0, linBands = 0;
  3446. std::array<double, maxBands - 1> linHz {};
  3447. std::array<int, maxBands - 1> linSlopes {};
  3448. bool linDesigned = false;
  3449. std::atomic<bool> linOn { false };
  3450. std::array<PitchDown, maxBands> pitch;
  3451. std::array<double, 2> lfoPhase {};
  3452. double envPower = 0.0;
  3453. EffectParams pm {};
  3454. // the FIR of band k < top: lo_k · Π_{j<k} (1 − lo_j), lo = 1 / (1 + (f / fc)^(slope / 6)) on the analog axis
  3455. // [EXEC: the trial's linear-phase bands, every slope, 0.00 dB]
  3456. void designLinear (const std::array<double, maxBands - 1>& f, const std::array<int, maxBands - 1>& slope, int nb) noexcept
  3457. {
  3458. linHz = f;
  3459. linSlopes = slope;
  3460. linBands = nb;
  3461. linDesigned = true;
  3462. if (linSize <= 0) return;
  3463. const auto bins = linSize / 2 + 1;
  3464. for (int k = 0; k < nb - 1; ++k)
  3465. {
  3466. for (int i = 0; i < bins; ++i)
  3467. {
  3468. const auto hz = (double) i * sr / (double) linSize;
  3469. double m = linearLow (hz, f[(std::size_t) k], slope[(std::size_t) k]);
  3470. for (int j = 0; j < k; ++j)
  3471. m *= 1.0 - linearLow (hz, f[(std::size_t) j], slope[(std::size_t) j]);
  3472. linTable[(std::size_t) i] = (float) m;
  3473. }
  3474. linConv[(std::size_t) k].setKernel (linTable);
  3475. }
  3476. }
  3477. Meters meters;
  3478. };
  3479. } // namespace
  3480. } // namespace ember
  3481. // ================================================================== registry
  3482. const EffectInfo& ridgeInfo()
  3483. {
  3484. static const EffectInfo info { "Ridge Strip",
  3485. { { "Input", 0.5f }, { "HPF", 0.0f }, { "LPF", 0.0f },
  3486. { "LF gain", 0.5f }, { "LF freq.", 0.40f }, { "LF bell", 0.0f },
  3487. { "LMF gain", 0.5f }, { "LMF freq.", 0.43f }, { "LMF Q", 0.43f },
  3488. { "HMF gain", 0.5f }, { "HMF freq.", 0.49f }, { "HMF Q", 0.43f },
  3489. { "HF gain", 0.5f }, { "HF freq.", 0.70f }, { "HF bell", 0.0f },
  3490. { "EQ curve", 0.0f }, { "EQ on", 1.0f },
  3491. { "Ratio", 0.0f }, { "Comp. threshold", 1.0f }, { "Comp. release", 0.29f }, { "Fast attack", 0.0f },
  3492. { "Gate range", 0.0f }, { "Gate threshold", 0.25f }, { "Gate release", 0.29f }, { "Gate fast", 0.0f },
  3493. { "Expander", 0.0f }, { "Dynamics on", 1.0f },
  3494. { "Order", 1.0f }, { "Filters to SC", 0.0f }, { "Output", 0.5f }, { "Phase", 0.0f }, { "Comp. mix", 1.0f },
  3495. { "Voicing", 0.0f }, { "Filter split", 0.0f },
  3496. // round 145: the Vocal strip voicing (core/VoxStrip.h); defaults = the reference's
  3497. { "Vocal strip", 0.0f }, { "Vocal input", 0.75f }, { "Vocal output", 0.75f }, { "Process order", 0.0f },
  3498. { "De-esser", 0.0f }, { "De-ess freq.", 0.672591f }, { "De-ess threshold", 0.5f },
  3499. { "De-ess split", 1.0f }, { "De-ess lookahead", 0.0f }, { "De-ess listen", 0.0f },
  3500. { "De-ploser", 0.0f }, { "De-plos freq.", 0.709403f }, { "De-plos threshold", 0.5f },
  3501. { "De-plos split", 1.0f }, { "De-plos lookahead", 0.0f }, { "De-plos listen", 0.0f },
  3502. { "Vocal EQ", 0.0f }, { "Vocal HPF", 1.0f }, { "Vocal HPF freq.", 0.0f },
  3503. { "Notch", 1.0f }, { "Notch freq.", 0.0f }, { "Notch gain", 0.75f },
  3504. { "High band", 1.0f }, { "High freq.", 0.0f }, { "High gain", 0.5f },
  3505. { "Compander", 0.0f }, { "Expander threshold", 0.0f }, { "Compander threshold", 1.0f },
  3506. { "Compander ratio", 0.504899f }, { "Compander attack", 0.0f }, { "Compander release", 0.049762f },
  3507. { "Make-up", 0.0f }, { "Soft knee", 0.0f }, { "Drive", 0.0f },
  3508. // round 147b
  3509. { "Auto make-up", 0.0f }, { "Analog", 0.0f },
  3510. // round 148: the 4K voicing
  3511. { "4K voicing", 0.0f }, { "EQ colour", 0.0f }, { "Mic pre", 0.0f }, { "Mic gain", 0.0f },
  3512. { "Make-up offset", 0.5f },
  3513. // round 165
  3514. { "EQ to SC", 0.0f },
  3515. // round tp172c: the Modern voicing (core/VoiceMatch.h, strip2)
  3516. { "Modern voicing", 0.0f }, { "Peak detect", 0.0f }, { "Gate hold", 0.0625f },
  3517. // round tp172c6: the DRIVE section (off = the old sound)
  3518. { "Drive in", 0.0f }, { "Drive model", 0.0f }, { "Drive amount", 0.5f }, { "Drive input", 0.5f },
  3519. { "Drive trim", 0.5f }, { "Drive mix", 1.0f } } };
  3520. return info;
  3521. }
  3522. const EffectInfo& glowInfo()
  3523. {
  3524. static const EffectInfo info { "Glow Leveler",
  3525. // round 152: a new instance starts as the reference does (the measured Opto classic
  3526. // voicing, Gain 40, Peak reduction 46, Hi Freq 50)
  3527. { { "Peak reduction", 0.46f }, { "Gain", 0.40f }, { "Limit", 0.0f }, { "Emphasis", 0.5f }, { "Mix", 1.0f },
  3528. { "Trim", 0.5f }, { "Auto makeup", 0.0f }, { "Voicing", 1.0f },
  3529. { "Opto law", 1.0f }, { "Analog", 0.0f },
  3530. // round 165w: the measured release (a project saved before keeps round 123's)
  3531. { "Opto release", 1.0f },
  3532. // round 165x: the dial where Auto makeup was switched on (0.46 = round 152's)
  3533. { "Makeup ref", 0.46f },
  3534. // round 165x: what the VU shows (0 gain reduction, 0.5 input, 1 output), saved
  3535. { "VU display", 0.0f },
  3536. // round 190dm: the cell's memory (off = the releases before, for every project)
  3537. { "Opto memory", 0.0f } } };
  3538. return info;
  3539. }
  3540. const EffectInfo& hushInfo()
  3541. {
  3542. static const EffectInfo info { "Hush De-esser",
  3543. // round 165x: 6.02 kHz on the extended law's 2 … 20 kHz scale (0.53 on the old 2 … 16 kHz)
  3544. { { "Frequency", 0.4786f }, { "Threshold", 0.5f }, { "Range", 0.5f }, { "Wideband", 0.0f },
  3545. { "Band detector", 0.0f }, { "Listen", 0.0f }, { "Lookahead", 1.0f }, { "Release", 0.56f },
  3546. { "Output", 0.5f }, { "Voicing", 0.0f }, // round 123 (vmatch123)
  3547. // round 128 (parity with the reference de-esser)
  3548. { "Allround", 0.0f }, { "Low-pass freq.", 1.0f }, { "Lookahead time", 2.0f / 15.0f },
  3549. { "Stereo link", 1.0f }, { "Link mid", 0.0f }, { "External SC", 0.0f }, { "Input", 0.5f },
  3550. { "Display", 1.0f },
  3551. { "Oversampling", 0.0f }, // round 135
  3552. { "Vocal voicing", 0.0f }, // round 148
  3553. { "Input pan", 0.5f }, { "Output pan", 0.5f }, // round 165w
  3554. // round 165x: the reference's ranges and link (a project saved before: the old law)
  3555. { "Range law", 1.0f }, { "Link only", 0.0f }, { "Link only side", 0.0f },
  3556. // round 190dm: the air shelf (off) and the mix (100 %): what every project had
  3557. { "Air", 0.0f }, { "Mix", 1.0f } } };
  3558. return info;
  3559. }
  3560. const EffectInfo& aetherInfo()
  3561. {
  3562. static const EffectInfo info { "Aether",
  3563. { { "Air", 0.0f }, { "Air freq.", 0.62f }, { "Harmonics", 0.0f }, { "Exciter freq.", 0.56f },
  3564. { "Character", 0.5f }, { "Mix", 1.0f }, { "Output", 0.5f },
  3565. // issue #77 (round tp172c): the measured dynamic air model is the default (a new
  3566. // Aether's Mid / High air were the round-114 shelf + exciter, nearly inaudible)
  3567. { "Dynamic", 1.0f }, { "Mid air", 0.0f }, { "High air", 0.0f } } };
  3568. return info;
  3569. }
  3570. const EffectInfo& kilnInfo()
  3571. {
  3572. // round tp172c7: the reference channel's defaults (0 dB trims, the first console, 6 dB drive, noise reduction on)
  3573. static const EffectInfo info { "Kiln Channel",
  3574. { { "Input", 0.5f }, { "Output", 0.5f }, { "Console", 0.0f }, { "Drive", 1.0f / 3.0f },
  3575. { "Noise reduction", 1.0f }, { "Group", 0.0f }, { "Group bypass", 0.0f },
  3576. { "Main in", 0.5f }, { "Main out", 0.5f }, { "Link I/O", 1.0f }, { "Group I/O", 1.0f } } };
  3577. return info;
  3578. }
  3579. const EffectInfo& emberInfo()
  3580. {
  3581. static const EffectInfo info { "Ember Saturator",
  3582. { { "Drive", 0.0f }, { "Style", 0.0f }, { "Tone", 0.5f }, { "Low cut", 0.0f }, { "High cut", 1.0f },
  3583. { "Mix", 1.0f }, { "Output", 0.5f }, { "Auto gain", 1.0f }, { "Oversampling", 0.5f },
  3584. { "Bands", 0.0f }, { "Crossover 1", 0.3266f }, { "Crossover 2", 0.6574f }, { "Crossover 3", 0.9375f },
  3585. { "Crossover slope", 0.5f }, { "B1 mix", 1.0f }, { "B1 level", 0.5f },
  3586. { "B2 style", 0.0f }, { "B2 drive", 0.0f }, { "B2 mix", 1.0f }, { "B2 level", 0.5f },
  3587. { "B3 style", 0.0f }, { "B3 drive", 0.0f }, { "B3 mix", 1.0f }, { "B3 level", 0.5f },
  3588. { "B4 style", 0.0f }, { "B4 drive", 0.0f }, { "B4 mix", 1.0f }, { "B4 level", 0.5f },
  3589. // round 128 (Saturn parity)
  3590. { "Input", 0.5f },
  3591. { "B1 on", 1.0f }, { "B1 state", 0.0f }, { "B1 bass", 0.5f }, { "B1 mid", 0.5f }, { "B1 treble", 0.5f }, { "B1 presence", 0.5f },
  3592. { "B1 feedback", 0.0f }, { "B1 fb freq.", 0.6990f }, { "B1 dynamics", 0.5f },
  3593. { "B2 on", 1.0f }, { "B2 state", 0.0f }, { "B2 bass", 0.5f }, { "B2 mid", 0.5f }, { "B2 treble", 0.5f }, { "B2 presence", 0.5f },
  3594. { "B2 feedback", 0.0f }, { "B2 fb freq.", 0.6990f }, { "B2 dynamics", 0.5f },
  3595. { "B3 on", 1.0f }, { "B3 state", 0.0f }, { "B3 bass", 0.5f }, { "B3 mid", 0.5f }, { "B3 treble", 0.5f }, { "B3 presence", 0.5f },
  3596. { "B3 feedback", 0.0f }, { "B3 fb freq.", 0.6990f }, { "B3 dynamics", 0.5f },
  3597. { "B4 on", 1.0f }, { "B4 state", 0.0f }, { "B4 bass", 0.5f }, { "B4 mid", 0.5f }, { "B4 treble", 0.5f }, { "B4 presence", 0.5f },
  3598. { "B4 feedback", 0.0f }, { "B4 fb freq.", 0.6990f }, { "B4 dynamics", 0.5f },
  3599. // round 135 (the Decapitator role)
  3600. { "Punish", 0.0f }, { "Thump", 0.0f }, { "Steep", 0.0f }, { "Voice", 0.0f }, { "Wide cuts", 0.0f },
  3601. { "Low cut (wide)", 0.0f }, { "High cut (wide)", 1.0f },
  3602. // round 136: the Classic voicing (EmberClassic.h)
  3603. { "Voicing", 0.0f }, { "Classic style", 0.0f }, { "Classic drive", 0.0f }, { "Classic punish", 0.0f },
  3604. { "Classic low cut", 0.0f }, { "Classic thump", 0.0f }, { "Classic tone", 0.5f }, { "Classic high cut", 1.0f },
  3605. { "Classic steep", 0.0f }, { "Classic mix", 1.0f }, { "Classic output", 1.0f },
  3606. // round 147 (Saturn 2's channel mode, drive pan and band pan)
  3607. { "Channel mode", 0.0f },
  3608. { "B1 drive pan", 0.5f }, { "B2 drive pan", 0.5f }, { "B3 drive pan", 0.5f }, { "B4 drive pan", 0.5f },
  3609. { "B1 pan", 0.5f }, { "B2 pan", 0.5f }, { "B3 pan", 0.5f }, { "B4 pan", 0.5f },
  3610. // round 150 (Saturn 2: bands 5 / 6, input / output pan, the measured characters)
  3611. { "More bands", 0.0f }, { "Crossover 4", 0.97f }, { "Crossover 5", 0.99f },
  3612. { "B5 style", 0.0f }, { "B5 drive", 0.0f }, { "B5 mix", 1.0f }, { "B5 level", 0.5f },
  3613. { "B6 style", 0.0f }, { "B6 drive", 0.0f }, { "B6 mix", 1.0f }, { "B6 level", 0.5f },
  3614. { "B5 on", 1.0f }, { "B5 state", 0.0f }, { "B5 bass", 0.5f }, { "B5 mid", 0.5f }, { "B5 treble", 0.5f }, { "B5 presence", 0.5f },
  3615. { "B5 feedback", 0.0f }, { "B5 fb freq.", 0.6990f }, { "B5 dynamics", 0.5f },
  3616. { "B6 on", 1.0f }, { "B6 state", 0.0f }, { "B6 bass", 0.5f }, { "B6 mid", 0.5f }, { "B6 treble", 0.5f }, { "B6 presence", 0.5f },
  3617. { "B6 feedback", 0.0f }, { "B6 fb freq.", 0.6990f }, { "B6 dynamics", 0.5f },
  3618. { "B5 drive pan", 0.5f }, { "B6 drive pan", 0.5f }, { "B5 pan", 0.5f }, { "B6 pan", 0.5f },
  3619. { "Input pan", 0.5f }, { "Output pan", 0.5f },
  3620. { "B1 character", 0.0f }, { "B2 character", 0.0f }, { "B3 character", 0.0f },
  3621. { "B4 character", 0.0f }, { "B5 character", 0.0f }, { "B6 character", 0.0f },
  3622. // round 152 (Saturn 2: own crossover slopes, processing, modulation, effect law)
  3623. { "Slope 1", 0.0f }, { "Slope 2", 0.0f }, { "Slope 3", 0.0f }, { "Slope 4", 0.0f }, { "Slope 5", 0.0f },
  3624. { "Processing", 0.0f },
  3625. { "LFO 1 rate", 0.248f }, { "LFO 1 shape", 0.0f }, { "LFO 2 rate", 0.248f }, { "LFO 2 shape", 0.0f },
  3626. { "Env attack", 0.0f }, { "Env release", 0.0f },
  3627. { "Mod 1 source", 0.0f }, { "Mod 1 target", 0.0f }, { "Mod 1 amount", 0.5f },
  3628. { "Mod 2 source", 0.0f }, { "Mod 2 target", 0.0f }, { "Mod 2 amount", 0.5f },
  3629. { "Mod 3 source", 0.0f }, { "Mod 3 target", 0.0f }, { "Mod 3 amount", 0.5f },
  3630. { "Mod 4 source", 0.0f }, { "Mod 4 target", 0.0f }, { "Mod 4 amount", 0.5f },
  3631. { "Effect law", 1.0f },
  3632. // round 165 (Decapitator: Auto, the Classic law)
  3633. { "Classic auto", 1.0f }, { "Classic law", 1.0f },
  3634. // round 190ar (the Classic voicing's high-drive coupling; old projects load it off)
  3635. { "Classic hot law", 1.0f } } };
  3636. return info;
  3637. }
  3638. std::unique_ptr<Effect> makeHush (const EffectParams* params) { return std::make_unique<hush::DeEsser> (params); }
  3639. std::unique_ptr<Effect> makeFret(); // round tp172c8 (core/StripSuite.inc)
  3640. std::unique_ptr<Effect> makeShell();
  3641. std::unique_ptr<Effect> makeStrike(); // round trans189d (core/StrikeSuite.inc)
  3642. std::unique_ptr<Effect> makeAnvil(); // round 190fk (core/AnvilSuite.inc)
  3643. bool isSuiteKind (EffectKind kind) noexcept
  3644. {
  3645. return kind == EffectKind::ridgeStrip || kind == EffectKind::glowLeveler || kind == EffectKind::hushDeEsser
  3646. || kind == EffectKind::aether || kind == EffectKind::ember || kind == EffectKind::kilnChannel
  3647. || kind == EffectKind::fretChannel || kind == EffectKind::shellChannel || kind == EffectKind::strikeShaper
  3648. || kind == EffectKind::anvilBus;
  3649. }
  3650. std::unique_ptr<Effect> make (EffectKind kind)
  3651. {
  3652. switch (kind)
  3653. {
  3654. case EffectKind::ridgeStrip: return std::make_unique<ridge::Strip>();
  3655. case EffectKind::glowLeveler: return std::make_unique<glow::Leveler>();
  3656. case EffectKind::hushDeEsser: return std::make_unique<hush::DeEsser>();
  3657. case EffectKind::aether: return std::make_unique<aether::Air>();
  3658. case EffectKind::ember: return std::make_unique<ember::Saturator>();
  3659. case EffectKind::kilnChannel: return ridge::makeKiln();
  3660. case EffectKind::fretChannel: return makeFret();
  3661. case EffectKind::shellChannel: return makeShell();
  3662. case EffectKind::strikeShaper: return makeStrike();
  3663. case EffectKind::anvilBus: return makeAnvil();
  3664. default: return nullptr;
  3665. }
  3666. }
  3667. std::unique_ptr<Effect> makeEmber (const EffectParams* params) { return std::make_unique<ember::Saturator> (params); }
  3668. std::unique_ptr<Effect> makeRidge (const EffectParams* params) { return std::make_unique<ridge::Strip> (params); }
  3669. std::unique_ptr<Effect> makeAether (const EffectParams* params) { return std::make_unique<aether::Air> (params); }
  3670. namespace
  3671. {
  3672. std::string hzText (double f)
  3673. {
  3674. char t[32];
  3675. if (f >= 1000.0)
  3676. std::snprintf (t, sizeof (t), "%.2f kHz", f / 1000.0);
  3677. else
  3678. std::snprintf (t, sizeof (t), "%.0f Hz", f);
  3679. return t;
  3680. }
  3681. std::string fmt (const char* f, double v)
  3682. {
  3683. char t[32];
  3684. std::snprintf (t, sizeof (t), f, v);
  3685. return t;
  3686. }
  3687. std::string onOff (float v) { return on (v) ? "on" : "off"; }
  3688. } // namespace
  3689. #include "core/StripSuite.inc" // round tp172c8: Fret Channel, Shell Channel
  3690. #include "core/StrikeSuite.inc" // round trans189d: Strike Shaper
  3691. #include "core/AnvilSuite.inc" // round 190fk: Anvil Bus
  3692. namespace glow
  3693. {
  3694. int vuModeOf (float v) noexcept { return v < 0.25f ? 0 : v < 0.75f ? 1 : 2; }
  3695. std::string classicParamText (int param, float v)
  3696. {
  3697. switch (param)
  3698. {
  3699. case peakReduction: case gain: case emphasis: return fmt ("%.1f", v * 100.0);
  3700. default: return paramText (EffectKind::glowLeveler, param, v);
  3701. }
  3702. }
  3703. } // namespace glow
  3704. namespace hush
  3705. {
  3706. int oversamplingOf (float v) noexcept { return 1 << std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 3.0f), 0, 3); }
  3707. std::string oldLawText (int param, float v)
  3708. {
  3709. switch (param)
  3710. {
  3711. case frequency: return hzText (hz (v));
  3712. case input: case output: return fmt ("%+.1f dB", lin (v, -12.0, 12.0));
  3713. default: return paramText (EffectKind::hushDeEsser, param, v);
  3714. }
  3715. }
  3716. std::string classicParamText (int param, float v)
  3717. {
  3718. switch (param)
  3719. {
  3720. case threshold: return fmt ("%.1f dB", classicThresholdDb (v));
  3721. case range: case lookahead: case release: return "-";
  3722. default: return oldLawText (param, v); // (the classic voicing keeps its ranges)
  3723. }
  3724. }
  3725. } // namespace hush
  3726. namespace ridge
  3727. {
  3728. std::string eParamText (int param, float v)
  3729. {
  3730. switch (param)
  3731. {
  3732. case lmfQ: case hmfQ: return fmt ("Q %.2f", eQ (v));
  3733. case lfGain: case lmfGain: case hmfGain: case hfGain: return fmt ("%+.1f dB", bandGainDb (v, false));
  3734. case compThreshold: return fmt ("%+.1f dB", eCompThresholdDb (v));
  3735. case gateRange: return v <= 0.0f ? "off" : fmt ("%.1f dB", eGateRangeDb (v));
  3736. case gateThreshold: return fmt ("%+.1f dB", eGateThresholdDb (v));
  3737. case curve: return "-";
  3738. default: return paramText (EffectKind::ridgeStrip, param, v);
  3739. }
  3740. }
  3741. std::string k4ParamText (int param, float v)
  3742. {
  3743. switch (param)
  3744. {
  3745. case lmfQ: case hmfQ: return fmt ("Q %.2f", k4Q (v));
  3746. case lpf: return v <= 0.0f || lpfHz (v) >= 15900.0 ? "off" : hzText (lpfHz (v));
  3747. case k4Colour: { static const char* names[3] { "brown", "black", "orange" }; return names[k4ColourIndex (v)]; }
  3748. case k4Mic: return fmt ("%.1f dB", k4MicDb (v));
  3749. case k4MakeupOffset: return fmt ("%+.1f dB", k4OffsetDb (v));
  3750. case analog: return on (v) ? "VCA on" : "VCA off";
  3751. default: return eParamText (param, v);
  3752. }
  3753. }
  3754. // round tp172c
  3755. std::string modernParamText (int param, float v)
  3756. {
  3757. switch (param)
  3758. {
  3759. case hpf: return v <= 0.0f ? "off" : hzText (mHpfHz (v));
  3760. case lpf: return v <= 0.0f ? "off" : hzText (mLpfKHz (v) * 1000.0);
  3761. case curve: return on (v) ? "G type" : "E type";
  3762. case lfGain: return fmt ("%+.1f dB", mLfGainDb (v));
  3763. case lmfGain: case hmfGain: case hfGain: return fmt ("%+.1f dB", mGainDb (v));
  3764. case lfFreq: return hzText (mLfHz (v));
  3765. case lmfFreq: return hzText (mLmfKHz (v) * 1000.0);
  3766. case hmfFreq: return hzText (mHmfKHz (v) * 1000.0);
  3767. case hfFreq: return hzText (mHfKHz (v) * 1000.0);
  3768. case lmfQ: case hmfQ: return fmt ("Q %.2f", mQ (v));
  3769. case compRatio: { const auto r = mRatio (v); return r > 1e6 ? "inf : 1" : fmt (r < 10.0 ? "%.1f : 1" : "%.0f : 1", r); }
  3770. case gateHold: return fmt ("%.2f s", mHoldSeconds (v));
  3771. case compPeak: return on (v) ? "peak" : "RMS";
  3772. case modern: return on (v) ? "modern console" : "off";
  3773. default: return k4ParamText (param, v);
  3774. }
  3775. }
  3776. std::string vocalParamText (int param, float v)
  3777. {
  3778. namespace vs = voxstrip;
  3779. switch (param)
  3780. {
  3781. case vocal: return on (v) ? "vocal strip" : "off";
  3782. case vInput: case vOutput: return fmt ("%+.1f dB", vs::gainDb (v));
  3783. case vOrder: return vs::orderName (vs::orderOf (v));
  3784. case vDsFreq: return hzText (vs::dsFreqHz (v));
  3785. case vDpFreq: return hzText (vs::dpFreqHz (v));
  3786. case vDsThreshold: case vDpThreshold: case vCompThreshold: return fmt ("%.1f dB", vs::thresholdDb (v));
  3787. case vDsSplit: case vDpSplit: return on (v) ? "split" : "broadband";
  3788. case vHpfFreq: return hzText (vs::hpfHz (v));
  3789. case vNotchFreq: return hzText (vs::notchHz (v));
  3790. case vNotchGain: return fmt ("%+.1f dB", vs::notchGainDb (v));
  3791. case vHighFreq: return hzText (vs::highHz (v));
  3792. case vHighGain: return fmt ("%+.1f dB", vs::highGainDb (v));
  3793. case vExpThreshold: return v <= 0.0f ? "off" : fmt ("%.1f dB", vs::expThresholdDb (v));
  3794. case vRatio: return fmt ("%.1f : 1", vs::ratio (v));
  3795. case vAttack: return fmt ("%.1f ms", vs::attackMs (v));
  3796. case vRelease: return fmt ("%.0f ms", vs::releaseMs (v));
  3797. case vMakeup: return fmt ("%+.1f dB", vs::makeupDb (v));
  3798. case vKnee: return on (v) ? "soft" : "hard";
  3799. default: return onOff (v);
  3800. }
  3801. }
  3802. } // namespace ridge
  3803. std::string paramText (EffectKind kind, int param, float v)
  3804. {
  3805. if (kind == EffectKind::fretChannel)
  3806. return fretParamText (param, v);
  3807. if (kind == EffectKind::shellChannel)
  3808. return shellParamText (param, v);
  3809. if (kind == EffectKind::strikeShaper)
  3810. return strikeParamText (param, v);
  3811. if (kind == EffectKind::anvilBus)
  3812. return anvilParamText (param, v);
  3813. if (kind == EffectKind::kilnChannel)
  3814. {
  3815. namespace k = kiln;
  3816. switch (param)
  3817. {
  3818. case k::input: case k::output: return fmt ("%+.1f dB", k::trimDb (v));
  3819. case k::mainIn: case k::mainOut: return fmt ("%+.1f dB", k::mainDb (v));
  3820. case k::console: return k::consoleName (k::consoleStep (v));
  3821. case k::drive: return fmt ("%.1f dB", k::driveDb (v));
  3822. case k::group: return k::groupOf (v) == 0 ? std::string ("None") : std::to_string (k::groupOf (v));
  3823. default: return v >= 0.5f ? "On" : "Off";
  3824. }
  3825. }
  3826. if (kind == EffectKind::ridgeStrip)
  3827. {
  3828. using namespace ridge;
  3829. switch (param)
  3830. {
  3831. case input: case output: return fmt ("%+.1f dB", trimDb (v));
  3832. case hpf: return v <= 0.0f ? "off" : hzText (hpfHz (v));
  3833. case lpf: return v <= 0.0f ? "off" : hzText (lpfHz (v));
  3834. case lfGain: case lmfGain: case hmfGain: case hfGain: return fmt ("%+.1f dB", bandGainDb (v, false)) + " / " + fmt ("%+.1f", bandGainDb (v, true));
  3835. case lfFreq: return hzText (lfHz (v));
  3836. case lmfFreq: return hzText (lmfHz (v));
  3837. case hmfFreq: return hzText (hmfHz (v));
  3838. case hfFreq: return hzText (hfHz (v));
  3839. case lmfQ: case hmfQ: return fmt ("Q %.2f", bandQ (v));
  3840. case lfBell: case hfBell: return on (v) ? "bell" : "shelf";
  3841. case curve: return on (v) ? "black" : "brown";
  3842. case compRatio: return v >= 0.999f ? "inf : 1" : fmt ("%.1f : 1", ratio (v));
  3843. case compThreshold: return fmt ("%.1f dB", compThresholdDb (v));
  3844. case compRelease: case gateRelease: return fmt ("%.2f s", releaseSeconds (v));
  3845. case gateRange: return v <= 0.0f ? "off" : fmt ("%.0f dB", gateRangeDb (v));
  3846. case gateThreshold: return fmt ("%.1f dB", gateThresholdDb (v));
  3847. case expander: return on (v) ? "expander" : "gate";
  3848. case order: return on (v) ? "dyn > EQ" : "EQ > dyn";
  3849. case voicing: return on (v) ? "E console" : "classic";
  3850. case console4k: return on (v) ? "4K console" : "off";
  3851. case modern: case compPeak: case gateHold: return modernParamText (param, v); // round tp172c
  3852. case k4Colour: case k4Mic: case k4MakeupOffset: return k4ParamText (param, v);
  3853. case filterSplit: return on (v) ? "split" : "with EQ";
  3854. case vocal: case vInput: case vOutput: case vOrder: case vDsFreq: case vDsThreshold: case vDsSplit:
  3855. case vDpFreq: case vDpThreshold: case vDpSplit: case vHpfFreq: case vNotchFreq: case vNotchGain: case vHighFreq:
  3856. case vHighGain: case vExpThreshold: case vCompThreshold: case vRatio: case vAttack: case vRelease: case vMakeup:
  3857. case vKnee:
  3858. return vocalParamText (param, v);
  3859. case phase: return on (v) ? "inverted" : "normal";
  3860. case compMix: return fmt ("%.0f%%", v * 100.0);
  3861. // round tp172c6: the DRIVE section (the amount reads in the selected model's units; see driveParamText)
  3862. case driveModel: return driveModelName (driveModelIndex (v));
  3863. case driveAmount: return fmt ("%.1f", v * 10.0);
  3864. // (whole dB from ±10 on: the read-out fits under the small dial)
  3865. case driveInput: case driveTrim:
  3866. {
  3867. const auto db = param == driveInput ? driveInputDb (v) : driveTrimDb (v);
  3868. return std::abs (db) >= 9.95 ? fmt ("%+.0f dB", db) : fmt ("%+.1f dB", db);
  3869. }
  3870. case driveMix: return fmt ("%.0f%%", v * 100.0);
  3871. default: return onOff (v);
  3872. }
  3873. }
  3874. if (kind == EffectKind::glowLeveler)
  3875. {
  3876. using namespace glow;
  3877. switch (param)
  3878. {
  3879. case peakReduction: return fmt ("%.0f", v * 100.0);
  3880. case gain: return fmt ("%+.1f dB", gainDb (v));
  3881. case mode: return on (v) ? "limit" : "compress";
  3882. case emphasis: case mix: return fmt ("%.0f%%", v * 100.0);
  3883. case trim: return fmt ("%+.1f dB", trimDb (v));
  3884. case autoMakeup: return on (v) ? "on" : "off";
  3885. case optoLaw: return on (v) ? "measured" : "round 123";
  3886. case releaseLaw: return on (v) ? "measured" : "round 123";
  3887. case makeupRef: return fmt ("%.0f", v * 100.0);
  3888. case vuDisplay: return vuModeOf (v) == 1 ? "input" : vuModeOf (v) == 2 ? "output" : "gain reduction";
  3889. case optoMemory: return on (v) ? "on" : "off";
  3890. case analog: return analogHz (v) == 0 ? "off" : analogHz (v) == 50 ? "50 Hz" : "60 Hz";
  3891. default: return fmt ("%.2f", v);
  3892. }
  3893. }
  3894. if (kind == EffectKind::hushDeEsser)
  3895. {
  3896. using namespace hush;
  3897. switch (param)
  3898. {
  3899. // round 165x: new instances use the reference's ranges (the old law's texts: oldLawText)
  3900. case frequency: return hzText (hpHz (v, true));
  3901. case threshold: return fmt ("%.1f dB", thresholdDb (v));
  3902. case range: return fmt ("%.1f dB", rangeDb (v));
  3903. case mode: return on (v) ? "wideband" : "split";
  3904. case detector: return on (v) ? "band" : "high-pass";
  3905. case allround: return on (v) ? "allround" : "single vocal";
  3906. case lowPass: return v >= 0.999f ? "off" : hzText (lowPassHz (v));
  3907. case lookaheadTime: return fmt ("%.1f ms", lookaheadTimeMs (v));
  3908. case stereoLink: return fmt ("%.0f%%", v * 100.0);
  3909. case linkMid: return on (v) ? "mid" : "stereo";
  3910. case external: return on (v) ? "external" : "internal";
  3911. case input: case output: return v <= 0.0f ? std::string ("-inf dB") : fmt ("%+.1f dB", levelDb (v, true));
  3912. case display: return on (v) ? "shown" : "hidden";
  3913. case release: return fmt ("%.0f ms", releaseMs (v));
  3914. case rangeLaw: return on (v) ? "extended" : "round 165w";
  3915. case linkOnly: return fmt ("%.0f%%", v * 100.0);
  3916. case linkSide: return on (v) ? "side" : "mid";
  3917. case air: return v <= 0.0f ? "off" : fmt ("+%.1f dB", airDb (v));
  3918. case mix: return fmt ("%.0f%%", v * 100.0);
  3919. case inputPan: case outputPan:
  3920. {
  3921. const auto pan = panValue (v);
  3922. return std::abs (pan) < 0.0005 ? std::string ("centre") : fmt (pan < 0.0 ? "%.0f%% L" : "%.0f%% R", std::abs (pan) * 100.0);
  3923. }
  3924. case voicing: return on (v) ? "classic split" : "modern";
  3925. case oversampling: return oversamplingOf (v) == 1 ? "off" : std::to_string (oversamplingOf (v)) + "x";
  3926. default: return onOff (v);
  3927. }
  3928. }
  3929. if (kind == EffectKind::aether)
  3930. {
  3931. using namespace aether;
  3932. switch (param)
  3933. {
  3934. case air: return fmt ("%+.1f dB", airDb (v));
  3935. case airFreq: return hzText (airHz (v));
  3936. case exciterFreq: return hzText (exciterHz (v));
  3937. case character: return v < 0.05f ? "even" : (v > 0.95f ? "odd" : fmt ("%.0f%% odd", v * 100.0));
  3938. case output: return fmt ("%+.1f dB", lin (v, -12.0, 12.0));
  3939. case dynamic: return onOff (v);
  3940. default: return fmt ("%.0f%%", v * 100.0);
  3941. }
  3942. }
  3943. if (kind == EffectKind::ember)
  3944. {
  3945. using namespace ember;
  3946. switch (param)
  3947. {
  3948. case drive: return fmt ("%+.1f dB", driveDb (v)) + fmt (" (%.1f)", driveSteps (v)); // round 135: + the 0 … 10 scale
  3949. case punish: case thump: case wideCuts: return onOff (v);
  3950. case steep: return on (v) ? "30 dB/oct" : "gentle";
  3951. case voice: return voiceName (voiceIndex (v));
  3952. case lowCutWide: return v <= 0.0f ? "off" : hzText (lowCutWideHz (v));
  3953. case highCutWide: return v >= 1.0f ? "off" : hzText (highCutWideHz (v));
  3954. case style:
  3955. {
  3956. static const char* names[] = { "tube", "tape", "transistor", "fuzz" };
  3957. return names[(int) styleOf (v)];
  3958. }
  3959. case tone: return fmt ("%+.0f%%", toneTilt (v) * 100.0);
  3960. case lowCut: return v <= 0.0f ? "off" : hzText (lowCutHz (v));
  3961. case highCut: return v >= 1.0f ? "off" : hzText (highCutHz (v));
  3962. case mix: return fmt ("%.0f%%", v * 100.0);
  3963. case output: return fmt ("%+.1f dB", lin (v, -18.0, 18.0));
  3964. case autoGain: return onOff (v);
  3965. case quality: return std::to_string (oversampling (v)) + "x";
  3966. case bands: return bandCount (v) == 1 ? "1 band" : std::to_string (bandCount (v)) + " bands";
  3967. case xover1: case xover2: case xover3: case xover4: case xover5: return hzText (crossoverHz (v));
  3968. case bandsMore: { const auto m = (int) std::lround (std::clamp (v, 0.0f, 1.0f) * 2.0f); return m == 0 ? std::string ("off") : std::to_string (firstBands + m) + " bands"; }
  3969. case band1Character: case band2Character: case band3Character: case band4Character: case band5Character: case band6Character:
  3970. return characterName (characterIndex (v));
  3971. case xoverSlope: return std::to_string (slopeDbOct (v)) + " dB/oct";
  3972. // round 152
  3973. case crossSlope1: case crossSlope2: case crossSlope3: case crossSlope4: case crossSlope5:
  3974. return ownSlopeDbOct (v) == 0 ? std::string ("global") : std::to_string (ownSlopeDbOct (v)) + " dB/oct";
  3975. case processing: return on (v) ? "Linear phase" : "Minimum phase";
  3976. case lfo1Rate: case lfo2Rate: return fmt (lfoRateHz (v) < 10.0 ? "%.2f Hz" : "%.1f Hz", lfoRateHz (v));
  3977. case lfo1Shape: case lfo2Shape:
  3978. {
  3979. static const char* names[lfoShapes] { "Sine", "Triangle", "Square", "Ease in", "Ease out" };
  3980. return names[(int) lfoShapeOf (v)];
  3981. }
  3982. case envAttack: return fmt ("%.1f ms", envAttackMs (v));
  3983. case envRelease: return envReleaseMs (v) < 1000.0 ? fmt ("%.0f ms", envReleaseMs (v)) : fmt ("%.2f s", envReleaseMs (v) / 1000.0);
  3984. case mod1Source: case mod2Source: case mod3Source: case mod4Source:
  3985. {
  3986. static const char* names[modSources] { "Off", "LFO 1", "LFO 2", "Envelope" };
  3987. return names[(int) modSourceOf (v)];
  3988. }
  3989. case mod1Target: case mod2Target: case mod3Target: case mod4Target: return modTargetName (modTargetOf (v));
  3990. case mod1Amount: case mod2Amount: case mod3Amount: case mod4Amount: return fmt ("%+.3f", modAmount (v));
  3991. case effectLaw: return on (v) ? "measured" : "static";
  3992. case classicAuto: return onOff (v);
  3993. case classicLaw: return on (v) ? "measured" : "round 136";
  3994. case classicHot: return on (v) ? "measured" : "round 187";
  3995. case band2Style: case band3Style: case band4Style: case band5Style: case band6Style:
  3996. {
  3997. static const char* names[] = { "tube", "tape", "transistor", "fuzz" };
  3998. return names[(int) styleOf (v)];
  3999. }
  4000. case band2Drive: case band3Drive: case band4Drive: case band5Drive: case band6Drive: return fmt ("%+.1f dB", driveDb (v));
  4001. case band1Mix: case band2Mix: case band3Mix: case band4Mix: case band5Mix: case band6Mix: return fmt ("%.0f%%", v * 100.0);
  4002. case band1Level: case band2Level: case band3Level: case band4Level: case band5Level: case band6Level: return fmt ("%+.1f dB", bandLevelDb (v));
  4003. case inputGain: return fmt ("%+.1f dB", inputDb (v));
  4004. // round 136: the Classic voicing, in the reference's own units
  4005. case voicing: return on (v) ? "Classic" : "Ember";
  4006. case classicStyle: { static const char* names[] = { "A", "E", "N", "T", "P" }; return names[emberclassic::styleOf (v)]; }
  4007. case classicDrive: return fmt ("%.1f", emberclassic::driveUnits (v));
  4008. case classicPunish: case classicThump: return onOff (v);
  4009. case classicSteep: return on (v) ? "steep" : "gentle";
  4010. case classicLowCut: return hzText (emberclassic::lowCutHz (v));
  4011. case classicTone: return fmt ("%+.1f dB", emberclassic::toneDb (v));
  4012. case classicHighCut: return hzText (emberclassic::highCutHz (v));
  4013. case classicMix: return fmt ("%.0f%%", v * 100.0);
  4014. case classicOutput: return fmt ("%+.1f dB", emberclassic::outputDb (v));
  4015. // round 147
  4016. case channelMode: return on (v) ? "Mid/Side" : "Left/Right";
  4017. case band1DrivePan: case band2DrivePan: case band3DrivePan: case band4DrivePan: case band5DrivePan: case band6DrivePan:
  4018. {
  4019. const auto pan = panOf (v);
  4020. return std::abs (pan) < 0.005 ? std::string ("centre") : fmt ("%.0f%% ", std::abs (pan) * 100.0) + (pan < 0.0 ? "L/M" : "R/S");
  4021. }
  4022. case band1Pan: case band2Pan: case band3Pan: case band4Pan: case band5Pan: case band6Pan: case inputPan: case outputPan:
  4023. {
  4024. const auto [a, b] = panScales (panOf (v));
  4025. const auto db = [] (float g) { return g <= 0.0f ? std::string ("-inf") : fmt ("%.1f", gainToDb (g)); };
  4026. return std::abs (panOf (v)) < 0.005 ? std::string ("centre") : db (a) + " / " + db (b) + " dB";
  4027. }
  4028. default:
  4029. if ((param >= band1On && param < band1On + firstBands * extraFields) || (param >= band5On && param < band5On + (maxBands - firstBands) * extraFields))
  4030. switch ((param - (param >= band5On ? (int) band5On : (int) band1On)) % extraFields)
  4031. {
  4032. case 0: return onOff (v);
  4033. case 1: { static const char* names[] = { "normal", "solo", "mute" }; return names[(int) bandState (v)]; }
  4034. case 2: case 3: case 4: case 5: return fmt ("%+.1f dB", toneDb (v));
  4035. case 6: return fmt ("%.0f%%", feedbackAmount (v) * 100.0);
  4036. case 7: return hzText (feedbackHz (v));
  4037. default: return fmt ("%+.2f", dynamicsAmount (v));
  4038. }
  4039. return fmt ("%.2f", v);
  4040. }
  4041. }
  4042. return fmt ("%.2f", v);
  4043. }
  4044. } // namespace lynx::mixsuite