ChorusFx.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. #include "core/ChorusFx.h"
  2. // FL's chorus family, exact engines (docs/analise/REA_rodada59.md). Clean-room: written from the behaviour, formulas
  3. // 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), in the SSE rounding and denormal modes each engine runs in.
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <functional>
  12. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) // x86 SSE, or ARM64 through core/Simd.h (sse2neon)
  13. #include "core/Simd.h"
  14. #define LYNX_CHORUS_MXCSR 1
  15. #if (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__))
  16. #include <cpuid.h>
  17. #include "core/Simd.h"
  18. #endif
  19. #endif
  20. #if defined(__clang__)
  21. #pragma clang fp contract(off)
  22. #pragma STDC FENV_ACCESS ON
  23. #elif defined(__GNUC__)
  24. #pragma GCC optimize("fp-contract=off", "rounding-math")
  25. #endif
  26. namespace lynx
  27. {
  28. namespace
  29. {
  30. // SSE mode for the duration of a render: `bits` are or-ed into MXCSR (0x8040 = flush-to-zero + denormals-are-zero,
  31. // 0xE040 = the same with rounding toward zero)
  32. struct CsrMode
  33. {
  34. #ifdef LYNX_CHORUS_MXCSR
  35. unsigned saved;
  36. explicit CsrMode (unsigned bits) noexcept : saved (_mm_getcsr()) { _mm_setcsr ((saved & ~0x6000u) | bits); }
  37. ~CsrMode() { _mm_setcsr (saved); }
  38. #else
  39. explicit CsrMode (unsigned) noexcept {}
  40. #endif
  41. CsrMode (const CsrMode&) = delete;
  42. CsrMode& operator= (const CsrMode&) = delete;
  43. };
  44. inline std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t o) noexcept
  45. {
  46. std::int32_t v;
  47. std::memcpy (&v, d.data() + o, 4);
  48. return v;
  49. }
  50. inline float norm (int raw, int lo, int hi) noexcept
  51. {
  52. return std::clamp ((float) (raw - lo) / (float) (hi - lo), 0.0f, 1.0f);
  53. }
  54. inline int rawFrom (float v01, int lo, int hi) noexcept
  55. {
  56. return lo + (int) std::lround (std::clamp ((double) v01, 0.0, 1.0) * (hi - lo));
  57. }
  58. // FL's control value: (raw − min) · (1 / (max − min)) in double, clamped to 0…1, as float
  59. inline float flValue (int raw, int lo, int hi) noexcept
  60. {
  61. const double v = (double) (raw - lo) * (1.0 / (double) (hi - lo));
  62. return (float) std::clamp (v, 0.0, 1.0);
  63. }
  64. inline int truncI (double v) noexcept { return (int) (long long) v; }
  65. // float square root as one SSE instruction, rounded in the current mode (a library sqrtf may not be)
  66. inline float sqrtF (float v) noexcept
  67. {
  68. #ifdef LYNX_CHORUS_MXCSR
  69. return _mm_cvtss_f32 (_mm_sqrt_ss (_mm_set_ss (v)));
  70. #else
  71. return std::sqrt (v);
  72. #endif
  73. }
  74. } // namespace
  75. // ================================================================================================= Fruity Chorus (§1)
  76. namespace fruitychorusfx
  77. {
  78. namespace
  79. {
  80. constexpr int lineSize = 4096, lineMask = lineSize - 1;
  81. // the four LFO tables (§1.3): parabola, its cube, the three-partial sine, its cube; 0…32766
  82. struct Tables
  83. {
  84. std::array<std::array<std::int32_t, 4096>, 4> t {};
  85. Tables() noexcept
  86. {
  87. auto value = [] (float v) { return truncI ((double) ((v + 1.0f) * 16383.0f)); };
  88. const double norm3 = 3.0 / (std::sqrt (2.0) * 2.0 + 1.0);
  89. for (int i = 0; i < 4096; ++i)
  90. {
  91. const float f = (float) ((double) i / 2048.0);
  92. const float p = 1.0 > (double) f ? (f * 4.0f) * (1.0f - f) : ((f - 1.0f) * -4.0f) * (2.0f - f);
  93. t[0][(std::size_t) i] = value (p);
  94. t[1][(std::size_t) i] = value ((p * p) * p);
  95. const double fd = f;
  96. const double s = std::sin (fd * 3.1415) + (std::sin (fd * 9.4245) + std::sin (fd * 6.283)) * 0.3333333333333333;
  97. const float sf = (float) (s * norm3);
  98. t[2][(std::size_t) i] = value (sf);
  99. t[3][(std::size_t) i] = value ((sf * sf) * sf);
  100. }
  101. }
  102. };
  103. const Tables& tables()
  104. {
  105. static const Tables t;
  106. return t;
  107. }
  108. } // namespace
  109. struct Engine::Impl
  110. {
  111. const Tables& tab = tables();
  112. State raw = defaults;
  113. std::array<float, params> x {};
  114. int fs = 44100;
  115. std::uint32_t phase[3] {}, inc[3] {};
  116. int wave[3] {};
  117. int stereo = 0, delay = 0, depth = 0;
  118. bool lpLine = false, wetOnly = false;
  119. float a = 0.0f, b = 0.0f;
  120. float st[2] {}, prev[2] {};
  121. std::array<float, lineSize> line[2] {};
  122. int w = 0;
  123. void cutoff() noexcept
  124. {
  125. const double note = 0.0 + (127.0 - 0.0) * (double) x[10];
  126. double f = (double) (float) (std::pow (2.0, (note - 69.0) / 12.0) * 440.0);
  127. if ((double) fs / 4.1 < f)
  128. f = (double) fs / 4.1;
  129. const double om = (f / (double) fs) * 6.283184;
  130. a = (float) ((1.0 - std::sin (om)) / std::cos (om));
  131. b = (float) ((double) (1.0f - a) * 0.5);
  132. }
  133. void set (int k, int r) noexcept
  134. {
  135. raw[(std::size_t) k] = r;
  136. const float v = flValue (r, 0, rawMax[(std::size_t) k]);
  137. x[(std::size_t) k] = v;
  138. switch (k)
  139. {
  140. case 0: delay = truncI (((double) v * 29.95 + 0.05) * (double) fs * 4.096); break;
  141. case 1: depth = truncI ((double) ((v * 5.0f) * (float) fs) * 4.096 / 64.0); break;
  142. case 2: stereo = truncI ((double) (v * 4096.0f) * 0.5) - 1; break;
  143. case 3:
  144. case 4:
  145. case 5: inc[k - 3] = (std::uint32_t) truncI ((double) (((v * v) * 83886080.0f) / (float) fs)); break;
  146. case 6:
  147. case 7:
  148. case 8: wave[k - 6] = std::clamp (truncI ((double) (v * 3.0f)), 0, 3); break;
  149. case 9: lpLine = 0.25 < (double) v; break;
  150. case 10: cutoff(); break;
  151. case 11: wetOnly = 0.25 < (double) v; break;
  152. default: break;
  153. }
  154. }
  155. void reset (std::uint32_t p1, std::uint32_t p2, std::uint32_t p3) noexcept
  156. {
  157. st[0] = st[1] = prev[0] = prev[1] = 0.0f;
  158. line[0].fill (0.0f);
  159. line[1].fill (0.0f);
  160. w = 0;
  161. phase[0] = p1, phase[1] = p2, phase[2] = p3;
  162. }
  163. void render (const float* in, float* out, int n) noexcept
  164. {
  165. CsrMode mode (0x8040u);
  166. for (int i = 0; i < n; ++i)
  167. {
  168. const float xin[2] { (float) ((double) in[2 * i] * 0.7), (float) ((double) in[2 * i + 1] * 0.7) };
  169. std::uint32_t off[3][2];
  170. for (int v = 0; v < 3; ++v)
  171. {
  172. const auto& t = tab.t[(std::size_t) wave[v]];
  173. const std::uint32_t j = (phase[v] >> 12) & 0xfffu;
  174. const auto lL = (std::uint32_t) t[j], lR = (std::uint32_t) t[(j + (std::uint32_t) stereo) & 0xfffu];
  175. off[v][0] = (lL * (std::uint32_t) depth) >> 9;
  176. off[v][1] = (lR * (std::uint32_t) depth) >> 9;
  177. }
  178. for (int v = 0; v < 3; ++v)
  179. phase[v] += inc[v];
  180. for (int c = 0; c < 2; ++c)
  181. {
  182. st[c] = b * (xin[c] + prev[c]) + a * st[c];
  183. prev[c] = xin[c];
  184. line[c][(std::size_t) w] = lpLine ? st[c] : xin[c] - st[c];
  185. }
  186. const auto base = (std::uint32_t) (w * 4096 - delay);
  187. float sum[2];
  188. for (int c = 0; c < 2; ++c)
  189. {
  190. const auto& L = line[c];
  191. float acc = 0.0f;
  192. for (int v = 0; v < 3; ++v)
  193. {
  194. const std::uint32_t u = base - off[v][c], j = (u >> 12) & 0xfffu, fr = u & 0xfffu;
  195. const float dlt = L[(j + 1) & 0xfffu] - L[j];
  196. const auto val = (float) ((double) L[j] + ((double) dlt * (double) fr) * 0.000244140625);
  197. acc = v == 0 ? val : acc + val;
  198. }
  199. sum[c] = acc;
  200. }
  201. w = (w + 1) & lineMask;
  202. for (int c = 0; c < 2; ++c)
  203. {
  204. const auto o = (float) ((double) sum[c] * 0.6);
  205. out[2 * i + c] = wetOnly ? o : o + xin[c];
  206. }
  207. }
  208. for (auto& s : st)
  209. if (std::fabs ((double) s) < 1e-11)
  210. s = 0.0f;
  211. }
  212. };
  213. Engine::Engine() : d (std::make_unique<Impl>())
  214. {
  215. load (defaults);
  216. d->reset (0, 0, 0);
  217. }
  218. Engine::~Engine() = default;
  219. void Engine::setRate (int sampleRate) noexcept { d->fs = std::max (1, sampleRate); }
  220. void Engine::setParam (int index, int raw) noexcept
  221. {
  222. if (index >= 0 && index < params)
  223. d->set (index, raw);
  224. }
  225. void Engine::load (const State& s) noexcept
  226. {
  227. for (int i = 0; i < params; ++i)
  228. d->set (i, s[(std::size_t) i]);
  229. }
  230. void Engine::reset (std::uint32_t p1, std::uint32_t p2, std::uint32_t p3) noexcept { d->reset (p1, p2, p3); }
  231. int Engine::param (int index) const noexcept { return d->raw[(std::size_t) index]; }
  232. void Engine::render (const float* in, float* out, int n) noexcept { d->render (in, out, n); }
  233. std::optional<State> parse (const std::vector<std::uint8_t>& state)
  234. {
  235. if (state.size() < 4 + 4 * params || i32At (state, 0) > 1)
  236. return std::nullopt;
  237. State s {};
  238. for (int i = 0; i < params; ++i)
  239. s[(std::size_t) i] = i32At (state, 4 + 4 * (std::size_t) i);
  240. return s;
  241. }
  242. void toParams (const State& s, float* p) noexcept
  243. {
  244. for (int i = 0; i < params; ++i)
  245. p[i] = norm (s[(std::size_t) i], 0, rawMax[(std::size_t) i]);
  246. }
  247. State fromParams (const float* p) noexcept
  248. {
  249. State s {};
  250. for (int i = 0; i < params; ++i)
  251. s[(std::size_t) i] = rawFrom (p[i], 0, rawMax[(std::size_t) i]);
  252. return s;
  253. }
  254. const char* paramName (int param) noexcept
  255. {
  256. static const char* names[params] { "Delay", "Depth", "Stereo", "LFO 1 freq", "LFO 2 freq", "LFO 3 freq",
  257. "LFO 1 wave", "LFO 2 wave", "LFO 3 wave", "Cross type", "Cross cutoff", "Wet only" };
  258. return param >= 0 && param < params ? names[param] : "";
  259. }
  260. std::string paramText (int param, float value01)
  261. {
  262. char text[32];
  263. const int p = std::clamp (param, 0, params - 1);
  264. const int raw = rawFrom (value01, 0, rawMax[(std::size_t) p]);
  265. const double x = (double) raw / rawMax[(std::size_t) p];
  266. switch (p)
  267. {
  268. case 0: std::snprintf (text, sizeof (text), "%.2f ms", x * 29.95 + 0.05); return text;
  269. case 1: std::snprintf (text, sizeof (text), "%.2f ms", x * 5.0 * 4.096 / 64.0 * 32766.0 / 512.0); return text;
  270. case 3:
  271. case 4:
  272. case 5: std::snprintf (text, sizeof (text), "%.3f Hz", x * x * 5.0); return text;
  273. case 6:
  274. case 7:
  275. case 8:
  276. {
  277. static const char* waves[4] { "parabola", "sharp", "sine", "sharp sine" };
  278. return waves[std::clamp ((int) (x * 3.0), 0, 3)];
  279. }
  280. case 9: return x > 0.25 ? "low" : "high";
  281. case 10: std::snprintf (text, sizeof (text), "%.0f Hz", 440.0 * std::pow (2.0, (127.0 * x - 69.0) / 12.0)); return text;
  282. case 11: return x > 0.25 ? "on" : "off";
  283. default: std::snprintf (text, sizeof (text), "%d%%", (int) std::lround (100.0 * x)); return text;
  284. }
  285. }
  286. namespace
  287. {
  288. class FruityChorusEffect final : public Effect
  289. {
  290. public:
  291. void prepare (double sampleRate) noexcept override
  292. {
  293. engine.setRate ((int) std::lround (sampleRate));
  294. engine.load (engine.raw()); // the rate-dependent values for this rate (as if FL had set it before the state)
  295. }
  296. // LYNX's reset = FL's "enabled" notification; the start phases FL draws at random are taken as 0
  297. void reset() noexcept override { engine.reset(); }
  298. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  299. {
  300. const auto s = fromParams (params.data());
  301. for (int i = 0; i < fruitychorusfx::params; ++i)
  302. if (s[(std::size_t) i] != engine.param (i))
  303. engine.setParam (i, s[(std::size_t) i]);
  304. for (int o = 0; o < n; o += 512)
  305. {
  306. const int m = std::min (512, n - o);
  307. for (int i = 0; i < m; ++i)
  308. {
  309. buf[2 * (std::size_t) i] = left[o + i];
  310. buf[2 * (std::size_t) i + 1] = right[o + i];
  311. }
  312. engine.render (buf.data(), buf.data(), m);
  313. for (int i = 0; i < m; ++i)
  314. {
  315. left[o + i] = buf[2 * (std::size_t) i];
  316. right[o + i] = buf[2 * (std::size_t) i + 1];
  317. }
  318. }
  319. }
  320. private:
  321. struct Wrapped : Engine
  322. {
  323. State raw() const noexcept
  324. {
  325. State s {};
  326. for (int i = 0; i < params; ++i)
  327. s[(std::size_t) i] = param (i);
  328. return s;
  329. }
  330. } engine;
  331. std::array<float, 1024> buf {};
  332. };
  333. } // namespace
  334. std::unique_ptr<Effect> make() { return std::make_unique<FruityChorusEffect>(); }
  335. const EffectInfo& info()
  336. {
  337. static const EffectInfo i = [] {
  338. EffectInfo e { "Fruity Chorus", {} };
  339. for (int k = 0; k < params; ++k)
  340. e.params.push_back ({ paramName (k), norm (defaults[(std::size_t) k], 0, rawMax[(std::size_t) k]) });
  341. return e;
  342. }();
  343. return i;
  344. }
  345. } // namespace fruitychorusfx
  346. // ================================================================================================== Hyper Chorus (§2)
  347. namespace hyperchorusfx
  348. {
  349. namespace
  350. {
  351. inline float bitsF (std::uint32_t u) noexcept
  352. {
  353. float f;
  354. std::memcpy (&f, &u, 4);
  355. return f;
  356. }
  357. // The engine's math library (§2.5), evaluated like it in the current SSE rounding mode (the engine's parameter
  358. // changes run inside its render, in "toward zero"): the vector sine (reduction by π in four parts, odd polynomial of
  359. // degree 15; the cosine is the sine of |x| + π/2) and powf for base 2 (ln 2 · y, then 2^(N/64) table and a cubic).
  360. inline std::uint64_t bitsOf (double v) noexcept
  361. {
  362. std::uint64_t u;
  363. std::memcpy (&u, &v, 8);
  364. return u;
  365. }
  366. inline double fromBits (std::uint64_t u) noexcept
  367. {
  368. double v;
  369. std::memcpy (&v, &u, 8);
  370. return v;
  371. }
  372. double crtSin (double x) noexcept
  373. {
  374. const double ax = std::fabs (x);
  375. double k = ax * 0.3183098861837907;
  376. k = k + 6755399441055744.0;
  377. const std::uint64_t parity = bitsOf (k) << 63;
  378. const double n = k - 6755399441055744.0;
  379. const double p1 = n * 3.141592502593994, p2 = n * 1.5099578831723193e-07, p3 = n * 1.078060505991553e-14,
  380. p4 = n * 6.564007085747001e-22;
  381. double r = ax - p1;
  382. r = r - p2;
  383. r = r - p3;
  384. r = r - p4;
  385. const double r2 = r * r;
  386. r = fromBits (bitsOf (r) ^ parity);
  387. double q = -7.373344533110665e-13 * r2 + 1.6048095404959637e-10;
  388. q = q * r2 + -2.505188027386288e-08;
  389. q = q * r2 + 2.7557316600552628e-06;
  390. q = q * r2 + -0.00019841269825056256;
  391. q = q * r2 + 0.008333333333285542;
  392. q = q * r2 + -0.16666666666666186;
  393. const double t = (r2 * q) * r;
  394. return fromBits (bitsOf (r + t) ^ (bitsOf (x) & 0x8000000000000000ull));
  395. }
  396. void crtSinCos (double x, double& s, double& c) noexcept
  397. {
  398. const double ax = std::fabs (x);
  399. if (ax < 7.450580596923828e-09)
  400. {
  401. s = x;
  402. c = 1.0 - (ax * ax) * 0.5;
  403. return;
  404. }
  405. s = crtSin (x);
  406. c = crtSin (ax + 1.5707963267948966);
  407. }
  408. constexpr double exp2Table[64] {
  409. 0x1.0000000000000p+0, 0x1.02c9a3e778061p+0, 0x1.059b0d3158574p+0, 0x1.0874518759bc8p+0,
  410. 0x1.0b5586cf9890fp+0, 0x1.0e3ec32d3d1a2p+0, 0x1.11301d0125b51p+0, 0x1.1429aaea92de0p+0,
  411. 0x1.172b83c7d517bp+0, 0x1.1a35beb6fcb75p+0, 0x1.1d4873168b9aap+0, 0x1.2063b88628cd6p+0,
  412. 0x1.2387a6e756238p+0, 0x1.26b4565e27cddp+0, 0x1.29e9df51fdee1p+0, 0x1.2d285a6e4030bp+0,
  413. 0x1.306fe0a31b715p+0, 0x1.33c08b26416ffp+0, 0x1.371a7373aa9cbp+0, 0x1.3a7db34e59ff7p+0,
  414. 0x1.3dea64c123422p+0, 0x1.4160a21f72e2ap+0, 0x1.44e086061892dp+0, 0x1.486a2b5c13cd0p+0,
  415. 0x1.4bfdad5362a27p+0, 0x1.4f9b2769d2ca7p+0, 0x1.5342b569d4f82p+0, 0x1.56f4736b527dap+0,
  416. 0x1.5ab07dd485429p+0, 0x1.5e76f15ad2148p+0, 0x1.6247eb03a5585p+0, 0x1.6623882552225p+0,
  417. 0x1.6a09e667f3bcdp+0, 0x1.6dfb23c651a2fp+0, 0x1.71f75e8ec5f74p+0, 0x1.75feb564267c9p+0,
  418. 0x1.7a11473eb0187p+0, 0x1.7e2f336cf4e62p+0, 0x1.82589994cce13p+0, 0x1.868d99b4492edp+0,
  419. 0x1.8ace5422aa0dbp+0, 0x1.8f1ae99157736p+0, 0x1.93737b0cdc5e5p+0, 0x1.97d829fde4e50p+0,
  420. 0x1.9c49182a3f090p+0, 0x1.a0c667b5de565p+0, 0x1.a5503b23e255dp+0, 0x1.a9e6b5579fdbfp+0,
  421. 0x1.ae89f995ad3adp+0, 0x1.b33a2b84f15fbp+0, 0x1.b7f76f2fb5e47p+0, 0x1.bcc1e904bc1d2p+0,
  422. 0x1.c199bdd85529cp+0, 0x1.c67f12e57d14bp+0, 0x1.cb720dcef9069p+0, 0x1.d072d4a07897cp+0,
  423. 0x1.d5818dcfba487p+0, 0x1.da9e603db3285p+0, 0x1.dfc97337b9b5fp+0, 0x1.e502ee78b3ff6p+0,
  424. 0x1.ea4afa2a490dap+0, 0x1.efa1bee615a27p+0, 0x1.f50765b6e4540p+0, 0x1.fa7c1819e90d8p+0,
  425. }; // 2^(j/64), correctly rounded
  426. // double → int32 in the current rounding mode (the library's cvtpd2dq)
  427. inline int roundCurrent (double v) noexcept
  428. {
  429. #ifdef LYNX_CHORUS_MXCSR
  430. return _mm_cvtsd_si32 (_mm_set_sd (v));
  431. #else
  432. return (int) std::nearbyint (v);
  433. #endif
  434. }
  435. float crtPow2f (float e) noexcept // powf (2, e) for 0 ≤ e < 128
  436. {
  437. const double t = (double) e * 0.6931471805599453;
  438. const int N = roundCurrent (t * 92.33248261689366);
  439. const double r = t - (double) N * 0.010830424696249145;
  440. const double q = (r * r) * ((r * 0.16666666666666666) + 0.5) + r;
  441. const double T = exp2Table[N & 63];
  442. const double v = q * T + T;
  443. return (float) fromBits (bitsOf (v) + ((std::uint64_t) (std::int64_t) (N >> 6) << 52));
  444. }
  445. // per voice of a chorus core: delay range (ms), modulation rate range (Hz), modulation amount range, movement speed
  446. // range (Hz); the first core's four voices, then the ArachnaChor core's (§2.3)
  447. constexpr float voiceTable[8][8] {
  448. { 2.0f, 6.0f, 1.5f, 2.47f, 20.0f, 80.0f, 0.33f, 3.56f }, { 3.5f, 8.25f, 1.57f, 3.41f, 20.0f, 80.0f, 0.29f, 2.66f },
  449. { 6.5f, 10.0f, 0.38f, 3.64f, 20.0f, 80.0f, 0.26f, 3.78f }, { 5.0f, 12.8f, 1.29f, 3.68f, 20.0f, 80.0f, 0.14f, 2.98f },
  450. { 7.1f, 16.6f, 3.41f, 5.03f, 20.0f, 80.0f, 0.44f, 5.06f }, { 9.1f, 15.1f, 2.47f, 5.37f, 20.0f, 80.0f, 0.66f, 8.08f },
  451. { 8.3f, 16.8f, 3.01f, 6.02f, 20.0f, 80.0f, 0.93f, 6.02f }, { 9.6f, 17.7f, 3.3f, 6.5f, 20.0f, 80.0f, 0.41f, 10.22f },
  452. };
  453. const float voiceGain = bitsF (0x3FB50BF3u); // 1.41442716
  454. std::uint32_t randStep (std::uint32_t s) noexcept { return s * 214013u + 2531011u; }
  455. struct Core
  456. {
  457. std::array<float, 8> o {}; // voice outputs: left voices 0…3, right voices 4…7
  458. float d[4] {}, fb[4] {}, amt[4] {}, phM[4] {}, incM[4] {}, phd[4] {}, phV[4] {}, incV[4] {}, mv[4] {};
  459. std::vector<float> line = std::vector<float> ((std::size_t) 8 * 65536, 0.0f);
  460. std::uint16_t w = 0;
  461. double fs = 44100.0;
  462. int first = 0; // first row of voiceTable
  463. void set (int k, float x) noexcept
  464. {
  465. for (int l = 0; l < 4; ++l)
  466. {
  467. const float* T = voiceTable[first + l];
  468. switch (k)
  469. {
  470. case 0: d[l] = (((T[1] - T[0]) * x + T[0]) * 0.001f) * (float) fs; break;
  471. case 1: fb[l] = x * 0.7f; break;
  472. case 2: incM[l] = ((T[3] - T[2]) * x + T[2]) / (float) fs; break;
  473. case 3:
  474. {
  475. const float v = (T[5] - T[4]) * x + T[4];
  476. amt[l] = (v * v) * bitsF (0x3851B717u); // 5e-5
  477. break;
  478. }
  479. case 4: mv[l] = x * bitsF (0x3EFF7CEEu); break; // 0.499
  480. case 5: incV[l] = ((T[7] - T[6]) * x + T[6]) / (float) fs; break;
  481. case 10: phd[l] = ((x * x) * x) * 0.25f; break;
  482. default: break;
  483. }
  484. }
  485. }
  486. static float shape (float t) noexcept
  487. {
  488. const float a = t < 1.0f ? t : 1.0f, b = (t > 1.0f ? t : 1.0f) - 1.0f;
  489. return (a - a * a) - (b - b * b);
  490. }
  491. static float frac (float v) noexcept { return v - (float) (int) v; }
  492. float tap (const float* s, float delay) const noexcept
  493. {
  494. const auto i = (std::uint16_t) (w - (std::uint16_t) (int) delay);
  495. const float y0 = s[i], y1 = s[(std::uint16_t) (i - 1)], y2 = s[(std::uint16_t) (i - 2)], y3 = s[(std::uint16_t) (i - 3)];
  496. const float c = (y2 - y0) * 0.5f, e = (y3 - y1) * 0.5f, A = y1 - y2, B = A + c, D = e + (B + A);
  497. const float f = delay - (float) (int) delay;
  498. return ((((f * D) - (D + B)) * f) + c) * f + y1;
  499. }
  500. void process (float inL, float inR) noexcept
  501. {
  502. float dl[4], dr[4], m[4];
  503. for (int l = 0; l < 4; ++l)
  504. {
  505. const float pR = frac (phM[l] + phd[l]);
  506. m[l] = ((shape (phV[l] + phV[l]) * 4.0f) * mv[l]) + 0.5f;
  507. const float dA = d[l] * amt[l];
  508. dl[l] = ((shape (phM[l] + phM[l]) * 4.0f) * dA) + d[l];
  509. dr[l] = ((shape (pR + pR) * 4.0f) * dA) + d[l];
  510. }
  511. float* L = line.data();
  512. for (int l = 0; l < 4; ++l)
  513. L[(std::size_t) l * 65536 + w] = ((sqrtF (1.0f - m[l]) * voiceGain) * inL) + fb[l] * o[(std::size_t) l];
  514. for (int l = 0; l < 4; ++l)
  515. o[(std::size_t) l] = tap (L + (std::size_t) l * 65536, dl[l]);
  516. for (int l = 0; l < 4; ++l)
  517. L[(std::size_t) (4 + l) * 65536 + w] = ((sqrtF (m[l]) * voiceGain) * inR) + o[(std::size_t) (4 + l)] * fb[l];
  518. for (int l = 0; l < 4; ++l)
  519. o[(std::size_t) (4 + l)] = tap (L + (std::size_t) (4 + l) * 65536, dr[l]);
  520. ++w;
  521. for (int l = 0; l < 4; ++l)
  522. {
  523. phM[l] = frac (incM[l] + phM[l]);
  524. phV[l] = frac (incV[l] + phV[l]);
  525. }
  526. }
  527. void clear() noexcept
  528. {
  529. o.fill (0.0f);
  530. w = 0;
  531. std::fill (line.begin(), line.end(), 0.0f);
  532. }
  533. };
  534. // RBJ low-/high-pass with α = sin ω (§2.4); designed in double, stored as float in the current rounding mode
  535. void design (bool highPass, double f, double fs, float* c) noexcept
  536. {
  537. const double om = (f * 6.283185307179586) * (1.0 / fs);
  538. double S, C;
  539. crtSinCos (om, S, C);
  540. const double inv = 1.0 / (S + 1.0);
  541. const double h = highPass ? (C + 1.0) * 0.5 : (1.0 - C) * 0.5, m = highPass ? -(C + 1.0) : 1.0 - C;
  542. c[0] = (float) (inv * h);
  543. c[1] = (float) (inv * m);
  544. c[2] = (float) (inv * h);
  545. c[3] = -(float) (inv * (C * -2.0));
  546. c[4] = -(float) (inv * (1.0 - S));
  547. }
  548. struct Biquad
  549. {
  550. float c[5] {};
  551. float y1[2] {}, y2[2] {}, x1[2] {}, x2[2] {};
  552. float run (int ch, float x) noexcept
  553. {
  554. const float y = ((c[3] * y1[ch] + c[4] * y2[ch]) + c[2] * x2[ch]) + (c[1] * x1[ch] + c[0] * x);
  555. x2[ch] = x1[ch], x1[ch] = x, y2[ch] = y1[ch], y1[ch] = y;
  556. return y;
  557. }
  558. void clear() noexcept
  559. {
  560. for (int ch = 0; ch < 2; ++ch)
  561. y1[ch] = y2[ch] = x1[ch] = x2[ch] = 0.0f;
  562. }
  563. };
  564. } // namespace
  565. struct Engine::Impl
  566. {
  567. Core core[2];
  568. float value[params] { 0.4f, 0.5f, 0.4f, 0.4f, 0.3f, 0.3f, 0.0f, 1.0f, 0.0f, 0.5f, 0.1f };
  569. struct Pending
  570. {
  571. int id;
  572. float cur, target;
  573. };
  574. std::vector<Pending> pending;
  575. float smooth = 1.0f;
  576. bool arachna = false;
  577. double fs = 44100.0;
  578. int fade = 0, fadeLen = 441;
  579. float fadeStep = 0.0f, dry = 1.0f, wet = 0.0f;
  580. Biquad lp, hp;
  581. explicit Impl (int instance)
  582. {
  583. pending.reserve (32);
  584. core[1].first = 4;
  585. std::uint32_t s = 1;
  586. for (int k = 0; k < 16 * std::max (0, instance); ++k)
  587. s = randStep (s);
  588. auto next = [&s] {
  589. s = randStep (s);
  590. const auto f = (float) ((double) (int) ((s >> 16) & 0x7fffu) * 3.051850947599719e-05);
  591. return std::clamp (f, 0.0f, 1.0f);
  592. };
  593. for (auto& c : core)
  594. {
  595. for (auto& p : c.phM)
  596. p = next();
  597. for (auto& p : c.phV)
  598. p = next();
  599. }
  600. }
  601. void apply (int k, float x) noexcept // the engine's parameter handler
  602. {
  603. switch (k)
  604. {
  605. case 6: arachna = x >= 0.5f; break;
  606. case 7: design (false, (double) (crtPow2f (bitsF (0x409E4102u) * x) * 714.0f), fs, lp.c); break;
  607. case 8: design (true, (double) (crtPow2f (bitsF (0x40D02A30u) * x) * 12.0f), fs, hp.c); break;
  608. case 9:
  609. {
  610. float g;
  611. if (x >= 0.5f)
  612. {
  613. const float t = 1.0f - (((x - 0.5f) + x) - 0.5f);
  614. g = 1.0f - (t * t) * 0.5f;
  615. }
  616. else
  617. g = ((x + x) * (x + x)) * 0.5f;
  618. wet = sqrtF (g);
  619. dry = sqrtF (1.0f - g);
  620. break;
  621. }
  622. default:
  623. core[0].set (k, x);
  624. core[1].set (k, x);
  625. break;
  626. }
  627. }
  628. void setRate (double rate) noexcept
  629. {
  630. fs = (double) (float) rate;
  631. fadeLen = (int) (fs * 0.01);
  632. fadeStep = 1.0f / (float) fadeLen;
  633. core[0].fs = core[1].fs = fs;
  634. smooth = (float) (1.0 - std::pow (0.09999999999999998, 1.0 / (fs * 0.05)));
  635. }
  636. void setParam (int k, float x) noexcept
  637. {
  638. value[k] = x;
  639. for (auto& p : pending)
  640. if (p.id == k)
  641. {
  642. p.target = x;
  643. return;
  644. }
  645. pending.push_back ({ k, value[k], x });
  646. }
  647. float hpFade (int ch, float x) noexcept
  648. {
  649. const float y = hp.run (ch, x), f = (float) fade * fadeStep;
  650. return x * (1.0f - f) + y * f;
  651. }
  652. void render (const float* in, float* out, int n) noexcept
  653. {
  654. CsrMode mode (0xE040u);
  655. for (int i = 0; i < n; ++i)
  656. {
  657. for (int j = (int) pending.size() - 1; j >= 0; --j)
  658. {
  659. auto& p = pending[(std::size_t) j];
  660. const float diff = p.target - p.cur;
  661. if (std::fabs (diff) >= bitsF (0x38D1B717u)) // 1e-4
  662. {
  663. p.cur = diff * smooth + p.cur;
  664. apply (p.id, p.cur);
  665. }
  666. else
  667. {
  668. apply (p.id, p.target);
  669. pending.erase (pending.begin() + j);
  670. }
  671. }
  672. const float inL = in[2 * i], inR = in[2 * i + 1];
  673. core[0].process (inL, inR);
  674. const auto& a = core[0].o;
  675. float sL = ((a[0] + a[1]) + a[2]) + a[3], sR = ((a[4] + a[5]) + a[6]) + a[7];
  676. if (arachna)
  677. {
  678. core[1].process (inL, inR);
  679. const auto& b = core[1].o;
  680. sR = (((sR + b[7]) + b[6]) + (b[5] + b[4])) * bitsF (0x3E8FF594u);
  681. sL = (((sL + b[3]) + b[2]) + (b[1] + b[0])) * bitsF (0x3E8FF594u);
  682. }
  683. else
  684. {
  685. sR = sR * bitsF (0x3F0FF594u);
  686. sL = sL * bitsF (0x3F0FF594u);
  687. }
  688. float yL = lp.run (0, sL), yR = lp.run (1, sR);
  689. if (value[8] == 0.0f)
  690. {
  691. if (fade > 0)
  692. {
  693. yL = hpFade (0, yL), yR = hpFade (1, yR);
  694. --fade;
  695. }
  696. }
  697. else if (fade < fadeLen)
  698. {
  699. yL = hpFade (0, yL), yR = hpFade (1, yR);
  700. ++fade;
  701. }
  702. else
  703. yL = hp.run (0, yL), yR = hp.run (1, yR);
  704. out[2 * i] = inL * dry + yL * wet;
  705. out[2 * i + 1] = inR * dry + yR * wet;
  706. }
  707. }
  708. };
  709. Engine::Engine (int instance) : d (std::make_unique<Impl> (instance)) {}
  710. Engine::~Engine() = default;
  711. void Engine::setRate (double sampleRate) noexcept { d->setRate (sampleRate); }
  712. void Engine::setParam (int index, float value) noexcept
  713. {
  714. if (index >= 0 && index < params)
  715. d->setParam (index, value);
  716. }
  717. float Engine::param (int index) const noexcept { return d->value[index]; }
  718. void Engine::reset() noexcept
  719. {
  720. d->lp.clear();
  721. d->hp.clear();
  722. d->fade = 0;
  723. d->core[0].clear();
  724. }
  725. void Engine::render (const float* in, float* out, int n) noexcept { d->render (in, out, n); }
  726. std::uint32_t randState (int calls) noexcept
  727. {
  728. std::uint32_t s = 1;
  729. for (int k = 0; k < calls; ++k)
  730. s = randStep (s);
  731. return s;
  732. }
  733. std::optional<State> parse (const std::vector<std::uint8_t>& state)
  734. {
  735. if (state.size() < 4 + 4 * params || i32At (state, 0) >= 3)
  736. return std::nullopt;
  737. State s {};
  738. for (int i = 0; i < params; ++i)
  739. s[(std::size_t) i] = i32At (state, 4 + 4 * (std::size_t) i);
  740. return s;
  741. }
  742. void toParams (const State& s, float* p) noexcept
  743. {
  744. for (int i = 0; i < params; ++i)
  745. p[i] = norm (s[(std::size_t) i], rawMin[(std::size_t) i], rawMax[(std::size_t) i]);
  746. }
  747. State fromParams (const float* p) noexcept
  748. {
  749. State s {};
  750. for (int i = 0; i < params; ++i)
  751. s[(std::size_t) i] = rawFrom (p[i], rawMin[(std::size_t) i], rawMax[(std::size_t) i]);
  752. return s;
  753. }
  754. const char* paramName (int param) noexcept
  755. {
  756. static const char* names[params] { "Delay", "Feedback", "Mod rate", "Mod amount", "Movement", "Movement speed",
  757. "ArachnaChor mode", "Low pass", "High pass", "Dry/wet", "LFO phase difference" };
  758. return param >= 0 && param < params ? names[param] : "";
  759. }
  760. std::string paramText (int param, float value01)
  761. {
  762. char text[40];
  763. const int p = std::clamp (param, 0, params - 1);
  764. const int raw = rawFrom (value01, rawMin[(std::size_t) p], rawMax[(std::size_t) p]);
  765. const double x = (double) (raw - rawMin[(std::size_t) p]) / (rawMax[(std::size_t) p] - rawMin[(std::size_t) p]);
  766. switch (p)
  767. {
  768. case 6: return x >= 0.5 ? "on" : "off";
  769. case 7: std::snprintf (text, sizeof (text), "%.0f Hz", 714.0 * std::pow (2.0, 4.9454355 * x)); return text;
  770. case 8:
  771. if (x == 0.0)
  772. return "off";
  773. std::snprintf (text, sizeof (text), "%.0f Hz", 12.0 * std::pow (2.0, 6.50515 * x));
  774. return text;
  775. case 9: std::snprintf (text, sizeof (text), "%.1f%% wet", x * 100.0); return text;
  776. default: std::snprintf (text, sizeof (text), "%.1f", x * 100.0); return text;
  777. }
  778. }
  779. namespace
  780. {
  781. class HyperChorusEffect final : public Effect
  782. {
  783. public:
  784. explicit HyperChorusEffect (int instance) : engine (instance) {}
  785. void prepare (double sampleRate) noexcept override
  786. {
  787. engine.setRate (sampleRate);
  788. for (int i = 0; i < hyperchorusfx::params; ++i) // re-applied at this rate (FL loads the state after the rate)
  789. engine.setParam (i, engine.param (i));
  790. }
  791. void reset() noexcept override { engine.reset(); }
  792. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  793. {
  794. const auto s = fromParams (params.data());
  795. for (int i = 0; i < hyperchorusfx::params; ++i)
  796. {
  797. const float v = flValue (s[(std::size_t) i], rawMin[(std::size_t) i], rawMax[(std::size_t) i]);
  798. if (v != engine.param (i) || ! started)
  799. engine.setParam (i, v);
  800. }
  801. started = true;
  802. for (int o = 0; o < n; o += 512)
  803. {
  804. const int m = std::min (512, n - o);
  805. for (int i = 0; i < m; ++i)
  806. {
  807. buf[2 * (std::size_t) i] = left[o + i];
  808. buf[2 * (std::size_t) i + 1] = right[o + i];
  809. }
  810. engine.render (buf.data(), buf.data(), m);
  811. for (int i = 0; i < m; ++i)
  812. {
  813. left[o + i] = buf[2 * (std::size_t) i];
  814. right[o + i] = buf[2 * (std::size_t) i + 1];
  815. }
  816. }
  817. }
  818. private:
  819. Engine engine;
  820. bool started = false;
  821. std::array<float, 1024> buf {};
  822. };
  823. } // namespace
  824. std::unique_ptr<Effect> make (int instance) { return std::make_unique<HyperChorusEffect> (instance); }
  825. int instanceOf (const std::vector<std::uint8_t>& slotState) noexcept
  826. {
  827. return slotState.size() == 4 ? std::clamp ((int) i32At (slotState, 0), 0, 1 << 20) : 0;
  828. }
  829. std::vector<std::uint8_t> instanceState (int instance)
  830. {
  831. std::vector<std::uint8_t> b (4);
  832. std::memcpy (b.data(), &instance, 4);
  833. return b;
  834. }
  835. const EffectInfo& info()
  836. {
  837. static const EffectInfo i = [] {
  838. EffectInfo e { "Hyper Chorus", {} };
  839. for (int k = 0; k < params; ++k)
  840. e.params.push_back ({ paramName (k), norm (defaults[(std::size_t) k], rawMin[(std::size_t) k], rawMax[(std::size_t) k]) });
  841. return e;
  842. }();
  843. return i;
  844. }
  845. } // namespace hyperchorusfx
  846. // ================================================================================================ Vintage Chorus (§3)
  847. namespace vintagechorusfx
  848. {
  849. namespace
  850. {
  851. // fused multiply-add: the engine's library runs its FMA variant on processors that have FMA (§3.7)
  852. #if defined(LYNX_CHORUS_MXCSR) && (defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__))
  853. __attribute__ ((target ("fma"))) double fmaHw (double a, double b, double c) noexcept
  854. {
  855. return _mm_cvtsd_f64 (_mm_fmadd_sd (_mm_set_sd (a), _mm_set_sd (b), _mm_set_sd (c)));
  856. }
  857. bool hasFma() noexcept
  858. {
  859. static const bool f = [] {
  860. unsigned a = 0, b = 0, c = 0, d = 0;
  861. if (! __get_cpuid (1, &a, &b, &c, &d))
  862. return false;
  863. if ((c & (1u << 12)) == 0 || (c & (1u << 27)) == 0 || (c & (1u << 28)) == 0) // FMA, OSXSAVE, AVX
  864. return false;
  865. unsigned lo = 0, hi = 0;
  866. asm volatile ("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
  867. return (lo & 6u) == 6u; // the OS saves the vector state
  868. }();
  869. return f;
  870. }
  871. inline double fmaD (double a, double b, double c) noexcept { return hasFma() ? fmaHw (a, b, c) : std::fma (a, b, c); }
  872. #else
  873. inline double fmaD (double a, double b, double c) noexcept { return std::fma (a, b, c); }
  874. #endif
  875. // the library's sinf for |x| < 2^24 (§3.7): double evaluation, odd/even polynomials after a reduction by π/2
  876. float libSinf (float xf) noexcept
  877. {
  878. const double x = xf, ax = std::fabs (x);
  879. if (! (ax > 0.7853981633974483))
  880. {
  881. if (ax < 0.0078125)
  882. {
  883. if (ax < 0.0001220703125)
  884. return xf;
  885. const double x3 = (x * x) * x;
  886. return (float) fmaD (-x3, 0.16666666666666666, x);
  887. }
  888. const double x2 = x * x;
  889. double p = fmaD (x2, 2.7557319223985893e-06, -0.0001984126984126984);
  890. p = fmaD (p, x2, 0.008333333333333333);
  891. p = fmaD (p, x2, -0.16666666666666666);
  892. return (float) fmaD (p, x * x2, x);
  893. }
  894. const int n = (int) fmaD (ax, 0.6366197723675814, 0.5);
  895. const double nd = n;
  896. const double r = fmaD (-nd, 1.5707963267341256, ax) - nd * 6.077100506506192e-11;
  897. const double r2 = r * r;
  898. double y;
  899. if (n & 1)
  900. {
  901. const double c = fmaD (r2, -0.5, 1.0);
  902. double p = fmaD (r2, -2.755731922398589e-07, 2.4801587301587298e-05);
  903. p = fmaD (p, r2, -0.0013888888888888887);
  904. p = fmaD (p, r2, 0.041666666666666664);
  905. y = fmaD (p, r2 * r2, c);
  906. }
  907. else
  908. {
  909. double p = fmaD (r2, 2.7557319223985893e-06, -0.0001984126984126984);
  910. p = fmaD (p, r2, 0.008333333333333333);
  911. p = fmaD (p, r2, -0.16666666666666666);
  912. y = fmaD (p, r * r2, r);
  913. }
  914. const bool negate = ((n & 3) >= 2) != std::signbit (x);
  915. return (float) (negate ? -y : y);
  916. }
  917. constexpr float pif = 3.14159274f;
  918. inline float hexF (std::uint32_t u) noexcept
  919. {
  920. float f;
  921. std::memcpy (&f, &u, 4);
  922. return f;
  923. }
  924. constexpr int lineSize = 32768, lineMask = lineSize - 1;
  925. struct Biquad5
  926. {
  927. float c[5] {}; // feed-forward x, x1, x2; feedback y1, y2 (already negated)
  928. float x1[2] {}, x2[2] {}, y1[2] {}, y2[2] {};
  929. float run (int ch, float x) noexcept
  930. {
  931. const float y = (((c[1] * x1[ch] + c[0] * x) + c[2] * x2[ch]) + c[3] * y1[ch]) + c[4] * y2[ch];
  932. x2[ch] = x1[ch], x1[ch] = x, y2[ch] = y1[ch], y1[ch] = y;
  933. return y;
  934. }
  935. void clear() noexcept
  936. {
  937. for (int ch = 0; ch < 2; ++ch)
  938. x1[ch] = x2[ch] = y1[ch] = y2[ch] = 0.0f;
  939. }
  940. };
  941. struct OnePole
  942. {
  943. float b0 = 0.0f, b1 = 0.0f, a1 = 0.0f, piOverFs = 0.0f;
  944. float x1[2] {}, y1[2] {};
  945. float run (int ch, float x) noexcept
  946. {
  947. const float y = (b1 * x1[ch] + b0 * x) + a1 * y1[ch];
  948. x1[ch] = x, y1[ch] = y;
  949. return y;
  950. }
  951. void highPass (float f) noexcept
  952. {
  953. const auto t = (float) std::tan ((double) (f * piOverFs));
  954. b0 = 1.0f / (t + 1.0f);
  955. b1 = -b0;
  956. a1 = -(t - 1.0f) / (t + 1.0f);
  957. }
  958. void lowPass (float f) noexcept
  959. {
  960. const auto t = (float) std::tan ((double) (piOverFs * f));
  961. b0 = b1 = t / (t + 1.0f);
  962. a1 = -(t - 1.0f) / (t + 1.0f);
  963. }
  964. void clear() noexcept { x1[0] = x1[1] = y1[0] = y1[1] = 0.0f; }
  965. };
  966. } // namespace
  967. struct Engine::Impl
  968. {
  969. float value[params] { 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.1f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f };
  970. int mode = 5;
  971. double fs = 44100.0;
  972. // the chorus core
  973. bool mono = false, sync = false, started = false, gate = false;
  974. float startPhase = 0.0f, inc = 0.0f, phase = 0.0f, phaseDiff = 0.5f, d1 = 0.0f, d2 = 0.0f, dd = 0.0f;
  975. double period = 0.0, lastPpq = -1.0;
  976. int wave = 0;
  977. std::vector<float> line[2] { std::vector<float> (lineSize, 0.0f), std::vector<float> (lineSize, 0.0f) };
  978. int w[2] {};
  979. OnePole pre, post;
  980. Biquad5 bq1, bq2, noiseF;
  981. float fb = 0.0f, out[2] {};
  982. std::uint32_t seed[2] {};
  983. float hiss = 0.0f, fsF = 44100.0f, invFs = 0.0f, k = 0.0f;
  984. int noiseCount = 16;
  985. float gateThr = 0.0025f, gateGain = 1.0f, gateDecay = 0.0f;
  986. int gateHold = 440, gateCount = 440;
  987. float gainT = 1.0f, polarity = 1.0f, mixT = 0.5f, mixS = 0.0f, gainS = 0.0f, gainS2 = 0.0f, dl = 0.01f, dr = 0.01f;
  988. float noiseC[2][5] {};
  989. explicit Impl (int instance)
  990. {
  991. const auto g = (std::uint32_t) (333784u + 0xb66adu * (std::uint32_t) std::max (0, instance));
  992. seed[0] = g, seed[1] = g + 0x1099eu;
  993. gateDecay = (float) std::pow (0.1, 0.00015117157373598967);
  994. rateSetup (44100.0);
  995. for (int pass = 0; pass < 2; ++pass)
  996. for (int i = 0; i < params; ++i)
  997. set (i, value[i]);
  998. }
  999. void rateSetup (double rate) noexcept
  1000. {
  1001. k = (float) (1.0 - std::pow (0.09999999999999998, 1.0 / (rate * 0.05)));
  1002. noiseCount = 16;
  1003. fsF = (float) rate;
  1004. invFs = 1.0f / fsF;
  1005. pre.piOverFs = (float) (1.0 / (double) fsF) * pif;
  1006. post.piOverFs = pre.piOverFs;
  1007. pre.highPass (50.0f);
  1008. post.lowPass (7000.0f);
  1009. const auto om = (double) (43982.2969f / fsF);
  1010. const auto S = (float) std::sin (om) / 1.41400003f, C = (float) std::cos (om);
  1011. const float inv = 1.0f / (S + 1.0f), h = ((1.0f - C) * 0.5f) * inv;
  1012. bq1.c[0] = h, bq1.c[1] = (1.0f - C) * inv, bq1.c[2] = h, bq1.c[3] = -(C * -2.0f) * inv, bq1.c[4] = -(1.0f - S) * inv;
  1013. std::copy (std::begin (bq1.c), std::end (bq1.c), bq2.c);
  1014. }
  1015. void setRate (double rate) noexcept
  1016. {
  1017. fs = (double) (float) rate;
  1018. rateSetup (fs);
  1019. for (int i = 0; i < params; ++i)
  1020. set (i, value[i]);
  1021. }
  1022. void wetHighPass (float f) noexcept { pre.highPass (f); }
  1023. void set (int i, float x) noexcept
  1024. {
  1025. value[i] = x;
  1026. switch (i)
  1027. {
  1028. case 0:
  1029. case 1:
  1030. {
  1031. mode = (int) ((value[0] * 3.0f) * 0.999899983f);
  1032. if (mode == 0 || mode == 1)
  1033. {
  1034. if (0.5f <= value[1])
  1035. {
  1036. inc = 8.0f / fsF, wave = 1, phaseDiff = hexF (0x3cea61dau);
  1037. d1 = hexF (0x3b3ac711u), d2 = hexF (0x3b5cdb38u), dd = hexF (0x3a08509cu);
  1038. }
  1039. else
  1040. {
  1041. inc = (mode == 0 ? 0.4f : 0.6f) / fsF, wave = 0, phaseDiff = 0.5f;
  1042. d1 = hexF (0x3a8d8ec9u), d2 = hexF (0x3bab606bu), dd = hexF (0x3b87fcb9u);
  1043. }
  1044. mixT = 0.5f, polarity = 1.0f, fb = 0.0f;
  1045. wetHighPass (0.5f);
  1046. sync = false, started = false;
  1047. }
  1048. else if (mode == 2)
  1049. for (int j = 2; j < params; ++j)
  1050. set (j, value[j]);
  1051. break;
  1052. }
  1053. case 2:
  1054. case 15:
  1055. if (mode == 2)
  1056. {
  1057. const float v = value[2];
  1058. started = false;
  1059. sync = 0.5f <= value[15];
  1060. inc = ((v * v) * 10.0f) / fsF;
  1061. const double e = std::pow (2.0, (double) ((1.0f - v) * 7.0f));
  1062. const auto t = (float) ((e - 1.0) * 1.0078740119934082);
  1063. period = ((double) (int) (((t * 48.0f) * 6143.0f) / 6144.0f + 1.10000002f) / 48.0) * 0.25;
  1064. if (sync)
  1065. started = false;
  1066. }
  1067. break;
  1068. case 3:
  1069. if (mode == 2)
  1070. wave = 0.5f <= x ? 1 : 0;
  1071. break;
  1072. case 4:
  1073. if (mode == 2)
  1074. phaseDiff = x;
  1075. break;
  1076. case 5:
  1077. if (mode == 2)
  1078. {
  1079. d1 = (x * 9.80000019f + 0.200000003f) * 0.00100000005f;
  1080. dd = d2 - d1;
  1081. }
  1082. break;
  1083. case 6:
  1084. if (mode == 2)
  1085. {
  1086. d2 = (x * 9.80000019f + 0.200000003f) * 0.00100000005f;
  1087. dd = d2 - d1;
  1088. }
  1089. break;
  1090. case 7:
  1091. if (mode == 2)
  1092. mixT = x;
  1093. break;
  1094. case 8: mono = 0.5f <= x; break;
  1095. case 9:
  1096. if (mode == 2)
  1097. polarity = x < 0.5f ? 1.0f : -1.0f;
  1098. break;
  1099. case 10:
  1100. if (mode == 2)
  1101. fb = x * 0.629999995f;
  1102. break;
  1103. case 11:
  1104. if (mode == 2)
  1105. wetHighPass ((x * x) * 160.0f + 10.0f);
  1106. break;
  1107. case 12: gainT = (float) std::pow (10.0, (double) ((x * 12.0f) / 20.0f)); break;
  1108. case 13:
  1109. if (x == 0.0f)
  1110. hiss = 0.0f, gateThr = 0.0f;
  1111. else
  1112. {
  1113. hiss = (float) std::pow (10.0, (double) ((x * 50.0f - 90.0f) / 20.0f)) * 0.200000003f;
  1114. gateThr = hiss * 8.0f;
  1115. }
  1116. break;
  1117. case 14: gate = 0.5f <= x; break;
  1118. case 16: startPhase = x; break;
  1119. default: break;
  1120. }
  1121. }
  1122. static float tri (float p) noexcept
  1123. {
  1124. if (p < 0.25f)
  1125. return (p + p) + 0.5f;
  1126. if (p < 0.75f)
  1127. return (1.5f - p) - p;
  1128. return (p - 0.75f) + (p - 0.75f);
  1129. }
  1130. float tap (int ch, float delay) const noexcept
  1131. {
  1132. const float* s = line[ch].data();
  1133. const float pos = delay * fsF;
  1134. const int n = (int) pos, u = w[ch] - n;
  1135. const float y0 = s[u & lineMask], y1 = s[(u - 1) & lineMask], y2 = s[(u - 2) & lineMask], y3 = s[(u - 3) & lineMask];
  1136. const float f = pos - (float) n;
  1137. const float c = (y2 - y0) * 0.5f, A = y1 - y2, B = A + c, D = (y3 - y1) * 0.5f + (B + A);
  1138. return ((((D * f) - (D + B)) * f) + c) * f + y1;
  1139. }
  1140. void sample (float& L, float& R, double tempo, double ppq) noexcept
  1141. {
  1142. if (mono)
  1143. {
  1144. const float v = (L + R) * 0.5f;
  1145. L = R = v;
  1146. }
  1147. if (lastPpq < 0.0 && 0.0 <= ppq)
  1148. started = true;
  1149. lastPpq = ppq;
  1150. if (sync)
  1151. {
  1152. if (started && 0.0 <= ppq)
  1153. {
  1154. const float f = (float) (ppq / period) + startPhase;
  1155. phase = f - (float) (int) f;
  1156. }
  1157. inc = (float) ((tempo / 60.0) / period) * invFs;
  1158. }
  1159. const float p = phase, pr = (p + phaseDiff) - (float) (int) (p + phaseDiff);
  1160. float lfoL, lfoR;
  1161. if (wave == 1)
  1162. {
  1163. lfoL = libSinf (p * 6.28318548f) * 0.5f + 0.5f;
  1164. lfoR = libSinf (pr * 6.28318548f) * 0.5f + 0.5f;
  1165. }
  1166. else
  1167. lfoL = tri (p), lfoR = tri (pr);
  1168. dl = ((dd * lfoL + d1) - dl) * k + dl;
  1169. dr = ((dd * lfoR + d1) - dr) * k + dr;
  1170. const float xL = fb * out[0] + L, xR = fb * out[1] + R;
  1171. phase = (p + inc) - (float) (int) (p + inc);
  1172. float yL = pre.run (0, xL), yR = pre.run (1, xR);
  1173. yL = bq2.run (0, bq1.run (0, yL));
  1174. yR = bq2.run (1, bq1.run (1, yR));
  1175. line[0][(std::size_t) w[0]] = yL;
  1176. w[0] = (w[0] + 1) & lineMask;
  1177. float oL = tap (0, dl);
  1178. line[1][(std::size_t) w[1]] = yR;
  1179. w[1] = (w[1] + 1) & lineMask;
  1180. out[0] = oL;
  1181. float oR = tap (1, dr);
  1182. out[1] = oR;
  1183. if (hiss > 0.0f)
  1184. {
  1185. ++noiseCount;
  1186. seed[0] = seed[0] * 0x0BB38435u + 0x3619636Bu;
  1187. seed[1] = seed[1] * 0x0BB38435u + 0x3619636Bu;
  1188. const float nL = (float) (std::int32_t) seed[0] * 4.65661287e-10f, nR = (float) (std::int32_t) seed[1] * 4.65661287e-10f;
  1189. if (noiseCount >= 16)
  1190. {
  1191. noiseCount = 0;
  1192. const auto om = (double) (40840.7070f / fsF);
  1193. const float S2 = (float) std::sin (om) + (float) std::sin (om), c2 = (float) std::cos (om) * -2.0f;
  1194. const float g[2] { (lfoL + lfoL) + 1.0f, (lfoR + lfoR) + 1.0f };
  1195. for (int ch = 0; ch < 2; ++ch)
  1196. {
  1197. const float q = S2 / g[ch], pp = S2 * g[ch], inv = 1.0f / (q + 1.0f);
  1198. noiseC[ch][0] = (pp + 1.0f) * inv;
  1199. noiseC[ch][1] = c2 * inv;
  1200. noiseC[ch][2] = (1.0f - pp) * inv;
  1201. noiseC[ch][3] = -c2 * inv;
  1202. noiseC[ch][4] = -(1.0f - q) * inv;
  1203. }
  1204. }
  1205. float nf[2];
  1206. for (int ch = 0; ch < 2; ++ch)
  1207. {
  1208. std::copy (std::begin (noiseC[ch]), std::end (noiseC[ch]), noiseF.c);
  1209. nf[ch] = noiseF.run (ch, ch == 0 ? nL : nR);
  1210. }
  1211. oL = oL + hiss * nf[0];
  1212. oR = oR + hiss * nf[1];
  1213. }
  1214. const float pL = post.run (0, oL), pR = post.run (1, oR);
  1215. const float m = (mixT - mixS) * k + mixS;
  1216. mixS = m;
  1217. const float dryArg = 1.0f - m;
  1218. const float g = (gainT - gainS) * k + gainS;
  1219. gainS = g;
  1220. gainS2 = (gainT - gainS2) * k + gainS2;
  1221. const float dry = sqrtF (dryArg), wet = sqrtF (m);
  1222. float sL = ((pL * wet) * polarity) + L * dry, sR = ((pR * wet) * polarity) + R * dry;
  1223. if (gate)
  1224. {
  1225. int cnt;
  1226. if (std::fabs (sL) >= gateThr || std::fabs (sR) >= gateThr)
  1227. {
  1228. cnt = gateHold;
  1229. gateCount = cnt;
  1230. gateGain = 1.0f;
  1231. }
  1232. else
  1233. cnt = gateCount;
  1234. if (cnt > 0)
  1235. gateCount = cnt - 1;
  1236. else
  1237. gateGain = gateGain * gateDecay;
  1238. sL = gateGain * sL, sR = gateGain * sR;
  1239. }
  1240. L = sL * g;
  1241. R = sR * g;
  1242. }
  1243. void settle() noexcept
  1244. {
  1245. for (auto& l : line)
  1246. std::fill (l.begin(), l.end(), 0.0f);
  1247. w[0] = w[1] = 0;
  1248. pre.clear(), post.clear(), bq1.clear(), bq2.clear(), noiseF.clear();
  1249. out[0] = out[1] = 0.0f;
  1250. mixS = mixT, gainS = gainS2 = gainT;
  1251. const float p = phase, pr = (p + phaseDiff) - (float) (int) (p + phaseDiff);
  1252. dl = dd * (wave == 1 ? libSinf (p * 6.28318548f) * 0.5f + 0.5f : tri (p)) + d1;
  1253. dr = dd * (wave == 1 ? libSinf (pr * 6.28318548f) * 0.5f + 0.5f : tri (pr)) + d1;
  1254. gateGain = 1.0f, gateCount = gateHold;
  1255. }
  1256. };
  1257. Engine::Engine (int instance) : d (std::make_unique<Impl> (instance)) {}
  1258. Engine::~Engine() = default;
  1259. void Engine::setRate (double sampleRate) noexcept { d->setRate (sampleRate); }
  1260. void Engine::setParam (int index, float value) noexcept
  1261. {
  1262. if (index >= 0 && index < params)
  1263. d->set (index, value);
  1264. }
  1265. float Engine::param (int index) const noexcept { return d->value[index]; }
  1266. void Engine::settle() noexcept { d->settle(); }
  1267. void Engine::render (const float* in, float* out, int n, double tempo, double ppq, bool playing) noexcept
  1268. {
  1269. CsrMode mode (0x8040u);
  1270. const double step = playing ? tempo / 60.0 / (double) d->fsF : 0.0;
  1271. double pos = playing ? ppq : -1.0;
  1272. for (int i = 0; i < n; ++i)
  1273. {
  1274. float L = in[2 * i], R = in[2 * i + 1];
  1275. d->sample (L, R, tempo, pos);
  1276. out[2 * i] = L, out[2 * i + 1] = R;
  1277. pos += step;
  1278. }
  1279. }
  1280. std::optional<State> parse (const std::vector<std::uint8_t>& state)
  1281. {
  1282. if (state.size() < 4 + 4 * params || i32At (state, 0) >= 2)
  1283. return std::nullopt;
  1284. State s {};
  1285. for (int i = 0; i < params; ++i)
  1286. s[(std::size_t) i] = i32At (state, 4 + 4 * (std::size_t) i);
  1287. return s;
  1288. }
  1289. void toParams (const State& s, float* p) noexcept
  1290. {
  1291. for (int i = 0; i < params; ++i)
  1292. p[i] = norm (s[(std::size_t) i], rawMin[(std::size_t) i], rawMax[(std::size_t) i]);
  1293. }
  1294. State fromParams (const float* p) noexcept
  1295. {
  1296. State s {};
  1297. for (int i = 0; i < params; ++i)
  1298. s[(std::size_t) i] = rawFrom (p[i], rawMin[(std::size_t) i], rawMax[(std::size_t) i]);
  1299. return s;
  1300. }
  1301. const char* paramName (int param) noexcept
  1302. {
  1303. static const char* names[params] { "Mode", "Link Chorus 1&2", "LFO speed", "LFO wave", "LFO L/R phase", "Delay time 1",
  1304. "Delay time 2", "Wet/dry mix", "Mono input", "Wet polarity", "Feedback", "Wet HP freq",
  1305. "Gain", "Hiss", "Noise gate", "LFO sync", "LFO start phase" };
  1306. return param >= 0 && param < params ? names[param] : "";
  1307. }
  1308. std::string paramText (int param, float value01)
  1309. {
  1310. char text[40];
  1311. const int p = std::clamp (param, 0, params - 1);
  1312. const int raw = rawFrom (value01, rawMin[(std::size_t) p], rawMax[(std::size_t) p]);
  1313. const double x = (double) (raw - rawMin[(std::size_t) p]) / (rawMax[(std::size_t) p] - rawMin[(std::size_t) p]);
  1314. switch (p)
  1315. {
  1316. case 0:
  1317. {
  1318. static const char* modes[3] { "Chorus 1", "Chorus 2", "custom" };
  1319. return modes[std::clamp ((int) (x * 3.0 * 0.9999), 0, 2)];
  1320. }
  1321. case 1:
  1322. case 8:
  1323. case 14:
  1324. case 15: return x >= 0.5 ? "on" : "off";
  1325. case 2: std::snprintf (text, sizeof (text), "%.2f Hz", x * x * 10.0); return text;
  1326. case 3: return x >= 0.5 ? "sine" : "triangle";
  1327. case 5:
  1328. case 6: std::snprintf (text, sizeof (text), "%.2f ms", x * 9.8 + 0.2); return text;
  1329. case 7: std::snprintf (text, sizeof (text), "%.0f%% wet", x * 100.0); return text;
  1330. case 9: return x >= 0.5 ? "inverted" : "normal";
  1331. case 11: std::snprintf (text, sizeof (text), "%.0f Hz", x * x * 160.0 + 10.0); return text;
  1332. case 12: std::snprintf (text, sizeof (text), "%.1f dB", x * 12.0); return text;
  1333. case 13:
  1334. if (x == 0.0)
  1335. return "off";
  1336. std::snprintf (text, sizeof (text), "%.1f dB", x * 50.0 - 90.0);
  1337. return text;
  1338. default: std::snprintf (text, sizeof (text), "%.2f", x); return text;
  1339. }
  1340. }
  1341. namespace
  1342. {
  1343. class VintageChorusEffect final : public Effect
  1344. {
  1345. public:
  1346. explicit VintageChorusEffect (int instance) : engine (instance) {}
  1347. void prepare (double sampleRate) noexcept override
  1348. {
  1349. rate = sampleRate;
  1350. engine.setRate (sampleRate);
  1351. engine.settle();
  1352. }
  1353. void reset() noexcept override { engine.settle(); }
  1354. void setSongPosition (double ticks) noexcept override { songTicks = ticks; }
  1355. void process (float* left, float* right, int n, const EffectParams& params, double bpm) noexcept override
  1356. {
  1357. const auto s = fromParams (params.data());
  1358. bool changed = false;
  1359. for (int i = 0; i < vintagechorusfx::params; ++i)
  1360. {
  1361. const float v = flValue (s[(std::size_t) i], rawMin[(std::size_t) i], rawMax[(std::size_t) i]);
  1362. if (v != engine.param (i) || ! started)
  1363. engine.setParam (i, v), changed = true;
  1364. }
  1365. if (! started && changed)
  1366. engine.settle();
  1367. started = true;
  1368. const double beats = songTicks / (double) lynx::ppq;
  1369. for (int o = 0; o < n; o += 512)
  1370. {
  1371. const int m = std::min (512, n - o);
  1372. for (int i = 0; i < m; ++i)
  1373. {
  1374. buf[2 * (std::size_t) i] = left[o + i];
  1375. buf[2 * (std::size_t) i + 1] = right[o + i];
  1376. }
  1377. engine.render (buf.data(), buf.data(), m, bpm, beats + (double) o * bpm / 60.0 / rate, songTicks >= 0.0);
  1378. for (int i = 0; i < m; ++i)
  1379. {
  1380. left[o + i] = buf[2 * (std::size_t) i];
  1381. right[o + i] = buf[2 * (std::size_t) i + 1];
  1382. }
  1383. }
  1384. }
  1385. private:
  1386. Engine engine;
  1387. bool started = false;
  1388. double songTicks = -1.0, rate = 44100.0;
  1389. std::array<float, 1024> buf {};
  1390. };
  1391. } // namespace
  1392. std::unique_ptr<Effect> make (int instance) { return std::make_unique<VintageChorusEffect> (instance); }
  1393. const EffectInfo& info()
  1394. {
  1395. static const EffectInfo i = [] {
  1396. EffectInfo e { "Vintage Chorus", {} };
  1397. for (int k = 0; k < params; ++k)
  1398. e.params.push_back ({ paramName (k), norm (defaults[(std::size_t) k], rawMin[(std::size_t) k], rawMax[(std::size_t) k]) });
  1399. return e;
  1400. }();
  1401. return i;
  1402. }
  1403. } // namespace vintagechorusfx
  1404. // ============================================================================================================ FL import
  1405. int chorusControls (const std::string& pluginName) noexcept
  1406. {
  1407. if (pluginName == "Fruity Chorus")
  1408. return fruitychorusfx::params;
  1409. if (pluginName == "Hyper Chorus")
  1410. return hyperchorusfx::params;
  1411. if (pluginName == "Vintage Chorus")
  1412. return vintagechorusfx::params;
  1413. return 0;
  1414. }
  1415. void numberChorusInstances (Song& song)
  1416. {
  1417. int next = 0, nextVintage = 0;
  1418. std::function<void (std::vector<EffectSlot>&)> walk = [&] (std::vector<EffectSlot>& chain) {
  1419. for (auto& fx : chain)
  1420. {
  1421. if (fx.kind == EffectKind::hyperChorus)
  1422. fx.state = hyperchorusfx::instanceState (next++);
  1423. if (fx.kind == EffectKind::vintageChorus)
  1424. fx.state = hyperchorusfx::instanceState (nextVintage++);
  1425. for (auto& b : fx.branches)
  1426. walk (b.chain);
  1427. }
  1428. };
  1429. for (auto& t : song.mixer)
  1430. walk (t.effects);
  1431. }
  1432. std::optional<EffectSlot> chorusFromFl (const std::string& pluginName, const std::vector<std::uint8_t>& state)
  1433. {
  1434. if (pluginName == "Fruity Chorus")
  1435. if (const auto s = fruitychorusfx::parse (state))
  1436. {
  1437. auto slot = makeEffectSlot (EffectKind::fruityChorus);
  1438. fruitychorusfx::toParams (*s, slot.params.data());
  1439. return slot;
  1440. }
  1441. if (pluginName == "Hyper Chorus")
  1442. if (const auto s = hyperchorusfx::parse (state))
  1443. {
  1444. auto slot = makeEffectSlot (EffectKind::hyperChorus);
  1445. hyperchorusfx::toParams (*s, slot.params.data());
  1446. return slot;
  1447. }
  1448. if (pluginName == "Vintage Chorus")
  1449. if (const auto s = vintagechorusfx::parse (state))
  1450. {
  1451. auto slot = makeEffectSlot (EffectKind::vintageChorus);
  1452. vintagechorusfx::toParams (*s, slot.params.data());
  1453. return slot;
  1454. }
  1455. return std::nullopt;
  1456. }
  1457. } // namespace lynx