Gms.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. #include "core/Gms.h"
  2. #include <algorithm>
  3. #include <atomic>
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <cstring>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <limits>
  10. #include <map>
  11. #include <mutex>
  12. #include <optional>
  13. #include <vector>
  14. // 1:1 port of our round-39 reference model (r39/py/gmsmodel.py + gmspatch.py, gmsfx.py, gmseq.py, gmsrev.py). The
  15. // model emulates the DLL's float32 arithmetic with numpy float32 scalars (operations between float32 values and Python
  16. // floats stay float32, NEP 50) and uses Python floats (doubles) where the DLL computes in double. This file keeps those
  17. // types — `float` where the model holds float32, `double` + an explicit (float) where it rounds a Python float — and
  18. // the operation order of every expression, so fusing a·b + c into one FMA would change the results: contraction stays
  19. // off.
  20. #if defined(__clang__)
  21. #pragma clang fp contract(off)
  22. #elif defined(__GNUC__)
  23. #pragma GCC optimize ("fp-contract=off")
  24. #endif
  25. namespace lynx
  26. {
  27. namespace
  28. {
  29. constexpr double twoPi = 6.283185307179586;
  30. constexpr int maxChunk = 512; // engine buffers of 0x200 floats (§1.1)
  31. constexpr int voiceCount = 99;
  32. constexpr int paramCount = 212;
  33. // cvttss2si / cvttsd2si: truncation toward zero; out of range or NaN → INT_MIN
  34. int i32 (double x) noexcept
  35. {
  36. if (! (x == x) || x >= 2147483648.0 || x < -2147483648.0)
  37. return std::numeric_limits<int>::min();
  38. return (int) x;
  39. }
  40. // MSVC powf as the model computes it: the exact power of the two floats, rounded to float
  41. float powf32 (float x, float y) noexcept { return (float) std::pow ((double) x, (double) y); }
  42. float clamp01 (float x) noexcept { return std::min (std::max (x, 0.0f), 1.0f); }
  43. // FUN_1800ef900: 0 below −80 dB, else powf(10, x·0.05)
  44. float db2lin (float x) noexcept
  45. {
  46. if (! (x >= -80.0f))
  47. return 0.0f;
  48. return powf32 (10.0f, x * 0.05f);
  49. }
  50. // ------------------------------------------------------------------------------------------------------- state
  51. struct Patch
  52. {
  53. std::array<float, paramCount> P {};
  54. std::array<std::string, 3> waves; // oscillator wave base names, as GMS resolves them
  55. };
  56. std::uint32_t le32 (const std::uint8_t* p) noexcept { return (std::uint32_t) p[0] | (std::uint32_t) p[1] << 8 | (std::uint32_t) p[2] << 16 | (std::uint32_t) p[3] << 24; }
  57. // "SSLF" + u32 n + preset; preset = tag "Syn4"|"Syn3" + 7 × 512 path slots + (Syn4: 3 × i32 UI) + 212 × f32 + 512 name
  58. std::optional<Patch> parseState (const std::vector<std::uint8_t>& s, int* paramOffset = nullptr)
  59. {
  60. if (s.size() < 8 || std::memcmp (s.data(), "SSLF", 4) != 0)
  61. return std::nullopt;
  62. const auto n = (std::size_t) le32 (s.data() + 4);
  63. if (s.size() < 8 + n)
  64. return std::nullopt;
  65. const auto* b = s.data() + 8;
  66. const auto tag = n >= 4 ? le32 (b) : 0u;
  67. if (tag != 0x53796E34u && tag != 0x53796E33u)
  68. return std::nullopt;
  69. auto o = (std::size_t) 4 + 7 * 512;
  70. if (tag == 0x53796E34u)
  71. o += 12;
  72. if (n < o + 4 * paramCount)
  73. return std::nullopt;
  74. Patch p;
  75. for (int i = 0; i < paramCount; ++i)
  76. std::memcpy (&p.P[(std::size_t) i], b + o + 4 * (std::size_t) i, 4);
  77. if (paramOffset != nullptr)
  78. *paramOffset = (int) (8 + o);
  79. for (int k = 0; k < 3; ++k)
  80. {
  81. const auto* slot = b + 4 + 512 * (std::size_t) k;
  82. std::string path;
  83. for (int i = 0; i < 512 && slot[i] != 0; ++i)
  84. path.push_back ((char) slot[i]);
  85. std::replace (path.begin(), path.end(), '\\', '/');
  86. if (const auto cut = path.rfind ('/'); cut != std::string::npos)
  87. path = path.substr (cut + 1);
  88. if (const auto dot = path.rfind ('.'); dot != std::string::npos)
  89. path = path.substr (0, dot);
  90. if (path == "Devils Horn")
  91. path = "Double Spike";
  92. p.waves[(std::size_t) k] = path;
  93. }
  94. return p;
  95. }
  96. // ------------------------------------------------------------------------------------------------- wave tables
  97. constexpr int tableCount = 512, tableSize = 1024;
  98. using Tables = std::vector<float>; // tableCount × tableSize
  99. std::vector<std::uint8_t> readFileBytes (std::string path)
  100. {
  101. std::ifstream in (path, std::ios::binary);
  102. if (! in)
  103. {
  104. std::replace (path.begin(), path.end(), '\\', '/');
  105. in.open (path, std::ios::binary);
  106. }
  107. if (! in)
  108. return {};
  109. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  110. }
  111. // channel 0 of a 16-bit PCM or float WAV (others: empty)
  112. std::vector<float> readWaveChannel0 (const std::vector<std::uint8_t>& b)
  113. {
  114. if (b.size() < 12)
  115. return {};
  116. std::size_t i = 12;
  117. int tag = 0, ch = 0, bits = 0;
  118. const std::uint8_t* data = nullptr;
  119. std::size_t dataSize = 0;
  120. while (i + 8 <= b.size() && i < b.size() - 8)
  121. {
  122. const auto n = (std::size_t) le32 (b.data() + i + 4);
  123. if (std::memcmp (b.data() + i, "fmt ", 4) == 0 && i + 8 + 16 <= b.size())
  124. {
  125. tag = b[i + 8] | b[i + 9] << 8;
  126. ch = b[i + 10] | b[i + 11] << 8;
  127. bits = b[i + 22] | b[i + 23] << 8;
  128. }
  129. if (std::memcmp (b.data() + i, "data", 4) == 0)
  130. {
  131. data = b.data() + i + 8;
  132. dataSize = std::min (n, b.size() - (i + 8));
  133. }
  134. i += 8 + n + (n & 1);
  135. }
  136. if (data == nullptr || ch <= 0)
  137. return {};
  138. std::vector<float> x;
  139. if (tag == 3)
  140. {
  141. const auto frames = dataSize / (4 * (std::size_t) ch);
  142. x.resize (frames);
  143. for (std::size_t k = 0; k < frames; ++k)
  144. std::memcpy (&x[k], data + 4 * (std::size_t) ch * k, 4);
  145. }
  146. else if (bits == 16)
  147. {
  148. const auto frames = dataSize / (2 * (std::size_t) ch);
  149. x.resize (frames);
  150. for (std::size_t k = 0; k < frames; ++k)
  151. {
  152. const auto v = (std::int16_t) (data[2 * (std::size_t) ch * k] | data[2 * (std::size_t) ch * k + 1] << 8);
  153. x[k] = (float) ((double) v / 32768.0);
  154. }
  155. }
  156. return x;
  157. }
  158. // FUN_1800f1570 (§1.6): resample to 1024 points with the 4-point cubic of FUN_1800dfa30 (value between p[j+1] and
  159. // p[j+2]), spectrum magnitude/1024 and phase of harmonics 1…512, table k = Σ_{h ≤ k+1} A_h·cos(2π·h·i/1024 + φ_h)
  160. // accumulated in float with double products.
  161. std::shared_ptr<const Tables> buildTables (const std::vector<float>& x)
  162. {
  163. auto T = std::make_shared<Tables> ((std::size_t) tableCount * tableSize, 0.0f);
  164. const auto L = x.size();
  165. if (L == 0)
  166. return T;
  167. std::vector<float> p (L + 4, 0.0f);
  168. std::copy (x.begin(), x.end(), p.begin());
  169. std::vector<double> y (tableSize);
  170. for (int i = 0; i < tableSize; ++i)
  171. {
  172. const auto pos = (double) i * (double) L * 0.0009765625;
  173. const auto idx = (std::size_t) pos;
  174. const auto t = (float) (pos - (double) idx);
  175. const auto p0 = p[idx], p1 = p[idx + 1], p2 = p[idx + 2], p3 = p[idx + 3];
  176. const auto a = (p2 - p0) * 0.5f;
  177. const auto b = (p3 - p0) * 0.5f + (p1 - p2) * 1.5f;
  178. const auto v = (((((p0 - p1) + a) - b) + b * t) * t + a) * t + p1;
  179. y[(std::size_t) i] = idx < L ? (double) v : 0.0;
  180. }
  181. // DFT of harmonics 1…512
  182. std::vector<double> cw (tableSize), sw (tableSize);
  183. for (int k = 0; k < tableSize; ++k)
  184. {
  185. cw[(std::size_t) k] = std::cos (twoPi * k / tableSize);
  186. sw[(std::size_t) k] = std::sin (twoPi * k / tableSize);
  187. }
  188. std::vector<float> amp (tableCount), ph (tableCount);
  189. for (int h = 1; h <= tableCount; ++h)
  190. {
  191. double re = 0.0, im = 0.0;
  192. for (int n = 0; n < tableSize; ++n)
  193. {
  194. const auto k = (std::size_t) ((h * n) & (tableSize - 1));
  195. re += y[(std::size_t) n] * cw[k];
  196. im -= y[(std::size_t) n] * sw[k];
  197. }
  198. amp[(std::size_t) h - 1] = (float) (std::hypot (re, im) / 1024.0);
  199. ph[(std::size_t) h - 1] = (float) std::atan2 (im, re);
  200. }
  201. std::vector<float> acc (tableSize, 0.0f);
  202. for (int kk = 0; kk < tableCount; ++kk)
  203. {
  204. const auto w = (double) ((float) kk + 1.0f) * 0.006135923151542565;
  205. auto* row = T->data() + (std::size_t) kk * tableSize;
  206. for (int n = 0; n < tableSize; ++n)
  207. {
  208. acc[(std::size_t) n] = (float) (std::cos ((double) n * w + (double) ph[(std::size_t) kk]) * (double) amp[(std::size_t) kk] + (double) acc[(std::size_t) n]);
  209. row[n] = acc[(std::size_t) n];
  210. }
  211. }
  212. return T;
  213. }
  214. std::shared_ptr<const Tables> zeroTables()
  215. {
  216. static const auto z = std::make_shared<const Tables> ((std::size_t) tableCount * tableSize, 0.0f);
  217. return z;
  218. }
  219. // tables per wave file, built once per process (a missing or unreadable file gives silent tables, like the model)
  220. std::shared_ptr<const Tables> waveTables (const std::string& dir, const std::string& name)
  221. {
  222. static std::mutex mutex;
  223. static std::map<std::string, std::shared_ptr<const Tables>> cache;
  224. if (dir.empty() || name.empty())
  225. return zeroTables();
  226. const auto path = dir + "/" + name + ".wav";
  227. {
  228. const std::lock_guard<std::mutex> lock (mutex);
  229. if (const auto it = cache.find (path); it != cache.end())
  230. return it->second;
  231. }
  232. const auto x = readWaveChannel0 (readFileBytes (path));
  233. auto T = x.empty() ? zeroTables() : buildTables (x);
  234. const std::lock_guard<std::mutex> lock (mutex);
  235. return cache.emplace (path, T).first->second;
  236. }
  237. // FUN_1800f0ed2…: 65 536 (g, k) pairs, independent of the sample rate
  238. struct CutoffTable
  239. {
  240. std::vector<float> g, k;
  241. CutoffTable() : g (65536), k (65536)
  242. {
  243. for (int i = 0; i < 65536; ++i)
  244. {
  245. const auto e = (float) i * 0.00014913368795532733f;
  246. g[(std::size_t) i] = (float) std::pow (2.0, (double) e) * 0.0005f;
  247. k[(std::size_t) i] = (float) ((double) (g[(std::size_t) i] * 7.5f + 5.0f) * 0.756525);
  248. }
  249. }
  250. };
  251. const CutoffTable& cutoffTable()
  252. {
  253. static const CutoffTable t;
  254. return t;
  255. }
  256. // ------------------------------------------------------------------------------------------------------- RNG
  257. // the process-global LCG of GMS (FUN_1800ef8c0); LYNX keeps one per instance (§1.7)
  258. struct Rng
  259. {
  260. static constexpr std::uint32_t A = 0x0bb38435u, C = 0x3619636bu;
  261. std::uint32_t s = 0;
  262. std::uint32_t next() noexcept { return s = s * A + C; }
  263. void skip (std::uint64_t n) noexcept // jump ahead by n draws
  264. {
  265. std::uint32_t a = 1, c = 0, ba = A, bc = C;
  266. while (n != 0)
  267. {
  268. if (n & 1)
  269. a = a * ba, c = c * ba + bc;
  270. bc = bc * ba + bc;
  271. ba = ba * ba;
  272. n >>= 1;
  273. }
  274. s = s * a + c;
  275. }
  276. };
  277. std::uint32_t phaseOfs (std::uint32_t base, float x) noexcept // FUN_1800ef960
  278. {
  279. auto d = (double) x * 4194303.0 + 268435392.0;
  280. d = std::clamp (d, -9.0e18, 9.0e18);
  281. return (std::uint32_t) ((std::uint32_t) (std::int64_t) d * 256u + base);
  282. }
  283. int tableFor (float freq, double fs) noexcept // FUN_1800ef980
  284. {
  285. const auto fsf = (float) fs;
  286. if (fsf * 0.0009765625f < freq)
  287. return std::clamp (i32 ((double) ((0.5f / freq) * fsf)), 0, 511);
  288. return 511;
  289. }
  290. std::uint32_t phaseInc (float f, double fs) noexcept // FUN_1800ef930
  291. {
  292. const auto fsf = (float) fs;
  293. if (f <= 0.0f)
  294. f = 0.0f;
  295. const auto lim = fsf * 0.4999000132083893f;
  296. if (lim <= f)
  297. f = lim;
  298. return (std::uint32_t) (std::int64_t) ((double) (f / fsf) * 4294967295.0);
  299. }
  300. bool matrixHit (int param, int dest) noexcept // FUN_1800ef810
  301. {
  302. switch (dest)
  303. {
  304. case 1: return param == 12;
  305. case 2: return param == 13;
  306. case 4: return param == 50;
  307. case 6: return param == 20;
  308. case 7: return param == 22;
  309. case 8:
  310. case 12: return param == 16;
  311. case 10: return param == 21;
  312. case 11: return param == 23;
  313. case 14: return param == 34;
  314. case 15: return param == 28;
  315. case 17: return param == 35;
  316. case 18: return param == 29;
  317. case 20: return param == 4;
  318. case 21: return param == 5;
  319. default: return false;
  320. }
  321. }
  322. constexpr float lfoSync[9] = { 0.125f, 0.25f, 0.375f, 0.5f, 0.75f, 1.0f, 2.0f, 4.0f, 8.0f };
  323. float lfoSyncValue (float x) noexcept // FUN_1800efa80
  324. {
  325. const auto k = i32 ((double) (x * 9.0f));
  326. if (k < 1)
  327. return 0.125f;
  328. if (k > 8)
  329. return 8.0f;
  330. return lfoSync[k];
  331. }
  332. float lfoWave (int shape, float ph) noexcept // FUN_1800ef9f0
  333. {
  334. if (shape == 0)
  335. return (float) std::sin ((double) ph * twoPi);
  336. if (shape == 1)
  337. {
  338. auto v = ph + ph;
  339. if (1.0f < v)
  340. v = 2.0f - v;
  341. return v;
  342. }
  343. if (shape == 2)
  344. return 1.0f - ph;
  345. return ph < 0.5f ? 1.0f : 0.0f;
  346. }
  347. // FUN_1800efb10: LFO frequency (Hz) and, for a synced LFO while the host plays (pos4 = song beats × 4), the phase
  348. struct LfoRate
  349. {
  350. float f = 0.0f;
  351. bool hasPhase = false;
  352. float phase = 0.0f;
  353. };
  354. LfoRate lfoRate (float rate, float sync, float tempo, bool hasPos, float pos4) noexcept
  355. {
  356. LfoRate r;
  357. const auto k = i32 ((double) (sync + sync));
  358. if (k == 0)
  359. {
  360. const auto v = clamp01 (1.0f - rate);
  361. r.f = (float) ((double) (v * v * 25.0f) + 0.01);
  362. return r;
  363. }
  364. auto v = lfoSyncValue (rate);
  365. if (k == 2)
  366. v = v * 4.0f;
  367. if (hasPos && 0.0f <= pos4)
  368. r.hasPhase = true, r.phase = (pos4 * 0.25f) / v;
  369. if (tempo > 0.0f)
  370. {
  371. const auto t = (v * 60.0f) / tempo;
  372. if (t != 0.0f)
  373. r.f = 1.0f / t;
  374. }
  375. return r;
  376. }
  377. // ------------------------------------------------------------------------------------------------------- effects
  378. constexpr float tiny9 = 1e-9f;
  379. float tiny0 (float v) noexcept { return (v < tiny9 && v > -tiny9) ? 0.0f : v; }
  380. // float sine of the DLL's packed CRT kernel (0x18012a460, SSE path)
  381. float sinK (float x) noexcept
  382. {
  383. const auto a = std::fabs (x);
  384. const auto t = a * 0.31830987334251404f + 12582912.0f;
  385. const auto n = t - 12582912.0f;
  386. std::uint32_t bits = 0;
  387. std::memcpy (&bits, &t, 4);
  388. const auto odd = (bits & 1u) != 0;
  389. auto r = a - 3.140625f * n;
  390. r = r - 0.0009675025939941406f * n;
  391. r = r - 1.5099067240953445e-07f * n;
  392. r = r - 5.126688136514179e-12f * n;
  393. const auto r2 = r * r;
  394. if (odd)
  395. r = -r;
  396. auto p = 2.605780537123792e-06f * r2 + -0.00019809602235909551f;
  397. p = p * r2 + 0.008333062753081322f;
  398. p = p * r2 + -0.16666659712791443f;
  399. const auto y = r + (r2 * p) * r;
  400. return x < 0.0f ? -y : y;
  401. }
  402. void sinCosK (float w, float& s, float& c) noexcept // FUN_18012a3f0
  403. {
  404. const auto a = std::fabs (w);
  405. if (a < 0.0001220703125f)
  406. {
  407. s = w;
  408. c = 1.0f - (a * a) * 0.5f;
  409. return;
  410. }
  411. s = sinK (w);
  412. c = sinK (a + 1.5707963705062866f);
  413. }
  414. struct Biquad // FUN_1800e4eb0/e4fb0 (low-pass), FUN_1800e4cc0/e4dc0 (high-pass)
  415. {
  416. bool hp = false;
  417. float f = 0, q = 0, fs = 0, a = 0, b1 = 0, b2 = 0, x2 = 0, x1 = 0, y2 = 0, y1 = 0;
  418. void init (bool highPass) noexcept
  419. {
  420. hp = highPass;
  421. f = q = fs = 0.0f;
  422. design (100.0f, 1.0f, 44100.0f);
  423. clear();
  424. }
  425. void clear() noexcept { x2 = x1 = y2 = y1 = 0.0f; }
  426. void design (float nf, float nq, float nfs) noexcept
  427. {
  428. if (nf == f && nq == q && nfs == fs)
  429. return;
  430. f = nf, q = nq, fs = nfs;
  431. const auto w = (float) ((double) f * twoPi / (double) fs);
  432. float s = 0, c = 0;
  433. sinCosK (w, s, c);
  434. const auto inv = 1.0f / (s + q);
  435. const auto den = s / q + 2.0f;
  436. const auto na = hp ? (c + 1.0f) / den : (1.0f - c) / den;
  437. const auto nb1 = (c * inv) * (q * -2.0f);
  438. const auto nb2 = (q - s) * inv;
  439. a = tiny0 (na), b1 = tiny0 (nb1), b2 = tiny0 (nb2);
  440. }
  441. float tick (float x) noexcept
  442. {
  443. x = tiny0 (x);
  444. const auto t = hp ? (x - (x1 + x1)) + x2 : ((x1 + x1) + x) + x2;
  445. auto y = ((t * a) - (y1 * b1)) - (b2 * y2);
  446. y = tiny0 (y);
  447. x2 = x1, y2 = y1, x1 = x, y1 = y;
  448. return y;
  449. }
  450. };
  451. // MFXTapeDelay (FUN_1800feae0 / FUN_1800fef90 / FUN_1800fed00 / reset FUN_1800fec70)
  452. struct TapeDelay
  453. {
  454. struct Worm
  455. {
  456. std::vector<float> v;
  457. std::vector<double> t;
  458. int count = 0, ridx = 0;
  459. };
  460. int len = 4;
  461. Worm worms[2];
  462. int w = 0, r = 1;
  463. float gain = 0.0f, fb = 0.0f, lp = 1.0f, hp = 0.0f, s1 = 0.0f, s2 = 0.0f;
  464. double rate = 0.0, pos = 0.0, minrate = 0.25;
  465. void init (int size)
  466. {
  467. len = std::max (4, size);
  468. for (auto& wm : worms)
  469. {
  470. wm.v.assign ((std::size_t) len + 0x100, 0.0f);
  471. wm.t.assign ((std::size_t) len + 0x100, 0.0);
  472. wm.count = wm.ridx = 0;
  473. }
  474. w = 0, r = 1;
  475. gain = 0.0f, fb = 0.0f, lp = 1.0f, hp = 0.0f, s1 = s2 = 0.0f;
  476. rate = 0.0, pos = 0.0, minrate = 1.0 / len;
  477. }
  478. void reset() noexcept
  479. {
  480. for (auto& wm : worms)
  481. {
  482. std::fill (wm.v.begin(), wm.v.end(), 0.0f);
  483. std::fill (wm.t.begin(), wm.t.end(), 0.0);
  484. wm.count = wm.ridx = 0;
  485. }
  486. s1 = s2 = 0.0f;
  487. }
  488. void setTime (double d) noexcept
  489. {
  490. d = std::min (d, 0.99);
  491. d = std::max (d, 0.0);
  492. const auto m2 = 2.0 * (1.0 / len);
  493. rate = 1.0 / ((d * (1.0 - m2) + m2) * len);
  494. }
  495. float tick (float x) noexcept
  496. {
  497. auto& rb = worms[r];
  498. float out = 0.0f;
  499. const auto p = pos;
  500. if (rb.count != 0)
  501. for (;;)
  502. {
  503. const auto i = rb.ridx;
  504. if (rb.count + 1 <= i)
  505. break;
  506. const auto ti = rb.t[(std::size_t) (10 + i)];
  507. if (p <= ti)
  508. {
  509. double fr = 0.0;
  510. const auto t0 = (float) rb.t[(std::size_t) (9 + i)];
  511. const auto t1 = (float) ti;
  512. if (t0 < t1)
  513. fr = (p - (double) t0) / (double) (t1 - t0);
  514. const auto v = (float) ((double) (rb.v[(std::size_t) (10 + i)] - rb.v[(std::size_t) (9 + i)]) * fr + (double) rb.v[(std::size_t) (9 + i)]);
  515. if (! (v < tiny9 && v > -tiny9))
  516. out = v;
  517. break;
  518. }
  519. rb.ridx = i + 1;
  520. }
  521. auto rt = std::min (rate, 0.2);
  522. rt = std::max (rt, minrate);
  523. const auto y = (((out * fb) + (x * gain)) - s1) * lp + s1;
  524. const auto newpos = rt + p;
  525. pos = newpos;
  526. s1 = y;
  527. s2 = ((y - s2) * hp) + s2;
  528. const auto wv = y - s2;
  529. if (newpos >= 1.0)
  530. {
  531. auto& wb = worms[w];
  532. const auto c = (std::size_t) rb.count;
  533. wb.v[9] = rb.v[9 + c], wb.t[9] = rb.t[9 + c] - 1.0;
  534. wb.v[8] = rb.v[8 + c], wb.t[8] = rb.t[8 + c] - 1.0;
  535. wb.v[7] = rb.v[7 + c], wb.t[7] = rb.t[7 + c] - 1.0;
  536. wb.v[(std::size_t) (10 + wb.count)] = wv;
  537. wb.t[(std::size_t) (10 + wb.count)] = pos;
  538. wb.ridx = 0;
  539. std::swap (w, r);
  540. pos -= 1.0;
  541. worms[w].count = 0;
  542. }
  543. auto& wb = worms[w];
  544. if ((std::size_t) (10 + wb.count) < wb.v.size())
  545. {
  546. wb.v[(std::size_t) (10 + wb.count)] = wv;
  547. wb.t[(std::size_t) (10 + wb.count)] = pos;
  548. ++wb.count;
  549. }
  550. return out;
  551. }
  552. };
  553. // output EQ band (FUN_1800e5050 / FUN_1800e50a0 / FUN_1800e5440); band 0 low shelf, 1–3 peaking, 4 high shelf
  554. struct EqBand
  555. {
  556. int idx = 0;
  557. float c[5] {}, s[4] {};
  558. void clear() noexcept { s[0] = s[1] = s[2] = s[3] = 0.0f; }
  559. void design (float gainDb, float fs) noexcept
  560. {
  561. static constexpr float freq[5] = { 80.0f, 220.0f, 1200.0f, 6000.0f, 12000.0f };
  562. static constexpr float bwt[5] = { 1.0f, 2.5f, 2.799999952316284f, 2.5f, 2.0f };
  563. const auto type = idx == 0 ? 0 : (idx == 4 ? 2 : 1);
  564. const auto f = std::max (freq[idx], 0.0f);
  565. const auto bw = std::max (bwt[idx], 9.99999975e-05f);
  566. const auto A = (float) std::pow (10.0, (double) (gainDb * 0.05000000074505806f));
  567. auto w = (float) ((double) f * 6.283185 / (double) fs);
  568. w = std::min (w, 3.0f);
  569. w = std::max (w, 0.0f);
  570. const auto sn = (float) std::sin ((double) w);
  571. const auto sh = std::sinh ((double) bw * 0.3465736 * (double) w / (double) sn);
  572. const auto al2 = (float) (sh + sh);
  573. const auto alpha = (sn * al2) * 0.5f;
  574. const auto beta = std::sqrt (A) * al2;
  575. const auto one = 1.0f;
  576. float b0, b1, b2, a0, a1, a2;
  577. if (type == 0)
  578. {
  579. const auto cs = (float) std::cos ((double) w);
  580. const auto k = sn * beta;
  581. const auto am1 = A - one, ap1 = A + one;
  582. const auto t = am1 * cs, u = ap1 - t, v = t + ap1;
  583. b0 = (u + k) * A;
  584. b2 = (u - k) * A;
  585. b1 = (A + A) * (am1 - (ap1 * cs));
  586. a1 = (am1 * -2.0f) - ((ap1 * cs) + (ap1 * cs));
  587. a0 = k + v;
  588. a2 = v - k;
  589. }
  590. else if (type == 2)
  591. {
  592. const auto cs = (float) std::cos ((double) w);
  593. const auto k = sn * beta;
  594. const auto am1 = A - one, ap1 = A + one;
  595. const auto t = am1 * cs, c2 = ap1 * cs;
  596. const auto p = t + ap1, q = ap1 - t;
  597. b0 = (k + p) * A;
  598. b2 = (p - k) * A;
  599. a0 = q + k;
  600. b1 = ((c2 + am1) * A) * -2.0f;
  601. const auto d = am1 - c2;
  602. a1 = d + d;
  603. a2 = q - k;
  604. }
  605. else
  606. {
  607. const auto aA = alpha * A, aD = alpha / A;
  608. b0 = aA + one;
  609. a0 = aD + one;
  610. const auto cc = (float) std::cos ((double) w) * -2.0f;
  611. a1 = cc, b1 = cc;
  612. a2 = one - aD;
  613. b2 = one - aA;
  614. }
  615. const auto inv = one / a0;
  616. const float cs[5] = { inv * b0, inv * b1, inv * b2, inv * a1, inv * a2 };
  617. for (int i = 0; i < 5; ++i)
  618. c[i] = tiny0 (cs[i]);
  619. }
  620. float tick (float x) noexcept
  621. {
  622. x = tiny0 (x);
  623. auto y = ((((s[0] * c[1]) + (x * c[0])) + (c[2] * s[1])) - (s[2] * c[3])) - (c[4] * s[3]);
  624. y = tiny0 (y);
  625. s[1] = s[0], s[3] = s[2], s[0] = x, s[2] = y;
  626. return y;
  627. }
  628. };
  629. // GMS reverb (class fxReverb, §1.13 slot 7). The early-reflection path is unreachable from GMS (switched off every
  630. // block) and is not built.
  631. struct Reverb
  632. {
  633. static constexpr double qa[18][2] = { { 421.0, -360.5 }, { 2981.0, 19.5 }, { 216.0, 5.5 }, { 250.0, 0.5 }, { 99.0, 1.5 }, { 100.0, 0.5 },
  634. { 28.0, 2.5 }, { 1975.0, 25.5 }, { 199.0, 1.5 }, { 216.0, 5.5 }, { 421.0, -360.5 }, { 421.0, -360.5 },
  635. { 421.0, -360.5 }, { 200.0, 0.5 }, { 1.0, 0.5 }, { 1.0, 0.5 }, { 1.0, 0.5 }, { 1.0, 0.5 } };
  636. static constexpr int lines[12] = { 142, 107, 379, 277, 672, 4453, 1800, 3720, 908, 4217, 2656, 3163 };
  637. static constexpr int taps0[14] = { 266, 2974, 1913, 2496, 1690, 187, 1566, 353, 3627, 1728, 3173, 1811, 335, 621 };
  638. static constexpr float tiny = 1e-30f;
  639. float fs = 44100.0f;
  640. int pi[18] {};
  641. std::vector<float> buf[12], pre;
  642. int len[12] {}, pos[12] {}, prepos = 0, prelen = 1, lastsize = 0;
  643. int taps[14] {};
  644. bool dirty = true;
  645. float lp = 0, hpx = 0, hpy = 0, dL[2] {}, dR[2] {}, tL = 0, tR = 0, lev = -1000.0f;
  646. float gin = 0, hpk = 0, lpk = 1, ap1 = 0, ap2 = 0, tap1 = 0, tap2 = 0, decay = 0, shelf = 0, shk = 0, damp = 1, dry = 0, tank = 0, width = 0;
  647. static int floorI (float x) noexcept
  648. {
  649. auto i = (int) x;
  650. if ((double) i != (double) x && x < 0.0f)
  651. --i;
  652. return i;
  653. }
  654. static int cdiv (long long a, long long b) noexcept { return (int) (a / b); }
  655. static float db (int i) noexcept // FUN_180102ff0
  656. {
  657. if (i < -360)
  658. return 0.0f;
  659. return (float) std::pow (10.0, (double) ((float) i * 0.004999999888241291f));
  660. }
  661. void init (float rate)
  662. {
  663. static constexpr float preset[18] = { 0.8574821949005127f, 0.018785642459988594f, 0.16203702986240387f, 0.0f, 0.49494948983192444f, 1.0f,
  664. 0.2857142984867096f, 0.2405063360929489f, 0.07035175710916519f, 0.16203702986240387f,
  665. 0.8574821949005127f, 0.7743467688560486f, 0.5724465847015381f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f };
  666. fs = rate;
  667. const auto fsi = (int) fs;
  668. for (int i = 0; i < 18; ++i)
  669. setp (i, preset[i]);
  670. for (int k = 0; k < 12; ++k)
  671. {
  672. const auto n = std::max (1, cdiv ((long long) fsi * lines[k], 20000));
  673. buf[k].assign ((std::size_t) n, tiny);
  674. len[k] = n;
  675. pos[k] = 0;
  676. }
  677. const auto predmax = cdiv ((long long) fsi * 250, 1000);
  678. pre.assign ((std::size_t) std::max (1, predmax), tiny);
  679. prepos = 0, prelen = std::max (1, predmax), lastsize = 0, dirty = true;
  680. reset();
  681. }
  682. void setp (int i, float v) noexcept
  683. {
  684. pi[i] = floorI ((v * (float) qa[i][0]) + (float) qa[i][1]);
  685. dirty = true;
  686. }
  687. void reset() noexcept
  688. {
  689. for (auto& b : buf)
  690. std::fill (b.begin(), b.end(), tiny);
  691. std::fill (pre.begin(), pre.end(), tiny);
  692. lp = hpx = hpy = 0.0f;
  693. dL[0] = dL[1] = dR[0] = dR[1] = 0.0f;
  694. tL = tR = 0.0f;
  695. lev = -1000.0f;
  696. }
  697. static float onePoleLp (int p, float fs) noexcept
  698. {
  699. if ((float) p < 221.0f)
  700. {
  701. const auto c = (float) std::cos ((double) (((float) p * 628.3185424804688f) / fs));
  702. const auto t = 2.0f - c;
  703. const auto s = std::sqrt ((t * t) - 1.0f);
  704. return 1.0f - (t - s);
  705. }
  706. return 1.0f;
  707. }
  708. void recalc() noexcept // FUN_180103020
  709. {
  710. const auto fsi = (int) fs;
  711. const auto* p = pi;
  712. const auto one = 1.0f;
  713. gin = p[14] == 0 ? db (p[0]) : 0.0f;
  714. if ((float) p[1] > 19.0f)
  715. {
  716. const auto x = (std::min (fs * 0.5f, (float) p[1]) * 3.1415927410125732f) / fs;
  717. const auto e = (float) std::tan ((double) x);
  718. hpk = (e - one) / (e + one);
  719. }
  720. else
  721. hpk = 0.0f;
  722. lpk = onePoleLp (p[2], fs);
  723. prelen = std::max (1, cdiv ((long long) fsi * p[3], 1000));
  724. prepos %= prelen;
  725. const auto base = cdiv ((long long) (p[4] + 15) * fsi, 115);
  726. for (int k = 0; k < 12; ++k)
  727. {
  728. len[k] = std::max (1, cdiv ((long long) base * lines[k], 20000));
  729. len[k] = std::min (len[k], (int) buf[k].size());
  730. pos[k] %= len[k];
  731. }
  732. for (int t = 0; t < 14; ++t)
  733. taps[t] = cdiv ((long long) base * taps0[t], 20000);
  734. if (p[4] != lastsize)
  735. {
  736. lastsize = p[4];
  737. for (int k = 0; k < 12; ++k)
  738. std::fill (buf[k].begin(), buf[k].begin() + len[k], 0.0f);
  739. }
  740. const auto diff = (float) p[5] * 0.01f;
  741. const auto inv = one / fs;
  742. if (diff != 0.0f)
  743. {
  744. ap1 = (float) std::exp ((double) (((float) (len[0] + len[1]) * (-49.34111022949219f / diff)) * inv));
  745. ap2 = (float) std::exp ((double) (((-31.398887634277344f / diff) * (float) (len[2] + len[3])) * inv));
  746. auto x = (((float) (len[4] + len[8]) * -3.4538776874542236f) / fs) * (2.857142925262451f / diff);
  747. tap1 = (float) std::exp ((double) x);
  748. x = ((-3.4538776874542236f / fs) * (float) (len[6] + len[10])) * (1.9607843160629272f / diff);
  749. tap2 = (float) std::exp ((double) x);
  750. }
  751. else
  752. ap1 = ap2 = tap1 = tap2 = 0.0f;
  753. const auto tsum = ((float) (len[9] + len[7] + len[5] + len[11]) / fs) * -6.907755374908447f;
  754. const auto t60 = (float) p[8] * 0.10000000149011612f;
  755. decay = (float) std::pow ((double) (float) std::exp ((double) (tsum / t60)), 0.25);
  756. auto r = (float) std::pow ((double) (float) std::exp ((double) ((10.0f / ((float) p[6] * t60)) * tsum)), 0.25);
  757. r = r / decay;
  758. shelf = (r - one) * 0.5f;
  759. const auto rm = std::min (r, one);
  760. const auto e = (float) std::tan ((double) (((float) p[7] * 3.1415927410125732f) / fs));
  761. shk = (e - rm) / (rm + e);
  762. damp = onePoleLp (p[9], fs);
  763. dry = p[15] == 0 ? db (p[10]) : 0.0f;
  764. tank = p[17] == 0 ? db (p[12]) * 0.30000001192092896f : 0.0f;
  765. width = (float) p[13] * 0.01f;
  766. dirty = false;
  767. }
  768. float ap (int k, float x, float c1, float c2) noexcept
  769. {
  770. auto& b = buf[k];
  771. const auto i = pos[k];
  772. const auto t = b[(std::size_t) i] - (c1 * x);
  773. b[(std::size_t) i] = (c2 * t) + x;
  774. pos[k] = i > 0 ? i - 1 : len[k] - 1;
  775. return t;
  776. }
  777. static int wrap (int i, int n) noexcept { return i >= n ? i - n : i; }
  778. // FUN_180103f30: adds the result into outL/outR
  779. void process (const float* inL, const float* inR, float* outL, float* outR, int n) noexcept
  780. {
  781. if (dirty)
  782. recalc();
  783. const auto d = decay, one = 1.0f, half = 0.5f, dd = one - d;
  784. const auto g41 = (dd * 0.6000000238418579f) + d, g42 = (dd * 0.25f) + d, g43 = (dd * 0.699999988079071f) + d;
  785. const auto g56 = (dd * 0.20000000298023224f) + d, g44 = (dd * half) + d;
  786. auto* B = buf;
  787. auto* L = len;
  788. auto* P = pos;
  789. const auto* T = taps;
  790. for (int s = 0; s < n; ++s)
  791. {
  792. const auto xl = gin * inL[s], xr = gin * inR[s];
  793. const auto m = ((xr + xl) * half) + tiny;
  794. auto l = ((m - lp) * lpk) + lp;
  795. lp = l;
  796. const auto h = ((l - hpx) * hpk) + hpy;
  797. hpy = l;
  798. hpx = h;
  799. if (hpk != 0.0f)
  800. l = ((l - h) * half) + tiny;
  801. auto t = ap (0, l, ap1, ap1);
  802. auto u = ap (1, t, ap2, ap1);
  803. t = ap (2, u, ap1, ap1);
  804. u = ap (3, t, ap2, ap1);
  805. const auto pv = pre[(std::size_t) prepos];
  806. pre[(std::size_t) prepos] = u;
  807. prepos = prepos > 0 ? prepos - 1 : prelen - 1;
  808. const auto tin = pv + 9.99999968e-21f;
  809. // tank, left half
  810. auto v = B[11][(std::size_t) P[11]] * d;
  811. auto s2 = ((v - dL[1]) * shk) + dL[0];
  812. dL[0] = v, dL[1] = s2;
  813. auto w = (((((s2 + v) * shelf) + v) + tin) - tL) * damp;
  814. w = w + tL;
  815. tL = w;
  816. u = ap (4, w, tap1, tap1);
  817. auto i = P[5];
  818. auto o = B[5][(std::size_t) i];
  819. B[5][(std::size_t) i] = u;
  820. P[5] = i > 0 ? i - 1 : L[5] - 1;
  821. o = o * d;
  822. u = ap (6, o, tap2, tap2);
  823. i = P[7];
  824. B[7][(std::size_t) i] = u;
  825. i = i > 0 ? i - 1 : L[7] - 1;
  826. P[7] = i;
  827. v = B[7][(std::size_t) i] * d;
  828. s2 = ((v - dR[1]) * shk) + dR[0];
  829. dR[0] = v, dR[1] = s2;
  830. w = (((((s2 + v) * shelf) + v) + tin) - tR) * damp;
  831. w = w + tR;
  832. tR = w;
  833. u = ap (8, w, tap1, tap1);
  834. i = P[9];
  835. o = B[9][(std::size_t) i];
  836. B[9][(std::size_t) i] = u;
  837. P[9] = i > 0 ? i - 1 : L[9] - 1;
  838. o = o * d;
  839. u = ap (10, o, tap2, tap2);
  840. i = P[11];
  841. B[11][(std::size_t) i] = u;
  842. P[11] = i > 0 ? i - 1 : L[11] - 1;
  843. auto at = [&] (int line, int tap) { return B[line][(std::size_t) wrap (tap + P[line], L[line])]; };
  844. const auto fA = at (9, T[0]), fB = at (9, T[1]), fC = at (10, T[2]), fD = at (11, T[3]);
  845. const auto fE = at (5, T[4]), fF = at (6, T[5]), fG = at (7, T[6]);
  846. const auto f7 = at (5, T[8]), f8 = at (5, T[7]), f9 = at (6, T[9]), f10 = at (7, T[10]), f11 = at (9, T[11]);
  847. const auto f12 = at (10, T[12]), f13 = at (11, T[13]);
  848. auto tl = (((fA - (g42 * fB)) - (g43 * fC)) - (g56 * fD)) + (g41 * fE);
  849. tl = (tl + fF) + (g44 * fG);
  850. auto tr = ((((g42 * f7) + f8) - (g43 * f9)) - (g56 * f10)) - (g41 * f11);
  851. tr = (tr + f12) + (g44 * f13);
  852. if (lev < -99.0f)
  853. lev = tank;
  854. lev = ((tank - lev) * 0.00100000005f) + lev;
  855. const auto a = (lev * tl) + 0.0f, b = (lev * tr) + 0.0f;
  856. const auto sm = b + a, df = (a - b) * width;
  857. outL[s] = ((xl * dry) + (df + sm)) + outL[s];
  858. outR[s] = ((xr * dry) + (sm - df)) + outR[s];
  859. }
  860. }
  861. };
  862. constexpr float rateSteps[9] = { 0.125f, 0.25f, 0.375f, 0.5f, 0.75f, 1.0f, 2.0f, 4.0f, 8.0f };
  863. // one effect object (FUN_1800fefe0 kind k; FUN_1800ff500 per block)
  864. struct Fx
  865. {
  866. int kind = 0;
  867. float fs = 44100.0f;
  868. float x = 0, y = 0, rate = 1.0f, amount = 0;
  869. int shape = 0;
  870. bool off = false;
  871. double phase = 0.0, inc = 0.0;
  872. float period = 0.0f, fade = 0.0f;
  873. int active = 0, tail = 0;
  874. Biquad flt[8], hpf[8];
  875. float hold = 0, holdL = 0, holdR = 0;
  876. float apS[12] {}, apfbL = 0, apfbR = 0, xs = -1.0f;
  877. bool eqOn = false;
  878. float eqGain[5] {};
  879. bool eqDirty[5] { true, true, true, true, true };
  880. EqBand eqL[5], eqR[5];
  881. std::vector<TapeDelay> dl;
  882. std::unique_ptr<Reverb> rev;
  883. std::vector<float> cl, cr; // reverb input copies
  884. void init (int k, double rateHz)
  885. {
  886. kind = k;
  887. fs = (float) rateHz;
  888. x = y = amount = 0.0f, rate = 1.0f, shape = 0, off = false;
  889. phase = inc = 0.0, period = 0.0f, fade = 0.0f, active = tail = 0;
  890. for (auto& b : flt)
  891. b.init (false);
  892. for (auto& b : hpf)
  893. b.init (true);
  894. hold = holdL = holdR = 0.0f;
  895. eqOn = false;
  896. for (int i = 0; i < 5; ++i)
  897. eqGain[i] = 0.0f, eqDirty[i] = true, eqL[i].idx = eqR[i].idx = i, eqL[i].clear(), eqR[i].clear();
  898. dl.clear();
  899. if (k == 6)
  900. dl.resize (2), std::for_each (dl.begin(), dl.end(), [&] (TapeDelay& d) { d.init ((int) (fs * 1.5f)); });
  901. else if (k == 8)
  902. dl.resize (2), std::for_each (dl.begin(), dl.end(), [&] (TapeDelay& d) { d.init ((int) ((double) fs * 0.002)); });
  903. else if (k == 2)
  904. dl.resize (6), std::for_each (dl.begin(), dl.end(), [&] (TapeDelay& d) { d.init ((int) ((double) fs * 0.002)); });
  905. rev.reset();
  906. if (k == 7)
  907. {
  908. rev = std::make_unique<Reverb>();
  909. rev->init (44100.0f); // re-created at its default rate (never told the host rate)
  910. cl.assign (maxChunk, 0.0f);
  911. cr.assign (maxChunk, 0.0f);
  912. }
  913. reset();
  914. }
  915. void reset() noexcept // FUN_1800ff2c0 with an unchanged rate
  916. {
  917. for (auto& d : dl)
  918. d.reset();
  919. std::fill (std::begin (apS), std::end (apS), 0.0f);
  920. apfbL = apfbR = 0.0f;
  921. xs = -1.0f;
  922. if (rev != nullptr)
  923. rev->reset();
  924. hold = 0.0f, holdL = 0.0f;
  925. for (int i = 0; i < 6; ++i)
  926. flt[i].clear(), hpf[i].clear();
  927. for (int i = 0; i < 5; ++i)
  928. eqL[i].clear(), eqR[i].clear(), eqDirty[i] = true;
  929. phase = 0.0, fade = 0.0f, tail = 0, active = 0;
  930. }
  931. float lfo() noexcept // FUN_1800fea00
  932. {
  933. const auto p = inc + phase;
  934. const auto pf = p - (double) (long long) p;
  935. if (shape == 0)
  936. {
  937. const auto v = (float) std::sin (phase * twoPi);
  938. phase = pf;
  939. return v * amount;
  940. }
  941. phase = pf;
  942. auto v = (float) pf;
  943. if (shape == 1)
  944. {
  945. v = v + v;
  946. if (1.0f < v)
  947. return (2.0f - v) * amount;
  948. }
  949. else if (shape != 2)
  950. {
  951. v = 0.5f < v ? 1.0f : 0.0f;
  952. return v * amount;
  953. }
  954. return v * amount;
  955. }
  956. void blockPhase (int n) noexcept
  957. {
  958. const auto d = (double) (n - 1) * inc + phase;
  959. phase = d - (double) (long long) d;
  960. }
  961. float fadeStep() noexcept
  962. {
  963. fade = (((off ? 0.0f : 1.0f) - fade) * 0.01f) + fade;
  964. return fade;
  965. }
  966. bool running() const noexcept { return ! off || 0.0001f <= fade; }
  967. void process (float* L, float* R, int n, float tempo) noexcept
  968. {
  969. if (! (1.0f <= fs && 1.0f <= tempo))
  970. return;
  971. const auto per = (rate * 60.0f) / tempo;
  972. active = 0;
  973. period = per;
  974. inc = per == 0.0f ? 0.0 : (double) (1.0f / (fs * per));
  975. switch (kind)
  976. {
  977. case 6: delay (L, R, n); break;
  978. case 4: lphp (L, R, n, false); break;
  979. case 5: lphp (L, R, n, true); break;
  980. case 7: reverb (L, R, n); break;
  981. case 0: dist (L, R, n); break;
  982. case 1: crush (L, R, n); break;
  983. case 2: flanger (L, R, n); break;
  984. case 9: trancer (L, R, n); break;
  985. case 3: phaser (L, R, n); break;
  986. case 8: panner (L, R, n); break;
  987. default: break;
  988. }
  989. if (eqOn)
  990. eq (L, R, n);
  991. }
  992. void dist (float* L, float* R, int n) noexcept
  993. {
  994. if (! running())
  995. return;
  996. active = 1;
  997. auto d = lfo() + x;
  998. d = clamp01 (d);
  999. blockPhase (n);
  1000. const auto d2 = d * d;
  1001. const auto kk = (d2 * d2) * 1024.0f;
  1002. const auto g = 0.5f / (1.0f / (kk + 1.0f));
  1003. const auto fc = powf32 (2.0f, (y * 4.28000020980835f) + 10.0f);
  1004. flt[0].design (fc, 0.99f, fs);
  1005. flt[1].design (fc, 0.99f, fs);
  1006. for (int i = 0; i < n; ++i)
  1007. {
  1008. auto fd = fadeStep();
  1009. auto v = L[i];
  1010. auto s = (v / ((std::fabs (v) * kk) + 1.0f)) * g;
  1011. L[i] = (flt[0].tick (s) * fd) + ((1.0f - fd) * v);
  1012. v = R[i];
  1013. fd = fade;
  1014. s = (v / ((std::fabs (v) * kk) + 1.0f)) * g;
  1015. R[i] = (flt[1].tick (s) * fd) + ((1.0f - fd) * v);
  1016. }
  1017. }
  1018. void crush (float* L, float* R, int n) noexcept
  1019. {
  1020. if (! running())
  1021. return;
  1022. active = 1;
  1023. auto c = (1.0f - x) + lfo();
  1024. c = std::max (c, 0.0f);
  1025. blockPhase (n);
  1026. c = std::min (c, 1.0f);
  1027. const auto hl = ((c * c) * 64.0f) + 3.0f;
  1028. const auto fc = powf32 (2.0f, (y * 4.28000020980835f) + 10.0f);
  1029. flt[0].design (fc, 0.99f, fs);
  1030. flt[1].design (fc, 0.99f, fs);
  1031. for (int i = 0; i < n; ++i)
  1032. {
  1033. auto h = hold;
  1034. float vL;
  1035. if (0.0f < h)
  1036. vL = holdL;
  1037. else
  1038. {
  1039. vL = L[i];
  1040. holdL = vL;
  1041. holdR = R[i];
  1042. h = hl;
  1043. }
  1044. hold = h - 1.0f;
  1045. auto fd = fadeStep();
  1046. L[i] = ((1.0f - fd) * L[i]) + (fd * flt[0].tick (vL));
  1047. fd = fade;
  1048. R[i] = ((1.0f - fd) * R[i]) + (fd * flt[1].tick (holdR));
  1049. }
  1050. }
  1051. void lphp (float* L, float* R, int n, bool high) noexcept
  1052. {
  1053. if (! running())
  1054. return;
  1055. active = 1;
  1056. auto v = lfo() + x;
  1057. const auto q = ((y * y) + (y * y)) + 1.0f;
  1058. v = clamp01 (v);
  1059. blockPhase (n);
  1060. Biquad* f = high ? hpf : flt;
  1061. const auto fc = high ? powf32 (2.0f, (v * 7.423999786376953f) + 5.0f) : powf32 (2.0f, (v * 8.279999732971191f) + 6.0f);
  1062. for (int k = 0; k < 8; ++k)
  1063. f[k].design (fc, q, fs);
  1064. for (int i = 0; i < n; ++i)
  1065. {
  1066. const auto fd = fadeStep();
  1067. auto t = f[0].tick (f[1].tick (f[2].tick (L[i])));
  1068. L[i] = ((1.0f - fd) * L[i]) + (fade * t);
  1069. t = f[4].tick (f[5].tick (f[6].tick (R[i])));
  1070. R[i] = ((1.0f - fade) * R[i]) + (fade * t);
  1071. }
  1072. }
  1073. void phaser (float* L, float* R, int n) noexcept
  1074. {
  1075. if (! running())
  1076. return;
  1077. active = 1;
  1078. const auto h = 0.5f / fs;
  1079. const auto lo = (float) ((double) h * 100.0), hi = (float) ((double) h * 40000.0);
  1080. const auto rng = hi - lo;
  1081. const auto fb = powf32 (y * 0.6000000238418579f, 0.3333333432674408f) * 0.9800000190734863f;
  1082. auto tgt = x * x;
  1083. tgt = std::max (tgt, 9.99999975e-06f);
  1084. if (xs < 0.0f)
  1085. xs = tgt;
  1086. for (int i = 0; i < n; ++i)
  1087. {
  1088. fadeStep();
  1089. const auto s = xs;
  1090. const auto lv = lfo();
  1091. auto w = ((s + ((lv * s) * 0.5f)) * rng) + lo;
  1092. w = std::min (w, 0.49900001287460327f);
  1093. if ((double) w < 0.001)
  1094. w = 0.00100000005f;
  1095. const auto a = (1.0f - w) / (w + 1.0f);
  1096. xs = (float) ((double) (tgt - s) * 0.001 + (double) s);
  1097. auto u = (L[i] - 9.99999997e-07f) + (fb * apfbL);
  1098. auto pr = a * u;
  1099. for (int k = 0; k < 6; ++k)
  1100. {
  1101. const auto t = apS[k] - pr;
  1102. pr = t * a;
  1103. apS[k] = pr + u;
  1104. u = t;
  1105. }
  1106. apfbL = u;
  1107. const auto outL = u;
  1108. u = (R[i] - 9.99999997e-07f) + (fb * apfbR);
  1109. pr = a * u;
  1110. for (int k = 6; k < 12; ++k)
  1111. {
  1112. const auto t = apS[k] - pr;
  1113. pr = t * a;
  1114. apS[k] = pr + u;
  1115. u = t;
  1116. }
  1117. apfbR = u;
  1118. L[i] = (outL * fade) + L[i];
  1119. R[i] = (u * fade) + R[i];
  1120. }
  1121. }
  1122. void trancer (float* L, float* R, int n) noexcept
  1123. {
  1124. if (off)
  1125. return;
  1126. active = 1;
  1127. if (! (0.0f < rate))
  1128. return;
  1129. const auto dep = 1.0f - (y * y);
  1130. const auto sh = (((1.0f - x) * (1.0f - x)) * 50.0f) + 1.0f;
  1131. for (int i = 0; i < n; ++i)
  1132. {
  1133. const auto p = phase;
  1134. float v;
  1135. if (0.009999999776482582 <= p)
  1136. {
  1137. v = (float) ((p - 0.009999999776482582) * 1.0101010003706326);
  1138. v = v * v;
  1139. v = (v * v) * sh;
  1140. v = std::min (v, 1.0f);
  1141. v = 1.0f - v;
  1142. }
  1143. else
  1144. v = (float) (p * 100.00000223517424);
  1145. hold = ((v - hold) * 0.01f) + hold;
  1146. const auto g = 1.0f - (hold * dep);
  1147. const auto q = p + inc;
  1148. phase = q - (double) (long long) q;
  1149. L[i] = g * L[i];
  1150. R[i] = g * R[i];
  1151. }
  1152. }
  1153. void delay (float* L, float* R, int n) noexcept
  1154. {
  1155. if (off && tail < 0)
  1156. return;
  1157. active = 1;
  1158. auto dt = period * 0.6666666865348816f;
  1159. if ((double) dt < 0.01)
  1160. dt = 0.00999999978f;
  1161. const auto y2 = y + y;
  1162. const auto lpv = std::min (y2, 1.0f);
  1163. const auto hpv = std::max (y2 - 1.0f, 0.0f);
  1164. const auto xx = x * x;
  1165. for (auto& d : dl)
  1166. {
  1167. d.lp = lpv * lpv;
  1168. d.hp = hpv * hpv;
  1169. d.fb = xx;
  1170. d.setTime ((double) dt);
  1171. }
  1172. const auto thr = 9.99999975e-05f;
  1173. for (int i = 0; i < n; ++i)
  1174. {
  1175. const auto fd = fadeStep();
  1176. const auto g = fd * xx;
  1177. dl[0].gain = g;
  1178. dl[1].gain = g;
  1179. const auto a = dl[0].tick (L[i]);
  1180. const auto b = dl[1].tick (R[i]);
  1181. if (thr < std::fabs (a) || thr < std::fabs (b) || thr < std::fabs (L[i]) || thr < std::fabs (R[i]))
  1182. tail = (int) (fs * 1.5f);
  1183. else
  1184. --tail;
  1185. L[i] = a + L[i];
  1186. R[i] = b + R[i];
  1187. }
  1188. }
  1189. void flanger (float* L, float* R, int n) noexcept
  1190. {
  1191. if (! running())
  1192. return;
  1193. active = 1;
  1194. const auto fbv = y * -0.5f, base = 1.0f - x;
  1195. for (auto& d : dl)
  1196. d.fb = fbv;
  1197. for (int i = 0; i < n; ++i)
  1198. {
  1199. const auto xl = L[i], xr = R[i];
  1200. const auto lv = lfo();
  1201. fadeStep();
  1202. const auto g = -fade;
  1203. for (auto& d : dl)
  1204. {
  1205. d.setTime ((double) (base - lv));
  1206. d.gain = g;
  1207. }
  1208. const auto a = dl[0].tick (dl[1].tick (dl[2].tick (xl)));
  1209. const auto b = dl[3].tick (dl[4].tick (dl[5].tick (xr)));
  1210. L[i] = a + L[i];
  1211. R[i] = b + R[i];
  1212. }
  1213. }
  1214. void panner (float* L, float* R, int n) noexcept
  1215. {
  1216. if (off && ! (0 < tail))
  1217. return;
  1218. active = 1;
  1219. for (auto& d : dl)
  1220. d.fb = 0.0f, d.gain = 1.0f;
  1221. for (int i = 0; i < n; ++i)
  1222. {
  1223. auto v = lfo() + x;
  1224. v = v + v;
  1225. auto a = v - 1.0f;
  1226. dl[0].setTime ((double) clamp01 (a));
  1227. dl[1].setTime ((double) clamp01 (1.0f - v));
  1228. a = a * y;
  1229. const auto gl = clamp01 ((1.0f - a) + (1.0f - a));
  1230. L[i] = dl[0].tick (gl * L[i]);
  1231. const auto gr = clamp01 ((a + a) + 2.0f);
  1232. R[i] = dl[1].tick (gr * R[i]);
  1233. }
  1234. }
  1235. void reverb (float* L, float* R, int n) noexcept // FUN_180100950
  1236. {
  1237. if (off && tail < 1)
  1238. return;
  1239. active = 1;
  1240. const auto tgt = off ? 0.0f : 1.0f;
  1241. for (int i = 0; i < n; ++i)
  1242. {
  1243. fade = ((tgt - fade) * 0.01f) + fade;
  1244. cl[(std::size_t) i] = fade * L[i];
  1245. cr[(std::size_t) i] = fade * R[i];
  1246. }
  1247. const auto x3 = ((x * x) * x) * 0.75f;
  1248. rev->setp (9, y * y);
  1249. rev->setp (8, x3);
  1250. rev->setp (12, (x3 * 0.30000001192092896f) + 0.5f);
  1251. rev->setp (2, 1.0f);
  1252. rev->setp (1, 0.10000000149011612f);
  1253. rev->setp (5, 1.0f);
  1254. rev->setp (4, 1.0f);
  1255. rev->setp (3, 0.0f);
  1256. rev->setp (10, 0.0f);
  1257. rev->setp (16, 1.0f);
  1258. rev->process (cl.data(), cr.data(), L, R, n);
  1259. const auto thr = 9.99999975e-05f;
  1260. const auto full = (int) (fs * 0.25f);
  1261. for (int i = 0; i < n; ++i)
  1262. if (thr < std::fabs (L[i]) || thr < std::fabs (R[i]))
  1263. tail = full;
  1264. else
  1265. --tail;
  1266. }
  1267. void eq (float* L, float* R, int n) noexcept
  1268. {
  1269. active = 1;
  1270. for (int b = 0; b < 5; ++b)
  1271. {
  1272. if (eqDirty[b])
  1273. {
  1274. eqDirty[b] = false;
  1275. const auto g = (eqGain[b] - 0.5f) * 24.0f;
  1276. eqL[b].design (g, fs);
  1277. eqR[b].design (g, fs);
  1278. }
  1279. for (int i = 0; i < n; ++i)
  1280. {
  1281. L[i] = eqL[b].tick (L[i]);
  1282. R[i] = eqR[b].tick (R[i]);
  1283. }
  1284. }
  1285. }
  1286. void setEq (const float* gains, bool on) noexcept
  1287. {
  1288. eqOn = on;
  1289. if (! on)
  1290. return;
  1291. for (int b = 0; b < 5; ++b)
  1292. {
  1293. const auto g = clamp01 (gains[b]);
  1294. if (g != eqGain[b])
  1295. eqDirty[b] = true;
  1296. eqGain[b] = g;
  1297. }
  1298. }
  1299. };
  1300. // ------------------------------------------------------------------------------------------------------- engine
  1301. struct Voice
  1302. {
  1303. bool active = false, released = false, rendered = false, flagRand = false;
  1304. int note = -1, notep = 0;
  1305. std::uint64_t counter = 0;
  1306. float vel = 0, pitch = 0;
  1307. float eAmp = 0, ampT = 0, ampC = 0, ampCd = 0, ampS = 0;
  1308. float e1 = 0, e1t = 0, e1c = 0, e1cd = 0, e2 = 0, e2t = 0, e2c = 0, e2cd = 0;
  1309. float lvl = 0, lvlT = 0;
  1310. float y[2][4] {};
  1311. float kt = 0, res = 0, amtE1 = 0, amtE2 = 0, amtL1 = 0, amtL2 = 0, lv1 = 0, lv2 = 0;
  1312. std::uint32_t ph1[16] {}, phm[16] {}, ph2[16] {}, ph3[16] {};
  1313. std::uint32_t inc1[16] {}, incm[16] {}, inc2[16] {}, inc3[16] {};
  1314. float g[8][16] {}; // rows: 1L 1R 2L 2R 3L 3R nL nR
  1315. int tab[3] { 511, 511, 511 };
  1316. };
  1317. class Engine
  1318. {
  1319. public:
  1320. double fs = 44100.0;
  1321. std::array<float, paramCount> P {};
  1322. std::shared_ptr<const Tables> tabs[3];
  1323. float velTab[128] {};
  1324. float velsrc = 0.0f, lastNote = -1.0f, bend = 0.0f;
  1325. std::uint64_t counter = 1;
  1326. float lfoPh[2] {}, lfoInc[2] {};
  1327. float cutS = -1.0f, cutRaw = 0.0f, modS = -1.0f;
  1328. float mgL = -1.0f, mgR = -1.0f;
  1329. int maxpoly = voiceCount, npoly2 = voiceCount;
  1330. float tempo = 140.0f;
  1331. bool playing = false;
  1332. double songpos = 0.0;
  1333. Rng rng;
  1334. std::vector<Voice> voices;
  1335. std::vector<Fx> fx;
  1336. bool fxOn = true;
  1337. bool lastActive = false;
  1338. // per-chunk state
  1339. int dstE1 = 0, dstE2 = 0, dstL1 = 0, dstL2 = 0, shpL1 = 0, shpL2 = 0, U = 1, ftype = 0, kind = 0;
  1340. std::uint32_t ph2ofs = 0;
  1341. float bufCut[maxChunk] {}, bufMod[maxChunk] {}, bufL1[maxChunk] {}, bufL2[maxChunk] {};
  1342. float ampb[maxChunk] {}, cutv[maxChunk] {}, lvlb[maxChunk] {}, modb[maxChunk] {}, vbL[maxChunk] {}, vbR[maxChunk] {};
  1343. void init (double rate, bool withFx)
  1344. {
  1345. fs = rate;
  1346. voices.assign (voiceCount, Voice {});
  1347. rng.s = 0;
  1348. // the constructor and the sample-rate set each reset all 99 voices (16 × 93 draws each); a rate other than the
  1349. // constructor's 44 100 Hz causes one more reset (§1.7)
  1350. const int nres = fs == 44100.0 ? 2 : 3;
  1351. rng.skip ((std::uint64_t) (nres - 1) * voiceCount * 16 * 93);
  1352. for (auto& v : voices)
  1353. resetVoice (v);
  1354. std::fill (std::begin (velTab), std::end (velTab), 0.0f);
  1355. velsrc = 0.0f, counter = 1, lastNote = -1.0f;
  1356. lfoPh[0] = lfoPh[1] = lfoInc[0] = lfoInc[1] = 0.0f;
  1357. cutS = -1.0f, cutRaw = 0.0f, modS = -1.0f, bend = 0.0f, mgL = mgR = -1.0f;
  1358. maxpoly = npoly2 = voiceCount;
  1359. playing = false, songpos = 0.0;
  1360. fxOn = withFx;
  1361. fx.clear();
  1362. if (withFx)
  1363. {
  1364. fx.resize (10);
  1365. for (int k = 0; k < 10; ++k)
  1366. fx[(std::size_t) k].init (k, fs);
  1367. }
  1368. lastActive = false;
  1369. }
  1370. float raw (int i) const noexcept { return i >= 0 && i < paramCount ? P[(std::size_t) i] : 0.0f; }
  1371. float par (int i) const noexcept // FUN_1800f50c0
  1372. {
  1373. auto v = raw (i);
  1374. const int rows[3][2] = { { 199, 198 }, { 202, 201 }, { 204, 203 } };
  1375. const float src[3] = { velsrc, 0.0f, 0.0f };
  1376. for (int r = 0; r < 3; ++r)
  1377. {
  1378. const auto d = raw (rows[r][0]);
  1379. if (d != 0.0f && matrixHit (i, i32 ((double) d)))
  1380. {
  1381. const auto a = raw (rows[r][1]);
  1382. v = (((a - 0.5f) + a) - 0.5f) * src[r] + v;
  1383. if (v <= 0.0f)
  1384. v = 0.0f;
  1385. if (1.0f <= v)
  1386. v = 1.0f;
  1387. }
  1388. }
  1389. return v;
  1390. }
  1391. int transposed (int note) const noexcept
  1392. {
  1393. auto n2 = note - i32 ((double) (par (2) * -48.0f + 24.0f));
  1394. n2 = n2 + (i32 ((double) (par (44) * -4.0f)) + 2) * -12;
  1395. return n2;
  1396. }
  1397. void noteOn (int note, int vel7) noexcept // FUN_1800ccd70 → FUN_1800f5250 / FUN_1800f53e0
  1398. {
  1399. velTab[note] = (float) vel7 * 0.007874015718698502f;
  1400. if (vel7 == 0)
  1401. {
  1402. noteOff (note);
  1403. return;
  1404. }
  1405. velsrc = velTab[note];
  1406. Voice* v = nullptr;
  1407. if (par (40) == 0.0f)
  1408. {
  1409. const auto lim = std::min (npoly2, maxpoly);
  1410. for (int k = 0; k < lim; ++k)
  1411. if (! voices[(std::size_t) k].active)
  1412. v = &voices[(std::size_t) k];
  1413. if (v == nullptr)
  1414. {
  1415. std::uint64_t best = 0xefffffffu;
  1416. for (int k = 0; k < lim; ++k)
  1417. {
  1418. const auto c = voices[(std::size_t) k].counter;
  1419. if (c < best)
  1420. v = &voices[(std::size_t) k];
  1421. if (best > c)
  1422. best = c;
  1423. }
  1424. }
  1425. if (v == nullptr)
  1426. return;
  1427. }
  1428. else
  1429. v = &voices[0];
  1430. v->note = note;
  1431. v->notep = transposed (note);
  1432. v->vel = velsrc;
  1433. v->counter = counter++;
  1434. start (*v);
  1435. lastNote = (float) v->notep;
  1436. }
  1437. void start (Voice& v) noexcept
  1438. {
  1439. v.active = true, v.released = false;
  1440. v.lv1 = v.lv2 = 0.0f;
  1441. const auto p8 = par (8);
  1442. v.lvlT = p8 * p8;
  1443. if (v.lvl < 0.0f)
  1444. v.lvl = v.lvlT;
  1445. const auto p5 = par (5), p4 = par (4);
  1446. const auto p5s = p5 * p5;
  1447. const auto dec = (double) (p5s * p5s * 50.0f) + 0.001;
  1448. const auto att = (float) (8.82 / (((double) (p4 * p4 * p4 * 50.0f) + 0.001) * fs));
  1449. v.ampCd = (float) (8.82 / (dec * fs));
  1450. v.ampS = par (6);
  1451. v.ampC = att;
  1452. v.ampT = 5.0f;
  1453. for (int e = 1; e <= 2; ++e)
  1454. {
  1455. const auto x = par (21 + e) * 0.800000011920929f + 0.20000000298023224f;
  1456. const auto a = par (19 + e);
  1457. const auto ca = (float) (8.82 / (((double) (a * a * a * a * a * 300.0f) + 0.001) * fs));
  1458. const auto cd = (float) (8.82 / (((double) (x * x * x * x * (x * 70.0f)) + 0.001) * fs));
  1459. if (e == 1)
  1460. v.e1t = 5.0f, v.e1c = ca, v.e1 = 0.0f, v.e1cd = cd;
  1461. else
  1462. v.e2t = 5.0f, v.e2c = ca, v.e2 = 0.0f, v.e2cd = cd;
  1463. }
  1464. if (par (11) != 0.0f && lastNote != -1.0f)
  1465. v.pitch = lastNote;
  1466. if (par (47) != 0.0f)
  1467. {
  1468. const auto m = par (46) * 4.0f;
  1469. for (int k = 0; k < 16; ++k)
  1470. {
  1471. const auto ph = phaseOfs (0, (float) k * m);
  1472. v.ph1[k] = v.phm[k] = v.ph2[k] = v.ph3[k] = ph;
  1473. }
  1474. v.flagRand = false;
  1475. }
  1476. else
  1477. v.flagRand = par (53) != 0.0f;
  1478. if (par (26) != 0.0f)
  1479. lfoPh[0] = 0.0f;
  1480. if (par (27) != 0.0f)
  1481. lfoPh[1] = 0.0f;
  1482. }
  1483. void noteOff (int note) noexcept // FUN_1800ccdb0 + FUN_1800f58e0
  1484. {
  1485. velTab[note] = 0.0f;
  1486. const auto x = (float) ((double) par (7) * 0.55 + 0.1);
  1487. const auto c = (float) (8.82 / (((double) (x * x * x * 25.0f) + 0.001) * fs));
  1488. for (auto& v : voices)
  1489. if (v.note == note && v.active)
  1490. v.ampC = c, v.released = true, v.ampT = 0.0f;
  1491. }
  1492. void resetVoice (Voice& v) noexcept // FUN_1800fe890
  1493. {
  1494. v.active = false, v.released = false, v.note = -1;
  1495. std::memset (v.y, 0, sizeof (v.y));
  1496. v.lvl = -1.0f, v.lvlT = 1.0f, v.eAmp = 0.0f, v.e1 = 0.0f, v.e2 = 0.0f;
  1497. for (int k = 0; k < 16; ++k)
  1498. {
  1499. const auto r = rng.next();
  1500. v.ph1[k] = r, v.phm[k] = r;
  1501. rng.skip (30);
  1502. v.ph2[k] = rng.next();
  1503. rng.skip (30);
  1504. v.ph3[k] = rng.next();
  1505. rng.skip (30);
  1506. }
  1507. }
  1508. bool anyActive() const noexcept
  1509. {
  1510. return std::any_of (voices.begin(), voices.end(), [] (const Voice& v) { return v.active; });
  1511. }
  1512. // FUN_1800f32f0 for one chunk of n samples (adds nothing: writes outL/outR)
  1513. void process (int n, const float (*LR)[2], const float* ptab, float* outL, float* outR) noexcept
  1514. {
  1515. std::fill (outL, outL + n, 0.0f);
  1516. std::fill (outR, outR + n, 0.0f);
  1517. const auto anyVoice = f5f00 (n, LR, ptab, outL, outR);
  1518. auto active = anyVoice;
  1519. if (fxOn)
  1520. {
  1521. auto act = anyVoice;
  1522. for (auto& f : fx)
  1523. {
  1524. if (! act)
  1525. f.off = true;
  1526. f.process (outL, outR, n, tempo);
  1527. act = act || f.active != 0;
  1528. }
  1529. active = act || active;
  1530. }
  1531. lastActive = active;
  1532. const auto vol = db2lin (raw (0) * 80.0f - 60.0f);
  1533. const auto pan = raw (1);
  1534. auto Lt = clamp01 (1.0f - pan) * vol;
  1535. auto Rt = clamp01 (pan) * vol;
  1536. Lt = std::max (Lt, 9.999999747378752e-05f);
  1537. Rt = std::max (Rt, 9.999999747378752e-05f);
  1538. if (active)
  1539. {
  1540. auto cl = mgL;
  1541. if (! (cl >= 0.0f))
  1542. mgL = Lt, cl = Lt;
  1543. const auto cr = mgR;
  1544. if (! (cr >= 0.0f))
  1545. mgL = Rt, cl = Rt;
  1546. if (! (cl >= 9.999999747378752e-05f))
  1547. mgL = 0.0f;
  1548. if (! (cr >= 9.999999747378752e-05f))
  1549. mgR = 0.0f;
  1550. auto gl = mgL, gr = mgR;
  1551. const auto c = 0.009999999776482582f;
  1552. for (int i = 0; i < n; ++i)
  1553. {
  1554. gl = (Lt - gl) * c + gl;
  1555. gr = (Rt - gr) * c + gr;
  1556. outL[i] = gl * outL[i];
  1557. outR[i] = gr * outR[i];
  1558. }
  1559. mgL = gl, mgR = gr;
  1560. }
  1561. else
  1562. mgL = Lt, mgR = Rt;
  1563. }
  1564. void pushFx() noexcept // effect-slot parameters (FUN_1800f5f00)
  1565. {
  1566. if (! fxOn)
  1567. return;
  1568. for (int k = 0; k < 10; ++k)
  1569. {
  1570. auto& f = fx[(std::size_t) k];
  1571. const auto on = P[(std::size_t) (147 + k)] != 0.0f;
  1572. f.off = ! on;
  1573. if (! on)
  1574. continue;
  1575. f.x = clamp01 (P[(std::size_t) (117 + k)]);
  1576. f.y = clamp01 (P[(std::size_t) (127 + k)]);
  1577. const auto a = (P[(std::size_t) (177 + k)] + P[(std::size_t) (177 + k)]) - 1.0f;
  1578. auto amt = a * a;
  1579. if (a < 0.0f)
  1580. amt = -amt;
  1581. f.amount = amt;
  1582. const auto shv = (double) (P[(std::size_t) (157 + k)] * 3.0f);
  1583. f.shape = shv != shv ? 0 : (int) std::clamp (shv, 0.0, 4.0);
  1584. const auto riv = (double) (P[(std::size_t) (137 + k)] * 9.0f);
  1585. auto r = rateSteps[riv != riv ? 0 : (int) std::clamp (riv, 0.0, 8.0)];
  1586. if (P[(std::size_t) (167 + k)] != 0.0f)
  1587. r = r * 4.0f;
  1588. f.rate = std::max (r, 0.0f);
  1589. if (playing) // song-time LFO phase (engine flag +0x4470)
  1590. {
  1591. const auto d = f.rate <= 0.0f ? f.phase : (0.25 / (double) f.rate) * songpos * 4.0;
  1592. f.phase = d - (double) (long long) d;
  1593. }
  1594. }
  1595. fx[9].setEq (P.data() + 207, P[206] != 0.0f);
  1596. }
  1597. bool f5f00 (int n, const float (*LR)[2], const float* ptab, float* outL, float* outR) noexcept
  1598. {
  1599. for (int l = 0; l < 2; ++l)
  1600. {
  1601. const auto lock = playing && par (26 + l) == 0.0f;
  1602. const auto r = lfoRate (par (34 + l), par (36 + l), tempo, lock, lock ? (float) (songpos * 4.0) : 0.0f);
  1603. if (r.hasPhase)
  1604. lfoPh[l] = r.phase;
  1605. lfoInc[l] = (float) ((double) r.f / fs);
  1606. }
  1607. pushFx();
  1608. bool any = false;
  1609. for (auto& v : voices)
  1610. {
  1611. v.rendered = false;
  1612. any = any || v.active;
  1613. }
  1614. if (! any)
  1615. return false;
  1616. dstE1 = i32 ((double) (par (24) * 3.0f));
  1617. dstE2 = i32 ((double) (par (25) * 3.0f));
  1618. dstL1 = i32 ((double) (par (38) * 3.0f));
  1619. dstL2 = i32 ((double) (par (39) * 3.0f));
  1620. shpL1 = i32 ((double) (par (32) * 3.0f));
  1621. shpL2 = i32 ((double) (par (33) * 3.0f));
  1622. f68f0 (LR, ptab);
  1623. return f7530 (n, outL, outR);
  1624. }
  1625. void f68f0 (const float (*LR)[2], const float* ptab) noexcept
  1626. {
  1627. U = std::clamp (1 - i32 ((double) (par (41) * -15.0f)), 1, 16);
  1628. auto spread = (float) ((double) par (42) * 0.4);
  1629. spread = spread * spread;
  1630. auto a = par (48);
  1631. a = a * a;
  1632. auto b = par (49);
  1633. b = b * b;
  1634. auto nz = par (45);
  1635. nz = nz * nz;
  1636. const auto one = 1.0f;
  1637. const auto x1 = (one - a) * (one - b) * (one - nz);
  1638. const auto x2 = (one - b) * a * (one - nz);
  1639. const auto x3 = (one - nz) * b;
  1640. cutRaw = raw (12);
  1641. if (cutS < 0.0f)
  1642. cutS = cutRaw;
  1643. ftype = i32 ((double) (par (15) + par (15)));
  1644. ph2ofs = phaseOfs (0, par (52) + par (52));
  1645. kind = i32 ((double) (par (51) + par (51)));
  1646. for (auto& v : voices)
  1647. {
  1648. if (! v.active)
  1649. continue;
  1650. velsrc = v.vel;
  1651. auto amt = par (16);
  1652. amt = amt * amt;
  1653. v.amtE1 = par (18) != 0.0f ? -amt : amt;
  1654. amt = par (17);
  1655. amt = amt * amt;
  1656. v.amtE2 = par (19) != 0.0f ? -amt : amt;
  1657. amt = par (28);
  1658. amt = amt * amt;
  1659. v.amtL1 = par (30) != 0.0f ? -amt : amt;
  1660. amt = par (29);
  1661. amt = amt * amt;
  1662. v.amtL2 = par (31) != 0.0f ? -amt : amt;
  1663. if (! (v.ampT != 0.0f || 9.999999747378752e-05f <= v.eAmp))
  1664. {
  1665. resetVoice (v);
  1666. continue;
  1667. }
  1668. const auto p14 = par (14);
  1669. v.kt = p14 == 0.0f ? 0.0f : p14 * (v.pitch - 57.0f) * 0.0078125f;
  1670. auto lv = db2lin (par (8) * 50.0f - 25.0f) * 200.0f;
  1671. if (par (200) != 0.0f)
  1672. lv = lv * v.vel;
  1673. v.lvlT = lv;
  1674. v.kt = (par (12) - cutRaw) + v.kt;
  1675. v.res = par (13);
  1676. v.notep = transposed (v.note);
  1677. if (par (11) == 0.0f)
  1678. v.pitch = (float) v.notep;
  1679. const auto e1o = v.e1, e2o = v.e2, l1o = v.lv1, l2o = v.lv2;
  1680. auto pv = (float) i32 ((double) (par (205) * 12.0f)) * bend + v.pitch;
  1681. const auto de = dstE1, d2 = dstE2, dl1 = dstL1, dl2 = dstL2;
  1682. auto fb = [] (bool c) { return c ? 1.0f : 0.0f; };
  1683. auto t = fb (d2 == 2) * v.amtE2 * e2o + fb (de == 2) * v.amtE1 * e1o;
  1684. t = t * 24.0f;
  1685. auto u = fb (dl2 == 2) * l2o + fb (dl1 == 2) * l1o;
  1686. u = u * 12.0f;
  1687. pv = pv + (t + u);
  1688. pv = pv - 57.0f;
  1689. pv = pv + ptab[v.note];
  1690. auto ofs = [&] (int cents, int semi) {
  1691. return (float) ((double) i32 ((double) (par (cents) * 100.0f - 50.0f)) * 0.01 + (double) (float) i32 ((double) (par (semi) * 72.0f - 36.0f)));
  1692. };
  1693. const auto o1 = ofs (56, 55), o2 = ofs (58, 57);
  1694. float m;
  1695. if (kind == 0)
  1696. {
  1697. m = par (50) + fb (de == 1) * v.amtE1 * e1o;
  1698. m = m + ((fb (dl2 == 1) * l2o + fb (dl1 == 1) * l1o) + fb (d2 == 1) * v.amtE2 * e2o);
  1699. m = clamp01 (m) * 3.0f + 1.0f;
  1700. }
  1701. else
  1702. m = 1.0f;
  1703. const auto k12 = 0.0833333358168602f;
  1704. const auto base = powf32 (2.0f, pv * k12) * 220.0f;
  1705. const auto f1 = powf32 (2.0f, o1 * k12) * base;
  1706. const auto f2 = powf32 (2.0f, o2 * k12) * base;
  1707. const auto o3 = ofs (60, 59);
  1708. const auto f3 = powf32 (2.0f, o3 * k12) * base;
  1709. const auto fsync = m * f1;
  1710. v.tab[0] = tableFor (fsync, fs), v.tab[1] = tableFor (f2, fs), v.tab[2] = tableFor (f3, fs);
  1711. const auto st = par (43);
  1712. const auto norm = 0.004999999888241291f / ((float) (U - 1) * 0.15000000596046448f + one);
  1713. const auto Lg = LR[v.note][0], Rg = LR[v.note][1];
  1714. for (int k = 0; k < 16; ++k)
  1715. {
  1716. auto det = one;
  1717. if (U > 1)
  1718. det = powf32 (2.0f, ((float) k / (float) (U - 1) - 0.5f) * spread);
  1719. v.inc1[k] = phaseInc (fsync * det, fs);
  1720. v.incm[k] = phaseInc (det * f1, fs);
  1721. v.inc2[k] = phaseInc (det * f2, fs);
  1722. v.inc3[k] = phaseInc (det * f3, fs);
  1723. auto s = (k & 1) == 0 ? one : -1.0f;
  1724. if ((U & 1) && k == 0)
  1725. s = 0.0f;
  1726. s = s * st;
  1727. const auto gl = clamp01 (one - s) * Lg;
  1728. const auto gr = clamp01 (s + one) * Rg;
  1729. v.g[0][k] = gl * x1 * norm, v.g[1][k] = gr * x1 * norm;
  1730. v.g[2][k] = gl * x2 * norm, v.g[3][k] = gr * x2 * norm;
  1731. v.g[4][k] = gl * x3 * norm, v.g[5][k] = gr * x3 * norm;
  1732. v.g[6][k] = gl * nz * norm, v.g[7][k] = gr * nz * norm;
  1733. if (v.flagRand)
  1734. {
  1735. const auto r = (std::uint32_t) (((std::uint64_t) rng.next() + 1) & 0x1ff);
  1736. v.ph1[k] = r * v.inc1[k];
  1737. v.ph2[k] = r * v.inc2[k];
  1738. v.phm[k] = v.ph1[k];
  1739. }
  1740. }
  1741. v.flagRand = false;
  1742. }
  1743. }
  1744. // FUN_1800f7530 (envelopes, glide, level, modulation, oscillators) + the ladder of FUN_1800f32f0 per voice
  1745. bool f7530 (int n, float* outL, float* outR) noexcept
  1746. {
  1747. const auto inv2 = par (54) != 0.0f;
  1748. auto sl = par (10);
  1749. sl = sl * sl * sl;
  1750. const auto gc = (float) (8.82 / (((double) (sl + sl) + 0.001) * fs));
  1751. auto m = par (50);
  1752. m = m * m;
  1753. if (modS < 0.0f)
  1754. modS = m;
  1755. const auto c01 = 0.009999999776482582f;
  1756. bool first = true, rendered = false;
  1757. for (auto& v : voices)
  1758. {
  1759. if (! v.active)
  1760. continue;
  1761. if (first)
  1762. {
  1763. auto cs = cutS, ms = modS, p1 = lfoPh[0], p2 = lfoPh[1];
  1764. for (int i = 0; i < n; ++i)
  1765. {
  1766. cs = (cutRaw - cs) * c01 + cs;
  1767. bufCut[i] = cs;
  1768. ms = (m - ms) * c01 + ms;
  1769. bufMod[i] = ms;
  1770. bufL1[i] = lfoWave (shpL1, p1);
  1771. auto t = lfoInc[0] + p1;
  1772. p1 = t - (float) i32 ((double) t);
  1773. bufL2[i] = lfoWave (shpL2, p2);
  1774. t = lfoInc[1] + p2;
  1775. p2 = t - (float) i32 ((double) t);
  1776. }
  1777. cutS = cs, modS = ms, lfoPh[0] = p1, lfoPh[1] = p2;
  1778. first = false;
  1779. }
  1780. v.rendered = true;
  1781. rendered = true;
  1782. renderVoice (v, n, inv2, gc, c01);
  1783. filter (v, n, outL, outR);
  1784. }
  1785. return rendered;
  1786. }
  1787. void renderVoice (Voice& v, int n, bool inv2, float gc, float c01) noexcept
  1788. {
  1789. auto e1 = v.e1, e1t = v.e1t, e1c = v.e1c, e2 = v.e2, e2t = v.e2t, e2c = v.e2c;
  1790. auto ea = v.eAmp, eat = v.ampT, eac = v.ampC;
  1791. auto lv = v.lvl;
  1792. const auto lvt = v.lvlT;
  1793. const auto de = dstE1, d2 = dstE2, dl1 = dstL1, dl2 = dstL2;
  1794. const auto a1 = v.amtE1, a2 = v.amtE2, al1 = v.amtL1, al2 = v.amtL2;
  1795. auto pitch = v.pitch;
  1796. const auto tgt = (float) v.notep;
  1797. float x1 = 0.0f, x2 = 0.0f;
  1798. for (int i = 0; i < n; ++i)
  1799. {
  1800. e1 = e1 + (e1t - e1) * e1c;
  1801. e2 = e2 + (e2t - e2) * e2c;
  1802. ea = ea + (eat - ea) * eac;
  1803. lv = lv + (lvt - lv) * c01;
  1804. pitch = (tgt - pitch) * gc + pitch;
  1805. if (i32 ((double) e1) != 0)
  1806. e1c = v.e1cd, e1 = 1.0f, e1t = 0.0f;
  1807. if (i32 ((double) e2) != 0)
  1808. e2c = v.e2cd, e2 = 1.0f, e2t = 0.0f;
  1809. if (i32 ((double) ea) != 0)
  1810. eac = v.ampCd, eat = v.ampS, ea = 1.0f;
  1811. lvlb[i] = lv;
  1812. x1 = bufL1[i] * al1;
  1813. if (de == 3)
  1814. x1 = x1 * e1;
  1815. x2 = bufL2[i] * al2;
  1816. if (d2 == 3)
  1817. x2 = x2 * e2;
  1818. if (kind == 1 || kind == 2)
  1819. {
  1820. auto mm = bufMod[i];
  1821. if (de == 1)
  1822. mm = mm + e1 * a1;
  1823. if (d2 == 1)
  1824. mm = mm + e2 * a2;
  1825. if (dl1 == 1)
  1826. mm = mm + x1;
  1827. if (dl2 == 1)
  1828. mm = mm + x2;
  1829. if (mm <= 0.0f)
  1830. mm = 0.0f;
  1831. modb[i] = mm * (kind == 1 ? 15.0f : 3.0f);
  1832. }
  1833. else
  1834. modb[i] = 0.0f;
  1835. auto aa = ea * ea;
  1836. if (dl1 == 3)
  1837. aa = aa + x1;
  1838. if (dl2 == 3)
  1839. aa = aa + x2;
  1840. if (aa <= 0.0f)
  1841. aa = 0.0f;
  1842. ampb[i] = aa;
  1843. auto cc = bufCut[i] + v.kt;
  1844. if (de == 0)
  1845. cc = cc + e1 * a1;
  1846. if (d2 == 0)
  1847. cc = cc + e2 * a2;
  1848. if (dl1 == 0)
  1849. cc = cc + x1;
  1850. if (dl2 == 0)
  1851. cc = cc + x2;
  1852. cutv[i] = cc;
  1853. }
  1854. v.lv1 = x1, v.lv2 = x2;
  1855. v.e1 = e1, v.e1t = e1t, v.e1c = e1c;
  1856. v.e2 = e2, v.e2t = e2t, v.e2c = e2c;
  1857. v.eAmp = ea, v.ampT = eat, v.ampC = eac;
  1858. v.lvl = lv, v.pitch = pitch;
  1859. // oscillators: unison slot outer, samples inner; four draws per sample (osc2, osc1, osc3, noise)
  1860. const auto* T1 = tabs[0]->data() + (std::size_t) v.tab[0] * tableSize;
  1861. const auto* T2 = tabs[1]->data() + (std::size_t) v.tab[1] * tableSize;
  1862. const auto* T3 = tabs[2]->data() + (std::size_t) v.tab[2] * tableSize;
  1863. std::fill (vbL, vbL + n, 0.0f);
  1864. std::fill (vbR, vbR + n, 0.0f);
  1865. for (int u = 0; u < U; ++u)
  1866. {
  1867. auto ph1 = v.ph1[u], phm = v.phm[u], ph2 = v.ph2[u], ph3 = v.ph3[u];
  1868. const auto inc1 = v.inc1[u], incm = v.incm[u], inc2 = v.inc2[u], inc3 = v.inc3[u];
  1869. const auto g0 = v.g[0][u], g1 = v.g[1][u], g2 = v.g[2][u], g3 = v.g[3][u];
  1870. const auto g4 = v.g[4][u], g5 = v.g[5][u], g6 = v.g[6][u], g7 = v.g[7][u];
  1871. for (int i = 0; i < n; ++i)
  1872. {
  1873. const auto r0 = rng.next(), r1 = rng.next(), r2 = rng.next(), r3 = rng.next();
  1874. auto o2 = T2[(((r0 & 0x3fffffu) + (ph2 + ph2ofs)) >> 22)];
  1875. if (inv2)
  1876. o2 = -o2;
  1877. float o1;
  1878. if (kind == 1)
  1879. {
  1880. const auto xx = (double) (o2 * modb[i]) * 4194303.0 + 268435392.0;
  1881. const auto idx = (std::uint32_t) (std::int64_t) std::clamp (xx, -9.0e18, 9.0e18) * 256u + ph1;
  1882. o1 = T1[((r1 & 0x3fffffu) + idx) >> 22];
  1883. ph1 += inc1;
  1884. }
  1885. else if (kind == 0)
  1886. {
  1887. const auto pmNew = phm + incm;
  1888. if (pmNew < phm)
  1889. ph1 = pmNew;
  1890. phm = pmNew;
  1891. o1 = T1[((r1 & 0x3fffffu) + ph1) >> 22];
  1892. ph1 += inc1;
  1893. }
  1894. else
  1895. {
  1896. o1 = T1[((r1 & 0x3fffffu) + ph1) >> 22];
  1897. if (kind == 2)
  1898. o1 = o1 * (1.0f - o2 * modb[i]);
  1899. ph1 += inc1;
  1900. }
  1901. const auto o3 = T3[((r2 & 0x3fffffu) + ph3) >> 22];
  1902. const auto nzv = ((float) r3 * 2.3283064365386963e-10f) - 0.5f;
  1903. auto xl = nzv * g6 + vbL[i];
  1904. xl = o3 * g4 + xl;
  1905. vbL[i] = xl + (o1 * g0 + o2 * g2);
  1906. auto xr = nzv * g7 + vbR[i];
  1907. xr = xr + o3 * g5;
  1908. vbR[i] = xr + (o1 * g1 + o2 * g3);
  1909. ph2 += inc2;
  1910. ph3 += inc3;
  1911. }
  1912. v.ph1[u] = ph1, v.phm[u] = phm, v.ph2[u] = ph2, v.ph3[u] = ph3;
  1913. }
  1914. for (int i = 0; i < n; ++i)
  1915. {
  1916. vbL[i] = vbL[i] * ampb[i];
  1917. vbR[i] = vbR[i] * ampb[i];
  1918. }
  1919. }
  1920. void filter (Voice& v, int n, float* outL, float* outR) noexcept
  1921. {
  1922. const auto& ct = cutoffTable();
  1923. const auto ft = ftype & 0xff;
  1924. const auto res = v.res;
  1925. const auto c004 = 0.03999999910593033f, one = 1.0f;
  1926. for (int lane = 0; lane < 2; ++lane)
  1927. {
  1928. const auto* buf = lane == 0 ? vbL : vbR;
  1929. auto* out = lane == 0 ? outL : outR;
  1930. auto y1 = v.y[lane][0], y2 = v.y[lane][1], y3 = v.y[lane][2], y4 = v.y[lane][3];
  1931. for (int i = 0; i < n; ++i)
  1932. {
  1933. const auto cx = (double) (cutv[i] * 65536.0f);
  1934. const auto idx = cx != cx ? 0 : (std::size_t) std::clamp (std::trunc (cx), 0.0, 65535.0);
  1935. const auto g = ct.g[idx];
  1936. const auto kr = ct.k[idx] * res;
  1937. const auto x = buf[i];
  1938. float s = 0.0f;
  1939. for (int it = 0; it < 4; ++it)
  1940. {
  1941. s = x - kr * y4;
  1942. s = s / (s * s * c004 + one);
  1943. y1 = y1 + (s - y1) * g;
  1944. y2 = y2 + (y1 - y2) * g;
  1945. y3 = y3 + (y2 - y3) * g;
  1946. y4 = y4 + (y3 - y4) * g;
  1947. }
  1948. if (ft == 0)
  1949. out[i] = y4 * lvlb[i] + out[i];
  1950. else if (ft == 1)
  1951. out[i] = (y3 - y4) * lvlb[i] + out[i];
  1952. else if (ft == 2)
  1953. out[i] = (s - y4) * lvlb[i] + out[i];
  1954. }
  1955. v.y[lane][0] = y1, v.y[lane][1] = y2, v.y[lane][2] = y3, v.y[lane][3] = y4;
  1956. }
  1957. }
  1958. };
  1959. // ------------------------------------------------------------------------------------------------------- wrapper
  1960. void lawA (double pan, double vol, float& l, float& r) noexcept
  1961. {
  1962. const auto t = (1.0 + std::clamp (pan, -1.0, 1.0)) * 0.78539816339744831;
  1963. l = (float) (vol * std::cos (t));
  1964. r = (float) (vol * std::sin (t));
  1965. }
  1966. int velFromInitVol (double iv) noexcept // CFLWrapper TriggerVoice (round 28 §4.2)
  1967. {
  1968. const auto x = (float) iv * 10.0f + 1.0f;
  1969. const auto v = (float) std::log ((double) x) * 0.3284587264060974f * 127.0f;
  1970. return std::clamp (i32 ((double) v), 0, 127);
  1971. }
  1972. // The FL wrapper (CFLWrapper): held notes with their FinalLevels, per-note tables, Gen_Render chunking, host-voice kill.
  1973. class Plugin
  1974. {
  1975. public:
  1976. struct Held
  1977. {
  1978. bool on = false;
  1979. double fvol = 1.0, fpan = 0.0, pitchCents = 0.0; // Final.Vol, Final.Pan, Final.Pitch − 100·(key − 60)
  1980. };
  1981. Engine eng;
  1982. Held held[128];
  1983. float LR[128][2] {};
  1984. float ptab[128] {};
  1985. float chunkL[maxChunk] {}, chunkR[maxChunk] {};
  1986. void init (double fs, bool withFx)
  1987. {
  1988. eng.init (fs, withFx);
  1989. for (auto& h : held)
  1990. h = Held {};
  1991. }
  1992. void noteOn (int key, double initVol, double fvol, double fpan, double pitchCents) noexcept
  1993. {
  1994. key = std::clamp (key, 0, 127);
  1995. if (held[key].on)
  1996. eng.noteOff (key);
  1997. held[key] = { true, fvol, fpan, pitchCents };
  1998. eng.noteOn (key, velFromInitVol (initVol));
  1999. }
  2000. void noteOff (int key) noexcept
  2001. {
  2002. if (key >= 0 && key < 128)
  2003. eng.noteOff (key);
  2004. }
  2005. void setLevels (int key, double fvol, double fpan) noexcept
  2006. {
  2007. if (key >= 0 && key < 128 && held[key].on)
  2008. held[key].fvol = fvol, held[key].fpan = fpan;
  2009. }
  2010. void tables() noexcept // FUN_1800c91b0
  2011. {
  2012. float cl = 0.0f, cr = 0.0f, cp = 0.0f;
  2013. bool first = true;
  2014. for (int i = 0; i < 128; ++i)
  2015. {
  2016. if (held[i].on)
  2017. {
  2018. const auto& h = held[i];
  2019. lawA (h.fpan, h.fvol, cl, cr);
  2020. const auto fp = 100.0 * i - 6000.0 + h.pitchCents;
  2021. cp = (float) ((double) (float) fp * 0.01 - (double) i + 60.0);
  2022. if (first)
  2023. {
  2024. for (int j = 0; j < i; ++j)
  2025. LR[j][0] = cl, LR[j][1] = cr, ptab[j] = cp;
  2026. first = false;
  2027. }
  2028. }
  2029. LR[i][0] = cl, LR[i][1] = cr, ptab[i] = cp;
  2030. }
  2031. }
  2032. bool idle() const noexcept
  2033. {
  2034. if (eng.lastActive || eng.anyActive())
  2035. return false;
  2036. return std::none_of (std::begin (held), std::end (held), [] (const Held& h) { return h.on; });
  2037. }
  2038. // one Gen_Render of n samples; songBeats ≥ 0 while the transport plays (song position at the first sample)
  2039. void render (float* outL, float* outR, int n, double songBeats) noexcept
  2040. {
  2041. tables();
  2042. auto tpos = songBeats;
  2043. eng.playing = songBeats >= 0.0;
  2044. for (int c0 = 0; c0 < n; c0 += maxChunk)
  2045. {
  2046. const auto cn = std::min (maxChunk, n - c0);
  2047. if (eng.playing)
  2048. eng.songpos = tpos;
  2049. eng.process (cn, LR, ptab, chunkL, chunkR);
  2050. if (eng.playing)
  2051. tpos = tpos + (double) eng.tempo * 0.016666666666666666 * (double) cn * (1.0 / eng.fs);
  2052. for (int i = 0; i < cn; ++i)
  2053. outL[c0 + i] += chunkL[i], outR[c0 + i] += chunkR[i];
  2054. }
  2055. // kill host voices whose engine voices ended and whose key is no longer held
  2056. for (int key = 0; key < 128; ++key)
  2057. if (held[key].on && eng.velTab[key] == 0.0f
  2058. && std::none_of (eng.voices.begin(), eng.voices.end(), [key] (const Voice& v) { return v.active && v.note == key; }))
  2059. held[key].on = false;
  2060. }
  2061. };
  2062. // ------------------------------------------------------------------------------------------------------- LYNX front-end
  2063. //
  2064. // Levels (§1.11, REA_rodada28 §4.1). FL hands GMS Final.Vol = chan.Vol (the wrapper switches "link velocity" off),
  2065. // Final.Pan = note pan + chan.Pan and Final.Pitch = 100·(key − 60) + channel pitch; the wrapper turns them into the
  2066. // per-note tables T_L = Vol·cos((1 + pan)π/4), T_R = Vol·sin(…) and T = Final.Pitch/100 − key + 60. LYNX's channel
  2067. // gains are gl = √2·V·cos((1 + p)π/4), gr = √2·V·sin(…), so V and p are recovered exactly and the channel volume/pan
  2068. // are not applied a second time. Velocity: Init.Vol = (11^(1.28·vel) − 1)/10 from LYNX's velocity (FL velocity/128,
  2069. // round 28 inference), then the wrapper's MIDI velocity. The tempo is the song tempo; the max-polyphony event is
  2070. // taken as value 0 (99 voices, FL's value is not confirmed). While the transport plays, the song position (beats)
  2071. // locks the synced LFOs and the effect LFOs (§1.8, §1.13).
  2072. // Random draws: one LCG per instance, started like a freshly created plugin; FL's GMS shares one process-global
  2073. // generator between all its instances, so noise, dither and the random phases of fresh voices differ from any given
  2074. // FL render by design.
  2075. struct Setup
  2076. {
  2077. std::array<float, paramCount> P {};
  2078. std::shared_ptr<const Tables> tabs[3];
  2079. };
  2080. class GmsInstrument final : public Instrument
  2081. {
  2082. public:
  2083. GmsInstrument()
  2084. {
  2085. setup = new Setup();
  2086. for (auto& t : setup->tabs)
  2087. t = zeroTables();
  2088. plugin.init (44100.0, true);
  2089. applySetup();
  2090. }
  2091. ~GmsInstrument() override
  2092. {
  2093. delete incoming.exchange (nullptr);
  2094. delete retired.exchange (nullptr);
  2095. delete setup;
  2096. }
  2097. void prepare (double sr) noexcept override
  2098. {
  2099. take();
  2100. rate = sr;
  2101. plugin.init (sr, true);
  2102. applySetup();
  2103. lastTicks = -1.0;
  2104. }
  2105. void configure (const ChannelDef& channel, double bpm) noexcept override
  2106. {
  2107. delete retired.exchange (nullptr);
  2108. pitchCents.store (channel.sampler.pitch * 100.0f);
  2109. tempo.store (bpm > 0.0 ? (float) bpm : 140.0f);
  2110. if (channel.pluginState == state && channel.samplePath == wavesDir)
  2111. return;
  2112. state = channel.pluginState;
  2113. wavesDir = channel.samplePath;
  2114. auto s = std::make_unique<Setup>();
  2115. if (auto p = parseState (state))
  2116. {
  2117. s->P = p->P;
  2118. for (int k = 0; k < 3; ++k)
  2119. s->tabs[k] = waveTables (wavesDir, p->waves[(std::size_t) k]);
  2120. }
  2121. else
  2122. for (auto& t : s->tabs)
  2123. t = zeroTables();
  2124. delete incoming.exchange (s.release());
  2125. }
  2126. void setNextNoteExpression (float pan, float cents, bool) noexcept override
  2127. {
  2128. nextPan = pan;
  2129. nextCents = cents;
  2130. }
  2131. void noteOn (int key, float velocity) noexcept override
  2132. {
  2133. take();
  2134. const auto vel = std::clamp ((double) velocity, 0.0, 1.0);
  2135. const auto initVol = (std::pow (11.0, 1.28 * vel) - 1.0) / 10.0;
  2136. const auto k = std::clamp (key, 0, 127);
  2137. notePan[k] = nextPan;
  2138. plugin.noteOn (k, initVol, channelVol, std::clamp ((double) nextPan + channelPan, -1.0, 1.0), (double) pitchCents.load() + (double) nextCents);
  2139. nextPan = 0.0f;
  2140. nextCents = 0.0f;
  2141. }
  2142. void noteOff (int key) noexcept override
  2143. {
  2144. take();
  2145. plugin.noteOff (key);
  2146. }
  2147. void allNotesOff() noexcept override
  2148. {
  2149. for (int k = 0; k < 128; ++k)
  2150. if (plugin.held[k].on)
  2151. plugin.noteOff (k);
  2152. }
  2153. bool idle() const noexcept override { return plugin.idle(); }
  2154. void setSongPosition (double ticks) noexcept override
  2155. {
  2156. // the transport plays when the position moves between blocks
  2157. playing = lastTicks >= 0.0 && ticks != lastTicks;
  2158. lastTicks = ticks;
  2159. songBeats = ticks / (double) ppq;
  2160. }
  2161. void render (float* left, float* right, int n, float gl, float gr) noexcept override
  2162. {
  2163. take();
  2164. plugin.eng.tempo = tempo.load();
  2165. const auto vol = std::sqrt (((double) gl * gl + (double) gr * gr) * 0.5);
  2166. auto pan = vol > 0.0 ? std::atan2 ((double) gr, (double) gl) * (4.0 / 3.141592653589793) - 1.0 : 0.0;
  2167. if (std::fabs (pan) < 1e-9)
  2168. pan = 0.0;
  2169. if (vol != channelVol || pan != channelPan)
  2170. {
  2171. channelVol = vol, channelPan = pan;
  2172. for (int k = 0; k < 128; ++k)
  2173. plugin.setLevels (k, vol, std::clamp ((double) notePan[k] + pan, -1.0, 1.0));
  2174. }
  2175. if (n <= 0)
  2176. return;
  2177. plugin.render (left, right, n, playing ? songBeats : -1.0);
  2178. }
  2179. private:
  2180. void applySetup() noexcept
  2181. {
  2182. plugin.eng.P = setup->P;
  2183. for (int k = 0; k < 3; ++k)
  2184. plugin.eng.tabs[k] = setup->tabs[k];
  2185. }
  2186. void take() noexcept
  2187. {
  2188. if (retired.load() != nullptr)
  2189. return;
  2190. if (auto* s = incoming.exchange (nullptr))
  2191. {
  2192. retired.store (setup);
  2193. setup = s;
  2194. applySetup();
  2195. }
  2196. }
  2197. Plugin plugin;
  2198. Setup* setup = nullptr;
  2199. std::atomic<Setup*> incoming { nullptr }, retired { nullptr };
  2200. std::atomic<float> pitchCents { 0.0f }, tempo { 140.0f };
  2201. std::vector<std::uint8_t> state;
  2202. std::string wavesDir;
  2203. double rate = 44100.0, channelVol = 1.0, channelPan = 0.0, lastTicks = -1.0, songBeats = 0.0;
  2204. bool playing = false;
  2205. float notePan[128] {};
  2206. float nextPan = 0.0f, nextCents = 0.0f;
  2207. };
  2208. } // namespace
  2209. bool gmsStateUsable (const std::vector<std::uint8_t>& state) { return parseState (state).has_value(); }
  2210. std::vector<std::string> gmsWaveNames (const std::vector<std::uint8_t>& state)
  2211. {
  2212. const auto p = parseState (state);
  2213. if (! p)
  2214. return {};
  2215. return { p->waves[0], p->waves[1], p->waves[2] };
  2216. }
  2217. int gmsParamOffset (const std::vector<std::uint8_t>& state, int index)
  2218. {
  2219. int at = -1;
  2220. if (index < 0 || index >= paramCount || ! parseState (state, &at))
  2221. return -1;
  2222. return at + 4 * index;
  2223. }
  2224. std::unique_ptr<Instrument> makeGmsInstrument() { return std::make_unique<GmsInstrument>(); }
  2225. namespace gms
  2226. {
  2227. std::vector<float> renderReference (const std::vector<std::uint8_t>& state, const std::vector<RefNote>& notes, double sampleRate,
  2228. int length, const std::string& wavesDir, int block, double bpm, int maxPoly, double songPos, bool effects)
  2229. {
  2230. const auto p = parseState (state);
  2231. if (! p || length <= 0)
  2232. return {};
  2233. auto plugin = std::make_unique<Plugin>();
  2234. plugin->init (sampleRate, effects);
  2235. plugin->eng.P = p->P;
  2236. for (int k = 0; k < 3; ++k)
  2237. plugin->eng.tabs[k] = waveTables (wavesDir, p->waves[(std::size_t) k]);
  2238. plugin->eng.tempo = (float) bpm;
  2239. plugin->eng.npoly2 = maxPoly == 0 ? voiceCount : std::clamp (maxPoly, 1, voiceCount); // ProcessEvent 1 (FUN_1800ed930)
  2240. std::vector<int> evs;
  2241. for (const auto& nt : notes)
  2242. {
  2243. evs.push_back (nt.start);
  2244. if (nt.release >= 0)
  2245. evs.push_back (nt.release);
  2246. }
  2247. std::sort (evs.begin(), evs.end());
  2248. std::vector<float> out (2 * (std::size_t) length, 0.0f), l ((std::size_t) std::max (block, 1)), r (l.size());
  2249. block = std::max (1, block);
  2250. int n0 = 0;
  2251. while (n0 < length)
  2252. {
  2253. // events at this sample in the model's order: (on, off) per note in list order, stable-sorted by sample
  2254. for (std::size_t i = 0; i < notes.size(); ++i)
  2255. {
  2256. if (notes[i].start == n0)
  2257. plugin->noteOn (notes[i].key, notes[i].initVol, notes[i].finalVol, notes[i].finalPan, notes[i].pitchCents);
  2258. if (notes[i].release == n0)
  2259. plugin->noteOff (std::clamp (notes[i].key, 0, 127));
  2260. }
  2261. auto ln = std::min (block, length - n0);
  2262. for (auto e : evs)
  2263. if (n0 < e && e < n0 + ln)
  2264. ln = e - n0;
  2265. std::fill (l.begin(), l.begin() + ln, 0.0f);
  2266. std::fill (r.begin(), r.begin() + ln, 0.0f);
  2267. plugin->render (l.data(), r.data(), ln, songPos >= 0.0 ? songPos + (double) n0 * bpm / (60.0 * sampleRate) : -1.0);
  2268. for (int j = 0; j < ln; ++j)
  2269. {
  2270. out[2 * (std::size_t) (n0 + j)] = l[(std::size_t) j];
  2271. out[2 * (std::size_t) (n0 + j) + 1] = r[(std::size_t) j];
  2272. }
  2273. n0 += ln;
  2274. }
  2275. return out;
  2276. }
  2277. } // namespace gms
  2278. } // namespace lynx