4
0

MixSuite.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. #include "core/MixSuite.h"
  2. #include <algorithm>
  3. #include <atomic>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include "core/Biquad.h"
  7. #include "core/Effects.h"
  8. #include "core/LrCrossover.h"
  9. #include "core/VoiceMatch.h"
  10. // REA_rodada96. Every formula below is textbook material, cited where it matters:
  11. // - biquads: R. Bristow-Johnson, "Cookbook formulae for audio EQ biquad filter coefficients";
  12. // - half-band FIR oversampling with a Kaiser window: Oppenheim & Schafer, "Discrete-Time Signal Processing", 7.6
  13. // and 4.7 (multirate), Kaiser's design formula for beta;
  14. // - compressor / gate gain computer with a soft knee and the "smooth branching" peak detector in the log domain:
  15. // D. Giannoulis, M. Massberg, J. D. Reiss, "Digital Dynamic Range Compressor Design — A Tutorial and Analysis",
  16. // JAES 60(6), 2012;
  17. // - the electro-optical cell (an electroluminescent panel lighting a photoresistor in a feedback leveler): the
  18. // published behaviour of such cells — about 10 ms attack, a release in two stages (roughly half the reduction
  19. // within ~60 ms, the rest over 0.5 to several seconds, longer after long or heavy reduction) — as described in
  20. // the public service literature and circuit analyses of opto levelers; the model (a fast and a slow conductance
  21. // and a memory that lengthens the slow release) is our own;
  22. // - waveshaping and its harmonics: U. Zölzer (ed.), "DAFX: Digital Audio Effects", 2nd ed., ch. 4 (nonlinear
  23. // processing: tube / tape / distortion curves, exciters and enhancers), and J. Pakarinen, D. T. Yeh, "A Review of
  24. // Digital Techniques for Modeling Vacuum-Tube Guitar Amplifiers", CMJ 33(2), 2009 (asymmetric curves → even
  25. // harmonics).
  26. namespace lynx::mixsuite
  27. {
  28. namespace
  29. {
  30. constexpr double pi = 3.141592653589793;
  31. inline double dbToGain (double db) noexcept { return std::pow (10.0, db / 20.0); }
  32. inline double gainToDb (double g) noexcept { return 20.0 * std::log10 (std::max (g, 1e-12)); }
  33. inline double logMap (float v, double lo, double hi) noexcept { return lo * std::pow (hi / lo, (double) std::clamp (v, 0.0f, 1.0f)); }
  34. inline double lin (float v, double lo, double hi) noexcept { return lo + (hi - lo) * (double) std::clamp (v, 0.0f, 1.0f); }
  35. inline bool on (float v) noexcept { return v >= 0.5f; }
  36. // one-pole smoothing coefficient for a time constant in seconds
  37. inline double coef (double seconds, double sr) noexcept { return seconds <= 0.0 ? 0.0 : std::exp (-1.0 / (seconds * sr)); }
  38. // |H(e^jw)| of a biquad
  39. double biquadMag (const Biquad& b, double w)
  40. {
  41. const double c1 = std::cos (w), s1 = std::sin (w), c2 = std::cos (2 * w), s2 = std::sin (2 * w);
  42. const double nr = b.b0 + b.b1 * c1 + b.b2 * c2, ni = -(b.b1 * s1 + b.b2 * s2);
  43. const double dr = 1.0 + b.a1 * c1 + b.a2 * c2, di = -(b.a1 * s1 + b.a2 * s2);
  44. return std::sqrt ((nr * nr + ni * ni) / std::max (dr * dr + di * di, 1e-300));
  45. }
  46. // first-order high-pass (bilinear), stereo
  47. struct OnePoleHp
  48. {
  49. double b0 = 1, b1 = 0, a1 = 0;
  50. double x1[2] {}, y1[2] {};
  51. void set (double sr, double hz) noexcept
  52. {
  53. const auto k = std::tan (pi * std::min (hz, sr * 0.45) / sr);
  54. b0 = 1.0 / (1.0 + k);
  55. b1 = -b0;
  56. a1 = (k - 1.0) / (k + 1.0);
  57. }
  58. void reset() noexcept { x1[0] = x1[1] = y1[0] = y1[1] = 0.0; }
  59. float process (float x, int ch) noexcept
  60. {
  61. const auto y = b0 * x + b1 * x1[ch] - a1 * y1[ch];
  62. x1[ch] = x;
  63. y1[ch] = y;
  64. return (float) y;
  65. }
  66. double mag (double w) const
  67. {
  68. const double nr = b0 + b1 * std::cos (w), ni = -b1 * std::sin (w);
  69. const double dr = 1.0 + a1 * std::cos (w), di = -a1 * std::sin (w);
  70. return std::sqrt ((nr * nr + ni * ni) / (dr * dr + di * di));
  71. }
  72. };
  73. // Stereo delay line of fixed capacity (no allocation).
  74. template <int N>
  75. struct Delay2
  76. {
  77. std::array<float, N> l {}, r {};
  78. int pos = 0, len = 0;
  79. void setLength (int n) noexcept { len = std::clamp (n, 0, N - 1); }
  80. void reset() noexcept { l.fill (0.0f); r.fill (0.0f); pos = 0; }
  81. void process (float& a, float& b) noexcept
  82. {
  83. if (len == 0)
  84. return;
  85. l[(std::size_t) pos] = a;
  86. r[(std::size_t) pos] = b;
  87. auto rd = pos - len;
  88. if (rd < 0)
  89. rd += N;
  90. a = l[(std::size_t) rd];
  91. b = r[(std::size_t) rd];
  92. if (++pos == N)
  93. pos = 0;
  94. }
  95. };
  96. struct Meters
  97. {
  98. std::atomic<float> gr0 { 0.0f }, gr1 { 0.0f }, in { 0.0f }, out { 0.0f };
  99. float read (int k) const noexcept
  100. {
  101. switch (k)
  102. {
  103. case 0: return gr0.load (std::memory_order_relaxed);
  104. case 1: return gr1.load (std::memory_order_relaxed);
  105. case 2: return in.load (std::memory_order_relaxed);
  106. case 3: return out.load (std::memory_order_relaxed);
  107. default: return 0.0f;
  108. }
  109. }
  110. static float peak (const float* l, const float* r, int n) noexcept
  111. {
  112. float p = 0.0f;
  113. for (int i = 0; i < n; ++i)
  114. p = std::max (p, std::max (std::abs (l[i]), std::abs (r[i])));
  115. return std::min (p, 1.0f);
  116. }
  117. void set (float grDb0, float grDb1, float inPeak, float outPeak) noexcept
  118. {
  119. gr0.store (std::clamp (grDb0 / 30.0f, 0.0f, 1.0f), std::memory_order_relaxed);
  120. gr1.store (std::clamp (grDb1 / 30.0f, 0.0f, 1.0f), std::memory_order_relaxed);
  121. in.store (inPeak, std::memory_order_relaxed);
  122. out.store (outPeak, std::memory_order_relaxed);
  123. }
  124. };
  125. // ---- oversampler design ----
  126. constexpr int stageTaps[Oversampler::maxStages] = { 95, 23, 15 }; // N = 4k + 3: the centre index c = (N − 1) / 2 is odd
  127. constexpr double stageBeta[Oversampler::maxStages] = { 10.0, 9.0, 8.5 };
  128. double besselI0 (double x)
  129. {
  130. double sum = 1.0, term = 1.0;
  131. for (int k = 1; k < 50; ++k)
  132. {
  133. term *= (x / (2.0 * k)) * (x / (2.0 * k));
  134. sum += term;
  135. if (term < 1e-12 * sum)
  136. break;
  137. }
  138. return sum;
  139. }
  140. // latency of a factor's path in units of the highest rate, before the pad
  141. int rawUnits (int stages)
  142. {
  143. const int f = 1 << stages;
  144. int u = 0;
  145. for (int s = 0; s < stages; ++s)
  146. u += ((stageTaps[s] - 1) / 2) * f / (1 << s);
  147. return u;
  148. }
  149. int pathLatency (int stages)
  150. {
  151. const int f = 1 << stages;
  152. const int u = rawUnits (stages);
  153. return (u + (f - u % f) % f) / f;
  154. }
  155. } // namespace
  156. // ================================================================== Oversampler
  157. Oversampler::Oversampler()
  158. {
  159. for (int s = 0; s < maxStages; ++s)
  160. {
  161. auto& stage = st[(std::size_t) s];
  162. const int n = stageTaps[s];
  163. const int c = (n - 1) / 2;
  164. const auto i0b = besselI0 (stageBeta[s]);
  165. double sum = 0.0;
  166. std::array<double, 64> h {};
  167. for (int j = 0; 2 * j < n; ++j)
  168. {
  169. const int t = 2 * j - c; // odd offset from the centre
  170. const auto r = (double) (2 * j - c) / c;
  171. const auto w = besselI0 (stageBeta[s] * std::sqrt (std::max (0.0, 1.0 - r * r))) / i0b;
  172. h[(std::size_t) j] = std::sin (0.5 * pi * t) / (0.5 * pi * t) * w; // sinc(t/2), scaled below
  173. sum += h[(std::size_t) j];
  174. }
  175. stage.taps = (n + 1) / 2;
  176. for (int j = 0; j < stage.taps; ++j)
  177. stage.coef[(std::size_t) j] = (float) (h[(std::size_t) j] * 0.5 / sum); // the side taps sum to 0.5
  178. stage.centreDelay = (c - 1) / 2;
  179. }
  180. setFactor (4);
  181. }
  182. int Oversampler::latency() noexcept { return pathLatency (maxStages); }
  183. void Oversampler::setFactor (int factor) noexcept
  184. {
  185. const int s = factor >= 8 ? 3 : (factor >= 4 ? 2 : 1);
  186. if (s == stages && (hiPadLen != 0 || loPadLen != 0 || s == maxStages))
  187. return;
  188. stages = s;
  189. const int f = 1 << s;
  190. const int u = rawUnits (s);
  191. hiPadLen = (f - u % f) % f;
  192. loPadLen = latency() - pathLatency (s);
  193. reset();
  194. }
  195. void Oversampler::reset() noexcept
  196. {
  197. for (auto& s : st)
  198. {
  199. s.upHist.fill (0.0f);
  200. s.evenHist.fill (0.0f);
  201. s.oddHist.fill (0.0f);
  202. s.upPos = s.downPos = 0;
  203. }
  204. hiPad.fill (0.0f);
  205. loPad.fill (0.0f);
  206. hiPadPos = loPadPos = 0;
  207. }
  208. void Oversampler::up (Stage& s, float x, float& a, float& b) noexcept
  209. {
  210. const int L = s.taps;
  211. s.upPos = s.upPos == 0 ? L - 1 : s.upPos - 1;
  212. s.upHist[(std::size_t) s.upPos] = s.upHist[(std::size_t) (s.upPos + L)] = x;
  213. const float* hist = s.upHist.data() + s.upPos; // hist[j] = x[m − j]
  214. float acc = 0.0f;
  215. for (int j = 0; j < L; ++j)
  216. acc += s.coef[(std::size_t) j] * hist[j];
  217. a = 2.0f * acc; // y[2m]
  218. b = hist[s.centreDelay]; // y[2m + 1] = x[m − (c − 1) / 2]
  219. }
  220. float Oversampler::down (Stage& s, float a, float b) noexcept
  221. {
  222. const int L = s.taps;
  223. s.downPos = s.downPos == 0 ? L - 1 : s.downPos - 1;
  224. s.evenHist[(std::size_t) s.downPos] = s.evenHist[(std::size_t) (s.downPos + L)] = a;
  225. s.oddHist[(std::size_t) s.downPos] = s.oddHist[(std::size_t) (s.downPos + L)] = b;
  226. const float* even = s.evenHist.data() + s.downPos;
  227. float acc = 0.0f;
  228. for (int j = 0; j < L; ++j)
  229. acc += s.coef[(std::size_t) j] * even[j];
  230. return acc + 0.5f * s.oddHist[(std::size_t) (s.downPos + s.centreDelay + 1)]; // v[2m − c]
  231. }
  232. float Oversampler::padIn (float x) noexcept
  233. {
  234. if (hiPadLen == 0)
  235. return x;
  236. const auto y = hiPad[(std::size_t) hiPadPos];
  237. hiPad[(std::size_t) hiPadPos] = x;
  238. hiPadPos = (hiPadPos + 1) % hiPadLen;
  239. return y;
  240. }
  241. float Oversampler::padOut (float x) noexcept
  242. {
  243. if (loPadLen == 0)
  244. return x;
  245. const auto y = loPad[(std::size_t) loPadPos];
  246. loPad[(std::size_t) loPadPos] = x;
  247. loPadPos = (loPadPos + 1) % loPadLen;
  248. return y;
  249. }
  250. // ================================================================== Ridge Strip
  251. namespace ridge
  252. {
  253. double hpfHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 16.0, 350.0); }
  254. double lpfHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 22000.0, 3000.0); }
  255. double bandGainDb (float v, bool black) noexcept { return (2.0 * std::clamp (v, 0.0f, 1.0f) - 1.0) * (black ? 18.0 : 15.0); }
  256. double lfHz (float v) noexcept { return logMap (v, 30.0, 450.0); }
  257. double lmfHz (float v) noexcept { return logMap (v, 200.0, 2500.0); }
  258. double hmfHz (float v) noexcept { return logMap (v, 600.0, 7000.0); }
  259. double hfHz (float v) noexcept { return logMap (v, 1500.0, 16000.0); }
  260. double bandQ (float v) noexcept { return logMap (v, 0.5, 2.5); }
  261. 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); }
  262. double compThresholdDb (float v) noexcept { return lin (v, -40.0, 0.0); }
  263. double releaseSeconds (float v) noexcept { return logMap (v, 0.1, 4.0); }
  264. double gateRangeDb (float v) noexcept { return lin (v, 0.0, 60.0); }
  265. double gateThresholdDb (float v) noexcept { return lin (v, -80.0, 0.0); }
  266. double trimDb (float v) noexcept { return lin (v, -20.0, 20.0); }
  267. double eQ (float v) noexcept { return vmatch::echannel::qFromDial (1.0 - (double) std::clamp (v, 0.0f, 1.0f)); }
  268. double eCompThresholdDb (float v) noexcept { return lin (v, -20.0, 10.0); }
  269. double eGateRangeDb (float v) noexcept { return lin (v, 0.0, 40.0); }
  270. double eGateThresholdDb (float v) noexcept { return lin (v, -30.0, 10.0); }
  271. bool eVoicing (const EffectParams& p) noexcept { return on (p[voicing]); }
  272. namespace
  273. {
  274. constexpr double kneeDb = 6.0;
  275. // Giannoulis et al. 2012, eq. (4): soft-knee gain computer, output level for input level x (dB)
  276. double gainComputer (double x, double threshold, double r)
  277. {
  278. const auto over = x - threshold;
  279. if (2.0 * over < -kneeDb)
  280. return x;
  281. if (2.0 * std::abs (over) <= kneeDb)
  282. return x + (1.0 / r - 1.0) * (over + kneeDb / 2.0) * (over + kneeDb / 2.0) / (2.0 * kneeDb);
  283. return threshold + over / r;
  284. }
  285. // the EQ section's filters (shared by the processor and responseDb)
  286. struct EqDesign
  287. {
  288. OnePoleHp hp1;
  289. Biquad hp2, lp, lf, lmf, hmf, hf;
  290. bool hpOn = false, lpOn = false;
  291. void design (const EffectParams& p, double sr)
  292. {
  293. const bool black = on (p[curve]);
  294. hpOn = p[hpf] > 0.0f;
  295. lpOn = p[lpf] > 0.0f;
  296. if (hpOn)
  297. {
  298. // 3rd-order Butterworth high-pass: a first-order section and a second-order one with Q = 1
  299. hp1.set (sr, hpfHz (p[hpf]));
  300. hp2.filter (sr, 1, hpfHz (p[hpf]), 1.0);
  301. }
  302. if (lpOn)
  303. lp.filter (sr, 0, std::min (lpfHz (p[lpf]), sr * 0.45), 0.70710678);
  304. const auto nyq = sr * 0.49;
  305. // shelves: "brown" curves are plain (S = 1, no overshoot); "black" ones have the resonant shelf with the small
  306. // dip / bump before the shelf that many console EQs show (S > 1)
  307. const auto shelfS = black ? 1.6 : 1.0;
  308. const auto lg = bandGainDb (p[lfGain], black), hg = bandGainDb (p[hfGain], black);
  309. if (on (p[lfBell]))
  310. lf.peaking (sr, lfHz (p[lfFreq]), black ? propQ (0.9, lg) : 0.9, lg);
  311. else
  312. lf.shelf (sr, lfHz (p[lfFreq]), lg, false, shelfS);
  313. if (on (p[hfBell]))
  314. hf.peaking (sr, std::min (hfHz (p[hfFreq]), nyq), black ? propQ (0.9, hg) : 0.9, hg);
  315. else
  316. hf.shelf (sr, std::min (hfHz (p[hfFreq]), nyq), hg, true, shelfS);
  317. const auto mg = bandGainDb (p[lmfGain], black), ng = bandGainDb (p[hmfGain], black);
  318. lmf.peaking (sr, lmfHz (p[lmfFreq]), black ? propQ (bandQ (p[lmfQ]), mg) : bandQ (p[lmfQ]), mg);
  319. hmf.peaking (sr, std::min (hmfHz (p[hmfFreq]), nyq), black ? propQ (bandQ (p[hmfQ]), ng) : bandQ (p[hmfQ]), ng);
  320. }
  321. // "black" bells: proportional Q — wide for small boosts / cuts, narrower as the gain grows
  322. static double propQ (double q, double gainDb) { return std::max (0.2, q * (0.5 + std::abs (gainDb) / 12.0)); }
  323. };
  324. } // namespace
  325. double responseDb (const EffectParams& p, double sampleRate, double hz)
  326. {
  327. EqDesign d;
  328. d.design (p, sampleRate);
  329. const auto w = 2.0 * pi * hz / sampleRate;
  330. double m = 1.0;
  331. if (d.hpOn)
  332. m *= d.hp1.mag (w) * biquadMag (d.hp2, w);
  333. if (d.lpOn)
  334. m *= biquadMag (d.lp, w);
  335. if (on (p[eqOn]))
  336. m *= biquadMag (d.lf, w) * biquadMag (d.lmf, w) * biquadMag (d.hmf, w) * biquadMag (d.hf, w);
  337. return gainToDb (m);
  338. }
  339. double compressorCurveDb (const EffectParams& p, double inputDb)
  340. {
  341. return gainComputer (inputDb, compThresholdDb (p[compThreshold]), ratio (p[compRatio]));
  342. }
  343. namespace
  344. {
  345. class Strip final : public Effect
  346. {
  347. public:
  348. void prepare (double rate) noexcept override
  349. {
  350. sr = rate;
  351. console.prepare (rate);
  352. reset();
  353. }
  354. void reset() noexcept override
  355. {
  356. console.reset();
  357. for (auto* d : { &audio, &side })
  358. {
  359. d->hp1.reset(); d->hp2.reset(); d->lp.reset(); d->lf.reset(); d->lmf.reset(); d->hmf.reset(); d->hf.reset();
  360. }
  361. compGr = 0.0;
  362. gateGr = 0.0;
  363. levelEnv = 0.0;
  364. gateOpen = false;
  365. }
  366. float controlOutput (int k) const noexcept override { return meters.read (k); }
  367. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  368. {
  369. if (eVoicing (p))
  370. {
  371. const auto inPeak = Meters::peak (l, r, n);
  372. console.process (l, r, n, consoleSettings (p));
  373. meters.set (console.compReductionDb(), console.gateReductionDb(), inPeak, Meters::peak (l, r, n));
  374. return;
  375. }
  376. audio.design (p, sr);
  377. side.design (p, sr);
  378. const auto inPeak = Meters::peak (l, r, n);
  379. const auto inGain = (float) dbToGain (trimDb (p[input]));
  380. const auto outGain = (float) (dbToGain (trimDb (p[output])) * (on (p[phase]) ? -1.0 : 1.0));
  381. const bool eq = on (p[eqOn]), dyn = on (p[dynOn]), eqFirst = ! on (p[order]), fts = on (p[filtersToSidechain]);
  382. // compressor
  383. const auto thr = compThresholdDb (p[compThreshold]), rat = ratio (p[compRatio]);
  384. const auto compAttack = coef (on (p[compFastAttack]) ? 0.001 : 0.010, sr);
  385. const auto compRel = coef (releaseSeconds (p[ridge::compRelease]), sr);
  386. const auto cmix = (float) std::clamp (p[compMix], 0.0f, 1.0f);
  387. const bool compOn = p[compRatio] > 0.0005f;
  388. // gate / expander
  389. const auto range = gateRangeDb (p[gateRange]), gthr = gateThresholdDb (p[gateThreshold]);
  390. const bool gateOn = range > 0.05, expand = on (p[expander]);
  391. const auto gateAttack = coef (on (p[gateFastAttack]) ? 0.0001 : 0.0015, sr);
  392. const auto gateRel = coef (releaseSeconds (p[ridge::gateRelease]), sr);
  393. const auto levelRelease = coef (0.030, sr);
  394. float maxComp = 0.0f, maxGate = 0.0f;
  395. for (int i = 0; i < n; ++i)
  396. {
  397. float a = l[i] * inGain, b = r[i] * inGain;
  398. if (! fts)
  399. filters (audio, a, b);
  400. if (eq && eqFirst)
  401. bands (audio, a, b);
  402. if (dyn && (compOn || gateOn))
  403. {
  404. float sa = a, sb = b;
  405. if (fts)
  406. filters (side, sa, sb);
  407. const auto rect = (double) std::max (std::abs (sa), std::abs (sb));
  408. // level detector: instant attack, 30 ms release (keeps the ripple of low notes out of the gain)
  409. levelEnv = rect > levelEnv ? rect : levelRelease * levelEnv + (1.0 - levelRelease) * rect;
  410. const auto peak = levelEnv;
  411. // gate / expander first (as in console channel dynamics), then the compressor
  412. if (gateOn)
  413. {
  414. const auto lvl = gainToDb (peak);
  415. double target;
  416. if (expand) // 1:2 downward expansion below the threshold, limited to the range
  417. target = lvl < gthr ? std::min (range, (gthr - lvl)) : 0.0;
  418. else
  419. {
  420. // hysteresis: opens at the threshold, closes 4 dB below it
  421. if (lvl >= gthr)
  422. gateOpen = true;
  423. else if (lvl < gthr - 4.0)
  424. gateOpen = false;
  425. target = gateOpen ? 0.0 : range;
  426. }
  427. gateGr = target < gateGr ? gateAttack * gateGr + (1.0 - gateAttack) * target
  428. : gateRel * gateGr + (1.0 - gateRel) * target;
  429. }
  430. else
  431. gateGr = 0.0;
  432. if (compOn)
  433. {
  434. const auto x = gainToDb (peak) - gateGr;
  435. const auto want = x - gainComputer (x, thr, rat);
  436. compGr = want > compGr ? compAttack * compGr + (1.0 - compAttack) * want
  437. : compRel * compGr + (1.0 - compRel) * want;
  438. }
  439. else
  440. compGr = 0.0;
  441. const auto g = (float) dbToGain (-gateGr);
  442. const auto c = (float) dbToGain (-compGr);
  443. const auto wet = g * (cmix * c + (1.0f - cmix));
  444. a *= wet;
  445. b *= wet;
  446. maxComp = std::max (maxComp, (float) compGr);
  447. maxGate = std::max (maxGate, (float) gateGr);
  448. }
  449. if (eq && ! eqFirst)
  450. bands (audio, a, b);
  451. l[i] = a * outGain;
  452. r[i] = b * outGain;
  453. }
  454. meters.set (maxComp, maxGate, inPeak, Meters::peak (l, r, n));
  455. }
  456. private:
  457. // E voicing: Ridge's controls in the console's units (the shared ranges use Ridge's own laws)
  458. static vmatch::echannel::Settings consoleSettings (const EffectParams& p) noexcept
  459. {
  460. vmatch::echannel::Settings e;
  461. e.inputDb = trimDb (p[input]);
  462. e.outputDb = trimDb (p[output]);
  463. e.phase = on (p[phase]);
  464. e.hpHz = p[hpf] > 0.0f ? hpfHz (p[hpf]) : 0.0;
  465. e.lpKHz = p[lpf] > 0.0f ? lpfHz (p[lpf]) / 1000.0 : 0.0;
  466. e.lfGain = bandGainDb (p[lfGain], false);
  467. e.lfHz = lfHz (p[lfFreq]);
  468. e.lfBell = on (p[lfBell]);
  469. e.lmfGain = bandGainDb (p[lmfGain], false);
  470. e.lmfKHz = lmfHz (p[lmfFreq]) / 1000.0;
  471. e.lmfQ = eQ (p[lmfQ]);
  472. e.hmfGain = bandGainDb (p[hmfGain], false);
  473. e.hmfKHz = hmfHz (p[hmfFreq]) / 1000.0;
  474. e.hmfQ = eQ (p[hmfQ]);
  475. e.hfGain = bandGainDb (p[hfGain], false);
  476. e.hfKHz = hfHz (p[hfFreq]) / 1000.0;
  477. e.hfBell = on (p[hfBell]);
  478. e.eqOn = on (p[eqOn]);
  479. e.compRatio = p[compRatio] <= 0.0005f ? 1.0 : ratio (p[compRatio]);
  480. e.compThreshold = eCompThresholdDb (p[compThreshold]);
  481. e.compRelease = releaseSeconds (p[ridge::compRelease]);
  482. e.compFast = on (p[compFastAttack]);
  483. e.expRange = eGateRangeDb (p[gateRange]);
  484. e.expThreshold = eGateThresholdDb (p[gateThreshold]);
  485. e.expRelease = releaseSeconds (p[ridge::gateRelease]);
  486. e.expFast = on (p[gateFastAttack]);
  487. e.gate = ! on (p[expander]);
  488. e.dynOn = on (p[dynOn]);
  489. e.dynamicsAfterEq = ! on (p[order]);
  490. e.filtersBeforeDynamics = on (p[filterSplit]);
  491. return e;
  492. }
  493. vmatch::echannel::Channel console;
  494. static void filters (EqDesign& d, float& a, float& b) noexcept
  495. {
  496. if (d.hpOn)
  497. {
  498. a = d.hp2.process (d.hp1.process (a, 0), 0);
  499. b = d.hp2.process (d.hp1.process (b, 1), 1);
  500. }
  501. if (d.lpOn)
  502. {
  503. a = d.lp.process (a, 0);
  504. b = d.lp.process (b, 1);
  505. }
  506. }
  507. static void bands (EqDesign& d, float& a, float& b) noexcept
  508. {
  509. a = d.hf.process (d.hmf.process (d.lmf.process (d.lf.process (a, 0), 0), 0), 0);
  510. b = d.hf.process (d.hmf.process (d.lmf.process (d.lf.process (b, 1), 1), 1), 1);
  511. }
  512. double sr = 44100.0;
  513. EqDesign audio, side;
  514. double compGr = 0.0, gateGr = 0.0, levelEnv = 0.0;
  515. bool gateOpen = false;
  516. Meters meters;
  517. };
  518. } // namespace
  519. } // namespace ridge
  520. // ================================================================== Glow Leveler
  521. namespace glow
  522. {
  523. double gainDb (float v) noexcept { return lin (v, 0.0, 30.0); }
  524. double sidechainDb (float v) noexcept { return lin (v, -20.0, 40.0); }
  525. bool classic (const EffectParams& p) noexcept { return on (p[voicing]); }
  526. double trimDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  527. namespace
  528. {
  529. class Leveler final : public Effect
  530. {
  531. public:
  532. void prepare (double rate) noexcept override
  533. {
  534. sr = rate;
  535. panelAttack = coef (0.001, sr);
  536. panelRelease = coef (0.030, sr);
  537. fastAttack = coef (0.010, sr);
  538. fastRelease = coef (0.050, sr);
  539. slowAttack = coef (0.400, sr);
  540. memoryUp = coef (2.0, sr);
  541. memoryDown = coef (8.0, sr);
  542. autoCoef = coef (0.5, sr);
  543. classicEngine.prepare (rate);
  544. reset();
  545. }
  546. void reset() noexcept override
  547. {
  548. emph.reset();
  549. classicEngine.reset();
  550. panel = cellFast = cellSlow = memory = autoGr = 0.0;
  551. lastL = lastR = 0.0f;
  552. }
  553. float controlOutput (int k) const noexcept override { return meters.read (k); }
  554. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  555. {
  556. const auto inPeak = Meters::peak (l, r, n);
  557. if (classic (p))
  558. {
  559. vmatch::opto::Settings c;
  560. c.peakReduction = 100.0 * std::clamp ((double) p[peakReduction], 0.0, 1.0);
  561. c.gain = 100.0 * std::clamp ((double) p[gain], 0.0, 1.0);
  562. c.hiFreq = 100.0 * std::clamp ((double) p[emphasis], 0.0, 1.0);
  563. c.limit = on (p[mode]);
  564. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  565. const auto trimGain = (float) dbToGain (trimDb (p[trim]));
  566. for (int off = 0; off < n; off += maxBlock)
  567. {
  568. const int m = std::min (maxBlock, n - off);
  569. std::copy (l + off, l + off + m, dryL.begin());
  570. std::copy (r + off, r + off + m, dryR.begin());
  571. classicEngine.process (l + off, r + off, m, c);
  572. for (int i = 0; i < m; ++i)
  573. {
  574. l[off + i] = (l[off + i] * mx + dryL[(std::size_t) i] * (1.0f - mx)) * trimGain;
  575. r[off + i] = (r[off + i] * mx + dryR[(std::size_t) i] * (1.0f - mx)) * trimGain;
  576. }
  577. }
  578. meters.set (classicEngine.reductionDb(), 0.0f, inPeak, Meters::peak (l, r, n));
  579. return;
  580. }
  581. const auto sc = p[peakReduction] <= 0.0f ? 0.0 : dbToGain (sidechainDb (p[peakReduction]));
  582. const auto gamma = on (p[mode]) ? 9.0 : 2.0; // asymptotic ratio 1 + gamma: about 3:1 (compress), 10:1 (limit)
  583. emph.shelf (sr, 2500.0, 10.0 * std::clamp ((double) p[emphasis], 0.0, 1.0), true, 0.7);
  584. const auto makeup = (float) dbToGain (gainDb (p[gain]));
  585. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  586. const auto trimGain = (float) dbToGain (trimDb (p[trim]));
  587. const auto autoUp = on (p[autoMakeup]);
  588. double maxGr = 0.0;
  589. for (int i = 0; i < n; ++i)
  590. {
  591. // feedback detection: the cell sees the leveler's own output (the previous sample)
  592. const auto s = std::max (std::abs (emph.process (lastL, 0)), std::abs (emph.process (lastR, 1)));
  593. panel = s > panel ? panelAttack * panel + (1.0 - panelAttack) * s : panelRelease * panel + (1.0 - panelRelease) * s;
  594. const auto light = std::pow (sc * panel, gamma);
  595. // the cell: a fast conductance (attack ~10 ms, release ~50 ms) and a slow one whose release lengthens
  596. // with the memory of past reduction (0.4 s fresh, up to 5 s after long, heavy leveling)
  597. cellFast = light > cellFast ? fastAttack * cellFast + (1.0 - fastAttack) * light
  598. : fastRelease * cellFast + (1.0 - fastRelease) * light;
  599. const auto slowRelease = coef (0.4 + 4.6 * memory, sr);
  600. cellSlow = light > cellSlow ? slowAttack * cellSlow + (1.0 - slowAttack) * light
  601. : slowRelease * cellSlow + (1.0 - slowRelease) * light;
  602. const auto busy = light > 0.1 ? 1.0 : 0.0;
  603. memory = busy > memory ? memoryUp * memory + (1.0 - memoryUp) * busy : memoryDown * memory + (1.0 - memoryDown) * busy;
  604. const auto conductance = 0.7 * cellFast + 0.3 * cellSlow;
  605. const auto g = (float) (1.0 / (1.0 + conductance));
  606. lastL = l[i] * g;
  607. lastR = r[i] * g;
  608. // auto make-up (round 129) [model]: gives back ~2/3 of the slow average reduction, so turning PEAK REDUCTION
  609. // up keeps the level roughly where it was (the reference's AUTO MAKEUP; its law is not measured)
  610. auto wet = makeup;
  611. if (autoUp)
  612. {
  613. const auto grDb = -gainToDb (g);
  614. autoGr = autoCoef * autoGr + (1.0 - autoCoef) * grDb;
  615. wet *= (float) dbToGain (0.66 * autoGr);
  616. }
  617. l[i] = ((lastL * wet) * mx + l[i] * (1.0f - mx)) * trimGain;
  618. r[i] = ((lastR * wet) * mx + r[i] * (1.0f - mx)) * trimGain;
  619. maxGr = std::max (maxGr, -gainToDb (g));
  620. }
  621. meters.set ((float) maxGr, 0.0f, inPeak, Meters::peak (l, r, n));
  622. }
  623. private:
  624. static constexpr int maxBlock = 1024;
  625. double sr = 44100.0;
  626. vmatch::opto::Leveler classicEngine;
  627. std::array<float, maxBlock> dryL {}, dryR {};
  628. Biquad emph;
  629. double panel = 0, cellFast = 0, cellSlow = 0, memory = 0;
  630. double panelAttack = 0, panelRelease = 0, fastAttack = 0, fastRelease = 0, slowAttack = 0, memoryUp = 0, memoryDown = 0;
  631. double autoGr = 0, autoCoef = 0;
  632. float lastL = 0, lastR = 0;
  633. Meters meters;
  634. };
  635. } // namespace
  636. } // namespace glow
  637. // ================================================================== Hush
  638. namespace hush
  639. {
  640. double hz (float v) noexcept { return logMap (v, 2000.0, 16000.0); }
  641. double lowPassHz (float v) noexcept { return logMap (v, 2000.0, 20000.0); }
  642. double thresholdDb (float v) noexcept { return lin (v, -60.0, 0.0); }
  643. double rangeDb (float v) noexcept { return lin (v, 0.0, 24.0); }
  644. double releaseMs (float v) noexcept { return logMap (v, 10.0, 250.0); }
  645. double lookaheadMs() noexcept { return 2.0; }
  646. 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)
  647. float lookaheadTimeValue (double ms) noexcept { return (float) std::clamp (ms / 15.0, 0.0, 1.0); }
  648. double classicThresholdDb (float v) noexcept { return lin (v, -80.0, 0.0); }
  649. bool classic (const EffectParams& p) noexcept { return on (p[voicing]); }
  650. namespace
  651. {
  652. class DeEsser final : public Effect
  653. {
  654. public:
  655. explicit DeEsser (const EffectParams* p = nullptr) : primedMs (p != nullptr ? lookaheadTimeMs ((*p)[lookaheadTime]) : lookaheadMs()) {}
  656. void prepare (double rate) noexcept override
  657. {
  658. sr = rate;
  659. setDelay (primedMs);
  660. classicEngine.prepare (rate);
  661. reset();
  662. }
  663. void reset() noexcept override
  664. {
  665. delay.reset();
  666. scDelay.reset();
  667. classicEngine.reset();
  668. detect.reset();
  669. detectLp.reset();
  670. listenF.reset();
  671. listenLp.reset();
  672. split.reset();
  673. bandLp.reset();
  674. bandLp2.reset();
  675. topHp.reset();
  676. topHp2.reset();
  677. env = 0.0;
  678. gr = 0.0;
  679. envCh[0] = envCh[1] = grCh[0] = grCh[1] = 0.0;
  680. fullEnv = 0.0;
  681. for (auto& o : os) o.reset();
  682. for (auto& o : osBand) o.reset();
  683. for (auto& o : osGain) o.reset();
  684. }
  685. int latencySamples() const noexcept override { return delay.len + (osFactor > 1 ? Oversampler::latency() : 0); }
  686. float controlOutput (int k) const noexcept override { return meters.read (k); }
  687. void setSidechain (const float* l, const float* r) noexcept override { scL = l; scR = r; }
  688. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  689. {
  690. if (n > maxBlock) // the oversampled path works on blocks of at most maxBlock samples
  691. {
  692. const auto* sl = scL;
  693. const auto* sr2 = scR;
  694. for (int i = 0; i < n; i += maxBlock)
  695. {
  696. if (sl != nullptr && sr2 != nullptr) { scL = sl + i; scR = sr2 + i; }
  697. processBlock (l + i, r + i, std::min (maxBlock, n - i), p, bpm);
  698. }
  699. scL = scR = nullptr;
  700. return;
  701. }
  702. processBlock (l, r, n, p, bpm);
  703. }
  704. private:
  705. void processBlock (float* l, float* r, int n, const EffectParams& p, double) noexcept
  706. {
  707. // round 128: the look-ahead time is the delay (and the reported latency; the engine re-plans the delay
  708. // compensation when it moves). The default 2 ms is the old fixed value.
  709. if (const auto ms = lookaheadTimeMs (p[lookaheadTime]); std::abs (ms - delayMs) > 1e-9)
  710. setDelay (ms);
  711. const auto inGainDb = lin (p[input], -12.0, 12.0);
  712. if (inGainDb != 0.0)
  713. {
  714. const auto gIn = (float) dbToGain (inGainDb);
  715. for (int i = 0; i < n; ++i) { l[i] *= gIn; r[i] *= gIn; }
  716. }
  717. const auto inPeak = Meters::peak (l, r, n);
  718. // round 135: oversampling (Hush's own voicing): the gain and the band it acts on are upsampled with the audio
  719. const auto factor = classic (p) ? 1 : oversamplingOf (p[oversampling]);
  720. if (factor != osFactor)
  721. {
  722. osFactor = factor;
  723. for (auto* bank : { &os, &osBand, &osGain })
  724. for (auto& o : *bank) { o.setFactor (std::max (2, factor)); o.reset(); }
  725. }
  726. const bool over = osFactor > 1;
  727. if (classic (p))
  728. {
  729. // the measured classic de-esser has no look-ahead; the fixed latency Hush reports is kept as a plain delay
  730. vmatch::deesser::Settings c;
  731. c.hz = hz (p[frequency]);
  732. c.thresholdDb = classicThresholdDb (p[threshold]);
  733. c.bandPass = on (p[detector]);
  734. c.wideband = on (p[mode]);
  735. c.listen = on (p[listen]);
  736. c.outputDb = lin (p[output], -12.0, 12.0);
  737. classicEngine.process (l, r, n, c);
  738. for (int i = 0; i < n; ++i)
  739. delay.process (l[i], r[i]);
  740. meters.set (classicEngine.reductionDb(), 0.0f, inPeak, Meters::peak (l, r, n));
  741. return;
  742. }
  743. const auto f = std::min (hz (p[frequency]), sr * 0.45);
  744. const bool band = on (p[detector]);
  745. if (band)
  746. detect.filter (sr, 2, f, 1.4);
  747. else
  748. detect.filter (sr, 1, f, 0.70710678);
  749. listenF = detect;
  750. split.filter (sr, 0, f, 0.70710678);
  751. const bool lpOn = p[lowPass] < 0.999f;
  752. if (lpOn)
  753. {
  754. const auto lpHz = std::min (std::max (lowPassHz (p[lowPass]), f * 1.05), sr * 0.45);
  755. detectLp.filter (sr, 0, lpHz, 0.70710678);
  756. listenLp.filter (sr, 0, lpHz, 0.70710678);
  757. bandLp.filter (sr, 0, lpHz, 0.70710678); // Linkwitz-Riley 4th order at the low-pass: band = LP², above = HP²
  758. bandLp2 = bandLp;
  759. topHp.filter (sr, 1, lpHz, 0.70710678);
  760. topHp2 = topHp;
  761. }
  762. const auto thr = thresholdDb (p[threshold]), range = rangeDb (p[hush::range]);
  763. const bool wide = on (p[mode]), listening = on (p[listen]), ahead = on (p[lookahead]);
  764. const bool allroundMode = on (p[allround]), mid = on (p[linkMid]);
  765. const bool ext = on (p[external]) && scL != nullptr && scR != nullptr;
  766. const auto link = std::clamp ((double) p[stereoLink], 0.0, 1.0);
  767. const bool linked = link >= 1.0 || mid;
  768. const auto release = coef (releaseMs (p[hush::release]) * 0.001, sr);
  769. const auto attack = coef (ahead ? std::max (delayMs, 0.03) * 0.001 / 3.0 : 0.0005, sr);
  770. const auto out = (float) dbToGain (lin (p[output], -12.0, 12.0));
  771. constexpr double knee = 3.0;
  772. double maxGr = 0.0;
  773. // gain reduction (dB) wanted for a detected level `s` (and the full-band level in allround mode)
  774. auto wanted = [&] (double s, double full) {
  775. auto over = gainToDb (s) - thr;
  776. if (allroundMode) // the sibilance must also stand out from the whole signal (steady highs in a mix trigger less)
  777. over -= 0.5 * std::max (0.0, gainToDb (full) - gainToDb (s) - 6.0);
  778. double want = 0.0;
  779. if (over > knee)
  780. want = over;
  781. else if (over > -knee)
  782. want = (over + knee) * (over + knee) / (4.0 * knee);
  783. return std::min (want, range);
  784. };
  785. for (int i = 0; i < n; ++i)
  786. {
  787. float a = l[i], b = r[i];
  788. float da = a, db = b;
  789. delay.process (da, db);
  790. // with lookahead the detector hears the signal before it is played; without, both see the delayed one
  791. auto xa = ahead ? a : da, xb = ahead ? b : db;
  792. if (ext)
  793. {
  794. float sa = scL[i], sb = scR[i];
  795. float dsa = sa, dsb = sb;
  796. scDelay.process (dsa, dsb);
  797. xa = ahead ? sa : dsa;
  798. xb = ahead ? sb : dsb;
  799. }
  800. double sA, sB;
  801. if (mid)
  802. {
  803. auto m = detect.process (0.5f * (xa + xb), 0);
  804. if (lpOn) m = detectLp.process (m, 0);
  805. sA = sB = std::abs (m);
  806. }
  807. else
  808. {
  809. auto ya = detect.process (xa, 0), yb = detect.process (xb, 1);
  810. if (lpOn) { ya = detectLp.process (ya, 0); yb = detectLp.process (yb, 1); }
  811. sA = std::abs (ya);
  812. sB = std::abs (yb);
  813. }
  814. double full = 0.0;
  815. if (allroundMode)
  816. {
  817. const double fa = std::max (std::abs (xa), std::abs (xb));
  818. fullEnv = fa > fullEnv ? fa : release * fullEnv + (1.0 - release) * fa;
  819. full = fullEnv;
  820. }
  821. float gA, gB;
  822. if (linked)
  823. {
  824. const auto s = std::max (sA, sB);
  825. env = s > env ? s : release * env + (1.0 - release) * s;
  826. const auto want = wanted (env, full);
  827. gr = want > gr ? attack * gr + (1.0 - attack) * want : release * gr + (1.0 - release) * want;
  828. gA = gB = (float) dbToGain (-gr);
  829. maxGr = std::max (maxGr, gr);
  830. }
  831. else
  832. {
  833. const auto s = std::max (sA, sB);
  834. const double ss[2] { link * s + (1.0 - link) * sA, link * s + (1.0 - link) * sB };
  835. for (int c = 0; c < 2; ++c)
  836. {
  837. envCh[c] = ss[c] > envCh[c] ? ss[c] : release * envCh[c] + (1.0 - release) * ss[c];
  838. const auto want = wanted (envCh[c], full);
  839. grCh[c] = want > grCh[c] ? attack * grCh[c] + (1.0 - attack) * want : release * grCh[c] + (1.0 - release) * want;
  840. maxGr = std::max (maxGr, grCh[c]);
  841. }
  842. gA = (float) dbToGain (-grCh[0]);
  843. gB = (float) dbToGain (-grCh[1]);
  844. }
  845. if (listening)
  846. {
  847. a = listenF.process (da, 0);
  848. b = listenF.process (db, 1);
  849. if (lpOn) { a = listenLp.process (a, 0); b = listenLp.process (b, 1); }
  850. }
  851. else if (over)
  852. {
  853. // output = input + band × (gain − 1): the band (whole signal, above the split, or between the two
  854. // handles) and the gain go to the high rate below
  855. float ba = da, bb = db;
  856. if (! wide)
  857. {
  858. const auto la = split.process (da, 0), lb = split.process (db, 1);
  859. ba = da - la;
  860. bb = db - lb;
  861. if (lpOn)
  862. {
  863. ba = bandLp2.process (bandLp.process (ba, 0), 0);
  864. bb = bandLp2.process (bandLp.process (bb, 1), 1);
  865. topHp2.process (topHp.process (da - la, 0), 0); // (kept running: switching back stays smooth)
  866. topHp2.process (topHp.process (db - lb, 1), 1);
  867. }
  868. }
  869. bandBuf[0][(std::size_t) i] = ba;
  870. bandBuf[1][(std::size_t) i] = bb;
  871. gainBuf[0][(std::size_t) i] = gA - 1.0f;
  872. gainBuf[1][(std::size_t) i] = gB - 1.0f;
  873. a = da;
  874. b = db;
  875. }
  876. else if (wide)
  877. {
  878. a = da * gA;
  879. b = db * gB;
  880. }
  881. else if (! lpOn)
  882. {
  883. // complementary split: low = 2nd-order low-pass, high = input − low (sums back exactly)
  884. const auto la = split.process (da, 0), lb = split.process (db, 1);
  885. a = la + (da - la) * gA;
  886. b = lb + (db - lb) * gB;
  887. }
  888. else
  889. {
  890. // round 128: only the band between the two handles is turned down; above the low-pass it passes
  891. // (the high part is split again at the low-pass by a Linkwitz-Riley pair: flat sum, no notch)
  892. const auto la = split.process (da, 0), lb = split.process (db, 1);
  893. const auto ha = da - la, hb = db - lb;
  894. const auto ma = bandLp2.process (bandLp.process (ha, 0), 0), mb = bandLp2.process (bandLp.process (hb, 1), 1);
  895. const auto ta = topHp2.process (topHp.process (ha, 0), 0), tb = topHp2.process (topHp.process (hb, 1), 1);
  896. a = la + ma * gA + ta;
  897. b = lb + mb * gB + tb;
  898. }
  899. l[i] = a * out;
  900. r[i] = b * out;
  901. }
  902. if (over)
  903. {
  904. const auto m = n * osFactor;
  905. for (int c = 0; c < 2; ++c)
  906. {
  907. auto* x = c == 0 ? l : r;
  908. if (listening)
  909. {
  910. os[(std::size_t) c].process (x, n, [] (float v) noexcept { return v; });
  911. continue;
  912. }
  913. auto& ub = upBand[(std::size_t) c];
  914. auto& ug = upGain[(std::size_t) c];
  915. int k = 0, j = 0, q = 0;
  916. 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; });
  917. 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; });
  918. os[(std::size_t) c].process (x, n, [&] (float v) noexcept {
  919. const auto at = (std::size_t) std::min (q++, m - 1);
  920. return v + ub[at] * ug[at] * out;
  921. });
  922. }
  923. }
  924. meters.set ((float) maxGr, 0.0f, inPeak, Meters::peak (l, r, n));
  925. scL = scR = nullptr;
  926. }
  927. void setDelay (double ms) noexcept
  928. {
  929. delayMs = ms;
  930. const auto len = (int) std::lround (ms * 0.001 * sr);
  931. delay.setLength (len);
  932. scDelay.setLength (len);
  933. }
  934. static constexpr int maxBlock = 1024, maxFactor = 8;
  935. int osFactor = 1;
  936. std::array<Oversampler, 2> os, osBand, osGain;
  937. std::array<std::array<float, maxBlock>, 2> bandBuf {}, gainBuf {};
  938. std::array<std::array<float, maxBlock * maxFactor>, 2> upBand {}, upGain {};
  939. double sr = 44100.0;
  940. double primedMs = 2.0, delayMs = 2.0;
  941. Delay2<4096> delay, scDelay;
  942. vmatch::deesser::DeEsser classicEngine;
  943. Biquad detect, detectLp, listenF, listenLp, split, bandLp, bandLp2, topHp, topHp2;
  944. double env = 0.0, gr = 0.0, fullEnv = 0.0;
  945. double envCh[2] {}, grCh[2] {};
  946. const float* scL = nullptr;
  947. const float* scR = nullptr;
  948. Meters meters;
  949. };
  950. } // namespace
  951. } // namespace hush
  952. // ================================================================== Aether
  953. namespace aether
  954. {
  955. double airDb (float v) noexcept { return lin (v, 0.0, 15.0); }
  956. double airHz (float v) noexcept { return logMap (v, 5000.0, 20000.0); }
  957. double exciterHz (float v) noexcept { return logMap (v, 1000.0, 12000.0); }
  958. namespace
  959. {
  960. // ---- the dynamic air model (round 124) ------------------------------------------------------------------------------
  961. // Black-box measurements of the official Fresh Air trial (docs/analise/REA_rodada124.md §3), our own code:
  962. // y = s(mid, high) * x + kMid(mid) * gMid(t) * HPmid(x) + kHigh(high) * gHigh(t) * HPhigh(x)
  963. // HPmid / HPhigh: RBJ 2nd-order high-passes (2993 Hz, Q 0.629 / 9000 Hz, Q 0.707; the same analog corners at 44.1
  964. // and 48 kHz); s, kMid, kHigh: measured on a 0.025 grid (small-signal transfer functions, residual < 0.4 %).
  965. // gMid: a linked peak detector (instant attack, 5 ms hold, 96 ms release) of the band, 4:1 above -30.1 dBFS,
  966. // released through a 7.7 ms one-pole. gHigh: two 4:1 stages — a peak detector with 2-pole gain smoothing, then a
  967. // feedback stage on the band after the first one, whose threshold rises as High air goes down (T + c (1 - high)^2).
  968. namespace dyn
  969. {
  970. 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,
  971. 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,
  972. 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,
  973. 1.11095, 1.10707 };
  974. 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,
  975. 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,
  976. 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,
  977. 3.55884, 3.67701 };
  978. 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,
  979. 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,
  980. 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,
  981. 1.23550, 1.23620 };
  982. 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,
  983. 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,
  984. 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,
  985. 11.86810, 12.37801 };
  986. inline double grid (const double (&t)[41], double v) noexcept
  987. {
  988. const auto x = std::clamp (v, 0.0, 1.0) * 40.0;
  989. const auto i = std::min (39, (int) x);
  990. return t[i] + (t[i + 1] - t[i]) * (x - i);
  991. }
  992. // the dry gain with both bands at 0
  993. constexpr double baseGain = 1.00237;
  994. // the fitted dynamics (REA_rodada124 §3)
  995. struct Tuning
  996. {
  997. double midThreshold = -30.13, midHoldMs = 5.0, midReleaseMs = 96.03, midGainReleaseMs = 7.706;
  998. double highThreshold = -38.03, highReleaseMs = 0.5354, highGainAttackMs = 2.1428, highGainReleaseMs = 55.52;
  999. double high2ReleaseMs = 0.0190, high2GainAttackMs = 56.73, high2GainReleaseMs = 53.45, high2Slope = 8.257;
  1000. double high2Threshold = -41.13, high2Curve = 15.585;
  1001. };
  1002. class Engine
  1003. {
  1004. public:
  1005. void prepare (double rate) noexcept
  1006. {
  1007. sr = rate;
  1008. mid.hp (rate, 2993.0, 0.6291);
  1009. high.hp (rate, 9000.0, 0.7069);
  1010. reset();
  1011. }
  1012. void reset() noexcept
  1013. {
  1014. mid.reset();
  1015. high.reset();
  1016. envM = holdM = gM = envH = g1a = g1b = env2 = g2a = g2b = 0.0;
  1017. }
  1018. // processes in place; `out` is the output gain (linear)
  1019. void process (float* l, float* r, int n, double midAmount, double highAmount, double out) noexcept
  1020. {
  1021. const Tuning t;
  1022. const auto c = [this] (double ms) { return ms <= 0.0 ? 0.0 : std::exp (-1000.0 / (ms * sr)); };
  1023. const auto s = (grid (gridMidS, midAmount) + grid (gridHighS, highAmount) - baseGain) * out;
  1024. const auto km = grid (gridMidK, midAmount) * out, kh = grid (gridHighK, highAmount) * out;
  1025. const auto mRel = c (t.midReleaseMs), mGr = c (t.midGainReleaseMs), mHold = t.midHoldMs * sr / 1000.0;
  1026. const auto hRel = c (t.highReleaseMs), h1a = c (t.highGainAttackMs), h1r = c (t.highGainReleaseMs);
  1027. const auto h2Rel = c (t.high2ReleaseMs), h2a = c (t.high2GainAttackMs), h2r = c (t.high2GainReleaseMs);
  1028. const auto t2 = t.high2Threshold + t.high2Curve * (1.0 - highAmount) * (1.0 - highAmount);
  1029. for (int i = 0; i < n; ++i)
  1030. {
  1031. const double xl = l[i], xr = r[i];
  1032. const double ml = mid.process ((float) xl, 0), mr = mid.process ((float) xr, 1);
  1033. const double hl = high.process ((float) xl, 0), hr = high.process ((float) xr, 1);
  1034. // mid: peak with hold, 4:1
  1035. const auto pm = std::max (std::fabs (ml), std::fabs (mr));
  1036. if (pm >= envM) { envM = pm; holdM = mHold; }
  1037. else if (holdM > 0.0) holdM -= 1.0;
  1038. else envM = pm + (envM - pm) * mRel;
  1039. const auto gmTarget = -0.75 * std::max (0.0, 20.0 * std::log10 (envM + 1e-30) - t.midThreshold);
  1040. gM = gmTarget < gM ? gmTarget : gmTarget + (gM - gmTarget) * mGr;
  1041. // high, stage 1: peak, 4:1, 2-pole gain smoothing
  1042. const auto ph = std::max (std::fabs (hl), std::fabs (hr));
  1043. envH = ph > envH ? ph : ph + (envH - ph) * hRel;
  1044. const auto g1Target = -0.75 * std::max (0.0, 20.0 * std::log10 (envH + 1e-30) - t.highThreshold);
  1045. const auto k1 = g1Target < g1b ? h1a : h1r;
  1046. g1a = g1Target + (g1a - g1Target) * k1;
  1047. g1b = g1a + (g1b - g1a) * k1;
  1048. const auto g1 = std::pow (10.0, g1b / 20.0);
  1049. // high, stage 2: feedback (detector after both stages), peak, 2-pole gain smoothing
  1050. const auto g2 = std::pow (10.0, g2b / 20.0);
  1051. const auto p2 = ph * g1 * g2;
  1052. env2 = p2 >= env2 ? p2 : p2 + (env2 - p2) * h2Rel;
  1053. const auto g2Target = -t.high2Slope * std::max (0.0, 20.0 * std::log10 (env2 + 1e-30) - t2);
  1054. const auto k2 = g2Target < g2b ? h2a : h2r;
  1055. g2a = g2Target + (g2a - g2Target) * k2;
  1056. g2b = g2a + (g2b - g2a) * k2;
  1057. const auto gm = km * std::pow (10.0, gM / 20.0), gh = kh * g1 * g2;
  1058. l[i] = (float) (s * xl + gm * ml + gh * hl);
  1059. r[i] = (float) (s * xr + gm * mr + gh * hr);
  1060. }
  1061. }
  1062. float reductionDb() const noexcept { return (float) std::min (gM, g1b + g2b); }
  1063. private:
  1064. struct Hp2 : Biquad
  1065. {
  1066. void hp (double rate, double fc, double q) noexcept
  1067. {
  1068. const auto w = 2.0 * 3.14159265358979323846 * fc / rate, a = std::sin (w) / (2.0 * q), cw = std::cos (w);
  1069. set ((1.0 + cw) / 2.0, -(1.0 + cw), (1.0 + cw) / 2.0, 1.0 + a, -2.0 * cw, 1.0 - a);
  1070. }
  1071. double process (float x, int ch) noexcept
  1072. {
  1073. const auto y = b0 * x + z1[ch];
  1074. z1[ch] = b1 * x - a1 * y + z2[ch];
  1075. z2[ch] = b2 * x - a2 * y;
  1076. return y;
  1077. }
  1078. };
  1079. double sr = 48000.0;
  1080. Hp2 mid, high;
  1081. double envM = 0, holdM = 0, gM = 0, envH = 0, g1a = 0, g1b = 0, env2 = 0, g2a = 0, g2b = 0;
  1082. };
  1083. } // namespace dyn
  1084. class Air final : public Effect
  1085. {
  1086. public:
  1087. Air()
  1088. {
  1089. for (auto& o : os)
  1090. o.setFactor (4);
  1091. }
  1092. void prepare (double rate) noexcept override
  1093. {
  1094. sr = rate;
  1095. dry.setLength (Oversampler::latency());
  1096. dynEngine.prepare (rate);
  1097. reset();
  1098. }
  1099. void reset() noexcept override
  1100. {
  1101. for (auto& o : os)
  1102. o.reset();
  1103. dry.reset();
  1104. shelf.reset();
  1105. pre.reset();
  1106. post.reset();
  1107. dynEngine.reset();
  1108. }
  1109. // the dynamic model has no latency (as the measured reference); the exciter path is oversampled
  1110. int latencySamples() const noexcept override { return dynamicMode ? 0 : Oversampler::latency(); }
  1111. float controlOutput (int k) const noexcept override { return meters.read (k); }
  1112. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  1113. {
  1114. const bool dynamicNow = p[dynamic] >= 0.5f;
  1115. if (dynamicNow != dynamicMode)
  1116. {
  1117. dynamicMode = dynamicNow;
  1118. reset();
  1119. }
  1120. for (int off = 0; off < n; off += maxBlock)
  1121. if (dynamicMode)
  1122. dynamicChunk (l + off, r + off, std::min (maxBlock, n - off), p);
  1123. else
  1124. chunk (l + off, r + off, std::min (maxBlock, n - off), p);
  1125. }
  1126. private:
  1127. void dynamicChunk (float* l, float* r, int n, const EffectParams& p) noexcept
  1128. {
  1129. const auto inPeak = Meters::peak (l, r, n);
  1130. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1131. if (mx < 1.0f)
  1132. for (int i = 0; i < n; ++i)
  1133. hl[(std::size_t) i] = l[i], hr[(std::size_t) i] = r[i];
  1134. 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)));
  1135. if (mx < 1.0f)
  1136. for (int i = 0; i < n; ++i)
  1137. {
  1138. l[i] = hl[(std::size_t) i] + (l[i] - hl[(std::size_t) i]) * mx;
  1139. r[i] = hr[(std::size_t) i] + (r[i] - hr[(std::size_t) i]) * mx;
  1140. }
  1141. meters.set (0.0f, -dynEngine.reductionDb(), inPeak, Meters::peak (l, r, n));
  1142. }
  1143. void chunk (float* l, float* r, int n, const EffectParams& p) noexcept
  1144. {
  1145. const auto inPeak = Meters::peak (l, r, n);
  1146. shelf.shelf (sr, std::min (airHz (p[airFreq]), sr * 0.45), airDb (p[air]), true, 0.6);
  1147. const auto ex = std::min (exciterHz (p[exciterFreq]), sr * 0.45);
  1148. pre.filter (sr, 1, ex, 0.70710678);
  1149. post.filter (sr, 1, ex, 0.70710678);
  1150. const auto amount = 2.0f * std::clamp (p[harmonics], 0.0f, 1.0f);
  1151. const auto odd = std::clamp (p[character], 0.0f, 1.0f);
  1152. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1153. const auto out = (float) dbToGain (lin (p[output], -12.0, 12.0));
  1154. // harmonic generator (DAFX ch. 4, exciters): only the band above the exciter frequency is shaped, at 4x;
  1155. // an even part (x² -like, 2nd harmonic) and an odd part (tanh − x, 3rd harmonic), blended by `character`
  1156. auto gen = [odd] (float x) noexcept
  1157. {
  1158. const auto k = 4.0f * x;
  1159. const auto even = (std::sqrt (1.0f + k * k) - 1.0f) * 0.25f;
  1160. const auto oddPart = std::tanh (3.0f * x) / 3.0f - x;
  1161. return (1.0f - odd) * even - odd * oddPart * 3.0f;
  1162. };
  1163. for (int i = 0; i < n; ++i)
  1164. {
  1165. hl[(std::size_t) i] = pre.process (l[i], 0);
  1166. hr[(std::size_t) i] = pre.process (r[i], 1);
  1167. }
  1168. if (amount > 0.0f)
  1169. {
  1170. os[0].process (hl.data(), n, gen);
  1171. os[1].process (hr.data(), n, gen);
  1172. }
  1173. else
  1174. {
  1175. // keep the oversamplers' state moving so turning the harmonics up does not click with stale history
  1176. auto zero = [] (float) noexcept { return 0.0f; };
  1177. os[0].process (hl.data(), n, zero);
  1178. os[1].process (hr.data(), n, zero);
  1179. }
  1180. for (int i = 0; i < n; ++i)
  1181. {
  1182. float a = l[i], b = r[i];
  1183. dry.process (a, b);
  1184. const auto wa = shelf.process (a, 0) + amount * post.process (hl[(std::size_t) i], 0);
  1185. const auto wb = shelf.process (b, 1) + amount * post.process (hr[(std::size_t) i], 1);
  1186. l[i] = (a + (wa - a) * mx) * out;
  1187. r[i] = (b + (wb - b) * mx) * out;
  1188. }
  1189. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  1190. }
  1191. static constexpr int maxBlock = 2048;
  1192. double sr = 44100.0;
  1193. std::array<Oversampler, 2> os;
  1194. Delay2<256> dry;
  1195. Biquad shelf, pre, post;
  1196. dyn::Engine dynEngine;
  1197. bool dynamicMode = false;
  1198. std::array<float, maxBlock> hl {}, hr {};
  1199. Meters meters;
  1200. };
  1201. } // namespace
  1202. } // namespace aether
  1203. // ================================================================== Ember
  1204. namespace ember
  1205. {
  1206. Style styleOf (float v) noexcept { return (Style) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * (styles - 1)), 0, styles - 1); }
  1207. double driveDb (float v) noexcept { return lin (v, 0.0, 36.0); }
  1208. double toneTilt (float v) noexcept { return lin (v, -1.0, 1.0); }
  1209. double lowCutHz (float v) noexcept { return v <= 0.0f ? 0.0 : logMap (v, 20.0, 500.0); }
  1210. double highCutHz (float v) noexcept { return v >= 1.0f ? 0.0 : logMap (v, 2000.0, 20000.0); }
  1211. int oversampling (float v) noexcept { return v < 0.25f ? 2 : (v < 0.75f ? 4 : 8); }
  1212. int bandCount (float v) noexcept { return std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 3.0f), 0, 3) + 1; }
  1213. double crossoverHz (float v) noexcept { return logMap (v, 30.0, 18000.0); }
  1214. float crossoverValue (double hz) noexcept { return (float) std::clamp (std::log (hz / 30.0) / std::log (600.0), 0.0, 1.0); }
  1215. int slopeDbOct (float v) noexcept { return v < 0.25f ? 12 : (v < 0.75f ? 24 : 48); }
  1216. double bandLevelDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  1217. BandState bandState (float v) noexcept { return (BandState) std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 2.0f), 0, 2); }
  1218. double inputDb (float v) noexcept { return lin (v, -24.0, 24.0); }
  1219. double toneDb (float v) noexcept { return lin (v, -18.0, 18.0); }
  1220. double feedbackAmount (float v) noexcept { return lin (v, 0.0, 1.0); }
  1221. double feedbackHz (float v) noexcept { return logMap (v, 10.0, 1000.0); }
  1222. double dynamicsAmount (float v) noexcept { return lin (v, -1.0, 1.0); }
  1223. int bandParam (int band, int field) noexcept
  1224. {
  1225. if (band <= 0)
  1226. {
  1227. static constexpr int first[] { style, drive, band1Mix, band1Level };
  1228. return first[std::clamp (field, 0, 3)];
  1229. }
  1230. return band2Style + (band - 1) * 4 + std::clamp (field, 0, 3);
  1231. }
  1232. float shape (Style s, float x) noexcept
  1233. {
  1234. switch (s)
  1235. {
  1236. case Style::tube:
  1237. {
  1238. // asymmetric: a biased tanh (even and odd harmonics, like a single-ended triode stage), unit slope at 0
  1239. constexpr float bias = 0.35f;
  1240. const float tb = std::tanh (bias);
  1241. return (std::tanh (x + bias) - tb) / (1.0f - tb * tb);
  1242. }
  1243. case Style::tape: return x / std::sqrt (1.0f + x * x); // gentle symmetric (odd) saturation
  1244. case Style::transistor:
  1245. {
  1246. const auto a = std::abs (x);
  1247. const auto a2 = a * a;
  1248. return x / std::sqrt (std::sqrt (1.0f + a2 * a2)); // harder knee: x / (1 + x⁴)^¼
  1249. }
  1250. case Style::fuzz:
  1251. default:
  1252. if (x >= 0.0f)
  1253. {
  1254. const auto y = x / 0.6f, y2 = y * y, y4 = y2 * y2;
  1255. return 0.6f * y / std::sqrt (std::sqrt (std::sqrt (1.0f + y4 * y4))); // near-hard positive clip at 0.6
  1256. }
  1257. return std::tanh (1.5f * x) / 1.5f;
  1258. }
  1259. }
  1260. namespace
  1261. {
  1262. // A band's round-128 stages (Saturn parity): tone after the shaper, a feedback loop around it, dynamics on its level.
  1263. // Each is skipped while it is at its neutral setting, so the old sound is untouched.
  1264. struct BandExtras
  1265. {
  1266. std::array<Biquad, 4> tone; // bass, mid, treble, presence
  1267. bool toneOn = false;
  1268. Biquad fbBand; // the feedback band (at the oversampled rate)
  1269. double fbGain = 0.0;
  1270. float fbLast[2] {};
  1271. double dyn = 0.0, env = 0.0, envAtk = 0.0, envRel = 0.0;
  1272. std::array<float, 4> lastTone { -1.0f, -1.0f, -1.0f, -1.0f };
  1273. float lastFbHz = -1.0f;
  1274. int lastFactor = 0;
  1275. void reset() noexcept
  1276. {
  1277. for (auto& t : tone) t.reset();
  1278. fbBand.reset();
  1279. fbLast[0] = fbLast[1] = 0.0f;
  1280. env = 0.0;
  1281. }
  1282. void configure (const EffectParams& p, int band, double sr, int factor) noexcept
  1283. {
  1284. std::array<float, 4> tv;
  1285. for (int k = 0; k < 4; ++k) tv[(std::size_t) k] = p[(std::size_t) bandExtra (band, 2 + k)];
  1286. if (tv != lastTone)
  1287. {
  1288. tone[0].shelf (sr, 120.0, toneDb (tv[0]), false, 0.7);
  1289. tone[1].peaking (sr, 800.0, 0.8, toneDb (tv[1]));
  1290. tone[2].shelf (sr, 3500.0, toneDb (tv[2]), true, 0.7);
  1291. tone[3].peaking (sr, 6000.0, 1.0, toneDb (tv[3]));
  1292. lastTone = tv;
  1293. }
  1294. toneOn = false;
  1295. for (const auto v : tv) toneOn = toneOn || std::abs (toneDb (v)) > 1e-6;
  1296. fbGain = 0.9 * feedbackAmount (p[(std::size_t) bandExtra (band, 6)]);
  1297. const auto fhz = p[(std::size_t) bandExtra (band, 7)];
  1298. if (fbGain > 0.0 && (fhz != lastFbHz || factor != lastFactor))
  1299. {
  1300. fbBand.filter (sr * factor, 2, feedbackHz (fhz), 1.4);
  1301. lastFbHz = fhz;
  1302. lastFactor = factor;
  1303. }
  1304. dyn = dynamicsAmount (p[(std::size_t) bandExtra (band, 8)]);
  1305. envAtk = coef (0.005, sr);
  1306. envRel = coef (0.080, sr);
  1307. }
  1308. // the shaper with the feedback loop: the band-passed last output added to the input (channel ch)
  1309. float shapeWithFeedback (Style s, float x, int ch) noexcept
  1310. {
  1311. const auto y = shape (s, x + (float) fbGain * fbLast[ch]);
  1312. fbLast[ch] = fbBand.process (y, ch);
  1313. return y;
  1314. }
  1315. // after the shaper: the level follows the band's input level (dynamics), then the tone
  1316. void post (float& a, float& b, float inA, float inB) noexcept
  1317. {
  1318. if (dyn != 0.0)
  1319. {
  1320. const double x = std::max (std::abs (inA), std::abs (inB));
  1321. env = x > env ? envAtk * env + (1.0 - envAtk) * x : envRel * env + (1.0 - envRel) * x;
  1322. const double over = gainToDb (env + 1e-9) + 18.0; // dB above −18 dBFS
  1323. const auto g = (float) dbToGain (std::clamp (dyn * 0.5 * over, -18.0, 18.0));
  1324. a *= g;
  1325. b *= g;
  1326. }
  1327. if (toneOn)
  1328. for (auto& t : tone)
  1329. {
  1330. a = t.process (a, 0);
  1331. b = t.process (b, 1);
  1332. }
  1333. }
  1334. };
  1335. class Saturator final : public Effect
  1336. {
  1337. public:
  1338. void prepare (double rate) noexcept override
  1339. {
  1340. sr = rate;
  1341. dry.setLength (Oversampler::latency());
  1342. dc.set (sr, 8.0);
  1343. for (auto& d : bandDry)
  1344. d.setLength (Oversampler::latency());
  1345. for (auto& d : bandDc)
  1346. d.set (sr, 8.0);
  1347. splitBands = 0; // redesign the crossovers at the new rate
  1348. reset();
  1349. }
  1350. void reset() noexcept override
  1351. {
  1352. for (auto& o : os)
  1353. o.reset();
  1354. dry.reset();
  1355. lowcut.reset();
  1356. emphasis.reset();
  1357. deemphasis.reset();
  1358. tiltLo.reset();
  1359. tiltHi.reset();
  1360. highcut.reset();
  1361. dc.reset();
  1362. for (auto& b : bandOs)
  1363. for (auto& o : b)
  1364. o.reset();
  1365. for (auto& d : bandDry)
  1366. d.reset();
  1367. for (auto& d : bandDc)
  1368. d.reset();
  1369. for (auto* x : { &split[0], &split[1], &split[2], &ap2Low, &ap3Low, &ap3Mid })
  1370. x->reset();
  1371. for (auto& x : extras)
  1372. x.reset();
  1373. }
  1374. int latencySamples() const noexcept override { return Oversampler::latency(); }
  1375. float controlOutput (int k) const noexcept override { return meters.read (k); }
  1376. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  1377. {
  1378. for (int off = 0; off < n; off += maxBlock)
  1379. chunk (l + off, r + off, std::min (maxBlock, n - off), p);
  1380. }
  1381. private:
  1382. void chunk (float* l, float* r, int n, const EffectParams& p) noexcept
  1383. {
  1384. if (const auto inDb = inputDb (p[inputGain]); std::abs (inDb) > 1e-9) // round 128: the input gain
  1385. {
  1386. const auto g = (float) dbToGain (inDb);
  1387. for (int i = 0; i < n; ++i) { l[i] *= g; r[i] *= g; }
  1388. }
  1389. if (bandCount (p[bands]) > 1)
  1390. {
  1391. chunkBands (l, r, n, p);
  1392. return;
  1393. }
  1394. const auto inPeak = Meters::peak (l, r, n);
  1395. const auto factor = oversampling (p[quality]);
  1396. for (auto& o : os)
  1397. o.setFactor (factor);
  1398. const auto s = styleOf (p[style]);
  1399. const auto drv = (float) dbToGain (driveDb (p[drive]));
  1400. const bool tape = s == Style::tape;
  1401. const auto lc = lowCutHz (p[lowCut]), hc = highCutHz (p[highCut]);
  1402. if (lc > 0.0)
  1403. lowcut.filter (sr, 1, lc, 0.70710678);
  1404. if (hc > 0.0)
  1405. highcut.filter (sr, 0, std::min (hc, sr * 0.45), 0.70710678);
  1406. // tape: pre-emphasis before the drive and the inverse after it, so the highs saturate first (DAFX 4.3)
  1407. emphasis.shelf (sr, 3000.0, 6.0, true, 0.7);
  1408. deemphasis.shelf (sr, 3000.0, -6.0, true, 0.7);
  1409. const auto tilt = toneTilt (p[tone]);
  1410. tiltLo.shelf (sr, 1000.0, -6.0 * tilt, false, 0.5);
  1411. tiltHi.shelf (sr, 1000.0, 6.0 * tilt, true, 0.5);
  1412. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1413. const auto out = (float) dbToGain (lin (p[output], -18.0, 18.0));
  1414. if (on (p[autoGain]) && (drv != lastDrive || s != lastStyle))
  1415. {
  1416. makeup = autoMakeup (s, drv);
  1417. lastDrive = drv;
  1418. lastStyle = s;
  1419. }
  1420. const auto comp = on (p[autoGain]) ? makeup : 1.0f;
  1421. for (int i = 0; i < n; ++i)
  1422. {
  1423. float a = l[i], b = r[i];
  1424. if (lc > 0.0)
  1425. {
  1426. a = lowcut.process (a, 0);
  1427. b = lowcut.process (b, 1);
  1428. }
  1429. if (tape)
  1430. {
  1431. a = emphasis.process (a, 0);
  1432. b = emphasis.process (b, 1);
  1433. }
  1434. wl[(std::size_t) i] = a * drv;
  1435. wr[(std::size_t) i] = b * drv;
  1436. }
  1437. // round 128: band 1's power, mute, feedback, dynamics and tone (single-band mode)
  1438. auto& ex = extras[0];
  1439. ex.configure (p, 0, sr, factor);
  1440. const bool powered = on (p[bandExtra (0, 0)]);
  1441. const bool muted = bandState (p[bandExtra (0, 1)]) == BandState::mute;
  1442. if (ex.fbGain > 0.0)
  1443. {
  1444. auto left = [&ex, s] (float x) noexcept { return ex.shapeWithFeedback (s, x, 0); };
  1445. auto right = [&ex, s] (float x) noexcept { return ex.shapeWithFeedback (s, x, 1); };
  1446. os[0].process (wl.data(), n, left);
  1447. os[1].process (wr.data(), n, right);
  1448. }
  1449. else
  1450. {
  1451. auto curve = [s] (float x) noexcept { return shape (s, x); };
  1452. os[0].process (wl.data(), n, curve);
  1453. os[1].process (wr.data(), n, curve);
  1454. }
  1455. const bool post = ex.toneOn || ex.dyn != 0.0;
  1456. for (int i = 0; i < n; ++i)
  1457. {
  1458. auto a = dc.process (wl[(std::size_t) i], 0) * comp, b = dc.process (wr[(std::size_t) i], 1) * comp;
  1459. if (tape)
  1460. {
  1461. a = deemphasis.process (a, 0);
  1462. b = deemphasis.process (b, 1);
  1463. }
  1464. if (post) ex.post (a, b, l[i], r[i]);
  1465. a = tiltHi.process (tiltLo.process (a, 0), 0);
  1466. b = tiltHi.process (tiltLo.process (b, 1), 1);
  1467. if (hc > 0.0)
  1468. {
  1469. a = highcut.process (a, 0);
  1470. b = highcut.process (b, 1);
  1471. }
  1472. float da = l[i], db = r[i];
  1473. dry.process (da, db);
  1474. if (! powered) { a = da; b = db; } // switched off: the band passes clean
  1475. if (muted) { a = 0.0f; b = 0.0f; }
  1476. l[i] = (da + (a - da) * mx) * out;
  1477. r[i] = (db + (b - db) * mx) * out;
  1478. }
  1479. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  1480. }
  1481. // Multiband mode (REA_rodada108): Linkwitz-Riley bands (the lower ones through the higher splits' all-passes, so
  1482. // the bands sum flat), each with its own style, drive, mix and level, oversampled separately; the cuts, tone,
  1483. // global mix and output as in the single-band mode.
  1484. void chunkBands (float* l, float* r, int n, const EffectParams& p) noexcept
  1485. {
  1486. const auto inPeak = Meters::peak (l, r, n);
  1487. const int nb = bandCount (p[bands]);
  1488. const auto factor = oversampling (p[quality]);
  1489. std::array<double, 3> f { crossoverHz (p[xover1]), crossoverHz (p[xover2]), crossoverHz (p[xover3]) };
  1490. f[1] = std::max (f[1], f[0] * 1.05);
  1491. f[2] = std::max (f[2], f[1] * 1.05);
  1492. const auto slope = slopeDbOct (p[xoverSlope]);
  1493. if (f != splitHz || slope != splitSlope || nb != splitBands)
  1494. {
  1495. for (int k = 0; k < 3; ++k)
  1496. split[(std::size_t) k].design (sr, f[(std::size_t) k], slope);
  1497. ap2Low.design (sr, f[1], slope);
  1498. ap3Low.design (sr, f[2], slope);
  1499. ap3Mid.design (sr, f[2], slope);
  1500. if (nb != splitBands)
  1501. {
  1502. for (auto* x : { &split[0], &split[1], &split[2], &ap2Low, &ap3Low, &ap3Mid })
  1503. x->reset();
  1504. for (auto& d : bandDry)
  1505. d.reset();
  1506. }
  1507. splitHz = f;
  1508. splitSlope = slope;
  1509. splitBands = nb;
  1510. }
  1511. const auto lc = lowCutHz (p[lowCut]), hc = highCutHz (p[highCut]);
  1512. if (lc > 0.0)
  1513. lowcut.filter (sr, 1, lc, 0.70710678);
  1514. if (hc > 0.0)
  1515. highcut.filter (sr, 0, std::min (hc, sr * 0.45), 0.70710678);
  1516. const auto tilt = toneTilt (p[tone]);
  1517. tiltLo.shelf (sr, 1000.0, -6.0 * tilt, false, 0.5);
  1518. tiltHi.shelf (sr, 1000.0, 6.0 * tilt, true, 0.5);
  1519. const auto mx = std::clamp (p[mix], 0.0f, 1.0f);
  1520. const auto out = (float) dbToGain (lin (p[output], -18.0, 18.0));
  1521. const bool autoOn = on (p[autoGain]);
  1522. std::array<Style, maxBands> st {};
  1523. std::array<float, maxBands> drv {}, bmix {}, lvl {}, comp {};
  1524. bool anySolo = false;
  1525. for (int b = 0; b < nb; ++b) anySolo = anySolo || bandState (p[(std::size_t) bandExtra (b, 1)]) == BandState::solo;
  1526. for (int b = 0; b < nb; ++b)
  1527. {
  1528. st[(std::size_t) b] = styleOf (p[(std::size_t) bandParam (b, 0)]);
  1529. drv[(std::size_t) b] = (float) dbToGain (driveDb (p[(std::size_t) bandParam (b, 1)]));
  1530. bmix[(std::size_t) b] = std::clamp (p[(std::size_t) bandParam (b, 2)], 0.0f, 1.0f);
  1531. lvl[(std::size_t) b] = (float) dbToGain (bandLevelDb (p[(std::size_t) bandParam (b, 3)]));
  1532. comp[(std::size_t) b] = autoOn ? autoMakeup (st[(std::size_t) b], drv[(std::size_t) b]) : 1.0f;
  1533. bandOs[(std::size_t) b][0].setFactor (factor);
  1534. bandOs[(std::size_t) b][1].setFactor (factor);
  1535. }
  1536. for (int i = 0; i < n; ++i)
  1537. {
  1538. float x[2] { l[i], r[i] };
  1539. for (int c = 0; c < 2; ++c)
  1540. {
  1541. auto v = x[c];
  1542. if (lc > 0.0)
  1543. v = lowcut.process (v, c);
  1544. float b0, rest, b1, rest2, b2, b3;
  1545. split[0].run (v, c, b0, rest);
  1546. if (nb == 2)
  1547. {
  1548. band (0, c, i) = b0;
  1549. band (1, c, i) = rest;
  1550. continue;
  1551. }
  1552. split[1].run (rest, c, b1, rest2);
  1553. b0 = ap2Low.allpass (b0, c);
  1554. if (nb == 3)
  1555. {
  1556. band (0, c, i) = b0;
  1557. band (1, c, i) = b1;
  1558. band (2, c, i) = rest2;
  1559. continue;
  1560. }
  1561. split[2].run (rest2, c, b2, b3);
  1562. band (0, c, i) = ap3Low.allpass (b0, c);
  1563. band (1, c, i) = ap3Mid.allpass (b1, c);
  1564. band (2, c, i) = b2;
  1565. band (3, c, i) = b3;
  1566. }
  1567. }
  1568. // each band: keep its dry copy (delayed like the oversampled path), drive and shape it
  1569. for (int b = 0; b < nb; ++b)
  1570. {
  1571. auto* bl = bandBuf[(std::size_t) b][0].data();
  1572. auto* br = bandBuf[(std::size_t) b][1].data();
  1573. for (int i = 0; i < n; ++i)
  1574. {
  1575. float da = bl[i], db = br[i];
  1576. bandDry[(std::size_t) b].process (da, db);
  1577. dryBand[0][(std::size_t) i] = da;
  1578. dryBand[1][(std::size_t) i] = db;
  1579. bl[i] *= drv[(std::size_t) b];
  1580. br[i] *= drv[(std::size_t) b];
  1581. }
  1582. const auto s = st[(std::size_t) b];
  1583. // round 128: the band's feedback, dynamics, tone, power and solo / mute
  1584. auto& ex = extras[(std::size_t) b];
  1585. ex.configure (p, b, sr, factor);
  1586. if (ex.fbGain > 0.0)
  1587. {
  1588. auto left = [&ex, s] (float v) noexcept { return ex.shapeWithFeedback (s, v, 0); };
  1589. auto right = [&ex, s] (float v) noexcept { return ex.shapeWithFeedback (s, v, 1); };
  1590. bandOs[(std::size_t) b][0].process (bl, n, left);
  1591. bandOs[(std::size_t) b][1].process (br, n, right);
  1592. }
  1593. else
  1594. {
  1595. auto curve = [s] (float v) noexcept { return shape (s, v); };
  1596. bandOs[(std::size_t) b][0].process (bl, n, curve);
  1597. bandOs[(std::size_t) b][1].process (br, n, curve);
  1598. }
  1599. const auto m = bmix[(std::size_t) b], g = lvl[(std::size_t) b], k = comp[(std::size_t) b];
  1600. const bool post = ex.toneOn || ex.dyn != 0.0;
  1601. const bool powered = on (p[(std::size_t) bandExtra (b, 0)]);
  1602. const auto state = bandState (p[(std::size_t) bandExtra (b, 1)]);
  1603. const bool silent = state == BandState::mute || (anySolo && state != BandState::solo);
  1604. for (int i = 0; i < n; ++i)
  1605. {
  1606. auto wa = bandDc[(std::size_t) b].process (bl[i], 0) * k, wb = bandDc[(std::size_t) b].process (br[i], 1) * k;
  1607. if (post) ex.post (wa, wb, dryBand[0][(std::size_t) i], dryBand[1][(std::size_t) i]);
  1608. auto ya = (dryBand[0][(std::size_t) i] + (wa - dryBand[0][(std::size_t) i]) * m) * g;
  1609. auto yb = (dryBand[1][(std::size_t) i] + (wb - dryBand[1][(std::size_t) i]) * m) * g;
  1610. if (! powered) { ya = dryBand[0][(std::size_t) i]; yb = dryBand[1][(std::size_t) i]; }
  1611. if (silent) { ya = 0.0f; yb = 0.0f; }
  1612. if (b == 0)
  1613. {
  1614. wl[(std::size_t) i] = ya;
  1615. wr[(std::size_t) i] = yb;
  1616. }
  1617. else
  1618. {
  1619. wl[(std::size_t) i] += ya;
  1620. wr[(std::size_t) i] += yb;
  1621. }
  1622. }
  1623. }
  1624. for (int i = 0; i < n; ++i)
  1625. {
  1626. auto a = tiltHi.process (tiltLo.process (wl[(std::size_t) i], 0), 0);
  1627. auto b = tiltHi.process (tiltLo.process (wr[(std::size_t) i], 1), 1);
  1628. if (hc > 0.0)
  1629. {
  1630. a = highcut.process (a, 0);
  1631. b = highcut.process (b, 1);
  1632. }
  1633. float da = l[i], db = r[i];
  1634. dry.process (da, db);
  1635. l[i] = (da + (a - da) * mx) * out;
  1636. r[i] = (db + (b - db) * mx) * out;
  1637. }
  1638. meters.set (0.0f, 0.0f, inPeak, Meters::peak (l, r, n));
  1639. }
  1640. float& band (int b, int c, int i) noexcept { return bandBuf[(std::size_t) b][(std::size_t) c][(std::size_t) i]; }
  1641. static constexpr int maxBlock = 2048;
  1642. // level compensation: the RMS gain of the curve on a −12 dBFS sine at this drive, inverted
  1643. static float autoMakeup (Style s, float drv) noexcept
  1644. {
  1645. double in = 0.0, out = 0.0, mean = 0.0;
  1646. constexpr int points = 64;
  1647. std::array<double, points> ys {};
  1648. for (int k = 0; k < points; ++k)
  1649. {
  1650. const auto x = 0.25 * std::sin (2.0 * pi * (k + 0.5) / points);
  1651. ys[(std::size_t) k] = shape (s, (float) (x * drv));
  1652. mean += ys[(std::size_t) k];
  1653. in += x * x;
  1654. }
  1655. mean /= points;
  1656. for (auto y : ys)
  1657. out += (y - mean) * (y - mean);
  1658. return out > 1e-12 ? (float) std::sqrt (in / out) : 1.0f;
  1659. }
  1660. double sr = 44100.0;
  1661. std::array<Oversampler, 2> os;
  1662. Delay2<256> dry;
  1663. Biquad lowcut, emphasis, deemphasis, tiltLo, tiltHi, highcut;
  1664. OnePoleHp dc;
  1665. std::array<float, maxBlock> wl {}, wr {};
  1666. float makeup = 1.0f, lastDrive = -1.0f;
  1667. Style lastStyle = Style::tube;
  1668. // multiband mode
  1669. std::array<LrSplit, 3> split;
  1670. LrSplit ap2Low, ap3Low, ap3Mid;
  1671. std::array<double, 3> splitHz {};
  1672. int splitSlope = 0, splitBands = 0;
  1673. std::array<std::array<Oversampler, 2>, maxBands> bandOs;
  1674. std::array<Delay2<256>, maxBands> bandDry;
  1675. std::array<OnePoleHp, maxBands> bandDc;
  1676. std::array<std::array<std::array<float, maxBlock>, 2>, maxBands> bandBuf {};
  1677. std::array<std::array<float, maxBlock>, 2> dryBand {};
  1678. std::array<BandExtras, maxBands> extras;
  1679. Meters meters;
  1680. };
  1681. } // namespace
  1682. } // namespace ember
  1683. // ================================================================== registry
  1684. const EffectInfo& ridgeInfo()
  1685. {
  1686. static const EffectInfo info { "Ridge Strip",
  1687. { { "Input", 0.5f }, { "HPF", 0.0f }, { "LPF", 0.0f },
  1688. { "LF gain", 0.5f }, { "LF freq.", 0.40f }, { "LF bell", 0.0f },
  1689. { "LMF gain", 0.5f }, { "LMF freq.", 0.43f }, { "LMF Q", 0.43f },
  1690. { "HMF gain", 0.5f }, { "HMF freq.", 0.49f }, { "HMF Q", 0.43f },
  1691. { "HF gain", 0.5f }, { "HF freq.", 0.70f }, { "HF bell", 0.0f },
  1692. { "EQ curve", 0.0f }, { "EQ on", 1.0f },
  1693. { "Ratio", 0.0f }, { "Comp. threshold", 1.0f }, { "Comp. release", 0.29f }, { "Fast attack", 0.0f },
  1694. { "Gate range", 0.0f }, { "Gate threshold", 0.25f }, { "Gate release", 0.29f }, { "Gate fast", 0.0f },
  1695. { "Expander", 0.0f }, { "Dynamics on", 1.0f },
  1696. { "Order", 1.0f }, { "Filters to SC", 0.0f }, { "Output", 0.5f }, { "Phase", 0.0f }, { "Comp. mix", 1.0f },
  1697. { "Voicing", 0.0f }, { "Filter split", 0.0f } } };
  1698. return info;
  1699. }
  1700. const EffectInfo& glowInfo()
  1701. {
  1702. static const EffectInfo info { "Glow Leveler",
  1703. { { "Peak reduction", 0.0f }, { "Gain", 0.0f }, { "Limit", 0.0f }, { "Emphasis", 0.0f }, { "Mix", 1.0f },
  1704. { "Trim", 0.5f }, { "Auto makeup", 0.0f }, { "Voicing", 0.0f } } };
  1705. return info;
  1706. }
  1707. const EffectInfo& hushInfo()
  1708. {
  1709. static const EffectInfo info { "Hush De-esser",
  1710. { { "Frequency", 0.53f }, { "Threshold", 0.5f }, { "Range", 0.5f }, { "Wideband", 0.0f },
  1711. { "Band detector", 0.0f }, { "Listen", 0.0f }, { "Lookahead", 1.0f }, { "Release", 0.56f },
  1712. { "Output", 0.5f }, { "Voicing", 0.0f }, // round 123 (vmatch123)
  1713. // round 128 (parity with the reference de-esser)
  1714. { "Allround", 0.0f }, { "Low-pass freq.", 1.0f }, { "Lookahead time", 2.0f / 15.0f },
  1715. { "Stereo link", 1.0f }, { "Link mid", 0.0f }, { "External SC", 0.0f }, { "Input", 0.5f },
  1716. { "Display", 1.0f },
  1717. { "Oversampling", 0.0f } } }; // round 135
  1718. return info;
  1719. }
  1720. const EffectInfo& aetherInfo()
  1721. {
  1722. static const EffectInfo info { "Aether",
  1723. { { "Air", 0.0f }, { "Air freq.", 0.62f }, { "Harmonics", 0.0f }, { "Exciter freq.", 0.56f },
  1724. { "Character", 0.5f }, { "Mix", 1.0f }, { "Output", 0.5f },
  1725. { "Dynamic", 0.0f }, { "Mid air", 0.0f }, { "High air", 0.0f } } };
  1726. return info;
  1727. }
  1728. const EffectInfo& emberInfo()
  1729. {
  1730. static const EffectInfo info { "Ember Saturator",
  1731. { { "Drive", 0.0f }, { "Style", 0.0f }, { "Tone", 0.5f }, { "Low cut", 0.0f }, { "High cut", 1.0f },
  1732. { "Mix", 1.0f }, { "Output", 0.5f }, { "Auto gain", 1.0f }, { "Oversampling", 0.5f },
  1733. { "Bands", 0.0f }, { "Crossover 1", 0.3266f }, { "Crossover 2", 0.6574f }, { "Crossover 3", 0.9375f },
  1734. { "Crossover slope", 0.5f }, { "B1 mix", 1.0f }, { "B1 level", 0.5f },
  1735. { "B2 style", 0.0f }, { "B2 drive", 0.0f }, { "B2 mix", 1.0f }, { "B2 level", 0.5f },
  1736. { "B3 style", 0.0f }, { "B3 drive", 0.0f }, { "B3 mix", 1.0f }, { "B3 level", 0.5f },
  1737. { "B4 style", 0.0f }, { "B4 drive", 0.0f }, { "B4 mix", 1.0f }, { "B4 level", 0.5f },
  1738. // round 128 (Saturn parity)
  1739. { "Input", 0.5f },
  1740. { "B1 on", 1.0f }, { "B1 state", 0.0f }, { "B1 bass", 0.5f }, { "B1 mid", 0.5f }, { "B1 treble", 0.5f }, { "B1 presence", 0.5f },
  1741. { "B1 feedback", 0.0f }, { "B1 fb freq.", 0.6990f }, { "B1 dynamics", 0.5f },
  1742. { "B2 on", 1.0f }, { "B2 state", 0.0f }, { "B2 bass", 0.5f }, { "B2 mid", 0.5f }, { "B2 treble", 0.5f }, { "B2 presence", 0.5f },
  1743. { "B2 feedback", 0.0f }, { "B2 fb freq.", 0.6990f }, { "B2 dynamics", 0.5f },
  1744. { "B3 on", 1.0f }, { "B3 state", 0.0f }, { "B3 bass", 0.5f }, { "B3 mid", 0.5f }, { "B3 treble", 0.5f }, { "B3 presence", 0.5f },
  1745. { "B3 feedback", 0.0f }, { "B3 fb freq.", 0.6990f }, { "B3 dynamics", 0.5f },
  1746. { "B4 on", 1.0f }, { "B4 state", 0.0f }, { "B4 bass", 0.5f }, { "B4 mid", 0.5f }, { "B4 treble", 0.5f }, { "B4 presence", 0.5f },
  1747. { "B4 feedback", 0.0f }, { "B4 fb freq.", 0.6990f }, { "B4 dynamics", 0.5f } } };
  1748. return info;
  1749. }
  1750. std::unique_ptr<Effect> makeHush (const EffectParams* params) { return std::make_unique<hush::DeEsser> (params); }
  1751. bool isSuiteKind (EffectKind kind) noexcept
  1752. {
  1753. return kind == EffectKind::ridgeStrip || kind == EffectKind::glowLeveler || kind == EffectKind::hushDeEsser
  1754. || kind == EffectKind::aether || kind == EffectKind::ember;
  1755. }
  1756. std::unique_ptr<Effect> make (EffectKind kind)
  1757. {
  1758. switch (kind)
  1759. {
  1760. case EffectKind::ridgeStrip: return std::make_unique<ridge::Strip>();
  1761. case EffectKind::glowLeveler: return std::make_unique<glow::Leveler>();
  1762. case EffectKind::hushDeEsser: return std::make_unique<hush::DeEsser>();
  1763. case EffectKind::aether: return std::make_unique<aether::Air>();
  1764. case EffectKind::ember: return std::make_unique<ember::Saturator>();
  1765. default: return nullptr;
  1766. }
  1767. }
  1768. namespace
  1769. {
  1770. std::string hzText (double f)
  1771. {
  1772. char t[32];
  1773. if (f >= 1000.0)
  1774. std::snprintf (t, sizeof (t), "%.2f kHz", f / 1000.0);
  1775. else
  1776. std::snprintf (t, sizeof (t), "%.0f Hz", f);
  1777. return t;
  1778. }
  1779. std::string fmt (const char* f, double v)
  1780. {
  1781. char t[32];
  1782. std::snprintf (t, sizeof (t), f, v);
  1783. return t;
  1784. }
  1785. std::string onOff (float v) { return on (v) ? "on" : "off"; }
  1786. } // namespace
  1787. namespace glow
  1788. {
  1789. std::string classicParamText (int param, float v)
  1790. {
  1791. switch (param)
  1792. {
  1793. case peakReduction: case gain: case emphasis: return fmt ("%.1f", v * 100.0);
  1794. default: return paramText (EffectKind::glowLeveler, param, v);
  1795. }
  1796. }
  1797. } // namespace glow
  1798. namespace hush
  1799. {
  1800. int oversamplingOf (float v) noexcept { return 1 << std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 3.0f), 0, 3); }
  1801. std::string classicParamText (int param, float v)
  1802. {
  1803. switch (param)
  1804. {
  1805. case threshold: return fmt ("%.1f dB", classicThresholdDb (v));
  1806. case range: case lookahead: case release: return "-";
  1807. default: return paramText (EffectKind::hushDeEsser, param, v);
  1808. }
  1809. }
  1810. } // namespace hush
  1811. namespace ridge
  1812. {
  1813. std::string eParamText (int param, float v)
  1814. {
  1815. switch (param)
  1816. {
  1817. case lmfQ: case hmfQ: return fmt ("Q %.2f", eQ (v));
  1818. case lfGain: case lmfGain: case hmfGain: case hfGain: return fmt ("%+.1f dB", bandGainDb (v, false));
  1819. case compThreshold: return fmt ("%+.1f dB", eCompThresholdDb (v));
  1820. case gateRange: return v <= 0.0f ? "off" : fmt ("%.1f dB", eGateRangeDb (v));
  1821. case gateThreshold: return fmt ("%+.1f dB", eGateThresholdDb (v));
  1822. case curve: return "-";
  1823. default: return paramText (EffectKind::ridgeStrip, param, v);
  1824. }
  1825. }
  1826. } // namespace ridge
  1827. std::string paramText (EffectKind kind, int param, float v)
  1828. {
  1829. if (kind == EffectKind::ridgeStrip)
  1830. {
  1831. using namespace ridge;
  1832. switch (param)
  1833. {
  1834. case input: case output: return fmt ("%+.1f dB", trimDb (v));
  1835. case hpf: return v <= 0.0f ? "off" : hzText (hpfHz (v));
  1836. case lpf: return v <= 0.0f ? "off" : hzText (lpfHz (v));
  1837. case lfGain: case lmfGain: case hmfGain: case hfGain: return fmt ("%+.1f dB", bandGainDb (v, false)) + " / " + fmt ("%+.1f", bandGainDb (v, true));
  1838. case lfFreq: return hzText (lfHz (v));
  1839. case lmfFreq: return hzText (lmfHz (v));
  1840. case hmfFreq: return hzText (hmfHz (v));
  1841. case hfFreq: return hzText (hfHz (v));
  1842. case lmfQ: case hmfQ: return fmt ("Q %.2f", bandQ (v));
  1843. case lfBell: case hfBell: return on (v) ? "bell" : "shelf";
  1844. case curve: return on (v) ? "black" : "brown";
  1845. case compRatio: return v >= 0.999f ? "inf : 1" : fmt ("%.1f : 1", ratio (v));
  1846. case compThreshold: return fmt ("%.1f dB", compThresholdDb (v));
  1847. case compRelease: case gateRelease: return fmt ("%.2f s", releaseSeconds (v));
  1848. case gateRange: return v <= 0.0f ? "off" : fmt ("%.0f dB", gateRangeDb (v));
  1849. case gateThreshold: return fmt ("%.1f dB", gateThresholdDb (v));
  1850. case expander: return on (v) ? "expander" : "gate";
  1851. case order: return on (v) ? "dyn > EQ" : "EQ > dyn";
  1852. case voicing: return on (v) ? "E console" : "classic";
  1853. case filterSplit: return on (v) ? "split" : "with EQ";
  1854. case phase: return on (v) ? "inverted" : "normal";
  1855. case compMix: return fmt ("%.0f%%", v * 100.0);
  1856. default: return onOff (v);
  1857. }
  1858. }
  1859. if (kind == EffectKind::glowLeveler)
  1860. {
  1861. using namespace glow;
  1862. switch (param)
  1863. {
  1864. case peakReduction: return fmt ("%.0f", v * 100.0);
  1865. case gain: return fmt ("%+.1f dB", gainDb (v));
  1866. case mode: return on (v) ? "limit" : "compress";
  1867. case emphasis: case mix: return fmt ("%.0f%%", v * 100.0);
  1868. case trim: return fmt ("%+.1f dB", trimDb (v));
  1869. case autoMakeup: return on (v) ? "on" : "off";
  1870. default: return fmt ("%.2f", v);
  1871. }
  1872. }
  1873. if (kind == EffectKind::hushDeEsser)
  1874. {
  1875. using namespace hush;
  1876. switch (param)
  1877. {
  1878. case frequency: return hzText (hz (v));
  1879. case threshold: return fmt ("%.1f dB", thresholdDb (v));
  1880. case range: return fmt ("%.1f dB", rangeDb (v));
  1881. case mode: return on (v) ? "wideband" : "split";
  1882. case detector: return on (v) ? "band" : "high-pass";
  1883. case allround: return on (v) ? "allround" : "single vocal";
  1884. case lowPass: return v >= 0.999f ? "off" : hzText (lowPassHz (v));
  1885. case lookaheadTime: return fmt ("%.1f ms", lookaheadTimeMs (v));
  1886. case stereoLink: return fmt ("%.0f%%", v * 100.0);
  1887. case linkMid: return on (v) ? "mid" : "stereo";
  1888. case external: return on (v) ? "external" : "internal";
  1889. case input: return fmt ("%+.1f dB", lin (v, -12.0, 12.0));
  1890. case display: return on (v) ? "shown" : "hidden";
  1891. case release: return fmt ("%.0f ms", releaseMs (v));
  1892. case output: return fmt ("%+.1f dB", lin (v, -12.0, 12.0));
  1893. case voicing: return on (v) ? "classic split" : "modern";
  1894. case oversampling: return oversamplingOf (v) == 1 ? "off" : std::to_string (oversamplingOf (v)) + "x";
  1895. default: return onOff (v);
  1896. }
  1897. }
  1898. if (kind == EffectKind::aether)
  1899. {
  1900. using namespace aether;
  1901. switch (param)
  1902. {
  1903. case air: return fmt ("%+.1f dB", airDb (v));
  1904. case airFreq: return hzText (airHz (v));
  1905. case exciterFreq: return hzText (exciterHz (v));
  1906. case character: return v < 0.05f ? "even" : (v > 0.95f ? "odd" : fmt ("%.0f%% odd", v * 100.0));
  1907. case output: return fmt ("%+.1f dB", lin (v, -12.0, 12.0));
  1908. case dynamic: return onOff (v);
  1909. default: return fmt ("%.0f%%", v * 100.0);
  1910. }
  1911. }
  1912. if (kind == EffectKind::ember)
  1913. {
  1914. using namespace ember;
  1915. switch (param)
  1916. {
  1917. case drive: return fmt ("%+.1f dB", driveDb (v));
  1918. case style:
  1919. {
  1920. static const char* names[] = { "tube", "tape", "transistor", "fuzz" };
  1921. return names[(int) styleOf (v)];
  1922. }
  1923. case tone: return fmt ("%+.0f%%", toneTilt (v) * 100.0);
  1924. case lowCut: return v <= 0.0f ? "off" : hzText (lowCutHz (v));
  1925. case highCut: return v >= 1.0f ? "off" : hzText (highCutHz (v));
  1926. case mix: return fmt ("%.0f%%", v * 100.0);
  1927. case output: return fmt ("%+.1f dB", lin (v, -18.0, 18.0));
  1928. case autoGain: return onOff (v);
  1929. case quality: return std::to_string (oversampling (v)) + "x";
  1930. case bands: return bandCount (v) == 1 ? "1 band" : std::to_string (bandCount (v)) + " bands";
  1931. case xover1: case xover2: case xover3: return hzText (crossoverHz (v));
  1932. case xoverSlope: return std::to_string (slopeDbOct (v)) + " dB/oct";
  1933. case band2Style: case band3Style: case band4Style:
  1934. {
  1935. static const char* names[] = { "tube", "tape", "transistor", "fuzz" };
  1936. return names[(int) styleOf (v)];
  1937. }
  1938. case band2Drive: case band3Drive: case band4Drive: return fmt ("%+.1f dB", driveDb (v));
  1939. case band1Mix: case band2Mix: case band3Mix: case band4Mix: return fmt ("%.0f%%", v * 100.0);
  1940. case band1Level: case band2Level: case band3Level: case band4Level: return fmt ("%+.1f dB", bandLevelDb (v));
  1941. case inputGain: return fmt ("%+.1f dB", inputDb (v));
  1942. default:
  1943. if (param >= band1On && param < band1On + maxBands * extraFields)
  1944. switch ((param - band1On) % extraFields)
  1945. {
  1946. case 0: return onOff (v);
  1947. case 1: { static const char* names[] = { "normal", "solo", "mute" }; return names[(int) bandState (v)]; }
  1948. case 2: case 3: case 4: case 5: return fmt ("%+.1f dB", toneDb (v));
  1949. case 6: return fmt ("%.0f%%", feedbackAmount (v) * 100.0);
  1950. case 7: return hzText (feedbackHz (v));
  1951. default: return fmt ("%+.2f", dynamicsAmount (v));
  1952. }
  1953. return fmt ("%.2f", v);
  1954. }
  1955. }
  1956. return fmt ("%.2f", v);
  1957. }
  1958. } // namespace lynx::mixsuite