AlsDevicesLot3.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. // Live's own devices → LYNX's, lot 3 (round 190ed; core/AlsDevices.h has the list and the evidence tags). The same
  2. // rules as lots 1 and 2: each conversion is a law in a unit, what is fitted or has no counterpart becomes a note for the
  3. // import report, only the device's saved state is read, and each translator records which Live parameter drives
  4. // which LYNX parameter (ParamLink) so automation and rack macros follow. Round 190ek refit the laws to Live 12.4's renders
  5. // (round 190ei): [MEASURED] = read off those renders, [FITTED] = LYNX's stages fitted to them, [ASSUMED] = still a guess.
  6. #include "core/AlsDevices.h"
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <complex>
  10. #include <cstdio>
  11. #include "core/AnvilBus.h"
  12. #include "core/DynamicsSuite.h"
  13. #include "core/Effects.h"
  14. #include "core/FacetEq.h"
  15. #include "core/MixSuite.h"
  16. #include "core/Rack.h"
  17. #include "core/XmlLite.h"
  18. namespace lynx::alsdev
  19. {
  20. namespace
  21. {
  22. using xml::Element;
  23. float unit (double v) noexcept { return (float) std::clamp (v, 0.0, 1.0); }
  24. float linValue (double v, double lo, double hi) noexcept { return unit ((v - lo) / (hi - lo)); }
  25. float logValue (double v, double lo, double hi) noexcept { return unit (std::log (std::clamp (v, lo, hi) / lo) / std::log (hi / lo)); }
  26. std::string text (const char* format, double a, double b = 0.0, double c = 0.0)
  27. {
  28. char out[240];
  29. std::snprintf (out, sizeof (out), format, a, b, c);
  30. return out;
  31. }
  32. double manual (const Element& device, const std::string& name, double fallback) { return device.number (name + "/Manual", fallback); }
  33. bool manualBool (const Element& device, const std::string& name, bool fallback) { return device.boolean (name + "/Manual", fallback); }
  34. bool has (const Element& device, const std::string& name) { return (bool) device.path (name + "/Manual"); }
  35. // Live's floor of a linear gain control (−70 dB) is shown as −inf [DATA: 0.0003162277571 at the bottom of the ranges]
  36. bool isFloor (double gain) noexcept { return gain <= 0.000317; }
  37. } // namespace
  38. // ================================================================================================ EQ Three
  39. bool eqThreeToPrism (const Element& device, Translated& out)
  40. {
  41. using namespace dyn::prism;
  42. if (! has (device, "GainLo") || ! has (device, "FreqLo") || ! has (device, "FreqHi"))
  43. return false;
  44. out.slot = makeEffectSlot (EffectKind::prismMultiband);
  45. auto& p = out.slot.params;
  46. p[bandCountParam] = bandCountValue (3);
  47. // the crossover sum (Live's EQ Three splits the signal into three bands and sums them [DOC]): Prism's minimum-phase
  48. // mode, the all-pass compensated crossover tree; 24 / 48 dB/oct [DOC; DATA Slope 0 … 1, ASSUMED 0 = 24]
  49. p[processingMode] = 1.0f;
  50. const auto slopeDb = manual (device, "Slope", 1.0) > 0.5 ? 48 : 24;
  51. out.links.push_back ({ "FreqLo", crossoverParam (0), [] (double hz) { return crossoverValue (hz); } });
  52. out.links.push_back ({ "FreqHi", crossoverParam (1), [] (double hz) { return crossoverValue (hz); } });
  53. auto lo = std::clamp (manual (device, "FreqLo", 250.0), 30.0, 18000.0), hi = std::clamp (manual (device, "FreqHi", 2500.0), 30.0, 18000.0);
  54. if (hi < lo)
  55. std::swap (lo, hi);
  56. p[(std::size_t) crossoverParam (0)] = crossoverValue (lo);
  57. p[(std::size_t) crossoverParam (1)] = crossoverValue (hi);
  58. p[slope] = (float) (slopeDb == 48 ? 1.0 : 0.5);
  59. for (int k = 0; k < 2; ++k)
  60. p[(std::size_t) (xoverSlope0 + k)] = crossoverSlopeValue (slopeDb);
  61. // no dynamics: each band only a level (its range 0 dB, ratio 1 : 1)
  62. static const char* const gains[3] { "GainLo", "GainMid", "GainHi" };
  63. static const char* const ons[3] { "LowOn", "MidOn", "HighOn" };
  64. std::array<bool, 3> sounds {};
  65. bool clamped = false;
  66. for (int b = 0; b < 3; ++b)
  67. {
  68. const auto gain = manual (device, gains[b], 1.0);
  69. sounds[(std::size_t) b] = manualBool (device, ons[b], true) && ! isFloor (gain);
  70. p[(std::size_t) param (b, fRange)] = 0.5f;
  71. p[(std::size_t) param (b, fRatio)] = 0.0f;
  72. p[(std::size_t) param (b, fEnabled)] = 1.0f;
  73. // a linear gain [DATA: 0.000316 … 1.995 = −70 … +6 dB] → Prism's band level (−30 … +30 dB)
  74. const auto db = dbOfGain (gain);
  75. p[(std::size_t) param (b, fLevel)] = linValue (db, -30.0, 30.0);
  76. clamped = clamped || (db < -30.0 && ! isFloor (gain));
  77. out.links.push_back ({ gains[b], param (b, fLevel), [] (double g) { return linValue (dbOfGain (g), -30.0, 30.0); } });
  78. }
  79. const auto alive = (int) std::count (sounds.begin(), sounds.end(), true);
  80. if (alive < 3)
  81. for (int b = 0; b < 3; ++b)
  82. p[(std::size_t) param (b, fSolo)] = sounds[(std::size_t) b] ? 1.0f : 0.0f;
  83. if (alive == 0)
  84. {
  85. p[output] = 0.0f; // Prism's lowest output (−36 dB): no setting of Prism is silent
  86. out.notes.approximated.push_back ("all three bands off: Prism's output at -36 dB (Live: silence)");
  87. }
  88. if (clamped)
  89. out.notes.approximated.push_back ("a band gain under -30 dB (Prism's lowest band level)");
  90. p[mix] = 1.0f;
  91. out.notes.approximated.push_back (text ("the bands as Prism's minimum-phase crossover sum at %.0f dB/oct (Live's filters: law assumed)", slopeDb));
  92. return true;
  93. }
  94. // ================================================================================================ Drum Buss
  95. // The laws below were fitted to Live 12.4's renders of round 190ei (one Drum Buss per track: noise, a 1 kHz sine
  96. // staircase, an impulse and a drum loop through it; LYNX's rack run on the same inputs, round 190ek's error table)
  97. namespace
  98. {
  99. double interpolate (const double* xs, const double* ys, int n, double x) noexcept
  100. {
  101. if (x <= xs[0])
  102. return ys[0];
  103. for (int i = 1; i < n; ++i)
  104. if (x <= xs[i])
  105. return ys[i - 1] + (ys[i] - ys[i - 1]) * (x - xs[i - 1]) / (xs[i] - xs[i - 1]);
  106. return ys[n - 1];
  107. }
  108. constexpr double driveAmounts[4] { 0.0, 0.25, 0.5, 1.0 };
  109. } // namespace
  110. double drumBussDriveGainDb (int type, double drive) noexcept
  111. {
  112. // the small-signal gain over the neutral device [MEASURED, 1 kHz at −60 dBFS: +1.97 dB at drive 0 for every type;
  113. // type 0: +3.92 / +8.91 / +11.52, type 1: +5.73 / +9.48 / +16.98, type 2: +5.94 / +7.75 / +14.23 dB at 25 / 50 / 100 %]
  114. static constexpr double gains[3][4] { { 0.0, 1.95, 6.94, 9.55 }, { 0.0, 3.76, 7.51, 15.01 }, { 0.0, 3.97, 5.78, 12.26 } };
  115. return interpolate (driveAmounts, gains[std::clamp (type, 0, 2)], 4, std::clamp (drive, 0.0, 1.0));
  116. }
  117. DrumBussDriveStage drumBussDriveStage (int type, double drive) noexcept
  118. {
  119. // Ember's voicing, drive and output per type and amount (core/AlsDevices.h drumbuss)
  120. DrumBussDriveStage st;
  121. const auto t = std::clamp (type, 0, 2);
  122. const auto d = std::clamp (drive, 0.0, 1.0);
  123. st.driveDb = interpolate (driveAmounts, drumbuss::emberDriveDb[t], 4, d);
  124. st.eqDb = drumbuss::typeEqDb[t] * d;
  125. // the analog prototype's gain at 1 kHz (a bell for Medium, a cookbook low shelf for Hard), so the measured 1 kHz levels hold
  126. const auto s = std::complex<double> (0.0, 1000.0 / drumbuss::typeEqHz[t]);
  127. const auto q = drumbuss::typeEqQ[t];
  128. const auto A = std::pow (10.0, st.eqDb / 40.0);
  129. const auto h = t == 2 ? A * (s * s + std::sqrt (A) / q * s + A) / (A * s * s + std::sqrt (A) / q * s + 1.0) : (s * s + s * A / q + 1.0) / (s * s + s / (A * q) + 1.0);
  130. st.outputDb = drumbuss::emberOutputDb - (t == 0 ? 0.0 : 20.0 * std::log10 (std::abs (h)));
  131. return st;
  132. }
  133. BoomBands drumBussBoom (double amount, double hz, double decay) noexcept
  134. {
  135. // [FITTED on the four renders at amount 0.5, rms 0.24 … 0.78 dB below 600 Hz: a bell at the Boom frequency and a
  136. // resonant 12 dB/oct high pass] decay 0.2: bell +13.75 dB, Q 3.32, high pass Q 0.581 at 1.470 × f; decay 1.0: bell
  137. // +18.65 dB, Q 2.18, high pass Q 0.476 at 0.816 × f; linear in the decay between (and beyond, held to sane ends).
  138. // The amount [ASSUMED]: a resonance added in proportion to it (the bell's linear gain − 1 ∝ the amount: 0.5 = the
  139. // fit, 1.0 = +24.1 dB at decay 1), the high pass sliding down from 0 to its place as the amount rises to 0.5
  140. BoomBands b;
  141. const auto a = std::clamp (amount, 0.0, 1.0);
  142. const auto x = (std::clamp (decay, 0.0, 1.0) - 0.2) / 0.8;
  143. b.hz = std::clamp (hz, 30.0, 90.0);
  144. const auto atHalf = std::pow (10.0, (13.75 + (18.65 - 13.75) * x) / 20.0) - 1.0;
  145. b.bellDb = std::clamp (20.0 * std::log10 (1.0 + atHalf * a / 0.5), 0.0, 30.0);
  146. b.bellQ = std::clamp (3.32 + (2.18 - 3.32) * x, 1.0, 6.0);
  147. b.cutQ = std::clamp (0.581 + (0.476 - 0.581) * x, 0.3, 1.5);
  148. b.cutHz = std::max (10.0, b.hz * std::clamp (1.470 + (0.816 - 1.470) * x, 0.5, 2.0) * std::min (1.0, a / 0.5));
  149. return b;
  150. }
  151. double drumBussCrunchDb (double crunch) noexcept
  152. {
  153. // [FITTED on Crunch 0.5: +6.28 dB between a low shelf at 95 Hz and a high shelf at 9041 Hz, rms 0.07 dB; ASSUMED the
  154. // added band in proportion to the amount: 20·log10 (1 + 2.14 · crunch)]
  155. return 20.0 * std::log10 (1.0 + 2.14 * std::clamp (crunch, 0.0, 1.0));
  156. }
  157. namespace
  158. {
  159. // a Facet EQ band from an analog prototype: a bell's textbook Q, a cookbook shelf Q, a 12 dB/oct cut's Q
  160. void facetBand (EffectParams& p, int b, facet::Shape shape, double hz, double db, double q, int slope = 12)
  161. {
  162. p[(std::size_t) facet::param (b, 0)] = facet::shapeValue (shape);
  163. p[(std::size_t) facet::param (b, 1)] = facet::hzValue (hz);
  164. p[(std::size_t) facet::param (b, 2)] = facet::gainValue (facet::hasGain (shape) ? db : 0.0);
  165. // Facet's prototypes (core/AlsDevices.h, EQ Eight): bell and 12 dB cut Q = Qfacet / √2; a shelf's cookbook Q =
  166. // 0.7071 · Qfacet^0.4697
  167. const auto shelf = shape == facet::Shape::lowShelf || shape == facet::Shape::highShelf;
  168. p[(std::size_t) facet::param (b, 3)] = facet::qValue (shelf ? std::pow (q / 0.7071, 1.0 / 0.4697) : std::sqrt (2.0) * q);
  169. p[(std::size_t) facet::param (b, 4)] = facet::slopeValue (slope);
  170. p[(std::size_t) (facet::powerParam0 + b)] = 0.0f;
  171. }
  172. } // namespace
  173. bool drumBussToRack (const Element& device, Translated& out)
  174. {
  175. namespace em = mixsuite::ember;
  176. namespace sh = mixsuite::shell;
  177. namespace cr = dyn::crest;
  178. if (! has (device, "DriveAmount") || ! has (device, "TransientShaping") || ! has (device, "BoomAmount"))
  179. return false;
  180. // 1. Ember "Drive": the input trim, the drive (its own curve per type and the gain the type gives each amount: Soft /
  181. // Medium / Hard [DOC order; DATA DriveType 0 … 2]), Damping as its 6 dB/oct high cut [MEASURED: a first-order low
  182. // pass, −3 dB at 1020 / 6662 Hz for 1 / 6 kHz], no level compensation
  183. auto ember = makeEffectSlot (EffectKind::ember);
  184. auto& e = ember.params;
  185. const auto type = std::clamp ((int) manual (device, "DriveType", 0), 0, 2);
  186. const auto driveAmount = std::clamp (manual (device, "DriveAmount", 0.2), 0.0, 1.0);
  187. const auto stage = drumBussDriveStage (type, driveAmount);
  188. const auto stageAt = [type] (double d) { return drumBussDriveStage (type, d); };
  189. e[em::voice] = (float) stage.voice / (float) em::voices;
  190. e[em::autoGain] = 0.0f;
  191. e[(std::size_t) em::law] = 0.0f; // (rollup 0.1.28, 190fk x 190fj: round 190ek's rack was fitted on the Classic tape)
  192. e[em::drive] = linValue (stage.driveDb, 0.0, 36.0);
  193. e[em::output] = linValue (stage.outputDb, -18.0, 18.0);
  194. const auto trimDb = dbOfGain (manual (device, "InputTrim", 1.0));
  195. e[em::inputGain] = linValue (trimDb, -24.0, 24.0);
  196. const auto damping = std::clamp (manual (device, "DampingFrequency", 20000.0), 500.0, 20000.0);
  197. e[em::wideCuts] = 1.0f;
  198. e[em::highCutWide] = damping >= 19999.0 ? 1.0f : logValue (damping, 1000.0, 20000.0);
  199. e[em::mix] = 1.0f;
  200. // 2. Facet EQ "Colour": the device's own colour, there at every setting [MEASURED at all-zero settings: a "smile",
  201. // +5 dB around 150 Hz and 4 kHz, +1.85 dB between; FITTED as three bells and the output, rms 0.14 dB], Crunch's mid band
  202. // [MEASURED 0.5: +6.3 dB 300 Hz … 4 kHz] as two shelves under the output's lift, Boom's resonance as a bell and a
  203. // resonant high pass
  204. auto colour = makeEffectSlot (EffectKind::facetEq);
  205. auto& c = colour.params;
  206. facetBand (c, 0, facet::Shape::bell, drumbuss::lowBellHz, drumbuss::lowBellDb, drumbuss::lowBellQ);
  207. facetBand (c, 1, facet::Shape::bell, drumbuss::highBellHz, drumbuss::highBellDb, drumbuss::highBellQ);
  208. facetBand (c, 6, facet::Shape::bell, drumbuss::dipBellHz, drumbuss::dipBellDb, drumbuss::dipBellQ);
  209. facetBand (c, 7, type == 2 ? facet::Shape::lowShelf : facet::Shape::bell, drumbuss::typeEqHz[type], stage.eqDb, drumbuss::typeEqQ[type]);
  210. const auto crunch = std::clamp (manual (device, "CrunchAmount", 0.0), 0.0, 1.0);
  211. const auto crunchDb = drumBussCrunchDb (crunch);
  212. facetBand (c, 4, facet::Shape::lowShelf, drumbuss::crunchLowHz, -crunchDb, drumbuss::crunchLowQ);
  213. facetBand (c, 5, facet::Shape::highShelf, drumbuss::crunchHighHz, -crunchDb, drumbuss::crunchHighQ);
  214. const auto boomAmount = std::clamp (manual (device, "BoomAmount", 0.0), 0.0, 1.0);
  215. const auto boomHz = std::clamp (manual (device, "BoomFrequency", 50.0), 30.0, 90.0);
  216. const auto boomDecay = std::clamp (manual (device, "BoomDecay", 0.5), 0.0, 1.0);
  217. const auto boom = drumBussBoom (boomAmount, boomHz, boomDecay);
  218. facetBand (c, 2, facet::Shape::bell, boom.hz, boom.bellDb, boom.bellQ);
  219. facetBand (c, 3, facet::Shape::lowCut, boom.cutHz, 0.0, boom.cutQ);
  220. c[facet::outputParam] = (float) ((std::clamp (drumbuss::colourGainDb + crunchDb, -24.0, 24.0) + 24.0) / 48.0);
  221. // 3. Crest "Comp" (only when on): Live's fixed drum compressor [MEASURED on the staircase: about 4 : 1 above
  222. // −21 dBFS, +11 dB of make-up]
  223. const auto compOn = device.boolean ("EnableCompression/Manual", device.boolean ("EnableCompression", false));
  224. EffectSlot comp;
  225. if (compOn)
  226. {
  227. comp = makeEffectSlot (EffectKind::crestCompressor);
  228. auto& k = comp.params;
  229. k[cr::style] = cr::styleValue (cr::Style::clean);
  230. k[cr::voicing] = 0.0f;
  231. k[cr::threshold] = linValue (drumbuss::compThresholdDb, -60.0, 0.0);
  232. k[cr::ratio] = cr::ratioValue (drumbuss::compRatio);
  233. k[cr::knee] = linValue (drumbuss::compKneeDb, 0.0, 72.0);
  234. k[cr::range] = 1.0f;
  235. k[cr::attack] = cr::attackValue (drumbuss::compAttackMs);
  236. k[cr::release] = cr::releaseValue (drumbuss::compReleaseMs);
  237. k[cr::autoRelease] = 0.0f;
  238. k[cr::lookahead] = 0.0f;
  239. k[cr::maxLookahead] = 0.0f;
  240. k[cr::output] = linValue (drumbuss::compMakeupDb, -36.0, 36.0);
  241. k[cr::autoGain] = 0.0f;
  242. k[cr::mix] = 1.0f;
  243. k[cr::dryGain] = 0.0f;
  244. }
  245. // 4. Shell Channel "Shape": the transient shaper (−1 … +1: Amount = |t|, Invert below 0) and the output gain
  246. auto shell = makeEffectSlot (EffectKind::shellChannel);
  247. auto& s = shell.params;
  248. const auto transients = std::clamp (manual (device, "TransientShaping", 0.0), -1.0, 1.0);
  249. s[sh::tsOn] = std::abs (transients) > 1.0e-4 ? 1.0f : 0.0f;
  250. s[sh::tsAmount] = (float) std::abs (transients);
  251. s[sh::tsInvert] = transients < 0.0 ? 1.0f : 0.0f;
  252. s[sh::lfOn] = 0.0f;
  253. s[sh::hfOn] = 0.0f;
  254. s[sh::lmcOn] = 0.0f;
  255. const auto outDb = dbOfGain (manual (device, "OutputGain", 1.0));
  256. s[sh::output] = linValue (outDb, -20.0, 20.0);
  257. s[sh::outputMix] = 1.0f;
  258. // the rack: "Drum Buss" chain (Ember → Facet → Crest → Shell) and, when the device is not fully wet or a macro moves
  259. // Dry/Wet, a "Dry" chain beside it [MEASURED: out = 0.5 · input + 0.5 · wet at 50 %, the dry branch the raw input]
  260. out.slot = rack::makeRackSlot (RackDoc::Kind::audioEffect);
  261. auto& doc = out.slot.patcher;
  262. if (auto* r = rack::rackOf (doc); r != nullptr && ! r->chains.empty())
  263. r->chains.front().name = "Drum Buss";
  264. const auto add = [&doc] (EffectSlot&& fx, const char* label) {
  265. PatchNode node;
  266. node.effect = std::move (fx);
  267. node.label = label;
  268. return rack::addDevice (doc, 0, std::move (node));
  269. };
  270. const auto emberNode = add (std::move (ember), "Drive");
  271. const auto colourNode = add (std::move (colour), "Colour");
  272. const auto compNode = compOn ? add (std::move (comp), "Comp") : 0;
  273. const auto shellNode = add (std::move (shell), "Shape");
  274. if (emberNode < 0 || colourNode < 0 || compNode < 0 || shellNode < 0)
  275. return false;
  276. const auto dryWet = std::clamp (manual (device, "DryWet", 1.0), 0.0, 1.0);
  277. // (a macro on Dry/Wet: KeyMidi on channel 16 [DATA]; an envelope on it without a dry chain stays listed)
  278. const auto wetMoves = (int) device.number ("DryWet/KeyMidi/Channel", -1) == 16;
  279. int dry = -1;
  280. if (dryWet < 0.9999 || wetMoves)
  281. {
  282. dry = rack::addChain (doc, "Dry");
  283. if (auto* r = rack::rackOf (doc); r != nullptr && dry >= 0)
  284. {
  285. r->chains[0].volume = (float) dryWet;
  286. r->chains[(std::size_t) dry].volume = (float) (1.0 - dryWet);
  287. }
  288. }
  289. // the macros, named after Live's controls; each macro's 0 … 127 is its Live control's whole range (Live's
  290. // parameters reach them through `links`), mapped onto the LYNX controls between their values at the two ends
  291. auto* r = rack::rackOf (doc);
  292. if (r == nullptr)
  293. return false;
  294. const auto valueOf = [&doc] (int node, int param) { return doc.nodes[(std::size_t) node].effect.params[(std::size_t) param]; };
  295. struct Target
  296. {
  297. int node, param;
  298. float min, max;
  299. };
  300. const auto mapMacro = [&doc] (int macro, const char* name, std::initializer_list<Target> targets) {
  301. for (const auto& t : targets)
  302. {
  303. RackMapping m;
  304. m.node = t.node;
  305. m.param = t.param;
  306. m.min = t.min;
  307. m.max = t.max;
  308. rack::map (doc, macro, m);
  309. }
  310. rack::rackOf (doc)->macros[(std::size_t) macro].name = name;
  311. };
  312. // a mapping's line through the control's value now at the macro's place now (`t`), so the rack sounds as imported
  313. // (the patcher's macro wires drive the controls) and a macro move follows the measured law in a straight line: from
  314. // its value at Live's 0, or — when that line would leave 0 … 1 (the wires' range) — back from its value at Live's 1
  315. const auto line = [&valueOf] (int node, int param, float atZero, double t, float atOne) {
  316. const auto now = (double) valueOf (node, param);
  317. Target out { node, param, atZero, atOne };
  318. if (t < 0.01)
  319. return out;
  320. if (const auto top = atZero + (now - atZero) / t; top >= 0.0 && top <= 1.0)
  321. out.max = (float) top;
  322. else if (t < 0.99)
  323. out.min = (float) std::clamp (now - (atOne - now) * t / (1.0 - t), 0.0, 1.0);
  324. return out;
  325. };
  326. const auto lo = stageAt (0.0), hi = stageAt (1.0);
  327. const auto g0 = facet::gainValue (0.0);
  328. mapMacro (dbDrive, "Drive", { line (emberNode, em::drive, linValue (lo.driveDb, 0.0, 36.0), driveAmount, linValue (hi.driveDb, 0.0, 36.0)),
  329. line (emberNode, em::output, linValue (lo.outputDb, -18.0, 18.0), driveAmount, linValue (hi.outputDb, -18.0, 18.0)),
  330. line (colourNode, facet::param (7, 2), g0, driveAmount, facet::gainValue (hi.eqDb)) });
  331. const auto top = drumBussCrunchDb (1.0);
  332. const auto outZero = (float) ((drumbuss::colourGainDb + 24.0) / 48.0);
  333. mapMacro (dbCrunch, "Crunch", { line (colourNode, facet::param (4, 2), g0, crunch, facet::gainValue (-top)),
  334. line (colourNode, facet::param (5, 2), g0, crunch, facet::gainValue (-top)),
  335. line (colourNode, facet::outputParam, outZero, crunch, (float) ((std::clamp (drumbuss::colourGainDb + top, -24.0, 24.0) + 24.0) / 48.0)) });
  336. mapMacro (dbDamping, "Damping", { { emberNode, em::highCutWide, 0.0f, 1.0f } });
  337. mapMacro (dbTransients, "Transients", { { shellNode, sh::tsAmount, 0.0f, 1.0f } });
  338. const auto boomTop = drumBussBoom (1.0, boomHz, boomDecay);
  339. mapMacro (dbBoom, "Boom", { line (colourNode, facet::param (2, 2), g0, boomAmount, facet::gainValue (boomTop.bellDb)),
  340. line (colourNode, facet::param (3, 1), facet::hzValue (10.0), boomAmount, facet::hzValue (boomTop.cutHz)) });
  341. mapMacro (dbBoomFreq, "Boom Freq", { line (colourNode, facet::param (2, 1), facet::hzValue (30.0), (boomHz - 30.0) / 60.0, facet::hzValue (90.0)) });
  342. mapMacro (dbTrim, "Trim", { { emberNode, em::inputGain, 0.0f, 1.0f } });
  343. mapMacro (dbOutput, "Output", { { shellNode, sh::output, 0.0f, 1.0f } });
  344. if (dry >= 0)
  345. {
  346. RackMapping wet, dryMap;
  347. wet.target = dryMap.target = RackMapping::Target::chainVolume;
  348. wet.chain = 0;
  349. dryMap.chain = dry;
  350. wet.min = dryMap.max = 0.0f;
  351. wet.max = dryMap.min = rack::volumeValue (1.0f);
  352. rack::map (doc, dbDryWet, wet);
  353. rack::map (doc, dbDryWet, dryMap);
  354. r = rack::rackOf (doc);
  355. r->macros[(std::size_t) dbDryWet].name = "Dry/Wet";
  356. }
  357. // each macro where Live's control is now (nothing moves)
  358. r = rack::rackOf (doc);
  359. const auto at = [&r] (int macro, double v) { r->macros[(std::size_t) macro].value = (float) (127.0 * std::clamp (v, 0.0, 1.0)); };
  360. at (dbDrive, driveAmount);
  361. at (dbCrunch, crunch);
  362. at (dbDamping, valueOf (emberNode, em::highCutWide));
  363. at (dbTransients, std::abs (transients));
  364. at (dbBoom, boomAmount);
  365. at (dbBoomFreq, (boomHz - 30.0) / 60.0);
  366. at (dbTrim, valueOf (emberNode, em::inputGain));
  367. at (dbOutput, valueOf (shellNode, sh::output));
  368. if (dry >= 0)
  369. at (dbDryWet, (double) rack::volumeValue ((float) dryWet) / rack::volumeValue (1.0f));
  370. r->shownMacros = dry >= 0 ? 10 : 8;
  371. r->name = "Drum Buss";
  372. rack::compile (doc);
  373. // Live's parameters → the macros (the macro's 0..1 is the Live control's range)
  374. out.links.push_back ({ "DriveAmount", dbDrive, [] (double v) { return unit (v); } });
  375. out.links.push_back ({ "CrunchAmount", dbCrunch, [] (double v) { return unit (v); } });
  376. out.links.push_back ({ "DampingFrequency", dbDamping, [] (double hz) { return hz >= 19999.0 ? 1.0f : logValue (hz, 1000.0, 20000.0); } });
  377. if (transients >= 0.0)
  378. out.links.push_back ({ "TransientShaping", dbTransients, [] (double t) { return unit (t); } }); // (the positive side: Invert stays off)
  379. out.links.push_back ({ "BoomAmount", dbBoom, [] (double v) { return unit (v); } });
  380. out.links.push_back ({ "BoomFrequency", dbBoomFreq, [] (double hz) { return unit ((hz - 30.0) / 60.0); } });
  381. out.links.push_back ({ "InputTrim", dbTrim, [] (double g) { return linValue (dbOfGain (g), -24.0, 24.0); } });
  382. out.links.push_back ({ "OutputGain", dbOutput, [] (double g) { return linValue (dbOfGain (g), -20.0, 20.0); } });
  383. if (dry >= 0)
  384. out.links.push_back ({ "DryWet", dbDryWet, [] (double x) {
  385. return (float) std::clamp ((double) rack::volumeValue ((float) std::clamp (x, 0.0, 1.0)) / rack::volumeValue (1.0f), 0.0, 1.0);
  386. } });
  387. // the report: what is a fit, what has no counterpart (a dedicated drum-bus engine would carry these)
  388. static const char* const typeNames[3] { "Soft", "Medium", "Hard" };
  389. out.notes.approximated.push_back (std::string ("drive type ") + typeNames[type] + " as Ember's " + em::voiceName (stage.voice) + " voicing at the measured drive gain"
  390. + (type == 0 ? std::string() : std::string (" and the type's EQ (fitted at 100 %, in proportion below)")));
  391. if (type == 0 && driveAmount > 0.25)
  392. out.notes.approximated.push_back ("drive type Soft folds the loudest peaks over (a wave folder): the voicing clips them instead");
  393. out.notes.approximated.push_back ("the device's own colour (EQ and soft clipping at every setting) as Facet EQ bands and Ember's curve");
  394. if (s[sh::tsOn] > 0.5f)
  395. out.notes.approximated.push_back ("transients as Shell Channel's transient shaper (Live also lifts the tail)");
  396. if (boomAmount > 1.0e-4)
  397. out.notes.approximated.push_back ("Boom as a resonant bell and high pass (fitted at amount 0.5; no ringing tail of its own)");
  398. if (crunch > 1.0e-4)
  399. out.notes.approximated.push_back ("Crunch as its mid-band level (fitted; its distortion is not carried)");
  400. if (compOn)
  401. out.notes.approximated.push_back ("the compressor as Crest Compressor at about 4 : 1 (fitted)");
  402. if (dry >= 0)
  403. out.notes.approximated.push_back ("Dry/Wet as the rack's dry and wet chains");
  404. if (damping < 1000.0)
  405. out.notes.approximated.push_back (text ("damping %.0f Hz as 1 kHz (Ember's high cut: from 1 kHz)", damping));
  406. if (trimDb < -24.0)
  407. out.notes.approximated.push_back (text ("input trim %.1f dB (Ember's input: down to -24 dB)", trimDb));
  408. if (std::abs (outDb) > 20.0)
  409. out.notes.approximated.push_back (text ("output %.1f dB (Shell Channel's output: +/-20 dB)", outDb));
  410. if (transients < 0.0)
  411. out.notes.approximated.push_back ("negative transients: automation / macros of Transients are not carried");
  412. return true;
  413. }
  414. // Round 190fk (owner decision O20): Drum Buss onto LYNX's own drum-bus engine, Anvil Bus (core/AnvilBus.h), whose laws
  415. // were fitted to the same renders of Live 12.4 (rounds 190ei / 190fk). Every control maps one to one; Live's parameters
  416. // reach the engine's through `links`, so envelopes and rack macros on them are carried. The rack above stays for the
  417. // A/B harness and its tests (Context::drumBussRack); projects that already hold the rack keep it as it is.
  418. bool drumBussToEngine (const Element& device, Translated& out)
  419. {
  420. namespace an = anvil;
  421. if (! has (device, "DriveAmount") || ! has (device, "TransientShaping") || ! has (device, "BoomAmount"))
  422. return false;
  423. out.slot = makeEffectSlot (EffectKind::anvilBus);
  424. auto& p = out.slot.params;
  425. const auto type = std::clamp ((int) manual (device, "DriveType", 0), 0, 2);
  426. p[an::drive] = unit (manual (device, "DriveAmount", 0.2));
  427. p[an::driveType] = an::typeValue ((an::Type) type);
  428. p[an::crunch] = unit (manual (device, "CrunchAmount", 0.0));
  429. const auto dampHz = std::clamp (manual (device, "DampingFrequency", 20000.0), 500.0, 20000.0);
  430. const auto dampOf = [] (double hz) { return hz >= 19999.0 ? 1.0f : an::dampValue (hz); };
  431. p[an::damp] = dampOf (dampHz);
  432. p[an::transients] = an::transientsValue (manual (device, "TransientShaping", 0.0));
  433. p[an::boom] = unit (manual (device, "BoomAmount", 0.0));
  434. p[an::boomFreq] = an::boomHzValue (manual (device, "BoomFrequency", 50.0));
  435. p[an::boomDecay] = unit (manual (device, "BoomDecay", 0.5));
  436. p[an::comp] = device.boolean ("EnableCompression/Manual", device.boolean ("EnableCompression", false)) ? 1.0f : 0.0f;
  437. const auto trimDb = dbOfGain (manual (device, "InputTrim", 1.0));
  438. const auto outDb = dbOfGain (manual (device, "OutputGain", 1.0));
  439. p[an::trim] = an::trimValue (trimDb);
  440. p[an::output] = an::outputValue (outDb);
  441. p[an::dryWet] = unit (manual (device, "DryWet", 1.0));
  442. out.links.push_back ({ "DriveAmount", an::drive, [] (double v) { return unit (v); } });
  443. out.links.push_back ({ "DriveType", an::driveType, [] (double v) { return an::typeValue ((an::Type) std::clamp ((int) std::lround (v), 0, 2)); } });
  444. out.links.push_back ({ "CrunchAmount", an::crunch, [] (double v) { return unit (v); } });
  445. out.links.push_back ({ "DampingFrequency", an::damp, dampOf });
  446. out.links.push_back ({ "TransientShaping", an::transients, [] (double t) { return an::transientsValue (t); } });
  447. out.links.push_back ({ "BoomAmount", an::boom, [] (double v) { return unit (v); } });
  448. out.links.push_back ({ "BoomFrequency", an::boomFreq, [] (double hz) { return an::boomHzValue (hz); } });
  449. out.links.push_back ({ "BoomDecay", an::boomDecay, [] (double v) { return unit (v); } });
  450. out.links.push_back ({ "EnableCompression", an::comp, [] (double v) { return v >= 0.5 ? 1.0f : 0.0f; } });
  451. out.links.push_back ({ "InputTrim", an::trim, [] (double g) { return an::trimValue (dbOfGain (g)); } });
  452. out.links.push_back ({ "OutputGain", an::output, [] (double g) { return an::outputValue (dbOfGain (g)); } });
  453. out.links.push_back ({ "DryWet", an::dryWet, [] (double v) { return unit (v); } });
  454. // the report: what the engine holds as a fit rather than a measurement at that very setting
  455. // (round 190gx: the measured settings are Live's 190ei and 190gu renders)
  456. const auto drive = (double) p[an::drive];
  457. const auto measured = [] (double v, std::initializer_list<double> at) {
  458. return std::any_of (at.begin(), at.end(), [v] (double a) { return std::abs (v - a) < 1.0e-3; });
  459. };
  460. if (drive > 0.0 && ! measured (drive, { 0.25, 0.5, 1.0 }) && ! (type == 0 && measured (drive, { 0.75 })))
  461. out.notes.approximated.push_back ("drive between the measured amounts (0, 25, 50, 100 %; Soft also 75 %): curve and gain interpolated");
  462. if (drive > 0.0 && ! measured (drive, { 0.25, 0.5, 1.0 }))
  463. out.notes.approximated.push_back ("the drive type's EQ fitted at 25, 50 and 100 % and interpolated between");
  464. if (p[an::boom] > 0.0f && ! measured ((double) p[an::boom], { 0.25, 0.5, 1.0 }))
  465. out.notes.approximated.push_back ("Boom fitted at amounts 0.25, 0.5 and 1.0; amounts between interpolate its lift and resonance");
  466. if (p[an::crunch] > 0.0f)
  467. out.notes.approximated.push_back ("Crunch as its mid-band level (fitted at 0.25, 0.5 and 1.0; its grit is not carried)");
  468. if (outDb > 12.0)
  469. out.notes.approximated.push_back (text ("output %.1f dB (Anvil Bus: up to +12 dB)", outDb));
  470. if (device.boolean ("BoomAudition/Manual", false))
  471. out.notes.dropped.push_back ("Boom audition (the boom heard alone): Anvil Bus plays the whole device");
  472. return true;
  473. }
  474. // ================================================================================================ Auto Filter modulation
  475. float peakAmountValue (double k) noexcept
  476. {
  477. const auto raw = (k < 0.0 ? -1.0 : 1.0) * 256.0 * std::log (1.0 + std::min (std::abs (k), 2.0) / 0.4) / std::log (6.0);
  478. return unit ((raw / 256.0 + 1.0) * 0.5);
  479. }
  480. float peakLfoAmountValue (double k) noexcept
  481. {
  482. const auto raw = (k < 0.0 ? -1.0 : 1.0) * 256.0 * std::log (1.0 + std::min (std::abs (k), 1.0) / 0.2) / std::log (6.0);
  483. return unit ((raw / 256.0 + 1.0) * 0.5);
  484. }
  485. float peakLfoSpeedValue (double beats) noexcept
  486. {
  487. const auto raw = 65536.0 * std::log (std::max (beats, 3.0 / 96.0) / 0.128 + 1.0) / std::log (1001.0);
  488. return unit ((raw - 200.0) / 65336.0);
  489. }
  490. float peakDecayValue (double ms, double bpm) noexcept
  491. {
  492. // per quarter note the peak falls by perBeat (1 = the whole range): 1 / (the release in quarter notes)
  493. const auto perBeat = 60000.0 / (std::max (ms, 0.1) * std::max (bpm, 1.0));
  494. const auto d = 128.0 * std::log (perBeat / 0.0192 + 1.0) / std::log (10001.0);
  495. return unit ((d - 5.0) / 123.0);
  496. }
  497. CutoffModulation cutoffModulation (double c0, double lfoDepth, double envDepth, bool turnOver) noexcept
  498. {
  499. // the output o = E'·peak + L'·(w + 1)/2 (bases 0) and the cutoff = y0 + S·o: S = ±(2L + |E|) carries both at full
  500. // scale without clipping the output; a negative E (or `turnOver`, an LFO alone) turns the curve over, and the LFO
  501. // with it (half a cycle later)
  502. CutoffModulation m;
  503. const auto L = std::abs (lfoDepth), E = envDepth;
  504. const auto sign = E < 0.0 || (turnOver && std::abs (E) < 1.0e-12) ? -1.0 : 1.0;
  505. const auto span = std::max (2.0 * L + std::abs (E), 1.0e-6);
  506. const auto S = sign * span;
  507. m.invert = sign < 0.0;
  508. m.lfoAmount = peakLfoAmountValue (2.0 * L / span);
  509. m.peakAmount = peakAmountValue (std::abs (E) / span);
  510. const auto y0 = c0 - sign * L;
  511. constexpr int points = 33;
  512. for (int i = 0; i < points; ++i)
  513. m.curve.push_back (unit (y0 + S * (double) i / (double) (points - 1)));
  514. return m;
  515. }
  516. double autoFilter2SyncedBeats (int index) noexcept
  517. {
  518. // the same 22 note values as the older device's BeatRate, in the other order: 8 bars at 0 … 1/64 at 21 [MEASURED
  519. // 190ei: index 9 = 1.5 beats = 3/8 (the older table's 12 = 21 − 9), its triplet / dotted 1.0 / 2.25 beats; BIN: the
  520. // engine holds the same 22 labels for both]
  521. return liveBeatRateBeats (21 - std::clamp (index, 0, 21));
  522. }
  523. double autoFilter2LfoBeats (int timeMode, double hz, double seconds, int syncedRate, double sixteenths, double bpm, bool* synced) noexcept
  524. {
  525. // Lfo_TimeMode [MEASURED 190ei at 120 BPM: 0 Hz (1 Hz → 1.000 s), 1 time (0.8 s → 0.799 s), 2 synced (index 9 →
  526. // 0.750 s), 3 triplet (× 2/3: 0.500 s), 4 dotted (× 1.5: 1.126 s), 5 sixteenths (3 → 0.375 s)]
  527. const auto beatsPerSecond = std::max (bpm, 1.0) / 60.0;
  528. const auto mode = std::clamp (timeMode, 0, 5);
  529. if (synced != nullptr)
  530. *synced = mode >= 2;
  531. switch (mode)
  532. {
  533. case 1: return std::max (seconds, 0.001) * beatsPerSecond;
  534. case 2: return autoFilter2SyncedBeats (syncedRate);
  535. case 3: return autoFilter2SyncedBeats (syncedRate) * 2.0 / 3.0;
  536. case 4: return autoFilter2SyncedBeats (syncedRate) * 1.5;
  537. case 5: return std::max (sixteenths, 1.0) * 0.25;
  538. default: return beatsPerSecond / std::max (hz, 0.01);
  539. }
  540. }
  541. void autoFilterModulation (const Element& device, const Context& context, Translated& out)
  542. {
  543. const auto newer = device.name() == "AutoFilter2";
  544. // the depths in the Filter's cutoff units (0..1 = 20 Hz … 20 kHz, log: one unit = log2 (1000) octaves)
  545. constexpr double octavesPerUnit = 9.965784284662087;
  546. // [MEASURED 190ei, the cutoff tracked as the resonant peak of the output] the older device: LFO Amount [DATA 0 … 30]
  547. // in semitones of its cutoff (12 → ±12.04, 30 → ±29.80); the envelope's ModHub [DATA −127 … 127] 0.94 semitone per
  548. // unit at a full-scale peak (Δ = 0.94 · ModHub · peak: 56.4 st per unit of sine peak at ±60, linear in the peak's
  549. // amplitude). The new one: Lfo_Amount [DATA 0 … 1] 94 semitones per 1.0 (0.5 → ±47 st) and Envelope_Amount [DATA
  550. // −1 … 1] 96 semitones per 1.0 at a full-scale peak (0.5 → 47.8 st per unit of peak, −0.5 the mirror)
  551. const auto lfoRaw = manual (device, newer ? "Lfo_Amount" : "LfoAmount", 0.0);
  552. const auto envRaw = manual (device, newer ? "Envelope_Amount" : "ModHub", 0.0);
  553. const auto lfoSemitones = newer ? std::clamp (lfoRaw, 0.0, 1.0) * af2LfoSemitonesPerUnit : lfoRaw;
  554. const auto envSemitones = newer ? std::clamp (envRaw, -1.0, 1.0) * af2EnvelopeSemitonesPerUnit : envRaw * legacyEnvelopeSemitonesPerUnit;
  555. const auto lfoDepth = lfoSemitones / 12.0 / octavesPerUnit;
  556. const auto envDepth = envSemitones / 12.0 / octavesPerUnit;
  557. const auto lfoOn = (newer || manualBool (device, "Lfo/IsOn", true)) && std::abs (lfoDepth) > 1.0e-5;
  558. const auto envOn = std::abs (envDepth) > 1.0e-5;
  559. if (! lfoOn && ! envOn)
  560. return;
  561. // the shape [MEASURED 190ei on the older device at 1 Hz: 0 sine (0 rising at song time 0), 1 square (+ first), 2
  562. // triangle (from the bottom), 3 saw up, 4 saw down, 5 and 6 random steps four per cycle; DATA new 0 … 7: ASSUMED the
  563. // same first six] → the controller's sine 0, triangle 1 (it starts at its top: half a cycle later), square 2, saw 3
  564. // (rising), random 4
  565. const auto type = (int) manual (device, newer ? "Lfo_Waveform" : "Lfo/Type", 0);
  566. const auto random = type == 5 || (type == 6 && ! newer);
  567. // a falling saw alone (no envelope) is the rising one with the curve turned over
  568. const auto fallingSaw = lfoOn && ! envOn && type == 4;
  569. const auto m = cutoffModulation (out.slot.params[1], lfoOn ? lfoDepth : 0.0, envOn ? envDepth : 0.0, fallingSaw);
  570. Translated::Modulator mod;
  571. mod.controller = makeEffectSlot (EffectKind::peakController);
  572. auto& p = mod.controller.params;
  573. p[0] = 0.0f; // Base
  574. p[1] = envOn ? m.peakAmount : 0.5f; // Amount (0.5 = none)
  575. p[2] = 0.5f; // Tension (linear)
  576. p[4] = 0.0f; // LFO base
  577. p[5] = lfoOn ? m.lfoAmount : 0.5f; // LFO amount (0.5 = none)
  578. p[6] = 0.5f; // LFO tension (linear)
  579. p[10] = 0.0f; // Mute off: the audio passes
  580. p[11] = 0.0f; // Ramp off
  581. mod.param = 1;
  582. mod.curve = m.curve;
  583. if (envOn)
  584. {
  585. // the follower's release → the peak's linear fall from full scale; its attack: the peak rises at once
  586. const auto release = newer ? manual (device, "Envelope_Release", 0.25) * 1000.0 : manual (device, "Release", 200.0); // [DATA s / ms]
  587. const auto attack = newer ? manual (device, "Envelope_Attack", 0.001) * 1000.0 : manual (device, "Attack", 6.0);
  588. p[3] = peakDecayValue (release, context.bpm);
  589. mod.notes.push_back (text ("envelope follower: amount %.2f as %.1f semitones at a full-scale peak (measured), release %.0f ms as a straight fall",
  590. envRaw, envSemitones, release));
  591. if (attack > 1.0)
  592. mod.notes.push_back (text ("envelope follower attack %.1f ms (the Peak Controller rises at once)", attack));
  593. if (manualBool (device, "SideChain/OnOff", false))
  594. {
  595. const auto side = sideInputOf (device);
  596. mod.sideTrackId = side.trackId;
  597. mod.sideOther = side.trackId < 0;
  598. }
  599. }
  600. if (lfoOn)
  601. {
  602. // the rate: Hz or a time (fixed at the set's tempo) or a note value [DATA: older RateType 0 / 1, BeatRate 0 … 21]
  603. double beats = 1.0;
  604. bool synced = false;
  605. if (newer)
  606. beats = autoFilter2LfoBeats ((int) manual (device, "Lfo_TimeMode", 0), manual (device, "Lfo_Frequency", 1.0), manual (device, "Lfo_Time", 1.0),
  607. (int) manual (device, "Lfo_SyncedRate", 9), manual (device, "Lfo_Sixteenth", 4.0), context.bpm, &synced);
  608. else
  609. {
  610. synced = (int) manual (device, "Lfo/RateType", 0) == 1;
  611. beats = synced ? liveBeatRateBeats ((int) manual (device, "Lfo/BeatRate", 4)) : context.bpm / 60.0 / std::max (manual (device, "Lfo/Frequency", 1.0), 0.01);
  612. }
  613. // random steps: four per cycle of the rate [MEASURED 190ei at 1 Hz: a new value every 0.25 s] = one per period
  614. // of the controller's random
  615. p[7] = peakLfoSpeedValue (random ? beats / 4.0 : beats);
  616. static constexpr int shapes[6] { 0, 2, 1, 3, 3, 4 };
  617. p[8] = (float) (random ? 4 : shapes[std::clamp (type, 0, 5)]) / 4.0f;
  618. // the phase offset (degrees), the triangle's half cycle and the half cycle a turned-over curve needs
  619. auto phase = manual (device, newer ? "Lfo_PhaseOffset" : "Lfo/Offset", 0.0) / 360.0;
  620. if (type == 2)
  621. phase += 0.5;
  622. if (m.invert && ! fallingSaw)
  623. phase += 0.5;
  624. p[9] = (float) (phase - std::floor (phase));
  625. mod.notes.push_back (text ("LFO: amount %.2f as %.1f semitones either side (measured)", lfoRaw, lfoSemitones));
  626. if (! synced)
  627. mod.notes.push_back (text ("LFO rate %.2f Hz held as a note length at the set's tempo", beats > 0.0 ? context.bpm / 60.0 / beats : 0.0));
  628. if (type == 4 && ! fallingSaw)
  629. mod.notes.push_back ("LFO falling saw as a rising one (the envelope follower shares the curve)");
  630. if (newer && type > 5)
  631. mod.notes.push_back ("LFO shape as a sine");
  632. if (std::abs (manual (device, newer ? "Lfo_Spin" : "Lfo/Spin", 0.0)) > 1.0e-4
  633. || (newer ? manual (device, "Lfo_Phase", 0.0) : manual (device, "Lfo/Phase", 0.0)) > 1.0e-3)
  634. mod.notes.push_back ("LFO stereo offset between left and right (one cutoff for both sides)");
  635. }
  636. mod.what = lfoOn && envOn ? "LFO and envelope follower" : (lfoOn ? "LFO" : "envelope follower");
  637. out.modulator = std::move (mod);
  638. }
  639. // ================================================================================================ Operator, Drift
  640. double operatorRatioSemitones (int coarse, double fine) noexcept
  641. {
  642. const auto c = coarse <= 0 ? 0.5 : (double) std::min (coarse, 48);
  643. return 12.0 * std::log2 (c * (1.0 + std::clamp (fine, 0.0, 1000.0) / 1000.0));
  644. }
  645. void setOscillatorPitch (SynthOscillator& osc, double semitones) noexcept
  646. {
  647. const auto whole = std::round (semitones);
  648. auto st = (int) whole;
  649. osc.detune = (float) std::clamp ((semitones - whole) * 100.0, -100.0, 100.0);
  650. osc.octave = std::clamp (st / 12, -3, 3); // (toward zero: ±11 semitones stay in the octave)
  651. osc.semitones = std::clamp (st - 12 * osc.octave, -11, 11);
  652. }
  653. namespace
  654. {
  655. // the synth's level and the channel volume for a linear gain (the channel's curve tops at 1 = 0 dB)
  656. void setLevel (ChannelDef& channel, double gain)
  657. {
  658. const auto g = std::clamp (gain, 0.0, 1.0);
  659. channel.volume = g <= 0.0 ? 0.0f : (float) (std::log (10.0 * g + 1.0) / std::log (11.0));
  660. }
  661. // Operator's envelope [DATA: ms and linear levels] as ADSR seconds; anything beyond a plain ADSR goes to the notes
  662. void operatorEnvelope (const Element& env, SynthSettings& s, Notes& notes)
  663. {
  664. s.attack = (float) (manual (env, "AttackTime", 0.1) / 1000.0);
  665. s.decay = (float) (manual (env, "DecayTime", 600.0) / 1000.0);
  666. s.sustain = (float) std::clamp (manual (env, "SustainLevel", 1.0), 0.0, 1.0);
  667. s.release = (float) (manual (env, "ReleaseTime", 50.0) / 1000.0);
  668. if (! isFloor (manual (env, "AttackLevel", 0.0)) || manual (env, "DecayLevel", 1.0) < 0.999 || ! isFloor (manual (env, "ReleaseLevel", 0.0)))
  669. notes.approximated.push_back ("envelope start / peak / end levels (the synth's envelope runs 0 → 1 → sustain → 0)");
  670. if (std::abs (manual (env, "AttackSlope", 0.0)) > 1.0e-3 || std::abs (manual (env, "DecaySlope", 1.0) - 1.0) > 1.0e-3 || std::abs (manual (env, "ReleaseSlope", 1.0) - 1.0) > 1.0e-3)
  671. notes.approximated.push_back ("envelope slopes (straight segments)");
  672. if ((int) manual (env, "LoopMode", 0) != 0)
  673. notes.dropped.push_back ("envelope loop mode");
  674. }
  675. } // namespace
  676. bool operatorToSynth (const Element& device, ChannelDef& channel, double bpm, Notes& notes)
  677. {
  678. if (! device.path ("Operator.0/Envelope") || ! device.path ("Globals/Algorithm"))
  679. return false;
  680. const auto algorithm = (int) manual (device, "Globals/Algorithm", 0);
  681. // which operators sound: on, above Live's floor
  682. std::array<bool, 4> sounding {};
  683. for (int k = 0; k < 4; ++k)
  684. {
  685. const auto stem = "Operator." + std::to_string (k) + "/";
  686. sounding[(std::size_t) k] = manualBool (device, stem + "IsOn", true) && ! isFloor (manual (device, stem + "Volume", 1.0));
  687. }
  688. // A is a carrier in every algorithm [DOC]; with B … D silent it is a plain oscillator. Algorithm 10 = four carriers,
  689. // no modulation [DOC: the last algorithm, all side by side]. Any other case is FM: not translated.
  690. std::vector<int> carriers;
  691. if (! sounding[1] && ! sounding[2] && ! sounding[3])
  692. carriers = { 0 };
  693. else if (algorithm == 10)
  694. for (int k = 0; k < 4; ++k)
  695. if (sounding[(std::size_t) k])
  696. carriers.push_back (k);
  697. if (carriers.empty() || ! sounding[(std::size_t) carriers.front()])
  698. return false;
  699. // waveforms: 0 = sine, 21 = white noise, 22 = User (its 64 harmonics) [DATA: 0, 15, 20, 21, 22 seen; 21 is operator A
  700. // of the shipped "White Noise" preset, alone; the User wave holds the harmonic table; the others' order is not
  701. // settled: not translated]
  702. for (const auto k : carriers)
  703. {
  704. const auto wave = (int) manual (device, "Operator." + std::to_string (k) + "/WaveForm", 0);
  705. if (wave != 0 && wave != 21 && wave != 22)
  706. return false;
  707. if (manual (device, "Operator." + std::to_string (k) + "/Feedback", 0.0) > 0.5 && carriers.size() > 1)
  708. return false;
  709. }
  710. channel.instrument = InstrumentKind::synth;
  711. auto& s = channel.synth;
  712. s = SynthSettings {};
  713. for (auto& o : s.osc)
  714. o.level = 0.0f;
  715. bool userUsed = false, differentEnvelopes = false;
  716. for (std::size_t i = 0; i < carriers.size() && i < 3; ++i)
  717. {
  718. const auto k = carriers[i];
  719. const auto stem = "Operator." + std::to_string (k) + "/";
  720. auto& o = s.osc[i];
  721. const auto wave = (int) manual (device, stem + "WaveForm", 0);
  722. if (wave == 22)
  723. {
  724. if (userUsed)
  725. return false; // one harmonic table per synth
  726. userUsed = true;
  727. o.wave = Waveform::spectral;
  728. for (int h = 0; h < 64 && h < SynthSettings::spectrumSize; ++h)
  729. s.spectrum[(std::size_t) h] = (float) std::clamp (device.number (stem + "UserHarmonics/Harmonics." + std::to_string (h), h == 0 ? 1.0 : 0.0), 0.0, 1.0);
  730. }
  731. else
  732. o.wave = wave == 21 ? Waveform::noise : Waveform::sine;
  733. setOscillatorPitch (o, operatorRatioSemitones ((int) manual (device, stem + "Tune/Coarse", 1), manual (device, stem + "Tune/Fine", 0.0))
  734. + manual (device, "Globals/Transpose", 0.0));
  735. o.level = (float) std::clamp (manual (device, stem + "Volume", 1.0), 0.0, 1.0);
  736. if (manualBool (device, stem + "Tune/FixedFrequencyOn", false))
  737. notes.dropped.push_back ("fixed-frequency operator (it follows the note here)");
  738. if (i == 0)
  739. operatorEnvelope (device.path (stem + "Envelope"), s, notes);
  740. else
  741. differentEnvelopes = differentEnvelopes || std::abs (manual (device, stem + "Envelope/DecayTime", 0.0) - s.decay * 1000.0) > 1.0
  742. || std::abs (manual (device, stem + "Envelope/AttackTime", 0.0) - s.attack * 1000.0) > 1.0;
  743. if (manual (device, stem + "Feedback", 0.0) > 0.5)
  744. notes.dropped.push_back ("operator feedback");
  745. }
  746. if (carriers.size() > 3)
  747. notes.dropped.push_back ("the fourth carrier (the synth has three oscillators)");
  748. if (differentEnvelopes)
  749. notes.approximated.push_back ("one envelope for every oscillator (operator A's)");
  750. // the filter [DATA: Filter/OnOff, Type 0 … 4 (low, high, band pass, notch, morph: the Auto Filter's list ASSUMED),
  751. // Frequency Hz, Resonance 0 … 1.25, Slope (24 dB/oct: the synth's 4-pole ladder ASSUMED), FrequencyKeyScale %]
  752. if (manualBool (device, "Filter/OnOff", false))
  753. {
  754. static constexpr int types[5] { 0, 2, 1, 3, 0 };
  755. s.filterType = types[std::clamp ((int) manual (device, "Filter/Type", 0), 0, 4)];
  756. s.cutoff = (float) std::clamp (manual (device, "Filter/Frequency", 20000.0), 20.0, 20000.0);
  757. s.resonance = (float) std::clamp (manual (device, "Filter/Resonance", 0.0) / 1.25, 0.0, 1.0);
  758. s.ladder = manualBool (device, "Filter/Slope", false) && s.filterType == 0;
  759. s.keyTrack = (float) std::clamp (manual (device, "Filter/FrequencyKeyScale", 0.0) / 100.0, 0.0, 2.0);
  760. notes.approximated.push_back ("filter on the synth's filter (resonance and key tracking laws assumed)");
  761. if (std::abs (manual (device, "Filter/EnvelopeAmount", 0.0)) > 0.5)
  762. notes.dropped.push_back ("filter envelope");
  763. if (manual (device, "Filter/Drive", 0.0) > 0.01 || (int) manual (device, "Filter/CircuitLpHp", 0) != 0)
  764. notes.dropped.push_back ("filter circuit / drive");
  765. }
  766. else
  767. s.cutoff = 20000.0f;
  768. // voices: Live's polyphony index; glide [DATA: PortamentoOn, PortamentoTime ms] at the set's tempo
  769. s.porta = manualBool (device, "Globals/PortamentoOn", false);
  770. if (s.porta)
  771. s.portaBeats = (float) (manual (device, "Globals/PortamentoTime", 50.0) / 1000.0 * bpm / 60.0);
  772. setLevel (channel, manual (device, "Globals/Volume", 1.0));
  773. channel.pan = (float) std::clamp (manual (device, "Globals/Panorama", 0.0), -1.0, 1.0);
  774. if (manual (device, "Globals/SpreadAmount", 0.0) > 0.5)
  775. notes.dropped.push_back ("spread");
  776. if (std::abs (manual (device, "Globals/Tone", 0.7) - 0.7) > 0.01)
  777. notes.dropped.push_back ("tone (the anti-alias filter)");
  778. // the LFO and the pitch envelope reach an operator only when both its own switch and the section's are on [DATA]
  779. bool lfo = false, pitchEnv = false;
  780. for (const auto k : carriers)
  781. {
  782. const auto stem = "Operator." + std::to_string (k) + "/";
  783. lfo = lfo || (manualBool (device, "Lfo/LfoOn", false) && manualBool (device, stem + "LfoOn", true));
  784. pitchEnv = pitchEnv || (manualBool (device, "PitchEnv/PitchEnvOn", false) && manualBool (device, stem + "PitchEnvOn", true));
  785. }
  786. if (lfo)
  787. notes.dropped.push_back ("LFO");
  788. if (pitchEnv)
  789. notes.dropped.push_back ("pitch envelope");
  790. notes.approximated.push_back (std::string (carriers.size() == 1 ? "operator A alone" : "the carriers of algorithm 11") + " as the built-in synth's oscillators (level not matched to Live's)");
  791. return true;
  792. }
  793. bool driftToSynth (const Element& device, ChannelDef& channel, double bpm, Notes& notes)
  794. {
  795. if (! has (device, "Oscillator1_Type") || ! has (device, "Filter_Frequency") || ! has (device, "Envelope1_Attack"))
  796. return false;
  797. channel.instrument = InstrumentKind::synth;
  798. auto& s = channel.synth;
  799. s = SynthSettings {};
  800. // oscillator 1 [DOC order: sine, triangle, shark tooth, saturated, saw, pulse, rectangle; DATA 0 … 6] and 2 [DOC:
  801. // sine, triangle, saturated, saw, rectangle; DATA 0 … 4] as the synth's nearest waves; octaves [DATA −2 … 3 / −3 … 2]
  802. static constexpr Waveform waves1[7] { Waveform::sine, Waveform::triangle, Waveform::saw, Waveform::sine, Waveform::saw, Waveform::square, Waveform::square };
  803. static constexpr Waveform waves2[5] { Waveform::sine, Waveform::triangle, Waveform::sine, Waveform::saw, Waveform::square };
  804. const auto t1 = std::clamp ((int) manual (device, "Oscillator1_Type", 0), 0, 6);
  805. const auto t2 = std::clamp ((int) manual (device, "Oscillator2_Type", 0), 0, 4);
  806. const auto transpose = manual (device, "Global_Transpose", 0.0);
  807. s.osc[0].wave = waves1[t1];
  808. setOscillatorPitch (s.osc[0], 12.0 * manual (device, "Oscillator1_Transpose", 0.0) + transpose);
  809. s.osc[0].level = manualBool (device, "Mixer_OscillatorOn1", true) ? (float) std::clamp (manual (device, "Mixer_OscillatorGain1", 1.0), 0.0, 1.0) : 0.0f;
  810. s.osc[1].wave = waves2[t2];
  811. setOscillatorPitch (s.osc[1], 12.0 * manual (device, "Oscillator2_Transpose", 0.0) + manual (device, "Oscillator2_Detune", 0.0) + transpose);
  812. s.osc[1].level = manualBool (device, "Mixer_OscillatorOn2", true) ? (float) std::clamp (manual (device, "Mixer_OscillatorGain2", 1.0), 0.0, 1.0) : 0.0f;
  813. s.osc[2].wave = Waveform::noise;
  814. s.osc[2].level = manualBool (device, "Mixer_NoiseOn", true) ? (float) std::clamp (manual (device, "Mixer_NoiseLevel", 0.0), 0.0, 1.0) : 0.0f;
  815. if (t1 == 2 || t1 == 3 || t2 == 2)
  816. notes.approximated.push_back ("shark tooth / saturated waves as the synth's saw / sine");
  817. if (std::abs (manual (device, "Oscillator1_Shape", 0.0)) > 1.0e-3)
  818. notes.dropped.push_back ("oscillator 1 shape");
  819. // the filter: low pass [DOC: type I 12 dB/oct, II 24 dB/oct ladder; DATA Filter_Type 0 … 1], key tracking, the high pass
  820. s.filterType = 0;
  821. s.ladder = (int) manual (device, "Filter_Type", 0) == 1;
  822. s.cutoff = (float) std::clamp (manual (device, "Filter_Frequency", 20000.0), 20.0, 20000.0);
  823. s.resonance = (float) std::clamp (manual (device, "Filter_Resonance", 0.0), 0.0, 1.0);
  824. s.keyTrack = (float) std::clamp (manual (device, "Filter_Tracking", 0.0), 0.0, 1.0);
  825. if (manual (device, "Filter_HiPassFrequency", 10.0) > 20.0)
  826. notes.dropped.push_back ("the high pass before the filter");
  827. // envelope 1 = the amp envelope [DOC] in seconds [DATA]
  828. s.attack = (float) manual (device, "Envelope1_Attack", 0.001);
  829. s.decay = (float) manual (device, "Envelope1_Decay", 0.6);
  830. s.sustain = (float) std::clamp (manual (device, "Envelope1_Sustain", 1.0), 0.0, 1.0);
  831. s.release = (float) manual (device, "Envelope1_Release", 0.6);
  832. // voice mode [MEASURED 190ei, the menu: 0 Poly, 1 Mono, 2 Stereo, 3 Unison] and glide (s) at the set's tempo
  833. const auto voiceMode = (int) manual (device, "Global_VoiceMode", 0);
  834. s.mono = voiceMode == 1;
  835. if (voiceMode == 2)
  836. notes.dropped.push_back ("stereo voices");
  837. else if (voiceMode == 3)
  838. notes.dropped.push_back ("unison voices");
  839. const auto glide = manual (device, "Global_Glide", 0.0);
  840. s.porta = glide > 0.001;
  841. s.portaBeats = (float) (glide * bpm / 60.0);
  842. setLevel (channel, manual (device, "Global_Volume", 0.5));
  843. // what the built-in synth has no place for
  844. notes.dropped.push_back ("modulation (envelope 2, LFO, cycling envelope and the modulation matrix)");
  845. if (manual (device, "Global_DriftDepth", 0.0) > 1.0e-3)
  846. notes.dropped.push_back ("drift (the random detune of each voice)");
  847. notes.approximated.push_back ("oscillators, filter and amp envelope on the built-in synth (level not matched to Live's)");
  848. return true;
  849. }
  850. } // namespace lynx::alsdev