MixSuite.cpp 209 KB

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