HarmlessFx.cpp 91 KB

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