ParametricEqFl.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. #include "core/ParametricEqFl.h"
  2. #include "core/ParametricEqLinear.h"
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <complex>
  6. #include <cstring>
  7. // bit-exactness needs every product and sum rounded on its own (no fused multiply-add)
  8. #if defined(__clang__)
  9. #pragma clang fp contract(off)
  10. #elif defined(__GNUC__)
  11. #pragma GCC optimize("fp-contract=off")
  12. #endif
  13. // Exact Fruity Parametric EQ 2 / Fruity Parametric EQ engines (docs/analise/REA_rodada50.md). Every float/double
  14. // rounding below is deliberate: it follows the order of operations observed in the plugins (§2–§3).
  15. namespace lynx::peqfl
  16. {
  17. namespace
  18. {
  19. constexpr double twoPi = 6.283185307179586;
  20. constexpr double ln2 = 0.69314718055994529;
  21. constexpr double halfLn2 = 0.34657359027997264;
  22. constexpr double invSqrt2 = 0.70710678118654746;
  23. constexpr double freqStep = 0.00015206580024203624; // log2(1000) / 65536 (§2.1)
  24. constexpr float dbToLn = 0.115129255f; // ln(10) / 20 as a float
  25. constexpr float flushBelow = 5.9604644775390625e-08f; // 2^-24
  26. // FL's UI type → internal design type (§2.2)
  27. constexpr int internalType[8] = { 0, 1, 2, 4, 3, 6, 7, 8 };
  28. // Per-stage tables of the "steep" orders, row = |order| (§2.2)
  29. constexpr float steepCutQ[4][4] = { { 1, 1, 1, 1 }, { 1.98340166f, 0.749214649f, 1, 1 }, { 2.64303994f, 1.08151865f, 0.694244742f, 1 },
  30. { 3.57645154f, 1.14173281f, 1.04471076f, 0.655118585f } };
  31. constexpr float steepShelfS[4][4] = { { 1, 1, 1, 1 }, { 2.63044715f, 0.615644455f, 1, 1 }, { 5.16122818f, 1.10927105f, 0.509867311f, 1 },
  32. { 10.3191233f, 1.51443529f, 0.694032848f, 0.552996695f } };
  33. constexpr float steepPeakOffset[4][4] = { { 0, 0, 0, 0 }, { -0.25f, 0.25f, 0, 0 }, { -0.475f, 0, 0.475f, 0 },
  34. { -0.775f, -0.3f, 0.3f, 0.775f } };
  35. constexpr float steepPeakWidth[4][4] = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1.57079637f, 1, 1 }, { 1, 1.57079637f, 1.57079637f, 1 } };
  36. constexpr float steepPeakGain[4] = { 0.5f, 0.32125f, 0.2442f, 0.214445f };
  37. constexpr float steepPeakScale[4] = { 1.0f, 0.5f, 1.0f / 3.0f, 0.25f };
  38. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t offset)
  39. {
  40. std::int32_t v = 0;
  41. if (offset + 4 <= d.size())
  42. std::memcpy (&v, d.data() + offset, 4);
  43. return v;
  44. }
  45. // hyperbolic sine as the plugin's run-time library computes it
  46. double sinhRtl (double x)
  47. {
  48. if (std::abs (x) <= 1.0e-12)
  49. return 0.0;
  50. return (std::exp (x) - std::exp (-x)) / 2.0;
  51. }
  52. // Power(base, e) on floats for a non-integral exponent: exp(e·ln base)
  53. float powerF (float base, float e)
  54. {
  55. if (e == 0.0f)
  56. return 1.0f;
  57. if (base == 0.0f)
  58. return e > 0.0f ? 0.0f : 1.0f;
  59. if (std::trunc (e) == e && std::abs (e) < 2147483647.0f)
  60. {
  61. // integral exponent: repeated multiplication (never reached by the EQ's 1/(n+1), n ≥ 1)
  62. double r = 1.0, b = base;
  63. auto k = (long long) std::abs (e);
  64. while (k > 0)
  65. {
  66. if (k & 1)
  67. r *= b;
  68. b *= b;
  69. k >>= 1;
  70. }
  71. return (float) (e < 0 ? 1.0 / r : r);
  72. }
  73. return (float) std::exp ((double) e * std::log ((double) base));
  74. }
  75. // Biquad design from (A, alpha, sin ω, cos ω), per internal type (§2.3)
  76. void designStage (Stage& st, int type, double A, double alpha, double sn, double cs)
  77. {
  78. st.type = type;
  79. if (type >= 1 && type <= 5)
  80. {
  81. const double d = 1.0 / (alpha + 1.0);
  82. st.a1 = d * cs * -2.0;
  83. st.a2 = d * (1.0 - alpha);
  84. switch (type)
  85. {
  86. case 1: st.b1 = d * (1.0 - cs); st.b0 = st.b1 * 0.5; st.b2 = st.b0; break;
  87. case 2: st.b1 = 0.0; st.b0 = d * alpha; st.b2 = d * -alpha; break;
  88. case 3: st.b1 = st.a1; st.b0 = d; st.b2 = d; break;
  89. case 4: st.b1 = d * -(cs + 1.0); st.b0 = -st.b1 * 0.5; st.b2 = st.b0; break;
  90. default: st.b1 = st.a1; st.b0 = st.a2; st.b2 = 1.0; break;
  91. }
  92. return;
  93. }
  94. if (type == 6 || type == 8)
  95. {
  96. const double k = (A - 1.0) * cs;
  97. const double beta = alpha * sn;
  98. if (type == 6)
  99. {
  100. const double d = 1.0 / (A + 1.0 + k + beta);
  101. st.a1 = d * ((A - 1.0) + (A + 1.0) * cs) * -2.0;
  102. st.a2 = d * ((A + 1.0 + k) - beta);
  103. st.b1 = d * A * 2.0 * ((A - 1.0) - (A + 1.0) * cs);
  104. st.b0 = d * A * (((A + 1.0) - k) + beta);
  105. st.b2 = d * A * (((A + 1.0) - k) - beta);
  106. }
  107. else
  108. {
  109. const double d = 1.0 / (((A + 1.0) - k) + beta);
  110. st.a1 = d * ((A - 1.0) - (A + 1.0) * cs) * 2.0;
  111. st.a2 = d * (((A + 1.0) - k) - beta);
  112. st.b1 = d * A * -2.0 * ((A - 1.0) + (A + 1.0) * cs);
  113. st.b0 = d * A * (A + 1.0 + k + beta);
  114. st.b2 = d * A * ((A + 1.0 + k) - beta);
  115. }
  116. return;
  117. }
  118. if (type == 7)
  119. {
  120. const double d = 1.0 / (alpha / A + 1.0);
  121. st.a1 = d * cs * -2.0;
  122. st.a2 = d * (1.0 - alpha / A);
  123. st.b1 = st.a1;
  124. st.b0 = d * (alpha * A + 1.0);
  125. st.b2 = d * (1.0 - alpha * A);
  126. return;
  127. }
  128. st.b0 = 1.0, st.b1 = st.b2 = st.a1 = st.a2 = 0.0;
  129. }
  130. // ---------------------------------------------------------------------------------- "alternative design" (§4)
  131. // With HQ on and the design option 1, Parametric EQ 2 asks Frequency Splitter's engine for each stage's
  132. // coefficients: a magnitude-matched design (poles placed as in the analog prototype, zeros fitted so that the gain
  133. // at DC, at Nyquist and at the centre/corner frequency follows the analog response; no frequency warping).
  134. // Inputs as the plugin passes them: f = frequency / (host rate / 2), Q and gain in dB, each rounded to float.
  135. double sqrtPos (double x) { return x > 0.0 ? std::sqrt (x) : 0.0; }
  136. double minOf (double a, double b) { return a < b ? a : b; }
  137. double maxOf (double a, double b) { return a > b ? a : b; }
  138. constexpr double pi = 3.141592653589793;
  139. constexpr double halfPi = 1.5707963267948966;
  140. // the analog prototype's pole pair, sampled (impulse invariance): a1, a2
  141. void analogPoles (double f, double q, double& a1, double& a2)
  142. {
  143. const double decay = std::exp ((f * -halfPi) / q);
  144. const double d = 1.0 / ((q * 4.0) * q);
  145. const double c = q <= 0.5 ? std::cosh ((std::sqrt (d - 1.0) * pi) * f) : std::cos ((std::sqrt (1.0 - d) * pi) * f);
  146. a1 = (decay * c) * -2.0;
  147. a2 = decay * decay;
  148. }
  149. // common final step: numerator and denominator from their squared-magnitude terms (P0, P1 and the cross terms),
  150. // normalised so that a0 = 1
  151. void finishMatched (double pole0, double poleX, double zero0, double zeroX, double ampScale, Stage& st)
  152. {
  153. const double rp = sqrtPos (pole0);
  154. const double hp = (rp + 1.0) * 0.5;
  155. const double norm = 1.0 / (sqrtPos (hp * hp + poleX) + hp);
  156. const double rz = sqrtPos (zero0);
  157. const double hz = (rz + 1.0) * 0.5;
  158. const double n0 = sqrtPos (hz * hz + zeroX) + hz;
  159. const double gain = norm * ampScale;
  160. st.b0 = gain * n0;
  161. st.b1 = gain * (1.0 - rz);
  162. st.b2 = gain * -(zeroX / n0);
  163. st.a1 = norm * (1.0 - rp);
  164. st.a2 = norm * -(norm * poleX);
  165. }
  166. void matchedLowPass (double f, double q, Stage& st)
  167. {
  168. double k = 1.0 / (q * q);
  169. if (! (k < 1.99) && ! (k >= 2.0))
  170. k = 1.99;
  171. const double c0 = 0.6366197723675814 / f;
  172. const double nyq = (c0 * c0) * (k - 2.0);
  173. const double f2 = f * f, f4 = f2 * f2;
  174. const double fm1 = f2 - 1.0;
  175. const double den = ((k * f) * f) + fm1 * fm1;
  176. const double atNyq = f4 / den; // analog |H|² at the Nyquist frequency
  177. const double atNyqR = atNyq / (atNyq - 1.0);
  178. // general solution, fitted at min(f, 0.5)
  179. const double fm = minOf (f, 0.5);
  180. const double fmf = fm * f;
  181. const double dm = f2 - fm * fm;
  182. const double atM = f4 / (((fmf * fmf) * k) + dm * dm);
  183. const double atMR = atM / (atM - 1.0);
  184. const double sm = std::sin (fm * halfPi);
  185. const double pm = sm * sm;
  186. const double g1 = ((((atMR - atNyqR) * nyq) + (1.0 / pm)) - 1.0) / (atNyqR - (atMR * pm));
  187. const double g2 = (g1 + nyq) * atNyqR;
  188. double pole0 = (nyq - g2) + g1, poleX = g1 * -0.25, zero0 = -g2, zeroX = -0.0;
  189. // resonant solution (k < 2 and the peak below Nyquist), fitted at the resonance
  190. const double h = 1.0 - (k * 0.5);
  191. if (! (k >= 2.0) && ! (((h * f) * f) >= 1.0))
  192. {
  193. const double sr = std::sin (((sqrtPos (h) * f) * halfPi));
  194. const double pr = sr * sr;
  195. const double cr = 1.0 - pr;
  196. const double peak = 1.0 / (k - ((k * 0.25) * k));
  197. const double peakM1 = peak - 1.0;
  198. const double p0 = ((cr * peakM1) * cr) / (((peak - atNyq) * sr) * (pr * sr));
  199. const double z0 = (p0 / den) * f4;
  200. const double ipr2 = 1.0 / (pr * pr);
  201. const double px = ((((p0 - z0) - nyq) * 0.25) / peakM1) - (ipr2 * 0.25);
  202. pole0 = p0, poleX = px, zero0 = z0;
  203. zeroX = ((peakM1 * 0.25) * ipr2) + (px * peak);
  204. }
  205. finishMatched (pole0, poleX, zero0, zeroX, 1.0, st);
  206. }
  207. void matchedHighPass (double f, double q, Stage& st)
  208. {
  209. const double k = 1.0 / (q * q);
  210. const double f2 = f * f;
  211. const double fm1 = f2 - 1.0;
  212. const double kf = f2 * k;
  213. const double c5 = 0.16425571607494938 / (f2 * f2);
  214. const double nyq = ((fm1 * fm1) + kf) * c5;
  215. const double hk = k * 0.5;
  216. const double h = 1.0 - hk;
  217. // general solution, fitted at min(f, 0.5)
  218. const double fm = minOf (f, 0.5);
  219. const double fm2 = fm * fm;
  220. const double dm = f2 - fm2;
  221. const double sm = std::sin (fm * halfPi);
  222. const double pm = sm * sm;
  223. const double gm = ((((((((kf * fm) * fm) + (dm * dm)) * (pm * c5)) / (fm2 * fm2)) - nyq) * 0.25) / (1.0 - pm)) - (0.25 / pm);
  224. const double gn = -0.25 - (nyq * 0.25);
  225. double pole0, poleX;
  226. if (k >= 2.0)
  227. pole0 = nyq, poleX = gm;
  228. else if (f2 >= h)
  229. pole0 = nyq, poleX = ((gm - gn) * minOf (((fm1 + hk) * q), 1.0)) + gn;
  230. else
  231. {
  232. // resonant solution, fitted at the resonance
  233. const double sr = std::sin (((f / sqrtPos (h)) * halfPi));
  234. const double pr = sr * sr;
  235. const double peak = 1.0 / (1.0 / (k - ((k * 0.25) * k)));
  236. const double px = (-0.25 / (pr * pr)) - ((peak * c5) * 0.25);
  237. pole0 = ((((c5 + c5) * pr) * peak) + 1.0) - ((4.0 - (pr * 8.0)) * px);
  238. poleX = px;
  239. }
  240. const double rz = sqrtPos (c5);
  241. // numerator: (1 − z⁻¹)² scaled
  242. const double rp = sqrtPos (pole0);
  243. const double hp = (rp + 1.0) * 0.5;
  244. const double norm = 1.0 / (sqrtPos (hp * hp + poleX) + hp);
  245. const double n0 = rz * 0.5;
  246. st.b0 = norm * n0;
  247. st.b1 = norm * -rz;
  248. st.b2 = norm * ((c5 * 0.25) / n0);
  249. st.a1 = norm * (1.0 - rp);
  250. st.a2 = norm * -(norm * poleX);
  251. }
  252. void matchedPeak (double f, double q, double A, Stage& st)
  253. {
  254. if (std::abs (A - 1.0) < 0.001)
  255. {
  256. st.b0 = 1.0, st.b1 = st.b2 = st.a1 = st.a2 = 0.0;
  257. return;
  258. }
  259. const double q2 = q * q;
  260. const double bwRoot = sqrtPos ((q2 + 0.5) + sqrtPos (q2 + 0.25));
  261. const double iq2 = 1.0 / q2;
  262. const double f2 = f * f;
  263. const double fm1 = f2 - 1.0;
  264. const double fm1sq = fm1 * fm1;
  265. const double ef = ((f2 * A) * iq2) + fm1sq;
  266. const double fq = f2 * iq2;
  267. const double iA = 1.0 / A;
  268. double pole0, poleX, zero0, zeroX;
  269. if (f >= 1.0)
  270. {
  271. // centre at or above Nyquist: fitted at the band edge
  272. const double e = q / bwRoot;
  273. const double se = std::sin (e * halfPi);
  274. const double pe = se * se;
  275. const double e2 = e * e;
  276. const double ef2 = e2 * f2;
  277. const double de = f2 - e2;
  278. const double dsq = de * de;
  279. const double atE = ((((ef2 * A) * iq2) + dsq) / (((ef2 * iq2) * iA) + dsq));
  280. const double atN = ef / ((fq * iA) + fm1sq);
  281. pole0 = ((1.0 - pe) * (atE - 1.0)) / ((atN - atE) * pe);
  282. poleX = 0.0;
  283. zero0 = atN * pole0;
  284. zeroX = 0.0;
  285. }
  286. else
  287. {
  288. const double sc = std::sin (f * halfPi);
  289. const double pc = sc * sc;
  290. const double ipc = 1.0 / pc;
  291. const double se = std::sin ((((f * q) / bwRoot) * halfPi));
  292. const double pe = se * se;
  293. const double A2 = A * A;
  294. const double t29 = 1.0 - A2;
  295. const double t30 = t29 * ipc;
  296. const double t31 = ((((ipc * pe) - 2.0) * t30) + ((1.0 - A) / pe)) / (A - A2);
  297. const double atN = ef / ((fq / A) + fm1sq);
  298. const double x = (((((ipc - 2.0) * t30) + (1.0 - atN)) / (atN - A2)) - t31) / (1.0 - pe);
  299. const double t34 = t31 - (x * pe);
  300. const double t41 = (t29 / (pc * pc)) + (x * A2);
  301. pole0 = (t34 + 1.0) + x;
  302. poleX = x * -0.25;
  303. zero0 = ((((((A2 - 1.0) + A2) - 1.0) * ipc) + (t34 * A2)) + 1.0) + t41;
  304. zeroX = t41 * -0.25;
  305. }
  306. finishMatched (pole0, poleX, zero0, zeroX, 1.0, st);
  307. }
  308. // magnitude-squared ratio of the shelf's analog response at normalised frequency² m2 (zero term over pole term)
  309. double shelfRatio (double zeroBase, double poleBase, double m2, double q2, double scale)
  310. {
  311. const double dz = zeroBase - m2, dp = poleBase - m2;
  312. return ((((dz * dz) * q2) + (zeroBase * m2)) * scale) / (((dp * dp) * q2) + (poleBase * m2));
  313. }
  314. void matchedShelf (bool low, double f, double q, double A, Stage& st)
  315. {
  316. if (std::abs (A - 1.0) < 0.001)
  317. {
  318. st.b0 = 1.0, st.b1 = st.b2 = st.a1 = st.a2 = 0.0;
  319. return;
  320. }
  321. const double q2 = q * q;
  322. const double spread = maxOf (1.0 / A, A);
  323. const double s1 = sqrtPos (spread);
  324. const double s2 = spread > 0.0 ? sqrtPos (std::sqrt (spread)) : 0.0;
  325. const double x = low ? s2 * f : f / s2;
  326. const double base = low ? (x * x) / (s1 * s1) : x * x;
  327. const double lo = base / minOf (A, 1.0), hi = maxOf (A, 1.0) * base;
  328. const double P = low ? lo : hi, Z = low ? hi : lo; // pole / zero corner (squared)
  329. const double scale = low ? 1.0 / (A * A) : A * A;
  330. auto ratio = [&] (double m2) { return shelfRatio (Z, P, m2, q2, scale); };
  331. // analog-pole solution (Q² ≤ 0.6)
  332. const double iq2 = 1.0 / q2;
  333. const double wp = sqrtPos (P) * halfPi;
  334. const double decay = std::exp ((-1.0 / q) * wp);
  335. const double c4 = 4.0 - iq2;
  336. const double osc = q <= 0.5 ? std::cosh (wp * sqrtPos (iq2 - 4.0)) : (c4 > 0.0 ? std::cos (wp * std::sqrt (c4)) : std::cos (wp * 0.0));
  337. const double pa1 = (osc * decay) * -2.0, pa2 = decay * decay;
  338. const double sumN = (1.0 - pa1) + pa2;
  339. const double sumN2 = sumN * sumN;
  340. const double sumD = (pa1 + 1.0) + pa2;
  341. const double isumD2 = 1.0 / (sumD * sumD);
  342. // matched solution: fit points
  343. const double k2 = iq2 - 2.0;
  344. const double rr = -((s1 * s1 + 1.0) / (k2 * s1));
  345. const double r = rr - sqrtPos ((rr * rr) - 1.0);
  346. const double v = low ? (1.0 / s1) * r : s1 * r;
  347. const double y = x * sqrtPos (v);
  348. const double c2 = k2 * 0.4052847345693511;
  349. const double ends = ((0.4052847345693511 / P) - (0.4052847345693511 / Z)) * k2;
  350. const double m = minOf (low ? (1.0 / s1) * x : x, 0.5);
  351. const double fm = ratio (m * m);
  352. const double fmR = fm / (fm - 1.0);
  353. const double f1 = ratio (1.0);
  354. const double f1R = f1 / (f1 - 1.0);
  355. const double sm = std::sin (m * halfPi);
  356. const double pm = sm * sm;
  357. const double g1 = ((((fmR - f1R) * ends) + (1.0 / pm)) - 1.0) / (f1R - (fmR * pm));
  358. const double g2 = -((ends + g1) * f1R) - 1.0;
  359. const double sy = std::sin (y * halfPi);
  360. const double py = sy * sy;
  361. const double sr = std::sin (r * halfPi);
  362. const double ipr = 1.0 / (sr * sr);
  363. const double wv = low ? v : r / s1;
  364. const double zv = low ? maxOf (A, 1.0) * wv : wv / minOf (A, 1.0);
  365. const double pv = low ? wv / minOf (A, 1.0) : wv * maxOf (A, 1.0);
  366. const double fv1 = shelfRatio (zv, pv, 1.0, q2, scale);
  367. const double r2 = r * r;
  368. const double fvr = shelfRatio (zv, pv, r2, q2, scale);
  369. const double expo = std::log (((r * sqrtPos (A)) * ((((2.0 - (ipr + ipr)) * (fv1 - 1.0)) * (fvr - 1.0)) / (fv1 - fvr)))
  370. / ((low ? A - 1.0 : 1.0 - A) * c2))
  371. / std::log (r);
  372. const double h = std::pow (y, expo) * (((Z - P) * c2) / (P * Z));
  373. const double fy = ratio (y * y);
  374. const double fyM1 = fy - 1.0;
  375. const double u = (-2.0 / py) - (h / fyM1);
  376. const double yr = y / r;
  377. const double fyr = ratio (yr * yr);
  378. const double fyrM1 = fyr - 1.0;
  379. const double syr = std::sin (yr * halfPi);
  380. const double pyr = syr * syr;
  381. const double dyr = fyr - fy;
  382. const double w87 = (((py * fyrM1) - (fyM1 * pyr)) * -2.0) / ((py * dyr) * pyr);
  383. const double py2 = py * py;
  384. const double t89 = fyM1 / py2;
  385. const double t90 = u - h;
  386. const double t91 = (((((f1 + t89) - 1.0) - t90) + (f1 * u)) / (fy - f1));
  387. const double t92 = pyr * py;
  388. const double t93 = ((py2 * fyrM1) - ((pyr * pyr) * fyM1)) / ((t92 * t92) * dyr);
  389. const bool above = y >= r, atNyq = y >= 1.0;
  390. const double t94 = above ? (atNyq ? g1 : t91) : t93;
  391. const double t104 = above ? (atNyq ? 0.0 : (fy * t91) - t89) : (fy * t93) - t89;
  392. const double pz = (f1 * sumN2);
  393. double pole0, poleX, zero0, zeroX;
  394. if (q2 <= 0.6)
  395. {
  396. const double mz = minOf (low ? x / sqrtPos (s1) : x * sqrtPos (s1), 0.2);
  397. const double mz2 = mz * mz;
  398. const double fz = ratio (mz2);
  399. const double sz = std::sin (mz * halfPi);
  400. const double pz2 = sz * sz;
  401. pole0 = sumN2 * isumD2;
  402. poleX = (pa2 * -4.0) * isumD2;
  403. zero0 = pz * isumD2;
  404. zeroX = (((((((sumN2 * fz) - pz) / (1.0 - pz2)) + ((fz - 1.0) / (isumD2 * pz2))) * 0.25) + ((pa2 * -4.0) * fz)) * isumD2);
  405. }
  406. else
  407. {
  408. pole0 = ((above ? (atNyq ? (ends + g2) : u) : w87) + 1.0) + t94;
  409. poleX = t94 * -0.25;
  410. zero0 = ((above ? (atNyq ? g2 : t90) : (((fyM1 + fyM1) / py) + (fy * w87))) + 1.0) + t104;
  411. zeroX = t104 * -0.25;
  412. }
  413. finishMatched (pole0, poleX, zero0, zeroX, low ? A : 1.0, st);
  414. }
  415. // FL internal type → the engine's design (§4.1); q, gain as the plugin computes them per stage
  416. void designAlternative (Stage& st, int type, double freqHz, double q, double gain, double hostRate)
  417. {
  418. st.type = type;
  419. const double f = (double) (float) ((freqHz / hostRate) * 2.0);
  420. const double qq = (double) (float) q;
  421. const double A = std::pow (10.0, (double) (float) gain * 0.05);
  422. switch (type)
  423. {
  424. case 1: matchedLowPass (f, qq, st); break;
  425. case 4: matchedHighPass (f, qq, st); break;
  426. case 7: matchedPeak (f, qq, A, st); break;
  427. case 6: matchedShelf (true, f, qq, A, st); break;
  428. case 8: matchedShelf (false, f, qq, A, st); break;
  429. case 2: // band pass: analog poles, numerator fitted to the response at DC, Nyquist and f/2
  430. {
  431. double a1, a2;
  432. analogPoles (f, qq, a1, a2);
  433. const double s = std::sin (f * halfPi);
  434. const double p1 = s * s, p0 = 1.0 - p1;
  435. const double e1 = (1.0 - a1) + a2, n1 = e1 * e1;
  436. const double e0 = (a1 + 1.0) + a2, n0 = e0 * e0;
  437. const double n2 = a2 * -4.0;
  438. const double t14 = (((p0 - p1) * 4.0) * n2) + (n1 - n0);
  439. const double r1 = ((((p1 * n1) + (p0 * n0)) + (((p0 * 4.0) * p1) * n2)) - (t14 * p1)) / ((p1 * 4.0) * p1);
  440. const double t16 = (((p1 - p0) * 4.0) * r1) + t14;
  441. st.b1 = std::sqrt (t16) * -0.5;
  442. st.b0 = (std::sqrt ((t16 * 0.25) + r1) - st.b1) * 0.5;
  443. st.b2 = -st.b0 - st.b1;
  444. st.a1 = a1, st.a2 = a2;
  445. break;
  446. }
  447. case 3: // notch: analog poles, zeros on the unit circle at f, unity gain at DC
  448. {
  449. double a1, a2;
  450. analogPoles (f, qq, a1, a2);
  451. const double c = std::cos (f * pi) * -2.0;
  452. const double g = ((a1 + 1.0) + a2) / ((1.0 + c) + 1.0);
  453. st.b0 = 1.0 * g, st.b1 = g * c, st.b2 = g * 1.0;
  454. st.a1 = a1, st.a2 = a2;
  455. break;
  456. }
  457. default: // not handled by the engine: the plugin's stage keeps whatever the call left (not confirmed)
  458. st.b0 = 1.0, st.b1 = st.b2 = st.a1 = st.a2 = 0.0;
  459. break;
  460. }
  461. }
  462. // Parametric EQ 2 band design (§2.1–§2.2)
  463. void designPeq2 (const Settings& s, int b, double rate, Band& band)
  464. {
  465. const int ui = s.type[(std::size_t) b];
  466. const int t = ui >= 0 && ui < 8 ? internalType[ui] : 0;
  467. const auto freq = (float) (std::exp ((double) (float) s.freq[(std::size_t) b] * freqStep * ln2) * 20.0);
  468. const int order = std::clamp (s.order[(std::size_t) b], -3, 3);
  469. const int n = std::abs (order);
  470. const bool steep = order < 0;
  471. const auto inv = (float) (1.0 / (n + 1));
  472. const auto kbp = (float) ((double) ((float) std::exp ((double) (dbToLn * 3.0f)) / (float) std::exp ((double) (dbToLn * 6.0f)))
  473. * std::sqrt ((double) (n + 1)));
  474. const auto knotch = 1.0f / kbp;
  475. band.type = t;
  476. band.stages = 0;
  477. if (t < 1)
  478. {
  479. band.active = 0;
  480. return;
  481. }
  482. const auto w = (float) s.width[(std::size_t) b] * 1.52587890625e-05f;
  483. float gain = 0.0f;
  484. if (t < 6)
  485. band.active = -1;
  486. else
  487. {
  488. gain = (float) s.gain[(std::size_t) b] * 0.01f;
  489. band.active = gain == 0.0f ? (int) rate / 5 : -1;
  490. }
  491. const double wk = (1.0 / rate) * twoPi;
  492. const bool alternative = s.processing == Processing::hqAlternative;
  493. band.stages = n + 1;
  494. for (int k = 0; k <= n; ++k)
  495. {
  496. double A = 0.0, alpha = 0.0, sn = 0.0, cs = 0.0;
  497. // what the plugin hands to the alternative design: frequency, Q and gain of the stage (§4.1)
  498. double altFreq = (double) freq, altQ = 0.0, altGain = 0.0;
  499. if (t == 2 || t == 3)
  500. {
  501. const double om = std::min ((double) freq * wk, 3.138451099395752);
  502. sn = std::sin (om), cs = std::cos (om);
  503. const double bw = ((double) w * 4.9 + 0.1) * (double) (t == 2 ? kbp : knotch);
  504. alpha = sn * sinhRtl ((halfLn2 * bw * om) / sn);
  505. altQ = 1.0 / (sinhRtl (halfLn2 * bw) * 2.0);
  506. }
  507. else if (t == 1 || t == 4)
  508. {
  509. const double om = std::min ((double) freq * wk, 3.138451099395752);
  510. sn = std::sin (om), cs = std::cos (om);
  511. double q = (double) (1.0f - w);
  512. q = q * q * q * 9.9 + 0.1;
  513. if (n > 0)
  514. q = (double) powerF ((float) q, inv);
  515. if (steep)
  516. q = q * (double) steepCutQ[n][k];
  517. alpha = (sn * invSqrt2) / q;
  518. altQ = q * std::sqrt (0.5);
  519. }
  520. else if (t == 7)
  521. {
  522. double om = std::min ((double) freq * wk, 2.1991147994995117);
  523. double bw = (double) w * 4.9 + 0.1;
  524. double g;
  525. if (n < 1 || ! steep)
  526. g = (double) gain * (double) inv * 0.5;
  527. else
  528. {
  529. const double scale = steepPeakScale[n];
  530. const double shifted = (double) freq * std::exp ((double) steepPeakOffset[n][k] * bw * scale * ln2);
  531. bw = bw * scale * (double) steepPeakWidth[n][k];
  532. g = n == 1 ? (double) steepPeakGain[1] - (bw - 1.0) * 0.01 : (double) steepPeakGain[n];
  533. g = (double) gain * g;
  534. om = std::min ((double) (float) shifted * wk, 2.5132741928100586);
  535. altFreq = shifted;
  536. }
  537. sn = std::sin (om), cs = std::cos (om);
  538. A = (double) (float) std::exp ((double) ((float) g * dbToLn));
  539. alpha = sn * sinhRtl ((halfLn2 * bw * om) / sn);
  540. altQ = 1.0 / (sinhRtl (halfLn2 * bw) * 2.0);
  541. altGain = g * 2.0;
  542. }
  543. else // shelves (and the unused all-pass type)
  544. {
  545. const double om = std::min ((double) freq * wk, 3.138451099395752);
  546. sn = std::sin (om), cs = std::cos (om);
  547. A = (double) (float) std::exp ((double) (gain * inv * 0.5f * dbToLn));
  548. double S = (double) (1.0f - w) * 1.7192982456140353 + 0.3333333333333333;
  549. if (steep)
  550. S = S * (double) steepShelfS[n][k];
  551. alpha = std::sqrt (A) * std::sqrt ((A + 1.0 / A) * (1.0 / S - 1.0) + 2.0);
  552. altQ = 1.0 / std::sqrt ((A + 1.0 / A) * (1.0 / S - 1.0) + 2.0);
  553. altGain = (double) (gain * inv);
  554. }
  555. if (alternative)
  556. designAlternative (band.stage[(std::size_t) k], t, altFreq, altQ, altGain, rate);
  557. else
  558. designStage (band.stage[(std::size_t) k], t, A, alpha, sn, cs);
  559. }
  560. }
  561. // Parametric EQ (v1): one biquad per band (§3)
  562. void designPeq1 (const Settings& s, int b, double rate, Band& band)
  563. {
  564. const int ui = s.type[(std::size_t) b];
  565. const int t = ui >= 0 && ui < 8 ? internalType[ui] : 0;
  566. band.type = t;
  567. band.stages = 0;
  568. if (t < 1)
  569. {
  570. band.active = 0;
  571. return;
  572. }
  573. const auto x = (float) ((double) s.freq[(std::size_t) b] * 1.52587890625e-05);
  574. const auto e = (float) (std::exp ((double) x * std::log (101.0)) - 1.0);
  575. auto freq = (float) ((double) e * 159.9 + 10.0);
  576. const auto w = (float) ((double) s.width[(std::size_t) b] * 1.52587890625e-05);
  577. const auto gain = (float) s.gain[(std::size_t) b] * 0.01f;
  578. band.active = gain == 0.0f && t > 5 ? (int) rate / 5 : -1;
  579. const auto nyquist = (float) ((double) (int) rate * 0.4999); // §3.2: the rate object's limit
  580. if (freq > nyquist)
  581. freq = nyquist;
  582. const double om = (double) freq * ((1.0 / rate) * twoPi);
  583. const double sn = std::sin (om), cs = std::cos (om);
  584. double A = 0.0, alpha;
  585. if (t >= 1 && t <= 5)
  586. alpha = sn / (((double) w * 18.9 + 0.1) * 2.0);
  587. else
  588. {
  589. const double ge = (double) gain * 0.025;
  590. A = ge == 0.0 ? 1.0 : std::exp (ge * std::log (10.0));
  591. if (t == 7)
  592. {
  593. const auto q = (float) (std::exp ((double) w * std::log ((double) (1000.0f + 1.0f))) - 1.0);
  594. alpha = sn / (((double) q * 0.0999 + 0.1) * 2.0);
  595. }
  596. else
  597. alpha = std::sqrt ((A * A + 1.0) / ((double) w * 1.9 + 0.1) - (A - 1.0) * (A - 1.0));
  598. }
  599. band.stages = 1;
  600. designStage (band.stage[0], t, A, alpha, sn, cs);
  601. }
  602. void runStage (Stage& st, float* io, int n) noexcept
  603. {
  604. double x1l = st.s[0], x1r = st.s[1], x2l = st.s[2], x2r = st.s[3];
  605. double y1l = st.s[4], y1r = st.s[5], y2l = st.s[6], y2r = st.s[7];
  606. const double b0 = st.b0, b1 = st.b1, b2 = st.b2, a1 = st.a1, a2 = st.a2;
  607. for (int i = 0; i < n; ++i)
  608. {
  609. const double xl = io[2 * i], xr = io[2 * i + 1];
  610. const double yl = xl * b0 + (((x1l * b1 - y2l * a2) + x2l * b2) - y1l * a1);
  611. const double yr = xr * b0 + (((x1r * b1 - y2r * a2) + x2r * b2) - y1r * a1);
  612. x2l = x1l, x2r = x1r, x1l = xl, x1r = xr;
  613. y2l = y1l, y2r = y1r, y1l = yl, y1r = yr;
  614. io[2 * i] = (float) yl;
  615. io[2 * i + 1] = (float) yr;
  616. }
  617. const double out[8] = { x1l, x1r, x2l, x2r, y1l, y1r, y2l, y2r };
  618. // end of call: state values below 2^-24 in magnitude are cleared (§2.4)
  619. for (int k = 0; k < 8; ++k)
  620. st.s[k] = std::abs (out[k]) < (double) flushBelow ? 0.0 : out[k];
  621. }
  622. void clearStages (Band& band) noexcept
  623. {
  624. for (auto& st : band.stage)
  625. std::fill (std::begin (st.s), std::end (st.s), 0.0);
  626. }
  627. // 2:1 polyphase half-band (§2.7): states per channel pair: outputs A1 A2 A3 B1 B2 B3 (L,R), then input delays
  628. constexpr double hbA[3] = { 0.14737711360104661, 0.48246854276970014, 0.88300502576937312 };
  629. constexpr double hbB[3] = { 0.039151597734460045, 0.30264684832849342, 0.6746159185469639 };
  630. void flushHalfBand (double* s) noexcept
  631. {
  632. for (int k = 0; k < 12; ++k)
  633. if (std::abs (s[k]) < (double) flushBelow)
  634. s[k] = 0.0;
  635. }
  636. void upsample (double* s, const float* in, float* out, int n) noexcept
  637. {
  638. for (int i = 0; i < n; ++i)
  639. for (int c = 0; c < 2; ++c)
  640. {
  641. double* o = s + c; // outputs at o[0], o[2], …, o[10]
  642. double* d = s + 12 + c; // input delays
  643. const double v = in[2 * i + c];
  644. const double a1 = d[0] + (v - o[0]) * hbA[0];
  645. d[0] = v, o[0] = a1;
  646. const double b1 = d[6] + (v - o[6]) * hbB[0];
  647. d[6] = v, o[6] = b1;
  648. const double a2 = d[2] + (a1 - o[2]) * hbA[1];
  649. d[2] = a1, o[2] = a2;
  650. const double b2 = d[8] + (b1 - o[8]) * hbB[1];
  651. d[8] = b1, o[8] = b2;
  652. const double a3 = d[4] + (a2 - o[4]) * hbA[2];
  653. o[4] = a3;
  654. const double b3 = d[10] + (b2 - o[10]) * hbB[2];
  655. o[10] = b3;
  656. d[4] = a2, d[10] = b2;
  657. out[4 * i + c] = (float) b3;
  658. out[4 * i + 2 + c] = (float) a3;
  659. }
  660. flushHalfBand (s);
  661. }
  662. void downsample (double* s, const float* in, float* out, int n) noexcept
  663. {
  664. for (int i = 0; i < n; ++i)
  665. for (int c = 0; c < 2; ++c)
  666. {
  667. double* o = s + c;
  668. double* d = s + 12 + c;
  669. const double e = in[4 * i + c], od = in[4 * i + 2 + c];
  670. const double a1 = d[0] + (e - o[0]) * hbA[0];
  671. d[0] = e, o[0] = a1;
  672. const double b1 = d[6] + (od - o[6]) * hbB[0];
  673. d[6] = od, o[6] = b1;
  674. const double a2 = d[2] + (a1 - o[2]) * hbA[1];
  675. d[2] = a1, o[2] = a2;
  676. const double b2 = d[8] + (b1 - o[8]) * hbB[1];
  677. d[8] = b1, o[8] = b2;
  678. const double a3 = d[4] + (a2 - o[4]) * hbA[2];
  679. d[4] = a2, o[4] = a3;
  680. const double b3 = d[10] + (b2 - o[10]) * hbB[2];
  681. d[10] = b2, o[10] = b3;
  682. out[2 * i + c] = (float) ((b3 + a3) * 0.5);
  683. }
  684. flushHalfBand (s);
  685. }
  686. // Frequency Splitter's 2:1 decimator (§2.9): 176-tap half-band windowed sinc, newest sample first
  687. const std::array<float, 176>& decimatorKernel()
  688. {
  689. static const auto k = [] {
  690. std::array<float, 176> h {};
  691. for (int i = 0; i < 176; ++i)
  692. {
  693. const double x = ((double) i - 89.0) * 1.5707963267948966;
  694. const double sinc = x != 0.0 ? std::sin (x) / x : 1.0;
  695. h[(std::size_t) i] = (float) (std::sin ((double) i * 0.017849958259032916) * (double) (float) (sinc * 0.5));
  696. }
  697. return h;
  698. }();
  699. return k;
  700. }
  701. // Σ buf[from + j]·h[k0 + j] over count values: four running float sums over groups of four, folded into acc as
  702. // (((s0 + acc) + s1) + s2) + s3, then the remainder one by one (§2.9)
  703. float convolve (const float* buf, const float* h, int count, float acc) noexcept
  704. {
  705. if (count < 0)
  706. return acc;
  707. float s[4] = { 0, 0, 0, 0 };
  708. const int groups = count >> 2;
  709. for (int g = 0; g < groups; ++g)
  710. for (int j = 0; j < 4; ++j)
  711. s[j] += buf[4 * g + j] * h[4 * g + j];
  712. acc = (((s[0] + acc) + s[1]) + s[2]) + s[3];
  713. for (int j = groups * 4; j < count; ++j)
  714. acc = buf[j] * h[j] + acc;
  715. return acc;
  716. }
  717. float decimate (float* ring, int& index, float even, float odd) noexcept
  718. {
  719. const auto& h = decimatorKernel();
  720. ring[index] = even;
  721. index = (index - 1) & 255;
  722. ring[index] = odd;
  723. const int start = index;
  724. int end = start + 176, wrap = -1;
  725. if (end >= 256)
  726. wrap = end - 256, end = 256;
  727. float acc = convolve (ring + start, h.data(), end - start, 0.0f);
  728. acc = convolve (ring, h.data() + (end - start), wrap, acc);
  729. index = (index - 1) & 255;
  730. return acc;
  731. }
  732. bool oversampled (const Settings& s) noexcept
  733. {
  734. return s.law == Law::peq2 && (s.processing == Processing::hq || s.processing == Processing::hqExternal);
  735. }
  736. float peq2MainGain (int v) noexcept
  737. {
  738. return (float) std::exp ((double) ((float) v * 0.01f * dbToLn));
  739. }
  740. float peq1MainGain (int v) noexcept
  741. {
  742. const float x = (float) v * 0.0078125f;
  743. if (x == 1.0f)
  744. return 1.0f;
  745. return (float) ((std::exp ((double) (x * 2.39789534f)) - 1.0) * (double) 0.1f);
  746. }
  747. float raw01 (int v, double range) { return (float) ((double) v / range); }
  748. int fromRaw01 (float p, double range) { return (int) std::lround ((double) p * range); }
  749. } // namespace
  750. // ------------------------------------------------------------------------------------------------------- states
  751. std::optional<Settings> parsePeq2 (const std::vector<std::uint8_t>& d)
  752. {
  753. // §1: version, 36 parameters, A/B copy, UI flags; optional fields by version
  754. if (d.size() < 0x12D)
  755. return std::nullopt;
  756. Settings s;
  757. s.law = Law::peq2;
  758. const auto version = i32At (d, 0);
  759. auto p = [&] (int i) { return i32At (d, 4 + 4 * (std::size_t) i); };
  760. for (int b = 0; b < 7; ++b)
  761. {
  762. s.gain[(std::size_t) b] = p (b);
  763. s.freq[(std::size_t) b] = p (7 + b);
  764. s.width[(std::size_t) b] = p (14 + b);
  765. s.type[(std::size_t) b] = p (21 + b);
  766. s.order[(std::size_t) b] = p (28 + b);
  767. }
  768. s.main = p (35);
  769. const auto ui = (std::uint32_t) i32At (d, 0x124);
  770. const bool hq = (ui & 1) != 0;
  771. const auto design = (std::uint8_t) (ui >> 1);
  772. std::uint8_t options = 0;
  773. if (version >= 4 && d.size() >= 0x142)
  774. {
  775. options = d[0x13D];
  776. s.mask = (std::uint32_t) i32At (d, 0x13E) & 0x7F;
  777. }
  778. s.linearPhase = (options & 1) != 0;
  779. const auto lin = parseLinearOptions (d);
  780. s.linearQuality = lin.quality, s.linearFade = lin.smoothing;
  781. if (hq && design == 0)
  782. s.processing = (options & 2) != 0 ? Processing::hqExternal : Processing::hq;
  783. else if (hq && design == 1)
  784. s.processing = Processing::hqAlternative;
  785. return s;
  786. }
  787. std::optional<Settings> parsePeq1 (const std::vector<std::uint8_t>& d)
  788. {
  789. // §3.1: 29 parameters (7 gains, 7 frequencies, 7 widths, 7 types, main), no version
  790. if (d.size() < 4 * 28)
  791. return std::nullopt;
  792. Settings s;
  793. s.law = Law::peq1;
  794. auto p = [&] (int i) { return i32At (d, 4 * (std::size_t) i); };
  795. for (int b = 0; b < 7; ++b)
  796. {
  797. s.gain[(std::size_t) b] = p (b);
  798. s.freq[(std::size_t) b] = p (7 + b);
  799. s.width[(std::size_t) b] = p (14 + b);
  800. s.type[(std::size_t) b] = p (21 + b);
  801. }
  802. s.main = d.size() >= 4 * 29 ? p (28) : 128;
  803. return s;
  804. }
  805. Law lawOf (const EffectParams& params) noexcept
  806. {
  807. const auto v = (int) std::lround (params[(std::size_t) lawParam] * 2.0f);
  808. return v == 1 ? Law::peq2 : v == 2 ? Law::peq1 : Law::lynx;
  809. }
  810. namespace
  811. {
  812. // LYNX's own frequency value of a v1 frequency (the editor's 20 Hz…20 kHz law)
  813. float lynxHzValue (double hz) { return (float) std::clamp (std::log (std::max (hz, 1.0) / 20.0) / std::log (1000.0), 0.0, 1.0); }
  814. double peq1Hz (int raw)
  815. {
  816. const auto x = (float) ((double) raw * 1.52587890625e-05);
  817. const auto e = (float) (std::exp ((double) x * std::log (101.0)) - 1.0);
  818. return (double) (float) ((double) e * 159.9 + 10.0);
  819. }
  820. double peq1MainDb (int raw) { return 20.0 * std::log10 (std::max ((double) peq1MainGain (raw), 1.0e-6)); }
  821. float gainParam (double db) { return (float) std::clamp (db / 36.0 + 0.5, 0.0, 1.0); }
  822. // FL UI type ↔ LYNX band type (core/Effects.h peq::BandType order: off, peak, lowShelf, highShelf, lowCut, highCut,
  823. // bandPass, notch)
  824. constexpr int lynxOfFl[8] = { 0, 5, 6, 4, 7, 2, 1, 3 };
  825. constexpr int flOfLynx[8] = { 0, 6, 5, 7, 3, 1, 2, 4 };
  826. } // namespace
  827. void toParams (const Settings& s, EffectParams& params) noexcept
  828. {
  829. for (int b = 0; b < 7; ++b)
  830. {
  831. const auto ub = (std::size_t) b;
  832. const int ui = s.type[ub] >= 0 && s.type[ub] < 8 ? s.type[ub] : 0;
  833. params[(std::size_t) (4 * b)] = (float) lynxOfFl[ui] / 7.0f;
  834. params[(std::size_t) (4 * b + 2)] = std::clamp ((float) ((double) s.gain[ub] / 3600.0 + 0.5), 0.0f, 1.0f);
  835. params[(std::size_t) (4 * b + 3)] = std::clamp (raw01 (s.width[ub], 65536.0), 0.0f, 1.0f);
  836. params[(std::size_t) (29 + b)] = (float) (std::clamp (s.order[ub], -3, 3) + 3) / 6.0f;
  837. if (s.law == Law::peq2)
  838. params[(std::size_t) (4 * b + 1)] = std::clamp (raw01 (s.freq[ub], 65536.0), 0.0f, 1.0f);
  839. else
  840. {
  841. params[(std::size_t) (v1FreqParam + b)] = std::clamp (raw01 (s.freq[ub], 65536.0), 0.0f, 1.0f);
  842. params[(std::size_t) (4 * b + 1)] = lynxHzValue (peq1Hz (s.freq[ub]));
  843. }
  844. }
  845. if (s.law == Law::peq2)
  846. {
  847. params[28] = std::clamp ((float) ((double) s.main / 3600.0 + 0.5), 0.0f, 1.0f);
  848. params[(std::size_t) linearQualityParam] = (float) std::clamp (s.linearQuality, 0, 7) / 7.0f;
  849. params[(std::size_t) linearFadeParam] = (float) std::clamp (s.linearFade, 0, 3) / 3.0f;
  850. }
  851. else
  852. {
  853. params[(std::size_t) v1MainParam] = std::clamp (raw01 (s.main, 256.0), 0.0f, 1.0f);
  854. params[28] = gainParam (peq1MainDb (s.main));
  855. }
  856. params[(std::size_t) lawParam] = s.law == Law::peq1 ? 1.0f : 0.5f;
  857. params[(std::size_t) processingParam] = (float) (int) s.processing / 3.0f;
  858. params[(std::size_t) linearParam] = s.linearPhase ? 1.0f : 0.0f;
  859. params[(std::size_t) maskParam] = (float) (s.mask & 0x7F) / 127.0f;
  860. }
  861. Settings fromParams (const EffectParams& params) noexcept
  862. {
  863. Settings s;
  864. s.law = lawOf (params) == Law::peq1 ? Law::peq1 : Law::peq2;
  865. for (int b = 0; b < 7; ++b)
  866. {
  867. const auto ub = (std::size_t) b;
  868. const int lt = std::clamp ((int) std::lround (params[(std::size_t) (4 * b)] * 7.0f), 0, 7);
  869. s.type[ub] = flOfLynx[lt];
  870. s.gain[ub] = (int) std::lround (((double) params[(std::size_t) (4 * b + 2)] - 0.5) * 3600.0);
  871. s.width[ub] = fromRaw01 (params[(std::size_t) (4 * b + 3)], 65536.0);
  872. s.order[ub] = std::clamp ((int) std::lround (params[(std::size_t) (29 + b)] * 6.0f) - 3, -3, 3);
  873. const auto f = params[(std::size_t) (4 * b + 1)];
  874. if (s.law == Law::peq2)
  875. s.freq[ub] = fromRaw01 (f, 65536.0);
  876. else
  877. {
  878. // FL's own value while the band has not been moved in the editor; else the nearest v1 value
  879. const int raw = fromRaw01 (params[(std::size_t) (v1FreqParam + b)], 65536.0);
  880. if (lynxHzValue (peq1Hz (raw)) == f)
  881. s.freq[ub] = raw;
  882. else
  883. {
  884. const double hz = 20.0 * std::pow (1000.0, std::clamp ((double) f, 0.0, 1.0));
  885. const double x = std::log ((std::clamp (hz, 10.0, 16000.0) - 10.0) / 159.9 + 1.0) / std::log (101.0);
  886. s.freq[ub] = std::clamp ((int) std::lround (x * 65536.0), 0, 65536);
  887. }
  888. s.order[ub] = 0;
  889. }
  890. }
  891. if (s.law == Law::peq2)
  892. {
  893. s.main = (int) std::lround (((double) params[28] - 0.5) * 3600.0);
  894. s.linearQuality = (int) std::lround (params[(std::size_t) linearQualityParam] * 7.0f);
  895. s.linearFade = (int) std::lround (params[(std::size_t) linearFadeParam] * 3.0f);
  896. }
  897. else
  898. {
  899. const int raw = fromRaw01 (params[(std::size_t) v1MainParam], 256.0);
  900. if (gainParam (peq1MainDb (raw)) == params[28])
  901. s.main = raw;
  902. else
  903. {
  904. // nearest raw level to the edited dB value: gain = (11^(raw/128) − 1)/10
  905. const double g = std::pow (10.0, ((double) params[28] - 0.5) * 36.0 / 20.0);
  906. s.main = std::clamp ((int) std::lround (128.0 * std::log (g * 10.0 + 1.0) / std::log (11.0)), 0, 256);
  907. }
  908. }
  909. s.processing = (Processing) std::clamp ((int) std::lround (params[(std::size_t) processingParam] * 3.0f), 0, 3);
  910. s.linearPhase = params[(std::size_t) linearParam] >= 0.5f;
  911. s.mask = (std::uint32_t) std::clamp ((int) std::lround (params[(std::size_t) maskParam] * 127.0f), 0, 127);
  912. return s;
  913. }
  914. void designBand (const Settings& s, int b, double rate, Band& band)
  915. {
  916. if (s.law == Law::peq1)
  917. designPeq1 (s, b, rate, band);
  918. else
  919. designPeq2 (s, b, rate, band);
  920. }
  921. double responseDb (const Settings& s, double sr, double hz)
  922. {
  923. const double rate = oversampled (s) ? sr * 2.0 : sr;
  924. const auto w = twoPi * std::clamp (hz, 1.0, sr * 0.499) / rate;
  925. const std::complex<double> z1 = std::polar (1.0, -w), z2 = std::polar (1.0, -2.0 * w);
  926. double total = 20.0 * std::log10 (std::max ((double) (s.law == Law::peq1 ? peq1MainGain (s.main) : peq2MainGain (s.main)), 1.0e-9));
  927. for (int b = 0; b < 7; ++b)
  928. {
  929. if (s.law == Law::peq2 && (s.mask & (1u << b)) == 0)
  930. continue;
  931. Band band;
  932. designBand (s, b, rate, band);
  933. if (band.active == 0)
  934. continue;
  935. for (int k = 0; k < band.stages; ++k)
  936. {
  937. const auto& c = band.stage[(std::size_t) k];
  938. const auto h = (c.b0 + c.b1 * z1 + c.b2 * z2) / (1.0 + c.a1 * z1 + c.a2 * z2);
  939. total += 20.0 * std::log10 (std::max (std::abs (h), 1.0e-9));
  940. }
  941. }
  942. return total;
  943. }
  944. // ------------------------------------------------------------------------------------------------------- engine
  945. Engine::Engine() = default;
  946. Engine::~Engine() = default;
  947. namespace
  948. {
  949. bool linearMode (const Settings& s) noexcept { return s.law == Law::peq2 && s.linearPhase; }
  950. } // namespace
  951. double Engine::processingRate() const noexcept
  952. {
  953. return oversampled (settings) ? sampleRate * 2.0 : sampleRate;
  954. }
  955. void Engine::prepare (double sr) noexcept
  956. {
  957. sampleRate = sr;
  958. silenceFrames = (int) sr / 5;
  959. buffer.assign (2 * 512, 0.0f);
  960. wide.assign (4 * 512, 0.0f);
  961. if (configured)
  962. for (int b = 0; b < 7; ++b)
  963. designBand (settings, b, processingRate(), bands[(std::size_t) b]);
  964. if (linear != nullptr)
  965. {
  966. linear->prepare ((int) sr);
  967. linear->set (settings);
  968. }
  969. reset();
  970. }
  971. void Engine::reset() noexcept
  972. {
  973. for (auto& band : bands)
  974. clearStages (band);
  975. std::fill (std::begin (up), std::end (up), 0.0);
  976. std::fill (std::begin (down), std::end (down), 0.0);
  977. std::fill (&ring[0][0], &ring[0][0] + 512, 0.0f);
  978. ringIndex[0] = ringIndex[1] = 0;
  979. silenceCount = 0;
  980. gainSnap = true;
  981. if (linear != nullptr)
  982. linear->reset();
  983. if (configured)
  984. for (int b = 0; b < 7; ++b)
  985. designBand (settings, b, processingRate(), bands[(std::size_t) b]); // restarts the 0 dB countdowns
  986. }
  987. void Engine::set (const Settings& s) noexcept
  988. {
  989. const bool rateChange = ! configured || oversampled (s) != oversampled (settings);
  990. const auto old = settings;
  991. settings = s;
  992. mainGain = s.law == Law::peq1 ? peq1MainGain (s.main) : peq2MainGain (s.main);
  993. for (int b = 0; b < 7; ++b)
  994. {
  995. auto& band = bands[(std::size_t) b];
  996. const auto ub = (std::size_t) b;
  997. const bool same = configured && old.law == s.law && old.gain[ub] == s.gain[ub] && old.freq[ub] == s.freq[ub]
  998. && old.width[ub] == s.width[ub] && old.type[ub] == s.type[ub] && old.order[ub] == s.order[ub];
  999. if (same && ! rateChange)
  1000. continue;
  1001. if (! configured || old.type[ub] != s.type[ub] || old.order[ub] != s.order[ub])
  1002. clearStages (band); // FL clears a band's filters when its type or order changes
  1003. designBand (s, b, processingRate(), band);
  1004. }
  1005. if (rateChange)
  1006. {
  1007. std::fill (std::begin (up), std::end (up), 0.0);
  1008. std::fill (std::begin (down), std::end (down), 0.0);
  1009. }
  1010. if (linearMode (s))
  1011. {
  1012. // the FIR is rebuilt from the new response and cross-faded in (§5); allocates on the first use
  1013. const bool fresh = linear == nullptr;
  1014. if (fresh)
  1015. linear = std::make_unique<LinearPhase>();
  1016. linear->setOptions ({ s.linearQuality, s.linearFade });
  1017. if (fresh)
  1018. linear->prepare ((int) sampleRate);
  1019. linear->set (s);
  1020. }
  1021. else
  1022. linear.reset();
  1023. configured = true;
  1024. }
  1025. void Engine::flushFilters() noexcept
  1026. {
  1027. // FL's silence reset: only bands still counting down (0 dB) lose their state; the resamplers are cleared
  1028. for (auto& band : bands)
  1029. if (band.active > 0)
  1030. clearStages (band);
  1031. std::fill (std::begin (up), std::end (up), 0.0);
  1032. std::fill (std::begin (down), std::end (down), 0.0);
  1033. gainSnap = true;
  1034. }
  1035. void Engine::block (float* io, int n) noexcept
  1036. {
  1037. if (settings.law == Law::peq2)
  1038. {
  1039. // silence gate (§2.8): a block whose samples are all within ±1e-6 counts towards rate/5 frames of silence
  1040. bool silent = true;
  1041. for (int i = 0; i < 2 * n && silent; ++i)
  1042. silent = std::abs (io[i]) <= 9.99999997e-07f;
  1043. if (! silent || settings.linearPhase)
  1044. silenceCount = silenceFrames;
  1045. else
  1046. silenceCount -= n;
  1047. if (silenceCount <= -n)
  1048. {
  1049. if (silenceCount > -0x3FFFFFFF)
  1050. flushFilters();
  1051. silenceCount = (int) 0xC0000001;
  1052. return; // passes the input unchanged
  1053. }
  1054. }
  1055. if (linear != nullptr && linearMode (settings))
  1056. {
  1057. linear->process (io, n); // main level, then the linear-phase FIR (§5)
  1058. return;
  1059. }
  1060. if (settings.law == Law::peq2)
  1061. {
  1062. // main level with FL's glide (§2.5); the step limit follows the processing rate
  1063. const auto maxStep = (float) (44100.0 / processingRate() * 0.001);
  1064. mainLevel (io, n, mainGain, gainNow, maxStep, gainSnap);
  1065. gainSnap = false;
  1066. }
  1067. else if (mainGain == 0.0f)
  1068. std::fill (io, io + 2 * n, 0.0f);
  1069. else if (mainGain != 1.0f)
  1070. for (int i = 0; i < 2 * n; ++i)
  1071. io[i] *= mainGain;
  1072. float* x = io;
  1073. int m = n;
  1074. const bool os = oversampled (settings);
  1075. const bool external = os && settings.processing == Processing::hqExternal;
  1076. if (external)
  1077. {
  1078. // Frequency Splitter's 1:2 up-sampler: each sample doubled, then a zero (§2.9)
  1079. for (int i = 0; i < n; ++i)
  1080. {
  1081. wide[(std::size_t) (4 * i)] = io[2 * i] + io[2 * i];
  1082. wide[(std::size_t) (4 * i + 1)] = io[2 * i + 1] + io[2 * i + 1];
  1083. wide[(std::size_t) (4 * i + 2)] = 0.0f;
  1084. wide[(std::size_t) (4 * i + 3)] = 0.0f;
  1085. }
  1086. x = wide.data();
  1087. m = 2 * n;
  1088. }
  1089. else if (os)
  1090. {
  1091. upsample (up, io, wide.data(), n);
  1092. x = wide.data();
  1093. m = 2 * n;
  1094. }
  1095. for (int b = 0; b < 7; ++b)
  1096. {
  1097. auto& band = bands[(std::size_t) b];
  1098. if (band.active == 0 || (settings.law == Law::peq2 && (settings.mask & (1u << b)) == 0))
  1099. continue;
  1100. for (int k = 0; k < band.stages; ++k)
  1101. runStage (band.stage[(std::size_t) k], x, m);
  1102. if (band.active > 0 && (band.active -= m) < 1)
  1103. {
  1104. band.active = 0;
  1105. clearStages (band);
  1106. }
  1107. }
  1108. if (external)
  1109. for (int i = 0; i < n; ++i)
  1110. for (int c = 0; c < 2; ++c)
  1111. io[2 * i + c] = decimate (ring[c], ringIndex[c], wide[(std::size_t) (4 * i + c)], wide[(std::size_t) (4 * i + 2 + c)]);
  1112. else if (os)
  1113. downsample (down, wide.data(), io, n);
  1114. }
  1115. int Engine::latencySamples() const noexcept
  1116. {
  1117. if (! configured)
  1118. return 0;
  1119. if (linear != nullptr && linearMode (settings))
  1120. return linear->latencySamples();
  1121. return oversampled (settings) && settings.processing == Processing::hqExternal ? 44 : 0;
  1122. }
  1123. void Engine::process (float* left, float* right, int n) noexcept
  1124. {
  1125. if (! configured)
  1126. return;
  1127. for (int done = 0; done < n;)
  1128. {
  1129. const int len = std::min (512, n - done);
  1130. for (int i = 0; i < len; ++i)
  1131. {
  1132. buffer[(std::size_t) (2 * i)] = left[done + i];
  1133. buffer[(std::size_t) (2 * i + 1)] = right[done + i];
  1134. }
  1135. block (buffer.data(), len);
  1136. for (int i = 0; i < len; ++i)
  1137. {
  1138. left[done + i] = buffer[(std::size_t) (2 * i)];
  1139. right[done + i] = buffer[(std::size_t) (2 * i + 1)];
  1140. }
  1141. done += len;
  1142. }
  1143. }
  1144. } // namespace lynx::peqfl