HardcoreFx.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. // FL Studio's Hardcore (REA_rodada71, clean-room): state, parameters, the five pedal boxes, cabinet and EQ.
  2. // Our own code, written from the behaviour documented in docs/analise/REA_rodada71.md. The plug-in computes in float32
  3. // with explicit double steps; this file keeps those types and the operation order of every expression (contraction
  4. // off), which is what makes it bit-identical to the plug-in.
  5. #include "core/HardcoreFx.h"
  6. #include "core/Effects.h"
  7. #include "core/IlFxKit.h"
  8. #include "core/IldspMath.h"
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <optional>
  14. #include <cpuid.h>
  15. #include <immintrin.h>
  16. #if defined(__clang__)
  17. #pragma clang fp contract(off)
  18. #elif defined(__GNUC__)
  19. #pragma GCC optimize ("fp-contract=off")
  20. #endif
  21. namespace lynx::hardcore
  22. {
  23. namespace
  24. {
  25. using namespace ilfx;
  26. constexpr float tinyNeg = -1e-9f;
  27. // The plug-in's statically linked CRT float routines are the same code as Shared/ildsp_x64.dll's (FMA path on CPUs
  28. // with FMA, chosen at run time): the re-creations of core/IldspMath.h (REA_rodada60 §4.5) are used
  29. struct HcCrt
  30. {
  31. static float powf (float x, float y) noexcept { return ildsp::powf_ (x, y); }
  32. static float sinf (float x) noexcept { return ildsp::sinf_ (x); }
  33. static float cosf (float x) noexcept { return ildsp::cosf_ (x); }
  34. static void sincosf (float x, float& s, float& c) noexcept { ildsp::sincosf_ (x, s, c); }
  35. static float expf (float x) noexcept { return ildsp::expf_ (x); }
  36. static float tanf (float x) noexcept { return tanfCrt (x); } // no FMA path in this routine
  37. };
  38. using LpBiquad = LpBiquadT<HcCrt>;
  39. using EqBand = EqBandT<HcCrt>;
  40. // cvttsd2si into an unsigned 32-bit count (the plug-in's (uint)(long long) x)
  41. unsigned u32 (double x) noexcept { return (unsigned) (long long) x; }
  42. // float sine/cosine pair of the plug-in's packed kernel (small arguments: x, 1 − x²/2)
  43. void sinCosF (float w, float& s, float& c) noexcept { ildsp::sincosf_ (w, s, c); }
  44. // double pair (sin x, sin(|x| + π/2)) of the plug-in's packed double kernel (small arguments: x, 1 − x²/2)
  45. void sinCosD (double x, double& s, double& c) noexcept
  46. {
  47. const auto a = std::fabs (x);
  48. if (a < 7.450580596923828e-09)
  49. {
  50. s = x;
  51. c = 1.0 - (a * a) * 0.5;
  52. return;
  53. }
  54. s = std::sin (x);
  55. c = std::sin (a + 1.5707963267948966);
  56. }
  57. // ------------------------------------------------------------------------------------------------ small filters
  58. // second-order high-pass with a design cache (the plug-in's FUN_18001d050 / FUN_18001d150)
  59. struct HpBiquad
  60. {
  61. float f = 0, q = 0, fs = 0, a = 0, b1 = 0, b2 = 0, x2 = 0, x1 = 0, y2 = 0, y1 = 0;
  62. HpBiquad()
  63. {
  64. design (100.0f, 1.0f, 44100.0f);
  65. clear();
  66. }
  67. void clear() noexcept { x2 = x1 = y2 = y1 = 0.0f; }
  68. void design (float nf, float nq, float nfs) noexcept
  69. {
  70. if (nf == f && nq == q && nfs == fs)
  71. return;
  72. f = nf, q = nq, fs = nfs;
  73. const auto w = (float) ((double) f * 6.2831853071795862 / (double) fs);
  74. float s = 0, c = 0;
  75. sinCosF (w, s, c);
  76. const auto inv = 1.0f / (s + q);
  77. const auto den = s / q + 2.0f;
  78. const auto na = (c + 1.0f) / den;
  79. const auto nb1 = (c * inv) * (q * -2.0f);
  80. const auto nb2 = (q - s) * inv;
  81. a = tiny0 (na), b1 = tiny0 (nb1), b2 = tiny0 (nb2);
  82. }
  83. float tick (float x) noexcept
  84. {
  85. x = tiny0 (x);
  86. auto y = (((x - (x1 + x1)) + x2) * a - y1 * b1) - b2 * y2;
  87. y = tiny0 (y);
  88. x2 = x1, y2 = y1, x1 = x, y1 = y;
  89. return y;
  90. }
  91. };
  92. // band-pass with a design cache, designed in double (FUN_18001d420 / FUN_18001d580)
  93. struct BpBiquad
  94. {
  95. float f = 0, q = 0, fs = 0, b0 = 0, a1 = 0, b2 = 0, x2 = 0, x1 = 0, y2 = 0, y1 = 0;
  96. BpBiquad()
  97. {
  98. design (100.0f, 1.0f, 44100.0f);
  99. clear();
  100. }
  101. void clear() noexcept { x2 = x1 = y2 = y1 = 0.0f; }
  102. void design (float nf, float nq, float nfs) noexcept
  103. {
  104. if (nf == f && nq == q && nfs == fs)
  105. return;
  106. f = nf, q = nq, fs = nfs;
  107. const auto w = (double) f * 6.2831853071795862 / (double) fs;
  108. double s = 0, c = 0;
  109. sinCosD (w, s, c);
  110. const auto d = 1.0 / ((0.5 / (double) q) * s + 1.0);
  111. const auto nb0 = (float) ((double) (float) (s * 0.5) * d);
  112. const auto na1 = (float) ((double) (float) (c + c) * d);
  113. const auto nb2 = (float) ((double) (float) (1.0 - s / (double) (q + q)) * d);
  114. b0 = tiny0 (nb0), a1 = tiny0 (na1), b2 = tiny0 (nb2);
  115. }
  116. float tick (float x) noexcept
  117. {
  118. x = tiny0 (x);
  119. auto y = ((x - x2) * b0 + y1 * a1) - b2 * y2;
  120. y = tiny0 (y);
  121. x2 = x1, y2 = y1, x1 = x, y1 = y;
  122. return y;
  123. }
  124. };
  125. // the soft clipper shared by the two distortions: 1 − 10^(−k·x)·(1 − x) above zero, 10^(k·x)·(x + 1) − 1 below
  126. float clip (float x, float k) noexcept
  127. {
  128. if (x >= 0.0f)
  129. return 1.0f - ildsp::powf_ (10.0f, -(k * x)) * (1.0f - x);
  130. return ildsp::powf_ (10.0f, k * x) * (x + 1.0f) - 1.0f;
  131. }
  132. // a tape line built for a size and start phase (CTapeDelay's constructor: time 0.5, gain 1, no feedback/speed/depth)
  133. void build (Tape& t, unsigned size, double phase)
  134. {
  135. const auto n = (int) std::max (4u, size);
  136. t.reserve (n);
  137. t.init (n, phase);
  138. }
  139. // ------------------------------------------------------------------------------------------------ pedals
  140. struct PedalFx
  141. {
  142. bool dirty = true; // cleared before the next block (constructor, box switched on)
  143. virtual ~PedalFx() = default;
  144. virtual void setRate (double fs) = 0;
  145. virtual void reset() noexcept = 0;
  146. virtual void setParam (int k, float v) noexcept = 0;
  147. virtual void process (float* L, float* R, int n) noexcept = 0;
  148. };
  149. // type 2: three modulated tape lines on the mono sum, added to the input (FUN_18002aff0 …)
  150. struct Chorus final : PedalFx
  151. {
  152. double fs = 0;
  153. float depth = 0.5f, rate = 0.5f, mix = 0.5f; // knobs 0, 1, 2
  154. Tape t[3];
  155. bool built = false;
  156. void setRate (double rate_) override
  157. {
  158. fs = rate_;
  159. const auto base = fs * 0.02;
  160. const auto b = (double) (int) base;
  161. build (t[0], u32 (b * 1.1000000000000001), 0.0);
  162. build (t[1], u32 (b * 0.85399999999999998), 0.46700000000000003);
  163. build (t[2], (unsigned) (int) base, 0.77800000000000002);
  164. built = true;
  165. update();
  166. reset();
  167. }
  168. void update() noexcept
  169. {
  170. if (! built)
  171. return;
  172. const auto rr = (double) (float) ((double) (rate * 5.0f) + 0.10000000000000001);
  173. t[0].setInc (rr * 0.89600000000000002, fs);
  174. t[1].setInc (rr, fs);
  175. t[2].setInc (rr * 0.68899999999999995, fs);
  176. const auto g = (double) -(mix * mix);
  177. for (auto& x : t)
  178. x.setGain (g);
  179. const auto d = (double) ((depth * depth) * depth);
  180. for (auto& x : t)
  181. x.setDepth (d);
  182. }
  183. void reset() noexcept override
  184. {
  185. if (built)
  186. for (auto& x : t)
  187. x.reset();
  188. }
  189. void setParam (int k, float v) noexcept override
  190. {
  191. if (k == 0)
  192. depth = v;
  193. else if (k == 1)
  194. rate = v;
  195. else if (k == 2)
  196. mix = v;
  197. update();
  198. }
  199. void process (float* L, float* R, int n) noexcept override
  200. {
  201. for (int i = 0; i < n; ++i)
  202. {
  203. const auto x = L[i] + R[i];
  204. const auto y0 = t[0].tick (x);
  205. const auto y2 = t[2].tick (x);
  206. const auto y1 = t[1].tick (x);
  207. L[i] = (y1 + y0) + L[i];
  208. R[i] = (y1 + y2) + R[i];
  209. }
  210. }
  211. };
  212. // type 9: per-channel gain computer (FUN_18002b430)
  213. struct Compressor final : PedalFx
  214. {
  215. float envL = 0, envR = 0;
  216. double fs = 44100.0;
  217. float attack = 0, release = 0, ratio = 0, thr = 0;
  218. void setRate (double rate) override
  219. {
  220. fs = rate;
  221. reset();
  222. }
  223. void reset() noexcept override { envL = envR = 0.0f; }
  224. void setParam (int k, float v) noexcept override
  225. {
  226. if (k == 0)
  227. thr = (float) std::pow (10.0, (double) ((1.0f - v) * -3.2f));
  228. else if (k == 1)
  229. ratio = v;
  230. else if (k == 2)
  231. attack = v;
  232. else if (k == 3)
  233. release = v;
  234. }
  235. void process (float* L, float* R, int n) noexcept override
  236. {
  237. const auto a2 = attack * attack;
  238. const auto slope = 1.0f / ((1.0f - ratio) * 30.0f + 1.0f);
  239. const auto att = 8.8199997771880589 / ((double) ((a2 + a2) + 0.1f) * fs);
  240. const auto rel = 8.8199997771880589 / ((double) ((release * release) * 4.8f + 0.2f) * fs);
  241. auto gainFor = [&] (float x) {
  242. const auto a = std::fabs (x);
  243. auto t = 1.0f;
  244. if (thr < a)
  245. t = thr / ((a - thr) * slope + thr);
  246. return t;
  247. };
  248. for (int i = 0; i < n; ++i)
  249. {
  250. const auto xl = L[i];
  251. auto t = gainFor (xl);
  252. envL = (float) (t < 1.0f ? att : rel) * (t - envL) + envL;
  253. const auto xr = R[i];
  254. t = gainFor (xr);
  255. envR = (float) (t < 1.0f ? att : rel) * (t - envR) + envR;
  256. L[i] = envL * xl;
  257. R[i] = envR * xr;
  258. }
  259. }
  260. };
  261. // types 6 and 3: one tape line per channel added to it (FUN_18002b730)
  262. struct TapePair : PedalFx
  263. {
  264. Tape t[2];
  265. double fs = 0;
  266. bool built = false;
  267. void reset() noexcept override
  268. {
  269. if (built)
  270. t[0].reset(), t[1].reset();
  271. }
  272. void process (float* L, float* R, int n) noexcept override
  273. {
  274. for (int i = 0; i < n; ++i)
  275. {
  276. const auto r = R[i];
  277. L[i] = t[0].tick (L[i]) + L[i];
  278. R[i] = t[1].tick (r) + R[i];
  279. }
  280. }
  281. };
  282. struct Delay final : TapePair
  283. {
  284. float gain = 0, time = 0, fb = 0;
  285. void setRate (double rate) override
  286. {
  287. fs = rate;
  288. for (int k = 0; k < 2; ++k)
  289. {
  290. build (t[k], u32 (fs), k == 0 ? 0.0 : 0.5);
  291. t[k].setTime ((double) time);
  292. t[k].setGain ((double) gain);
  293. t[k].setFb ((double) fb);
  294. t[k].setInc (1.0, fs);
  295. t[k].setDepth ((double) 0.01f);
  296. }
  297. built = true;
  298. reset();
  299. }
  300. void setParam (int k, float v) noexcept override
  301. {
  302. if (k == 0)
  303. {
  304. time = v < 0.1f ? 0.1f : (v > 1.0f ? 1.0f : v);
  305. if (built)
  306. t[0].setTime ((double) time), t[1].setTime ((double) time);
  307. }
  308. else if (k == 1)
  309. {
  310. fb = (float) ((double) v * -0.94999999999999996 * (double) v);
  311. if (built)
  312. t[0].setFb ((double) fb), t[1].setFb ((double) fb);
  313. }
  314. else if (k == 2)
  315. {
  316. gain = (float) -((double) ((v * 0.9f) * v) + 0.10000000000000001);
  317. if (built)
  318. t[0].setGain ((double) gain), t[1].setGain ((double) gain);
  319. }
  320. }
  321. };
  322. struct Flanger final : TapePair
  323. {
  324. float gain = 0, rate = 0, time = 0; // FL's ctor leaves gain/rate 0.5 and time 0.5 until the knobs arrive
  325. Flanger() { gain = rate = 0.5f; }
  326. void setRate (double fsIn) override
  327. {
  328. fs = fsIn;
  329. const auto n = u32 (fs * 0.0050000000000000001);
  330. for (int k = 0; k < 2; ++k)
  331. {
  332. build (t[k], n, k == 0 ? 0.0 : 0.5);
  333. t[k].setTime ((double) time);
  334. t[k].setGain ((double) gain);
  335. t[k].setInc ((double) rate, fs);
  336. t[k].setFb (-0.10000000000000001);
  337. t[k].setDepth (1.0);
  338. }
  339. built = true;
  340. reset();
  341. }
  342. void setParam (int k, float v) noexcept override
  343. {
  344. if (k == 0)
  345. {
  346. auto d = v * v;
  347. d = d < 0.01f ? 0.01f : (d > 1.0f ? 1.0f : d);
  348. time = d;
  349. if (built)
  350. t[0].setTime ((double) d), t[1].setTime ((double) time);
  351. }
  352. else if (k == 1)
  353. {
  354. rate = (v * v) * (v * 10.0f) + 0.01f;
  355. if (built)
  356. t[0].setInc ((double) rate, fs), t[1].setInc ((double) rate, fs);
  357. }
  358. else if (k == 2)
  359. {
  360. gain = -0.1f - v * 0.9f;
  361. if (built)
  362. t[0].setGain ((double) gain), t[1].setGain ((double) gain);
  363. }
  364. }
  365. };
  366. // type 1: two band-passed paths, 8× oversampled through cubic interpolation, soft clipped, two short combs, low-pass,
  367. // high-pass and a peaking tone filter (FUN_18002bc00)
  368. struct DistModelA final : PedalFx
  369. {
  370. float drive = 0.5f, comb = 0.5f, tone = 0.5f, body = 0.5f; // knobs 0, 1, 3 (EQ gain), 2 (high-pass Q)
  371. float fs = 0, pre = 1, k = 1;
  372. std::vector<float> buf1, buf2;
  373. int len1 = 1, idx1 = 0, len2 = 0, alloc2 = 1, idx2 = 0;
  374. float ha[3] {}, hb[3] {}; // interpolation histories (oldest first)
  375. BpBiquad bpA, bpB;
  376. LpBiquad lp;
  377. HpBiquad hp;
  378. EqBand eq;
  379. bool built = false;
  380. DistModelA() { eq.init (1000.0f, 1); }
  381. void setRate (double rate) override
  382. {
  383. fs = (float) rate;
  384. len1 = std::max (1, (int) ((double) (fs * 8.0f) * 0.001));
  385. buf1.assign ((std::size_t) len1, 0.0f);
  386. alloc2 = std::max (1, (int) ((double) (fs * 8.0f) * 0.00020000000000000001));
  387. buf2.assign ((std::size_t) alloc2, 0.0f);
  388. bpA.design (200.0f, 0.99f, fs);
  389. bpB.design (1000.0f, 0.99f, fs);
  390. eq.type = 1;
  391. eq.setBandwidth (3.0f);
  392. eq.setFreq (2500.0f);
  393. lp.design (500.0f, 4.0f, fs);
  394. built = true;
  395. reset();
  396. update();
  397. }
  398. void reset() noexcept override
  399. {
  400. bpA.clear(), bpB.clear();
  401. std::fill (buf1.begin(), buf1.end(), 0.0f);
  402. idx1 = 0;
  403. std::fill (buf2.begin(), buf2.end(), 0.0f);
  404. idx2 = 0;
  405. lp.clear(), hp.clear();
  406. for (int i = 0; i < 3; ++i)
  407. ha[i] = hb[i] = 0.0f;
  408. }
  409. void update() noexcept // FUN_18002c2f0
  410. {
  411. k = (drive * drive) * 99.0f + 1.0f;
  412. pre = 1.0f / (drive * 8.0f + 1.0f);
  413. hp.design (85.0f, body * 30.0f + 1.0f, fs);
  414. eq.setGain ((tone - 0.5f) * 20.0f);
  415. eq.design (fs);
  416. auto l = (int) ((float) alloc2 * comb);
  417. if (l >= alloc2)
  418. l = alloc2 - 1;
  419. len2 = l < 0 ? 0 : l;
  420. }
  421. void setParam (int kk, float v) noexcept override
  422. {
  423. if (kk == 0)
  424. drive = v;
  425. else if (kk == 1)
  426. comb = v;
  427. else if (kk == 2)
  428. body = v;
  429. else if (kk == 3)
  430. tone = v;
  431. if (built)
  432. update();
  433. }
  434. void process (float* L, float* R, int n) noexcept override
  435. {
  436. for (int i = 0; i < n; ++i)
  437. {
  438. const auto x = L[i] + R[i];
  439. const auto a = (float) ((double) bpA.tick (x) * 0.050000000000000003);
  440. const auto b = bpB.tick (x) * 5.0f;
  441. const auto y0 = ha[0], y1 = ha[1], y2 = ha[2], y3 = a;
  442. ha[0] = y1, ha[1] = y2, ha[2] = y3;
  443. const auto z0 = hb[0], z1 = hb[1], z2 = hb[2], z3 = b;
  444. hb[0] = z1, hb[1] = z2, hb[2] = z3;
  445. const auto c3 = (y3 - y0) * 0.5f + (y1 - y2) * 1.5f;
  446. const auto c1 = (y2 - y0) * 0.5f;
  447. const auto c2 = ((y0 - y1) + c1) - c3;
  448. const auto d1 = (z2 - z0) * 0.5f;
  449. const auto d3 = (z3 - z0) * 0.5f + (z1 - z2) * 1.5f;
  450. const auto d2 = ((z0 - z1) + d1) - d3;
  451. float t = 0.0f, y = 0.0f;
  452. for (int s = 0; s < 8; ++s)
  453. {
  454. auto va = ((t * c3 + c2) * t + c1) * t + y1;
  455. auto vb = ((t * d3 + d2) * t + d1) * t + z1;
  456. t = t + 0.125f;
  457. va = clip (va, k);
  458. vb = clip (vb, k);
  459. const auto old = buf1[(std::size_t) idx1];
  460. buf1[(std::size_t) idx1] = vb - old * 0.25f;
  461. if (++idx1 >= len1)
  462. idx1 = 0;
  463. auto m = (float) ((double) old * 0.69999999999999996 + (double) va);
  464. if (len2 != 0)
  465. {
  466. const auto o2 = buf2[(std::size_t) idx2];
  467. buf2[(std::size_t) idx2] = m;
  468. if (++idx2 >= len2)
  469. idx2 = 0;
  470. m = (float) ((double) m - (double) o2 * 0.29999999999999999);
  471. }
  472. y = lp.tick (m) * pre;
  473. }
  474. const auto out = eq.tick (hp.tick (y));
  475. L[i] = out;
  476. R[i] = out;
  477. }
  478. }
  479. };
  480. // type 11: shelf → low-pass → soft clip (fixed 8) → slow DC follower removed → shelf, mono (FUN_18002c520)
  481. struct TubeDrive final : PedalFx
  482. {
  483. LpBiquad lp;
  484. EqBand lo, hi;
  485. float hpState = 0, drive = 1.0f, knob1 = 0;
  486. double fs = 0;
  487. TubeDrive()
  488. {
  489. lo.init (1000.0f, 1);
  490. hi.init (1000.0f, 1);
  491. lo.type = 0, lo.setBandwidth (2.5f), lo.setFreq (200.0f), lo.dirty = true;
  492. hi.type = 2, hi.setBandwidth (2.0f), hi.setFreq (7000.0f), hi.dirty = true;
  493. }
  494. void setRate (double rate) override
  495. {
  496. fs = rate;
  497. lp.design (4000.0f, 0.5f, (float) rate);
  498. lo.design ((float) rate);
  499. hi.design ((float) rate);
  500. reset();
  501. }
  502. void reset() noexcept override
  503. {
  504. lp.clear();
  505. lo.clear(), hi.clear();
  506. hpState = 0.0f;
  507. }
  508. void setParam (int k, float v) noexcept override
  509. {
  510. if (k == 0)
  511. {
  512. lo.setGain ((1.0f - v) * -28.0f);
  513. lo.design ((float) fs);
  514. }
  515. else if (k == 1)
  516. {
  517. knob1 = v;
  518. hi.setGain ((1.0f - v) * -28.0f);
  519. hi.design ((float) fs);
  520. }
  521. else if (k == 2)
  522. drive = (float) ((double) ((v * v) * (v * 10.0f)) + 0.01);
  523. }
  524. void process (float* L, float* R, int n) noexcept override
  525. {
  526. const auto h = drive * 0.5f;
  527. const auto comp = 1.0f - ildsp::powf_ (10.0f, h * -8.0f) * (1.0f - h);
  528. const auto outGain = 0.5f / comp;
  529. for (int i = 0; i < n; ++i)
  530. {
  531. auto x = lo.tick ((R[i] + L[i]) * drive);
  532. x = lp.tick (x);
  533. x = clip (x, 8.0f);
  534. hpState = (float) ((double) (x - hpState) * 0.01 + (double) hpState);
  535. const auto y = hi.tick (x - hpState) * outGain;
  536. L[i] = y;
  537. R[i] = y;
  538. }
  539. }
  540. };
  541. // type 8: four bands per channel, low shelf 300 Hz, peaks 700 Hz and 1.8 kHz, high shelf 7 kHz (FUN_18002cbc0)
  542. struct Equalizer final : PedalFx
  543. {
  544. EqBand l[4], r[4];
  545. double fs = 0;
  546. Equalizer()
  547. {
  548. static constexpr float f[4] { 300.0f, 700.0f, 1800.0f, 7000.0f };
  549. static constexpr int t[4] { 0, 1, 1, 2 };
  550. for (int b = 0; b < 4; ++b)
  551. for (auto* e : { &l[b], &r[b] })
  552. {
  553. e->init (1000.0f, 1);
  554. if (e->type != t[b])
  555. e->dirty = true;
  556. e->type = t[b];
  557. e->setFreq (f[b]);
  558. e->setBandwidth (0.985f);
  559. }
  560. }
  561. void setRate (double rate) override
  562. {
  563. fs = rate;
  564. for (int b = 0; b < 4; ++b)
  565. l[b].design ((float) fs), r[b].design ((float) fs);
  566. reset();
  567. }
  568. void reset() noexcept override
  569. {
  570. for (int b = 0; b < 4; ++b)
  571. l[b].clear(), r[b].clear();
  572. }
  573. void setParam (int k, float v) noexcept override
  574. {
  575. if (k < 0 || k >= 4)
  576. return;
  577. const auto x = ((v - 0.5f) + v) - 0.5f;
  578. auto g = x * x;
  579. if (x < 0.0f)
  580. g = -g;
  581. g = g * 10.0f;
  582. l[k].setGain (g), r[k].setGain (g);
  583. l[k].design ((float) fs), r[k].design ((float) fs);
  584. }
  585. void process (float* L, float* R, int n) noexcept override
  586. {
  587. for (int b = 0; b < 4; ++b)
  588. for (int i = 0; i < n; ++i)
  589. L[i] = l[b].tick (L[i]), R[i] = r[b].tick (R[i]);
  590. }
  591. };
  592. // type 10: tape vibrato on the mono sum with amplitude and pan modulation by one LFO (FUN_18002d270)
  593. struct Modulator final : PedalFx
  594. {
  595. double fs = 0, ph = 0, inc = 0;
  596. float k0 = 0.5f, amDepth = 0.5f, panDepth = 0.5f, k3 = 0.5f, am = 0, pan = 0;
  597. Tape t;
  598. bool built = false;
  599. void setRate (double rate) override
  600. {
  601. fs = rate;
  602. build (t, u32 (fs * 0.01), 0.0);
  603. built = true;
  604. update();
  605. reset();
  606. }
  607. void update() noexcept // FUN_18002d580
  608. {
  609. const auto r = (k3 * k3) * 30.0f;
  610. if (built)
  611. {
  612. auto d = k0 * k0;
  613. if (d <= 0.01f)
  614. d = 0.01f;
  615. if (1.0f <= d)
  616. d = 1.0f;
  617. t.setTime ((double) d);
  618. t.setGain (1.0);
  619. t.setFb (0.0);
  620. t.setInc ((double) r, fs);
  621. t.setDepth (1.0);
  622. }
  623. inc = (double) r / fs;
  624. if (inc < 0.0)
  625. inc = 0.0;
  626. }
  627. void reset() noexcept override
  628. {
  629. if (built)
  630. t.reset();
  631. ph = 0.0, inc = 0.0;
  632. am = pan = 0.0f;
  633. update();
  634. }
  635. void setParam (int k, float v) noexcept override
  636. {
  637. if (k == 0)
  638. k0 = v;
  639. else if (k == 1)
  640. amDepth = v;
  641. else if (k == 2)
  642. panDepth = v;
  643. else if (k == 3)
  644. k3 = v;
  645. else
  646. return;
  647. update();
  648. }
  649. void process (float* L, float* R, int n) noexcept override
  650. {
  651. for (int i = 0; i < n; ++i)
  652. {
  653. const auto lfo = (float) std::cos (ph * 6.2831853071795862);
  654. ph = ph + inc;
  655. if (ph >= 1.0)
  656. ph = ph - 1.0;
  657. const auto y = t.tick (L[i] + R[i]);
  658. const auto a = am, p = pan;
  659. const auto v = (1.0f - (a * lfo) * (lfo * lfo)) * y;
  660. const auto pl = p * lfo;
  661. am = (amDepth - a) * 0.001f + a;
  662. pan = (panDepth - p) * 0.001f + p;
  663. if (am < tiny9 && am > tinyNeg)
  664. am = 0.0f;
  665. if (pan < tiny9 && pan > tinyNeg)
  666. pan = 0.0f;
  667. L[i] = (1.0f - pl) * v;
  668. R[i] = (pl + 1.0f) * v;
  669. }
  670. }
  671. };
  672. // type 7: per-channel gate with hold (FUN_18002d6a0)
  673. struct NoiseGate final : PedalFx
  674. {
  675. unsigned cntL = 0, cntR = 0;
  676. float gL = 0, gR = 0, attack = 0, release = 0, thr = 0;
  677. double fs = 44100.0;
  678. void setRate (double rate) override
  679. {
  680. fs = rate;
  681. reset();
  682. }
  683. void reset() noexcept override
  684. {
  685. cntL = cntR = 0;
  686. gL = gR = 0.0f;
  687. }
  688. void setParam (int k, float v) noexcept override
  689. {
  690. if (k == 0)
  691. attack = v;
  692. else if (k == 1)
  693. release = v;
  694. else if (k == 2)
  695. thr = (float) std::pow (10.0, (double) ((1.0f - v) * -3.2f));
  696. }
  697. void process (float* L, float* R, int n) noexcept override
  698. {
  699. const auto hold = u32 (fs * 0.10000000000000001);
  700. const auto a2 = attack * attack, r2 = release * release;
  701. const auto att = (float) (8.8200000000000003 / ((double) (float) ((double) (a2 + a2) + 0.050000000000000003) * fs));
  702. const auto rel = (float) (8.8200000000000003 / ((double) (float) ((double) (r2 + r2) + 0.050000000000000003) * fs));
  703. auto step = [&] (float x, unsigned& cnt, float& g) {
  704. float d = 0;
  705. if (std::fabs (x) <= thr)
  706. {
  707. ++cnt;
  708. d = cnt <= hold ? (1.0f - g) * att : -(g * rel);
  709. }
  710. else
  711. {
  712. cnt = 0;
  713. d = (1.0f - g) * att;
  714. }
  715. g = g + d;
  716. if (g < 1e-7f)
  717. g = 1e-7f;
  718. };
  719. for (int i = 0; i < n; ++i)
  720. {
  721. step (L[i], cntL, gL);
  722. step (R[i], cntR, gR);
  723. L[i] = gL * L[i];
  724. R[i] = gR * R[i];
  725. }
  726. }
  727. };
  728. // type 4: six first-order all-passes per channel swept by a cosine LFO, with feedback (FUN_18002d970)
  729. struct Phaser final : PedalFx
  730. {
  731. float rate = 0, mixK = 0, freqK = 0, depthK = 0; // knobs 0, 3, 1, 2
  732. double fs = 44100.0;
  733. float sL[6] {}, sR[6] {}, fbL = 0, fbR = 0, ph = 0, sfrq = -1.0f, smix = -1.0f;
  734. void setRate (double r) override
  735. {
  736. fs = r;
  737. reset();
  738. }
  739. void reset() noexcept override
  740. {
  741. for (int k = 0; k < 6; ++k)
  742. sL[k] = sR[k] = 0.0f;
  743. fbL = fbR = 0.0f;
  744. ph = 0.0f; // the plug-in clears the LFO phase with the right channel's feedback (one 8-byte store)
  745. sfrq = smix = -1.0f;
  746. }
  747. void setParam (int k, float v) noexcept override
  748. {
  749. if (k == 0)
  750. rate = v;
  751. else if (k == 1)
  752. freqK = v;
  753. else if (k == 2)
  754. depthK = v;
  755. else if (k == 3)
  756. mixK = v;
  757. }
  758. void process (float* L, float* R, int n) noexcept override
  759. {
  760. const auto inv = 1.0 / fs;
  761. const auto lo = (float) (inv * 50.0), hi = (float) (inv * 20000.0);
  762. const auto K = (depthK * depthK) * (depthK * 0.5f);
  763. const auto frq = std::max (freqK * freqK, 9.99999975e-06f);
  764. const auto inc = (float) ((double) ((rate * 88200.0f) * rate) * inv) * 0.0005f;
  765. const auto mix = std::max (mixK, 9.99999975e-06f);
  766. if (0.0f > smix)
  767. smix = mix;
  768. if (0.0f > sfrq)
  769. sfrq = frq;
  770. const auto rng = hi - lo;
  771. for (int i = 0; i < n; ++i)
  772. {
  773. auto xl = L[i] - 9.99999997e-07f, xr = R[i] - 9.99999997e-07f;
  774. const auto c = ildsp::cosf_ (ph);
  775. auto nph = inc + ph;
  776. if (nph > 6.28318548f)
  777. nph = nph - 6.28318548f;
  778. ph = nph;
  779. const auto mm = (c + 1.0f) * K;
  780. auto wv = (sfrq + mm) * rng + lo;
  781. wv = wv < 0.499000013f ? wv : 0.499000013f;
  782. sfrq = (frq - sfrq) * 0.001f + sfrq;
  783. xl = xl + fbL * 0.454875678f;
  784. xr = xr + fbR * 0.454875678f;
  785. const auto a = (1.0f - wv) / (wv + 1.0f);
  786. for (int k = 0; k < 6; ++k)
  787. {
  788. const auto tl = sL[k] - a * xl;
  789. sL[k] = tl * a + xl;
  790. xl = tl;
  791. const auto tr = sR[k] - a * xr;
  792. sR[k] = tr * a + xr;
  793. xr = tr;
  794. }
  795. fbL = xl, fbR = xr;
  796. const auto sm = smix;
  797. smix = (mix - sm) * 0.001f + sm;
  798. L[i] = sm * xl + L[i];
  799. R[i] = sm * xr + R[i];
  800. }
  801. }
  802. };
  803. // type 5: the plate reverb of Toxic Biohazard's rack (class fxReverb) driven by three knobs (FUN_18002dfc0 …)
  804. struct ReverbPedal final : PedalFx
  805. {
  806. ReverbT<HcCrt> rev;
  807. ReverbPedal()
  808. {
  809. rev.allTaps = true;
  810. rev.construct();
  811. }
  812. void setRate (double fs) override
  813. {
  814. rev.setRate ((float) fs);
  815. reset();
  816. }
  817. void reset() noexcept override
  818. {
  819. rev.reset();
  820. rev.setp (2, 1.0f);
  821. rev.setp (5, 1.0f);
  822. }
  823. void setParam (int k, float v) noexcept override
  824. {
  825. if (k == 0)
  826. rev.setp (8, (float) ((double) ((v * v) * v) * 0.59999999999999998));
  827. else if (k == 1)
  828. rev.setp (9, std::fabs (1.0f - v));
  829. else if (k == 2)
  830. rev.setp (12, (float) ((double) (v * 0.999000013f) + 0.001));
  831. }
  832. void process (float* L, float* R, int n) noexcept override { rev.process (L, R, n); }
  833. };
  834. std::unique_ptr<PedalFx> makePedal (int type)
  835. {
  836. switch (type)
  837. {
  838. case distModelA: return std::make_unique<DistModelA>();
  839. case chorus: return std::make_unique<Chorus>();
  840. case flanger: return std::make_unique<Flanger>();
  841. case phaser: return std::make_unique<Phaser>();
  842. case reverb: return std::make_unique<ReverbPedal>();
  843. case delay: return std::make_unique<Delay>();
  844. case noiseGate: return std::make_unique<NoiseGate>();
  845. case equalizer: return std::make_unique<Equalizer>();
  846. case compressor: return std::make_unique<Compressor>();
  847. case modulator: return std::make_unique<Modulator>();
  848. case tubeDrive: return std::make_unique<TubeDrive>();
  849. default: return nullptr;
  850. }
  851. }
  852. // ------------------------------------------------------------------------------------------------ cabinet
  853. // acc[j] += x·h[j] (a product then a sum, as the plug-in's SSE loop: any vector width gives the same floats)
  854. bool cpuHasAvx()
  855. {
  856. static const bool f = [] {
  857. unsigned a = 0, b = 0, c = 0, d = 0;
  858. if (! __get_cpuid (1, &a, &b, &c, &d))
  859. return false;
  860. if ((c & (1u << 27)) == 0 || (c & (1u << 28)) == 0)
  861. return false;
  862. unsigned lo = 0, hi = 0;
  863. asm volatile ("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
  864. return (lo & 6u) == 6u;
  865. }();
  866. return f;
  867. }
  868. __attribute__ ((target ("avx"))) void macAvx (float* acc, const float* h, float x, std::size_t n) noexcept
  869. {
  870. const auto vx = _mm256_set1_ps (x);
  871. std::size_t j = 0;
  872. for (; j + 8 <= n; j += 8)
  873. _mm256_storeu_ps (acc + j, _mm256_add_ps (_mm256_mul_ps (vx, _mm256_loadu_ps (h + j)), _mm256_loadu_ps (acc + j)));
  874. for (; j < n; ++j)
  875. acc[j] = x * h[j] + acc[j];
  876. }
  877. void mac (float* acc, const float* h, float x, std::size_t n) noexcept
  878. {
  879. if (cpuHasAvx())
  880. {
  881. macAvx (acc, h, x, n);
  882. return;
  883. }
  884. for (std::size_t j = 0; j < n; ++j)
  885. acc[j] = x * h[j] + acc[j];
  886. }
  887. // impulse resampled to the rate by linear interpolation, ×0.2, run as a direct convolution whose sums add the oldest
  888. // input first (FUN_180026f00 / FUN_180027260)
  889. // the plug-in's RIFF/WAVE reader (FUN_18001c890): integer PCM 16/24/32 bits or float32, planar; its integer scales are
  890. // 2^-15, 1.1920930376163597e-07 (one ulp above 2^-23) and 2^-31. nullopt = not readable (other formats not decoded)
  891. struct WavData
  892. {
  893. std::vector<float> ch[2];
  894. int channels = 0;
  895. float rate = 44100.0f;
  896. };
  897. std::optional<WavData> readWav (const std::vector<std::uint8_t>& b)
  898. {
  899. auto u32at = [&] (std::size_t o) { std::uint32_t x = 0; std::memcpy (&x, b.data() + o, 4); return x; };
  900. auto u16at = [&] (std::size_t o) { std::uint16_t x = 0; std::memcpy (&x, b.data() + o, 2); return x; };
  901. if (b.size() < 12 || std::memcmp (b.data(), "RIFF", 4) != 0 || std::memcmp (b.data() + 8, "WAVE", 4) != 0)
  902. return std::nullopt;
  903. int format = 0, channels = 0, bits = 0, align = 0;
  904. std::uint32_t rate = 0;
  905. std::size_t o = 12;
  906. while (o + 8 <= b.size())
  907. {
  908. const auto size = (std::size_t) u32at (o + 4);
  909. const auto body = o + 8;
  910. if (body + size > b.size())
  911. break;
  912. if (std::memcmp (b.data() + o, "fmt ", 4) == 0 && size >= 16)
  913. {
  914. format = u16at (body), channels = u16at (body + 2), rate = u32at (body + 4), align = u16at (body + 12), bits = u16at (body + 14);
  915. if ((format != 1 && format != 3) || channels < 1 || channels > 2 || align != bits * channels / 8)
  916. return std::nullopt;
  917. }
  918. else if (std::memcmp (b.data() + o, "data", 4) == 0 && align > 0)
  919. {
  920. const auto frames = size / (std::size_t) align;
  921. WavData w;
  922. w.channels = channels;
  923. w.rate = (float) rate;
  924. for (int c = 0; c < channels; ++c)
  925. {
  926. auto& v = w.ch[c];
  927. v.resize (frames);
  928. for (std::size_t i = 0; i < frames; ++i)
  929. {
  930. const auto* p = b.data() + body + i * (std::size_t) align + (std::size_t) (c * bits / 8);
  931. if (format == 3 && bits == 32)
  932. std::memcpy (&v[i], p, 4);
  933. else if (format == 1 && bits == 16)
  934. v[i] = (float) (std::int16_t) (p[0] | (p[1] << 8)) * 3.0517578125e-05f;
  935. else if (format == 1 && bits == 24)
  936. {
  937. auto x = (std::int32_t) (p[0] | (p[1] << 8) | (p[2] << 16));
  938. if (x & 0x800000)
  939. x |= (std::int32_t) 0xff000000u;
  940. v[i] = (float) x * 1.1920930376163597e-07f;
  941. }
  942. else if (format == 1 && bits == 32)
  943. {
  944. std::int32_t x = 0;
  945. std::memcpy (&x, p, 4);
  946. v[i] = (float) x * 4.656612873077393e-10f;
  947. }
  948. else
  949. return std::nullopt;
  950. }
  951. }
  952. return w;
  953. }
  954. o = body + size + (size & 1);
  955. }
  956. return std::nullopt;
  957. }
  958. struct Cabinet
  959. {
  960. std::vector<float> hl, hr; // taps (a multiple of 4, zero padded)
  961. std::vector<float> accL, accR; // shifting accumulators, 2·taps each
  962. int taps = 0, pos = 0;
  963. std::vector<std::uint8_t> file; // the loaded file (reloaded on a rate change)
  964. double fs = 44100.0;
  965. bool load (const std::vector<std::uint8_t>& wav)
  966. {
  967. const auto a = readWav (wav);
  968. if (! a || a->ch[0].empty())
  969. return false;
  970. file = wav;
  971. const auto fileRate = (double) a->rate;
  972. const auto scale = (float) (fileRate / fs);
  973. std::vector<float> l = a->ch[0], r = a->channels > 1 ? a->ch[1] : std::vector<float> {};
  974. for (auto& v : l)
  975. v = scale * v;
  976. for (auto& v : r)
  977. v = scale * v;
  978. const auto frames = (unsigned) l.size();
  979. const auto count = (int) ((double) frames * (fs / fileRate));
  980. const auto n4 = (count >> 2) + 1;
  981. taps = n4 * 4;
  982. hl.assign ((std::size_t) taps, 0.0f);
  983. hr.assign ((std::size_t) taps, 0.0f);
  984. auto at = [&] (const std::vector<float>& ch, unsigned idx, float f) {
  985. if (idx >= frames)
  986. return 0.0f;
  987. const auto b = idx + 1 < ch.size() ? ch[idx + 1] : 0.0f;
  988. return (1.0f - f) * ch[idx] + f * b;
  989. };
  990. if (count > 0)
  991. {
  992. const auto inv = 1.0 / (double) count;
  993. for (int i = 0; i < count; ++i)
  994. {
  995. const auto p = (double) i * inv * (double) (frames - 1);
  996. const auto idx = (unsigned) (long long) p;
  997. const auto f = (float) (p - (double) (int) idx);
  998. auto v = at (l, idx, f) * 0.2f;
  999. hl[(std::size_t) i] = v;
  1000. if (! r.empty())
  1001. v = at (r, idx, f) * 0.2f;
  1002. hr[(std::size_t) i] = v;
  1003. }
  1004. }
  1005. accL.assign ((std::size_t) taps * 2, 0.0f);
  1006. accR.assign ((std::size_t) taps * 2, 0.0f);
  1007. pos = 0;
  1008. return true;
  1009. }
  1010. void unload()
  1011. {
  1012. file.clear(), hl.clear(), hr.clear(), accL.clear(), accR.clear();
  1013. taps = pos = 0;
  1014. }
  1015. void setRate (double rate)
  1016. {
  1017. if (rate != 0.0)
  1018. fs = rate;
  1019. if (! file.empty())
  1020. {
  1021. const auto f = file;
  1022. load (f);
  1023. }
  1024. std::fill (accL.begin(), accL.end(), 0.0f);
  1025. std::fill (accR.begin(), accR.end(), 0.0f);
  1026. pos = 0;
  1027. }
  1028. void process (float* L, float* R, int n) noexcept
  1029. {
  1030. if (taps == 0)
  1031. return;
  1032. const auto T = (std::size_t) taps;
  1033. for (int i = 0; i < n; ++i)
  1034. {
  1035. const auto xl = L[i], xr = R[i];
  1036. float* al = accL.data() + pos;
  1037. float* ar = accR.data() + pos;
  1038. const float* h0 = hl.data();
  1039. const float* h1 = hr.data();
  1040. mac (al, h0, xl, T);
  1041. mac (ar, h1, xr, T);
  1042. L[i] = al[0];
  1043. R[i] = ar[0];
  1044. ++pos;
  1045. if ((std::size_t) pos == T)
  1046. {
  1047. std::memmove (accL.data(), accL.data() + T, T * sizeof (float));
  1048. std::memmove (accR.data(), accR.data() + T, T * sizeof (float));
  1049. std::fill (accL.begin() + (std::ptrdiff_t) T, accL.end(), 0.0f);
  1050. std::fill (accR.begin() + (std::ptrdiff_t) T, accR.end(), 0.0f);
  1051. pos = 0;
  1052. }
  1053. accL[(std::size_t) pos + T - 1] = 0.0f;
  1054. accR[(std::size_t) pos + T - 1] = 0.0f;
  1055. }
  1056. }
  1057. };
  1058. } // namespace
  1059. // ------------------------------------------------------------------------------------------------ engine
  1060. struct Engine::Impl
  1061. {
  1062. std::array<float, numParams> v {};
  1063. std::unique_ptr<PedalFx> box[numBoxes];
  1064. std::array<std::vector<std::uint8_t>, numCabinets + 1> cabFiles;
  1065. Cabinet cab;
  1066. HpBiquad dcL, dcR;
  1067. EqBand eqL[8], eqR[8];
  1068. bool eqOn = false, eqClear = true;
  1069. float inT = 1.0f, inG = 1.0f, outT = 1.0f, outG = 1.0f;
  1070. double fs = 44100.0;
  1071. Impl()
  1072. {
  1073. for (int b = 0; b < 8; ++b)
  1074. for (auto* e : { &eqL[b], &eqR[b] })
  1075. {
  1076. e->init (1000.0f, 1);
  1077. const auto t = b == 0 ? 0 : (b == 7 ? 2 : 1);
  1078. if (t != e->type)
  1079. e->dirty = true;
  1080. e->type = t;
  1081. auto f = (float) (std::pow (2.0, (double) b) * 125.0);
  1082. if (f <= 0.0f)
  1083. f = 0.0f;
  1084. e->setFreq (f);
  1085. e->setBandwidth (1.0f);
  1086. }
  1087. const auto d = defaultValues();
  1088. for (int i = 0; i < numParams; ++i)
  1089. set (i, d[(std::size_t) i]);
  1090. eqClear = true;
  1091. }
  1092. void set (int i, float x)
  1093. {
  1094. if (i < 0 || i >= numParams)
  1095. return;
  1096. v[(std::size_t) i] = x;
  1097. if (i >= pBoxType && i < pBoxType + numBoxes)
  1098. {
  1099. const auto b = i - pBoxType;
  1100. auto t = (int) (float) (int) x;
  1101. t = t < 0 ? -1 : (t > 11 ? 11 : t);
  1102. box[b] = makePedal (t);
  1103. if (box[b])
  1104. {
  1105. box[b]->setRate (fs);
  1106. box[b]->reset();
  1107. for (int k = 0; k < paramsPerBox; ++k)
  1108. box[b]->setParam (k, v[(std::size_t) (pBoxParam + b * paramsPerBox + k)]);
  1109. }
  1110. }
  1111. else if (i >= pBoxParam && i < pBoxParam + numBoxes * paramsPerBox)
  1112. {
  1113. const auto b = (i - pBoxParam) / paramsPerBox;
  1114. if (box[b])
  1115. box[b]->setParam ((i - pBoxParam) % paramsPerBox, x);
  1116. }
  1117. else if (i == pCabinet)
  1118. {
  1119. const auto c = (int) x;
  1120. if (c > 0 && c <= numCabinets && ! cabFiles[(std::size_t) c].empty())
  1121. {
  1122. if (! cab.load (cabFiles[(std::size_t) c]))
  1123. cab.unload();
  1124. }
  1125. else
  1126. cab.unload();
  1127. }
  1128. else if (i >= pEqBand && i < pEqBand + 8)
  1129. {
  1130. const auto b = i - pEqBand;
  1131. const auto y = ((x - 0.5f) + x) - 0.5f;
  1132. auto g = y * y;
  1133. if (y < 0.0f)
  1134. g = -g;
  1135. const auto gain = (float) ((double) g * 18.0);
  1136. eqL[b].setGain (gain), eqR[b].setGain (gain);
  1137. eqL[b].design ((float) fs), eqR[b].design ((float) fs);
  1138. }
  1139. else if (i == pEqOn)
  1140. {
  1141. eqOn = x != 0.0f;
  1142. eqClear = true;
  1143. }
  1144. else if (i == pInLevel)
  1145. inT = ildsp::powf_ (10.0f, x * 2.4f - 1.2f);
  1146. else if (i == pOutLevel)
  1147. outT = ildsp::powf_ (10.0f, x * 2.4f - 1.2f);
  1148. else if (i >= pBoxOn && i < pBoxOn + numBoxes)
  1149. {
  1150. if (box[i - pBoxOn])
  1151. box[i - pBoxOn]->dirty = true;
  1152. }
  1153. }
  1154. void prepare (double rate)
  1155. {
  1156. if (rate > 0.0)
  1157. fs = rate;
  1158. for (auto& p : box)
  1159. if (p)
  1160. {
  1161. if (rate != 0.0)
  1162. p->setRate (rate);
  1163. p->reset();
  1164. p->dirty = false;
  1165. }
  1166. cab.setRate (rate);
  1167. inG = -1.0f, outG = -1.0f;
  1168. dcL.design (30.0f, 1.0f, (float) fs);
  1169. dcR.design (30.0f, 1.0f, (float) fs);
  1170. dcL.clear(), dcR.clear();
  1171. for (int b = 0; b < 8; ++b)
  1172. {
  1173. eqL[b].clear(), eqL[b].design ((float) fs);
  1174. eqR[b].clear(), eqR[b].design ((float) fs);
  1175. }
  1176. }
  1177. void process (float* L, float* R, int n) noexcept
  1178. {
  1179. if (eqClear)
  1180. {
  1181. eqClear = false;
  1182. for (int b = 0; b < 8; ++b)
  1183. eqL[b].clear(), eqR[b].clear();
  1184. }
  1185. if (inG <= 0.0f && inG != 0.0f)
  1186. inG = inT;
  1187. if (outG <= 0.0f && outG != 0.0f)
  1188. outG = outT;
  1189. for (int i = 0; i < n; ++i)
  1190. {
  1191. const auto g = inG;
  1192. const auto l = dcL.tick (L[i]) * g;
  1193. const auto r = dcR.tick (R[i]) * g;
  1194. inG = (inT - g) * 0.00100000005f + g;
  1195. L[i] = l, R[i] = r;
  1196. }
  1197. for (int b = 0; b < numBoxes; ++b)
  1198. if (auto* p = box[b].get())
  1199. {
  1200. if (p->dirty)
  1201. p->reset();
  1202. p->dirty = false;
  1203. if (v[(std::size_t) (pBoxOn + b)] != 0.0f)
  1204. p->process (L, R, n);
  1205. }
  1206. cab.process (L, R, n);
  1207. if (eqOn)
  1208. for (int b = 0; b < 8; ++b)
  1209. for (int i = 0; i < n; ++i)
  1210. L[i] = eqL[b].tick (L[i]), R[i] = eqR[b].tick (R[i]);
  1211. for (int i = 0; i < n; ++i)
  1212. {
  1213. const auto g = outG;
  1214. const auto r = R[i], l = L[i];
  1215. outG = (outT - g) * 0.00100000005f + g;
  1216. L[i] = g * l;
  1217. R[i] = g * r;
  1218. }
  1219. }
  1220. };
  1221. Engine::Engine() : impl (std::make_unique<Impl>()) {}
  1222. Engine::~Engine() = default;
  1223. void Engine::setCabinetFile (int cabinet, std::vector<std::uint8_t> wav)
  1224. {
  1225. if (cabinet >= 1 && cabinet <= numCabinets)
  1226. impl->cabFiles[(std::size_t) cabinet] = std::move (wav);
  1227. }
  1228. void Engine::set (int param, float raw) { impl->set (param, raw); }
  1229. float Engine::get (int param) const noexcept { return param >= 0 && param < numParams ? impl->v[(std::size_t) param] : 0.0f; }
  1230. void Engine::prepare (double sampleRate) { impl->prepare (sampleRate); }
  1231. void Engine::process (float* left, float* right, int n) noexcept
  1232. {
  1233. if (n > 0)
  1234. impl->process (left, right, n);
  1235. }
  1236. // ------------------------------------------------------------------------------------------------ state, parameters
  1237. std::array<float, numParams> defaultValues() noexcept
  1238. {
  1239. std::array<float, numParams> d {};
  1240. for (int i = 0; i < numParams; ++i)
  1241. d[(std::size_t) i] = (i >= pBoxOn && i < pBoxOn + numBoxes) ? 1.0f : ((i == pEqOn || i >= pBoxType) ? 0.0f : 0.5f);
  1242. return d;
  1243. }
  1244. std::optional<State> parse (const std::vector<std::uint8_t>& d)
  1245. {
  1246. std::size_t o = 0;
  1247. if (d.size() >= 8 && std::memcmp (d.data(), "SSLF", 4) == 0)
  1248. o = 8;
  1249. if (d.size() < o + 4 + 4 * numParams)
  1250. return std::nullopt;
  1251. if (std::memcmp (d.data() + o, "1RCH", 4) != 0 && std::memcmp (d.data() + o, "DRCH", 4) != 0)
  1252. return std::nullopt;
  1253. State s;
  1254. std::memcpy (s.values.data(), d.data() + o + 4, 4 * numParams);
  1255. for (auto& x : s.values)
  1256. if (! std::isfinite (x))
  1257. x = 0.0f;
  1258. const auto nameAt = o + 4 + 4 * numParams;
  1259. for (std::size_t i = nameAt; i < d.size() && i < nameAt + 256 && d[i] != 0; ++i)
  1260. s.presetName += (char) d[i];
  1261. return s;
  1262. }
  1263. float toSlot (int param, float raw) noexcept
  1264. {
  1265. if (param == pCabinet)
  1266. return std::clamp (raw / (float) numCabinets, 0.0f, 1.0f);
  1267. if (param >= pBoxType && param < pBoxType + numBoxes)
  1268. return std::clamp (raw / (float) numPedalTypes, 0.0f, 1.0f);
  1269. return std::clamp (raw, 0.0f, 1.0f);
  1270. }
  1271. float fromSlot (int param, float v01) noexcept
  1272. {
  1273. if (param == pCabinet)
  1274. return (float) (int) std::lround (std::clamp (v01, 0.0f, 1.0f) * (float) numCabinets);
  1275. if (param >= pBoxType && param < pBoxType + numBoxes)
  1276. return (float) (int) std::lround (std::clamp (v01, 0.0f, 1.0f) * (float) numPedalTypes);
  1277. return v01;
  1278. }
  1279. void toParams (const State& s, EffectParams& params) noexcept
  1280. {
  1281. for (int i = 0; i < numParams; ++i)
  1282. params[(std::size_t) i] = toSlot (i, s.values[(std::size_t) i]);
  1283. }
  1284. const char* pedalName (int type) noexcept
  1285. {
  1286. static const char* names[] = { "Empty", "Distortion", "Chorus", "Flanger", "Phaser", "Reverb", "Delay",
  1287. "Noise Gate", "Equalizer", "Compressor", "Modulator", "Tubedrive" };
  1288. return type >= 0 && type <= 11 ? names[type] : "Empty";
  1289. }
  1290. std::string paramName (int i)
  1291. {
  1292. char b[48];
  1293. if (i == pInLevel)
  1294. return "Input level";
  1295. if (i == pOutLevel)
  1296. return "Output level";
  1297. if (i == pEqOn)
  1298. return "EQ on";
  1299. if (i >= pEqBand && i < pEqBand + 8)
  1300. {
  1301. std::snprintf (b, sizeof (b), "EQ band %d", i - pEqBand + 1);
  1302. return b;
  1303. }
  1304. if (i == pCabinet)
  1305. return "Cabinet";
  1306. if (i >= pBoxParam && i < pBoxParam + numBoxes * paramsPerBox)
  1307. {
  1308. std::snprintf (b, sizeof (b), "Box %d knob %d", (i - pBoxParam) / paramsPerBox + 1, (i - pBoxParam) % paramsPerBox + 1);
  1309. return b;
  1310. }
  1311. if (i >= pBoxOn && i < pBoxOn + numBoxes)
  1312. {
  1313. std::snprintf (b, sizeof (b), "Box %d on", i - pBoxOn + 1);
  1314. return b;
  1315. }
  1316. if (i >= pBoxType && i < pBoxType + numBoxes)
  1317. {
  1318. std::snprintf (b, sizeof (b), "Box %d pedal", i - pBoxType + 1);
  1319. return b;
  1320. }
  1321. std::snprintf (b, sizeof (b), "(unused %d)", i); // FL's parameters 12, 38 and 44 do nothing
  1322. return b;
  1323. }
  1324. std::string paramText (int i, float v01)
  1325. {
  1326. char b[48];
  1327. const auto raw = fromSlot (i, v01);
  1328. if (i == pInLevel || i == pOutLevel)
  1329. std::snprintf (b, sizeof (b), "%+.1f dB", 48.0 * (double) raw - 24.0);
  1330. else if (i >= pEqBand && i < pEqBand + 8)
  1331. {
  1332. const auto y = ((double) raw - 0.5) * 2.0;
  1333. std::snprintf (b, sizeof (b), "%+.1f dB", (y < 0 ? -y * y : y * y) * 18.0);
  1334. }
  1335. else if (i == pEqOn || (i >= pBoxOn && i < pBoxOn + numBoxes))
  1336. return raw != 0.0f ? "On" : "Off";
  1337. else if (i == pCabinet)
  1338. {
  1339. const auto c = (int) raw;
  1340. if (c <= 0)
  1341. return "None";
  1342. std::snprintf (b, sizeof (b), "Model %c", (char) ('A' + c - 1));
  1343. }
  1344. else if (i >= pBoxType && i < pBoxType + numBoxes)
  1345. return pedalName ((int) raw);
  1346. else
  1347. std::snprintf (b, sizeof (b), "%.0f%%", (double) raw * 100.0);
  1348. return b;
  1349. }
  1350. std::string cabinetPath (int cabinet)
  1351. {
  1352. if (cabinet < 1 || cabinet > numCabinets)
  1353. return {};
  1354. // the installation's file names (two of them carry a space before the extension)
  1355. std::string name = "Model ";
  1356. name += (char) ('A' + cabinet - 1);
  1357. if (cabinet == 9 || cabinet == 10)
  1358. name += ' ';
  1359. return "Plugins\\Fruity\\Effects\\Hardcore\\Imp\\" + name + ".wav";
  1360. }
  1361. // ------------------------------------------------------------------------------------------------ LYNX effect
  1362. namespace
  1363. {
  1364. // EffectSlot::state of a Hardcore slot: "LXHC", u32 count, then per cabinet file: u32 index, u32 size, bytes
  1365. std::vector<std::pair<int, std::vector<std::uint8_t>>> unpackFiles (const std::vector<std::uint8_t>& s)
  1366. {
  1367. std::vector<std::pair<int, std::vector<std::uint8_t>>> out;
  1368. auto u = [&] (std::size_t at) {
  1369. std::uint32_t x = 0;
  1370. std::memcpy (&x, s.data() + at, 4);
  1371. return x;
  1372. };
  1373. if (s.size() < 8 || std::memcmp (s.data(), "LXHC", 4) != 0)
  1374. return out;
  1375. const auto count = u (4);
  1376. std::size_t o = 8;
  1377. for (std::uint32_t k = 0; k < count && o + 8 <= s.size(); ++k)
  1378. {
  1379. const int idx = (int) u (o);
  1380. const std::uint32_t size = u (o + 4);
  1381. o += 8;
  1382. if (o + size > s.size())
  1383. break;
  1384. out.emplace_back (idx, std::vector<std::uint8_t> (s.begin() + (std::ptrdiff_t) o, s.begin() + (std::ptrdiff_t) (o + size)));
  1385. o += size;
  1386. }
  1387. return out;
  1388. }
  1389. class HardcoreEffect final : public Effect
  1390. {
  1391. public:
  1392. explicit HardcoreEffect (const std::vector<std::uint8_t>& state)
  1393. {
  1394. for (auto& [idx, bytes] : unpackFiles (state))
  1395. engine.setCabinetFile (idx, std::move (bytes));
  1396. }
  1397. void prepare (double sampleRate) noexcept override
  1398. {
  1399. rate = sampleRate;
  1400. primed = false;
  1401. }
  1402. void reset() noexcept override { primed = false; }
  1403. void process (float* left, float* right, int n, const EffectParams& params, double) noexcept override
  1404. {
  1405. if (! primed)
  1406. {
  1407. for (int i = 0; i < numParams; ++i)
  1408. {
  1409. last[(std::size_t) i] = params[(std::size_t) i];
  1410. engine.set (i, fromSlot (i, params[(std::size_t) i]));
  1411. }
  1412. engine.prepare (rate);
  1413. primed = true;
  1414. }
  1415. else
  1416. for (int i = 0; i < numParams; ++i)
  1417. if (params[(std::size_t) i] != last[(std::size_t) i])
  1418. {
  1419. last[(std::size_t) i] = params[(std::size_t) i];
  1420. const auto raw = fromSlot (i, params[(std::size_t) i]);
  1421. if (raw != engine.get (i))
  1422. engine.set (i, raw);
  1423. }
  1424. engine.process (left, right, n);
  1425. }
  1426. private:
  1427. Engine engine;
  1428. std::array<float, numParams> last {};
  1429. double rate = 44100.0;
  1430. bool primed = false;
  1431. };
  1432. } // namespace
  1433. std::unique_ptr<Effect> make (const EffectSlot& slot) { return std::make_unique<HardcoreEffect> (slot.state); }
  1434. const EffectInfo& info()
  1435. {
  1436. static const std::vector<std::string> names = [] {
  1437. std::vector<std::string> n;
  1438. for (int i = 0; i < numParams; ++i)
  1439. n.push_back (paramName (i));
  1440. return n;
  1441. }();
  1442. static const EffectInfo i = [] {
  1443. EffectInfo e { "Hardcore", {} };
  1444. const auto d = defaultValues();
  1445. for (int k = 0; k < numParams; ++k)
  1446. e.params.push_back ({ names[(std::size_t) k].c_str(), toSlot (k, d[(std::size_t) k]) });
  1447. return e;
  1448. }();
  1449. return i;
  1450. }
  1451. std::vector<std::uint8_t> packCabinets (const std::vector<std::pair<int, std::vector<std::uint8_t>>>& files)
  1452. {
  1453. std::vector<std::uint8_t> out { 'L', 'X', 'H', 'C' };
  1454. auto put = [&] (std::uint32_t x) {
  1455. for (int b = 0; b < 4; ++b)
  1456. out.push_back ((std::uint8_t) (x >> (8 * b)));
  1457. };
  1458. put ((std::uint32_t) files.size());
  1459. for (const auto& [idx, bytes] : files)
  1460. {
  1461. put ((std::uint32_t) idx);
  1462. put ((std::uint32_t) bytes.size());
  1463. out.insert (out.end(), bytes.begin(), bytes.end());
  1464. }
  1465. return out;
  1466. }
  1467. bool hasCabinet (const EffectSlot& slot, int cabinet)
  1468. {
  1469. for (const auto& f : unpackFiles (slot.state))
  1470. if (f.first == cabinet && ! f.second.empty())
  1471. return true;
  1472. return false;
  1473. }
  1474. std::optional<EffectSlot> fromFl (const std::vector<std::uint8_t>& state, const FileReader& read)
  1475. {
  1476. const auto s = parse (state);
  1477. if (! s)
  1478. return std::nullopt;
  1479. EffectSlot slot;
  1480. slot.kind = EffectKind::hardcore;
  1481. toParams (*s, slot.params);
  1482. // every impulse of the installation travels with the slot (about 290 kB) so the cabinet stays switchable
  1483. std::vector<std::pair<int, std::vector<std::uint8_t>>> files;
  1484. if (read)
  1485. for (int c = 1; c <= numCabinets; ++c)
  1486. if (auto b = read (cabinetPath (c)); ! b.empty() && readWav (b))
  1487. files.emplace_back (c, std::move (b));
  1488. if (! files.empty())
  1489. slot.state = packCabinets (files);
  1490. return slot;
  1491. }
  1492. } // namespace lynx::hardcore