LynxVerbs.cpp 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  1. #include "core/LynxVerbs.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstdint>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <string>
  8. #include "core/Biquad.h"
  9. #include "core/Effects.h"
  10. #include "core/NebulaCloud.h"
  11. namespace lynx::verbs
  12. {
  13. namespace
  14. {
  15. constexpr double pi = 3.14159265358979323846;
  16. constexpr int lineCount = 8;
  17. // Line-length sets of the late tank, ms at lineScale 1 (spread over about an octave, no common factors, so the
  18. // modes of the lines do not line up). The last set is a ratio set (× lineScale in ms) for delay networks.
  19. constexpr double shapes[][lineCount] = {
  20. { 37.1, 41.9, 47.3, 53.9, 61.1, 67.7, 74.3, 83.9 }, // 0 hall
  21. { 21.1, 24.7, 28.3, 32.9, 37.3, 41.9, 46.1, 51.7 }, // 1 room / chamber
  22. { 29.3, 33.1, 38.9, 44.3, 51.7, 57.1, 63.7, 71.3 }, // 2 plate
  23. { 0.031, 0.117, 0.229, 0.357, 0.493, 0.641, 0.809, 1.000 }, // 3 delay network (fractions of the delay, see lineSpread)
  24. };
  25. constexpr int shapeCount = (int) (sizeof (shapes) / sizeof (shapes[0]));
  26. // input diffusers: J. Dattorro's lengths (142, 107, 379, 277 samples at 29761 Hz), the right channel slightly apart
  27. constexpr double inApMs[2][4] = { { 4.771, 3.595, 12.735, 9.307 }, { 4.513, 3.871, 11.913, 8.711 } };
  28. constexpr double inApCoef[4] = { 0.75, 0.75, 0.625, 0.625 };
  29. // in-loop all-pass length as a fraction of its line, early smearing all-passes (ms)
  30. constexpr double loopApFraction = 0.16;
  31. constexpr double earlyApMs[2][2] = { { 7.13, 2.31 }, { 6.47, 2.83 } };
  32. constexpr int earlyTaps = 14;
  33. constexpr int buildTaps = 40;
  34. constexpr int maxBandSections = 16;
  35. // a small deterministic generator for tap patterns (the same pattern on every machine)
  36. struct Lcg
  37. {
  38. std::uint32_t s;
  39. double next() noexcept
  40. {
  41. s = s * 1664525u + 1013904223u;
  42. return (double) (s >> 8) / 16777216.0;
  43. }
  44. };
  45. inline int nextPow2 (int n)
  46. {
  47. int p = 1;
  48. while (p < n)
  49. p <<= 1;
  50. return p;
  51. }
  52. struct Line
  53. {
  54. std::vector<float> buf;
  55. int mask = 0, pos = 0;
  56. void allocate (int samples)
  57. {
  58. const auto n = nextPow2 (std::max (4, samples + 4));
  59. buf.assign ((std::size_t) n, 0.0f);
  60. mask = n - 1;
  61. pos = 0;
  62. }
  63. void clear() noexcept
  64. {
  65. std::fill (buf.begin(), buf.end(), 0.0f);
  66. pos = 0;
  67. }
  68. void write (float x) noexcept
  69. {
  70. buf[(std::size_t) pos] = x;
  71. pos = (pos + 1) & mask;
  72. }
  73. // d ≥ 1: the sample written d writes ago
  74. float read (int d) const noexcept { return buf[(std::size_t) ((pos - d) & mask)]; }
  75. // fractional delay (≥ 2), 4-point Hermite
  76. float readFrac (double d) const noexcept
  77. {
  78. const auto i = (int) d;
  79. const auto f = (float) (d - i);
  80. const auto xm1 = read (i - 1), x0 = read (i), x1 = read (i + 1), x2 = read (i + 2);
  81. const auto c1 = 0.5f * (x1 - xm1);
  82. const auto c2 = xm1 - 2.5f * x0 + 2.0f * x1 - 0.5f * x2;
  83. const auto c3 = 0.5f * (x2 - xm1) + 1.5f * (x0 - x1);
  84. return ((c3 * f + c2) * f + c1) * f + x0;
  85. }
  86. };
  87. struct Allpass
  88. {
  89. Line line;
  90. int len = 1;
  91. float process (float x, float g) noexcept
  92. {
  93. const auto d = line.read (len);
  94. const auto v = x + g * d;
  95. line.write (v);
  96. return d - g * v;
  97. }
  98. };
  99. struct OnePole
  100. {
  101. float a = 0.0f, z = 0.0f;
  102. void setLowpass (double hz, double sr) noexcept { a = (float) std::exp (-2.0 * pi * std::clamp (hz, 1.0, sr * 0.49) / sr); }
  103. float lp (float x) noexcept { return z = x + a * (z - x); }
  104. };
  105. // the two-head pitch shifter (also plays backwards: ratio −1)
  106. struct Shifter
  107. {
  108. Line line;
  109. double phase = 0.0, window = 2400.0;
  110. void prepare (double sr, double windowSec)
  111. {
  112. window = std::max (64.0, windowSec * sr);
  113. line.allocate ((int) (window * 2.5) + 8);
  114. phase = 0.0;
  115. }
  116. void clear() noexcept
  117. {
  118. line.clear();
  119. phase = 0.0;
  120. }
  121. float process (float x, double ratio) noexcept
  122. {
  123. line.write (x);
  124. phase += (1.0 - ratio) / window;
  125. phase -= std::floor (phase);
  126. float y = 0.0f;
  127. for (int h = 0; h < 2; ++h)
  128. {
  129. auto p = phase + 0.5 * h;
  130. p -= std::floor (p);
  131. const auto w = (float) std::sin (pi * p);
  132. y += w * w * line.readFrac (2.0 + p * window);
  133. }
  134. return y;
  135. }
  136. };
  137. double clampPos (double v, double lo, double hi) noexcept { return std::isfinite (v) ? std::clamp (v, lo, hi) : lo; }
  138. // round 190it: where revision 1's build groups part (Hz; 250 / 400 / 600 / 800 tried on refcap030's room bank: 600 kept
  139. // the long-decay music renders where they were); the lab build reads LYNX_SPLITHZ
  140. double splitHz() noexcept
  141. {
  142. #ifdef LYNX_VERB_FIT
  143. static const double hz = [] { const char* e = std::getenv ("LYNX_SPLITHZ"); return e != nullptr ? std::atof (e) : 600.0; }();
  144. return hz;
  145. #else
  146. return 600.0;
  147. #endif
  148. }
  149. // ---- engine law 1 (0.1.27, round 190bz) ------------------------------------------------------------------------
  150. // Per-band decay (Jot & Chaigne 1991: one absorption filter per line whose attenuation in dB is proportional to the
  151. // line's length gives the same RT60 on every line; the filter here is a cascade of first-order shelves whose gains are
  152. // solved by least squares on the octave bands, the way proportional graphic equalisers are designed: Välimäki &
  153. // Liski 2017, Prawda, Välimäki & Schlecht 2019), a tone corrector of the same kind after the loop, a diffusion-step
  154. // onset (delay bank → polarity → Hadamard, three steps), rotation scattering inside the halves and a swinging
  155. // rotation between them (Schlecht & Habets 2015: a time-varying unitary matrix moves all bands alike).
  156. constexpr double bandHz[engineBands] = { 63.0, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0 };
  157. constexpr int loopShelves = 6;
  158. constexpr double loopShelfHz[loopShelves] = { 90.0, 250.0, 700.0, 1600.0, 3200.0, 6000.0 };
  159. constexpr int maxShelves = 7;
  160. constexpr int onsetSteps = 3, onsetChans = 4;
  161. constexpr double onsetFrac[onsetSteps][onsetChans] = { { 0.23, 0.52, 0.77, 1.0 }, { 0.19, 0.47, 0.71, 1.0 }, { 0.27, 0.44, 0.83, 1.0 } };
  162. constexpr double onsetSpan[onsetSteps] = { 1.0 / 7.0, 2.0 / 7.0, 4.0 / 7.0 };
  163. constexpr float onsetSign[onsetSteps][onsetChans] = { { 1.0f, -1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, -1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f, 1.0f } };
  164. constexpr double maxOnsetSec = 0.25;
  165. constexpr double maxLoopGain = 0.99995;
  166. // dB of the symmetric first-order low shelf (gain gDb at 0 Hz, 0 dB at the top, half of it at the corner) at the
  167. // warped frequency t = tan (π f / sr), corner tc: |H|² = (t² + tc² G) / (t² + tc² / G)
  168. double shelfDbAt (double gDb, double tc, double t) noexcept
  169. {
  170. const auto g = std::pow (10.0, gDb / 20.0);
  171. return 10.0 * std::log10 ((t * t + tc * tc * g) / (t * t + tc * tc / g));
  172. }
  173. // least squares: a broadband gain and n low shelves (dB) whose sum meets the dB targets at the eight band centres.
  174. // The shelves' shapes depend a little on their gains, so the solve is repeated on the shapes of the last answer
  175. void fitShelves (const double* tc, int n, const double* tb, const double* target, double ridge, double& gainDb, double* shelfDb) noexcept
  176. {
  177. const int m = n + 1;
  178. for (int k = 0; k < n; ++k)
  179. shelfDb[k] = 0.0;
  180. gainDb = 0.0;
  181. for (int pass = 0; pass < 3; ++pass)
  182. {
  183. double a[engineBands][maxShelves + 1];
  184. for (int b = 0; b < engineBands; ++b)
  185. {
  186. a[b][0] = 1.0;
  187. for (int k = 0; k < n; ++k)
  188. a[b][k + 1] = std::abs (shelfDb[k]) < 0.05 ? tc[k] * tc[k] / (tb[b] * tb[b] + tc[k] * tc[k])
  189. : shelfDbAt (shelfDb[k], tc[k], tb[b]) / shelfDb[k];
  190. }
  191. double nm[maxShelves + 1][maxShelves + 2] {};
  192. for (int i = 0; i < m; ++i)
  193. {
  194. for (int j = 0; j < m; ++j)
  195. for (int b = 0; b < engineBands; ++b)
  196. nm[i][j] += a[b][i] * a[b][j];
  197. for (int b = 0; b < engineBands; ++b)
  198. nm[i][m] += a[b][i] * target[b];
  199. if (i > 0)
  200. nm[i][i] *= 1.0 + ridge;
  201. }
  202. for (int c = 0; c < m; ++c) // Gauss–Jordan with partial pivoting
  203. {
  204. int piv = c;
  205. for (int r = c + 1; r < m; ++r)
  206. if (std::abs (nm[r][c]) > std::abs (nm[piv][c]))
  207. piv = r;
  208. if (std::abs (nm[piv][c]) < 1e-14)
  209. continue;
  210. if (piv != c)
  211. for (int j = 0; j <= m; ++j)
  212. std::swap (nm[piv][j], nm[c][j]);
  213. for (int r = 0; r < m; ++r)
  214. if (r != c)
  215. {
  216. const auto f = nm[r][c] / nm[c][c];
  217. for (int j = c; j <= m; ++j)
  218. nm[r][j] -= f * nm[c][j];
  219. }
  220. }
  221. gainDb = std::abs (nm[0][0]) > 1e-14 ? nm[0][m] / nm[0][0] : 0.0;
  222. for (int k = 0; k < n; ++k)
  223. shelfDb[k] = std::abs (nm[k + 1][k + 1]) > 1e-14 ? std::clamp (nm[k + 1][m] / nm[k + 1][k + 1], -48.0, 48.0) : 0.0;
  224. }
  225. }
  226. // dB per pass, at the eight band centres, of the classic three-band absorption of a line of `len` samples (its two
  227. // one-pole crossovers included), divided by the per-band multipliers: what law 1's loop filter is designed to
  228. void bandLossDb (const Settings& s, double sr, double len, double out[engineBands]) noexcept
  229. {
  230. const auto rt = std::max (0.01, s.gateSec > 0.0 ? std::min (s.rt60, 0.12) : s.rt60);
  231. const auto gain = [&] (double mult) { return std::min (maxLoopGain, std::pow (10.0, -3.0 * len / (rt * std::max (0.02, mult) * sr))); };
  232. const auto gM = gain (1.0), gL = gain (s.lowMult), gH = gain (s.highMult);
  233. const auto aLo = std::exp (-2.0 * pi * std::clamp (std::max (20.0, s.lowXover), 1.0, sr * 0.49) / sr);
  234. const auto aHi = std::exp (-2.0 * pi * std::clamp (std::max (20.0, s.highXover), 1.0, sr * 0.49) / sr);
  235. for (int b = 0; b < engineBands; ++b)
  236. {
  237. const auto w = 2.0 * pi * std::min (bandHz[b], 0.45 * sr) / sr;
  238. const auto cw = std::cos (w), sw = std::sin (w);
  239. // one-pole low-pass (1 − a) / (1 − a z⁻¹) at e^{jw}
  240. const auto lp = [&] (double a, double& re, double& im)
  241. {
  242. const auto dr = 1.0 - a * cw, di = a * sw, d = dr * dr + di * di;
  243. re = (1.0 - a) * dr / d;
  244. im = -(1.0 - a) * di / d;
  245. };
  246. double lr, li, hr, hi;
  247. lp (aLo, lr, li);
  248. lp (aHi, hr, hi);
  249. const auto re = gM + (gL - gM) * lr + (gH - gM) * (1.0 - hr), im = (gL - gM) * li - (gH - gM) * hi;
  250. const auto db = 10.0 * std::log10 (std::max (1e-12, re * re + im * im));
  251. out[b] = std::clamp (db / std::clamp (s.bandRtMult[b], 0.02, 50.0), -60.0, -1e-7);
  252. }
  253. }
  254. } // namespace
  255. void bandTargets (const Settings& s, double sampleRate, double lineMs, double out[engineBands]) noexcept
  256. {
  257. const auto len = lineMs * sampleRate / 1000.0;
  258. double db[engineBands];
  259. bandLossDb (s, sampleRate, len, db);
  260. // the loop's own one-pole band limits (both laws have them) take their share on top of the designed filter
  261. const auto onePoleDb = [&] (double hz, double at)
  262. {
  263. const auto a = std::exp (-2.0 * pi * hz / sampleRate), w = 2.0 * pi * std::min (at, 0.45 * sampleRate) / sampleRate;
  264. return 10.0 * std::log10 ((1.0 - a) * (1.0 - a) / (1.0 - 2.0 * a * std::cos (w) + a * a));
  265. };
  266. for (int b = 0; b < engineBands; ++b)
  267. {
  268. auto loss = db[b];
  269. if (s.loopBandwidth > 0.0 && s.loopBandwidth < 0.49 * sampleRate)
  270. loss += onePoleDb (s.loopBandwidth, bandHz[b]);
  271. if (s.loopLowCut > 0.0)
  272. {
  273. // the high-pass is the input minus its low-pass: a (1 − z⁻¹) / (1 − a z⁻¹)
  274. const auto a = std::exp (-2.0 * pi * s.loopLowCut / sampleRate), w = 2.0 * pi * std::min (bandHz[b], 0.45 * sampleRate) / sampleRate;
  275. loss += 10.0 * std::log10 (std::max (1e-12, a * a * (2.0 - 2.0 * std::cos (w)) / (1.0 - 2.0 * a * std::cos (w) + a * a)));
  276. }
  277. out[b] = -60.0 * len / (std::min (loss, -1e-7) * sampleRate);
  278. }
  279. }
  280. // =================================================================================================================
  281. // laws
  282. double skewLaw (double v, double lo, double hi, double k) noexcept
  283. {
  284. return lo + (hi - lo) * std::pow (std::clamp (v, 0.0, 1.0), k);
  285. }
  286. double skewInverse (double unit, double lo, double hi, double k) noexcept
  287. {
  288. return std::pow (std::clamp ((unit - lo) / (hi - lo), 0.0, 1.0), 1.0 / k);
  289. }
  290. double knotLaw (double v, const double* knots, int count) noexcept
  291. {
  292. v = std::clamp (v, knots[0], knots[2 * (count - 1)]);
  293. for (int i = 1; i < count; ++i)
  294. if (v <= knots[2 * i] || i == count - 1)
  295. {
  296. const auto v0 = knots[2 * i - 2], v1 = knots[2 * i];
  297. const auto u0 = knots[2 * i - 1], u1 = knots[2 * i + 1];
  298. return v1 > v0 ? u0 + (u1 - u0) * (v - v0) / (v1 - v0) : u1;
  299. }
  300. return knots[1];
  301. }
  302. double knotInverse (double unit, const double* knots, int count) noexcept
  303. {
  304. unit = std::clamp (unit, knots[1], knots[2 * count - 1]);
  305. for (int i = 1; i < count; ++i)
  306. if (unit <= knots[2 * i + 1] || i == count - 1)
  307. {
  308. const auto v0 = knots[2 * i - 2], v1 = knots[2 * i];
  309. const auto u0 = knots[2 * i - 1], u1 = knots[2 * i + 1];
  310. return u1 > u0 ? v0 + (v1 - v0) * (unit - u0) / (u1 - u0) : v0;
  311. }
  312. return knots[0];
  313. }
  314. int choice (float v, int count) noexcept
  315. {
  316. if (count <= 1)
  317. return 0;
  318. return std::clamp ((int) std::lround (std::clamp ((double) v, 0.0, 1.0) * (count - 1)), 0, count - 1);
  319. }
  320. void mixGains (float mix, float& dry, float& wet) noexcept
  321. {
  322. const auto m = std::clamp ((double) mix, 0.0, 1.0) * pi * 0.5;
  323. dry = (float) std::cos (m);
  324. wet = (float) std::sin (m);
  325. if (mix >= 1.0f)
  326. dry = 0.0f;
  327. if (mix <= 0.0f)
  328. wet = 0.0f;
  329. }
  330. // =================================================================================================================
  331. // engine
  332. struct Engine::Impl
  333. {
  334. double sr = 48000.0, maxLineSec, maxPreSec;
  335. Line pre[2], raw[2], dif[2];
  336. Allpass inAp[2][4], earlyAp[2][2];
  337. Line lines[lineCount];
  338. Allpass loopAp[lineCount];
  339. struct Absorb
  340. {
  341. OnePole lo, hi, bw, lc;
  342. float gL = 1, gM = 1, gH = 1;
  343. } absorb[lineCount];
  344. double curLen[lineCount] {}; // glided line lengths (samples)
  345. double lfo[lineCount] {};
  346. Shifter shifters[4]; // inside lines 0, 2, 4, 6 (shimmer)
  347. float shiftHp[4][2] {}; // round tp172d: the shifted branch's two one-pole high-pass states (energy-keeping shimmer)
  348. float splitLp[2][4] {}; // round 190it: the build groups' low band (one-pole states), which still reaches the lines as one sum
  349. Biquad outLow, outHigh, shelfLow, shelfHigh, toneHigh, toneMid, toneLow, band[maxBandSections];
  350. bool outLowOn = false, outHighOn = false, shelfLowOn = false, shelfHighOn = false, toneHighOn = false, toneMidOn = false, toneLowOn = false, bandOn = false;
  351. int bandSections = 0;
  352. // tap patterns (unit times and gains), drawn once
  353. double eT[2][earlyTaps] {}, eG[2][earlyTaps] {};
  354. double bT[2][buildTaps] {}, bG[2][buildTaps] {};
  355. int eD[2][earlyTaps] {}, bD[2][buildTaps] {};
  356. float eW[2][earlyTaps] {}, bW[2][buildTaps] {};
  357. Settings last;
  358. bool first = true;
  359. float shiftState = 0.0f;
  360. float apG[lineCount] {}, inG[4] {}, eApG = 0;
  361. double modDepth = 0, modInc[lineCount] {};
  362. double eModDepth = 0, eModInc = 0, eModPhase = 0; // round 170c: the early taps' modulation (samples, rad / sample)
  363. double feedbackGain = -1;
  364. // ---- law 1 (round 190bz) ----
  365. float shB0[loopShelves][lineCount] {}, shB1[loopShelves][lineCount] {}, shA1[loopShelves][lineCount] {}, shZ[loopShelves][lineCount] {};
  366. double loopKey[14 + lineCount] {}; // the settings and lengths the loop filters were designed for
  367. bool loopDesigned = false;
  368. Biquad geq[engineBands];
  369. bool geqOn[engineBands] {};
  370. double toneKey[engineBands] {};
  371. bool toneDesigned = false, toneOn = false;
  372. double lfoC[lineCount] {}, lfoS[lineCount] {}, lfoRc[lineCount] {}, lfoRs[lineCount] {}, lfoInc[lineCount] {};
  373. double mmC = 1.0, mmS = 0.0, mmRc = 1.0, mmRs = 0.0, mmInc = -1.0;
  374. unsigned renorm = 0;
  375. Line onsetLine[onsetSteps][onsetChans];
  376. Biquad bell[3];
  377. bool bellOn[3] {};
  378. int onsetD[onsetSteps][onsetChans] {};
  379. Impl (double maxLine, double maxPre) : maxLineSec (maxLine), maxPreSec (maxPre)
  380. {
  381. Lcg r { 0x2545F491u };
  382. for (int c = 0; c < 2; ++c)
  383. {
  384. for (int k = 0; k < earlyTaps; ++k)
  385. {
  386. eT[c][k] = (k + 0.15 + 0.7 * r.next()) / earlyTaps;
  387. eG[c][k] = r.next() < 0.5 ? -1.0 : 1.0;
  388. }
  389. for (int k = 0; k < buildTaps; ++k)
  390. {
  391. bT[c][k] = (k + 0.1 + 0.8 * r.next()) / buildTaps;
  392. bG[c][k] = r.next() < 0.5 ? -1.0 : 1.0;
  393. }
  394. }
  395. }
  396. void prepare (double rate)
  397. {
  398. sr = rate;
  399. const auto ms = sr / 1000.0;
  400. for (int c = 0; c < 2; ++c)
  401. {
  402. pre[c].allocate ((int) (maxPreSec * sr) + 8);
  403. raw[c].allocate ((int) (1.05 * sr) + 8);
  404. dif[c].allocate ((int) (2.65 * sr) + 8); // (Halo's build reaches 2.42 s)
  405. for (int k = 0; k < 4; ++k)
  406. {
  407. inAp[c][k].len = std::max (1, (int) std::lround (inApMs[c][k] * ms));
  408. inAp[c][k].line.allocate (inAp[c][k].len + 2);
  409. }
  410. for (int k = 0; k < 2; ++k)
  411. {
  412. earlyAp[c][k].len = std::max (1, (int) std::lround (earlyApMs[c][k] * ms));
  413. earlyAp[c][k].line.allocate (earlyAp[c][k].len + 2);
  414. }
  415. }
  416. const auto maxLine = (int) (maxLineSec * sr) + 16;
  417. for (int i = 0; i < lineCount; ++i)
  418. {
  419. lines[i].allocate (maxLine + (int) (0.05 * sr));
  420. loopAp[i].line.allocate ((int) (0.2 * maxLineSec * sr) + 8);
  421. }
  422. for (auto& sh : shifters)
  423. sh.prepare (sr, 0.06);
  424. for (int k = 0; k < onsetSteps; ++k)
  425. for (auto& l : onsetLine[k])
  426. l.allocate ((int) (maxOnsetSec * onsetSpan[k] * sr) + 8);
  427. reset();
  428. }
  429. void reset() noexcept
  430. {
  431. for (int c = 0; c < 2; ++c)
  432. {
  433. pre[c].clear();
  434. raw[c].clear();
  435. dif[c].clear();
  436. for (auto& a : inAp[c])
  437. a.line.clear();
  438. for (auto& a : earlyAp[c])
  439. a.line.clear();
  440. }
  441. for (int i = 0; i < lineCount; ++i)
  442. {
  443. lines[i].clear();
  444. loopAp[i].line.clear();
  445. absorb[i].lo.z = absorb[i].hi.z = absorb[i].bw.z = absorb[i].lc.z = 0.0f;
  446. lfo[i] = 2.0 * pi * i / lineCount;
  447. }
  448. for (auto& sh : shifters)
  449. sh.clear();
  450. for (auto& h : shiftHp)
  451. h[0] = h[1] = 0.0f;
  452. for (auto& c : splitLp)
  453. for (auto& z : c)
  454. z = 0.0f;
  455. outLow.reset();
  456. outHigh.reset();
  457. shelfLow.reset();
  458. shelfHigh.reset();
  459. for (auto& b : band)
  460. b.reset();
  461. toneHigh.reset();
  462. toneLow.reset();
  463. toneMid.reset();
  464. shiftState = 0.0f;
  465. first = true;
  466. for (int i = 0; i < lineCount; ++i)
  467. {
  468. lfoC[i] = std::cos (2.0 * pi * i / lineCount);
  469. lfoS[i] = std::sin (2.0 * pi * i / lineCount);
  470. lfoInc[i] = -1.0;
  471. }
  472. for (auto& row : shZ)
  473. for (auto& z : row)
  474. z = 0.0f;
  475. for (auto& b : geq)
  476. b.reset();
  477. for (auto& step : onsetLine)
  478. for (auto& l : step)
  479. l.clear();
  480. for (auto& b : bell)
  481. b.reset();
  482. mmC = 1.0;
  483. mmS = 0.0;
  484. mmInc = -1.0;
  485. renorm = 0;
  486. loopDesigned = toneDesigned = false;
  487. }
  488. double targetLen (const Settings& s, int i) const noexcept
  489. {
  490. const auto shape = std::clamp (s.shape, 0, shapeCount - 1);
  491. const auto spread = std::clamp (s.lineSpread, 0.0, 1.0);
  492. const auto ms = shape == 3 ? std::max (0.0, s.lineScale) * (1.0 - spread * (1.0 - shapes[3][i]))
  493. : shapes[shape][i] * std::max (0.0, s.lineScale);
  494. return clampPos (ms * sr / 1000.0, 8.0, maxLineSec * sr);
  495. }
  496. // coefficients that depend on the settings (once per block)
  497. void update (const Settings& s) noexcept
  498. {
  499. const auto ms = sr / 1000.0;
  500. for (int k = 0; k < 4; ++k)
  501. inG[k] = (float) (inApCoef[k] * std::clamp (s.inDiffusion, 0.0, 1.0));
  502. eApG = (float) (0.7 * std::clamp (s.earlyDiffusion, 0.0, 1.0));
  503. // tap delays and gains: early (energy-normalised, falling) and build-up (rising then falling, sum of squares 1)
  504. for (int c = 0; c < 2; ++c)
  505. {
  506. double e = 0, b = 0;
  507. for (int k = 0; k < earlyTaps; ++k)
  508. {
  509. eD[c][k] = std::clamp ((int) (eT[c][k] * std::max (0.0005, s.earlySec) * sr), 1, raw[c].mask - 4);
  510. const auto a = std::exp (-s.earlyDecay * eT[c][k]);
  511. eW[c][k] = (float) (a * eG[c][k]);
  512. e += a * a;
  513. }
  514. for (auto& w : eW[c])
  515. w = (float) (w / std::sqrt (std::max (e, 1e-9)));
  516. const auto gated = s.gateSec > 0.0;
  517. const auto span = gated ? s.gateSec : s.buildSec;
  518. for (int k = 0; k < buildTaps; ++k)
  519. {
  520. bD[c][k] = std::clamp ((int) (bT[c][k] * span * sr), 0, dif[c].mask - 4);
  521. const auto u = bT[c][k];
  522. const auto a = gated ? 0.6 + 0.4 * u
  523. : s.buildRise >= 1.0 ? std::pow (std::max (u, 1e-6), 0.5 * (s.buildRise - 1.0)) // round tp172d
  524. : std::sin (pi * std::sqrt (u)); // build: rises, then falls
  525. bW[c][k] = (float) (a * bG[c][k]);
  526. b += a * a;
  527. }
  528. for (auto& w : bW[c])
  529. w = (float) (w / std::sqrt (std::max (b, 1e-9)));
  530. }
  531. // round 170c: the early taps' modulation (four sines, a tap on each in turn; never past the shortest tap)
  532. eModDepth = std::max (0.0, s.earlyModDepthMs) * ms;
  533. if (eModDepth > 0.0)
  534. {
  535. int shortest = raw[0].mask;
  536. for (int c = 0; c < 2; ++c)
  537. for (int k = 0; k < earlyTaps; ++k) shortest = std::min (shortest, eD[c][k]);
  538. eModDepth = std::min (eModDepth, std::max (0.0, 0.45 * (double) shortest - 1.0));
  539. }
  540. eModInc = 2.0 * pi * std::max (0.0, s.earlyModRateHz) / sr;
  541. // modulation
  542. modDepth = std::max (0.0, s.modDepthMs) * ms;
  543. for (int i = 0; i < lineCount; ++i)
  544. modInc[i] = 2.0 * pi * std::max (0.0, s.modRateHz) * (1.0 + 0.137 * i - 0.019 * i * i * 0.5) / sr;
  545. // loop filters
  546. for (int i = 0; i < lineCount; ++i)
  547. {
  548. auto& a = absorb[i];
  549. a.lo.setLowpass (std::max (20.0, s.lowXover), sr);
  550. a.hi.setLowpass (std::max (20.0, s.highXover), sr);
  551. if (s.loopBandwidth > 0.0 && s.loopBandwidth < 0.49 * sr)
  552. a.bw.setLowpass (s.loopBandwidth, sr);
  553. else
  554. a.bw.a = 0.0f;
  555. if (s.loopLowCut > 0.0)
  556. a.lc.setLowpass (s.loopLowCut, sr);
  557. else
  558. a.lc.a = 1.0f;
  559. apG[i] = (float) (0.6 * std::clamp (s.loopDiffusion, 0.0, 1.0));
  560. }
  561. // output filters
  562. outLowOn = s.outLowCut > 0.0;
  563. if (outLowOn)
  564. {
  565. if (s.outLowOrder == 1) // first order (bilinear)
  566. {
  567. const auto k = std::tan (pi * std::min (s.outLowCut, 0.45 * sr) / sr);
  568. outLow.set (1.0, -1.0, 0.0, 1.0 + k, k - 1.0, 0.0);
  569. }
  570. else
  571. outLow.filter (sr, 1, s.outLowCut, 0.7071);
  572. }
  573. outHighOn = s.outHighCut > 0.0 && s.outHighCut < 0.45 * sr;
  574. if (outHighOn)
  575. {
  576. if (s.outHighOrder == 1)
  577. {
  578. const auto k = std::tan (pi * s.outHighCut / sr);
  579. outHigh.set (k, k, 0.0, 1.0 + k, k - 1.0, 0.0);
  580. }
  581. else
  582. outHigh.filter (sr, 0, s.outHighCut, 0.7071);
  583. }
  584. bandOn = s.bandLimit > 0.0 && s.bandLimit < 0.45 * sr;
  585. if (bandOn)
  586. {
  587. // Butterworth of order 2 · sections: section k's Q = 1 / (2 cos ((2k + 1) π / (4 · sections)))
  588. bandSections = std::clamp (s.bandLimitSections, 1, maxBandSections);
  589. for (int k = 0; k < bandSections; ++k)
  590. band[k].filter (sr, 0, s.bandLimit, 1.0 / (2.0 * std::cos ((2 * k + 1) * pi / (4.0 * bandSections))));
  591. }
  592. toneLowOn = std::abs (s.toneLowDb) > 0.01;
  593. if (toneLowOn)
  594. toneLow.shelf (sr, s.toneLowHz, s.toneLowDb, false, 0.7);
  595. toneHighOn = std::abs (s.toneHighDb) > 0.01;
  596. if (toneHighOn)
  597. toneHigh.shelf (sr, std::min (s.toneHighHz, 0.45 * sr), s.toneHighDb, true, 0.7);
  598. toneMidOn = std::abs (s.toneMidDb) > 0.01;
  599. if (toneMidOn)
  600. toneMid.peaking (sr, std::min (s.toneMidHz, 0.45 * sr), std::max (0.1, s.toneMidQ), s.toneMidDb);
  601. shelfLowOn = std::abs (s.lowShelfDb) > 0.01;
  602. if (shelfLowOn)
  603. shelfLow.shelf (sr, s.lowShelfHz, s.lowShelfDb, false, 0.7);
  604. shelfHighOn = std::abs (s.highShelfDb) > 0.01;
  605. if (shelfHighOn)
  606. shelfHigh.shelf (sr, std::min (s.highShelfHz, 0.45 * sr), s.highShelfDb, true, 0.7);
  607. }
  608. // per-line gains for the current lengths (cheap: once per block)
  609. void decayGains (const Settings& s) noexcept
  610. {
  611. for (int i = 0; i < lineCount; ++i)
  612. {
  613. auto& a = absorb[i];
  614. const auto len = curLen[i] + (apG[i] != 0.0f ? (double) loopAp[i].len : 0.0);
  615. if (s.feedback >= 0.0)
  616. {
  617. const auto g = std::min (0.99995, s.feedback);
  618. a.gM = (float) g;
  619. a.gL = (float) std::min (0.99995, std::pow (g, 1.0 / std::max (0.05, s.lowMult)));
  620. a.gH = (float) std::min (0.99995, std::pow (g, 1.0 / std::max (0.05, s.highMult)));
  621. continue;
  622. }
  623. const auto rt = std::max (0.01, s.gateSec > 0.0 ? std::min (s.rt60, 0.12) : s.rt60);
  624. auto gain = [&] (double mult) { return (float) std::min (0.99995, std::pow (10.0, -3.0 * len / (rt * std::max (0.02, mult) * sr))); };
  625. a.gM = gain (1.0);
  626. a.gL = gain (s.lowMult);
  627. a.gH = gain (s.highMult);
  628. }
  629. }
  630. // ---- law 1 ---------------------------------------------------------------------------------------------------
  631. // the loop filter of every line: its dB per pass at the octave bands (the classic law's, times the per-band
  632. // multipliers) met by a gain and six shelves; redone only when a setting or a length it depends on moves
  633. void designLoop (const Settings& s) noexcept
  634. {
  635. double key[14 + lineCount] = { s.rt60, s.lowMult, s.lowXover, s.highMult, s.highXover, s.gateSec };
  636. for (int b = 0; b < engineBands; ++b)
  637. key[6 + b] = s.bandRtMult[b];
  638. for (int i = 0; i < lineCount; ++i)
  639. key[14 + i] = curLen[i] + (apG[i] != 0.0f ? (double) loopAp[i].len : 0.0);
  640. if (loopDesigned && std::equal (std::begin (key), std::end (key), std::begin (loopKey)))
  641. return;
  642. std::copy (std::begin (key), std::end (key), std::begin (loopKey));
  643. loopDesigned = true;
  644. double tc[loopShelves], tb[engineBands];
  645. for (int k = 0; k < loopShelves; ++k)
  646. tc[k] = std::tan (pi * std::min (loopShelfHz[k], 0.45 * sr) / sr);
  647. for (int b = 0; b < engineBands; ++b)
  648. tb[b] = std::tan (pi * std::min (bandHz[b], 0.45 * sr) / sr);
  649. for (int i = 0; i < lineCount; ++i)
  650. {
  651. double target[engineBands], shelf[loopShelves], gainDb = 0.0;
  652. bandLossDb (s, sr, key[14 + i], target);
  653. fitShelves (tc, loopShelves, tb, target, 1e-3, gainDb, shelf);
  654. // never above unity at any frequency: the highest point of the response on a grid of 40 frequencies from
  655. // 0 Hz to the top (the cascade of first-order shelves is smooth: between two grid points it cannot rise by
  656. // more than a small share of the shelves' gains, which is added)
  657. double top = -1000.0, sum = 0.0;
  658. for (int k = 0; k < loopShelves; ++k)
  659. sum += std::abs (shelf[k]);
  660. for (int j = 0; j < 40; ++j)
  661. {
  662. const auto t = j == 0 ? 0.0 : j == 39 ? 1.0e6 : std::tan (pi * std::min (10.0 * std::pow (2.0, 0.3 * j), 0.499 * sr) / sr);
  663. auto db = gainDb;
  664. for (int k = 0; k < loopShelves; ++k)
  665. db += shelfDbAt (shelf[k], tc[k], t);
  666. top = std::max (top, db);
  667. }
  668. top += 0.03 * sum;
  669. const auto limit = 20.0 * std::log10 (maxLoopGain);
  670. if (top > limit)
  671. gainDb -= top - limit;
  672. const auto g = std::pow (10.0, gainDb / 20.0);
  673. for (int k = 0; k < loopShelves; ++k)
  674. {
  675. const auto r = std::pow (10.0, shelf[k] / 40.0), den = 1.0 + tc[k] / r, fold = k == 0 ? g : 1.0;
  676. shB0[k][i] = (float) (fold * (1.0 + tc[k] * r) / den);
  677. shB1[k][i] = (float) (fold * (tc[k] * r - 1.0) / den);
  678. shA1[k][i] = (float) ((tc[k] / r - 1.0) / den);
  679. }
  680. }
  681. }
  682. // the tone corrector: dB per octave band after the loop. A proportional graphic equaliser (a low shelf, six bells,
  683. // a high shelf); the bands overlap, so the gains are solved through their interaction at the band centres (two
  684. // rounds: the shapes change a little with the gains) — Välimäki & Liski 2017
  685. void setToneBand (int k, double db) noexcept
  686. {
  687. if (k == 0)
  688. geq[k].shelf (sr, 88.0, db, false, 1.0);
  689. else if (k == engineBands - 1)
  690. geq[k].shelf (sr, std::min (5657.0, 0.4 * sr), db, true, 1.0);
  691. else
  692. geq[k].peaking (sr, std::min (bandHz[k], 0.4 * sr), 1.25, db);
  693. }
  694. void designTone (const Settings& s) noexcept
  695. {
  696. if (toneDesigned && std::equal (std::begin (s.bandDb), std::end (s.bandDb), std::begin (toneKey)))
  697. return;
  698. std::copy (std::begin (s.bandDb), std::end (s.bandDb), std::begin (toneKey));
  699. toneDesigned = true;
  700. toneOn = false;
  701. double target[engineBands], g[engineBands];
  702. for (int b = 0; b < engineBands; ++b)
  703. {
  704. g[b] = target[b] = std::clamp (s.bandDb[b], -18.0, 18.0);
  705. toneOn = toneOn || std::abs (target[b]) > 0.01;
  706. }
  707. if (! toneOn)
  708. return;
  709. const auto magDb = [this] (const Biquad& q, double hz)
  710. {
  711. const auto w = 2.0 * pi * std::min (hz, 0.45 * sr) / sr;
  712. const auto c1 = std::cos (w), s1 = std::sin (w), c2 = std::cos (2.0 * w), s2 = std::sin (2.0 * w);
  713. const auto nr = q.b0 + q.b1 * c1 + q.b2 * c2, ni = -(q.b1 * s1 + q.b2 * s2);
  714. const auto dr = 1.0 + q.a1 * c1 + q.a2 * c2, di = -(q.a1 * s1 + q.a2 * s2);
  715. return 10.0 * std::log10 (std::max (1e-20, (nr * nr + ni * ni) / (dr * dr + di * di)));
  716. };
  717. for (int round = 0; round < 2; ++round)
  718. {
  719. double m[engineBands][engineBands + 1];
  720. for (int k = 0; k < engineBands; ++k)
  721. {
  722. const auto proto = std::abs (g[k]) < 0.5 ? (g[k] < 0.0 ? -0.5 : 0.5) : g[k];
  723. setToneBand (k, proto);
  724. for (int j = 0; j < engineBands; ++j)
  725. m[j][k] = magDb (geq[k], bandHz[j]) / proto;
  726. }
  727. for (int j = 0; j < engineBands; ++j)
  728. m[j][engineBands] = target[j];
  729. for (int c = 0; c < engineBands; ++c)
  730. {
  731. int piv = c;
  732. for (int r = c + 1; r < engineBands; ++r)
  733. if (std::abs (m[r][c]) > std::abs (m[piv][c]))
  734. piv = r;
  735. if (std::abs (m[piv][c]) < 1e-12)
  736. continue;
  737. if (piv != c)
  738. for (int j = 0; j <= engineBands; ++j)
  739. std::swap (m[piv][j], m[c][j]);
  740. for (int r = 0; r < engineBands; ++r)
  741. if (r != c)
  742. {
  743. const auto f = m[r][c] / m[c][c];
  744. for (int j = c; j <= engineBands; ++j)
  745. m[r][j] -= f * m[c][j];
  746. }
  747. }
  748. for (int k = 0; k < engineBands; ++k)
  749. g[k] = std::abs (m[k][k]) > 1e-12 ? std::clamp (m[k][engineBands] / m[k][k], -24.0, 24.0) : 0.0;
  750. }
  751. for (int k = 0; k < engineBands; ++k)
  752. {
  753. geqOn[k] = std::abs (g[k]) > 0.01;
  754. setToneBand (k, g[k]);
  755. }
  756. }
  757. void processLaw1 (float* L, float* R, int n, const Settings& s) noexcept
  758. {
  759. update (s);
  760. const auto pd = std::clamp ((int) std::lround (s.predelaySec * sr), 0, pre[0].mask - 2);
  761. double target[lineCount];
  762. for (int i = 0; i < lineCount; ++i)
  763. {
  764. // whole samples: a still line is then read without interpolation loss (the glide moves in quarter samples
  765. // and lands on them), so the top band decays as designed
  766. target[i] = std::floor (targetLen (s, i));
  767. if (first)
  768. curLen[i] = target[i];
  769. loopAp[i].len = std::clamp ((int) (target[i] * loopApFraction), 1, loopAp[i].line.mask - 2);
  770. }
  771. first = false;
  772. double step[lineCount]; // round 190ie: the glide per sample (fixed, or a tempo change spread over the block)
  773. for (int i = 0; i < lineCount; ++i)
  774. step[i] = s.lengthSpread > 0 ? std::min (0.25, std::abs (target[i] - curLen[i]) / (double) s.lengthSpread) : 0.25; // (0.25: the fixed glide)
  775. designLoop (s);
  776. designTone (s);
  777. // modulators as rotating pairs (one rotation per sample instead of a sine), the lines' rates as in law 0
  778. for (int i = 0; i < lineCount; ++i)
  779. if (modInc[i] != lfoInc[i])
  780. {
  781. lfoInc[i] = modInc[i];
  782. lfoRc[i] = std::cos (modInc[i]);
  783. lfoRs[i] = std::sin (modInc[i]);
  784. }
  785. const auto mmDepth = std::clamp (s.matrixModDepth, 0.0, 0.7);
  786. {
  787. const auto inc = 2.0 * pi * std::clamp (s.matrixModRateHz, 0.0, 20.0) / sr;
  788. if (inc != mmInc)
  789. {
  790. mmInc = inc;
  791. mmRc = std::cos (inc);
  792. mmRs = std::sin (inc);
  793. }
  794. }
  795. const auto norm = (float) (1.0 / std::sqrt ((double) lineCount));
  796. const auto early = (float) s.earlyLevel, late = (float) s.lateLevel, send = (float) s.earlySend;
  797. const auto gated = s.gateSec > 0.0, build = gated || s.buildSec > 0.0005;
  798. const auto split = s.buildSplit && ! gated; // round 190it: the build-up taps in four groups (below)
  799. const auto splitCoef = (float) (1.0 - std::exp (-2.0 * pi * splitHz() / sr));
  800. const auto crossAngle = 0.25 * pi * std::clamp (s.crossFeed, 0.0, 1.0);
  801. const auto crossCos0 = std::cos (crossAngle), crossSin0 = std::sin (crossAngle);
  802. const auto mixA = 0.25 * pi * std::clamp (s.mixAngle, 0.0, 1.0);
  803. const auto mc = (float) std::cos (mixA), msn = (float) std::sin (mixA);
  804. const auto ratio = std::pow (2.0, s.shiftSemis / 12.0), ratio2 = std::pow (2.0, s.shiftSemis2 / 12.0);
  805. const auto voice2 = (float) std::clamp (s.shift2Mix, 0.0, 1.0);
  806. const auto shimmer = (float) (std::clamp (s.shiftFeedback, 0.0, 1.0) * std::clamp (s.shiftMix, 0.0, 1.0));
  807. const auto width = (float) std::clamp (s.width, -2.0, 2.0), gain = (float) s.gain;
  808. float shimmerKeep[2] = { 1.0f, 1.0f }, shimmerB[2] = { 0.0f, 0.0f };
  809. const auto hpC = (float) (1.0 - std::exp (-2.0 * pi * 120.0 / sr));
  810. if (s.shimmerEnergy > 0.0)
  811. for (int v = 0; v < 2; ++v)
  812. {
  813. const auto a = (double) (v == 1 ? shimmer * voice2 : shimmer);
  814. const auto f = std::min (1.0, std::abs (v == 1 ? s.shiftSemis2 : s.shiftSemis) / 2.0);
  815. shimmerB[v] = (float) (s.shimmerEnergy * a * f);
  816. shimmerKeep[v] = 1.0f - shimmerB[v] * (float) (1.0 - f);
  817. }
  818. // onset network
  819. const auto onsetLevel = (float) s.onsetLevel, onsetFeed = (float) std::clamp (s.onsetFeed, 0.0, 1.0);
  820. const auto onsetFront = (float) std::clamp (s.onsetFront, 0.0, 1.0);
  821. const auto onsetOn = onsetLevel != 0.0f || onsetFeed != 0.0f;
  822. const auto splitShare = 0.5f * (1.0f - onsetFeed); // (revision 1's build groups, below)
  823. if (onsetOn)
  824. for (int k = 0; k < onsetSteps; ++k)
  825. for (int j = 0; j < onsetChans; ++j)
  826. onsetD[k][j] = std::clamp ((int) (std::clamp (s.onsetSec, 0.001, maxOnsetSec) * onsetSpan[k] * onsetFrac[k][j] * sr), 1,
  827. onsetLine[k][j].mask - 2);
  828. const auto bwOn = absorb[0].bw.a != 0.0f, lcOn = absorb[0].lc.a != 1.0f;
  829. constexpr float r2 = 0.70710678f; // (two inputs spread over four channels)
  830. constexpr float tiny = 1.0e-18f; // keeps the recirculating states out of the denormal range
  831. for (int t = 0; t < n; ++t)
  832. {
  833. float x[2] = { L[t], R[t] };
  834. float e[2], d[2], pin[2];
  835. float dg[2][4] {};
  836. for (int c = 0; c < 2; ++c)
  837. {
  838. pre[c].write (x[c]);
  839. const auto p = pd == 0 ? x[c] : pre[c].read (pd);
  840. pin[c] = p;
  841. raw[c].write (p);
  842. float er = 0.0f;
  843. if ((early != 0.0f || send != 0.0f) && eModDepth > 0.0)
  844. {
  845. double eLfo[4];
  846. for (int j = 0; j < 4; ++j)
  847. eLfo[j] = 0.5 * eModDepth * (1.0 + std::sin (eModPhase + 1.5707963267948966 * j + 0.9 * c));
  848. const auto longest = (double) (raw[c].mask - 3);
  849. for (int k = 0; k < earlyTaps; ++k)
  850. er += eW[c][k] * raw[c].readFrac (std::min (longest, (double) eD[c][k] + eLfo[k & 3]));
  851. }
  852. else if (early != 0.0f || send != 0.0f)
  853. for (int k = 0; k < earlyTaps; ++k)
  854. er += eW[c][k] * raw[c].read (eD[c][k]);
  855. if (eApG != 0.0f)
  856. for (auto& a : earlyAp[c])
  857. er = a.process (er, eApG);
  858. e[c] = er;
  859. auto v = p;
  860. for (int k = 0; k < 4; ++k)
  861. if (inG[k] != 0.0f)
  862. v = inAp[c][k].process (v, inG[k]);
  863. if (build && split)
  864. {
  865. // revision 1 (round 190it): four interleaved groups of the build-up taps, each spanning the whole
  866. // build, reach the four lines of the half through a 4-point Hadamard (below) instead of one FIR
  867. // into every line: one fixed random-sign FIR left its ripple (±3 … 4 dB, 50 … 200 Hz wide) on the
  868. // whole tail; four independent ones average it out (a steady sine meets the tail's mean gain)
  869. dif[c].write (v);
  870. // (round 190kg, perf: the four groups' sums in registers, each still added in the taps' order — the
  871. // indexed array went through memory at every tap)
  872. static_assert (buildTaps % 4 == 0);
  873. const auto tap = [&] (int k) noexcept { return bW[c][k] * (bD[c][k] == 0 ? v : dif[c].read (bD[c][k])); };
  874. float g4[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  875. {
  876. float g0 = 0.0f, g1 = 0.0f, g2 = 0.0f, g3 = 0.0f;
  877. for (int k = 0; k < buildTaps; k += 4)
  878. {
  879. g0 += tap (k);
  880. g1 += tap (k + 1);
  881. g2 += tap (k + 2);
  882. g3 += tap (k + 3);
  883. }
  884. g4[0] = g0;
  885. g4[1] = g1;
  886. g4[2] = g2;
  887. g4[3] = g3;
  888. }
  889. // below ~600 Hz the groups go on as one sum (the build's low band as before: there the few modes
  890. // of the tank, not the taps, set the tone, and music read them as before); above, apart
  891. float low = 0.0f;
  892. for (int g = 0; g < 4; ++g)
  893. {
  894. splitLp[c][g] += splitCoef * (g4[g] - splitLp[c][g]);
  895. low += splitLp[c][g];
  896. dg[c][g] = g4[g] - splitLp[c][g];
  897. }
  898. v = low;
  899. }
  900. else if (build)
  901. {
  902. dif[c].write (v);
  903. float b = 0.0f;
  904. for (int k = 0; k < buildTaps; ++k)
  905. b += bW[c][k] * (bD[c][k] == 0 ? v : dif[c].read (bD[c][k]));
  906. v = b;
  907. }
  908. d[c] = v;
  909. }
  910. // revision 1's build groups into the lines of each half: a 4-point Hadamard (signs per line as below), × the
  911. // build's share (round 190kg, perf: once per sample here instead of inside the line loop; the same operations)
  912. float splitIn[2][4];
  913. if (build && split)
  914. for (int c = 0; c < 2; ++c)
  915. {
  916. const auto* q = dg[c];
  917. splitIn[c][0] = splitShare * (q[0] + q[1] + q[2] + q[3]);
  918. splitIn[c][1] = splitShare * (q[0] + (-q[1]) + q[2] + (-q[3]));
  919. splitIn[c][2] = splitShare * (q[0] + q[1] + (-q[2]) + (-q[3]));
  920. splitIn[c][3] = splitShare * (q[0] + (-q[1]) + (-q[2]) + q[3]);
  921. }
  922. eModPhase += eModInc;
  923. if (eModPhase > 2.0 * pi)
  924. eModPhase -= 2.0 * pi;
  925. // onset: three diffusion steps on four channels
  926. float on[2] = { 0.0f, 0.0f }, onOut[2] = { 0.0f, 0.0f };
  927. if (onsetOn)
  928. {
  929. float oc[onsetChans] = { pin[0] * r2, pin[1] * r2, pin[0] * r2, pin[1] * r2 };
  930. float front[2] = { 0.0f, 0.0f };
  931. for (int k = 0; k < onsetSteps; ++k)
  932. {
  933. float dl[onsetChans];
  934. for (int j = 0; j < onsetChans; ++j)
  935. {
  936. onsetLine[k][j].write (oc[j]);
  937. dl[j] = onsetSign[k][j] * onsetLine[k][j].read (onsetD[k][j]);
  938. }
  939. const auto a0 = dl[0] + dl[1], a1 = dl[0] - dl[1], a2 = dl[2] + dl[3], a3 = dl[2] - dl[3];
  940. oc[0] = 0.5f * (a0 + a2);
  941. oc[1] = 0.5f * (a1 + a3);
  942. oc[2] = 0.5f * (a0 - a2);
  943. oc[3] = 0.5f * (a1 - a3);
  944. if (k + 1 < onsetSteps)
  945. {
  946. front[0] += oc[0];
  947. front[1] += oc[1];
  948. }
  949. }
  950. // two of the four channels (each holds every path), at the input's energy
  951. on[0] = 1.41421356f * oc[0];
  952. on[1] = 1.41421356f * oc[1];
  953. onOut[0] = onsetLevel * (on[0] + onsetFront * 1.41421356f * front[0]);
  954. onOut[1] = onsetLevel * (on[1] + onsetFront * 1.41421356f * front[1]);
  955. }
  956. // late tank
  957. float sOut[lineCount];
  958. for (int i = 0; i < lineCount; ++i)
  959. {
  960. curLen[i] += std::clamp (target[i] - curLen[i], -step[i], step[i]);
  961. if (s.lengthSpread > 0) // (round 190ie: the loop all-pass follows its gliding line)
  962. loopAp[i].len = std::clamp ((int) (curLen[i] * loopApFraction), 1, loopAp[i].line.mask - 2);
  963. const auto c = lfoC[i] * lfoRc[i] - lfoS[i] * lfoRs[i];
  964. lfoS[i] = lfoS[i] * lfoRc[i] + lfoC[i] * lfoRs[i];
  965. lfoC[i] = c;
  966. const auto dl = std::max (2.0, curLen[i] - modDepth * 0.5 * (1.0 + lfoS[i]));
  967. auto y = lines[i].readFrac (dl);
  968. if (apG[i] != 0.0f)
  969. y = loopAp[i].process (y, apG[i]);
  970. sOut[i] = y;
  971. }
  972. for (int k = 0; k < loopShelves; ++k)
  973. for (int i = 0; i < lineCount; ++i)
  974. {
  975. const auto y = shB0[k][i] * sOut[i] + shZ[k][i];
  976. shZ[k][i] = shB1[k][i] * sOut[i] - shA1[k][i] * y;
  977. sOut[i] = y;
  978. }
  979. if (bwOn)
  980. for (int i = 0; i < lineCount; ++i)
  981. sOut[i] = absorb[i].bw.lp (sOut[i]);
  982. if (lcOn)
  983. for (int i = 0; i < lineCount; ++i)
  984. sOut[i] -= absorb[i].lc.lp (sOut[i]);
  985. float lateL = (sOut[0] + sOut[1] + sOut[2] - sOut[3] + sOut[4] + sOut[5] - sOut[6] + sOut[7]) * norm;
  986. float lateR = (sOut[0] - sOut[1] + sOut[2] + sOut[3] + sOut[4] - sOut[5] + sOut[6] + sOut[7]) * norm;
  987. if (s.splitSides)
  988. {
  989. lateL = (sOut[0] + sOut[1] + sOut[2] - sOut[3]) * norm * 1.41421356f;
  990. lateR = (sOut[4] - sOut[5] + sOut[6] + sOut[7]) * norm * 1.41421356f;
  991. }
  992. const float in[2] = { (1.0f - onsetFeed) * (d[0] + send * e[0]) + onsetFeed * on[0] + tiny,
  993. (1.0f - onsetFeed) * (d[1] + send * e[1]) + onsetFeed * on[1] + tiny };
  994. if (gated)
  995. {
  996. lateL = 0.3f * lateL + d[0];
  997. lateR = 0.3f * lateR + d[1];
  998. }
  999. // mixing: each half through two layers of rotations by mixAngle × 45° (at 45° a normalised 4-point
  1000. // Hadamard up to signs), then the halves rotated into each other by crossFeed × 45°, swinging by the
  1001. // matrix modulator (orthogonal for any amount)
  1002. auto crossCos = (float) crossCos0, crossSin = (float) crossSin0;
  1003. float hc[2] = { mc, mc }, hs[2] = { msn, msn };
  1004. if (mmDepth > 0.0)
  1005. {
  1006. const auto c = mmC * mmRc - mmS * mmRs;
  1007. mmS = mmS * mmRc + mmC * mmRs;
  1008. mmC = c;
  1009. const auto dlt = mmDepth * mmS, d2 = dlt * dlt;
  1010. const auto cd = 1.0 - d2 * (0.5 - d2 * (1.0 / 24.0 - d2 * (1.0 / 720.0)));
  1011. const auto sd = dlt * (1.0 - d2 * (1.0 / 6.0 - d2 * (1.0 / 120.0 - d2 * (1.0 / 5040.0))));
  1012. crossCos = (float) (crossCos0 * cd - crossSin0 * sd);
  1013. crossSin = (float) (crossSin0 * cd + crossCos0 * sd);
  1014. // the rotations inside the halves swing too, the two halves in opposite ways
  1015. hc[0] = (float) (mc * cd - msn * sd);
  1016. hs[0] = (float) (msn * cd + mc * sd);
  1017. hc[1] = (float) (mc * cd + msn * sd);
  1018. hs[1] = (float) (msn * cd - mc * sd);
  1019. }
  1020. float h[lineCount];
  1021. for (int half = 0; half < lineCount; half += 4)
  1022. {
  1023. const auto qc = hc[half / 4], qs = hs[half / 4];
  1024. const auto a0 = qc * sOut[half] + qs * sOut[half + 1], a1 = qc * sOut[half + 1] - qs * sOut[half];
  1025. const auto a2 = qc * sOut[half + 2] + qs * sOut[half + 3], a3 = qc * sOut[half + 3] - qs * sOut[half + 2];
  1026. h[half] = qc * a0 + qs * a2;
  1027. h[half + 2] = qc * a2 - qs * a0;
  1028. h[half + 1] = qc * a1 + qs * a3;
  1029. h[half + 3] = qc * a3 - qs * a1;
  1030. }
  1031. for (int i = 0; i < 4; ++i)
  1032. {
  1033. const auto a = h[i], b = h[i + 4];
  1034. auto u = crossCos * a + crossSin * b, w = -crossSin * a + crossCos * b;
  1035. if (shimmer > 0.0f && (i & 1) == 0)
  1036. {
  1037. const auto second = i == 2 && voice2 > 0.0f;
  1038. const auto r = second ? ratio2 : ratio;
  1039. const auto amount = second ? shimmer * voice2 : shimmer;
  1040. if (s.shimmerEnergy > 0.0)
  1041. {
  1042. const auto bb = shimmerB[second ? 1 : 0], keep = shimmerKeep[second ? 1 : 0];
  1043. auto hp = [&] (int k, float v)
  1044. {
  1045. shiftHp[k][0] += hpC * (v - shiftHp[k][0]);
  1046. v -= shiftHp[k][0];
  1047. shiftHp[k][1] += hpC * (v - shiftHp[k][1]);
  1048. return v - shiftHp[k][1];
  1049. };
  1050. u = keep * u + bb * hp (i / 2, shifters[i / 2].process (u, r));
  1051. w = keep * w + bb * hp (2 + i / 2, shifters[2 + i / 2].process (w, r));
  1052. }
  1053. else
  1054. {
  1055. u = (1.0f - amount) * u + amount * shifters[i / 2].process (u, r);
  1056. w = (1.0f - amount) * w + amount * shifters[2 + i / 2].process (w, r);
  1057. }
  1058. }
  1059. const auto alt = (i & 1) == 0 ? 0.5f : -0.5f;
  1060. if (build && split)
  1061. {
  1062. lines[i].write (u + alt * in[0] + splitIn[0][i]);
  1063. lines[i + 4].write (w + alt * in[1] + splitIn[1][i]);
  1064. }
  1065. else
  1066. {
  1067. lines[i].write (u + alt * in[0]);
  1068. lines[i + 4].write (w + alt * in[1]);
  1069. }
  1070. }
  1071. if ((++renorm & 8191u) == 0) // the rotating pairs drift by rounding: back onto the unit circle
  1072. {
  1073. for (int i = 0; i < lineCount; ++i)
  1074. {
  1075. const auto m = 1.0 / std::sqrt (lfoC[i] * lfoC[i] + lfoS[i] * lfoS[i]);
  1076. lfoC[i] *= m;
  1077. lfoS[i] *= m;
  1078. }
  1079. const auto m = 1.0 / std::sqrt (mmC * mmC + mmS * mmS);
  1080. mmC *= m;
  1081. mmS *= m;
  1082. }
  1083. auto ol = late * lateL + early * e[0] + onOut[0];
  1084. auto orr = late * lateR + early * e[1] + onOut[1];
  1085. const auto mid = 0.5f * (ol + orr), side = 0.5f * (ol - orr) * width;
  1086. L[t] = (mid + side) * gain;
  1087. R[t] = (mid - side) * gain;
  1088. }
  1089. if (toneOn)
  1090. for (int k = 0; k < engineBands; ++k)
  1091. if (geqOn[k])
  1092. geq[k].processBlock (L, R, n);
  1093. for (int k = 0; k < 3; ++k)
  1094. {
  1095. bellOn[k] = std::abs (s.bellDb[k]) > 0.01;
  1096. if (bellOn[k])
  1097. {
  1098. bell[k].peaking (sr, std::clamp (s.bellHz[k], 20.0, 0.45 * sr), std::clamp (s.bellQ[k], 0.1, 12.0), std::clamp (s.bellDb[k], -18.0, 18.0));
  1099. bell[k].processBlock (L, R, n);
  1100. }
  1101. }
  1102. outputFilters (L, R, n);
  1103. last = s;
  1104. }
  1105. void outputFilters (float* L, float* R, int n) noexcept
  1106. {
  1107. if (outLowOn)
  1108. outLow.processBlock (L, R, n);
  1109. if (outHighOn)
  1110. outHigh.processBlock (L, R, n);
  1111. if (bandOn)
  1112. for (int k = 0; k < bandSections; ++k)
  1113. band[k].processBlock (L, R, n);
  1114. if (toneHighOn)
  1115. toneHigh.processBlock (L, R, n);
  1116. if (toneLowOn)
  1117. toneLow.processBlock (L, R, n);
  1118. if (toneMidOn)
  1119. toneMid.processBlock (L, R, n);
  1120. if (shelfLowOn)
  1121. shelfLow.processBlock (L, R, n);
  1122. if (shelfHighOn)
  1123. shelfHigh.processBlock (L, R, n);
  1124. }
  1125. void process (float* L, float* R, int n, const Settings& s) noexcept
  1126. {
  1127. if (s.law == 1 && s.feedback < 0.0 && ! s.parallelLines)
  1128. {
  1129. processLaw1 (L, R, n, s);
  1130. return;
  1131. }
  1132. update (s);
  1133. const auto pd = std::clamp ((int) std::lround (s.predelaySec * sr), 0, pre[0].mask - 2);
  1134. // loop all-pass lengths follow the line lengths (fixed per block)
  1135. for (int i = 0; i < lineCount; ++i)
  1136. {
  1137. const auto target = targetLen (s, i);
  1138. if (first)
  1139. curLen[i] = target;
  1140. loopAp[i].len = std::clamp ((int) (target * loopApFraction), 1, loopAp[i].line.mask - 2);
  1141. }
  1142. first = false;
  1143. decayGains (s);
  1144. const auto glide = 0.25; // samples per sample: a length change glides like tape
  1145. const auto norm = (float) (1.0 / std::sqrt ((double) lineCount));
  1146. const auto early = (float) s.earlyLevel, late = (float) s.lateLevel, send = (float) s.earlySend;
  1147. const auto gated = s.gateSec > 0.0, build = gated || s.buildSec > 0.0005;
  1148. const auto parallel = s.parallelLines;
  1149. const auto crossAngle = parallel ? 0.0 : 0.25 * pi * std::clamp (s.crossFeed, 0.0, 1.0);
  1150. const auto crossCos = (float) std::cos (crossAngle), crossSin = (float) std::sin (crossAngle);
  1151. const auto ratio = std::pow (2.0, s.shiftSemis / 12.0), ratio2 = std::pow (2.0, s.shiftSemis2 / 12.0);
  1152. const auto voice2 = (float) std::clamp (s.shift2Mix, 0.0, 1.0);
  1153. // shimmer: a part of what four of the lines write back goes through a pitch shifter. (1 − a)² + a² ≤ 1 and
  1154. // the shifted part is decorrelated, so the loop keeps the decay the absorption filters set: always stable
  1155. const auto shimmer = (float) (std::clamp (s.shiftFeedback, 0.0, 1.0) * std::clamp (s.shiftMix, 0.0, 1.0));
  1156. const auto width = (float) std::clamp (s.width, -2.0, 2.0), gain = (float) s.gain;
  1157. // round tp172d: the injected shimmer (Halo). The plain crossfade moved a share of every pass up an octave,
  1158. // where the absorption takes it: a shimmer tail died in ~2 s where the reference's keeps the RT of its size
  1159. // (the reference's RT barely depends on the shift). Here the line keeps all of itself and the shifted voice is
  1160. // added on top (high-passed at 120 Hz: the grains' slow amplitude modulation would pile up below it), small
  1161. // enough that the loop stays below unity at the longest RT; it fades in over the first 2 semitones, where
  1162. // the voices are still correlated (unshifted, the feedback is the RT law's alone)
  1163. float shimmerKeep[2] = { 1.0f, 1.0f }, shimmerB[2] = { 0.0f, 0.0f };
  1164. const auto hpC = (float) (1.0 - std::exp (-2.0 * pi * 120.0 / sr));
  1165. if (s.shimmerEnergy > 0.0)
  1166. for (int v = 0; v < 2; ++v)
  1167. {
  1168. const auto a = (double) (v == 1 ? shimmer * voice2 : shimmer);
  1169. const auto f = std::min (1.0, std::abs (v == 1 ? s.shiftSemis2 : s.shiftSemis) / 2.0);
  1170. shimmerB[v] = (float) (s.shimmerEnergy * a * f);
  1171. shimmerKeep[v] = 1.0f - shimmerB[v] * (float) (1.0 - f); // (unshifted: plain feedback, the RT law's own)
  1172. }
  1173. double target[lineCount], step[lineCount];
  1174. for (int i = 0; i < lineCount; ++i)
  1175. {
  1176. target[i] = targetLen (s, i);
  1177. // round 190ie: the glide per sample (fixed, or a tempo change spread over the block)
  1178. step[i] = s.lengthSpread > 0 ? std::min (glide, std::abs (target[i] - curLen[i]) / (double) s.lengthSpread) : glide;
  1179. }
  1180. for (int t = 0; t < n; ++t)
  1181. {
  1182. float x[2] = { L[t], R[t] };
  1183. float e[2], d[2];
  1184. for (int c = 0; c < 2; ++c)
  1185. {
  1186. pre[c].write (x[c]);
  1187. const auto p = pd == 0 ? x[c] : pre[c].read (pd);
  1188. raw[c].write (p);
  1189. // early reflections
  1190. float er = 0.0f;
  1191. if ((early != 0.0f || send != 0.0f) && eModDepth > 0.0) // round 170c: modulated taps
  1192. {
  1193. double eLfo[4];
  1194. for (int j = 0; j < 4; ++j)
  1195. eLfo[j] = 0.5 * eModDepth * (1.0 + std::sin (eModPhase + 1.5707963267948966 * j + 0.9 * c));
  1196. const auto longest = (double) (raw[c].mask - 3);
  1197. for (int k = 0; k < earlyTaps; ++k)
  1198. er += eW[c][k] * raw[c].readFrac (std::min (longest, (double) eD[c][k] + eLfo[k & 3]));
  1199. }
  1200. else if (early != 0.0f || send != 0.0f)
  1201. for (int k = 0; k < earlyTaps; ++k)
  1202. er += eW[c][k] * raw[c].read (eD[c][k]);
  1203. if (eApG != 0.0f)
  1204. for (auto& a : earlyAp[c])
  1205. er = a.process (er, eApG);
  1206. e[c] = er;
  1207. // input diffusion
  1208. auto v = p;
  1209. for (int k = 0; k < 4; ++k)
  1210. if (inG[k] != 0.0f)
  1211. v = inAp[c][k].process (v, inG[k]);
  1212. if (build)
  1213. {
  1214. dif[c].write (v);
  1215. float b = 0.0f;
  1216. for (int k = 0; k < buildTaps; ++k)
  1217. b += bW[c][k] * (bD[c][k] == 0 ? v : dif[c].read (bD[c][k]));
  1218. v = b;
  1219. }
  1220. d[c] = v;
  1221. }
  1222. // late tank
  1223. eModPhase += eModInc;
  1224. if (eModPhase > 2.0 * pi)
  1225. eModPhase -= 2.0 * pi;
  1226. float sOut[lineCount];
  1227. for (int i = 0; i < lineCount; ++i)
  1228. {
  1229. curLen[i] += std::clamp (target[i] - curLen[i], -step[i], step[i]);
  1230. if (s.lengthSpread > 0) // (round 190ie: the loop all-pass follows its gliding line)
  1231. loopAp[i].len = std::clamp ((int) (curLen[i] * loopApFraction), 1, loopAp[i].line.mask - 2);
  1232. lfo[i] += modInc[i];
  1233. if (lfo[i] > 2.0 * pi)
  1234. lfo[i] -= 2.0 * pi;
  1235. const auto dl = std::max (2.0, curLen[i] - modDepth * 0.5 * (1.0 + std::sin (lfo[i])));
  1236. auto y = lines[i].readFrac (dl);
  1237. if (apG[i] != 0.0f)
  1238. y = loopAp[i].process (y, apG[i]);
  1239. auto& a = absorb[i];
  1240. const auto lo = a.lo.lp (y), hiLp = a.hi.lp (y);
  1241. y = a.gM * y + (a.gL - a.gM) * lo + (a.gH - a.gM) * (y - hiLp);
  1242. if (a.bw.a != 0.0f)
  1243. y = a.bw.lp (y);
  1244. if (a.lc.a != 1.0f)
  1245. y -= a.lc.lp (y);
  1246. sOut[i] = y;
  1247. }
  1248. // outputs: two orthogonal sign patterns
  1249. // (neither pattern sums to zero, so low frequencies, where the lines move together, are kept)
  1250. float lateL = (sOut[0] + sOut[1] + sOut[2] - sOut[3] + sOut[4] + sOut[5] - sOut[6] + sOut[7]) * norm;
  1251. float lateR = (sOut[0] - sOut[1] + sOut[2] + sOut[3] + sOut[4] - sOut[5] + sOut[6] + sOut[7]) * norm;
  1252. if (s.splitSides) // two separate halves (round 135): each side hears only its own four lines
  1253. {
  1254. lateL = (sOut[0] + sOut[1] + sOut[2] - sOut[3]) * norm * 1.41421356f;
  1255. lateR = (sOut[4] - sOut[5] + sOut[6] + sOut[7]) * norm * 1.41421356f;
  1256. }
  1257. const float in[2] = { d[0] + send * e[0], d[1] + send * e[1] };
  1258. if (gated)
  1259. {
  1260. // gated: the dense tap block is the sound; the short tank only smooths it
  1261. lateL = 0.3f * lateL + d[0];
  1262. lateR = 0.3f * lateR + d[1];
  1263. }
  1264. // mixing: each half of the tank (lines 0–3 fed from the left, 4–7 from the right) through a normalised 4-point
  1265. // Hadamard, then the halves rotated into each other by crossFeed × 45° (orthogonal for any amount)
  1266. float h[lineCount];
  1267. if (parallel)
  1268. for (int i = 0; i < lineCount; ++i)
  1269. h[i] = sOut[i];
  1270. for (int half = 0; half < lineCount && ! parallel; half += 4)
  1271. {
  1272. const auto a0 = sOut[half] + sOut[half + 1], a1 = sOut[half] - sOut[half + 1];
  1273. const auto a2 = sOut[half + 2] + sOut[half + 3], a3 = sOut[half + 2] - sOut[half + 3];
  1274. h[half] = 0.5f * (a0 + a2);
  1275. h[half + 1] = 0.5f * (a1 + a3);
  1276. h[half + 2] = 0.5f * (a0 - a2);
  1277. h[half + 3] = 0.5f * (a1 - a3);
  1278. }
  1279. for (int i = 0; i < 4; ++i)
  1280. {
  1281. const auto a = h[i], b = h[i + 4];
  1282. // parallel lines (plain echoes): input signs = the output patterns of their own side, so the left input
  1283. // reaches only the left output and the right only the right
  1284. constexpr float injL[4] = { 0.5f, 0.5f, 0.5f, -0.5f }, injR[4] = { 0.5f, -0.5f, 0.5f, 0.5f };
  1285. auto u = crossCos * a + crossSin * b, w = -crossSin * a + crossCos * b;
  1286. if (shimmer > 0.0f && (i & 1) == 0)
  1287. {
  1288. // the second voice (dual modes) takes line 2 and line 6
  1289. const auto r = i == 2 && voice2 > 0.0f ? ratio2 : ratio;
  1290. const auto amount = i == 2 && voice2 > 0.0f ? shimmer * voice2 : shimmer;
  1291. if (s.shimmerEnergy > 0.0)
  1292. {
  1293. const auto b = shimmerB[i == 2 && voice2 > 0.0f ? 1 : 0], keep = shimmerKeep[i == 2 && voice2 > 0.0f ? 1 : 0];
  1294. auto hp = [&] (int k, float x)
  1295. {
  1296. shiftHp[k][0] += hpC * (x - shiftHp[k][0]);
  1297. x -= shiftHp[k][0];
  1298. shiftHp[k][1] += hpC * (x - shiftHp[k][1]);
  1299. return x - shiftHp[k][1];
  1300. };
  1301. u = keep * u + b * hp (i / 2, shifters[i / 2].process (u, r));
  1302. w = keep * w + b * hp (2 + i / 2, shifters[2 + i / 2].process (w, i == 2 && voice2 > 0.0f ? ratio2 : ratio));
  1303. }
  1304. else
  1305. {
  1306. u = (1.0f - amount) * u + amount * shifters[i / 2].process (u, r);
  1307. w = (1.0f - amount) * w + amount * shifters[2 + i / 2].process (w, i == 2 && voice2 > 0.0f ? ratio2 : ratio);
  1308. }
  1309. }
  1310. const auto alt = (i & 1) == 0 ? 0.5f : -0.5f; // reverbs: decorrelated sides from the first pass on
  1311. lines[i].write (u + (parallel ? injL[i] : alt) * in[0]);
  1312. lines[i + 4].write (w + (parallel ? injR[i] : alt) * in[1]);
  1313. }
  1314. auto ol = late * lateL + early * e[0];
  1315. auto orr = late * lateR + early * e[1];
  1316. // width (M/S)
  1317. const auto mid = 0.5f * (ol + orr), side = 0.5f * (ol - orr) * width;
  1318. L[t] = (mid + side) * gain;
  1319. R[t] = (mid - side) * gain;
  1320. }
  1321. if (outLowOn)
  1322. outLow.processBlock (L, R, n);
  1323. if (outHighOn)
  1324. outHigh.processBlock (L, R, n);
  1325. if (bandOn)
  1326. for (int k = 0; k < bandSections; ++k)
  1327. band[k].processBlock (L, R, n);
  1328. if (toneHighOn)
  1329. toneHigh.processBlock (L, R, n);
  1330. if (toneLowOn)
  1331. toneLow.processBlock (L, R, n);
  1332. if (toneMidOn)
  1333. toneMid.processBlock (L, R, n);
  1334. if (shelfLowOn)
  1335. shelfLow.processBlock (L, R, n);
  1336. if (shelfHighOn)
  1337. shelfHigh.processBlock (L, R, n);
  1338. last = s;
  1339. }
  1340. };
  1341. Engine::Engine (double maxLineSec, double maxPredelaySec) : impl (std::make_unique<Impl> (maxLineSec, maxPredelaySec)) {}
  1342. Engine::~Engine() = default;
  1343. void Engine::prepare (double sampleRate) { impl->prepare (sampleRate); }
  1344. void Engine::reset() noexcept { impl->reset(); }
  1345. void Engine::process (float* left, float* right, int n, const Settings& s) noexcept { impl->process (left, right, n, s); }
  1346. // =================================================================================================================
  1347. // front-ends
  1348. namespace
  1349. {
  1350. // a 0..1 control as % text
  1351. std::string pct (double v) { char b[32]; std::snprintf (b, sizeof b, "%.0f%%", v * 100.0); return b; }
  1352. std::string fmt (const char* f, double v) { char b[48]; std::snprintf (b, sizeof b, f, v); return b; }
  1353. std::string hz (double v) { return v >= 1000.0 ? fmt ("%.2f kHz", v / 1000.0) : fmt ("%.0f Hz", v); }
  1354. std::string ms (double v) { return v >= 1000.0 ? fmt ("%.2f s", v / 1000.0) : fmt ("%.1f ms", v); }
  1355. std::string sec (double v) { return v >= 10.0 ? fmt ("%.1f s", v) : fmt ("%.2f s", v); }
  1356. // the decay a front-end asks for, with a floor for modes whose structure cannot decay faster (measured)
  1357. double withFloor (double rt, double floor) { return std::sqrt (rt * rt + floor * floor); }
  1358. // note lengths of the tempo-synced delays (quarter notes)
  1359. constexpr double noteQuarters[] = { 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0 / 2, 1.0, 2.0, 4.0 };
  1360. const char* const noteNames[] = { "1/64", "1/32", "1/16", "1/8", "1/4", "1/2", "1/1" };
  1361. // Delay time of a synced control set: sync 0 = ms, 1 = note, 2 = dotted, 3 = triplet
  1362. double syncedMs (int sync, int note, double msValue, double bpm)
  1363. {
  1364. if (sync <= 0 || bpm <= 0.0)
  1365. return msValue;
  1366. auto q = noteQuarters[std::clamp (note, 0, 6)] * 60000.0 / bpm;
  1367. if (sync == 2)
  1368. q *= 1.5;
  1369. else if (sync == 3)
  1370. q *= 2.0 / 3.0;
  1371. return q;
  1372. }
  1373. // ---- law 1 (round 190bz): what a mode / type adds on the 0.1.27 engine law, neutral by default. Fitted with the tail
  1374. // lab (scripts/taillab.py) to black-box renders of the reference each effect stands in for.
  1375. struct LawVoicing
  1376. {
  1377. double rtMult[engineBands] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }; // decay per octave band × the classic law's
  1378. double db[engineBands] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; // tone corrector, dB per octave band
  1379. double gainDb = 0.0; // wet level
  1380. double onset = 0.0, onsetMs = 30.0, front = 0.0, feed = 0.0; // onset network: level, span, front weight, tank feed
  1381. double early = 1.0; // × the classic early taps' level
  1382. double angle = 1.0; // mixing inside the halves (× 45°)
  1383. double mmDepth = 0.0, mmRate = 0.3; // matrix modulation (rad, Hz)
  1384. double mod = 1.0; // × the delay modulation's depth
  1385. double bandLimit = 1.0; // × the steep band limit's frequency
  1386. double bellHz[3] = { 100.0, 1000.0, 5000.0 }, bellDb[3] = { 0.0, 0.0, 0.0 }, bellQ[3] = { 1.4, 1.4, 1.4 }; // narrow colour
  1387. };
  1388. void applyLaw (Settings& s, const LawVoicing& v) noexcept
  1389. {
  1390. s.law = 1;
  1391. for (int b = 0; b < engineBands; ++b)
  1392. {
  1393. s.bandRtMult[b] = v.rtMult[b];
  1394. s.bandDb[b] = v.db[b];
  1395. }
  1396. s.gain *= std::pow (10.0, v.gainDb / 20.0);
  1397. s.onsetLevel = v.onset;
  1398. s.onsetSec = v.onsetMs / 1000.0;
  1399. s.onsetFront = v.front;
  1400. s.onsetFeed = v.feed;
  1401. s.earlyLevel *= v.early;
  1402. s.mixAngle = v.angle;
  1403. s.matrixModDepth = v.mmDepth;
  1404. s.matrixModRateHz = v.mmRate;
  1405. s.modDepthMs *= v.mod;
  1406. s.bandLimit *= v.bandLimit;
  1407. for (int k = 0; k < 3; ++k)
  1408. {
  1409. s.bellHz[k] = v.bellHz[k];
  1410. s.bellDb[k] = v.bellDb[k];
  1411. s.bellQ[k] = v.bellQ[k];
  1412. }
  1413. }
  1414. // between two voicings (t = 0 … 1): decay multipliers and bell frequencies on a ratio scale, the rest linear
  1415. LawVoicing blendLaw (const LawVoicing& a, const LawVoicing& b, double t) noexcept
  1416. {
  1417. t = std::clamp (t, 0.0, 1.0);
  1418. const auto lin = [t] (double x, double y) { return x + (y - x) * t; };
  1419. const auto geo = [t] (double x, double y) { return x > 0.0 && y > 0.0 ? x * std::pow (y / x, t) : x + (y - x) * t; };
  1420. LawVoicing v;
  1421. for (int i = 0; i < engineBands; ++i)
  1422. {
  1423. v.rtMult[i] = geo (a.rtMult[i], b.rtMult[i]);
  1424. v.db[i] = lin (a.db[i], b.db[i]);
  1425. }
  1426. v.gainDb = lin (a.gainDb, b.gainDb);
  1427. v.onset = lin (a.onset, b.onset);
  1428. v.onsetMs = lin (a.onsetMs, b.onsetMs);
  1429. v.front = lin (a.front, b.front);
  1430. v.feed = lin (a.feed, b.feed);
  1431. v.early = lin (a.early, b.early);
  1432. v.angle = lin (a.angle, b.angle);
  1433. v.mmDepth = lin (a.mmDepth, b.mmDepth);
  1434. v.mmRate = geo (a.mmRate, b.mmRate);
  1435. v.mod = geo (a.mod, b.mod);
  1436. v.bandLimit = geo (a.bandLimit, b.bandLimit);
  1437. for (int k = 0; k < 3; ++k)
  1438. {
  1439. v.bellHz[k] = geo (a.bellHz[k], b.bellHz[k]);
  1440. v.bellDb[k] = lin (a.bellDb[k], b.bellDb[k]);
  1441. v.bellQ[k] = geo (a.bellQ[k], b.bellQ[k]);
  1442. }
  1443. return v;
  1444. }
  1445. #ifdef LYNX_VERB_FIT
  1446. // the fitting harness: LYNX_VFIT="key=value;key=value" changes law 1's settings after the front-end has set them
  1447. void fitOverride (Settings& s)
  1448. {
  1449. static const std::string spec = [] { const char* e = std::getenv ("LYNX_VFIT"); return std::string (e != nullptr ? e : ""); }();
  1450. if (spec.empty() || s.law != 1)
  1451. return;
  1452. std::size_t at = 0;
  1453. while (at < spec.size())
  1454. {
  1455. const auto end = std::min (spec.find (';', at), spec.size());
  1456. const auto item = spec.substr (at, end - at);
  1457. at = end + 1;
  1458. const auto eq = item.find ('=');
  1459. if (eq == std::string::npos)
  1460. continue;
  1461. const auto k = item.substr (0, eq);
  1462. const auto v = std::atof (item.substr (eq + 1).c_str());
  1463. if (k.size() == 3 && k[0] == 'r' && k[1] == 't' && k[2] >= '0' && k[2] <= '7') s.bandRtMult[k[2] - '0'] *= v;
  1464. else if (k.size() == 3 && k[0] == 'd' && k[1] == 'b' && k[2] >= '0' && k[2] <= '7') s.bandDb[k[2] - '0'] += v;
  1465. else if (k == "gain") s.gain *= std::pow (10.0, v / 20.0);
  1466. else if (k == "onset") s.onsetLevel = v;
  1467. else if (k == "onsetms") s.onsetSec = v / 1000.0;
  1468. else if (k == "front") s.onsetFront = v;
  1469. else if (k == "feed") s.onsetFeed = v;
  1470. else if (k == "early") s.earlyLevel *= v;
  1471. else if (k == "late") s.lateLevel *= v;
  1472. else if (k == "angle") s.mixAngle = v;
  1473. else if (k == "mmd") s.matrixModDepth = v;
  1474. else if (k == "mmr") s.matrixModRateHz = v;
  1475. else if (k == "mod") s.modDepthMs *= v;
  1476. else if (k == "indiff") s.inDiffusion *= v;
  1477. else if (k == "loopdiff") s.loopDiffusion *= v;
  1478. else if (k == "pre") s.predelaySec += v / 1000.0;
  1479. else if (k == "build") s.buildSec *= v;
  1480. else if (k == "buildms") s.buildSec = v / 1000.0;
  1481. else if (k == "scale") s.lineScale *= v;
  1482. else if (k == "rt") s.rt60 *= v;
  1483. else if (k == "cross") s.crossFeed = v;
  1484. else if (k == "width") s.width *= v;
  1485. else if (k == "earlyms") s.earlySec = v / 1000.0;
  1486. else if (k == "bl") s.bandLimit *= v;
  1487. else if (k == "rev") s.revision = (int) v;
  1488. else if (k == "split") s.buildSplit = v >= 0.5;
  1489. else if (k == "lbw") s.loopBandwidth = v;
  1490. else if (k == "hcut") s.outHighCut = v;
  1491. else if (k == "lcut") s.outLowCut = v;
  1492. else if (k == "lsdb") s.lowShelfDb = v;
  1493. else if (k == "lshz") s.lowShelfHz = v;
  1494. else if (k == "hsdb") s.highShelfDb = v;
  1495. else if (k == "hshz") s.highShelfHz = v;
  1496. else if (k == "tmdb") s.toneMidDb = v;
  1497. else if (k == "ebuild") s.buildRise = v;
  1498. else if (k == "shim") s.shimmerEnergy *= v;
  1499. else if (k == "shfb") s.shiftFeedback *= v;
  1500. else if (k == "lowmult") s.lowMult *= v;
  1501. else if (k == "highmult") s.highMult *= v;
  1502. else if (k.size() == 3 && k[0] == 'b' && (k[1] == 'f' || k[1] == 'g' || k[1] == 'q') && k[2] >= '0' && k[2] <= '2')
  1503. (k[1] == 'f' ? s.bellHz : k[1] == 'g' ? s.bellDb : s.bellQ)[k[2] - '0'] = v;
  1504. }
  1505. }
  1506. #endif
  1507. class VerbEffect : public Effect
  1508. {
  1509. public:
  1510. VerbEffect (EffectKind k, double maxLineSec, double maxPreSec) : kind (k), engine (maxLineSec, maxPreSec) {}
  1511. void prepare (double sampleRate) noexcept override
  1512. {
  1513. rate = sampleRate;
  1514. engine.prepare (sampleRate);
  1515. }
  1516. void reset() noexcept override
  1517. {
  1518. engine.reset();
  1519. fade = Fade::none;
  1520. started = false;
  1521. }
  1522. void process (float* left, float* right, int n, const EffectParams& p, double bpm) noexcept override
  1523. {
  1524. // round 173d: a new type / mode re-tunes the whole tank at once (line lengths, taps, band limits): the wet part
  1525. // fades out on the old one, the new one starts and fades in (11 ms each way) instead of a click of 10…15 dB
  1526. const auto st = structure (p);
  1527. if (! started)
  1528. lastStructure = st, started = true;
  1529. if (st != lastStructure && fade == Fade::none)
  1530. {
  1531. fade = Fade::out;
  1532. fadeLeft = fadeLength;
  1533. held = last;
  1534. }
  1535. for (int done = 0; done < n;)
  1536. {
  1537. const auto m = fade == Fade::none ? n - done : std::min (n - done, fadeLeft);
  1538. run (left + done, right + done, m, fade == Fade::out ? held : p, bpm);
  1539. done += m;
  1540. if (fade != Fade::none && (fadeLeft -= m) <= 0)
  1541. {
  1542. if (fade == Fade::out)
  1543. fade = Fade::in, fadeLeft = fadeLength, lastStructure = st;
  1544. else
  1545. fade = Fade::none;
  1546. }
  1547. }
  1548. last = p;
  1549. }
  1550. protected:
  1551. // the setting that rebuilds the tank's structure (its type / mode), −1 = none
  1552. virtual int structure (const EffectParams&) const noexcept { return -1; }
  1553. virtual void configure (const EffectParams& p, double bpm, Settings& s, float& mix) const noexcept = 0;
  1554. virtual void preStage (float*, float*, int, const EffectParams&, double) noexcept {}
  1555. virtual void postStage (float*, float*, int, const EffectParams&, double) noexcept {}
  1556. void run (float* left, float* right, int n, const EffectParams& p, double bpm) noexcept
  1557. {
  1558. Settings s;
  1559. float mix = 1.0f;
  1560. configure (p, bpm, s, mix);
  1561. #ifdef LYNX_VERB_FIT
  1562. fitOverride (s);
  1563. #endif
  1564. float dry, wet;
  1565. mixGains (mix, dry, wet);
  1566. for (int start = 0; start < n; start += chunk)
  1567. {
  1568. const auto m = std::min (chunk, n - start);
  1569. auto* l = left + start;
  1570. auto* r = right + start;
  1571. std::copy (l, l + m, dl.begin());
  1572. std::copy (r, r + m, dr.begin());
  1573. preStage (l, r, m, p, bpm);
  1574. if (s.lengthSpread > 0) // (round 190ie: a tempo change spreads over each chunk it covers)
  1575. {
  1576. auto spread = s;
  1577. spread.lengthSpread = m;
  1578. engine.process (l, r, m, spread);
  1579. }
  1580. else
  1581. engine.process (l, r, m, s);
  1582. postStage (l, r, m, p, bpm);
  1583. for (int i = 0; i < m; ++i)
  1584. {
  1585. auto w = wet;
  1586. if (fade != Fade::none)
  1587. {
  1588. const auto g = (float) (fadeLeft - start - i) / (float) fadeLength; // 1 … 0 over the fade
  1589. w *= fade == Fade::out ? g : 1.0f - g;
  1590. }
  1591. l[i] = dry * dl[(std::size_t) i] + w * l[i];
  1592. r[i] = dry * dr[(std::size_t) i] + w * r[i];
  1593. }
  1594. }
  1595. }
  1596. static constexpr int chunk = 1024;
  1597. static constexpr int fadeLength = 512;
  1598. enum class Fade { none, out, in };
  1599. Fade fade = Fade::none;
  1600. int fadeLeft = 0, lastStructure = -1;
  1601. bool started = false;
  1602. EffectParams last {}, held {};
  1603. EffectKind kind;
  1604. Engine engine;
  1605. double rate = 48000.0;
  1606. std::array<float, chunk> dl {}, dr {};
  1607. };
  1608. // ---------------------------------------------------------------------------------------------- Epoch Verb
  1609. // Laws [EXEC, REA_rodada100 §1]: predelay 0..500 ms (v^2.32), decay 0.2..70 s (v^3.36), bass mult 0.25..4 X (v^2.32),
  1610. // bass crossover 100..10000 Hz (v^4.04), high shelf −24..0 dB, high freq / high cut 100..20000 Hz (v^1.754),
  1611. // mod rate 0.1..10 Hz, low cut 10..1500 Hz.
  1612. struct EpochMode
  1613. {
  1614. const char* name;
  1615. int shape;
  1616. double scale, rtScale, rtFloor, lowRt, inDiff, early, earlyMs, buildMs, modScale, gain, preMs, gate;
  1617. };
  1618. // shape, line scale, RT / Decay, RT floor (s), bass RT ×, input diffusion, early level / span (ms), build-up (ms),
  1619. // modulation scale, output gain, own pre-delay (ms), gate (fraction of the decay; 0 = none) — fitted to §1.
  1620. // (LYNX_VERB_FIT: the fitting harness builds this file with the tables writable)
  1621. #ifdef LYNX_VERB_FIT
  1622. #define LYNX_FIT_TABLE
  1623. #else
  1624. #define LYNX_FIT_TABLE const
  1625. #endif
  1626. // round 190it: revision 1's trims per type / mode, measured on refcap030's black-box captures (each type / mode at the
  1627. // capture's base setting against the reference's impulse): the wet level (dB) and the tone corrector (dB added per octave
  1628. // band, 63 Hz … 8 kHz). Neutral = the 0.1.27 Measured law
  1629. struct Rev1Trim
  1630. {
  1631. double gainDb = 0.0;
  1632. double db[engineBands] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
  1633. };
  1634. // Epoch Verb ← ValhallaVintageVerb's ReverbMode sweep (base Decay 0.51 s; two passes of the closed loop; Concert Hall, the
  1635. // mode of every other capture, keeps the 0.1.27 voicing); faded out by Decay 1.41 s (the front-end)
  1636. LYNX_FIT_TABLE Rev1Trim epochTrim[epoch::modeCount] = {
  1637. {}, // Concert Hall
  1638. { 9.61, { -4.69, -4.20, -0.99, -0.50, -0.90, 0.78, 5.83, 8.39 } }, // Plate
  1639. { 2.64, { 1.64, 1.89, -0.39, -1.30, 0.48, 0.52, -1.21, -1.82 } }, // Room
  1640. { 5.75, { -5.15, -3.53, 0.12, -0.79, -1.89, 0.86, 5.24, 8.18 } }, // Chamber
  1641. { 0.78, { -0.70, -0.01, -1.31, -2.10, -0.64, 1.80, 2.26, -1.39 } }, // Random Space
  1642. { 6.89, { -1.34, 0.87, -0.46, -1.63, -0.84, 1.22, 0.85, -4.69 } }, // Chorus Space
  1643. { 8.59, { -3.62, -4.93, -1.21, -0.82, -2.59, 2.12, 7.44, 9.41 } }, // Ambience
  1644. { 5.04, { -3.57, -2.60, -2.05, -1.92, -1.13, 0.71, 6.98, 12.96 } }, // Bright Hall
  1645. { 5.90, { 2.10, -2.48, -3.26, 0.65, 2.22, 3.39, -0.52, -14.57 } }, // Nave
  1646. { 2.98, { -1.90, -5.90, -1.84, 2.74, 2.75, 5.34, -3.07, -20.56 } }, // Grit Hall
  1647. { -0.75, { -0.85, -1.41, -1.33, -1.20, -0.41, 3.00, 1.35, -10.24 } }, // Grit Plate
  1648. { 5.73, { -2.66, -3.85, -0.89, 1.00, 0.17, 2.85, 0.72, -12.61 } }, // Smooth Plate
  1649. { 9.37, { 7.68, 7.33, 3.15, -4.53, -3.14, 1.07, -3.86, -17.78 } }, // Smooth Room
  1650. { 6.79, { 8.10, 6.32, 0.66, -3.45, -1.97, 1.36, -2.91, -16.40 } }, // Smooth Space
  1651. { -0.63, { 0.87, 1.07, 0.89, 0.50, 0.43, 2.94, -5.83, -17.37 } }, // Gated
  1652. { 6.21, { -3.32, -3.60, -0.98, -1.01, -1.09, 1.08, 5.59, 10.46 } }, // Drift Chamber
  1653. { 7.74, { -3.33, -6.49, -2.05, 2.00, 1.74, 3.35, 1.45, -11.92 } }, // Drift Hall
  1654. { -3.48, { 1.08, 1.38, 0.80, -0.64, 0.29, 4.17, -6.00, -14.52 } }, // Drift Space
  1655. { -4.48, { -0.55, -1.86, 0.11, 0.50, -0.27, 2.86, -1.36, -11.65 } }, // Cathedral
  1656. { -3.93, { 3.64, -2.34, -0.88, 2.28, 2.00, 4.49, -5.56, -14.07 } }, // Grand Hall
  1657. { -6.17, { 1.19, -1.22, -1.27, 0.88, 0.86, 2.55, -1.82, -11.16 } }, // Vintage Chamber
  1658. { -2.85, { 0.07, -5.03, -1.68, 1.48, 1.22, 4.32, -0.31, -12.19 } }, // Vintage Hall
  1659. };
  1660. // round 190kw (revision 1 + loopLaw): Epoch's level over Decay and Size from the reference's loop (static REA of
  1661. // ValhallaVintageVerb, docs/analise/REA_rodada190kw.md §4 / §11). Each of its modes is one loop whose gain is the RT60 law
  1662. // 10^(−3 L / (fs T)) split over N gain stages, the input entering after a stage, with no output gain of its own: its wet
  1663. // energy is E(T, s) = E0 / (1 − 10^(−6 ℓ(s) / T)), ℓ = c · L(s) / N, and the first 80 ms do not move with Decay. Per mode:
  1664. // ℓ at Size 100 % (s), the loop's size law L(s) / L(1) = a + b · s^e, the level at the capture's base Decay (0.513 s) against
  1665. // revision 1 (dB, refcap030's mode sweep, impulse) and Epoch's own wet-energy growth over Decay (dB at the knots below,
  1666. // measured on the impulse with the law's level off). The trim is the reference's growth minus Epoch's own. Fitted on the
  1667. // scores after that (the stand-in's spectrum is not the reference's, so the level that scores best is not always the
  1668. // energy match): Concert Hall +0.3 dB, and its own growth 0.1 / 0.6 / 0.2 / 0.9 dB less at 2 / 4 / 7 / 15 s and 0.3 / 0.3 dB
  1669. // more at 33 / 70 s (refcap030's pairs and sr48 / sr96 captures, refcap182's programme and tp172d at 0.51 / 2.9 / 4 / 7 /
  1670. // 12.8 / 33 s), Room −0.4 dB (its 1.41 s captures), Bright Hall +1.25 dB, Grit Plate −3.25 dB, Gated −2 dB (its impulse).
  1671. // A negative ℓ: the mode keeps revision 1 (early part and level as round 190it left them).
  1672. struct EpochLoop
  1673. {
  1674. double ell, a, b, e, atDb;
  1675. double grow[8];
  1676. };
  1677. constexpr double epochLoopKnots[8] = { 0.513, 1.0, 2.0, 4.0, 7.0, 15.0, 33.0, 70.0 }; // Decay (s)
  1678. LYNX_FIT_TABLE EpochLoop epochLoop[epoch::modeCount] = {
  1679. { 0.39520, 0.200, 0.800, 1.0, 0.85, { 0.00, 0.35, 1.39, 2.79, 4.78, 6.48, 10.11, 12.31 } }, // Concert Hall: 0.5605 s (0.2 + 0.8 s), N 2, c 1.41
  1680. { 0.18654, 0.16155, 0.83845, 1.0, -2.05, { 0.00, 0.93, 3.42, 6.14, 8.22, 10.84, 13.33, 15.50 } }, // Plate: 5100 (1 + 5.19 s) / 33850, N 5
  1681. { 0.13013, 0.250, 0.750, 1.0, -1.22, { 0.00, 1.76, 3.86, 5.93, 7.57, 9.72, 11.82, 13.71 } }, // Room: 26770 (0.5 + 1.5 s) / 48000, N 6, c 0.7
  1682. { 0.15545, 0.16155, 0.83845, 1.0, -1.68, { 0.00, 1.09, 3.60, 6.26, 8.29, 10.85, 13.28, 15.42 } }, // Chamber: as Plate, N 6
  1683. { 0.26899, 0.19750, 0.80250, 1.0, 1.43, { 0.00, 1.14, 3.53, 6.35, 8.53, 11.28, 13.87, 16.12 } }, // Random Space: 5100 (3 + 12.19 s) / 48000, N 6
  1684. { 0.26899, 0.19750, 0.80250, 1.0, 0.97, { 0.00, 1.00, 3.10, 5.71, 7.84, 10.58, 13.18, 15.44 } }, // Chorus Space: as Random Space
  1685. { 0.20566, 0.36630, 0.63370, 1.0, -2.95, { 0.00, 0.18, 2.39, 4.89, 6.81, 9.25, 11.59, 13.67 } }, // Ambience: 5100 (3 + 5.19 s) / 33850, N 6
  1686. { 0.30797, 0.200, 0.800, 1.0, -3.41, { 0.00, -2.44, -2.10, -0.65, 0.79, 2.92, 5.15, 7.22 } }, // Bright Hall: 11210 (0.2 + 0.8 s) / 18200, N 2
  1687. { 1.13677, 0.200, 0.800, 1.0, -2.93, { 0.00, -0.33, 0.74, 2.65, 4.39, 6.81, 9.25, 11.46 } }, // Nave ← Sanctuary: 21826 (1 + 4 s) / 48000, N 2
  1688. { 0.28025, 0.200, 0.800, 1.0, -2.47, { 0.00, -1.33, -1.02, 0.30, 1.65, 3.68, 5.86, 7.89 } }, // Grit Hall ← Dirty Hall: as Concert Hall, c 1
  1689. { 0.08252, 0.09434, 0.90566, 1.0, -0.33, { 0.00, 1.65, 4.25, 6.99, 9.06, 11.66, 14.12, 16.29 } }, // Grit Plate ← Dirty Plate: 11210 (0.8 s fs / 20000 + 0.2) / fs (48 kHz), N 6
  1690. { 0.47326, 0.05263, 0.94737, 1.0, -2.39, { 0.00, 0.29, 1.66, 3.60, 5.29, 7.60, 9.90, 11.97 } }, // Smooth Plate: 4250 (1 + 18 s) / 34125, N 5
  1691. { 0.32854, 0.07582, 0.92418, 1.0, -3.36, { 0.00, 1.43, 3.90, 6.05, 7.84, 10.21, 12.47, 14.33 } }, // Smooth Room: 5100 (1 + 12.19 s) / 34125, N 6
  1692. { 0.26899, 0.19750, 0.80250, 1.0, -2.55, { 0.00, 1.10, 3.35, 5.62, 7.53, 10.07, 12.52, 14.59 } }, // Smooth Space ← Smooth Random: as Random Space
  1693. { 0.0, 0.07582, 0.92418, 2.0, 0.43, { 0.00, -0.44, -0.36, -0.36, -2.06, -4.42, -7.61, -7.28 } }, // Gated ← Nonlin: 5100 (1 + 12.19 s²) / 34125; its gate, not the loop, sets the level
  1694. { -0.39438, 0.15789, 0.84211, 1.0, -3.96, { 0.00, -0.95, 0.21, 2.17, 3.87, 6.15, 8.39, 10.40 } }, // Drift Chamber ← Chaotic Chamber: 4250 (3 + 16 s) / 34125, N 6; keeps revision 1 (its test signal lost a step)
  1695. { 0.28025, 0.200, 0.800, 1.0, -1.57, { 0.00, -0.73, -0.13, 1.48, 3.03, 5.26, 7.56, 9.66 } }, // Drift Hall ← Chaotic Hall: as Grit Hall
  1696. { 0.44835, 0.11111, 0.88889, 1.0, -0.02, { 0.00, 0.96, 3.35, 6.09, 8.20, 10.87, 13.39, 15.59 } }, // Drift Space ← Chaotic Neutral: 5100 (2 + 16 s) / 34125, N 6
  1697. { 0.38147, 0.200, 0.800, 1.0, 1.39, { 0.00, 0.91, 1.36, 2.99, 4.45, 6.83, 9.64, 12.25 } }, // Cathedral: 30000 (0.5 + 2 s) / 32768, N 6
  1698. { 0.32005, 0.03125, 0.96875, 1.5, 0.43, { 0.00, -0.82, -0.12, 1.73, 3.59, 6.23, 8.84, 11.17 } }, // Grand Hall ← Palace: 61449 (0.125 + 3.875 s^1.5) / 48000, N 16
  1699. { 0.41550, 0.500, 0.500, 1.0, 2.93, { 0.00, 0.97, 3.13, 5.73, 7.76, 10.34, 12.79, 14.97 } }, // Vintage Chamber ← Chamber1979: 9972 (0.5 + 0.5 s) / 20000 per line, RT 1.2 × Decay
  1700. { 0.46667, 0.500, 0.500, 1.0, 2.33, { 0.00, -0.79, -0.33, 1.10, 2.54, 4.67, 6.91, 8.97 } }, // Vintage Hall ← Hall1984: 11200 (0.5 + 0.5 s) / 20000 per line, RT 1.2 × Decay
  1701. };
  1702. // the level trim (dB) of revision 1 + loopLaw for mode k at Decay T (s) and Size s (0..1): the base level, plus the
  1703. // reference's growth from (0.513 s, 100 %) to (T, s) through its own size law, minus Epoch's own growth over Decay and Size
  1704. double epochLoopDb (int k, double decay, double size) noexcept
  1705. {
  1706. const auto& m = epochLoop[k];
  1707. if (m.ell <= 0.0)
  1708. return m.atDb;
  1709. const auto t = std::clamp (decay, epochLoopKnots[0], epochLoopKnots[7]);
  1710. const auto grow = [t] (double ell) { return -10.0 * std::log10 (std::max (1.0e-9, 1.0 - std::pow (10.0, -6.0 * ell / t))); };
  1711. const auto lawSize = m.a + m.b * std::pow (std::clamp (size, 0.0, 1.0), m.e);
  1712. // Epoch's own lines scale by 0.3 + 0.7 s, and its wet energy rises by that factor^−0.64 at any Decay (measured:
  1713. // +1.2 / +2.0 dB at Size 50 / 25 %, Concert Hall and Room, 0.51 … 12.8 s)
  1714. const auto ownSizeDb = -6.4 * std::log10 (0.3 + 0.7 * std::clamp (size, 0.0, 1.0));
  1715. const auto ref = grow (m.ell * lawSize) - ownSizeDb + 10.0 * std::log10 (1.0 - std::pow (10.0, -6.0 * m.ell / epochLoopKnots[0]));
  1716. int i = 0;
  1717. while (i < 6 && t > epochLoopKnots[i + 1])
  1718. ++i;
  1719. const auto f = std::log (t / epochLoopKnots[i]) / std::log (epochLoopKnots[i + 1] / epochLoopKnots[i]);
  1720. const auto own = m.grow[i] + (m.grow[i + 1] - m.grow[i]) * std::clamp (f, 0.0, 1.0);
  1721. return m.atDb + ref - own;
  1722. }
  1723. // Atrium ← ValhallaRoom's type sweep (3-point smoothed over the bands, half the 63 Hz band's; Large Room, the type of
  1724. // every other capture, keeps its 0.1.27 voicing; Medium Room's level a compromise between its impulse (+2 dB) and its
  1725. // test signal (−3 dB))
  1726. LYNX_FIT_TABLE Rev1Trim atriumTrim[atrium::typeCount] = {
  1727. {}, // Large Room
  1728. { -1.50, { 0.77, -1.36, -0.63, 0.39, 0.02, 0.45, 1.13, 1.24 } }, // Medium Room
  1729. {}, // Bright Room (its 0.1.27 law was fitted on tp172d's S2, which the sweep's trim cost a step)
  1730. { -0.75, { 0.04, 0.01, 0.26, 1.05, 0.45, -0.63, -1.14, -1.63 } }, // Large Chamber
  1731. { 0.40, { 0.01, 1.43, 0.90, 0.45, 0.00, -0.87, -1.90, -3.50 } }, // Dark Room
  1732. { -0.20, { 0.08, 0.20, 0.11, 0.92, 0.43, -0.85, -0.82, -0.22 } }, // Dark Chamber
  1733. { 2.30, { -1.15, -0.14, 0.89, 1.18, 0.04, -1.07, -0.91, -0.23 } }, // Dark Space
  1734. { 3.75, { -1.13, 0.74, 1.19, 0.98, -0.26, -1.38, -1.26, -0.93 } }, // Deep Space
  1735. { 2.42, { -0.56, 0.82, 0.80, 0.56, 0.07, -0.91, -1.35, -1.30 } }, // Mirror Space
  1736. { 3.05, { -0.86, 0.03, 1.16, 1.51, 0.16, -1.32, -1.54, -1.17 } }, // Vast Space
  1737. { 5.16, { -1.21, 1.24, 1.62, 1.05, -0.22, -1.62, -2.06, -2.00 } }, // Distant Space
  1738. {}, // Dense Room
  1739. };
  1740. // Foil Plate ← ValhallaPlate's type sweep (as Atrium's; Silver and Pewter, the types of the other captures, keep theirs:
  1741. // Pewter's trim at the sweep's decay cost the 0.5 decay of refcap182's S2 a step); the last entry: the measured plate voicing
  1742. LYNX_FIT_TABLE Rev1Trim foilTrim[foil::typeCount + 1] = {
  1743. {}, // Silver
  1744. {}, // Pewter
  1745. { 3.26, { -1.89, -0.54, 0.24, 0.47, 0.04, -0.13, -0.08, -0.79 } }, // Nickel
  1746. { 1.65, { -0.36, 0.27, 0.31, 0.61, 0.10, -0.82, -0.46, 0.09 } }, // Bronze
  1747. { 1.87, { -1.12, -0.78, 0.06, 1.34, 0.90, -0.35, -1.18, -2.21 } }, // Bismuth
  1748. { 0.39, { 1.63, 1.84, 0.02, -0.88, -0.69, -0.38, 0.09, 0.21 } }, // Tin
  1749. { 0.28, { -0.85, -0.82, -0.67, -0.31, -0.10, 0.50, 1.39, 1.48 } }, // Zinc
  1750. { 3.88, { 0.55, 1.93, 1.04, -0.30, -0.64, -1.11, -0.92, -0.18 } }, // Platinum
  1751. { 2.14, { 0.71, 0.58, -1.25, -1.52, -0.19, 1.17, 1.20, -0.70 } }, // Tungsten
  1752. { 3.74, { -0.55, 0.54, 0.41, 0.29, -0.02, -0.26, -0.96, -3.08 } }, // Iron
  1753. { 2.39, { -0.35, -0.03, -1.06, -0.94, -0.02, 0.97, 1.08, -0.80 } }, // Gold
  1754. { 0.68, { 0.93, 0.69, -0.59, -0.32, 0.30, 0.22, -0.31, -1.42 } }, // Lead
  1755. {}, // the measured plate voicing
  1756. };
  1757. LYNX_FIT_TABLE Rev1Trim haloTrim[7] = {}; // per reverb mode
  1758. #ifdef LYNX_VERB_FIT
  1759. // the fitting harness: LYNX_VTRIM=<file>, lines "<epoch|atrium|foil|halo> <index> <gain dB> <8 × dB>"
  1760. bool loadTrims()
  1761. {
  1762. if (const char* f = std::getenv ("LYNX_VTRIM"))
  1763. if (auto* in = std::fopen (f, "r"))
  1764. {
  1765. char name[32];
  1766. int i;
  1767. Rev1Trim t;
  1768. while (std::fscanf (in, "%31s %d %lf %lf %lf %lf %lf %lf %lf %lf %lf", name, &i, &t.gainDb, &t.db[0], &t.db[1], &t.db[2],
  1769. &t.db[3], &t.db[4], &t.db[5], &t.db[6], &t.db[7]) == 11)
  1770. {
  1771. const std::string n = name;
  1772. Rev1Trim* table = n == "epoch" ? epochTrim : n == "atrium" ? atriumTrim : n == "foil" ? foilTrim : n == "halo" ? haloTrim : nullptr;
  1773. const int count = n == "epoch" ? epoch::modeCount : n == "atrium" ? atrium::typeCount : n == "foil" ? foil::typeCount + 1 : 7;
  1774. if (table != nullptr && i >= 0 && i < count)
  1775. table[i] = t;
  1776. }
  1777. std::fclose (in);
  1778. }
  1779. return true;
  1780. }
  1781. #endif
  1782. void applyTrim (Settings& s, const Rev1Trim& t, double weight = 1.0) noexcept
  1783. {
  1784. #ifdef LYNX_VERB_FIT
  1785. static const bool loaded = loadTrims();
  1786. (void) loaded;
  1787. #endif
  1788. s.gain *= std::pow (10.0, weight * t.gainDb / 20.0);
  1789. for (int b = 0; b < engineBands; ++b)
  1790. s.bandDb[b] += weight * t.db[b];
  1791. }
  1792. LYNX_FIT_TABLE EpochMode epochModes[epoch::modeCount] = {
  1793. { "Concert Hall", 0, 0.9394, 1, 1.2, 1, 0.975, 0.4, 42.94, 0.0, 1, 1.031, 3.2, 0.0 },
  1794. { "Plate", 2, 0.80, 0.90, 0.45, 1.0, 1.0, 0.10, 2.0, 60.0, 1.0, 0.70, 2.0, 0.0 },
  1795. { "Room", 1, 0.85, 1.02, 0.45, 1.0, 1.0, 0.10, 20.0, 150.0, 1.0, 0.82, 1.0, 0.0 },
  1796. { "Chamber", 1, 1.00, 1.04, 0.45, 1.0, 1.0, 0.15, 30.0, 140.0, 1.0, 0.70, 1.2, 0.0 },
  1797. { "Random Space", 0, 1.10, 1.00, 0.90, 1.0, 1.0, 0.00, 1.0, 220.0, 2.0, 1.50, 15.0, 0.0 },
  1798. { "Chorus Space", 0, 1.10, 1.00, 1.10, 1.0, 1.0, 0.05, 40.0, 280.0, 3.0, 0.70, 9.0, 0.0 },
  1799. { "Ambience", 1, 0.70, 1.00, 0.45, 1.0, 1.0, 0.15, 40.0, 25.0, 1.0, 0.80, 7.5, 0.0 },
  1800. { "Bright Hall", 0, 1.05, 1.15, 1.20, 1.0, 1.0, 0.50, 9.0, 0.0, 1.0, 1.00, 5.5, 0.0 },
  1801. { "Nave", 0, 1.20, 1.00, 0.80, 1.0, 1.0, 0.35, 14.0, 20.0, 1.0, 0.62, 0.5, 0.0 },
  1802. { "Grit Hall", 0, 1.00, 1.10, 1.20, 1.0, 1.0, 0.55, 9.0, 0.0, 1.3, 0.83, 5.0, 0.0 },
  1803. { "Grit Plate", 2, 0.75, 0.92, 0.45, 1.0, 1.0, 0.10, 6.0, 40.0, 1.3, 1.25, 1.0, 0.0 },
  1804. { "Smooth Plate", 2, 0.85, 1.12, 0.60, 1.0, 1.0, 0.40, 6.0, 0.0, 0.8, 0.46, 4.5, 0.0 },
  1805. { "Smooth Room", 1, 0.95, 0.97, 0.70, 1.0, 1.0, 0.30, 8.0, 6.0, 0.8, 0.53, 9.0, 0.0 },
  1806. { "Smooth Space", 0, 1.00, 1.04, 0.80, 1.0, 1.0, 0.25, 8.0, 6.0, 0.8, 0.81, 7.5, 0.0 },
  1807. { "Gated", 1, 0.60, 0.62, 0.30, 1.0, 1.0, 0.10, 4.0, 0.0, 0.5, 0.90, 1.5, 0.55 },
  1808. { "Drift Chamber", 1, 1.00, 1.08, 0.70, 1.0, 1.0, 0.40, 6.0, 0.0, 2.5, 0.43, 4.5, 0.0 },
  1809. { "Drift Hall", 0, 1.00, 1.13, 1.20, 1.0, 1.0, 0.45, 9.0, 0.0, 2.5, 0.53, 5.0, 0.0 },
  1810. { "Drift Space", 0, 1.30, 1.70, 1.20, 1.0, 1.0, 0.05, 9.0, 110.0, 2.5, 1.00, 4.0, 0.0 },
  1811. { "Cathedral", 0, 1.49, 1, 0.8762, 1.203, 1, 0.4, 84.68, 39.77, 1, 0.756, 0.0, 0.0 },
  1812. { "Grand Hall", 0, 1.50, 1.30, 2.40, 1.7, 1.0, 0.20, 30.0, 20.0, 1.0, 1.30, 55.0, 0.0 },
  1813. { "Vintage Chamber", 1, 1.10, 1.20, 0.90, 1.0, 1.0, 0.05, 25.0, 110.0, 0.7, 1.40, 11.0, 0.0 },
  1814. { "Vintage Hall", 0, 1.00, 1.08, 1.20, 1.0, 1.0, 0.50, 9.0, 0.0, 0.7, 0.95, 5.0, 0.0 },
  1815. };
  1816. const char* const epochColours[epoch::colourCount] = { "1970s", "1980s", "Now" };
  1817. // colour: loop bandwidth (Hz, 0 = none), output high cut (Hz, first order, 0 = none), steep band limit (Hz, 0 = none) and
  1818. // its Butterworth sections
  1819. struct EpochColour
  1820. {
  1821. double loopBw, outCut, bandLimit;
  1822. int sections;
  1823. };
  1824. LYNX_FIT_TABLE EpochColour epochColours2[3] = { { 9000.0, 0.0, 11800.0, 16 }, { 14000.0, 16000.0, 0.0, 2 }, { 0.0, 0.0, 0.0, 2 } };
  1825. // Round 124 (REA_rodada124_extras §3): each algorithm's own voicing, fitted to the reference at her saved settings
  1826. // (time–frequency energy of the impulse response and RT60 per octave). Neutral for the modes not refitted.
  1827. struct EpochVoicing
  1828. {
  1829. double bassExp = 0.35; // bass multiplier law below 1 × (RT × mult^bassExp)
  1830. double highMult = 1.0; // × the high-band RT
  1831. double loopBwScale = 1.0; // × the colour's loop bandwidth
  1832. double earlyDecay = 3.0; // early taps' fall over their span (e-folds)
  1833. double toneLowHz = 200.0, toneLowDb = 0.0;
  1834. double toneMidHz = 1000.0, toneMidDb = 0.0, toneMidQ = 0.7;
  1835. double toneHighHz = 8000.0, toneHighDb = 0.0;
  1836. // the mid-band RT60 (s) the reference measures at Size 100 % for each Decay (s): (decay, rt) pairs, increasing;
  1837. // empty = the round-100 law (Decay × RT / Decay with the floor)
  1838. int rtKnotCount = 0;
  1839. double rtKnots[2 * 16] {};
  1840. };
  1841. LYNX_FIT_TABLE EpochVoicing epochVoicing[epoch::modeCount] = {
  1842. /* Concert Hall */ { 0.0, 1.88, 1.455, 0.5, 155.8, -0.6, 1455, -0.6, 2.45, 4852, 1.5, 8, { 0.5, 1.38, 1, 1.6, 1.78, 2.27, 2, 2.46, 2.67, 3.17, 3.28, 3.7, 4, 4.63, 8, 8.68 } },
  1843. {},
  1844. {},
  1845. {},
  1846. {},
  1847. {},
  1848. {},
  1849. {},
  1850. {},
  1851. {},
  1852. {},
  1853. {},
  1854. {},
  1855. {},
  1856. {},
  1857. {},
  1858. {},
  1859. {},
  1860. /* Cathedral */ { 0.875, 0.76, 1.868, 0.0, 61.95, -1.8, 200, -0.6, 0.2, 1.164e+04, -3, 13, { 0.53, 1.15, 1, 2.2, 1.85, 2.54, 1.99, 2.72, 2.5, 3.28, 2.9, 3.7, 3.87, 4.62, 4.36, 5.07, 5.15, 5.76, 6.38, 6.85, 7.37, 7.73, 12, 11.86, 14, 13.68 } },
  1861. {},
  1862. {},
  1863. {},
  1864. };
  1865. // round 190bz: law 1 per mode
  1866. // fitted with the tail lab (round 190bz; the reference and the scores per row: docs/PARITY.md); a neutral entry is the classic voicing on the new loop
  1867. LYNX_FIT_TABLE LawVoicing epochLaw[epoch::modeCount] = {
  1868. { { 0.998, 1.001, 0.969, 0.971, 0.957, 0.928, 0.881, 0.770 }, { -2.03, -0.49, -0.60, -1.21, -1.43, -0.37, 1.07, 1.53 }, 2.22, 0.000, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 100, 1000, 5000 }, { 0.00, 0.00, 0.00 }, { 1.4, 1.4, 1.4 } }, // Concert Hall
  1869. {},
  1870. { { 0.894, 1.486, 0.845, 1.106, 1.151, 1.092, 2.405, 4.000 }, { -1.29, -2.74, -2.79, -0.81, -1.29, -0.33, 1.15, 6.09 }, 1.67, 0.300, 45, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 4.00, 1.00, { 100, 1000, 5000 }, { 0.00, 0.00, 0.00 }, { 1.4, 1.4, 1.4 } }, // Room
  1871. };
  1872. double epochDecay (float v) { return skewLaw (v, 0.2, 70.0, 3.3596); }
  1873. double epochPredelay (float v) { return skewLaw (v, 0.0, 500.0, 2.3219); }
  1874. double epochBassMult (float v) { return skewLaw (v, 0.25, 4.0, 2.3219); }
  1875. double epochBassXover (float v) { return skewLaw (v, 100.0, 10000.0, 4.0444); }
  1876. double epochFreq (float v) { return skewLaw (v, 100.0, 20000.0, 1.7540); }
  1877. class EpochFx final : public VerbEffect
  1878. {
  1879. public:
  1880. EpochFx() : VerbEffect (EffectKind::epochVerb, 0.25, 0.62) {}
  1881. protected:
  1882. int structure (const EffectParams& p) const noexcept override { return choice (p[16], epoch::modeCount) + (p[epoch::engineLaw] >= 0.5f ? 1000 : 0); }
  1883. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  1884. {
  1885. const auto modeIndex = choice (p[16], epoch::modeCount);
  1886. const auto& m = epochModes[modeIndex];
  1887. const auto& vo = epochVoicing[modeIndex];
  1888. const auto colour = choice (p[15], epoch::colourCount);
  1889. const auto& co = epochColours2[colour];
  1890. mix = p[0];
  1891. const auto size = (double) p[3];
  1892. // a very slow attack also delays the onset (the reference: +45 ms at 100 %)
  1893. s.predelaySec = (epochPredelay (p[1]) + m.preMs * (0.4 + 0.6 * size) + 225.0 * std::max (0.0, p[4] - 0.8)) / 1000.0;
  1894. const auto decay = epochDecay (p[2]);
  1895. s.shape = m.shape;
  1896. s.lineScale = m.scale * (0.3 + 0.7 * size);
  1897. if (vo.rtKnotCount >= 2)
  1898. {
  1899. // measured RT per Decay, scaled like the law below for Size and late diffusion
  1900. const auto rt = knotLaw (std::clamp (decay, vo.rtKnots[0], vo.rtKnots[2 * (vo.rtKnotCount - 1)]), vo.rtKnots, vo.rtKnotCount);
  1901. const auto beyond = decay > vo.rtKnots[2 * (vo.rtKnotCount - 1)] ? decay / vo.rtKnots[2 * (vo.rtKnotCount - 1)] : 1.0;
  1902. s.rt60 = rt * beyond * m.rtScale * (0.85 + 0.15 * size) * (0.84 + 0.16 * p[10]);
  1903. }
  1904. else
  1905. s.rt60 = withFloor (decay * m.rtScale, m.rtFloor * (0.4 + 0.6 * size)) * (0.84 + 0.16 * p[10]);
  1906. const auto bass = epochBassMult (p[5]);
  1907. s.lowMult = (bass < 1.0 ? std::pow (bass, vo.bassExp) : bass) * m.lowRt;
  1908. s.lowXover = epochBassXover (p[6]);
  1909. const auto shelf = -24.0 + 24.0 * p[7];
  1910. s.highXover = epochFreq (p[8]);
  1911. s.highMult = 1.1 * std::pow (10.0, std::max (shelf, -14.0) / 50.0) * vo.highMult;
  1912. s.highShelfHz = s.highXover;
  1913. s.highShelfDb = std::max (shelf, -16.0);
  1914. s.inDiffusion = m.inDiff * p[9];
  1915. s.loopDiffusion = 0.2 + 0.8 * p[10];
  1916. s.modRateHz = 0.1 + 9.9 * p[11];
  1917. s.modDepthMs = 0.6 * m.modScale * p[12] / std::sqrt (std::max (0.3, s.modRateHz));
  1918. s.outHighCut = epochFreq (p[13]);
  1919. s.outHighOrder = 1;
  1920. if (co.outCut > 0.0)
  1921. s.outHighCut = std::min (s.outHighCut, co.outCut);
  1922. s.loopBandwidth = co.loopBw * vo.loopBwScale;
  1923. s.bandLimit = co.bandLimit;
  1924. s.bandLimitSections = co.sections;
  1925. s.earlyDecay = vo.earlyDecay;
  1926. s.lowShelfHz = vo.toneLowHz;
  1927. s.lowShelfDb = vo.toneLowDb;
  1928. s.toneMidHz = vo.toneMidHz;
  1929. s.toneMidDb = vo.toneMidDb;
  1930. s.toneMidQ = vo.toneMidQ;
  1931. s.toneHighHz = vo.toneHighHz;
  1932. s.toneHighDb = vo.toneHighDb;
  1933. s.outLowCut = 10.0 + 1490.0 * p[14];
  1934. s.outLowOrder = 1;
  1935. s.earlyLevel = m.early;
  1936. s.earlySec = m.earlyMs / 1000.0 * (0.4 + 0.6 * size);
  1937. s.buildSec = m.buildMs / 1000.0 * (0.25 + 1.5 * p[4]);
  1938. s.gateSec = m.gate > 0.0 ? m.gate * decay + 0.05 : 0.0;
  1939. s.gain = m.gain;
  1940. // round 183b (the reference's impulse at −20 dB, 17 settings; it is linear): its first 80 ms hold more of the
  1941. // energy at longer decays (−3.8 dB of the total at its default 4 s, ours −7.2; at 2 s round 100's −3.1 dB in the
  1942. // first 100 ms still holds), its modes sit denser low down (lines × 0.85: the −20 dB impulses and music both
  1943. // closer), and it is 0.7 dB lower
  1944. s.lineScale *= 0.85;
  1945. // round 190kw (loopLaw): the reference's first 80 ms do not move with Decay (−10.65 dB at 0.51 / 7 / 33 s): the
  1946. // early level holds at its long-decay value
  1947. const auto loopLaw = p[epoch::engineLaw] >= 0.5f && p[epoch::revision] >= 0.5f && p[epoch::loopLaw] >= 0.5f
  1948. && epochLoop[modeIndex].ell >= 0.0;
  1949. s.earlyLevel *= loopLaw ? 1.6 : std::min (1.6, 1.13 * std::sqrt (std::max (1.0, decay / 2.0)));
  1950. s.gain *= 0.92;
  1951. if (p[epoch::engineLaw] >= 0.5f)
  1952. applyLaw (s, epochLaw[modeIndex]);
  1953. if (p[epoch::engineLaw] >= 0.5f && p[epoch::revision] >= 0.5f)
  1954. {
  1955. s.revision = 1; // round 190it
  1956. // Epoch's trims were measured at the capture's base Decay (0.51 s); its Room mode needs none at 1.41 s
  1957. // (refcap182 S2): they fade out between the two on the decay's logarithm
  1958. const auto fade = std::clamp (std::log (1.41 / std::max (0.01, decay)) / std::log (1.41 / 0.51), 0.0, 1.0);
  1959. if (! loopLaw)
  1960. applyTrim (s, epochTrim[modeIndex], fade);
  1961. else
  1962. {
  1963. // round 190kw: the tone trims fade as before; the level is the loop law's at every Decay and Size
  1964. auto tone = epochTrim[modeIndex];
  1965. const auto levelDb = tone.gainDb;
  1966. tone.gainDb = 0.0;
  1967. applyTrim (s, tone, fade);
  1968. s.gain *= std::pow (10.0, (levelDb + epochLoopDb (modeIndex, decay, size)) / 20.0);
  1969. }
  1970. }
  1971. }
  1972. };
  1973. // ---------------------------------------------------------------------------------------------- Atrium
  1974. // Laws: like the reference room (linear: predelay 0..500 ms, high cut 100..15000 Hz, sizes / crosses 0..1, mod rates
  1975. // 0.05..5 Hz, bass mult 0.5..2 X, bass xover 100..10000 Hz, high mult 0.1..1 X, high xover 100..15000 Hz, early size
  1976. // 1..1000 ms, low cut 0..1000 Hz) except Decay: 0.1..100 s with 2 s at the middle (the reference is linear).
  1977. struct AtriumType
  1978. {
  1979. const char* name;
  1980. int shape;
  1981. double scale, rtScale, inDiff, early, lateGain, cut, modScale;
  1982. };
  1983. const AtriumType atriumTypes[atrium::typeCount] = {
  1984. { "Large Room", 1, 1.00, 1.00, 1.0, 1.00, 1.00, 0.0, 1.0 },
  1985. { "Medium Room", 1, 0.80, 1.00, 1.0, 1.00, 1.00, 0.0, 1.0 },
  1986. { "Bright Room", 1, 0.90, 0.88, 0.9, 0.80, 0.90, 0.0, 1.0 },
  1987. { "Large Chamber", 1, 1.20, 1.00, 1.0, 0.85, 1.05, 0.0, 1.0 },
  1988. { "Dark Room", 1, 1.00, 1.00, 1.0, 1.00, 1.00, 0.0, 1.0 },
  1989. { "Dark Chamber", 1, 1.15, 1.00, 1.0, 0.90, 1.00, 11000.0, 1.0 },
  1990. { "Dark Space", 0, 1.10, 1.00, 1.0, 0.90, 1.00, 10000.0, 1.0 },
  1991. { "Deep Space", 0, 1.20, 1.00, 1.0, 0.85, 1.00, 10500.0, 1.2 },
  1992. { "Mirror Space", 0, 0.90, 1.00, 0.7, 1.10, 1.00, 10500.0, 1.0 },
  1993. { "Vast Space", 0, 1.30, 1.00, 0.8, 1.00, 1.00, 10000.0, 1.2 },
  1994. { "Distant Space", 0, 1.25, 1.00, 1.0, 0.70, 1.10, 10500.0, 1.0 },
  1995. { "Dense Room", 1, 1.00, 1.00, 1.0, 1.05, 1.00, 10500.0, 1.0 },
  1996. };
  1997. // round 190bz: law 1 per type
  1998. // fitted with the tail lab (round 190bz; the reference and the scores per row: docs/PARITY.md); a neutral entry is the classic voicing on the new loop
  1999. LYNX_FIT_TABLE LawVoicing atriumLaw[atrium::typeCount] = {
  2000. { { 1.161, 0.971, 1.153, 0.990, 1.001, 1.113, 1.069, 1.374 }, { 1.17, 0.78, 1.30, 1.29, -0.71, 0.20, -0.93, -1.68 }, -1.09, 0.650, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 100, 159, 5000 }, { 1.26, -1.21, 0.00 }, { 2.0, 2.0, 1.4 } }, // Large Room
  2001. {},
  2002. { { 0.695, 0.920, 0.713, 0.900, 0.910, 1.075, 1.257, 1.773 }, { 2.40, 1.43, 0.65, 1.22, -1.46, -0.47, -1.43, -1.80 }, 0.40, 0.650, 30, 0.00, 0.00, 1.00, 0.60, 0.00, 0.30, 2.50, 1.00, { 100, 1000, 5000 }, { 0.00, 0.00, 0.00 }, { 1.4, 1.4, 1.4 } }, // Bright Room
  2003. };
  2004. double atriumDecaySec (float v) { return skewLaw (v, 0.1, 100.0, 5.717); }
  2005. class AtriumFx final : public VerbEffect
  2006. {
  2007. public:
  2008. AtriumFx() : VerbEffect (EffectKind::atrium, 0.25, 1.05) {}
  2009. protected:
  2010. int structure (const EffectParams& p) const noexcept override { return choice (p[19], atrium::typeCount) + (p[atrium::engineLaw] >= 0.5f ? 1000 : 0); }
  2011. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  2012. {
  2013. const auto& t = atriumTypes[choice (p[19], atrium::typeCount)];
  2014. mix = p[0];
  2015. s.predelaySec = 0.5 * p[1];
  2016. s.rt60 = atriumDecaySec (p[2]) * t.rtScale;
  2017. s.outHighCut = 100.0 + 14900.0 * p[3];
  2018. s.bandLimit = t.cut;
  2019. const auto earlyLate = (double) p[4]; // 0 = early only, 1 = late only
  2020. // round 183b: an equal-power crossfade (the reference's total level moves only 2.6 dB from early only to late
  2021. // only: −28.7 / −27.2 / −26.1 dB at 0 / 50 / 100 %, impulse at −20 dB; the old linear one moved 10.7 dB). The
  2022. // late level holds over Decay (the reference: −27.2 / −26.8 dB at RT 2.1 / 8.6 s, ours rose with the RT) and
  2023. // over Late size (0 % ran +3 dB, 100 % −1.2 dB)
  2024. const auto fade = 0.5 * pi * earlyLate;
  2025. s.earlyLevel = t.early * 0.92 * 1.46 * std::cos (fade);
  2026. const auto lateSizeDb = p[5] <= 0.5f ? -3.1 * (1.0 - 2.0 * p[5]) : 2.4 * (p[5] - 0.5);
  2027. s.lateLevel = t.lateGain * 3.8 * 0.575 * std::sin (fade) * std::sqrt (2.1 / std::max (0.1, atriumDecaySec (p[2]) * t.rtScale))
  2028. * std::pow (10.0, lateSizeDb / 20.0);
  2029. s.shape = t.shape;
  2030. s.lineScale = t.scale * (0.35 + 0.9 * p[5]);
  2031. s.crossFeed = 0.35 + 0.65 * p[6];
  2032. s.modRateHz = 0.05 + 4.95 * p[7];
  2033. s.modDepthMs = 0.8 * t.modScale * p[8] / std::sqrt (std::max (0.3, s.modRateHz));
  2034. s.lowMult = 0.5 + 1.5 * p[9];
  2035. s.lowXover = 100.0 + 9900.0 * p[10];
  2036. s.highMult = 1.15 * (0.1 + 0.9 * p[11]); // (+15 %: what interpolation and diffusion take away)
  2037. s.highXover = 100.0 + 14900.0 * p[12];
  2038. s.earlySec = (1.0 + 999.0 * p[13]) / 1000.0;
  2039. s.earlyDecay = 2.0 + 3.0 * p[14];
  2040. s.earlyDiffusion = 0.3 + 0.7 * p[14];
  2041. s.earlySend = p[17];
  2042. // round 170c: Early mod rate / depth were read by nothing (the "shell" audit); the early taps now move like the
  2043. // late lines (same laws: 0.05 … 5 Hz, depth falling with the rate)
  2044. s.earlyModRateHz = 0.05 + 4.95 * p[15];
  2045. s.earlyModDepthMs = 0.8 * t.modScale * p[16] / std::sqrt (std::max (0.3, s.earlyModRateHz));
  2046. s.inDiffusion = t.inDiff * p[18];
  2047. s.loopDiffusion = 0.3 + 0.6 * p[18];
  2048. const auto space = (double) p[20];
  2049. s.buildSec = 0.02 + 0.06 * space;
  2050. s.gain = 0.47 * (1.0 + 0.6 * space) * 1.25 * 1.18; // round 172d: +2 dB with the crossfade below (music / drums RMS within 1 dB at the defaults); 183b: +1.4 dB with the equal-power fade and the lower shelf
  2051. // round 172d: the reference's late field holds far more below 100 Hz (impulse and music, octave bands: 31 / 63 Hz
  2052. // +8 … +15 dB at the defaults) than this tank's few low modes give
  2053. s.lowShelfHz = 90.0;
  2054. s.lowShelfDb = 5.0; // round 183b: +10 dB was 4 … 7 dB too much below 80 Hz with the louder early part (impulse at −20 dB, music)
  2055. s.toneMidHz = 140.0; // (the tank's own hump around 125 … 160 Hz taken back)
  2056. s.toneMidDb = -5.0;
  2057. s.toneMidQ = 1.4;
  2058. s.highShelfHz = 9000.0; // and keeps more air above 9 kHz (third octaves 10 … 16 kHz −4 … −7 dB without it)
  2059. s.highShelfDb = 4.0;
  2060. s.outLowCut = 1000.0 * p[21];
  2061. s.outLowOrder = 1;
  2062. if (s.outLowCut < 5.0)
  2063. s.outLowCut = 0.0;
  2064. if (p[atrium::engineLaw] >= 0.5f)
  2065. applyLaw (s, atriumLaw[choice (p[19], atrium::typeCount)]);
  2066. if (p[atrium::engineLaw] >= 0.5f && p[atrium::revision] >= 0.5f)
  2067. {
  2068. s.revision = 1; // round 190it
  2069. applyTrim (s, atriumTrim[choice (p[19], atrium::typeCount)]);
  2070. s.buildSplit = true;
  2071. }
  2072. }
  2073. };
  2074. // ---------------------------------------------------------------------------------------------- Foil Plate
  2075. // Laws: predelay 0..500 ms (v^2.32), decay 0.5..30 s (measured knots), size / width 0..200 %, mod rate 0.05..5 Hz
  2076. // (v^2.38), low EQ 20..2000 Hz, high EQ 200..20000 Hz, EQ gains ±12 dB.
  2077. constexpr double foilDecayKnots[] = { 0.0, 0.5, 0.5, 3.0, 0.75, 5.9, 0.775, 6.7, 0.8, 8.0, 0.825, 9.4, 0.85, 11.3, 0.875, 13.1,
  2078. 0.9, 15.7, 0.925, 18.4, 0.95, 21.7, 0.975, 25.8, 1.0, 30.0 };
  2079. constexpr int foilDecayCount = (int) (sizeof (foilDecayKnots) / sizeof (double) / 2);
  2080. struct FoilType
  2081. {
  2082. const char* name;
  2083. double scale, preMs, lowRt, damp, bw, early, gain;
  2084. };
  2085. // plate types: line scale, own onset (ms), bass RT ×, high-frequency absorption (1/s at 8 kHz), band limit (Hz, 0 = full band),
  2086. // early level, gain
  2087. const FoilType foilTypes[foil::typeCount] = {
  2088. { "Silver", 1.00, 20.5, 1.0, 0.34, 11000.0, 0.35, 1.00 },
  2089. { "Pewter", 0.85, 1.0, 1.0, 0.30, 11000.0, 0.45, 1.13 },
  2090. { "Nickel", 1.10, 56.0, 1.0, 0.25, 11500.0, 0.35, 0.83 },
  2091. { "Bronze", 0.90, 17.0, 1.0, 0.30, 11000.0, 0.55, 1.05 },
  2092. { "Bismuth", 1.20, 6.5, 1.0, 0.20, 11000.0, 0.30, 1.00 },
  2093. { "Tin", 1.00, 18.5, 1.0, 0.32, 11000.0, 0.35, 1.13 },
  2094. { "Zinc", 1.30, 6.5, 1.1, 0.18, 11300.0, 0.30, 1.16 },
  2095. { "Platinum", 1.20, 81.0, 1.3, 0.35, 10800.0, 0.25, 0.62 },
  2096. { "Tungsten", 1.00, 21.0, 1.0, 0.30, 0.0, 0.35, 0.68 },
  2097. { "Iron", 1.10, 56.0, 1.1, 0.30, 0.0, 0.30, 0.60 },
  2098. { "Gold", 1.00, 20.5, 1.0, 0.30, 0.0, 0.35, 0.68 },
  2099. { "Lead", 1.00, 1.5, 1.0, 0.45, 11000.0, 0.40, 1.00 },
  2100. };
  2101. double foilDecaySec (float v) { return knotLaw (v, foilDecayKnots, foilDecayCount); }
  2102. // round 190bz: law 1 per plate type, and for the measured plate voicing
  2103. // fitted with the tail lab (round 190bz; the reference and the scores per row: docs/PARITY.md); a neutral entry is the classic voicing on the new loop
  2104. LYNX_FIT_TABLE LawVoicing foilLaw[foil::typeCount] = {
  2105. { { 1.062, 0.891, 0.982, 0.948, 0.906, 0.886, 0.789, 0.832 }, { -1.25, -1.92, -1.60, -1.46, -0.93, -0.35, 1.42, -1.23 }, 2.01, 0.150, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 10159, 8064, 5000 }, { 1.95, -1.15, 0.00 }, { 2.0, 2.0, 1.4 } }, // Silver
  2106. { { 1.111, 0.990, 1.033, 0.957, 0.940, 0.947, 0.889, 1.042 }, { -1.42, -0.59, -0.09, -0.79, 0.47, -0.56, 0.42, -4.09 }, -0.29, 0.650, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 10159, 400, 5000 }, { 1.90, -1.79, 0.00 }, { 2.0, 2.0, 1.4 } }, // Pewter
  2107. };
  2108. // the measured plate voicing: fitted at three decay times, blended between them on the decay's logarithm (the third
  2109. // bell fills a 2 … 3 dB dip around 190 Hz that every setting showed against the reference)
  2110. constexpr double foilMeasuredLawSec[3] = { 1.0, 3.0, 10.0 };
  2111. LYNX_FIT_TABLE LawVoicing foilMeasuredLaw[3] = {
  2112. { { 1.416, 0.965, 1.094, 0.917, 1.058, 1.076, 1.001, 0.856 }, { 1.92, 1.10, -0.87, -3.09, -3.11, -2.49, -0.60, -0.84 }, -2.73, 0.150, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 318, 63, 188 }, { -1.49, -1.43, 3.00 }, { 2.0, 2.0, 4.0 } }, // decay 1 s
  2113. { { 1.106, 0.936, 1.018, 1.009, 0.992, 0.971, 1.011, 0.839 }, { -0.36, -0.71, -1.22, -2.71, -2.48, -1.30, 0.30, -0.04 }, -0.02, 0.300, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 318, 63, 188 }, { -1.41, -1.15, 3.00 }, { 2.0, 2.0, 4.0 } }, // decay 3 s
  2114. { { 1.231, 0.924, 0.967, 1.069, 0.969, 0.945, 0.968, 0.830 }, { -3.00, -1.28, 0.11, -0.43, -0.67, -0.06, -0.15, -2.05 }, 0.10, 0.450, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 1.00, 1.00, { 318, 100, 188 }, { -1.34, 1.20, 3.00 }, { 2.0, 2.0, 4.0 } }, // decay 10 s
  2115. };
  2116. // round 150 (parity150a) [EXEC]: the measured plate voicing (the Little Plate role, official trial, impulses at Decay 0.5 …
  2117. // 20 s and ∞, REA_rodada150a §7): the plate speaks after 0.7 ms (no plate delay), and its decay per band against the
  2118. // Decay label — the bass rings longer than the label at long settings (20 s: 35 s at 125 Hz), the highs shorter
  2119. // (20 s: 5.5 s at 8 kHz), short settings ring a little longer than the label in the presence band, "∞" is ≈ 80 s.
  2120. // Rows: label (s), the engine's RT60 (mid band), bass / high multipliers (fitted through Foil's own engine, plate.py)
  2121. struct FoilVoicingRow { double label, rt, low, high; };
  2122. LYNX_FIT_TABLE FoilVoicingRow foilVoicingRows[] = {
  2123. #include "core/FoilVoicing.inc"
  2124. };
  2125. constexpr int foilVoicingCount = (int) (sizeof (foilVoicingRows) / sizeof (foilVoicingRows[0]));
  2126. FoilVoicingRow foilVoicingAt (double label) noexcept
  2127. {
  2128. const auto& a = foilVoicingRows[0];
  2129. if (label <= a.label)
  2130. return a;
  2131. for (int i = 1; i < foilVoicingCount; ++i)
  2132. {
  2133. const auto& lo = foilVoicingRows[i - 1];
  2134. const auto& hi = foilVoicingRows[i];
  2135. if (label <= hi.label || i == foilVoicingCount - 1)
  2136. {
  2137. const auto t = std::clamp (std::log (label / lo.label) / std::log (hi.label / lo.label), 0.0, 1.0);
  2138. const auto lg = [t] (double x, double y) { return std::exp (std::log (x) + t * (std::log (y) - std::log (x))); };
  2139. return { label, lg (lo.rt, hi.rt), lg (lo.low, hi.low), lg (lo.high, hi.high) };
  2140. }
  2141. }
  2142. return foilVoicingRows[foilVoicingCount - 1];
  2143. }
  2144. class FoilFx final : public VerbEffect
  2145. {
  2146. public:
  2147. FoilFx() : VerbEffect (EffectKind::foilPlate, 0.2, 0.62) {}
  2148. protected:
  2149. // (round 190cm: the measured plate voicing is another tank too — its own level (+6 … +16 dB), band edges, onset
  2150. // network and, under the Measured law, loop voicing — so switching it while playing takes the same fade as a type)
  2151. int structure (const EffectParams& p) const noexcept override
  2152. {
  2153. return choice (p[11], foil::typeCount) + (p[foil::engineLaw] >= 0.5f ? 1000 : 0) + (p[14] >= 0.5f ? 2000 : 0);
  2154. }
  2155. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  2156. {
  2157. const auto& t = foilTypes[choice (p[11], foil::typeCount)];
  2158. mix = p[0];
  2159. const auto size = 2.0 * p[3];
  2160. s.predelaySec = (epochPredelay (p[1]) + t.preMs * (0.55 + 0.45 * size)) / 1000.0;
  2161. s.shape = 2;
  2162. s.lineScale = t.scale * (0.5 + 0.5 * size);
  2163. const auto rt = foilDecaySec (p[2]);
  2164. // round 135 (the Little Plate role): Infinite holds the plate (the decay knob's ∞), damped as at 30 s
  2165. const auto infinite = p[12] >= 0.5f;
  2166. s.rt60 = infinite ? 1000.0 : rt;
  2167. // plates lose highs faster the longer they ring: 1/RT(8 kHz) = 1/RT + damp (measured, §3)
  2168. s.highMult = 1.0 / (1.0 + t.damp * (infinite ? 30.0 : rt));
  2169. s.highXover = 5000.0;
  2170. s.lowMult = t.lowRt;
  2171. s.lowXover = 250.0;
  2172. // round 172d: the reference plate's band edge is higher and far steeper (third octaves: 10 kHz kept, 12.7 kHz
  2173. // −35 dB, 16 kHz −70 dB under 10 kHz, against the old 9 kHz, 4-pole edge): 16 poles
  2174. s.bandLimit = t.bw;
  2175. s.bandLimitSections = 8;
  2176. s.inDiffusion = 1.0;
  2177. s.loopDiffusion = 0.85;
  2178. // 0 % = mono, 100 % = decorrelated, 200 % = slightly anti-correlated (−0.21, measured)
  2179. s.width = p[4] <= 0.5f ? 2.0 * p[4] : 1.0 + 0.48 * (p[4] - 0.5);
  2180. s.modRateHz = skewLaw (p[5], 0.05, 5.0, 2.381);
  2181. s.modDepthMs = 1.0 * p[6] / std::sqrt (std::max (0.3, s.modRateHz));
  2182. // round 183b: the reference's Low / High EQ (Mix 100 %, the impulse at −20 dB, gain at 0 / 100 %): the low shelf
  2183. // spans ±12 dB with its half-gain point at ~0.47 × the frequency law (−6 dB near 330 Hz at the default 711 Hz),
  2184. // the high one +12 dB with its half-gain point at ~0.5 × (5 kHz at the default) but only −4.5 dB down
  2185. s.lowShelfHz = 0.47 * (20.0 + 1980.0 * p[7]);
  2186. s.lowShelfDb = -12.0 + 24.0 * p[8];
  2187. s.highShelfHz = 0.5 * (200.0 + 19800.0 * p[9]);
  2188. s.highShelfDb = p[10] >= 0.5f ? 24.0 * (p[10] - 0.5) : -9.0 * (0.5 - p[10]);
  2189. // the plate speaks at once after its own delay: a short, smeared early burst
  2190. s.earlyLevel = t.early;
  2191. s.earlySec = 0.015;
  2192. s.earlyDiffusion = 0.8;
  2193. s.gain = t.gain * 1.3; // round 172d: +2.3 dB (the reference plate at its defaults: music / drums RMS −3.6 / −4.2 dB)
  2194. // round 183b (the ValhallaPlate role, voicing off; the reference's impulse at −20 dB and music): Width keeps the
  2195. // level (the reference: −27.4 / −27.5 / −27.2 dB at 0 / 50 / 100 %, ours moved 4 dB), its tail is fuller below
  2196. // 80 Hz (+5 dB at 28 … 71 Hz) and less forward at 2.8 … 7 kHz (−3.5 dB), and it sits ~1 dB lower; shorter decays
  2197. // are no louder than the default
  2198. if (p[14] < 0.5f)
  2199. {
  2200. s.gain *= 0.9 * std::sqrt (2.0 / (1.0 + s.width * s.width));
  2201. // and shorter decays a little quieter (Decay 20 / 25 %: this tank ran +1.4 … +2.7 dB on music, +3 dB on the impulse)
  2202. s.gain *= std::pow (10.0, -2.5 * std::max (0.0, 0.5 - (double) p[2]) / 0.3 / 20.0);
  2203. s.toneLowHz = 60.0;
  2204. s.toneLowDb = 5.0;
  2205. s.toneMidHz = 4500.0;
  2206. s.toneMidDb = -3.5;
  2207. s.toneMidQ = 0.8;
  2208. }
  2209. // round 135: a low cut on the plate's output, 20 Hz … 1 kHz (0 = off; 12 dB/oct)
  2210. s.outLowCut = foil::lowCutHz (p[13]);
  2211. s.outLowOrder = 2;
  2212. // round 150: the measured plate voicing (the Little Plate role): no plate delay, the measured decay per band
  2213. if (p[14] >= 0.5f)
  2214. {
  2215. const auto row = foilVoicingAt (infinite ? 1000.0 : rt);
  2216. s.predelaySec = epochPredelay (p[1]) / 1000.0 + 0.0005;
  2217. s.rt60 = row.rt;
  2218. s.lowMult = row.low;
  2219. s.lowXover = 300.0;
  2220. s.highMult = row.high;
  2221. s.highXover = 3000.0;
  2222. s.bandLimit = 0.0;
  2223. // round 172d: re-measured at Mix 100 % (the trial, the impulse's whole response at Decay 1 / 3 / 10 s, with the
  2224. // darker tail and quieter early part below): the plate is 6 … 16 dB louder than round 150's level, more so at
  2225. // short decays (dB per decade of the Decay label)
  2226. static constexpr double levelKnots[] = { 0.0, 15.7, 0.477, 9.0, 1.0, 5.8, 1.5, 4.3 };
  2227. const auto label = infinite ? 30.0 : std::max (1.0, rt);
  2228. s.gain *= 0.82 / 1.3 * std::pow (10.0, knotLaw (std::log10 (label), levelKnots, 4) / 20.0); // (round 150: −1.7 dB)
  2229. // round 172d: it is darker above 2 kHz than this tank (third octaves 2.5 … 8 kHz −5 … −8 dB, both the impulse
  2230. // and music, the decay per band already matched) and its first 80 ms hold 7 … 10 dB less of the energy
  2231. s.toneHighHz = 2500.0;
  2232. // round 172d slice 10: deeper at short decays (the trial's 2 … 8 kHz octaves, sweep and impulses at Decay 0.5 …
  2233. // 19 s, refcap0124: this tank was 7 … 9 dB too bright up to 1.7 s, 5 … 7 dB at 3 s, 2 … 3 dB from 5.7 s on). The
  2234. // shelf goes from −5 dB to −13.5 / −12 / −9 dB and the edge from 7 to 9 kHz (8 … 16 kHz then within 1 … 2.5 dB)
  2235. static constexpr double darkKnots[] = { -0.301, -13.5, 0.225, -13.0, 0.477, -12.0, 0.753, -9.0, 1.28, -9.0 };
  2236. s.toneHighDb = knotLaw (std::log10 (infinite ? 30.0 : std::max (0.5, rt)), darkKnots, 5);
  2237. s.outHighCut = std::min (s.outHighCut > 0.0 ? s.outHighCut : 1.0e9, 9000.0); // (and from 8 kHz up −8 … −14 dB)
  2238. // round 172d slice 10: the trial's plate has no sub-bass (20 … 32 Hz 11 … 16 dB, 32 … 40 Hz 2 … 8 dB under this tank's,
  2239. // 50 … 63 Hz level): a 12 dB/oct cut at 55 Hz unless the panel's Low Cut is higher
  2240. if (s.outLowCut < 55.0)
  2241. {
  2242. s.outLowCut = 55.0;
  2243. s.outLowOrder = 2;
  2244. }
  2245. s.outHighOrder = 2;
  2246. s.earlyLevel *= 0.35;
  2247. }
  2248. if (p[foil::engineLaw] >= 0.5f)
  2249. {
  2250. if (p[14] >= 0.5f)
  2251. {
  2252. const auto at = std::log (std::clamp (infinite ? 30.0 : rt, foilMeasuredLawSec[0], foilMeasuredLawSec[2]));
  2253. const auto k = at < std::log (foilMeasuredLawSec[1]) ? 0 : 1;
  2254. applyLaw (s, blendLaw (foilMeasuredLaw[k], foilMeasuredLaw[k + 1],
  2255. (at - std::log (foilMeasuredLawSec[k])) / std::log (foilMeasuredLawSec[k + 1] / foilMeasuredLawSec[k])));
  2256. }
  2257. else
  2258. applyLaw (s, foilLaw[choice (p[11], foil::typeCount)]);
  2259. if (p[foil::revision] >= 0.5f)
  2260. {
  2261. s.revision = 1; // round 190it
  2262. applyTrim (s, foilTrim[p[14] >= 0.5f ? foil::typeCount : choice (p[11], foil::typeCount)]);
  2263. }
  2264. }
  2265. }
  2266. };
  2267. // ---------------------------------------------------------------------------------------------- Nebula
  2268. // Laws: delay 0..2000 ms (measured knots), feedback / density / warp 0..100 %, width −100..100 %, low cut 10..2000 Hz,
  2269. // high cut 200..20000 Hz, mod rate 0.01..10 Hz (v^2.33). Feedback F gives a gain of √F per pass of the delay.
  2270. constexpr double nebulaDelayKnots[] = { 0.0, 0.0, 0.5, 300.0, 0.75, 588.5, 0.775, 675.0, 0.925, 1425.0, 0.95, 1583.3, 0.975, 1791.7, 1.0, 2000.0 };
  2271. constexpr int nebulaDelayCount = (int) (sizeof (nebulaDelayKnots) / sizeof (double) / 2);
  2272. struct NebulaMode
  2273. {
  2274. const char* name;
  2275. double dm0, dm100, mult0, mult100, corr, gain;
  2276. };
  2277. // decay multiplier at density 0 / 100, line length × delay at density 0 / 100, stereo correlation (round 183b: the
  2278. // reference's tail at its defaults, Mod depth 50 %; mono modes 1.0, one mode −1),
  2279. // wet gain (round 172d: the reference's wet level per mode at Mix 100 %, impulse / bursts / music, defaults otherwise)
  2280. LYNX_FIT_TABLE NebulaMode nebulaModes[nebula::modeCount] = {
  2281. { "Ripple", 1.03, 1.26, 1, 1, 0.0, 2.41 }, { "Swell", 3.43, 3.82, 1, 2, 0.04, 1.60 },
  2282. { "Fold", 1.33, 1.39, 1, 2, 0.04, 1.79 }, { "Drift", 2.56, 2.67, 1, 1, 0.09, 1.12 },
  2283. { "Abyss", 3.64, 3.97, 1, 2, -0.01, 1.26 }, { "Vortex", 7.04, 6.77, 2, 2, 0.02, 1.05 },
  2284. { "Glint", 1.03, 1.17, 1, 1, 0.07, 1.67 }, { "Pulse", 1.03, 1.22, 1, 1, 0.0, 1.93 },
  2285. { "Expanse", 4.29, 12.8, 8, 1, 1, 3.57 }, { "Haze", 2.14, 12.3, 4, 1, 1, 3.87 },
  2286. { "Veil", 2.02, 5.04, 2, 1, 0.0, 1.43 }, { "Mist", 1.00, 1.01, 1, 1, 0.0, 1.49 },
  2287. { "Monolith", 2.16, 16.3, 4, 1, 1, 3.84 }, { "Eternity", 20.0, 20.7, 8, 1, -1, 2.06 },
  2288. { "Tide", 1.00, 1.12, 1, 1, 0.0, 1.50 }, { "Shoal", 1.03, 1.30, 1, 1, -0.04, 1.51 },
  2289. { "Spark", 1.00, 1.05, 1, 1, -0.02, 2.43 }, { "Plateau", 2.10, 9.65, 4, 1, 0.95, 3.62 },
  2290. { "Dusk", 4.15, 12.65, 8, 1, 0.99, 2.86 }, { "Mirage", 1.00, 1.35, 1, 2, 0.0, 1.50 },
  2291. { "Cluster", 1.02, 1.01, 1, 1, 0.02, 2.56 }, { "Beacon", 1.16, 1.04, 1, 1, -0.01, 2.27 },
  2292. };
  2293. double nebulaDelayMs (float v) { return knotLaw (v, nebulaDelayKnots, nebulaDelayCount); }
  2294. const char* const syncNames[4] = { "ms", "Note", "Dotted", "Triplet" };
  2295. // Round 190ca: the cloud law (parameter 14, on for new instances). Six of the 22 networks — the ones whose structure
  2296. // is published — run on core/NebulaCloud (a port of an MIT-licensed model, see that header); the other sixteen and
  2297. // every project saved before the round (Law off: upgradeLoadedEffectParams) keep the classic engine below, unchanged.
  2298. constexpr int nebulaLaw = 14;
  2299. cloud::Topology nebulaTopology (int mode) noexcept
  2300. {
  2301. switch (mode)
  2302. {
  2303. case 0: return cloud::Topology::fdn16;
  2304. case 2: return cloud::Topology::serialQuad;
  2305. case 3: return cloud::Topology::serialStereo;
  2306. case 4: return cloud::Topology::loopedQuad;
  2307. case 6: return cloud::Topology::fdn4;
  2308. case 19: return cloud::Topology::fdn4Filtered;
  2309. default: return cloud::Topology::none;
  2310. }
  2311. }
  2312. class NebulaFx final : public VerbEffect
  2313. {
  2314. public:
  2315. NebulaFx() : VerbEffect (EffectKind::nebula, 4.2, 0.05) {}
  2316. void prepare (double sampleRate) noexcept override
  2317. {
  2318. VerbEffect::prepare (sampleRate);
  2319. network.prepare (sampleRate);
  2320. }
  2321. void reset() noexcept override
  2322. {
  2323. VerbEffect::reset();
  2324. network.reset();
  2325. change = Fade::none;
  2326. placed = false;
  2327. }
  2328. // CLEAR (round 129): the panel's push button flips parameter 13; a flip empties the delay network at once
  2329. void process (float* left, float* right, int n, const EffectParams& p, double bpm) noexcept override
  2330. {
  2331. if (p[13] != lastClear)
  2332. {
  2333. if (lastClear >= 0.0f)
  2334. reset();
  2335. lastClear = p[13];
  2336. }
  2337. const auto want = p[nebulaLaw] >= 0.5f ? nebulaTopology (choice (p[12], nebula::modeCount)) : cloud::Topology::none;
  2338. if (! placed)
  2339. placed = true, active = want, before = p;
  2340. // round 190ie (issue #185): a synced delay whose tempo moved (and nothing else of its time): the lines' change
  2341. // spreads over the block (cloud::Controls::spread, Settings::lengthSpread) instead of the fixed glide's burst
  2342. tempoGlide = choice (p[1], 4) > 0 && lastBpm > 0.0 && bpm != lastBpm && p[1] == before[1] && p[2] == before[2] && p[3] == before[3];
  2343. lastBpm = bpm;
  2344. if (want != active && change == Fade::none)
  2345. {
  2346. change = Fade::out;
  2347. changeLeft = fadeLength;
  2348. kept = before;
  2349. }
  2350. if (active == cloud::Topology::none && change == Fade::none)
  2351. {
  2352. VerbEffect::process (left, right, n, p, bpm); // the classic engine, as before the round
  2353. before = p;
  2354. return;
  2355. }
  2356. // a cloud network, or the 11 ms either side of a change between the two engines / two cloud networks (the wet
  2357. // part fades out on the old one with its settings held, the new one starts empty and fades in)
  2358. for (int done = 0; done < n;)
  2359. {
  2360. auto m = std::min (n - done, chunk);
  2361. if (change != Fade::none)
  2362. m = std::min (m, changeLeft);
  2363. const auto& q = change == Fade::out ? kept : p;
  2364. auto* l = left + done;
  2365. auto* r = right + done;
  2366. float dry, wet;
  2367. mixGains (q[0], dry, wet);
  2368. std::copy (l, l + m, inL.begin());
  2369. std::copy (r, r + m, inR.begin());
  2370. if (active == cloud::Topology::none)
  2371. {
  2372. VerbEffect::process (l, r, m, q, bpm);
  2373. for (int i = 0; i < m; ++i)
  2374. {
  2375. l[i] -= dry * inL[(std::size_t) i];
  2376. r[i] -= dry * inR[(std::size_t) i];
  2377. }
  2378. wet = 1.0f;
  2379. }
  2380. else
  2381. {
  2382. auto c = controls (q, bpm, active);
  2383. c.spread = tempoGlide && change == Fade::none ? m : 0;
  2384. network.process (inL.data(), inR.data(), l, r, m, c);
  2385. }
  2386. for (int i = 0; i < m; ++i)
  2387. {
  2388. auto w = wet;
  2389. if (change != Fade::none)
  2390. {
  2391. const auto g = (float) (changeLeft - i) / (float) fadeLength; // 1 … 0 over the fade
  2392. w *= change == Fade::out ? g : 1.0f - g;
  2393. }
  2394. l[i] = dry * inL[(std::size_t) i] + w * l[i];
  2395. r[i] = dry * inR[(std::size_t) i] + w * r[i];
  2396. }
  2397. done += m;
  2398. if (change != Fade::none && (changeLeft -= m) <= 0)
  2399. {
  2400. if (change == Fade::out)
  2401. {
  2402. change = Fade::in;
  2403. changeLeft = fadeLength;
  2404. active = want;
  2405. if (active == cloud::Topology::none)
  2406. VerbEffect::reset();
  2407. else
  2408. network.reset();
  2409. }
  2410. else
  2411. change = Fade::none;
  2412. }
  2413. }
  2414. before = p;
  2415. }
  2416. private:
  2417. // the cloud law's controls: the same parameters in the reference's units (the model takes them directly)
  2418. cloud::Controls controls (const EffectParams& p, double bpm, cloud::Topology topology) const noexcept
  2419. {
  2420. cloud::Controls c;
  2421. c.topology = topology;
  2422. c.delayMs = syncedMs (choice (p[1], 4), choice (p[2], 7), nebulaDelayMs (p[3]), bpm);
  2423. c.warp = p[4];
  2424. c.feedback = p[5];
  2425. c.density = p[6];
  2426. c.width = -1.0 + 2.0 * p[7];
  2427. c.lowCutHz = 10.0 + 1990.0 * p[8];
  2428. c.highCutHz = 200.0 + 19800.0 * p[9];
  2429. c.modRateHz = skewLaw (p[10], 0.01, 10.0, 2.328);
  2430. c.modDepth = p[11];
  2431. return c;
  2432. }
  2433. float lastClear = -1.0f;
  2434. double lastBpm = 0.0;
  2435. bool tempoGlide = false; // (this block: the synced delay's tempo moved)
  2436. cloud::Network network;
  2437. cloud::Topology active = cloud::Topology::none;
  2438. Fade change = Fade::none;
  2439. int changeLeft = 0;
  2440. bool placed = false;
  2441. EffectParams before {}, kept {};
  2442. std::array<float, chunk> inL {}, inR {};
  2443. protected:
  2444. int structure (const EffectParams& p) const noexcept override { return choice (p[12], nebula::modeCount); }
  2445. void configure (const EffectParams& p, double bpm, Settings& s, float& mix) const noexcept override
  2446. {
  2447. const auto& m = nebulaModes[choice (p[12], nebula::modeCount)];
  2448. mix = p[0];
  2449. const auto delay = std::max (1.0, syncedMs (choice (p[1], 4), choice (p[2], 7), nebulaDelayMs (p[3]), bpm));
  2450. const auto warp = (double) p[4];
  2451. const auto fb = (double) p[5];
  2452. const auto dens = (double) p[6];
  2453. const auto lerpLog = [&] (double a, double b) { return std::exp (std::log (a) + (std::log (b) - std::log (a)) * dens); };
  2454. const auto mult = lerpLog (m.mult0, m.mult100);
  2455. const auto sw = warp * warp * (3.0 - 2.0 * warp); // smoothstep: lines shorten as warp rises
  2456. s.shape = 3;
  2457. s.lineScale = delay * mult;
  2458. s.lineSpread = sw;
  2459. s.lengthSpread = tempoGlide ? 1 : 0; // (round 190ie: VerbEffect::run spreads it over each chunk)
  2460. // √F per pass of the delay: RT = 6·D / −log10 F, times the shape's measured decay multiple (which already holds
  2461. // its line-length multiple); warp shortens the lines and so the decay
  2462. const auto meanSec = delay * (1.0 - 0.53 * sw) / 1000.0; // (the warp fractions average 0.47)
  2463. s.rt60 = fb >= 0.9999 ? 1000.0 : 6.0 * meanSec * lerpLog (m.dm0, m.dm100) / std::max (1e-4, -std::log10 (std::max (1e-6, fb)));
  2464. s.rt60 = std::min (s.rt60, 1000.0);
  2465. s.inDiffusion = dens;
  2466. s.loopDiffusion = 0.85 * dens;
  2467. s.crossFeed = 0.3 + 0.7 * dens;
  2468. s.parallelLines = dens < 0.02; // density 0: plain repeats of the delay
  2469. // round 183b: stereo. The reference's tail is decorrelated whenever it modulates (L/R correlation −0.01 at the
  2470. // default Mod depth, 0.13 / 0.34 at 25 / 10 %, 1.0 at 0, at any Density) and this network decorrelates the
  2471. // same way through its modulated lines, so the width is the panel's; only the reference's mono modes (its
  2472. // correlation, measured per mode at the defaults) narrow it. Before, Density 0 played mono
  2473. const auto corr = m.corr;
  2474. const auto w = std::sqrt ((1.0 - corr) / (1.0 + corr + 1e-3));
  2475. s.width = (-1.0 + 2.0 * p[7]) * std::min (2.0, w);
  2476. // round 172d: both cuts are first order in the reference (Mix 100 %, cuts at 5 … 70 % / 10 … 75 %: −3 dB at the
  2477. // linear law's frequency, 6 dB per octave beyond)
  2478. s.outLowCut = 10.0 + 1990.0 * p[8];
  2479. s.outLowOrder = 1;
  2480. s.outHighCut = 200.0 + 19800.0 * p[9];
  2481. s.outHighOrder = 1;
  2482. s.modRateHz = skewLaw (p[10], 0.01, 10.0, 2.328);
  2483. s.modDepthMs = 2.0 * p[11] / std::sqrt (std::max (0.1, s.modRateHz));
  2484. // round 172d: the reference keeps its level nearly flat over Feedback and Density (Mix 100 %, its first mode):
  2485. // the measured wet level against this loop's, in dB per Feedback knot; Density keeps its lows there (this tank's
  2486. // diffusion takes 5 … 10 dB below 250 Hz from the summed sides), so a low shelf comes in with it
  2487. static constexpr double fbKnots[] = { 0.0, 7.0, 0.2, 6.3, 0.5, 0.0, 0.8, -2.7, 0.95, -3.6, 1.0, -4.0 };
  2488. const auto fbDb = knotLaw (fb, fbKnots, 6);
  2489. s.gain = m.gain * std::pow (10.0, fbDb / 20.0);
  2490. s.lowShelfHz = 220.0;
  2491. s.lowShelfDb = 8.0 * std::min (1.0, dens / 0.15);
  2492. // round 183b: the reference's eight voices fall apart as they modulate (its tail ~9 dB louder and mono at Mod
  2493. // depth 0: +8.9 dB against 50 %, −3.6 dB at 10 %); this network's lines part less, so the rest comes in here.
  2494. // The level is re-measured with the full width: −0.9 dB at Density 0, −4.2 dB from Density 50 % up (the dense
  2495. // network's sides were nearly mono before)
  2496. s.gain *= (0.9 - 0.28 * std::min (1.0, dens / 0.5)) * std::pow (10.0, 5.0 * std::exp (-(double) p[11] / 0.1) / 20.0);
  2497. }
  2498. };
  2499. // ---------------------------------------------------------------------------------------------- Halo
  2500. // The reference exposes bare 0..1 values; the laws below are what the measurements showed (§5): shift −12..+12
  2501. // semitones (linear), decay set mainly by Size (≈ 1.5 s + 12.5 s × size), high cut 100..15000 Hz first order (the
  2502. // reference room's law), low cut 0..1000 Hz; mod rate / depth laws not confirmed.
  2503. struct HaloMode
  2504. {
  2505. const char* name;
  2506. double scale, rtScale, bright;
  2507. float value; // the Reverb mode value that selects it (nearest wins)
  2508. int stereo; // 0 = LYNX's own wide tank, 1 = mono in (the reference's Mono), 2 = two separate sides (its stereo modes)
  2509. double width; // side gain of the output
  2510. };
  2511. // round 135: the reference's four modes [EXEC: its trial, a left-only impulse — below 0.25 the input is summed and the
  2512. // tail comes out of both sides, from 0.25 up the left input stays on the left; 0.25 / 0.5 / 0.75 are the steps] beside
  2513. // LYNX's own Wide / Dense / Bright, which keep their values 0 / ½ / 1 (older projects sound the same)
  2514. const HaloMode haloModes[7] = { { "Wide", 1.2, 1.0, 0.0, 0.0f, 0, 1.0 }, { "Mono", 1.0, 1.0, 0.0, 0.125f, 1, 1.0 },
  2515. { "Small stereo", 0.75, 0.85, 0.0, 0.25f, 2, 1.0 }, { "Medium stereo", 1.0, 1.0, 0.0, 0.375f, 2, 1.0 },
  2516. { "Dense", 0.9, 1.05, 0.0, 0.5f, 0, 1.0 }, { "Big stereo", 1.3, 1.15, 0.0, 0.625f, 2, 1.0 },
  2517. { "Bright", 1.0, 0.75, 1.0, 1.0f, 0, 1.0 } };
  2518. int haloModeIndex (float v)
  2519. {
  2520. int best = 0;
  2521. for (int i = 1; i < 7; ++i)
  2522. if (std::abs (v - haloModes[i].value) < std::abs (v - haloModes[best].value))
  2523. best = i;
  2524. return best;
  2525. }
  2526. const char* const haloShiftModes[5] = { "Single", "Dual", "Single, bright", "Dual, bright", "No shift" };
  2527. const char* const haloColours[2] = { "Bright", "Dark" };
  2528. double haloShiftSemis (float v) { return -12.0 + 24.0 * v; }
  2529. // round 190bz: law 1
  2530. // fitted with the tail lab (round 190bz; the reference and the scores per row: docs/PARITY.md); a neutral entry is the classic voicing on the new loop
  2531. LYNX_FIT_TABLE LawVoicing haloLaw = { { 1.108, 1.268, 1.316, 1.083, 1.164, 1.001, 1.182, 1.427 }, { 1.63, -1.12, -1.41, -0.40, -0.68, -1.46, -1.95, -0.59 }, 2.13, 0.000, 30, 0.00, 0.00, 1.00, 1.00, 0.00, 0.30, 6.00, 1.00, { 63, 159, 5000 }, { -2.29, -1.54, 0.00 }, { 2.0, 2.0, 1.4 } };
  2532. // round 190it (revision 1): the RT per reverb mode against the reference's (refcap030's mode sweep, impulses: ours ran
  2533. // ×1.13 / ×0.75 / ×1.0 / ×2.85 its RT in Mono / Small / Medium / Big stereo)
  2534. LYNX_FIT_TABLE double haloRev1Rt[7] = { 1.0, 0.88, 1.33, 1.0, 1.0, 0.35, 1.0 };
  2535. #ifdef LYNX_VERB_FIT
  2536. const double* haloRtTable()
  2537. {
  2538. static const bool once = []
  2539. {
  2540. if (const char* e = std::getenv ("LYNX_HALORT"))
  2541. std::sscanf (e, "%lf,%lf,%lf,%lf,%lf,%lf,%lf", &haloRev1Rt[0], &haloRev1Rt[1], &haloRev1Rt[2], &haloRev1Rt[3], &haloRev1Rt[4], &haloRev1Rt[5], &haloRev1Rt[6]);
  2542. return true;
  2543. }();
  2544. (void) once;
  2545. return haloRev1Rt;
  2546. }
  2547. #else
  2548. const double* haloRtTable() { return haloRev1Rt; }
  2549. #endif
  2550. class HaloFx final : public VerbEffect
  2551. {
  2552. public:
  2553. HaloFx() : VerbEffect (EffectKind::halo, 0.3, 0.05) {}
  2554. protected:
  2555. int structure (const EffectParams& p) const noexcept override { return p[halo::engineLaw] >= 0.5f ? 1000 : 0; }
  2556. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  2557. {
  2558. mix = p[0];
  2559. const auto& m = haloModes[haloModeIndex (p[9])];
  2560. const auto shiftMode = choice (p[10], 5);
  2561. const auto dark = choice (p[11], 2) == 1;
  2562. const auto size = (double) p[4];
  2563. s.shiftSemis = haloShiftSemis (p[1]);
  2564. s.shiftFeedback = shiftMode == 4 ? 0.0 : 0.9 * p[2]; // the shifted share of the recirculation
  2565. s.shiftMix = 1.0;
  2566. s.shift2Mix = shiftMode == 1 || shiftMode == 3 ? 0.7 : 0.0;
  2567. s.shiftSemis2 = s.shiftSemis - 24.0;
  2568. s.inDiffusion = p[3];
  2569. s.loopDiffusion = 0.2 + 0.7 * p[3];
  2570. // round tp172d (refs/halo, 20 s impulses): the reference's energy builds for 0.6 / 0.9 / 1.3 s at Size 30 / 50 / 80 %
  2571. // (Diffusion 92 %) — an even feed over 0.62 + 1.8 × Size seconds into the tank fits its envelope within 0.7 dB.
  2572. // With less diffusion it gathers late (Diffusion 50 %: a cluster near 1.2 s; 0 %: one arrival at 1.217 s): the
  2573. // feed leans to the end of a span that shrinks to 0.8 × (a model between those three measured points)
  2574. {
  2575. const auto d = std::clamp ((double) p[3] / 0.92, 0.0, 1.0);
  2576. s.buildSec = (0.62 + 1.8 * size) * (0.8 + 0.2 * d);
  2577. s.buildRise = 1.0 + 15.0 * (1.0 - d) * (1.0 - d);
  2578. }
  2579. s.shape = 0;
  2580. s.lineScale = m.scale * (0.3 + 1.2 * size);
  2581. // round tp172d: the reference's RT per Size and Feedback, measured (refs/halo: 20 s impulses; Size 30 / 50 / 80 %
  2582. // ≈ 5.6 / 8.4 / 12 s at feedback 0; feedback lengthens it ×1.2 / 1.65 / 2.2 at 50 / 75 / 100 % with the octave
  2583. // shift, ×1.4 / 2.1 / 4.3 unshifted, where the loop's own voice comes back in phase)
  2584. {
  2585. const auto fb = (double) p[2], f = std::min (1.0, std::abs (s.shiftSemis) / 2.0);
  2586. const auto mShift = 1.0 + 1.2 * std::pow (fb, 2.5), mPlain = 1.0 + 3.3 * fb * fb * fb;
  2587. // with less diffusion the reference's tail is shorter (Diffusion 0 / 50 %: ~5.5 / 6 s against 9.8 s at 92 %)
  2588. const auto dn = std::clamp ((double) p[3] / 0.92, 0.0, 1.0);
  2589. s.rt60 = (1.5 + 12.5 * size) * m.rtScale * (mPlain + (mShift - mPlain) * f) * (0.55 + 0.45 * dn * dn * dn);
  2590. if (p[halo::engineLaw] >= 0.5f && p[halo::revision] >= 0.5f)
  2591. s.rt60 *= haloRtTable()[haloModeIndex (p[9])];
  2592. }
  2593. // (the energy of its impulse matched before the RT did: a mode's RT moves with the energy kept, gain × 1 / √RT ratio)
  2594. const auto rtKeep = p[halo::engineLaw] >= 0.5f && p[halo::revision] >= 0.5f ? 1.0 / std::sqrt (haloRtTable()[haloModeIndex (p[9])]) : 1.0;
  2595. // round tp172d: its first 0.3 s are 5–9 dB under ours at 0.45; with less diffusion they are empty (Diffusion 50 %:
  2596. // −55 dB, 0 %: silent until the first arrival at 1.2 s)
  2597. s.earlyLevel = 0.3 * std::pow (std::clamp ((double) p[3] / 0.92, 0.0, 1.0), 8.0);
  2598. s.earlySec = 0.03;
  2599. s.earlyDiffusion = p[3];
  2600. s.outLowCut = 250.0 * p[5]; // gentle: the reference's low cut barely shows at 63 Hz (§5, not confirmed)
  2601. s.outLowOrder = 1;
  2602. if (s.outLowCut < 5.0)
  2603. s.outLowCut = 0.0;
  2604. s.outHighCut = 100.0 + 14900.0 * p[6];
  2605. s.outHighOrder = 1;
  2606. if (shiftMode == 2 || shiftMode == 3 || m.bright > 0.0)
  2607. s.highShelfDb = 4.0, s.highShelfHz = 3000.0;
  2608. // round 190it (revision 1): the reference's Dark colour is a tone on the output, not in the loop: against
  2609. // Bright its tail keeps the RT at every octave (8.4 / 7.8 s at 4 / 8 kHz against 8.8 / 8.7) and its spectrum
  2610. // falls −3 / −6.6 / −11.7 dB at 4 / 8 / 16 kHz and −4.7 … −7.9 dB below 50 Hz, the mids −0.5 dB (refcap030's
  2611. // colour sweep); the loop low-pass took the highs' RT down to 2.2 s and 2 dB of the level
  2612. const auto rev1 = p[halo::engineLaw] >= 0.5f && p[halo::revision] >= 0.5f;
  2613. s.loopBandwidth = dark && ! rev1 ? 5000.0 : 0.0;
  2614. if (dark && rev1)
  2615. {
  2616. s.toneHighHz = 6500.0;
  2617. s.toneHighDb = -13.0;
  2618. s.toneLowHz = 70.0;
  2619. s.toneLowDb = -6.0;
  2620. }
  2621. s.modRateHz = 0.05 + 4.95 * p[7];
  2622. s.modDepthMs = 1.2 * p[8] / std::sqrt (std::max (0.3, s.modRateHz));
  2623. // round tp172d: the wet level measured on music and drums (refs/halo): −3 dB up to Feedback 75 %, rising to +1 dB
  2624. // at 100 % (the reference swells). Its impulse has a fuller low end than ours (+9 / +5 dB at 63 / 125 Hz) but its
  2625. // music and drums don't (a shelf for the impulse made drums 5 dB loud there): left flat
  2626. {
  2627. const auto fb = (double) p[2];
  2628. const auto dn = std::clamp ((double) p[3] / 0.92, 0.0, 1.0); // less diffusion: louder (+4.5 / +5.4 dB measured at 0 / 50 %)
  2629. // round 190it (revision 1, refcap030: Feedback × Diffusion pairs, music and impulse): half that lift (at 20 /
  2630. // 50 % diffusion ours ran 2.5 dB over its level at 80 %, where the reference's does not), and 0.5 dB less overall
  2631. // (its music and test signal sat 2.6 dB under ours at the capture's base setting, the impulse 0.1 dB: −1 dB
  2632. // scored higher on refcap030 but cost tp172d's impulses, −0.5 dB is the cut no bank loses a block to)
  2633. const auto trimDb = (fb <= 0.75 ? -3.0 : -3.0 + 16.0 * (fb - 0.75)) + (rev1 ? 2.5 : 5.0) * (1.0 - dn * dn * dn) - (rev1 ? 0.5 : 0.0);
  2634. s.gain = (dark ? (rev1 ? 0.95 * 0.94 : 0.75) : 0.95) * std::pow (10.0, trimDb / 20.0) * rtKeep;
  2635. }
  2636. s.shimmerEnergy = 0.2; // the shifted voice's share added per pass (× the shimmer amount)
  2637. if (m.stereo == 2)
  2638. s.crossFeed = 0.0, s.splitSides = true; // the two sides stay apart
  2639. // round 183b: the reference's Mono mode (mono in) gives a partly correlated tail: L/R correlation 0.25 / 0.31 /
  2640. // 0.43 at Size 30 / 50 / 80 % (its stereo modes: ~0, as this tank's)
  2641. if (m.stereo == 1)
  2642. {
  2643. const auto c = 0.13 + 0.375 * size;
  2644. s.width = std::sqrt ((1.0 - c) / (1.0 + c));
  2645. s.gain *= std::sqrt (2.0 / (1.0 + s.width * s.width)); // (the same energy as the full width)
  2646. s.earlyLevel *= 0.4; // its first 80 ms hold 3.4 dB less of the energy than ours did (impulse at −20 dB)
  2647. }
  2648. if (p[halo::engineLaw] >= 0.5f)
  2649. applyLaw (s, haloLaw);
  2650. if (p[halo::engineLaw] >= 0.5f && p[halo::revision] >= 0.5f)
  2651. {
  2652. s.revision = 1; // round 190it
  2653. applyTrim (s, haloTrim[haloModeIndex (p[9])]);
  2654. s.buildSplit = true;
  2655. }
  2656. }
  2657. // the reference's Mono mode sums the input (mono in, a stereo tail)
  2658. void preStage (float* l, float* r, int n, const EffectParams& p, double) noexcept override
  2659. {
  2660. if (haloModes[haloModeIndex (p[9])].stereo != 1)
  2661. return;
  2662. for (int i = 0; i < n; ++i)
  2663. l[i] = r[i] = 0.5f * (l[i] + r[i]);
  2664. }
  2665. };
  2666. // ---------------------------------------------------------------------------------------------- Horizon
  2667. constexpr double horizonDecayKnots[] = { 0.0, 0.2, 0.5, 5.0, 0.75, 9.81, 0.775, 12.5, 0.8, 16.67, 0.825, 21.25, 0.85, 27.5,
  2668. 0.875, 33.75, 0.9, 41.67, 0.925, 50.0, 0.95, 57.5, 0.975, 63.75, 1.0, 70.0 };
  2669. constexpr int horizonDecayCount = (int) (sizeof (horizonDecayKnots) / sizeof (double) / 2);
  2670. constexpr double horizonEchoKnots[] = { 0.0, 0.0, 0.5, 100.0, 0.675, 197.2, 0.7, 250.0, 0.75, 375.0, 0.775, 456.2, 0.8, 550.0,
  2671. 0.825, 643.8, 0.85, 762.5, 0.875, 918.8, 0.9, 1075.0, 0.925, 1250.0, 0.95, 1500.0, 0.975, 1750.0,
  2672. 1.0, 2000.0 };
  2673. constexpr int horizonEchoCount = (int) (sizeof (horizonEchoKnots) / sizeof (double) / 2);
  2674. const char* const horizonEchoModes[horizon::echoModeCount] = { "Clean", "Tape", "Digital", "Analog", "Detune", "Reverse",
  2675. "Reverse Up", "Reverse Down", "Reverse Up/Down", "Sparkle", "Swarm", "Lo-fi" };
  2676. struct HorizonReverb
  2677. {
  2678. const char* name;
  2679. int shape;
  2680. double scale, rtScale, preMs, buildMs, early, gain;
  2681. double lowMult, lowDb, highDb; // round 183b: bass decay multiple (below 500 Hz), the mode's own low / high shelf
  2682. };
  2683. // round 183b, measured on the reference (Mix 100 %, Decay 3 s, Size 50 %, its default Chamber otherwise; refcap
  2684. // verb183b): RT of each mode against Decay (Room 0.77, Plate 0.97, Hall 0.93, Space 1.06), its bass decay (Room
  2685. // 0.82 × the mids, Chamber 1.2, Plate 1.12, Hall 1.3, Space 0.9), its own onset after the echo's pre-delay (Room 23,
  2686. // Chamber 13, Plate 6, Hall 31, Space 74 ms; the table holds Size 50 %), Hall's darker top (−7 dB above 4 kHz) and
  2687. // Space's fuller lows (+4 dB). gain: the wet level per mode (impulse at −20 dB and music, 100 % wet)
  2688. LYNX_FIT_TABLE HorizonReverb horizonReverbs[horizon::reverbModeCount] = {
  2689. { "Room", 1, 1.6, 0.77, 23.0, 60.0, 0.3, 2.4, 0.82, 0.0, 0.0 },
  2690. { "Chamber", 1, 1.2, 1.00, 13.0, 45.0, 0.3, 3.0, 1.20, 0.0, 0.0 },
  2691. { "Plate", 2, 1.0, 0.97, 6.0, 0.0, 0.0, 4.6, 1.12, 0.0, -5.0 },
  2692. { "Hall", 0, 1.0, 0.93, 31.0, 60.0, 0.2, 2.64, 1.30, 0.0, -7.0 },
  2693. { "Cathedral", 0, 1.3, 1.05, 45.0, 80.0, 0.1, 3.2, 1.30, 3.0, -3.0 },
  2694. { "Space", 0, 1.5, 1.06, 24.0, 90.0, 0.0, 1.4, 0.90, 4.0, -4.0 },
  2695. { "Freeze", 0, 1.4, 3.30, 30.0, 300.0, 0.0, 4.0, 1.00, 0.0, 0.0 },
  2696. { "Gated", 1, 0.8, 0.60, 2.0, 0.0, 0.1, 3.6, 1.00, 0.0, 0.0 },
  2697. };
  2698. // round 183b: the reference's colours (its manual and measured): Bright and Neutral play the same at these decays;
  2699. // Dark adds a steep low-pass at 8 kHz (and −1.5 dB from 3 kHz up); the fourth (the reference's Studio) a steep
  2700. // 600 Hz low cut (12 dB/oct, −3 dB at ~560 Hz) and a steep 10 kHz high cut (36 dB/oct at 12 kHz)
  2701. const char* const horizonColours[4] = { "Bright", "Neutral", "Dark", "Studio" };
  2702. double horizonDecaySec (float v) { return knotLaw (v, horizonDecayKnots, horizonDecayCount); }
  2703. // round 190bz: law 1 per reverb mode
  2704. // fitted with the tail lab (round 190bz; the reference and the scores per row: docs/PARITY.md); a neutral entry is the classic voicing on the new loop
  2705. LYNX_FIT_TABLE LawVoicing horizonLaw[horizon::reverbModeCount] = {
  2706. {},
  2707. { { 1.177, 0.994, 1.035, 0.965, 0.987, 0.962, 0.896, 0.980 }, { -2.48, 0.06, 1.67, -1.40, 1.21, -0.06, -1.35, -1.94 }, 0.75, 0.450, 30, 0.00, 0.00, 1.00, 0.40, 0.40, 1.00, 1.00, 1.00, { 63, 200, 5000 }, { -1.86, -1.37, 0.00 }, { 2.0, 2.0, 1.4 } }, // Chamber
  2708. };
  2709. double horizonEchoMs (float v) { return knotLaw (v, horizonEchoKnots, horizonEchoCount); }
  2710. class HorizonFx final : public VerbEffect
  2711. {
  2712. public:
  2713. HorizonFx() : VerbEffect (EffectKind::horizon, 0.25, 0.25) {}
  2714. void prepare (double sampleRate) noexcept override
  2715. {
  2716. VerbEffect::prepare (sampleRate);
  2717. for (auto& l : echo)
  2718. l.allocate ((int) (2.6 * sampleRate) + 16);
  2719. for (auto& sh : shifters)
  2720. sh.prepare (sampleRate, 0.08);
  2721. reset();
  2722. }
  2723. void reset() noexcept override
  2724. {
  2725. VerbEffect::reset();
  2726. for (auto& l : echo)
  2727. l.clear();
  2728. for (auto& sh : shifters)
  2729. sh.clear();
  2730. tone[0].z = tone[1].z = 0.0f;
  2731. lowCutState[0].z = lowCutState[1].z = 0.0f;
  2732. compEnv = 0.0f;
  2733. compGain = 1.0f;
  2734. compTick = 0;
  2735. wow = 0.0;
  2736. crush[0] = crush[1] = 0.0f;
  2737. crushPhase = 0.0;
  2738. }
  2739. protected:
  2740. int structure (const EffectParams& p) const noexcept override { return choice (p[22], horizon::reverbModeCount) + (p[horizon::engineLaw] >= 0.5f ? 1000 : 0); }
  2741. void configure (const EffectParams& p, double, Settings& s, float& mix) const noexcept override
  2742. {
  2743. const auto mode = choice (p[22], horizon::reverbModeCount);
  2744. const auto& m = horizonReverbs[mode];
  2745. mix = p[0];
  2746. const auto size = (double) p[12];
  2747. s.shape = m.shape;
  2748. s.lineScale = m.scale * (0.5 + 0.8 * size);
  2749. // round 183b: the echo is the pre-delay (Echo time); the mode adds its own onset, which grows with Size
  2750. // (measured: 25 % / 100 % / 139 % of the Size 50 % value at 0 / 50 / 100 %)
  2751. s.predelaySec = m.preMs * (0.25 + 1.85 * size - 0.7 * size * size) / 1000.0;
  2752. const auto decay = horizonDecaySec (p[11]);
  2753. s.rt60 = decay * m.rtScale * (1.03 - 0.12 * size) / 0.97; // (the reference's RT shortens ~10 % with Size)
  2754. s.buildSec = m.buildMs / 1000.0 * (0.3 + 1.4 * p[13]);
  2755. const auto earlyLate = (double) p[14];
  2756. s.earlyLevel = m.early * (1.5 - earlyLate);
  2757. s.earlySec = 0.04 * (0.5 + size);
  2758. // round 183b: the reference's level barely moves with Early/Late (+1.6 / −1.3 dB at 0 / 100 %)
  2759. s.lateLevel = 1.1 - 0.2 * earlyLate;
  2760. s.inDiffusion = 0.4 + 0.6 * p[15];
  2761. s.loopDiffusion = 0.1 + 0.8 * p[15];
  2762. // round 183b: the reference's Frozen and Nonlin modes take their length from Size, not Decay (its manual;
  2763. // measured at Size 0 / 50 / 100 %: Nonlin gates after ~0.1 / 0.33 / 1.05 s; Frozen holds with an RT of
  2764. // ~2 / 15 s at Size 0 / 50 %, "tens of seconds" at 100 %)
  2765. s.gateSec = mode == 7 ? 0.1 * std::pow (10.5, size) : 0.0;
  2766. if (mode == 6)
  2767. s.rt60 = 2.0 * std::pow (7.5, 2.0 * size);
  2768. s.modRateHz = skewLaw (p[17], 0.01, 10.0, 2.328);
  2769. s.modDepthMs = 1.0 * p[18] / std::sqrt (std::max (0.1, s.modRateHz));
  2770. // round 183b: Low EQ is the echo section's −6 dB/oct high-pass (Echo → Reverb: the reverb hears the echo,
  2771. // so it is cut once; Reverb → Echo: on the reverb's output here). High EQ is a −6 dB/oct low-pass in both the
  2772. // echo and the reverb and sets the reverb's high decay: measured RT(8 kHz) / RT(1 kHz) 0.47 / 0.65 / 0.90 /
  2773. // 0.99 at High EQ 1 / 4.5 / 11.9 / 20 kHz (this tank's own losses give ~0.65 with no damping)
  2774. const auto lowEq = skewLaw (p[19], 20.0, 1000.0, 1.807), highEq = skewLaw (p[20], 1000.0, 20000.0, 1.078);
  2775. s.outLowCut = choice (p[10], 2) == 1 ? lowEq : 0.0;
  2776. s.outHighCut = highEq;
  2777. s.outLowOrder = 1;
  2778. s.outHighOrder = 1;
  2779. if (highEq < 4500.0)
  2780. {
  2781. s.highXover = highEq;
  2782. s.highMult = 0.5 + 0.45 * std::log2 (highEq / 1000.0) / std::log2 (4.5);
  2783. }
  2784. else
  2785. {
  2786. s.highXover = 3000.0;
  2787. s.highMult = 1.0 + 0.9 * std::log2 (highEq / 4500.0) / std::log2 (20000.0 / 4500.0);
  2788. }
  2789. s.lowMult = m.lowMult;
  2790. s.lowXover = 500.0;
  2791. // round 183b: the reference's tail is fuller below ~1.4 kHz than this tank's (impulse at −20 dB, music)
  2792. s.lowShelfHz = 1400.0;
  2793. s.lowShelfDb = 3.0 + m.lowDb;
  2794. s.highShelfHz = 4000.0;
  2795. s.highShelfDb = m.highDb;
  2796. const auto colour = choice (p[23], 4);
  2797. if (colour == 2) // Dark
  2798. {
  2799. s.bandLimit = 8000.0;
  2800. s.bandLimitSections = 8;
  2801. s.toneHighHz = 3000.0;
  2802. s.toneHighDb = -1.5;
  2803. }
  2804. else if (colour == 3) // the reference's Studio
  2805. {
  2806. s.outLowCut = std::max (s.outLowCut, 560.0);
  2807. s.outLowOrder = 2;
  2808. s.bandLimit = 12000.0;
  2809. s.bandLimitSections = 3;
  2810. }
  2811. s.width = -1.0 + 2.0 * p[1];
  2812. // round 172d: the reference holds its wet energy nearly constant over Decay, so the level follows 1 / √RT
  2813. s.gain = m.gain * p[16] * 0.65 * 1.25 / std::sqrt (std::max (0.2, s.rt60)); // (× 1.25: the compressor's make-up)
  2814. // round 183b: this tank's short lines (small Size) and lower Density run louder than the reference's
  2815. // (measured: Size 0 at −0.9 dB against Size 50 %; Density 0 / 50 % at +2.3 / +1.9 dB against 100 %)
  2816. s.gain *= std::min (1.0, 0.56 + 0.88 * size) * std::pow (10.0, 2.2 * std::min (1.0, 2.0 * (1.0 - (double) p[15])) / 20.0);
  2817. if (p[horizon::engineLaw] >= 0.5f)
  2818. applyLaw (s, horizonLaw[mode]);
  2819. }
  2820. // round 183b: the echo section as the reference's manual has it. Echo → Reverb: the echo's output (not the dry
  2821. // input) is the reverb's input, and Level sets only how much of the echo is heard beside the reverb, so at Level 0
  2822. // and Feedback 0 the echo is the reverb's pre-delay (Echo time, 30 ms by default; the reference's onset follows
  2823. // it). Reverb → Echo: the reverb's output feeds the echo and Level sets the echo heard on top of it. The echo's
  2824. // Low EQ is a −6 dB/oct high-pass on what enters its line (input and feedback); Drive is used by the plain modes
  2825. // (Modern, Tape, Digital, Analog, Lo-fi) and Detune by the granular ones (Detune … Swarm), as the manual says
  2826. void echoStage (float* l, float* r, int n, const EffectParams& p, double bpm, bool beforeReverb) noexcept
  2827. {
  2828. const auto level = (float) p[9];
  2829. if (! beforeReverb && level <= 0.0f)
  2830. return;
  2831. const auto delay = std::max (1.0, syncedMs (choice (p[2], 4), std::min (5, choice (p[3], 6)), horizonEchoMs (p[4]), bpm)) * rate / 1000.0;
  2832. const auto fb = (float) (0.98 * p[5]);
  2833. const auto spread = (double) p[6];
  2834. const auto mode = choice (p[21], horizon::echoModeCount);
  2835. const auto granular = mode >= 4 && mode <= 10;
  2836. const auto drive = granular ? 1.0 : std::pow (10.0, 24.0 * p[7] / 20.0);
  2837. const auto detune = granular ? (-30.0 + 60.0 * p[8]) / 100.0 : 0.0; // semitones
  2838. double ratio = 1.0, ratio2 = 1.0;
  2839. bool shift = false, dual = false;
  2840. double cut = 18000.0, wowDepth = 0.0;
  2841. switch (mode)
  2842. {
  2843. case 1: cut = 7000.0; wowDepth = 0.0015; break; // tape
  2844. case 3: cut = 5000.0; break; // analog
  2845. case 4: shift = true; ratio = std::pow (2.0, (detune + 0.12) / 12.0); ratio2 = std::pow (2.0, -(detune + 0.12) / 12.0); dual = true; break;
  2846. case 5: shift = true; ratio = -1.0; break; // reverse
  2847. case 6: shift = true; ratio = -2.0; break;
  2848. case 7: shift = true; ratio = -0.5; break;
  2849. case 8: shift = true; ratio = -2.0; ratio2 = -0.5; dual = true; break;
  2850. case 9: shift = true; ratio = 2.0; break; // sparkle: an octave up in the repeats
  2851. case 10: shift = true; ratio = std::pow (2.0, 0.15 / 12.0); ratio2 = std::pow (2.0, -0.15 / 12.0); dual = true; wowDepth = 0.003; break;
  2852. case 11: cut = 4000.0; break; // lo-fi
  2853. default: break;
  2854. }
  2855. if (! shift && std::abs (detune) > 1e-3)
  2856. {
  2857. shift = true;
  2858. ratio = std::pow (2.0, detune / 12.0);
  2859. }
  2860. tone[0].setLowpass (cut, rate);
  2861. tone[1].a = tone[0].a;
  2862. const auto lowCut = skewLaw (p[19], 20.0, 1000.0, 1.807);
  2863. lowCutState[0].setLowpass (lowCut, rate);
  2864. lowCutState[1].a = lowCutState[0].a;
  2865. const auto spreadL = delay * (1.0 - 0.25 * spread), spreadR = delay * (1.0 + 0.25 * spread);
  2866. const auto wowInc = 2.0 * pi * 0.7 / rate;
  2867. const auto hold = mode == 11 ? std::max (1.0, rate / 11025.0) : 1.0;
  2868. for (int i = 0; i < n; ++i)
  2869. {
  2870. wow += wowInc;
  2871. if (wow > 2.0 * pi)
  2872. wow -= 2.0 * pi;
  2873. const auto w = 1.0 + wowDepth * std::sin (wow);
  2874. float y[2] = { echo[0].readFrac (std::max (2.0, spreadL * w)), echo[1].readFrac (std::max (2.0, spreadR * w)) };
  2875. float x[2] = { l[i], r[i] };
  2876. for (int c = 0; c < 2; ++c)
  2877. {
  2878. auto v = y[c];
  2879. if (shift)
  2880. {
  2881. auto a = shifters[c].process (v, ratio);
  2882. if (dual)
  2883. a = 0.5f * (a + shifters[c + 2].process (v, ratio2));
  2884. v = a;
  2885. }
  2886. v = tone[c].lp (v);
  2887. if (drive > 1.0001)
  2888. v = (float) (std::tanh (drive * v) / drive);
  2889. if (mode == 11)
  2890. {
  2891. if (c == 0)
  2892. crushPhase += 1.0;
  2893. if (crushPhase >= hold)
  2894. crush[c] = std::round (v * 64.0f) / 64.0f;
  2895. v = crush[c];
  2896. }
  2897. y[c] = v;
  2898. }
  2899. if (crushPhase >= hold)
  2900. crushPhase -= hold;
  2901. for (int c = 0; c < 2; ++c)
  2902. {
  2903. const auto in = x[c] + fb * y[c];
  2904. echo[c].write (in - lowCutState[c].lp (in));
  2905. }
  2906. if (beforeReverb)
  2907. {
  2908. echoOut[0][(std::size_t) i] = level * y[0];
  2909. echoOut[1][(std::size_t) i] = level * y[1];
  2910. l[i] = y[0];
  2911. r[i] = y[1];
  2912. }
  2913. else
  2914. {
  2915. l[i] = x[0] + level * y[0];
  2916. r[i] = x[1] + level * y[1];
  2917. }
  2918. }
  2919. }
  2920. void preStage (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  2921. {
  2922. // round 183b: the reference's wet runs 1 … 2 dB lower on loud programme than its response at −20 dB predicts
  2923. // (music, drums, noise and a decaying burst; ~1 dB with its clean echo mode): a gentle compressor on what
  2924. // enters the echo and reverb (mean-square follower, 30 ms; −30 dBFS, 1.25 : 1; gain updated every 32 samples)
  2925. const auto follow = (float) (1.0 - std::exp (-1.0 / (0.03 * rate)));
  2926. for (int i = 0; i < n; ++i)
  2927. {
  2928. compEnv += follow * (0.5f * (l[i] * l[i] + r[i] * r[i]) - compEnv);
  2929. if ((compTick++ & 31) == 0)
  2930. {
  2931. const auto level = 10.0f * std::log10 (compEnv + 1e-12f);
  2932. compGain = level > -30.0f ? std::pow (10.0f, -(level + 30.0f) * 0.2f / 20.0f) : 1.0f;
  2933. }
  2934. l[i] *= compGain;
  2935. r[i] *= compGain;
  2936. }
  2937. if (choice (p[10], 2) == 0)
  2938. echoStage (l, r, n, p, bpm, true);
  2939. }
  2940. void postStage (float* l, float* r, int n, const EffectParams& p, double bpm) noexcept override
  2941. {
  2942. if (choice (p[10], 2) == 1)
  2943. echoStage (l, r, n, p, bpm, false);
  2944. else
  2945. for (int i = 0; i < n; ++i)
  2946. {
  2947. l[i] += echoOut[0][(std::size_t) i];
  2948. r[i] += echoOut[1][(std::size_t) i];
  2949. }
  2950. }
  2951. Line echo[2];
  2952. Shifter shifters[4];
  2953. OnePole tone[2], lowCutState[2];
  2954. float compEnv = 0.0f, compGain = 1.0f;
  2955. unsigned compTick = 0;
  2956. std::array<std::array<float, chunk>, 2> echoOut {};
  2957. double wow = 0.0, crushPhase = 0.0;
  2958. float crush[2] {};
  2959. };
  2960. // ---------------------------------------------------------------------------------------------- tables for the UI
  2961. std::vector<EffectParamInfo> params (std::initializer_list<EffectParamInfo> list) { return list; }
  2962. } // namespace
  2963. const char* epoch::modeName (int i) noexcept { return epochModes[std::clamp (i, 0, epoch::modeCount - 1)].name; }
  2964. const char* epoch::colourName (int i) noexcept { return epochColours[std::clamp (i, 0, epoch::colourCount - 1)]; }
  2965. double epoch::predelayMs (float v) noexcept { return epochPredelay (v); }
  2966. double epoch::decaySec (float v) noexcept { return epochDecay (v); }
  2967. const char* atrium::typeName (int i) noexcept { return atriumTypes[std::clamp (i, 0, atrium::typeCount - 1)].name; }
  2968. double atrium::decaySec (float v) noexcept { return atriumDecaySec (v); }
  2969. float atrium::decayValue (double s) noexcept { return (float) skewInverse (s, 0.1, 100.0, 5.717); }
  2970. double foil::lowCutHz (float v) noexcept { return v <= 0.0f ? 0.0 : 20.0 * std::pow (50.0, (double) std::min (v, 1.0f)); }
  2971. const char* foil::typeName (int i) noexcept { return foilTypes[std::clamp (i, 0, foil::typeCount - 1)].name; }
  2972. double foil::decaySec (float v) noexcept { return foilDecaySec (v); }
  2973. const char* nebula::modeName (int i) noexcept { return nebulaModes[std::clamp (i, 0, nebula::modeCount - 1)].name; }
  2974. double nebula::delayMs (float v) noexcept { return nebulaDelayMs (v); }
  2975. bool nebula::cloudMode (int mode) noexcept { return nebulaTopology (mode) != cloud::Topology::none; }
  2976. const char* horizon::echoModeName (int i) noexcept { return horizonEchoModes[std::clamp (i, 0, horizon::echoModeCount - 1)]; }
  2977. const char* horizon::reverbModeName (int i) noexcept { return horizonReverbs[std::clamp (i, 0, horizon::reverbModeCount - 1)].name; }
  2978. double horizon::decaySec (float v) noexcept { return horizonDecaySec (v); }
  2979. double horizon::echoMs (float v) noexcept { return horizonEchoMs (v); }
  2980. bool isLynxVerb (EffectKind k) noexcept
  2981. {
  2982. return k == EffectKind::epochVerb || k == EffectKind::atrium || k == EffectKind::foilPlate || k == EffectKind::nebula
  2983. || k == EffectKind::halo || k == EffectKind::horizon;
  2984. }
  2985. const EffectInfo& info (EffectKind kind)
  2986. {
  2987. // defaults = the reference plugins' defaults (their saved normalised values)
  2988. static const EffectInfo epochInfo { "Epoch Verb",
  2989. params ({ { "Mix", 1.0f }, { "Predelay", 0.25f }, { "Decay", 0.42048f }, { "Size", 1.0f }, { "Attack", 0.5f },
  2990. { "Bass mult", 0.623039f }, { "Bass xover", 0.381065f }, { "High shelf", 0.0f }, { "High freq", 0.5f },
  2991. { "Early diffusion", 1.0f }, { "Late diffusion", 1.0f }, { "Mod rate", 0.245455f }, { "Mod depth", 0.38f },
  2992. { "High cut", 0.590539f }, { "Low cut", 0.0f }, { "Color", 0.0f }, { "Mode", 0.0f },
  2993. { "Law", 1.0f } }) }; // round 190bz: the 0.1.27 engine law (a project saved before keeps the classic engine: upgradeLoadedEffectParams)
  2994. static const EffectInfo atriumInfo { "Atrium",
  2995. params ({ { "Mix", 1.0f }, { "Predelay", 0.02f }, { "Decay", 0.5f }, { "High cut", 0.530201f }, { "Early/late", 0.5f },
  2996. { "Late size", 0.5f }, { "Late cross", 1.0f }, { "Late mod rate", 0.090909f }, { "Late mod depth", 0.5f },
  2997. { "Bass mult", 0.333333f }, { "Bass xover", 0.090909f }, { "High mult", 0.444444f }, { "High xover", 0.530201f },
  2998. { "Early size", 0.029029f }, { "Early cross", 0.1f }, { "Early mod rate", 0.090909f }, { "Early mod depth", 0.0f },
  2999. { "Early send", 0.0f }, { "Diffusion", 1.0f }, { "Type", 0.0f }, { "Space", 0.0f }, { "Low cut", 0.0f },
  3000. { "Law", 1.0f } }) }; // round 190bz
  3001. static const EffectInfo foilInfo { "Foil Plate",
  3002. params ({ { "Mix", 1.0f }, { "Predelay", 0.0f }, { "Decay", 0.5f }, { "Size", 0.5f }, { "Width", 0.5f },
  3003. { "Mod rate", 0.365345f }, { "Mod depth", 0.0f }, { "Low EQ freq", 0.343434f }, { "Low EQ gain", 0.5f },
  3004. { "High EQ freq", 0.494949f }, { "High EQ gain", 0.5f }, { "Type", 0.0f }, // EQ at 700 Hz / 10 kHz (round 129)
  3005. { "Infinite", 0.0f }, { "Low cut", 0.0f }, // round 135 (the Little Plate role)
  3006. { "Voicing", 0.0f }, // round 150: the measured plate voicing (off = the plate types as before)
  3007. { "View", 0.0f }, // round 152: the editor's view (Full | Plate: the one-knob panel of the Little Plate role)
  3008. { "Law", 1.0f } }) }; // round 190bz
  3009. static const EffectInfo nebulaInfo { "Nebula",
  3010. params ({ { "Mix", 0.5f }, { "Sync", 0.0f }, { "Note", 0.333333f }, { "Delay", 0.5f }, { "Warp", 0.0f },
  3011. { "Feedback", 0.5f }, { "Density", 0.0f }, { "Width", 1.0f }, { "Low cut", 0.0f }, { "High cut", 1.0f },
  3012. { "Mod rate", 0.273834f }, { "Mod depth", 0.5f }, { "Mode", 0.0f },
  3013. { "Clear", 0.0f }, // round 129: the reference's CLEAR button (any change of the value empties the network)
  3014. // round 190ca: the cloud law (core/NebulaCloud.h) for new instances; projects saved before keep the
  3015. // classic engine: upgradeLoadedEffectParams
  3016. { "Law", 1.0f } }) };
  3017. static const EffectInfo haloInfo { "Halo",
  3018. params ({ { "Mix", 0.5f }, { "Shift", 1.0f }, { "Feedback", 0.0f }, { "Diffusion", 0.919192f }, { "Size", 0.5f },
  3019. { "Low cut", 0.0f }, { "High cut", 0.530201f }, { "Mod rate", 0.1f }, { "Mod depth", 0.3f },
  3020. { "Reverb mode", 0.0f }, { "Shift mode", 0.0f }, { "Color", 0.0f },
  3021. { "Law", 1.0f } }) }; // round 190bz
  3022. static const EffectInfo horizonInfo { "Horizon",
  3023. params ({ { "Mix", 0.5f }, { "Width", 1.0f }, { "Echo sync", 0.0f }, { "Echo note", 0.4f }, { "Echo time", 0.15f },
  3024. { "Echo feedback", 0.0f }, { "Echo spread", 0.0f }, { "Echo drive", 0.125f }, { "Echo detune", 0.566667f },
  3025. { "Echo level", 0.0f }, { "Routing", 0.0f }, { "Decay", 0.291667f }, { "Size", 0.5f }, { "Attack", 0.5f },
  3026. { "Early/late", 0.5f }, { "Density", 1.0f }, { "Reverb level", 1.0f }, { "Mod rate", 0.132226f },
  3027. { "Mod depth", 0.4f }, { "Low EQ freq", 0.25f }, { "High EQ freq", 0.208197f }, { "Echo mode", 0.090909f },
  3028. { "Reverb mode", 0.142857f }, { "Color", 0.333333f },
  3029. { "Law", 1.0f } }) }; // round 190bz
  3030. switch (kind)
  3031. {
  3032. case EffectKind::atrium: return atriumInfo;
  3033. case EffectKind::foilPlate: return foilInfo;
  3034. case EffectKind::nebula: return nebulaInfo;
  3035. case EffectKind::halo: return haloInfo;
  3036. case EffectKind::horizon: return horizonInfo;
  3037. default: return epochInfo;
  3038. }
  3039. }
  3040. std::string paramText (EffectKind kind, int param, float v, double bpm)
  3041. {
  3042. (void) bpm;
  3043. switch (kind)
  3044. {
  3045. case EffectKind::epochVerb:
  3046. switch (param)
  3047. {
  3048. case 1: return ms (epochPredelay (v));
  3049. case 2: return sec (epochDecay (v));
  3050. case 5: return fmt ("%.2f X", epochBassMult (v));
  3051. case 6:
  3052. case 8:
  3053. case 13: return hz (param == 6 ? epochBassXover (v) : epochFreq (v));
  3054. case 7: return fmt ("%.1f dB", -24.0 + 24.0 * v);
  3055. case 11: return fmt ("%.2f Hz", 0.1 + 9.9 * v);
  3056. case 14: return hz (10.0 + 1490.0 * v);
  3057. case 15: return epoch::colourName (choice (v, epoch::colourCount));
  3058. case 16: return epoch::modeName (choice (v, epoch::modeCount));
  3059. case epoch::engineLaw: return v >= 0.5f ? "Measured" : "Classic";
  3060. default: return pct (v);
  3061. }
  3062. case EffectKind::atrium:
  3063. switch (param)
  3064. {
  3065. case 1: return ms (500.0 * v);
  3066. case 2: return sec (atriumDecaySec (v));
  3067. case 3: return hz (100.0 + 14900.0 * v);
  3068. case 5: case 6: case 8: case 14: case 16: case 17: case 18: return fmt ("%.2f", v);
  3069. case 7: case 15: return fmt ("%.2f Hz", 0.05 + 4.95 * v);
  3070. case 9: return fmt ("%.2f X", 0.5 + 1.5 * v);
  3071. case 10: return hz (100.0 + 9900.0 * v);
  3072. case 11: return fmt ("%.2f X", 0.1 + 0.9 * v);
  3073. case 12: return hz (100.0 + 14900.0 * v);
  3074. case 13: return ms (1.0 + 999.0 * v);
  3075. case 19: return atrium::typeName (choice (v, atrium::typeCount));
  3076. case 21: return hz (1000.0 * v);
  3077. case atrium::engineLaw: return v >= 0.5f ? "Measured" : "Classic";
  3078. default: return pct (v);
  3079. }
  3080. case EffectKind::foilPlate:
  3081. switch (param)
  3082. {
  3083. case 1: return ms (epochPredelay (v));
  3084. case 2: return sec (foilDecaySec (v));
  3085. case 3: case 4: return pct (2.0 * v);
  3086. case 5: return fmt ("%.2f Hz", skewLaw (v, 0.05, 5.0, 2.381));
  3087. case 7: return hz (20.0 + 1980.0 * v);
  3088. case 9: return hz (200.0 + 19800.0 * v);
  3089. case 8: case 10: return fmt ("%+.1f dB", -12.0 + 24.0 * v);
  3090. case 11: return foil::typeName (choice (v, foil::typeCount));
  3091. case 12: return v >= 0.5f ? "on" : "off";
  3092. case 13: return v <= 0.0f ? "off" : hz (foil::lowCutHz (v));
  3093. case 14: return v >= 0.5f ? "measured" : "off";
  3094. case 15: return v >= 0.5f ? "Plate" : "Full"; // round 152: the editor's view
  3095. case foil::engineLaw: return v >= 0.5f ? "Measured" : "Classic";
  3096. default: return pct (v);
  3097. }
  3098. case EffectKind::nebula:
  3099. switch (param)
  3100. {
  3101. case 13: return "Clear";
  3102. case 14: return v >= 0.5f ? "measured" : "classic";
  3103. case 1: return syncNames[choice (v, 4)];
  3104. case 2: return noteNames[choice (v, 7)];
  3105. case 3: return ms (nebulaDelayMs (v));
  3106. case 7: return pct (-1.0 + 2.0 * v);
  3107. case 8: return hz (10.0 + 1990.0 * v);
  3108. case 9: return hz (200.0 + 19800.0 * v);
  3109. case 10: return fmt ("%.2f Hz", skewLaw (v, 0.01, 10.0, 2.328));
  3110. case 12: return nebula::modeName (choice (v, nebula::modeCount));
  3111. default: return pct (v);
  3112. }
  3113. case EffectKind::halo:
  3114. switch (param)
  3115. {
  3116. case 1: return fmt ("%+.1f st", haloShiftSemis (v));
  3117. // round 129: what the engine applies (0 … 250 Hz, off below 5 Hz; configure()) — the display said
  3118. // 10 … 1000 Hz, the reference editor's range, while the filter never went above 250 Hz
  3119. case 5: return 250.0 * v < 5.0 ? std::string ("off") : hz (250.0 * v);
  3120. case 6: return hz (100.0 + 14900.0 * v);
  3121. case 7: return fmt ("%.2f Hz", 0.05 + 4.95 * v);
  3122. case 9: return haloModes[haloModeIndex (v)].name;
  3123. case 10: return haloShiftModes[choice (v, 5)];
  3124. case 11: return haloColours[choice (v, 2)];
  3125. case halo::engineLaw: return v >= 0.5f ? "Measured" : "Classic";
  3126. default: return pct (v);
  3127. }
  3128. case EffectKind::horizon:
  3129. switch (param)
  3130. {
  3131. case 1: return pct (-1.0 + 2.0 * v);
  3132. case 2: return syncNames[choice (v, 4)];
  3133. case 3: return noteNames[std::min (5, choice (v, 6))];
  3134. case 4: return ms (horizonEchoMs (v));
  3135. case 7: return fmt ("%.1f dB", 24.0 * v);
  3136. case 8: return fmt ("%+.1f ct", -30.0 + 60.0 * v);
  3137. case 10: return choice (v, 2) == 0 ? "Echo > Reverb" : "Reverb > Echo";
  3138. case 11: return sec (horizonDecaySec (v));
  3139. case 17: return fmt ("%.2f Hz", skewLaw (v, 0.01, 10.0, 2.328));
  3140. case 19: return hz (skewLaw (v, 20.0, 1000.0, 1.807));
  3141. case 20: return hz (skewLaw (v, 1000.0, 20000.0, 1.078));
  3142. case 21: return horizon::echoModeName (choice (v, horizon::echoModeCount));
  3143. case 22: return horizon::reverbModeName (choice (v, horizon::reverbModeCount));
  3144. case 23: return horizonColours[choice (v, 4)];
  3145. case horizon::engineLaw: return v >= 0.5f ? "Measured" : "Classic";
  3146. default: return pct (v);
  3147. }
  3148. default: return pct (v);
  3149. }
  3150. }
  3151. std::unique_ptr<Effect> make (EffectKind kind)
  3152. {
  3153. switch (kind)
  3154. {
  3155. case EffectKind::atrium: return std::make_unique<AtriumFx>();
  3156. case EffectKind::foilPlate: return std::make_unique<FoilFx>();
  3157. case EffectKind::nebula: return std::make_unique<NebulaFx>();
  3158. case EffectKind::halo: return std::make_unique<HaloFx>();
  3159. case EffectKind::horizon: return std::make_unique<HorizonFx>();
  3160. default: return std::make_unique<EpochFx>();
  3161. }
  3162. }
  3163. } // namespace lynx::verbs