StereoFx.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #include "core/StereoFx.h"
  2. #include <algorithm>
  3. #include <array>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstring>
  7. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  8. #include "core/Simd.h"
  9. #endif
  10. // Clean-room implementation of docs/analise/REA_rodada54.md. Comments name the behaviour, not FL's code.
  11. // The engines are single-precision code whose results depend on the order of operations: no FMA contraction.
  12. #if defined(__clang__)
  13. #pragma clang fp contract(off)
  14. #endif
  15. namespace lynx::stereofx
  16. {
  17. namespace
  18. {
  19. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t pos) noexcept
  20. {
  21. std::int32_t v = 0;
  22. std::memcpy (&v, d.data() + pos, 4);
  23. return v;
  24. }
  25. constexpr int flBlock = 512; // FL's usual block size: longer host blocks are split into calls of this size
  26. constexpr float tiny = 5.9604644775390625e-08f; // 2⁻²⁴
  27. float toUnit (int raw, int lo, int hi) noexcept { return (float) (std::clamp (raw, lo, hi) - lo) / (float) (hi - lo); }
  28. int fromUnit (float v, int lo, int hi) noexcept
  29. {
  30. return lo + (int) std::lround (std::clamp ((double) v, 0.0, 1.0) * (double) (hi - lo));
  31. }
  32. // flush-to-zero / denormals-are-zero while an engine runs, as the plugins' own render calls do
  33. class Ftz
  34. {
  35. public:
  36. Ftz() noexcept
  37. {
  38. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  39. saved = _mm_getcsr();
  40. _mm_setcsr (saved | 0x8040u);
  41. #endif
  42. }
  43. ~Ftz()
  44. {
  45. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  46. _mm_setcsr (saved);
  47. #endif
  48. }
  49. Ftz (const Ftz&) = delete;
  50. Ftz& operator= (const Ftz&) = delete;
  51. private:
  52. unsigned int saved = 0;
  53. };
  54. // ---- the shared plugin framework of Stereo Shaper and Stereo Enhancer (REA_rodada54 §1) ------------------------
  55. // per-block linear ramp of a gain towards its target, slope limited to `maxSlope` per sample
  56. struct Ramp
  57. {
  58. float current = 0.0f, target = 0.0f;
  59. void next (int n, float maxSlope, float& start, float& inc) noexcept
  60. {
  61. start = current;
  62. inc = target - start;
  63. std::uint32_t bits = 0;
  64. std::memcpy (&bits, &inc, 4);
  65. if (bits == 0)
  66. return;
  67. current = target;
  68. inc = inc / (float) n;
  69. if (std::fabs (inc) > maxSlope)
  70. {
  71. inc = std::copysign (maxSlope, inc);
  72. current = start + inc * (float) n;
  73. if (std::fabs (current) < tiny)
  74. {
  75. current = 0.0f;
  76. inc = -start / (float) n;
  77. }
  78. }
  79. }
  80. };
  81. // all-pass biquad (direct form I, double state, float in/out); state values below 2⁻²⁴ are zeroed after each block
  82. struct AllPass
  83. {
  84. double b0 = 1.0, b1 = 0.0, b2 = 0.0, a1 = 0.0, a2 = 0.0;
  85. double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0;
  86. void design (double alpha, double cosw) noexcept
  87. {
  88. const auto d = 1.0 / (alpha + 1.0);
  89. a1 = d * cosw * -2.0;
  90. a2 = d * (1.0 - alpha);
  91. b1 = a1;
  92. b0 = a2;
  93. b2 = 1.0;
  94. }
  95. void clear() noexcept { x1 = x2 = y1 = y2 = 0.0; }
  96. void run (float* x, int n, int stride) noexcept
  97. {
  98. for (int i = 0; i < n; ++i)
  99. {
  100. auto acc = x1 * b1 - y2 * a2;
  101. acc = acc + x2 * b2;
  102. acc = acc - y1 * a1;
  103. const auto in = (double) x[i * stride];
  104. const auto y = in * b0 + acc;
  105. x[i * stride] = (float) y;
  106. x2 = x1;
  107. x1 = in;
  108. y2 = y1;
  109. y1 = y;
  110. }
  111. for (auto* v : { &x1, &x2, &y1, &y2 })
  112. if (std::fabs (*v) < (double) tiny)
  113. *v = 0.0;
  114. }
  115. };
  116. // knob law shared by the delay controls: time in units of 10 µs, (1001^x − 1)·50 (0…500 ms)
  117. float delayUnits (double x) noexcept
  118. {
  119. const auto xf = (float) x;
  120. return (float) ((double) (float) (std::exp ((double) xf * std::log (1001.0)) - 1.0) * 50.0);
  121. }
  122. // a 2×2 gain matrix with ramped gains, a delay on one channel and an optional all-pass on one channel; the delay and
  123. // the all-pass run before or after the matrix
  124. struct MatrixCore
  125. {
  126. float maxSlope = 0.001f, maxHz = 22045.59f, rateScale = 0.441f;
  127. double fs = 44100.0;
  128. std::array<Ramp, 4> gains {}; // L←L, R←R, L←R, R←L
  129. std::vector<float> ring;
  130. int ringPos = 0;
  131. bool delayRight = false;
  132. int phaseSide = 0; // 0 off, < 0 left, > 0 right
  133. AllPass ap;
  134. bool post = false;
  135. void prepare (double sampleRate) noexcept
  136. {
  137. const auto rate = (int) std::lround (sampleRate > 0.0 ? sampleRate : 44100.0);
  138. fs = rate;
  139. maxSlope = (float) ((44100.0 / rate) * 0.001);
  140. maxHz = (float) ((double) rate * 0.4999);
  141. rateScale = (float) ((double) rate / 100000.0);
  142. ap.clear();
  143. for (auto& g : gains)
  144. g.current = 0.0f; // the plugins fade the matrix in when they are created
  145. }
  146. void setDelay (float units, bool right)
  147. {
  148. const auto samples = std::max (0, (int) std::nearbyint ((double) (units * rateScale)));
  149. ring.assign ((std::size_t) samples, 0.0f);
  150. ringPos = 0;
  151. delayRight = right;
  152. }
  153. void setPhase (int side, float hz) noexcept
  154. {
  155. phaseSide = side;
  156. if (side == 0)
  157. {
  158. ap.clear();
  159. return;
  160. }
  161. if (maxHz <= hz && hz != maxHz)
  162. hz = maxHz;
  163. const auto w = (double) hz * ((1.0 / fs) * 6.283185307179586);
  164. ap.design (std::sin (w), std::cos (w));
  165. }
  166. void snap() noexcept
  167. {
  168. ap.clear();
  169. for (auto& g : gains)
  170. g.current = g.target;
  171. }
  172. void effects (float* x, int n) noexcept // interleaved
  173. {
  174. if (! ring.empty())
  175. {
  176. auto* c = x + (delayRight ? 1 : 0);
  177. const auto len = (int) ring.size();
  178. for (int i = 0; i < n; ++i)
  179. {
  180. std::swap (c[2 * i], ring[(std::size_t) ringPos]);
  181. if (++ringPos >= len)
  182. ringPos = 0;
  183. }
  184. }
  185. if (phaseSide != 0)
  186. ap.run (x + (phaseSide > 0 ? 1 : 0), n, 2);
  187. }
  188. void matrix (float* x, int n) noexcept
  189. {
  190. float c[4], inc[4];
  191. bool ramping = false;
  192. for (int k = 0; k < 4; ++k)
  193. {
  194. gains[(std::size_t) k].next (n, maxSlope, c[k], inc[k]);
  195. ramping = ramping || inc[k] != 0.0f;
  196. }
  197. if (! ramping && c[2] == 0.0f && c[3] == 0.0f)
  198. {
  199. for (int i = 0; i < n; ++i)
  200. {
  201. x[2 * i] *= c[0];
  202. x[2 * i + 1] *= c[1];
  203. }
  204. return;
  205. }
  206. for (int i = 0; i < n; ++i)
  207. {
  208. const auto l = x[2 * i], r = x[2 * i + 1];
  209. x[2 * i] = l * c[0] + r * c[2];
  210. x[2 * i + 1] = r * c[1] + l * c[3];
  211. if (ramping)
  212. for (int k = 0; k < 4; ++k)
  213. c[k] += inc[k];
  214. }
  215. }
  216. void block (float* x, int n) noexcept
  217. {
  218. if (! post)
  219. effects (x, n);
  220. matrix (x, n);
  221. if (post)
  222. effects (x, n);
  223. }
  224. };
  225. // stereo, in place, split into FL-sized blocks; `difference` outputs input − output
  226. void runMatrix (MatrixCore& core, float* left, float* right, int n, bool difference) noexcept
  227. {
  228. Ftz ftz;
  229. float buf[2 * flBlock], dry[2 * flBlock];
  230. for (int done = 0; done < n;)
  231. {
  232. const auto m = std::min (flBlock, n - done);
  233. for (int i = 0; i < m; ++i)
  234. {
  235. buf[2 * i] = left[done + i];
  236. buf[2 * i + 1] = right[done + i];
  237. }
  238. if (difference)
  239. std::copy (buf, buf + 2 * m, dry);
  240. core.block (buf, m);
  241. for (int i = 0; i < m; ++i)
  242. {
  243. left[done + i] = difference ? dry[2 * i] - buf[2 * i] : buf[2 * i];
  244. right[done + i] = difference ? dry[2 * i + 1] - buf[2 * i + 1] : buf[2 * i + 1];
  245. }
  246. done += m;
  247. }
  248. }
  249. } // namespace
  250. // ------------------------------------------------------------------------------------------- Fruity Stereo Shaper
  251. namespace
  252. {
  253. // knob law of the matrix gains: x = raw / 12800; |g| = (3^|x| − 1) / 2, exactly 1 at |x| = 1, sign of x
  254. float matrixGain (int raw) noexcept
  255. {
  256. const auto x = (float) ((double) raw * 7.8125e-05);
  257. const auto a = std::fabs (x);
  258. float g = a;
  259. if (a != 1.0f)
  260. {
  261. static const auto ln3 = (float) std::log (3.0);
  262. g = (float) ((std::exp ((double) (a * ln3)) - 1.0) * (double) 0.5f);
  263. }
  264. const float s = x > 0.0f ? 1.0f : (x < 0.0f ? -1.0f : 0.0f);
  265. return g * s;
  266. }
  267. float shaperDelayUnits (int raw) noexcept { return delayUnits ((double) std::abs (raw) * 0.000244140625); }
  268. // phase knob: all-pass frequency (5001^(1 − |v|/4096) − 1)·4.398 + 10 Hz (22 kHz … 10 Hz)
  269. float shaperPhaseHz (int raw) noexcept
  270. {
  271. const auto x = (float) (1.0 - std::fabs ((double) raw * 0.000244140625));
  272. return (float) ((double) (float) (std::exp ((double) x * std::log (5001.0)) - 1.0) * 4.398 + 10.0);
  273. }
  274. } // namespace
  275. std::optional<StereoShaperState> parseStereoShaper (const std::vector<std::uint8_t>& d)
  276. {
  277. if (d.size() < 36) // the leading version word (0 in every known state) is read and ignored by FL
  278. return std::nullopt;
  279. StereoShaperState s;
  280. int* f[8] = { &s.rightToLeft, &s.left, &s.right, &s.leftToRight, &s.delay, &s.phase, &s.send, &s.post };
  281. for (int i = 0; i < 8; ++i)
  282. *f[i] = i32At (d, 4 + 4 * (std::size_t) i);
  283. return s;
  284. }
  285. void stereoShaperToParams (const StereoShaperState& s, float* p) noexcept
  286. {
  287. p[0] = toUnit (s.rightToLeft, -25600, 25600);
  288. p[1] = toUnit (s.left, -25600, 25600);
  289. p[2] = toUnit (s.right, -25600, 25600);
  290. p[3] = toUnit (s.leftToRight, -25600, 25600);
  291. p[4] = toUnit (s.delay, -4096, 4096);
  292. p[5] = toUnit (s.phase, -4096, 4096);
  293. p[6] = s.post > 0 ? 1.0f : 0.0f;
  294. p[7] = 0.0f;
  295. }
  296. StereoShaperState stereoShaperFromParams (const float* p) noexcept
  297. {
  298. StereoShaperState s;
  299. s.rightToLeft = fromUnit (p[0], -25600, 25600);
  300. s.left = fromUnit (p[1], -25600, 25600);
  301. s.right = fromUnit (p[2], -25600, 25600);
  302. s.leftToRight = fromUnit (p[3], -25600, 25600);
  303. s.delay = fromUnit (p[4], -4096, 4096);
  304. s.phase = fromUnit (p[5], -4096, 4096);
  305. s.post = p[6] >= 0.5f ? 1 : 0;
  306. return s;
  307. }
  308. std::string stereoShaperParamText (int param, float v)
  309. {
  310. std::array<float, 8> p {};
  311. p.fill (v);
  312. const auto s = stereoShaperFromParams (p.data());
  313. char t[48];
  314. switch (param)
  315. {
  316. case 0: case 1: case 2: case 3:
  317. {
  318. const auto raw = param == 0 ? s.rightToLeft : param == 1 ? s.left : param == 2 ? s.right : s.leftToRight;
  319. std::snprintf (t, sizeof (t), "%d%%", (int) std::lround (matrixGain (raw) * 100.0f));
  320. return t;
  321. }
  322. case 4:
  323. if (s.delay == 0)
  324. return "off";
  325. std::snprintf (t, sizeof (t), "%s %.1f ms", s.delay > 0 ? "R" : "L", shaperDelayUnits (s.delay) / 100.0);
  326. return t;
  327. case 5:
  328. if (s.phase == 0)
  329. return "off";
  330. std::snprintf (t, sizeof (t), "%s %.0f Hz", s.phase > 0 ? "R" : "L", (double) shaperPhaseHz (s.phase));
  331. return t;
  332. case 6: return s.post ? "post" : "pre";
  333. case 7: return v >= 0.5f ? "difference" : "output";
  334. default: return {};
  335. }
  336. }
  337. namespace
  338. {
  339. class StereoShaperEffect final : public Effect
  340. {
  341. public:
  342. void prepare (double sampleRate) noexcept override
  343. {
  344. core.prepare (sampleRate);
  345. if (have)
  346. apply (st, true);
  347. }
  348. void reset() noexcept override { core.snap(); } // FL's Flush: filter state cleared, gains jump to their targets
  349. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  350. {
  351. apply (stereoShaperFromParams (params.data()), ! have);
  352. runMatrix (core, left, right, n, params[7] >= 0.5f);
  353. }
  354. private:
  355. MatrixCore core;
  356. StereoShaperState st;
  357. bool have = false;
  358. void apply (const StereoShaperState& s, bool all)
  359. {
  360. const auto old = st;
  361. st = s;
  362. have = true;
  363. if (all || old.delay != s.delay)
  364. core.setDelay (shaperDelayUnits (s.delay), s.delay > 0);
  365. if (all || old.phase != s.phase)
  366. core.setPhase (s.phase > 0 ? 1 : (s.phase < 0 ? -1 : 0), shaperPhaseHz (s.phase));
  367. core.post = s.post > 0;
  368. core.gains[0].target = matrixGain (s.left);
  369. core.gains[1].target = matrixGain (s.right);
  370. core.gains[2].target = matrixGain (s.rightToLeft);
  371. core.gains[3].target = matrixGain (s.leftToRight);
  372. }
  373. };
  374. } // namespace
  375. std::unique_ptr<Effect> makeStereoShaper() { return std::make_unique<StereoShaperEffect>(); }
  376. // ----------------------------------------------------------------------------------------- Fruity Stereo Enhancer
  377. std::optional<StereoEnhancerState> parseStereoEnhancer (const std::vector<std::uint8_t>& d)
  378. {
  379. if (d.size() < 24)
  380. return std::nullopt;
  381. StereoEnhancerState s;
  382. int* f[6] = { &s.pan, &s.volume, &s.separation, &s.delay, &s.post, &s.invert };
  383. for (int i = 0; i < 6; ++i)
  384. *f[i] = i32At (d, 4 * (std::size_t) i);
  385. return s;
  386. }
  387. void stereoEnhancerToParams (const StereoEnhancerState& s, float* p) noexcept
  388. {
  389. p[0] = toUnit (s.pan, -128, 128);
  390. p[1] = toUnit (s.volume, 0, 320);
  391. p[2] = toUnit (s.separation, -96, 96);
  392. p[3] = toUnit (s.delay, -512, 512);
  393. p[4] = s.post > 0 ? 1.0f : 0.0f;
  394. p[5] = toUnit (s.invert, 0, 2);
  395. }
  396. StereoEnhancerState stereoEnhancerFromParams (const float* p) noexcept
  397. {
  398. StereoEnhancerState s;
  399. s.pan = fromUnit (p[0], -128, 128);
  400. s.volume = fromUnit (p[1], 0, 320);
  401. s.separation = fromUnit (p[2], -96, 96);
  402. s.delay = fromUnit (p[3], -512, 512);
  403. s.post = p[4] >= 0.5f ? 1 : 0;
  404. s.invert = fromUnit (p[5], 0, 2);
  405. return s;
  406. }
  407. namespace
  408. {
  409. // volume knob: x = raw/256, (11^x − 1)/10, exactly 1 at x = 1
  410. float enhancerVolume (int raw) noexcept
  411. {
  412. const auto x = (float) raw * 0.00390625f;
  413. if (x == 1.0f)
  414. return 1.0f;
  415. return (float) ((std::exp ((double) (x * 2.39789534f)) - 1.0) * (double) 0.1f);
  416. }
  417. float enhancerDelayUnits (int raw) noexcept { return delayUnits ((double) std::abs (raw) * 0.001953125); }
  418. std::array<float, 4> enhancerGains (const StereoEnhancerState& s) noexcept
  419. {
  420. const auto pan = (float) s.pan * 0.0078125f;
  421. const auto sep = (float) s.separation * 0.0078125f;
  422. const auto a = enhancerVolume (s.volume) * (1.0f - sep * 0.5f);
  423. std::array<float, 4> c { a, a, a * sep, a * sep }; // L←L, R←R, L←R, R←L
  424. if (pan < 0.0f)
  425. {
  426. const auto f = pan + 1.0f;
  427. c[1] *= f;
  428. c[3] *= f;
  429. }
  430. else
  431. {
  432. const auto f = 1.0f - pan;
  433. c[0] *= f;
  434. c[2] *= f;
  435. }
  436. if (s.invert == 1)
  437. {
  438. c[0] = -c[0];
  439. c[2] = -c[2];
  440. }
  441. else if (s.invert == 2)
  442. {
  443. c[1] = -c[1];
  444. c[3] = -c[3];
  445. }
  446. return c;
  447. }
  448. } // namespace
  449. std::string stereoEnhancerParamText (int param, float v)
  450. {
  451. std::array<float, 6> p {};
  452. p.fill (v);
  453. const auto s = stereoEnhancerFromParams (p.data());
  454. char t[48];
  455. switch (param)
  456. {
  457. case 0: std::snprintf (t, sizeof (t), "%d%% %s", std::abs (s.pan) * 100 / 128, s.pan < 0 ? "L" : (s.pan > 0 ? "R" : "")); return t;
  458. case 1: std::snprintf (t, sizeof (t), "%.1f dB", 20.0 * std::log10 (std::max (1e-9f, enhancerVolume (s.volume)))); return t;
  459. case 2: std::snprintf (t, sizeof (t), "%d%%", s.separation * 100 / 96); return t;
  460. case 3:
  461. if (s.delay == 0)
  462. return "off";
  463. std::snprintf (t, sizeof (t), "%s %.1f ms", s.delay > 0 ? "R" : "L", enhancerDelayUnits (s.delay) / 100.0);
  464. return t;
  465. case 4: return s.post ? "post" : "pre";
  466. case 5: return s.invert == 1 ? "invert L" : (s.invert == 2 ? "invert R" : "none");
  467. default: return {};
  468. }
  469. }
  470. namespace
  471. {
  472. class StereoEnhancerEffect final : public Effect
  473. {
  474. public:
  475. void prepare (double sampleRate) noexcept override
  476. {
  477. core.prepare (sampleRate);
  478. if (have)
  479. apply (st, true);
  480. }
  481. void reset() noexcept override { core.snap(); }
  482. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  483. {
  484. apply (stereoEnhancerFromParams (params.data()), ! have);
  485. runMatrix (core, left, right, n, false);
  486. }
  487. private:
  488. MatrixCore core;
  489. StereoEnhancerState st;
  490. bool have = false;
  491. void apply (const StereoEnhancerState& s, bool all)
  492. {
  493. const auto old = st;
  494. st = s;
  495. have = true;
  496. if (all || old.delay != s.delay)
  497. core.setDelay (enhancerDelayUnits (s.delay), s.delay > 0);
  498. core.post = s.post > 0; // without a delay the order does not matter
  499. const auto c = enhancerGains (s);
  500. for (int k = 0; k < 4; ++k)
  501. core.gains[(std::size_t) k].target = c[(std::size_t) k];
  502. }
  503. };
  504. } // namespace
  505. std::unique_ptr<Effect> makeStereoEnhancer() { return std::make_unique<StereoEnhancerEffect>(); }
  506. // -------------------------------------------------------------------------------------------- Transient Processor
  507. std::optional<TransientProcessorState> parseTransientProcessor (const std::vector<std::uint8_t>& d)
  508. {
  509. if (d.size() < 40 || i32At (d, 0) >= 2) // FL reads nothing from versions ≥ 2
  510. return std::nullopt;
  511. TransientProcessorState s;
  512. int* f[9] = { &s.attack, &s.drive, &s.release, &s.gain, &s.attackShape, &s.releaseShape, &s.split, &s.balance, &s.on };
  513. for (int i = 0; i < 9; ++i)
  514. *f[i] = i32At (d, 4 + 4 * (std::size_t) i);
  515. return s;
  516. }
  517. namespace
  518. {
  519. constexpr int tpMax[9] = { 160, 160, 160, 160, 2, 2, 160, 160, 1 };
  520. }
  521. void transientProcessorToParams (const TransientProcessorState& s, float* p) noexcept
  522. {
  523. const int raw[9] = { s.attack, s.drive, s.release, s.gain, s.attackShape, s.releaseShape, s.split, s.balance, s.on };
  524. for (int i = 0; i < 9; ++i)
  525. p[i] = toUnit (raw[i], 0, tpMax[i]);
  526. }
  527. TransientProcessorState transientProcessorFromParams (const float* p) noexcept
  528. {
  529. TransientProcessorState s;
  530. int* f[9] = { &s.attack, &s.drive, &s.release, &s.gain, &s.attackShape, &s.releaseShape, &s.split, &s.balance, &s.on };
  531. for (int i = 0; i < 9; ++i)
  532. *f[i] = fromUnit (p[i], 0, tpMax[i]);
  533. return s;
  534. }
  535. std::string transientProcessorParamText (int param, float v)
  536. {
  537. if (param < 0 || param > 8)
  538. return {};
  539. const auto raw = fromUnit (v, 0, tpMax[param]);
  540. const auto x = (double) (float) ((double) raw / tpMax[param]);
  541. char t[48];
  542. switch (param)
  543. {
  544. case 0: case 2: std::snprintf (t, sizeof (t), "%.1f%%", x * 200.0 - 100.0); return t;
  545. case 1: std::snprintf (t, sizeof (t), "%.1f %%", x * 100.0); return t;
  546. case 3: std::snprintf (t, sizeof (t), "%.1f dB", (x - 0.5) * 18.0); return t;
  547. case 4: case 5: return raw == 0 ? "Sharp" : (raw == 1 ? "Mid" : "Slow");
  548. case 6: std::snprintf (t, sizeof (t), "%.1f Hz", x * x * 995.0 + 5.0); return t;
  549. case 7:
  550. {
  551. const auto dry = std::min (1.0, 2.0 * (1.0 - x)), wet = std::min (1.0, 2.0 * x);
  552. std::snprintf (t, sizeof (t), "%d%% Dry/%d%% Wet", (int) (dry * 100.0), (int) (wet * 100.0));
  553. return t;
  554. }
  555. case 8: return raw ? "on" : "off";
  556. default: return {};
  557. }
  558. }
  559. namespace
  560. {
  561. float powf_ (double a, double b) noexcept { return (float) std::pow (a, b); }
  562. // the transient engine (REA_rodada54 §4): a one-pole split, a detector driving a gain on the high band, a soft
  563. // saturator, all in single precision; parameters glide every 8 samples
  564. class TransientEngine
  565. {
  566. public:
  567. TransientEngine()
  568. {
  569. detectorCoefficients (44100.0f);
  570. for (int i = 0; i < 8; ++i)
  571. apply (i, 0.5f);
  572. }
  573. // the host's parameter call: smoothed parameters are queued, the shapes apply at once
  574. void setParameter (int i, float x) noexcept
  575. {
  576. if (i == 4 || i == 5)
  577. apply (i, x);
  578. else if (i >= 0 && i < 8)
  579. {
  580. bool found = false;
  581. for (int k = 0; k < queued; ++k)
  582. if (queue[k].index == i)
  583. {
  584. queue[k].target = x;
  585. found = true;
  586. break;
  587. }
  588. if (! found && queued < 30)
  589. queue[queued++] = { i, x };
  590. }
  591. if (i >= 0 && i < 8)
  592. shown[i] = x;
  593. }
  594. void applyAll() noexcept // the wrapper's call after a state load: every value at once
  595. {
  596. for (int i = 0; i < 8; ++i)
  597. apply (i, shown[i]);
  598. }
  599. void setSampleRate (float fs) noexcept
  600. {
  601. k = 1.0f - (float) std::pow (0.1, 200.0 / (double) fs);
  602. detectorCoefficients (fs);
  603. inv = 1.0f / fs;
  604. actual[6] = shown[6];
  605. crossover (shown[6]);
  606. }
  607. void process (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  608. {
  609. for (int i = 0; i < n; ++i)
  610. {
  611. if (++counter == 8)
  612. {
  613. counter = 0;
  614. for (int j = 0; j < queued;)
  615. {
  616. const auto idx = queue[j].index;
  617. const auto target = queue[j].target;
  618. const auto cur = actual[idx];
  619. const auto next = cur + (target - cur) * k;
  620. if (std::fabs (next - target) < 9.999999747378752e-05f)
  621. {
  622. apply (idx, target);
  623. for (int m = j; m + 1 < queued; ++m)
  624. queue[m] = queue[m + 1];
  625. --queued;
  626. }
  627. else
  628. {
  629. apply (idx, next);
  630. ++j;
  631. }
  632. }
  633. }
  634. const auto l = inL[i], r = inR[i];
  635. const auto yR = lp[0] * r + lp[1] * lx1R + lp[2] * ly1R;
  636. const auto yL = lp[0] * l + lp[1] * lx1L + lp[2] * ly1L;
  637. lx1L = l;
  638. lx1R = r;
  639. ly1L = yL;
  640. ly1R = yR;
  641. const auto hR = hp[0] * r + hp[1] * hx1R + hp[2] * hy1R;
  642. const auto hL = hp[1] * hx1L + hp[0] * l + hp[2] * hy1L;
  643. hx1L = l;
  644. hx1R = r;
  645. hy1L = hL;
  646. hy1R = hR;
  647. float pa = 0.0f, pb = 0.0f;
  648. detector (hL, hR, pa, pb);
  649. outL[i] = (g1 * yL + g2 * pa) * gain;
  650. outR[i] = (g1 * yR + g2 * pb) * gain;
  651. }
  652. }
  653. private:
  654. struct Pending
  655. {
  656. int index = 0;
  657. float target = 0.0f;
  658. };
  659. Pending queue[30] {};
  660. int queued = 0, counter = 0;
  661. float shown[8] { 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f };
  662. float actual[8] {};
  663. float k = 1e-05f, inv = 0.0f, fsD = 44100.0f;
  664. float lp[3] {}, hp[3] {};
  665. float lx1L = 0, lx1R = 0, ly1L = 0, ly1R = 0, hx1L = 0, hx1R = 0, hy1L = 0, hy1R = 0;
  666. float gain = 1.0f, g1 = 1.0f, g2 = 1.0f;
  667. // detector parameters (current / target pairs advance once per sample) and coefficients
  668. float drive = 0.01f, driveTarget = 0.1f, attackAmt = 0.0f, attackTarget = 0.0f, releaseAmt = 0.0f, releaseTarget = 0.0f;
  669. float outScale = 1.0f, outScaleTarget = 1.0f, outUnit = 1.0f;
  670. float attackShape = 1.0f, releaseShape = 1.0f;
  671. float decay = 0, envAttack = 0, relBase = 0, relRange = 0, holdDecay = 0, peakDecay = 0;
  672. float pk = 0, env = 0, hold = 0, slow = 0, peak = 0;
  673. void detectorCoefficients (float fs) noexcept
  674. {
  675. fsD = fs;
  676. const auto d = (double) fs;
  677. decay = (float) std::pow (0.1, 5.0 / d);
  678. envAttack = 1.0f - (float) std::pow (0.1, 1.0 / ((double) attackShape * 0.01 * d));
  679. relRange = (float) std::pow (0.1, 1.0 / ((double) releaseShape * 0.02 * d));
  680. relBase = 1.0f - relRange;
  681. holdDecay = (float) std::pow (0.1, 1.0 / ((double) releaseShape * 0.1 * d));
  682. peakDecay = (float) std::pow (0.1, 1.0 / (double) (fs * 4.0f));
  683. }
  684. void crossover (float v) noexcept
  685. {
  686. const auto w = v * v * inv * 3125.884765625f + inv * 15.707963943481445f;
  687. const auto t = (float) std::tan ((double) w);
  688. const auto d = 1.0f / (t + 1.0f);
  689. hp[0] = d;
  690. hp[1] = -d;
  691. hp[2] = -((t - 1.0f) * d);
  692. lp[0] = lp[1] = d * t;
  693. lp[2] = -((t - 1.0f) * d);
  694. }
  695. void apply (int i, float x) noexcept
  696. {
  697. actual[i] = x;
  698. switch (i)
  699. {
  700. case 0: case 2:
  701. {
  702. const auto u = ((x - 0.5f) + x) - 0.5f;
  703. auto q = u * u;
  704. if (! (u >= 0.0f))
  705. q = -q;
  706. if (i == 0)
  707. attackTarget = (q >= 0.0f ? 0.4000000059604645f : 0.6000000238418579f) * q;
  708. else
  709. releaseTarget = q >= 0.0f ? q * 0.6000000238418579f : q;
  710. break;
  711. }
  712. case 1:
  713. driveTarget = x < 0.10000000149011612f ? x * 9.0f + 0.10000000149011612f
  714. : powf_ (15.0, (double) ((x - 0.10000000149011612f) * 1.111111044883728f));
  715. break;
  716. case 3: gain = powf_ (10.0, (double) ((x - 0.5f) * 0.9000000357627869f)); break;
  717. case 4: case 5:
  718. {
  719. const auto shape = (double) x < 0.333 ? 0.5f : ((double) x < 0.666 ? 1.0f : 2.0f);
  720. (i == 4 ? attackShape : releaseShape) = shape;
  721. detectorCoefficients (fsD);
  722. break;
  723. }
  724. case 6: crossover (x); break;
  725. case 7:
  726. g1 = std::min ((1.0f - x) + (1.0f - x), 1.0f);
  727. g2 = std::min (x + x, 1.0f);
  728. break;
  729. default: break;
  730. }
  731. }
  732. static float saturate (float v) noexcept
  733. {
  734. if (v < -3.0f)
  735. return -1.0f;
  736. if (v > 3.0f)
  737. return 1.0f;
  738. return ((v * v + 27.0f) * v) / (v * 9.0f * v + 27.0f);
  739. }
  740. void detector (float a, float b, float& oa, float& ob) noexcept
  741. {
  742. const auto dScale = outUnit * outScaleTarget - outScale;
  743. const auto dAttack = attackTarget - attackAmt;
  744. const auto dRelease = releaseTarget - releaseAmt;
  745. const auto dDrive = driveTarget - drive;
  746. const auto m = (std::fabs (b) + std::fabs (a)) * 0.5f;
  747. pk = m > pk ? m : pk * decay;
  748. env = m > env ? (m - env) * envAttack + env : env * decay;
  749. float r = 1.0f;
  750. if (env > 0.0f)
  751. {
  752. r = pk / env;
  753. if (r >= 1.0f)
  754. {
  755. if (attackAmt >= 0.0f)
  756. r = r > 10.0f ? 6.599999904632568f : r * (0.9600000381469727f - r * 0.003000000026077032f * r);
  757. }
  758. else
  759. r = 1.0f;
  760. }
  761. peak = m >= peak ? m : peak * peakDecay;
  762. hold = m >= hold ? m : hold * holdDecay;
  763. const auto x6 = hold - slow;
  764. float q = 1.0f;
  765. if (releaseAmt >= 0.0f && hold > 0.0f)
  766. {
  767. const auto h = hold + 9.999999747378752e-05f;
  768. q = (h - slow) / h;
  769. }
  770. q = std::min (std::max (q * 3.0f, 0.0f), 1.0f);
  771. slow = (q * relRange + relBase) * x6 + slow;
  772. float t = 1.0f;
  773. if (slow > 9.999999974752427e-07f)
  774. {
  775. t = peak / slow;
  776. if (t >= 1.0f)
  777. {
  778. if (releaseAmt >= 0.0f)
  779. t = t > 20.0f ? 12.0f : t * 0.8399999737739563f - (t * t) * (t * 0.0006000000284984708f);
  780. }
  781. else
  782. t = 1.0f;
  783. }
  784. auto g = (t - 1.0f) * releaseAmt + (r - 1.0f) * attackAmt;
  785. g = g >= 0.0f ? g + 1.0f : 1.0f / (1.0f - g);
  786. const auto ag = a * g, bg = b * g;
  787. oa = saturate (drive * ag) / drive * outScale;
  788. ob = saturate (drive * bg) / drive * outScale;
  789. outScale = dScale + outScale;
  790. attackAmt = dAttack + attackAmt;
  791. releaseAmt = dRelease + releaseAmt;
  792. drive = dDrive + drive;
  793. }
  794. };
  795. class TransientProcessorEffect final : public Effect
  796. {
  797. public:
  798. void prepare (double sampleRate) noexcept override
  799. {
  800. fs = sampleRate > 0.0 ? sampleRate : 44100.0;
  801. tail = (int) std::nearbyint (std::lround (fs) * 1.5) + 88;
  802. fresh = true;
  803. }
  804. void reset() noexcept override {} // FL's Flush showed no effect on this plugin (REA_rodada54 §4.6)
  805. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  806. {
  807. const auto s = transientProcessorFromParams (params.data());
  808. if (fresh)
  809. start (s);
  810. else
  811. update (s);
  812. Ftz ftz;
  813. for (int done = 0; done < n;)
  814. {
  815. const auto m = std::min (flBlock, n - done);
  816. block (left + done, right + done, m);
  817. done += m;
  818. }
  819. }
  820. private:
  821. double fs = 44100.0;
  822. int tail = 66238, quiet = 0;
  823. bool fresh = true;
  824. TransientProcessorState st;
  825. std::unique_ptr<TransientEngine> engine;
  826. float outL[flBlock] {}, outR[flBlock] {};
  827. static float unit (int raw, int i) noexcept
  828. {
  829. const auto v = std::clamp ((double) raw * (1.0 / tpMax[i]), 0.0, 1.0);
  830. return (float) v;
  831. }
  832. void start (const TransientProcessorState& s)
  833. {
  834. engine = std::make_unique<TransientEngine>();
  835. const int raw[9] = { s.attack, s.drive, s.release, s.gain, s.attackShape, s.releaseShape, s.split, s.balance, s.on };
  836. for (int i = 0; i < 8; ++i)
  837. engine->setParameter (i, unit (raw[i], i));
  838. engine->applyAll();
  839. engine->setSampleRate ((float) std::lround (fs));
  840. st = s;
  841. quiet = 0;
  842. fresh = false;
  843. }
  844. void update (const TransientProcessorState& s) noexcept
  845. {
  846. const int now[9] = { s.attack, s.drive, s.release, s.gain, s.attackShape, s.releaseShape, s.split, s.balance, s.on };
  847. const int was[9] = { st.attack, st.drive, st.release, st.gain, st.attackShape, st.releaseShape, st.split, st.balance, st.on };
  848. for (int i = 0; i < 8; ++i)
  849. if (now[i] != was[i])
  850. engine->setParameter (i, unit (now[i], i));
  851. st = s;
  852. }
  853. void block (float* left, float* right, int n) noexcept
  854. {
  855. // after 1.5 s (+88 samples) of input within ±10⁻⁶ the plugin passes its input and the engine stops
  856. bool silent = true;
  857. for (int i = 0; i < n && silent; ++i)
  858. silent = std::fabs (left[i]) <= 9.999999974752427e-07f && std::fabs (right[i]) <= 9.999999974752427e-07f;
  859. quiet = silent ? quiet - n : tail;
  860. if (! (-n < quiet))
  861. {
  862. quiet = -0x3fffffff;
  863. return;
  864. }
  865. if (st.on == 0)
  866. return;
  867. engine->process (left, right, outL, outR, n);
  868. std::copy (outL, outL + n, left);
  869. std::copy (outR, outR + n, right);
  870. }
  871. };
  872. } // namespace
  873. std::unique_ptr<Effect> makeTransientProcessor() { return std::make_unique<TransientProcessorEffect>(); }
  874. // ------------------------------------------------------------------------------------------------------- Spreader
  875. std::optional<SpreaderState> parseSpreader (const std::vector<std::uint8_t>& d)
  876. {
  877. if (d.size() < 24)
  878. return std::nullopt;
  879. const auto version = (std::uint32_t) i32At (d, 0);
  880. if (version != 0x10002 && version != 0x10003) // older layouts occur in no installation state
  881. return std::nullopt;
  882. SpreaderState s;
  883. int* f[5] = { &s.width, &s.separation, &s.lowBypass, &s.monoBass, &s.enable };
  884. for (int i = 0; i < 5; ++i)
  885. *f[i] = i32At (d, 4 + 4 * (std::size_t) i);
  886. return s;
  887. }
  888. namespace
  889. {
  890. constexpr int spLo[5] = { 0, -10000, 1000, 0, 0 }, spHi[5] = { 10000, 10000, 1000000, 1, 1 };
  891. // FL's normalised control value (double, clamped), as the plugin hands it on
  892. double spreaderUnit (int raw, int i) noexcept
  893. {
  894. return std::clamp ((double) (raw - spLo[i]) * (1.0 / (spHi[i] - spLo[i])), 0.0, 1.0);
  895. }
  896. // low bypass control → Hz: 0…500 Hz over the first half, 500 Hz…10 kHz over the second, at least 10 Hz
  897. float spreaderLowHz (float unit) noexcept
  898. {
  899. auto x = (double) unit;
  900. double lo = 0.0, hi = 1000.0;
  901. if (! (0.5 >= x))
  902. {
  903. x = (x - 0.5) * 2.0;
  904. lo = 500.0;
  905. hi = 10000.0;
  906. }
  907. const auto v = x > 0.0 ? std::exp (std::log (x)) : 0.0;
  908. const auto hz = (float) (lo + (hi - lo) * v);
  909. return std::isnan (hz) || hz < 10.0f ? 10.0f : hz;
  910. }
  911. } // namespace
  912. void spreaderToParams (const SpreaderState& s, float* p) noexcept
  913. {
  914. const int raw[5] = { s.width, s.separation, s.lowBypass, s.monoBass, s.enable };
  915. for (int i = 0; i < 5; ++i)
  916. p[i] = toUnit (raw[i], spLo[i], spHi[i]);
  917. }
  918. SpreaderState spreaderFromParams (const float* p) noexcept
  919. {
  920. SpreaderState s;
  921. int* f[5] = { &s.width, &s.separation, &s.lowBypass, &s.monoBass, &s.enable };
  922. for (int i = 0; i < 5; ++i)
  923. *f[i] = fromUnit (p[i], spLo[i], spHi[i]);
  924. return s;
  925. }
  926. std::string spreaderParamText (int param, float v)
  927. {
  928. if (param < 0 || param > 4)
  929. return {};
  930. const auto raw = fromUnit (v, spLo[param], spHi[param]);
  931. char t[48];
  932. switch (param)
  933. {
  934. case 0: std::snprintf (t, sizeof (t), "%.0f%%", raw / 100.0); return t;
  935. case 1: std::snprintf (t, sizeof (t), "%.0f%%", raw / 100.0); return t;
  936. case 2: std::snprintf (t, sizeof (t), "%.0f Hz", (double) spreaderLowHz ((float) spreaderUnit (raw, 2))); return t;
  937. default: return raw ? "on" : "off";
  938. }
  939. }
  940. namespace
  941. {
  942. // the Spreader engine (REA_rodada54 §5): a Faust-built single-precision stereo widener with smoothed controls
  943. class SpreaderEngine
  944. {
  945. public:
  946. float width = 1.0f, separation = 0.5f, lowBypass = 10.0f, monoBass = 0.0f, enable = 0.0f; // the engine's controls
  947. void constants (int sampleRate) noexcept
  948. {
  949. const auto r = (float) sampleRate;
  950. const auto c0 = std::min (r > 1.0f ? r : 1.0f, 192000.0f);
  951. const auto c1 = 1.0f / c0;
  952. k = (float) std::exp ((double) (c1 * -500.0f));
  953. k1 = 1.0f - k;
  954. wc = c1 * 3.1415927410125732f;
  955. maxDelay = (int) (c0 * 0.0063492064364254475f);
  956. maxDelayF = (float) maxDelay;
  957. fadeStep = 1.0f / (float) (int) (c0 * 0.02321995422244072f);
  958. const auto t = (float) std::tan ((double) (c1 * 47123.890625f));
  959. const auto f = 1.0f / t;
  960. cb4 = 1.0f - f;
  961. cc4 = f * 0.5f;
  962. cb8 = 1.0f / (f + 1.0f);
  963. }
  964. void clear() noexcept
  965. {
  966. lb = en = w = fade = fadeStepNow = 0.0f;
  967. std::fill (std::begin (s), std::end (s), 0.0f);
  968. b0 = bc = c8 = d0 = d8 = e0 = sepNow = mbNow = 0.0f;
  969. idx0 = idx1 = 0;
  970. iota = 0;
  971. std::fill (std::begin (lineA), std::end (lineA), 0.0f);
  972. std::fill (std::begin (lineB), std::end (lineB), 0.0f);
  973. }
  974. void compute (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  975. {
  976. constexpr float sq2 = 1.4142135381698608f;
  977. constexpr float c796 = 0.7962143421173096f, c398 = 0.3981071710586548f;
  978. const auto aLb = k1 * lowBypass, aEn = k1 * enable, aW = k1 * width, aSep = k1 * separation, aMb = k1 * monoBass;
  979. for (int i = 0; i < n; ++i)
  980. {
  981. lb = lb * k + aLb;
  982. const auto t = (float) std::tan ((double) (lb * wc));
  983. const auto t2 = t + sq2;
  984. const auto g = 1.0f / (t2 * t + 1.0f);
  985. en = en * k + aEn;
  986. const auto xL = inL[i], xR = inR[i];
  987. float hpL, lowL, hpR, lowR;
  988. chain (en * xL, t, t2, g, s, hpL, lowL);
  989. chain (en * xR, t, t2, g, s + 4, hpR, lowR);
  990. const auto io = iota & 2047;
  991. lineA[io] = (lowR + hpL) - (hpR + lowL);
  992. w = w * k + aW;
  993. const auto iv = (int) (w * maxDelayF);
  994. float x = 0.0f;
  995. if (fadeStepNow != 0.0f)
  996. x = (fade > 0.0f && fade < 1.0f) ? fadeStepNow : 0.0f;
  997. else if (fade == 0.0f)
  998. x = iv != idx0 ? fadeStep : 0.0f;
  999. else if (fade == 1.0f)
  1000. x = iv != idx1 ? -fadeStep : 0.0f;
  1001. fadeStepNow = x;
  1002. const auto y = fade + x;
  1003. const auto f = std::max (y >= 1.0f ? 1.0f : y, 0.0f);
  1004. const auto n0 = (fade >= 1.0f && idx1 != iv) ? iv : idx0;
  1005. const auto n1 = (fade <= 0.0f && idx0 != iv) ? iv : idx1;
  1006. fade = f;
  1007. const auto d1 = std::min (n0 > 0 ? n0 : 0, maxDelay), d2 = std::min (n1 > 0 ? n1 : 0, maxDelay);
  1008. const auto a = lineA[(iota - d1) & 2047], b = lineA[(iota - d2) & 2047];
  1009. const auto x10 = ((a - b) * f - a) * w;
  1010. const auto nbc = -(((b0 + x10) * 0.5f + cb4 * bc) * cb8);
  1011. const auto nc8 = -(((x10 - b0) * cc4 + cb4 * c8) * cb8);
  1012. const auto hps = hpR + hpL, lows = lowR + lowL;
  1013. const auto hx = nc8 * c796 + hps;
  1014. lineB[io] = hps - lows;
  1015. const auto c = lineB[(iota - d1) & 2047], d = lineB[(iota - d2) & 2047];
  1016. const auto nd0 = (c - (c - d) * f) * w;
  1017. const auto mb = k * mbNow + aMb;
  1018. const auto sep = k * sepNow + aSep;
  1019. const auto ne0 = ((nd0 - d0) * cc4 - cb4 * e0) * cb8;
  1020. const auto nd8 = ((d0 + nd0) * 0.5f - cb4 * d8) * cb8;
  1021. const auto mid = (((mb * lows + nbc) + (hx + nbc)) - lows) * 0.5f;
  1022. const auto side0 = (lows - ((nbc - (hpL - lowL)) * 2.0f + hx)) * 0.5f;
  1023. const auto side = ((side0 + (ne0 + nc8) * c398) + (nd8 + nbc)) * (sep + sep);
  1024. const auto dry = 1.0f - en, mono = 1.0f - mb;
  1025. outL[i] = (dry * xL + mid) + (mono * lowL + side);
  1026. outR[i] = ((mono * lowR + dry * xR) + mid) - side;
  1027. b0 = x10;
  1028. bc = nbc;
  1029. c8 = nc8;
  1030. d0 = nd0;
  1031. d8 = nd8;
  1032. e0 = ne0;
  1033. mbNow = mb;
  1034. sepNow = sep;
  1035. idx0 = n0;
  1036. idx1 = n1;
  1037. ++iota;
  1038. }
  1039. }
  1040. private:
  1041. float k = 0, k1 = 0, wc = 0, maxDelayF = 0, fadeStep = 0, cb4 = 0, cc4 = 0, cb8 = 0;
  1042. int maxDelay = 0;
  1043. float lb = 0, en = 0, w = 0, fade = 0, fadeStepNow = 0;
  1044. float s[8] {}; // two cascaded 2-pole sections per channel
  1045. float b0 = 0, bc = 0, c8 = 0, d0 = 0, d8 = 0, e0 = 0, sepNow = 0, mbNow = 0;
  1046. int idx0 = 0, idx1 = 0;
  1047. unsigned iota = 0;
  1048. float lineA[2048] {}, lineB[2048] {};
  1049. static void chain (float e, float t, float t2, float g, float* st, float& hp, float& low) noexcept
  1050. {
  1051. const auto v1 = ((e - (st[0] * t2 + st[1])) * t) * g;
  1052. const auto na = (v1 + v1) + st[0];
  1053. const auto u1 = st[0] + v1;
  1054. hp = e - u1 * sq8c;
  1055. const auto w1 = u1 * t;
  1056. const auto nb = (w1 + w1) + st[1];
  1057. const auto bb = w1 + st[1];
  1058. const auto v2 = ((bb - (st[2] * t2 + st[3])) * t) * g;
  1059. const auto nc = (v2 + v2) + st[2];
  1060. const auto u2 = st[2] + v2;
  1061. const auto w2 = u2 * t;
  1062. const auto nd = (w2 + w2) + st[3];
  1063. low = st[3] + w2;
  1064. st[0] = na;
  1065. st[1] = nb;
  1066. st[2] = nc;
  1067. st[3] = nd;
  1068. }
  1069. static constexpr float sq8c = 2.8284270763397217f;
  1070. };
  1071. class SpreaderEffect final : public Effect
  1072. {
  1073. public:
  1074. void prepare (double sampleRate) noexcept override
  1075. {
  1076. rate = (int) std::lround (sampleRate > 0.0 ? sampleRate : 44100.0);
  1077. engine.constants (rate);
  1078. engine.clear(); // the plugin clears its engine when the sample rate is set
  1079. }
  1080. void reset() noexcept override { engine.clear(); } // FL's Flush clears the engine too
  1081. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  1082. {
  1083. const auto s = spreaderFromParams (params.data());
  1084. engine.width = (float) spreaderUnit (s.width, 0);
  1085. engine.separation = (float) spreaderUnit (s.separation, 1);
  1086. engine.lowBypass = spreaderLowHz ((float) spreaderUnit (s.lowBypass, 2));
  1087. engine.monoBass = (float) s.monoBass;
  1088. engine.enable = (float) s.enable;
  1089. Ftz ftz;
  1090. for (int done = 0; done < n;)
  1091. {
  1092. const auto m = std::min (flBlock, n - done);
  1093. engine.compute (left + done, right + done, outL, outR, m);
  1094. // FL's engine diverges where its fixed 15 kHz section passes Nyquist (22.05 kHz): keep NaN/inf out of
  1095. // the mixer (REA_rodada54 §5.2)
  1096. for (int i = 0; i < m; ++i)
  1097. {
  1098. left[done + i] = std::isfinite (outL[i]) ? outL[i] : 0.0f;
  1099. right[done + i] = std::isfinite (outR[i]) ? outR[i] : 0.0f;
  1100. }
  1101. done += m;
  1102. }
  1103. }
  1104. private:
  1105. int rate = 44100;
  1106. SpreaderEngine engine;
  1107. float outL[flBlock] {}, outR[flBlock] {};
  1108. };
  1109. } // namespace
  1110. std::unique_ptr<Effect> makeSpreader() { return std::make_unique<SpreaderEffect>(); }
  1111. } // namespace lynx::stereofx