LynxVerbs.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. #include "core/LynxVerbs.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstdint>
  5. #include <cstdio>
  6. #include "core/Biquad.h"
  7. #include "core/Effects.h"
  8. namespace lynx::verbs
  9. {
  10. namespace
  11. {
  12. constexpr double pi = 3.14159265358979323846;
  13. constexpr int lineCount = 8;
  14. // Line-length sets of the late tank, ms at lineScale 1 (spread over about an octave, no common factors, so the
  15. // modes of the lines do not line up). The last set is a ratio set (× lineScale in ms) for delay networks.
  16. constexpr double shapes[][lineCount] = {
  17. { 37.1, 41.9, 47.3, 53.9, 61.1, 67.7, 74.3, 83.9 }, // 0 hall
  18. { 21.1, 24.7, 28.3, 32.9, 37.3, 41.9, 46.1, 51.7 }, // 1 room / chamber
  19. { 29.3, 33.1, 38.9, 44.3, 51.7, 57.1, 63.7, 71.3 }, // 2 plate
  20. { 0.031, 0.117, 0.229, 0.357, 0.493, 0.641, 0.809, 1.000 }, // 3 delay network (fractions of the delay, see lineSpread)
  21. };
  22. constexpr int shapeCount = (int) (sizeof (shapes) / sizeof (shapes[0]));
  23. // input diffusers: J. Dattorro's lengths (142, 107, 379, 277 samples at 29761 Hz), the right channel slightly apart
  24. constexpr double inApMs[2][4] = { { 4.771, 3.595, 12.735, 9.307 }, { 4.513, 3.871, 11.913, 8.711 } };
  25. constexpr double inApCoef[4] = { 0.75, 0.75, 0.625, 0.625 };
  26. // in-loop all-pass length as a fraction of its line, early smearing all-passes (ms)
  27. constexpr double loopApFraction = 0.16;
  28. constexpr double earlyApMs[2][2] = { { 7.13, 2.31 }, { 6.47, 2.83 } };
  29. constexpr int earlyTaps = 14;
  30. constexpr int buildTaps = 40;
  31. constexpr int maxBandSections = 16;
  32. // a small deterministic generator for tap patterns (the same pattern on every machine)
  33. struct Lcg
  34. {
  35. std::uint32_t s;
  36. double next() noexcept
  37. {
  38. s = s * 1664525u + 1013904223u;
  39. return (double) (s >> 8) / 16777216.0;
  40. }
  41. };
  42. inline int nextPow2 (int n)
  43. {
  44. int p = 1;
  45. while (p < n)
  46. p <<= 1;
  47. return p;
  48. }
  49. struct Line
  50. {
  51. std::vector<float> buf;
  52. int mask = 0, pos = 0;
  53. void allocate (int samples)
  54. {
  55. const auto n = nextPow2 (std::max (4, samples + 4));
  56. buf.assign ((std::size_t) n, 0.0f);
  57. mask = n - 1;
  58. pos = 0;
  59. }
  60. void clear() noexcept
  61. {
  62. std::fill (buf.begin(), buf.end(), 0.0f);
  63. pos = 0;
  64. }
  65. void write (float x) noexcept
  66. {
  67. buf[(std::size_t) pos] = x;
  68. pos = (pos + 1) & mask;
  69. }
  70. // d ≥ 1: the sample written d writes ago
  71. float read (int d) const noexcept { return buf[(std::size_t) ((pos - d) & mask)]; }
  72. // fractional delay (≥ 2), 4-point Hermite
  73. float readFrac (double d) const noexcept
  74. {
  75. const auto i = (int) d;
  76. const auto f = (float) (d - i);
  77. const auto xm1 = read (i - 1), x0 = read (i), x1 = read (i + 1), x2 = read (i + 2);
  78. const auto c1 = 0.5f * (x1 - xm1);
  79. const auto c2 = xm1 - 2.5f * x0 + 2.0f * x1 - 0.5f * x2;
  80. const auto c3 = 0.5f * (x2 - xm1) + 1.5f * (x0 - x1);
  81. return ((c3 * f + c2) * f + c1) * f + x0;
  82. }
  83. };
  84. struct Allpass
  85. {
  86. Line line;
  87. int len = 1;
  88. float process (float x, float g) noexcept
  89. {
  90. const auto d = line.read (len);
  91. const auto v = x + g * d;
  92. line.write (v);
  93. return d - g * v;
  94. }
  95. };
  96. struct OnePole
  97. {
  98. float a = 0.0f, z = 0.0f;
  99. void setLowpass (double hz, double sr) noexcept { a = (float) std::exp (-2.0 * pi * std::clamp (hz, 1.0, sr * 0.49) / sr); }
  100. float lp (float x) noexcept { return z = x + a * (z - x); }
  101. };
  102. // the two-head pitch shifter (also plays backwards: ratio −1)
  103. struct Shifter
  104. {
  105. Line line;
  106. double phase = 0.0, window = 2400.0;
  107. void prepare (double sr, double windowSec)
  108. {
  109. window = std::max (64.0, windowSec * sr);
  110. line.allocate ((int) (window * 2.5) + 8);
  111. phase = 0.0;
  112. }
  113. void clear() noexcept
  114. {
  115. line.clear();
  116. phase = 0.0;
  117. }
  118. float process (float x, double ratio) noexcept
  119. {
  120. line.write (x);
  121. phase += (1.0 - ratio) / window;
  122. phase -= std::floor (phase);
  123. float y = 0.0f;
  124. for (int h = 0; h < 2; ++h)
  125. {
  126. auto p = phase + 0.5 * h;
  127. p -= std::floor (p);
  128. const auto w = (float) std::sin (pi * p);
  129. y += w * w * line.readFrac (2.0 + p * window);
  130. }
  131. return y;
  132. }
  133. };
  134. double clampPos (double v, double lo, double hi) noexcept { return std::isfinite (v) ? std::clamp (v, lo, hi) : lo; }
  135. } // namespace
  136. // =================================================================================================================
  137. // laws
  138. double skewLaw (double v, double lo, double hi, double k) noexcept
  139. {
  140. return lo + (hi - lo) * std::pow (std::clamp (v, 0.0, 1.0), k);
  141. }
  142. double skewInverse (double unit, double lo, double hi, double k) noexcept
  143. {
  144. return std::pow (std::clamp ((unit - lo) / (hi - lo), 0.0, 1.0), 1.0 / k);
  145. }
  146. double knotLaw (double v, const double* knots, int count) noexcept
  147. {
  148. v = std::clamp (v, knots[0], knots[2 * (count - 1)]);
  149. for (int i = 1; i < count; ++i)
  150. if (v <= knots[2 * i] || i == count - 1)
  151. {
  152. const auto v0 = knots[2 * i - 2], v1 = knots[2 * i];
  153. const auto u0 = knots[2 * i - 1], u1 = knots[2 * i + 1];
  154. return v1 > v0 ? u0 + (u1 - u0) * (v - v0) / (v1 - v0) : u1;
  155. }
  156. return knots[1];
  157. }
  158. double knotInverse (double unit, const double* knots, int count) noexcept
  159. {
  160. unit = std::clamp (unit, knots[1], knots[2 * count - 1]);
  161. for (int i = 1; i < count; ++i)
  162. if (unit <= knots[2 * i + 1] || i == count - 1)
  163. {
  164. const auto v0 = knots[2 * i - 2], v1 = knots[2 * i];
  165. const auto u0 = knots[2 * i - 1], u1 = knots[2 * i + 1];
  166. return u1 > u0 ? v0 + (v1 - v0) * (unit - u0) / (u1 - u0) : v0;
  167. }
  168. return knots[0];
  169. }
  170. int choice (float v, int count) noexcept
  171. {
  172. if (count <= 1)
  173. return 0;
  174. return std::clamp ((int) std::lround (std::clamp ((double) v, 0.0, 1.0) * (count - 1)), 0, count - 1);
  175. }
  176. void mixGains (float mix, float& dry, float& wet) noexcept
  177. {
  178. const auto m = std::clamp ((double) mix, 0.0, 1.0) * pi * 0.5;
  179. dry = (float) std::cos (m);
  180. wet = (float) std::sin (m);
  181. if (mix >= 1.0f)
  182. dry = 0.0f;
  183. if (mix <= 0.0f)
  184. wet = 0.0f;
  185. }
  186. // =================================================================================================================
  187. // engine
  188. struct Engine::Impl
  189. {
  190. double sr = 48000.0, maxLineSec, maxPreSec;
  191. Line pre[2], raw[2], dif[2];
  192. Allpass inAp[2][4], earlyAp[2][2];
  193. Line lines[lineCount];
  194. Allpass loopAp[lineCount];
  195. struct Absorb
  196. {
  197. OnePole lo, hi, bw, lc;
  198. float gL = 1, gM = 1, gH = 1;
  199. } absorb[lineCount];
  200. double curLen[lineCount] {}; // glided line lengths (samples)
  201. double lfo[lineCount] {};
  202. Shifter shifters[4]; // inside lines 0, 2, 4, 6 (shimmer)
  203. Biquad outLow, outHigh, shelfLow, shelfHigh, toneHigh, toneMid, band[maxBandSections];
  204. bool outLowOn = false, outHighOn = false, shelfLowOn = false, shelfHighOn = false, toneHighOn = false, toneMidOn = false, bandOn = false;
  205. int bandSections = 0;
  206. // tap patterns (unit times and gains), drawn once
  207. double eT[2][earlyTaps] {}, eG[2][earlyTaps] {};
  208. double bT[2][buildTaps] {}, bG[2][buildTaps] {};
  209. int eD[2][earlyTaps] {}, bD[2][buildTaps] {};
  210. float eW[2][earlyTaps] {}, bW[2][buildTaps] {};
  211. Settings last;
  212. bool first = true;
  213. float shiftState = 0.0f;
  214. float apG[lineCount] {}, inG[4] {}, eApG = 0;
  215. double modDepth = 0, modInc[lineCount] {};
  216. double feedbackGain = -1;
  217. Impl (double maxLine, double maxPre) : maxLineSec (maxLine), maxPreSec (maxPre)
  218. {
  219. Lcg r { 0x2545F491u };
  220. for (int c = 0; c < 2; ++c)
  221. {
  222. for (int k = 0; k < earlyTaps; ++k)
  223. {
  224. eT[c][k] = (k + 0.15 + 0.7 * r.next()) / earlyTaps;
  225. eG[c][k] = r.next() < 0.5 ? -1.0 : 1.0;
  226. }
  227. for (int k = 0; k < buildTaps; ++k)
  228. {
  229. bT[c][k] = (k + 0.1 + 0.8 * r.next()) / buildTaps;
  230. bG[c][k] = r.next() < 0.5 ? -1.0 : 1.0;
  231. }
  232. }
  233. }
  234. void prepare (double rate)
  235. {
  236. sr = rate;
  237. const auto ms = sr / 1000.0;
  238. for (int c = 0; c < 2; ++c)
  239. {
  240. pre[c].allocate ((int) (maxPreSec * sr) + 8);
  241. raw[c].allocate ((int) (1.05 * sr) + 8);
  242. dif[c].allocate ((int) (2.05 * sr) + 8);
  243. for (int k = 0; k < 4; ++k)
  244. {
  245. inAp[c][k].len = std::max (1, (int) std::lround (inApMs[c][k] * ms));
  246. inAp[c][k].line.allocate (inAp[c][k].len + 2);
  247. }
  248. for (int k = 0; k < 2; ++k)
  249. {
  250. earlyAp[c][k].len = std::max (1, (int) std::lround (earlyApMs[c][k] * ms));
  251. earlyAp[c][k].line.allocate (earlyAp[c][k].len + 2);
  252. }
  253. }
  254. const auto maxLine = (int) (maxLineSec * sr) + 16;
  255. for (int i = 0; i < lineCount; ++i)
  256. {
  257. lines[i].allocate (maxLine + (int) (0.05 * sr));
  258. loopAp[i].line.allocate ((int) (0.2 * maxLineSec * sr) + 8);
  259. }
  260. for (auto& sh : shifters)
  261. sh.prepare (sr, 0.06);
  262. reset();
  263. }
  264. void reset() noexcept
  265. {
  266. for (int c = 0; c < 2; ++c)
  267. {
  268. pre[c].clear();
  269. raw[c].clear();
  270. dif[c].clear();
  271. for (auto& a : inAp[c])
  272. a.line.clear();
  273. for (auto& a : earlyAp[c])
  274. a.line.clear();
  275. }
  276. for (int i = 0; i < lineCount; ++i)
  277. {
  278. lines[i].clear();
  279. loopAp[i].line.clear();
  280. absorb[i].lo.z = absorb[i].hi.z = absorb[i].bw.z = absorb[i].lc.z = 0.0f;
  281. lfo[i] = 2.0 * pi * i / lineCount;
  282. }
  283. for (auto& sh : shifters)
  284. sh.clear();
  285. outLow.reset();
  286. outHigh.reset();
  287. shelfLow.reset();
  288. shelfHigh.reset();
  289. for (auto& b : band)
  290. b.reset();
  291. toneHigh.reset();
  292. toneMid.reset();
  293. shiftState = 0.0f;
  294. first = true;
  295. }
  296. double targetLen (const Settings& s, int i) const noexcept
  297. {
  298. const auto shape = std::clamp (s.shape, 0, shapeCount - 1);
  299. const auto spread = std::clamp (s.lineSpread, 0.0, 1.0);
  300. const auto ms = shape == 3 ? std::max (0.0, s.lineScale) * (1.0 - spread * (1.0 - shapes[3][i]))
  301. : shapes[shape][i] * std::max (0.0, s.lineScale);
  302. return clampPos (ms * sr / 1000.0, 8.0, maxLineSec * sr);
  303. }
  304. // coefficients that depend on the settings (once per block)
  305. void update (const Settings& s) noexcept
  306. {
  307. const auto ms = sr / 1000.0;
  308. for (int k = 0; k < 4; ++k)
  309. inG[k] = (float) (inApCoef[k] * std::clamp (s.inDiffusion, 0.0, 1.0));
  310. eApG = (float) (0.7 * std::clamp (s.earlyDiffusion, 0.0, 1.0));
  311. // tap delays and gains: early (energy-normalised, falling) and build-up (rising then falling, sum of squares 1)
  312. for (int c = 0; c < 2; ++c)
  313. {
  314. double e = 0, b = 0;
  315. for (int k = 0; k < earlyTaps; ++k)
  316. {
  317. eD[c][k] = std::clamp ((int) (eT[c][k] * std::max (0.0005, s.earlySec) * sr), 1, raw[c].mask - 4);
  318. const auto a = std::exp (-s.earlyDecay * eT[c][k]);
  319. eW[c][k] = (float) (a * eG[c][k]);
  320. e += a * a;
  321. }
  322. for (auto& w : eW[c])
  323. w = (float) (w / std::sqrt (std::max (e, 1e-9)));
  324. const auto gated = s.gateSec > 0.0;
  325. const auto span = gated ? s.gateSec : s.buildSec;
  326. for (int k = 0; k < buildTaps; ++k)
  327. {
  328. bD[c][k] = std::clamp ((int) (bT[c][k] * span * sr), 0, dif[c].mask - 4);
  329. const auto u = bT[c][k];
  330. const auto a = gated ? 0.6 + 0.4 * u : std::sin (pi * std::sqrt (u)); // build: rises, then falls
  331. bW[c][k] = (float) (a * bG[c][k]);
  332. b += a * a;
  333. }
  334. for (auto& w : bW[c])
  335. w = (float) (w / std::sqrt (std::max (b, 1e-9)));
  336. }
  337. // modulation
  338. modDepth = std::max (0.0, s.modDepthMs) * ms;
  339. for (int i = 0; i < lineCount; ++i)
  340. modInc[i] = 2.0 * pi * std::max (0.0, s.modRateHz) * (1.0 + 0.137 * i - 0.019 * i * i * 0.5) / sr;
  341. // loop filters
  342. for (int i = 0; i < lineCount; ++i)
  343. {
  344. auto& a = absorb[i];
  345. a.lo.setLowpass (std::max (20.0, s.lowXover), sr);
  346. a.hi.setLowpass (std::max (20.0, s.highXover), sr);
  347. if (s.loopBandwidth > 0.0 && s.loopBandwidth < 0.49 * sr)
  348. a.bw.setLowpass (s.loopBandwidth, sr);
  349. else
  350. a.bw.a = 0.0f;
  351. if (s.loopLowCut > 0.0)
  352. a.lc.setLowpass (s.loopLowCut, sr);
  353. else
  354. a.lc.a = 1.0f;
  355. apG[i] = (float) (0.6 * std::clamp (s.loopDiffusion, 0.0, 1.0));
  356. }
  357. // output filters
  358. outLowOn = s.outLowCut > 0.0;
  359. if (outLowOn)
  360. {
  361. if (s.outLowOrder == 1) // first order (bilinear)
  362. {
  363. const auto k = std::tan (pi * std::min (s.outLowCut, 0.45 * sr) / sr);
  364. outLow.set (1.0, -1.0, 0.0, 1.0 + k, k - 1.0, 0.0);
  365. }
  366. else
  367. outLow.filter (sr, 1, s.outLowCut, 0.7071);
  368. }
  369. outHighOn = s.outHighCut > 0.0 && s.outHighCut < 0.45 * sr;
  370. if (outHighOn)
  371. {
  372. if (s.outHighOrder == 1)
  373. {
  374. const auto k = std::tan (pi * s.outHighCut / sr);
  375. outHigh.set (k, k, 0.0, 1.0 + k, k - 1.0, 0.0);
  376. }
  377. else
  378. outHigh.filter (sr, 0, s.outHighCut, 0.7071);
  379. }
  380. bandOn = s.bandLimit > 0.0 && s.bandLimit < 0.45 * sr;
  381. if (bandOn)
  382. {
  383. // Butterworth of order 2 · sections: section k's Q = 1 / (2 cos ((2k + 1) π / (4 · sections)))
  384. bandSections = std::clamp (s.bandLimitSections, 1, maxBandSections);
  385. for (int k = 0; k < bandSections; ++k)
  386. band[k].filter (sr, 0, s.bandLimit, 1.0 / (2.0 * std::cos ((2 * k + 1) * pi / (4.0 * bandSections))));
  387. }
  388. toneHighOn = std::abs (s.toneHighDb) > 0.01;
  389. if (toneHighOn)
  390. toneHigh.shelf (sr, std::min (s.toneHighHz, 0.45 * sr), s.toneHighDb, true, 0.7);
  391. toneMidOn = std::abs (s.toneMidDb) > 0.01;
  392. if (toneMidOn)
  393. toneMid.peaking (sr, std::min (s.toneMidHz, 0.45 * sr), std::max (0.1, s.toneMidQ), s.toneMidDb);
  394. shelfLowOn = std::abs (s.lowShelfDb) > 0.01;
  395. if (shelfLowOn)
  396. shelfLow.shelf (sr, s.lowShelfHz, s.lowShelfDb, false, 0.7);
  397. shelfHighOn = std::abs (s.highShelfDb) > 0.01;
  398. if (shelfHighOn)
  399. shelfHigh.shelf (sr, std::min (s.highShelfHz, 0.45 * sr), s.highShelfDb, true, 0.7);
  400. }
  401. // per-line gains for the current lengths (cheap: once per block)
  402. void decayGains (const Settings& s) noexcept
  403. {
  404. for (int i = 0; i < lineCount; ++i)
  405. {
  406. auto& a = absorb[i];
  407. const auto len = curLen[i] + (apG[i] != 0.0f ? (double) loopAp[i].len : 0.0);
  408. if (s.feedback >= 0.0)
  409. {
  410. const auto g = std::min (0.99995, s.feedback);
  411. a.gM = (float) g;
  412. a.gL = (float) std::min (0.99995, std::pow (g, 1.0 / std::max (0.05, s.lowMult)));
  413. a.gH = (float) std::min (0.99995, std::pow (g, 1.0 / std::max (0.05, s.highMult)));
  414. continue;
  415. }
  416. const auto rt = std::max (0.01, s.gateSec > 0.0 ? std::min (s.rt60, 0.12) : s.rt60);
  417. auto gain = [&] (double mult) { return (float) std::min (0.99995, std::pow (10.0, -3.0 * len / (rt * std::max (0.02, mult) * sr))); };
  418. a.gM = gain (1.0);
  419. a.gL = gain (s.lowMult);
  420. a.gH = gain (s.highMult);
  421. }
  422. }
  423. void process (float* L, float* R, int n, const Settings& s) noexcept
  424. {
  425. update (s);
  426. const auto pd = std::clamp ((int) std::lround (s.predelaySec * sr), 0, pre[0].mask - 2);
  427. // loop all-pass lengths follow the line lengths (fixed per block)
  428. for (int i = 0; i < lineCount; ++i)
  429. {
  430. const auto target = targetLen (s, i);
  431. if (first)
  432. curLen[i] = target;
  433. loopAp[i].len = std::clamp ((int) (target * loopApFraction), 1, loopAp[i].line.mask - 2);
  434. }
  435. first = false;
  436. decayGains (s);
  437. const auto glide = 0.25; // samples per sample: a length change glides like tape
  438. const auto norm = (float) (1.0 / std::sqrt ((double) lineCount));
  439. const auto early = (float) s.earlyLevel, late = (float) s.lateLevel, send = (float) s.earlySend;
  440. const auto gated = s.gateSec > 0.0, build = gated || s.buildSec > 0.0005;
  441. const auto parallel = s.parallelLines;
  442. const auto crossAngle = parallel ? 0.0 : 0.25 * pi * std::clamp (s.crossFeed, 0.0, 1.0);
  443. const auto crossCos = (float) std::cos (crossAngle), crossSin = (float) std::sin (crossAngle);
  444. const auto ratio = std::pow (2.0, s.shiftSemis / 12.0), ratio2 = std::pow (2.0, s.shiftSemis2 / 12.0);
  445. const auto voice2 = (float) std::clamp (s.shift2Mix, 0.0, 1.0);
  446. // shimmer: a part of what four of the lines write back goes through a pitch shifter. (1 − a)² + a² ≤ 1 and
  447. // the shifted part is decorrelated, so the loop keeps the decay the absorption filters set: always stable
  448. const auto shimmer = (float) (std::clamp (s.shiftFeedback, 0.0, 1.0) * std::clamp (s.shiftMix, 0.0, 1.0));
  449. const auto width = (float) std::clamp (s.width, -2.0, 2.0), gain = (float) s.gain;
  450. double target[lineCount];
  451. for (int i = 0; i < lineCount; ++i)
  452. target[i] = targetLen (s, i);
  453. for (int t = 0; t < n; ++t)
  454. {
  455. float x[2] = { L[t], R[t] };
  456. float e[2], d[2];
  457. for (int c = 0; c < 2; ++c)
  458. {
  459. pre[c].write (x[c]);
  460. const auto p = pd == 0 ? x[c] : pre[c].read (pd);
  461. raw[c].write (p);
  462. // early reflections
  463. float er = 0.0f;
  464. if (early != 0.0f || send != 0.0f)
  465. for (int k = 0; k < earlyTaps; ++k)
  466. er += eW[c][k] * raw[c].read (eD[c][k]);
  467. if (eApG != 0.0f)
  468. for (auto& a : earlyAp[c])
  469. er = a.process (er, eApG);
  470. e[c] = er;
  471. // input diffusion
  472. auto v = p;
  473. for (int k = 0; k < 4; ++k)
  474. if (inG[k] != 0.0f)
  475. v = inAp[c][k].process (v, inG[k]);
  476. if (build)
  477. {
  478. dif[c].write (v);
  479. float b = 0.0f;
  480. for (int k = 0; k < buildTaps; ++k)
  481. b += bW[c][k] * (bD[c][k] == 0 ? v : dif[c].read (bD[c][k]));
  482. v = b;
  483. }
  484. d[c] = v;
  485. }
  486. // late tank
  487. float sOut[lineCount];
  488. for (int i = 0; i < lineCount; ++i)
  489. {
  490. curLen[i] += std::clamp (target[i] - curLen[i], -glide, glide);
  491. lfo[i] += modInc[i];
  492. if (lfo[i] > 2.0 * pi)
  493. lfo[i] -= 2.0 * pi;
  494. const auto dl = std::max (2.0, curLen[i] - modDepth * 0.5 * (1.0 + std::sin (lfo[i])));
  495. auto y = lines[i].readFrac (dl);
  496. if (apG[i] != 0.0f)
  497. y = loopAp[i].process (y, apG[i]);
  498. auto& a = absorb[i];
  499. const auto lo = a.lo.lp (y), hiLp = a.hi.lp (y);
  500. y = a.gM * y + (a.gL - a.gM) * lo + (a.gH - a.gM) * (y - hiLp);
  501. if (a.bw.a != 0.0f)
  502. y = a.bw.lp (y);
  503. if (a.lc.a != 1.0f)
  504. y -= a.lc.lp (y);
  505. sOut[i] = y;
  506. }
  507. // outputs: two orthogonal sign patterns
  508. // (neither pattern sums to zero, so low frequencies, where the lines move together, are kept)
  509. float lateL = (sOut[0] + sOut[1] + sOut[2] - sOut[3] + sOut[4] + sOut[5] - sOut[6] + sOut[7]) * norm;
  510. float lateR = (sOut[0] - sOut[1] + sOut[2] + sOut[3] + sOut[4] - sOut[5] + sOut[6] + sOut[7]) * norm;
  511. if (s.splitSides) // two separate halves (round 135): each side hears only its own four lines
  512. {
  513. lateL = (sOut[0] + sOut[1] + sOut[2] - sOut[3]) * norm * 1.41421356f;
  514. lateR = (sOut[4] - sOut[5] + sOut[6] + sOut[7]) * norm * 1.41421356f;
  515. }
  516. const float in[2] = { d[0] + send * e[0], d[1] + send * e[1] };
  517. if (gated)
  518. {
  519. // gated: the dense tap block is the sound; the short tank only smooths it
  520. lateL = 0.3f * lateL + d[0];
  521. lateR = 0.3f * lateR + d[1];
  522. }
  523. // mixing: each half of the tank (lines 0–3 fed from the left, 4–7 from the right) through a normalised 4-point
  524. // Hadamard, then the halves rotated into each other by crossFeed × 45° (orthogonal for any amount)
  525. float h[lineCount];
  526. if (parallel)
  527. for (int i = 0; i < lineCount; ++i)
  528. h[i] = sOut[i];
  529. for (int half = 0; half < lineCount && ! parallel; half += 4)
  530. {
  531. const auto a0 = sOut[half] + sOut[half + 1], a1 = sOut[half] - sOut[half + 1];
  532. const auto a2 = sOut[half + 2] + sOut[half + 3], a3 = sOut[half + 2] - sOut[half + 3];
  533. h[half] = 0.5f * (a0 + a2);
  534. h[half + 1] = 0.5f * (a1 + a3);
  535. h[half + 2] = 0.5f * (a0 - a2);
  536. h[half + 3] = 0.5f * (a1 - a3);
  537. }
  538. for (int i = 0; i < 4; ++i)
  539. {
  540. const auto a = h[i], b = h[i + 4];
  541. // parallel lines (plain echoes): input signs = the output patterns of their own side, so the left input
  542. // reaches only the left output and the right only the right
  543. constexpr float injL[4] = { 0.5f, 0.5f, 0.5f, -0.5f }, injR[4] = { 0.5f, -0.5f, 0.5f, 0.5f };
  544. auto u = crossCos * a + crossSin * b, w = -crossSin * a + crossCos * b;
  545. if (shimmer > 0.0f && (i & 1) == 0)
  546. {
  547. // the second voice (dual modes) takes line 2 and line 6
  548. const auto r = i == 2 && voice2 > 0.0f ? ratio2 : ratio;
  549. const auto amount = i == 2 && voice2 > 0.0f ? shimmer * voice2 : shimmer;
  550. u = (1.0f - amount) * u + amount * shifters[i / 2].process (u, r);
  551. w = (1.0f - amount) * w + amount * shifters[2 + i / 2].process (w, i == 2 && voice2 > 0.0f ? ratio2 : ratio);
  552. }
  553. const auto alt = (i & 1) == 0 ? 0.5f : -0.5f; // reverbs: decorrelated sides from the first pass on
  554. lines[i].write (u + (parallel ? injL[i] : alt) * in[0]);
  555. lines[i + 4].write (w + (parallel ? injR[i] : alt) * in[1]);
  556. }
  557. auto ol = late * lateL + early * e[0];
  558. auto orr = late * lateR + early * e[1];
  559. // width (M/S)
  560. const auto mid = 0.5f * (ol + orr), side = 0.5f * (ol - orr) * width;
  561. L[t] = (mid + side) * gain;
  562. R[t] = (mid - side) * gain;
  563. }
  564. if (outLowOn)
  565. outLow.processBlock (L, R, n);
  566. if (outHighOn)
  567. outHigh.processBlock (L, R, n);
  568. if (bandOn)
  569. for (int k = 0; k < bandSections; ++k)
  570. band[k].processBlock (L, R, n);
  571. if (toneHighOn)
  572. toneHigh.processBlock (L, R, n);
  573. if (toneMidOn)
  574. toneMid.processBlock (L, R, n);
  575. if (shelfLowOn)
  576. shelfLow.processBlock (L, R, n);
  577. if (shelfHighOn)
  578. shelfHigh.processBlock (L, R, n);
  579. last = s;
  580. }
  581. };
  582. Engine::Engine (double maxLineSec, double maxPredelaySec) : impl (std::make_unique<Impl> (maxLineSec, maxPredelaySec)) {}
  583. Engine::~Engine() = default;
  584. void Engine::prepare (double sampleRate) { impl->prepare (sampleRate); }
  585. void Engine::reset() noexcept { impl->reset(); }
  586. void Engine::process (float* left, float* right, int n, const Settings& s) noexcept { impl->process (left, right, n, s); }
  587. // =================================================================================================================
  588. // front-ends
  589. namespace
  590. {
  591. // a 0..1 control as % text
  592. std::string pct (double v) { char b[32]; std::snprintf (b, sizeof b, "%.0f%%", v * 100.0); return b; }
  593. std::string fmt (const char* f, double v) { char b[48]; std::snprintf (b, sizeof b, f, v); return b; }
  594. std::string hz (double v) { return v >= 1000.0 ? fmt ("%.2f kHz", v / 1000.0) : fmt ("%.0f Hz", v); }
  595. std::string ms (double v) { return v >= 1000.0 ? fmt ("%.2f s", v / 1000.0) : fmt ("%.1f ms", v); }
  596. std::string sec (double v) { return v >= 10.0 ? fmt ("%.1f s", v) : fmt ("%.2f s", v); }
  597. // the decay a front-end asks for, with a floor for modes whose structure cannot decay faster (measured)
  598. double withFloor (double rt, double floor) { return std::sqrt (rt * rt + floor * floor); }
  599. // note lengths of the tempo-synced delays (quarter notes)
  600. constexpr double noteQuarters[] = { 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0 / 2, 1.0, 2.0, 4.0 };
  601. const char* const noteNames[] = { "1/64", "1/32", "1/16", "1/8", "1/4", "1/2", "1/1" };
  602. // Delay time of a synced control set: sync 0 = ms, 1 = note, 2 = dotted, 3 = triplet
  603. double syncedMs (int sync, int note, double msValue, double bpm)
  604. {
  605. if (sync <= 0 || bpm <= 0.0)
  606. return msValue;
  607. auto q = noteQuarters[std::clamp (note, 0, 6)] * 60000.0 / bpm;
  608. if (sync == 2)
  609. q *= 1.5;
  610. else if (sync == 3)
  611. q *= 2.0 / 3.0;
  612. return q;
  613. }
  614. class VerbEffect : public Effect
  615. {
  616. public:
  617. VerbEffect (EffectKind k, double maxLineSec, double maxPreSec) : kind (k), engine (maxLineSec, maxPreSec) {}
  618. void prepare (double sampleRate) noexcept override
  619. {
  620. rate = sampleRate;
  621. engine.prepare (sampleRate);
  622. }
  623. void reset() noexcept override { engine.reset(); }
  624. void process (float* left, float* right, int n, const EffectParams& p, double bpm) noexcept override
  625. {
  626. Settings s;
  627. float mix = 1.0f;
  628. configure (p, bpm, s, mix);
  629. float dry, wet;
  630. mixGains (mix, dry, wet);
  631. for (int start = 0; start < n; start += chunk)
  632. {
  633. const auto m = std::min (chunk, n - start);
  634. auto* l = left + start;
  635. auto* r = right + start;
  636. std::copy (l, l + m, dl.begin());
  637. std::copy (r, r + m, dr.begin());
  638. preStage (l, r, m, p, bpm);
  639. engine.process (l, r, m, s);
  640. postStage (l, r, m, p, bpm);
  641. for (int i = 0; i < m; ++i)
  642. {
  643. l[i] = dry * dl[(std::size_t) i] + wet * l[i];
  644. r[i] = dry * dr[(std::size_t) i] + wet * r[i];
  645. }
  646. }
  647. }
  648. protected:
  649. virtual void configure (const EffectParams& p, double bpm, Settings& s, float& mix) const noexcept = 0;
  650. virtual void preStage (float*, float*, int, const EffectParams&, double) noexcept {}
  651. virtual void postStage (float*, float*, int, const EffectParams&, double) noexcept {}
  652. static constexpr int chunk = 1024;
  653. EffectKind kind;
  654. Engine engine;
  655. double rate = 48000.0;
  656. std::array<float, chunk> dl {}, dr {};
  657. };
  658. // ---------------------------------------------------------------------------------------------- Epoch Verb
  659. // Laws [EXEC, REA_rodada100 §1]: predelay 0..500 ms (v^2.32), decay 0.2..70 s (v^3.36), bass mult 0.25..4 X (v^2.32),
  660. // bass crossover 100..10000 Hz (v^4.04), high shelf −24..0 dB, high freq / high cut 100..20000 Hz (v^1.754),
  661. // mod rate 0.1..10 Hz, low cut 10..1500 Hz.
  662. struct EpochMode
  663. {
  664. const char* name;
  665. int shape;
  666. double scale, rtScale, rtFloor, lowRt, inDiff, early, earlyMs, buildMs, modScale, gain, preMs, gate;
  667. };
  668. // shape, line scale, RT / Decay, RT floor (s), bass RT ×, input diffusion, early level / span (ms), build-up (ms),
  669. // modulation scale, output gain, own pre-delay (ms), gate (fraction of the decay; 0 = none) — fitted to §1.
  670. // (LYNX_VERB_FIT: the fitting harness builds this file with the tables writable)
  671. #ifdef LYNX_VERB_FIT
  672. #define LYNX_FIT_TABLE
  673. #else
  674. #define LYNX_FIT_TABLE const
  675. #endif
  676. LYNX_FIT_TABLE EpochMode epochModes[epoch::modeCount] = {
  677. { "Concert Hall", 0, 0.9394, 1, 1.2, 1, 0.975, 0.4, 42.94, 0.0, 1, 1.031, 3.2, 0.0 },
  678. { "Plate", 2, 0.80, 0.90, 0.45, 1.0, 1.0, 0.10, 2.0, 60.0, 1.0, 0.70, 2.0, 0.0 },
  679. { "Room", 1, 0.85, 1.02, 0.45, 1.0, 1.0, 0.10, 20.0, 150.0, 1.0, 0.82, 1.0, 0.0 },
  680. { "Chamber", 1, 1.00, 1.04, 0.45, 1.0, 1.0, 0.15, 30.0, 140.0, 1.0, 0.70, 1.2, 0.0 },
  681. { "Random Space", 0, 1.10, 1.00, 0.90, 1.0, 1.0, 0.00, 1.0, 220.0, 2.0, 1.50, 15.0, 0.0 },
  682. { "Chorus Space", 0, 1.10, 1.00, 1.10, 1.0, 1.0, 0.05, 40.0, 280.0, 3.0, 0.70, 9.0, 0.0 },
  683. { "Ambience", 1, 0.70, 1.00, 0.45, 1.0, 1.0, 0.15, 40.0, 25.0, 1.0, 0.80, 7.5, 0.0 },
  684. { "Bright Hall", 0, 1.05, 1.15, 1.20, 1.0, 1.0, 0.50, 9.0, 0.0, 1.0, 1.00, 5.5, 0.0 },
  685. { "Nave", 0, 1.20, 1.00, 0.80, 1.0, 1.0, 0.35, 14.0, 20.0, 1.0, 0.62, 0.5, 0.0 },
  686. { "Grit Hall", 0, 1.00, 1.10, 1.20, 1.0, 1.0, 0.55, 9.0, 0.0, 1.3, 0.83, 5.0, 0.0 },
  687. { "Grit Plate", 2, 0.75, 0.92, 0.45, 1.0, 1.0, 0.10, 6.0, 40.0, 1.3, 1.25, 1.0, 0.0 },
  688. { "Smooth Plate", 2, 0.85, 1.12, 0.60, 1.0, 1.0, 0.40, 6.0, 0.0, 0.8, 0.46, 4.5, 0.0 },
  689. { "Smooth Room", 1, 0.95, 0.97, 0.70, 1.0, 1.0, 0.30, 8.0, 6.0, 0.8, 0.53, 9.0, 0.0 },
  690. { "Smooth Space", 0, 1.00, 1.04, 0.80, 1.0, 1.0, 0.25, 8.0, 6.0, 0.8, 0.81, 7.5, 0.0 },
  691. { "Gated", 1, 0.60, 0.62, 0.30, 1.0, 1.0, 0.10, 4.0, 0.0, 0.5, 0.90, 1.5, 0.55 },
  692. { "Drift Chamber", 1, 1.00, 1.08, 0.70, 1.0, 1.0, 0.40, 6.0, 0.0, 2.5, 0.43, 4.5, 0.0 },
  693. { "Drift Hall", 0, 1.00, 1.13, 1.20, 1.0, 1.0, 0.45, 9.0, 0.0, 2.5, 0.53, 5.0, 0.0 },
  694. { "Drift Space", 0, 1.30, 1.70, 1.20, 1.0, 1.0, 0.05, 9.0, 110.0, 2.5, 1.00, 4.0, 0.0 },
  695. { "Cathedral", 0, 1.49, 1, 0.8762, 1.203, 1, 0.4, 84.68, 39.77, 1, 0.756, 0.0, 0.0 },
  696. { "Grand Hall", 0, 1.50, 1.30, 2.40, 1.7, 1.0, 0.20, 30.0, 20.0, 1.0, 1.30, 55.0, 0.0 },
  697. { "Vintage Chamber", 1, 1.10, 1.20, 0.90, 1.0, 1.0, 0.05, 25.0, 110.0, 0.7, 1.40, 11.0, 0.0 },
  698. { "Vintage Hall", 0, 1.00, 1.08, 1.20, 1.0, 1.0, 0.50, 9.0, 0.0, 0.7, 0.95, 5.0, 0.0 },
  699. };
  700. const char* const epochColours[epoch::colourCount] = { "1970s", "1980s", "Now" };
  701. // colour: loop bandwidth (Hz, 0 = none), output high cut (Hz, first order, 0 = none), steep band limit (Hz, 0 = none) and
  702. // its Butterworth sections
  703. struct EpochColour
  704. {
  705. double loopBw, outCut, bandLimit;
  706. int sections;
  707. };
  708. LYNX_FIT_TABLE EpochColour epochColours2[3] = { { 9000.0, 0.0, 11800.0, 16 }, { 14000.0, 16000.0, 0.0, 2 }, { 0.0, 0.0, 0.0, 2 } };
  709. // Round 124 (REA_rodada124_extras §3): each algorithm's own voicing, fitted to the reference at her saved settings
  710. // (time–frequency energy of the impulse response and RT60 per octave). Neutral for the modes not refitted.
  711. struct EpochVoicing
  712. {
  713. double bassExp = 0.35; // bass multiplier law below 1 × (RT × mult^bassExp)
  714. double highMult = 1.0; // × the high-band RT
  715. double loopBwScale = 1.0; // × the colour's loop bandwidth
  716. double earlyDecay = 3.0; // early taps' fall over their span (e-folds)
  717. double toneLowHz = 200.0, toneLowDb = 0.0;
  718. double toneMidHz = 1000.0, toneMidDb = 0.0, toneMidQ = 0.7;
  719. double toneHighHz = 8000.0, toneHighDb = 0.0;
  720. // the mid-band RT60 (s) the reference measures at Size 100 % for each Decay (s): (decay, rt) pairs, increasing;
  721. // empty = the round-100 law (Decay × RT / Decay with the floor)
  722. int rtKnotCount = 0;
  723. double rtKnots[2 * 16] {};
  724. };
  725. LYNX_FIT_TABLE EpochVoicing epochVoicing[epoch::modeCount] = {
  726. /* Concert Hall */ { 0.0, 1.88, 1.455, 0.5, 155.8, -0.6, 1455, -0.6, 2.45, 4852, 1.5, 8, { 0.5, 1.38, 1, 1.6, 1.78, 2.27, 2, 2.46, 2.67, 3.17, 3.28, 3.7, 4, 4.63, 8, 8.68 } },
  727. {},
  728. {},
  729. {},
  730. {},
  731. {},
  732. {},
  733. {},
  734. {},
  735. {},
  736. {},
  737. {},
  738. {},
  739. {},
  740. {},
  741. {},
  742. {},
  743. {},
  744. /* Cathedral */ { 0.875, 0.76, 1.868, 0.0, 61.95, -1.8, 200, -0.6, 0.2, 1.164e+04, -3, 13, { 0.53, 1.15, 1, 2.2, 1.85, 2.54, 1.99, 2.72, 2.5, 3.28, 2.9, 3.7, 3.87, 4.62, 4.36, 5.07, 5.15, 5.76, 6.38, 6.85, 7.37, 7.73, 12, 11.86, 14, 13.68 } },
  745. {},
  746. {},
  747. {},
  748. };
  749. double epochDecay (float v) { return skewLaw (v, 0.2, 70.0, 3.3596); }
  750. double epochPredelay (float v) { return skewLaw (v, 0.0, 500.0, 2.3219); }
  751. double epochBassMult (float v) { return skewLaw (v, 0.25, 4.0, 2.3219); }
  752. double epochBassXover (float v) { return skewLaw (v, 100.0, 10000.0, 4.0444); }
  753. double epochFreq (float v) { return skewLaw (v, 100.0, 20000.0, 1.7540); }
  754. class EpochFx final : public VerbEffect
  755. {
  756. public:
  757. EpochFx() : VerbEffect (EffectKind::epochVerb, 0.25, 0.62) {}
  758. protected:
  759. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  760. {
  761. const auto modeIndex = choice (p[16], epoch::modeCount);
  762. const auto& m = epochModes[modeIndex];
  763. const auto& vo = epochVoicing[modeIndex];
  764. const auto colour = choice (p[15], epoch::colourCount);
  765. const auto& co = epochColours2[colour];
  766. mix = p[0];
  767. const auto size = (double) p[3];
  768. // a very slow attack also delays the onset (the reference: +45 ms at 100 %)
  769. s.predelaySec = (epochPredelay (p[1]) + m.preMs * (0.4 + 0.6 * size) + 225.0 * std::max (0.0, p[4] - 0.8)) / 1000.0;
  770. const auto decay = epochDecay (p[2]);
  771. s.shape = m.shape;
  772. s.lineScale = m.scale * (0.3 + 0.7 * size);
  773. if (vo.rtKnotCount >= 2)
  774. {
  775. // measured RT per Decay, scaled like the law below for Size and late diffusion
  776. const auto rt = knotLaw (std::clamp (decay, vo.rtKnots[0], vo.rtKnots[2 * (vo.rtKnotCount - 1)]), vo.rtKnots, vo.rtKnotCount);
  777. const auto beyond = decay > vo.rtKnots[2 * (vo.rtKnotCount - 1)] ? decay / vo.rtKnots[2 * (vo.rtKnotCount - 1)] : 1.0;
  778. s.rt60 = rt * beyond * m.rtScale * (0.85 + 0.15 * size) * (0.84 + 0.16 * p[10]);
  779. }
  780. else
  781. s.rt60 = withFloor (decay * m.rtScale, m.rtFloor * (0.4 + 0.6 * size)) * (0.84 + 0.16 * p[10]);
  782. const auto bass = epochBassMult (p[5]);
  783. s.lowMult = (bass < 1.0 ? std::pow (bass, vo.bassExp) : bass) * m.lowRt;
  784. s.lowXover = epochBassXover (p[6]);
  785. const auto shelf = -24.0 + 24.0 * p[7];
  786. s.highXover = epochFreq (p[8]);
  787. s.highMult = 1.1 * std::pow (10.0, std::max (shelf, -14.0) / 50.0) * vo.highMult;
  788. s.highShelfHz = s.highXover;
  789. s.highShelfDb = std::max (shelf, -16.0);
  790. s.inDiffusion = m.inDiff * p[9];
  791. s.loopDiffusion = 0.2 + 0.8 * p[10];
  792. s.modRateHz = 0.1 + 9.9 * p[11];
  793. s.modDepthMs = 0.6 * m.modScale * p[12] / std::sqrt (std::max (0.3, s.modRateHz));
  794. s.outHighCut = epochFreq (p[13]);
  795. s.outHighOrder = 1;
  796. if (co.outCut > 0.0)
  797. s.outHighCut = std::min (s.outHighCut, co.outCut);
  798. s.loopBandwidth = co.loopBw * vo.loopBwScale;
  799. s.bandLimit = co.bandLimit;
  800. s.bandLimitSections = co.sections;
  801. s.earlyDecay = vo.earlyDecay;
  802. s.lowShelfHz = vo.toneLowHz;
  803. s.lowShelfDb = vo.toneLowDb;
  804. s.toneMidHz = vo.toneMidHz;
  805. s.toneMidDb = vo.toneMidDb;
  806. s.toneMidQ = vo.toneMidQ;
  807. s.toneHighHz = vo.toneHighHz;
  808. s.toneHighDb = vo.toneHighDb;
  809. s.outLowCut = 10.0 + 1490.0 * p[14];
  810. s.outLowOrder = 1;
  811. s.earlyLevel = m.early;
  812. s.earlySec = m.earlyMs / 1000.0 * (0.4 + 0.6 * size);
  813. s.buildSec = m.buildMs / 1000.0 * (0.25 + 1.5 * p[4]);
  814. s.gateSec = m.gate > 0.0 ? m.gate * decay + 0.05 : 0.0;
  815. s.gain = m.gain;
  816. }
  817. };
  818. // ---------------------------------------------------------------------------------------------- Atrium
  819. // Laws: like the reference room (linear: predelay 0..500 ms, high cut 100..15000 Hz, sizes / crosses 0..1, mod rates
  820. // 0.05..5 Hz, bass mult 0.5..2 X, bass xover 100..10000 Hz, high mult 0.1..1 X, high xover 100..15000 Hz, early size
  821. // 1..1000 ms, low cut 0..1000 Hz) except Decay: 0.1..100 s with 2 s at the middle (the reference is linear).
  822. struct AtriumType
  823. {
  824. const char* name;
  825. int shape;
  826. double scale, rtScale, inDiff, early, lateGain, cut, modScale;
  827. };
  828. const AtriumType atriumTypes[atrium::typeCount] = {
  829. { "Large Room", 1, 1.00, 1.00, 1.0, 1.00, 1.00, 0.0, 1.0 },
  830. { "Medium Room", 1, 0.80, 1.00, 1.0, 1.00, 1.00, 0.0, 1.0 },
  831. { "Bright Room", 1, 0.90, 0.88, 0.9, 0.80, 0.90, 0.0, 1.0 },
  832. { "Large Chamber", 1, 1.20, 1.00, 1.0, 0.85, 1.05, 0.0, 1.0 },
  833. { "Dark Room", 1, 1.00, 1.00, 1.0, 1.00, 1.00, 0.0, 1.0 },
  834. { "Dark Chamber", 1, 1.15, 1.00, 1.0, 0.90, 1.00, 11000.0, 1.0 },
  835. { "Dark Space", 0, 1.10, 1.00, 1.0, 0.90, 1.00, 10000.0, 1.0 },
  836. { "Deep Space", 0, 1.20, 1.00, 1.0, 0.85, 1.00, 10500.0, 1.2 },
  837. { "Mirror Space", 0, 0.90, 1.00, 0.7, 1.10, 1.00, 10500.0, 1.0 },
  838. { "Vast Space", 0, 1.30, 1.00, 0.8, 1.00, 1.00, 10000.0, 1.2 },
  839. { "Distant Space", 0, 1.25, 1.00, 1.0, 0.70, 1.10, 10500.0, 1.0 },
  840. { "Dense Room", 1, 1.00, 1.00, 1.0, 1.05, 1.00, 10500.0, 1.0 },
  841. };
  842. double atriumDecaySec (float v) { return skewLaw (v, 0.1, 100.0, 5.717); }
  843. class AtriumFx final : public VerbEffect
  844. {
  845. public:
  846. AtriumFx() : VerbEffect (EffectKind::atrium, 0.25, 1.05) {}
  847. protected:
  848. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  849. {
  850. const auto& t = atriumTypes[choice (p[19], atrium::typeCount)];
  851. mix = p[0];
  852. s.predelaySec = 0.5 * p[1];
  853. s.rt60 = atriumDecaySec (p[2]) * t.rtScale;
  854. s.outHighCut = 100.0 + 14900.0 * p[3];
  855. s.bandLimit = t.cut;
  856. const auto earlyLate = (double) p[4]; // 0 = early only, 1 = late only
  857. s.earlyLevel = t.early * std::min (1.0, 2.0 * (1.0 - earlyLate)) * 1.4;
  858. s.lateLevel = t.lateGain * std::min (1.0, 2.0 * earlyLate);
  859. s.shape = t.shape;
  860. s.lineScale = t.scale * (0.35 + 0.9 * p[5]);
  861. s.crossFeed = 0.35 + 0.65 * p[6];
  862. s.modRateHz = 0.05 + 4.95 * p[7];
  863. s.modDepthMs = 0.8 * t.modScale * p[8] / std::sqrt (std::max (0.3, s.modRateHz));
  864. s.lowMult = 0.5 + 1.5 * p[9];
  865. s.lowXover = 100.0 + 9900.0 * p[10];
  866. s.highMult = 1.15 * (0.1 + 0.9 * p[11]); // (+15 %: what interpolation and diffusion take away)
  867. s.highXover = 100.0 + 14900.0 * p[12];
  868. s.earlySec = (1.0 + 999.0 * p[13]) / 1000.0;
  869. s.earlyDecay = 2.0 + 3.0 * p[14];
  870. s.earlyDiffusion = 0.3 + 0.7 * p[14];
  871. s.earlySend = p[17];
  872. s.inDiffusion = t.inDiff * p[18];
  873. s.loopDiffusion = 0.3 + 0.6 * p[18];
  874. const auto space = (double) p[20];
  875. s.buildSec = 0.02 + 0.06 * space;
  876. s.gain = 0.47 * (1.0 + 0.6 * space);
  877. s.outLowCut = 1000.0 * p[21];
  878. s.outLowOrder = 1;
  879. if (s.outLowCut < 5.0)
  880. s.outLowCut = 0.0;
  881. }
  882. };
  883. // ---------------------------------------------------------------------------------------------- Foil Plate
  884. // Laws: predelay 0..500 ms (v^2.32), decay 0.5..30 s (measured knots), size / width 0..200 %, mod rate 0.05..5 Hz
  885. // (v^2.38), low EQ 20..2000 Hz, high EQ 200..20000 Hz, EQ gains ±12 dB.
  886. constexpr double foilDecayKnots[] = { 0.0, 0.5, 0.5, 3.0, 0.75, 5.9, 0.775, 6.7, 0.8, 8.0, 0.825, 9.4, 0.85, 11.3, 0.875, 13.1,
  887. 0.9, 15.7, 0.925, 18.4, 0.95, 21.7, 0.975, 25.8, 1.0, 30.0 };
  888. constexpr int foilDecayCount = (int) (sizeof (foilDecayKnots) / sizeof (double) / 2);
  889. struct FoilType
  890. {
  891. const char* name;
  892. double scale, preMs, lowRt, damp, bw, early, gain;
  893. };
  894. // plate types: line scale, own onset (ms), bass RT ×, high-frequency absorption (1/s at 8 kHz), band limit (Hz, 0 = full band),
  895. // early level, gain
  896. const FoilType foilTypes[foil::typeCount] = {
  897. { "Silver", 1.00, 20.5, 1.0, 0.34, 9000.0, 0.35, 1.00 },
  898. { "Pewter", 0.85, 1.0, 1.0, 0.30, 9000.0, 0.45, 1.13 },
  899. { "Nickel", 1.10, 56.0, 1.0, 0.25, 10000.0, 0.35, 0.83 },
  900. { "Bronze", 0.90, 17.0, 1.0, 0.30, 9000.0, 0.55, 1.05 },
  901. { "Bismuth", 1.20, 6.5, 1.0, 0.20, 8500.0, 0.30, 1.00 },
  902. { "Tin", 1.00, 18.5, 1.0, 0.32, 9000.0, 0.35, 1.13 },
  903. { "Zinc", 1.30, 6.5, 1.1, 0.18, 9500.0, 0.30, 1.16 },
  904. { "Platinum", 1.20, 81.0, 1.3, 0.35, 8500.0, 0.25, 0.62 },
  905. { "Tungsten", 1.00, 21.0, 1.0, 0.30, 0.0, 0.35, 0.68 },
  906. { "Iron", 1.10, 56.0, 1.1, 0.30, 0.0, 0.30, 0.60 },
  907. { "Gold", 1.00, 20.5, 1.0, 0.30, 0.0, 0.35, 0.68 },
  908. { "Lead", 1.00, 1.5, 1.0, 0.45, 8500.0, 0.40, 1.00 },
  909. };
  910. double foilDecaySec (float v) { return knotLaw (v, foilDecayKnots, foilDecayCount); }
  911. class FoilFx final : public VerbEffect
  912. {
  913. public:
  914. FoilFx() : VerbEffect (EffectKind::foilPlate, 0.2, 0.62) {}
  915. protected:
  916. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  917. {
  918. const auto& t = foilTypes[choice (p[11], foil::typeCount)];
  919. mix = p[0];
  920. const auto size = 2.0 * p[3];
  921. s.predelaySec = (epochPredelay (p[1]) + t.preMs * (0.55 + 0.45 * size)) / 1000.0;
  922. s.shape = 2;
  923. s.lineScale = t.scale * (0.5 + 0.5 * size);
  924. const auto rt = foilDecaySec (p[2]);
  925. // round 135 (the Little Plate role): Infinite holds the plate (the decay knob's ∞), damped as at 30 s
  926. const auto infinite = p[12] >= 0.5f;
  927. s.rt60 = infinite ? 1000.0 : rt;
  928. // plates lose highs faster the longer they ring: 1/RT(8 kHz) = 1/RT + damp (measured, §3)
  929. s.highMult = 1.0 / (1.0 + t.damp * (infinite ? 30.0 : rt));
  930. s.highXover = 5000.0;
  931. s.lowMult = t.lowRt;
  932. s.lowXover = 250.0;
  933. s.bandLimit = t.bw;
  934. s.inDiffusion = 1.0;
  935. s.loopDiffusion = 0.85;
  936. // 0 % = mono, 100 % = decorrelated, 200 % = slightly anti-correlated (−0.21, measured)
  937. s.width = p[4] <= 0.5f ? 2.0 * p[4] : 1.0 + 0.48 * (p[4] - 0.5);
  938. s.modRateHz = skewLaw (p[5], 0.05, 5.0, 2.381);
  939. s.modDepthMs = 1.0 * p[6] / std::sqrt (std::max (0.3, s.modRateHz));
  940. s.lowShelfHz = 20.0 + 1980.0 * p[7];
  941. s.lowShelfDb = 0.75 * (-12.0 + 24.0 * p[8]);
  942. s.highShelfHz = 200.0 + 19800.0 * p[9];
  943. s.highShelfDb = -12.0 + 24.0 * p[10];
  944. // the plate speaks at once after its own delay: a short, smeared early burst
  945. s.earlyLevel = t.early;
  946. s.earlySec = 0.015;
  947. s.earlyDiffusion = 0.8;
  948. s.gain = t.gain;
  949. // round 135: a low cut on the plate's output, 20 Hz … 1 kHz (0 = off; 12 dB/oct)
  950. s.outLowCut = foil::lowCutHz (p[13]);
  951. s.outLowOrder = 2;
  952. }
  953. };
  954. // ---------------------------------------------------------------------------------------------- Nebula
  955. // Laws: delay 0..2000 ms (measured knots), feedback / density / warp 0..100 %, width −100..100 %, low cut 10..2000 Hz,
  956. // high cut 200..20000 Hz, mod rate 0.01..10 Hz (v^2.33). Feedback F gives a gain of √F per pass of the delay.
  957. constexpr double nebulaDelayKnots[] = { 0.0, 0.0, 0.5, 300.0, 0.75, 588.5, 0.775, 675.0, 0.925, 1425.0, 0.95, 1583.3, 0.975, 1791.7, 1.0, 2000.0 };
  958. constexpr int nebulaDelayCount = (int) (sizeof (nebulaDelayKnots) / sizeof (double) / 2);
  959. struct NebulaMode
  960. {
  961. const char* name;
  962. double dm0, dm100, mult0, mult100, corr;
  963. };
  964. // decay multiplier at density 0 / 100, line length × delay at density 0 / 100, stereo correlation (measured, §4)
  965. LYNX_FIT_TABLE NebulaMode nebulaModes[nebula::modeCount] = {
  966. { "Ripple", 1.03, 1.26, 1, 1, 0.93 }, { "Swell", 3.43, 3.82, 1, 2, -0.31 },
  967. { "Fold", 1.33, 1.39, 1, 2, -0.25 }, { "Drift", 2.56, 2.67, 1, 1, 0.25 },
  968. { "Abyss", 3.64, 3.97, 1, 2, -0.21 }, { "Vortex", 7.04, 6.77, 2, 2, 0.29 },
  969. { "Glint", 1.03, 1.17, 1, 1, 0.77 }, { "Pulse", 1.03, 1.22, 1, 1, 0.87 },
  970. { "Expanse", 4.29, 12.8, 8, 1, 0.39 }, { "Haze", 2.14, 12.3, 4, 1, 0.63 },
  971. { "Veil", 2.02, 5.04, 2, 1, 0.72 }, { "Mist", 1.00, 1.01, 1, 1, 0.47 },
  972. { "Monolith", 2.16, 16.3, 4, 1, 1.0 }, { "Eternity", 20.0, 20.7, 8, 1, -0.30 },
  973. { "Tide", 1.00, 1.12, 1, 1, 0.54 }, { "Shoal", 1.03, 1.30, 1, 1, 0.37 },
  974. { "Spark", 1.00, 1.05, 1, 1, 0.95 }, { "Plateau", 2.10, 9.65, 4, 1, 0.0 },
  975. { "Dusk", 4.15, 12.65, 8, 1, 0.31 }, { "Mirage", 1.00, 1.35, 1, 2, -0.43 },
  976. { "Cluster", 1.02, 1.01, 1, 1, -0.10 }, { "Beacon", 1.16, 1.04, 1, 1, 0.84 },
  977. };
  978. double nebulaDelayMs (float v) { return knotLaw (v, nebulaDelayKnots, nebulaDelayCount); }
  979. const char* const syncNames[4] = { "ms", "Note", "Dotted", "Triplet" };
  980. class NebulaFx final : public VerbEffect
  981. {
  982. public:
  983. NebulaFx() : VerbEffect (EffectKind::nebula, 4.2, 0.05) {}
  984. // CLEAR (round 129): the panel's push button flips parameter 13; a flip empties the delay network at once
  985. void process (float* left, float* right, int n, const EffectParams& p, double bpm) noexcept override
  986. {
  987. if (p[13] != lastClear)
  988. {
  989. if (lastClear >= 0.0f)
  990. reset();
  991. lastClear = p[13];
  992. }
  993. VerbEffect::process (left, right, n, p, bpm);
  994. }
  995. private:
  996. float lastClear = -1.0f;
  997. protected:
  998. protected:
  999. void configure (const EffectParams& p, double bpm, Settings& s, float& mix) const noexcept override
  1000. {
  1001. const auto& m = nebulaModes[choice (p[12], nebula::modeCount)];
  1002. mix = p[0];
  1003. const auto delay = std::max (1.0, syncedMs (choice (p[1], 4), choice (p[2], 7), nebulaDelayMs (p[3]), bpm));
  1004. const auto warp = (double) p[4];
  1005. const auto fb = (double) p[5];
  1006. const auto dens = (double) p[6];
  1007. const auto lerpLog = [&] (double a, double b) { return std::exp (std::log (a) + (std::log (b) - std::log (a)) * dens); };
  1008. const auto mult = lerpLog (m.mult0, m.mult100);
  1009. const auto sw = warp * warp * (3.0 - 2.0 * warp); // smoothstep: lines shorten as warp rises
  1010. s.shape = 3;
  1011. s.lineScale = delay * mult;
  1012. s.lineSpread = sw;
  1013. // √F per pass of the delay: RT = 6·D / −log10 F, times the shape's measured decay multiple (which already holds
  1014. // its line-length multiple); warp shortens the lines and so the decay
  1015. const auto meanSec = delay * (1.0 - 0.53 * sw) / 1000.0; // (the warp fractions average 0.47)
  1016. s.rt60 = fb >= 0.9999 ? 1000.0 : 6.0 * meanSec * lerpLog (m.dm0, m.dm100) / std::max (1e-4, -std::log10 (std::max (1e-6, fb)));
  1017. s.rt60 = std::min (s.rt60, 1000.0);
  1018. s.inDiffusion = dens;
  1019. s.loopDiffusion = 0.85 * dens;
  1020. s.crossFeed = 0.3 + 0.7 * dens;
  1021. s.parallelLines = dens < 0.02; // density 0: plain repeats of the delay
  1022. // stereo: plain echoes are mono (density 0); the shape's correlation at full density; warp decorrelates
  1023. const auto corr = (1.0 + (m.corr - 1.0) * dens) * (1.0 - std::min (1.0, 2.0 * warp));
  1024. const auto w = std::sqrt ((1.0 - corr) / (1.0 + corr + 1e-3));
  1025. s.width = (-1.0 + 2.0 * p[7]) * std::min (2.0, w);
  1026. s.outLowCut = 10.0 + 1990.0 * p[8];
  1027. s.outHighCut = 200.0 + 19800.0 * p[9];
  1028. s.modRateHz = skewLaw (p[10], 0.01, 10.0, 2.328);
  1029. s.modDepthMs = 2.0 * p[11] / std::sqrt (std::max (0.1, s.modRateHz));
  1030. s.gain = 0.9;
  1031. }
  1032. };
  1033. // ---------------------------------------------------------------------------------------------- Halo
  1034. // The reference exposes bare 0..1 values; the laws below are what the measurements showed (§5): shift −12..+12
  1035. // semitones (linear), decay set mainly by Size (≈ 1.5 s + 12.5 s × size), high cut 100..15000 Hz first order (the
  1036. // reference room's law), low cut 0..1000 Hz; mod rate / depth laws not confirmed.
  1037. struct HaloMode
  1038. {
  1039. const char* name;
  1040. double scale, rtScale, bright;
  1041. float value; // the Reverb mode value that selects it (nearest wins)
  1042. int stereo; // 0 = LYNX's own wide tank, 1 = mono in (the reference's Mono), 2 = two separate sides (its stereo modes)
  1043. double width; // side gain of the output
  1044. };
  1045. // round 135: the reference's four modes [EXEC: its trial, a left-only impulse — below 0.25 the input is summed and the
  1046. // tail comes out of both sides, from 0.25 up the left input stays on the left; 0.25 / 0.5 / 0.75 are the steps] beside
  1047. // LYNX's own Wide / Dense / Bright, which keep their values 0 / ½ / 1 (older projects sound the same)
  1048. const HaloMode haloModes[7] = { { "Wide", 1.2, 1.0, 0.0, 0.0f, 0, 1.0 }, { "Mono", 1.0, 1.0, 0.0, 0.125f, 1, 1.0 },
  1049. { "Small stereo", 0.75, 0.85, 0.0, 0.25f, 2, 1.0 }, { "Medium stereo", 1.0, 1.0, 0.0, 0.375f, 2, 1.0 },
  1050. { "Dense", 0.9, 1.05, 0.0, 0.5f, 0, 1.0 }, { "Big stereo", 1.3, 1.15, 0.0, 0.625f, 2, 1.0 },
  1051. { "Bright", 1.0, 0.75, 1.0, 1.0f, 0, 1.0 } };
  1052. int haloModeIndex (float v)
  1053. {
  1054. int best = 0;
  1055. for (int i = 1; i < 7; ++i)
  1056. if (std::abs (v - haloModes[i].value) < std::abs (v - haloModes[best].value))
  1057. best = i;
  1058. return best;
  1059. }
  1060. const char* const haloShiftModes[5] = { "Single", "Dual", "Single, bright", "Dual, bright", "No shift" };
  1061. const char* const haloColours[2] = { "Bright", "Dark" };
  1062. double haloShiftSemis (float v) { return -12.0 + 24.0 * v; }
  1063. class HaloFx final : public VerbEffect
  1064. {
  1065. public:
  1066. HaloFx() : VerbEffect (EffectKind::halo, 0.3, 0.05) {}
  1067. protected:
  1068. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  1069. {
  1070. mix = p[0];
  1071. const auto& m = haloModes[haloModeIndex (p[9])];
  1072. const auto shiftMode = choice (p[10], 5);
  1073. const auto dark = choice (p[11], 2) == 1;
  1074. const auto size = (double) p[4];
  1075. s.shiftSemis = haloShiftSemis (p[1]);
  1076. s.shiftFeedback = shiftMode == 4 ? 0.0 : 0.9 * p[2]; // the shifted share of the recirculation
  1077. s.shiftMix = 1.0;
  1078. s.shift2Mix = shiftMode == 1 || shiftMode == 3 ? 0.7 : 0.0;
  1079. s.shiftSemis2 = s.shiftSemis - 24.0;
  1080. s.inDiffusion = p[3];
  1081. s.loopDiffusion = 0.2 + 0.7 * p[3];
  1082. s.buildSec = 0.02 + 0.3 * size * (1.0 - p[3] * 0.5);
  1083. s.shape = 0;
  1084. s.lineScale = m.scale * (0.3 + 1.2 * size);
  1085. s.rt60 = (1.5 + 12.5 * size) * m.rtScale * std::min (3.0, 1.0 + 2.8 * p[2] * p[2]); // feedback lengthens the tail (§5)
  1086. s.earlyLevel = 0.45; // it speaks at once
  1087. s.earlySec = 0.03;
  1088. s.earlyDiffusion = p[3];
  1089. s.outLowCut = 250.0 * p[5]; // gentle: the reference's low cut barely shows at 63 Hz (§5, not confirmed)
  1090. s.outLowOrder = 1;
  1091. if (s.outLowCut < 5.0)
  1092. s.outLowCut = 0.0;
  1093. s.outHighCut = 100.0 + 14900.0 * p[6];
  1094. s.outHighOrder = 1;
  1095. if (shiftMode == 2 || shiftMode == 3 || m.bright > 0.0)
  1096. s.highShelfDb = 4.0, s.highShelfHz = 3000.0;
  1097. s.loopBandwidth = dark ? 5000.0 : 0.0;
  1098. s.modRateHz = 0.05 + 4.95 * p[7];
  1099. s.modDepthMs = 1.2 * p[8] / std::sqrt (std::max (0.3, s.modRateHz));
  1100. s.gain = dark ? 0.75 : 0.95;
  1101. if (m.stereo == 2)
  1102. s.crossFeed = 0.0, s.splitSides = true; // the two sides stay apart
  1103. }
  1104. // the reference's Mono mode sums the input (mono in, a stereo tail)
  1105. void preStage (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  1106. {
  1107. if (haloModes[haloModeIndex (p[9])].stereo != 1)
  1108. return;
  1109. for (int i = 0; i < n; ++i)
  1110. l[i] = r[i] = 0.5f * (l[i] + r[i]);
  1111. }
  1112. };
  1113. // ---------------------------------------------------------------------------------------------- Horizon
  1114. constexpr double horizonDecayKnots[] = { 0.0, 0.2, 0.5, 5.0, 0.75, 9.81, 0.775, 12.5, 0.8, 16.67, 0.825, 21.25, 0.85, 27.5,
  1115. 0.875, 33.75, 0.9, 41.67, 0.925, 50.0, 0.95, 57.5, 0.975, 63.75, 1.0, 70.0 };
  1116. constexpr int horizonDecayCount = (int) (sizeof (horizonDecayKnots) / sizeof (double) / 2);
  1117. constexpr double horizonEchoKnots[] = { 0.0, 0.0, 0.5, 100.0, 0.675, 197.2, 0.7, 250.0, 0.75, 375.0, 0.775, 456.2, 0.8, 550.0,
  1118. 0.825, 643.8, 0.85, 762.5, 0.875, 918.8, 0.9, 1075.0, 0.925, 1250.0, 0.95, 1500.0, 0.975, 1750.0,
  1119. 1.0, 2000.0 };
  1120. constexpr int horizonEchoCount = (int) (sizeof (horizonEchoKnots) / sizeof (double) / 2);
  1121. const char* const horizonEchoModes[horizon::echoModeCount] = { "Clean", "Tape", "Digital", "Analog", "Detune", "Reverse",
  1122. "Reverse Up", "Reverse Down", "Reverse Up/Down", "Sparkle", "Swarm", "Lo-fi" };
  1123. struct HorizonReverb
  1124. {
  1125. const char* name;
  1126. int shape;
  1127. double scale, rtScale, preMs, buildMs, early, gain;
  1128. };
  1129. LYNX_FIT_TABLE HorizonReverb horizonReverbs[horizon::reverbModeCount] = {
  1130. { "Room", 1, 1.0, 0.80, 50.0, 60.0, 0.3, 1.5 }, { "Chamber", 1, 1.2, 1.00, 45.0, 45.0, 0.3, 3.0 },
  1131. { "Plate", 2, 1.0, 1.00, 25.0, 8.0, 0.0, 3.6 }, { "Hall", 0, 1.0, 0.92, 35.0, 60.0, 0.2, 1.25 },
  1132. { "Cathedral", 0, 1.3, 1.05, 60.0, 80.0, 0.1, 1.6 }, { "Space", 0, 1.5, 1.10, 70.0, 90.0, 0.0, 0.5 },
  1133. { "Freeze", 0, 1.4, 3.30, 70.0, 900.0, 0.0, 0.4 }, { "Gated", 1, 0.8, 0.60, 30.0, 0.0, 0.1, 0.9 },
  1134. };
  1135. const char* const horizonColours[4] = { "Bright", "Neutral", "Dark", "Vintage" };
  1136. constexpr double horizonColourCut[4] = { 0.0, 16000.0, 11000.0, 9000.0 };
  1137. double horizonDecaySec (float v) { return knotLaw (v, horizonDecayKnots, horizonDecayCount); }
  1138. double horizonEchoMs (float v) { return knotLaw (v, horizonEchoKnots, horizonEchoCount); }
  1139. class HorizonFx final : public VerbEffect
  1140. {
  1141. public:
  1142. HorizonFx() : VerbEffect (EffectKind::horizon, 0.25, 0.25) {}
  1143. void prepare (double sampleRate) noexcept override
  1144. {
  1145. VerbEffect::prepare (sampleRate);
  1146. for (auto& l : echo)
  1147. l.allocate ((int) (2.6 * sampleRate) + 16);
  1148. for (auto& sh : shifters)
  1149. sh.prepare (sampleRate, 0.08);
  1150. reset();
  1151. }
  1152. void reset() noexcept override
  1153. {
  1154. VerbEffect::reset();
  1155. for (auto& l : echo)
  1156. l.clear();
  1157. for (auto& sh : shifters)
  1158. sh.clear();
  1159. tone[0].z = tone[1].z = 0.0f;
  1160. wow = 0.0;
  1161. crush[0] = crush[1] = 0.0f;
  1162. crushPhase = 0.0;
  1163. }
  1164. protected:
  1165. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  1166. {
  1167. const auto& m = horizonReverbs[choice (p[22], horizon::reverbModeCount)];
  1168. mix = p[0];
  1169. const auto size = (double) p[12];
  1170. s.shape = m.shape;
  1171. s.lineScale = m.scale * (0.5 + 0.8 * size);
  1172. s.predelaySec = m.preMs * (0.6 + 0.8 * size) / 1000.0;
  1173. const auto decay = horizonDecaySec (p[11]);
  1174. s.rt60 = decay * m.rtScale;
  1175. s.buildSec = m.buildMs / 1000.0 * (0.3 + 1.4 * p[13]);
  1176. const auto earlyLate = (double) p[14];
  1177. s.earlyLevel = m.early * (1.5 - earlyLate);
  1178. s.earlySec = 0.04 * (0.5 + size);
  1179. s.lateLevel = 0.5 + earlyLate;
  1180. s.inDiffusion = 0.4 + 0.6 * p[15];
  1181. s.loopDiffusion = 0.1 + 0.8 * p[15];
  1182. s.gateSec = choice (p[22], horizon::reverbModeCount) == 7 ? 0.5 * decay + 0.05 : 0.0;
  1183. s.modRateHz = skewLaw (p[17], 0.01, 10.0, 2.328);
  1184. s.modDepthMs = 1.0 * p[18] / std::sqrt (std::max (0.1, s.modRateHz));
  1185. s.outLowCut = skewLaw (p[19], 20.0, 1000.0, 1.807);
  1186. s.outHighCut = skewLaw (p[20], 1000.0, 20000.0, 1.078);
  1187. const auto colour = choice (p[23], 4);
  1188. if (horizonColourCut[colour] > 0.0)
  1189. s.outHighCut = std::min (s.outHighCut, horizonColourCut[colour]);
  1190. if (colour == 3)
  1191. s.outLowCut = std::max (s.outLowCut, 250.0);
  1192. s.width = -1.0 + 2.0 * p[1];
  1193. s.gain = m.gain * p[16];
  1194. }
  1195. // the echo before the reverb (routing "echo → reverb"), or after it
  1196. void echoStage (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept
  1197. {
  1198. const auto level = (float) p[9];
  1199. if (level <= 0.0f)
  1200. return;
  1201. const auto delay = std::max (1.0, syncedMs (choice (p[2], 4), std::min (5, choice (p[3], 6)), horizonEchoMs (p[4]), bpm)) * rate / 1000.0;
  1202. const auto fb = (float) (0.98 * p[5]);
  1203. const auto spread = (double) p[6];
  1204. const auto drive = std::pow (10.0, 24.0 * p[7] / 20.0);
  1205. const auto detune = (-30.0 + 60.0 * p[8]) / 100.0; // semitones
  1206. const auto mode = choice (p[21], horizon::echoModeCount);
  1207. double ratio = 1.0, ratio2 = 1.0;
  1208. bool shift = false, dual = false;
  1209. double cut = 18000.0, wowDepth = 0.0;
  1210. switch (mode)
  1211. {
  1212. case 1: cut = 7000.0; wowDepth = 0.0015; break; // tape
  1213. case 3: cut = 5000.0; break; // analog
  1214. case 4: shift = true; ratio = std::pow (2.0, (detune + 0.12) / 12.0); ratio2 = std::pow (2.0, -(detune + 0.12) / 12.0); dual = true; break;
  1215. case 5: shift = true; ratio = -1.0; break; // reverse
  1216. case 6: shift = true; ratio = -2.0; break;
  1217. case 7: shift = true; ratio = -0.5; break;
  1218. case 8: shift = true; ratio = -2.0; ratio2 = -0.5; dual = true; break;
  1219. case 9: shift = true; ratio = 2.0; break; // sparkle: an octave up in the repeats
  1220. case 10: shift = true; ratio = std::pow (2.0, 0.15 / 12.0); ratio2 = std::pow (2.0, -0.15 / 12.0); dual = true; wowDepth = 0.003; break;
  1221. case 11: cut = 4000.0; break; // lo-fi
  1222. default: break;
  1223. }
  1224. if (! shift && std::abs (detune) > 1e-3)
  1225. {
  1226. shift = true;
  1227. ratio = std::pow (2.0, detune / 12.0);
  1228. }
  1229. tone[0].setLowpass (cut, rate);
  1230. tone[1].a = tone[0].a;
  1231. const auto spreadL = delay * (1.0 - 0.25 * spread), spreadR = delay * (1.0 + 0.25 * spread);
  1232. const auto wowInc = 2.0 * pi * 0.7 / rate;
  1233. const auto hold = mode == 11 ? std::max (1.0, rate / 11025.0) : 1.0;
  1234. for (int i = 0; i < n; ++i)
  1235. {
  1236. wow += wowInc;
  1237. if (wow > 2.0 * pi)
  1238. wow -= 2.0 * pi;
  1239. const auto w = 1.0 + wowDepth * std::sin (wow);
  1240. float y[2] = { echo[0].readFrac (std::max (2.0, spreadL * w)), echo[1].readFrac (std::max (2.0, spreadR * w)) };
  1241. float x[2] = { l[i], r[i] };
  1242. for (int c = 0; c < 2; ++c)
  1243. {
  1244. auto v = y[c];
  1245. if (shift)
  1246. {
  1247. auto a = shifters[c].process (v, ratio);
  1248. if (dual)
  1249. a = 0.5f * (a + shifters[c + 2].process (v, ratio2));
  1250. v = a;
  1251. }
  1252. v = tone[c].lp (v);
  1253. if (drive > 1.0001)
  1254. v = (float) (std::tanh (drive * v) / std::sqrt (drive));
  1255. if (mode == 11)
  1256. {
  1257. if (c == 0)
  1258. crushPhase += 1.0;
  1259. if (crushPhase >= hold)
  1260. crush[c] = std::round (v * 64.0f) / 64.0f;
  1261. v = crush[c];
  1262. }
  1263. y[c] = v;
  1264. }
  1265. if (crushPhase >= hold)
  1266. crushPhase -= hold;
  1267. echo[0].write (x[0] + fb * y[0]);
  1268. echo[1].write (x[1] + fb * y[1]);
  1269. l[i] = x[0] + level * y[0];
  1270. r[i] = x[1] + level * y[1];
  1271. }
  1272. }
  1273. void preStage (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  1274. {
  1275. if (choice (p[10], 2) == 0)
  1276. echoStage (l, r, n, p, bpm);
  1277. }
  1278. void postStage (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  1279. {
  1280. if (choice (p[10], 2) == 1)
  1281. echoStage (l, r, n, p, bpm);
  1282. }
  1283. Line echo[2];
  1284. Shifter shifters[4];
  1285. OnePole tone[2];
  1286. double wow = 0.0, crushPhase = 0.0;
  1287. float crush[2] {};
  1288. };
  1289. // ---------------------------------------------------------------------------------------------- tables for the UI
  1290. std::vector<EffectParamInfo> params (std::initializer_list<EffectParamInfo> list) { return list; }
  1291. } // namespace
  1292. const char* epoch::modeName (int i) noexcept { return epochModes[std::clamp (i, 0, epoch::modeCount - 1)].name; }
  1293. const char* epoch::colourName (int i) noexcept { return epochColours[std::clamp (i, 0, epoch::colourCount - 1)]; }
  1294. double epoch::predelayMs (float v) noexcept { return epochPredelay (v); }
  1295. double epoch::decaySec (float v) noexcept { return epochDecay (v); }
  1296. const char* atrium::typeName (int i) noexcept { return atriumTypes[std::clamp (i, 0, atrium::typeCount - 1)].name; }
  1297. double atrium::decaySec (float v) noexcept { return atriumDecaySec (v); }
  1298. float atrium::decayValue (double s) noexcept { return (float) skewInverse (s, 0.1, 100.0, 5.717); }
  1299. double foil::lowCutHz (float v) noexcept { return v <= 0.0f ? 0.0 : 20.0 * std::pow (50.0, (double) std::min (v, 1.0f)); }
  1300. const char* foil::typeName (int i) noexcept { return foilTypes[std::clamp (i, 0, foil::typeCount - 1)].name; }
  1301. double foil::decaySec (float v) noexcept { return foilDecaySec (v); }
  1302. const char* nebula::modeName (int i) noexcept { return nebulaModes[std::clamp (i, 0, nebula::modeCount - 1)].name; }
  1303. double nebula::delayMs (float v) noexcept { return nebulaDelayMs (v); }
  1304. const char* horizon::echoModeName (int i) noexcept { return horizonEchoModes[std::clamp (i, 0, horizon::echoModeCount - 1)]; }
  1305. const char* horizon::reverbModeName (int i) noexcept { return horizonReverbs[std::clamp (i, 0, horizon::reverbModeCount - 1)].name; }
  1306. double horizon::decaySec (float v) noexcept { return horizonDecaySec (v); }
  1307. double horizon::echoMs (float v) noexcept { return horizonEchoMs (v); }
  1308. bool isLynxVerb (EffectKind k) noexcept
  1309. {
  1310. return k == EffectKind::epochVerb || k == EffectKind::atrium || k == EffectKind::foilPlate || k == EffectKind::nebula
  1311. || k == EffectKind::halo || k == EffectKind::horizon;
  1312. }
  1313. const EffectInfo& info (EffectKind kind)
  1314. {
  1315. // defaults = the reference plugins' defaults (their saved normalised values)
  1316. static const EffectInfo epochInfo { "Epoch Verb",
  1317. params ({ { "Mix", 1.0f }, { "Predelay", 0.25f }, { "Decay", 0.42048f }, { "Size", 1.0f }, { "Attack", 0.5f },
  1318. { "Bass mult", 0.623039f }, { "Bass xover", 0.381065f }, { "High shelf", 0.0f }, { "High freq", 0.5f },
  1319. { "Early diffusion", 1.0f }, { "Late diffusion", 1.0f }, { "Mod rate", 0.245455f }, { "Mod depth", 0.38f },
  1320. { "High cut", 0.590539f }, { "Low cut", 0.0f }, { "Color", 0.0f }, { "Mode", 0.0f } }) };
  1321. static const EffectInfo atriumInfo { "Atrium",
  1322. params ({ { "Mix", 1.0f }, { "Predelay", 0.02f }, { "Decay", 0.5f }, { "High cut", 0.530201f }, { "Early/late", 0.5f },
  1323. { "Late size", 0.5f }, { "Late cross", 1.0f }, { "Late mod rate", 0.090909f }, { "Late mod depth", 0.5f },
  1324. { "Bass mult", 0.333333f }, { "Bass xover", 0.090909f }, { "High mult", 0.444444f }, { "High xover", 0.530201f },
  1325. { "Early size", 0.029029f }, { "Early cross", 0.1f }, { "Early mod rate", 0.090909f }, { "Early mod depth", 0.0f },
  1326. { "Early send", 0.0f }, { "Diffusion", 1.0f }, { "Type", 0.0f }, { "Space", 0.0f }, { "Low cut", 0.0f } }) };
  1327. static const EffectInfo foilInfo { "Foil Plate",
  1328. params ({ { "Mix", 1.0f }, { "Predelay", 0.0f }, { "Decay", 0.5f }, { "Size", 0.5f }, { "Width", 0.5f },
  1329. { "Mod rate", 0.365345f }, { "Mod depth", 0.0f }, { "Low EQ freq", 0.343434f }, { "Low EQ gain", 0.5f },
  1330. { "High EQ freq", 0.494949f }, { "High EQ gain", 0.5f }, { "Type", 0.0f }, // EQ at 700 Hz / 10 kHz (round 129)
  1331. { "Infinite", 0.0f }, { "Low cut", 0.0f } }) }; // round 135 (the Little Plate role)
  1332. static const EffectInfo nebulaInfo { "Nebula",
  1333. params ({ { "Mix", 0.5f }, { "Sync", 0.0f }, { "Note", 0.333333f }, { "Delay", 0.5f }, { "Warp", 0.0f },
  1334. { "Feedback", 0.5f }, { "Density", 0.0f }, { "Width", 1.0f }, { "Low cut", 0.0f }, { "High cut", 1.0f },
  1335. { "Mod rate", 0.273834f }, { "Mod depth", 0.5f }, { "Mode", 0.0f },
  1336. { "Clear", 0.0f } }) }; // round 129: the reference's CLEAR button (any change of the value empties the network)
  1337. static const EffectInfo haloInfo { "Halo",
  1338. params ({ { "Mix", 0.5f }, { "Shift", 1.0f }, { "Feedback", 0.0f }, { "Diffusion", 0.919192f }, { "Size", 0.5f },
  1339. { "Low cut", 0.0f }, { "High cut", 0.530201f }, { "Mod rate", 0.1f }, { "Mod depth", 0.3f },
  1340. { "Reverb mode", 0.0f }, { "Shift mode", 0.0f }, { "Color", 0.0f } }) };
  1341. static const EffectInfo horizonInfo { "Horizon",
  1342. params ({ { "Mix", 0.5f }, { "Width", 1.0f }, { "Echo sync", 0.0f }, { "Echo note", 0.4f }, { "Echo time", 0.15f },
  1343. { "Echo feedback", 0.0f }, { "Echo spread", 0.0f }, { "Echo drive", 0.125f }, { "Echo detune", 0.566667f },
  1344. { "Echo level", 0.0f }, { "Routing", 0.0f }, { "Decay", 0.291667f }, { "Size", 0.5f }, { "Attack", 0.5f },
  1345. { "Early/late", 0.5f }, { "Density", 1.0f }, { "Reverb level", 1.0f }, { "Mod rate", 0.132226f },
  1346. { "Mod depth", 0.4f }, { "Low EQ freq", 0.25f }, { "High EQ freq", 0.208197f }, { "Echo mode", 0.090909f },
  1347. { "Reverb mode", 0.142857f }, { "Color", 0.333333f } }) };
  1348. switch (kind)
  1349. {
  1350. case EffectKind::atrium: return atriumInfo;
  1351. case EffectKind::foilPlate: return foilInfo;
  1352. case EffectKind::nebula: return nebulaInfo;
  1353. case EffectKind::halo: return haloInfo;
  1354. case EffectKind::horizon: return horizonInfo;
  1355. default: return epochInfo;
  1356. }
  1357. }
  1358. std::string paramText (EffectKind kind, int param, float v, double bpm)
  1359. {
  1360. (void) bpm;
  1361. switch (kind)
  1362. {
  1363. case EffectKind::epochVerb:
  1364. switch (param)
  1365. {
  1366. case 1: return ms (epochPredelay (v));
  1367. case 2: return sec (epochDecay (v));
  1368. case 5: return fmt ("%.2f X", epochBassMult (v));
  1369. case 6:
  1370. case 8:
  1371. case 13: return hz (param == 6 ? epochBassXover (v) : epochFreq (v));
  1372. case 7: return fmt ("%.1f dB", -24.0 + 24.0 * v);
  1373. case 11: return fmt ("%.2f Hz", 0.1 + 9.9 * v);
  1374. case 14: return hz (10.0 + 1490.0 * v);
  1375. case 15: return epoch::colourName (choice (v, epoch::colourCount));
  1376. case 16: return epoch::modeName (choice (v, epoch::modeCount));
  1377. default: return pct (v);
  1378. }
  1379. case EffectKind::atrium:
  1380. switch (param)
  1381. {
  1382. case 1: return ms (500.0 * v);
  1383. case 2: return sec (atriumDecaySec (v));
  1384. case 3: return hz (100.0 + 14900.0 * v);
  1385. case 5: case 6: case 8: case 14: case 16: case 17: case 18: return fmt ("%.2f", v);
  1386. case 7: case 15: return fmt ("%.2f Hz", 0.05 + 4.95 * v);
  1387. case 9: return fmt ("%.2f X", 0.5 + 1.5 * v);
  1388. case 10: return hz (100.0 + 9900.0 * v);
  1389. case 11: return fmt ("%.2f X", 0.1 + 0.9 * v);
  1390. case 12: return hz (100.0 + 14900.0 * v);
  1391. case 13: return ms (1.0 + 999.0 * v);
  1392. case 19: return atrium::typeName (choice (v, atrium::typeCount));
  1393. case 21: return hz (1000.0 * v);
  1394. default: return pct (v);
  1395. }
  1396. case EffectKind::foilPlate:
  1397. switch (param)
  1398. {
  1399. case 1: return ms (epochPredelay (v));
  1400. case 2: return sec (foilDecaySec (v));
  1401. case 3: case 4: return pct (2.0 * v);
  1402. case 5: return fmt ("%.2f Hz", skewLaw (v, 0.05, 5.0, 2.381));
  1403. case 7: return hz (20.0 + 1980.0 * v);
  1404. case 9: return hz (200.0 + 19800.0 * v);
  1405. case 8: case 10: return fmt ("%+.1f dB", -12.0 + 24.0 * v);
  1406. case 11: return foil::typeName (choice (v, foil::typeCount));
  1407. case 12: return v >= 0.5f ? "on" : "off";
  1408. case 13: return v <= 0.0f ? "off" : hz (foil::lowCutHz (v));
  1409. default: return pct (v);
  1410. }
  1411. case EffectKind::nebula:
  1412. switch (param)
  1413. {
  1414. case 13: return "Clear";
  1415. case 1: return syncNames[choice (v, 4)];
  1416. case 2: return noteNames[choice (v, 7)];
  1417. case 3: return ms (nebulaDelayMs (v));
  1418. case 7: return pct (-1.0 + 2.0 * v);
  1419. case 8: return hz (10.0 + 1990.0 * v);
  1420. case 9: return hz (200.0 + 19800.0 * v);
  1421. case 10: return fmt ("%.2f Hz", skewLaw (v, 0.01, 10.0, 2.328));
  1422. case 12: return nebula::modeName (choice (v, nebula::modeCount));
  1423. default: return pct (v);
  1424. }
  1425. case EffectKind::halo:
  1426. switch (param)
  1427. {
  1428. case 1: return fmt ("%+.1f st", haloShiftSemis (v));
  1429. // round 129: what the engine applies (0 … 250 Hz, off below 5 Hz; configure()) — the display said
  1430. // 10 … 1000 Hz, the reference editor's range, while the filter never went above 250 Hz
  1431. case 5: return 250.0 * v < 5.0 ? std::string ("off") : hz (250.0 * v);
  1432. case 6: return hz (100.0 + 14900.0 * v);
  1433. case 7: return fmt ("%.2f Hz", 0.05 + 4.95 * v);
  1434. case 9: return haloModes[haloModeIndex (v)].name;
  1435. case 10: return haloShiftModes[choice (v, 5)];
  1436. case 11: return haloColours[choice (v, 2)];
  1437. default: return pct (v);
  1438. }
  1439. case EffectKind::horizon:
  1440. switch (param)
  1441. {
  1442. case 1: return pct (-1.0 + 2.0 * v);
  1443. case 2: return syncNames[choice (v, 4)];
  1444. case 3: return noteNames[std::min (5, choice (v, 6))];
  1445. case 4: return ms (horizonEchoMs (v));
  1446. case 7: return fmt ("%.1f dB", 24.0 * v);
  1447. case 8: return fmt ("%+.1f ct", -30.0 + 60.0 * v);
  1448. case 10: return choice (v, 2) == 0 ? "Echo > Reverb" : "Reverb > Echo";
  1449. case 11: return sec (horizonDecaySec (v));
  1450. case 17: return fmt ("%.2f Hz", skewLaw (v, 0.01, 10.0, 2.328));
  1451. case 19: return hz (skewLaw (v, 20.0, 1000.0, 1.807));
  1452. case 20: return hz (skewLaw (v, 1000.0, 20000.0, 1.078));
  1453. case 21: return horizon::echoModeName (choice (v, horizon::echoModeCount));
  1454. case 22: return horizon::reverbModeName (choice (v, horizon::reverbModeCount));
  1455. case 23: return horizonColours[choice (v, 4)];
  1456. default: return pct (v);
  1457. }
  1458. default: return pct (v);
  1459. }
  1460. }
  1461. std::unique_ptr<Effect> make (EffectKind kind)
  1462. {
  1463. switch (kind)
  1464. {
  1465. case EffectKind::atrium: return std::make_unique<AtriumFx>();
  1466. case EffectKind::foilPlate: return std::make_unique<FoilFx>();
  1467. case EffectKind::nebula: return std::make_unique<NebulaFx>();
  1468. case EffectKind::halo: return std::make_unique<HaloFx>();
  1469. case EffectKind::horizon: return std::make_unique<HorizonFx>();
  1470. default: return std::make_unique<EpochFx>();
  1471. }
  1472. }
  1473. } // namespace lynx::verbs