EchoSuite.cpp 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. #include "core/EchoSuite.h"
  2. #include <atomic>
  3. #include <algorithm>
  4. #include <array>
  5. #include <cmath>
  6. #include <complex>
  7. #include <cstdint>
  8. #include <cstdio>
  9. #include <vector>
  10. #include "core/Biquad.h"
  11. #include "core/BlockConvolver.h"
  12. namespace lynx::ppkernels
  13. {
  14. #include "core/EchoBoyKernels.inc" // round 148: EchoBoy ping-pong kernels
  15. }
  16. #include "core/Effects.h"
  17. namespace lynx::echosuite
  18. {
  19. namespace
  20. {
  21. constexpr double pi = 3.14159265358979323846;
  22. constexpr double twoPi = 2.0 * pi;
  23. float clamp01 (float v) noexcept { return std::clamp (std::isfinite (v) ? v : 0.0f, 0.0f, 1.0f); }
  24. double bipolar (float v) noexcept { return 2.0 * (double) clamp01 (v) - 1.0; }
  25. double signedPow (double x, double k) noexcept { return x < 0.0 ? -std::pow (-x, k) : std::pow (x, k); }
  26. bool on (float v) noexcept { return v >= 0.5f; }
  27. // ---- building blocks -----------------------------------------------------------------------------------------------
  28. // Circular delay line (power-of-two size). at (d) is x[n − d] for the sample about to be written (d ≥ 2), read with
  29. // 4-point cubic (Catmull-Rom / Hermite) interpolation.
  30. struct Line
  31. {
  32. std::vector<float> buf;
  33. int mask = 0;
  34. int w = 0;
  35. void allocate (double samples)
  36. {
  37. int size = 16;
  38. while ((double) size < samples + 8.0)
  39. size <<= 1;
  40. buf.assign ((std::size_t) size, 0.0f);
  41. mask = size - 1;
  42. w = 0;
  43. }
  44. void clear() noexcept
  45. {
  46. std::fill (buf.begin(), buf.end(), 0.0f);
  47. w = 0;
  48. }
  49. double maxDelay() const noexcept { return (double) mask - 4.0; }
  50. void push (float x) noexcept
  51. {
  52. buf[(std::size_t) w] = x;
  53. w = (w + 1) & mask;
  54. }
  55. float tap (int d) const noexcept { return buf[(std::size_t) ((w - d) & mask)]; } // x[n − d], d ≥ 1
  56. float at (double d) const noexcept
  57. {
  58. if (buf.empty())
  59. return 0.0f;
  60. d = std::clamp (d, 2.0, maxDelay());
  61. const auto i = (int) d;
  62. const auto t = (float) (d - (double) i);
  63. const auto xm1 = tap (i - 1), x0 = tap (i), x1 = tap (i + 1), x2 = tap (i + 2);
  64. const auto c1 = 0.5f * (x1 - xm1);
  65. const auto c2 = xm1 - 2.5f * x0 + 2.0f * x1 - 0.5f * x2;
  66. const auto c3 = 0.5f * (x2 - xm1) + 1.5f * (x0 - x1);
  67. return ((c3 * t + c2) * t + c1) * t + x0;
  68. }
  69. };
  70. // one-pole low-pass (impulse invariant); hp() = input − low-pass
  71. struct OnePole
  72. {
  73. float a = 1.0f, z = 0.0f;
  74. void set (double hz, double sr) noexcept { a = (float) (1.0 - std::exp (-twoPi * std::clamp (hz, 1.0, sr * 0.49) / sr)); }
  75. void open() noexcept { a = 1.0f; } // passes everything (the low-pass output is the input)
  76. float lp (float x) noexcept { return z += a * (x - z); }
  77. float hp (float x) noexcept { return x - lp (x); }
  78. void reset() noexcept { z = 0.0f; }
  79. };
  80. // 8th-order Butterworth low-pass (4 cookbook sections)
  81. struct Butter8
  82. {
  83. std::array<Biquad, 4> s;
  84. double hz = -1.0;
  85. void set (double f, double sr) noexcept
  86. {
  87. f = std::clamp (f, 20.0, sr * 0.45);
  88. if (std::abs (f - hz) < 0.5)
  89. return;
  90. hz = f;
  91. static constexpr double q[4] = { 0.50979558, 0.60134489, 0.89997622, 2.56291545 };
  92. const auto w0 = twoPi * f / sr, cw = std::cos (w0), sw = std::sin (w0);
  93. for (int k = 0; k < 4; ++k)
  94. {
  95. const auto alpha = sw / (2.0 * q[k]);
  96. const auto a0 = 1.0 + alpha;
  97. auto& b = s[(std::size_t) k];
  98. b.b0 = (1.0 - cw) * 0.5 / a0;
  99. b.b1 = (1.0 - cw) / a0;
  100. b.b2 = b.b0;
  101. b.a1 = -2.0 * cw / a0;
  102. b.a2 = (1.0 - alpha) / a0;
  103. }
  104. }
  105. float process (float x, int ch) noexcept
  106. {
  107. for (auto& b : s)
  108. x = b.process (x, ch);
  109. return x;
  110. }
  111. void reset() noexcept
  112. {
  113. for (auto& b : s)
  114. b.reset();
  115. }
  116. };
  117. // Saturation with unity small-signal gain: (tanh (k (x + b)) − tanh (k b)) / (k sech² (k b)). The bias b gives even
  118. // harmonics (fitted to the measured 2nd / 3rd harmonic levels of each character, REA_rodada101 §3.3).
  119. struct Shaper
  120. {
  121. float k = 0.5f, b = 0.0f, t0 = 0.0f, g = 1.0f;
  122. void set (float kk, float bb) noexcept
  123. {
  124. k = kk;
  125. b = bb;
  126. t0 = std::tanh (k * b);
  127. g = 1.0f / (k * (1.0f - t0 * t0));
  128. }
  129. float operator() (float x) const noexcept { return (std::tanh (k * (x + b)) - t0) * g; }
  130. };
  131. // xorshift noise, uniform in [−1, 1)
  132. struct Noise
  133. {
  134. std::uint32_t s = 0x9e3779b9u;
  135. float next() noexcept
  136. {
  137. s ^= s << 13;
  138. s ^= s >> 17;
  139. s ^= s << 5;
  140. return (float) ((double) s / 2147483648.0 - 1.0);
  141. }
  142. };
  143. // smooth random LFO (cosine interpolation between random points `rate` times per second), about unit rms
  144. struct RandomLfo
  145. {
  146. Noise noise;
  147. double phase = 1.0;
  148. float from = 0.0f, to = 0.0f;
  149. void seed (std::uint32_t s) noexcept
  150. {
  151. noise.s = s == 0 ? 1u : s;
  152. from = noise.next();
  153. to = noise.next();
  154. phase = 0.0;
  155. }
  156. float next (double rate, double sr) noexcept
  157. {
  158. phase += rate / sr;
  159. if (phase >= 1.0)
  160. {
  161. phase -= std::floor (phase);
  162. from = to;
  163. to = noise.next();
  164. }
  165. const auto u = (float) (0.5 - 0.5 * std::cos (pi * phase));
  166. return 2.1f * (from + (to - from) * u);
  167. }
  168. };
  169. // 90° phase-difference allpass pair (the published 8th-order IIR design, Niemitalo): re / im of the analytic signal
  170. struct Hilbert
  171. {
  172. static constexpr float a[4] = { 0.6923878f, 0.9360654322959f, 0.9882295226860f, 0.9987488452737f };
  173. static constexpr float b[4] = { 0.4021921162426f, 0.8561710882420f, 0.9722909545651f, 0.9952884791278f };
  174. std::array<float, 4> ax1 {}, ax2 {}, ay1 {}, ay2 {}, bx1 {}, bx2 {}, by1 {}, by2 {};
  175. float delayed = 0.0f;
  176. static float stage (float k, float x, float& x1, float& x2, float& y1, float& y2) noexcept
  177. {
  178. const auto y = k * k * (x + y2) - x2;
  179. x2 = x1;
  180. x1 = x;
  181. y2 = y1;
  182. y1 = y;
  183. return y;
  184. }
  185. void analytic (float x, float& re, float& im) noexcept
  186. {
  187. auto u = x, v = x;
  188. for (std::size_t k = 0; k < 4; ++k)
  189. {
  190. u = stage (a[k], u, ax1[k], ax2[k], ay1[k], ay2[k]);
  191. v = stage (b[k], v, bx1[k], bx2[k], by1[k], by2[k]);
  192. }
  193. re = delayed;
  194. im = v;
  195. delayed = u;
  196. }
  197. void reset() noexcept { *this = Hilbert {}; }
  198. };
  199. // single-sideband frequency shifter (shift in Hz, either sign)
  200. struct Shifter
  201. {
  202. Hilbert h;
  203. double phase = 0.0;
  204. float process (float x, double hz, double sr) noexcept
  205. {
  206. float up = 0.0f, down = 0.0f;
  207. both (x, hz, sr, up, down);
  208. return up;
  209. }
  210. // the input shifted by +hz and by −hz (the two sidebands of the same analytic signal)
  211. void both (float x, double hz, double sr, float& up, float& down) noexcept
  212. {
  213. float re = 0.0f, im = 0.0f;
  214. h.analytic (x, re, im);
  215. const auto c = (float) std::cos (twoPi * phase), s = (float) std::sin (twoPi * phase);
  216. phase += hz / sr;
  217. phase -= std::floor (phase);
  218. up = re * c + im * s;
  219. down = re * c - im * s;
  220. }
  221. void reset() noexcept
  222. {
  223. h.reset();
  224. phase = 0.0;
  225. }
  226. };
  227. // delay-line pitch shifter: two taps sweeping a window, crossfaded with sin² / cos² (the classic rotating-tape idea)
  228. struct PitchShifter
  229. {
  230. Line line;
  231. double phase = 0.0;
  232. double window = 2400.0;
  233. void prepare (double sr)
  234. {
  235. window = 0.05 * sr;
  236. line.allocate (window * 2.0 + 16.0);
  237. }
  238. void reset() noexcept
  239. {
  240. line.clear();
  241. phase = 0.0;
  242. }
  243. float process (float x, double ratio) noexcept
  244. {
  245. line.push (x);
  246. phase += (1.0 - ratio) / window;
  247. phase -= std::floor (phase);
  248. float y = 0.0f;
  249. for (int k = 0; k < 2; ++k)
  250. {
  251. auto p = phase + 0.5 * k;
  252. p -= std::floor (p);
  253. const auto g = (float) std::sin (pi * p);
  254. y += g * g * line.at (3.0 + p * window);
  255. }
  256. return y;
  257. }
  258. };
  259. // Schroeder allpass with a fractional delay
  260. struct Allpass
  261. {
  262. Line line;
  263. float process (float x, double d, float g) noexcept
  264. {
  265. const auto v = line.at (d);
  266. const auto u = x + g * v;
  267. line.push (u);
  268. return v - g * u;
  269. }
  270. };
  271. // first-order allpass (phaser stage)
  272. struct Ap1
  273. {
  274. float z = 0.0f;
  275. float process (float x, float a) noexcept
  276. {
  277. const auto y = a * x + z;
  278. z = x - a * y;
  279. return y;
  280. }
  281. };
  282. // smoothing of a control value toward a target (one pole, time constant in seconds)
  283. struct Glide
  284. {
  285. double value = -1.0;
  286. double next (double target, double seconds, double sr) noexcept
  287. {
  288. if (value < 0.0)
  289. value = target;
  290. const auto a = 1.0 - std::exp (-1.0 / std::max (1.0, seconds * sr));
  291. value += (target - value) * a;
  292. return value;
  293. }
  294. };
  295. float softLimit (float x) noexcept { return 2.0f * std::tanh (0.5f * x); } // unity small-signal, bounded at ±2
  296. std::string fmt (const char* f, double v)
  297. {
  298. char b[64];
  299. std::snprintf (b, sizeof (b), f, v);
  300. return b;
  301. }
  302. std::string hzText (double f) { return f >= 1000.0 ? fmt ("%.2f kHz", f / 1000.0) : fmt ("%.0f Hz", f); }
  303. std::string msText (double ms) { return ms >= 1000.0 ? fmt ("%.2f s", ms / 1000.0) : fmt (ms < 10.0 ? "%.2f ms" : "%.1f ms", ms); }
  304. std::string pct (double v) { return fmt ("%.0f%%", v); }
  305. 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" };
  306. } // namespace
  307. void equalPowerMix (float m, float& dry, float& wet) noexcept
  308. {
  309. const auto a = 0.5 * pi * (double) clamp01 (m);
  310. dry = (float) std::cos (a);
  311. wet = (float) std::sin (a);
  312. if (m <= 0.0f)
  313. wet = 0.0f;
  314. if (m >= 1.0f)
  315. dry = 0.0f;
  316. }
  317. // =====================================================================================================================
  318. // Echoform
  319. // =====================================================================================================================
  320. namespace echoform
  321. {
  322. namespace
  323. {
  324. 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 },
  325. { 0.88, 3000.0 }, { 0.94, 6000.0 }, { 1.0, 20000.0 } } };
  326. } // namespace
  327. double timeMs (float v) noexcept
  328. {
  329. const auto x = (double) clamp01 (v);
  330. for (std::size_t i = 1; i < timeLaw.size(); ++i)
  331. if (x <= timeLaw[i].first)
  332. {
  333. const auto [x0, y0] = timeLaw[i - 1];
  334. const auto [x1, y1] = timeLaw[i];
  335. return y0 + (y1 - y0) * (x - x0) / (x1 - x0);
  336. }
  337. return 20000.0;
  338. }
  339. float timeValue (double ms) noexcept
  340. {
  341. ms = std::clamp (ms, 0.0, 20000.0);
  342. for (std::size_t i = 1; i < timeLaw.size(); ++i)
  343. if (ms <= timeLaw[i].second)
  344. {
  345. const auto [x0, y0] = timeLaw[i - 1];
  346. const auto [x1, y1] = timeLaw[i];
  347. return (float) (x0 + (x1 - x0) * (ms - y0) / (y1 - y0));
  348. }
  349. return 1.0f;
  350. }
  351. int syncKind (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 4.0f + 1e-4f) - 1, 0, 3); }
  352. int noteIndex (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 10.0f + 1e-4f), 0, 9); }
  353. double noteBeats (int index, int kind) noexcept
  354. {
  355. 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
  356. return kind == 2 ? beats * 1.5 : kind == 3 ? beats * 2.0 / 3.0 : beats;
  357. }
  358. double sideMs (const EffectParams& p, bool right, double bpm) noexcept
  359. {
  360. const auto kind = syncKind (p[right ? syncR : syncL]);
  361. if (kind == 0)
  362. return timeMs (p[right ? timeR : timeL]);
  363. bpm = tempoFor (p, bpm);
  364. return std::min (20000.0, noteBeats (noteIndex (p[right ? noteR : noteL]), kind) * 60000.0 / bpm);
  365. }
  366. double bpmOf (float v) noexcept { return 30.0 + 270.0 * (double) clamp01 (v); }
  367. float bpmValue (double bpm) noexcept { return (float) std::clamp ((bpm - 30.0) / 270.0, 0.0, 1.0); }
  368. // round 137: Rhythm mode. Measured black-box on EchoBoy (REA_rodada137 §2, impulses at 60 and 120 BPM): `repeats`
  369. // evenly spaced slots (slot 0 = the input itself, not heard on the first pass), levels falling by `decay` dB from the
  370. // first slot to the last one that fits, equal-power normalised over those slots (1 / √slots with no decay); the
  371. // pattern spans 2 beats whatever the spacing (taps past it are dropped) and the feedback replays it every 2 beats.
  372. int eraOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * 2.0f), 0, 2); }
  373. // round 137 [EXEC] ValhallaDelay demo, first repeat of an impulse (300 ms, no feedback / modulation), Past → Present
  374. // → Future: Tape 8 kHz −10.1 / −7.1 / −6.5 dB, Digital 12 kHz −44.9 / −6.6 / −6.3, LoFi 8 kHz −10.0 / −5.3 / −1.8,
  375. // BBD 4 kHz −27 / −17 / −8, Analog 8 kHz rises by 13 / 24 dB; HiFi and Clarity unchanged. Fitted as cut-off factors
  376. // on the mode's loop low-pass (the 2-pole tape roll-off, the steep BBD / analog / digital filters).
  377. double eraBandwidth (Mode m, int e) noexcept
  378. {
  379. e = std::clamp (e, 0, 2);
  380. if (e == 0)
  381. return 1.0;
  382. switch (m)
  383. {
  384. case Mode::tape: return e == 1 ? 1.13 : 1.27;
  385. case Mode::lofi: return e == 1 ? 1.45 : 2.2;
  386. case Mode::bucket: return e == 1 ? 1.25 : 1.6;
  387. case Mode::analog: return e == 1 ? 1.45 : 2.0;
  388. case Mode::digital: return e == 1 ? 1.65 : 1.8;
  389. default: return 1.0;
  390. }
  391. }
  392. int rhythmRepeatsOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * 15.0f), 0, 15) + 1; }
  393. double rhythmDecayDb (float v) noexcept { return 40.0 * (double) clamp01 (v); }
  394. int rhythmBeatsOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * 7.0f), 0, 7) + 1; }
  395. double rhythmSpacingMs (const EffectParams& p, double bpm) noexcept
  396. {
  397. const auto kind = syncKind (p[rhythmSync]);
  398. if (kind == 0)
  399. return std::max (1.0, timeMs (p[rhythmTime]));
  400. return std::max (1.0, noteBeats (noteIndex (p[rhythmNote]), kind) * 60000.0 / tempoFor (p, bpm));
  401. }
  402. double rhythmLengthMs (const EffectParams& p, double bpm) noexcept
  403. {
  404. return std::min (19000.0, (double) rhythmBeatsOf (p[rhythmLength]) * 60000.0 / tempoFor (p, bpm));
  405. }
  406. double inputLevelDb (float v) noexcept { return 48.0 * ((double) clamp01 (v) - 0.5); }
  407. double loopEqDb (float v) noexcept { return 36.0 * ((double) clamp01 (v) - 0.5); }
  408. int modShapeOf (float v) noexcept { return std::clamp ((int) (clamp01 (v) * 5.0f), 0, 4); }
  409. int modSyncOf (float v) noexcept { return std::clamp ((int) (clamp01 (v) * 7.0f), 0, 6); }
  410. double modSyncBeats (int index) noexcept
  411. {
  412. static constexpr double beats[7] = { 0.0, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0 };
  413. return beats[std::clamp (index, 0, 6)];
  414. }
  415. // round 144: the rhythm editor's laws (LYNX's own [model]; Even keeps the measured round-137 law)
  416. RhythmEdit rhythmEditOf (float v) noexcept
  417. {
  418. v = clamp01 (v);
  419. return v < 1.0f / 3.0f ? RhythmEdit::even : (v < 2.0f / 3.0f ? RhythmEdit::free : RhythmEdit::grid);
  420. }
  421. int rhythmGridOf (float v) noexcept { return std::clamp ((int) (clamp01 (v) * 6.0f), 0, 5); }
  422. double rhythmGridBeats (int index) noexcept { return 2.0 / (double) (1 << std::clamp (index, 0, 5)); }
  423. RhythmShape rhythmShapeOf (float v) noexcept { return (RhythmShape) std::clamp ((int) (clamp01 (v) * 5.0f), 0, 4); }
  424. double rhythmShapeDb (RhythmShape shape, double decayDb, double u) noexcept
  425. {
  426. u = std::clamp (u, 0.0, 1.0);
  427. switch (shape)
  428. {
  429. case RhythmShape::reverse: return -decayDb * (1.0 - u); // the last slot loudest
  430. case RhythmShape::swell: return -decayDb * std::abs (1.0 - 2.0 * u); // quiet ends, the middle loudest
  431. case RhythmShape::fade: // a straight fade in level (not in dB) down to −decay at the last slot
  432. {
  433. const auto end = std::pow (10.0, -decayDb / 20.0);
  434. return 20.0 * std::log10 (std::max (1.0e-6, 1.0 - (1.0 - end) * u));
  435. }
  436. case RhythmShape::nonlin: return -decayDb * u * u * u; // holds, then falls steeply
  437. case RhythmShape::decay: default: return -decayDb * u; // the measured law
  438. }
  439. }
  440. double rhythmTapMs (const EffectParams& p, int k, double bpm) noexcept
  441. {
  442. const auto length = rhythmLengthMs (p, bpm);
  443. const auto beatMs = 60000.0 / tempoFor (p, bpm);
  444. auto ms = (double) clamp01 (p[rhythmTime1 + std::clamp (k, 0, 14)]) * length;
  445. if (rhythmEditOf (p[rhythmEdit]) == RhythmEdit::grid)
  446. {
  447. const auto step = rhythmGridBeats (rhythmGridOf (p[rhythmGrid])) * beatMs;
  448. ms = std::round (ms / step) * step;
  449. if (ms >= length - 0.5) // the pattern end is where it starts again: the last grid line before it
  450. ms = std::floor ((length - 0.5) / step) * step;
  451. if (ms < 0.5)
  452. ms = std::min (step, std::floor ((length - 0.5) / step) * step);
  453. }
  454. return std::clamp (ms, 1.0, std::max (1.0, length - 1.0));
  455. }
  456. float rhythmTimeValue (const EffectParams& p, double ms, double bpm) noexcept
  457. {
  458. const auto length = rhythmLengthMs (p, bpm);
  459. if (rhythmEditOf (p[rhythmEdit]) == RhythmEdit::grid)
  460. {
  461. const auto step = rhythmGridBeats (rhythmGridOf (p[rhythmGrid])) * 60000.0 / tempoFor (p, bpm);
  462. ms = std::max (step, std::round (ms / step) * step);
  463. }
  464. return clamp01 ((float) (ms / std::max (1.0, length)));
  465. }
  466. // the pattern without allocating (the audio thread): up to 15 heard taps, returns their count
  467. int rhythmPattern (const EffectParams& p, double bpm, std::array<RhythmTap, 16>& taps, double& slot0) noexcept
  468. {
  469. const auto spacing = rhythmSpacingMs (p, bpm), length = rhythmLengthMs (p, bpm);
  470. const auto edit = rhythmEditOf (p[rhythmEdit]);
  471. // Even: the slots that fit in the length (measured); Free / Grid: every tap sits inside the length by construction
  472. const auto slots = edit == RhythmEdit::even
  473. ? std::clamp (std::min (rhythmRepeatsOf (p[rhythmRepeats]), (int) std::floor (length / spacing + 1e-6) + 1), 1, 16)
  474. : rhythmRepeatsOf (p[rhythmRepeats]);
  475. const auto decay = rhythmDecayDb (p[rhythmDecay]);
  476. const auto shape = rhythmShapeOf (p[rhythmShape]);
  477. std::array<double, 16> g {};
  478. double power = 0.0;
  479. for (int k = 0; k < slots; ++k)
  480. {
  481. g[(std::size_t) k] = slots > 1 ? std::pow (10.0, rhythmShapeDb (shape, decay, (double) k / (double) (slots - 1)) / 20.0) : 1.0;
  482. power += g[(std::size_t) k] * g[(std::size_t) k];
  483. }
  484. const auto norm = 1.0 / std::sqrt (power);
  485. slot0 = g[0] * norm;
  486. for (int k = 1; k < slots; ++k)
  487. taps[(std::size_t) (k - 1)] = { edit == RhythmEdit::even ? spacing * (double) k : rhythmTapMs (p, k - 1, bpm),
  488. g[(std::size_t) k] * norm * (double) clamp01 (p[rhythmTap1 + k - 1]) };
  489. return slots - 1;
  490. }
  491. namespace
  492. {
  493. #ifndef TR_NOOP
  494. #define TR_NOOP(text) text // marks a literal for translation (tools/i18n_keys.py scans core too)
  495. #endif
  496. // LYNX's own tap patterns (positions in grid steps, levels 0 … 1); written for Echoform, not taken from any product
  497. struct RhythmPreset
  498. {
  499. const char* name;
  500. int beats; // pattern length
  501. int grid; // 0 … 5 = 1/2 … 1/64; −1 = Free times given in beats
  502. std::array<double, 15> at;
  503. std::array<float, 15> level;
  504. int count;
  505. };
  506. constexpr float F = 1.0f;
  507. const RhythmPreset rhythmPresets[] {
  508. { TR_NOOP ("Gallop"), 2, 3, { 2, 3, 6, 7 }, { 0.8f, F, 0.8f, F }, 4 },
  509. { TR_NOOP ("Offbeat eighths"), 2, 2, { 1, 3 }, { F, F }, 2 },
  510. { TR_NOOP ("Dotted bounce"), 2, 3, { 3, 6 }, { F, 0.75f }, 2 },
  511. { TR_NOOP ("Tresillo"), 4, 2, { 3, 6 }, { F, F }, 2 },
  512. { TR_NOOP ("Son clave 3-2"), 8, 2, { 3, 6, 10, 12 }, { F, F, 0.85f, 0.85f }, 4 },
  513. { TR_NOOP ("Rumba clave"), 8, 2, { 3, 7, 10, 12 }, { F, F, 0.85f, 0.85f }, 4 },
  514. { TR_NOOP ("Ghost sixteenths"), 2, 3, { 1, 2, 3, 5, 6, 7 }, { 0.35f, F, 0.35f, 0.35f, F, 0.35f }, 6 },
  515. { TR_NOOP ("Stutter"), 1, 4, { 1, 2, 3, 4, 5, 6 }, { F, 0.9f, 0.8f, 0.7f, 0.6f, 0.5f }, 6 },
  516. { TR_NOOP ("Build-up"), 2, 3, { 4, 6, 7 }, { 0.45f, 0.7f, F }, 3 },
  517. { TR_NOOP ("Triplet roll"), 2, -1, { 1.0 / 3.0, 2.0 / 3.0, 1.0, 4.0 / 3.0, 5.0 / 3.0 }, { F, 0.85f, 0.7f, 0.85f, F }, 5 },
  518. { TR_NOOP ("Swing eighths"), 2, -1, { 2.0 / 3.0, 1.0, 5.0 / 3.0 }, { 0.7f, F, 0.7f }, 3 },
  519. { TR_NOOP ("Long and short"), 8, 2, { 3, 4, 11, 12 }, { F, 0.5f, F, 0.5f }, 4 },
  520. };
  521. } // namespace
  522. int rhythmPresetCount() noexcept { return (int) std::size (rhythmPresets); }
  523. const char* rhythmPresetName (int index) noexcept
  524. {
  525. return index >= 0 && index < rhythmPresetCount() ? rhythmPresets[index].name : "";
  526. }
  527. std::vector<std::pair<int, float>> rhythmPresetEdits (int index)
  528. {
  529. std::vector<std::pair<int, float>> e;
  530. if (index < 0 || index >= rhythmPresetCount())
  531. return e;
  532. const auto& r = rhythmPresets[index];
  533. const auto grid = r.grid < 0 ? 3 : r.grid;
  534. e.push_back ({ rhythm, 1.0f });
  535. e.push_back ({ rhythmEdit, r.grid < 0 ? 0.5f : 1.0f });
  536. e.push_back ({ rhythmGrid, ((float) grid + 0.5f) / 6.0f });
  537. e.push_back ({ rhythmLength, (float) (r.beats - 1) / 7.0f });
  538. e.push_back ({ rhythmRepeats, (float) r.count / 15.0f }); // count taps = count + 1 slots
  539. for (int k = 0; k < 15; ++k)
  540. {
  541. const auto beatsAt = k < r.count ? (r.grid < 0 ? r.at[(std::size_t) k] : r.at[(std::size_t) k] * rhythmGridBeats (r.grid))
  542. : (double) r.beats * (double) (k + 1) / 16.0;
  543. e.push_back ({ rhythmTime1 + k, clamp01 ((float) (beatsAt / (double) r.beats)) });
  544. e.push_back ({ rhythmTap1 + k, k < r.count ? r.level[(std::size_t) k] : 1.0f });
  545. }
  546. return e;
  547. }
  548. std::vector<std::pair<int, float>> rhythmFreeFromEven (const EffectParams& p, double bpm)
  549. {
  550. std::vector<std::pair<int, float>> e;
  551. const auto length = rhythmLengthMs (p, bpm);
  552. const auto spacing = rhythmSpacingMs (p, bpm);
  553. const auto heard = (int) rhythmTaps (p, bpm).size();
  554. e.push_back ({ rhythmEdit, 0.5f });
  555. e.push_back ({ rhythmRepeats, (float) heard / 15.0f });
  556. for (int k = 0; k < heard; ++k)
  557. e.push_back ({ rhythmTime1 + k, clamp01 ((float) (spacing * (double) (k + 1) / std::max (1.0, length))) });
  558. return e;
  559. }
  560. std::vector<RhythmTap> rhythmTaps (const EffectParams& p, double bpm, double* slot0)
  561. {
  562. std::array<RhythmTap, 16> taps {};
  563. double first = 0.0;
  564. const auto n = rhythmPattern (p, bpm, taps, first);
  565. if (slot0 != nullptr)
  566. *slot0 = first;
  567. return { taps.begin(), taps.begin() + n };
  568. }
  569. double tempoFor (const EffectParams& p, double projectBpm) noexcept
  570. {
  571. if (p[tempoSource] >= 0.5f)
  572. return bpmOf (p[ownBpm]);
  573. return projectBpm > 1.0 ? projectBpm : 120.0;
  574. }
  575. std::vector<std::pair<int, float>> linkedEdit (const EffectParams& p, int param, float value)
  576. {
  577. std::vector<std::pair<int, float>> out { { param, value } };
  578. if (p[filterLink] < 0.5f || (param != lowCut && param != highCut))
  579. return out;
  580. const auto hzOf = [] (int which, float v) { return which == lowCut ? lowCutHz (v) : highCutHz (v); };
  581. const auto ratio = hzOf (param, value) / std::max (1.0, hzOf (param, p[(std::size_t) param]));
  582. const auto other = param == lowCut ? highCut : lowCut;
  583. const auto hz = hzOf (other, p[(std::size_t) other]) * ratio;
  584. const auto v = other == lowCut ? (hz - 10.0) / 1990.0 : (hz - 200.0) / 19800.0;
  585. out.push_back ({ other, (float) std::clamp (v, 0.0, 1.0) });
  586. return out;
  587. }
  588. int nearestPrime (int samples) noexcept
  589. {
  590. const auto isPrime = [] (int n) {
  591. if (n < 2) return false;
  592. if (n % 2 == 0) return n == 2;
  593. for (int k = 3; k * k <= n; k += 2)
  594. if (n % k == 0) return false;
  595. return true;
  596. };
  597. samples = std::max (2, samples);
  598. for (int d = 0;; ++d)
  599. {
  600. if (isPrime (samples - d)) return samples - d;
  601. if (isPrime (samples + d)) return samples + d;
  602. }
  603. }
  604. double feedbackGain (float v) noexcept { return 2.0 * clamp01 (v); }
  605. double modRateHz (float v) noexcept { return 0.01 + 9.99 * std::pow ((double) clamp01 (v), 2.3277); }
  606. double shiftHz (float v) noexcept { return 1000.0 * signedPow (bipolar (v), 5.0); }
  607. double shiftDetuneHz (float v) noexcept { return 10.0 * signedPow (bipolar (v), 2.5); }
  608. double pitchSemis (float v) noexcept { return 24.0 * bipolar (v); }
  609. double lowCutHz (float v) noexcept { return 10.0 + 1990.0 * clamp01 (v); }
  610. double highCutHz (float v) noexcept { return 200.0 + 19800.0 * clamp01 (v); }
  611. double outputDb (float v) noexcept { return -18.0 + 36.0 * clamp01 (v); }
  612. int hybridOf (float v) noexcept { return v < 0.25f ? 0 : (v < 0.75f ? 1 : 2); }
  613. int tapeVoicingOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * tapeVoicingCount), 0, tapeVoicingCount); }
  614. float tapeVoicingValue (int k) noexcept { return (float) std::clamp (k, 0, tapeVoicingCount) / (float) tapeVoicingCount; }
  615. const char* tapeVoicingName (int k) noexcept
  616. {
  617. static const char* names[tapeVoicingCount + 1] { "off", "ping-pong tape", "studio tape", "studio tape (hot)" };
  618. return names[std::clamp (k, 0, tapeVoicingCount)];
  619. }
  620. float hybridValue (int h) noexcept { return h <= 0 ? 0.0f : (h == 1 ? 0.5f : 1.0f); }
  621. // ---- round 147: the echo voicings, the saturation curves and Multiply (REA_rodada147a) ---------------------------
  622. // [EXEC] each voicing is one of EchoBoy's 31 factory styles measured black-box on the official trial (Single, 250 ms,
  623. // mix 100 %, its Saturation at 0 / 6 / 12 / 18 / 24): the first echo's tone and the tone of every further pass (white
  624. // noise bursts, 1/3-octave power ratios: rep 1 = output tone × loop tone, rep 2 / rep 1 = feedback × loop tone),
  625. // the steady-sine gain / 2nd / 3rd harmonic over −40 … 0 dBFS, the pitch wobble of a steady 1 kHz echo and the
  626. // impulse's spread. LYNX's model (our own code): write path = saturation → loop tone, read path = output tone; the
  627. // tones are RBJ biquads fitted to the measured curves (≤ 0.3 dB rms), the saturation either a knee shaper
  628. // x / (1 + |x|^p)^(1/p) with a bias b (even harmonics) between a drive and a gain, or a compressor (the dynamic
  629. // styles: ratio, threshold, make-up per Saturation step; attack 4 ms, release 120 ms from level steps), linear
  630. // in dB between the measured Saturation steps; the wobble a sine (or smooth random) delay modulation of the measured
  631. // depth (cents rms) and rate.
  632. namespace
  633. {
  634. struct VoicingData
  635. {
  636. const char* name;
  637. float outGain; // dB
  638. int outHp, outLp; // cascaded 2nd-order high / low passes (0 … 2 / 0 … 4)
  639. float hpHz, hpQ, lpHz, lpQ;
  640. float pkHz[3], pkDb[3], pkQ[3];
  641. float lsHz, lsDb, lpkHz, lpkDb, lpkQ; // the loop tone: a low shelf, a bell
  642. int loopLp; // and 0 … 2 low passes
  643. float loopLpHz, loopLpQ;
  644. int comp; // 0 = the shaper, 1 = the compressor
  645. float p, b, driveDb[5], gainDb[5];
  646. float ratio, thresholdDb[5], makeupDb[5];
  647. float wobbleCents, wobbleHz;
  648. int wobbleRandom; // 0 = sine, 1 = smooth random, 2 = triangle (round 150)
  649. float wobblePhase; // the right side's LFO phase offset (turns)
  650. float diffusion; // 0 … 1, Echoform's diffusion amount
  651. };
  652. const VoicingData voicingTable[voicingCount] {
  653. #include "core/EchoVoicings.inc"
  654. };
  655. // the saturation types: the measured curve of a representative style (clean digital, studio tape, valve tape,
  656. // broadcast FM's compressor, fuzzed, squashed) — LYNX names
  657. constexpr int satTypeSource[satTypeCount] { 15, 1, 4, 12, 25, 24 };
  658. const VoicingData& voicingData (int k) noexcept { return voicingTable[std::clamp (k - 1, 0, voicingCount - 1)]; }
  659. const VoicingData& satTypeData (int type) noexcept { return voicingTable[satTypeSource[std::clamp (type - 1, 0, satTypeCount - 1)]]; }
  660. // a value of the five-step table (Saturation 0, 6, 12, 18, 24 dB), linear in between
  661. double satStep (const float* table, double driveDb) noexcept
  662. {
  663. const auto t = std::clamp (driveDb, 0.0, 24.0) / 6.0;
  664. const auto i = std::min (3, (int) t);
  665. return (double) table[i] + ((double) table[i + 1] - (double) table[i]) * (t - (double) i);
  666. }
  667. double kneeShape (double u, double p) noexcept { return u / std::pow (1.0 + std::pow (std::abs (u), p), 1.0 / p); }
  668. // the shaper with unity slope at 0: (f (u + b) − f (b)) / f′(b), f′(u) = (1 + |u|^p)^(−1/p − 1)
  669. double biasedShape (double u, double p, double b) noexcept
  670. {
  671. const auto slope = std::pow (1.0 + std::pow (std::abs (b), p), -1.0 / p - 1.0);
  672. return (kneeShape (u + b, p) - kneeShape (b, p)) / slope;
  673. }
  674. // the compressor's static curve (dB of gain reduction for `x` dB above the threshold; 6 dB soft knee)
  675. double compressCurve (double x, double ratio) noexcept
  676. {
  677. const auto s = 1.0 - 1.0 / std::max (1.0, ratio);
  678. if (x < -3.0) return 0.0;
  679. if (x > 3.0) return s * x;
  680. return s * (x + 3.0) * (x + 3.0) / 12.0;
  681. }
  682. // the RBJ magnitude (dB) of one biquad at w
  683. double biquadDb (const Biquad& q, double w) noexcept
  684. {
  685. const std::complex<double> z1 = std::polar (1.0, -w), z2 = z1 * z1;
  686. const auto num = q.b0 + q.b1 * z1 + q.b2 * z2, den = 1.0 + q.a1 * z1 + q.a2 * z2;
  687. return 20.0 * std::log10 (std::max (1e-12, std::abs (num / den)));
  688. }
  689. // the voicing's tones as biquads (out: up to 2 HP + 4 LP + 3 bells; loop: shelf, bell, up to 2 LP)
  690. int designOutTone (const VoicingData& v, double sr, Biquad* q) noexcept
  691. {
  692. int n = 0;
  693. const auto lim = [sr] (double hz) { return std::clamp (hz, 10.0, 0.45 * sr); };
  694. for (int i = 0; i < v.outHp; ++i)
  695. q[n++].filter (sr, 1, lim (v.hpHz), v.hpQ);
  696. if (v.lpHz < 0.45 * sr)
  697. for (int i = 0; i < v.outLp; ++i)
  698. q[n++].filter (sr, 0, lim (v.lpHz), v.lpQ);
  699. for (int i = 0; i < 3; ++i)
  700. if (std::abs (v.pkDb[i]) > 0.01f && v.pkHz[i] < 0.45 * sr)
  701. q[n++].peaking (sr, lim (v.pkHz[i]), v.pkQ[i], v.pkDb[i]);
  702. return n;
  703. }
  704. int designLoopTone (const VoicingData& v, double sr, Biquad* q) noexcept
  705. {
  706. int n = 0;
  707. const auto lim = [sr] (double hz) { return std::clamp (hz, 10.0, 0.45 * sr); };
  708. if (std::abs (v.lsDb) > 0.01f)
  709. q[n++].shelf (sr, lim (v.lsHz), v.lsDb, false);
  710. if (std::abs (v.lpkDb) > 0.01f && v.lpkHz < 0.45 * sr)
  711. q[n++].peaking (sr, lim (v.lpkHz), v.lpkQ, v.lpkDb);
  712. if (v.loopLpHz < 0.45 * sr)
  713. for (int i = 0; i < v.loopLp; ++i)
  714. q[n++].filter (sr, 0, lim (v.loopLpHz), v.loopLpQ);
  715. return n;
  716. }
  717. } // namespace
  718. int voicingOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * voicingCount), 0, voicingCount); }
  719. float voicingValue (int k) noexcept { return (float) std::clamp (k, 0, voicingCount) / (float) voicingCount; }
  720. const char* voicingName (int k) noexcept { return k <= 0 ? "off" : voicingData (k).name; }
  721. int voicingForEchoBoyStyle (int style) noexcept { return style >= 0 && style < voicingCount ? style + 1 : 0; }
  722. int satTypeOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * satTypeCount), 0, satTypeCount); }
  723. float satTypeValue (int k) noexcept { return (float) std::clamp (k, 0, satTypeCount) / (float) satTypeCount; }
  724. const char* satTypeName (int k) noexcept
  725. {
  726. static const char* const names[satTypeCount + 1] { "auto", "clean", "tape", "tube", "squash", "grit", "ceiling" };
  727. return names[std::clamp (k, 0, satTypeCount)];
  728. }
  729. int multiplyOf (float v) noexcept { return 1 << std::clamp ((int) std::lround (clamp01 (v) * 3.0f), 0, 3); }
  730. float multiplyValue (int factor) noexcept
  731. {
  732. int k = 0;
  733. while (k < 3 && (1 << k) < factor)
  734. ++k;
  735. return (float) k / 3.0f;
  736. }
  737. // [EXEC] PrimalTap 5.5.5: Multiply 2x / 4x / 8x doubles / quadruples / octuples the delay and leaves a brick-wall
  738. // band of ≈ 6.2 / 3.1 / 1.55 kHz (−1 … −2 dB just below, −43 … −49 dB a third of an octave above)
  739. double multiplyCutoffHz (int factor) noexcept { return factor <= 1 ? 0.0 : 12400.0 / (double) factor; }
  740. // round 150 [EXEC]: EchoBoy's L/R offset in Single and Dual, measured at its default 8 ms with every style and with
  741. // 100 / 250 / 500 ms: the left side's odd repeats at n · T − 4 ms, its even ones at n · T + 6 ms; the right side +4 /
  742. // −6 ms (a left-only input leaves the right side silent: no cross-feed). Other offsets: the same law scaled [model].
  743. double lrOffsetMs (float v) noexcept { return 25.0 * (double) clamp01 (v); }
  744. float lrOffsetValue (double ms) noexcept { return (float) std::clamp (ms / 25.0, 0.0, 1.0); }
  745. double lrOddMs (double offsetMs, bool right) noexcept { return (right ? 0.5 : -0.5) * offsetMs; }
  746. double lrEvenMs (double offsetMs, bool right) noexcept { return (right ? -0.75 : 0.75) * offsetMs; }
  747. namespace
  748. {
  749. // round 150: the ping-pong pair tone per voicing (EchoBoy's Ping-Pong, measured black-box: a left-right pair's tone
  750. // beyond the loop tone of Single, white-noise bursts at −30 dBFS, feedback 0.5; fitted, tmp/parity150/pya/fitpp.py)
  751. struct PairToneData
  752. {
  753. float gainDb;
  754. int hp; // 0 … 2 second-order high passes
  755. float hpHz, hpQ;
  756. int lp; // 0 … 2 second-order low passes
  757. float lpHz, lpQ;
  758. float pkHz[2], pkDb[2], pkQ[2];
  759. };
  760. const PairToneData pairToneTable[voicingCount] {
  761. #include "core/EchoPairTones.inc"
  762. };
  763. int designPairTone (int k, double sr, Biquad* q) noexcept
  764. {
  765. const auto& v = pairToneTable[std::clamp (k - 1, 0, voicingCount - 1)];
  766. int n = 0;
  767. const auto lim = [sr] (double hz) { return std::clamp (hz, 10.0, 0.45 * sr); };
  768. for (int i = 0; i < v.hp; ++i)
  769. q[n++].filter (sr, 1, lim (v.hpHz), v.hpQ);
  770. if (v.lpHz < 0.45 * sr)
  771. for (int i = 0; i < v.lp; ++i)
  772. q[n++].filter (sr, 0, lim (v.lpHz), v.lpQ);
  773. for (int i = 0; i < 2; ++i)
  774. if (std::abs (v.pkDb[i]) > 0.01f && v.pkHz[i] < 0.45 * sr)
  775. q[n++].peaking (sr, lim (v.pkHz[i]), v.pkQ[i], v.pkDb[i]);
  776. return n;
  777. }
  778. float pairToneGain (int k) noexcept { return (float) std::pow (10.0, pairToneTable[std::clamp (k - 1, 0, voicingCount - 1)].gainDb / 20.0); }
  779. void copyCoefficients (Biquad& to, const Biquad& from) noexcept
  780. {
  781. to.b0 = from.b0; to.b1 = from.b1; to.b2 = from.b2; to.a1 = from.a1; to.a2 = from.a2;
  782. }
  783. } // namespace
  784. double voicingPairToneDb (int k, double hz, double sampleRate)
  785. {
  786. if (k <= 0)
  787. return 0.0;
  788. std::array<Biquad, 6> q {};
  789. const int n = designPairTone (k, sampleRate, q.data());
  790. auto db = 20.0 * std::log10 ((double) pairToneGain (k));
  791. for (int i = 0; i < n; ++i)
  792. db += biquadDb (q[(std::size_t) i], twoPi * hz / sampleRate);
  793. return db;
  794. }
  795. double voicingToneDb (int k, bool loop, double hz, double sampleRate)
  796. {
  797. if (k <= 0)
  798. return 0.0;
  799. const auto& v = voicingData (k);
  800. std::array<Biquad, 9> q {};
  801. const int n = loop ? designLoopTone (v, sampleRate, q.data()) : designOutTone (v, sampleRate, q.data());
  802. const auto w = twoPi * hz / sampleRate;
  803. double db = loop ? 0.0 : (double) v.outGain;
  804. for (int i = 0; i < n; ++i)
  805. db += biquadDb (q[(std::size_t) i], w);
  806. return db;
  807. }
  808. double satGainDb (int k, int type, double driveDb, double levelDb)
  809. {
  810. const auto& v = type > 0 ? satTypeData (type) : voicingData (k);
  811. if (v.comp != 0)
  812. return satStep (v.makeupDb, driveDb) - compressCurve (levelDb - satStep (v.thresholdDb, driveDb), v.ratio);
  813. // the fundamental of the shaped sine (64 points)
  814. const auto d = std::pow (10.0, satStep (v.driveDb, driveDb) / 20.0), a = std::pow (10.0, levelDb / 20.0);
  815. double re = 0.0, im = 0.0;
  816. for (int i = 0; i < 64; ++i)
  817. {
  818. const auto ph = twoPi * i / 64.0;
  819. const auto y = biasedShape (a * d * std::sin (ph), (double) v.p, (double) v.b);
  820. re += y * std::sin (ph);
  821. im += y * std::cos (ph);
  822. }
  823. const auto fund = 2.0 * std::sqrt (re * re + im * im) / 64.0;
  824. return satStep (v.gainDb, driveDb) + 20.0 * std::log10 (std::max (1e-12, fund / (a * d)));
  825. }
  826. Mode modeOf (float v) noexcept { return (Mode) std::clamp ((int) std::floor (clamp01 (v) * (float) modeCount), 0, modeCount - 1); }
  827. float modeValue (Mode m) noexcept { return ((float) m + 0.5f) / (float) modeCount; }
  828. Style styleOf (float v) noexcept { return (Style) std::clamp ((int) std::floor (clamp01 (v) * (float) styleCount), 0, styleCount - 1); }
  829. float styleValue (Style s) noexcept { return ((float) s + 0.5f) / (float) styleCount; }
  830. const char* modeName (Mode m) noexcept
  831. {
  832. static const char* const names[modeCount] = { "Tape", "Lo-Fi", "Hi-Fi tape", "Bucket brigade", "Analog", "Digital", "Clean",
  833. "Phaser", "Shifter", "Pitch", "Reverse", "Detune" };
  834. return names[std::clamp ((int) m, 0, modeCount - 1)];
  835. }
  836. namespace
  837. {
  838. const char* const styleNames[styleCount] = { "Single", "Ratio", "Quad", "Dual", "Ping-pong" };
  839. class Echoform final : public Effect
  840. {
  841. public:
  842. void prepare (double rate) noexcept override
  843. {
  844. sr = rate;
  845. const auto lineSamples = sr * 20.6 + 4096.0; // 20 s + modulation
  846. for (auto& c : ch)
  847. {
  848. c.line.allocate (lineSamples);
  849. c.pitch.prepare (sr);
  850. for (auto& a : c.diff)
  851. a.line.allocate (sr * 0.05);
  852. for (auto& a : c.diffOut) // round 144: the diffusion after the loop
  853. a.line.allocate (sr * 0.05);
  854. }
  855. for (auto& b : bpath) // round 150: the second loop of the L/R offset
  856. {
  857. b.line.allocate (lineSamples);
  858. for (auto& a : b.diff)
  859. a.line.allocate (sr * 0.05);
  860. }
  861. reset();
  862. }
  863. void reset() noexcept override
  864. {
  865. std::uint32_t seed = 0x1234567u;
  866. for (auto& c : ch)
  867. {
  868. c.line.clear();
  869. c.pitch.reset();
  870. c.shifter.reset();
  871. for (auto& a : c.diff)
  872. a.line.clear();
  873. for (auto& a : c.diffOut)
  874. a.line.clear();
  875. for (auto& a : c.phaser)
  876. a = {};
  877. c.shelf.reset();
  878. c.tapeLp1.reset();
  879. c.tapeLp2.reset();
  880. c.hifiLp.reset();
  881. c.hifiHp.reset();
  882. c.lowCut.reset();
  883. c.highCut.reset();
  884. c.held = 0.0f;
  885. c.holdPhase = 0.0;
  886. c.time = {};
  887. c.wow.seed (seed += 0x9e3779b9u);
  888. c.flutter1.seed (seed += 0x9e3779b9u);
  889. c.flutter2.seed (seed += 0x9e3779b9u);
  890. c.drift.seed (seed += 0x9e3779b9u);
  891. c.noise.s = seed += 0x9e3779b9u;
  892. }
  893. steep.reset();
  894. lofiLp.reset();
  895. eqBass.reset();
  896. eqMid.reset();
  897. eqTreble.reset();
  898. for (auto& q : outQ) q.reset();
  899. for (auto& q : loopQ) q.reset();
  900. multLp1.reset();
  901. multLp2.reset();
  902. compEnv[0] = compEnv[1] = 0.0;
  903. for (auto& b : bpath)
  904. {
  905. b.line.clear();
  906. for (auto& a : b.diff)
  907. a.line.clear();
  908. b.lowCut.reset();
  909. b.highCut.reset();
  910. }
  911. for (auto& q : loopQB) q.reset();
  912. for (auto& q : pairQ) q.reset();
  913. eqBassB.reset();
  914. eqMidB.reset();
  915. eqTrebleB.reset();
  916. lofiLpB.reset();
  917. multLpB1.reset();
  918. multLpB2.reset();
  919. compEnvB[0] = compEnvB[1] = 0.0;
  920. voicePhase = 0.0;
  921. lfoPhase = 0.0;
  922. envelope = 0.0f;
  923. duckGr = 0.0;
  924. hyb.dirty = true;
  925. tape.dirty = true;
  926. }
  927. // round 135w: 2 = input peak, 3 = output peak of the last block (linear, the editor's output meter and clip lamp)
  928. float controlOutput (int output) const noexcept override
  929. {
  930. return output == 2 ? meterIn.load (std::memory_order_relaxed) : output == 3 ? meterOut.load (std::memory_order_relaxed) : 0.0f;
  931. }
  932. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  933. {
  934. if (ch[0].line.buf.empty())
  935. prepare (sr);
  936. float inPeak = 0.0f;
  937. for (int i = 0; i < n; ++i)
  938. inPeak = std::max (inPeak, std::max (std::abs (l[i]), std::abs (r[i])));
  939. meterIn.store (inPeak, std::memory_order_relaxed);
  940. if (tapeVoicingOf (p[tapeVoicing]) != 0)
  941. {
  942. tapeProcess (l, r, n, p, bpm);
  943. return;
  944. }
  945. tape.dirty = true;
  946. if (hybridOf (p[hybrid]) != 0)
  947. hybridProcess (l, r, n, p, bpm);
  948. else
  949. {
  950. hyb.dirty = true;
  951. echoProcess (l, r, n, p, bpm);
  952. }
  953. float outPeak = 0.0f;
  954. for (int i = 0; i < n; ++i)
  955. outPeak = std::max (outPeak, std::max (std::abs (l[i]), std::abs (r[i])));
  956. meterOut.store (outPeak, std::memory_order_relaxed);
  957. }
  958. private:
  959. std::atomic<float> meterIn { 0.0f }, meterOut { 0.0f };
  960. void echoProcess (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept
  961. {
  962. // round 147: a measured echo voicing replaces the mode's character (its read path is the clean one), and the
  963. // saturation curve can be chosen apart from it
  964. const int vk = voicingOf (p[voicing]);
  965. const VoicingData* vd = vk > 0 ? &voicingData (vk) : nullptr;
  966. const int stk = satTypeOf (p[satType]);
  967. prepareSaturation (stk > 0 ? &satTypeData (stk) : vd, 24.0 * (double) clamp01 (p[drive]));
  968. if (vd != nullptr && (vk != toneVoicing || sr != toneSr))
  969. {
  970. toneVoicing = vk;
  971. toneSr = sr;
  972. for (auto& q : outQ) q = {};
  973. for (auto& q : loopQ) q = {};
  974. outN = designOutTone (*vd, sr, outQ.data());
  975. loopN = designLoopTone (*vd, sr, loopQ.data());
  976. outLin = (float) std::pow (10.0, vd->outGain / 20.0);
  977. }
  978. const auto md = vd != nullptr ? Mode::clean : modeOf (p[mode]);
  979. const bool rhythmOn = p[rhythm] >= 0.5f; // round 137: Rhythm mode overrides the style
  980. const auto st = rhythmOn ? Style::single : styleOf (p[style]);
  981. float dryGain = 1.0f, wetGain = 0.0f;
  982. equalPowerMix (p[mix], dryGain, wetGain);
  983. // times: sync / ms, spread (±¼ of the spread on each side), style
  984. auto tL = sideMs (p, false, bpm);
  985. auto tR = sideMs (p, true, bpm);
  986. if (st == Style::single || st == Style::quad)
  987. tR = tL;
  988. else if (st == Style::ratio)
  989. tR = tL * (0.01 + 0.99 * (double) clamp01 (p[ratio]));
  990. const auto spreadMs = 20.0 * bipolar (p[spread]);
  991. tL = std::max (0.0, tL + spreadMs * 0.25);
  992. tR = std::max (0.0, tR - spreadMs * 0.25);
  993. // round 147: PrimalTap's Multiply — the times × 1 / 2 / 4 / 8 (up to the line's 20 s), the band ÷ the same
  994. const int mult = multiplyOf (p[multiply]);
  995. if (mult > 1)
  996. {
  997. tL = std::min (tL * mult, 20000.0);
  998. tR = std::min (tR * mult, 20000.0);
  999. multLp1.set (multiplyCutoffHz (mult), sr);
  1000. multLp2.set (multiplyCutoffHz (mult), sr);
  1001. }
  1002. std::array<RhythmTap, 16> rTaps {};
  1003. double rSlot0 = 0.0;
  1004. int rCount = 0;
  1005. if (rhythmOn) // the loop is the pattern length; the taps are read inside it
  1006. {
  1007. tL = tR = rhythmLengthMs (p, bpm);
  1008. rCount = rhythmPattern (p, bpm, rTaps, rSlot0);
  1009. }
  1010. const auto g = feedbackGain (p[feedback]);
  1011. const bool pairsOn = p[pairs] >= 0.5f;
  1012. const auto polarityL = p[phaseL] >= 0.5f ? -1.0f : 1.0f, polarityR = p[phaseR] >= 0.5f ? -1.0f : 1.0f;
  1013. const auto outGain = (float) std::pow (10.0, outputDb (p[output]) / 20.0);
  1014. wetGain *= outGain;
  1015. const auto ageV = clamp01 (p[age]);
  1016. const auto driveGain = (float) std::pow (10.0, 24.0 * (double) clamp01 (p[drive]) / 20.0);
  1017. const auto widthV = (float) bipolar (p[width]);
  1018. const auto diffAmount = std::max (clamp01 (p[diffusion]), vd != nullptr ? vd->diffusion : 0.0f); // round 147: the voicing's own
  1019. const auto diffScale = 0.25 + 0.75 * (double) clamp01 (p[diffSize]);
  1020. const auto duck = clamp01 (p[ducking]);
  1021. // round 144: the LFO locked to a note value of the tempo (Mod sync), else its free rate
  1022. const auto syncBeats = modSyncBeats (modSyncOf (p[modSync]));
  1023. const auto rate = syncBeats > 0.0 ? tempoFor (p, bpm) / 60.0 / syncBeats : modRateHz (p[modRate]);
  1024. const auto lfoShape = modShapeOf (p[modShape]);
  1025. const auto inG = (float) std::pow (10.0, inputLevelDb (p[inputLevel]) / 20.0);
  1026. const auto fbBal = (float) bipolar (p[fbBalance]);
  1027. const auto fbL = 1.0f - std::max (0.0f, fbBal), fbR = 1.0f + std::min (0.0f, fbBal);
  1028. const bool postDiff = p[diffPost] >= 0.5f;
  1029. const auto bassDb = loopEqDb (p[loopBass]), midDb = loopEqDb (p[loopMid]), trebleDb = loopEqDb (p[loopTreble]);
  1030. const bool loopEq = std::abs (bassDb) > 1e-3 || std::abs (midDb) > 1e-3 || std::abs (trebleDb) > 1e-3;
  1031. if (loopEq)
  1032. {
  1033. eqBass.shelf (sr, 120.0, bassDb, false);
  1034. eqMid.peaking (sr, 1200.0, 0.7, midDb);
  1035. eqTreble.shelf (sr, std::min (5000.0, 0.4 * sr), trebleDb, true);
  1036. }
  1037. const auto depth = (double) clamp01 (p[modDepth]);
  1038. const auto wowV = (double) clamp01 (p[wow]), flutterV = (double) clamp01 (p[flutter]);
  1039. const auto fShift = shiftHz (p[shift]), fDetune = shiftDetuneHz (p[shiftDetune]);
  1040. const auto semis = pitchSemis (p[pitch]);
  1041. const auto cents = 100.0 * bipolar (p[pitchDetune]);
  1042. // character: the write-path filter / shaper and the modulation source
  1043. configure (md, tL, ageV, eraOf (p[era]));
  1044. for (auto& c : ch)
  1045. {
  1046. c.lowCut.set (lowCutHz (p[lowCut]), sr);
  1047. // the measured high cut acts like a one-pole at about half its label (§3.3); it opens up fully at the top
  1048. const auto hc = clamp01 (p[highCut]);
  1049. const auto open = std::max (0.0f, (hc - 0.8f) / 0.2f);
  1050. if (hc >= 0.999f)
  1051. c.highCut.open();
  1052. else
  1053. c.highCut.set (highCutHz (hc) * (0.5 + 1.5 * (double) (open * open)), sr);
  1054. }
  1055. const auto quadEl = std::pow (2.0, -0.4 * bipolar (p[spacing]));
  1056. const auto quadEr = std::pow (2.0, 0.4 * bipolar (p[spacing]));
  1057. const auto envAttack = (float) (1.0 - std::exp (-1.0 / (0.01 * sr)));
  1058. const bool lofiOn = p[lofi] >= 0.5f;
  1059. if (lofiOn)
  1060. lofiLp.set (5800.0, sr);
  1061. const auto duckThresholdDb = -51.0 * std::pow ((double) std::max (1e-6f, duck), 1.28);
  1062. const auto duckReleaseStep = 85.0 / sr;
  1063. static constexpr double diffMs[4] = { 4.3, 6.7, 9.1, 12.7 };
  1064. // round 135w: quad taps / swell, groove / feel / accent, prime numbers, cross feedback, balance, freeze, start side
  1065. const bool tapOn[4] = { p[tapA] >= 0.5f, p[tapB] >= 0.5f, p[tapC] >= 0.5f, p[tapD] >= 0.5f };
  1066. const bool swellOn = p[swell] >= 0.5f;
  1067. const auto grooveV = 0.25 * bipolar (p[groove]), feelV = 0.25 * bipolar (p[feel]);
  1068. const auto accentV = (float) bipolar (p[accent]);
  1069. const bool primesOn = p[primes] >= 0.5f, freezeOn = p[freeze] >= 0.5f;
  1070. const auto crossV = clamp01 (p[crossFeedback]);
  1071. const auto balanceV = (float) bipolar (p[balance]);
  1072. const int first = (st == Style::pingPong && p[startSide] >= 0.5f) ? 1 : 0, second = 1 - first;
  1073. // the plain read path (no shifter / pitch / reverse / phaser on the echoes) can read the echoes from their own
  1074. // taps: Feel moves the heard taps, Groove / Accent use a loop of two echo times (odd and even repeats apart)
  1075. const bool plainRead = (int) md < (int) Mode::phaser && st != Style::quad && ! rhythmOn;
  1076. const auto longest = std::max (tL, tR) * 0.001 * sr * (1.0 + std::abs (feelV) + std::abs (grooveV)) + 0.02 * sr;
  1077. const bool twoTap = plainRead && st != Style::pingPong && (std::abs (grooveV) > 1e-6 || std::abs (accentV) > 1e-6)
  1078. && 2.0 * longest < ch[0].line.maxDelay();
  1079. const bool feelTap = plainRead && std::abs (feelV) > 1e-6;
  1080. const auto maxRead = ch[0].line.maxDelay() - 2.0;
  1081. const auto loopG = freezeOn ? 1.0 : (twoTap ? g * g : g);
  1082. // round 150 [EXEC]: the voicing's measured stereo behaviour (EchoBoy's L/R offset in Single / Dual, its Ping-Pong)
  1083. const bool vStereo = vd != nullptr && p[voicingStereo] >= 0.5f && ! rhythmOn && ! freezeOn;
  1084. const auto lrO = lrOffsetMs (p[lrOffset]);
  1085. const bool lrOn = vStereo && lrO > 1e-3 && (st == Style::single || st == Style::dual) && plainRead && ! twoTap && ! feelTap
  1086. && std::max (tL, tR) * 0.001 * sr + 0.05 * sr < maxRead;
  1087. const bool ppV = vStereo && st == Style::pingPong;
  1088. double offA[2] {}, offB[2] {};
  1089. if (lrOn)
  1090. {
  1091. for (int c = 0; c < 2; ++c)
  1092. {
  1093. offA[c] = lrOddMs (lrO, c == 1) * 0.001 * sr;
  1094. offB[c] = lrEvenMs (lrO, c == 1) * 0.001 * sr;
  1095. bpath[(std::size_t) c].lowCut.a = ch[(std::size_t) c].lowCut.a;
  1096. bpath[(std::size_t) c].highCut.a = ch[(std::size_t) c].highCut.a;
  1097. }
  1098. for (int k = 0; k < loopN; ++k)
  1099. copyCoefficients (loopQB[(std::size_t) k], loopQ[(std::size_t) k]);
  1100. copyCoefficients (eqBassB, eqBass);
  1101. copyCoefficients (eqMidB, eqMid);
  1102. copyCoefficients (eqTrebleB, eqTreble);
  1103. if (lofiOn)
  1104. lofiLpB.set (5800.0, sr);
  1105. if (mult > 1)
  1106. {
  1107. multLpB1.set (multiplyCutoffHz (mult), sr);
  1108. multLpB2.set (multiplyCutoffHz (mult), sr);
  1109. }
  1110. }
  1111. if (ppV && (vk != pairVoicing || sr != pairSr))
  1112. {
  1113. pairVoicing = vk;
  1114. pairSr = sr;
  1115. for (auto& q : pairQ) q = {};
  1116. pairN = designPairTone (vk, sr, pairQ.data());
  1117. pairLin = pairToneGain (vk);
  1118. }
  1119. // round 147: the voicing's wobble as a delay swing (ms): a sine of depth c cents rms at f Hz swings the delay by
  1120. // √2 · c / (1731.2 · 2π f) s (the smooth random LFO about its rms, so without the √2)
  1121. // (round 150: a triangle of constant pitch ±c cents swings it by c / (1731.2 · 4 f) s)
  1122. wobbleMs = vd == nullptr || vd->wobbleCents <= 0.0f ? 0.0
  1123. : vd->wobbleRandom == 2 ? 1000.0 * vd->wobbleCents / (1731.234 * 4.0 * std::max (0.05f, vd->wobbleHz))
  1124. : 1000.0 * (vd->wobbleRandom != 0 ? 1.0 : std::sqrt (2.0)) * vd->wobbleCents / (1731.234 * twoPi * std::max (0.05f, vd->wobbleHz));
  1125. for (int i = 0; i < n; ++i)
  1126. {
  1127. const float in[2] = { l[i], r[i] };
  1128. // modulation (ms): wow / flutter in the tape characters, an LFO in the others
  1129. lfoPhase += rate / sr;
  1130. lfoPhase -= std::floor (lfoPhase);
  1131. double modMs[2] = { 0.0, 0.0 };
  1132. if (vd != nullptr && vd->wobbleCents > 0.0f) // round 147: the voicing's measured wobble
  1133. {
  1134. voicePhase += vd->wobbleHz / sr;
  1135. voicePhase -= std::floor (voicePhase);
  1136. for (int c = 0; c < 2; ++c)
  1137. {
  1138. if (vd->wobbleRandom == 1) // both sides on one random swing when the reference's are in step
  1139. modMs[c] = c == 1 && vd->wobblePhase == 0.0f ? modMs[0]
  1140. : wobbleMs * (double) ch[(std::size_t) c].drift.next (2.0 * vd->wobbleHz, sr); // ≈ unit rms
  1141. else
  1142. {
  1143. auto ph = voicePhase + (c == 1 ? (double) vd->wobblePhase : 0.0);
  1144. ph -= std::floor (ph);
  1145. // round 150: a triangle where the reference's delay track is one (wobbleRandom 2), in the sine's phase
  1146. const auto u = ph + 0.25 - std::floor (ph + 0.25);
  1147. modMs[c] = vd->wobbleRandom == 2 ? wobbleMs * (1.0 - 4.0 * std::abs (u - 0.5)) : wobbleMs * std::sin (twoPi * ph);
  1148. }
  1149. }
  1150. if (ppV) // round 150: the ping-pong's second side repeats the first one's echo as it is
  1151. modMs[second] = 0.0;
  1152. }
  1153. for (int c = 0; c < 2 && vd == nullptr; ++c)
  1154. {
  1155. auto& s = ch[(std::size_t) c];
  1156. switch (modulation)
  1157. {
  1158. case ModSource::tape:
  1159. // measured: wow 100 % ≈ 14 c rms around 1.5–2.5 Hz, flutter 100 % ≈ 14 c rms at 7–15 Hz
  1160. modMs[c] = wowAmountMs * wowV * s.wow.next (4.0, sr)
  1161. + flutterAmountMs * flutterV * 0.7 * (s.flutter1.next (18.0, sr) + s.flutter2.next (26.0, sr));
  1162. break;
  1163. case ModSource::sine:
  1164. {
  1165. auto ph = lfoPhase + (c == 1 ? 0.25 : 0.0);
  1166. ph -= std::floor (ph);
  1167. double w = 0.0;
  1168. switch (lfoShape) // round 144: the LFO's wave (0 = the sine as before)
  1169. {
  1170. case 1: w = 1.0 - 4.0 * std::abs (ph - 0.5); break; // triangle
  1171. case 2: w = std::tanh (6.0 * std::sin (twoPi * ph)) / std::tanh (6.0); break; // square (soft edges)
  1172. case 3: w = 2.0 * ph - 1.0; break; // saw
  1173. case 4: w = 0.5 * s.drift.next (rate * 2.0, sr); break; // random
  1174. default: w = std::sin (twoPi * ph); break;
  1175. }
  1176. modMs[c] = modAmountMs * depth * depth * w;
  1177. break;
  1178. }
  1179. case ModSource::random:
  1180. modMs[c] = 0.5 * modAmountMs * depth * depth * 0.5 * s.drift.next (rate * 2.0, sr);
  1181. break;
  1182. case ModSource::none: break;
  1183. }
  1184. }
  1185. // targets in samples (tape glide when the time changes)
  1186. double target[2] = { tL * 0.001 * sr, tR * 0.001 * sr };
  1187. if (primesOn)
  1188. for (auto& t : target)
  1189. t = (double) nearestPrime ((int) std::lround (t));
  1190. double d[2];
  1191. for (int c = 0; c < 2; ++c)
  1192. {
  1193. auto& s = ch[(std::size_t) c];
  1194. d[c] = std::max (2.0, s.time.next (target[c], 0.12, sr) + modMs[c] * 0.001 * sr);
  1195. }
  1196. // read the echoes (read path: shifter / pitch / reverse / phaser / detune)
  1197. float echo[2], fbSig[2], oddEcho[2] { 0.0f, 0.0f };
  1198. for (int c = 0; c < 2; ++c)
  1199. {
  1200. auto& s = ch[(std::size_t) c];
  1201. float y = 0.0f;
  1202. if (rhythmOn)
  1203. {
  1204. // slot 0 comes back through the loop (the input of the last pass), the taps scale with the glide
  1205. const auto scale = target[c] > 1.0 ? d[c] / target[c] : 1.0;
  1206. fbSig[c] = s.line.at (std::min (maxRead, d[c]));
  1207. y = (float) (rSlot0 * loopG) * fbSig[c];
  1208. for (int k = 0; k < rCount; ++k)
  1209. y += (float) rTaps[(std::size_t) k].level * s.line.at (std::clamp (rTaps[(std::size_t) k].ms * 0.001 * sr * scale, 2.0, maxRead));
  1210. }
  1211. else if (st == Style::quad)
  1212. {
  1213. const auto e = c == 0 ? quadEl : quadEr;
  1214. float last = 0.0f, heard = 0.0f;
  1215. int count = 0;
  1216. for (int k = 1; k <= 4; ++k)
  1217. {
  1218. const auto v = s.line.at (std::max (2.0, d[c] * std::pow ((double) k / 4.0, e)));
  1219. if (tapOn[k - 1])
  1220. {
  1221. y += 0.5f * v;
  1222. heard += v;
  1223. ++count;
  1224. }
  1225. last = v;
  1226. }
  1227. // Repeat: the last tap feeds back (heard or not); Swell: the mean of the taps heard
  1228. fbSig[c] = swellOn ? (count > 0 ? heard / (float) count : 0.0f) : last;
  1229. }
  1230. else if (md == Mode::reverse)
  1231. {
  1232. y = reverseRead (s, d[c], std::pow (2.0, semis / 12.0));
  1233. fbSig[c] = y;
  1234. }
  1235. else if (lrOn)
  1236. {
  1237. // round 150: the L/R offset — the odd repeats from the first loop (T + odd offset, which feeds the
  1238. // second loop), the even ones from the second (T + even − odd offset), which feeds the first again
  1239. auto& b = bpath[(std::size_t) c];
  1240. oddEcho[c] = s.line.at (std::clamp (d[c] + offA[c], 2.0, maxRead));
  1241. y = oddEcho[c] + b.line.at (std::clamp (d[c] + offB[c] - offA[c], 2.0, maxRead));
  1242. fbSig[c] = b.line.at (std::clamp (d[c] - offA[c], 2.0, maxRead));
  1243. }
  1244. else if (twoTap)
  1245. {
  1246. // a loop of two echo times: odd repeats at T (1 + feel + groove), even ones at T (2 + feel)
  1247. fbSig[c] = s.line.at (std::min (maxRead, 2.0 * d[c]));
  1248. y = (1.0f + accentV) * s.line.at (std::clamp (d[c] * (1.0 + feelV + grooveV), 2.0, maxRead))
  1249. + (1.0f - accentV) * (float) g * s.line.at (std::clamp (d[c] * (2.0 + feelV), 2.0, maxRead));
  1250. }
  1251. else if (feelTap)
  1252. {
  1253. fbSig[c] = s.line.at (d[c]);
  1254. y = s.line.at (std::clamp (d[c] * (1.0 + feelV), 2.0, maxRead));
  1255. }
  1256. else
  1257. {
  1258. // the pitch shifter's window delays the echo by half a window: read that much earlier
  1259. const auto early = (md == Mode::pitch || md == Mode::detune) ? s.pitch.window * 0.5 : 0.0;
  1260. y = s.line.at (std::max (2.0, d[c] - early));
  1261. fbSig[c] = y;
  1262. }
  1263. const auto sign = c == 0 ? 1.0 : -1.0;
  1264. switch (md)
  1265. {
  1266. case Mode::shifter:
  1267. y = s.shifter.process (y, fShift + sign * fDetune, sr);
  1268. break;
  1269. case Mode::pitch:
  1270. y = s.pitch.process (y, std::pow (2.0, (semis + sign * cents / 100.0) / 12.0));
  1271. break;
  1272. case Mode::detune:
  1273. {
  1274. const auto wobble = 0.2 * s.drift.next (1.0, sr);
  1275. y = s.pitch.process (y, std::pow (2.0, (sign * cents * (1.0 + wobble)) / 1200.0));
  1276. break;
  1277. }
  1278. case Mode::phaser:
  1279. {
  1280. const auto lfo = std::sin (twoPi * (lfoPhase + (c == 1 ? 0.25 : 0.0)));
  1281. 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);
  1282. const auto t = std::tan (pi * fc / sr);
  1283. const auto a = (float) ((t - 1.0) / (t + 1.0));
  1284. auto u = y;
  1285. for (auto& ap : s.phaser)
  1286. u = ap.process (u, a);
  1287. y = 0.5f * (y + u);
  1288. break;
  1289. }
  1290. default: break;
  1291. }
  1292. if (st != Style::quad && ! twoTap && ! feelTap && ! rhythmOn && ! lrOn) // the quad style feeds back its last tap as read
  1293. fbSig[c] = y;
  1294. if (vd != nullptr) // round 147: the voicing's output tone (once, on the way out)
  1295. {
  1296. y *= outLin;
  1297. for (int k = 0; k < outN; ++k)
  1298. y = outQ[(std::size_t) k].process (y, c);
  1299. }
  1300. echo[c] = y;
  1301. }
  1302. // loop inputs per style
  1303. const auto mono = 0.5f * (in[0] + in[1]);
  1304. const auto feed = freezeOn ? 0.0f : inG; // frozen: the input stays out of the loop; round 144: Input level
  1305. float loopIn[2];
  1306. if (ppV)
  1307. {
  1308. // round 150 [EXEC] EchoBoy's Ping-Pong: (L + R) / √2 into the first side; the second side's echo is the
  1309. // first one's at full level (same tone), the pair comes back through the pair tone and the loop gain
  1310. auto back = fbSig[second] * pairLin;
  1311. for (int k = 0; k < pairN; ++k)
  1312. back = pairQ[(std::size_t) k].process (back, 0);
  1313. loopIn[first] = feed * (in[0] + in[1]) * 0.70710678f + (float) loopG * back;
  1314. loopIn[second] = fbSig[first];
  1315. }
  1316. else if (st == Style::pingPong && pairsOn)
  1317. {
  1318. // in pairs: the start side → the other one at full level, the loop gain once per pair
  1319. loopIn[first] = feed * mono + (float) loopG * fbSig[second];
  1320. loopIn[second] = fbSig[first];
  1321. }
  1322. else if (st == Style::pingPong)
  1323. {
  1324. const auto half = (float) std::sqrt (loopG);
  1325. loopIn[first] = feed * mono + half * fbSig[second];
  1326. loopIn[second] = half * fbSig[first];
  1327. }
  1328. else
  1329. {
  1330. const auto mine = 1.0f - crossV;
  1331. loopIn[0] = feed * in[0] + fbL * (float) loopG * (mine * fbSig[0] + crossV * fbSig[1]);
  1332. loopIn[1] = feed * in[1] + fbR * (float) loopG * (mine * fbSig[1] + crossV * fbSig[0]);
  1333. }
  1334. // write path: diffusion, character, cuts, a soft limit (feedback above 100 % stays bounded)
  1335. for (int c = 0; c < 2; ++c)
  1336. {
  1337. auto& s = ch[(std::size_t) c];
  1338. auto u = loopIn[c];
  1339. if (freezeOn || (ppV && c == second)) // the loop as it is (round 150: the ping-pong copy too)
  1340. {
  1341. s.line.push (u);
  1342. continue;
  1343. }
  1344. if (diffAmount > 0.0f && ! postDiff)
  1345. for (std::size_t k = 0; k < 4; ++k)
  1346. u = s.diff[k].process (u, diffMs[k] * diffScale * 0.001 * sr * (c == 1 ? 1.07 : 1.0), 0.65f * diffAmount);
  1347. if (vd != nullptr) // round 147: the voicing's saturation and loop tone (every pass)
  1348. {
  1349. u = saturate (u, c);
  1350. for (int k = 0; k < loopN; ++k)
  1351. u = loopQ[(std::size_t) k].process (u, c);
  1352. }
  1353. else
  1354. u = character (s, c, u, md, ageV, driveGain);
  1355. u = s.highCut.lp (s.lowCut.hp (u));
  1356. if (loopEq) // round 144: the loop EQ shapes every repeat again
  1357. u = eqTreble.process (eqMid.process (eqBass.process (u, c), c), c);
  1358. if (lofiOn)
  1359. u = lofiLp.process (u, c);
  1360. if (mult > 1) // round 147: Multiply's band
  1361. u = multLp2.process (multLp1.process (u, c), c);
  1362. s.line.push (softLimit (u));
  1363. }
  1364. // round 150: the L/R offset's second loop gets the odd repeats (one more pass: gain, diffusion, colour, cuts)
  1365. for (int c = 0; c < 2; ++c)
  1366. {
  1367. auto& b = bpath[(std::size_t) c];
  1368. if (! lrOn)
  1369. {
  1370. b.line.push (0.0f);
  1371. continue;
  1372. }
  1373. auto u = (c == 0 ? fbL : fbR) * (float) loopG * oddEcho[c];
  1374. if (diffAmount > 0.0f && ! postDiff)
  1375. for (std::size_t k = 0; k < 4; ++k)
  1376. u = b.diff[k].process (u, diffMs[k] * diffScale * 0.001 * sr * (c == 1 ? 1.07 : 1.0), 0.65f * diffAmount);
  1377. u = saturate (u, compEnvB[c]);
  1378. for (int k = 0; k < loopN; ++k)
  1379. u = loopQB[(std::size_t) k].process (u, c);
  1380. u = b.highCut.lp (b.lowCut.hp (u));
  1381. if (loopEq)
  1382. u = eqTrebleB.process (eqMidB.process (eqBassB.process (u, c), c), c);
  1383. if (lofiOn)
  1384. u = lofiLpB.process (u, c);
  1385. if (mult > 1)
  1386. u = multLpB2.process (multLpB1.process (u, c), c);
  1387. b.line.push (softLimit (u));
  1388. }
  1389. // ducking, round 137 [EXEC] measured on the ValhallaDelay demo (REA_rodada137 §5): a gain reduction on the
  1390. // echoes keyed by the input's level: GR = input rms − T, T = −51 dB × ducking^1.28 (a 6 dB soft knee),
  1391. // fast attack (10 ms), released at ≈ 85 dB / s
  1392. auto duckGain = 1.0f;
  1393. if (duck > 0.0f)
  1394. {
  1395. const auto e = mono * mono;
  1396. envelope += (e - envelope) * envAttack;
  1397. const auto keyDb = 10.0 * std::log10 (std::max (1e-12, (double) envelope));
  1398. const auto x = keyDb - duckThresholdDb;
  1399. const auto target = x <= -3.0 ? 0.0 : x >= 3.0 ? x : (x + 3.0) * (x + 3.0) / 12.0;
  1400. duckGr = target > duckGr ? duckGr + (target - duckGr) * (double) envAttack : std::max (target, duckGr - duckReleaseStep);
  1401. duckGain = (float) std::pow (10.0, -duckGr / 20.0);
  1402. }
  1403. else
  1404. duckGr = 0.0;
  1405. // round 144: Diffusion post: the echoes diffused on their way out (the loop stays clean)
  1406. if (postDiff && diffAmount > 0.0f)
  1407. for (int c = 0; c < 2; ++c)
  1408. for (std::size_t k = 0; k < 4; ++k)
  1409. echo[c] = ch[(std::size_t) c].diffOut[k].process (echo[c], diffMs[k] * diffScale * 0.001 * sr * (c == 1 ? 1.07 : 1.0), 0.65f * diffAmount);
  1410. // balance of the echoes, width (M / S of the echoes) and the equal-power mix
  1411. echo[0] *= 1.0f - std::max (0.0f, balanceV);
  1412. echo[1] *= 1.0f + std::min (0.0f, balanceV);
  1413. const auto m = 0.5f * (echo[0] + echo[1]), sd = 0.5f * (echo[0] - echo[1]) * widthV;
  1414. l[i] = dryGain * in[0] + wetGain * duckGain * (m + sd) * polarityL;
  1415. r[i] = dryGain * in[1] + wetGain * duckGain * (m - sd) * polarityR;
  1416. }
  1417. }
  1418. enum class ModSource { none, tape, sine, random };
  1419. struct Channel
  1420. {
  1421. Line line;
  1422. PitchShifter pitch;
  1423. Shifter shifter;
  1424. std::array<Allpass, 4> diff;
  1425. std::array<Allpass, 4> diffOut; // round 144: Diffusion post
  1426. std::array<Ap1, 6> phaser;
  1427. OnePole shelf, tapeLp1, tapeLp2, hifiLp, hifiHp, lowCut, highCut;
  1428. RandomLfo wow, flutter1, flutter2, drift;
  1429. Noise noise;
  1430. Glide time;
  1431. float held = 0.0f;
  1432. double holdPhase = 0.0, revPhase = 0.0;
  1433. };
  1434. // reversed grains: two readers sweeping backwards through the last (1 + r)·D samples, Hann crossfade
  1435. float reverseRead (Channel& s, double d, double r) noexcept
  1436. {
  1437. s.revPhase += 1.0 / std::max (64.0, d);
  1438. s.revPhase -= std::floor (s.revPhase);
  1439. float y = 0.0f;
  1440. for (int k = 0; k < 2; ++k)
  1441. {
  1442. auto p = s.revPhase + 0.5 * k;
  1443. p -= std::floor (p);
  1444. const auto w = (float) std::sin (pi * p);
  1445. y += w * w * s.line.at (2.0 + p * (1.0 + r) * d);
  1446. }
  1447. return y;
  1448. }
  1449. void configure (Mode m, double timeMsL, float ageV, int eraIndex = 0) noexcept
  1450. {
  1451. modAmountMs = 6.1;
  1452. const auto bw = eraBandwidth (m, eraIndex); // round 137: Era (1 = Past)
  1453. switch (m)
  1454. {
  1455. case Mode::tape:
  1456. case Mode::lofi:
  1457. case Mode::phaser:
  1458. {
  1459. // tape: the head bump / low-end loss (low shelf −5 dB), and a two-pole roll-off that falls with the
  1460. // delay time (tape speed): ≈ 9 kHz at 100 ms, 5 kHz at 300 ms, 2.7 kHz at 1 s
  1461. const auto fc = std::min (0.45 * sr, bw * (m == Mode::tape ? std::clamp (9000.0 * std::pow (std::max (1.0, timeMsL) / 100.0, -0.5), 1500.0, 10000.0) : 9000.0));
  1462. for (auto& c : ch)
  1463. {
  1464. c.shelf.set (350.0, sr);
  1465. c.tapeLp1.set (fc, sr);
  1466. c.tapeLp2.set (fc, sr);
  1467. }
  1468. shaper.set (m == Mode::lofi ? 0.9f : 0.95f, m == Mode::lofi ? 0.0f : 0.11f);
  1469. modulation = m == Mode::tape ? ModSource::tape : (m == Mode::lofi ? ModSource::sine : ModSource::none);
  1470. break;
  1471. }
  1472. case Mode::hifi:
  1473. case Mode::shifter:
  1474. for (auto& c : ch)
  1475. {
  1476. c.hifiLp.set (13000.0, sr);
  1477. c.hifiHp.set (15.0, sr);
  1478. }
  1479. shaper.set (0.55f, m == Mode::hifi ? 0.17f : 0.0f);
  1480. modulation = m == Mode::hifi ? ModSource::random : ModSource::none;
  1481. break;
  1482. case Mode::bucket:
  1483. steep.set (1650.0 * bw, sr);
  1484. shaper.set (0.6f, 0.05f);
  1485. modulation = ModSource::sine;
  1486. break;
  1487. case Mode::analog:
  1488. steep.set (3450.0 * bw, sr);
  1489. shaper.set (1.525f, 0.0f);
  1490. modulation = ModSource::sine;
  1491. break;
  1492. case Mode::digital:
  1493. case Mode::pitch:
  1494. case Mode::reverse:
  1495. case Mode::detune:
  1496. steep.set (std::min (0.45 * sr, 8000.0 * bw), sr);
  1497. shaper.set (0.375f, 0.0f);
  1498. modulation = m == Mode::digital ? ModSource::sine : ModSource::none;
  1499. break;
  1500. case Mode::clean:
  1501. {
  1502. // age = a steep low-pass: off at 0, 5.3 kHz at 50 %, 3.3 kHz at 100 %
  1503. const auto a = (double) ageV;
  1504. 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));
  1505. steep.set (fc, sr);
  1506. shaper.set (0.3f, 0.0f);
  1507. modulation = ModSource::sine;
  1508. modAmountMs = 10.4;
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. float character (Channel& s, int c, float u, Mode m, float ageV, float driveGain) noexcept
  1514. {
  1515. // round 147: a chosen saturation type replaces the mode's own shaper
  1516. const auto drive = [this, driveGain, c] (float x) { return satCurve != nullptr ? saturate (x, c) : shaper (x * driveGain) / driveGain; };
  1517. switch (m)
  1518. {
  1519. case Mode::tape:
  1520. case Mode::phaser:
  1521. u = drive (u); // saturation at the record head, then the tape's response
  1522. u = 0.8f * (u - 0.44f * s.shelf.lp (u)); // −1.9 dB through the tape path, −5 dB low shelf
  1523. return s.tapeLp2.lp (s.tapeLp1.lp (u));
  1524. case Mode::lofi:
  1525. {
  1526. u = u - 0.44f * s.shelf.lp (u);
  1527. u = s.tapeLp2.lp (s.tapeLp1.lp (u));
  1528. u = crush (s, u, 16.0 - 10.0 * ageV, 1.0 + 5.0 * ageV);
  1529. return drive (u);
  1530. }
  1531. case Mode::hifi:
  1532. case Mode::shifter:
  1533. return drive (s.hifiLp.lp (s.hifiHp.hp (u)));
  1534. case Mode::bucket:
  1535. case Mode::analog:
  1536. {
  1537. u = drive (steep.process (u, c));
  1538. const auto level = m == Mode::bucket ? 1e-3f : 2.5e-4f; // −60 / −72 dBFS at full age (∝ age²)
  1539. return u + level * ageV * ageV * 1.73f * s.noise.next();
  1540. }
  1541. case Mode::digital:
  1542. case Mode::pitch:
  1543. case Mode::reverse:
  1544. case Mode::detune:
  1545. {
  1546. u = drive (steep.process (u, c));
  1547. if (ageV > 0.0f)
  1548. u = crush (s, u, 24.0 - 14.0 * ageV, 1.0 + 1.5 * ageV * ageV);
  1549. return u;
  1550. }
  1551. case Mode::clean:
  1552. return ageV > 0.0f ? drive (steep.process (u, c)) : drive (u);
  1553. }
  1554. return u;
  1555. }
  1556. // sample-and-hold rate reduction and bit-depth reduction (lo-fi / digital age)
  1557. static float crush (Channel& s, float u, double bits, double hold) noexcept
  1558. {
  1559. s.holdPhase += 1.0;
  1560. if (s.holdPhase >= hold)
  1561. {
  1562. s.holdPhase -= hold;
  1563. const auto q = std::pow (2.0, std::clamp (bits, 4.0, 24.0) - 1.0);
  1564. s.held = (float) (std::round ((double) u * q) / q);
  1565. }
  1566. return s.held;
  1567. }
  1568. // ---- the hybrid echo (round 124) -----------------------------------------------------------------------------
  1569. // [EXEC] black-box measurement of the official H-Delay trial (docs/analise/REA_rodada124.md §5), our own code:
  1570. // - ping-pong: the left line takes (L + R)/√2 plus fb² × the right echo; the right line takes the left echo; the left
  1571. // echo comes T after the input, every later pass T + 1 (single-sample impulses land exactly there);
  1572. // - each write into a line goes through y = v·g, g = min (1 / (1 + |v|), released toward 1 with a 10 ms time
  1573. // constant): a lone impulse of 1/√2 comes back as 0.41421 = x / (1 + x), the next pass 0.29289 = f (0.41421);
  1574. // - stereo: each side its own line, fb × its echo, the same gain on every write;
  1575. // - analog: the same loop between an input gain and an output gain (fitted on impulses at -66 … +6 dBFS and her takes)
  1576. // and a gentle low tilt on the echoes; its hiss (≈ -85 dBFS) is not reproduced.
  1577. struct HybridState
  1578. {
  1579. Line a, b;
  1580. double ga = 1.0, gb = 1.0, aPrev = 0.0, bPrev = 0.0;
  1581. Biquad tilt;
  1582. bool dirty = true;
  1583. } hyb;
  1584. static constexpr double analogIn = 0.8866, analogOut = 0.9776;
  1585. static inline double linkedGain (double level, double& g, double rel) noexcept
  1586. {
  1587. const auto inst = 1.0 / (1.0 + level);
  1588. g = std::min (inst, 1.0 - (1.0 - g) * rel);
  1589. return g;
  1590. }
  1591. void hybridProcess (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept
  1592. {
  1593. if (hyb.a.buf.empty() || hyb.dirty)
  1594. {
  1595. if (hyb.a.buf.empty())
  1596. {
  1597. hyb.a.allocate (sr * 3.6 + 8.0);
  1598. hyb.b.allocate (sr * 3.6 + 8.0);
  1599. }
  1600. hyb.a.clear();
  1601. hyb.b.clear();
  1602. hyb.ga = hyb.gb = 1.0;
  1603. hyb.aPrev = hyb.bPrev = 0.0;
  1604. hyb.tilt.reset();
  1605. hyb.dirty = false;
  1606. }
  1607. const bool analog = hybridOf (p[hybrid]) == 2;
  1608. const bool pp = styleOf (p[style]) == Style::pingPong;
  1609. const bool startRight = p[startSide] >= 0.5f;
  1610. const auto t = std::clamp ((int) std::lround (sideMs (p, false, bpm) * sr / 1000.0), 1, (int) hyb.a.maxDelay());
  1611. const auto fb = feedbackGain (p[feedback]); // H-Delay's feedback / 100 (0 … 2)
  1612. const auto fbPair = fb * fb;
  1613. float dryGain = 1.0f, wetGain = 0.0f;
  1614. equalPowerMix (p[mix], dryGain, wetGain); // [EXEC] cos / sin of mix × 90° (Analog off, 0 … 100 % in 10 % steps)
  1615. // [EXEC] the output level applies to the whole output, dry included (her mix-54 / +4.6 dB state)
  1616. const auto outLevel = (float) std::pow (10.0, outputDb (p[output]) / 20.0);
  1617. wetGain *= outLevel;
  1618. dryGain *= outLevel;
  1619. if (hybridOf (p[hybrid]) == 2)
  1620. dryGain *= 0.928f; // [EXEC] analog: the dry path goes through the analog stage too (her mix-41 state on her take)
  1621. const auto rel = std::exp (-1.0 / (0.010 * sr));
  1622. const auto inGain = analog ? analogIn : 1.0, outGain = analog ? analogOut : 1.0;
  1623. if (analog)
  1624. hyb.tilt.shelf (sr, 942.0, 0.63, false, 0.5);
  1625. constexpr double invSqrt2 = 0.70710678118654752;
  1626. for (int i = 0; i < n; ++i)
  1627. {
  1628. const double xl = l[i], xr = r[i];
  1629. const double aOut = hyb.a.tap (t), bOut = hyb.b.tap (t);
  1630. // the two lines share one gain (a loud left pass also turns the right one down)
  1631. const auto va = pp ? inGain * (xl + xr) * invSqrt2 + fbPair * hyb.bPrev : inGain * xl + fb * hyb.aPrev;
  1632. const auto vb = pp ? hyb.aPrev : inGain * xr + fb * hyb.bPrev;
  1633. const auto g = linkedGain (std::max (std::fabs (va), std::fabs (vb)), hyb.ga, rel);
  1634. hyb.a.push ((float) (va * g));
  1635. hyb.b.push ((float) (vb * g));
  1636. hyb.aPrev = aOut;
  1637. hyb.bPrev = bOut;
  1638. float el = (float) (aOut * outGain), er = (float) (bOut * outGain);
  1639. if (pp && startRight) // round 135w: the first echo on the right
  1640. std::swap (el, er);
  1641. if (analog)
  1642. {
  1643. el = hyb.tilt.process (el, 0);
  1644. er = hyb.tilt.process (er, 1);
  1645. }
  1646. l[i] = dryGain * (float) xl + wetGain * el * (p[phaseL] >= 0.5f ? -1.0f : 1.0f);
  1647. r[i] = dryGain * (float) xr + wetGain * er * (p[phaseR] >= 0.5f ? -1.0f : 1.0f);
  1648. }
  1649. }
  1650. // ---- the tape voicing (round 136) ------------------------------------------------------------------------------
  1651. // [EXEC] black-box impulse responses of the official EchoBoy trial at her saved states (REA_rodada136 §4), our own
  1652. // code: each output side is z[n] = Σ_k g_k·F(v)[n − d_k] + G·F(z)[n − P], v = the input (both sides summed for the
  1653. // ping-pong, each its own otherwise) through a soft saturation, F = a pass's colour (the tap and a one-pole tail
  1654. // b·ρ^(m−1)), d_k = m_k·T + offset samples (T = the note at the tempo; the offsets measured with the trial's 32-sample
  1655. // latency removed), G = the loop gain per period P; the edited ping-pong style adds eight diffusion taps around each
  1656. // first tap; the pass colour is a 3rd-order IIR fitted to the deconvolved pass kernel (−21 dB ping-pong, −57 dB
  1657. // studio tape). No delay modulation was found (constant integer delays); her hiss-level noise is not reproduced.
  1658. struct TapeSide
  1659. {
  1660. int taps;
  1661. double mult[2];
  1662. int off[2];
  1663. double gain[2]; // gain[1] is relative to gain[0] at the reference loop gain, scaled by √(G / g0)
  1664. };
  1665. struct TapeVoicingData
  1666. {
  1667. bool sum;
  1668. double satC, satP;
  1669. double b[4], a[4]; // the pass colour: a 3rd-order IIR, unity tap (a[0] = 1)
  1670. bool ghosts;
  1671. double g0; // the loop gain the second-tap ratios were measured at
  1672. TapeSide side[2];
  1673. double periodMult;
  1674. };
  1675. static const TapeVoicingData& tapeData (int k) noexcept
  1676. {
  1677. static const TapeVoicingData v[tapeVoicingCount] {
  1678. // ping-pong, edited style (her 9 + 6 + 3 main states): L one beat + 38, R two beats + 43, loop two beats;
  1679. // the saturation constant is the per-channel one doubled (both inputs are summed before it)
  1680. { true, 2.64, 1.585, { 1.02697244, -2.98057822, 2.88052472, -0.926918896 }, { 1, -2.92001423, 2.84046497, -0.920450366 }, true, 0.356, { { 1, { 1.0, 0.0 }, { 38, 0 }, { 0.3887, 0.0 } }, { 1, { 2.0, 0.0 }, { 43, 0 }, { 0.3887, 0.0 } } }, 2.0 },
  1681. // studio tape, single: L at T − 184 and 2T + 296, R at T + 200 and 2T − 280, loop 2T
  1682. { false, 1.8895, 3.7594, { 1.00009827, -2.77614305, 2.55245962, -0.776414786 }, { 1, -2.81491464, 2.63055012, -0.815634356 }, false, 0.0574, { { 2, { 1.0, 2.0 }, { -184, 296 }, { 0.5748, 0.2592 } }, { 2, { 1.0, 2.0 }, { 200, -280 }, { 0.5748, 0.2592 } } }, 2.0 },
  1683. // studio tape with its saturation up (her 1/8-note states)
  1684. { false, 3.5838, 1.4594, { 1.00009827, -2.77614305, 2.55245962, -0.776414786 }, { 1, -2.81491464, 2.63055012, -0.815634356 }, false, 0.0466, { { 2, { 1.0, 2.0 }, { -184, 296 }, { 0.6100, 0.2350 } }, { 2, { 1.0, 2.0 }, { 200, -280 }, { 0.6100, 0.2350 } } }, 2.0 },
  1685. };
  1686. return v[std::clamp (k - 1, 0, tapeVoicingCount - 1)];
  1687. }
  1688. struct TapeState
  1689. {
  1690. Line in[2], z[2];
  1691. double passX[2][2][3] {}, passY[2][2][3] {}, loopX[2][3] {}, loopY[2][3] {};
  1692. bool dirty = true;
  1693. // round 148: the ping-pong's measured kernels (first pass and loop per side) and the lines their output is read from
  1694. BlockConvolver first[2], loop[2];
  1695. Line firstOut[2], loopOut[2];
  1696. double kernelRate = 0.0;
  1697. } tape;
  1698. // round 148: [EXEC] EchoBoy's ping-pong (her edited style) on the official trial: an impulse's first pass and each
  1699. // recirculation are linear (round 136); its 3rd-order colour + eight diffusion taps left the first pass at −18 dB.
  1700. // The measured kernels themselves (4500 samples around each side's tap, 1280 around the loop period) are used now,
  1701. // resampled for other rates (linear interpolation of the 48 kHz kernel, scaled by 48000 / rate) [model]
  1702. void tapeKernels() noexcept
  1703. {
  1704. if (tape.kernelRate == sr && tape.first[0].ready())
  1705. return;
  1706. tape.kernelRate = sr;
  1707. auto resampled = [this] (const float* k, int n) {
  1708. const double ratio = 48000.0 / sr;
  1709. const int m = std::max (1, (int) std::lround (n / ratio));
  1710. std::vector<float> out ((std::size_t) m);
  1711. for (int i = 0; i < m; ++i)
  1712. {
  1713. const double t = i * ratio;
  1714. const int j = (int) t;
  1715. const double f = t - j;
  1716. const double a = j < n ? k[j] : 0.0, b = j + 1 < n ? k[j + 1] : 0.0;
  1717. out[(std::size_t) i] = (float) ((a + (b - a) * f) * ratio);
  1718. }
  1719. return out;
  1720. };
  1721. tape.first[0].setKernel (resampled (ppkernels::ppFirstL, (int) std::size (ppkernels::ppFirstL)));
  1722. tape.first[1].setKernel (resampled (ppkernels::ppFirstR, (int) std::size (ppkernels::ppFirstR)));
  1723. tape.loop[0].setKernel (resampled (ppkernels::ppLoopL, (int) std::size (ppkernels::ppLoopL)), 128);
  1724. tape.loop[1].setKernel (resampled (ppkernels::ppLoopR, (int) std::size (ppkernels::ppLoopR)), 128);
  1725. for (int c = 0; c < 2; ++c)
  1726. {
  1727. tape.firstOut[c].allocate (sr * 4.6 + 1300.0);
  1728. tape.loopOut[c].allocate (sr * 4.6 + 1300.0);
  1729. }
  1730. }
  1731. // the pass colour (direct form I, 3rd order)
  1732. static double colour (const TapeVoicingData& v, double x, double* xh, double* yh) noexcept
  1733. {
  1734. const double y = v.b[0] * x + v.b[1] * xh[0] + v.b[2] * xh[1] + v.b[3] * xh[2] - v.a[1] * yh[0] - v.a[2] * yh[1] - v.a[3] * yh[2];
  1735. xh[2] = xh[1]; xh[1] = xh[0]; xh[0] = x;
  1736. yh[2] = yh[1]; yh[1] = yh[0]; yh[0] = y;
  1737. return y;
  1738. }
  1739. static double tapeSide (double x, double c, double p) noexcept
  1740. {
  1741. return x / std::pow (1.0 + std::pow (std::abs (x / c), p), 1.0 / p);
  1742. }
  1743. void tapeProcess (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept
  1744. {
  1745. const auto& v = tapeData (tapeVoicingOf (p[tapeVoicing]));
  1746. if (tape.in[0].buf.empty() || tape.dirty)
  1747. {
  1748. for (int c = 0; c < 2; ++c)
  1749. {
  1750. if (tape.in[c].buf.empty())
  1751. {
  1752. tape.in[c].allocate (sr * 4.6 + 1300.0);
  1753. tape.z[c].allocate (sr * 4.6 + 1300.0);
  1754. }
  1755. tape.in[c].clear();
  1756. tape.z[c].clear();
  1757. if (tape.first[c].ready())
  1758. {
  1759. tape.first[c].reset();
  1760. tape.loop[c].reset();
  1761. tape.firstOut[c].clear();
  1762. tape.loopOut[c].clear();
  1763. }
  1764. for (int k = 0; k < 2; ++k)
  1765. for (int j = 0; j < 3; ++j)
  1766. tape.passX[c][k][j] = tape.passY[c][k][j] = 0.0;
  1767. for (int j = 0; j < 3; ++j)
  1768. tape.loopX[c][j] = tape.loopY[c][j] = 0.0;
  1769. }
  1770. tape.dirty = false;
  1771. }
  1772. const double T = sideMs (p, false, bpm) * sr / 1000.0;
  1773. const double maxD = tape.in[0].maxDelay() - 700.0;
  1774. const double G = std::clamp (feedbackGain (p[feedback]), 0.0, 1.2);
  1775. const double r2 = v.g0 > 0.0 ? std::sqrt (G / v.g0) : 0.0;
  1776. const int P = (int) std::clamp (std::lround (v.periodMult * T), 2L, (long) maxD);
  1777. float dryGain = 1.0f, wetGain = 0.0f;
  1778. equalPowerMix (p[mix], dryGain, wetGain); // [EXEC] her mix 50 / 53 % states: dry cos, wet sin
  1779. wetGain *= (float) std::pow (10.0, outputDb (p[output]) / 20.0);
  1780. static constexpr int ghostOff[2][8] { { -595, -493, -366, -300, 300, 366, 493, 595 },
  1781. { -641, -519, -465, -331, 331, 465, 519, 641 } }; // [EXEC] per side
  1782. static constexpr double ghostGain[8] { -0.1532, -0.1532, -0.1532, -0.1532, 0.1481, 0.1481, 0.1481, 0.1481 };
  1783. int d[2][2];
  1784. for (int c = 0; c < 2; ++c)
  1785. for (int k = 0; k < 2; ++k)
  1786. d[c][k] = (int) std::clamp (std::lround (v.side[c].mult[k] * T) + v.side[c].off[k], 1L, (long) maxD);
  1787. if (v.ghosts) // the ping-pong: the measured kernels (when the delays leave room for their lead-in)
  1788. {
  1789. tapeKernels();
  1790. const double rs = sr / 48000.0;
  1791. const int pre = (int) std::lround (ppkernels::ppPre * rs), loopPre = (int) std::lround (ppkernels::ppLoopPre * rs);
  1792. const int bFirst = tape.first[0].latency(), bLoop = tape.loop[0].latency();
  1793. int df[2];
  1794. for (int c = 0; c < 2; ++c)
  1795. df[c] = d[c][0] - pre - bFirst;
  1796. const int dl = P - loopPre - bLoop;
  1797. if (df[0] >= 1 && df[1] >= 1 && dl >= 1)
  1798. {
  1799. for (int i = 0; i < n; ++i)
  1800. {
  1801. const double xl = l[i], xr = r[i];
  1802. // the input curve and a gentle one in the loop, refitted with the kernels on her state-0 takes
  1803. // (tmp/vox148/eb/ebsatfit.py: go / kwe / lu −25.8 / −33.3 / −28.7 → −27.2 / −33.4 / −29.4 dB)
  1804. const double sat = tapeSide (xl + xr, 2.4933, 1.6387);
  1805. double out[2];
  1806. for (int c = 0; c < 2; ++c)
  1807. {
  1808. const double z = tape.firstOut[c].tap (df[c]) + G * tapeSide (tape.loopOut[c].tap (dl), 4.1677, 1.2692);
  1809. tape.firstOut[c].push (tape.first[c].push ((float) sat));
  1810. tape.loopOut[c].push (tape.loop[c].push ((float) z));
  1811. out[c] = z;
  1812. }
  1813. l[i] = dryGain * (float) xl + wetGain * (float) out[0];
  1814. r[i] = dryGain * (float) xr + wetGain * (float) out[1];
  1815. }
  1816. return;
  1817. }
  1818. }
  1819. for (int i = 0; i < n; ++i)
  1820. {
  1821. const double xl = l[i], xr = r[i];
  1822. double out[2];
  1823. for (int c = 0; c < 2; ++c)
  1824. {
  1825. const double raw = v.sum ? xl + xr : (c == 0 ? xl : xr);
  1826. const double sat = tapeSide (raw, v.satC, v.satP);
  1827. const auto& sd = v.side[c];
  1828. double acc = 0.0;
  1829. for (int k = 0; k < sd.taps; ++k)
  1830. {
  1831. const double g = k == 0 ? sd.gain[0] : sd.gain[0] * sd.gain[1] * r2;
  1832. double x = tape.in[c].tap (d[c][k]);
  1833. if (v.ghosts)
  1834. for (int j = 0; j < 8; ++j)
  1835. x += ghostGain[j] * tape.in[c].tap (d[c][k] + ghostOff[c][j]);
  1836. acc += g * colour (v, x, tape.passX[c][k], tape.passY[c][k]);
  1837. }
  1838. // the loop: G·F(z)[n − P]
  1839. const double y = colour (v, tape.z[c].tap (P), tape.loopX[c], tape.loopY[c]);
  1840. const double z = acc + G * y;
  1841. tape.in[c].push ((float) sat); // (after the reads: tap (d) = x[n − d])
  1842. tape.z[c].push ((float) z);
  1843. out[c] = z;
  1844. }
  1845. l[i] = dryGain * (float) xl + wetGain * (float) out[0];
  1846. r[i] = dryGain * (float) xr + wetGain * (float) out[1];
  1847. }
  1848. }
  1849. // ---- round 147: the saturation curve of the block (a voicing's own or a chosen type) -------------------------------
  1850. void prepareSaturation (const VoicingData* v, double driveDb) noexcept
  1851. {
  1852. satCurve = v;
  1853. if (v == nullptr)
  1854. return;
  1855. if (v->comp != 0)
  1856. {
  1857. satThreshold = satStep (v->thresholdDb, driveDb);
  1858. satMakeup = satStep (v->makeupDb, driveDb);
  1859. compAttack = 1.0 - std::exp (-1.0 / (0.004 * sr));
  1860. compRelease = 1.0 - std::exp (-1.0 / (0.120 * sr));
  1861. }
  1862. else
  1863. {
  1864. satDrive = std::pow (10.0, satStep (v->driveDb, driveDb) / 20.0);
  1865. satGain = std::pow (10.0, satStep (v->gainDb, driveDb) / 20.0);
  1866. satSlope = std::pow (1.0 + std::pow (std::abs ((double) v->b), (double) v->p), -1.0 / v->p - 1.0);
  1867. satKb = kneeShape ((double) v->b, (double) v->p);
  1868. }
  1869. }
  1870. float saturate (float x, int c) noexcept { return saturate (x, compEnv[c]); }
  1871. float saturate (float x, double& e) noexcept
  1872. {
  1873. const auto& v = *satCurve;
  1874. if (v.comp != 0)
  1875. {
  1876. const auto a = (double) std::abs (x);
  1877. e += (a - e) * (a > e ? compAttack : compRelease);
  1878. const auto lvl = 20.0 * std::log10 (std::max (1e-9, e));
  1879. const auto gdb = satMakeup - compressCurve (lvl - satThreshold, v.ratio);
  1880. return (float) (x * std::exp (gdb * 0.11512925464970228)); // 10^(dB / 20)
  1881. }
  1882. const auto u = satDrive * (double) x + (double) v.b;
  1883. const auto y = (kneeShape (u, (double) v.p) - satKb) / satSlope;
  1884. return (float) (satGain * y / satDrive);
  1885. }
  1886. const VoicingData* satCurve = nullptr;
  1887. double satDrive = 1.0, satGain = 1.0, satSlope = 1.0, satKb = 0.0, satThreshold = 0.0, satMakeup = 0.0;
  1888. double compAttack = 0.0, compRelease = 0.0, compEnv[2] {};
  1889. std::array<Biquad, 9> outQ {};
  1890. std::array<Biquad, 4> loopQ {};
  1891. int outN = 0, loopN = 0, toneVoicing = -1;
  1892. double toneSr = 0.0, voicePhase = 0.0, wobbleMs = 0.0;
  1893. float outLin = 1.0f;
  1894. Butter8 multLp1, multLp2;
  1895. // round 150: the L/R offset's second loop per side (the even repeats; its own write path state) and the ping-pong
  1896. // pair tone
  1897. struct SecondLoop
  1898. {
  1899. Line line;
  1900. std::array<Allpass, 4> diff;
  1901. OnePole lowCut, highCut;
  1902. };
  1903. std::array<SecondLoop, 2> bpath;
  1904. std::array<Biquad, 4> loopQB {};
  1905. Biquad eqBassB, eqMidB, eqTrebleB;
  1906. Butter8 lofiLpB, multLpB1, multLpB2;
  1907. double compEnvB[2] {};
  1908. std::array<Biquad, 6> pairQ {};
  1909. int pairN = 0, pairVoicing = -1;
  1910. float pairLin = 1.0f;
  1911. double pairSr = 0.0;
  1912. double sr = 48000.0;
  1913. std::array<Channel, 2> ch;
  1914. Butter8 steep;
  1915. Butter8 lofiLp; // round 137: the LoFi switch
  1916. Biquad eqBass, eqMid, eqTreble; // round 144: the loop EQ (both channels)
  1917. Shaper shaper;
  1918. ModSource modulation = ModSource::tape;
  1919. double modAmountMs = 6.1;
  1920. double wowAmountMs = 1.15, flutterAmountMs = 0.2;
  1921. double lfoPhase = 0.0;
  1922. float envelope = 0.0f;
  1923. double duckGr = 0.0; // round 137: the ducking's gain reduction (dB)
  1924. };
  1925. } // namespace
  1926. } // namespace echoform
  1927. // =====================================================================================================================
  1928. // Spiral Echo
  1929. // =====================================================================================================================
  1930. namespace spiral
  1931. {
  1932. double shiftHz (float v) noexcept { return 1000.0 * signedPow (bipolar (v), 5.0); }
  1933. double delayMs (float v) noexcept { return 0.01 + 999.99 * std::pow ((double) clamp01 (v), 3.322); }
  1934. float delayValue (double ms) noexcept { return (float) std::pow (std::clamp ((ms - 0.01) / 999.99, 0.0, 1.0), 1.0 / 3.322); }
  1935. int syncIndex (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 23.0f), 0, 23); } // as measured: floor (23 v) in float
  1936. double syncBeats (int index) noexcept
  1937. {
  1938. if (index <= 0 || index > 23)
  1939. return 0.0;
  1940. if (index <= 8)
  1941. return std::pow (2.0, (double) index - 5.0); // 1/64 = 1/16 beat … 2/1 = 8 beats
  1942. if (index <= 15)
  1943. return 1.5 * std::pow (2.0, (double) (index - 8) - 5.0); // dotted 1/64 … 1/1
  1944. return 2.0 / 3.0 * std::pow (2.0, (double) (index - 15) - 5.0); // triplet 1/64 … 2/1
  1945. }
  1946. double loopGain (float v) noexcept { return 1.5 * clamp01 (v); }
  1947. double lowCutHz (float v) noexcept { return 20.0 + 2980.0 * clamp01 (v); }
  1948. double highCutHz (float v) noexcept { return 50.0 + 14950.0 * clamp01 (v); }
  1949. namespace
  1950. {
  1951. class SpiralEcho final : public Effect
  1952. {
  1953. public:
  1954. void prepare (double rate) noexcept override
  1955. {
  1956. sr = rate;
  1957. for (auto& c : ch)
  1958. c.line.allocate (sr * 12.0);
  1959. reset();
  1960. }
  1961. void reset() noexcept override
  1962. {
  1963. for (auto& c : ch)
  1964. {
  1965. c.line.clear();
  1966. c.shifter.reset();
  1967. c.lo.reset();
  1968. c.hi.reset();
  1969. }
  1970. time = {};
  1971. }
  1972. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  1973. {
  1974. if (ch[0].line.buf.empty())
  1975. prepare (sr);
  1976. const auto wet = clamp01 (p[mix]), dry = 1.0f - wet; // linear crossfade (REA_rodada101 §4)
  1977. const auto beats = syncBeats (syncIndex (p[sync]));
  1978. const auto ms = beats > 0.0 ? beats * 60000.0 / (bpm > 1.0 ? bpm : 120.0) : delayMs (p[delay]);
  1979. const auto target = std::clamp (ms * 0.001 * sr, 2.0, ch[0].line.maxDelay() - 4.0);
  1980. const auto g = (float) loopGain (p[feedback]);
  1981. const auto hz = shiftHz (p[shift]);
  1982. const auto stereoMode = on (p[stereo]);
  1983. for (auto& c : ch)
  1984. {
  1985. c.lo.set (lowCutHz (p[lowCut]), sr);
  1986. c.hi.set (highCutHz (p[highCut]), sr);
  1987. }
  1988. for (int i = 0; i < n; ++i)
  1989. {
  1990. const auto d = time.next (target, 0.05, sr);
  1991. float* io[2] = { l + i, r + i };
  1992. for (int c = 0; c < 2; ++c)
  1993. {
  1994. auto& s = ch[(std::size_t) c];
  1995. const auto x = *io[c];
  1996. // every pass around the loop shifts by +hz; in stereo the right output is the same repeat taken
  1997. // with the opposite shift (measured: L 1100 1200 1300 …, R 900 1000 1100 … for +100 Hz)
  1998. const auto y = s.line.at (d);
  1999. float up = 0.0f, down = 0.0f;
  2000. s.shifter.both (s.hi.lp (s.lo.hp (y)), hz, sr, up, down);
  2001. s.line.push (x + g * std::tanh (up)); // the loop is bounded by a soft limiter
  2002. *io[c] = dry * x + wet * (c == 1 && stereoMode ? down : up);
  2003. }
  2004. }
  2005. }
  2006. private:
  2007. struct Channel
  2008. {
  2009. Line line;
  2010. Shifter shifter;
  2011. OnePole lo, hi;
  2012. };
  2013. double sr = 48000.0;
  2014. std::array<Channel, 2> ch;
  2015. Glide time;
  2016. };
  2017. } // namespace
  2018. } // namespace spiral
  2019. // =====================================================================================================================
  2020. // Orbit
  2021. // =====================================================================================================================
  2022. namespace orbit
  2023. {
  2024. double rateHz (float v) noexcept { return 10.0 * signedPow (bipolar (v), 3.333); }
  2025. double depthMs (float v) noexcept { return 50.0 * std::pow ((double) clamp01 (v), 2.322); }
  2026. double manualMs (float v) noexcept { return 50.0 * signedPow (bipolar (v), 4.0); }
  2027. double feedbackGain (float v) noexcept { return 0.7 * signedPow (bipolar (v), 1.5); } // measured ≈ 0.22 at 50 %, 0.7 at 100 %
  2028. Mode modeOf (float v) noexcept { return (Mode) std::clamp ((int) std::floor (clamp01 (v) * (float) modeCount + 1e-3f) - 1, 0, modeCount - 1); }
  2029. float modeValue (Mode m) noexcept { return ((float) m + 1.0f) / (float) modeCount; }
  2030. const char* modeName (Mode m) noexcept
  2031. {
  2032. static const char* const names[modeCount] = { "Rise", "Rise / fall", "Through-zero +", "Through-zero −", "Tri chorus", "Octo chorus",
  2033. "Doubler", "Drift rise", "Drift rise / fall", "Ensemble", "Symphonic" };
  2034. return names[std::clamp ((int) m, 0, modeCount - 1)];
  2035. }
  2036. namespace
  2037. {
  2038. class Orbit final : public Effect
  2039. {
  2040. public:
  2041. void prepare (double rate) noexcept override
  2042. {
  2043. sr = rate;
  2044. for (auto& c : ch)
  2045. c.line.allocate (sr * 0.25);
  2046. reset();
  2047. }
  2048. void reset() noexcept override
  2049. {
  2050. std::uint32_t seed = 0xabcdef1u;
  2051. for (auto& c : ch)
  2052. {
  2053. c.line.clear();
  2054. for (auto& rl : c.random)
  2055. rl.seed (seed += 0x9e3779b9u);
  2056. c.fb = 0.0f;
  2057. }
  2058. phase = fast = 0.0;
  2059. speedRandom.seed (0x51ed27u);
  2060. }
  2061. void process (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  2062. {
  2063. if (ch[0].line.buf.empty())
  2064. prepare (sr);
  2065. float dryGain = 1.0f, wetGain = 0.0f;
  2066. equalPowerMix (p[mix], dryGain, wetGain);
  2067. const auto m = modeOf (p[mode]);
  2068. const auto hz = rateHz (p[rate]);
  2069. const auto dm = depthMs (p[depth]) * 0.001 * sr;
  2070. const auto base = (0.25 + std::abs (manualMs (p[manual]))) * 0.001 * sr;
  2071. const auto g = (float) feedbackGain (p[feedback]);
  2072. rateNow = hz;
  2073. for (int i = 0; i < n; ++i)
  2074. {
  2075. auto step = hz / sr;
  2076. if (m == Mode::driftRise || m == Mode::driftRiseFall)
  2077. step *= 1.0 + 0.5 * speedRandom.next (std::max (0.05, std::abs (hz)), sr) / 2.1;
  2078. phase += step;
  2079. phase -= std::floor (phase);
  2080. fast += 7.3 * step;
  2081. fast -= std::floor (fast);
  2082. const float in[2] = { l[i], r[i] };
  2083. for (int c = 0; c < 2; ++c)
  2084. {
  2085. auto& s = ch[(std::size_t) c];
  2086. const auto wet = voices (s, c, m, base, dm);
  2087. s.line.push (in[c] + g * s.fb);
  2088. s.fb = wet;
  2089. const auto out = dryGain * in[c] + wetGain * wet;
  2090. (c == 0 ? l : r)[i] = out;
  2091. }
  2092. }
  2093. }
  2094. private:
  2095. struct Channel
  2096. {
  2097. Line line;
  2098. std::array<RandomLfo, 4> random;
  2099. float fb = 0.0f;
  2100. };
  2101. static double frac (double x) noexcept { return x - std::floor (x); }
  2102. static double sine (double p) noexcept { return std::sin (twoPi * p); }
  2103. float voices (Channel& s, int c, Mode m, double base, double dm) noexcept
  2104. {
  2105. const auto at = [&s] (double d) { return s.line.at (std::max (2.0, d)); };
  2106. const auto off = c == 0 ? 0.0 : 0.25;
  2107. switch (m)
  2108. {
  2109. case Mode::rise:
  2110. case Mode::driftRise:
  2111. {
  2112. // barber pole: two taps ramping across the range, each faded in / out at the ends (sin window)
  2113. const auto lo = m == Mode::rise ? 0.25 : 0.02, span = m == Mode::rise ? 0.5 : 0.96;
  2114. float y = 0.0f;
  2115. for (int k = 0; k < 2; ++k)
  2116. {
  2117. const auto q = frac (phase + off + 0.5 * k);
  2118. const auto w = (float) std::sin (pi * q);
  2119. y += w * w * at (base + dm * (lo + span * (1.0 - q)));
  2120. }
  2121. return y;
  2122. }
  2123. case Mode::riseFall:
  2124. case Mode::driftRiseFall:
  2125. {
  2126. const auto q = frac (phase + off);
  2127. const auto tri = 1.0 - std::abs (2.0 * q - 1.0);
  2128. const auto lo = m == Mode::riseFall ? 0.25 : 0.02, span = m == Mode::riseFall ? 0.5 : 0.96;
  2129. return at (base + dm * (lo + span * tri));
  2130. }
  2131. case Mode::throughZeroPlus:
  2132. case Mode::throughZeroMinus:
  2133. {
  2134. // a fixed tap at the top of the sweep and one sweeping up to (and just past) it: through-zero
  2135. const auto fixed = base + 2.36 * dm;
  2136. const auto moving = base + dm * (0.65 + 1.75 * (0.5 + 0.5 * sine (phase + (c == 0 ? 0.0 : 0.02))));
  2137. const auto sign = m == Mode::throughZeroMinus ? -1.0f : 1.0f;
  2138. return 0.5f * at (fixed) + sign * 0.5f * at (moving);
  2139. }
  2140. case Mode::tri:
  2141. {
  2142. float y = 0.0f;
  2143. for (int k = 0; k < 3; ++k)
  2144. y += at (base + dm * (0.5 + 0.5 * sine (phase + k / 3.0 + (c == 0 ? 0.0 : 1.0 / 6.0))));
  2145. return y * 0.57735f;
  2146. }
  2147. case Mode::octo:
  2148. {
  2149. float y = 0.0f;
  2150. for (int k = 0; k < 4; ++k)
  2151. y += at (base + dm * (0.15 + 0.7 * (0.5 + 0.5 * sine (phase + (2 * k + c) / 8.0))));
  2152. return 0.5f * y;
  2153. }
  2154. case Mode::doubler:
  2155. 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)));
  2156. case Mode::ensemble:
  2157. {
  2158. float y = 0.0f;
  2159. for (int k = 0; k < 2; ++k)
  2160. {
  2161. const auto q = phase + (2 * k + c) / 6.0;
  2162. y += at (base + dm * (0.5 + 0.35 * sine (q) + 0.15 * sine (fast + q)));
  2163. }
  2164. return 0.70711f * y;
  2165. }
  2166. case Mode::symphonic:
  2167. {
  2168. float y = 0.0f;
  2169. for (int k = 0; k < 2; ++k)
  2170. {
  2171. const auto q = phase + (2 * k + c) / 4.0;
  2172. const auto rnd = std::clamp ((double) s.random[(std::size_t) k + 1].next (0.7, sr) / 2.1, -1.0, 1.0);
  2173. y += at (base + dm * (0.5 + 0.3 * sine (q) + 0.18 * rnd));
  2174. }
  2175. return 0.70711f * y;
  2176. }
  2177. }
  2178. return 0.0f;
  2179. }
  2180. double sr = 48000.0;
  2181. std::array<Channel, 2> ch;
  2182. RandomLfo speedRandom;
  2183. double phase = 0.0, fast = 0.0, rateNow = 0.5;
  2184. };
  2185. } // namespace
  2186. } // namespace orbit
  2187. // =====================================================================================================================
  2188. // Lattice Mod
  2189. // =====================================================================================================================
  2190. namespace lattice
  2191. {
  2192. int inputPanOf (float v) noexcept { return std::clamp ((int) std::lround (clamp01 (v) * 8.0f), 0, 8); }
  2193. namespace
  2194. {
  2195. // the older Sync law (by length; kept for projects saved before round 144)
  2196. double syncBeats (int index) noexcept
  2197. {
  2198. 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,
  2199. 3.0 / 8, 1.0 / 2, 2.0 / 3, 3.0 / 4, 1.0, 4.0 / 3, 3.0 / 2, 2.0, 4.0 };
  2200. return beats[std::clamp (index, 0, 17)];
  2201. }
  2202. } // namespace
  2203. int delaySyncIndex (float v) noexcept { return std::clamp ((int) std::floor (clamp01 (v) * 17.0f + 1e-5f), 0, 17); }
  2204. double delaySyncBeats (int index) noexcept
  2205. {
  2206. // straight 1/16 … 2/1, dotted 1/16. … 1/1., triplet 1/16T … 1/1T (quarter notes)
  2207. static constexpr double beats[18] = { 0.0, 0.0, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0, 0.375, 0.75, 1.5, 3.0, 6.0,
  2208. 1.0 / 6.0, 1.0 / 3.0, 2.0 / 3.0, 4.0 / 3.0, 8.0 / 3.0 };
  2209. return beats[std::clamp (index, 0, 17)];
  2210. }
  2211. double syncedDelayMs (const EffectParams& p, double bpm) noexcept
  2212. {
  2213. const auto tempo = bpm > 1.0 ? bpm : 120.0;
  2214. if (const auto b = delaySyncBeats (delaySyncIndex (p[delaySync])); b > 0.0)
  2215. return std::min (delaySyncCapMs, b * 60000.0 / tempo);
  2216. const auto old = syncBeats ((int) std::lround (clamp01 (p[sync]) * 17.0f));
  2217. return old > 0.0 ? old * 60000.0 / tempo : 0.0;
  2218. }
  2219. int tapCount (float v) noexcept
  2220. {
  2221. static constexpr int counts[10] = { 1, 1, 2, 3, 3, 3, 4, 8, 8, 16 };
  2222. return counts[std::clamp ((int) std::floor (clamp01 (v) * 24.0f + 1e-4f), 0, 9)];
  2223. }
  2224. double delayMs (float v) noexcept { return 1000.0 * clamp01 (v); }
  2225. double tapMs (const EffectParams& p, int tap, int taps, bool right) noexcept
  2226. {
  2227. const auto d = delayMs (p[delay]) * (on (p[halfSpeed]) ? 2.0 : 1.0);
  2228. auto t = d * (1.0 - (double) clamp01 (p[spread]) * (double) tap / (double) std::max (1, taps));
  2229. const auto skewV = bipolar (p[skew]);
  2230. if (! right && skewV < 0.0)
  2231. t *= 1.0 + 0.5 * skewV;
  2232. if (right && skewV > 0.0)
  2233. t *= 1.0 - 0.5 * skewV;
  2234. // random: a fixed scatter of each tap by up to half the tap spacing
  2235. if (p[random] > 0.0f && taps > 1)
  2236. {
  2237. const auto h = std::sin ((double) (tap * 7919 + (right ? 104729 : 0)) * 12.9898) * 43758.5453;
  2238. const auto u = (h - std::floor (h)) * 2.0 - 1.0;
  2239. t += u * 0.5 * (double) clamp01 (p[random]) * d * (double) clamp01 (p[spread]) / (double) taps;
  2240. }
  2241. return std::max (0.0, t);
  2242. }
  2243. double tapLevel (const EffectParams& p, int tap, int taps) noexcept
  2244. {
  2245. auto gain = std::pow (10.0, 12.0 * bipolar (p[tapGain]) / 20.0) / std::sqrt ((double) std::max (1, taps));
  2246. if (taps > 1)
  2247. {
  2248. const auto pos = 1.0 - 2.0 * (double) tap / (double) (taps - 1); // +1 at the longest tap, −1 at the shortest
  2249. gain *= std::pow (10.0, 6.0 * bipolar (p[slope]) * pos / 20.0);
  2250. }
  2251. return -gain; // the measured wet is polarity-inverted (REA_rodada101 §6)
  2252. }
  2253. namespace
  2254. {
  2255. class LatticeMod final : public Effect
  2256. {
  2257. public:
  2258. void prepare (double rate) noexcept override
  2259. {
  2260. sr = rate;
  2261. for (auto& c : ch)
  2262. {
  2263. c.line.allocate (sr * 6.0);
  2264. for (auto& a : c.diff)
  2265. a.line.allocate (sr * 1.1);
  2266. }
  2267. reset();
  2268. }
  2269. void reset() noexcept override
  2270. {
  2271. std::uint32_t seed = 0x7a11ce5u;
  2272. for (auto& c : ch)
  2273. {
  2274. c.line.clear();
  2275. for (auto& a : c.diff)
  2276. a.line.clear();
  2277. for (auto& rl : c.detune)
  2278. rl.seed (seed += 0x9e3779b9u);
  2279. c.diffLfo.seed (seed += 0x9e3779b9u);
  2280. c.noise.s = seed += 0x9e3779b9u;
  2281. c.lo.reset();
  2282. c.hi.reset();
  2283. c.dark.reset();
  2284. c.xover.reset();
  2285. c.fb = 0.0f;
  2286. }
  2287. vibPhase = 0.0;
  2288. base = {};
  2289. }
  2290. void process (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  2291. {
  2292. if (ch[0].line.buf.empty())
  2293. prepare (sr);
  2294. float dryGain = 1.0f, wetGain = 0.0f;
  2295. equalPowerMix (p[mix], dryGain, wetGain);
  2296. const auto nTaps = tapCount (p[lattice::taps]);
  2297. const auto half = on (p[halfSpeed]);
  2298. const auto syncMs = syncedDelayMs (p, bpm); // round 144: the reference's DelaySync law, else the older one
  2299. const auto delayScale = syncMs > 0.0 && delayMs (p[delay]) > 0.0 ? syncMs / delayMs (p[delay]) : 1.0;
  2300. std::array<std::array<double, 16>, 2> tap {};
  2301. std::array<double, 16> gains {};
  2302. for (int k = 0; k < nTaps; ++k)
  2303. {
  2304. tap[0][(std::size_t) k] = tapMs (p, k, nTaps, false) * delayScale * 0.001 * sr;
  2305. tap[1][(std::size_t) k] = tapMs (p, k, nTaps, true) * delayScale * 0.001 * sr;
  2306. gains[(std::size_t) k] = tapLevel (p, k, nTaps);
  2307. }
  2308. const auto master = (double) clamp01 (p[depth]) * (1.0 + 99.0 * (double) clamp01 (p[overMod]));
  2309. const auto detuneHz = (0.01 + 9.99 * (double) clamp01 (p[detuneRate])) * (half ? 0.5 : 1.0);
  2310. const auto vibHz = (0.05 + 19.95 * (double) clamp01 (p[vibratoRate])) * (half ? 0.5 : 1.0);
  2311. // depth in delay samples: the pitch swing stays the same at any rate (detune ±25 c, vibrato ±174 c at 100 %)
  2312. 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);
  2313. 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);
  2314. const auto g = (float) clamp01 (p[feedback]);
  2315. const auto theta = pi * (double) clamp01 (p[rotate]);
  2316. const auto cr = (float) std::cos (theta), sn = (float) std::sin (theta);
  2317. const auto widthV = 2.0f * clamp01 (p[width]);
  2318. const auto useDiff = on (p[diffOn]);
  2319. const auto diffG = 0.7f * clamp01 (p[diffusion]);
  2320. const auto diffSizeMs = 10.0 + 490.0 * (double) clamp01 (p[diffSize]);
  2321. const auto diffRate = 0.01 + 9.99 * (double) clamp01 (p[diffModRate]);
  2322. const auto diffDepth = (double) clamp01 (p[diffModDepth]);
  2323. const auto useDrive = on (p[driveOn]), post = on (p[drivePost]);
  2324. const auto inGain = (float) std::pow (10.0, 24.0 * clamp01 (p[driveIn]) / 20.0);
  2325. const auto outGain = (float) std::pow (10.0, (-24.0 + 24.0 * clamp01 (p[driveOut])) / 20.0);
  2326. const auto noiseLevel = (float) std::pow (10.0, (-120.0 + 90.0 * clamp01 (p[noise])) / 20.0);
  2327. const auto smoothS = (1.0 + 999.0 * (double) clamp01 (p[smoothing])) * 0.001;
  2328. const auto isDark = on (p[colour]);
  2329. const auto panMode = inputPanOf (p[inputPan]);
  2330. static constexpr double diffRatios[4] = { 0.131, 0.217, 0.293, 0.359 };
  2331. for (auto& c : ch)
  2332. {
  2333. c.lo.set (10.0 + 1990.0 * (double) clamp01 (p[lowCut]), sr);
  2334. if (p[highCut] >= 0.999f)
  2335. c.hi.open();
  2336. else
  2337. c.hi.set (100.0 + 19900.0 * (double) clamp01 (p[highCut]), sr);
  2338. if (isDark) // measured "dark": about a one-pole at 13 kHz; "bright" is flat
  2339. c.dark.set (13000.0, sr);
  2340. else
  2341. c.dark.open();
  2342. c.xover.set (10.0 + 1990.0 * (double) clamp01 (p[xover]), sr);
  2343. }
  2344. const auto driveFn = [inGain, outGain] (float x) { return outGain * std::tanh (inGain * x); };
  2345. for (int i = 0; i < n; ++i)
  2346. {
  2347. const auto scale = base.next (1.0, smoothS, sr); // glides the taps after a change (smoothing time)
  2348. vibPhase += vibHz / sr;
  2349. vibPhase -= std::floor (vibPhase);
  2350. float wet[2];
  2351. const float in[2] = { l[i], r[i] };
  2352. // round 137: Input pan (the reference's InputPan, measured [EXEC]): what enters the taps
  2353. const auto monoIn = 0.5f * (in[0] + in[1]);
  2354. const float src[2] = { panMode == 2 ? in[1] : panMode == 3 || panMode == 4 ? monoIn : panMode == 5 ? 0.0f : in[0],
  2355. panMode == 2 ? in[0] : panMode == 3 || panMode == 5 ? monoIn : panMode == 4 ? 0.0f : in[1] };
  2356. for (int c = 0; c < 2; ++c)
  2357. {
  2358. auto& s = ch[(std::size_t) c];
  2359. const auto sign = c == 0 ? 1.0 : -1.0;
  2360. double y = 0.0;
  2361. for (int k = 0; k < nTaps; ++k)
  2362. {
  2363. const auto det = detuneAmp > 0.0 ? detuneAmp * (double) s.detune[(std::size_t) k].next (detuneHz, sr) / 2.1 : 0.0;
  2364. const auto vib = vibAmp > 0.0 ? vibAmp * std::sin (twoPi * (vibPhase + (double) k / (double) nTaps)) : 0.0;
  2365. const auto d = tap[(std::size_t) c][(std::size_t) k] * scale + std::abs (det) + vibAmp + sign * (det + vib) * 0.5;
  2366. y += gains[(std::size_t) k] * (double) s.line.at (std::max (2.0, d));
  2367. }
  2368. auto v = (float) y;
  2369. v = s.dark.lp (s.hi.lp (s.lo.hp (v)));
  2370. if (useDrive && post)
  2371. v = driveFn (v) + noiseLevel * s.noise.next();
  2372. wet[c] = v;
  2373. }
  2374. // feedback, rotated between the channels
  2375. const auto fl = cr * wet[0] - sn * wet[1];
  2376. const auto fr = sn * wet[0] + cr * wet[1];
  2377. const float fbv[2] = { fl, fr };
  2378. for (int c = 0; c < 2; ++c)
  2379. {
  2380. auto& s = ch[(std::size_t) c];
  2381. auto x = src[c];
  2382. if (useDrive && ! post)
  2383. x = driveFn (x) + noiseLevel * s.noise.next();
  2384. if (useDiff && diffG > 0.0f)
  2385. {
  2386. const auto mod = 1.0 + 0.02 * diffDepth * (double) s.diffLfo.next (diffRate, sr) / 2.1;
  2387. for (std::size_t k = 0; k < 4; ++k)
  2388. x = s.diff[k].process (x, diffSizeMs * diffRatios[k] * mod * (c == 1 ? 1.05 : 1.0) * 0.001 * sr, diffG);
  2389. }
  2390. s.line.push (x + g * softLimit (fbv[c]));
  2391. }
  2392. // spatial crossover (the side below it is removed) and width
  2393. const auto m = 0.5f * (wet[0] + wet[1]);
  2394. auto sd = 0.5f * (wet[0] - wet[1]);
  2395. sd = ch[0].xover.hp (sd) * widthV;
  2396. l[i] = dryGain * in[0] + wetGain * (m + sd);
  2397. r[i] = dryGain * in[1] + wetGain * (m - sd);
  2398. }
  2399. }
  2400. private:
  2401. struct Channel
  2402. {
  2403. Line line;
  2404. std::array<Allpass, 4> diff;
  2405. std::array<RandomLfo, 16> detune;
  2406. RandomLfo diffLfo;
  2407. Noise noise;
  2408. OnePole lo, hi, dark, xover;
  2409. float fb = 0.0f;
  2410. };
  2411. double sr = 48000.0;
  2412. std::array<Channel, 2> ch;
  2413. double vibPhase = 0.0;
  2414. Glide base;
  2415. };
  2416. } // namespace
  2417. } // namespace lattice
  2418. // =====================================================================================================================
  2419. // registry
  2420. // =====================================================================================================================
  2421. const EffectInfo& echoformInfo()
  2422. {
  2423. static const EffectInfo info { "Echoform",
  2424. { { "Mix", 0.5f }, { "Style", 0.1f }, { "Sync L", 0.25f }, { "Note L", 0.45f }, { "Time L", 0.3f },
  2425. { "Sync R", 0.25f }, { "Note R", 0.45f }, { "Time R", 0.3f }, { "Spread", 0.5f }, { "Spacing", 0.5f },
  2426. { "Ratio", 0.6141414f }, { "Feedback", 0.35f }, { "Width", 1.0f }, { "Drive", 0.0f }, { "Age", 0.5f },
  2427. { "Diffusion", 0.0f }, { "Diffusion size", 1.0f }, { "Low cut", 0.0f }, { "High cut", 1.0f },
  2428. { "Mod rate", 0.2738341f }, { "Mod depth", 0.5f }, { "Wow", 0.5f }, { "Flutter", 0.5f },
  2429. { "Shift", 0.5f }, { "Shift detune", 0.58f }, { "Pitch", 0.5f }, { "Pitch detune", 0.5f },
  2430. { "Mode", 0.5f / 12.0f }, { "Ducking", 0.0f }, { "Output", 0.5f }, { "Pairs", 0.0f },
  2431. { "Phase L", 0.0f }, { "Phase R", 0.0f },
  2432. { "Hybrid", 0.0f },
  2433. // round 135w (appended; the defaults keep the old sound)
  2434. { "Tempo source", 0.0f }, { "BPM", 1.0f / 3.0f }, { "Start side", 0.0f }, { "Filter link", 0.0f },
  2435. { "Tap A", 1.0f }, { "Tap B", 1.0f }, { "Tap C", 1.0f }, { "Tap D", 1.0f }, { "Swell", 0.0f },
  2436. { "Groove", 0.5f }, { "Feel", 0.5f }, { "Accent", 0.5f }, { "Prime numbers", 0.0f },
  2437. { "Cross feedback", 0.0f }, { "Balance", 0.5f }, { "Freeze", 0.0f },
  2438. // round 137: Rhythm mode (appended; off = the old sound); 1/8 note, 8 repeats, 2 beats
  2439. { "Rhythm", 0.0f }, { "Rhythm sync", 0.5f }, { "Rhythm note", 0.35f }, { "Rhythm time", 0.3f },
  2440. { "Repeats", 7.0f / 15.0f }, { "Decay", 0.0f }, { "Length", 1.0f / 7.0f },
  2441. { "Era", 0.0f },
  2442. { "Tap 1", 1.0f }, { "Tap 2", 1.0f }, { "Tap 3", 1.0f }, { "Tap 4", 1.0f }, { "Tap 5", 1.0f },
  2443. { "Tap 6", 1.0f }, { "Tap 7", 1.0f }, { "Tap 8", 1.0f }, { "Tap 9", 1.0f }, { "Tap 10", 1.0f },
  2444. { "Tap 11", 1.0f }, { "Tap 12", 1.0f }, { "Tap 13", 1.0f }, { "Tap 14", 1.0f }, { "Tap 15", 1.0f },
  2445. { "LoFi", 0.0f },
  2446. { "Tape voicing", 0.0f }, // round 136 (after round 137's)
  2447. // round 144: the rhythm editor (appended; Even = the round-137 pattern)
  2448. { "Rhythm edit", 0.0f }, { "Rhythm grid", 3.5f / 6.0f }, { "Rhythm shape", 0.0f },
  2449. { "Tap 1 time", 1.0f / 16.0f }, { "Tap 2 time", 2.0f / 16.0f }, { "Tap 3 time", 3.0f / 16.0f },
  2450. { "Tap 4 time", 4.0f / 16.0f }, { "Tap 5 time", 5.0f / 16.0f }, { "Tap 6 time", 6.0f / 16.0f },
  2451. { "Tap 7 time", 7.0f / 16.0f }, { "Tap 8 time", 8.0f / 16.0f }, { "Tap 9 time", 9.0f / 16.0f },
  2452. { "Tap 10 time", 10.0f / 16.0f }, { "Tap 11 time", 11.0f / 16.0f }, { "Tap 12 time", 12.0f / 16.0f },
  2453. { "Tap 13 time", 13.0f / 16.0f }, { "Tap 14 time", 14.0f / 16.0f }, { "Tap 15 time", 15.0f / 16.0f },
  2454. // round 144: the Style-Edit-role controls (appended; the defaults = the old sound)
  2455. { "Input level", 0.5f }, { "FB balance", 0.5f }, { "Diffusion post", 0.0f },
  2456. { "Mod shape", 0.0f }, { "Mod sync", 0.0f },
  2457. { "Loop bass", 0.5f }, { "Loop mid", 0.5f }, { "Loop treble", 0.5f },
  2458. // round 147: the echo voicings, the saturation type, Multiply (appended; off = the old sound)
  2459. { "Echo voicing", 0.0f }, { "Saturation type", 0.0f }, { "Multiply", 0.0f },
  2460. // round 150: the voicings' measured stereo behaviour, EchoBoy's L/R offset (appended;
  2461. // projects saved before load with it off)
  2462. { "Voicing stereo", 1.0f }, { "L/R offset", 8.0f / 25.0f } } };
  2463. return info;
  2464. }
  2465. const EffectInfo& spiralInfo()
  2466. {
  2467. static const EffectInfo info { "Spiral Echo",
  2468. { { "Mix", 0.5f }, { "Shift", 0.5f }, { "Delay", 0.5f }, { "Sync", 0.0f }, { "Feedback", 0.5f },
  2469. { "Low cut", 0.0604027f }, { "High cut", 0.3311037f }, { "Stereo", 1.0f } } };
  2470. return info;
  2471. }
  2472. const EffectInfo& orbitInfo()
  2473. {
  2474. static const EffectInfo info { "Orbit", { { "Mix", 0.5f }, { "Rate", 0.626f }, { "Depth", 0.5f }, { "Feedback", 0.5f },
  2475. { "Manual", 0.5f }, { "Mode", 1.0f / 11.0f } } };
  2476. return info;
  2477. }
  2478. const EffectInfo& latticeInfo()
  2479. {
  2480. static const EffectInfo info { "Lattice Mod",
  2481. { { "Mix", 0.5f }, { "Depth", 0.5f }, { "Width", 0.5f }, { "Delay", 0.01f }, { "Feedback", 0.0f },
  2482. { "Spread", 0.5f }, { "Slope", 0.5f }, { "Skew", 0.5f }, { "Random", 0.0f }, { "Tap gain", 0.5f },
  2483. { "Diffusion", 0.5f }, { "Diffusion size", 0.0204082f }, { "Diffusion rate", 0.049049f },
  2484. { "Diffusion depth", 0.5f }, { "Low cut", 0.0f }, { "High cut", 1.0f }, { "Crossover", 0.1457286f },
  2485. { "Detune rate", 0.049049f }, { "Detune depth", 0.5f }, { "Vibrato rate", 0.2982456f },
  2486. { "Vibrato depth", 0.0f }, { "Over-mod", 0.0f }, { "Drive in", 0.375f }, { "Drive out", 0.75f },
  2487. { "Noise", 0.3333333f }, { "Rotate", 0.5f }, { "Smoothing", 0.0990991f }, { "Colour", 0.0f },
  2488. { "Sync", 0.0f }, { "Taps", 1.5f / 24.0f } /* round 129: two taps (one a side), the reference default */, { "Diffusion on", 0.0f }, { "Drive on", 0.0f },
  2489. { "Drive post", 1.0f }, { "Half speed", 0.0f },
  2490. { "Input pan", 0.125f } /* round 137: index 1 = stereo, the reference default */,
  2491. { "Delay sync", 0.0f } /* round 144: the reference's DelaySync law, free */ } };
  2492. return info;
  2493. }
  2494. bool isSuiteKind (EffectKind kind) noexcept
  2495. {
  2496. return kind == EffectKind::echoform || kind == EffectKind::spiralEcho || kind == EffectKind::orbit || kind == EffectKind::latticeMod;
  2497. }
  2498. std::unique_ptr<Effect> make (EffectKind kind)
  2499. {
  2500. switch (kind)
  2501. {
  2502. case EffectKind::echoform: return std::make_unique<echoform::Echoform>();
  2503. case EffectKind::spiralEcho: return std::make_unique<spiral::SpiralEcho>();
  2504. case EffectKind::orbit: return std::make_unique<orbit::Orbit>();
  2505. case EffectKind::latticeMod: return std::make_unique<lattice::LatticeMod>();
  2506. default: return nullptr;
  2507. }
  2508. }
  2509. std::string paramText (EffectKind kind, int param, float v)
  2510. {
  2511. v = clamp01 (v);
  2512. if (kind == EffectKind::echoform)
  2513. {
  2514. using namespace echoform;
  2515. switch (param)
  2516. {
  2517. case mix: case age: case diffSize: case modDepth: case wow: case flutter: case ducking: return pct (v * 100.0);
  2518. case style: return styleNames[(int) styleOf (v)];
  2519. case syncL: case syncR:
  2520. {
  2521. static const char* const kinds[4] = { "ms", "note", "dotted", "triplet" };
  2522. return kinds[syncKind (v)];
  2523. }
  2524. case noteL: case noteR: return noteNames[noteIndex (v)];
  2525. case timeL: case timeR: return msText (timeMs (v));
  2526. case spread: return fmt ("%+.1f ms", 20.0 * bipolar (v));
  2527. case spacing: case width: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  2528. case ratio: return fmt ("%.1f%%", 1.0 + 99.0 * v);
  2529. case feedback: return pct (200.0 * v);
  2530. case drive: return fmt ("%.1f dB", 24.0 * v);
  2531. case diffusion: return v <= 0.0f ? "off" : pct (v * 100.0);
  2532. case lowCut: return hzText (lowCutHz (v));
  2533. case highCut: return hzText (highCutHz (v));
  2534. case modRate: return fmt ("%.2f Hz", modRateHz (v));
  2535. case shift: return fmt ("%+.1f Hz", shiftHz (v));
  2536. case shiftDetune: return fmt ("%+.2f Hz", shiftDetuneHz (v));
  2537. case pitch: return fmt ("%+.2f st", pitchSemis (v));
  2538. case pitchDetune: return fmt ("%+.0f c", 100.0 * bipolar (v));
  2539. case mode: return modeName (modeOf (v));
  2540. case output: return fmt ("%+.1f dB", outputDb (v));
  2541. case pairs: return v >= 0.5f ? "pairs" : "alternate";
  2542. case phaseL: case phaseR: return v >= 0.5f ? "inverted" : "normal";
  2543. case hybrid: return hybridOf (v) == 0 ? "off" : (hybridOf (v) == 1 ? "clean" : "analog");
  2544. case tapeVoicing: return tapeVoicingName (tapeVoicingOf (v));
  2545. case tempoSource: return v >= 0.5f ? "own BPM" : "project";
  2546. case ownBpm: return fmt ("%.1f BPM", bpmOf (v));
  2547. case startSide: return v >= 0.5f ? "right" : "left";
  2548. case filterLink: case tapA: case tapB: case tapC: case tapD: case primes: case freeze: return v >= 0.5f ? "on" : "off";
  2549. case swell: return v >= 0.5f ? "swell" : "repeat";
  2550. case groove: case feel: return fmt ("%+.1f%%", 25.0 * bipolar (v));
  2551. case accent: case balance: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  2552. case crossFeedback: return pct (v * 100.0);
  2553. case rhythm: return v >= 0.5f ? "on" : "off";
  2554. case rhythmSync:
  2555. {
  2556. static const char* const kinds[4] = { "ms", "note", "dotted", "triplet" };
  2557. return kinds[syncKind (v)];
  2558. }
  2559. case rhythmNote: return noteNames[noteIndex (v)];
  2560. case rhythmTime: return msText (timeMs (v));
  2561. case rhythmRepeats: return fmt ("%.0f", (double) rhythmRepeatsOf (v));
  2562. case rhythmDecay: return fmt ("%.1f dB", rhythmDecayDb (v));
  2563. case rhythmTap1: case rhythmTap1 + 1: case rhythmTap1 + 2: case rhythmTap1 + 3: case rhythmTap1 + 4: case rhythmTap1 + 5:
  2564. case rhythmTap1 + 6: case rhythmTap1 + 7: case rhythmTap1 + 8: case rhythmTap1 + 9: case rhythmTap1 + 10: case rhythmTap1 + 11:
  2565. case rhythmTap1 + 12: case rhythmTap1 + 13: case rhythmTapLast: return pct (v * 100.0);
  2566. case lofi: return v >= 0.5f ? "on" : "off";
  2567. case inputLevel: return fmt ("%+.1f dB", inputLevelDb (v));
  2568. case fbBalance: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  2569. case diffPost: return v >= 0.5f ? "post" : "loop";
  2570. case modShape:
  2571. {
  2572. static const char* const shapes[5] = { "sine", "triangle", "square", "saw", "random" };
  2573. return shapes[modShapeOf (v)];
  2574. }
  2575. case modSync:
  2576. {
  2577. static const char* const syncs[7] = { "free", "1/16", "1/8", "1/4", "1/2", "1 bar", "2 bars" };
  2578. return syncs[modSyncOf (v)];
  2579. }
  2580. case loopBass: case loopMid: case loopTreble: return fmt ("%+.1f dB", loopEqDb (v));
  2581. case voicing: return voicingName (voicingOf (v));
  2582. case satType: return satTypeName (satTypeOf (v));
  2583. case multiply: return std::to_string (multiplyOf (v)) + "x";
  2584. case voicingStereo: return v >= 0.5f ? "measured" : "classic";
  2585. case lrOffset: return fmt ("%.1f ms", lrOffsetMs (v));
  2586. case rhythmEdit: return rhythmEditOf (v) == RhythmEdit::even ? "even" : rhythmEditOf (v) == RhythmEdit::free ? "free" : "grid";
  2587. case rhythmGrid:
  2588. {
  2589. static const char* const grids[6] = { "1/2", "1/4", "1/8", "1/16", "1/32", "1/64" };
  2590. return grids[rhythmGridOf (v)];
  2591. }
  2592. case rhythmShape:
  2593. {
  2594. static const char* const shapes[5] = { "decay", "reverse", "swell", "fade", "nonlin" };
  2595. return shapes[(int) rhythmShapeOf (v)];
  2596. }
  2597. case era: return eraOf (v) == 0 ? "past" : eraOf (v) == 1 ? "present" : "future";
  2598. case rhythmTime1: case rhythmTime1 + 1: case rhythmTime1 + 2: case rhythmTime1 + 3: case rhythmTime1 + 4: case rhythmTime1 + 5:
  2599. case rhythmTime1 + 6: case rhythmTime1 + 7: case rhythmTime1 + 8: case rhythmTime1 + 9: case rhythmTime1 + 10: case rhythmTime1 + 11:
  2600. case rhythmTime1 + 12: case rhythmTime1 + 13: case rhythmTimeLast: return fmt ("%.1f%% of the pattern", v * 100.0);
  2601. case rhythmLength: return rhythmBeatsOf (v) == 1 ? std::string ("1 beat") : fmt ("%.0f beats", (double) rhythmBeatsOf (v));
  2602. default: return fmt ("%.2f", v);
  2603. }
  2604. }
  2605. if (kind == EffectKind::spiralEcho)
  2606. {
  2607. using namespace spiral;
  2608. switch (param)
  2609. {
  2610. case mix: case feedback: return pct (v * 100.0);
  2611. case shift: return fmt ("%+.2f Hz", shiftHz (v));
  2612. case delay: return msText (delayMs (v));
  2613. case sync:
  2614. {
  2615. const auto k = syncIndex (v);
  2616. if (k == 0)
  2617. return "off";
  2618. const auto group = k <= 8 ? 0 : k <= 15 ? 1 : 2;
  2619. const auto idx = k <= 8 ? k - 1 : k <= 15 ? k - 9 : k - 16;
  2620. return std::string (noteNames[idx]) + (group == 1 ? "." : group == 2 ? "T" : "");
  2621. }
  2622. case lowCut: return hzText (lowCutHz (v));
  2623. case highCut: return hzText (highCutHz (v));
  2624. case stereo: return on (v) ? "stereo" : "mono";
  2625. default: return fmt ("%.2f", v);
  2626. }
  2627. }
  2628. if (kind == EffectKind::orbit)
  2629. {
  2630. using namespace orbit;
  2631. switch (param)
  2632. {
  2633. case mix: return pct (v * 100.0);
  2634. case rate: return fmt ("%+.2f Hz", rateHz (v));
  2635. case depth: return msText (depthMs (v));
  2636. case feedback: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  2637. case manual: return fmt ("%+.2f ms", manualMs (v));
  2638. case mode: return modeName (modeOf (v));
  2639. default: return fmt ("%.2f", v);
  2640. }
  2641. }
  2642. if (kind == EffectKind::latticeMod)
  2643. {
  2644. using namespace lattice;
  2645. switch (param)
  2646. {
  2647. case mix: case depth: case feedback: case spread: case random: case diffusion: case diffModDepth: case rotate:
  2648. case detuneDepth: case vibratoDepth: return pct (v * 100.0);
  2649. case width: return pct (v * 200.0);
  2650. case delay: return msText (delayMs (v));
  2651. case slope: case skew: return fmt ("%+.0f%%", 100.0 * bipolar (v));
  2652. case tapGain: return fmt ("%+.1f dB", 12.0 * bipolar (v));
  2653. case diffSize: return msText (10.0 + 490.0 * v);
  2654. case diffModRate: case detuneRate: return fmt ("%.2f Hz", 0.01 + 9.99 * v);
  2655. case vibratoRate: return fmt ("%.2f Hz", 0.05 + 19.95 * v);
  2656. case lowCut: case xover: return hzText (10.0 + 1990.0 * v);
  2657. case highCut: return hzText (100.0 + 19900.0 * v);
  2658. case overMod: return fmt ("%.1fx", 1.0 + 99.0 * v);
  2659. case driveIn: return fmt ("%.1f dB", 24.0 * v);
  2660. case driveOut: return fmt ("%.1f dB", -24.0 + 24.0 * v);
  2661. case noise: return fmt ("%.1f dB", -120.0 + 90.0 * v);
  2662. case smoothing: return msText (1.0 + 999.0 * v);
  2663. case colour: return on (v) ? "dark" : "bright";
  2664. case sync:
  2665. {
  2666. static const char* const names[18] = { "off", "1/64", "1/32T", "1/64.", "1/32", "1/16T", "1/32.", "1/16", "1/8T",
  2667. "1/16.", "1/8", "1/4T", "1/8.", "1/4", "1/2T", "1/4.", "1/2", "1/1" };
  2668. return names[std::clamp ((int) std::lround (v * 17.0f), 0, 17)];
  2669. }
  2670. case taps: return fmt ("%.0f", (double) tapCount (v));
  2671. case diffOn: case driveOn: return on (v) ? "on" : "off";
  2672. case drivePost: return on (v) ? "post" : "pre";
  2673. case halfSpeed: return on (v) ? "1/2x" : "1x";
  2674. case delaySync:
  2675. {
  2676. static const char* const names[18] = { "free", "free", "1/16", "1/8", "1/4", "1/2", "1/1", "2/1", "1/16.", "1/8.",
  2677. "1/4.", "1/2.", "1/1.", "1/16T", "1/8T", "1/4T", "1/2T", "1/1T" };
  2678. return names[delaySyncIndex (v)];
  2679. }
  2680. case inputPan:
  2681. {
  2682. static const char* const names[9] = { "stereo", "stereo", "swapped", "mono", "mono left", "mono right", "stereo", "stereo", "stereo" };
  2683. return names[inputPanOf (v)];
  2684. }
  2685. default: return fmt ("%.2f", v);
  2686. }
  2687. }
  2688. return fmt ("%.2f", v);
  2689. }
  2690. } // namespace lynx::echosuite