EmberClassic.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "core/EmberClassic.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <complex>
  5. namespace lynx::emberclassic
  6. {
  7. namespace
  8. {
  9. #include "core/EmberClassicTables.inc"
  10. constexpr double pi = 3.141592653589793;
  11. constexpr int osFactor = 4;
  12. constexpr int cutHalf = 15;
  13. constexpr int osTaps[2] { 59, 19 }; // N = 4k + 3: the centre index (N − 1) / 2 is odd
  14. constexpr double osBeta[2] { 9.0, 8.5 };
  15. double besselI0 (double x)
  16. {
  17. double sum = 1.0, term = 1.0;
  18. for (int k = 1; k < 50; ++k)
  19. {
  20. term *= (x / (2.0 * k)) * (x / (2.0 * k));
  21. sum += term;
  22. if (term < 1e-12 * sum)
  23. break;
  24. }
  25. return sum;
  26. }
  27. double dbToGain (double db) noexcept { return std::pow (10.0, db / 20.0); }
  28. // The five styles' constants, fitted on the official trial (REA_rodada136 §1; tmp/vox136/lab). Style A is the one
  29. // every state of her corpus uses; E, N, T, P are fitted the same way on the same signal set (round 138: started from
  30. // A's constants with each style's measured colour FIR; mean null over the stairs, bursts and takes at drive 0 … 6:
  31. // E −16.3, N −16.6, T −12.1, P −12.5 dB, from −8.9 … −9.5 with A's curve; A: −25.2 dB).
  32. const StyleModel styleTable[styleCount] {
  33. StyleModel { -0.255358, 3.28943, 9.75, -1.14311, 424.977, -0.326658, 1090.62, -0.367324, 0.539481, 1.51943, 1.53834, 2.58446, -0.356477, -1.10167, 69.6495, 124.432, 16.5416,
  34. { -0.050348522, 0.45404479, 0.79960583, -0.25489203, 0.00071080674, 0.13172658, -0.18265066, 0.1758486, -0.13859833, 0.089003814, -0.046502489, 0.012880354, 0.0057738622, -0.013574345, 0.012208355, -0.0082160702 } },
  35. StyleModel { -1.75235, 3.28943, 9.75, 1.11786, 1115.83, 0.0747911, 9922.27, 2.51661, 0.451642, 7.54995, 0.882348, 7.96975, -0.172261, -1.54865, 64.4605, 53.2492, 8.13625,
  36. { -0.10601218, 0.88559837, 0.52104064, -0.59799496, 0.51338433, -0.44356996, 0.3946877, -0.32920235, 0.27590921, -0.21574306, 0.16618424, -0.11710939, 0.082003896, -0.04956957, 0.029232918, -0.011986428 } }, // E
  37. StyleModel { -0.350645, 3.28943, 9.75, -1.63499, 632.752, 1.62653, 1283.86, 0.729425, 0.742618, 5.08067, 1.10494, 5.74673, -0.154669, -1.96319, 28.2141, 36.5461, 13.2041,
  38. { -0.057462716, 0.51475856, 0.59565603, -0.1951887, 0.20100539, -0.16211082, 0.1583298, -0.12293189, 0.11337933, -0.08247574, 0.070085289, -0.045254, 0.03620832, -0.01890969, 0.013629928, -0.0037943876 } }, // N
  39. StyleModel { -0.0156997, 3.28943, 9.75, -0.875387, 42.6041, -2.6432, 5406.14, 2.39267, 0.237349, 2.3023, 1.07218, 4.50339, -0.380718, -2.98134, 127.063, 102.321, 25.3318,
  40. { -0.095832879, 0.81800219, 0.58278911, -0.55857331, 0.43114637, -0.35812354, 0.31761663, -0.26122303, 0.22193496, -0.17084589, 0.13440571, -0.092138368, 0.067173398, -0.038256847, 0.024569006, -0.0080880511 } }, // T
  41. StyleModel { 0.703021, 3.28943, 9.75, -0.757538, 174.47, 4.1792, 1155.66, 1.21455, 0.203818, 3.41136, 1.40577, 2.1345, -0.476152, -2.62538, 94.3472, 69.8909, 39.9236,
  42. { -0.07587156, 0.67775215, 0.59516886, -0.39260415, 0.32049256, -0.26406084, 0.24365413, -0.19383356, 0.17184911, -0.12649202, 0.10493743, -0.067138583, 0.053183781, -0.026498863, 0.020158807, -0.0039552972 } }, // P
  43. };
  44. // analog biquad (b2 s² + b1 s + b0) / (a2 s² + a1 s + a0), s = j·f in Hz → digital by the bilinear transform
  45. // prewarped at `hz`
  46. void bilinear (Biquad& f, double sr, double hz, double b2, double b1, double b0, double a2, double a1, double a0) noexcept
  47. {
  48. const double k = hz / std::tan (pi * hz / sr); // s = k (1 − z⁻¹) / (1 + z⁻¹)
  49. const double k2 = k * k;
  50. f.set (b2 * k2 + b1 * k + b0, 2.0 * (b0 - b2 * k2), b2 * k2 - b1 * k + b0,
  51. a2 * k2 + a1 * k + a0, 2.0 * (a0 - a2 * k2), a2 * k2 - a1 * k + a0);
  52. }
  53. // first-order bilinear shelf: gain `db` below (low) or above (high) `hz`, unity on the other side
  54. template <typename P>
  55. void shelf1 (P& f, double sr, double hz, double db, bool high) noexcept
  56. {
  57. const auto g = dbToGain (db);
  58. const auto k = std::tan (pi * std::min (hz, sr * 0.45) / sr);
  59. double b0, b1;
  60. if (high) { b0 = g + k; b1 = k - g; }
  61. else { b0 = 1.0 + g * k; b1 = g * k - 1.0; }
  62. const double a0 = 1.0 + k, a1 = k - 1.0;
  63. f.b0 = b0 / a0; f.b1 = b1 / a0; f.a1 = a1 / a0;
  64. }
  65. } // namespace
  66. const StyleModel& styleModel (int style) noexcept { return styleTable[std::clamp (style, 0, styleCount - 1)]; }
  67. // [EXEC] the trial's own value → text laws (VST3 parameter read-back, REA_rodada136 §1)
  68. double driveUnits (float v) noexcept { return 10.0 * std::clamp ((double) v, 0.0, 1.0); }
  69. double toneDb (float v) noexcept { return -12.0 + 24.0 * std::clamp ((double) v, 0.0, 1.0); }
  70. double lowCutHz (float v) noexcept { return 20.0 * std::pow (50.0, std::clamp ((double) v, 0.0, 1.0)); }
  71. double highCutHz (float v) noexcept { return 1000.0 * std::pow (20.0, std::clamp ((double) v, 0.0, 1.0)); }
  72. double outputDb (float v) noexcept { return -24.0 + 24.0 * std::clamp ((double) v, 0.0, 1.0); }
  73. int styleOf (float v) noexcept { return std::clamp ((int) std::lround (std::clamp (v, 0.0f, 1.0f) * 4.0f), 0, styleCount - 1); }
  74. // ---- the ×4 oversampler (same half-band polyphase scheme as mixsuite::Oversampler, shorter filters)
  75. Engine::Os4::Os4()
  76. {
  77. for (int k = 0; k < 2; ++k)
  78. {
  79. auto& stage = st[(std::size_t) k];
  80. const int n = osTaps[k], c = (n - 1) / 2;
  81. const auto i0b = besselI0 (osBeta[k]);
  82. double sum = 0.0;
  83. std::array<double, 32> h {};
  84. for (int j = 0; 2 * j < n; ++j)
  85. {
  86. const int t = 2 * j - c;
  87. const auto r = (double) t / c;
  88. const auto w = besselI0 (osBeta[k] * std::sqrt (std::max (0.0, 1.0 - r * r))) / i0b;
  89. h[(std::size_t) j] = std::sin (0.5 * pi * t) / (0.5 * pi * t) * w;
  90. sum += h[(std::size_t) j];
  91. }
  92. stage.taps = (n + 1) / 2;
  93. for (int j = 0; j < stage.taps; ++j)
  94. stage.coef[(std::size_t) j] = (float) (h[(std::size_t) j] * 0.5 / sum);
  95. stage.centreDelay = (c - 1) / 2;
  96. }
  97. }
  98. void Engine::Os4::reset() noexcept
  99. {
  100. for (auto& s : st)
  101. {
  102. s.upHist.fill (0.0f);
  103. s.evenHist.fill (0.0f);
  104. s.oddHist.fill (0.0f);
  105. s.upPos = s.downPos = 0;
  106. }
  107. pad.fill (0.0f);
  108. padPos = 0;
  109. }
  110. // raw path: stage 1 (59 taps) 29 base samples, stage 2 (19 taps) 9 samples at 2× = 4.5 → 33.5, plus the half sample
  111. // of the 4× → base alignment: 34 [EXEC: impulse test in tests/VoiceMatch136Tests.inc]
  112. int Engine::Os4::latency() noexcept { return 34; }
  113. void Engine::Os4::up (Stage& s, float x, float& a, float& b) noexcept
  114. {
  115. const int L = s.taps;
  116. s.upPos = s.upPos == 0 ? L - 1 : s.upPos - 1;
  117. s.upHist[(std::size_t) s.upPos] = s.upHist[(std::size_t) (s.upPos + L)] = x;
  118. const float* hist = s.upHist.data() + s.upPos;
  119. float acc = 0.0f;
  120. for (int j = 0; j < L; ++j)
  121. acc += s.coef[(std::size_t) j] * hist[j];
  122. a = 2.0f * acc;
  123. b = hist[s.centreDelay];
  124. }
  125. float Engine::Os4::down (Stage& s, float a, float b) noexcept
  126. {
  127. const int L = s.taps;
  128. s.downPos = s.downPos == 0 ? L - 1 : s.downPos - 1;
  129. s.evenHist[(std::size_t) s.downPos] = s.evenHist[(std::size_t) (s.downPos + L)] = a;
  130. s.oddHist[(std::size_t) s.downPos] = s.oddHist[(std::size_t) (s.downPos + L)] = b;
  131. const float* even = s.evenHist.data() + s.downPos;
  132. float acc = 0.0f;
  133. for (int j = 0; j < L; ++j)
  134. acc += s.coef[(std::size_t) j] * even[j];
  135. return acc + 0.5f * s.oddHist[(std::size_t) (s.downPos + s.centreDelay + 1)];
  136. }
  137. int Engine::latency() noexcept { return mixsuite::Oversampler::latency(); }
  138. void Engine::prepare (double sampleRate)
  139. {
  140. sr = sampleRate > 0 ? sampleRate : 48000.0;
  141. wet.assign ((size_t) maxBlock, 0.0f);
  142. for (auto& c : ch)
  143. {
  144. c.dry.assign ((size_t) latency(), 0.0f);
  145. c.wetPad.assign ((size_t) std::max (1, latency() - Os4::latency() - cutHalf), 0.0f);
  146. }
  147. design();
  148. reset();
  149. }
  150. void Engine::reset() noexcept
  151. {
  152. lowCut.reset();
  153. tone.reset();
  154. for (auto* p : { &preLow, &preHigh, &postLow, &postHigh }) p->reset();
  155. for (auto& c : ch)
  156. {
  157. c.dcX = c.dcY = 0.0;
  158. c.env = 0.0;
  159. c.os.reset();
  160. std::fill (c.dry.begin(), c.dry.end(), 0.0f);
  161. std::fill (c.wetPad.begin(), c.wetPad.end(), 0.0f);
  162. c.padPos = 0;
  163. c.styleHist.fill (0.0f);
  164. c.cutHist.fill (0.0f);
  165. c.dryPos = c.stylePos = c.cutPos = 0;
  166. }
  167. }
  168. void Engine::set (const Settings& settings, const StyleModel* model)
  169. {
  170. s = settings;
  171. m = model != nullptr ? *model : styleModel (s.style);
  172. design();
  173. }
  174. void Engine::design()
  175. {
  176. // [EXEC] Low cut: a 2nd-order high-pass at the knob's frequency, Q 0.847; Thump raises its Q to 1.51 (the bump
  177. // sits at the corner). Max error 0.1–0.4 dB over 20 Hz … 1 kHz (REA_rodada136 §1.2).
  178. const double fc = std::clamp (s.lowCutHz, 10.0, sr * 0.4), q = s.thump ? 1.5133 : 0.8466;
  179. bilinear (lowCut, sr, fc, 1.0 / (fc * fc), 0.0, 0.0, 1.0 / (fc * fc), 1.0 / (q * fc), 1.0);
  180. // [EXEC] Tone: a 2nd-order tilt: poles fixed (494.6 Hz, Q 0.352), zeros at 494.6 · 10^(−0.9946 t / 20) Hz, the
  181. // highs at +0.3935 t dB (t > 0) / +1.5885 t dB (t < 0); within 0.25 dB over ±12 dB
  182. {
  183. const double t = std::clamp (s.toneDb, -12.0, 12.0), fp = 494.569, qq = 0.3520;
  184. const double fz = fp * std::pow (10.0, -t * 0.9946 / 20.0);
  185. const double g = dbToGain ((t > 0 ? 0.3935 : 1.5885) * t);
  186. bilinear (tone, sr, 1000.0, g, g * fz / qq, g * fz * fz, 1.0, fp / qq, fp * fp);
  187. }
  188. shelf1 (preLow, sr, m.preLowHz, m.preLowDb, false);
  189. shelf1 (preHigh, sr, m.preHighHz, m.preHighDb, true);
  190. shelf1 (postLow, sr, m.preLowHz, -m.preLowDb, false);
  191. shelf1 (postHigh, sr, m.preHighHz, -m.preHighDb, true);
  192. // [EXEC] High cut: the measured 31-tap linear-phase FIR, interpolated between the grid points of the knob
  193. {
  194. const double v = std::clamp (std::log (std::clamp (s.highCutHz, 1000.0, 20000.0) / 1000.0) / std::log (20.0), 0.0, 1.0)
  195. * (highCutGrid - 1);
  196. const int k0 = std::min ((int) v, highCutGrid - 2);
  197. const double a = v - k0;
  198. const auto& t0 = highCutTaps[s.steep ? 1 : 0][k0];
  199. const auto& t1 = highCutTaps[s.steep ? 1 : 0][k0 + 1];
  200. for (int i = 0; i <= cutHalf; ++i)
  201. {
  202. const auto c = (float) ((1.0 - a) * t0[i] + a * t1[i]);
  203. cut[(size_t) (cutHalf + i)] = c;
  204. cut[(size_t) (cutHalf - i)] = c;
  205. }
  206. }
  207. {
  208. const double mi = std::clamp (s.mix, 0.0, 1.0) * (dryMixSteps - 1);
  209. const double oi = std::clamp (-s.outputDb, 0.0, 24.0) / 2.0;
  210. const int m0 = std::min ((int) mi, dryMixSteps - 2), o0 = std::min ((int) oi, dryOutSteps - 2);
  211. const double fm = mi - m0, fo = oi - o0;
  212. const double db = (1 - fm) * ((1 - fo) * dryGainDb[m0][o0] + fo * dryGainDb[m0][o0 + 1])
  213. + fm * ((1 - fo) * dryGainDb[m0 + 1][o0] + fo * dryGainDb[m0 + 1][o0 + 1]);
  214. dryGain = dbToGain (db);
  215. }
  216. const double osr = sr * osFactor; // the curve (and the bias envelope) runs at 4×
  217. gain = dbToGain (m.gain0Db + m.gainPerUnitDb * std::clamp (s.drive, 0.0, 10.0) + (s.punish ? m.punishDb : 0.0));
  218. out = dbToGain (m.outDb);
  219. dcA = std::exp (-2.0 * pi * m.dcHz / sr);
  220. envA = std::exp (-1.0 / (std::max (m.envAttackMs, 0.01) * 0.001 * osr));
  221. envR = std::exp (-1.0 / (std::max (m.envReleaseMs, 0.01) * 0.001 * osr));
  222. }
  223. float Engine::curve (float v, double& env) const noexcept
  224. {
  225. // the bias follows the curve's own low-passed output (a coupling capacitor charged by the asymmetric current:
  226. // "blocking" distortion), with separate charge / discharge times [model fitted to the trial, §1.3]
  227. const double b = m.bias + m.biasDyn * env;
  228. auto side = [this] (double x) noexcept
  229. {
  230. const double c = x >= 0.0 ? m.cPos : m.cNeg, p = x >= 0.0 ? m.pPos : m.pNeg;
  231. return x / std::pow (1.0 + std::pow (std::abs (x / c), p), 1.0 / p);
  232. };
  233. const double y = side ((double) v + b) - side (b);
  234. env = y + (y > env ? envA : envR) * (env - y);
  235. return (float) y;
  236. }
  237. void Engine::process (float* left, float* right, int n) noexcept
  238. {
  239. for (int off = 0; off < n; off += maxBlock)
  240. block (left != nullptr ? left + off : nullptr, right != nullptr ? right + off : nullptr, std::min (maxBlock, n - off));
  241. }
  242. void Engine::block (float* left, float* right, int n) noexcept
  243. {
  244. float* io[2] { left, right };
  245. const int lat = latency();
  246. // [EXEC] the Output knob scales the wet path; the dry part of the Mix follows it partly (dryGainDb, measured)
  247. const double trim = dbToGain (s.outputDb), wetMix = s.mix * trim, dryMix = (1.0 - s.mix) * dryGain;
  248. for (int c = 0; c < 2; ++c)
  249. {
  250. if (io[c] == nullptr) continue;
  251. auto& k = ch[(size_t) c];
  252. float* x = io[c];
  253. for (int i = 0; i < n; ++i)
  254. {
  255. float v = lowCut.process (x[i], c);
  256. v = tone.process (v, c);
  257. v = preLow.process (v, c);
  258. v = preHigh.process (v, c);
  259. wet[(size_t) i] = (float) (v * gain);
  260. }
  261. auto f = [this, &k] (float v) noexcept { return curve (v, k.env); };
  262. for (int i = 0; i < n; ++i)
  263. wet[(size_t) i] = k.os.run (wet[(size_t) i], f);
  264. for (int i = 0; i < n; ++i)
  265. {
  266. float v = postLow.process (wet[(size_t) i], c);
  267. v = postHigh.process (v, c);
  268. const double y0 = v - k.dcX + dcA * k.dcY; // DC blocker
  269. k.dcX = v;
  270. k.dcY = y0;
  271. // pad to Ember's latency (the oversampler and the High cut take less)
  272. const float y = k.wetPad[(size_t) k.padPos];
  273. k.wetPad[(size_t) k.padPos] = (float) y0;
  274. k.padPos = (k.padPos + 1) % (int) k.wetPad.size();
  275. // style FIR (causal) then the High cut FIR (centre 15)
  276. k.styleHist[(size_t) k.stylePos] = (float) y;
  277. double acc = 0.0;
  278. for (int j = 0; j < styleFirTaps; ++j) acc += m.fir[(size_t) j] * k.styleHist[(size_t) ((k.stylePos - j) & 63)];
  279. k.stylePos = (k.stylePos + 1) & 63;
  280. k.cutHist[(size_t) k.cutPos] = (float) acc;
  281. double w = 0.0;
  282. for (int j = 0; j < 2 * cutHalf + 1; ++j) w += cut[(size_t) j] * k.cutHist[(size_t) ((k.cutPos - j) & 63)];
  283. k.cutPos = (k.cutPos + 1) & 63;
  284. // dry: the input delayed by the wet path's latency
  285. const float d = k.dry[(size_t) k.dryPos];
  286. k.dry[(size_t) k.dryPos] = x[i];
  287. k.dryPos = (k.dryPos + 1) % lat;
  288. x[i] = (float) (wetMix * w * out + dryMix * d);
  289. }
  290. }
  291. }
  292. } // namespace lynx::emberclassic