AlsDevicesLot3.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. No Live render of these devices exists yet:
  5. // every law that is not plain unit maths is tagged [ASSUMED] and the round's handoff lists the renders that settle it.
  6. #include "core/AlsDevices.h"
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <cstdio>
  10. #include "core/DynamicsSuite.h"
  11. #include "core/Effects.h"
  12. #include "core/MixSuite.h"
  13. #include "core/Rack.h"
  14. #include "core/XmlLite.h"
  15. namespace lynx::alsdev
  16. {
  17. namespace
  18. {
  19. using xml::Element;
  20. float unit (double v) noexcept { return (float) std::clamp (v, 0.0, 1.0); }
  21. float linValue (double v, double lo, double hi) noexcept { return unit ((v - lo) / (hi - lo)); }
  22. float logValue (double v, double lo, double hi) noexcept { return unit (std::log (std::clamp (v, lo, hi) / lo) / std::log (hi / lo)); }
  23. std::string text (const char* format, double a, double b = 0.0, double c = 0.0)
  24. {
  25. char out[240];
  26. std::snprintf (out, sizeof (out), format, a, b, c);
  27. return out;
  28. }
  29. double manual (const Element& device, const std::string& name, double fallback) { return device.number (name + "/Manual", fallback); }
  30. bool manualBool (const Element& device, const std::string& name, bool fallback) { return device.boolean (name + "/Manual", fallback); }
  31. bool has (const Element& device, const std::string& name) { return (bool) device.path (name + "/Manual"); }
  32. // Live's floor of a linear gain control (−70 dB) is shown as −inf [DATA: 0.0003162277571 at the bottom of the ranges]
  33. bool isFloor (double gain) noexcept { return gain <= 0.000317; }
  34. } // namespace
  35. // ================================================================================================ EQ Three
  36. bool eqThreeToPrism (const Element& device, Translated& out)
  37. {
  38. using namespace dyn::prism;
  39. if (! has (device, "GainLo") || ! has (device, "FreqLo") || ! has (device, "FreqHi"))
  40. return false;
  41. out.slot = makeEffectSlot (EffectKind::prismMultiband);
  42. auto& p = out.slot.params;
  43. p[bandCountParam] = bandCountValue (3);
  44. // the crossover sum (Live's EQ Three splits the signal into three bands and sums them [DOC]): Prism's minimum-phase
  45. // mode, the all-pass compensated crossover tree; 24 / 48 dB/oct [DOC; DATA Slope 0 … 1, ASSUMED 0 = 24]
  46. p[processingMode] = 1.0f;
  47. const auto slopeDb = manual (device, "Slope", 1.0) > 0.5 ? 48 : 24;
  48. out.links.push_back ({ "FreqLo", crossoverParam (0), [] (double hz) { return crossoverValue (hz); } });
  49. out.links.push_back ({ "FreqHi", crossoverParam (1), [] (double hz) { return crossoverValue (hz); } });
  50. 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);
  51. if (hi < lo)
  52. std::swap (lo, hi);
  53. p[(std::size_t) crossoverParam (0)] = crossoverValue (lo);
  54. p[(std::size_t) crossoverParam (1)] = crossoverValue (hi);
  55. p[slope] = (float) (slopeDb == 48 ? 1.0 : 0.5);
  56. for (int k = 0; k < 2; ++k)
  57. p[(std::size_t) (xoverSlope0 + k)] = crossoverSlopeValue (slopeDb);
  58. // no dynamics: each band only a level (its range 0 dB, ratio 1 : 1)
  59. static const char* const gains[3] { "GainLo", "GainMid", "GainHi" };
  60. static const char* const ons[3] { "LowOn", "MidOn", "HighOn" };
  61. std::array<bool, 3> sounds {};
  62. bool clamped = false;
  63. for (int b = 0; b < 3; ++b)
  64. {
  65. const auto gain = manual (device, gains[b], 1.0);
  66. sounds[(std::size_t) b] = manualBool (device, ons[b], true) && ! isFloor (gain);
  67. p[(std::size_t) param (b, fRange)] = 0.5f;
  68. p[(std::size_t) param (b, fRatio)] = 0.0f;
  69. p[(std::size_t) param (b, fEnabled)] = 1.0f;
  70. // a linear gain [DATA: 0.000316 … 1.995 = −70 … +6 dB] → Prism's band level (−30 … +30 dB)
  71. const auto db = dbOfGain (gain);
  72. p[(std::size_t) param (b, fLevel)] = linValue (db, -30.0, 30.0);
  73. clamped = clamped || (db < -30.0 && ! isFloor (gain));
  74. out.links.push_back ({ gains[b], param (b, fLevel), [] (double g) { return linValue (dbOfGain (g), -30.0, 30.0); } });
  75. }
  76. const auto alive = (int) std::count (sounds.begin(), sounds.end(), true);
  77. if (alive < 3)
  78. for (int b = 0; b < 3; ++b)
  79. p[(std::size_t) param (b, fSolo)] = sounds[(std::size_t) b] ? 1.0f : 0.0f;
  80. if (alive == 0)
  81. {
  82. p[output] = 0.0f; // Prism's lowest output (−36 dB): no setting of Prism is silent
  83. out.notes.approximated.push_back ("all three bands off: Prism's output at -36 dB (Live: silence)");
  84. }
  85. if (clamped)
  86. out.notes.approximated.push_back ("a band gain under -30 dB (Prism's lowest band level)");
  87. p[mix] = 1.0f;
  88. 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));
  89. return true;
  90. }
  91. // ================================================================================================ Drum Buss
  92. double drumBussDriveDb (double drive) noexcept { return 24.0 * std::clamp (drive, 0.0, 1.0); }
  93. double drumBussBoomLfHz (double boomHz) noexcept { return std::clamp (boomHz, 30.0, 90.0) / 0.72; }
  94. bool drumBussToRack (const Element& device, Translated& out)
  95. {
  96. namespace em = mixsuite::ember;
  97. namespace sh = mixsuite::shell;
  98. if (! has (device, "DriveAmount") || ! has (device, "TransientShaping") || ! has (device, "BoomAmount"))
  99. return false;
  100. // Ember: input trim, drive (Soft / Medium / Hard [DOC order; DATA DriveType 0 … 2]), damping as its high cut, no
  101. // level compensation
  102. auto ember = makeEffectSlot (EffectKind::ember);
  103. auto& e = ember.params;
  104. static constexpr int styles[3] { 0, 1, 2 }; // tube, tape, transistor [ASSUMED: soft, medium, hard]
  105. const auto type = std::clamp ((int) manual (device, "DriveType", 0), 0, 2);
  106. e[em::style] = (float) styles[type] / (float) (em::styles - 1);
  107. e[em::autoGain] = 0.0f;
  108. e[em::drive] = linValue (drumBussDriveDb (manual (device, "DriveAmount", 0.2)), 0.0, 36.0);
  109. const auto trimDb = dbOfGain (manual (device, "InputTrim", 1.0));
  110. e[em::inputGain] = linValue (trimDb, -24.0, 24.0);
  111. // Damping [DATA 500 … 20000 Hz] → Ember's high cut (2 … 20 kHz; its wide range 1 … 20 kHz below that, off at 20 kHz)
  112. const auto damping = std::clamp (manual (device, "DampingFrequency", 20000.0), 500.0, 20000.0);
  113. const auto wide = damping < 2000.0;
  114. e[em::wideCuts] = wide ? 1.0f : 0.0f;
  115. e[em::highCut] = damping >= 19999.0 ? 1.0f : logValue (damping, 2000.0, 20000.0);
  116. e[em::highCutWide] = damping >= 19999.0 ? 1.0f : logValue (damping, 1000.0, 20000.0);
  117. e[em::mix] = 1.0f;
  118. // Shell Channel: the transient shaper (−1 … +1: Amount = |t|, Invert below 0), Boom as the low enhancer (its pitch =
  119. // the enhancer's corner, Amount as its amount), Crunch as the high enhancer, the Compressor as the listen-mic
  120. // compressor, the output gain
  121. auto shell = makeEffectSlot (EffectKind::shellChannel);
  122. auto& s = shell.params;
  123. const auto transients = std::clamp (manual (device, "TransientShaping", 0.0), -1.0, 1.0);
  124. s[sh::tsOn] = std::abs (transients) > 1.0e-4 ? 1.0f : 0.0f;
  125. s[sh::tsAmount] = (float) std::abs (transients);
  126. s[sh::tsInvert] = transients < 0.0 ? 1.0f : 0.0f;
  127. const auto boom = std::clamp (manual (device, "BoomAmount", 0.0), 0.0, 1.0);
  128. s[sh::lfOn] = boom > 1.0e-4 ? 1.0f : 0.0f;
  129. s[sh::lfFreq] = sh::lfValue (drumBussBoomLfHz (manual (device, "BoomFrequency", 50.0)));
  130. s[sh::lfAmount] = (float) boom;
  131. const auto crunch = std::clamp (manual (device, "CrunchAmount", 0.0), 0.0, 1.0);
  132. s[sh::hfOn] = crunch > 1.0e-4 ? 1.0f : 0.0f;
  133. s[sh::hfAmount] = (float) crunch;
  134. if (device.boolean ("EnableCompression/Manual", device.boolean ("EnableCompression", false)))
  135. {
  136. s[sh::lmcOn] = 1.0f;
  137. s[sh::lmcComp] = 0.25f; // [ASSUMED: Live's fixed drum compressor as a light listen-mic compression]
  138. }
  139. const auto outDb = dbOfGain (manual (device, "OutputGain", 1.0));
  140. s[sh::output] = linValue (outDb, -20.0, 20.0);
  141. s[sh::outputMix] = 1.0f;
  142. // the rack: "Drum Buss" chain (Ember → Shell) and, when the device is not fully wet or a macro moves Dry/Wet, a "Dry"
  143. // chain beside it (the chains' volumes: dry 1 − x, wet x [ASSUMED linear])
  144. out.slot = rack::makeRackSlot (RackDoc::Kind::audioEffect);
  145. auto& doc = out.slot.patcher;
  146. if (auto* r = rack::rackOf (doc); r != nullptr && ! r->chains.empty())
  147. r->chains.front().name = "Drum Buss";
  148. PatchNode en, sn;
  149. en.effect = std::move (ember);
  150. en.label = "Drive";
  151. sn.effect = std::move (shell);
  152. sn.label = "Shape";
  153. const auto emberNode = rack::addDevice (doc, 0, std::move (en));
  154. const auto shellNode = rack::addDevice (doc, 0, std::move (sn));
  155. if (emberNode < 0 || shellNode < 0)
  156. return false;
  157. const auto dryWet = std::clamp (manual (device, "DryWet", 1.0), 0.0, 1.0);
  158. // (a macro on Dry/Wet: KeyMidi on channel 16 [DATA]; an envelope on it without a dry chain stays listed)
  159. const auto wetMoves = (int) device.number ("DryWet/KeyMidi/Channel", -1) == 16;
  160. int dry = -1;
  161. if (dryWet < 0.9999 || wetMoves)
  162. {
  163. dry = rack::addChain (doc, "Dry");
  164. if (auto* r = rack::rackOf (doc); r != nullptr && dry >= 0)
  165. {
  166. r->chains[0].volume = (float) dryWet;
  167. r->chains[(std::size_t) dry].volume = (float) (1.0 - dryWet);
  168. }
  169. }
  170. // the macros, each over the whole range of its LYNX control (a macro at v sets the control to v), named after
  171. // Live's controls; Live's own parameters reach them through `links`
  172. auto* r = rack::rackOf (doc);
  173. if (r == nullptr)
  174. return false;
  175. struct M
  176. {
  177. int macro;
  178. const char* name;
  179. int node, param;
  180. };
  181. const M macros[] { { dbDrive, "Drive", emberNode, em::drive }, { dbCrunch, "Crunch", shellNode, sh::hfAmount },
  182. { dbDamping, "Damping", emberNode, wide ? em::highCutWide : em::highCut },
  183. { dbTransients, "Transients", shellNode, sh::tsAmount }, { dbBoom, "Boom", shellNode, sh::lfAmount },
  184. { dbBoomFreq, "Boom Freq", shellNode, sh::lfFreq }, { dbTrim, "Trim", emberNode, em::inputGain },
  185. { dbOutput, "Output", shellNode, sh::output } };
  186. for (const auto& m : macros)
  187. {
  188. RackMapping target;
  189. target.node = m.node;
  190. target.param = m.param;
  191. rack::map (doc, m.macro, target);
  192. r = rack::rackOf (doc);
  193. r->macros[(std::size_t) m.macro].name = m.name;
  194. }
  195. if (dry >= 0)
  196. {
  197. RackMapping wet, dryMap;
  198. wet.target = dryMap.target = RackMapping::Target::chainVolume;
  199. wet.chain = 0;
  200. dryMap.chain = dry;
  201. wet.min = dryMap.max = 0.0f;
  202. wet.max = dryMap.min = rack::volumeValue (1.0f);
  203. rack::map (doc, dbDryWet, wet);
  204. rack::map (doc, dbDryWet, dryMap);
  205. r = rack::rackOf (doc);
  206. r->macros[(std::size_t) dbDryWet].name = "Dry/Wet";
  207. }
  208. // each macro where its control is now (nothing moves)
  209. const auto valueOf = [&doc] (int node, int param) { return doc.nodes[(std::size_t) node].effect.params[(std::size_t) param]; };
  210. for (const auto& m : macros)
  211. r->macros[(std::size_t) m.macro].value = valueOf (m.node, m.param) * 127.0f;
  212. if (dry >= 0)
  213. r->macros[(std::size_t) dbDryWet].value = (float) (127.0 * std::clamp ((double) rack::volumeValue ((float) dryWet) / rack::volumeValue (1.0f), 0.0, 1.0));
  214. r->shownMacros = dry >= 0 ? 10 : 8;
  215. r->name = "Drum Buss";
  216. rack::compile (doc);
  217. // Live's parameters → the macros (the macro's 0..1 is its control's 0..1)
  218. out.links.push_back ({ "DriveAmount", dbDrive, [] (double v) { return linValue (drumBussDriveDb (v), 0.0, 36.0); } });
  219. out.links.push_back ({ "CrunchAmount", dbCrunch, [] (double v) { return unit (v); } });
  220. if (wide)
  221. out.links.push_back ({ "DampingFrequency", dbDamping, [] (double hz) { return hz >= 19999.0 ? 1.0f : logValue (hz, 1000.0, 20000.0); } });
  222. else
  223. out.links.push_back ({ "DampingFrequency", dbDamping, [] (double hz) { return hz >= 19999.0 ? 1.0f : logValue (hz, 2000.0, 20000.0); } });
  224. if (transients >= 0.0)
  225. out.links.push_back ({ "TransientShaping", dbTransients, [] (double t) { return unit (t); } }); // (the positive side: Invert stays off)
  226. out.links.push_back ({ "BoomAmount", dbBoom, [] (double v) { return unit (v); } });
  227. out.links.push_back ({ "BoomFrequency", dbBoomFreq, [] (double hz) { return sh::lfValue (drumBussBoomLfHz (hz)); } });
  228. out.links.push_back ({ "InputTrim", dbTrim, [] (double g) { return linValue (dbOfGain (g), -24.0, 24.0); } });
  229. out.links.push_back ({ "OutputGain", dbOutput, [] (double g) { return linValue (dbOfGain (g), -20.0, 20.0); } });
  230. if (dry >= 0)
  231. out.links.push_back ({ "DryWet", dbDryWet, [] (double x) {
  232. return (float) std::clamp ((double) rack::volumeValue ((float) std::clamp (x, 0.0, 1.0)) / rack::volumeValue (1.0f), 0.0, 1.0);
  233. } });
  234. // the report: what is a fit, what has no counterpart
  235. out.notes.approximated.push_back (std::string ("drive as Ember's ") + (type == 0 ? "tube" : (type == 1 ? "tape" : "transistor"))
  236. + " style (Soft / Medium / Hard; drive law assumed)");
  237. if (s[sh::tsOn] > 0.5f)
  238. out.notes.approximated.push_back ("transients as Shell Channel's transient shaper");
  239. if (boom > 1.0e-4)
  240. out.notes.approximated.push_back ("Boom as Shell Channel's low enhancer (its decay is not applied)");
  241. if (crunch > 1.0e-4)
  242. out.notes.approximated.push_back ("Crunch as Shell Channel's high enhancer (a lift of the highs, not a distortion)");
  243. if (s[sh::lmcOn] > 0.5f)
  244. out.notes.approximated.push_back ("the compressor as Shell Channel's listen-mic compressor");
  245. if (dry >= 0)
  246. out.notes.approximated.push_back ("Dry/Wet as the rack's dry and wet chains");
  247. if (damping < 1000.0)
  248. out.notes.approximated.push_back (text ("damping %.0f Hz as 1 kHz (Ember's high cut: from 1 kHz)", damping));
  249. if (trimDb < -24.0)
  250. out.notes.approximated.push_back (text ("input trim %.1f dB (Ember's input: down to -24 dB)", trimDb));
  251. if (std::abs (outDb) > 20.0)
  252. out.notes.approximated.push_back (text ("output %.1f dB (Shell Channel's output: +/-20 dB)", outDb));
  253. if (transients < 0.0)
  254. out.notes.approximated.push_back ("negative transients: automation / macros of Transients are not carried");
  255. return true;
  256. }
  257. // ================================================================================================ Auto Filter modulation
  258. float peakAmountValue (double k) noexcept
  259. {
  260. 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);
  261. return unit ((raw / 256.0 + 1.0) * 0.5);
  262. }
  263. float peakLfoAmountValue (double k) noexcept
  264. {
  265. 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);
  266. return unit ((raw / 256.0 + 1.0) * 0.5);
  267. }
  268. float peakLfoSpeedValue (double beats) noexcept
  269. {
  270. const auto raw = 65536.0 * std::log (std::max (beats, 3.0 / 96.0) / 0.128 + 1.0) / std::log (1001.0);
  271. return unit ((raw - 200.0) / 65336.0);
  272. }
  273. float peakDecayValue (double ms, double bpm) noexcept
  274. {
  275. // per quarter note the peak falls by perBeat (1 = the whole range): 1 / (the release in quarter notes)
  276. const auto perBeat = 60000.0 / (std::max (ms, 0.1) * std::max (bpm, 1.0));
  277. const auto d = 128.0 * std::log (perBeat / 0.0192 + 1.0) / std::log (10001.0);
  278. return unit ((d - 5.0) / 123.0);
  279. }
  280. CutoffModulation cutoffModulation (double c0, double lfoDepth, double envDepth) noexcept
  281. {
  282. // the output o = E'·peak + L'·(w + 1)/2 (bases 0) and the cutoff = y0 + S·o: S = ±(2L + |E|) carries both at full
  283. // scale without clipping the output; a negative E turns the curve over, and the LFO with it (half a cycle later)
  284. CutoffModulation m;
  285. const auto L = std::abs (lfoDepth), E = envDepth;
  286. const auto sign = E < 0.0 ? -1.0 : 1.0;
  287. const auto span = std::max (2.0 * L + std::abs (E), 1.0e-6);
  288. const auto S = sign * span;
  289. m.invert = sign < 0.0;
  290. m.lfoAmount = peakLfoAmountValue (2.0 * L / span);
  291. m.peakAmount = peakAmountValue (std::abs (E) / span);
  292. const auto y0 = c0 - sign * L;
  293. constexpr int points = 33;
  294. for (int i = 0; i < points; ++i)
  295. m.curve.push_back (unit (y0 + S * (double) i / (double) (points - 1)));
  296. return m;
  297. }
  298. void autoFilterModulation (const Element& device, const Context& context, Translated& out)
  299. {
  300. const auto newer = device.name() == "AutoFilter2";
  301. // the depths in the Filter's cutoff units (0..1 = 20 Hz … 20 kHz, log: one unit = log2 (1000) octaves)
  302. constexpr double octavesPerUnit = 9.965784284662087;
  303. // the older device: LFO Amount [DATA 0 … 30] and the envelope's ModHub [DATA −127 … 127] in semitones of its cutoff
  304. // (a pitch) [ASSUMED]; the new one: Lfo_Amount [DATA 0 … 1] and Envelope_Amount [DATA −1 … 1] as fractions of the
  305. // whole range [ASSUMED]
  306. const auto lfoRaw = manual (device, newer ? "Lfo_Amount" : "LfoAmount", 0.0);
  307. const auto envRaw = manual (device, newer ? "Envelope_Amount" : "ModHub", 0.0);
  308. const auto lfoDepth = newer ? std::clamp (lfoRaw, 0.0, 1.0) * 0.5 : lfoRaw / 12.0 / octavesPerUnit;
  309. const auto envDepth = newer ? std::clamp (envRaw, -1.0, 1.0) : envRaw / 12.0 / octavesPerUnit;
  310. const auto lfoOn = (newer || manualBool (device, "Lfo/IsOn", true)) && std::abs (lfoDepth) > 1.0e-5;
  311. const auto envOn = std::abs (envDepth) > 1.0e-5;
  312. if (! lfoOn && ! envOn)
  313. return;
  314. const auto c0 = (double) out.slot.params[1];
  315. const auto m = cutoffModulation (c0, lfoOn ? lfoDepth : 0.0, envOn ? envDepth : 0.0);
  316. Translated::Modulator mod;
  317. mod.controller = makeEffectSlot (EffectKind::peakController);
  318. auto& p = mod.controller.params;
  319. p[0] = 0.0f; // Base
  320. p[1] = envOn ? m.peakAmount : 0.5f; // Amount (0.5 = none)
  321. p[2] = 0.5f; // Tension (linear)
  322. p[4] = 0.0f; // LFO base
  323. p[5] = lfoOn ? m.lfoAmount : 0.5f; // LFO amount (0.5 = none)
  324. p[6] = 0.5f; // LFO tension (linear)
  325. p[10] = 0.0f; // Mute off: the audio passes
  326. p[11] = 0.0f; // Ramp off
  327. mod.param = 1;
  328. mod.curve = m.curve;
  329. if (envOn)
  330. {
  331. // the follower's release → the peak's linear fall from full scale; its attack: the peak rises at once
  332. const auto release = newer ? manual (device, "Envelope_Release", 0.25) * 1000.0 : manual (device, "Release", 200.0); // [DATA s / ms]
  333. const auto attack = newer ? manual (device, "Envelope_Attack", 0.001) * 1000.0 : manual (device, "Attack", 6.0);
  334. p[3] = peakDecayValue (release, context.bpm);
  335. mod.notes.push_back (text ("envelope follower: amount %.0f as %.2f octaves at full scale, release %.0f ms as a straight fall (law assumed)", envRaw,
  336. envDepth * octavesPerUnit, release));
  337. if (attack > 1.0)
  338. mod.notes.push_back (text ("envelope follower attack %.1f ms (the Peak Controller rises at once)", attack));
  339. if (manualBool (device, "SideChain/OnOff", false))
  340. {
  341. const auto side = sideInputOf (device);
  342. mod.sideTrackId = side.trackId;
  343. mod.sideOther = side.trackId < 0;
  344. }
  345. }
  346. if (lfoOn)
  347. {
  348. // the rate: Hz (fixed at the set's tempo) or a synced note value [DATA: older RateType 0 / 1, BeatRate 0 … 21; the
  349. // new device's Lfo_TimeMode 0 = Hz, 2 = synced: ASSUMED]
  350. double beats = 1.0;
  351. bool synced = false;
  352. if (newer)
  353. {
  354. const auto mode = (int) manual (device, "Lfo_TimeMode", 0);
  355. synced = mode == 2;
  356. beats = synced ? liveBeatRateBeats ((int) manual (device, "Lfo_SyncedRate", 4)) : context.bpm / 60.0 / std::max (manual (device, "Lfo_Frequency", 1.0), 0.01);
  357. if (mode != 0 && mode != 2)
  358. mod.notes.push_back (text ("LFO time mode %.0f (taken as Hz)", mode));
  359. }
  360. else
  361. {
  362. synced = (int) manual (device, "Lfo/RateType", 0) == 1;
  363. beats = synced ? liveBeatRateBeats ((int) manual (device, "Lfo/BeatRate", 4)) : context.bpm / 60.0 / std::max (manual (device, "Lfo/Frequency", 1.0), 0.01);
  364. }
  365. p[7] = peakLfoSpeedValue (beats);
  366. // the shape [DOC order: sine, square, triangle, saw up, saw down, S&H; DATA older Type 0 … 6, new 0 … 7: ASSUMED
  367. // the same first six] → the controller's sine 0, triangle 1, square 2, saw 3, random 4
  368. const auto type = (int) manual (device, newer ? "Lfo_Waveform" : "Lfo/Type", 0);
  369. static constexpr int shapes[6] { 0, 2, 1, 3, 3, 4 };
  370. p[8] = (float) shapes[std::clamp (type, 0, 5)] / 4.0f;
  371. // the phase offset (degrees) and the half cycle a turned-over curve needs
  372. const auto phase = manual (device, newer ? "Lfo_PhaseOffset" : "Lfo/Offset", 0.0) / 360.0 + (m.invert ? 0.5 : 0.0);
  373. p[9] = (float) (phase - std::floor (phase));
  374. mod.notes.push_back (text ("LFO: amount %.2f as %.2f octaves either side (law assumed)", lfoRaw, lfoDepth * octavesPerUnit));
  375. if (! synced)
  376. 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));
  377. if (type == 4)
  378. mod.notes.push_back ("LFO falling saw as a rising one");
  379. if (type > 5)
  380. mod.notes.push_back ("LFO shape as a sine");
  381. if (std::abs (manual (device, newer ? "Lfo_Spin" : "Lfo/Spin", 0.0)) > 1.0e-4
  382. || (newer ? manual (device, "Lfo_Phase", 0.0) : manual (device, "Lfo/Phase", 0.0)) > 1.0e-3)
  383. mod.notes.push_back ("LFO stereo offset between left and right (one cutoff for both sides)");
  384. }
  385. mod.what = lfoOn && envOn ? "LFO and envelope follower" : (lfoOn ? "LFO" : "envelope follower");
  386. out.modulator = std::move (mod);
  387. }
  388. // ================================================================================================ Operator, Drift
  389. double operatorRatioSemitones (int coarse, double fine) noexcept
  390. {
  391. const auto c = coarse <= 0 ? 0.5 : (double) std::min (coarse, 48);
  392. return 12.0 * std::log2 (c * (1.0 + std::clamp (fine, 0.0, 1000.0) / 1000.0));
  393. }
  394. void setOscillatorPitch (SynthOscillator& osc, double semitones) noexcept
  395. {
  396. const auto whole = std::round (semitones);
  397. auto st = (int) whole;
  398. osc.detune = (float) std::clamp ((semitones - whole) * 100.0, -100.0, 100.0);
  399. osc.octave = std::clamp (st / 12, -3, 3); // (toward zero: ±11 semitones stay in the octave)
  400. osc.semitones = std::clamp (st - 12 * osc.octave, -11, 11);
  401. }
  402. namespace
  403. {
  404. // the synth's level and the channel volume for a linear gain (the channel's curve tops at 1 = 0 dB)
  405. void setLevel (ChannelDef& channel, double gain)
  406. {
  407. const auto g = std::clamp (gain, 0.0, 1.0);
  408. channel.volume = g <= 0.0 ? 0.0f : (float) (std::log (10.0 * g + 1.0) / std::log (11.0));
  409. }
  410. // Operator's envelope [DATA: ms and linear levels] as ADSR seconds; anything beyond a plain ADSR goes to the notes
  411. void operatorEnvelope (const Element& env, SynthSettings& s, Notes& notes)
  412. {
  413. s.attack = (float) (manual (env, "AttackTime", 0.1) / 1000.0);
  414. s.decay = (float) (manual (env, "DecayTime", 600.0) / 1000.0);
  415. s.sustain = (float) std::clamp (manual (env, "SustainLevel", 1.0), 0.0, 1.0);
  416. s.release = (float) (manual (env, "ReleaseTime", 50.0) / 1000.0);
  417. if (! isFloor (manual (env, "AttackLevel", 0.0)) || manual (env, "DecayLevel", 1.0) < 0.999 || ! isFloor (manual (env, "ReleaseLevel", 0.0)))
  418. notes.approximated.push_back ("envelope start / peak / end levels (the synth's envelope runs 0 → 1 → sustain → 0)");
  419. 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)
  420. notes.approximated.push_back ("envelope slopes (straight segments)");
  421. if ((int) manual (env, "LoopMode", 0) != 0)
  422. notes.dropped.push_back ("envelope loop mode");
  423. }
  424. } // namespace
  425. bool operatorToSynth (const Element& device, ChannelDef& channel, double bpm, Notes& notes)
  426. {
  427. if (! device.path ("Operator.0/Envelope") || ! device.path ("Globals/Algorithm"))
  428. return false;
  429. const auto algorithm = (int) manual (device, "Globals/Algorithm", 0);
  430. // which operators sound: on, above Live's floor
  431. std::array<bool, 4> sounding {};
  432. for (int k = 0; k < 4; ++k)
  433. {
  434. const auto stem = "Operator." + std::to_string (k) + "/";
  435. sounding[(std::size_t) k] = manualBool (device, stem + "IsOn", true) && ! isFloor (manual (device, stem + "Volume", 1.0));
  436. }
  437. // A is a carrier in every algorithm [DOC]; with B … D silent it is a plain oscillator. Algorithm 10 = four carriers,
  438. // no modulation [DOC: the last algorithm, all side by side]. Any other case is FM: not translated.
  439. std::vector<int> carriers;
  440. if (! sounding[1] && ! sounding[2] && ! sounding[3])
  441. carriers = { 0 };
  442. else if (algorithm == 10)
  443. for (int k = 0; k < 4; ++k)
  444. if (sounding[(std::size_t) k])
  445. carriers.push_back (k);
  446. if (carriers.empty() || ! sounding[(std::size_t) carriers.front()])
  447. return false;
  448. // waveforms: 0 = sine, 21 = white noise, 22 = User (its 64 harmonics) [DATA: 0, 15, 20, 21, 22 seen; 21 is operator A
  449. // of the shipped "White Noise" preset, alone; the User wave holds the harmonic table; the others' order is not
  450. // settled: not translated]
  451. for (const auto k : carriers)
  452. {
  453. const auto wave = (int) manual (device, "Operator." + std::to_string (k) + "/WaveForm", 0);
  454. if (wave != 0 && wave != 21 && wave != 22)
  455. return false;
  456. if (manual (device, "Operator." + std::to_string (k) + "/Feedback", 0.0) > 0.5 && carriers.size() > 1)
  457. return false;
  458. }
  459. channel.instrument = InstrumentKind::synth;
  460. auto& s = channel.synth;
  461. s = SynthSettings {};
  462. for (auto& o : s.osc)
  463. o.level = 0.0f;
  464. bool userUsed = false, differentEnvelopes = false;
  465. for (std::size_t i = 0; i < carriers.size() && i < 3; ++i)
  466. {
  467. const auto k = carriers[i];
  468. const auto stem = "Operator." + std::to_string (k) + "/";
  469. auto& o = s.osc[i];
  470. const auto wave = (int) manual (device, stem + "WaveForm", 0);
  471. if (wave == 22)
  472. {
  473. if (userUsed)
  474. return false; // one harmonic table per synth
  475. userUsed = true;
  476. o.wave = Waveform::spectral;
  477. for (int h = 0; h < 64 && h < SynthSettings::spectrumSize; ++h)
  478. 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);
  479. }
  480. else
  481. o.wave = wave == 21 ? Waveform::noise : Waveform::sine;
  482. setOscillatorPitch (o, operatorRatioSemitones ((int) manual (device, stem + "Tune/Coarse", 1), manual (device, stem + "Tune/Fine", 0.0))
  483. + manual (device, "Globals/Transpose", 0.0));
  484. o.level = (float) std::clamp (manual (device, stem + "Volume", 1.0), 0.0, 1.0);
  485. if (manualBool (device, stem + "Tune/FixedFrequencyOn", false))
  486. notes.dropped.push_back ("fixed-frequency operator (it follows the note here)");
  487. if (i == 0)
  488. operatorEnvelope (device.path (stem + "Envelope"), s, notes);
  489. else
  490. differentEnvelopes = differentEnvelopes || std::abs (manual (device, stem + "Envelope/DecayTime", 0.0) - s.decay * 1000.0) > 1.0
  491. || std::abs (manual (device, stem + "Envelope/AttackTime", 0.0) - s.attack * 1000.0) > 1.0;
  492. if (manual (device, stem + "Feedback", 0.0) > 0.5)
  493. notes.dropped.push_back ("operator feedback");
  494. }
  495. if (carriers.size() > 3)
  496. notes.dropped.push_back ("the fourth carrier (the synth has three oscillators)");
  497. if (differentEnvelopes)
  498. notes.approximated.push_back ("one envelope for every oscillator (operator A's)");
  499. // the filter [DATA: Filter/OnOff, Type 0 … 4 (low, high, band pass, notch, morph: the Auto Filter's list ASSUMED),
  500. // Frequency Hz, Resonance 0 … 1.25, Slope (24 dB/oct: the synth's 4-pole ladder ASSUMED), FrequencyKeyScale %]
  501. if (manualBool (device, "Filter/OnOff", false))
  502. {
  503. static constexpr int types[5] { 0, 2, 1, 3, 0 };
  504. s.filterType = types[std::clamp ((int) manual (device, "Filter/Type", 0), 0, 4)];
  505. s.cutoff = (float) std::clamp (manual (device, "Filter/Frequency", 20000.0), 20.0, 20000.0);
  506. s.resonance = (float) std::clamp (manual (device, "Filter/Resonance", 0.0) / 1.25, 0.0, 1.0);
  507. s.ladder = manualBool (device, "Filter/Slope", false) && s.filterType == 0;
  508. s.keyTrack = (float) std::clamp (manual (device, "Filter/FrequencyKeyScale", 0.0) / 100.0, 0.0, 2.0);
  509. notes.approximated.push_back ("filter on the synth's filter (resonance and key tracking laws assumed)");
  510. if (std::abs (manual (device, "Filter/EnvelopeAmount", 0.0)) > 0.5)
  511. notes.dropped.push_back ("filter envelope");
  512. if (manual (device, "Filter/Drive", 0.0) > 0.01 || (int) manual (device, "Filter/CircuitLpHp", 0) != 0)
  513. notes.dropped.push_back ("filter circuit / drive");
  514. }
  515. else
  516. s.cutoff = 20000.0f;
  517. // voices: Live's polyphony index; glide [DATA: PortamentoOn, PortamentoTime ms] at the set's tempo
  518. s.porta = manualBool (device, "Globals/PortamentoOn", false);
  519. if (s.porta)
  520. s.portaBeats = (float) (manual (device, "Globals/PortamentoTime", 50.0) / 1000.0 * bpm / 60.0);
  521. setLevel (channel, manual (device, "Globals/Volume", 1.0));
  522. channel.pan = (float) std::clamp (manual (device, "Globals/Panorama", 0.0), -1.0, 1.0);
  523. if (manual (device, "Globals/SpreadAmount", 0.0) > 0.5)
  524. notes.dropped.push_back ("spread");
  525. if (std::abs (manual (device, "Globals/Tone", 0.7) - 0.7) > 0.01)
  526. notes.dropped.push_back ("tone (the anti-alias filter)");
  527. // the LFO and the pitch envelope reach an operator only when both its own switch and the section's are on [DATA]
  528. bool lfo = false, pitchEnv = false;
  529. for (const auto k : carriers)
  530. {
  531. const auto stem = "Operator." + std::to_string (k) + "/";
  532. lfo = lfo || (manualBool (device, "Lfo/LfoOn", false) && manualBool (device, stem + "LfoOn", true));
  533. pitchEnv = pitchEnv || (manualBool (device, "PitchEnv/PitchEnvOn", false) && manualBool (device, stem + "PitchEnvOn", true));
  534. }
  535. if (lfo)
  536. notes.dropped.push_back ("LFO");
  537. if (pitchEnv)
  538. notes.dropped.push_back ("pitch envelope");
  539. 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)");
  540. return true;
  541. }
  542. bool driftToSynth (const Element& device, ChannelDef& channel, double bpm, Notes& notes)
  543. {
  544. if (! has (device, "Oscillator1_Type") || ! has (device, "Filter_Frequency") || ! has (device, "Envelope1_Attack"))
  545. return false;
  546. channel.instrument = InstrumentKind::synth;
  547. auto& s = channel.synth;
  548. s = SynthSettings {};
  549. // oscillator 1 [DOC order: sine, triangle, shark tooth, saturated, saw, pulse, rectangle; DATA 0 … 6] and 2 [DOC:
  550. // sine, triangle, saturated, saw, rectangle; DATA 0 … 4] as the synth's nearest waves; octaves [DATA −2 … 3 / −3 … 2]
  551. static constexpr Waveform waves1[7] { Waveform::sine, Waveform::triangle, Waveform::saw, Waveform::sine, Waveform::saw, Waveform::square, Waveform::square };
  552. static constexpr Waveform waves2[5] { Waveform::sine, Waveform::triangle, Waveform::sine, Waveform::saw, Waveform::square };
  553. const auto t1 = std::clamp ((int) manual (device, "Oscillator1_Type", 0), 0, 6);
  554. const auto t2 = std::clamp ((int) manual (device, "Oscillator2_Type", 0), 0, 4);
  555. const auto transpose = manual (device, "Global_Transpose", 0.0);
  556. s.osc[0].wave = waves1[t1];
  557. setOscillatorPitch (s.osc[0], 12.0 * manual (device, "Oscillator1_Transpose", 0.0) + transpose);
  558. s.osc[0].level = manualBool (device, "Mixer_OscillatorOn1", true) ? (float) std::clamp (manual (device, "Mixer_OscillatorGain1", 1.0), 0.0, 1.0) : 0.0f;
  559. s.osc[1].wave = waves2[t2];
  560. setOscillatorPitch (s.osc[1], 12.0 * manual (device, "Oscillator2_Transpose", 0.0) + manual (device, "Oscillator2_Detune", 0.0) + transpose);
  561. s.osc[1].level = manualBool (device, "Mixer_OscillatorOn2", true) ? (float) std::clamp (manual (device, "Mixer_OscillatorGain2", 1.0), 0.0, 1.0) : 0.0f;
  562. s.osc[2].wave = Waveform::noise;
  563. s.osc[2].level = manualBool (device, "Mixer_NoiseOn", true) ? (float) std::clamp (manual (device, "Mixer_NoiseLevel", 0.0), 0.0, 1.0) : 0.0f;
  564. if (t1 == 2 || t1 == 3 || t2 == 2)
  565. notes.approximated.push_back ("shark tooth / saturated waves as the synth's saw / sine");
  566. if (std::abs (manual (device, "Oscillator1_Shape", 0.0)) > 1.0e-3)
  567. notes.dropped.push_back ("oscillator 1 shape");
  568. // 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
  569. s.filterType = 0;
  570. s.ladder = (int) manual (device, "Filter_Type", 0) == 1;
  571. s.cutoff = (float) std::clamp (manual (device, "Filter_Frequency", 20000.0), 20.0, 20000.0);
  572. s.resonance = (float) std::clamp (manual (device, "Filter_Resonance", 0.0), 0.0, 1.0);
  573. s.keyTrack = (float) std::clamp (manual (device, "Filter_Tracking", 0.0), 0.0, 1.0);
  574. if (manual (device, "Filter_HiPassFrequency", 10.0) > 20.0)
  575. notes.dropped.push_back ("the high pass before the filter");
  576. // envelope 1 = the amp envelope [DOC] in seconds [DATA]
  577. s.attack = (float) manual (device, "Envelope1_Attack", 0.001);
  578. s.decay = (float) manual (device, "Envelope1_Decay", 0.6);
  579. s.sustain = (float) std::clamp (manual (device, "Envelope1_Sustain", 1.0), 0.0, 1.0);
  580. s.release = (float) manual (device, "Envelope1_Release", 0.6);
  581. // voice mode [DATA Global_VoiceMode 0 … 2, ASSUMED 0 poly, 1 mono, 2 stereo / unison] and glide (s) at the set's tempo
  582. const auto voiceMode = (int) manual (device, "Global_VoiceMode", 0);
  583. s.mono = voiceMode == 1;
  584. if (voiceMode == 2)
  585. notes.dropped.push_back ("stereo / unison voices");
  586. const auto glide = manual (device, "Global_Glide", 0.0);
  587. s.porta = glide > 0.001;
  588. s.portaBeats = (float) (glide * bpm / 60.0);
  589. setLevel (channel, manual (device, "Global_Volume", 0.5));
  590. // what the built-in synth has no place for
  591. notes.dropped.push_back ("modulation (envelope 2, LFO, cycling envelope and the modulation matrix)");
  592. if (manual (device, "Global_DriftDepth", 0.0) > 1.0e-3)
  593. notes.dropped.push_back ("drift (the random detune of each voice)");
  594. notes.approximated.push_back ("oscillators, filter and amp envelope on the built-in synth (level not matched to Live's)");
  595. return true;
  596. }
  597. } // namespace lynx::alsdev