FruityShapers.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. #include "core/FruityShapers.h"
  2. // Fruity WaveShaper and Fruity Soft Clipper, exact engines (docs/analise/REA_rodada51.md). Clean-room: written from
  3. // the behaviour, formulas and data layouts described there; nothing of Image-Line's code or data is copied.
  4. //
  5. // Arithmetic follows the plugins operation by operation: float/double steps in their order with one rounding each
  6. // (no contraction), and the x87 kernels in 80-bit long double under the control word the plugins use (round to
  7. // nearest, or toward zero in the interpolating WaveShaper kernels).
  8. #include <algorithm>
  9. #include <array>
  10. #include <cmath>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include "core/Instruments.h"
  14. #if defined(__clang__)
  15. #pragma clang fp contract(off)
  16. #pragma STDC FENV_ACCESS ON
  17. #elif defined(__GNUC__)
  18. #pragma GCC optimize("fp-contract=off", "rounding-math")
  19. #endif
  20. namespace lynx::flshape
  21. {
  22. namespace
  23. {
  24. using ld = long double;
  25. // ------------------------------------------------------------------------------------------- x87 control word (§1.3)
  26. #if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
  27. struct X87Mode
  28. {
  29. unsigned short saved = 0;
  30. explicit X87Mode (bool truncate) noexcept
  31. {
  32. asm volatile ("fnstcw %0" : "=m"(saved) : : "memory");
  33. unsigned short cw = (unsigned short) ((saved & ~0x0F00u) | 0x0300u | (truncate ? 0x0C00u : 0u));
  34. asm volatile ("fldcw %0" : : "m"(cw) : "memory");
  35. }
  36. ~X87Mode() { asm volatile ("fldcw %0" : : "m"(saved) : "memory"); }
  37. X87Mode (const X87Mode&) = delete;
  38. X87Mode& operator= (const X87Mode&) = delete;
  39. };
  40. #else
  41. // no x87 (ARM64): conversions follow the kernel's rounding direction (arithmetic stays round-to-nearest; precision
  42. // is the platform's long double)
  43. inline thread_local bool x87Truncate = false; // the conversions below follow it (arithmetic stays round-to-nearest)
  44. struct X87Mode
  45. {
  46. bool saved;
  47. explicit X87Mode (bool truncate) noexcept : saved (x87Truncate) { x87Truncate = truncate; }
  48. ~X87Mode() { x87Truncate = saved; }
  49. X87Mode (const X87Mode&) = delete;
  50. X87Mode& operator= (const X87Mode&) = delete;
  51. };
  52. #endif
  53. // long double → integer in the current x87 rounding mode (what the plugins' fist/fistp do), without the control-word
  54. // round trip a C++ conversion makes; |v| < 2^63
  55. #if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
  56. inline long long x87Int (long double v) noexcept
  57. {
  58. long long r;
  59. asm ("fistpll %0" : "=m"(r) : "t"(v) : "st");
  60. return r;
  61. }
  62. // exact conversions straight between memory and the x87 stack (a C++ conversion goes through an SSE register and a
  63. // temporary), and frndint: the value rounded to an integer in the current mode, as a long double
  64. inline long double x87Load (const float* p) noexcept
  65. {
  66. long double r;
  67. asm ("flds %1" : "=t"(r) : "m"(*p));
  68. return r;
  69. }
  70. inline long double x87Load (const std::int16_t* p) noexcept
  71. {
  72. long double r;
  73. asm ("filds %1" : "=t"(r) : "m"(*p));
  74. return r;
  75. }
  76. inline long double x87Round (long double v) noexcept
  77. {
  78. long double r;
  79. asm ("frndint" : "=t"(r) : "0"(v));
  80. return r;
  81. }
  82. // (float) v in the current rounding mode, stored
  83. inline void x87Store (float* p, long double v) noexcept { asm ("fstps %0" : "=m"(*p) : "t"(v) : "st"); }
  84. #else
  85. inline long long x87Int (long double v) noexcept { return x87Truncate ? (long long) std::trunc (v) : (long long) std::nearbyint (v); }
  86. inline long double x87Load (const float* p) noexcept { return *p; }
  87. inline long double x87Load (const std::int16_t* p) noexcept { return *p; }
  88. inline long double x87Round (long double v) noexcept { return x87Truncate ? std::trunc (v) : std::nearbyint (v); }
  89. inline void x87Store (float* p, long double v) noexcept { *p = (float) v; }
  90. #endif
  91. inline float F (double v) noexcept { return (float) v; }
  92. inline std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t o) noexcept
  93. {
  94. std::int32_t v = 0;
  95. std::memcpy (&v, d.data() + o, 4);
  96. return v;
  97. }
  98. inline double f64At (const std::vector<std::uint8_t>& d, std::size_t o) noexcept
  99. {
  100. double v = 0;
  101. std::memcpy (&v, d.data() + o, 8);
  102. return v;
  103. }
  104. inline float f32At (const std::vector<std::uint8_t>& d, std::size_t o) noexcept
  105. {
  106. float v = 0;
  107. std::memcpy (&v, d.data() + o, 4);
  108. return v;
  109. }
  110. int rawFrom (float v, int lo, int hi) noexcept
  111. {
  112. return lo + (int) std::lround (std::clamp ((double) v, 0.0, 1.0) * (hi - lo));
  113. }
  114. float norm (int raw, int lo, int hi) noexcept { return (float) std::clamp ((double) (raw - lo) / (hi - lo), 0.0, 1.0); }
  115. // FL's level law L11: (11^x − 1)/10 with 1 kept exact (both plugins' post gains, §1.1 / §2.1)
  116. float postGain (int raw) noexcept
  117. {
  118. const float n = (float) raw * 0.0078125f;
  119. if (n == 1.0f)
  120. return 1.0f;
  121. return F ((std::exp ((double) (n * 2.39789534f)) - 1.0) * (double) 0.1f);
  122. }
  123. // ----------------------------------------------------------------------------------------------- map evaluator (§2.4)
  124. constexpr double kEps = 5.960464477539063e-08; // float 2^-24 (as a double)
  125. struct Pt
  126. {
  127. float dt = 0, y = 0, t = 0;
  128. int mode = 0, sign = 0;
  129. float k = 0, ilk = 0, L = 0; // modes 0/1: curvature; other modes reuse p4…p6
  130. float p4 = 0, p5 = 0, p6 = 0;
  131. };
  132. int stepCount (const Pt& p) noexcept
  133. {
  134. if (p.mode < 3 || p.mode > 6)
  135. return -1;
  136. const auto a = F (1.0 - std::abs ((double) p.t) * 0.6);
  137. auto n = (int) std::nearbyint ((double) F (std::exp ((double) a * std::log (1000001.0)) - 1.0) * 0.001);
  138. if (p.mode == 3 && p.t >= 0.0f)
  139. ++n;
  140. return n;
  141. }
  142. void prepareSmooth (std::vector<Pt>& pts, int i) noexcept
  143. {
  144. auto& p = pts[(std::size_t) i];
  145. const int n = (int) pts.size();
  146. const double h = (double) p.dt > kEps ? (double) p.dt : kEps;
  147. const double y1 = p.y;
  148. const int j = i - 1;
  149. const double y0 = j >= 0 ? (double) pts[(std::size_t) j].y : y1;
  150. double rp = 1.0, ypp = y0;
  151. if (j >= 0 && pts[(std::size_t) j].mode == 10)
  152. {
  153. rp = (double) pts[(std::size_t) j].dt / h;
  154. if (i - 2 >= 0)
  155. ypp = pts[(std::size_t) (i - 2)].y;
  156. }
  157. double rn = 1.0, yn = y1;
  158. if (i + 1 < n && pts[(std::size_t) (i + 1)].mode == 10)
  159. {
  160. rn = (double) pts[(std::size_t) (i + 1)].dt / h;
  161. yn = pts[(std::size_t) (i + 1)].y;
  162. }
  163. const double d = y1 - y0;
  164. if (std::abs (d) < kEps)
  165. {
  166. p.sign = 2;
  167. return;
  168. }
  169. const double a = (ypp - y0) / d, b = (yn - y0) / d;
  170. rp = rp > kEps ? rp : kEps;
  171. rn = rn > kEps ? rn : kEps;
  172. const double sr = (b - 1.0) / rn, s = 1.0 / 1.0, sl = -a / rp;
  173. if (! (F (sr) * F (s) > 0.0f))
  174. p.p4 = 0.0f;
  175. else
  176. {
  177. const float w1 = F (rn) * 2.0f + F (1.0), w2 = F (rn) + F (1.0) * 2.0f;
  178. p.p4 = (F (sr) * F (s) * (w1 + w2)) / (w1 * F (sr) + w2 * F (s));
  179. }
  180. if (! (F (s) * F (sl) > 0.0f))
  181. p.p5 = 0.0f;
  182. else
  183. {
  184. const float w1 = F (1.0) * 2.0f + F (rp), w2 = F (1.0) + F (rp) * 2.0f;
  185. p.p5 = (F (s) * F (sl) * (w1 + w2)) / (w1 * F (s) + w2 * F (sl));
  186. }
  187. p.p6 = F (1.0 - std::abs ((double) p.t));
  188. }
  189. std::vector<Pt> preparePoints (const std::vector<WsPoint>& in)
  190. {
  191. std::vector<Pt> pts;
  192. for (const auto& q : in)
  193. {
  194. Pt p;
  195. p.dt = q.dt;
  196. p.y = q.y;
  197. p.t = q.tension;
  198. p.mode = q.mode;
  199. pts.push_back (p);
  200. }
  201. for (int i = 0; i < (int) pts.size(); ++i)
  202. {
  203. auto& p = pts[(std::size_t) i];
  204. if (p.dt < 0.0f)
  205. p.dt = 0.0f;
  206. const float t = p.t;
  207. p.sign = t == 0.0f ? 0 : (t > 0.0f ? 1 : -1);
  208. if (i == 0)
  209. continue;
  210. const int m = p.mode;
  211. if (m == 0 || m == 1)
  212. {
  213. const auto e1 = F (std::exp ((double) std::abs (t) * std::log (1000001.0)) - 1.0);
  214. const float sg = t > 0.0f ? 1.0f : (t < 0.0f ? -1.0f : 0.0f);
  215. p.k = e1 * sg;
  216. if (p.k != 0.0f)
  217. {
  218. p.ilk = F (1.0 / std::abs ((double) p.k));
  219. p.L = F (std::log (std::abs ((double) p.k) + 1.0));
  220. }
  221. }
  222. else if (m == 3)
  223. {
  224. const int sg = t < 0.0f ? -1 : 1;
  225. const int c = stepCount (p);
  226. p.p6 = (float) sg * 0.5f;
  227. p.p4 = (float) (c + 1);
  228. p.p5 = 1.0f / (p.p4 - (float) sg);
  229. }
  230. else if (m == 4)
  231. {
  232. p.p4 = (float) (stepCount (p) + 1);
  233. p.p5 = 1.0f / p.p4;
  234. }
  235. else if (m == 5)
  236. {
  237. const int sg = t < 0.0f ? -1 : 1;
  238. p.p6 = (float) sg * 0.5f;
  239. p.p4 = (float) ((stepCount (p) + 1) * 2);
  240. p.p5 = 6.2831855f;
  241. }
  242. else if (m == 6)
  243. {
  244. const int c = stepCount (p);
  245. p.p4 = F ((double) c + 0.5);
  246. p.p5 = 6.2831855f;
  247. if (c == 0 && p.sign > 0)
  248. p.sign = 2;
  249. }
  250. else if (m == 7 || m == 8)
  251. p.p4 = t;
  252. else if (m == 9)
  253. p.p4 = F (std::abs ((double) t));
  254. else if (m == 10)
  255. prepareSmooth (pts, i);
  256. else if (m == 11 || m == 12)
  257. {
  258. auto d = std::abs ((double) t) * 0.9;
  259. if (d <= 1.52587890625e-05)
  260. d = 1.52587890625e-05;
  261. const auto u = 1.0 - d;
  262. p.p4 = F (1.0 - u * u * u);
  263. p.p5 = (1.0f - p.p4) / p.p4;
  264. p.p6 = p.p5;
  265. if (p.sign > 0)
  266. {
  267. p.p5 = -p.p5;
  268. p.p6 = p.p5 - 1.0f;
  269. }
  270. if (m == 12)
  271. {
  272. p.p5 = F ((double) p.p5 * 0.5);
  273. p.p6 = F ((double) p.p6 * 0.5);
  274. }
  275. }
  276. // linear when flat: modes 0, 1, 7, 8, 9, 11, 12 with no tension
  277. if (m < 16 && ((0x1b83u >> m) & 1u) != 0 && t == 0.0f)
  278. p.sign = 2;
  279. }
  280. return pts;
  281. }
  282. double curveK (const Pt& p, double x) noexcept
  283. {
  284. if (p.k == 0.0f)
  285. return x;
  286. if (p.k > 0.0f)
  287. return 1.0 - (std::exp ((1.0 - x) * (double) p.L) - 1.0) * (double) p.ilk;
  288. return (std::exp (x * (double) p.L) - 1.0) * (double) p.ilk;
  289. }
  290. // the shape of the segment ending at p, at phase ph (0…1)
  291. float shape (const Pt& p, double ph) noexcept
  292. {
  293. if (p.sign == 2)
  294. return F (ph);
  295. const double p4 = p.p4, p5 = p.p5, p6 = p.p6;
  296. switch (p.mode)
  297. {
  298. case 0: return F (curveK (p, ph));
  299. case 1:
  300. if (ph < 0.5)
  301. return F (curveK (p, ph * 2.0) * 0.5);
  302. return F (1.0 - curveK (p, (1.0 - ph) * 2.0) * 0.5);
  303. case 3: return (float) (int) std::nearbyint (ph * p4 - p6) * p.p5;
  304. case 4:
  305. {
  306. const auto v = ph * p4 - 0.5;
  307. const auto i2 = std::nearbyint (v - 0.5);
  308. auto d = (v - i2) * 2.0 - 1.0;
  309. if (p.sign < 0)
  310. {
  311. const auto z = 1.0 - std::abs (d);
  312. d = (1.0 - z * z * z) * (std::signbit (d) ? -1.0 : 1.0);
  313. }
  314. else
  315. d = d * d * d;
  316. return F ((i2 + d * 0.5 + 1.0) * p5);
  317. }
  318. case 5: return (float) (((long long) std::nearbyint (ph * p4 - p6)) & 1);
  319. case 6:
  320. {
  321. if (p.sign < 0)
  322. return F ((std::sin ((ph * p4 + 0.75) * p5) + 1.0) * 0.5);
  323. const auto v = ph * p4 - 0.5;
  324. const auto i2 = std::nearbyint (v - 0.5);
  325. return F (std::abs ((v - i2) * 2.0 - 1.0));
  326. }
  327. case 7:
  328. {
  329. const auto d = p.sign > 0 ? 1.0 - ph : ph;
  330. const auto d3 = d * d * d;
  331. return F (ph - (d3 * d3 - d) * p4);
  332. }
  333. case 8:
  334. {
  335. if (ph < 0.5)
  336. {
  337. const auto x = ph * 2.0;
  338. const auto d = p.sign > 0 ? 1.0 - x : x;
  339. const auto d3 = d * d * d;
  340. return F ((x - (d3 * d3 - d) * p4) * 0.5);
  341. }
  342. const auto x = (1.0 - ph) * 2.0;
  343. const auto d = p.sign > 0 ? 1.0 - x : x;
  344. const auto d3 = d * d * d;
  345. return F (1.0 - (x - (d3 * d3 - d) * p4) * 0.5);
  346. }
  347. case 9:
  348. {
  349. const auto hp = (double) 1.5707963705062866;
  350. const auto d = p.sign > 0 ? std::sin (ph * hp) : 1.0 - std::cos (ph * hp);
  351. return F (ph - (ph - d) * p4);
  352. }
  353. case 10:
  354. {
  355. const auto x2 = ph * ph, x1 = ph - 1.0;
  356. return F (ph + ((((x2 * 3.0 - x2 * 2.0 * ph) + x2 * x1 * p4) + ph * x1 * x1 * p5) - ph) * p6);
  357. }
  358. case 11:
  359. if (p.sign < 1)
  360. return F (p5 / (1.0 - p4 * ph) - p6);
  361. return F (p5 / (1.0 - p4 * (1.0 - ph)) - p6);
  362. case 12:
  363. if (ph < 0.5)
  364. {
  365. if (p.sign < 1)
  366. return F (p5 / (1.0 - p4 * ph * 2.0) - p6);
  367. return F (p5 / (1.0 - p4 * (1.0 - ph * 2.0)) - p6);
  368. }
  369. else
  370. {
  371. const auto x = (1.0 - ph) * 2.0;
  372. if (p.sign < 1)
  373. return F ((1.0 - p5 / (1.0 - p4 * x)) + p6);
  374. return F ((1.0 - p5 / (1.0 - p4 * (1.0 - x))) + p6);
  375. }
  376. default: return 0.0f; // 2 (hold) and unknown modes
  377. }
  378. }
  379. const std::vector<WsPoint>& defaultCurve()
  380. {
  381. static const std::vector<WsPoint> c { { 0.0f, 0.0f, 0.0f, 0 }, { 1.0f, 1.0f, 0.0f, 0 } };
  382. return c;
  383. }
  384. // --------------------------------------------------------------------------------------------- oversampling (§2.6)
  385. // polyphase IIR half-band: two chains of three first-order all-passes (y = x[n−1] + (x − y[n−1])·a), in double
  386. constexpr double kA[3] = { 0.1473771136010466, 0.48246854276970014, 0.8830050257693731 };
  387. constexpr double kB[3] = { 0.039151597734460045, 0.3026468483284934, 0.6746159185469639 };
  388. struct HalfBand
  389. {
  390. // per channel: outputs A1 A2 A3 B1 B2 B3, then the previous inputs of each all-pass
  391. double s[12] {};
  392. void clear() noexcept { std::fill (std::begin (s), std::end (s), 0.0); }
  393. void flush() noexcept
  394. {
  395. for (int i = 0; i < 6; ++i)
  396. if (std::abs (s[i]) < kEps)
  397. s[i] = 0.0;
  398. }
  399. // Both channels in one pass, on two-lane double vectors (lane 0 = left, lane 1 = right): each lane gets the
  400. // channel's scalar steps (y = x[n−1] + (x − y[n−1])·a per all-pass), in the same order with one rounding each.
  401. static void up2 (HalfBand& h0, HalfBand& h1, const float* x0, const float* x1, float* y0, float* y1, int m) noexcept
  402. {
  403. V2 s[12];
  404. load (h0, h1, s);
  405. for (int n = 0; n < m; ++n)
  406. {
  407. const V2 v = { (double) x0[n], (double) x1[n] };
  408. const V2 a1 = s[6] + (v - s[0]) * kA[0];
  409. s[6] = v, s[0] = a1;
  410. const V2 b1 = s[9] + (v - s[3]) * kB[0];
  411. s[9] = v, s[3] = b1;
  412. const V2 a2 = s[7] + (s[0] - s[1]) * kA[1];
  413. s[7] = s[0], s[1] = a2;
  414. const V2 b2 = s[10] + (s[3] - s[4]) * kB[1];
  415. s[10] = s[3], s[4] = b2;
  416. const V2 a3 = s[8] + (s[1] - s[2]) * kA[2];
  417. s[2] = a3;
  418. const V2 b3 = s[11] + (s[4] - s[5]) * kB[2];
  419. s[5] = b3;
  420. s[8] = s[1], s[11] = s[4];
  421. y0[2 * n] = (float) b3[0];
  422. y0[2 * n + 1] = (float) a3[0];
  423. y1[2 * n] = (float) b3[1];
  424. y1[2 * n + 1] = (float) a3[1];
  425. }
  426. store (s, h0, h1);
  427. }
  428. static void down2 (HalfBand& h0, HalfBand& h1, const float* x0, const float* x1, float* y0, float* y1, int m) noexcept
  429. {
  430. V2 s[12];
  431. load (h0, h1, s);
  432. for (int n = 0; n < m; ++n)
  433. {
  434. const V2 e = { (double) x0[2 * n], (double) x1[2 * n] }, o = { (double) x0[2 * n + 1], (double) x1[2 * n + 1] };
  435. const V2 a1 = s[6] + (e - s[0]) * kA[0];
  436. s[6] = e, s[0] = a1;
  437. const V2 b1 = s[9] + (o - s[3]) * kB[0];
  438. s[9] = o, s[3] = b1;
  439. const V2 a2 = s[7] + (s[0] - s[1]) * kA[1];
  440. s[7] = s[0], s[1] = a2;
  441. const V2 b2 = s[10] + (s[3] - s[4]) * kB[1];
  442. s[10] = s[3], s[4] = b2;
  443. const V2 a3 = s[8] + (s[1] - s[2]) * kA[2];
  444. s[8] = s[1], s[2] = a3;
  445. const V2 b3 = s[11] + (s[4] - s[5]) * kB[2];
  446. s[11] = s[4], s[5] = b3;
  447. const V2 y = (b3 + a3) * 0.5;
  448. y0[n] = (float) y[0];
  449. y1[n] = (float) y[1];
  450. }
  451. store (s, h0, h1);
  452. }
  453. private:
  454. using V2 = double __attribute__ ((vector_size (16)));
  455. static void load (const HalfBand& h0, const HalfBand& h1, V2* s) noexcept
  456. {
  457. for (int i = 0; i < 12; ++i)
  458. s[i] = V2 { h0.s[i], h1.s[i] };
  459. }
  460. static void store (const V2* s, HalfBand& h0, HalfBand& h1) noexcept
  461. {
  462. for (int i = 0; i < 12; ++i)
  463. h0.s[i] = s[i][0], h1.s[i] = s[i][1];
  464. h0.flush();
  465. h1.flush();
  466. }
  467. };
  468. // ------------------------------------------------------------------------------------------------- kernels (§2.5)
  469. struct KernelArgs
  470. {
  471. const std::int16_t* T = nullptr; // entry k at T[k + 1]
  472. int N = 0;
  473. float dry = 0, wet = 0, pre = 0;
  474. bool unipolar = false;
  475. };
  476. constexpr ld kTwo31 = 2147483648.0L;
  477. // The kernels' bodies run under a control word their caller set (X87Mode): the block entry points below set it once
  478. // per call; the smoothed path sets it once per block and calls the bodies sample by sample.
  479. [[gnu::always_inline]] inline void nearestBody (const float* x, float* y, int m, const KernelArgs& a) noexcept
  480. {
  481. const auto* T = a.T + 1;
  482. const ld A = a.dry, bK = (ld) a.wet * 3.0517578125e-05L;
  483. if (! a.unipolar)
  484. {
  485. const ld preN = (ld) a.pre * (ld) a.N;
  486. for (int k = 0; k < m; ++k)
  487. {
  488. const ld X = x[k];
  489. const ld v = X * preN;
  490. const long long i = std::abs (v) >= kTwo31 ? -2147483648LL : x87Int (v);
  491. const long long ai = std::min<long long> (i < 0 ? -i : i, a.N);
  492. const ld t = i < 0 ? -(ld) T[ai] : (ld) T[ai];
  493. y[k] = (float) (X * A + t * bK);
  494. }
  495. return;
  496. }
  497. const ld hN = (ld) a.N * 0.5L, pHN = (ld) a.pre * (ld) a.N * 0.5L;
  498. for (int k = 0; k < m; ++k)
  499. {
  500. const ld X = x[k];
  501. const ld v = X * pHN + hN;
  502. long long i = std::abs (v) >= kTwo31 ? -2147483648LL : x87Int (v);
  503. i = std::clamp<long long> (i, 0, a.N);
  504. y[k] = (float) (X * A + (ld) T[i] * bK);
  505. }
  506. }
  507. // the interpolating kernels run with the x87 rounding set toward zero: every product, sum and the float store
  508. [[gnu::always_inline]] inline void interpBody (const float* x, float* y, int m, const KernelArgs& a) noexcept
  509. {
  510. const auto* T = a.T + 1;
  511. volatile float vdry = a.dry, vwet = a.wet, vpre = a.pre; // computed after the mode change
  512. const ld A = vdry, bK = (ld) vwet * 3.0517578125e-05L;
  513. if (! a.unipolar)
  514. {
  515. const ld preN = (ld) vpre * (ld) a.N;
  516. for (int k = 0; k < m; ++k)
  517. {
  518. const ld X = x87Load (x + k);
  519. const ld v = X * preN;
  520. ld t;
  521. if (std::abs (v) >= kTwo31)
  522. t = 0.0L;
  523. else
  524. {
  525. const auto i = x87Int (v); // toward zero in this mode
  526. const ld fr = v - x87Round (v); // (ld) i: the same integer, without the round trip through memory
  527. if (i >= 0)
  528. {
  529. const auto j = std::min<long long> (i, a.N);
  530. const ld t0 = x87Load (T + j), t1 = x87Load (T + j + 1);
  531. t = t0 + (t1 - t0) * fr;
  532. }
  533. else
  534. {
  535. const auto j = std::min<long long> (-i, a.N);
  536. const ld t0 = x87Load (T + j), t1 = x87Load (T + j + 1);
  537. t = -(t0 - (t1 - t0) * fr);
  538. }
  539. }
  540. x87Store (y + k, X * A + t * bK);
  541. }
  542. return;
  543. }
  544. const ld hN = (ld) a.N * 0.5L, pHN = (ld) vpre * (ld) a.N * 0.5L;
  545. for (int k = 0; k < m; ++k)
  546. {
  547. const ld X = x[k];
  548. const ld v = X * pHN + hN;
  549. long long i;
  550. ld fr;
  551. if (std::abs (v) >= kTwo31)
  552. {
  553. i = -2147483648LL;
  554. fr = v - (ld) i;
  555. }
  556. else
  557. {
  558. i = x87Int (v);
  559. fr = v - (ld) i;
  560. }
  561. i = std::clamp<long long> (i, -1, a.N);
  562. const ld t0 = T[i], t1 = T[i + 1];
  563. y[k] = (float) (X * A + (t0 + (t1 - t0) * fr) * bK);
  564. }
  565. }
  566. [[gnu::noinline]] void kernelNearest (const float* x, float* y, int m, const KernelArgs& a) noexcept
  567. {
  568. const X87Mode mode (false);
  569. nearestBody (x, y, m, a);
  570. }
  571. [[gnu::noinline]] void kernelInterp (const float* x, float* y, int m, const KernelArgs& a) noexcept
  572. {
  573. const X87Mode mode (true);
  574. interpBody (x, y, m, a);
  575. }
  576. inline bool sameBits (float a, float b) noexcept
  577. {
  578. std::uint32_t x, y;
  579. std::memcpy (&x, &a, 4);
  580. std::memcpy (&y, &b, 4);
  581. return x == y;
  582. }
  583. // SMOOTH: the gains glide sample by sample (float steps, as in FL) and each sample of both channels goes through the
  584. // kernel with that sample's gains. The control word is set once for the block instead of once per sample and channel.
  585. template <bool interp>
  586. [[gnu::noinline]] void kernelSmoothed (float* x0, float* x1, int m, KernelArgs a, float (&sm)[3], float preGain, float wet,
  587. float dry, float smoothC) noexcept
  588. {
  589. const X87Mode mode (interp);
  590. auto body = [&a] (float* x, int len) {
  591. if constexpr (interp)
  592. interpBody (x, x, len, a);
  593. else
  594. nearestBody (x, x, len, a);
  595. };
  596. for (int i = 0; i < m; ++i)
  597. {
  598. const float p = sm[0] + (preGain - sm[0]) * smoothC;
  599. const float w = sm[1] + (wet - sm[1]) * smoothC;
  600. const float d = sm[2] + (dry - sm[2]) * smoothC;
  601. if (sameBits (p, sm[0]) && sameBits (w, sm[1]) && sameBits (d, sm[2]))
  602. {
  603. // the glide reached its fixed point (bit for bit): every later step gives these same gains, so the rest of
  604. // the block is one constant-gain run (each sample's result depends only on its input and the gains)
  605. a.pre = p, a.wet = w, a.dry = d;
  606. body (x0 + i, m - i);
  607. body (x1 + i, m - i);
  608. return;
  609. }
  610. sm[0] = p, sm[1] = w, sm[2] = d;
  611. a.pre = p, a.wet = w, a.dry = d;
  612. body (x0 + i, 1);
  613. body (x1 + i, 1);
  614. }
  615. }
  616. } // namespace
  617. // ================================================================================================== Soft Clipper
  618. std::optional<SoftClipperState> parseSoftClipper (const std::vector<std::uint8_t>& state)
  619. {
  620. if (state.size() < 8)
  621. return std::nullopt;
  622. return SoftClipperState { i32At (state, 0), i32At (state, 4) };
  623. }
  624. void softClipperToParams (const SoftClipperState& s, float* params) noexcept
  625. {
  626. params[0] = norm (s.threshold, 1, 127);
  627. params[1] = norm (s.post, 0, 160);
  628. }
  629. SoftClipperState softClipperFromParams (const float* params) noexcept
  630. {
  631. return { rawFrom (params[0], 1, 127), rawFrom (params[1], 0, 160) };
  632. }
  633. void softClip (float* x, int n, int threshold, int post) noexcept
  634. {
  635. // threshold t = v/128 (ceiling 1); range 1 − t (2^-16 when ≤ 0); its inverse — all floats (§1.1)
  636. const float t = (float) threshold * 0.0078125f;
  637. float r = 1.0f - t;
  638. if (r <= 0.0f)
  639. r = 1.52587890625e-05f;
  640. const float inv = 1.0f / r;
  641. std::int32_t tb = 0;
  642. std::memcpy (&tb, &t, 4);
  643. {
  644. const X87Mode mode (false);
  645. for (int k = 0; k < n; ++k)
  646. {
  647. std::uint32_t xb = 0;
  648. std::memcpy (&xb, x + k, 4);
  649. const auto ab = (std::int32_t) (xb & 0x7fffffffu);
  650. if (ab <= tb) // compared as integers (|x| > t for every |x| when t < 0)
  651. continue;
  652. float a = 0;
  653. std::memcpy (&a, &ab, 4);
  654. const ld e = ((ld) t - (ld) a) * (ld) inv;
  655. const auto yv = (float) ((1.0L - std::exp (e)) * (ld) r + (ld) t);
  656. std::uint32_t yb = 0;
  657. std::memcpy (&yb, &yv, 4);
  658. yb |= xb & 0x80000000u;
  659. std::memcpy (x + k, &yb, 4);
  660. }
  661. }
  662. const float g = postGain (post);
  663. if (g != 1.0f)
  664. for (int k = 0; k < n; ++k)
  665. x[k] = x[k] * g;
  666. }
  667. const char* softClipperParamName (int param) noexcept
  668. {
  669. static const char* names[softClipperParams] = { "Threshold", "Post" };
  670. return param >= 0 && param < softClipperParams ? names[param] : "";
  671. }
  672. std::string softClipperParamText (int param, float value01)
  673. {
  674. char text[32];
  675. if (param == 0)
  676. {
  677. const auto v = rawFrom (value01, 1, 127);
  678. std::snprintf (text, sizeof (text), "%.1f dB", 20.0 * std::log10 (v / 128.0));
  679. }
  680. else
  681. {
  682. const auto g = postGain (rawFrom (value01, 0, 160));
  683. if (g <= 0.0f)
  684. return "-inf dB";
  685. std::snprintf (text, sizeof (text), "%+.1f dB", 20.0 * std::log10 ((double) g));
  686. }
  687. return text;
  688. }
  689. namespace
  690. {
  691. class SoftClipperEffect final : public Effect
  692. {
  693. public:
  694. void prepare (double) noexcept override {}
  695. void reset() noexcept override {}
  696. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  697. {
  698. const auto s = softClipperFromParams (params.data());
  699. softClip (left, n, s.threshold, s.post);
  700. softClip (right, n, s.threshold, s.post);
  701. }
  702. };
  703. } // namespace
  704. std::unique_ptr<Effect> makeSoftClipper() { return std::make_unique<SoftClipperEffect>(); }
  705. // ==================================================================================================== WaveShaper
  706. std::optional<WaveShaperState> parseWaveShaper (const std::vector<std::uint8_t>& d)
  707. {
  708. // version (low word 3; high word = layout 2…4), pre / mix / post, unipolar, CENTER, oversampling, the map
  709. // (kind, version 3, points of 24 bytes, 20-byte header), then the options of later layouts (§2.2)
  710. if (d.size() < 34)
  711. return std::nullopt;
  712. auto v = (std::uint32_t) i32At (d, 0);
  713. if ((std::int32_t) v > 0x40003)
  714. return std::nullopt;
  715. if (v > 0xffff)
  716. v >>= 16;
  717. WaveShaperState s;
  718. s.version = (int) v;
  719. s.pre = i32At (d, 4);
  720. s.mix = i32At (d, 8);
  721. s.post = i32At (d, 12);
  722. s.unipolar = d[16] != 0;
  723. s.center = d[17] != 0;
  724. s.oversampling = i32At (d, 18);
  725. std::size_t o = 22;
  726. const auto mapVersion = i32At (d, o + 4);
  727. const auto n = i32At (d, o + 8);
  728. o += 12;
  729. if (mapVersion != 3 || n < 0 || n > 100000 || d.size() < o + 24 * (std::size_t) n + 20)
  730. return std::nullopt;
  731. float prev = 0.0f;
  732. for (int i = 0; i < n; ++i, o += 24)
  733. {
  734. WsPoint p;
  735. p.dt = (float) f64At (d, o);
  736. p.y = (float) f64At (d, o + 8);
  737. if (std::isnan (p.y))
  738. p.y = prev;
  739. prev = p.y;
  740. p.tension = f32At (d, o + 16);
  741. p.mode = d[o + 20];
  742. s.points.push_back (p);
  743. }
  744. o += 20;
  745. if (v > 1 && d.size() >= o + 4)
  746. o += 4; // step-editing flag (editor only)
  747. if (v > 2 && d.size() > o)
  748. s.hq = d[o++] != 0;
  749. if (v > 3 && d.size() > o)
  750. s.smooth = d[o++] != 0;
  751. // the map spans exactly 1: a point at 0 is added if the first starts later; every Δt is scaled by 1/total
  752. auto& pts = s.points;
  753. if (! pts.empty() && pts[0].dt > 0.0f)
  754. pts.insert (pts.begin(), WsPoint { 0.0f, pts[0].y, 0.0f, 0 });
  755. double total = 0.0;
  756. for (const auto& p : pts)
  757. total += (double) p.dt;
  758. if (total > 0.0)
  759. {
  760. const double f = 1.0 / total;
  761. if (f != 1.0)
  762. for (auto& p : pts)
  763. p.dt = (float) ((double) p.dt * f);
  764. }
  765. else
  766. pts.push_back (WsPoint { 1.0f, pts.empty() ? 0.0f : pts.back().y, 0.0f, 0 });
  767. return s;
  768. }
  769. void waveShaperToParams (const WaveShaperState& s, float* params) noexcept
  770. {
  771. params[0] = norm (s.pre, 64, 192);
  772. params[1] = norm (s.mix, 0, 128);
  773. params[2] = norm (s.post, 0, 128);
  774. params[3] = norm (s.oversampling, 0, 4);
  775. params[4] = s.hq ? 1.0f : 0.0f;
  776. params[5] = s.smooth ? 1.0f : 0.0f;
  777. params[6] = s.center ? 1.0f : 0.0f;
  778. params[7] = s.unipolar ? 1.0f : 0.0f;
  779. }
  780. const char* waveShaperParamName (int param) noexcept
  781. {
  782. static const char* names[waveShaperParams] = { "Pre amp", "Mix", "Post gain", "Oversampling", "HQ", "Smooth", "Center", "Unipolar" };
  783. return param >= 0 && param < waveShaperParams ? names[param] : "";
  784. }
  785. std::string waveShaperParamText (int param, float v)
  786. {
  787. char text[32];
  788. switch (param)
  789. {
  790. case 0:
  791. {
  792. const float s0 = (float) rawFrom (v, 64, 192) * 0.0078125f;
  793. const auto g = (std::exp ((double) s0 * std::log (4.0)) - 1.0) / 3.0;
  794. std::snprintf (text, sizeof (text), "%+.1f dB", 20.0 * std::log10 (g));
  795. return text;
  796. }
  797. case 1: std::snprintf (text, sizeof (text), "%d%%", (int) std::lround (rawFrom (v, 0, 128) * 100.0 / 128.0)); return text;
  798. case 2:
  799. {
  800. const auto g = postGain (rawFrom (v, 0, 128));
  801. if (g <= 0.0f)
  802. return "-inf dB";
  803. std::snprintf (text, sizeof (text), "%+.1f dB", 20.0 * std::log10 ((double) g));
  804. return text;
  805. }
  806. case 3:
  807. {
  808. const auto k = rawFrom (v, 0, 4);
  809. if (k == 0)
  810. return "Off";
  811. std::snprintf (text, sizeof (text), "x%d", 1 << k);
  812. return text;
  813. }
  814. default: return v >= 0.5f ? "On" : "Off";
  815. }
  816. }
  817. std::vector<std::int16_t> shaperTable (const std::vector<WsPoint>& points, bool unipolar)
  818. {
  819. // the map read by FL's incremental evaluator in steps of 1/N, each value × (32767 or 65535) rounded (§2.3)
  820. auto pts = preparePoints (points.empty() ? defaultCurve() : points);
  821. const int n = (int) pts.size();
  822. const double off = unipolar ? 0.5 : 0.0;
  823. auto level = [&] (int i) { return (double) pts[(std::size_t) i].y - off; };
  824. const int N = (unipolar ? 2 : 1) * 0x4000 - 1;
  825. const double sc = (double) ((unipolar ? 2 : 1) * 0x8000 - 1);
  826. const double dx = 1.0 / N;
  827. std::vector<std::int16_t> T ((std::size_t) N + 3, 0);
  828. int idx = 0;
  829. double rate, ph;
  830. if (pts[0].dt == 0.0f)
  831. rate = 1.0, ph = 1.0;
  832. else
  833. rate = 1.0 / (double) pts[0].dt, ph = 0.0;
  834. float start = F (level (0)), delta = 0.0f, val = start;
  835. for (int i = 0; i <= N; ++i)
  836. {
  837. const auto r = std::nearbyint ((double) val * sc);
  838. T[(std::size_t) i + 1] = (std::int16_t) std::clamp (r, -32768.0, 32767.0);
  839. ph += dx * rate;
  840. while (ph >= 1.0)
  841. {
  842. const int old = idx;
  843. ph = rate == 0.0 ? dx : (ph - 1.0) / rate;
  844. start = F (level (idx));
  845. if (idx < n - 1)
  846. ++idx;
  847. delta = F (level (idx) - (double) start);
  848. if (old == idx)
  849. ph = 0.0, rate = 0.0;
  850. else
  851. {
  852. const double dt = pts[(std::size_t) idx].dt;
  853. if (dt <= 0.0)
  854. rate = 1.0, ph += 1.0;
  855. else
  856. rate = 1.0 / dt, ph *= rate;
  857. }
  858. }
  859. val = start + shape (pts[(std::size_t) idx], ph) * delta;
  860. }
  861. T[0] = T[1];
  862. T[(std::size_t) N + 2] = T[(std::size_t) N + 1];
  863. return T;
  864. }
  865. // ----------------------------------------------------------------------------------------------------- engine
  866. struct WaveShaperEngine::Impl
  867. {
  868. static constexpr int maxBlock = 512, maxOrder = 4;
  869. std::vector<std::int16_t> tables[2]; // bipolar, unipolar
  870. double rate = 44100.0;
  871. float smoothC = 0.0f, dcA = 0.0f, dcB = 0.96f;
  872. // controls
  873. int pre = -1, mix = -1, post = -1, order = 0;
  874. bool hq = false, smooth = false, center = false, unipolar = false, render = false;
  875. float dry = 0.0f, wet = 1.0f, preGain = 1.0f;
  876. float sm[3] {}; // smoothed pre, wet, dry (start at 0, as in FL)
  877. int stages = -1;
  878. HalfBand up[maxOrder][2], down[maxOrder][2];
  879. double dc[2][2] {}; // per channel: position, velocity
  880. std::vector<float> a[2], b[2];
  881. Impl()
  882. {
  883. for (int c = 0; c < 2; ++c)
  884. {
  885. a[c].assign ((std::size_t) maxBlock << maxOrder, 0.0f);
  886. b[c].assign ((std::size_t) maxBlock << maxOrder, 0.0f);
  887. }
  888. }
  889. void computeGains() noexcept
  890. {
  891. const float k = 0.0078125f;
  892. const float s0 = (float) pre * k;
  893. preGain = F (std::exp ((double) s0 * std::log (4.0)) - 1.0) / 3.0f;
  894. const float g = postGain (post);
  895. const float m = (float) mix * k;
  896. wet = m * g;
  897. dry = ((g - wet) * preGain) * g;
  898. }
  899. void resetStages() noexcept
  900. {
  901. for (auto& s : up)
  902. for (auto& h : s)
  903. h.clear();
  904. for (auto& s : down)
  905. for (auto& h : s)
  906. h.clear();
  907. }
  908. void dcBlock (float* x, int m, int c) noexcept
  909. {
  910. // CENTER: a two-pole tracker subtracted from the signal, x87 state kept in long double within the block
  911. const X87Mode mode (false);
  912. ld pos = dc[c][0], vel = dc[c][1];
  913. const ld A = dcA, B = dcB;
  914. for (int k = 0; k < m; ++k)
  915. {
  916. const ld X = x[k];
  917. vel = vel + (X - pos) * A;
  918. pos = pos + vel;
  919. vel = vel * B;
  920. x[k] = (float) (X - pos);
  921. }
  922. auto fl = [] (ld v) { return std::abs (v) <= (ld) kEps ? 0.0 : (double) v; };
  923. dc[c][0] = fl (pos);
  924. dc[c][1] = fl (vel);
  925. }
  926. void block (float* L, float* R, int n) noexcept
  927. {
  928. const int k = order > 0 ? (render ? 4 : order) : 0;
  929. if (k != stages)
  930. {
  931. resetStages();
  932. stages = k;
  933. }
  934. float* io[2] = { L, R };
  935. float* buf[2] = { L, R };
  936. int m = n;
  937. if (k > 0)
  938. {
  939. const float* src[2] = { L, R };
  940. int len = n;
  941. for (int s = k - 1; s >= 0; --s)
  942. {
  943. const bool toA = ((k - 1 - s) & 1) == 0;
  944. float* dst0 = toA ? a[0].data() : b[0].data();
  945. float* dst1 = toA ? a[1].data() : b[1].data();
  946. HalfBand::up2 (up[s][0], up[s][1], src[0], src[1], dst0, dst1, len);
  947. src[0] = dst0, src[1] = dst1;
  948. len *= 2;
  949. }
  950. buf[0] = (float*) src[0], buf[1] = (float*) src[1];
  951. m = len;
  952. }
  953. KernelArgs args;
  954. args.unipolar = unipolar;
  955. const auto& T = tables[unipolar ? 1 : 0];
  956. args.T = T.data();
  957. args.N = (int) T.size() - 3;
  958. const bool interp = render || hq;
  959. auto run = [&] (const float* x, float* y, int len) {
  960. if (interp)
  961. kernelInterp (x, y, len, args);
  962. else
  963. kernelNearest (x, y, len, args);
  964. };
  965. if (! smooth)
  966. {
  967. args.dry = dry, args.wet = wet, args.pre = preGain;
  968. for (int c = 0; c < 2; ++c)
  969. run (buf[c], buf[c], m);
  970. }
  971. else if (interp)
  972. kernelSmoothed<true> (buf[0], buf[1], m, args, sm, preGain, wet, dry, smoothC);
  973. else
  974. kernelSmoothed<false> (buf[0], buf[1], m, args, sm, preGain, wet, dry, smoothC);
  975. if (k > 0)
  976. {
  977. const float* src[2] = { buf[0], buf[1] };
  978. int len = m;
  979. for (int s = k - 1; s >= 0; --s)
  980. {
  981. float* dst[2];
  982. for (int c = 0; c < 2; ++c)
  983. dst[c] = s == 0 ? io[c] : (src[c] == a[c].data() ? b[c].data() : a[c].data());
  984. len /= 2;
  985. HalfBand::down2 (down[s][0], down[s][1], src[0], src[1], dst[0], dst[1], len);
  986. src[0] = dst[0], src[1] = dst[1];
  987. }
  988. }
  989. if (center)
  990. {
  991. dcBlock (L, n, 0);
  992. dcBlock (R, n, 1);
  993. }
  994. }
  995. };
  996. WaveShaperEngine::WaveShaperEngine() : d (std::make_unique<Impl>())
  997. {
  998. setCurve ({});
  999. prepare (44100.0);
  1000. }
  1001. WaveShaperEngine::~WaveShaperEngine() = default;
  1002. void WaveShaperEngine::setCurve (const std::vector<WsPoint>& points)
  1003. {
  1004. d->tables[0] = shaperTable (points, false);
  1005. d->tables[1] = shaperTable (points, true);
  1006. }
  1007. void WaveShaperEngine::prepare (double sampleRate) noexcept
  1008. {
  1009. d->rate = sampleRate > 0 ? sampleRate : 44100.0;
  1010. // parameter glide: 1 − 0.1^(1/(fs·0.005)); CENTER: 2.2835e-5·44100/fs and 0.96 (§2.7, §2.8)
  1011. const double den = (double) ((float) d->rate * 0.005f);
  1012. d->smoothC = F (1.0 - std::exp ((1.0 / den) * std::log (0.1)));
  1013. d->dcA = F (44100.0 / d->rate * 2.2835e-05);
  1014. d->dcB = 0.96f;
  1015. reset();
  1016. }
  1017. void WaveShaperEngine::reset() noexcept
  1018. {
  1019. d->resetStages();
  1020. d->stages = -1;
  1021. for (auto& c : d->dc)
  1022. c[0] = c[1] = 0.0;
  1023. }
  1024. void WaveShaperEngine::setControls (int pre, int mix, int post, int oversampling, bool hq, bool smooth, bool center,
  1025. bool unipolar, bool renderMode) noexcept
  1026. {
  1027. if (pre != d->pre || mix != d->mix || post != d->post)
  1028. {
  1029. d->pre = pre, d->mix = mix, d->post = post;
  1030. d->computeGains();
  1031. }
  1032. d->order = std::clamp (oversampling, 0, Impl::maxOrder);
  1033. d->hq = hq, d->smooth = smooth, d->center = center, d->unipolar = unipolar, d->render = renderMode;
  1034. }
  1035. void WaveShaperEngine::process (float* left, float* right, int n) noexcept
  1036. {
  1037. for (int i = 0; i < n; i += Impl::maxBlock)
  1038. d->block (left + i, right + i, std::min (Impl::maxBlock, n - i));
  1039. }
  1040. namespace
  1041. {
  1042. class WaveShaperEffect final : public Effect
  1043. {
  1044. public:
  1045. explicit WaveShaperEffect (const std::vector<WsPoint>& points) { engine.setCurve (points); }
  1046. void prepare (double sampleRate) noexcept override { engine.prepare (sampleRate); }
  1047. void reset() noexcept override { engine.reset(); }
  1048. void process (float* left, float* right, int n, const EffectParams& p, double) noexcept override
  1049. {
  1050. engine.setControls (rawFrom (p[0], 64, 192), rawFrom (p[1], 0, 128), rawFrom (p[2], 0, 128), rawFrom (p[3], 0, 4),
  1051. p[4] >= 0.5f, p[5] >= 0.5f, p[6] >= 0.5f, p[7] >= 0.5f, offlineRendering());
  1052. engine.process (left, right, n);
  1053. }
  1054. private:
  1055. WaveShaperEngine engine;
  1056. };
  1057. } // namespace
  1058. std::unique_ptr<Effect> makeWaveShaper (const std::vector<std::uint8_t>& flState)
  1059. {
  1060. std::vector<WsPoint> points;
  1061. if (const auto s = parseWaveShaper (flState))
  1062. points = s->points;
  1063. return std::make_unique<WaveShaperEffect> (points);
  1064. }
  1065. } // namespace lynx::flshape