VoiceTools.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #include "core/VoiceTools.h"
  2. #include <algorithm>
  3. #include <array>
  4. #include <atomic>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <vector>
  8. #include "core/Biquad.h"
  9. #include "core/Effects.h"
  10. #include "core/LrCrossover.h"
  11. // REA_rodada119. Literature behind the two engines:
  12. // - Linkwitz-Riley crossovers and the all-pass compensation that keeps a band split phase-coherent:
  13. // S. Linkwitz, "Active Crossover Networks for Noncoincident Drivers", JAES 24(1), 1976; the tree / all-pass
  14. // arrangement as in U. Zölzer (ed.), "DAFX", 2nd ed., ch. 2 (filters) and ch. 4 (dynamics);
  15. // - multiband downward expansion as a broadband noise reducer (the classic "noise gate per band" scheme of tape
  16. // noise reduction units and single-ended hiss reducers), with a soft-knee gain computer and log-domain smoothing:
  17. // D. Giannoulis, M. Massberg, J. D. Reiss, "Digital Dynamic Range Compressor Design — A Tutorial and Analysis",
  18. // JAES 60(6), 2012 (the same computer, below the threshold);
  19. // - delay-line pitch shifting with two crossfaded read taps (the "rotating tape head" / Doppler shifter):
  20. // DAFX ch. 6 (time-segment processing) and J. Dattorro, "Effect Design, Part 2: Delay-Line Modulation and
  21. // Chorus", JAES 45(10), 1997; doubling by short, slightly detuned delays: the same paper (doubler / chorus);
  22. // - shelving filters: R. Bristow-Johnson, "Cookbook formulae for audio EQ biquad filter coefficients".
  23. namespace lynx::voicetools
  24. {
  25. namespace
  26. {
  27. constexpr double pi = 3.141592653589793;
  28. inline double clamp01 (float v) noexcept { return (double) std::clamp (v, 0.0f, 1.0f); }
  29. inline double dbToGain (double db) noexcept { return std::pow (10.0, db / 20.0); }
  30. inline bool on (float v) noexcept { return v >= 0.5f; }
  31. inline double coef (double ms, double sr) noexcept { return ms <= 0.0 ? 0.0 : std::exp (-1000.0 / (ms * sr)); }
  32. std::string fmt (const char* f, double v)
  33. {
  34. char b[64];
  35. std::snprintf (b, sizeof b, f, v);
  36. return b;
  37. }
  38. std::string hzText (double f) { return f >= 1000.0 ? fmt ("%.2f kHz", f / 1000.0) : fmt ("%.0f Hz", f); }
  39. } // namespace
  40. // =====================================================================================================================
  41. // Quell Denoiser
  42. // =====================================================================================================================
  43. namespace quell
  44. {
  45. double outputDb (float v) noexcept { return -12.0 + 24.0 * clamp01 (v); }
  46. const Tuning& defaultTuning() noexcept
  47. {
  48. // REA_rodada119 §3: fitted to the reference's band gains on three vocal takes (raw and −12 dB) at Amount 23, 40
  49. // and 70 % plus sine stairs and tone bursts (tmp/w119/py/qfit.py)
  50. static const Tuning t = []
  51. {
  52. Tuning u;
  53. const double thr0[bands] { -34.00, -52.00, -43.50, -47.50, -51.00, -65.00 };
  54. const double thr1[bands] { -21.00, -42.50, -39.00, -40.00, -42.00, -48.50 };
  55. const double r0[bands] { 8.00, 27.00, 29.00, 26.00, 29.00, 13.50 };
  56. const double r1[bands] { 25.00, 26.00, 27.50, 26.00, 26.00, 17.50 };
  57. const double c0[bands] { 0.00, 0.00, 0.00, 0.50, 0.50, 2.30 };
  58. const double c1[bands] { 1.50, -0.75, 0.25, 0.00, 0.50, 3.20 };
  59. for (int b = 0; b < bands; ++b)
  60. {
  61. u.thr0[b] = thr0[b];
  62. u.thr1[b] = thr1[b];
  63. u.range0[b] = r0[b];
  64. u.range1[b] = r1[b];
  65. u.floorCut0[b] = c0[b];
  66. u.floorCut1[b] = c1[b];
  67. }
  68. u.slope = 1.40;
  69. u.knee = 19.00;
  70. u.detAttackMs = 2.00;
  71. u.detReleaseMs = 5.00;
  72. u.openMs = 2.00;
  73. u.closeMs = 45.00;
  74. return u;
  75. }();
  76. return t;
  77. }
  78. double reductionDb (const Tuning& t, int b, double levelDb, double amount) noexcept
  79. {
  80. b = std::clamp (b, 0, bands - 1);
  81. amount = std::clamp (amount, 0.0, 1.0);
  82. const auto thr = t.thr0[b] + (t.thr1[b] - t.thr0[b]) * amount;
  83. const auto range = t.range0[b] + (t.range1[b] - t.range0[b]) * amount;
  84. const auto cut = t.floorCut0[b] + (t.floorCut1[b] - t.floorCut0[b]) * amount;
  85. // soft-knee downward expander: below the knee the reduction grows by `slope` dB per dB
  86. const auto under = thr - levelDb, half = 0.5 * t.knee;
  87. double r;
  88. if (under <= -half)
  89. r = 0.0;
  90. else if (under >= half || t.knee <= 0.0)
  91. r = t.slope * under;
  92. else
  93. r = t.slope * (under + half) * (under + half) / (2.0 * t.knee);
  94. return std::min (range, std::max (0.0, r)) + cut;
  95. }
  96. namespace
  97. {
  98. class Quell final : public Effect
  99. {
  100. public:
  101. explicit Quell (const Tuning& t) : tuning (t) {}
  102. void prepare (double sampleRate) noexcept override
  103. {
  104. sr = sampleRate > 0.0 ? sampleRate : 48000.0;
  105. for (int k = 0; k < bands - 1; ++k)
  106. {
  107. splitters[(std::size_t) k].design (sr, split[k], 24);
  108. for (auto& a : comp[(std::size_t) k])
  109. a.design (sr, split[k], 24);
  110. }
  111. detA = coef (tuning.detAttackMs, sr);
  112. detR = coef (tuning.detReleaseMs, sr);
  113. openC = coef (tuning.openMs, sr);
  114. closeC = coef (tuning.closeMs, sr);
  115. reset();
  116. }
  117. void reset() noexcept override
  118. {
  119. for (auto& s : splitters)
  120. s.reset();
  121. for (auto& row : comp)
  122. for (auto& a : row)
  123. a.reset();
  124. power.fill (0.0);
  125. gainDb.fill (0.0);
  126. }
  127. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  128. {
  129. const auto amt = clamp01 (p[amount]);
  130. const bool listenOnly = on (p[listen]);
  131. const auto out = (float) dbToGain (outputDb (p[output]));
  132. std::array<double, bands> sumRed {};
  133. for (int i = 0; i < n; ++i)
  134. {
  135. float band[2][bands];
  136. for (int c = 0; c < 2; ++c)
  137. {
  138. float rest = c == 0 ? l[i] : r[i];
  139. for (int k = 0; k < bands - 1; ++k)
  140. {
  141. float lo, hi;
  142. splitters[(std::size_t) k].run (rest, c, lo, hi);
  143. // keep the low band in phase with the bands split after it (their splits' all-passes)
  144. for (int j = k + 1; j < bands - 1; ++j)
  145. lo = comp[(std::size_t) j][(std::size_t) k].allpass (lo, c);
  146. band[c][k] = lo;
  147. rest = hi;
  148. }
  149. band[c][bands - 1] = rest;
  150. }
  151. float yl = 0.0f, yr = 0.0f, dl = 0.0f, dr = 0.0f;
  152. for (int b = 0; b < bands; ++b)
  153. {
  154. const auto e = 0.5 * ((double) band[0][b] * band[0][b] + (double) band[1][b] * band[1][b]);
  155. auto& pw = power[(std::size_t) b];
  156. pw = e + (pw - e) * (e > pw ? detA : detR);
  157. const auto levelDb = 10.0 * std::log10 (2.0 * pw + 1e-30); // a sine reads its peak level
  158. const auto target = -reductionDb (tuning, b, levelDb, amt);
  159. auto& g = gainDb[(std::size_t) b];
  160. g = target + (g - target) * (target > g ? openC : closeC);
  161. sumRed[(std::size_t) b] += -g;
  162. const auto lin = (float) dbToGain (g);
  163. yl += lin * band[0][b];
  164. yr += lin * band[1][b];
  165. dl += (1.0f - lin) * band[0][b];
  166. dr += (1.0f - lin) * band[1][b];
  167. }
  168. l[i] = out * (listenOnly ? dl : yl);
  169. r[i] = out * (listenOnly ? dr : yr);
  170. }
  171. double mean = 0.0;
  172. for (int b = 0; b < bands; ++b)
  173. {
  174. const auto red = n > 0 ? sumRed[(std::size_t) b] / n : 0.0;
  175. meter[(std::size_t) b + 1].store ((float) std::clamp (red / 30.0, 0.0, 1.0), std::memory_order_relaxed);
  176. mean += red / bands;
  177. }
  178. meter[0].store ((float) std::clamp (mean / 30.0, 0.0, 1.0), std::memory_order_relaxed);
  179. }
  180. float controlOutput (int k) const noexcept override
  181. {
  182. return k >= 0 && k <= bands ? meter[(std::size_t) k].load (std::memory_order_relaxed) : 0.0f;
  183. }
  184. private:
  185. Tuning tuning;
  186. double sr = 48000.0, detA = 0.0, detR = 0.0, openC = 0.0, closeC = 0.0;
  187. std::array<LrSplit, bands - 1> splitters;
  188. // comp[j][k]: split j's all-pass applied to band k (k < j)
  189. std::array<std::array<LrSplit, bands - 1>, bands - 1> comp;
  190. std::array<double, bands> power {}, gainDb {};
  191. std::array<std::atomic<float>, bands + 1> meter {};
  192. };
  193. } // namespace
  194. std::unique_ptr<Effect> make (const Tuning& t) { return std::make_unique<Quell> (t); }
  195. } // namespace quell
  196. // =====================================================================================================================
  197. // Gemini Doubler
  198. // =====================================================================================================================
  199. namespace gemini
  200. {
  201. double outputDb (float v) noexcept { return -24.0 + 24.0 * clamp01 (v); }
  202. double gainDb (float v) noexcept { return -48.0 + 48.0 * clamp01 (v); }
  203. double panDegrees (float v) noexcept { return -45.0 + 90.0 * clamp01 (v); }
  204. double delayMs (float v) noexcept { return 100.0 * clamp01 (v); }
  205. double feedback (float v) noexcept { return clamp01 (v); }
  206. double detuneCents (float v) noexcept { return -100.0 + 200.0 * clamp01 (v); }
  207. double depthCents (float v) noexcept { return -200.0 + 400.0 * clamp01 (v); }
  208. double rateValue (float v) noexcept { return 0.1 * std::pow (4000.0, clamp01 (v)); }
  209. double shelfDb (float v) noexcept { return -30.0 + 42.0 * clamp01 (v); }
  210. double freqHz (float v) noexcept { return 16.0 * std::pow (21357.0 / 16.0, clamp01 (v)); }
  211. float freqValue (double hz) noexcept { return (float) std::clamp (std::log (std::max (hz, 1.0) / 16.0) / std::log (21357.0 / 16.0), 0.0, 1.0); }
  212. int sourceOf (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 3.0), 0, 2); }
  213. double windowMs (float rangeV) noexcept { return on (rangeV) ? 53.333 : 13.333; }
  214. namespace
  215. {
  216. // mono delay line with linear-interpolated reads
  217. struct Line
  218. {
  219. std::vector<float> buf;
  220. int pos = 0;
  221. void allocate (int n) { buf.assign ((std::size_t) std::max (n, 4), 0.0f); pos = 0; }
  222. void clear() noexcept { std::fill (buf.begin(), buf.end(), 0.0f); pos = 0; }
  223. void push (float x) noexcept
  224. {
  225. buf[(std::size_t) pos] = x;
  226. if (++pos == (int) buf.size())
  227. pos = 0;
  228. }
  229. // d samples ago, linearly interpolated (the measured reference's voices)
  230. float linear (double d) const noexcept
  231. {
  232. const auto size = (int) buf.size();
  233. d = std::clamp (d, 1.0, (double) size - 4.0);
  234. const auto di = (int) d;
  235. const auto f = (float) (d - di);
  236. auto i0 = pos - di;
  237. if (i0 < 0)
  238. i0 += size;
  239. auto i1 = i0 - 1;
  240. if (i1 < 0)
  241. i1 += size;
  242. return buf[(std::size_t) i0] + f * (buf[(std::size_t) i1] - buf[(std::size_t) i0]);
  243. }
  244. // d samples ago (d ≥ 1: the sample pushed last is 1 ago)
  245. float at (double d) const noexcept
  246. {
  247. const auto size = (int) buf.size();
  248. d = std::clamp (d, 1.0, (double) size - 4.0);
  249. const auto di = (int) d;
  250. const auto f = (float) (d - di);
  251. auto i0 = pos - di;
  252. if (i0 < 0)
  253. i0 += size;
  254. // 4-point Hermite between the samples d and d + 1 ago (keeps the top octave of a fractional delay)
  255. const auto at = [this, size] (int i) { i %= size; return buf[(std::size_t) (i < 0 ? i + size : i)]; };
  256. const auto xm1 = at (i0 + 1), x0 = at (i0), x1 = at (i0 - 1), x2 = at (i0 - 2);
  257. const auto c1 = 0.5f * (x1 - xm1), c2 = xm1 - 2.5f * x0 + 2.0f * x1 - 0.5f * x2, c3 = 0.5f * (x2 - xm1) + 1.5f * (x0 - x1);
  258. return ((c3 * f + c2) * f + c1) * f + x0;
  259. }
  260. };
  261. class Gemini final : public Effect
  262. {
  263. public:
  264. void prepare (double sampleRate) noexcept override
  265. {
  266. sr = sampleRate > 0.0 ? sampleRate : 48000.0;
  267. // longest read: 100 ms of delay + the 53 ms window (+ the octave's doubled sweep) + margin
  268. const auto n = (int) std::ceil (0.25 * sr) + 8;
  269. for (auto& v : voice)
  270. v.line.allocate (n);
  271. for (auto& d : direct)
  272. d.allocate ((int) std::ceil (0.03 * sr) + 8);
  273. reset();
  274. }
  275. void reset() noexcept override
  276. {
  277. for (auto& v : voice)
  278. {
  279. v.line.clear();
  280. v.offset = 495.0;
  281. v.second = -1.0;
  282. v.phase = 0.0;
  283. v.lfo = 0.0;
  284. v.last = 0.0f;
  285. v.fbHp.reset();
  286. }
  287. for (auto& d : direct)
  288. d.clear();
  289. lowShelf.reset();
  290. highShelf.reset();
  291. designed = false;
  292. }
  293. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  294. {
  295. if (voice[0].line.buf.empty())
  296. return;
  297. design (p);
  298. const auto master = (float) dbToGain (outputDb (p[output]));
  299. const bool dirOn = on (p[directOn]);
  300. const auto dirGain = dirOn ? (float) dbToGain (gainDb (p[directGain])) : 0.0f;
  301. const auto rot = -panDegrees (p[directPan]) * pi / 180.0; // −45°: L = (L + R)/√2, R = (R − L)/√2
  302. const auto rc = (float) std::cos (rot), rs = (float) std::sin (rot);
  303. const auto window = windowMs (p[range]) * 0.001 * sr;
  304. // [EXEC] measured: with Align the direct signal comes 6.67 ms late and every pass through a voice takes its
  305. // delay + 10.3 ms (the repeats too); without it a voice takes its delay alone
  306. const bool aligned = on (p[align]);
  307. // [EXEC] round 124: 320 samples at 44.1 and 48 kHz alike (a fixed sample count, not a time)
  308. const auto alignSamples = aligned ? 320.0 : 0.0;
  309. const auto voiceExtra = (aligned ? 0.0103 * sr : 0.0) - 0.5 * window;
  310. const bool lowOnV = on (p[lowOn]), highOnV = on (p[highOn]);
  311. struct VoiceSetup
  312. {
  313. bool active;
  314. float gl, gr, fb;
  315. double delay, cents, depth, lfoInc;
  316. bool octave;
  317. int source;
  318. } vs[voices];
  319. for (int k = 0; k < voices; ++k)
  320. {
  321. auto& s = vs[k];
  322. s.active = on (p[(std::size_t) voiceParam (k, vOn)]);
  323. const auto g = dbToGain (gainDb (p[(std::size_t) voiceParam (k, vGain)]));
  324. // equal-power pan over ±45°: −45 = left only, 0 = −3 dB on each side
  325. const auto a = (panDegrees (p[(std::size_t) voiceParam (k, vPan)]) + 45.0) * pi / 180.0;
  326. s.gl = (float) (g * std::cos (a));
  327. s.gr = (float) (g * std::sin (a));
  328. s.fb = (float) feedback (p[(std::size_t) voiceParam (k, vFeedback)]);
  329. s.delay = delayMs (p[(std::size_t) voiceParam (k, vDelay)]) * 0.001 * sr;
  330. s.cents = detuneCents (p[(std::size_t) voiceParam (k, vDetune)]);
  331. s.depth = depthCents (p[(std::size_t) voiceParam (k, vDepth)]);
  332. s.lfoInc = 0.5 * rateValue (p[(std::size_t) voiceParam (k, vRate)]) / sr;
  333. s.octave = on (p[(std::size_t) voiceParam (k, vOctave)]);
  334. s.source = sourceOf (p[(std::size_t) voiceParam (k, vSource)]);
  335. }
  336. for (int i = 0; i < n; ++i)
  337. {
  338. const float inL = l[i], inR = r[i];
  339. float wetL = 0.0f, wetR = 0.0f;
  340. for (int k = 0; k < voices; ++k)
  341. {
  342. auto& v = voice[k];
  343. const auto& s = vs[k];
  344. const auto own = k == 0 ? inL : inR, other = k == 0 ? inR : inL;
  345. const auto src = s.source == 0 ? own : (s.source == 1 ? 0.5f * (own + other) : 0.5f * (own - other));
  346. // feedback from the voice's last output through the low cut
  347. v.line.push (src + s.fb * v.fbHp.process (v.last, 0));
  348. if (! s.active)
  349. {
  350. v.last = 0.0f;
  351. continue;
  352. }
  353. // triangle LFO on the pitch
  354. v.lfo += s.lfoInc;
  355. v.lfo -= std::floor (v.lfo);
  356. const auto tri = v.lfo < 0.5 ? 4.0 * v.lfo - 1.0 : 3.0 - 4.0 * v.lfo;
  357. auto ratio = std::pow (2.0, (s.cents + s.depth * tri) / 1200.0);
  358. if (s.octave)
  359. ratio *= 0.5;
  360. if (! s.octave && aligned)
  361. {
  362. // [EXEC] round 124 (official trial, impulse trains at ±1 … ±100 cents, 44.1 / 48 kHz): ONE read head
  363. // at delay + offset, the offset starting at 495 samples (so 0 cents = delay + 495 exactly) and
  364. // drifting by (1 − ratio) per sample inside a 496-sample range; at the range's end the head fades
  365. // out over 1.3 samples of offset, jumps back by the range and fades in again (pitch down), or a
  366. // second head enters at the top while the first finishes at the bottom (pitch up); linear reads
  367. const auto drift = 1.0 - ratio;
  368. float y = 0.0f;
  369. if (drift >= 0.0)
  370. {
  371. v.offset += drift;
  372. if (v.offset >= downMax)
  373. v.offset -= downMax - downMin;
  374. const auto g = std::min ({ 1.0, (downMax - v.offset) / fade, (v.offset - downMin) / fade });
  375. y = (float) std::max (0.0, g) * v.line.linear (1.0 + s.delay + v.offset);
  376. v.second = -1.0;
  377. }
  378. else
  379. {
  380. v.offset += drift;
  381. if (v.second < 0.0 && v.offset <= upMin + upOverlap)
  382. v.second = v.offset + (upMax - upMin - upOverlap);
  383. if (v.second >= 0.0)
  384. v.second += drift;
  385. if (v.offset <= upMin)
  386. {
  387. v.offset = v.second >= 0.0 ? v.second : v.offset + (upMax - upMin);
  388. v.second = -1.0;
  389. }
  390. const auto gIn = std::clamp ((upMax - v.offset) / upFadeIn, 0.0, 1.0);
  391. y = (float) gIn * v.line.linear (1.0 + s.delay + v.offset);
  392. if (v.second >= 0.0)
  393. y += (float) std::clamp ((upMax - v.second) / upFadeIn, 0.0, 1.0) * v.line.linear (1.0 + s.delay + v.second);
  394. }
  395. v.last = y;
  396. wetL += s.gl * y;
  397. wetR += s.gr * y;
  398. continue;
  399. }
  400. // two read taps half a window apart sweep the window at (1 − ratio) samples per sample
  401. v.phase += (1.0 - ratio) / window;
  402. v.phase -= std::floor (v.phase);
  403. float y = 0.0f;
  404. for (int t = 0; t < 2; ++t)
  405. {
  406. auto ph = v.phase + 0.5 * t;
  407. ph -= std::floor (ph);
  408. const auto w = (float) std::sin (pi * ph); // power-complementary crossfade of the two grains
  409. y += w * v.line.at (1.0 + s.delay + voiceExtra + ph * window);
  410. }
  411. v.last = y;
  412. wetL += s.gl * y;
  413. wetR += s.gr * y;
  414. }
  415. if (lowOnV)
  416. {
  417. wetL = lowShelf.process (wetL, 0);
  418. wetR = lowShelf.process (wetR, 1);
  419. }
  420. if (highOnV)
  421. {
  422. wetL = highShelf.process (wetL, 0);
  423. wetR = highShelf.process (wetR, 1);
  424. }
  425. // the direct signal: rotated in the stereo field, delayed by half a window when aligned with the voices
  426. direct[0].push (inL);
  427. direct[1].push (inR);
  428. const auto dL = alignSamples > 0.0 ? direct[0].at (1.0 + alignSamples) : inL;
  429. const auto dR = alignSamples > 0.0 ? direct[1].at (1.0 + alignSamples) : inR;
  430. const auto oL = dirGain * (rc * dL + rs * dR), oR = dirGain * (rc * dR - rs * dL);
  431. l[i] = master * (oL + wetL);
  432. r[i] = master * (oR + wetR);
  433. }
  434. }
  435. private:
  436. void design (const EffectParams& p) noexcept
  437. {
  438. float key[5] { p[lowGain], p[lowFreq], p[highGain], p[highFreq], p[feedbackCut] };
  439. if (designed && std::equal (key, key + 5, lastKey))
  440. return;
  441. std::copy (key, key + 5, lastKey);
  442. designed = true;
  443. // [EXEC] measured: first-order shelves; a high shelf of gain g at f0 has its corners at f0 and f0 · max (g, 1/g)
  444. // (-12 dB at 3538 Hz: -2.1 dB at 3.5 kHz, -6.8 at 8 kHz, -11.9 at 16 kHz; +6 dB at 1306 Hz: +2.6 at 1.5 kHz,
  445. // +4.5 at 3 kHz); the low shelf mirrors it (corners f0 / max (g, 1/g) and f0)
  446. firstOrderShelf (lowShelf, freqHz (p[lowFreq]), shelfDb (p[lowGain]), false);
  447. firstOrderShelf (highShelf, freqHz (p[highFreq]), shelfDb (p[highGain]), true);
  448. // [EXEC] round 124: the feedback low cut is a first-order (bilinear) high-pass — 16 Hz: -3.0 dB / +45° at
  449. // 16 Hz, first sample 0.99895 of a repeat's impulse
  450. const auto k = std::tan (pi * std::min (freqHz (p[feedbackCut]), 0.45 * sr) / sr);
  451. for (auto& v : voice)
  452. {
  453. v.fbHp.b0 = 1.0 / (1.0 + k);
  454. v.fbHp.b1 = -v.fbHp.b0;
  455. v.fbHp.b2 = 0.0;
  456. v.fbHp.a1 = (k - 1.0) / (k + 1.0);
  457. v.fbHp.a2 = 0.0;
  458. }
  459. }
  460. // H(s) = k (1 + s/wz) / (1 + s/wp), bilinear with the corners pre-warped
  461. void firstOrderShelf (Biquad& b, double f0, double db, bool high) const noexcept
  462. {
  463. const auto g = dbToGain (db);
  464. const auto r = std::max (g, 1.0 / g);
  465. const auto lo = high ? f0 : f0 / r, up = high ? f0 * r : f0;
  466. // high boost: zero lo, pole up; high cut: pole lo, zero up; low boost: pole lo, zero up, × r; low cut: zero lo, pole up, ÷ r
  467. double fz, fp, k;
  468. if (high)
  469. fz = g >= 1.0 ? lo : up, fp = g >= 1.0 ? up : lo, k = 1.0;
  470. else
  471. fz = g >= 1.0 ? up : lo, fp = g >= 1.0 ? lo : up, k = g >= 1.0 ? r : 1.0 / r;
  472. const auto warp = [this] (double f) { return std::tan (pi * std::min (f, 0.49 * sr) / sr); };
  473. const auto wz = warp (fz), wp = warp (fp);
  474. // s → (1 − z⁻¹)/(1 + z⁻¹) with s/w: (1 + s/wz) → ((wz + 1) + (wz − 1) z⁻¹) / (wz (1 + z⁻¹))
  475. const auto n0 = k * (wz + 1.0) / wz, n1 = k * (wz - 1.0) / wz;
  476. const auto d0 = (wp + 1.0) / wp, d1 = (wp - 1.0) / wp;
  477. b.b0 = n0 / d0;
  478. b.b1 = n1 / d0;
  479. b.b2 = 0.0;
  480. b.a1 = d1 / d0;
  481. b.a2 = 0.0;
  482. }
  483. // the measured read-head range (samples of offset over the voice's delay; REA_rodada124 §4)
  484. static constexpr double downMin = 79.4, downMax = 575.7, fade = 1.3;
  485. static constexpr double upMin = 62.0, upMax = 558.4, upOverlap = 4.2, upFadeIn = 0.7;
  486. struct Voice
  487. {
  488. Line line;
  489. double offset = 495.0, second = -1.0;
  490. double phase = 0.0, lfo = 0.0;
  491. float last = 0.0f;
  492. Biquad fbHp;
  493. };
  494. double sr = 48000.0;
  495. std::array<Voice, voices> voice;
  496. std::array<Line, 2> direct;
  497. Biquad lowShelf, highShelf;
  498. bool designed = false;
  499. float lastKey[5] {};
  500. };
  501. } // namespace
  502. } // namespace gemini
  503. // =====================================================================================================================
  504. // registry
  505. // =====================================================================================================================
  506. const EffectInfo& quellInfo()
  507. {
  508. static const EffectInfo info { "Quell Denoiser", { { "Amount", 0.3f }, { "Listen", 0.0f }, { "Output", 0.5f } } };
  509. return info;
  510. }
  511. const EffectInfo& geminiInfo()
  512. {
  513. static const EffectInfo info { "Gemini Doubler",
  514. { { "Output", 1.0f }, { "Direct", 1.0f }, { "Direct gain", 1.0f }, { "Direct pan", 0.5f },
  515. { "Align", 1.0f }, { "Low shelf", 0.0f }, { "Low gain", 30.0f / 42.0f },
  516. { "Low freq", gemini::freqValue (100.0) }, { "High shelf", 0.0f }, { "High gain", 30.0f / 42.0f },
  517. { "High freq", gemini::freqValue (5000.0) }, { "Feedback cut", 0.0f }, { "Range", 0.0f },
  518. // voice 1 (left) and voice 2 (right): a classic double, 9.4 / 23.7 ms, ±6 cents, −6 dB
  519. { "V1 on", 1.0f }, { "V1 gain", 0.875f }, { "V1 pan", 0.0f }, { "V1 delay", 0.094f },
  520. { "V1 feedback", 0.0f }, { "V1 detune", 0.53f }, { "V1 depth", 0.5f }, { "V1 rate", 0.2776f },
  521. { "V1 octave", 0.0f }, { "V1 source", 0.1667f },
  522. { "V2 on", 1.0f }, { "V2 gain", 0.875f }, { "V2 pan", 1.0f }, { "V2 delay", 0.237f },
  523. { "V2 feedback", 0.0f }, { "V2 detune", 0.47f }, { "V2 depth", 0.5f }, { "V2 rate", 0.2776f },
  524. { "V2 octave", 0.0f }, { "V2 source", 0.1667f } } };
  525. return info;
  526. }
  527. bool isVoiceKind (EffectKind kind) noexcept { return kind == EffectKind::quellDenoiser || kind == EffectKind::geminiDoubler; }
  528. std::unique_ptr<Effect> make (EffectKind kind)
  529. {
  530. switch (kind)
  531. {
  532. case EffectKind::quellDenoiser: return quell::make (quell::defaultTuning());
  533. case EffectKind::geminiDoubler: return std::make_unique<gemini::Gemini>();
  534. default: return nullptr;
  535. }
  536. }
  537. std::string paramText (EffectKind kind, int param, float v)
  538. {
  539. v = std::clamp (v, 0.0f, 1.0f);
  540. if (kind == EffectKind::quellDenoiser)
  541. {
  542. switch (param)
  543. {
  544. case quell::amount: return fmt ("%.1f%%", 100.0 * v);
  545. case quell::listen: return on (v) ? "removed noise" : "off";
  546. case quell::output: return fmt ("%+.1f dB", quell::outputDb (v));
  547. default: return fmt ("%.2f", v);
  548. }
  549. }
  550. if (kind == EffectKind::geminiDoubler)
  551. {
  552. using namespace gemini;
  553. if (param >= voice1 && param < paramCount)
  554. {
  555. switch ((VoiceParam) ((param - voice1) % voiceParams))
  556. {
  557. case vOn: case vOctave: return on (v) ? "on" : "off";
  558. case vGain: return fmt ("%.1f dB", gainDb (v));
  559. case vPan: return fmt ("%+.1f°", panDegrees (v));
  560. case vDelay: return fmt ("%.1f ms", delayMs (v));
  561. case vFeedback: return fmt ("%.0f%%", 100.0 * feedback (v));
  562. case vDetune: return fmt ("%+.0f c", detuneCents (v));
  563. case vDepth: return fmt ("%+.0f c", depthCents (v));
  564. case vRate: return fmt ("%.1f", rateValue (v));
  565. case vSource:
  566. {
  567. static const char* const names[3] { "own side", "L + R", "L − R" };
  568. return names[sourceOf (v)];
  569. }
  570. default: break;
  571. }
  572. return fmt ("%.2f", v);
  573. }
  574. switch (param)
  575. {
  576. case output: return fmt ("%.1f dB", outputDb (v));
  577. case directGain: return fmt ("%.1f dB", gainDb (v));
  578. case directPan: return fmt ("%+.1f°", panDegrees (v));
  579. case lowGain: case highGain: return fmt ("%+.1f dB", shelfDb (v));
  580. case lowFreq: case highFreq: case feedbackCut: return hzText (freqHz (v));
  581. case range: return on (v) ? "20 Hz" : "80 Hz";
  582. default: return on (v) ? "on" : "off";
  583. }
  584. }
  585. return fmt ("%.2f", v);
  586. }
  587. } // namespace lynx::voicetools