HarmlessFx.cpp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  1. #include "core/HarmlessFx.h"
  2. #include "core/PlateReverb.h"
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <deque>
  7. // Specified as float32 / double operations in the order of the reference model (REA_rodada36); contraction of a·b + c
  8. // into one FMA would change the roundings, so it stays off in this file.
  9. #if defined(__clang__)
  10. #pragma clang fp contract(off)
  11. #elif defined(__GNUC__)
  12. #pragma GCC optimize ("fp-contract=off")
  13. #endif
  14. namespace lynx::hlfx
  15. {
  16. namespace
  17. {
  18. constexpr double pi = 3.141592653589793;
  19. constexpr float flushLevel = 5.960464477539063e-08f; // 2^-24: FL's denormal guard
  20. const double ln001 = std::log (0.01);
  21. inline float f32 (double x) noexcept { return (float) x; }
  22. inline double rint (double x) noexcept { return std::nearbyint (x); } // round half to even (cvtsd2si)
  23. // FUN_00627cc0 (x87): a double state below 2^-24 in magnitude becomes 0; the others are kept as they are (the
  24. // decompiler shows a float conversion that the instructions do not do: fldl / fcmovb / fstpl, REA_rodada40 §2.4)
  25. inline double flushState (double v) noexcept { return std::fabs (v) < (double) flushLevel ? 0.0 : v; }
  26. // (11^x − 1)/10, exactly 1 at x = 1 (ln 11 as the float of the plugin's run-time global)
  27. float g11 (float x) noexcept
  28. {
  29. if (x == 1.0f)
  30. return 1.0f;
  31. return f32 ((std::exp ((double) x * (double) (float) std::log (11.0)) - 1.0) * (double) 0.1f);
  32. }
  33. // 1 − (5^x − 1)/4 (chorus dry and wet gains)
  34. float g5 (float x) noexcept
  35. {
  36. return f32 (1.0 - (double) f32 (std::exp ((double) x * std::log (5.0)) - 1.0) * 0.25);
  37. }
  38. // FUN_00612db0: start value, per-sample step (limited to maxStep) and new current value
  39. struct Ramp
  40. {
  41. double start = 0.0, step = 0.0, end = 0.0;
  42. };
  43. Ramp ramp (double cur, double target, int n, double maxStep) noexcept
  44. {
  45. const auto d = (double) f32 (target - cur);
  46. if (d == 0.0)
  47. return { cur, 0.0, cur };
  48. auto step = (double) f32 (d / n);
  49. auto end = target;
  50. if (std::fabs (step) > maxStep)
  51. {
  52. step = std::copysign (maxStep, step);
  53. end = (double) f32 (cur + step * n);
  54. }
  55. return { cur, step, end };
  56. }
  57. std::int32_t i32At (const std::vector<std::uint8_t>& d, std::size_t at) noexcept
  58. {
  59. std::int32_t v = 0;
  60. if (at + 4 <= d.size())
  61. std::memcpy (&v, d.data() + at, 4);
  62. return v;
  63. }
  64. } // namespace
  65. // =================================================================================================================
  66. // Maximus (REA_rodada36 §2)
  67. // =================================================================================================================
  68. namespace
  69. {
  70. // 2·(501^(v/10000) − 1) ms, kept as the float of (501^x − 1)
  71. double tms (int v) noexcept
  72. {
  73. return (double) f32 (std::exp ((double) f32 ((float) v * 0.0001f) * std::log (501.0)) - 1.0);
  74. }
  75. // L7: (7^x − 1)/6, exactly 1 at x = 1
  76. float l7 (int v) noexcept
  77. {
  78. const auto x = (float) v * 0.001f;
  79. if (x == 1.0f)
  80. return 1.0f;
  81. return f32 ((std::exp ((double) (x * (float) std::log (7.0))) - 1.0) * (double) (1.0f / 6.0f));
  82. }
  83. double coefFor (double n) noexcept // 1 % in n samples
  84. {
  85. return n > 0.0 ? std::exp (ln001 / std::max (1.0, n)) : 0.0;
  86. }
  87. struct Point
  88. {
  89. double dx = 0.0;
  90. float y = 0.0f, tension = 0.0f, q = 0.0f, b = 0.0f, c = 0.0f;
  91. int type = 0, sign = 0;
  92. };
  93. // one drawn envelope (the compression curve) and its incremental evaluation (FUN_007885d0)
  94. class Curve
  95. {
  96. public:
  97. explicit Curve (std::vector<Point> pts) : p (std::move (pts)) {}
  98. bool supported() const noexcept
  99. {
  100. if (p.empty())
  101. return false;
  102. for (std::size_t i = 1; i < p.size(); ++i)
  103. if (p[i].type != 0 && p[i].type != 7)
  104. return false;
  105. return true;
  106. }
  107. void start() noexcept
  108. {
  109. idx = 0;
  110. y0 = p[0].y;
  111. dy = 0.0;
  112. if (p[0].dx == 0.0)
  113. rate = 1.0, t = 1.0;
  114. else
  115. rate = 1.0 / p[0].dx, t = 0.0;
  116. }
  117. double step (double dx) noexcept
  118. {
  119. const auto n = (int) p.size();
  120. t += dx * rate;
  121. while (t >= 1.0)
  122. {
  123. const auto old = idx;
  124. t = rate == 0.0 ? dx : (t - 1.0) / rate;
  125. y0 = p[(std::size_t) idx].y;
  126. if (idx < n - 1)
  127. ++idx;
  128. dy = (double) p[(std::size_t) idx].y - y0;
  129. if (old == idx)
  130. t = 0.0, rate = 0.0;
  131. else if (const auto d = p[(std::size_t) idx].dx; d <= 0.0)
  132. rate = 1.0, t += 1.0;
  133. else
  134. rate = 1.0 / d, t *= rate;
  135. }
  136. auto s = t;
  137. const auto& pt = p[(std::size_t) idx];
  138. if (pt.sign != 2)
  139. {
  140. if (pt.type == 0)
  141. {
  142. if (pt.q > 0.0f)
  143. s = 1.0 - (std::exp ((1.0 - s) * (double) pt.c) - 1.0) * (double) pt.b;
  144. else if (pt.q < 0.0f)
  145. s = (std::exp (s * (double) pt.c) - 1.0) * (double) pt.b;
  146. }
  147. else // type 7
  148. {
  149. const auto u = pt.sign > 0 ? 1.0 - s : s;
  150. const auto u3 = u * u * u;
  151. s = s - (u3 * u3 - u) * (double) pt.tension;
  152. }
  153. }
  154. return y0 + s * dy;
  155. }
  156. private:
  157. std::vector<Point> p;
  158. int idx = 0;
  159. double t = 0.0, rate = 0.0, y0 = 0.0, dy = 0.0;
  160. };
  161. } // namespace
  162. struct MaximusPreset
  163. {
  164. std::array<int, 59> p {};
  165. int oversampling = 0; // order: ×2^order
  166. float scale = 0.975f; // output ceiling of the curves (−0.22 dB)
  167. struct Band
  168. {
  169. std::vector<float> gain; // static gain (output / input) for input levels i/8192
  170. int size = 0; // last entry before the curve reaches the ceiling
  171. double end = 0.0, peak = 1.0;
  172. std::vector<float> sat; // soft-saturation table (mode A), 65 538 entries; empty = none
  173. float satOut = 0.0f, satIn = 0.0f;
  174. int satB = 0; // 1: saturation mode B (REA_rodada40 §2.2) with threshold satT and range satR
  175. float satT = 0.0f, satR = 1.0f, satK = 1.0f;
  176. int mode = 3; // effective mode (solo applied, FUN_008417e0)
  177. bool rms = false; // envelope mode RMS (REA_rodada40 §2.1)
  178. float pre = 1.0f, post = 1.0f, g20 = 1.0f;
  179. std::array<float, 4> matrix { 1.0f, 1.0f, 0.0f, 0.0f };
  180. };
  181. std::array<Band, 4> band {};
  182. bool valid = false;
  183. bool legacyFir = false; // "Linear-phase filters (legacy mode)": windowed-sinc FIR band split (FUN_0083c200)
  184. bool linearPhaseSplit = false; // the v14+ linear-phase band split option: not decoded, the normal crossover is used
  185. MaximusBuild build = MaximusBuild::shared;
  186. };
  187. namespace
  188. {
  189. std::vector<Point> readCurve (const std::vector<std::uint8_t>& d, std::size_t& pos, bool& ok)
  190. {
  191. std::vector<Point> pts;
  192. const auto n = i32At (d, pos + 8);
  193. if (n < 1 || n > 4096 || pos + 12 + 24 * (std::size_t) n + 20 > d.size())
  194. {
  195. ok = false;
  196. return pts;
  197. }
  198. for (int k = 0; k < n; ++k)
  199. {
  200. const auto at = pos + 12 + 24 * (std::size_t) k;
  201. double dx = 0.0, y = 0.0;
  202. float ten = 0.0f;
  203. std::memcpy (&dx, d.data() + at, 8);
  204. std::memcpy (&y, d.data() + at + 8, 8);
  205. std::memcpy (&ten, d.data() + at + 16, 4);
  206. Point pt;
  207. pt.dx = std::isfinite (dx) && dx > 0.0 ? dx : 0.0;
  208. pt.y = std::isfinite (y) ? (float) y : 0.0f;
  209. pt.tension = std::isfinite (ten) ? ten : 0.0f;
  210. pt.type = d[at + 20];
  211. pt.sign = pt.tension == 0.0f ? 0 : (pt.tension < 0.0f ? -1 : 1);
  212. if (k > 0)
  213. {
  214. if (pt.type < 2)
  215. {
  216. auto q = f32 (std::exp ((double) std::fabs (pt.tension) * std::log (1000001.0)) - 1.0);
  217. if (pt.tension < 0.0f)
  218. q = -q;
  219. pt.q = q;
  220. if (q != 0.0f)
  221. {
  222. pt.b = f32 (1.0 / std::fabs ((double) q));
  223. pt.c = f32 (std::log (std::fabs ((double) q) + 1.0));
  224. }
  225. }
  226. if ((pt.type == 0 || pt.type == 7) && pt.tension == 0.0f)
  227. pt.sign = 2; // straight segment
  228. }
  229. pts.push_back (pt);
  230. }
  231. pos += 12 + 24 * (std::size_t) n + 20;
  232. return pts;
  233. }
  234. // FUN_00845b50
  235. void buildGainTable (Curve& curve, float scale, MaximusPreset::Band& b, bool soundgoodizer)
  236. {
  237. if (soundgoodizer)
  238. {
  239. // Soundgoodizer_x64.dll's older build (FUN_0083b050, REA_rodada40 §3.2): the saturated output snaps to the
  240. // double 0.975 (not the float), the stored end value is the last output before the snap and the peak is
  241. // divided by the float 0.975
  242. curve.start();
  243. b.gain.assign (32770, 0.0f);
  244. const auto dv9 = 0.5 / std::log (3.0), dv10 = std::log (3.0) * 2.0, half = 0.48750001192092896;
  245. double prev = 0.0, out = 0.0, mx = 0.0, last = 0.0;
  246. int lastOk = 0;
  247. for (int i = 0; i < 32769; ++i)
  248. {
  249. const auto lv = (double) std::max (i, 1);
  250. const auto c = std::log (lv * 0.0001220703125 * 2.0 + 1.0) * dv9;
  251. const auto y = curve.step (c - prev);
  252. prev = c;
  253. out = (std::exp (y * dv10) - 1.0) * half;
  254. auto idx = i;
  255. auto keep = out;
  256. if (std::fabs (out - 0.975) < 5.960464477539063e-08)
  257. out = 0.975, idx = lastOk, keep = last;
  258. mx = std::max (mx, out);
  259. b.gain[(std::size_t) i] = f32 (out / (lv * 0.0001220703125));
  260. lastOk = idx;
  261. last = keep;
  262. }
  263. b.gain[(std::size_t) lastOk + 1] = b.gain[(std::size_t) lastOk];
  264. b.gain.resize ((std::size_t) lastOk + 2);
  265. b.size = lastOk;
  266. b.end = last;
  267. b.peak = mx / 0.9750000238418579;
  268. return;
  269. }
  270. curve.start();
  271. b.gain.assign (32770, 0.0f);
  272. const auto dv9 = 0.5 / std::log (3.0), dv10 = std::log (3.0) * 2.0, dv14 = (double) scale * 0.5;
  273. double prev = 0.0, out = 0.0, mx = 0.0;
  274. int lastOk = 0;
  275. for (int i = 0; i < 32769; ++i)
  276. {
  277. const auto lv = (double) std::max (i, 1);
  278. const auto c = std::log (lv * 0.0001220703125 * 2.0 + 1.0) * dv9;
  279. const auto y = curve.step (c - prev);
  280. prev = c;
  281. out = (std::exp (y * dv10) - 1.0) * dv14;
  282. auto idx = i;
  283. if (std::fabs (out - (double) scale) < 5.960464477539063e-08)
  284. out = (double) scale, idx = lastOk;
  285. mx = std::max (mx, out);
  286. b.gain[(std::size_t) i] = f32 (out / (lv * 0.0001220703125));
  287. lastOk = idx;
  288. }
  289. b.gain[(std::size_t) lastOk + 1] = b.gain[(std::size_t) lastOk];
  290. b.gain.resize ((std::size_t) lastOk + 2);
  291. b.size = lastOk;
  292. b.end = out;
  293. b.peak = mx / (double) scale;
  294. }
  295. // FUN_00841380, mode A (threshold < 0)
  296. void buildSaturation (int thr, float ceil, double top, MaximusPreset::Band& b, bool soundgoodizer)
  297. {
  298. const auto p = (double) f32 ((float) (20.0f - f32 ((float) std::log ((double) f32 (f32 (thr * -0.1) + 1.0f)) / std::log (101.0)) * 19.0f));
  299. const auto ip = 1.0 / p;
  300. auto d = 1.0;
  301. if (ceil > 0.0f)
  302. d = top / ((double) ceil * 65536.0);
  303. const auto norm = 1.0 / std::pow (std::atan (std::pow (d * 65536.0, p)), ip);
  304. b.sat.assign (65538, 0.0f);
  305. for (int i = 0; i <= 65536; ++i)
  306. {
  307. auto v = std::pow (std::atan (std::pow (i * d, p)), ip) * norm;
  308. if (soundgoodizer)
  309. {
  310. // FUN_00837d90: a 16-bit table, round(v·32767) clamped to ±32767, read back ×ceiling/32768
  311. const auto q = std::clamp (std::nearbyint (v * 32767.0), -32767.0, 32767.0);
  312. b.sat[(std::size_t) i] = (float) q;
  313. continue;
  314. }
  315. if (v < 5.960464477539063e-08)
  316. v = 0.0;
  317. b.sat[(std::size_t) i] = (float) v;
  318. }
  319. b.sat[65537] = b.sat[65536];
  320. b.satOut = soundgoodizer ? f32 ((double) ceil * 3.0517578125e-05) : ceil;
  321. // (post gain and ceiling both at 0 left top = 0: an infinite input scale, then 0 · inf = NaN read as a table index.
  322. // Round 111: the scale stays finite; that band is silent anyway, its output scale being the ceiling 0.)
  323. b.satIn = top > 0.0 ? f32 (1.0 / top) : 0.0f;
  324. }
  325. } // namespace
  326. std::optional<std::vector<std::uint8_t>> maximusStateFromFst (const std::vector<std::uint8_t>& b)
  327. {
  328. if (b.size() < 22 || std::memcmp (b.data(), "FLhd", 4) != 0)
  329. return std::nullopt;
  330. auto pos = 8 + (std::size_t) std::max (0, i32At (b, 4));
  331. if (pos + 8 > b.size() || std::memcmp (b.data() + pos, "FLdt", 4) != 0)
  332. return std::nullopt;
  333. pos += 8;
  334. while (pos < b.size())
  335. {
  336. const auto id = b[pos++];
  337. std::size_t len = 0;
  338. if (id < 64)
  339. len = 1;
  340. else if (id < 128)
  341. len = 2;
  342. else if (id < 192)
  343. len = 4;
  344. else
  345. {
  346. int shift = 0;
  347. while (pos < b.size())
  348. {
  349. const auto c = b[pos++];
  350. len |= (std::size_t) (c & 0x7f) << shift;
  351. shift += 7;
  352. if (! (c & 0x80) || shift > 28)
  353. break;
  354. }
  355. }
  356. if (pos + len > b.size())
  357. return std::nullopt;
  358. if (id == 0xD5)
  359. return std::vector<std::uint8_t> (b.begin() + (std::ptrdiff_t) pos, b.begin() + (std::ptrdiff_t) (pos + len));
  360. pos += len;
  361. }
  362. return std::nullopt;
  363. }
  364. std::shared_ptr<const MaximusPreset> makeMaximusPreset (const std::vector<std::uint8_t>& d)
  365. {
  366. return makeMaximusPreset (d, MaximusBuild::shared);
  367. }
  368. std::shared_ptr<const MaximusPreset> makeMaximusPreset (const std::vector<std::uint8_t>& d, MaximusBuild build)
  369. {
  370. const auto sgz = build == MaximusBuild::soundgoodizer;
  371. if (sgz && (std::uint32_t) i32At (d, 0) > 13)
  372. return nullptr; // that build's loader stops after the fixed block (no v14+ options)
  373. if (d.size() < 0x1fe + 12)
  374. return nullptr;
  375. auto m = std::make_shared<MaximusPreset>();
  376. m->build = build;
  377. const auto header = (std::uint32_t) i32At (d, 0);
  378. const auto version = header > 0xFFFFu ? (int) (header >> 16) : (int) header;
  379. if (version < 3)
  380. return nullptr; // the pre-v3 piecewise layout is not used by any preset or demo state
  381. // fixed block read raw into the object (FUN_0083ef50): 0x1FA bytes for v ≥ 7, 0x1F9 (v6), 0x1F5 (v5), 0x1F1 (v4),
  382. // 0x1EE (v3); the bytes the block does not reach keep the plugin's reset values (0)
  383. const std::size_t block = version >= 7 ? 0x1fa : version == 6 ? 0x1f9 : version == 5 ? 0x1f5 : version == 4 ? 0x1f1 : 0x1ee;
  384. if (d.size() < 4 + block)
  385. return nullptr;
  386. auto byteAt = [&] (std::size_t at) { return at < 4 + block ? (int) d[at] : 0; };
  387. for (int i = 0; i < 59; ++i)
  388. m->p[(std::size_t) i] = i32At (d, 4 + 4 * (std::size_t) i);
  389. m->oversampling = byteAt (0x1f4) | (byteAt (0x1f5) << 8) | (byteAt (0x1f6) << 16) | (byteAt (0x1f7) << 24);
  390. if (m->oversampling < 0 || m->oversampling > 1)
  391. return nullptr; // only ×1 and ×2 are used by the presets and the demo projects
  392. // menu options read with the block: "Master Mid mode" (+0x324) is not decoded for rendering; the legacy
  393. // linear-phase FIR crossover (+0x325) and solo (+0x317) of the editor's band (+0x308) are (REA_rodada40 §2.5)
  394. if (byteAt (0x1fc) != 0)
  395. return nullptr;
  396. m->legacyFir = byteAt (0x1fd) != 0;
  397. const auto solo = byteAt (0x1ef) != 0;
  398. const auto soloBand = i32At (d, 0x1e0);
  399. std::size_t pos = 4 + block;
  400. if (version >= 14)
  401. {
  402. if ((d[pos + 8] & 0x40) != 0)
  403. m->scale = 1.0f;
  404. // linear-phase band split (through Frequency Splitter's engine_x64.dll) is not decoded: the exact engine
  405. // with its normal crossover is far closer than the generic limiter (REA_rodada89 §4.3: LollieVox −5.0 → +0.5 dB)
  406. m->linearPhaseSplit = (d[pos + 8] & 0x01) != 0;
  407. pos += 9 + 20;
  408. }
  409. if (version >= 15)
  410. pos += 4;
  411. std::array<std::vector<Point>, 8> env;
  412. bool ok = true;
  413. for (auto& e : env)
  414. if (ok)
  415. e = readCurve (d, pos, ok);
  416. if (! ok)
  417. return nullptr;
  418. const auto& p = m->p;
  419. for (int b = 0; b < 4; ++b)
  420. {
  421. auto q = [&] (int j) { return p[(std::size_t) (13 * b + j)]; };
  422. auto& B = m->band[(std::size_t) b];
  423. const auto mode = q (7);
  424. B.pre = l7 (q (8));
  425. B.post = l7 (q (9));
  426. // stereo separation (FUN_00616320 with pan 0, volume 1): positive narrows toward mono, negative widens
  427. if (const auto sep = (float) q (10) * 0.001f; sep != 0.0f)
  428. {
  429. auto s = sep < 0.0f ? sep : f32 ((double) f32 (std::exp ((double) sep * std::log (5.0)) - 1.0) * 0.25);
  430. s = std::min (1.0f, s);
  431. const auto h = s * 0.5f;
  432. B.matrix = { 1.0f - h, 1.0f - h, h, h };
  433. }
  434. Curve curve (env[(std::size_t) (2 * b)]);
  435. if (! curve.supported())
  436. return nullptr;
  437. buildGainTable (curve, m->scale, B, sgz);
  438. B.g20 = (B.pre * B.post) * B.gain[0];
  439. // effective mode (FUN_008417e0): with solo on and another band selected, the master is at least
  440. // "compression off" and the other bands are muted
  441. B.mode = mode;
  442. if (solo && soloBand != 3 && b != soloBand)
  443. B.mode = std::max (B.mode, b == 3 ? 1 : 2);
  444. B.rms = q (19) != 0;
  445. const auto thr = q (11);
  446. if (thr < 0)
  447. {
  448. const auto top0 = (q (19) == 0 && mode < 1) ? B.peak : 4.0;
  449. const auto ceil = l7 (q (12));
  450. buildSaturation (thr, ceil, std::max (top0 * (double) B.post, (double) ceil), B, sgz);
  451. }
  452. else if (thr > 0)
  453. {
  454. // mode B (FUN_00841380 → FUN_00629fd0): threshold t = (1 − thr/1000)·ceiling, range = ceiling − t
  455. const auto ceil = l7 (q (12));
  456. B.satB = 1;
  457. B.satT = f32 ((double) (1.0f - (float) thr * 0.001f) * (double) ceil);
  458. B.satR = ceil - B.satT;
  459. if (B.satR <= 0.0f)
  460. B.satR = 1.5258789e-05f;
  461. B.satK = 1.0f / B.satR;
  462. }
  463. }
  464. m->valid = true;
  465. return m;
  466. }
  467. std::vector<std::vector<std::pair<double, float>>> maximusEnvelopes (const std::vector<std::uint8_t>& d)
  468. {
  469. // the same walk as makeMaximusPreset: fixed block by version, the v14 / v15 options, then 8 envelopes
  470. std::vector<std::vector<std::pair<double, float>>> out;
  471. if (d.size() < 0x1fe + 12)
  472. return out;
  473. const auto header = (std::uint32_t) i32At (d, 0);
  474. const auto version = header > 0xFFFFu ? (int) (header >> 16) : (int) header;
  475. if (version < 3)
  476. return out;
  477. const std::size_t block = version >= 7 ? 0x1fa : version == 6 ? 0x1f9 : version == 5 ? 0x1f5 : version == 4 ? 0x1f1 : 0x1ee;
  478. auto pos = 4 + block;
  479. if (version >= 14)
  480. pos += 9 + 20;
  481. if (version >= 15)
  482. pos += 4;
  483. bool ok = true;
  484. for (int e = 0; e < 8 && ok; ++e)
  485. {
  486. const auto pts = readCurve (d, pos, ok);
  487. if (! ok)
  488. break;
  489. std::vector<std::pair<double, float>> curve;
  490. for (const auto& p : pts)
  491. curve.emplace_back (p.dx, p.y);
  492. out.push_back (std::move (curve));
  493. }
  494. if (! ok)
  495. out.clear();
  496. return out;
  497. }
  498. namespace
  499. {
  500. // FUN_00624580 (types 1 low-pass, 4 high-pass, 5 all-pass) + FUN_00624e90 (stereo direct form I in double, the
  501. // states flushed below 2^-24 at the end of every block, kept in double)
  502. struct Biquad
  503. {
  504. int type = 1;
  505. double b0 = 0, b1 = 0, b2 = 0, a1 = 0, a2 = 0;
  506. std::array<double, 8> s {}; // per channel: x1 x2 y1 y2
  507. void design (int kind, double w, double alpha) noexcept
  508. {
  509. type = kind;
  510. const auto sn = std::sin (w), cs = std::cos (w);
  511. const auto a = alpha * sn;
  512. const auto norm = 1.0 / (a + 1.0);
  513. a1 = norm * cs * -2.0;
  514. a2 = norm * (1.0 - a);
  515. if (kind == 1)
  516. b1 = norm * (1.0 - cs), b0 = b1 * 0.5, b2 = b0;
  517. else if (kind == 4)
  518. b1 = norm * -(cs + 1.0), b0 = -b1 * 0.5, b2 = b0;
  519. else
  520. b1 = a1, b0 = a2, b2 = 1.0;
  521. }
  522. void reset() noexcept { s = {}; }
  523. void process (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  524. {
  525. // both channels in one loop (two independent recursions; the same arithmetic per channel)
  526. auto lx1 = s[0], lx2 = s[1], ly1 = s[2], ly2 = s[3], rx1 = s[4], rx2 = s[5], ry1 = s[6], ry2 = s[7];
  527. for (int i = 0; i < n; ++i)
  528. {
  529. const auto xl = (double) inL[i], xr = (double) inR[i];
  530. const auto yl = xl * b0 + (((lx1 * b1 - ly2 * a2) + lx2 * b2) - ly1 * a1);
  531. const auto yr = xr * b0 + (((rx1 * b1 - ry2 * a2) + rx2 * b2) - ry1 * a1);
  532. outL[i] = (float) yl, outR[i] = (float) yr;
  533. lx2 = lx1, lx1 = xl, ly2 = ly1, ly1 = yl;
  534. rx2 = rx1, rx1 = xr, ry2 = ry1, ry1 = yr;
  535. }
  536. s = { flushState (lx1), flushState (lx2), flushState (ly1), flushState (ly2),
  537. flushState (rx1), flushState (rx2), flushState (ry1), flushState (ry2) };
  538. }
  539. };
  540. // alpha = sin·k: order 0 (12 dB stage) k = 1, order 1 (24 dB, two stages) k = 1/√2
  541. double crossoverAlpha (int order) noexcept { return order == 1 ? 0.70710678118654746 : 1.0; }
  542. // one 2:1 stage (FUN_0083a9c0 up, FUN_0083ab90 down): two cascades of three first-order all-passes
  543. struct Halfband
  544. {
  545. static constexpr double ka[3] { 0.14737711360104661, 0.48246854276970014, 0.88300502576937312 };
  546. static constexpr double kb[3] { 0.039151597734460045, 0.30264684832849342, 0.6746159185469639 };
  547. std::array<double, 24> s {}; // per channel 12: A outputs, A inputs, B outputs, B inputs
  548. void reset() noexcept { s = {}; }
  549. static double chain (double* st, double x, const double* k) noexcept
  550. {
  551. const auto y1 = st[3] + (x - st[0]) * k[0];
  552. st[3] = x;
  553. const auto y2 = st[4] + (y1 - st[1]) * k[1];
  554. st[4] = y1;
  555. const auto y3 = st[5] + (y2 - st[2]) * k[2];
  556. st[5] = y2;
  557. st[0] = y1, st[1] = y2, st[2] = y3;
  558. return y3;
  559. }
  560. void endBlock() noexcept
  561. {
  562. for (int c = 0; c < 2; ++c)
  563. for (int k : { 0, 1, 2, 6, 7, 8 })
  564. s[(std::size_t) (12 * c + k)] = flushState (s[(std::size_t) (12 * c + k)]);
  565. }
  566. void up (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  567. {
  568. const float* in[2] { inL, inR };
  569. float* out[2] { outL, outR };
  570. for (int c = 0; c < 2; ++c)
  571. {
  572. auto* st = s.data() + 12 * c;
  573. for (int i = 0; i < n; ++i)
  574. {
  575. const auto v = (double) in[c][i];
  576. const auto a = chain (st, v, ka);
  577. const auto b = chain (st + 6, v, kb);
  578. out[c][2 * i] = (float) b;
  579. out[c][2 * i + 1] = (float) a;
  580. }
  581. }
  582. endBlock();
  583. }
  584. void down (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept // n output frames
  585. {
  586. const float* in[2] { inL, inR };
  587. float* out[2] { outL, outR };
  588. for (int c = 0; c < 2; ++c)
  589. {
  590. auto* st = s.data() + 12 * c;
  591. for (int i = 0; i < n; ++i)
  592. {
  593. const auto a = chain (st, (double) in[c][2 * i], ka);
  594. const auto b = chain (st + 6, (double) in[c][2 * i + 1], kb);
  595. out[c][i] = (float) ((b + a) * 0.5);
  596. }
  597. }
  598. endBlock();
  599. }
  600. };
  601. // FUN_00605410 (x87): one-pole on a control signal, c0 while the input is ≥ the state, c1 otherwise. The state stays
  602. // on the x87 stack (64-bit mantissa) through the block, the coefficients are doubles and only the output samples are
  603. // rounded to float; between blocks the state is kept as a double, 0 below 2^-24 (REA_rodada40 §2.4). long double is
  604. // the 80-bit x87 format on the x86-64 GCC/Clang/MinGW targets LYNX is built for.
  605. struct Smoother
  606. {
  607. double c0 = 0.0, c1 = 0.0, s = 0.0;
  608. void run (float* x, int n) noexcept
  609. {
  610. long double st = s;
  611. if (c0 == c1)
  612. {
  613. const long double c = c0;
  614. for (int i = 0; i < n; ++i)
  615. {
  616. const long double v = x[i];
  617. st = (st - v) * c + v;
  618. x[i] = (float) st;
  619. }
  620. }
  621. else
  622. {
  623. const long double k0 = c0, k1 = c1;
  624. for (int i = 0; i < n; ++i)
  625. {
  626. const long double v = x[i];
  627. const auto c = v >= st ? k0 : k1;
  628. st = (st - v) * c + v;
  629. x[i] = (float) st;
  630. }
  631. }
  632. s = std::fabs ((double) st) > (double) flushLevel ? (double) st : 0.0;
  633. }
  634. };
  635. // FUN_00629710 + FUN_007806b0: max(|L|, |R|), then the running maximum over the last W samples
  636. struct PeakWindow
  637. {
  638. int W = 1, pos = 0, head = 0, tail = 0, count = 0;
  639. std::vector<std::int32_t> ring, dq;
  640. void setLength (int w)
  641. {
  642. W = std::max (1, w);
  643. ring.assign ((std::size_t) W, 0);
  644. dq.assign ((std::size_t) W, 0);
  645. pos = 0, head = W > 1 ? 1 : 0, tail = 0, count = 0;
  646. }
  647. void run (const float* L, const float* R, float* out, int n) noexcept
  648. {
  649. for (int i = 0; i < n; ++i)
  650. out[i] = std::max (std::fabs (L[i]), std::fabs (R[i]));
  651. if (W <= 1)
  652. return;
  653. for (int i = 0; i < n; ++i)
  654. {
  655. std::int32_t x = 0;
  656. std::memcpy (&x, out + i, 4);
  657. auto next = pos + 1;
  658. if (next >= W)
  659. next = 0;
  660. const auto old = count < 1 ? ring[(std::size_t) pos] : ring[(std::size_t) dq[(std::size_t) head]];
  661. if (ring[(std::size_t) pos] < x)
  662. {
  663. while (count > 0)
  664. {
  665. if (x <= ring[(std::size_t) dq[(std::size_t) tail]])
  666. {
  667. const auto j = dq[(std::size_t) head] - 1;
  668. if (next == j || next == j + W)
  669. {
  670. --count;
  671. if (++head >= W)
  672. head = 0;
  673. }
  674. break;
  675. }
  676. --count;
  677. if (--tail < 0)
  678. tail = W - 1;
  679. }
  680. }
  681. else
  682. {
  683. if (++tail >= W)
  684. tail = 0;
  685. dq[(std::size_t) tail] = pos;
  686. const auto j = dq[(std::size_t) head] - 1;
  687. if (next == j || next == j + W)
  688. {
  689. if (++head >= W)
  690. head = 0;
  691. }
  692. else
  693. ++count;
  694. }
  695. ring[(std::size_t) next] = x;
  696. if (x < old)
  697. x = old;
  698. std::memcpy (out + i, &x, 4);
  699. pos = next;
  700. }
  701. }
  702. };
  703. // the RMS detector (envelope mode 1; SG class at 0x780020, process FUN_00780b90 with the window kernel FUN_00780a60;
  704. // REA_rodada38, REA_rodada40 §2.1): L² + R² summed over the last W samples in an x87 running sum (kept as a double
  705. // between blocks), scaled by 1/(2W), clamped below at 0 (ippsThreshold_LT_32f_I) and square-rooted (ippsSqrt_32f_I)
  706. struct RmsWindow
  707. {
  708. int W = 1, idx = 0;
  709. double sum = 0.0;
  710. float scale = 0.5f;
  711. std::vector<float> ring;
  712. void setLength (int w)
  713. {
  714. W = std::max (1, w);
  715. ring.assign ((std::size_t) W, 0.0f);
  716. scale = (float) (1.0 / (double) (W * 2));
  717. reset();
  718. }
  719. void reset() noexcept
  720. {
  721. std::fill (ring.begin(), ring.end(), 0.0f);
  722. idx = 0;
  723. sum = 0.0;
  724. }
  725. void run (const float* L, const float* R, float* out, int n) noexcept
  726. {
  727. // the running sum stays in an x87 register over the block (fldl … fstpl): extended precision, saved as a double
  728. auto acc = (long double) sum;
  729. auto k = idx;
  730. for (int i = 0; i < n; ++i)
  731. {
  732. const auto x = L[i] * L[i] + R[i] * R[i];
  733. if (++k >= W)
  734. k = 0;
  735. acc = (acc - (long double) ring[(std::size_t) k]) + (long double) x;
  736. ring[(std::size_t) k] = x;
  737. out[i] = (float) (acc * (long double) scale);
  738. }
  739. sum = (double) acc;
  740. idx = k;
  741. for (int i = 0; i < n; ++i)
  742. out[i] = std::sqrt (std::max (out[i], 0.0f));
  743. }
  744. };
  745. // saturation mode B (FUN_0062a090, x87): above the threshold t, |y| = t + range·(1 − e^((t − |x|)/range)), sign kept
  746. inline float saturateB (float x, float t, float range, float k) noexcept
  747. {
  748. const auto a = std::fabs (x);
  749. if (! (a > t))
  750. return x;
  751. const auto e = std::exp ((long double) ((long double) t - (long double) a) * (long double) k);
  752. const auto y = (float) ((1.0L - e) * (long double) range + (long double) t);
  753. return std::signbit (x) ? -y : y;
  754. }
  755. // FUN_0077f350: L-frame delay (swap ring)
  756. struct FrameDelay
  757. {
  758. std::vector<float> l, r;
  759. int L = 0, p = 0;
  760. void setLength (int n)
  761. {
  762. L = std::max (0, n);
  763. l.assign ((std::size_t) std::max (1, L), 0.0f);
  764. r.assign ((std::size_t) std::max (1, L), 0.0f);
  765. p = 0;
  766. }
  767. void reset() noexcept
  768. {
  769. std::fill (l.begin(), l.end(), 0.0f);
  770. std::fill (r.begin(), r.end(), 0.0f);
  771. p = 0;
  772. }
  773. void run (float* xl, float* xr, int n) noexcept
  774. {
  775. if (L == 0)
  776. return;
  777. for (int i = 0; i < n; ++i)
  778. {
  779. std::swap (xl[i], l[(std::size_t) p]);
  780. std::swap (xr[i], r[(std::size_t) p]);
  781. if (++p >= L)
  782. p = 0;
  783. }
  784. }
  785. };
  786. } // namespace
  787. namespace
  788. {
  789. // the legacy linear-phase band split (REA_rodada40 §2.5): FUN_0083c0d0 (windowed sinc), FUN_0062ac70 type 0 (4-term
  790. // Blackman-Harris), FUN_0083c070 (spectral inversion), FUN_0083c200 (placement in the N-tap buffer), taps converted to
  791. // float and run by ippsFIR_32f per channel (FUN_0083bce0)
  792. std::vector<double> sincLowpass (int L, double fc)
  793. {
  794. std::vector<double> h ((std::size_t) L);
  795. const auto w = fc * 6.283185307179586;
  796. for (int i = 0; i < L; ++i)
  797. {
  798. if (i == (L - 1) / 2)
  799. h[(std::size_t) i] = fc * 2.0;
  800. else
  801. {
  802. const auto d = (double) ((float) i - (float) (L - 1) * 0.5f);
  803. h[(std::size_t) i] = std::sin (w * d) / (d * 3.141592653589793);
  804. }
  805. }
  806. const auto a = 6.283185307179586 / (double) (L - 1);
  807. for (int i = 0; i < L; ++i)
  808. {
  809. const auto c1 = std::cos (a * (double) i), c2 = std::cos (a * 2.0 * (double) i), c3 = std::cos (a * 3.0 * (double) i);
  810. h[(std::size_t) i] *= ((0.35875 - c1 * 0.48829) + c2 * 0.14128) - c3 * 0.01168;
  811. }
  812. return h;
  813. }
  814. void invert (std::vector<double>& h)
  815. {
  816. for (auto& v : h)
  817. v *= -1.0;
  818. h[(h.size() - 1) / 2] += 1.0;
  819. }
  820. // kind 0 low-pass (f1, L1), 3 high-pass (f1, L1), 1 band-pass (f1, L1 / f2, L2); placed in the middle of N taps
  821. std::vector<float> legacyTaps (int N, int kind, double f1, int L1, double f2, int L2)
  822. {
  823. std::vector<double> buf ((std::size_t) N, 0.0);
  824. f1 = std::min (f1, 0.5), f2 = std::min (f2, 0.5);
  825. L1 |= 1, L2 |= 1;
  826. auto place = [&] (std::vector<double>& to, const std::vector<double>& h) {
  827. const auto at = (N - (int) h.size()) / 2;
  828. for (std::size_t i = 0; i < h.size(); ++i)
  829. to[(std::size_t) at + i] = h[i];
  830. };
  831. if (kind == 0)
  832. place (buf, sincLowpass (L1, f1));
  833. else if (kind == 3)
  834. {
  835. auto h = sincLowpass (L1, f1);
  836. invert (h);
  837. place (buf, h);
  838. }
  839. else
  840. {
  841. std::vector<double> hi ((std::size_t) N, 0.0);
  842. place (buf, sincLowpass (L1, f1));
  843. auto h = sincLowpass (L2, f2);
  844. invert (h);
  845. place (hi, h);
  846. for (int i = 0; i < N; ++i)
  847. buf[(std::size_t) i] += hi[(std::size_t) i];
  848. if (kind == 1)
  849. invert (buf);
  850. }
  851. std::vector<float> taps ((std::size_t) N);
  852. for (int i = 0; i < N; ++i)
  853. taps[(std::size_t) i] = (float) buf[(std::size_t) i];
  854. return taps;
  855. }
  856. struct Fir
  857. {
  858. std::vector<float> taps, rev; // rev: the taps in time order of the history (oldest first)
  859. std::array<std::vector<float>, 2> hist; // the last N − 1 inputs, then the block
  860. void reset() noexcept
  861. {
  862. for (auto& h : hist)
  863. std::fill (h.begin(), h.end(), 0.0f);
  864. }
  865. void prepare (std::vector<float> t, int maxFrames)
  866. {
  867. taps = std::move (t);
  868. rev.assign (taps.rbegin(), taps.rend());
  869. for (auto& h : hist)
  870. h.assign (taps.size() - 1 + (std::size_t) maxFrames, 0.0f);
  871. }
  872. void process (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  873. {
  874. const auto N = (int) taps.size();
  875. const float* in[2] { inL, inR };
  876. float* out[2] { outL, outR };
  877. for (int c = 0; c < 2; ++c)
  878. {
  879. auto& h = hist[(std::size_t) c];
  880. std::copy (in[c], in[c] + n, h.begin() + (N - 1));
  881. // double accumulation in four interleaved partial sums (IPP's own summation order is not known; the
  882. // difference stays far below the float output's rounding)
  883. const float* rt = rev.data();
  884. for (int i = 0; i < n; ++i)
  885. {
  886. const float* x = h.data() + i;
  887. double a0 = 0.0, a1 = 0.0, a2 = 0.0, a3 = 0.0;
  888. int k = 0;
  889. for (; k + 4 <= N; k += 4)
  890. {
  891. a0 += (double) rt[k] * (double) x[k];
  892. a1 += (double) rt[k + 1] * (double) x[k + 1];
  893. a2 += (double) rt[k + 2] * (double) x[k + 2];
  894. a3 += (double) rt[k + 3] * (double) x[k + 3];
  895. }
  896. for (; k < N; ++k)
  897. a0 += (double) rt[k] * (double) x[k];
  898. out[c][i] = (float) ((a0 + a1) + (a2 + a3));
  899. }
  900. std::copy (h.begin() + n, h.begin() + n + (N - 1), h.begin());
  901. }
  902. }
  903. };
  904. } // namespace
  905. class Maximus
  906. {
  907. public:
  908. static constexpr int maxFrames = Rack::maxBlock * 8;
  909. void prepare (std::shared_ptr<const MaximusPreset> preset, double rate, int mixParam)
  910. {
  911. pr = std::move (preset);
  912. fs = rate;
  913. const auto& P = *pr;
  914. const auto& p = P.p;
  915. K = 1 << P.oversampling;
  916. mix = (float) mixParam * 0.001f;
  917. mixOn = mix != 1.0f;
  918. std::array<int, 4> mode {};
  919. for (int b = 0; b < 4; ++b)
  920. mode[(std::size_t) b] = P.band[(std::size_t) b].mode;
  921. const auto anyComp = mode[0] < 1 || mode[1] < 1 || mode[2] < 1;
  922. int latency = 0;
  923. for (int b = 0; b < 4; ++b)
  924. {
  925. auto q = [&] (int j) { return p[(std::size_t) (13 * b + j)]; };
  926. auto& B = band[(std::size_t) b];
  927. B.mode = mode[(std::size_t) b];
  928. const auto k = b == 3 ? 1 : K;
  929. int L = 0;
  930. if (B.mode < 2 && (b != 3 || B.mode < 1) && (b > 2 || anyComp))
  931. L = (int) rint (tms (b == 3 ? q (13) : p[4]) * 2.0 * 0.001 * fs) * k;
  932. B.L = L;
  933. if (b < 3)
  934. latency = std::max (latency, L);
  935. const auto a = q (13);
  936. const auto A = a == 0 ? (double) L : tms (a) * 2.0 * 0.001 * fs * k;
  937. const auto na = std::max (1, q (15));
  938. const auto attN = (A * 0.800000011920929) / std::sqrt ((double) na);
  939. const auto rel2N = q (14) == 0 ? attN : (tms (q (14)) * 2.0 * 0.001 * fs * k * 2.0) / std::sqrt ((double) na);
  940. const auto sus = (int) rint (tms (q (18)) * 2.0 * 0.001 * fs);
  941. B.det.setLength (std::max (std::max (L, 1), sus * k));
  942. B.rms = P.band[(std::size_t) b].rms;
  943. if (B.rms)
  944. B.rmsDet.setLength (std::max (std::max (L, 1), sus * k));
  945. B.att.assign ((std::size_t) na, Smoother { coefFor (rel2N), coefFor (attN), 0.0 });
  946. const auto R = tms (q (16)) * 2.0 * 0.001 * fs * k;
  947. B.rel.clear();
  948. if (R > 0.0)
  949. {
  950. const auto nr = std::max (1, q (17));
  951. B.rel.assign ((std::size_t) nr, Smoother { 0.0, coefFor ((R * 2.0) / std::sqrt ((double) nr)), 0.0 });
  952. }
  953. B.delay.setLength (L);
  954. }
  955. const auto latencyBands = latency;
  956. latency = latency / K + band[3].L;
  957. reportedLatency = latency;
  958. tailLength = latency + (int) rint (fs * 1.5);
  959. // crossover (FUN_00844e10)
  960. auto freq = [&] (int v) { return f32 (std::exp ((double) v * 0.00013822774794088088 * 0.69314718055994529) * 30.0); };
  961. auto fL = freq (p[0]), fH = freq (p[2]);
  962. const auto sL = mode[0] != 3 ? std::clamp (p[1], 0, 1) : -1;
  963. const auto sH = mode[2] != 3 ? std::clamp (p[3], 0, 1) : -1;
  964. if (sL >= 0 && sH >= 0 && fH < fL)
  965. std::swap (fL, fH);
  966. first = 1;
  967. for (int b = 0; b < 3; ++b)
  968. if (mode[(std::size_t) b] != 3)
  969. {
  970. first = b;
  971. break;
  972. }
  973. auto polarity = [] (int s) { return s < 0 ? 0.0f : s == 0 ? -1.0f : 1.0f; };
  974. band[0].g18 = polarity (sL), band[1].g18 = 1.0f, band[2].g18 = polarity (sH), band[3].g18 = 1.0f;
  975. const auto rateB = fs * K, fmax = (double) f32 (0.4999 * rateB), w = 2.0 * pi / rateB;
  976. auto stage = [&] (int type, float f, int order) {
  977. Biquad q;
  978. const auto fc = std::min ((double) f, fmax);
  979. q.design (type, fc * w, crossoverAlpha (order));
  980. return q;
  981. };
  982. for (auto& s : stages)
  983. s.clear();
  984. // the low band's all-pass at the high crossover (phase match with a 24 dB high split) is absent from the
  985. // older build in Soundgoodizer_x64.dll (FUN_0083a4a0: sL + 1 low-pass stages only)
  986. const auto allPass = sH > 0 && P.build == MaximusBuild::shared;
  987. const auto n0 = sL + (allPass ? 1 : 0) + 1;
  988. for (int i = 0; i < n0; ++i)
  989. stages[0].push_back (i == n0 - 1 && allPass ? stage (5, fH, 0) : stage (1, fL, sL));
  990. for (int i = 0; i <= sL; ++i)
  991. stages[1].push_back (stage (4, fL, sL));
  992. for (int i = 0; i <= sH; ++i)
  993. stages[1].push_back (stage (1, fH, sH));
  994. for (int i = 0; i <= sH; ++i)
  995. stages[2].push_back (stage (4, fH, sH));
  996. inPlaceStages = std::min (sL + 1, (int) stages[1].size() - 1);
  997. firOn = P.legacyFir;
  998. if (firOn)
  999. {
  1000. // FUN_00840bb0: N = round(0.004·band rate) | 1 (≥ 5); FUN_00844e10: per split round(K·c·(slope + 1)/f) | 1
  1001. // (≥ 5, ≤ N) with c = 27 500 (the high split of Soundgoodizer_x64.dll's build: 110 000)
  1002. auto N = (int) rint (rateB * 0.004) | 1;
  1003. N = std::max (N, 5);
  1004. auto len = [&] (double c, int slope, float f) {
  1005. const auto v = (double) K * c * (double) (slope + 1) / (double) f;
  1006. auto L = (int) rint (v);
  1007. L = (L | 1) < 6 ? 5 : (L | 1);
  1008. return std::min (L, N);
  1009. };
  1010. const auto Ll = len (27500.0, sL, fL);
  1011. const auto Lh = len (P.build == MaximusBuild::soundgoodizer ? 110000.0 : 27500.0, sH, fH);
  1012. const auto nl = (double) (float) (fL / (float) rateB), nh = (double) (float) (fH / (float) rateB);
  1013. fir[0].prepare (legacyTaps (N, 0, nl, Ll, 0.0, 0), maxFrames);
  1014. if (sL < 0)
  1015. fir[1].prepare (sH >= 0 ? legacyTaps (N, 0, nh, Lh, 0.0, 0) : std::vector<float> ((std::size_t) N, 0.0f), maxFrames);
  1016. else if (sH < 0)
  1017. fir[1].prepare (legacyTaps (N, 3, nl, Ll, 0.0, 0), maxFrames);
  1018. else
  1019. fir[1].prepare (legacyTaps (N, 1, nl, Ll, nh, Lh), maxFrames);
  1020. fir[2].prepare (legacyTaps (N, 3, nh, Lh, 0.0, 0), maxFrames);
  1021. band[0].g18 = band[2].g18 = 1.0f;
  1022. firLatency = (N - 1) / 2;
  1023. reportedLatency = (latencyBands + firLatency) / K + band[3].L;
  1024. tailLength = reportedLatency + (int) rint (fs * 1.5);
  1025. }
  1026. // low cut: four high-pass stages at 0.8·f (FUN_00840b40, FUN_0083a5c0)
  1027. lowCut.clear();
  1028. if (p[6] >= 0)
  1029. {
  1030. const auto f = f32 (std::exp ((double) p[6] * 6.103515625e-05 * 0.69314718055994529) * 5.0);
  1031. for (const auto k : { 0.19771184027194977, 0.61932772397995, 0.6768445372581482, 1.0793569087982178 })
  1032. {
  1033. Biquad q;
  1034. q.design (4, ((double) f * 0.8) * (2.0 * pi / fs), k);
  1035. lowCut.push_back (q);
  1036. }
  1037. }
  1038. for (auto& h : up)
  1039. h.reset();
  1040. for (auto& h : down)
  1041. h.reset();
  1042. reset();
  1043. counter = -1073741823;
  1044. }
  1045. void reset() noexcept
  1046. {
  1047. for (int b = 0; b < 4; ++b)
  1048. {
  1049. auto& B = band[(std::size_t) b];
  1050. for (auto& s : B.att)
  1051. s.s = pr->band[(std::size_t) b].g20;
  1052. for (auto& s : B.rel)
  1053. s.s = 0.0f;
  1054. B.delay.reset(); // (the detector keeps its window: SG's reset FUN_00845940 does not touch it)
  1055. }
  1056. for (auto& st : stages)
  1057. for (auto& q : st)
  1058. q.reset();
  1059. for (auto& q : lowCut)
  1060. q.reset();
  1061. for (auto& f : fir)
  1062. f.reset();
  1063. for (auto& h : up)
  1064. h.reset();
  1065. for (auto& h : down)
  1066. h.reset();
  1067. }
  1068. // silence fed while the rack is idle (Eff_Render's silent-block counter, then the reset after 1.5 s)
  1069. void silence (int n) noexcept
  1070. {
  1071. counter -= n;
  1072. if (! (counter > -n))
  1073. {
  1074. if (counter > -0x3fffffff)
  1075. reset();
  1076. counter = -1073741823;
  1077. }
  1078. }
  1079. void process (float* L, float* R, int n) noexcept
  1080. {
  1081. bool silent = true;
  1082. for (int i = 0; i < n && silent; ++i)
  1083. silent = std::fabs (L[i]) <= 9.999999974752427e-07f && std::fabs (R[i]) <= 9.999999974752427e-07f;
  1084. if (! silent)
  1085. counter = tailLength;
  1086. else
  1087. counter -= n;
  1088. if (! (counter > -n))
  1089. {
  1090. if (counter > -0x3fffffff)
  1091. reset();
  1092. counter = -1073741823;
  1093. return;
  1094. }
  1095. for (auto& q : lowCut)
  1096. q.process (L, R, L, R, n);
  1097. // X: the signal being split (at the band rate)
  1098. const auto nb = n * K;
  1099. float* XL = L;
  1100. float* XR = R;
  1101. if (K > 1)
  1102. {
  1103. up[0].up (L, R, xl.data(), xr.data(), n);
  1104. XL = xl.data(), XR = xr.data();
  1105. }
  1106. bool haveSum = false;
  1107. for (int b = 0; b < 3; ++b)
  1108. {
  1109. auto& st = stages[(std::size_t) b];
  1110. float* yl = bl.data();
  1111. float* yr = br.data();
  1112. if (st.empty())
  1113. std::copy (XL, XL + nb, yl), std::copy (XR, XR + nb, yr);
  1114. else if (firOn) // the legacy linear-phase split: every band from the input (FUN_0083bce0)
  1115. fir[(std::size_t) b].process (XL, XR, yl, yr, nb);
  1116. else if (b == 1 && b != first)
  1117. {
  1118. const float* sl = XL;
  1119. const float* sr = XR;
  1120. for (int k = 0; k < (int) st.size(); ++k)
  1121. {
  1122. float* dl = k < inPlaceStages ? XL : yl;
  1123. float* dr = k < inPlaceStages ? XR : yr;
  1124. st[(std::size_t) k].process (sl, sr, dl, dr, nb);
  1125. sl = dl, sr = dr;
  1126. }
  1127. if (st.size() <= (std::size_t) inPlaceStages) // (not reached with the presets: kept literal)
  1128. std::copy (sl, sl + nb, yl), std::copy (sr, sr + nb, yr);
  1129. }
  1130. else
  1131. {
  1132. const float* sl = XL;
  1133. const float* sr = XR;
  1134. for (auto& q : st)
  1135. {
  1136. q.process (sl, sr, yl, yr, nb);
  1137. sl = yl, sr = yr;
  1138. }
  1139. }
  1140. auto& B = band[(std::size_t) b];
  1141. processBand (b, yl, yr, nb);
  1142. if (B.mode < 2)
  1143. {
  1144. if (! haveSum && b == 2)
  1145. {
  1146. // the high band is the first one on: its buffer is the sum buffer, and the final add (ippsAdd_32f
  1147. // of the band and the sum) doubles it (FUN_00843a40)
  1148. for (int i = 0; i < nb; ++i)
  1149. sl[(std::size_t) i] = yl[i] + yl[i], sr[(std::size_t) i] = yr[i] + yr[i];
  1150. haveSum = true;
  1151. }
  1152. else if (! haveSum)
  1153. std::copy (yl, yl + nb, sl.data()), std::copy (yr, yr + nb, sr.data()), haveSum = true;
  1154. else
  1155. for (int i = 0; i < nb; ++i)
  1156. sl[(std::size_t) i] = sl[(std::size_t) i] + yl[i], sr[(std::size_t) i] = sr[(std::size_t) i] + yr[i];
  1157. }
  1158. else if (b == first)
  1159. {
  1160. std::fill (sl.begin(), sl.begin() + nb, 0.0f), std::fill (sr.begin(), sr.begin() + nb, 0.0f);
  1161. haveSum = true;
  1162. }
  1163. }
  1164. if (! haveSum)
  1165. std::fill (sl.begin(), sl.begin() + nb, 0.0f), std::fill (sr.begin(), sr.begin() + nb, 0.0f);
  1166. if (K > 1)
  1167. down[0].down (sl.data(), sr.data(), L, R, n);
  1168. else
  1169. std::copy (sl.begin(), sl.begin() + n, L), std::copy (sr.begin(), sr.begin() + n, R);
  1170. processBand (3, L, R, n);
  1171. }
  1172. private:
  1173. struct BandState
  1174. {
  1175. int mode = 3, L = 0;
  1176. float g18 = 1.0f;
  1177. bool rms = false;
  1178. PeakWindow det;
  1179. RmsWindow rmsDet;
  1180. std::vector<Smoother> att, rel;
  1181. FrameDelay delay;
  1182. };
  1183. void processBand (int b, float* xl, float* xr, int n) noexcept
  1184. {
  1185. auto& B = band[(std::size_t) b];
  1186. const auto& S = pr->band[(std::size_t) b];
  1187. if (B.mode < 2)
  1188. {
  1189. const auto& m = S.matrix;
  1190. if (m[2] != 0.0f || m[3] != 0.0f)
  1191. for (int i = 0; i < n; ++i)
  1192. {
  1193. const auto l = xl[i], r = xr[i];
  1194. xl[i] = l * m[0] + r * m[2];
  1195. xr[i] = r * m[1] + l * m[3];
  1196. }
  1197. else if (m[0] != 1.0f || m[1] != 1.0f)
  1198. for (int i = 0; i < n; ++i)
  1199. xl[i] = xl[i] * m[0], xr[i] = xr[i] * m[1];
  1200. }
  1201. float* g = gain.data();
  1202. if (B.mode == 0)
  1203. {
  1204. if (B.rms)
  1205. B.rmsDet.run (xl, xr, g, n);
  1206. else
  1207. B.det.run (xl, xr, g, n);
  1208. for (auto& s : B.rel)
  1209. s.run (g, n);
  1210. const auto k = f32 ((double) S.pre * 8192.25);
  1211. const auto pp = S.pre * S.post;
  1212. const auto hiGain = f32 (S.end * (double) S.post);
  1213. const auto* t = S.gain.data();
  1214. for (int i = 0; i < n; ++i)
  1215. {
  1216. const auto lv = g[i];
  1217. const auto idxf = lv * k;
  1218. auto idx = (int) idxf; // ippsConvert_32f32s_Sfs, rounding toward zero
  1219. if (idx < 0)
  1220. idx = 0;
  1221. if (idx > S.size)
  1222. g[i] = hiGain / lv;
  1223. else
  1224. {
  1225. const auto fr = idxf - (float) idx;
  1226. g[i] = (t[idx] + (t[idx + 1] - t[idx]) * fr) * pp;
  1227. }
  1228. }
  1229. for (auto& s : B.att)
  1230. s.run (g, n);
  1231. }
  1232. B.delay.run (xl, xr, n);
  1233. const auto keepDry = mixOn && b < 3;
  1234. if (keepDry)
  1235. std::copy (xl, xl + n, dl.data()), std::copy (xr, xr + n, dr.data());
  1236. if (B.mode == 0)
  1237. for (int i = 0; i < n; ++i)
  1238. xl[i] = xl[i] * g[i], xr[i] = xr[i] * g[i];
  1239. else
  1240. for (int i = 0; i < n; ++i)
  1241. xl[i] = xl[i] * S.post, xr[i] = xr[i] * S.post;
  1242. if (! S.sat.empty() && B.mode < 2)
  1243. {
  1244. // FUN_0062a240 (x87, truncating control word): odd table read with linear interpolation
  1245. const auto k = (double) S.satIn * 65536.0;
  1246. const auto* t = S.sat.data();
  1247. auto sat = [&] (float x) {
  1248. const auto v = (double) x * k;
  1249. const auto a = std::fabs (v);
  1250. if (! (a < 65536.0)) // (also NaN / inf: never an index outside the table)
  1251. return (float) ((v < 0.0 ? -(double) t[65536] : (double) t[65536]) * (double) S.satOut);
  1252. auto i = (long long) a;
  1253. const auto fr = a - (double) i;
  1254. const auto y = (double) t[i] + ((double) t[i + 1] - (double) t[i]) * fr;
  1255. return (float) ((v < 0.0 ? -y : y) * (double) S.satOut);
  1256. };
  1257. for (int i = 0; i < n; ++i)
  1258. xl[i] = sat (xl[i]), xr[i] = sat (xr[i]);
  1259. }
  1260. else if (S.satB != 0 && B.mode < 2)
  1261. for (int i = 0; i < n; ++i)
  1262. xl[i] = saturateB (xl[i], S.satT, S.satR, S.satK), xr[i] = saturateB (xr[i], S.satT, S.satR, S.satK);
  1263. if (b == 3)
  1264. {
  1265. if (B.mode == 2)
  1266. std::fill (xl, xl + n, 0.0f), std::fill (xr, xr + n, 0.0f);
  1267. return;
  1268. }
  1269. if (B.mode < 2)
  1270. {
  1271. if (mixOn)
  1272. {
  1273. const auto a = mix * B.g18, d = (1.0f - mix) * B.g18;
  1274. for (int i = 0; i < n; ++i)
  1275. {
  1276. xl[i] = xl[i] * a + dl[(std::size_t) i] * d;
  1277. xr[i] = xr[i] * a + dr[(std::size_t) i] * d;
  1278. }
  1279. }
  1280. else if (B.g18 == -1.0f)
  1281. for (int i = 0; i < n; ++i)
  1282. xl[i] = -xl[i], xr[i] = -xr[i];
  1283. }
  1284. }
  1285. public:
  1286. // FUN_00840a00: the latency the plugin reports to the host (PDC), max(L0…2)/K + L3
  1287. int latency() const noexcept { return reportedLatency; }
  1288. private:
  1289. std::shared_ptr<const MaximusPreset> pr;
  1290. double fs = 44100.0;
  1291. int K = 1, first = 0, inPlaceStages = 0, tailLength = 0, counter = 0, reportedLatency = 0, firLatency = 0;
  1292. bool firOn = false;
  1293. std::array<Fir, 3> fir {};
  1294. float mix = 1.0f;
  1295. bool mixOn = false;
  1296. std::array<BandState, 4> band {};
  1297. std::array<std::vector<Biquad>, 3> stages {};
  1298. std::vector<Biquad> lowCut;
  1299. std::array<Halfband, 1> up {}, down {};
  1300. std::array<float, maxFrames> xl {}, xr {}, bl {}, br {}, sl {}, sr {}, dl {}, dr {}, gain {};
  1301. };
  1302. // =================================================================================================================
  1303. // Chorus, delay, reverb (REA_rodada36 §1)
  1304. // =================================================================================================================
  1305. // the chorus of the shared effects library (REA_rodada31 §7.3): order N, depth, speed, delay, spread, cross (the
  1306. // library's fields), dry gain and wet volume
  1307. struct ChorusUnit
  1308. {
  1309. struct Tap
  1310. {
  1311. int ch = 0;
  1312. float gl = 0, gr = 0, p3 = 0, p8 = 0, p9 = 0, p10 = 0;
  1313. std::uint32_t phase = 0, inc = 0;
  1314. };
  1315. std::vector<Tap> taps;
  1316. float dry = 1.0f;
  1317. int maxDelay = 0;
  1318. std::vector<float> ringL, ringR; // input history, power-of-two length
  1319. int mask = 0, w = 0;
  1320. ChorusUnit (int N, float depth, float speed, float dly, float spread, float cross, float dryGain, float vol, double fs)
  1321. {
  1322. static constexpr float q[20] { 0.967f, 1.039f, 1.021f, 0.1013f, 0.977f, 1.033f, 0.997f, 0.971f, 1.061f, 0.947f,
  1323. 1.031f, 0.1009f, 1.063f, 0.953f, 0.991f, 1.049f, 0.983f, 1.051f, 1.019f, 1.069f };
  1324. N = std::max (N, 1);
  1325. dry = dryGain;
  1326. const auto A = f32 ((std::pow (11.0, (double) depth) - 1.0) * (1092.5 / 10.0));
  1327. const auto B = (2147483648.0 / ((double) f32 (std::pow (101.0, (double) f32 (1.0f - speed)) - 1.0) * 75000.0 + 50000.0)) * 500.0;
  1328. const auto C = f32 ((std::pow (11.0, (double) dly) - 1.0) * (4410.0 / 10.0));
  1329. const auto sg = f32 (f32 (std::pow (1001.0, (double) spread) - 1.0) / 1000.0f);
  1330. auto mixf = [&] (double a, double b) { return a * (double) sg + b * (1.0 - (double) sg); };
  1331. const auto fsr = f32 (44100.0 / fs);
  1332. float sumAbs = 0.0f, mx = 0.0f;
  1333. for (int i = 0; i < 2 * N; ++i)
  1334. {
  1335. Tap t;
  1336. float f = 0.0f;
  1337. std::uint32_t ph = 0;
  1338. if (i < N)
  1339. f = (float) i, t.ch = 0;
  1340. else
  1341. f = (float) (2 * N - 1 - i), t.ch = 1, ph = 0x80000000u;
  1342. const auto wv = 1.0f - f / (float) N;
  1343. const auto rr = q[i % 20];
  1344. const auto dV = f32 (mixf ((double) (rr * rr), 1.0));
  1345. float gl = 1.0f, gr = cross * wv;
  1346. if (t.ch)
  1347. std::swap (gl, gr);
  1348. sumAbs = f32 ((double) sumAbs + std::fabs ((double) gl) + std::fabs ((double) gr));
  1349. auto p0 = A * wv + 10.0f;
  1350. const auto d11 = mixf (B * (double) dV / (double) p0, B / (double) (A + 10.0f));
  1351. auto p3 = f32 (mixf ((double) ((C * wv) * dV), (double) C));
  1352. ph += (std::uint32_t) ((std::int64_t) rint ((double) ((wv * sg) * 2147483648.0f) / 256.0) << 8);
  1353. t.phase = ph;
  1354. t.inc = (std::uint32_t) (std::int64_t) rint (d11 * (double) fsr);
  1355. p0 = p0 / fsr;
  1356. auto t3 = p3 / fsr;
  1357. if (! (t3 > 8.0f))
  1358. t3 = 8.0f;
  1359. p3 = p0 + t3;
  1360. if (p3 + p0 > mx)
  1361. mx = p3 + p0;
  1362. const auto qq = std::sqrt ((double) (p0 * 4.0f));
  1363. t.gl = gl, t.gr = gr, t.p3 = p3;
  1364. t.p8 = f32 (qq / 2147483648.0), t.p9 = f32 (qq), t.p10 = f32 (qq / 2.0);
  1365. taps.push_back (t);
  1366. }
  1367. const auto norm = f32 ((double) (1.0f - f32 ((double) f32 (std::pow (1001.0, (double) (1.0f - 1.0f / sumAbs)) - 1.0) / 1000.0)) * (double) vol);
  1368. for (auto& t : taps)
  1369. t.gl = t.gl * norm, t.gr = t.gr * norm;
  1370. maxDelay = (int) rint ((double) mx) + 8;
  1371. int size = 1;
  1372. while (size < maxDelay + Rack::maxBlock + 8)
  1373. size <<= 1;
  1374. ringL.assign ((std::size_t) size, 0.0f);
  1375. ringR.assign ((std::size_t) size, 0.0f);
  1376. mask = size - 1;
  1377. }
  1378. void advance (int n) noexcept
  1379. {
  1380. for (auto& t : taps)
  1381. t.phase += t.inc * (std::uint32_t) n;
  1382. }
  1383. // out = Σ taps (no dry); in: this block of the mix
  1384. void process (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  1385. {
  1386. for (int i = 0; i < n; ++i)
  1387. ringL[(std::size_t) ((w + i) & mask)] = inL[i], ringR[(std::size_t) ((w + i) & mask)] = inR[i];
  1388. std::fill (outL, outL + n, 0.0f);
  1389. std::fill (outR, outR + n, 0.0f);
  1390. bool firstTap = true;
  1391. for (auto& t : taps)
  1392. {
  1393. const auto& ring = t.ch ? ringR : ringL;
  1394. auto ph = t.phase;
  1395. for (int i = 0; i < n; ++i)
  1396. {
  1397. const auto sp = (double) (std::int32_t) ph;
  1398. auto v = std::fabs (sp * (double) t.p8) - (double) t.p10;
  1399. v = ((double) t.p9 - std::fabs (v)) * v + (double) t.p3;
  1400. ph += t.inc;
  1401. const auto k = (int) v; // x87 fistl with the truncating control word (v > 0)
  1402. const auto at = w + i - k;
  1403. const auto a = (double) ring[(std::size_t) (at & mask)], b = (double) ring[(std::size_t) ((at - 1) & mask)];
  1404. const auto s = (b - a) * (v - (double) k) + a;
  1405. if (firstTap)
  1406. outL[i] = (float) (s * (double) t.gl), outR[i] = (float) (s * (double) t.gr);
  1407. else
  1408. outL[i] = (float) (s * (double) t.gl + (double) outL[i]), outR[i] = (float) (s * (double) t.gr + (double) outR[i]);
  1409. }
  1410. t.phase = ph;
  1411. firstTap = false;
  1412. }
  1413. w = (w + n) & mask;
  1414. }
  1415. };
  1416. // Harmless's chorus records (table 0x7ee73c + 0x20·type, DATA): order, depth/1024, speed/1024, delay/128, spread/128,
  1417. // cross/128; the rack's mix knob gives the dry gain and the wet volume
  1418. struct Rack::Chorus : ChorusUnit
  1419. {
  1420. static ChorusUnit make (int type, float mixKnob, double fs)
  1421. {
  1422. static constexpr int rec[8][6] { { 4, 512, 768, 8, 128, -32 }, { 5, 1024, 856, 0, 128, 68 }, { 7, 512, 512, 128, 128, 54 },
  1423. { 6, 561, 854, 0, 0, 38 }, { 1, 136, 312, 0, 0, 128 }, { 2, 86, 632, 0, 0, 0 },
  1424. { 1, 42, 944, 0, 0, -22 }, { 9, 81, 170, 0, 0, -66 } };
  1425. const auto* r = rec[std::clamp (type, 1, 8) - 1];
  1426. return ChorusUnit (r[0], (float) r[1] * 0.0009765625f, (float) r[2] * 0.0009765625f, (float) r[3] * 0.0078125f,
  1427. (float) r[4] * 0.0078125f, (float) r[5] * 0.0078125f, g5 (mixKnob), g5 (f32 (1.0 - (double) mixKnob)), fs);
  1428. }
  1429. Chorus (int type, float mixKnob, double fs) : ChorusUnit (make (type, mixKnob, fs)) {}
  1430. };
  1431. // delay records (table 0x7ee67c + 8·type, DATA): stereo offset in ms (sign = side), ping-pong
  1432. struct Rack::Delay
  1433. {
  1434. std::vector<float> l, r;
  1435. int S = 1, rp = 0, D = 1, oL = 0, oR = 0;
  1436. bool pingPong = false;
  1437. float vol = 0.0f, fb = 0.0f;
  1438. double cur = 0.0, maxStep = 0.001;
  1439. Delay (int type, int volKnob, int fbKnob, int time, double fs, double bpm)
  1440. {
  1441. static constexpr int rec[3][2] { { 0, 0 }, { 1, 2 }, { -20, 2 } };
  1442. const auto* rc = rec[std::clamp (type, 1, 3) - 1];
  1443. const auto o = (int) rint (std::abs (rc[0]) * 0.001 * fs);
  1444. if (rc[0] > 0)
  1445. oR = o;
  1446. else
  1447. oL = o;
  1448. pingPong = rc[1] != 0;
  1449. D = std::max (1, (int) rint ((double) time * fs * 0.3125 / std::max (1.0, bpm)));
  1450. vol = g11 ((float) volKnob * 0.001f);
  1451. fb = (float) fbKnob * 0.001f;
  1452. cur = fb; // the feedback ramp starts at its target (EXEC with the plugin's state loaded before the rate is set)
  1453. maxStep = (double) f32 (0.001 * 44100.0 / fs);
  1454. S = D + o + Rack::maxBlock + 8;
  1455. l.assign ((std::size_t) S, 0.0f);
  1456. r.assign ((std::size_t) S, 0.0f);
  1457. }
  1458. void write (const float* inL, const float* inR, int n) noexcept
  1459. {
  1460. const auto w = rp + D;
  1461. for (int i = 0; i < n; ++i)
  1462. {
  1463. auto& a = l[(std::size_t) ((w + oL + i) % S)];
  1464. a = a + inL[i] * vol;
  1465. auto& b = r[(std::size_t) ((w + oR + i) % S)];
  1466. b = b + inR[i] * vol;
  1467. }
  1468. }
  1469. void advance (int n) noexcept { cur = ramp (cur, fb, n, maxStep).end; }
  1470. void process (float* outL, float* outR, int n) noexcept
  1471. {
  1472. const auto rr = ramp (cur, fb, n, maxStep);
  1473. cur = rr.end;
  1474. auto g = rr.start;
  1475. for (int i = 0; i < n; ++i)
  1476. {
  1477. const auto a = l[(std::size_t) rp], b = r[(std::size_t) rp];
  1478. l[(std::size_t) rp] = 0.0f, r[(std::size_t) rp] = 0.0f;
  1479. const auto w = (rp + D) % S;
  1480. float fl = a, fr = b;
  1481. if (pingPong)
  1482. outL[i] = b, outR[i] = a, fl = b, fr = a;
  1483. else
  1484. outL[i] = a, outR[i] = b;
  1485. auto u = (float) ((double) fl * g + (double) l[(std::size_t) w]);
  1486. auto v = (float) ((double) fr * g + (double) r[(std::size_t) w]);
  1487. l[(std::size_t) w] = std::fabs (u) < flushLevel ? 0.0f : u;
  1488. r[(std::size_t) w] = std::fabs (v) < flushLevel ? 0.0f : v;
  1489. g += rr.step;
  1490. if (++rp >= S)
  1491. rp = 0;
  1492. }
  1493. }
  1494. };
  1495. // the shared Reverb_x64.dll (REA_rodada31 §7.5) with the library wrapper's fields: lowcut Hz (0 off), highcut and
  1496. // damping in hundreds of Hz (0 off), predelay samples, room, diffusion, decay in samples, colour
  1497. struct ReverbUnit : PlateReverb
  1498. {
  1499. ReverbUnit (float lowcut, float highcut, int preS, int room, int diffusion, float decaySamples, float damping, int colourIndex, double fs)
  1500. {
  1501. const auto rate = (long long) fs;
  1502. const auto decay = (double) decaySamples;
  1503. const auto f17 = (double) f32 (1.0 / (double) rate), half = (double) f32 ((double) rate * 0.5);
  1504. if (lowcut != 0.0f)
  1505. {
  1506. const auto t = (double) f32 (std::tan (std::min ((double) lowcut, half) * pi * f17));
  1507. cLc = f32 ((t - 1.0) / (t + 1.0));
  1508. }
  1509. auto lp = [&] (float v) {
  1510. if (v == 0.0f)
  1511. return 1.0f;
  1512. const auto x = (double) f32 (2.0 - std::cos (std::min ((double) v, half) * 628.3185307179587 * f17));
  1513. return f32 (1.0 - (x - std::sqrt (x * x - 1.0)));
  1514. };
  1515. cHc = lp (highcut);
  1516. cDamp = lp (damping);
  1517. auto muldiv = [] (long long a, long long b, long long c) { return (int) ((a * b) / c); };
  1518. const auto sc = muldiv (rate, room + 15, 115);
  1519. for (int i = 0; i < 12; ++i)
  1520. L[(std::size_t) i] = std::max (1, muldiv (lines[i], sc, 20000));
  1521. for (int i = 0; i < 7; ++i)
  1522. tl[(std::size_t) i] = muldiv (tapsL[i], sc, 20000), tr[(std::size_t) i] = muldiv (tapsR[i], sc, 20000);
  1523. const auto dv = (double) f32 (diffusion * 0.01), f13 = (double) f32 (f17 * 0.5);
  1524. if (dv != 0.0)
  1525. {
  1526. auto e = [&] (int a, int b, double k) { return f32 (std::exp (std::log (0.001) * (double) f32 ((a + b) * f13) / (dv * k))); };
  1527. cIn = e (L[0], L[1], 0.07), c1 = e (L[4], L[8], 0.35), c2 = e (L[6], L[10], 0.51);
  1528. }
  1529. const auto ssum = (double) (L[5] + L[7] + L[9] + L[11]);
  1530. g = f32 (std::pow (std::exp (std::log (0.001) * ssum / decay), 0.25));
  1531. auto r2 = (double) f32 ((double) f32 (std::pow (std::exp (std::log (0.001) * ssum / (double) f32 (decay * colourFactor[std::clamp (colourIndex, 0, 4)])), 0.25)) / (double) g);
  1532. c48 = f32 ((r2 - 1.0) * 0.5);
  1533. if (r2 > 1.0)
  1534. r2 = 1.0;
  1535. const auto t = (double) f32 (std::tan (500.0 * pi * f17));
  1536. c44 = f32 ((t - r2) / (t + r2));
  1537. allocate (std::max (1, preS));
  1538. }
  1539. };
  1540. // Harmless's reverb records (table 0x7ee67c + 0x20·type, DATA): lowcut Hz, highcut ×100 Hz, predelay, room,
  1541. // diffusion, decay ×0.1 s, damping ×100 Hz, colour
  1542. struct Rack::Reverb : ReverbUnit
  1543. {
  1544. static ReverbUnit make (int type, double fs)
  1545. {
  1546. static constexpr int rec[6][8] { { 75, 40, 0, 40, 100, 15, 40, 2 }, { 0, 115, 18, 40, 100, 22, 50, 2 },
  1547. { 34, 21, 27, 90, 100, 45, 31, 3 }, { 400, 60, 31, 60, 65, 23, 37, 1 },
  1548. { 100, 36, 0, 35, 100, 6, 36, 2 }, { 100, 60, 0, 17, 100, 5, 30, 2 } };
  1549. const auto* r = rec[std::clamp (type, 1, 6) - 1];
  1550. const auto rate = (long long) fs;
  1551. return ReverbUnit ((float) r[0], (float) r[1], (int) rint ((double) r[2] * (double) rate / 384.0), r[3], r[4],
  1552. f32 ((double) r[5] * (double) rate / 10.0), (float) r[6], r[7], fs);
  1553. }
  1554. Reverb (int type, double fs) : ReverbUnit (make (type, fs)) {}
  1555. };
  1556. Rack::Rack() = default;
  1557. Rack::~Rack() = default;
  1558. void Rack::prepare (const std::array<int, 100>& p, const std::array<int, 7>& t, double fs, double bpm,
  1559. std::shared_ptr<const MaximusPreset> preset)
  1560. {
  1561. chorus.reset();
  1562. delay.reset();
  1563. reverb.reset();
  1564. comp.reset();
  1565. compPreset.reset();
  1566. if (p[90] > 0)
  1567. chorus = std::make_unique<Chorus> (p[90], (float) p[91] * 0.001f, fs);
  1568. if (p[92] > 0)
  1569. delay = std::make_unique<Delay> (p[92], p[93], p[94], p[95], fs, bpm);
  1570. if (p[96] > 0)
  1571. reverb = std::make_unique<Reverb> (p[96], fs);
  1572. wet = f32 ((double) g11 ((float) p[97] * 0.001f) * 0.5);
  1573. if (t[3] > 0 && preset != nullptr && preset->valid)
  1574. {
  1575. compPreset = std::move (preset);
  1576. comp = std::make_unique<Maximus>();
  1577. comp->prepare (compPreset, fs, p[98]);
  1578. }
  1579. on = chorus || delay || reverb || comp;
  1580. tailLength = (int) rint (fs * 0.004) + (chorus ? maxBlock + chorus->maxDelay : 0);
  1581. tail = 0;
  1582. quietNeeded = (long long) (0.25 * fs) + (chorus ? chorus->maxDelay : 0)
  1583. + (delay ? (long long) delay->D * 2 + delay->oL + delay->oR : 0)
  1584. + (reverb ? (long long) reverb->pre.size() + reverb->L[5] + reverb->L[9] : 0);
  1585. quietFor = quietNeeded; // a new rack holds no sound
  1586. }
  1587. void Rack::process (float* left, float* right, int n, bool voices) noexcept
  1588. {
  1589. if (! on || n <= 0)
  1590. return;
  1591. if (voices)
  1592. tail = tailLength;
  1593. else if (tail > 0)
  1594. tail -= n;
  1595. if (chorus)
  1596. {
  1597. if (tail < 1)
  1598. chorus->advance (n);
  1599. else
  1600. {
  1601. chorus->process (left, right, scratchL.data(), scratchR.data(), n);
  1602. if (chorus->dry != 1.0f)
  1603. for (int i = 0; i < n; ++i)
  1604. left[i] = left[i] * chorus->dry, right[i] = right[i] * chorus->dry;
  1605. for (int i = 0; i < n; ++i)
  1606. left[i] = left[i] + scratchL[(std::size_t) i], right[i] = right[i] + scratchR[(std::size_t) i];
  1607. }
  1608. }
  1609. if (delay)
  1610. {
  1611. if (tail > 0)
  1612. delay->write (left, right, n);
  1613. delay->process (scratchL.data(), scratchR.data(), n);
  1614. for (int i = 0; i < n; ++i)
  1615. left[i] = left[i] + scratchL[(std::size_t) i], right[i] = right[i] + scratchR[(std::size_t) i];
  1616. }
  1617. if (reverb)
  1618. {
  1619. for (int i = 0; i < n; ++i)
  1620. mono[(std::size_t) i] = left[i] + right[i];
  1621. reverb->process (mono.data(), scratchL.data(), scratchR.data(), n);
  1622. for (int i = 0; i < n; ++i)
  1623. left[i] = left[i] + scratchL[(std::size_t) i] * wet, right[i] = right[i] + scratchR[(std::size_t) i] * wet;
  1624. }
  1625. if (comp)
  1626. comp->process (left, right, n);
  1627. float peak = 0.0f;
  1628. for (int i = 0; i < n; ++i)
  1629. peak = std::max (peak, std::max (std::fabs (left[i]), std::fabs (right[i])));
  1630. if (voices || peak > 1.0e-7f)
  1631. quietFor = 0;
  1632. else
  1633. quietFor += n;
  1634. }
  1635. void Rack::advance (int n) noexcept
  1636. {
  1637. if (! on || n <= 0)
  1638. return;
  1639. if (tail > 0)
  1640. tail -= n;
  1641. if (chorus)
  1642. chorus->advance (n);
  1643. if (delay)
  1644. delay->advance (n);
  1645. if (comp)
  1646. comp->silence (n);
  1647. }
  1648. // =================================================================================================================
  1649. // Harmor's rack (REA_rodada38): Gen_Render's unit list (FUN_00892e10) over the record mix
  1650. // =================================================================================================================
  1651. std::vector<std::uint8_t> withMaximusParams (std::vector<std::uint8_t> state, const std::vector<std::pair<int, int>>& params)
  1652. {
  1653. for (const auto& [index, value] : params)
  1654. if (index >= 0 && index < 59 && 4 + 4 * (std::size_t) index + 4 <= state.size())
  1655. std::memcpy (state.data() + 4 + 4 * (std::size_t) index, &value, 4);
  1656. return state;
  1657. }
  1658. namespace
  1659. {
  1660. const double ln2 = 0.69314718055994529;
  1661. constexpr float rampEps = 5.960464477539063e-08f; // 0x91b14c
  1662. // the library's stereo gain with a one-pole ramp (FUN_007a4cc0 snap, FUN_007a4a90 / FUN_007a4c00 multiply,
  1663. // FUN_007a49b0 / FUN_007a4b10 multiply-add, FUN_007a4c70 values): per sample d ← d·c, gain = target + d
  1664. struct StereoRamp
  1665. {
  1666. float tL = 0, tR = 0, cL = 0, cR = 0, coef = 1;
  1667. bool settled() noexcept
  1668. {
  1669. if (! (std::fabs (tL - cL) >= rampEps) && ! (std::fabs (tR - cR) >= rampEps))
  1670. {
  1671. cL = tL, cR = tR;
  1672. return true;
  1673. }
  1674. return false;
  1675. }
  1676. void mul (const float* iL, const float* iR, float* oL, float* oR, int n) noexcept
  1677. {
  1678. if (settled())
  1679. {
  1680. if (tL == 0.0f && tR == 0.0f && ! std::signbit (tL) && ! std::signbit (tR))
  1681. std::fill (oL, oL + n, 0.0f), std::fill (oR, oR + n, 0.0f);
  1682. else
  1683. for (int i = 0; i < n; ++i)
  1684. oL[i] = iL[i] * tL, oR[i] = iR[i] * tR;
  1685. return;
  1686. }
  1687. auto dL = cL - tL, dR = cR - tR;
  1688. for (int i = 0; i < n; ++i)
  1689. {
  1690. dL = dL * coef, dR = dR * coef;
  1691. oL[i] = iL[i] * (dL + tL), oR[i] = iR[i] * (dR + tR);
  1692. }
  1693. cL = dL + tL, cR = dR + tR;
  1694. }
  1695. void mulAdd (const float* iL, const float* iR, float* oL, float* oR, int n) noexcept
  1696. {
  1697. if (settled())
  1698. {
  1699. std::uint32_t bl = 0, br = 0;
  1700. std::memcpy (&bl, &tL, 4);
  1701. std::memcpy (&br, &tR, 4);
  1702. if (bl != 0 || br != 0)
  1703. for (int i = 0; i < n; ++i)
  1704. oL[i] = iL[i] * tL + oL[i], oR[i] = iR[i] * tR + oR[i];
  1705. return;
  1706. }
  1707. auto dL = cL - tL, dR = cR - tR;
  1708. for (int i = 0; i < n; ++i)
  1709. {
  1710. dL = dL * coef, dR = dR * coef;
  1711. oL[i] = iL[i] * (dL + tL) + oL[i], oR[i] = iR[i] * (dR + tR) + oR[i];
  1712. }
  1713. cL = dL + tL, cR = dR + tR;
  1714. }
  1715. void values (float* oL, float* oR, int n) noexcept
  1716. {
  1717. auto dL = cL - tL, dR = cR - tR;
  1718. for (int i = 0; i < n; ++i)
  1719. {
  1720. dL = dL * coef, dR = dR * coef;
  1721. oL[i] = dL + tL, oR[i] = dR + tR;
  1722. }
  1723. cL = dL + tL, cR = dR + tR;
  1724. }
  1725. bool allZero() const noexcept // FUN_007a4d30 (bit patterns)
  1726. {
  1727. std::uint32_t b[4] {};
  1728. std::memcpy (&b[0], &tL, 4), std::memcpy (&b[1], &tR, 4), std::memcpy (&b[2], &cL, 4), std::memcpy (&b[3], &cR, 4);
  1729. return b[0] == 0 && b[1] == 0 && b[2] == 0 && b[3] == 0;
  1730. }
  1731. };
  1732. // 16.3515987·2^(v/100) Hz (the library's frequency law: float v·0.01, exp in double, × the run-time float 16.35)
  1733. float noteHz (int v) noexcept
  1734. {
  1735. return f32 (std::exp ((double) ((float) v * 0.01f) * ln2) * (double) 16.3515987f);
  1736. }
  1737. // FUN_0062d460: L = cos((1 + pan)·π/4)·v, R = sin(…)·v (x87)
  1738. void panLaw (float pan, float v, float& l, float& r) noexcept
  1739. {
  1740. const auto t = ((long double) 1 + (long double) pan) * (long double) 0.78539816339744830961566;
  1741. l = (float) (std::cos ((double) t) * (double) v);
  1742. r = (float) (std::sin ((double) t) * (double) v);
  1743. }
  1744. } // namespace
  1745. struct HarmorRack::Impl
  1746. {
  1747. HarmorRackSettings S;
  1748. double fs = 44100.0;
  1749. std::array<int, 6> list {};
  1750. int count = 0;
  1751. int counter = 0, reload = 0;
  1752. bool on = false;
  1753. long long quietFor = 0, quietNeeded = 0;
  1754. // distortion (FUN_008944a0)
  1755. int distType = 0, stages = 1, F = 2;
  1756. StereoRamp bias, drive, dry, wet;
  1757. bool biasFlag = false, post = false;
  1758. float qMul = 0.0f, qInv = 0.0f; // types ≥ 11 (+0x2cd0, +0x2cd4)
  1759. std::array<Halfband, 3> up {}, down {};
  1760. std::array<Biquad, 2> postLp {};
  1761. float dcC2 = 0.0f, dcC1 = 0.96f;
  1762. long double dcV[2] {}, dcP[2] {};
  1763. // chorus
  1764. std::unique_ptr<ChorusUnit> chorus;
  1765. // delay (FUN_00892f90 set-up, FUN_00893fc0 process; library FUN_007a5xxx)
  1766. bool delayOn = false, delayLoop = false;
  1767. int dMode = 0, D = 1, S0 = 1, rp = 0, oL = 0, oR = 0;
  1768. float fbTarget = 0.5f, fbCur = 0.0f, maxStep = 0.001f;
  1769. std::vector<float> ringL, ringR;
  1770. StereoRamp inGain;
  1771. std::array<Biquad, 2> dFilt {};
  1772. std::array<bool, 2> dFiltOn {};
  1773. // reverb
  1774. std::unique_ptr<ReverbUnit> reverb;
  1775. StereoRamp revWet;
  1776. // compressor
  1777. std::unique_ptr<Maximus> comp;
  1778. std::array<float, maxBlock * 8> xL {}, xR {}, aL {}, aR {}, bL {}, bR {}, cL {}, cR {};
  1779. std::array<float, maxBlock> tL {}, tR {}, mono {};
  1780. void prepare (const HarmorRackSettings& s)
  1781. {
  1782. S = s;
  1783. fs = s.fs;
  1784. const auto& g = s.g;
  1785. const auto rampCoef = f32 (std::exp (ln001 / (0.015 * fs))); // the rate object's +0x40
  1786. // tail counter reload (FUN_008937f0): 30 ms, 530 ms with the distortion on, + the chorus ring
  1787. distType = s.distortion;
  1788. reload = (int) rint ((double) (distType > 0 ? 0x212 : 0x1e) * 0.001 * fs);
  1789. chorus.reset();
  1790. if (g[32] > 0)
  1791. {
  1792. const auto m = (float) std::abs (g[38]) * 0.001f;
  1793. const auto sign = g[38] == 0 ? 0.0f : (g[38] < 0 ? -1.0f : 1.0f);
  1794. chorus = std::make_unique<ChorusUnit> (g[32], (float) g[33] * 0.0009765625f, (float) g[34] * 0.0009765625f,
  1795. (float) g[35] * 0.0078125f, (float) g[36] * 0.0078125f, (float) g[37] * 0.0078125f,
  1796. g5 (m), sign * g5 (1.0f - m), fs);
  1797. reload += 384 + chorus->maxDelay;
  1798. }
  1799. counter = reload;
  1800. // distortion set-up (ProcessParam cases 0x315…0x319, rate set-up)
  1801. stages = std::clamp (s.precision + 1, 1, 3);
  1802. F = 1 << stages;
  1803. for (auto& h : up)
  1804. h.reset();
  1805. for (auto& h : down)
  1806. h.reset();
  1807. dcV[0] = dcV[1] = dcP[0] = dcP[1] = 0;
  1808. dcC2 = f32 ((44100.0 / fs) * 2.2834999999999997e-05);
  1809. if (distType > 0)
  1810. setupDistortion (g);
  1811. post = distType > 0 && g[21] != 1100;
  1812. if (post)
  1813. {
  1814. const auto f = std::min (noteHz (g[21]), f32 (0.4999 * fs) * (float) F);
  1815. for (auto& q : postLp)
  1816. {
  1817. q.design (1, (double) f * ((2.0 * pi / fs) / (double) F), 0.70710678118654746);
  1818. q.reset();
  1819. }
  1820. }
  1821. for (auto* r : { &bias, &drive, &dry, &wet })
  1822. r->coef = f32 (std::exp (ln001 / ((double) (F * 15) * 0.001 * fs)));
  1823. // delay
  1824. delayOn = g[39] > 0;
  1825. if (delayOn)
  1826. {
  1827. dMode = std::clamp (g[40], 0, 2);
  1828. const auto offMs = f32 ((std::exp ((double) std::abs (g[43]) * 0.015625 * ln2) - 1.0) * 1.9607843137254901);
  1829. const auto off = (int) rint ((double) offMs * 0.001 * fs);
  1830. double t = 0.0;
  1831. if (g[42] < 0)
  1832. t = (std::exp ((double) g[42] * -0.010416666666666666 * ln2) - 1.0) * 0.06666666666666667;
  1833. else
  1834. t = ((double) g[42] * 0.3125) / (double) (float) s.bpm;
  1835. D = std::max (1, (int) rint ((double) ((float) t * (float) fs)));
  1836. oL = g[43] > 0 ? 0 : off;
  1837. oR = g[43] > 0 ? off : 0;
  1838. S0 = off + 384 + D;
  1839. ringL.assign ((std::size_t) S0, 0.0f);
  1840. ringR.assign ((std::size_t) S0, 0.0f);
  1841. rp = 0;
  1842. fbTarget = (float) g[41] * 0.001f;
  1843. fbCur = 0.5f; // the library constructor's feedback (FUN_007a5290); the ramp starts there (EXEC)
  1844. maxStep = f32 (0.001 * 44100.0 / fs);
  1845. auto vol = (float) g[44] * 0.001f;
  1846. vol = vol * vol * vol;
  1847. auto pan = (float) g[45] * 0.0078125f;
  1848. if (dMode > 0)
  1849. pan = -pan;
  1850. if (pan == 0.0f && ! std::signbit (pan))
  1851. inGain.tL = inGain.tR = vol;
  1852. else if (vol == 0.0f)
  1853. inGain.tL = inGain.tR = 0.0f;
  1854. else
  1855. panLaw (pan, vol * 1.41421354f, inGain.tL, inGain.tR);
  1856. inGain.cL = inGain.cR = 0.0f; // (from 0 at state load, EXEC)
  1857. inGain.coef = rampCoef;
  1858. delayLoop = g[48] > 0;
  1859. const int types[2] { 4, 1 };
  1860. for (int k = 0; k < 2; ++k)
  1861. {
  1862. dFiltOn[(std::size_t) k] = g[(std::size_t) (46 + k)] != k * 1050;
  1863. if (dFiltOn[(std::size_t) k])
  1864. {
  1865. const auto f = std::min (noteHz (g[(std::size_t) (46 + k)]), f32 (0.4999 * fs));
  1866. dFilt[(std::size_t) k].design (types[k], (double) f * (2.0 * pi / fs), 1.0);
  1867. dFilt[(std::size_t) k].reset();
  1868. }
  1869. }
  1870. }
  1871. // reverb (FUN_00893470)
  1872. reverb.reset();
  1873. if (g[22] > 0)
  1874. {
  1875. const auto rate = (long long) fs;
  1876. double pre = 0.0;
  1877. if (g[25] < 0)
  1878. pre = (std::exp ((double) g[25] * -0.015625 * ln2) - 1.0) * 0.015873015873015872;
  1879. else
  1880. pre = ((double) g[25] * 0.3125) / (double) (float) s.bpm;
  1881. const auto preS = (int) rint (pre * (double) rate);
  1882. const auto decay = f32 (std::exp ((double) ((float) g[28] * 0.01f) * ln2) * 0.1 * (double) rate);
  1883. const auto lc = g[23] > 0 ? noteHz (g[23]) : 0.0f;
  1884. auto hz100 = [&] (int v) {
  1885. return v < 1050 ? f32 (std::exp ((double) ((float) v * 0.01f) * ln2) * (double) 16.3515987f * (double) 0.01f) : 0.0f;
  1886. };
  1887. reverb = std::make_unique<ReverbUnit> (lc, hz100 (g[24]), preS, g[26], g[27], decay, hz100 (g[29]), g[30], fs);
  1888. const auto w = (double) ((float) g[31] * 0.001f);
  1889. revWet.tL = revWet.tR = f32 (w * w * w * 0.5);
  1890. revWet.cL = revWet.cR = 0.0f; // the ramp starts from 0 at state load (EXEC)
  1891. revWet.coef = rampCoef;
  1892. }
  1893. // compressor (hosted SG.dll with Harmor's own preset)
  1894. comp.reset();
  1895. if (s.comp != nullptr && s.comp->valid)
  1896. {
  1897. comp = std::make_unique<Maximus>();
  1898. comp->prepare (s.comp, fs, s.compMix);
  1899. }
  1900. // the unit list (FUN_00892e10)
  1901. count = 0;
  1902. for (const auto t : s.order)
  1903. {
  1904. const bool present = t == 0 ? distType > 0 : t == 1 ? chorus != nullptr : t == 2 ? delayOn : t == 3 ? reverb != nullptr
  1905. : t == 4 ? true : comp != nullptr;
  1906. if (present && count < 6)
  1907. list[(std::size_t) count++] = t;
  1908. }
  1909. on = distType > 0 || chorus || delayOn || reverb || comp;
  1910. quietNeeded = (long long) (0.25 * fs) + reload + (delayOn ? (long long) D * 2 + oL + oR : 0)
  1911. + (reverb ? (long long) reverb->pre.size() + reverb->L[5] + reverb->L[9] : 0) + (long long) (1.6 * fs);
  1912. quietFor = quietNeeded;
  1913. }
  1914. void setupDistortion (const std::array<int, 53>& g)
  1915. {
  1916. const auto type = distType;
  1917. // volume (g19) and mix (g20): wet = vol·(1 − |mix|)/normaliser, dry = mix
  1918. const auto v = (float) g[19] * 0.001f;
  1919. const auto vol = v == 1.0f ? 1.0f : f32 ((std::exp ((double) (v * 1.09861231f)) - 1.0) * (double) 0.5f);
  1920. const auto mixF = (float) g[20] * 0.001f;
  1921. dry.tL = dry.tR = mixF;
  1922. const auto d12 = (double) vol * (1.0 - std::fabs ((double) mixF));
  1923. const auto g17 = g[17];
  1924. biasFlag = true;
  1925. float dr = 0.0f, wt = 0.0f;
  1926. if (type >= 1 && type <= 3)
  1927. {
  1928. const auto d = std::exp ((double) ((float) g17 * 0.01f - 4.0f) * ln2);
  1929. dr = f32 (d * pi);
  1930. wt = f32 (d12 / (std::tanh (d) * pi));
  1931. }
  1932. else if (type == 4)
  1933. {
  1934. const auto d = std::exp ((double) ((float) g17 * 0.01f - 2.0f) * ln2);
  1935. const auto t = std::tanh (d * 4.0);
  1936. dr = f32 (d * pi);
  1937. wt = f32 (d12 / (t * t * t * pi));
  1938. }
  1939. else if (type == 5)
  1940. {
  1941. const auto d = std::exp (((double) g17 * 0.025 - 5.0) * ln2);
  1942. dr = f32 (d);
  1943. wt = f32 (d12 / (std::log (d + 1.0) * (double) ((float) g17 * 0.001f + 1.0f)));
  1944. }
  1945. else if (type == 6)
  1946. {
  1947. const auto d = std::exp (((double) g17 * 0.005 - 3.0) * ln2);
  1948. dr = f32 (d * pi);
  1949. wt = f32 (d12 / (std::tanh (d) * pi));
  1950. }
  1951. else if (type == 7)
  1952. {
  1953. const auto d = std::exp (((double) g17 * 0.002 - 1.0) * ln2) * pi;
  1954. dr = f32 (d * d);
  1955. wt = f32 (d12 / std::sqrt (d));
  1956. }
  1957. else if (type == 8)
  1958. {
  1959. const auto d = std::exp ((double) ((float) g17 * 0.01f - 4.0f) * ln2);
  1960. dr = f32 (d * pi);
  1961. wt = f32 (d12 / (std::tanh (d) * pi));
  1962. }
  1963. else if (type == 9)
  1964. {
  1965. const auto b = f32 (std::exp ((double) g[18] * 0.004 * ln2));
  1966. bias.tL = bias.tR = b;
  1967. dr = f32 (std::exp ((7.0 - (double) g17 * 0.007) * ln2) * pi);
  1968. wt = f32 (d12 / (double) (dr * b));
  1969. biasFlag = false;
  1970. }
  1971. else if (type == 10)
  1972. {
  1973. dr = f32 (std::exp ((9.0 - (double) g17 * 0.007) * ln2));
  1974. wt = f32 (d12 / (double) dr);
  1975. }
  1976. else
  1977. {
  1978. const auto q = f32 (std::exp ((double) g[18] * 0.02 * ln2) * 1024.0);
  1979. dr = q;
  1980. qMul = f32 (std::exp ((10.0 - (double) g17 * 0.007) * ln2) / std::log ((double) (q + 1.0f)));
  1981. qInv = 1.0f / qMul;
  1982. wt = f32 (d12 / (double) q);
  1983. biasFlag = false;
  1984. }
  1985. drive.tL = drive.tR = dr;
  1986. wet.tL = wet.tR = wt;
  1987. if (biasFlag)
  1988. {
  1989. const auto b = (float) ((double) g[18] * 0.002);
  1990. const auto f = type == 8 ? b * b * dr : f32 ((double) (b * b) * 0.75);
  1991. bias.tL = f;
  1992. bias.tR = b <= 0.0f ? -f : f; // (an integer test on the float's bits: g18 ≤ 0 → negated, EXEC)
  1993. biasFlag = type != 8;
  1994. }
  1995. for (auto* r : { &bias, &drive, &dry, &wet })
  1996. r->cL = r->tL, r->cR = r->tR; // the ramps start at their targets (EXEC)
  1997. }
  1998. void distortion (float* L, float* R, int n) noexcept
  1999. {
  2000. // up-sampling cascade (FUN_00863fd0): stage S−1 first
  2001. const float* sl = L;
  2002. const float* sr = R;
  2003. int m = n;
  2004. float* outs[2][2] { { aL.data(), aR.data() }, { bL.data(), bR.data() } };
  2005. int k = 0;
  2006. for (int st = stages - 1; st >= 0; --st, k ^= 1)
  2007. {
  2008. up[(std::size_t) st].up (sl, sr, outs[k][0], outs[k][1], m);
  2009. sl = outs[k][0], sr = outs[k][1];
  2010. m *= 2;
  2011. }
  2012. auto* xl = const_cast<float*> (sl);
  2013. auto* xr = const_cast<float*> (sr);
  2014. float* dl = xL.data();
  2015. float* drr = xR.data();
  2016. dry.mul (xl, xr, dl, drr, m);
  2017. float* yl = cL.data();
  2018. float* yr = cR.data();
  2019. if (biasFlag && ! bias.allZero())
  2020. {
  2021. bias.values (yl, yr, m);
  2022. for (int i = 0; i < m; ++i)
  2023. xl[i] = yl[i] + xl[i], xr[i] = yr[i] + xr[i];
  2024. }
  2025. drive.mul (xl, xr, xl, xr, m);
  2026. shape (xl, xr, yl, yr, m);
  2027. wet.mulAdd (yl, yr, dl, drr, m);
  2028. if (post)
  2029. for (auto& q : postLp)
  2030. q.process (dl, drr, dl, drr, m);
  2031. // down-sampling cascade (FUN_00864070): in place, the last stage into the output
  2032. for (int st = stages - 1; st >= 0; --st)
  2033. {
  2034. m /= 2;
  2035. float* ol = st == 0 ? xl : dl;
  2036. float* orr = st == 0 ? xr : drr;
  2037. down[(std::size_t) st].down (dl, drr, ol, orr, m);
  2038. }
  2039. // DC stage (FUN_0062a3c0, x87): v += (x − p)·c2, p += v, v ·= c1, out = x − p
  2040. float* ch[2] { xl, xr };
  2041. float* out[2] { L, R };
  2042. for (int c = 0; c < 2; ++c)
  2043. {
  2044. auto v = dcV[c], pp = dcP[c];
  2045. for (int i = 0; i < n; ++i)
  2046. {
  2047. const auto x = (long double) ch[c][i];
  2048. v = v + (x - pp) * (long double) dcC2;
  2049. pp = pp + v;
  2050. v = v * (long double) dcC1;
  2051. out[c][i] = (float) (x - pp);
  2052. }
  2053. dcV[c] = (long double) (double) v, dcP[c] = (long double) (double) pp;
  2054. }
  2055. }
  2056. void shape (float* xl, float* xr, float* yl, float* yr, int m) noexcept
  2057. {
  2058. auto sgn1 = [] (float x) { return std::signbit (x) ? -1.0f : 1.0f; }; // AndC 0x80000000, OrC 1.0
  2059. float* ch[2] { xl, xr };
  2060. float* out[2] { yl, yr };
  2061. for (int c = 0; c < 2; ++c)
  2062. {
  2063. auto* x = ch[c];
  2064. auto* y = out[c];
  2065. switch (distType)
  2066. {
  2067. case 1:
  2068. for (int i = 0; i < m; ++i)
  2069. y[i] = std::tanh (x[i]);
  2070. break;
  2071. case 2:
  2072. for (int i = 0; i < m; ++i)
  2073. y[i] = std::exp (std::tanh (x[i])) + -1.0f;
  2074. break;
  2075. case 3:
  2076. for (int i = 0; i < m; ++i)
  2077. y[i] = std::tanh (std::exp (x[i]) + -1.0f);
  2078. break;
  2079. case 4:
  2080. for (int i = 0; i < m; ++i)
  2081. {
  2082. const auto t = std::tanh (x[i]);
  2083. y[i] = t * (t * t);
  2084. }
  2085. break;
  2086. case 5:
  2087. for (int i = 0; i < m; ++i)
  2088. y[i] = std::log (std::fabs (x[i]) + 1.0f) * sgn1 (x[i]);
  2089. break;
  2090. case 6:
  2091. for (int i = 0; i < m; ++i)
  2092. y[i] = std::cbrt (std::tanh ((x[i] * x[i]) * x[i]));
  2093. break;
  2094. case 7:
  2095. for (int i = 0; i < m; ++i)
  2096. y[i] = std::sin (std::sqrt (std::fabs (x[i]))) * sgn1 (x[i]);
  2097. break;
  2098. case 9:
  2099. for (int i = 0; i < m; ++i)
  2100. y[i] = std::sin (x[i]);
  2101. break;
  2102. case 10:
  2103. for (int i = 0; i < m; ++i)
  2104. y[i] = std::round (x[i]);
  2105. break;
  2106. case 8:
  2107. break;
  2108. default:
  2109. for (int i = 0; i < m; ++i)
  2110. {
  2111. const auto l = std::log (std::fabs (x[i]) + 1.0f) * qMul;
  2112. y[i] = std::expm1 (std::round (l) * qInv) * sgn1 (x[i]);
  2113. }
  2114. break;
  2115. }
  2116. }
  2117. if (distType == 8)
  2118. {
  2119. // tanh(x)·tanh(x + b)·tanh(x + 2b), b = the bias ramp
  2120. float* bl = aL.data() == xl ? bL.data() : aL.data();
  2121. float* br = aR.data() == xr ? bR.data() : aR.data();
  2122. bias.values (bl, br, m);
  2123. for (int i = 0; i < m; ++i)
  2124. {
  2125. yl[i] = std::tanh (xl[i]), yr[i] = std::tanh (xr[i]);
  2126. auto ul = xl[i] + bl[i], ur = xr[i] + br[i];
  2127. yl[i] = yl[i] * std::tanh (ul), yr[i] = yr[i] * std::tanh (ur);
  2128. ul = bl[i] + ul, ur = br[i] + ur;
  2129. yl[i] = yl[i] * std::tanh (ul), yr[i] = yr[i] * std::tanh (ur);
  2130. }
  2131. }
  2132. else if (distType == 9)
  2133. bias.mulAdd (xl, xr, yl, yr, m);
  2134. }
  2135. void delay (float* L, float* R, int n, bool active) noexcept
  2136. {
  2137. for (int at = 0; at < n; at += 384)
  2138. {
  2139. const auto c = std::min (384, n - at);
  2140. float* tl = tL.data() + at;
  2141. float* tr = tR.data() + at;
  2142. if (! active)
  2143. inGain.cL = inGain.tL, inGain.cR = inGain.tR;
  2144. else
  2145. {
  2146. inGain.mul (L + at, R + at, tl, tr, c);
  2147. if (! delayLoop)
  2148. for (int k = 0; k < 2; ++k)
  2149. if (dFiltOn[(std::size_t) k])
  2150. dFilt[(std::size_t) k].process (tl, tr, tl, tr, c);
  2151. const auto w = rp + D;
  2152. for (int i = 0; i < c; ++i)
  2153. {
  2154. auto& a = ringL[(std::size_t) ((w + oL + i) % S0)];
  2155. a = a + tl[i];
  2156. auto& b = ringR[(std::size_t) ((w + oR + i) % S0)];
  2157. b = b + tr[i];
  2158. }
  2159. }
  2160. int left = c, o = 0;
  2161. while (left > 0)
  2162. {
  2163. auto seg = std::min (D, S0 - rp); // FUN_007a5260: up to the write pointer or the ring end
  2164. seg = std::min (seg, left);
  2165. if (delayLoop)
  2166. for (int k = 0; k < 2; ++k)
  2167. if (dFiltOn[(std::size_t) k])
  2168. dFilt[(std::size_t) k].process (ringL.data() + rp, ringR.data() + rp, ringL.data() + rp, ringR.data() + rp, seg);
  2169. // feedback ramp (FUN_0062d390)
  2170. const auto start = fbCur;
  2171. auto step = fbTarget - start;
  2172. if (step != 0.0f)
  2173. {
  2174. fbCur = fbTarget;
  2175. step = step / (float) seg;
  2176. if (maxStep < std::fabs (step))
  2177. {
  2178. step = std::copysign (maxStep, step);
  2179. fbCur = start + step * (float) seg;
  2180. if (std::fabs (fbCur) < 5.960464477539063e-08f)
  2181. fbCur = 0.0f, step = -start / (float) seg;
  2182. }
  2183. }
  2184. auto g = start;
  2185. for (int i = 0; i < seg; ++i)
  2186. {
  2187. const auto r = rp;
  2188. auto w = rp + D;
  2189. if (w >= S0)
  2190. w -= S0;
  2191. const auto a = ringL[(std::size_t) r], b = ringR[(std::size_t) r];
  2192. ringL[(std::size_t) r] = 0.0f, ringR[(std::size_t) r] = 0.0f;
  2193. float outL = a, outR = b, fl = a, fr = b;
  2194. if (dMode == 1)
  2195. outL = b, outR = a;
  2196. else if (dMode == 2)
  2197. outL = b, outR = a, fl = b, fr = a;
  2198. tl[o + i] = outL, tr[o + i] = outR;
  2199. auto u = fl * g + ringL[(std::size_t) w];
  2200. auto v = fr * g + ringR[(std::size_t) w];
  2201. ringL[(std::size_t) w] = std::fabs (u) < 5.960464477539063e-08f ? 0.0f : u;
  2202. ringR[(std::size_t) w] = std::fabs (v) < 5.960464477539063e-08f ? 0.0f : v;
  2203. g = g + step;
  2204. if (++rp >= S0)
  2205. rp = 0;
  2206. }
  2207. left -= seg, o += seg;
  2208. }
  2209. for (int i = 0; i < c; ++i)
  2210. L[at + i] = L[at + i] + tl[i], R[at + i] = R[at + i] + tr[i];
  2211. }
  2212. }
  2213. void process (float* L, float* R, const float* fxL, const float* fxR, int n, bool voices) noexcept
  2214. {
  2215. if (voices)
  2216. counter = reload;
  2217. else if (counter > 0)
  2218. counter -= n;
  2219. auto active = counter;
  2220. for (int k = 0; k < count; ++k)
  2221. switch (list[(std::size_t) k])
  2222. {
  2223. case 0:
  2224. if (active >= 1)
  2225. distortion (L, R, n);
  2226. break;
  2227. case 1:
  2228. if (active < 1)
  2229. chorus->advance (n);
  2230. else
  2231. {
  2232. chorus->process (L, R, tL.data(), tR.data(), n);
  2233. if (chorus->dry != 1.0f)
  2234. for (int i = 0; i < n; ++i)
  2235. L[i] = L[i] * chorus->dry, R[i] = R[i] * chorus->dry;
  2236. for (int i = 0; i < n; ++i)
  2237. L[i] = L[i] + tL[(std::size_t) i], R[i] = R[i] + tR[(std::size_t) i];
  2238. }
  2239. break;
  2240. case 2:
  2241. delay (L, R, n, active >= 1);
  2242. active = 0x7fffffff;
  2243. break;
  2244. case 3:
  2245. for (int i = 0; i < n; ++i)
  2246. mono[(std::size_t) i] = L[i] + R[i];
  2247. reverb->process (mono.data(), tL.data(), tR.data(), n);
  2248. revWet.mulAdd (tL.data(), tR.data(), L, R, n);
  2249. active = 0x7fffffff;
  2250. break;
  2251. case 4:
  2252. if (fxL != nullptr)
  2253. for (int i = 0; i < n; ++i)
  2254. L[i] = L[i] + fxL[i], R[i] = R[i] + fxR[i];
  2255. break;
  2256. default:
  2257. comp->process (L, R, n);
  2258. active = 0x7fffffff;
  2259. break;
  2260. }
  2261. float peak = 0.0f;
  2262. for (int i = 0; i < n; ++i)
  2263. peak = std::max (peak, std::max (std::fabs (L[i]), std::fabs (R[i])));
  2264. if (voices || peak > 1.0e-7f)
  2265. quietFor = 0;
  2266. else
  2267. quietFor += n;
  2268. }
  2269. void advance (int n) noexcept
  2270. {
  2271. if (counter > 0)
  2272. counter -= n;
  2273. if (chorus)
  2274. chorus->advance (n);
  2275. if (delayOn)
  2276. {
  2277. inGain.cL = inGain.tL, inGain.cR = inGain.tR;
  2278. fbCur = fbTarget;
  2279. }
  2280. if (reverb)
  2281. {
  2282. revWet.cL = revWet.tL, revWet.cR = revWet.tR;
  2283. }
  2284. if (comp)
  2285. comp->silence (n);
  2286. }
  2287. };
  2288. HarmorRack::HarmorRack() : d (std::make_unique<Impl>()) {}
  2289. HarmorRack::~HarmorRack() = default;
  2290. void HarmorRack::prepare (const HarmorRackSettings& settings) { d->prepare (settings); }
  2291. bool HarmorRack::enabled() const noexcept { return d->on; }
  2292. void HarmorRack::process (float* left, float* right, const float* fxL, const float* fxR, int n, bool voices) noexcept
  2293. {
  2294. d->process (left, right, fxL, fxR, n, voices);
  2295. }
  2296. void HarmorRack::advance (int n) noexcept { d->advance (n); }
  2297. bool HarmorRack::quiet() const noexcept { return ! d->on || d->quietFor >= d->quietNeeded; }
  2298. } // namespace lynx::hlfx
  2299. namespace lynx::hlfx
  2300. {
  2301. // =================================================================================================================
  2302. // The engine on its own (mixer Maximus, Soundgoodizer, Vocodex's SG stage; REA_rodada40)
  2303. // =================================================================================================================
  2304. MaximusProcessor::MaximusProcessor() = default;
  2305. MaximusProcessor::~MaximusProcessor() = default;
  2306. void MaximusProcessor::prepare (std::shared_ptr<const MaximusPreset> preset, double sampleRate, int mixParameter)
  2307. {
  2308. if (preset == nullptr)
  2309. {
  2310. engine.reset();
  2311. return;
  2312. }
  2313. engine = std::make_unique<Maximus>();
  2314. engine->prepare (std::move (preset), sampleRate, mixParameter);
  2315. }
  2316. void MaximusProcessor::process (float* left, float* right, int n) noexcept
  2317. {
  2318. if (engine == nullptr)
  2319. return;
  2320. for (int done = 0; done < n; done += Rack::maxBlock)
  2321. {
  2322. const auto len = std::min (Rack::maxBlock, n - done);
  2323. engine->process (left + done, right + done, len);
  2324. }
  2325. }
  2326. void MaximusProcessor::reset() noexcept
  2327. {
  2328. if (engine != nullptr)
  2329. engine->reset();
  2330. }
  2331. int MaximusProcessor::latencySamples() const noexcept { return engine != nullptr ? engine->latency() : 0; }
  2332. int maximusMixParameter (const MaximusPreset& preset) noexcept { return preset.p[5]; }
  2333. } // namespace lynx::hlfx