MultibandDelayFx.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. #include "core/StateHandoff.h"
  2. #include "core/MultibandDelayFx.h"
  3. #include "core/Effects.h"
  4. #include "core/IldspMath.h"
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <cstdio>
  8. #include <cstring>
  9. #if defined(__SSE2__) || defined(_M_X64) || defined(__x86_64__)
  10. #include "core/Simd.h"
  11. #define LYNX_MBD_MXCSR 1
  12. #endif
  13. // The plug-in's float arithmetic is reproduced operation by operation (REA_rodada71 §3): no contraction into FMA,
  14. // and the render runs with the plug-in's own SSE mode (flush to zero, denormals as zero, rounding toward zero).
  15. #if defined(__clang__)
  16. #pragma clang fp contract(off)
  17. #pragma STDC FENV_ACCESS ON
  18. #elif defined(__GNUC__)
  19. #pragma GCC optimize("fp-contract=off", "rounding-math")
  20. #endif
  21. namespace lynx::mbdelay
  22. {
  23. namespace
  24. {
  25. // ------------------------------------------------------------------------------------------------ front end
  26. struct Range
  27. {
  28. int min, max;
  29. };
  30. // the twelve global controls (engine parameters 384…395), FL's control ranges
  31. constexpr Range globalRange[numGlobals] = { { -6000, 6000 }, { 0, 1 }, { 0, 6000 }, { 0, 6000 }, { 0, 2 }, { 0, 1 },
  32. { 0, 7000 }, { -128, 128 }, { 0, 1 }, { 0, 1 }, { 1000, 100000 }, { 0, 2 } };
  33. // a control's value when a state does not store it (FL's editor defaults)
  34. constexpr int globalDefault[numGlobals] = { 0, 0, 0, 1500, 0, 0, 0, 128, 0, 0, 1000, 0 };
  35. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t o) noexcept
  36. {
  37. std::int32_t v;
  38. std::memcpy (&v, d.data() + o, 4);
  39. return v;
  40. }
  41. void putI32 (std::vector<std::uint8_t>& d, std::int32_t v)
  42. {
  43. std::uint8_t b[4];
  44. std::memcpy (b, &v, 4);
  45. d.insert (d.end(), b, b + 4);
  46. }
  47. // ------------------------------------------------------------------------------------------------ math
  48. // the plug-in's CRT tanf for |x| ≤ π/4 (the high pass never leaves that range at audio rates)
  49. float tanfCrt (float x) noexcept
  50. {
  51. const double xd = (double) x, ax = std::fabs (xd);
  52. if (! std::isfinite (xd) || ax > 0.7853981633974483)
  53. return (float) std::tan (xd);
  54. if (ax < 7.450580596923828e-09)
  55. return x;
  56. if (ax < 0.0001220703125)
  57. return (float) (((xd * xd) * xd) * 0.3333333333333333 + xd);
  58. const double x2 = xd * xd;
  59. const double num = -0.017203248047148168 * x2 + 0.3852960712639954;
  60. const double den = (0.01844239256901656 * x2 + -0.5139650547885454) * x2 + 1.1558882143468838;
  61. return (float) (xd + (x2 * xd) * (num / den));
  62. }
  63. // the plug-in's CRT tanhf (single precision throughout). Its exp table 2^(j/32) as a head (cut to 9 fraction bits) and
  64. // a tail (2^(j/32) cut to 33 fraction bits, minus the head, rounded to float), generated from that definition
  65. constexpr float tanhHi[32] = {0x1.0000000000000p+0f, 0x1.0580000000000p+0f, 0x1.0b00000000000p+0f, 0x1.1100000000000p+0f, 0x1.1700000000000p+0f, 0x1.1d00000000000p+0f, 0x1.2380000000000p+0f, 0x1.2980000000000p+0f, 0x1.3000000000000p+0f, 0x1.3700000000000p+0f, 0x1.3d80000000000p+0f, 0x1.4480000000000p+0f, 0x1.4b80000000000p+0f, 0x1.5300000000000p+0f, 0x1.5a80000000000p+0f, 0x1.6200000000000p+0f, 0x1.6a00000000000p+0f, 0x1.7180000000000p+0f, 0x1.7a00000000000p+0f, 0x1.8200000000000p+0f, 0x1.8a80000000000p+0f, 0x1.9300000000000p+0f, 0x1.9c00000000000p+0f, 0x1.a500000000000p+0f, 0x1.ae80000000000p+0f, 0x1.b780000000000p+0f, 0x1.c180000000000p+0f, 0x1.cb00000000000p+0f, 0x1.d580000000000p+0f, 0x1.df80000000000p+0f, 0x1.ea00000000000p+0f, 0x1.f500000000000p+0f};
  66. constexpr float tanhLo[32] = {0x0.0p+0f, 0x1.b0d3100000000p-12f, 0x1.561b3e0000000p-10f, 0x1.80e8080000000p-11f, 0x1.5c1e3c0000000p-11f, 0x1.21cc5a0000000p-10f, 0x1.e9b9c00000000p-14f, 0x1.a77d460000000p-10f, 0x1.bf828c0000000p-10f, 0x1.a737380000000p-12f, 0x1.a993040000000p-10f, 0x1.8218180000000p-10f, 0x1.f6b54c0000000p-10f, 0x1.0ad5a60000000p-10f, 0x1.83eea40000000p-11f, 0x1.1fac0e0000000p-10f, 0x1.3cccf00000000p-13f, 0x1.dd7a3a0000000p-10f, 0x1.1473e80000000p-12f, 0x1.6266520000000p-10f, 0x1.39508a0000000p-10f, 0x1.cdec320000000p-10f, 0x1.2460a80000000p-10f, 0x1.40ec8e0000000p-10f, 0x1.3f32b00000000p-13f, 0x1.ddbcbe0000000p-10f, 0x1.9bdd800000000p-12f, 0x1.c8373a0000000p-10f, 0x1.8dcf800000000p-16f, 0x1.25ccde0000000p-10f, 0x1.2be8a80000000p-10f, 0x1.d96da00000000p-14f};
  67. float tanhfCrt (float x) noexcept
  68. {
  69. const std::uint32_t bits = ildsp::fbits (x), ax = bits & 0x7fffffffu;
  70. if (ax < 0x39000000u || ax > 0x7f800000u)
  71. return x; // tiny or NaN
  72. const float sgn = 1.0f - (float) (ax != bits ? 1 : 0) * 2.0f;
  73. const float a = sgn * x;
  74. float r;
  75. if (a > 10.0f)
  76. r = 1.0f;
  77. else if (! (1.0f < a))
  78. {
  79. const float x2 = a * a, x3 = x2 * a;
  80. float num, den;
  81. if (0.899999976f > a)
  82. {
  83. num = ((x2 * 4.89163103e-05f - 0.00146283559f) * x2) - 0.281928062f;
  84. den = x2 * 0.342701793f + 0.845784187f;
  85. }
  86. else
  87. {
  88. num = ((x2 * 3.82753497e-05f - 0.00123256445f) * x2) - 0.240698591f;
  89. den = x2 * 0.292529076f + 0.722097397f;
  90. }
  91. r = (num * x3) / den + a;
  92. }
  93. else
  94. {
  95. float t = a + a;
  96. float y = t * 46.1662407f;
  97. y = y > 0.0f ? y + 0.5f : y - 0.5f;
  98. const int n = (int) y;
  99. const int j = n & 31, m = n & ~31;
  100. const float hi = tanhHi[j], lo = tanhLo[j];
  101. t = t - (float) n * 0.0216598511f;
  102. const float tl = (float) (-n) * 9.98318228e-07f;
  103. const int k = m / 32, k2 = k / 2;
  104. const float s1 = ildsp::ffrom ((std::uint32_t) (k2 + 127) << 23), s2 = ildsp::ffrom ((std::uint32_t) (k - k2 + 127) << 23);
  105. const float rr = tl + t;
  106. float q = rr * 0.166666672f + 0.5f;
  107. q = q * (rr * rr);
  108. q = q + tl;
  109. q = q + t;
  110. q = q * (lo + hi);
  111. q = q + lo;
  112. q = q + hi;
  113. q = q * s1;
  114. q = q * s2;
  115. q = q + 1.0f;
  116. r = 1.0f - 2.0f / q;
  117. }
  118. return r * sgn;
  119. }
  120. // the plug-in's double sine (a two-lane polynomial kernel; its sincos evaluates it at x and |x| + π/2)
  121. double vsin (double x) noexcept
  122. {
  123. using ildsp::dbits;
  124. using ildsp::dfrom;
  125. const std::uint64_t sgn = dbits (x) & 0x8000000000000000ull;
  126. const double ax = dfrom (dbits (x) & 0x7fffffffffffffffull);
  127. const double t = ax * 0.3183098861837907 + 6755399441055744.0;
  128. const double n = t - 6755399441055744.0;
  129. const std::uint64_t par = dbits (t) << 63;
  130. double r = ax - n * 3.141592502593994;
  131. r = r - n * 1.5099578831723193e-07;
  132. r = r - n * 1.078060505991553e-14;
  133. r = r - n * 6.564007085747001e-22;
  134. const double r2 = r * r;
  135. const double rp = dfrom (dbits (r) ^ par);
  136. double p = -7.373344533110665e-13 * r2 + 1.6048095404959637e-10;
  137. p = p * r2 + -2.505188027386288e-08;
  138. p = p * r2 + 2.7557316600552628e-06;
  139. p = p * r2 + -0.00019841269825056256;
  140. p = p * r2 + 0.008333333333285542;
  141. p = p * r2 + -0.16666666666666186;
  142. double q = r2 * p;
  143. q = q * rp;
  144. return dfrom (dbits (rp + q) ^ sgn);
  145. }
  146. void sincosD (double x, double& s, double& c) noexcept
  147. {
  148. const double ax = std::fabs (x);
  149. if (ax >= 7.450580596923828e-09)
  150. {
  151. s = vsin (x);
  152. c = vsin (ax + 1.5707963267948966);
  153. }
  154. else
  155. {
  156. s = x;
  157. c = 1.0 - (ax * ax) * 0.5;
  158. }
  159. }
  160. float maxF (float a, float b) noexcept { return a > b ? a : b; } // maxss/maxps operand order
  161. float minF (float a, float b) noexcept { return a < b ? a : b; }
  162. constexpr double smooth01 = 0.09999999999999998; // the base of the plug-in's smoothing coefficients
  163. constexpr int crossover[16] = { 31, 47, 71, 112, 172, 264, 413, 624, 958, 1502, 2267, 3611, 5458, 8391, 12663, 18994 };
  164. constexpr float weightTime[8] = { 0.0f, 0.02f, 0.1f, 0.5f, 2.5f, 10.0f, 10.0f, 0.0f }; // weighted feedback, seconds
  165. struct RenderMode
  166. {
  167. #ifdef LYNX_MBD_MXCSR
  168. unsigned saved;
  169. explicit RenderMode (unsigned bits) noexcept : saved (_mm_getcsr()) { _mm_setcsr ((saved & ~0x6000u) | bits); }
  170. ~RenderMode() { _mm_setcsr (saved); }
  171. #else
  172. explicit RenderMode (unsigned) noexcept {}
  173. #endif
  174. RenderMode (const RenderMode&) = delete;
  175. RenderMode& operator= (const RenderMode&) = delete;
  176. };
  177. // ------------------------------------------------------------------------------------------------ DSP blocks
  178. // four-lane biquad (lanes: band A left/right, band B left/right)
  179. struct Biquad4
  180. {
  181. float b0[4] {}, b1[4] {}, b2[4] {}, a1[4] {}, a2[4] {};
  182. float y1[4] {}, y2[4] {}, x1[4] {}, x2[4] {};
  183. double fs = 44100.0;
  184. void tick (float* v) noexcept
  185. {
  186. for (int l = 0; l < 4; ++l)
  187. {
  188. const float x = v[l];
  189. const float y = (((x1[l] * b1[l] + x * b0[l]) + b2[l] * x2[l]) + y1[l] * a1[l]) + a2[l] * y2[l];
  190. x2[l] = x1[l], x1[l] = x, y2[l] = y1[l], y1[l] = y;
  191. v[l] = y;
  192. }
  193. }
  194. void identity (int pair) noexcept
  195. {
  196. for (int l = 2 * pair; l < 2 * pair + 2; ++l)
  197. b0[l] = 1.0f, b1[l] = b2[l] = a1[l] = a2[l] = 0.0f;
  198. }
  199. void set (int lane, float c0, float c1, float c2, float c3, float c4) noexcept
  200. {
  201. b0[lane] = c0, b1[lane] = c1, b2[lane] = c2, a1[lane] = c3, a2[lane] = c4;
  202. }
  203. // the plug-in's filter design (types 0 identity, 1 low pass, 2 high pass, 6 band pass, 9 all pass,
  204. // 10 / 11 first-order low / high pass) into one lane pair
  205. void design (int pair, int type, double f, double q, float gain) noexcept
  206. {
  207. double nb0 = 1.0, nb1 = 0.0, nb2 = 0.0, na0 = 1.0, na1 = 0.0, na2 = 0.0;
  208. if (type == 10 || type == 11)
  209. {
  210. const float t = (float) std::tan ((f * 3.141592653589793) / fs);
  211. const double td = (double) t;
  212. nb0 = type == 10 ? td : 1.0;
  213. nb1 = type == 10 ? td : -1.0;
  214. na1 = (double) (float) (t - 1.0f);
  215. na0 = (double) (float) (t + 1.0f);
  216. nb2 = na2 = 0.0;
  217. }
  218. else if (type != 0)
  219. {
  220. double s, c;
  221. sincosD ((f * 6.283185307179586) * (1.0 / fs), s, c);
  222. const double alpha = (0.5 / q) * s;
  223. const double m2c = c * -2.0;
  224. na0 = alpha + 1.0;
  225. na1 = m2c;
  226. na2 = 1.0 - alpha;
  227. switch (type)
  228. {
  229. case 1: nb0 = (1.0 - c) * 0.5, nb1 = 1.0 - c, nb2 = (1.0 - c) * 0.5; break;
  230. case 2: nb0 = (c + 1.0) * 0.5, nb1 = -(c + 1.0), nb2 = (c + 1.0) * 0.5; break;
  231. case 6: nb0 = s * 0.5, nb1 = 0.0, nb2 = s * -0.5; break;
  232. case 9: nb0 = 1.0 - alpha, nb1 = m2c, nb2 = alpha + 1.0; break;
  233. default: break;
  234. }
  235. }
  236. const double inv = 1.0 / na0;
  237. const float c0 = (float) (inv * nb0), c1 = (float) (inv * nb1), c2 = (float) (inv * nb2);
  238. const float c3 = -(float) (inv * na1), c4 = -(float) (inv * na2);
  239. for (int l = 2 * pair; l < 2 * pair + 2; ++l)
  240. set (l, gain * c0, gain * c1, gain * c2, c3, c4);
  241. }
  242. };
  243. // FFT convolver of the linear-phase crossover: 1024-tap FIR, blocks of 1024, FFT size 2048 (overlap-add). Our own
  244. // double-precision radix-2 FFT (FL uses IPP's single-precision transforms; REA_rodada71 §4)
  245. struct Convolver
  246. {
  247. static constexpr int N = 2048, H = 1024;
  248. std::vector<double> kre, kim; // spectrum of the zero-padded FIR
  249. std::vector<double> wre, wim; // scratch
  250. std::vector<float> in[2], out[2]; // per channel: input block, output accumulator (N)
  251. int pos = 0;
  252. struct Tables
  253. {
  254. std::vector<double> c, s; // cos / sin (2πk/N), k < N/2
  255. std::vector<int> rev;
  256. Tables() : c ((std::size_t) N / 2), s ((std::size_t) N / 2), rev ((std::size_t) N)
  257. {
  258. for (int k = 0; k < N / 2; ++k)
  259. c[(std::size_t) k] = std::cos (2.0 * 3.141592653589793 * k / N), s[(std::size_t) k] = std::sin (2.0 * 3.141592653589793 * k / N);
  260. for (int i = 0, j = 0; i < N; ++i)
  261. {
  262. rev[(std::size_t) i] = j;
  263. int bit = N >> 1;
  264. for (; j & bit; bit >>= 1)
  265. j ^= bit;
  266. j ^= bit;
  267. }
  268. }
  269. };
  270. static const Tables& tables()
  271. {
  272. static const Tables t;
  273. return t;
  274. }
  275. Convolver()
  276. {
  277. kre.assign (N, 0.0), kim.assign (N, 0.0), wre.assign (N, 0.0), wim.assign (N, 0.0);
  278. for (auto& v : in)
  279. v.assign (H, 0.0f);
  280. for (auto& v : out)
  281. v.assign (N, 0.0f);
  282. }
  283. void clear() noexcept
  284. {
  285. pos = 0;
  286. for (auto& v : in)
  287. std::fill (v.begin(), v.end(), 0.0f);
  288. for (auto& v : out)
  289. std::fill (v.begin(), v.end(), 0.0f);
  290. }
  291. // in place; inverse: conjugate twiddles, unscaled
  292. static void fft (double* re, double* im, bool inverse) noexcept
  293. {
  294. const auto& t = tables();
  295. for (int i = 0; i < N; ++i)
  296. {
  297. const int j = t.rev[(std::size_t) i];
  298. if (i < j)
  299. std::swap (re[i], re[j]), std::swap (im[i], im[j]);
  300. }
  301. const double sg = inverse ? 1.0 : -1.0;
  302. for (int len = 2; len <= N; len <<= 1)
  303. {
  304. const int half = len / 2, step = N / len;
  305. for (int i = 0; i < N; i += len)
  306. for (int k = 0; k < half; ++k)
  307. {
  308. const double c = t.c[(std::size_t) (k * step)], s = sg * t.s[(std::size_t) (k * step)];
  309. const int a = i + k, b = a + half;
  310. const double xr = re[b] * c - im[b] * s, xi = re[b] * s + im[b] * c;
  311. re[b] = re[a] - xr, im[b] = im[a] - xi;
  312. re[a] = re[a] + xr, im[a] = im[a] + xi;
  313. }
  314. }
  315. }
  316. // the band's FIR: the brick-wall bin mask (1025 bins) as a zero-phase response centred on N/2, the middle 1024
  317. // taps under a sine window
  318. void design (const std::vector<float>& mask)
  319. {
  320. std::vector<double> re ((std::size_t) N), im ((std::size_t) N, 0.0);
  321. for (int k = 0; k < N; ++k)
  322. {
  323. const int kk = k <= N / 2 ? k : N - k;
  324. re[(std::size_t) k] = (double) mask[(std::size_t) kk] * ((k & 1) ? -1.0 : 1.0); // delay of N/2
  325. }
  326. fft (re.data(), im.data(), true);
  327. std::fill (kre.begin(), kre.end(), 0.0);
  328. std::fill (kim.begin(), kim.end(), 0.0);
  329. for (int i = 0; i < H; ++i)
  330. {
  331. const double tap = re[(std::size_t) (H / 2 + i)] / N;
  332. const double w = std::sin ((double) i * 3.141592653589793 / (double) (H - 1));
  333. kre[(std::size_t) i] = (double) (float) ((float) tap * (float) w);
  334. }
  335. fft (kre.data(), kim.data(), false);
  336. }
  337. void tick (float& l, float& r) noexcept
  338. {
  339. in[0][(std::size_t) pos] = l, in[1][(std::size_t) pos] = r;
  340. l = out[0][(std::size_t) pos], r = out[1][(std::size_t) pos];
  341. if (++pos == H)
  342. {
  343. pos = 0;
  344. // both channels in one complex transform (left real, right imaginary; the FIR is real)
  345. double* xr = wre.data();
  346. double* xi = wim.data();
  347. for (int i = 0; i < N; ++i)
  348. {
  349. xr[i] = i < H ? (double) in[0][(std::size_t) i] : 0.0;
  350. xi[i] = i < H ? (double) in[1][(std::size_t) i] : 0.0;
  351. }
  352. fft (xr, xi, false);
  353. for (int k = 0; k < N; ++k)
  354. {
  355. const double a = xr[k], b = xi[k], c = kre[(std::size_t) k], d = kim[(std::size_t) k];
  356. xr[k] = a * c - b * d;
  357. xi[k] = a * d + b * c;
  358. }
  359. fft (xr, xi, true);
  360. const float inv = 1.0f / (float) N;
  361. for (int c = 0; c < 2; ++c)
  362. {
  363. auto& o = out[c];
  364. const double* v = c == 0 ? xr : xi;
  365. std::memmove (o.data(), o.data() + H, (std::size_t) H * sizeof (float));
  366. std::fill (o.begin() + H, o.end(), 0.0f);
  367. for (int i = 0; i < N; ++i)
  368. o[(std::size_t) i] = inv * (float) v[i] + o[(std::size_t) i];
  369. }
  370. }
  371. }
  372. };
  373. // per-band-pair delay line (four lanes per frame)
  374. struct DelayLine
  375. {
  376. float fb[4] {};
  377. float coef = 0.001f;
  378. float dA = 0, dB = 0, tA = 0, tB = 0;
  379. int tap[4] {}; // keep-pitch taps: A slot 0, A slot 1, B slot 0, B slot 1
  380. double maxSeconds = 1.0;
  381. std::vector<float> buf;
  382. int size = 1, w = 0, mask = 0;
  383. };
  384. // one band pair (bands 2k and 2k + 1)
  385. struct BandPair
  386. {
  387. Biquad4 f[10];
  388. Convolver conv[2];
  389. DelayLine dl;
  390. int mode = 0; // 0 steep (8 biquads), 1 linear phase, 2 gentle
  391. bool second = false; // gentle mode runs f[9] after f[8] (set for the top pair only)
  392. float gCur[4] { 1, 1, 1, 1 }, gTgt[4] { 1, 1, 1, 1 }, gCoef[4] { 0.5f, 0.5f, 0.5f, 0.5f };
  393. };
  394. } // namespace
  395. // ------------------------------------------------------------------------------------------------ state
  396. State::State()
  397. {
  398. for (int b = 0; b < numBanks; ++b)
  399. for (int k = 0; k < numBands; ++k)
  400. {
  401. raw[(std::size_t) (b * 48 + k)] = 0;
  402. raw[(std::size_t) (b * 48 + 16 + k)] = 1 << 30;
  403. raw[(std::size_t) (b * 48 + 32 + k)] = 1 << 29;
  404. }
  405. for (int g = 0; g < numGlobals; ++g)
  406. raw[(std::size_t) (384 + g)] = globalDefault[g];
  407. for (auto& m : mode)
  408. m.fill (0);
  409. }
  410. std::optional<State> parse (const std::vector<std::uint8_t>& d)
  411. {
  412. if (d.size() < 4)
  413. return std::nullopt;
  414. State s;
  415. std::size_t o = 0;
  416. const int ver = i32At (d, o);
  417. o += 4;
  418. if (ver < 1 || ver > 7)
  419. return std::nullopt;
  420. int count = 393;
  421. if (ver != 1)
  422. {
  423. if (d.size() < 8)
  424. return std::nullopt;
  425. count = i32At (d, o);
  426. o += 4;
  427. if (count < 1)
  428. return std::nullopt;
  429. }
  430. if (d.size() < o + 4 * (std::size_t) count + 8)
  431. return std::nullopt;
  432. for (int i = 0; i < count; ++i, o += 4)
  433. if (i < numEngineParams)
  434. s.raw[(std::size_t) i] = i32At (d, o);
  435. s.version = ver;
  436. s.targetSel = i32At (d, o);
  437. s.bank = i32At (d, o + 4);
  438. o += 8;
  439. if (ver >= 4)
  440. {
  441. if (d.size() < o + 1)
  442. return std::nullopt;
  443. s.flagA = d[o++];
  444. if (ver > 4 && o < d.size())
  445. s.flagB = d[o++];
  446. if (ver > 5 && o < d.size())
  447. s.flagC = d[o++];
  448. for (auto& m : s.mode)
  449. for (auto& v : m)
  450. {
  451. if (o + 4 > d.size())
  452. return s;
  453. v = i32At (d, o);
  454. o += 4;
  455. }
  456. }
  457. return s;
  458. }
  459. std::vector<std::uint8_t> serialize (const State& s)
  460. {
  461. std::vector<std::uint8_t> d;
  462. putI32 (d, 7);
  463. putI32 (d, numEngineParams);
  464. for (int v : s.raw)
  465. putI32 (d, v);
  466. putI32 (d, s.targetSel);
  467. putI32 (d, s.bank);
  468. d.push_back (s.flagA);
  469. d.push_back (s.flagB);
  470. d.push_back (s.flagC);
  471. for (const auto& m : s.mode)
  472. for (int v : m)
  473. putI32 (d, v);
  474. return d;
  475. }
  476. float engineValue (const State& s, int i) noexcept
  477. {
  478. if (i < 0 || i >= numEngineParams)
  479. return 0.0f;
  480. const int raw = s.raw[(std::size_t) i];
  481. if (i < 384)
  482. {
  483. // after the load FL's editor re-sends the selected bank's cells, clamped to 0…2 in its own units (raw / 2^29)
  484. const int bank = i / 48, target = (i % 48) / 16, band = i % 16;
  485. if (bank == s.bank && s.mode[(std::size_t) band][(std::size_t) target] == 0)
  486. {
  487. float v = (float) ((double) raw / 536870912.0);
  488. v = 0.0f <= v ? (2.0f < v ? 2.0f : v) : 0.0f;
  489. const double r = std::nearbyint ((double) v * 536870912.0); // Delphi Round: ties to even
  490. return (float) (r / 1073741824.0);
  491. }
  492. return (float) ((double) raw / 1073741824.0);
  493. }
  494. const auto r = globalRange[i - 384];
  495. double v = (double) (raw - r.min) * (1.0 / (double) (r.max - r.min));
  496. v = std::clamp (v, 0.0, 1.0);
  497. return (float) v;
  498. }
  499. void toParams (const State& s, EffectParams& p) noexcept
  500. {
  501. for (int g = 0; g < numGlobals; ++g)
  502. {
  503. const auto r = globalRange[g];
  504. p[(std::size_t) g] = std::clamp ((float) (s.raw[(std::size_t) (384 + g)] - r.min) / (float) (r.max - r.min), 0.0f, 1.0f);
  505. }
  506. }
  507. namespace
  508. {
  509. int rawOf (float v, Range r) noexcept
  510. {
  511. return r.min + (int) std::lround (std::clamp (v, 0.0f, 1.0f) * (float) (r.max - r.min));
  512. }
  513. } // namespace
  514. void applyParams (const EffectParams& p, State& s) noexcept
  515. {
  516. for (int g = 0; g < numGlobals; ++g)
  517. s.raw[(std::size_t) (384 + g)] = rawOf (p[(std::size_t) g], globalRange[g]);
  518. }
  519. std::string paramText (int param, float v)
  520. {
  521. char t[48];
  522. if (param < 0 || param >= numGlobals)
  523. return {};
  524. const int raw = rawOf (v, globalRange[param]);
  525. switch (param)
  526. {
  527. case 0: std::snprintf (t, sizeof (t), "%+.1f%%", raw / 60.0); return t;
  528. case 1:
  529. case 5:
  530. case 8:
  531. case 9: return raw != 0 ? "on" : "off";
  532. case 2: std::snprintf (t, sizeof (t), "%.1f%%", raw * 95.0 / 6000.0); return t;
  533. case 3: std::snprintf (t, sizeof (t), "%.0f ms", raw / 6.0); return t;
  534. case 4: return raw == 0 ? "gentle" : (raw == 1 ? "steep" : "linear phase");
  535. case 6: std::snprintf (t, sizeof (t), "bank %.2f", 1.0 + raw * 0.001); return t;
  536. case 7: std::snprintf (t, sizeof (t), "%d%% wet", (int) std::lround ((raw + 128) * 100.0 / 256.0)); return t;
  537. case 10:
  538. {
  539. const double x = (double) (raw - 1000) / 99000.0;
  540. std::snprintf (t, sizeof (t), "%.0f Hz", 10.0 * std::pow (10.0, 2.0 * x));
  541. return t;
  542. }
  543. case 11: return raw == 0 ? "comp" : (raw == 1 ? "saturate" : "clip");
  544. default: return {};
  545. }
  546. }
  547. // ------------------------------------------------------------------------------------------------ engine
  548. struct Engine::Impl
  549. {
  550. float p[numEngineParams + 64] {}; // morph reads one bank past the last one (always weighted by 0)
  551. std::uint8_t bandOn[numBands];
  552. double fs = 44100.0;
  553. BandPair bp[8];
  554. bool dirty = false;
  555. // output stage
  556. float mixS = 0, hpS = 0, smooth = 0, piOverFs = 0, hpFreq = 100.0f;
  557. float hpX1L = 0, hpY1L = 0, hpX1R = 0, hpY1R = 0;
  558. int satType = 0;
  559. float envL = 0, envR = 0, attL = 0.00520202f, relL = 0.000521f, attR = 0.00520202f, relR = 0.000521f;
  560. double attT = 0.01, relT = 0.1;
  561. // keep pitch
  562. bool keepPitch = false, fading = false, recomputed = false;
  563. float fadePos = 1.0f, fadeInc = 0.0f, fadeStep = 0.0f;
  564. int fadeSlot = 0;
  565. // dry path
  566. std::vector<float> dryL, dryR;
  567. int drySize = 0, dryPos = 0, latency = 0;
  568. Impl()
  569. {
  570. std::fill (std::begin (bandOn), std::end (bandOn), (std::uint8_t) 1);
  571. for (int b = 0; b < numBanks; ++b)
  572. for (int k = 0; k < numBands; ++k)
  573. {
  574. p[b * 48 + 32 + k] = 0.5f;
  575. p[b * 48 + 16 + k] = 1.0f;
  576. p[b * 48 + k] = (float) k * 0.0666666701f;
  577. }
  578. p[0x187] = 1.0f;
  579. p[0x180] = 0.5f;
  580. p[0x183] = 0.25f;
  581. attL = attR = ildsp::ffrom (0x3baaa523u);
  582. relL = relR = ildsp::ffrom (0x3a08d648u);
  583. bp[7].second = true;
  584. setSampleRate (44100.0); // the constructor's own set-up
  585. }
  586. static float ramp (double t, double rate)
  587. {
  588. if (! (t > 0.0))
  589. return 1.0f;
  590. return 1.0f - (float) std::pow (0.1, 1.0 / (t * rate));
  591. }
  592. void setParam (int i, float v) noexcept // FUN_1801d1ff0
  593. {
  594. if (i < 0 || i >= numEngineParams)
  595. return;
  596. p[i] = v;
  597. if (i < 0x183 || i == 0x188 || i == 0x189 || i == 0x186)
  598. {
  599. dirty = true;
  600. return;
  601. }
  602. switch (i)
  603. {
  604. case 0x18b: satType = v < 0.333000004f ? 0 : (v >= 0.666000009f ? 3 : 2); break;
  605. case 0x183:
  606. {
  607. const double c = v == 0.0f ? 1.0 : 1.0 - std::pow (smooth01, 1.0 / ((double) v * fs));
  608. for (auto& b : bp)
  609. b.dl.coef = (float) c;
  610. break;
  611. }
  612. case 0x184:
  613. {
  614. const int m = v < 0.333332986f ? 2 : (v >= 0.666665971f ? 1 : 0);
  615. for (auto& b : bp)
  616. b.mode = m;
  617. latency = m == 1 ? 0x600 : 0;
  618. break;
  619. }
  620. case 0x185:
  621. {
  622. keepPitch = 0.5f < v;
  623. fadePos = 1.0f;
  624. fading = false;
  625. if (keepPitch)
  626. {
  627. fadeSlot = 1;
  628. recomputed = false;
  629. fadeInc = 0.0f;
  630. for (auto& b : bp)
  631. {
  632. auto& dl = b.dl;
  633. dl.dA = dl.tA, dl.dB = dl.tB;
  634. dl.tap[0] = (int) dl.tA, dl.tap[2] = (int) dl.tB;
  635. dl.dA = dl.tA, dl.dB = dl.tB;
  636. dl.tap[1] = (int) dl.tA, dl.tap[3] = (int) dl.tB;
  637. }
  638. }
  639. else
  640. fadeSlot = 0;
  641. break;
  642. }
  643. default: break;
  644. }
  645. }
  646. void recompute() noexcept // FUN_1801d2250 (runs inside the render, rounding toward zero)
  647. {
  648. recomputed = true;
  649. const float fsMs = (float) (fs * 0.001);
  650. const double xScale = 0.5f > p[0x181] ? 1.0 : 0.10000000149011612;
  651. const bool harmonic = 0.5f <= p[0x188];
  652. const bool weighted = 0.1f <= p[0x189];
  653. float m = p[0x186] * 7.0f;
  654. const int bank = (int) m;
  655. const float f = m - (float) bank, g = 1.0f - f;
  656. const float fbPlain = p[0x182] * 0.949999988f;
  657. const float* P0 = p + bank * 48;
  658. const float* P1 = P0 + 48;
  659. for (int k = 0; k < numBands; ++k)
  660. {
  661. auto& b = bp[k >> 1];
  662. const int ln = (k & 1) * 2;
  663. if (bandOn[k] != 0)
  664. {
  665. const float vol = g * P0[16 + k] + f * P1[16 + k];
  666. float pan = g * P0[32 + k] + f * P1[32 + k];
  667. pan = minF (maxF (pan, 0.0f), 1.0f);
  668. const float sl = std::sqrt (1.0f - pan), sr = std::sqrt (pan);
  669. const float v2 = (vol * 0.327543139f) * vol;
  670. const float v3 = (vol * vol) * (vol * 1.39229143f);
  671. b.gTgt[ln] = ((v3 * sl) - (v2 * sl)) + (vol * sl) * 0.349678934f;
  672. b.gTgt[ln + 1] = ((v3 * sr) - (v2 * sr)) + (vol * sr) * 0.349678934f;
  673. }
  674. else
  675. b.gTgt[ln] = b.gTgt[ln + 1] = 0.0f;
  676. const float dly = f * P1[k] + g * P0[k];
  677. int samples;
  678. if (harmonic)
  679. {
  680. const int semis = (int) ((p[0x180] - 0.5f) * 96.0f);
  681. const float e = (float) semis * -0.0833333358f - (dly - 0.5f) * 4.0f;
  682. float x = ildsp::powf_ (2.0f, e) * 0.00191112794f;
  683. x = minF (x, 1.0f);
  684. samples = std::max (1, (int) ((double) x * fs));
  685. }
  686. else
  687. {
  688. const float s = p[0x180];
  689. float d = dly;
  690. const float sc = std::fabs ((s + s) - 1.0f);
  691. if (! (s >= 0.5f))
  692. d = 1.0f - d;
  693. samples = std::max (1, (int) ((double) (sc * d) * fs * xScale));
  694. }
  695. if (ln == 0)
  696. b.dl.tA = (float) samples;
  697. else
  698. b.dl.tB = (float) samples;
  699. float fb = fbPlain;
  700. if (weighted)
  701. {
  702. const float x5 = fbPlain * 5.0f;
  703. const int ix = (int) x5;
  704. const float fr = x5 - (float) ix;
  705. float t = (1.0f - fr) * weightTime[ix] + fr * weightTime[ix + 1];
  706. t = t * (float) fs;
  707. const int den = std::max (samples, (int) fsMs);
  708. t = t / (float) den;
  709. fb = ildsp::powf_ (0.1f, 1.0f / t);
  710. }
  711. b.dl.fb[ln] = b.dl.fb[ln + 1] = fb;
  712. }
  713. }
  714. void designCrossover() // FUN_1801d28c0
  715. {
  716. // gentle: one filter per band (first-order low pass, band passes, first-order high pass)
  717. for (int k = 0; k < numBands; ++k)
  718. {
  719. auto& b = bp[k >> 1];
  720. const int pair = k & 1;
  721. b.f[9].identity (pair);
  722. const int type = k == 0 ? 10 : (k == 15 ? 11 : 6);
  723. const int freq = k == 0 ? 31 : (k == 15 ? 18994 : crossover[k]);
  724. b.f[8].design (pair, type, (double) freq, 0.5, k == 0 ? 1.0f : 0.5f);
  725. }
  726. // the top pair's second gentle filter: band 15 also runs through twice an all pass at 18994 Hz
  727. {
  728. auto& f9 = bp[7].f[9];
  729. f9.identity (0);
  730. double s, c;
  731. sincosD (119342.82172456906 / f9.fs, s, c);
  732. const double inv = 1.0 / (s + 1.0);
  733. const float c0 = (float) ((1.0 - s) * inv), c1 = (float) ((c * -2.0) * inv), c2 = (float) (inv * (s + 1.0));
  734. f9.set (2, c0 + c0, c1 + c1, c2 + c2, (float) ((c + c) * inv), (float) ((s - 1.0) * inv));
  735. f9.set (3, c0 + c0, c1 + c1, c2 + c2, (float) ((c + c) * inv), (float) ((s - 1.0) * inv));
  736. }
  737. // linear phase: brick-wall FIRs between the crossover frequencies
  738. for (int k = 0; k < numBands; ++k)
  739. {
  740. std::vector<float> mask (1025, 0.0f);
  741. const double lo = k == 0 ? 0.0 : (double) crossover[k - 1];
  742. const double hi = k < 15 ? (double) crossover[k] : fs * 0.5;
  743. if (k == 15)
  744. mask[1024] = 1.0f;
  745. const double scale = 2048.0 / fs;
  746. const long a = (long) (scale * lo), z = (long) (scale * hi);
  747. for (long i = a; i < z; ++i)
  748. mask[(std::size_t) i] = 1.0f;
  749. bp[k >> 1].conv[k & 1].design (mask);
  750. }
  751. // steep: band 0 = four low-pass sections at 31 Hz; bands 1…15 = four high-pass sections at the lower crossover
  752. // then four low-pass sections at the upper one (band 15: four all passes at 18994 Hz instead)
  753. {
  754. const float gain = 0.5f * 2.8283999f;
  755. const double qs[4] = { 0.92387972283855013, 0.38268341629224528, 0.92387972283855013, 0.38268341629224528 };
  756. for (int j = 0; j < 4; ++j)
  757. {
  758. auto& fl = bp[0].f[j];
  759. double s, c;
  760. sincosD (194.77874452256717 / fl.fs, s, c);
  761. const double k2 = s * qs[j];
  762. const double inv = 1.0 / (k2 + 1.0);
  763. const double omc = 1.0 - c;
  764. float c0, c1, c2;
  765. if (j == 0)
  766. {
  767. c0 = (float) ((omc * 0.5) * inv) * gain;
  768. c1 = (float) (inv * omc) * gain;
  769. c2 = (float) ((omc * 0.5) * inv) * gain;
  770. }
  771. else
  772. {
  773. c0 = (float) ((omc * 0.5) * inv);
  774. c1 = (float) (inv * omc);
  775. c2 = c0;
  776. }
  777. const float a1 = (float) ((c + c) * inv), a2 = (float) ((k2 - 1.0) * inv);
  778. fl.set (0, c0, c1, c2, a1, a2);
  779. fl.set (1, c0, c1, c2, a1, a2);
  780. }
  781. for (int j = 4; j < 7; ++j)
  782. bp[0].f[j].identity (0);
  783. bp[0].f[7].design (0, 0, 0.0, 0.0, 1.0f);
  784. const double qa = 0.54119598865509033, qb = 1.3065630197525024;
  785. for (int k = 1; k < numBands; ++k)
  786. {
  787. auto& b = bp[k >> 1];
  788. const int pair = k & 1;
  789. const double lo = crossover[k - 1], hi = crossover[k];
  790. b.f[0].design (pair, 2, lo, qa, gain);
  791. b.f[1].design (pair, 2, lo, qb, 1.0f);
  792. b.f[2].design (pair, 2, lo, qa, 1.0f);
  793. b.f[3].design (pair, 2, lo, qb, 1.0f);
  794. const int t = k == 15 ? 9 : 1;
  795. b.f[4].design (pair, t, hi, qa, 1.0f);
  796. b.f[5].design (pair, t, hi, qb, 1.0f);
  797. b.f[6].design (pair, t, hi, qa, 1.0f);
  798. b.f[7].design (pair, t, hi, qb, 1.0f);
  799. }
  800. }
  801. }
  802. void setSampleRate (double rate) // FUN_1801d3240
  803. {
  804. fs = rate;
  805. piOverFs = (float) (1.0 / rate) * 3.14159274f;
  806. hpFreq = (float) std::fmin ((double) hpFreq, (double) ((float) rate * 0.49000001f));
  807. const float gc = (float) (1.0 - std::pow (smooth01, 20.0 / rate));
  808. for (auto& b : bp)
  809. {
  810. auto& dl = b.dl;
  811. int want = (int) (rate * dl.maxSeconds + 0.5), size = 1;
  812. while (size < want)
  813. size *= 2;
  814. dl.size = size;
  815. dl.mask = size - 1;
  816. dl.buf.assign ((std::size_t) size * 4, 0.0f);
  817. dl.w = 0;
  818. for (int j = 0; j < 8; ++j) // the gentle filters f[8], f[9] keep the constructor's 44.1 kHz (as in FL)
  819. b.f[j].fs = rate;
  820. std::fill (std::begin (b.gCoef), std::end (b.gCoef), gc);
  821. }
  822. for (int i = 0; i < numEngineParams; ++i)
  823. setParam (i, p[i]);
  824. attL = attR = ramp (attT, rate);
  825. relL = relR = ramp (relT, rate);
  826. designCrossover();
  827. fadeStep = 1.0f / (float) (rate * 0.10000000000000001);
  828. drySize = 0x60a;
  829. smooth = (float) (1.0 - std::pow (smooth01, 200.0 / rate));
  830. dryL.assign ((std::size_t) drySize, 0.0f);
  831. dryR.assign ((std::size_t) drySize, 0.0f);
  832. if (dryPos >= drySize)
  833. dryPos = 0;
  834. }
  835. void suspend() noexcept // FUN_1801d6e00
  836. {
  837. hpX1L = hpY1L = hpX1R = hpY1R = 0.0f;
  838. for (auto& b : bp)
  839. {
  840. std::fill (b.dl.buf.begin(), b.dl.buf.end(), 0.0f);
  841. for (auto& c : b.conv)
  842. c.clear();
  843. }
  844. }
  845. void processBand (BandPair& b, float* v) noexcept // FUN_1801d3f90
  846. {
  847. if (b.mode == 1)
  848. {
  849. b.conv[0].tick (v[0], v[1]);
  850. b.conv[1].tick (v[2], v[3]);
  851. }
  852. else if (b.mode == 0)
  853. for (int j = 0; j < 8; ++j)
  854. b.f[j].tick (v);
  855. else
  856. {
  857. b.f[8].tick (v);
  858. if (b.second)
  859. b.f[9].tick (v);
  860. }
  861. auto& dl = b.dl;
  862. float r[4];
  863. if (keepPitch)
  864. {
  865. const float a = fadePos, bb = 1.0f - fadePos;
  866. auto at = [&] (int tap, int lane) { return dl.buf[(std::size_t) ((((dl.w - tap) & dl.mask)) * 4 + lane)]; };
  867. if (a == 1.0f)
  868. r[0] = at (dl.tap[0], 0), r[1] = at (dl.tap[0], 1), r[2] = at (dl.tap[2], 2), r[3] = at (dl.tap[2], 3);
  869. else if (bb == 1.0f)
  870. r[0] = at (dl.tap[1], 0), r[1] = at (dl.tap[1], 1), r[2] = at (dl.tap[3], 2), r[3] = at (dl.tap[3], 3);
  871. else
  872. {
  873. r[0] = bb * at (dl.tap[1], 0) + a * at (dl.tap[0], 0);
  874. r[1] = bb * at (dl.tap[1], 1) + a * at (dl.tap[0], 1);
  875. r[2] = bb * at (dl.tap[3], 2) + a * at (dl.tap[2], 2);
  876. r[3] = bb * at (dl.tap[3], 3) + a * at (dl.tap[2], 3);
  877. }
  878. }
  879. else
  880. {
  881. dl.dA = (dl.tA - dl.dA) * dl.coef + dl.dA;
  882. dl.dB = (dl.tB - dl.dB) * dl.coef + dl.dB;
  883. const int iA = (int) dl.dA, iB = (int) dl.dB;
  884. const float fA = dl.dA - (float) iA, fB = dl.dB - (float) iB;
  885. const int jA = (dl.w - iA) & dl.mask, jB = (dl.w - iB) & dl.mask;
  886. const int kA = (jA - 1) & dl.mask, kB = (jB - 1) & dl.mask;
  887. const float* bA0 = &dl.buf[(std::size_t) jA * 4];
  888. const float* bA1 = &dl.buf[(std::size_t) kA * 4];
  889. const float* bB0 = &dl.buf[(std::size_t) jB * 4];
  890. const float* bB1 = &dl.buf[(std::size_t) kB * 4];
  891. const float gA = 1.0f - fA, gB = 1.0f - fB;
  892. r[0] = gA * bA0[0] + fA * bA1[0];
  893. r[1] = fA * bA1[1] + gA * bA0[1];
  894. r[2] = fB * bB1[2] + gB * bB0[2];
  895. r[3] = fB * bB1[3] + gB * bB0[3];
  896. }
  897. float* wp = &dl.buf[(std::size_t) dl.w * 4];
  898. for (int l = 0; l < 4; ++l)
  899. wp[l] = minF (maxF (r[l] * dl.fb[l], -1.0f), 1.0f) + v[l];
  900. dl.w = (dl.w + 1) & dl.mask;
  901. for (int l = 0; l < 4; ++l)
  902. {
  903. v[l] = r[l] * b.gCur[l];
  904. b.gCur[l] = (b.gTgt[l] - b.gCur[l]) * b.gCoef[l] + b.gCur[l];
  905. }
  906. }
  907. void process (float* L, float* R, int n) noexcept // FUN_1801d1750
  908. {
  909. RenderMode mode (0xE040u);
  910. if (dirty)
  911. {
  912. recompute();
  913. dirty = false;
  914. }
  915. if (keepPitch && ! fading && recomputed)
  916. {
  917. fading = true;
  918. recomputed = false;
  919. for (auto& b : bp)
  920. {
  921. auto& dl = b.dl;
  922. dl.dA = dl.tA, dl.dB = dl.tB;
  923. dl.tap[fadeSlot] = (int) dl.tA;
  924. dl.tap[2 + fadeSlot] = (int) dl.tB;
  925. }
  926. if (fadeSlot == 0)
  927. {
  928. fadeInc = fadeStep;
  929. fadeSlot = 1;
  930. }
  931. else
  932. {
  933. fadeSlot = 0;
  934. fadeInc = -fadeStep;
  935. }
  936. }
  937. const bool fusedPow = true;
  938. (void) fusedPow;
  939. for (int i = 0; i < n; ++i)
  940. {
  941. const float inL = L[i], inR = R[i];
  942. float s[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  943. for (auto& b : bp)
  944. {
  945. float v[4] = { inL, inR, inL, inR };
  946. processBand (b, v);
  947. for (int l = 0; l < 4; ++l)
  948. s[l] = s[l] + v[l];
  949. }
  950. if (fading)
  951. {
  952. const float x = fadeInc + fadePos;
  953. fadePos = x;
  954. if (! (x >= 0.0f))
  955. fadePos = 0.0f, fading = false;
  956. else if (x > 1.0f)
  957. fadePos = 1.0f, fading = false;
  958. }
  959. hpS = (p[0x18a] - hpS) * smooth + hpS;
  960. const float wl = s[0] + s[2], wr = s[1] + s[3];
  961. const float fq = ildsp::powf_ (10.0f, hpS * 2.0f) * 10.0f;
  962. hpFreq = (float) std::fmin ((double) fq, (double) ((float) fs * 0.49000001f));
  963. const float t = tanfCrt (fq * piOverFs);
  964. const float a0 = 1.0f / (t + 1.0f);
  965. const float a1 = -a0;
  966. const float c1 = -((t - 1.0f) * a0);
  967. float yl = ((a0 * wl) + a1 * hpX1L) + c1 * hpY1L;
  968. float yr = ((a0 * wr) + a1 * hpX1R) + c1 * hpY1R;
  969. hpX1L = wl, hpX1R = wr, hpY1L = yl, hpY1R = yr;
  970. if (satType == 0)
  971. {
  972. float ax = std::fabs (yl);
  973. envL = (ax - envL) * (ax < envL ? relL : attL) + envL;
  974. if (envL > 1.0f)
  975. yl = yl / envL;
  976. ax = std::fabs (yr);
  977. envR = (ax - envR) * (ax < envR ? relR : attR) + envR;
  978. if (envR > 1.0f)
  979. yr = yr / envR;
  980. }
  981. else if (satType == 2)
  982. {
  983. yl = tanhfCrt (yl);
  984. yr = tanhfCrt (yr);
  985. }
  986. else if (satType == 3)
  987. {
  988. yl = yl >= -1.0f ? minF (yl, 1.0f) : -1.0f;
  989. yr = yr >= -1.0f ? minF (yr, 1.0f) : -1.0f;
  990. }
  991. dryL[(std::size_t) dryPos] = inL;
  992. dryR[(std::size_t) dryPos] = inR;
  993. int j = dryPos - latency;
  994. while (j < 0)
  995. j += drySize;
  996. const float dl = dryL[(std::size_t) j], dr = dryR[(std::size_t) j];
  997. dryPos = dryPos + 1 >= drySize ? 0 : dryPos + 1;
  998. mixS = (p[0x187] - mixS) * smooth + mixS;
  999. const float dm = 1.0f - mixS;
  1000. L[i] = mixS * yl + dm * dl;
  1001. R[i] = yr * mixS + dm * dr;
  1002. }
  1003. }
  1004. };
  1005. Engine::Engine() : d (std::make_unique<Impl>()) {}
  1006. Engine::~Engine() = default;
  1007. void Engine::setSampleRate (double sampleRate) { d->setSampleRate (sampleRate); }
  1008. void Engine::setParameter (int index, float value) noexcept { d->setParam (index, value); }
  1009. void Engine::load (const State& s) noexcept
  1010. {
  1011. for (int i = 0; i < numEngineParams; ++i)
  1012. d->setParam (i, engineValue (s, i));
  1013. }
  1014. void Engine::suspend() noexcept { d->suspend(); }
  1015. void Engine::process (float* left, float* right, int n) noexcept
  1016. {
  1017. if (n > 0)
  1018. d->process (left, right, n);
  1019. }
  1020. int Engine::latencySamples() const noexcept { return d->latency; }
  1021. // ------------------------------------------------------------------------------------------------ LYNX effect
  1022. namespace
  1023. {
  1024. class MultibandDelayEffect final : public Effect
  1025. {
  1026. public:
  1027. explicit MultibandDelayEffect (const std::vector<std::uint8_t>& flState)
  1028. {
  1029. if (auto s = parse (flState))
  1030. state = *s;
  1031. engine.load (state);
  1032. }
  1033. void prepare (double sampleRate) noexcept override
  1034. {
  1035. engine.load (state);
  1036. engine.setSampleRate (sampleRate);
  1037. engine.suspend();
  1038. }
  1039. void reset() noexcept override { engine.suspend(); }
  1040. // the editor changed band cells or the selected bank: the running engine takes the new cell values
  1041. bool acceptState (const std::vector<std::uint8_t>& bytes) noexcept override
  1042. {
  1043. const auto s = parse (bytes);
  1044. return s && incoming.offer (*s);
  1045. }
  1046. void process (float* left, float* right, int n, const EffectParams& p, double) noexcept override
  1047. {
  1048. if (incoming.take (next))
  1049. {
  1050. for (int i = 0; i < 384; ++i)
  1051. if (const auto v = engineValue (next, i); v != engineValue (state, i))
  1052. engine.setParameter (i, v);
  1053. for (int i = 0; i < 384; ++i)
  1054. state.raw[(std::size_t) i] = next.raw[(std::size_t) i];
  1055. state.targetSel = next.targetSel, state.bank = next.bank, state.mode = next.mode;
  1056. }
  1057. if (! haveParams || p != lastParams)
  1058. {
  1059. lastParams = p, haveParams = true;
  1060. auto s = state;
  1061. applyParams (p, s);
  1062. for (int g = 0; g < numGlobals; ++g)
  1063. if (s.raw[(std::size_t) (384 + g)] != state.raw[(std::size_t) (384 + g)])
  1064. {
  1065. state.raw[(std::size_t) (384 + g)] = s.raw[(std::size_t) (384 + g)];
  1066. engine.setParameter (384 + g, engineValue (state, 384 + g));
  1067. }
  1068. }
  1069. engine.process (left, right, n);
  1070. }
  1071. int latencySamples() const noexcept override { return engine.latencySamples(); }
  1072. private:
  1073. State state, next;
  1074. StateHandoff<State> incoming;
  1075. bool haveParams = false;
  1076. EffectParams lastParams {};
  1077. Engine engine;
  1078. };
  1079. } // namespace
  1080. std::unique_ptr<Effect> make (const std::vector<std::uint8_t>& flState)
  1081. {
  1082. return std::make_unique<MultibandDelayEffect> (flState);
  1083. }
  1084. } // namespace lynx::mbdelay