EchoSuite.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. #include "core/EchoSuite.h"
  2. #include <algorithm>
  3. #include <array>
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <vector>
  8. #include "core/Biquad.h"
  9. #include "core/Effects.h"
  10. namespace lynx::echosuite
  11. {
  12. namespace
  13. {
  14. constexpr double pi = 3.14159265358979323846;
  15. constexpr double twoPi = 2.0 * pi;
  16. float clamp01 (float v) noexcept { return std::clamp (std::isfinite (v) ? v : 0.0f, 0.0f, 1.0f); }
  17. double bipolar (float v) noexcept { return 2.0 * (double) clamp01 (v) - 1.0; }
  18. double signedPow (double x, double k) noexcept { return x < 0.0 ? -std::pow (-x, k) : std::pow (x, k); }
  19. bool on (float v) noexcept { return v >= 0.5f; }
  20. // ---- building blocks -----------------------------------------------------------------------------------------------
  21. // Circular delay line (power-of-two size). at (d) is x[n − d] for the sample about to be written (d ≥ 2), read with
  22. // 4-point cubic (Catmull-Rom / Hermite) interpolation.
  23. struct Line
  24. {
  25. std::vector<float> buf;
  26. int mask = 0;
  27. int w = 0;
  28. void allocate (double samples)
  29. {
  30. int size = 16;
  31. while ((double) size < samples + 8.0)
  32. size <<= 1;
  33. buf.assign ((std::size_t) size, 0.0f);
  34. mask = size - 1;
  35. w = 0;
  36. }
  37. void clear() noexcept
  38. {
  39. std::fill (buf.begin(), buf.end(), 0.0f);
  40. w = 0;
  41. }
  42. double maxDelay() const noexcept { return (double) mask - 4.0; }
  43. void push (float x) noexcept
  44. {
  45. buf[(std::size_t) w] = x;
  46. w = (w + 1) & mask;
  47. }
  48. float tap (int d) const noexcept { return buf[(std::size_t) ((w - d) & mask)]; } // x[n − d], d ≥ 1
  49. float at (double d) const noexcept
  50. {
  51. if (buf.empty())
  52. return 0.0f;
  53. d = std::clamp (d, 2.0, maxDelay());
  54. const auto i = (int) d;
  55. const auto t = (float) (d - (double) i);
  56. const auto xm1 = tap (i - 1), x0 = tap (i), x1 = tap (i + 1), x2 = tap (i + 2);
  57. const auto c1 = 0.5f * (x1 - xm1);
  58. const auto c2 = xm1 - 2.5f * x0 + 2.0f * x1 - 0.5f * x2;
  59. const auto c3 = 0.5f * (x2 - xm1) + 1.5f * (x0 - x1);
  60. return ((c3 * t + c2) * t + c1) * t + x0;
  61. }
  62. };
  63. // one-pole low-pass (impulse invariant); hp() = input − low-pass
  64. struct OnePole
  65. {
  66. float a = 1.0f, z = 0.0f;
  67. void set (double hz, double sr) noexcept { a = (float) (1.0 - std::exp (-twoPi * std::clamp (hz, 1.0, sr * 0.49) / sr)); }
  68. void open() noexcept { a = 1.0f; } // passes everything (the low-pass output is the input)
  69. float lp (float x) noexcept { return z += a * (x - z); }
  70. float hp (float x) noexcept { return x - lp (x); }
  71. void reset() noexcept { z = 0.0f; }
  72. };
  73. // 8th-order Butterworth low-pass (4 cookbook sections)
  74. struct Butter8
  75. {
  76. std::array<Biquad, 4> s;
  77. double hz = -1.0;
  78. void set (double f, double sr) noexcept
  79. {
  80. f = std::clamp (f, 20.0, sr * 0.45);
  81. if (std::abs (f - hz) < 0.5)
  82. return;
  83. hz = f;
  84. static constexpr double q[4] = { 0.50979558, 0.60134489, 0.89997622, 2.56291545 };
  85. const auto w0 = twoPi * f / sr, cw = std::cos (w0), sw = std::sin (w0);
  86. for (int k = 0; k < 4; ++k)
  87. {
  88. const auto alpha = sw / (2.0 * q[k]);
  89. const auto a0 = 1.0 + alpha;
  90. auto& b = s[(std::size_t) k];
  91. b.b0 = (1.0 - cw) * 0.5 / a0;
  92. b.b1 = (1.0 - cw) / a0;
  93. b.b2 = b.b0;
  94. b.a1 = -2.0 * cw / a0;
  95. b.a2 = (1.0 - alpha) / a0;
  96. }
  97. }
  98. float process (float x, int ch) noexcept
  99. {
  100. for (auto& b : s)
  101. x = b.process (x, ch);
  102. return x;
  103. }
  104. void reset() noexcept
  105. {
  106. for (auto& b : s)
  107. b.reset();
  108. }
  109. };
  110. // Saturation with unity small-signal gain: (tanh (k (x + b)) − tanh (k b)) / (k sech² (k b)). The bias b gives even
  111. // harmonics (fitted to the measured 2nd / 3rd harmonic levels of each character, REA_rodada101 §3.3).
  112. struct Shaper
  113. {
  114. float k = 0.5f, b = 0.0f, t0 = 0.0f, g = 1.0f;
  115. void set (float kk, float bb) noexcept
  116. {
  117. k = kk;
  118. b = bb;
  119. t0 = std::tanh (k * b);
  120. g = 1.0f / (k * (1.0f - t0 * t0));
  121. }
  122. float operator() (float x) const noexcept { return (std::tanh (k * (x + b)) - t0) * g; }
  123. };
  124. // xorshift noise, uniform in [−1, 1)
  125. struct Noise
  126. {
  127. std::uint32_t s = 0x9e3779b9u;
  128. float next() noexcept
  129. {
  130. s ^= s << 13;
  131. s ^= s >> 17;
  132. s ^= s << 5;
  133. return (float) ((double) s / 2147483648.0 - 1.0);
  134. }
  135. };
  136. // smooth random LFO (cosine interpolation between random points `rate` times per second), about unit rms
  137. struct RandomLfo
  138. {
  139. Noise noise;
  140. double phase = 1.0;
  141. float from = 0.0f, to = 0.0f;
  142. void seed (std::uint32_t s) noexcept
  143. {
  144. noise.s = s == 0 ? 1u : s;
  145. from = noise.next();
  146. to = noise.next();
  147. phase = 0.0;
  148. }
  149. float next (double rate, double sr) noexcept
  150. {
  151. phase += rate / sr;
  152. if (phase >= 1.0)
  153. {
  154. phase -= std::floor (phase);
  155. from = to;
  156. to = noise.next();
  157. }
  158. const auto u = (float) (0.5 - 0.5 * std::cos (pi * phase));
  159. return 2.1f * (from + (to - from) * u);
  160. }
  161. };
  162. // 90° phase-difference allpass pair (the published 8th-order IIR design, Niemitalo): re / im of the analytic signal
  163. struct Hilbert
  164. {
  165. static constexpr float a[4] = { 0.6923878f, 0.9360654322959f, 0.9882295226860f, 0.9987488452737f };
  166. static constexpr float b[4] = { 0.4021921162426f, 0.8561710882420f, 0.9722909545651f, 0.9952884791278f };
  167. std::array<float, 4> ax1 {}, ax2 {}, ay1 {}, ay2 {}, bx1 {}, bx2 {}, by1 {}, by2 {};
  168. float delayed = 0.0f;
  169. static float stage (float k, float x, float& x1, float& x2, float& y1, float& y2) noexcept
  170. {
  171. const auto y = k * k * (x + y2) - x2;
  172. x2 = x1;
  173. x1 = x;
  174. y2 = y1;
  175. y1 = y;
  176. return y;
  177. }
  178. void analytic (float x, float& re, float& im) noexcept
  179. {
  180. auto u = x, v = x;
  181. for (std::size_t k = 0; k < 4; ++k)
  182. {
  183. u = stage (a[k], u, ax1[k], ax2[k], ay1[k], ay2[k]);
  184. v = stage (b[k], v, bx1[k], bx2[k], by1[k], by2[k]);
  185. }
  186. re = delayed;
  187. im = v;
  188. delayed = u;
  189. }
  190. void reset() noexcept { *this = Hilbert {}; }
  191. };
  192. // single-sideband frequency shifter (shift in Hz, either sign)
  193. struct Shifter
  194. {
  195. Hilbert h;
  196. double phase = 0.0;
  197. float process (float x, double hz, double sr) noexcept
  198. {
  199. float up = 0.0f, down = 0.0f;
  200. both (x, hz, sr, up, down);
  201. return up;
  202. }
  203. // the input shifted by +hz and by −hz (the two sidebands of the same analytic signal)
  204. void both (float x, double hz, double sr, float& up, float& down) noexcept
  205. {
  206. float re = 0.0f, im = 0.0f;
  207. h.analytic (x, re, im);
  208. const auto c = (float) std::cos (twoPi * phase), s = (float) std::sin (twoPi * phase);
  209. phase += hz / sr;
  210. phase -= std::floor (phase);
  211. up = re * c + im * s;
  212. down = re * c - im * s;
  213. }
  214. void reset() noexcept
  215. {
  216. h.reset();
  217. phase = 0.0;
  218. }
  219. };
  220. // delay-line pitch shifter: two taps sweeping a window, crossfaded with sin² / cos² (the classic rotating-tape idea)
  221. struct PitchShifter
  222. {
  223. Line line;
  224. double phase = 0.0;
  225. double window = 2400.0;
  226. void prepare (double sr)
  227. {
  228. window = 0.05 * sr;
  229. line.allocate (window * 2.0 + 16.0);
  230. }
  231. void reset() noexcept
  232. {
  233. line.clear();
  234. phase = 0.0;
  235. }
  236. float process (float x, double ratio) noexcept
  237. {
  238. line.push (x);
  239. phase += (1.0 - ratio) / window;
  240. phase -= std::floor (phase);
  241. float y = 0.0f;
  242. for (int k = 0; k < 2; ++k)
  243. {
  244. auto p = phase + 0.5 * k;
  245. p -= std::floor (p);
  246. const auto g = (float) std::sin (pi * p);
  247. y += g * g * line.at (3.0 + p * window);
  248. }
  249. return y;
  250. }
  251. };
  252. // Schroeder allpass with a fractional delay
  253. struct Allpass
  254. {
  255. Line line;
  256. float process (float x, double d, float g) noexcept
  257. {
  258. const auto v = line.at (d);
  259. const auto u = x + g * v;
  260. line.push (u);
  261. return v - g * u;
  262. }
  263. };
  264. // first-order allpass (phaser stage)
  265. struct Ap1
  266. {
  267. float z = 0.0f;
  268. float process (float x, float a) noexcept
  269. {
  270. const auto y = a * x + z;
  271. z = x - a * y;
  272. return y;
  273. }
  274. };
  275. // smoothing of a control value toward a target (one pole, time constant in seconds)
  276. struct Glide
  277. {
  278. double value = -1.0;
  279. double next (double target, double seconds, double sr) noexcept
  280. {
  281. if (value < 0.0)
  282. value = target;
  283. const auto a = 1.0 - std::exp (-1.0 / std::max (1.0, seconds * sr));
  284. value += (target - value) * a;
  285. return value;
  286. }
  287. };
  288. float softLimit (float x) noexcept { return 2.0f * std::tanh (0.5f * x); } // unity small-signal, bounded at ±2
  289. std::string fmt (const char* f, double v)
  290. {
  291. char b[64];
  292. std::snprintf (b, sizeof (b), f, v);
  293. return b;
  294. }
  295. std::string hzText (double f) { return f >= 1000.0 ? fmt ("%.2f kHz", f / 1000.0) : fmt ("%.0f Hz", f); }
  296. std::string msText (double ms) { return ms >= 1000.0 ? fmt ("%.2f s", ms / 1000.0) : fmt (ms < 10.0 ? "%.2f ms" : "%.1f ms", ms); }
  297. std::string pct (double v) { return fmt ("%.0f%%", v); }
  298. const char* const noteNames[10] = { "1/64", "1/32", "1/16", "1/8", "1/4", "1/2", "1/1", "2/1", "4/1", "8/1" };
  299. } // namespace
  300. void equalPowerMix (float m, float& dry, float& wet) noexcept
  301. {
  302. const auto a = 0.5 * pi * (double) clamp01 (m);
  303. dry = (float) std::cos (a);
  304. wet = (float) std::sin (a);
  305. if (m <= 0.0f)
  306. wet = 0.0f;
  307. if (m >= 1.0f)
  308. dry = 0.0f;
  309. }
  310. // =====================================================================================================================
  311. // Echoform
  312. // =====================================================================================================================
  313. namespace echoform
  314. {
  315. namespace
  316. {
  317. constexpr std::array<std::pair<double, double>, 7> timeLaw { { { 0.0, 0.0 }, { 0.5, 500.0 }, { 0.76, 1000.0 }, { 0.82, 1500.0 },
  318. { 0.88, 3000.0 }, { 0.94, 6000.0 }, { 1.0, 20000.0 } } };
  319. } // namespace
  320. double timeMs (float v) noexcept
  321. {
  322. const auto x = (double) clamp01 (v);
  323. for (std::size_t i = 1; i < timeLaw.size(); ++i)
  324. if (x <= timeLaw[i].first)
  325. {
  326. const auto [x0, y0] = timeLaw[i - 1];
  327. const auto [x1, y1] = timeLaw[i];
  328. return y0 + (y1 - y0) * (x - x0) / (x1 - x0);
  329. }
  330. return 20000.0;
  331. }
  332. float timeValue (double ms) noexcept
  333. {
  334. ms = std::clamp (ms, 0.0, 20000.0);
  335. for (std::size_t i = 1; i < timeLaw.size(); ++i)
  336. if (ms <= timeLaw[i].second)
  337. {
  338. const auto [x0, y0] = timeLaw[i - 1];
  339. const auto [x1, y1] = timeLaw[i];
  340. return (float) (x0 + (x1 - x0) * (ms - y0) / (y1 - y0));
  341. }
  342. return 1.0f;
  343. }
  344. int syncKind (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 4.0f + 1e-4f) - 1, 0, 3); }
  345. int noteIndex (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 10.0f + 1e-4f), 0, 9); }
  346. double noteBeats (int index, int kind) noexcept
  347. {
  348. const auto beats = std::pow (2.0, (double) std::clamp (index, 0, 9) - 4.0); // 1/64 = 1/16 of a quarter … 8/1 = 32
  349. return kind == 2 ? beats * 1.5 : kind == 3 ? beats * 2.0 / 3.0 : beats;
  350. }
  351. double sideMs (const EffectParams& p, bool right, double bpm) noexcept
  352. {
  353. const auto kind = syncKind (p[right ? syncR : syncL]);
  354. if (kind == 0)
  355. return timeMs (p[right ? timeR : timeL]);
  356. bpm = bpm > 1.0 ? bpm : 120.0;
  357. return std::min (20000.0, noteBeats (noteIndex (p[right ? noteR : noteL]), kind) * 60000.0 / bpm);
  358. }
  359. double feedbackGain (float v) noexcept { return 2.0 * clamp01 (v); }
  360. double modRateHz (float v) noexcept { return 0.01 + 9.99 * std::pow ((double) clamp01 (v), 2.3277); }
  361. double shiftHz (float v) noexcept { return 1000.0 * signedPow (bipolar (v), 5.0); }
  362. double shiftDetuneHz (float v) noexcept { return 10.0 * signedPow (bipolar (v), 2.5); }
  363. double pitchSemis (float v) noexcept { return 24.0 * bipolar (v); }
  364. double lowCutHz (float v) noexcept { return 10.0 + 1990.0 * clamp01 (v); }
  365. double highCutHz (float v) noexcept { return 200.0 + 19800.0 * clamp01 (v); }
  366. double outputDb (float v) noexcept { return -18.0 + 36.0 * clamp01 (v); }
  367. int hybridOf (float v) noexcept { return v < 0.25f ? 0 : (v < 0.75f ? 1 : 2); }
  368. float hybridValue (int h) noexcept { return h <= 0 ? 0.0f : (h == 1 ? 0.5f : 1.0f); }
  369. Mode modeOf (float v) noexcept { return (Mode) std::clamp ((int) std::floor (clamp01 (v) * (float) modeCount), 0, modeCount - 1); }
  370. float modeValue (Mode m) noexcept { return ((float) m + 0.5f) / (float) modeCount; }
  371. Style styleOf (float v) noexcept { return (Style) std::clamp ((int) std::floor (clamp01 (v) * (float) styleCount), 0, styleCount - 1); }
  372. float styleValue (Style s) noexcept { return ((float) s + 0.5f) / (float) styleCount; }
  373. const char* modeName (Mode m) noexcept
  374. {
  375. static const char* const names[modeCount] = { "Tape", "Lo-Fi", "Hi-Fi tape", "Bucket brigade", "Analog", "Digital", "Clean",
  376. "Phaser", "Shifter", "Pitch", "Reverse", "Detune" };
  377. return names[std::clamp ((int) m, 0, modeCount - 1)];
  378. }
  379. namespace
  380. {
  381. const char* const styleNames[styleCount] = { "Single", "Ratio", "Quad", "Dual", "Ping-pong" };
  382. class Echoform final : public Effect
  383. {
  384. public:
  385. void prepare (double rate) noexcept override
  386. {
  387. sr = rate;
  388. const auto lineSamples = sr * 20.6 + 4096.0; // 20 s + modulation
  389. for (auto& c : ch)
  390. {
  391. c.line.allocate (lineSamples);
  392. c.pitch.prepare (sr);
  393. for (auto& a : c.diff)
  394. a.line.allocate (sr * 0.05);
  395. }
  396. reset();
  397. }
  398. void reset() noexcept override
  399. {
  400. std::uint32_t seed = 0x1234567u;
  401. for (auto& c : ch)
  402. {
  403. c.line.clear();
  404. c.pitch.reset();
  405. c.shifter.reset();
  406. for (auto& a : c.diff)
  407. a.line.clear();
  408. for (auto& a : c.phaser)
  409. a = {};
  410. c.shelf.reset();
  411. c.tapeLp1.reset();
  412. c.tapeLp2.reset();
  413. c.hifiLp.reset();
  414. c.hifiHp.reset();
  415. c.lowCut.reset();
  416. c.highCut.reset();
  417. c.held = 0.0f;
  418. c.holdPhase = 0.0;
  419. c.time = {};
  420. c.wow.seed (seed += 0x9e3779b9u);
  421. c.flutter1.seed (seed += 0x9e3779b9u);
  422. c.flutter2.seed (seed += 0x9e3779b9u);
  423. c.drift.seed (seed += 0x9e3779b9u);
  424. c.noise.s = seed += 0x9e3779b9u;
  425. }
  426. steep.reset();
  427. lfoPhase = 0.0;
  428. envelope = 0.0f;
  429. hyb.dirty = true;
  430. }
  431. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  432. {
  433. if (ch[0].line.buf.empty())
  434. prepare (sr);
  435. if (hybridOf (p[hybrid]) != 0)
  436. {
  437. hybridProcess (l, r, n, p, bpm);
  438. return;
  439. }
  440. hyb.dirty = true;
  441. const auto md = modeOf (p[mode]);
  442. const auto st = styleOf (p[style]);
  443. float dryGain = 1.0f, wetGain = 0.0f;
  444. equalPowerMix (p[mix], dryGain, wetGain);
  445. // times: sync / ms, spread (±¼ of the spread on each side), style
  446. auto tL = sideMs (p, false, bpm);
  447. auto tR = sideMs (p, true, bpm);
  448. if (st == Style::single || st == Style::quad)
  449. tR = tL;
  450. else if (st == Style::ratio)
  451. tR = tL * (0.01 + 0.99 * (double) clamp01 (p[ratio]));
  452. const auto spreadMs = 20.0 * bipolar (p[spread]);
  453. tL = std::max (0.0, tL + spreadMs * 0.25);
  454. tR = std::max (0.0, tR - spreadMs * 0.25);
  455. const auto g = feedbackGain (p[feedback]);
  456. const bool pairsOn = p[pairs] >= 0.5f;
  457. const auto outGain = (float) std::pow (10.0, outputDb (p[output]) / 20.0);
  458. wetGain *= outGain;
  459. const auto ageV = clamp01 (p[age]);
  460. const auto driveGain = (float) std::pow (10.0, 24.0 * (double) clamp01 (p[drive]) / 20.0);
  461. const auto widthV = (float) bipolar (p[width]);
  462. const auto diffAmount = clamp01 (p[diffusion]);
  463. const auto diffScale = 0.25 + 0.75 * (double) clamp01 (p[diffSize]);
  464. const auto duck = clamp01 (p[ducking]);
  465. const auto rate = modRateHz (p[modRate]);
  466. const auto depth = (double) clamp01 (p[modDepth]);
  467. const auto wowV = (double) clamp01 (p[wow]), flutterV = (double) clamp01 (p[flutter]);
  468. const auto fShift = shiftHz (p[shift]), fDetune = shiftDetuneHz (p[shiftDetune]);
  469. const auto semis = pitchSemis (p[pitch]);
  470. const auto cents = 100.0 * bipolar (p[pitchDetune]);
  471. // character: the write-path filter / shaper and the modulation source
  472. configure (md, tL, ageV);
  473. for (auto& c : ch)
  474. {
  475. c.lowCut.set (lowCutHz (p[lowCut]), sr);
  476. // the measured high cut acts like a one-pole at about half its label (§3.3); it opens up fully at the top
  477. const auto hc = clamp01 (p[highCut]);
  478. const auto open = std::max (0.0f, (hc - 0.8f) / 0.2f);
  479. if (hc >= 0.999f)
  480. c.highCut.open();
  481. else
  482. c.highCut.set (highCutHz (hc) * (0.5 + 1.5 * (double) (open * open)), sr);
  483. }
  484. const auto quadEl = std::pow (2.0, -0.4 * bipolar (p[spacing]));
  485. const auto quadEr = std::pow (2.0, 0.4 * bipolar (p[spacing]));
  486. const auto envAttack = (float) (1.0 - std::exp (-1.0 / (0.01 * sr)));
  487. const auto envRelease = (float) (1.0 - std::exp (-1.0 / (0.25 * sr)));
  488. static constexpr double diffMs[4] = { 4.3, 6.7, 9.1, 12.7 };
  489. for (int i = 0; i < n; ++i)
  490. {
  491. const float in[2] = { l[i], r[i] };
  492. // modulation (ms): wow / flutter in the tape characters, an LFO in the others
  493. lfoPhase += rate / sr;
  494. lfoPhase -= std::floor (lfoPhase);
  495. double modMs[2] = { 0.0, 0.0 };
  496. for (int c = 0; c < 2; ++c)
  497. {
  498. auto& s = ch[(std::size_t) c];
  499. switch (modulation)
  500. {
  501. case ModSource::tape:
  502. // measured: wow 100 % ≈ 14 c rms around 1.5–2.5 Hz, flutter 100 % ≈ 14 c rms at 7–15 Hz
  503. modMs[c] = wowAmountMs * wowV * s.wow.next (4.0, sr)
  504. + flutterAmountMs * flutterV * 0.7 * (s.flutter1.next (18.0, sr) + s.flutter2.next (26.0, sr));
  505. break;
  506. case ModSource::sine:
  507. modMs[c] = modAmountMs * depth * depth * std::sin (twoPi * (lfoPhase + (c == 1 ? 0.25 : 0.0)));
  508. break;
  509. case ModSource::random:
  510. modMs[c] = 0.5 * modAmountMs * depth * depth * 0.5 * s.drift.next (rate * 2.0, sr);
  511. break;
  512. case ModSource::none: break;
  513. }
  514. }
  515. // targets in samples (tape glide when the time changes)
  516. const double target[2] = { tL * 0.001 * sr, tR * 0.001 * sr };
  517. double d[2];
  518. for (int c = 0; c < 2; ++c)
  519. {
  520. auto& s = ch[(std::size_t) c];
  521. d[c] = std::max (2.0, s.time.next (target[c], 0.12, sr) + modMs[c] * 0.001 * sr);
  522. }
  523. // read the echoes (read path: shifter / pitch / reverse / phaser / detune)
  524. float echo[2], fbSig[2];
  525. for (int c = 0; c < 2; ++c)
  526. {
  527. auto& s = ch[(std::size_t) c];
  528. float y = 0.0f;
  529. if (st == Style::quad)
  530. {
  531. const auto e = c == 0 ? quadEl : quadEr;
  532. float last = 0.0f;
  533. for (int k = 1; k <= 4; ++k)
  534. {
  535. const auto v = s.line.at (std::max (2.0, d[c] * std::pow ((double) k / 4.0, e)));
  536. y += 0.5f * v;
  537. last = v;
  538. }
  539. fbSig[c] = last;
  540. }
  541. else if (md == Mode::reverse)
  542. {
  543. y = reverseRead (s, d[c], std::pow (2.0, semis / 12.0));
  544. fbSig[c] = y;
  545. }
  546. else
  547. {
  548. // the pitch shifter's window delays the echo by half a window: read that much earlier
  549. const auto early = (md == Mode::pitch || md == Mode::detune) ? s.pitch.window * 0.5 : 0.0;
  550. y = s.line.at (std::max (2.0, d[c] - early));
  551. fbSig[c] = y;
  552. }
  553. const auto sign = c == 0 ? 1.0 : -1.0;
  554. switch (md)
  555. {
  556. case Mode::shifter:
  557. y = s.shifter.process (y, fShift + sign * fDetune, sr);
  558. break;
  559. case Mode::pitch:
  560. y = s.pitch.process (y, std::pow (2.0, (semis + sign * cents / 100.0) / 12.0));
  561. break;
  562. case Mode::detune:
  563. {
  564. const auto wobble = 0.2 * s.drift.next (1.0, sr);
  565. y = s.pitch.process (y, std::pow (2.0, (sign * cents * (1.0 + wobble)) / 1200.0));
  566. break;
  567. }
  568. case Mode::phaser:
  569. {
  570. const auto lfo = std::sin (twoPi * (lfoPhase + (c == 1 ? 0.25 : 0.0)));
  571. const auto fc = std::clamp (200.0 * std::pow (8.0, (double) ageV) * std::pow (2.0, 2.0 * depth * lfo), 40.0, 0.4 * sr);
  572. const auto t = std::tan (pi * fc / sr);
  573. const auto a = (float) ((t - 1.0) / (t + 1.0));
  574. auto u = y;
  575. for (auto& ap : s.phaser)
  576. u = ap.process (u, a);
  577. y = 0.5f * (y + u);
  578. break;
  579. }
  580. default: break;
  581. }
  582. if (st != Style::quad) // the quad style feeds back its last tap as read
  583. fbSig[c] = y;
  584. echo[c] = y;
  585. }
  586. // loop inputs per style
  587. const auto mono = 0.5f * (in[0] + in[1]);
  588. float loopIn[2];
  589. if (st == Style::pingPong && pairsOn)
  590. {
  591. // in pairs: left → right at full level, the loop gain once per pair
  592. loopIn[0] = mono + (float) g * fbSig[1];
  593. loopIn[1] = fbSig[0];
  594. }
  595. else if (st == Style::pingPong)
  596. {
  597. const auto half = (float) std::sqrt (g);
  598. loopIn[0] = mono + half * fbSig[1];
  599. loopIn[1] = half * fbSig[0];
  600. }
  601. else
  602. {
  603. loopIn[0] = in[0] + (float) g * fbSig[0];
  604. loopIn[1] = in[1] + (float) g * fbSig[1];
  605. }
  606. // write path: diffusion, character, cuts, a soft limit (feedback above 100 % stays bounded)
  607. for (int c = 0; c < 2; ++c)
  608. {
  609. auto& s = ch[(std::size_t) c];
  610. auto u = loopIn[c];
  611. if (diffAmount > 0.0f)
  612. for (std::size_t k = 0; k < 4; ++k)
  613. u = s.diff[k].process (u, diffMs[k] * diffScale * 0.001 * sr * (c == 1 ? 1.07 : 1.0), 0.65f * diffAmount);
  614. u = character (s, c, u, md, ageV, driveGain);
  615. u = s.highCut.lp (s.lowCut.hp (u));
  616. s.line.push (softLimit (u));
  617. }
  618. // ducking (not measured: a plain envelope-driven gain on the echoes)
  619. auto duckGain = 1.0f;
  620. if (duck > 0.0f)
  621. {
  622. const auto e = std::abs (mono);
  623. envelope += (e - envelope) * (e > envelope ? envAttack : envRelease);
  624. duckGain = 1.0f / (1.0f + 8.0f * duck * envelope);
  625. }
  626. // width (M / S of the echoes) and the equal-power mix
  627. const auto m = 0.5f * (echo[0] + echo[1]), sd = 0.5f * (echo[0] - echo[1]) * widthV;
  628. l[i] = dryGain * in[0] + wetGain * duckGain * (m + sd);
  629. r[i] = dryGain * in[1] + wetGain * duckGain * (m - sd);
  630. }
  631. }
  632. private:
  633. enum class ModSource { none, tape, sine, random };
  634. struct Channel
  635. {
  636. Line line;
  637. PitchShifter pitch;
  638. Shifter shifter;
  639. std::array<Allpass, 4> diff;
  640. std::array<Ap1, 6> phaser;
  641. OnePole shelf, tapeLp1, tapeLp2, hifiLp, hifiHp, lowCut, highCut;
  642. RandomLfo wow, flutter1, flutter2, drift;
  643. Noise noise;
  644. Glide time;
  645. float held = 0.0f;
  646. double holdPhase = 0.0, revPhase = 0.0;
  647. };
  648. // reversed grains: two readers sweeping backwards through the last (1 + r)·D samples, Hann crossfade
  649. float reverseRead (Channel& s, double d, double r) noexcept
  650. {
  651. s.revPhase += 1.0 / std::max (64.0, d);
  652. s.revPhase -= std::floor (s.revPhase);
  653. float y = 0.0f;
  654. for (int k = 0; k < 2; ++k)
  655. {
  656. auto p = s.revPhase + 0.5 * k;
  657. p -= std::floor (p);
  658. const auto w = (float) std::sin (pi * p);
  659. y += w * w * s.line.at (2.0 + p * (1.0 + r) * d);
  660. }
  661. return y;
  662. }
  663. void configure (Mode m, double timeMsL, float ageV) noexcept
  664. {
  665. modAmountMs = 6.1;
  666. switch (m)
  667. {
  668. case Mode::tape:
  669. case Mode::lofi:
  670. case Mode::phaser:
  671. {
  672. // tape: the head bump / low-end loss (low shelf −5 dB), and a two-pole roll-off that falls with the
  673. // delay time (tape speed): ≈ 9 kHz at 100 ms, 5 kHz at 300 ms, 2.7 kHz at 1 s
  674. const auto fc = m == Mode::tape ? std::clamp (9000.0 * std::pow (std::max (1.0, timeMsL) / 100.0, -0.5), 1500.0, 10000.0) : 9000.0;
  675. for (auto& c : ch)
  676. {
  677. c.shelf.set (350.0, sr);
  678. c.tapeLp1.set (fc, sr);
  679. c.tapeLp2.set (fc, sr);
  680. }
  681. shaper.set (m == Mode::lofi ? 0.9f : 0.95f, m == Mode::lofi ? 0.0f : 0.11f);
  682. modulation = m == Mode::tape ? ModSource::tape : (m == Mode::lofi ? ModSource::sine : ModSource::none);
  683. break;
  684. }
  685. case Mode::hifi:
  686. case Mode::shifter:
  687. for (auto& c : ch)
  688. {
  689. c.hifiLp.set (13000.0, sr);
  690. c.hifiHp.set (15.0, sr);
  691. }
  692. shaper.set (0.55f, m == Mode::hifi ? 0.17f : 0.0f);
  693. modulation = m == Mode::hifi ? ModSource::random : ModSource::none;
  694. break;
  695. case Mode::bucket:
  696. steep.set (1650.0, sr);
  697. shaper.set (0.6f, 0.05f);
  698. modulation = ModSource::sine;
  699. break;
  700. case Mode::analog:
  701. steep.set (3450.0, sr);
  702. shaper.set (1.525f, 0.0f);
  703. modulation = ModSource::sine;
  704. break;
  705. case Mode::digital:
  706. case Mode::pitch:
  707. case Mode::reverse:
  708. case Mode::detune:
  709. steep.set (8000.0, sr);
  710. shaper.set (0.375f, 0.0f);
  711. modulation = m == Mode::digital ? ModSource::sine : ModSource::none;
  712. break;
  713. case Mode::clean:
  714. {
  715. // age = a steep low-pass: off at 0, 5.3 kHz at 50 %, 3.3 kHz at 100 %
  716. const auto a = (double) ageV;
  717. const auto fc = a < 0.5 ? 5300.0 * std::pow (24000.0 / 5300.0, (0.5 - a) / 0.5) : 3300.0 * std::pow (5300.0 / 3300.0, 2.0 * (1.0 - a));
  718. steep.set (fc, sr);
  719. shaper.set (0.3f, 0.0f);
  720. modulation = ModSource::sine;
  721. modAmountMs = 10.4;
  722. break;
  723. }
  724. }
  725. }
  726. float character (Channel& s, int c, float u, Mode m, float ageV, float driveGain) noexcept
  727. {
  728. const auto drive = [this, driveGain] (float x) { return shaper (x * driveGain) / driveGain; };
  729. switch (m)
  730. {
  731. case Mode::tape:
  732. case Mode::phaser:
  733. u = drive (u); // saturation at the record head, then the tape's response
  734. u = 0.8f * (u - 0.44f * s.shelf.lp (u)); // −1.9 dB through the tape path, −5 dB low shelf
  735. return s.tapeLp2.lp (s.tapeLp1.lp (u));
  736. case Mode::lofi:
  737. {
  738. u = u - 0.44f * s.shelf.lp (u);
  739. u = s.tapeLp2.lp (s.tapeLp1.lp (u));
  740. u = crush (s, u, 16.0 - 10.0 * ageV, 1.0 + 5.0 * ageV);
  741. return drive (u);
  742. }
  743. case Mode::hifi:
  744. case Mode::shifter:
  745. return drive (s.hifiLp.lp (s.hifiHp.hp (u)));
  746. case Mode::bucket:
  747. case Mode::analog:
  748. {
  749. u = drive (steep.process (u, c));
  750. const auto level = m == Mode::bucket ? 1e-3f : 2.5e-4f; // −60 / −72 dBFS at full age (∝ age²)
  751. return u + level * ageV * ageV * 1.73f * s.noise.next();
  752. }
  753. case Mode::digital:
  754. case Mode::pitch:
  755. case Mode::reverse:
  756. case Mode::detune:
  757. {
  758. u = drive (steep.process (u, c));
  759. if (ageV > 0.0f)
  760. u = crush (s, u, 24.0 - 14.0 * ageV, 1.0 + 1.5 * ageV * ageV);
  761. return u;
  762. }
  763. case Mode::clean:
  764. return ageV > 0.0f ? drive (steep.process (u, c)) : drive (u);
  765. }
  766. return u;
  767. }
  768. // sample-and-hold rate reduction and bit-depth reduction (lo-fi / digital age)
  769. static float crush (Channel& s, float u, double bits, double hold) noexcept
  770. {
  771. s.holdPhase += 1.0;
  772. if (s.holdPhase >= hold)
  773. {
  774. s.holdPhase -= hold;
  775. const auto q = std::pow (2.0, std::clamp (bits, 4.0, 24.0) - 1.0);
  776. s.held = (float) (std::round ((double) u * q) / q);
  777. }
  778. return s.held;
  779. }
  780. // ---- the hybrid echo (round 124) -----------------------------------------------------------------------------
  781. // [EXEC] black-box measurement of the official H-Delay trial (docs/analise/REA_rodada124.md §5), our own code:
  782. // - ping-pong: the left line takes (L + R)/√2 plus fb² × the right echo; the right line takes the left echo; the left
  783. // echo comes T after the input, every later pass T + 1 (single-sample impulses land exactly there);
  784. // - each write into a line goes through y = v·g, g = min (1 / (1 + |v|), released toward 1 with a 10 ms time
  785. // constant): a lone impulse of 1/√2 comes back as 0.41421 = x / (1 + x), the next pass 0.29289 = f (0.41421);
  786. // - stereo: each side its own line, fb × its echo, the same gain on every write;
  787. // - analog: the same loop between an input gain and an output gain (fitted on impulses at -66 … +6 dBFS and her takes)
  788. // and a gentle low tilt on the echoes; its hiss (≈ -85 dBFS) is not reproduced.
  789. struct HybridState
  790. {
  791. Line a, b;
  792. double ga = 1.0, gb = 1.0, aPrev = 0.0, bPrev = 0.0;
  793. Biquad tilt;
  794. bool dirty = true;
  795. } hyb;
  796. static constexpr double analogIn = 0.8866, analogOut = 0.9776;
  797. static inline double linkedGain (double level, double& g, double rel) noexcept
  798. {
  799. const auto inst = 1.0 / (1.0 + level);
  800. g = std::min (inst, 1.0 - (1.0 - g) * rel);
  801. return g;
  802. }
  803. void hybridProcess (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept
  804. {
  805. if (hyb.a.buf.empty() || hyb.dirty)
  806. {
  807. if (hyb.a.buf.empty())
  808. {
  809. hyb.a.allocate (sr * 3.6 + 8.0);
  810. hyb.b.allocate (sr * 3.6 + 8.0);
  811. }
  812. hyb.a.clear();
  813. hyb.b.clear();
  814. hyb.ga = hyb.gb = 1.0;
  815. hyb.aPrev = hyb.bPrev = 0.0;
  816. hyb.tilt.reset();
  817. hyb.dirty = false;
  818. }
  819. const bool analog = hybridOf (p[hybrid]) == 2;
  820. const bool pp = styleOf (p[style]) == Style::pingPong;
  821. const auto t = std::clamp ((int) std::lround (sideMs (p, false, bpm) * sr / 1000.0), 1, (int) hyb.a.maxDelay());
  822. const auto fb = feedbackGain (p[feedback]); // H-Delay's feedback / 100 (0 … 2)
  823. const auto fbPair = fb * fb;
  824. float dryGain = 1.0f, wetGain = 0.0f;
  825. equalPowerMix (p[mix], dryGain, wetGain);
  826. wetGain *= (float) std::pow (10.0, outputDb (p[output]) / 20.0);
  827. const auto rel = std::exp (-1.0 / (0.010 * sr));
  828. const auto inGain = analog ? analogIn : 1.0, outGain = analog ? analogOut : 1.0;
  829. if (analog)
  830. hyb.tilt.shelf (sr, 942.0, 0.63, false, 0.5);
  831. constexpr double invSqrt2 = 0.70710678118654752;
  832. for (int i = 0; i < n; ++i)
  833. {
  834. const double xl = l[i], xr = r[i];
  835. const double aOut = hyb.a.tap (t), bOut = hyb.b.tap (t);
  836. // the two lines share one gain (a loud left pass also turns the right one down)
  837. const auto va = pp ? inGain * (xl + xr) * invSqrt2 + fbPair * hyb.bPrev : inGain * xl + fb * hyb.aPrev;
  838. const auto vb = pp ? hyb.aPrev : inGain * xr + fb * hyb.bPrev;
  839. const auto g = linkedGain (std::max (std::fabs (va), std::fabs (vb)), hyb.ga, rel);
  840. hyb.a.push ((float) (va * g));
  841. hyb.b.push ((float) (vb * g));
  842. hyb.aPrev = aOut;
  843. hyb.bPrev = bOut;
  844. float el = (float) (aOut * outGain), er = (float) (bOut * outGain);
  845. if (analog)
  846. {
  847. el = hyb.tilt.process (el, 0);
  848. er = hyb.tilt.process (er, 1);
  849. }
  850. l[i] = dryGain * (float) xl + wetGain * el;
  851. r[i] = dryGain * (float) xr + wetGain * er;
  852. }
  853. }
  854. double sr = 48000.0;
  855. std::array<Channel, 2> ch;
  856. Butter8 steep;
  857. Shaper shaper;
  858. ModSource modulation = ModSource::tape;
  859. double modAmountMs = 6.1;
  860. double wowAmountMs = 1.15, flutterAmountMs = 0.2;
  861. double lfoPhase = 0.0;
  862. float envelope = 0.0f;
  863. };
  864. } // namespace
  865. } // namespace echoform
  866. // =====================================================================================================================
  867. // Spiral Echo
  868. // =====================================================================================================================
  869. namespace spiral
  870. {
  871. double shiftHz (float v) noexcept { return 1000.0 * signedPow (bipolar (v), 5.0); }
  872. double delayMs (float v) noexcept { return 0.01 + 999.99 * std::pow ((double) clamp01 (v), 3.322); }
  873. float delayValue (double ms) noexcept { return (float) std::pow (std::clamp ((ms - 0.01) / 999.99, 0.0, 1.0), 1.0 / 3.322); }
  874. int syncIndex (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 23.0f), 0, 23); } // as measured: floor (23 v) in float
  875. double syncBeats (int index) noexcept
  876. {
  877. if (index <= 0 || index > 23)
  878. return 0.0;
  879. if (index <= 8)
  880. return std::pow (2.0, (double) index - 5.0); // 1/64 = 1/16 beat … 2/1 = 8 beats
  881. if (index <= 15)
  882. return 1.5 * std::pow (2.0, (double) (index - 8) - 5.0); // dotted 1/64 … 1/1
  883. return 2.0 / 3.0 * std::pow (2.0, (double) (index - 15) - 5.0); // triplet 1/64 … 2/1
  884. }
  885. double loopGain (float v) noexcept { return 1.5 * clamp01 (v); }
  886. double lowCutHz (float v) noexcept { return 20.0 + 2980.0 * clamp01 (v); }
  887. double highCutHz (float v) noexcept { return 50.0 + 14950.0 * clamp01 (v); }
  888. namespace
  889. {
  890. class SpiralEcho final : public Effect
  891. {
  892. public:
  893. void prepare (double rate) noexcept override
  894. {
  895. sr = rate;
  896. for (auto& c : ch)
  897. c.line.allocate (sr * 12.0);
  898. reset();
  899. }
  900. void reset() noexcept override
  901. {
  902. for (auto& c : ch)
  903. {
  904. c.line.clear();
  905. c.shifter.reset();
  906. c.lo.reset();
  907. c.hi.reset();
  908. }
  909. time = {};
  910. }
  911. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  912. {
  913. if (ch[0].line.buf.empty())
  914. prepare (sr);
  915. const auto wet = clamp01 (p[mix]), dry = 1.0f - wet; // linear crossfade (REA_rodada101 §4)
  916. const auto beats = syncBeats (syncIndex (p[sync]));
  917. const auto ms = beats > 0.0 ? beats * 60000.0 / (bpm > 1.0 ? bpm : 120.0) : delayMs (p[delay]);
  918. const auto target = std::clamp (ms * 0.001 * sr, 2.0, ch[0].line.maxDelay() - 4.0);
  919. const auto g = (float) loopGain (p[feedback]);
  920. const auto hz = shiftHz (p[shift]);
  921. const auto stereoMode = on (p[stereo]);
  922. for (auto& c : ch)
  923. {
  924. c.lo.set (lowCutHz (p[lowCut]), sr);
  925. c.hi.set (highCutHz (p[highCut]), sr);
  926. }
  927. for (int i = 0; i < n; ++i)
  928. {
  929. const auto d = time.next (target, 0.05, sr);
  930. float* io[2] = { l + i, r + i };
  931. for (int c = 0; c < 2; ++c)
  932. {
  933. auto& s = ch[(std::size_t) c];
  934. const auto x = *io[c];
  935. // every pass around the loop shifts by +hz; in stereo the right output is the same repeat taken
  936. // with the opposite shift (measured: L 1100 1200 1300 …, R 900 1000 1100 … for +100 Hz)
  937. const auto y = s.line.at (d);
  938. float up = 0.0f, down = 0.0f;
  939. s.shifter.both (s.hi.lp (s.lo.hp (y)), hz, sr, up, down);
  940. s.line.push (x + g * std::tanh (up)); // the loop is bounded by a soft limiter
  941. *io[c] = dry * x + wet * (c == 1 && stereoMode ? down : up);
  942. }
  943. }
  944. }
  945. private:
  946. struct Channel
  947. {
  948. Line line;
  949. Shifter shifter;
  950. OnePole lo, hi;
  951. };
  952. double sr = 48000.0;
  953. std::array<Channel, 2> ch;
  954. Glide time;
  955. };
  956. } // namespace
  957. } // namespace spiral
  958. // =====================================================================================================================
  959. // Orbit
  960. // =====================================================================================================================
  961. namespace orbit
  962. {
  963. double rateHz (float v) noexcept { return 10.0 * signedPow (bipolar (v), 3.333); }
  964. double depthMs (float v) noexcept { return 50.0 * std::pow ((double) clamp01 (v), 2.322); }
  965. double manualMs (float v) noexcept { return 50.0 * signedPow (bipolar (v), 4.0); }
  966. double feedbackGain (float v) noexcept { return 0.7 * signedPow (bipolar (v), 1.5); } // measured ≈ 0.22 at 50 %, 0.7 at 100 %
  967. Mode modeOf (float v) noexcept { return (Mode) std::clamp ((int) std::floor (clamp01 (v) * (float) modeCount + 1e-3f) - 1, 0, modeCount - 1); }
  968. float modeValue (Mode m) noexcept { return ((float) m + 1.0f) / (float) modeCount; }
  969. const char* modeName (Mode m) noexcept
  970. {
  971. static const char* const names[modeCount] = { "Rise", "Rise / fall", "Through-zero +", "Through-zero −", "Tri chorus", "Octo chorus",
  972. "Doubler", "Drift rise", "Drift rise / fall", "Ensemble", "Symphonic" };
  973. return names[std::clamp ((int) m, 0, modeCount - 1)];
  974. }
  975. namespace
  976. {
  977. class Orbit final : public Effect
  978. {
  979. public:
  980. void prepare (double rate) noexcept override
  981. {
  982. sr = rate;
  983. for (auto& c : ch)
  984. c.line.allocate (sr * 0.25);
  985. reset();
  986. }
  987. void reset() noexcept override
  988. {
  989. std::uint32_t seed = 0xabcdef1u;
  990. for (auto& c : ch)
  991. {
  992. c.line.clear();
  993. for (auto& rl : c.random)
  994. rl.seed (seed += 0x9e3779b9u);
  995. c.fb = 0.0f;
  996. }
  997. phase = fast = 0.0;
  998. speedRandom.seed (0x51ed27u);
  999. }
  1000. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  1001. {
  1002. if (ch[0].line.buf.empty())
  1003. prepare (sr);
  1004. float dryGain = 1.0f, wetGain = 0.0f;
  1005. equalPowerMix (p[mix], dryGain, wetGain);
  1006. const auto m = modeOf (p[mode]);
  1007. const auto hz = rateHz (p[rate]);
  1008. const auto dm = depthMs (p[depth]) * 0.001 * sr;
  1009. const auto base = (0.25 + std::abs (manualMs (p[manual]))) * 0.001 * sr;
  1010. const auto g = (float) feedbackGain (p[feedback]);
  1011. rateNow = hz;
  1012. for (int i = 0; i < n; ++i)
  1013. {
  1014. auto step = hz / sr;
  1015. if (m == Mode::driftRise || m == Mode::driftRiseFall)
  1016. step *= 1.0 + 0.5 * speedRandom.next (std::max (0.05, std::abs (hz)), sr) / 2.1;
  1017. phase += step;
  1018. phase -= std::floor (phase);
  1019. fast += 7.3 * step;
  1020. fast -= std::floor (fast);
  1021. const float in[2] = { l[i], r[i] };
  1022. for (int c = 0; c < 2; ++c)
  1023. {
  1024. auto& s = ch[(std::size_t) c];
  1025. const auto wet = voices (s, c, m, base, dm);
  1026. s.line.push (in[c] + g * s.fb);
  1027. s.fb = wet;
  1028. const auto out = dryGain * in[c] + wetGain * wet;
  1029. (c == 0 ? l : r)[i] = out;
  1030. }
  1031. }
  1032. }
  1033. private:
  1034. struct Channel
  1035. {
  1036. Line line;
  1037. std::array<RandomLfo, 4> random;
  1038. float fb = 0.0f;
  1039. };
  1040. static double frac (double x) noexcept { return x - std::floor (x); }
  1041. static double sine (double p) noexcept { return std::sin (twoPi * p); }
  1042. float voices (Channel& s, int c, Mode m, double base, double dm) noexcept
  1043. {
  1044. const auto at = [&s] (double d) { return s.line.at (std::max (2.0, d)); };
  1045. const auto off = c == 0 ? 0.0 : 0.25;
  1046. switch (m)
  1047. {
  1048. case Mode::rise:
  1049. case Mode::driftRise:
  1050. {
  1051. // barber pole: two taps ramping across the range, each faded in / out at the ends (sin window)
  1052. const auto lo = m == Mode::rise ? 0.25 : 0.02, span = m == Mode::rise ? 0.5 : 0.96;
  1053. float y = 0.0f;
  1054. for (int k = 0; k < 2; ++k)
  1055. {
  1056. const auto q = frac (phase + off + 0.5 * k);
  1057. const auto w = (float) std::sin (pi * q);
  1058. y += w * w * at (base + dm * (lo + span * (1.0 - q)));
  1059. }
  1060. return y;
  1061. }
  1062. case Mode::riseFall:
  1063. case Mode::driftRiseFall:
  1064. {
  1065. const auto q = frac (phase + off);
  1066. const auto tri = 1.0 - std::abs (2.0 * q - 1.0);
  1067. const auto lo = m == Mode::riseFall ? 0.25 : 0.02, span = m == Mode::riseFall ? 0.5 : 0.96;
  1068. return at (base + dm * (lo + span * tri));
  1069. }
  1070. case Mode::throughZeroPlus:
  1071. case Mode::throughZeroMinus:
  1072. {
  1073. // a fixed tap at the top of the sweep and one sweeping up to (and just past) it: through-zero
  1074. const auto fixed = base + 2.36 * dm;
  1075. const auto moving = base + dm * (0.65 + 1.75 * (0.5 + 0.5 * sine (phase + (c == 0 ? 0.0 : 0.02))));
  1076. const auto sign = m == Mode::throughZeroMinus ? -1.0f : 1.0f;
  1077. return 0.5f * at (fixed) + sign * 0.5f * at (moving);
  1078. }
  1079. case Mode::tri:
  1080. {
  1081. float y = 0.0f;
  1082. for (int k = 0; k < 3; ++k)
  1083. y += at (base + dm * (0.5 + 0.5 * sine (phase + k / 3.0 + (c == 0 ? 0.0 : 1.0 / 6.0))));
  1084. return y * 0.57735f;
  1085. }
  1086. case Mode::octo:
  1087. {
  1088. float y = 0.0f;
  1089. for (int k = 0; k < 4; ++k)
  1090. y += at (base + dm * (0.15 + 0.7 * (0.5 + 0.5 * sine (phase + (2 * k + c) / 8.0))));
  1091. return 0.5f * y;
  1092. }
  1093. case Mode::doubler:
  1094. return at (base + dm * (0.5 + 0.4 / 2.1 * std::clamp ((double) s.random[0].next (0.3 + std::abs (rateNow), sr), -2.1, 2.1)));
  1095. case Mode::ensemble:
  1096. {
  1097. float y = 0.0f;
  1098. for (int k = 0; k < 2; ++k)
  1099. {
  1100. const auto q = phase + (2 * k + c) / 6.0;
  1101. y += at (base + dm * (0.5 + 0.35 * sine (q) + 0.15 * sine (fast + q)));
  1102. }
  1103. return 0.70711f * y;
  1104. }
  1105. case Mode::symphonic:
  1106. {
  1107. float y = 0.0f;
  1108. for (int k = 0; k < 2; ++k)
  1109. {
  1110. const auto q = phase + (2 * k + c) / 4.0;
  1111. const auto rnd = std::clamp ((double) s.random[(std::size_t) k + 1].next (0.7, sr) / 2.1, -1.0, 1.0);
  1112. y += at (base + dm * (0.5 + 0.3 * sine (q) + 0.18 * rnd));
  1113. }
  1114. return 0.70711f * y;
  1115. }
  1116. }
  1117. return 0.0f;
  1118. }
  1119. double sr = 48000.0;
  1120. std::array<Channel, 2> ch;
  1121. RandomLfo speedRandom;
  1122. double phase = 0.0, fast = 0.0, rateNow = 0.5;
  1123. };
  1124. } // namespace
  1125. } // namespace orbit
  1126. // =====================================================================================================================
  1127. // Lattice Mod
  1128. // =====================================================================================================================
  1129. namespace lattice
  1130. {
  1131. int tapCount (float v) noexcept
  1132. {
  1133. static constexpr int counts[10] = { 1, 1, 2, 3, 3, 3, 4, 8, 8, 16 };
  1134. return counts[std::clamp ((int) std::floor (clamp01 (v) * 24.0f + 1e-4f), 0, 9)];
  1135. }
  1136. double delayMs (float v) noexcept { return 1000.0 * clamp01 (v); }
  1137. double tapMs (const EffectParams& p, int tap, int taps, bool right) noexcept
  1138. {
  1139. const auto d = delayMs (p[delay]) * (on (p[halfSpeed]) ? 2.0 : 1.0);
  1140. auto t = d * (1.0 - (double) clamp01 (p[spread]) * (double) tap / (double) std::max (1, taps));
  1141. const auto skewV = bipolar (p[skew]);
  1142. if (! right && skewV < 0.0)
  1143. t *= 1.0 + 0.5 * skewV;
  1144. if (right && skewV > 0.0)
  1145. t *= 1.0 - 0.5 * skewV;
  1146. // random: a fixed scatter of each tap by up to half the tap spacing
  1147. if (p[random] > 0.0f && taps > 1)
  1148. {
  1149. const auto h = std::sin ((double) (tap * 7919 + (right ? 104729 : 0)) * 12.9898) * 43758.5453;
  1150. const auto u = (h - std::floor (h)) * 2.0 - 1.0;
  1151. t += u * 0.5 * (double) clamp01 (p[random]) * d * (double) clamp01 (p[spread]) / (double) taps;
  1152. }
  1153. return std::max (0.0, t);
  1154. }
  1155. double tapLevel (const EffectParams& p, int tap, int taps) noexcept
  1156. {
  1157. auto gain = std::pow (10.0, 12.0 * bipolar (p[tapGain]) / 20.0) / std::sqrt ((double) std::max (1, taps));
  1158. if (taps > 1)
  1159. {
  1160. const auto pos = 1.0 - 2.0 * (double) tap / (double) (taps - 1); // +1 at the longest tap, −1 at the shortest
  1161. gain *= std::pow (10.0, 6.0 * bipolar (p[slope]) * pos / 20.0);
  1162. }
  1163. return -gain; // the measured wet is polarity-inverted (REA_rodada101 §6)
  1164. }
  1165. namespace
  1166. {
  1167. double syncBeats (int index) noexcept
  1168. {
  1169. static constexpr double beats[18] = { 0.0, 1.0 / 16, 1.0 / 12, 3.0 / 32, 1.0 / 8, 1.0 / 6, 3.0 / 16, 1.0 / 4, 1.0 / 3,
  1170. 3.0 / 8, 1.0 / 2, 2.0 / 3, 3.0 / 4, 1.0, 4.0 / 3, 3.0 / 2, 2.0, 4.0 };
  1171. return beats[std::clamp (index, 0, 17)];
  1172. }
  1173. class LatticeMod final : public Effect
  1174. {
  1175. public:
  1176. void prepare (double rate) noexcept override
  1177. {
  1178. sr = rate;
  1179. for (auto& c : ch)
  1180. {
  1181. c.line.allocate (sr * 6.0);
  1182. for (auto& a : c.diff)
  1183. a.line.allocate (sr * 1.1);
  1184. }
  1185. reset();
  1186. }
  1187. void reset() noexcept override
  1188. {
  1189. std::uint32_t seed = 0x7a11ce5u;
  1190. for (auto& c : ch)
  1191. {
  1192. c.line.clear();
  1193. for (auto& a : c.diff)
  1194. a.line.clear();
  1195. for (auto& rl : c.detune)
  1196. rl.seed (seed += 0x9e3779b9u);
  1197. c.diffLfo.seed (seed += 0x9e3779b9u);
  1198. c.noise.s = seed += 0x9e3779b9u;
  1199. c.lo.reset();
  1200. c.hi.reset();
  1201. c.dark.reset();
  1202. c.xover.reset();
  1203. c.fb = 0.0f;
  1204. }
  1205. vibPhase = 0.0;
  1206. base = {};
  1207. }
  1208. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  1209. {
  1210. if (ch[0].line.buf.empty())
  1211. prepare (sr);
  1212. float dryGain = 1.0f, wetGain = 0.0f;
  1213. equalPowerMix (p[mix], dryGain, wetGain);
  1214. const auto nTaps = tapCount (p[lattice::taps]);
  1215. const auto half = on (p[halfSpeed]);
  1216. const auto syncB = syncBeats ((int) std::lround (clamp01 (p[sync]) * 17.0f));
  1217. const auto delayScale = syncB > 0.0 && delayMs (p[delay]) > 0.0
  1218. ? (syncB * 60000.0 / (bpm > 1.0 ? bpm : 120.0)) * (half ? 2.0 : 1.0) / (delayMs (p[delay]) * (half ? 2.0 : 1.0))
  1219. : 1.0;
  1220. std::array<std::array<double, 16>, 2> tap {};
  1221. std::array<double, 16> gains {};
  1222. for (int k = 0; k < nTaps; ++k)
  1223. {
  1224. tap[0][(std::size_t) k] = tapMs (p, k, nTaps, false) * delayScale * 0.001 * sr;
  1225. tap[1][(std::size_t) k] = tapMs (p, k, nTaps, true) * delayScale * 0.001 * sr;
  1226. gains[(std::size_t) k] = tapLevel (p, k, nTaps);
  1227. }
  1228. const auto master = (double) clamp01 (p[depth]) * (1.0 + 99.0 * (double) clamp01 (p[overMod]));
  1229. const auto detuneHz = (0.01 + 9.99 * (double) clamp01 (p[detuneRate])) * (half ? 0.5 : 1.0);
  1230. const auto vibHz = (0.05 + 19.95 * (double) clamp01 (p[vibratoRate])) * (half ? 0.5 : 1.0);
  1231. // depth in delay samples: the pitch swing stays the same at any rate (detune ±25 c, vibrato ±174 c at 100 %)
  1232. const auto detuneAmp = std::min (0.05 * sr, master * clamp01 (p[detuneDepth]) * (std::pow (2.0, 25.0 / 1200.0) - 1.0) / (twoPi * detuneHz) * sr);
  1233. const auto vibAmp = std::min (0.02 * sr, master * clamp01 (p[vibratoDepth]) * (std::pow (2.0, 174.0 / 1200.0) - 1.0) / (twoPi * vibHz) * sr);
  1234. const auto g = (float) clamp01 (p[feedback]);
  1235. const auto theta = pi * (double) clamp01 (p[rotate]);
  1236. const auto cr = (float) std::cos (theta), sn = (float) std::sin (theta);
  1237. const auto widthV = 2.0f * clamp01 (p[width]);
  1238. const auto useDiff = on (p[diffOn]);
  1239. const auto diffG = 0.7f * clamp01 (p[diffusion]);
  1240. const auto diffSizeMs = 10.0 + 490.0 * (double) clamp01 (p[diffSize]);
  1241. const auto diffRate = 0.01 + 9.99 * (double) clamp01 (p[diffModRate]);
  1242. const auto diffDepth = (double) clamp01 (p[diffModDepth]);
  1243. const auto useDrive = on (p[driveOn]), post = on (p[drivePost]);
  1244. const auto inGain = (float) std::pow (10.0, 24.0 * clamp01 (p[driveIn]) / 20.0);
  1245. const auto outGain = (float) std::pow (10.0, (-24.0 + 24.0 * clamp01 (p[driveOut])) / 20.0);
  1246. const auto noiseLevel = (float) std::pow (10.0, (-120.0 + 90.0 * clamp01 (p[noise])) / 20.0);
  1247. const auto smoothS = (1.0 + 999.0 * (double) clamp01 (p[smoothing])) * 0.001;
  1248. const auto isDark = on (p[colour]);
  1249. static constexpr double diffRatios[4] = { 0.131, 0.217, 0.293, 0.359 };
  1250. for (auto& c : ch)
  1251. {
  1252. c.lo.set (10.0 + 1990.0 * (double) clamp01 (p[lowCut]), sr);
  1253. if (p[highCut] >= 0.999f)
  1254. c.hi.open();
  1255. else
  1256. c.hi.set (100.0 + 19900.0 * (double) clamp01 (p[highCut]), sr);
  1257. if (isDark) // measured "dark": about a one-pole at 13 kHz; "bright" is flat
  1258. c.dark.set (13000.0, sr);
  1259. else
  1260. c.dark.open();
  1261. c.xover.set (10.0 + 1990.0 * (double) clamp01 (p[xover]), sr);
  1262. }
  1263. const auto driveFn = [inGain, outGain] (float x) { return outGain * std::tanh (inGain * x); };
  1264. for (int i = 0; i < n; ++i)
  1265. {
  1266. const auto scale = base.next (1.0, smoothS, sr); // glides the taps after a change (smoothing time)
  1267. vibPhase += vibHz / sr;
  1268. vibPhase -= std::floor (vibPhase);
  1269. float wet[2];
  1270. const float in[2] = { l[i], r[i] };
  1271. for (int c = 0; c < 2; ++c)
  1272. {
  1273. auto& s = ch[(std::size_t) c];
  1274. const auto sign = c == 0 ? 1.0 : -1.0;
  1275. double y = 0.0;
  1276. for (int k = 0; k < nTaps; ++k)
  1277. {
  1278. const auto det = detuneAmp > 0.0 ? detuneAmp * (double) s.detune[(std::size_t) k].next (detuneHz, sr) / 2.1 : 0.0;
  1279. const auto vib = vibAmp > 0.0 ? vibAmp * std::sin (twoPi * (vibPhase + (double) k / (double) nTaps)) : 0.0;
  1280. const auto d = tap[(std::size_t) c][(std::size_t) k] * scale + std::abs (det) + vibAmp + sign * (det + vib) * 0.5;
  1281. y += gains[(std::size_t) k] * (double) s.line.at (std::max (2.0, d));
  1282. }
  1283. auto v = (float) y;
  1284. v = s.dark.lp (s.hi.lp (s.lo.hp (v)));
  1285. if (useDrive && post)
  1286. v = driveFn (v) + noiseLevel * s.noise.next();
  1287. wet[c] = v;
  1288. }
  1289. // feedback, rotated between the channels
  1290. const auto fl = cr * wet[0] - sn * wet[1];
  1291. const auto fr = sn * wet[0] + cr * wet[1];
  1292. const float fbv[2] = { fl, fr };
  1293. for (int c = 0; c < 2; ++c)
  1294. {
  1295. auto& s = ch[(std::size_t) c];
  1296. auto x = in[c];
  1297. if (useDrive && ! post)
  1298. x = driveFn (x) + noiseLevel * s.noise.next();
  1299. if (useDiff && diffG > 0.0f)
  1300. {
  1301. const auto mod = 1.0 + 0.02 * diffDepth * (double) s.diffLfo.next (diffRate, sr) / 2.1;
  1302. for (std::size_t k = 0; k < 4; ++k)
  1303. x = s.diff[k].process (x, diffSizeMs * diffRatios[k] * mod * (c == 1 ? 1.05 : 1.0) * 0.001 * sr, diffG);
  1304. }
  1305. s.line.push (x + g * softLimit (fbv[c]));
  1306. }
  1307. // spatial crossover (the side below it is removed) and width
  1308. const auto m = 0.5f * (wet[0] + wet[1]);
  1309. auto sd = 0.5f * (wet[0] - wet[1]);
  1310. sd = ch[0].xover.hp (sd) * widthV;
  1311. l[i] = dryGain * in[0] + wetGain * (m + sd);
  1312. r[i] = dryGain * in[1] + wetGain * (m - sd);
  1313. }
  1314. }
  1315. private:
  1316. struct Channel
  1317. {
  1318. Line line;
  1319. std::array<Allpass, 4> diff;
  1320. std::array<RandomLfo, 16> detune;
  1321. RandomLfo diffLfo;
  1322. Noise noise;
  1323. OnePole lo, hi, dark, xover;
  1324. float fb = 0.0f;
  1325. };
  1326. double sr = 48000.0;
  1327. std::array<Channel, 2> ch;
  1328. double vibPhase = 0.0;
  1329. Glide base;
  1330. };
  1331. } // namespace
  1332. } // namespace lattice
  1333. // =====================================================================================================================
  1334. // registry
  1335. // =====================================================================================================================
  1336. const EffectInfo& echoformInfo()
  1337. {
  1338. static const EffectInfo info { "Echoform",
  1339. { { "Mix", 0.5f }, { "Style", 0.1f }, { "Sync L", 0.25f }, { "Note L", 0.45f }, { "Time L", 0.3f },
  1340. { "Sync R", 0.25f }, { "Note R", 0.45f }, { "Time R", 0.3f }, { "Spread", 0.5f }, { "Spacing", 0.5f },
  1341. { "Ratio", 0.6141414f }, { "Feedback", 0.35f }, { "Width", 1.0f }, { "Drive", 0.0f }, { "Age", 0.5f },
  1342. { "Diffusion", 0.0f }, { "Diffusion size", 1.0f }, { "Low cut", 0.0f }, { "High cut", 1.0f },
  1343. { "Mod rate", 0.2738341f }, { "Mod depth", 0.5f }, { "Wow", 0.5f }, { "Flutter", 0.5f },
  1344. { "Shift", 0.5f }, { "Shift detune", 0.58f }, { "Pitch", 0.5f }, { "Pitch detune", 0.5f },
  1345. { "Mode", 0.5f / 12.0f }, { "Ducking", 0.0f }, { "Output", 0.5f }, { "Pairs", 0.0f },
  1346. { "Hybrid", 0.0f } } };
  1347. return info;
  1348. }
  1349. const EffectInfo& spiralInfo()
  1350. {
  1351. static const EffectInfo info { "Spiral Echo",
  1352. { { "Mix", 0.5f }, { "Shift", 0.5f }, { "Delay", 0.5f }, { "Sync", 0.0f }, { "Feedback", 0.5f },
  1353. { "Low cut", 0.0604027f }, { "High cut", 0.3311037f }, { "Stereo", 1.0f } } };
  1354. return info;
  1355. }
  1356. const EffectInfo& orbitInfo()
  1357. {
  1358. static const EffectInfo info { "Orbit", { { "Mix", 0.5f }, { "Rate", 0.626f }, { "Depth", 0.5f }, { "Feedback", 0.5f },
  1359. { "Manual", 0.5f }, { "Mode", 1.0f / 11.0f } } };
  1360. return info;
  1361. }
  1362. const EffectInfo& latticeInfo()
  1363. {
  1364. static const EffectInfo info { "Lattice Mod",
  1365. { { "Mix", 0.5f }, { "Depth", 0.5f }, { "Width", 0.5f }, { "Delay", 0.01f }, { "Feedback", 0.0f },
  1366. { "Spread", 0.5f }, { "Slope", 0.5f }, { "Skew", 0.5f }, { "Random", 0.0f }, { "Tap gain", 0.5f },
  1367. { "Diffusion", 0.5f }, { "Diffusion size", 0.0204082f }, { "Diffusion rate", 0.049049f },
  1368. { "Diffusion depth", 0.5f }, { "Low cut", 0.0f }, { "High cut", 1.0f }, { "Crossover", 0.1457286f },
  1369. { "Detune rate", 0.049049f }, { "Detune depth", 0.5f }, { "Vibrato rate", 0.2982456f },
  1370. { "Vibrato depth", 0.0f }, { "Over-mod", 0.0f }, { "Drive in", 0.375f }, { "Drive out", 0.75f },
  1371. { "Noise", 0.3333333f }, { "Rotate", 0.5f }, { "Smoothing", 0.0990991f }, { "Colour", 0.0f },
  1372. { "Sync", 0.0f }, { "Taps", 6.5f / 24.0f }, { "Diffusion on", 0.0f }, { "Drive on", 0.0f },
  1373. { "Drive post", 1.0f }, { "Half speed", 0.0f } } };
  1374. return info;
  1375. }
  1376. bool isSuiteKind (EffectKind kind) noexcept
  1377. {
  1378. return kind == EffectKind::echoform || kind == EffectKind::spiralEcho || kind == EffectKind::orbit || kind == EffectKind::latticeMod;
  1379. }
  1380. std::unique_ptr<Effect> make (EffectKind kind)
  1381. {
  1382. switch (kind)
  1383. {
  1384. case EffectKind::echoform: return std::make_unique<echoform::Echoform>();
  1385. case EffectKind::spiralEcho: return std::make_unique<spiral::SpiralEcho>();
  1386. case EffectKind::orbit: return std::make_unique<orbit::Orbit>();
  1387. case EffectKind::latticeMod: return std::make_unique<lattice::LatticeMod>();
  1388. default: return nullptr;
  1389. }
  1390. }
  1391. std::string paramText (EffectKind kind, int param, float v)
  1392. {
  1393. v = clamp01 (v);
  1394. if (kind == EffectKind::echoform)
  1395. {
  1396. using namespace echoform;
  1397. switch (param)
  1398. {
  1399. case mix: case age: case diffSize: case modDepth: case wow: case flutter: case ducking: return pct (v * 100.0);
  1400. case style: return styleNames[(int) styleOf (v)];
  1401. case syncL: case syncR:
  1402. {
  1403. static const char* const kinds[4] = { "ms", "note", "dotted", "triplet" };
  1404. return kinds[syncKind (v)];
  1405. }
  1406. case noteL: case noteR: return noteNames[noteIndex (v)];
  1407. case timeL: case timeR: return msText (timeMs (v));
  1408. case spread: return fmt ("%+.1f ms", 20.0 * bipolar (v));
  1409. case spacing: case width: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  1410. case ratio: return fmt ("%.1f%%", 1.0 + 99.0 * v);
  1411. case feedback: return pct (200.0 * v);
  1412. case drive: return fmt ("%.1f dB", 24.0 * v);
  1413. case diffusion: return v <= 0.0f ? "off" : pct (v * 100.0);
  1414. case lowCut: return hzText (lowCutHz (v));
  1415. case highCut: return hzText (highCutHz (v));
  1416. case modRate: return fmt ("%.2f Hz", modRateHz (v));
  1417. case shift: return fmt ("%+.1f Hz", shiftHz (v));
  1418. case shiftDetune: return fmt ("%+.2f Hz", shiftDetuneHz (v));
  1419. case pitch: return fmt ("%+.2f st", pitchSemis (v));
  1420. case pitchDetune: return fmt ("%+.0f c", 100.0 * bipolar (v));
  1421. case mode: return modeName (modeOf (v));
  1422. case output: return fmt ("%+.1f dB", outputDb (v));
  1423. case pairs: return v >= 0.5f ? "pairs" : "alternate";
  1424. case hybrid: return hybridOf (v) == 0 ? "off" : (hybridOf (v) == 1 ? "clean" : "analog");
  1425. default: return fmt ("%.2f", v);
  1426. }
  1427. }
  1428. if (kind == EffectKind::spiralEcho)
  1429. {
  1430. using namespace spiral;
  1431. switch (param)
  1432. {
  1433. case mix: case feedback: return pct (v * 100.0);
  1434. case shift: return fmt ("%+.2f Hz", shiftHz (v));
  1435. case delay: return msText (delayMs (v));
  1436. case sync:
  1437. {
  1438. const auto k = syncIndex (v);
  1439. if (k == 0)
  1440. return "off";
  1441. const auto group = k <= 8 ? 0 : k <= 15 ? 1 : 2;
  1442. const auto idx = k <= 8 ? k - 1 : k <= 15 ? k - 9 : k - 16;
  1443. return std::string (noteNames[idx]) + (group == 1 ? "." : group == 2 ? "T" : "");
  1444. }
  1445. case lowCut: return hzText (lowCutHz (v));
  1446. case highCut: return hzText (highCutHz (v));
  1447. case stereo: return on (v) ? "stereo" : "mono";
  1448. default: return fmt ("%.2f", v);
  1449. }
  1450. }
  1451. if (kind == EffectKind::orbit)
  1452. {
  1453. using namespace orbit;
  1454. switch (param)
  1455. {
  1456. case mix: return pct (v * 100.0);
  1457. case rate: return fmt ("%+.2f Hz", rateHz (v));
  1458. case depth: return msText (depthMs (v));
  1459. case feedback: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  1460. case manual: return fmt ("%+.2f ms", manualMs (v));
  1461. case mode: return modeName (modeOf (v));
  1462. default: return fmt ("%.2f", v);
  1463. }
  1464. }
  1465. if (kind == EffectKind::latticeMod)
  1466. {
  1467. using namespace lattice;
  1468. switch (param)
  1469. {
  1470. case mix: case depth: case feedback: case spread: case random: case diffusion: case diffModDepth: case rotate:
  1471. case detuneDepth: case vibratoDepth: return pct (v * 100.0);
  1472. case width: return pct (v * 200.0);
  1473. case delay: return msText (delayMs (v));
  1474. case slope: case skew: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  1475. case tapGain: return fmt ("%+.1f dB", 12.0 * bipolar (v));
  1476. case diffSize: return msText (10.0 + 490.0 * v);
  1477. case diffModRate: case detuneRate: return fmt ("%.2f Hz", 0.01 + 9.99 * v);
  1478. case vibratoRate: return fmt ("%.2f Hz", 0.05 + 19.95 * v);
  1479. case lowCut: case xover: return hzText (10.0 + 1990.0 * v);
  1480. case highCut: return hzText (100.0 + 19900.0 * v);
  1481. case overMod: return fmt ("%.1fx", 1.0 + 99.0 * v);
  1482. case driveIn: return fmt ("%.1f dB", 24.0 * v);
  1483. case driveOut: return fmt ("%.1f dB", -24.0 + 24.0 * v);
  1484. case noise: return fmt ("%.1f dB", -120.0 + 90.0 * v);
  1485. case smoothing: return msText (1.0 + 999.0 * v);
  1486. case colour: return on (v) ? "dark" : "bright";
  1487. case sync:
  1488. {
  1489. static const char* const names[18] = { "off", "1/64", "1/32T", "1/64.", "1/32", "1/16T", "1/32.", "1/16", "1/8T",
  1490. "1/16.", "1/8", "1/4T", "1/8.", "1/4", "1/2T", "1/4.", "1/2", "1/1" };
  1491. return names[std::clamp ((int) std::lround (v * 17.0f), 0, 17)];
  1492. }
  1493. case taps: return fmt ("%.0f", (double) tapCount (v));
  1494. case diffOn: case driveOn: return on (v) ? "on" : "off";
  1495. case drivePost: return on (v) ? "post" : "pre";
  1496. case halfSpeed: return on (v) ? "1/2x" : "1x";
  1497. default: return fmt ("%.2f", v);
  1498. }
  1499. }
  1500. return fmt ("%.2f", v);
  1501. }
  1502. } // namespace lynx::echosuite