FruityBasicFx.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. #include "core/FruityBasicFx.h"
  2. // Fruity PanOMatic, Fruity Balance and Fruity Blood Overdrive, exact engines (docs/analise/REA_rodada58.md).
  3. // Clean-room: written from the behaviour, formulas and data layouts described there; nothing of Image-Line's code or
  4. // data is copied. Arithmetic follows the plugins operation by operation (float/double steps in their order, one
  5. // rounding each, no contraction); the renderer's FTZ/DAZ matches the plugins' MXCSR.
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstring>
  10. #if defined(__clang__)
  11. #pragma clang fp contract(off)
  12. #elif defined(__GNUC__)
  13. #pragma GCC optimize("fp-contract=off")
  14. #endif
  15. namespace lynx::flbasic
  16. {
  17. namespace
  18. {
  19. constexpr double kPi = 3.141592653589793;
  20. constexpr int engineBlock = 512;
  21. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t o) noexcept
  22. {
  23. std::int32_t v = 0;
  24. std::memcpy (&v, d.data() + o, 4);
  25. return v;
  26. }
  27. std::uint32_t bitsOf (float f) noexcept
  28. {
  29. std::uint32_t b = 0;
  30. std::memcpy (&b, &f, 4);
  31. return b;
  32. }
  33. float fromBits (std::uint32_t b) noexcept
  34. {
  35. float f = 0;
  36. std::memcpy (&f, &b, 4);
  37. return f;
  38. }
  39. int rawFrom (float v, int lo, int hi) noexcept
  40. {
  41. return lo + (int) std::lround (std::clamp ((double) v, 0.0, 1.0) * (hi - lo));
  42. }
  43. float norm (int raw, int lo, int hi) noexcept { return (float) std::clamp ((double) (raw - lo) / (hi - lo), 0.0, 1.0); }
  44. // FL's level law L11 with a float ln 11 and a float 0.1 (volume 0…320, 256 = 1 exactly)
  45. float volumeGain (int raw) noexcept
  46. {
  47. const float f = (float) raw * 0.00390625f;
  48. if (f == 1.0f)
  49. return f;
  50. return (float) ((std::exp ((double) (f * 2.39789534f)) - 1.0) * (double) 0.1f);
  51. }
  52. // The host's gain-ramp step: 0.001 per sample at 44.1 kHz, scaled by 44100/fs (Balance uses 10×)
  53. float rampStep (int rate) noexcept { return (float) (44100.0 / (double) rate * 0.001); }
  54. // One gain's per-call ramp (§1.4): start, per-sample delta and the value reached at the end of the call
  55. void ramp (float& cur, float target, int n, float maxStep, float& start, float& delta) noexcept
  56. {
  57. start = cur;
  58. float d = target - start;
  59. if (bitsOf (d) == 0)
  60. {
  61. delta = 0.0f;
  62. return;
  63. }
  64. cur = target;
  65. d = d / (float) n;
  66. if (std::fabs (d) > maxStep)
  67. {
  68. d = fromBits ((bitsOf (d) & 0x80000000u) | bitsOf (maxStep));
  69. cur = start + d * (float) n;
  70. if ((bitsOf (cur) & 0x7fffffffu) < 0x33800000u) // below 2^-24: lands on 0
  71. {
  72. cur = 0.0f;
  73. d = -start / (float) n;
  74. }
  75. }
  76. delta = d;
  77. }
  78. // The stereo gain-matrix kernel: outL = L·g0 + R·g2, outR = R·g1 + L·g3, gains stepping by their deltas per frame
  79. void matrix (float* left, float* right, int n, const float* g0, const float* d) noexcept
  80. {
  81. float g[4] = { g0[0], g0[1], g0[2], g0[3] };
  82. if ((bitsOf (d[0]) | bitsOf (d[1]) | bitsOf (d[2]) | bitsOf (d[3])) != 0)
  83. {
  84. for (int i = 0; i < n; ++i)
  85. {
  86. const float l = left[i], r = right[i];
  87. left[i] = l * g[0] + r * g[2];
  88. right[i] = r * g[1] + l * g[3];
  89. for (int k = 0; k < 4; ++k)
  90. g[k] = g[k] + d[k];
  91. }
  92. return;
  93. }
  94. if ((bitsOf (g[2]) | bitsOf (g[3])) != 0)
  95. {
  96. for (int i = 0; i < n; ++i)
  97. {
  98. const float l = left[i], r = right[i];
  99. left[i] = l * g[0] + r * g[2];
  100. right[i] = r * g[1] + l * g[3];
  101. }
  102. return;
  103. }
  104. if (bitsOf (g[0]) == 0x3f800000u && bitsOf (g[1]) == 0x3f800000u)
  105. return; // in place with unit gains: untouched
  106. for (int i = 0; i < n; ++i)
  107. {
  108. left[i] = left[i] * g[0];
  109. right[i] = right[i] * g[1];
  110. }
  111. }
  112. // FL's host LFO tables (WaveTables[0…2] of the plugin host, 16384 points each)
  113. struct HostTables
  114. {
  115. std::array<std::array<float, 16384>, 3> t {};
  116. HostTables()
  117. {
  118. for (int i = 0; i < 16384; ++i)
  119. t[0][(std::size_t) i] = (float) std::sin ((double) (2 * i) * kPi / 16384.0);
  120. for (int i = 0; i < 0x2000; ++i)
  121. t[1][(std::size_t) ((i + 0x3000) & 0x3fff)] = (float) ((double) (i * 4) / 16384.0 + -1.0);
  122. for (int i = 0x2000; i < 0x4000; ++i)
  123. t[1][(std::size_t) ((i + 0x3000) & 0x3fff)] = (float) (1.0 - (double) ((i - 0x2000) * 4) / 16384.0);
  124. for (int i = 0; i < 16384; ++i)
  125. t[2][(std::size_t) i] = i < 0x2000 ? 1.0f : -1.0f;
  126. }
  127. };
  128. const HostTables& hostTables()
  129. {
  130. static const HostTables tables;
  131. return tables;
  132. }
  133. // Blood Overdrive's shaper: 65536 points, a half sine from −1 to 1 (index 0x8000 + k = sin(kπ/65536))
  134. struct OverdriveTable
  135. {
  136. std::array<float, 65536> t {};
  137. OverdriveTable()
  138. {
  139. for (int k = 0; k < 0x8000; ++k)
  140. {
  141. const auto s = (float) std::sin ((double) k * kPi / 65536.0);
  142. t[(std::size_t) (0x8000 + k)] = s;
  143. t[(std::size_t) (0x7fff - k)] = fromBits (bitsOf (s) ^ 0x80000000u);
  144. }
  145. }
  146. };
  147. const OverdriveTable& overdriveTable()
  148. {
  149. static const OverdriveTable table;
  150. return table;
  151. }
  152. } // namespace
  153. // ================================================================================================== state parsing
  154. std::optional<PanOMaticState> parsePanOMatic (const std::vector<std::uint8_t>& d)
  155. {
  156. if (d.size() < 24)
  157. return std::nullopt;
  158. return PanOMaticState { i32At (d, 0), i32At (d, 4), i32At (d, 8), i32At (d, 12), i32At (d, 16), i32At (d, 20) };
  159. }
  160. std::optional<BalanceState> parseBalance (const std::vector<std::uint8_t>& d)
  161. {
  162. if (d.size() < 8)
  163. return std::nullopt;
  164. return BalanceState { i32At (d, 0), i32At (d, 4) };
  165. }
  166. std::optional<SendState> parseSend (const std::vector<std::uint8_t>& d)
  167. {
  168. // pan, volume, dry (the plugin's three controls) and the insert it sends to (REA_rodada105_send §1)
  169. if (d.size() < 16)
  170. return std::nullopt;
  171. return SendState { i32At (d, 0), i32At (d, 4), i32At (d, 8), i32At (d, 12) };
  172. }
  173. std::optional<BloodOverdriveState> parseBloodOverdrive (const std::vector<std::uint8_t>& d)
  174. {
  175. // i32 version (the plugin reads versions < 4; 3 today), 6 × i32 controls, then (version > 2) an editor-only i32
  176. if (d.size() < 28 || i32At (d, 0) >= 4)
  177. return std::nullopt;
  178. BloodOverdriveState s;
  179. for (int i = 0; i < 6; ++i)
  180. s.raw[(std::size_t) i] = i32At (d, 4 + 4 * (std::size_t) i);
  181. return s;
  182. }
  183. namespace
  184. {
  185. constexpr int pomLo[panOMaticParams] = { 0, 0, 0, 0, 0, 0 };
  186. constexpr int pomHi[panOMaticParams] = { 256, 320, 2, 2, 128, 55000 };
  187. constexpr int odHi[bloodOverdriveParams] = { 10000, 10000, 10000, 1, 10000, 10000 };
  188. } // namespace
  189. void panOMaticToParams (const PanOMaticState& s, float* p) noexcept
  190. {
  191. const int v[6] = { s.pan, s.vol, s.shape, s.target, s.amount, s.speed };
  192. for (int i = 0; i < 6; ++i)
  193. p[i] = norm (v[i], pomLo[i], pomHi[i]);
  194. }
  195. PanOMaticState panOMaticFromParams (const float* p) noexcept
  196. {
  197. int v[6];
  198. for (int i = 0; i < 6; ++i)
  199. v[i] = rawFrom (p[i], pomLo[i], pomHi[i]);
  200. return { v[0], v[1], v[2], v[3], v[4], v[5] };
  201. }
  202. void balanceToParams (const BalanceState& s, float* p) noexcept
  203. {
  204. p[0] = norm (s.pan, -128, 128);
  205. p[1] = norm (s.vol, 0, 320);
  206. }
  207. BalanceState balanceFromParams (const float* p) noexcept { return { rawFrom (p[0], -128, 128), rawFrom (p[1], 0, 320) }; }
  208. float sendTrackValue (int track) noexcept { return norm (std::clamp (track, 0, maxMixerTracks - 1), 0, maxMixerTracks - 1); }
  209. int sendTrack (float value) noexcept { return rawFrom (value, 0, maxMixerTracks - 1); }
  210. void sendToParams (const SendState& s, float* p) noexcept
  211. {
  212. p[0] = norm (s.pan, -128, 128);
  213. p[1] = norm (s.vol, 0, 320);
  214. p[2] = norm (s.dry, 0, 256);
  215. p[3] = sendTrackValue (s.track);
  216. }
  217. SendState sendFromParams (const float* p) noexcept
  218. {
  219. return { rawFrom (p[0], -128, 128), rawFrom (p[1], 0, 320), rawFrom (p[2], 0, 256), sendTrack (p[3]) };
  220. }
  221. void bloodOverdriveToParams (const BloodOverdriveState& s, float* p) noexcept
  222. {
  223. for (int i = 0; i < 6; ++i)
  224. p[i] = norm (s.raw[(std::size_t) i], 0, odHi[i]);
  225. }
  226. BloodOverdriveState bloodOverdriveFromParams (const float* p) noexcept
  227. {
  228. BloodOverdriveState s;
  229. for (int i = 0; i < 6; ++i)
  230. s.raw[(std::size_t) i] = rawFrom (p[i], 0, odHi[i]);
  231. return s;
  232. }
  233. // =================================================================================================== param texts
  234. namespace
  235. {
  236. std::string dbText (float g)
  237. {
  238. if (g <= 0.0f)
  239. return "-inf dB";
  240. char t[32];
  241. std::snprintf (t, sizeof (t), "%+.1f dB", 20.0 * std::log10 ((double) g));
  242. return t;
  243. }
  244. std::string panText (int v, int centre, int half)
  245. {
  246. char t[32];
  247. if (v == centre)
  248. return "Centre";
  249. std::snprintf (t, sizeof (t), "%d%% %s", (int) std::lround (std::abs (v - centre) * 100.0 / half), v < centre ? "L" : "R");
  250. return t;
  251. }
  252. // the LFO period at 44.1 kHz (samples → seconds), the law of §1.2
  253. double lfoPeriodSeconds (int speed)
  254. {
  255. const int s = 65536 - speed;
  256. const double x = (double) (float) ((double) s / 65536.0) * std::log (50001.0);
  257. const float per = ((float) (std::exp (x) - 1.0) * (float) s) * (float) (2048.0 / 44100.0);
  258. return (double) std::llrint ((double) per) / 44100.0;
  259. }
  260. } // namespace
  261. std::string panOMaticParamText (int param, float v01)
  262. {
  263. if (param < 0 || param >= panOMaticParams)
  264. return {};
  265. const int v = rawFrom (v01, pomLo[param], pomHi[param]);
  266. char t[48];
  267. switch (param)
  268. {
  269. case 0: return panText (v, 128, 128);
  270. case 1: return dbText (volumeGain (v));
  271. case 2: return v == 0 ? "Sine" : v == 1 ? "Triangle" : "Square";
  272. case 3: return v == 0 ? "Off" : v == 1 ? "Pan" : "Volume";
  273. case 4: std::snprintf (t, sizeof (t), "%d%%", (int) std::lround (v * 100.0 / 128.0)); return t;
  274. default: std::snprintf (t, sizeof (t), "%.2f s", lfoPeriodSeconds (v)); return t;
  275. }
  276. }
  277. std::string balanceParamText (int param, float v01)
  278. {
  279. if (param == 0)
  280. return panText (rawFrom (v01, -128, 128), 0, 128);
  281. return dbText (volumeGain (rawFrom (v01, 0, 320)));
  282. }
  283. std::string sendParamText (int param, float v01)
  284. {
  285. char t[32];
  286. switch (param)
  287. {
  288. case 0: return panText (rawFrom (v01, -128, 128), 0, 128);
  289. case 1: return dbText (volumeGain (rawFrom (v01, 0, 320)));
  290. case 2:
  291. std::snprintf (t, sizeof (t), "%.0f%%", rawFrom (v01, 0, 256) * 100.0 / 256.0);
  292. return t;
  293. case 3:
  294. if (sendTrack (v01) <= 0)
  295. return "None";
  296. std::snprintf (t, sizeof (t), "Insert %d", sendTrack (v01));
  297. return t;
  298. default: return {};
  299. }
  300. }
  301. std::string bloodOverdriveParamText (int param, float v01)
  302. {
  303. char t[32];
  304. if (param == 3)
  305. return v01 >= 0.5f ? "On" : "Off";
  306. std::snprintf (t, sizeof (t), "%.0f%%", std::clamp ((double) v01, 0.0, 1.0) * 100.0);
  307. return t;
  308. }
  309. // ===================================================================================================== PanOMatic
  310. void PanOMaticEngine::prepare (int sampleRate) noexcept
  311. {
  312. rate = std::max (1, sampleRate);
  313. step = rampStep (rate);
  314. if (known)
  315. {
  316. auto s = raw;
  317. known = false;
  318. set (s); // the rate notification recomputes the LFO increment
  319. }
  320. }
  321. void PanOMaticEngine::set (const PanOMaticState& in) noexcept
  322. {
  323. PanOMaticState s = in;
  324. s.pan = std::clamp (s.pan, 0, 256);
  325. s.vol = std::clamp (s.vol, 0, 320);
  326. s.shape = std::clamp (s.shape, 0, 2);
  327. s.target = std::clamp (s.target, 0, 2);
  328. s.amount = std::clamp (s.amount, 0, 128);
  329. s.speed = std::clamp (s.speed, 0, 55000);
  330. const bool all = ! known;
  331. if (all || s.pan != raw.pan)
  332. pan = (float) (s.pan - 128) * 0.0078125f;
  333. if (all || s.vol != raw.vol)
  334. vol = volumeGain (s.vol);
  335. if (all || s.shape != raw.shape)
  336. table = hostTables().t[(std::size_t) s.shape].data();
  337. if (all || s.target != raw.target || s.amount != raw.amount)
  338. amount = (float) s.amount * (s.target == 1 ? 0.0078125f : 0.00390625f);
  339. if (all || s.speed != raw.speed)
  340. {
  341. // LFO period in samples: (50001^(s/65536) − 1)·s·(2048/fs), s = 65536 − speed; the phase step is 2^32/period
  342. const int sp = 65536 - s.speed;
  343. const double x = (double) (float) ((double) sp / 65536.0) * std::log (50001.0);
  344. const float per = ((float) (std::exp (x) - 1.0) * (float) sp) * (float) (2048.0 / (double) rate);
  345. const auto samples = (std::uint32_t) std::max (2LL, std::llrint ((double) per));
  346. inc = (std::uint32_t) ((1ULL << 32) / samples);
  347. }
  348. if (all || s.pan != raw.pan || s.vol != raw.vol || s.shape != raw.shape || s.target != raw.target
  349. || s.amount != raw.amount || s.speed != raw.speed)
  350. dirty = true;
  351. raw = s;
  352. known = true;
  353. }
  354. void PanOMaticEngine::gains (float p, float v) noexcept
  355. {
  356. if (p >= 0.0f)
  357. {
  358. const float a = 1.0f - p;
  359. target[3] = ((1.0f - a) * 0.5f) * v;
  360. target[0] = a * v;
  361. target[1] = v - target[3];
  362. target[2] = 0.0f;
  363. }
  364. else
  365. {
  366. const float b = 1.0f + p;
  367. target[2] = ((1.0f - b) * 0.5f) * v;
  368. target[1] = b * v;
  369. target[0] = v - target[2];
  370. target[3] = 0.0f;
  371. }
  372. dirty = false;
  373. }
  374. void PanOMaticEngine::flush() noexcept
  375. {
  376. cur = target;
  377. phase = 0;
  378. }
  379. void PanOMaticEngine::prime() noexcept
  380. {
  381. float l = 0.0f, r = 0.0f;
  382. process (&l, &r, 1);
  383. flush();
  384. }
  385. void PanOMaticEngine::process (float* left, float* right, int n) noexcept
  386. {
  387. if (n <= 0)
  388. return;
  389. if (raw.target > 0 && table != nullptr)
  390. {
  391. phase += (std::uint32_t) ((std::uint64_t) inc * (std::uint64_t) n);
  392. const float l = table[phase >> 18] * amount;
  393. if (raw.target == 1)
  394. {
  395. float p = l + pan;
  396. if (! (-1.0f <= p))
  397. p = -1.0f;
  398. else if (p > 1.0f)
  399. p = 1.0f;
  400. gains (p, vol);
  401. }
  402. else
  403. {
  404. float v = l + vol;
  405. if (! (0.0f <= v))
  406. v = 0.0f;
  407. gains (pan, v);
  408. }
  409. }
  410. else if (dirty)
  411. gains (pan, vol);
  412. float start[4], delta[4];
  413. for (int k = 0; k < 4; ++k)
  414. ramp (cur[(std::size_t) k], target[(std::size_t) k], n, step, start[k], delta[k]);
  415. matrix (left, right, n, start, delta);
  416. }
  417. // ======================================================================================================= Balance
  418. void BalanceEngine::prepare (int sampleRate) noexcept { step = rampStep (std::max (1, sampleRate)) * 10.0f; }
  419. void BalanceEngine::set (const BalanceState& in) noexcept
  420. {
  421. BalanceState s { std::clamp (in.pan, -128, 128), std::clamp (in.vol, 0, 320) };
  422. if (known && s.pan == raw.pan && s.vol == raw.vol)
  423. return;
  424. raw = s;
  425. known = true;
  426. const double a = (double) ((float) (s.pan + 128) * 0.00390625f);
  427. const double b = (double) ((float) s.pan * 0.0078125f);
  428. const double c190 = 1.0 / (std::cos (0.78539816339744828) * 10.0);
  429. const double k = (1.0 - std::sin (std::fabs (a - 0.5) * kPi) * 0.5) * c190;
  430. const double r = (double) s.vol / 320.0;
  431. const auto x = (float) (r * 1.25);
  432. const auto G = (float) (std::exp ((double) x * std::log (11.0)) - 1.0);
  433. const double g = (double) G * k;
  434. const double angle = (b + 1.0) * 0.78539816339744828;
  435. const double sn = std::sin (angle), cs = std::cos (angle);
  436. target = { g * cs, g * sn, 0.0, 0.0 };
  437. if (a >= 0.5)
  438. target[3] = target[1] - target[0];
  439. else
  440. target[2] = target[0] - target[1];
  441. }
  442. void BalanceEngine::flush() noexcept
  443. {
  444. for (int k = 0; k < 4; ++k)
  445. cur[(std::size_t) k] = (float) target[(std::size_t) k];
  446. }
  447. void BalanceEngine::process (float* left, float* right, int n) noexcept
  448. {
  449. if (n <= 0)
  450. return;
  451. float start[4], delta[4];
  452. for (int k = 0; k < 4; ++k)
  453. ramp (cur[(std::size_t) k], (float) target[(std::size_t) k], n, step, start[k], delta[k]);
  454. matrix (left, right, n, start, delta);
  455. }
  456. // ========================================================================================================== Send
  457. // REA_rodada105_send §2: the pan and the volume give a 2×2 matrix (one side keeps its gain, the other fades and
  458. // half of what it loses is folded into the kept side); the send is the input through that matrix, ramped like
  459. // Balance (host step +0x34, 0.001 per sample at 44.1 kHz), added into the destination's buffer; then the signal
  460. // continues × dry, without a ramp. Both levels use the L11 law.
  461. void SendEngine::prepare (int sampleRate) noexcept { step = rampStep (std::max (1, sampleRate)); }
  462. void SendEngine::set (const SendState& in) noexcept
  463. {
  464. const SendState s { std::clamp (in.pan, -128, 128), std::clamp (in.vol, 0, 320), std::clamp (in.dry, 0, 256), in.track };
  465. if (known && s.pan == raw.pan && s.vol == raw.vol && s.dry == raw.dry && s.track == raw.track)
  466. return;
  467. if (! known || s.pan != raw.pan)
  468. {
  469. pan = (float) s.pan * 0.0078125f;
  470. dirty = true;
  471. }
  472. if (! known || s.vol != raw.vol)
  473. {
  474. volume = volumeGain (s.vol);
  475. dirty = true;
  476. }
  477. if (! known || s.dry != raw.dry)
  478. dry = volumeGain (s.dry);
  479. raw = s;
  480. known = true;
  481. }
  482. void SendEngine::flush() noexcept
  483. {
  484. dirty = true;
  485. // the targets now (only computed while an insert is chosen, as in FL), the current gains at them
  486. process (nullptr, nullptr, nullptr, nullptr, 0);
  487. cur = target;
  488. }
  489. void SendEngine::process (const float* left, const float* right, float* sendLeft, float* sendRight, int n) noexcept
  490. {
  491. if (raw.track <= 0)
  492. return;
  493. if (dirty)
  494. {
  495. if (pan < 0.0f)
  496. {
  497. const float kept = pan + 1.0f;
  498. target[2] = ((1.0f - kept) * 0.5f) * volume;
  499. target[1] = kept * volume;
  500. target[0] = volume - target[2];
  501. target[3] = 0.0f;
  502. }
  503. else
  504. {
  505. const float kept = 1.0f - pan;
  506. target[3] = ((1.0f - kept) * 0.5f) * volume;
  507. target[0] = kept * volume;
  508. target[1] = volume - target[3];
  509. target[2] = 0.0f;
  510. }
  511. dirty = false;
  512. }
  513. if (n <= 0 || left == nullptr || sendLeft == nullptr)
  514. return;
  515. float g[4], d[4];
  516. for (int k = 0; k < 4; ++k)
  517. ramp (cur[(std::size_t) k], target[(std::size_t) k], n, step, g[k], d[k]);
  518. if ((bitsOf (d[0]) | bitsOf (d[1]) | bitsOf (d[2]) | bitsOf (d[3])) != 0)
  519. {
  520. for (int i = 0; i < n; ++i)
  521. {
  522. const float l = left[i], r = right[i];
  523. sendLeft[i] = l * g[0] + r * g[2] + sendLeft[i];
  524. sendRight[i] = r * g[1] + l * g[3] + sendRight[i];
  525. for (int k = 0; k < 4; ++k)
  526. g[k] = g[k] + d[k];
  527. }
  528. }
  529. else if ((bitsOf (g[2]) | bitsOf (g[3])) != 0)
  530. {
  531. for (int i = 0; i < n; ++i)
  532. {
  533. const float l = left[i], r = right[i];
  534. sendLeft[i] = l * g[0] + r * g[2] + sendLeft[i];
  535. sendRight[i] = r * g[1] + l * g[3] + sendRight[i];
  536. }
  537. }
  538. else if ((bitsOf (g[0]) | bitsOf (g[1])) != 0)
  539. {
  540. for (int i = 0; i < n; ++i)
  541. {
  542. sendLeft[i] = left[i] * g[0] + sendLeft[i];
  543. sendRight[i] = right[i] * g[1] + sendRight[i];
  544. }
  545. }
  546. }
  547. // =============================================================================================== Blood Overdrive
  548. namespace
  549. {
  550. const float kSmooth = fromBits (0x3f7fef9eu), kSmoothIn = fromBits (0x3983126fu), kClip = fromBits (0x3f7ffcb9u);
  551. float shape (float x) noexcept
  552. {
  553. const double v = (double) (x * 32767.0f);
  554. const long long t = std::fabs (v) < 9.2233720368547758e18 ? (long long) v : (long long) (1ULL << 63); // cvttsd2si
  555. const auto i = (std::int32_t) ((std::uint32_t) t + 0x8000u);
  556. return overdriveTable().t[(std::size_t) std::clamp (i, 0, 0xffff)];
  557. }
  558. } // namespace
  559. void BloodOverdriveEngine::derive() noexcept
  560. {
  561. const float pb = p[0], x100 = p[3], col = p[1], pre = p[2], filt = p[4], post = p[5];
  562. const float c148 = (pre * pre) * pre;
  563. c17c = ((col * col) * col) * kSmoothIn;
  564. const float sw = (double) x100 <= 0.5 ? 25.0f : 2500.0f;
  565. c14c = (sw * c148 + 1.0f) * kSmoothIn;
  566. c150 = (1.0f - pb) * kSmoothIn;
  567. c154 = (post * post) * kSmoothIn;
  568. c164 = filt * 0.7f + fromBits (0x3e4ccccdu);
  569. c168 = 1.0f - c164;
  570. }
  571. void BloodOverdriveEngine::set (const BloodOverdriveState& s) noexcept
  572. {
  573. for (int i = 0; i < 6; ++i)
  574. {
  575. const double d = (double) s.raw[(std::size_t) i] * (i == 3 ? 1.0 : 1.0 / 10000.0);
  576. p[(std::size_t) i] = (float) std::clamp (d, 0.0, 1.0);
  577. }
  578. raw = s;
  579. derive();
  580. }
  581. void BloodOverdriveEngine::start (int sampleRate, const BloodOverdriveState& s) noexcept
  582. {
  583. s158 = s15c = s160 = 1.0f;
  584. s178 = 0.0f;
  585. std::fill (std::begin (L), std::end (L), 0.0f);
  586. std::fill (std::begin (R), std::end (R), 0.0f);
  587. std::fill (std::begin (lp), std::end (lp), 0.0f);
  588. set (s);
  589. for (int i = 0; i < sampleRate; ++i)
  590. smooth();
  591. }
  592. void BloodOverdriveEngine::smooth() noexcept
  593. {
  594. s158 = s158 * kSmooth + c14c;
  595. s15c = s15c * kSmooth + c150;
  596. s160 = s160 * kSmooth + c154;
  597. s178 = s178 * kSmooth + c17c;
  598. }
  599. float BloodOverdriveEngine::channel (float* st, float x) noexcept
  600. {
  601. // st: low, band, high (state-variable filter, coefficient s178), then a, b, c (the shaper's three taps)
  602. st[0] = st[0] + s178 * st[1];
  603. st[2] = (x - st[0]) - st[1];
  604. st[1] = st[1] + s178 * st[2];
  605. st[3] = st[3] + st[4] * 0.25f;
  606. st[5] = ((((st[2] + st[0]) * s15c + st[1]) * s158) - st[3]) - st[4];
  607. st[4] = st[4] + st[5] * 0.25f;
  608. float y = ((shape (st[3]) + shape (st[4])) + shape (st[5])) * s160;
  609. if (y > kClip)
  610. y = kClip;
  611. if (y < -kClip)
  612. y = -kClip;
  613. return y;
  614. }
  615. void BloodOverdriveEngine::process (float* left, float* right, int n) noexcept
  616. {
  617. for (int i = 0; i < n; ++i)
  618. {
  619. smooth();
  620. const float yl = channel (L, left[i]);
  621. lp[0] = yl * c168 + lp[0] * c164;
  622. lp[1] = lp[0] * c168 + lp[1] * c164;
  623. left[i] = lp[1];
  624. const float yr = channel (R, right[i]);
  625. lp[2] = yr * c168 + lp[2] * c164;
  626. lp[3] = lp[2] * c168 + lp[3] * c164;
  627. right[i] = lp[3];
  628. }
  629. }
  630. // ================================================================================================ mixer effects
  631. namespace
  632. {
  633. class PanOMaticEffect final : public Effect
  634. {
  635. public:
  636. void prepare (double sampleRate) noexcept override
  637. {
  638. rate = (int) std::lround (sampleRate);
  639. ready = false;
  640. }
  641. void reset() noexcept override
  642. {
  643. if (ready)
  644. engine.flush();
  645. }
  646. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  647. {
  648. if (! ready)
  649. {
  650. engine = PanOMaticEngine();
  651. engine.prepare (rate);
  652. engine.set (panOMaticFromParams (params.data()));
  653. engine.prime();
  654. ready = true;
  655. }
  656. else
  657. engine.set (panOMaticFromParams (params.data()));
  658. for (int done = 0; done < n;)
  659. {
  660. const int m = std::min (engineBlock, n - done);
  661. engine.process (left + done, right + done, m);
  662. done += m;
  663. }
  664. }
  665. private:
  666. PanOMaticEngine engine;
  667. int rate = 44100;
  668. bool ready = false;
  669. };
  670. class BalanceEffect final : public Effect
  671. {
  672. public:
  673. void prepare (double sampleRate) noexcept override
  674. {
  675. rate = (int) std::lround (sampleRate);
  676. ready = false;
  677. }
  678. void reset() noexcept override
  679. {
  680. if (ready)
  681. engine.flush();
  682. }
  683. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  684. {
  685. engine.set (balanceFromParams (params.data()));
  686. if (! ready)
  687. {
  688. engine.prepare (rate);
  689. engine.flush();
  690. ready = true;
  691. }
  692. for (int done = 0; done < n;)
  693. {
  694. const int m = std::min (engineBlock, n - done);
  695. engine.process (left + done, right + done, m);
  696. done += m;
  697. }
  698. }
  699. private:
  700. BalanceEngine engine;
  701. int rate = 44100;
  702. bool ready = false;
  703. };
  704. class SendEffect final : public Effect
  705. {
  706. public:
  707. void prepare (double sampleRate) noexcept override
  708. {
  709. rate = (int) std::lround (sampleRate);
  710. ready = false;
  711. }
  712. void reset() noexcept override
  713. {
  714. if (ready)
  715. engine.flush();
  716. }
  717. void setSendOutput (float* left, float* right) noexcept override
  718. {
  719. outL = left;
  720. outR = right;
  721. }
  722. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  723. {
  724. engine.set (sendFromParams (params.data()));
  725. if (! ready)
  726. {
  727. engine.prepare (rate);
  728. engine.flush();
  729. ready = true;
  730. }
  731. for (int done = 0; done < n;)
  732. {
  733. const int m = std::min (engineBlock, n - done);
  734. engine.process (left + done, right + done, outL != nullptr ? outL + done : nullptr, outR != nullptr ? outR + done : nullptr, m);
  735. done += m;
  736. }
  737. outL = outR = nullptr; // given again before every block
  738. const float g = engine.dryGain();
  739. if (bitsOf (g) == 0x3f800000u)
  740. return;
  741. for (int i = 0; i < n; ++i)
  742. {
  743. left[i] = left[i] * g;
  744. right[i] = right[i] * g;
  745. }
  746. }
  747. private:
  748. SendEngine engine;
  749. float* outL = nullptr;
  750. float* outR = nullptr;
  751. int rate = 44100;
  752. bool ready = false;
  753. };
  754. class BloodOverdriveEffect final : public Effect
  755. {
  756. public:
  757. void prepare (double sampleRate) noexcept override
  758. {
  759. rate = (int) std::lround (sampleRate);
  760. ready = false;
  761. }
  762. void reset() noexcept override {} // the plugin has no flush: its filters keep their state
  763. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  764. {
  765. const auto s = bloodOverdriveFromParams (params.data());
  766. if (! ready)
  767. {
  768. engine.start (rate, s);
  769. current = s.raw;
  770. ready = true;
  771. }
  772. else if (s.raw != current)
  773. {
  774. engine.set (s);
  775. current = s.raw;
  776. }
  777. engine.process (left, right, n);
  778. }
  779. private:
  780. BloodOverdriveEngine engine;
  781. std::array<int, 6> current {};
  782. int rate = 44100;
  783. bool ready = false;
  784. };
  785. } // namespace
  786. std::unique_ptr<Effect> makePanOMatic() { return std::make_unique<PanOMaticEffect>(); }
  787. std::unique_ptr<Effect> makeBalance() { return std::make_unique<BalanceEffect>(); }
  788. std::unique_ptr<Effect> makeBloodOverdrive() { return std::make_unique<BloodOverdriveEffect>(); }
  789. std::unique_ptr<Effect> makeSend() { return std::make_unique<SendEffect>(); }
  790. } // namespace lynx::flbasic