IlFxKit.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. #pragma once
  2. // Building blocks shared by plug-ins of one third-party DSP code base that FL Studio ships (Toxic Biohazard's effects
  3. // rack, REA_rodada45 §FX, and Hardcore, REA_rodada71): float re-creations of their statically linked MSVC CRT math
  4. // (SSE2 paths, not correctly rounded), the cookbook biquads with a design cache, and the "tape" delay line. Written
  5. // from observed behaviour (clean-room) and checked bit for bit against the plug-ins by the rounds named above.
  6. //
  7. // The DLLs compute in float32 with explicit double steps; these keep the types and the operation order of every
  8. // expression, so fusing a·b + c into one FMA would change the results: contraction stays off.
  9. #include <algorithm>
  10. #include <array>
  11. #include <cmath>
  12. #include <cstdint>
  13. #include <cstring>
  14. #include <vector>
  15. #if defined(__clang__)
  16. #pragma clang fp contract(off)
  17. #elif defined(__GNUC__)
  18. #pragma GCC optimize ("fp-contract=off")
  19. #endif
  20. namespace lynx::ilfx
  21. {
  22. constexpr float tiny9 = 1e-9f;
  23. inline float tiny0 (float v) noexcept { return (v < tiny9 && v > -tiny9) ? 0.0f : v; }
  24. // cvttss2si / cvttsd2si: truncation toward zero; out of range or NaN → INT_MIN
  25. inline int i32 (double x) noexcept
  26. {
  27. if (! (x == x) || x >= 2147483648.0 || x < -2147483648.0)
  28. return (int) 0x80000000u;
  29. return (int) x;
  30. }
  31. // C round(): half away from zero
  32. inline double cround (double x) noexcept { return x >= 0.0 ? std::floor (x + 0.5) : -std::floor (-x + 0.5); }
  33. inline std::uint64_t bitsOf (double d) noexcept
  34. {
  35. std::uint64_t b = 0;
  36. std::memcpy (&b, &d, 8);
  37. return b;
  38. }
  39. inline double fromBits (std::uint64_t b) noexcept
  40. {
  41. double d = 0;
  42. std::memcpy (&d, &b, 8);
  43. return d;
  44. }
  45. inline std::uint32_t bitsOf (float f) noexcept
  46. {
  47. std::uint32_t b = 0;
  48. std::memcpy (&b, &f, 4);
  49. return b;
  50. }
  51. // ------------------------------------------------------------------------------------------------ CRT float math
  52. // The plug-in's MSVC CRT float functions (SSE2 paths) are not correctly rounded; these reproduce them bit for bit on
  53. // the argument ranges the rack uses (REA_rodada45 §FX.8). Tables are recomputed: 1/c, ln(1 + j/256), 2^(i/64).
  54. struct CrtTables
  55. {
  56. std::array<double, 257> invC {}, lnC {};
  57. std::array<double, 64> exp2 {};
  58. CrtTables()
  59. {
  60. for (int j = 0; j < 257; ++j)
  61. {
  62. invC[(std::size_t) j] = 1.0 / (0.5 + j / 512.0);
  63. lnC[(std::size_t) j] = std::log (1.0 + j / 256.0);
  64. }
  65. for (int i = 0; i < 64; ++i)
  66. exp2[(std::size_t) i] = std::exp2 (i / 64.0);
  67. }
  68. };
  69. inline const CrtTables& crt()
  70. {
  71. static const CrtTables t;
  72. return t;
  73. }
  74. // exp(t) of the powf/expf tails: table 2^(k/64), degree-3 polynomial, scaling by 2^(k>>6)
  75. inline double crtExpTail (double t) noexcept
  76. {
  77. const auto k = (long long) std::nearbyint (t * 92.33248261689366);
  78. const auto r = t - (double) k * 0.010830424696249145;
  79. const auto q = (r * r) * (0.16666666666666666 * r + 0.5) + r;
  80. const auto e = crt().exp2[(std::size_t) (k & 63)];
  81. return std::ldexp (q * e + e, (int) ((k - (k & 63)) / 64));
  82. }
  83. inline float powfCrt (float x, float y) noexcept // 0x180057f90
  84. {
  85. const auto ay = bitsOf (y) & 0x7fffffffu, ax = bitsOf (x) & 0x7fffffffu, sx = bitsOf (x);
  86. if (ay == 0)
  87. return 1.0f;
  88. if (y == 1.0f)
  89. return x;
  90. if (ay >= 0x7f800000u || ax >= 0x7f800000u || sx >= 0x80000000u || ax == 0)
  91. return (float) std::pow ((double) x, (double) y); // special values: never reached by the rack (not emulated)
  92. const auto xd = (double) x, yd = (double) y;
  93. double lg = 0;
  94. if (sx < 0x3f880000u && std::fabs (xd - 1.0) < 0.0625)
  95. {
  96. const auto u = xd - 1.0;
  97. const auto s = u / (u + 2.0);
  98. const auto v = s + s;
  99. const auto su = s * u;
  100. const auto v2 = v * v;
  101. const auto a = v2 * 0.012500000003771751 + 0.08333333333333179;
  102. const auto b = v2 * 0.0004348877777076146 + 0.0022321399879194482;
  103. const auto v3 = v * v2;
  104. const auto v7 = (v2 * v2) * v3;
  105. lg = u + ((v3 * a + v7 * b) - su);
  106. }
  107. else
  108. {
  109. const auto bx = bitsOf (xd);
  110. const auto m = bx & 0xfffffffffffffull;
  111. const auto j = (m >> 44) + ((m >> 43) & 1);
  112. const auto e = (int) ((bx >> 52) & 0x7ff) - 0x3ff;
  113. const auto f = fromBits (m | 0x3fe0000000000000ull);
  114. const auto c = fromBits ((j | 0x3fe00ull) << 44);
  115. const auto r = (c - f) * crt().invC[(std::size_t) j];
  116. const auto p = (r * r) * (0.3333333333333333 * r + 0.5) + r;
  117. lg = ((double) e * 0.6931471805599453 + crt().lnC[(std::size_t) j]) - p;
  118. }
  119. const auto t = yd * lg;
  120. if (t > 88.72283935546875)
  121. return INFINITY;
  122. if (! (t > -103.2789306640625))
  123. return 0.0f; // underflow path (not decoded in detail)
  124. return (float) crtExpTail (t);
  125. }
  126. inline float expfCrt (float x) noexcept // 0x18006e440
  127. {
  128. const auto xd = (double) x;
  129. const auto t = xd * 92.33248261689366;
  130. if (! std::isfinite (xd) || t >= 8192.0 || t < -9600.0)
  131. return (float) std::exp (xd); // overflow/underflow handlers: not emulated
  132. const auto k = (long long) std::nearbyint (t);
  133. const auto r = xd - (double) k * 0.010830424696249145;
  134. const auto q = (r * r) * (r * 0.16666666666666666 + 0.5) + r;
  135. const auto e = crt().exp2[(std::size_t) (k & 63)];
  136. return (float) ((q * e + e) * std::ldexp (1.0, (int) ((k - (k & 63)) / 64)));
  137. }
  138. inline double sinPoly (double r) noexcept
  139. {
  140. const auto x2 = r * r;
  141. const auto x4 = x2 * x2;
  142. const auto p = (2.7557319223985893e-06 * x2 + -0.0001984126984126984) * x4 + (0.008333333333333333 * x2 + -0.16666666666666666);
  143. return r + p * (r * x2);
  144. }
  145. inline double cosPoly (double r) noexcept
  146. {
  147. const auto x2 = r * r;
  148. const auto x4 = x2 * x2;
  149. const auto p = ((-2.755731922398589e-07 * x2 + 2.4801587301587298e-05) * x4 + (-0.0013888888888888887 * x2 + 0.041666666666666664)) * x4;
  150. return (-0.5 * x2 + 1.0) + p;
  151. }
  152. // |x| → quadrant n, reduced r and the exponent of the first r (Cody-Waite in up to two steps)
  153. inline void reduceQuadrant (double ax, int& n, double& r, int& er) noexcept
  154. {
  155. n = (int) (ax * 0.6366197723675814 + 0.5);
  156. const auto nd = (double) n;
  157. auto head = ax - nd * 1.5707963267341256;
  158. r = head - nd * 6.077100506506192e-11;
  159. const auto ex = (int) (bitsOf (ax) >> 52);
  160. er = (int) ((bitsOf (r) << 1) >> 53);
  161. if (ex - er > 15)
  162. {
  163. const auto t = head;
  164. const auto p2 = nd * 6.077100506303966e-11;
  165. head = head - p2;
  166. const auto tail = nd * 2.0222662487959506e-21 - ((t - head) - p2);
  167. r = head - tail;
  168. }
  169. }
  170. inline float sinfCrt (float x) noexcept // 0x18006edd0
  171. {
  172. const auto xd = (double) x, ax = std::fabs (xd);
  173. if (! std::isfinite (xd) || ax >= 16000000.0)
  174. return (float) std::sin (xd); // large-argument path: not emulated
  175. if (ax <= 0.7853981633974483)
  176. {
  177. if (ax < 0.0078125)
  178. {
  179. if (ax < 0.0001220703125)
  180. return x;
  181. return (float) (xd - ((xd * xd) * xd) * 0.16666666666666666);
  182. }
  183. return (float) sinPoly (xd);
  184. }
  185. const int neg = xd < 0.0 ? 1 : 0;
  186. int n = 0, er = 0;
  187. double r = 0;
  188. reduceQuadrant (ax, n, r, er);
  189. double v = 0;
  190. if (er >= 0x3f2)
  191. v = (n & 1) ? cosPoly (r) : sinPoly (r);
  192. else if (er > 0x3de)
  193. v = (n & 1) ? (1.0 - (r * r) * 0.5) : (r - (0.16666666666666666 * r) * (r * r));
  194. else
  195. v = (n & 1) ? 1.0 : r;
  196. const int h = (n >> 1) & 1;
  197. if ((((h & neg) | ((~h) & (~neg))) & 1) == 0)
  198. v = 0.0 - v;
  199. return (float) v;
  200. }
  201. inline float cosfCrt (float x) noexcept // 0x18006db40
  202. {
  203. const auto xd = (double) x, ax = std::fabs (xd);
  204. if (! std::isfinite (xd) || ax >= 500000.0)
  205. return (float) std::cos (xd); // large-argument path: not emulated
  206. if (ax <= 0.7853981633974483)
  207. return (float) cosPoly (xd);
  208. int n = 0, er = 0;
  209. double r = 0;
  210. reduceQuadrant (ax, n, r, er);
  211. double v = 0;
  212. if (er >= 0x3f2)
  213. v = (n & 1) ? sinPoly (r) : cosPoly (r);
  214. else if (er > 0x3de)
  215. v = (n & 1) ? (r - (0.16666666666666666 * r) * (r * r)) : (1.0 - (r * r) * 0.5);
  216. else
  217. v = (n & 1) ? r : 1.0;
  218. if ((n + 1) & 2)
  219. v = 0.0 - v;
  220. return (float) v;
  221. }
  222. inline float tanfCrt (float x) noexcept // 0x18006fa20, |x| ≤ π/4 (the rack never leaves that range)
  223. {
  224. const auto xd = (double) x, ax = std::fabs (xd);
  225. if (! std::isfinite (xd) || ax > 0.7853981633974483)
  226. return (float) std::tan (xd); // reduction path: not emulated
  227. if (ax < 7.450580596923828e-09)
  228. return x;
  229. if (ax < 0.0001220703125)
  230. return (float) (((xd * xd) * xd) * 0.3333333333333333 + xd);
  231. const auto x2 = xd * xd;
  232. const auto num = -0.017203248047148168 * x2 + 0.3852960712639954;
  233. const auto den = (0.01844239256901656 * x2 + -0.5139650547885454) * x2 + 1.1558882143468838;
  234. return (float) (xd + (x2 * xd) * (num / den));
  235. }
  236. // float sine of the DLL's packed kernel (0x18006b520, SSE path; the same constants as GMS's, REA_rodada39 §1.13)
  237. inline float sinK (float x) noexcept
  238. {
  239. const auto a = std::fabs (x);
  240. const auto t = a * 0.31830987334251404f + 12582912.0f;
  241. const auto n = t - 12582912.0f;
  242. const auto odd = (bitsOf (t) & 1u) != 0;
  243. auto r = a - 3.140625f * n;
  244. r = r - 0.0009675025939941406f * n;
  245. r = r - 1.5099067240953445e-07f * n;
  246. r = r - 5.126688136514179e-12f * n;
  247. const auto r2 = r * r;
  248. if (odd)
  249. r = -r;
  250. auto p = 2.605780537123792e-06f * r2 + -0.00019809602235909551f;
  251. p = p * r2 + 0.008333062753081322f;
  252. p = p * r2 + -0.16666659712791443f;
  253. const auto y = r + (r2 * p) * r;
  254. return x < 0.0f ? -y : y;
  255. }
  256. inline void sinCosK (float w, float& s, float& c) noexcept // FUN_18006b460
  257. {
  258. const auto a = std::fabs (w);
  259. if (a < 0.0001220703125f)
  260. {
  261. s = w;
  262. c = 1.0f - (a * a) * 0.5f;
  263. return;
  264. }
  265. s = sinK (w);
  266. c = sinK (a + 1.5707963705062866f);
  267. }
  268. // The CRT float routines a structure below uses: the plain (SSE2) re-creations above (Toxic Biohazard's rack, REA_rodada45)
  269. // or another set with the same static members (Hardcore uses the FMA-aware ones of core/IldspMath.h, REA_rodada71)
  270. struct PlainCrt
  271. {
  272. static float powf (float x, float y) noexcept { return powfCrt (x, y); }
  273. static float sinf (float x) noexcept { return sinfCrt (x); }
  274. static float cosf (float x) noexcept { return cosfCrt (x); }
  275. static void sincosf (float x, float& s, float& c) noexcept { sinCosK (x, s, c); }
  276. static float expf (float x) noexcept { return expfCrt (x); }
  277. static float tanf (float x) noexcept { return tanfCrt (x); }
  278. };
  279. // low-pass biquad of the lo-fi effect (FUN_18001c3a0 design, skipped when unchanged; FUN_18001c4a0 tick)
  280. template <class Crt>
  281. struct LpBiquadT
  282. {
  283. float f = 0, q = 0, fs = 0, a = 0, b1 = 0, b2 = 0, x2 = 0, x1 = 0, y2 = 0, y1 = 0;
  284. LpBiquadT()
  285. {
  286. design (100.0f, 1.0f, 44100.0f);
  287. clear();
  288. }
  289. void clear() noexcept { x2 = x1 = y2 = y1 = 0.0f; }
  290. void design (float nf, float nq, float nfs) noexcept
  291. {
  292. if (nf == f && nq == q && nfs == fs)
  293. return;
  294. f = nf, q = nq, fs = nfs;
  295. const auto w = (float) ((double) f * 6.2831853071795862 / (double) fs);
  296. float s = 0, c = 0;
  297. Crt::sincosf (w, s, c);
  298. const auto inv = 1.0f / (s + q);
  299. const auto den = s / q + 2.0f;
  300. const auto na = (1.0f - c) / den;
  301. const auto nb1 = (c * inv) * (q * -2.0f);
  302. const auto nb2 = (q - s) * inv;
  303. a = tiny0 (na), b1 = tiny0 (nb1), b2 = tiny0 (nb2);
  304. }
  305. float tick (float x) noexcept
  306. {
  307. x = tiny0 (x);
  308. auto y = ((((x1 + x1) + x) + x2) * a - y1 * b1) - b2 * y2;
  309. y = tiny0 (y);
  310. x2 = x1, y2 = y1, x1 = x, y1 = y;
  311. return y;
  312. }
  313. };
  314. // ------------------------------------------------------------------------------------------------ tape line
  315. // CTapeDelay (REA_rodada45 §FX.3): samples stored with a position stamp in [0, 1) in two halves ("worms"); the read
  316. // head interpolates the previous cycle; a triangle-shaped LFO bends the speed. Storage is reserved by setSampleRate so
  317. // that type changes do not allocate.
  318. struct Tape
  319. {
  320. struct Worm
  321. {
  322. std::vector<float> v;
  323. std::vector<double> t;
  324. int count = 0, ridx = 0;
  325. };
  326. int n = 4;
  327. Worm worms[2];
  328. int w = 0, r = 1;
  329. double phase0 = 0, minrate = 0.25, rateT = 0, rateS = 0, inc = 0, pos = 0, lph = 0;
  330. float gainT = 1.0f, gainS = 0.0f, fb = 0.0f, depth = 0.0f;
  331. void reserve (int maxSize)
  332. {
  333. for (auto& wm : worms)
  334. {
  335. wm.v.assign ((std::size_t) maxSize + 0x100, 0.0f);
  336. wm.t.assign ((std::size_t) maxSize + 0x100, 0.0);
  337. }
  338. }
  339. // FUN_180026140 (constructor defaults) on reserved storage
  340. void init (int size, double ph0) noexcept
  341. {
  342. n = std::max (4, size);
  343. if ((std::size_t) n + 0x100 > worms[0].v.size())
  344. n = (int) worms[0].v.size() - 0x100; // never with storage reserved for this rate
  345. w = 0, r = 1;
  346. phase0 = ph0;
  347. minrate = 1.0 / n;
  348. setTime (0.5);
  349. gainT = 1.0f;
  350. fb = 0.0f;
  351. inc = 0.0;
  352. depth = 0.0f;
  353. reset();
  354. }
  355. void setTime (double d) noexcept // FUN_1800267a0
  356. {
  357. if (d >= 0.99)
  358. d = 0.99;
  359. if (d <= 0.0)
  360. d = 0.0;
  361. auto m = 1.0 / n;
  362. m = m + m;
  363. rateT = 1.0 / ((d * (1.0 - m) + m) * n);
  364. }
  365. void setGain (double g) noexcept { gainT = (float) g; }
  366. void setFb (double g) noexcept { fb = (float) g; }
  367. void setInc (double rr, double fs) noexcept
  368. {
  369. if (rr <= 0.0)
  370. rr = 0.0;
  371. inc = rr / fs;
  372. }
  373. void setDepth (double x) noexcept
  374. {
  375. if (x >= 1.0)
  376. x = 1.0;
  377. if (x <= 0.0)
  378. x = 0.0;
  379. depth = (float) x;
  380. }
  381. void reset() noexcept // FUN_180026340
  382. {
  383. pos = phase0;
  384. rateS = rateT;
  385. gainS = 0.0f;
  386. lph = 0.0;
  387. for (auto& wm : worms)
  388. {
  389. std::fill (wm.v.begin(), wm.v.begin() + (n + 0x100), 0.0f);
  390. std::fill (wm.t.begin(), wm.t.begin() + (n + 0x100), 0.0);
  391. wm.count = wm.ridx = 0;
  392. }
  393. }
  394. float tick (float x) noexcept // FUN_180026400
  395. {
  396. if (x < tiny9 && x > -tiny9)
  397. x = 0.0f;
  398. const auto ph = lph;
  399. const auto tr = ph >= 0.5 ? 1.0 - (((ph - 0.5) + ph) - 0.5) : ph + ph;
  400. const auto nph = ph + inc;
  401. lph = nph;
  402. auto mod = ((3.0 - (tr + tr)) * tr) * ((double) depth * tr) + 1.0;
  403. if (! (mod > 1.0))
  404. mod = 1.0;
  405. if (nph >= 1.0)
  406. lph = nph - (double) i32 (nph);
  407. auto& rb = worms[r];
  408. float out = 0.0f;
  409. if (rb.count != 0)
  410. for (;;)
  411. {
  412. const auto i = rb.ridx;
  413. if (i >= rb.count + 1)
  414. break;
  415. const auto ti = rb.t[(std::size_t) (10 + i)];
  416. if (pos > ti)
  417. {
  418. rb.ridx = i + 1;
  419. continue;
  420. }
  421. const auto t0 = (float) rb.t[(std::size_t) (9 + i)];
  422. const auto t1 = (float) ti;
  423. double fr = 0.0;
  424. if (t0 < t1)
  425. fr = (pos - (double) t0) / (double) (t1 - t0);
  426. const auto v0 = rb.v[(std::size_t) (9 + i)];
  427. out = tiny0 ((float) ((double) (rb.v[(std::size_t) (10 + i)] - v0) * fr + (double) v0));
  428. break;
  429. }
  430. auto rt = mod * rateS;
  431. if (! (rt < 0.2))
  432. rt = 0.2;
  433. const auto wv = out * fb + x;
  434. const auto ret = out * gainS;
  435. if (! (rt > minrate))
  436. rt = minrate;
  437. pos = rt + pos;
  438. if (pos >= 1.0)
  439. {
  440. auto& wb = worms[w];
  441. const auto c = (std::size_t) rb.count;
  442. wb.v[9] = rb.v[9 + c], wb.t[9] = rb.t[9 + c] - 1.0;
  443. wb.v[8] = rb.v[8 + c], wb.t[8] = rb.t[8 + c] - 1.0;
  444. wb.v[7] = rb.v[7 + c], wb.t[7] = rb.t[7 + c] - 1.0;
  445. wb.v[(std::size_t) (10 + wb.count)] = wv;
  446. wb.t[(std::size_t) (10 + wb.count)] = pos;
  447. wb.ridx = 0;
  448. std::swap (w, r);
  449. pos = pos - 1.0;
  450. worms[w].count = 0;
  451. }
  452. auto& wb = worms[w];
  453. if ((std::size_t) (10 + wb.count) < wb.v.size())
  454. {
  455. wb.v[(std::size_t) (10 + wb.count)] = wv;
  456. wb.t[(std::size_t) (10 + wb.count)] = pos;
  457. ++wb.count;
  458. }
  459. gainS = tiny0 ((float) ((double) (gainT - gainS) * 0.01 + (double) gainS));
  460. rateS = (rateT - rateS) * 0.005 + rateS;
  461. return ret;
  462. }
  463. };
  464. // ------------------------------------------------------------------------------------------------ EQ band
  465. // FUN_18001c590 (cookbook design with a dirty flag) / FUN_18001c930 (tick); band 0 low shelf, 1–6 peaking, 7 high shelf
  466. template <class Crt>
  467. struct EqBandT
  468. {
  469. float fs = 44100.0f, gain = 0.0f, freq = 1000.0f, bw = 1.0f;
  470. bool dirty = true;
  471. int type = 1;
  472. float c[5] {}, x1 = 0, x2 = 0, y1 = 0, y2 = 0;
  473. void init (float f, int t) noexcept
  474. {
  475. freq = f, type = t, fs = 44100.0f, gain = 0.0f, bw = 1.0f, dirty = true;
  476. design (44100.0f);
  477. clear();
  478. }
  479. void clear() noexcept { x1 = x2 = y1 = y2 = 0.0f; }
  480. void setGain (float gn) noexcept
  481. {
  482. if (gn != gain)
  483. dirty = true;
  484. gain = gn;
  485. }
  486. void setFreq (float f) noexcept
  487. {
  488. if (f != freq)
  489. dirty = true;
  490. freq = f;
  491. }
  492. void setBandwidth (float b) noexcept
  493. {
  494. if (b != bw)
  495. dirty = true;
  496. bw = b;
  497. }
  498. void design (float nfs) noexcept
  499. {
  500. if (nfs != fs)
  501. dirty = true;
  502. fs = nfs;
  503. if (! dirty)
  504. return;
  505. dirty = false;
  506. const auto A = Crt::powf (10.0f, gain * 0.05000000074505806f);
  507. const auto num = (double) freq * 6.2831849999999996;
  508. auto w = (double) fs != 0.0 ? (float) (num / (double) fs) : INFINITY;
  509. if (w >= 3.0f)
  510. w = 3.0f;
  511. if (w <= 0.0f)
  512. w = 0.0f;
  513. const auto sn = Crt::sinf (w);
  514. const auto sh = std::sinh ((double) bw * 0.34657359999999998 * (double) w / (double) sn);
  515. const auto al2 = (float) (sh + sh);
  516. const auto beta = std::sqrt (A) * al2;
  517. const auto alpha = (sn * al2) * 0.5f;
  518. const auto one = 1.0f;
  519. float b0, b1, b2, a0, a1, a2;
  520. if (type == 0)
  521. {
  522. const auto cs = Crt::cosf (w);
  523. const auto k = sn * beta;
  524. const auto am1 = A - one, ap1 = A + one;
  525. const auto t = am1 * cs, u = ap1 - t, v = t + ap1;
  526. const auto c2 = ap1 * cs;
  527. a0 = k + v;
  528. b2 = (u - k) * A;
  529. b0 = (u + k) * A;
  530. a1 = am1 * -2.0f - (c2 + c2);
  531. b1 = (A + A) * (am1 - c2);
  532. a2 = v - k;
  533. }
  534. else if (type == 2)
  535. {
  536. const auto cs = Crt::cosf (w);
  537. const auto k = sn * beta;
  538. const auto am1 = A - one, ap1 = A + one;
  539. const auto t = am1 * cs, c2 = ap1 * cs;
  540. const auto p = t + ap1, q = ap1 - t;
  541. a0 = q + k;
  542. b0 = (k + p) * A;
  543. const auto d = am1 - c2;
  544. b2 = (p - k) * A;
  545. a1 = d + d;
  546. b1 = ((c2 + am1) * A) * -2.0f;
  547. a2 = q - k;
  548. }
  549. else
  550. {
  551. const auto aA = alpha * A, aD = alpha / A;
  552. b0 = aA + one;
  553. a0 = aD + one;
  554. const auto cc = Crt::cosf (w) * -2.0f;
  555. a1 = cc, b1 = cc;
  556. a2 = one - aD;
  557. b2 = one - aA;
  558. }
  559. const auto inv = one / a0;
  560. c[0] = tiny0 (inv * b0), c[1] = tiny0 (inv * b1), c[2] = tiny0 (inv * b2), c[3] = tiny0 (inv * a1), c[4] = tiny0 (inv * a2);
  561. }
  562. float tick (float x) noexcept
  563. {
  564. x = tiny0 (x);
  565. auto y = (((x1 * c[1] + x * c[0]) + c[2] * x2) - y1 * c[3]) - c[4] * y2;
  566. y = tiny0 (y);
  567. x2 = x1, y2 = y1, x1 = x, y1 = y;
  568. return y;
  569. }
  570. };
  571. // ------------------------------------------------------------------------------------------------ reverb
  572. // class fxReverb (the plate of GMS's reverb slot, REA_rodada39 §1.13) with its early-reflection tap active, processing
  573. // in place (REA_rodada45 §FX.5; Hardcore's reverb pedal is the same class, REA_rodada71).
  574. template <class Crt>
  575. struct ReverbT
  576. {
  577. static constexpr double qa[18][2] = { { 421.0, -360.5 }, { 2981.0, 19.5 }, { 216.0, 5.5 }, { 250.0, 0.5 }, { 99.0, 1.5 }, { 100.0, 0.5 },
  578. { 28.0, 2.5 }, { 1975.0, 25.5 }, { 199.0, 1.5 }, { 216.0, 5.5 }, { 421.0, -360.5 }, { 421.0, -360.5 },
  579. { 421.0, -360.5 }, { 200.0, 0.5 }, { 1.0, 0.5 }, { 1.0, 0.5 }, { 1.0, 0.5 }, { 1.0, 0.5 } };
  580. static constexpr int lines[12] = { 142, 107, 379, 277, 672, 4453, 1800, 3720, 908, 4217, 2656, 3163 };
  581. static constexpr int taps0[14] = { 266, 2974, 1913, 2496, 1690, 187, 1566, 353, 3627, 1728, 3173, 1811, 335, 621 };
  582. static constexpr int erTapL = 1102, erTapR = 837; // first entries of the early-reflection tables (the only taps with gain ≠ 0)
  583. // the whole early-reflection tables (Hardcore's build gives every tap a gain and sums 7 left / 8 right taps)
  584. static constexpr int erTabL[8] = { 1102, 2904, 3784, 4873, 5878, 7470, 9062, 9263 };
  585. static constexpr int erTabR[8] = { 837, 1423, 2865, 3938, 5278, 5978, 7532, 9263 };
  586. bool allTaps = false;
  587. int erTL[8] {}, erTR[8] {};
  588. float erGL[8] {}, erGR[8] {};
  589. static constexpr float tiny = 1e-30f;
  590. float fs = 44100.0f;
  591. int pi[18] {};
  592. std::vector<float> buf[12], pre, er;
  593. int len[12] {}, pos[12] {}, prepos = 0, prelen = 1, lastsize = 0, erSize = 2, erPos = 0, erTl = 0, erTr = 0;
  594. int taps[14] {};
  595. bool dirty = true;
  596. float lp = 0, hpx = 0, hpy = 0, dL0 = 0, dL1 = 0, dR0 = 0, dR1 = 0, tL = 0, tR = 0, lev = -1000.0f;
  597. float gin = 0, hpk = 0, lpk = 1, ap1 = 0, ap2 = 0, tap1 = 0, tap2 = 0, decay = 0, shelf = 0, shk = 0, damp = 1, dry = 0;
  598. float erLev = 0, tank = 0, erGl = 0, erGr = 0;
  599. static int floorI (float x) noexcept
  600. {
  601. auto i = (int) x;
  602. if ((double) i != (double) x && x < 0.0f)
  603. --i;
  604. return i;
  605. }
  606. static int cdiv (long long a, long long b) noexcept { return (int) (a / b); }
  607. static float db (int i) noexcept // FUN_18002c200
  608. {
  609. if (i <= -361)
  610. return 0.0f;
  611. return Crt::powf (10.0f, (float) i * 0.004999999888241291f);
  612. }
  613. void construct()
  614. {
  615. static constexpr float preset[18] = { 0.8574821949005127f, 0.018785642459988594f, 0.16203702986240387f, 0.0f, 0.49494948983192444f, 1.0f,
  616. 0.2857142984867096f, 0.2405063360929489f, 0.07035175710916519f, 0.16203702986240387f,
  617. 0.8574821949005127f, 0.7743467688560486f, 0.5724465847015381f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f };
  618. lastsize = 0;
  619. setRate (44100.0f);
  620. for (int i = 0; i < 18; ++i)
  621. setp (i, preset[i]);
  622. reset();
  623. }
  624. void setRate (float rate) // FUN_18002a7d0 / FUN_18002a7e0
  625. {
  626. fs = rate;
  627. const auto fsi = (int) fs;
  628. for (int k = 0; k < 12; ++k)
  629. {
  630. const auto n = cdiv ((long long) fsi * lines[k], 20000);
  631. buf[k].assign ((std::size_t) std::max (1, n), tiny);
  632. len[k] = std::max (1, n);
  633. pos[k] = 0;
  634. }
  635. prelen = cdiv ((long long) fsi * 250, 1000);
  636. pre.assign ((std::size_t) std::max (1, prelen), tiny);
  637. prelen = std::max (1, prelen);
  638. prepos = 0;
  639. auto u = (unsigned) (fsi * 0.31632653061224492);
  640. int bitsN = 0;
  641. while (u != 0)
  642. ++bitsN, u >>= 1;
  643. erSize = 1 << (bitsN + 1);
  644. er.assign ((std::size_t) erSize, tiny);
  645. erPos = 0;
  646. for (int t = 0; t < 14; ++t)
  647. taps[t] = cdiv ((long long) fsi * taps0[t], 20000);
  648. }
  649. void setp (int i, float v) noexcept // FUN_18002b380
  650. {
  651. const auto b = (float) qa[i][1];
  652. const auto x = b >= 0.0f ? v * (float) qa[i][0] + b : v * (float) qa[i][0] - (-b);
  653. pi[i] = floorI (x);
  654. dirty = true;
  655. }
  656. void reset() noexcept // FUN_18002b170 (positions are kept)
  657. {
  658. for (auto& b : buf)
  659. std::fill (b.begin(), b.end(), tiny);
  660. std::fill (pre.begin(), pre.end(), tiny);
  661. std::fill (er.begin(), er.end(), tiny);
  662. lp = hpx = hpy = 0.0f;
  663. dL0 = dL1 = dR0 = dR1 = 0.0f;
  664. tL = tR = 0.0f;
  665. lev = -1000.0f;
  666. }
  667. float onePole (int p) const noexcept
  668. {
  669. if ((float) p < 221.0f)
  670. {
  671. const auto c = Crt::cosf (((float) p * 628.3185424804688f) / fs);
  672. const auto t = 2.0f - c;
  673. const auto s = std::sqrt ((t * t) - 1.0f);
  674. return 1.0f - (t - s);
  675. }
  676. return 1.0f;
  677. }
  678. void recalc() noexcept // FUN_18002c230
  679. {
  680. const auto fsi = (int) fs;
  681. const auto* p = pi;
  682. const auto one = 1.0f;
  683. gin = p[14] == 0 ? db (p[0]) : 0.0f;
  684. if ((float) p[1] > 19.0f)
  685. {
  686. const auto x = (std::min (fs * 0.5f, (float) p[1]) * 3.1415927410125732f) / fs;
  687. const auto e = Crt::tanf (x);
  688. hpk = (e - one) / (e + one);
  689. }
  690. else
  691. hpk = 0.0f;
  692. lpk = onePole (p[2]);
  693. prelen = std::max (1, cdiv ((long long) fsi * p[3], 1000));
  694. prelen = std::min (prelen, (int) pre.size());
  695. prepos %= prelen;
  696. const auto base = cdiv ((long long) (p[4] + 15) * fsi, 115);
  697. for (int k = 0; k < 12; ++k)
  698. {
  699. len[k] = std::max (1, cdiv ((long long) base * lines[k], 20000));
  700. len[k] = std::min (len[k], (int) buf[k].size());
  701. pos[k] %= len[k];
  702. }
  703. for (int t = 0; t < 14; ++t)
  704. taps[t] = cdiv ((long long) base * taps0[t], 20000);
  705. const auto sc = (float) cdiv ((long long) (p[4] + 3) * fsi, 103);
  706. erTl = (int) (((float) erTapL * 3.4013603e-05f) * sc);
  707. erTr = (int) (((float) erTapR * 3.4013603e-05f) * sc);
  708. const auto m = erTl < erTr ? (float) erTl : (float) erTr;
  709. erGl = (m * 0.5f) / (float) erTl;
  710. if (allTaps)
  711. for (int k = 0; k < 8; ++k)
  712. {
  713. erTL[k] = (int) (((float) erTabL[k] * 3.4013603e-05f) * sc);
  714. erTR[k] = (int) (((float) erTabR[k] * 3.4013603e-05f) * sc);
  715. erGL[k] = (m * 0.5f) / (float) erTL[k];
  716. erGR[k] = (m * 0.36000001430511475f) / (float) erTR[k];
  717. }
  718. erGr = (m * 0.36000001430511475f) / (float) erTr;
  719. if (p[4] != lastsize)
  720. {
  721. lastsize = p[4];
  722. for (int k = 0; k < 12; ++k)
  723. std::fill (buf[k].begin(), buf[k].begin() + len[k], 0.0f);
  724. std::fill (er.begin(), er.end(), 0.0f);
  725. }
  726. const auto diff = (float) p[5] * 0.01f;
  727. const auto inv = one / fs;
  728. if (diff != 0.0f)
  729. {
  730. ap1 = Crt::expf (((float) (len[0] + len[1]) * (-49.34111022949219f / diff)) * inv);
  731. ap2 = Crt::expf (((-31.398887634277344f / diff) * (float) (len[2] + len[3])) * inv);
  732. auto x = (((float) (len[4] + len[8]) * -3.4538776874542236f) / fs) * (2.857142925262451f / diff);
  733. tap1 = Crt::expf (x);
  734. x = ((-3.4538776874542236f / fs) * (float) (len[6] + len[10])) * (1.9607843160629272f / diff);
  735. tap2 = Crt::expf (x);
  736. }
  737. else
  738. ap1 = ap2 = tap1 = tap2 = 0.0f;
  739. const auto tsum = ((float) (len[9] + len[7] + len[5] + len[11]) / fs) * -6.907755374908447f;
  740. const auto t60 = (float) p[8] * 0.10000000149011612f;
  741. decay = Crt::powf (Crt::expf (tsum / t60), 0.25f);
  742. auto rr = Crt::powf (Crt::expf ((10.0f / ((float) p[6] * t60)) * tsum), 0.25f);
  743. rr = rr / decay;
  744. shelf = (rr - one) * 0.5f;
  745. const auto rm = std::min (rr, one);
  746. const auto e = Crt::tanf (((float) p[7] * 3.1415927410125732f) / fs);
  747. shk = (e - rm) / (rm + e);
  748. damp = onePole (p[9]);
  749. dry = p[15] == 0 ? db (p[10]) : 0.0f;
  750. erLev = p[16] == 0 ? db (p[11]) * 0.5f : 0.0f;
  751. tank = p[17] == 0 ? db (p[12]) * 0.30000001192092896f : 0.0f;
  752. dirty = false;
  753. }
  754. float ap (int k, float x, float c1, float c2) noexcept
  755. {
  756. auto& b = buf[k];
  757. const auto i = pos[k];
  758. const auto t = b[(std::size_t) i] - (c1 * x);
  759. b[(std::size_t) i] = (c2 * t) + x;
  760. pos[k] = i > 0 ? i - 1 : len[k] - 1;
  761. return t;
  762. }
  763. static int wrap (int i, int n) noexcept { return i >= n ? i - n : i; }
  764. void process (float* outL, float* outR, int n) noexcept // FUN_18002dda0, in place
  765. {
  766. if (dirty)
  767. recalc();
  768. const auto d = decay, one = 1.0f, half = 0.5f, dd = one - d;
  769. const auto g41 = (dd * 0.6000000238418579f) + d, g42 = (dd * 0.25f) + d, g43 = (dd * 0.699999988079071f) + d;
  770. const auto g56 = (dd * 0.20000000298023224f) + d, g44 = (dd * half) + d;
  771. const auto width = (float) pi[13] * 0.009999999776482582f;
  772. const auto erm = erSize - 1;
  773. auto* B = buf;
  774. auto* L = len;
  775. auto* P = pos;
  776. const auto* T = taps;
  777. for (int s = 0; s < n; ++s)
  778. {
  779. const auto xl = gin * outL[s], xr = gin * outR[s];
  780. const auto m = ((xr + xl) * half) + tiny;
  781. auto l = ((m - lp) * lpk) + lp;
  782. lp = l;
  783. const auto h = ((l - hpx) * hpk) + hpy;
  784. hpy = l;
  785. hpx = h;
  786. if (hpk != 0.0f)
  787. l = ((l - h) * half) + tiny;
  788. float erl = 0.0f, err = 0.0f;
  789. if (erLev != 0.0f)
  790. {
  791. if (allTaps)
  792. {
  793. auto e = [&] (int tap) { return er[(std::size_t) ((tap + erPos) & erm)]; };
  794. erl = e (erTL[0]) * erGL[0] + e (erTL[1]) * erGL[1];
  795. erl = erl - e (erTL[2]) * erGL[2];
  796. erl = e (erTL[3]) * erGL[3] + erl;
  797. erl = erl + e (erTL[4]) * erGL[4];
  798. erl = erl + e (erTL[5]) * erGL[5];
  799. erl = erl + e (erTL[6]) * erGL[6];
  800. err = e (erTR[0]) * erGR[0] - e (erTR[1]) * erGR[1];
  801. err = err + e (erTR[2]) * erGR[2];
  802. err = err + e (erTR[3]) * erGR[3];
  803. err = err - e (erTR[4]) * erGR[4];
  804. err = err + e (erTR[5]) * erGR[5];
  805. err = err + e (erTR[6]) * erGR[6];
  806. err = err - e (erTR[7]) * erGR[7];
  807. }
  808. else
  809. {
  810. erl = er[(std::size_t) ((erTl + erPos) & erm)] * erGl;
  811. err = er[(std::size_t) ((erTr + erPos) & erm)] * erGr;
  812. }
  813. }
  814. auto t = ap (0, l, ap1, ap1);
  815. auto u = ap (1, t, ap2, ap1);
  816. er[(std::size_t) erPos] = t;
  817. erPos = erPos > 0 ? erPos - 1 : erm;
  818. t = ap (2, u, ap1, ap1);
  819. u = ap (3, t, ap2, ap1);
  820. const auto pv = pre[(std::size_t) prepos];
  821. pre[(std::size_t) prepos] = u;
  822. prepos = prepos > 0 ? prepos - 1 : prelen - 1;
  823. const auto tin = pv + 9.99999968e-21f;
  824. auto v = B[11][(std::size_t) P[11]] * d;
  825. auto s2 = ((v - dL1) * shk) + dL0;
  826. dL0 = v, dL1 = s2;
  827. auto w = (((((s2 + v) * shelf) + v) + tin) - tL) * damp;
  828. w = w + tL;
  829. tL = w;
  830. u = ap (4, w, tap1, tap1);
  831. auto i = P[5];
  832. auto o = B[5][(std::size_t) i];
  833. B[5][(std::size_t) i] = u;
  834. P[5] = i > 0 ? i - 1 : L[5] - 1;
  835. o = o * d;
  836. u = ap (6, o, tap2, tap2);
  837. i = P[7];
  838. B[7][(std::size_t) i] = u;
  839. i = i > 0 ? i - 1 : L[7] - 1;
  840. P[7] = i;
  841. v = B[7][(std::size_t) i] * d;
  842. s2 = ((v - dR1) * shk) + dR0;
  843. dR0 = v, dR1 = s2;
  844. w = (((((s2 + v) * shelf) + v) + tin) - tR) * damp;
  845. w = w + tR;
  846. tR = w;
  847. u = ap (8, w, tap1, tap1);
  848. i = P[9];
  849. o = B[9][(std::size_t) i];
  850. B[9][(std::size_t) i] = u;
  851. P[9] = i > 0 ? i - 1 : L[9] - 1;
  852. o = o * d;
  853. u = ap (10, o, tap2, tap2);
  854. i = P[11];
  855. B[11][(std::size_t) i] = u;
  856. P[11] = i > 0 ? i - 1 : L[11] - 1;
  857. auto at = [&] (int line, int tap) { return B[line][(std::size_t) wrap (tap + P[line], L[line])]; };
  858. const auto fA = at (9, T[0]), fB = at (9, T[1]), fC = at (10, T[2]), fD = at (11, T[3]);
  859. const auto fE = at (5, T[4]), fF = at (6, T[5]), fG = at (7, T[6]);
  860. const auto f7 = at (5, T[8]), f8 = at (5, T[7]), f9 = at (6, T[9]), f10 = at (7, T[10]), f11 = at (9, T[11]);
  861. const auto f12 = at (10, T[12]), f13 = at (11, T[13]);
  862. auto tl = (((fA - (g42 * fB)) - (g43 * fC)) - (g56 * fD)) + (g41 * fE);
  863. tl = (tl + fF) + (g44 * fG);
  864. auto tr = ((((g42 * f7) + f8) - (g43 * f9)) - (g56 * f10)) - (g41 * f11);
  865. tr = (tr + f12) + (g44 * f13);
  866. if (lev < -99.0f)
  867. lev = tank;
  868. lev = ((tank - lev) * 0.0010000000474974513f) + lev;
  869. const auto a = (lev * tl) + (erLev * erl), b = (lev * tr) + (erLev * err);
  870. const auto sm = b + a, df = (a - b) * width;
  871. outL[s] = (xl * dry) + (df + sm);
  872. outR[s] = (xr * dry) + (sm - df);
  873. }
  874. }
  875. };
  876. using LpBiquad = LpBiquadT<PlainCrt>;
  877. using Reverb = ReverbT<PlainCrt>;
  878. using EqBand = EqBandT<PlainCrt>;
  879. } // namespace lynx::ilfx