EnvController162Tests.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Round 162 (issue #7): the Envelope Controller's LFO, keyboard / velocity / Mod X / Mod Y / random maps, ADSR knobs,
  2. // global Mod X / Y and continuous output (docs/analise/REA_rodada162e-envelope.md).
  3. namespace ec162
  4. {
  5. // the 0xD5 state of one of the installation's Envelope Controller presets (empty without an installation)
  6. std::vector<std::uint8_t> presetState (const char* relative)
  7. {
  8. if (realFlpDir.empty())
  9. return {};
  10. std::ifstream in (std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Fruity Envelope Controller" / relative,
  11. std::ios::binary);
  12. const std::vector<std::uint8_t> b ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  13. std::size_t i = 0;
  14. while (i + 8 <= b.size() && std::memcmp (b.data() + i, "FLdt", 4) != 0)
  15. ++i;
  16. i += 8;
  17. while (i < b.size())
  18. {
  19. const auto id = b[i++];
  20. std::size_t n = id < 64 ? 1 : id < 128 ? 2 : id < 192 ? 4 : 0;
  21. if (id >= 192)
  22. for (int shift = 0; i < b.size(); shift += 7)
  23. {
  24. const auto c = b[i++];
  25. n |= (std::size_t) (c & 0x7F) << shift;
  26. if (c < 0x80)
  27. break;
  28. }
  29. if (id == 0xD5 && i + n <= b.size())
  30. return { b.begin() + (std::ptrdiff_t) i, b.begin() + (std::ptrdiff_t) (i + n) };
  31. i += n;
  32. }
  33. return {};
  34. }
  35. lynx::ControllerCurve line (float y0, float y1)
  36. {
  37. lynx::ControllerCurve c;
  38. c.count = 2;
  39. c.time = { 0.0f, 1.0f };
  40. c.level = { y0, y1 };
  41. return c;
  42. }
  43. bool near (double a, double b, double tolerance = 1.0e-4) { return std::abs (a - b) <= tolerance; }
  44. } // namespace ec162
  45. TEST (envelope_controller_velocity_input_and_lfo_rate_follow_fl)
  46. {
  47. // FL's velocity map input undoes its velocity -> volume law: ≈ 0.9918 · velocity
  48. CHECK (ec162::near (lynx::controllerVelocityInput (0.0f), 0.0));
  49. CHECK (ec162::near (lynx::controllerVelocityInput (1.0f), 0.9918, 2.0e-3));
  50. CHECK (ec162::near (lynx::controllerVelocityInput (100.0f / 128.0f), 0.775, 2.0e-3));
  51. CHECK (lynx::controllerVelocityInput (0.3f) < lynx::controllerVelocityInput (0.6f));
  52. // LFO speed: period 625 / (10001^(v/65536) − 1)
  53. lynx::ControllerArticulator a;
  54. a.lfoSpeed = 40898;
  55. CHECK (ec162::near (1.0 / lynx::controllerLfoRate (a), 2.0, 2.0e-3));
  56. a.lfoSpeed = 65536;
  57. CHECK (ec162::near (1.0 / lynx::controllerLfoRate (a), 0.0625, 1.0e-4));
  58. }
  59. TEST (envelope_controller_lfo_wave_shapes)
  60. {
  61. lynx::ControllerArticulator a; // straight triangle: 1 → −1 over the first half, back to 1
  62. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.0), 1.0));
  63. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.25), 0.0));
  64. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.5), -1.0, 1.0e-3));
  65. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.75), 0.0));
  66. CHECK (ec162::near (lynx::controllerLfoWave (a, 1.25), 0.0)); // periodic
  67. // a note starts the LFO at 0, rising
  68. const auto start = lynx::controllerLfoStartPhase (a);
  69. CHECK (ec162::near (lynx::controllerLfoWave (a, start), 0.0, 1.0e-3));
  70. CHECK (lynx::controllerLfoWave (a, start + 0.05) > 0.1f);
  71. // skew moves the bottom of the wave; pulse width > 0 holds the top at the end of the cycle
  72. a.lfoSkew = 64; // skew 0.25: the bottom comes at a quarter of the cycle instead of half
  73. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.25), -1.0, 1.0e-3));
  74. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.125), 0.0, 1.0e-3));
  75. CHECK (lynx::controllerLfoWave (a, 0.5) > -0.5f);
  76. a.lfoSkew = 0;
  77. a.lfoPulseWidth = 64;
  78. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.9), 1.0));
  79. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.25), -1.0, 1.0e-3)); // the triangle runs in the first half
  80. a.lfoPulseWidth = 0;
  81. // tension bends the triangle: positive pushes the values outwards, negative inwards (sign kept)
  82. a.lfoTension = 64;
  83. const auto bent = lynx::controllerLfoWave (a, 0.125); // straight: 0.5
  84. CHECK (bent > 0.6f && bent < 1.0f);
  85. a.lfoTension = -64;
  86. const auto pulled = lynx::controllerLfoWave (a, 0.125);
  87. CHECK (pulled > 0.0f && pulled < 0.4f);
  88. CHECK (ec162::near (lynx::controllerLfoWave (a, 0.375), -pulled, 1.0e-4));
  89. }
  90. TEST (envelope_controller_maps_and_modes)
  91. {
  92. // bipolar: base + (map − 0.5); unipolar: base × map
  93. lynx::ControllerArticulator a;
  94. a.bipolar = true;
  95. a.base = 0.5f;
  96. a.maps[lynx::mapModX] = ec162::line (0.0f, 1.0f);
  97. CHECK (ec162::near (lynx::controllerRestingValue (a, 0.2f, 0.5f), 0.2));
  98. CHECK (ec162::near (lynx::controllerRestingValue (a, 0.9f, 0.5f), 0.9));
  99. a.bipolar = false;
  100. a.base = 1.0f;
  101. a.maps[lynx::mapModX] = ec162::line (1.0f, 0.0f); // FL's "Smart X … (xfade)"
  102. a.maps[lynx::mapModY] = ec162::line (0.0f, 1.0f);
  103. CHECK (ec162::near (lynx::controllerRestingValue (a, 0.25f, 0.5f), 0.75 * 0.5));
  104. // empty maps change nothing in either mode
  105. lynx::ControllerArticulator plain;
  106. plain.base = 0.3f;
  107. CHECK (ec162::near (lynx::controllerRestingValue (plain, 0.0f, 1.0f), 0.3));
  108. plain.bipolar = true;
  109. CHECK (ec162::near (lynx::controllerRestingValue (plain, 0.0f, 1.0f), 0.3));
  110. // keyboard map: a step at key 24 (x = key / 120), unipolar gate envelope
  111. lynx::ControllerArticulator gate;
  112. gate.base = 0.0f;
  113. gate.envOn = true;
  114. gate.count = 2;
  115. gate.time = { 0.0f, 0.2f };
  116. gate.level = { 1.0f, 1.0f };
  117. auto& key = gate.maps[lynx::mapKeyboard];
  118. key.count = 4;
  119. key.time = { 0.0f, 0.1917f, 0.2f, 0.2083f };
  120. key.level = { 0.0f, 0.0f, 1.0f, 0.0f };
  121. lynx::ControllerNote note;
  122. note.key = 24.0f;
  123. CHECK (ec162::near (lynx::controllerValue (gate, 0.1, -1.0, 0.0, note, 0.5f, 0.5f), 1.0, 1.0e-3));
  124. note.key = 26.0f;
  125. CHECK (ec162::near (lynx::controllerValue (gate, 0.1, -1.0, 0.0, note, 0.5f, 0.5f), 0.0, 1.0e-3));
  126. // velocity map (unipolar, FL's "Note gate (velocity)") and the random map
  127. gate.maps[lynx::mapKeyboard] = {};
  128. gate.maps[lynx::mapVelocity] = ec162::line (0.0f, 1.0f);
  129. note.velocity = 0.5f;
  130. CHECK (ec162::near (lynx::controllerValue (gate, 0.1, -1.0, 0.0, note, 0.5f, 0.5f), lynx::controllerVelocityInput (0.5f), 1.0e-3));
  131. gate.maps[lynx::mapRandom] = ec162::line (0.0f, 1.0f);
  132. note.random = 0.5f;
  133. CHECK (ec162::near (lynx::controllerValue (gate, 0.1, -1.0, 0.0, note, 0.5f, 0.5f), 0.5 * lynx::controllerVelocityInput (0.5f), 1.0e-3));
  134. // the note's own Mod X is added to the global knob (0.5 = none)
  135. lynx::ControllerArticulator xs;
  136. xs.bipolar = true;
  137. xs.base = 0.5f;
  138. xs.maps[lynx::mapModX] = ec162::line (0.0f, 1.0f);
  139. note = {};
  140. note.modX = 0.7f;
  141. CHECK (ec162::near (lynx::controllerValue (xs, 0.0, -1.0, 0.0, note, 0.4f, 0.5f), 0.6, 1.0e-3));
  142. }
  143. TEST (envelope_controller_lfo_combines_by_mode)
  144. {
  145. lynx::ControllerArticulator a;
  146. a.lfoOn = true;
  147. a.lfoLevel = 0.5f;
  148. a.base = 0.5f;
  149. a.bipolar = true;
  150. lynx::ControllerNote note;
  151. // phase 0: wave = 1 → bipolar adds 0.5
  152. CHECK (ec162::near (lynx::controllerValue (a, 0.0, -1.0, 0.0, note, 0.5f, 0.5f), 1.0));
  153. CHECK (ec162::near (lynx::controllerValue (a, 0.0, -1.0, 0.5, note, 0.5f, 0.5f), 0.0, 1.0e-3));
  154. // unipolar multiplies by 1 + 2 · wave · level
  155. a.bipolar = false;
  156. a.base = 0.4f;
  157. CHECK (ec162::near (lynx::controllerValue (a, 0.0, -1.0, 0.0, note, 0.5f, 0.5f), 0.8));
  158. CHECK (ec162::near (lynx::controllerValue (a, 0.0, -1.0, 0.25, note, 0.5f, 0.5f), 0.4));
  159. // the amplitude curve fades it in
  160. a.lfoAmp.count = 2;
  161. a.lfoAmp.time = { 0.0f, 1.0f };
  162. a.lfoAmp.level = { 0.0f, 1.0f };
  163. CHECK (ec162::near (lynx::controllerValue (a, 0.0, -1.0, 0.0, note, 0.5f, 0.5f), 0.4));
  164. CHECK (ec162::near (lynx::controllerValue (a, 0.5, -1.0, 0.0, note, 0.5f, 0.5f), 0.6));
  165. // a voice ends once envelope and LFO amplitude have run out after the release
  166. CHECK (! lynx::controllerVoiceFinished (a, 0.5, -1.0));
  167. CHECK (lynx::controllerVoiceFinished (a, 2.0, 0.1));
  168. }
  169. TEST (envelope_controller_adsr_knobs)
  170. {
  171. auto a = lynx::defaultEnvelopeController()[0]; // 0 → 1 (0.02 s) → 0.6 sustain (0.3 s) → 0 (0.8 s)
  172. CHECK (lynx::controllerWithKnobs (a) == a);
  173. a.adsr = { 256, 64, 0, 128 };
  174. const auto k = lynx::controllerWithKnobs (a);
  175. CHECK (ec162::near (k.time[1], 0.04) && ec162::near (k.time[2], 0.04 + 0.28 * 0.5) && ec162::near (k.time[3], k.time[2] + 0.5));
  176. a.adsr = { 128, 128, 26, 128 };
  177. const auto s = lynx::controllerWithKnobs (a);
  178. CHECK (ec162::near (s.level[2], 0.6 + 26.0 / 128.0) && ec162::near (s.level[0], 0.0) && ec162::near (s.level[3], 0.0));
  179. }
  180. TEST (envelope_controller_lyec_versions_round_trip)
  181. {
  182. // version 1 (envelopes only) still loads
  183. std::vector<std::uint8_t> v1;
  184. auto put = [&] (const void* p, std::size_t n) { v1.insert (v1.end(), (const std::uint8_t*) p, (const std::uint8_t*) p + n); };
  185. const std::int32_t header[2] { 0x4345594C, 1 };
  186. put (header, 8);
  187. for (int k = 0; k < 8; ++k)
  188. {
  189. const std::int32_t flags = 1 | 2;
  190. const float base = 0.25f, level = 0.5f;
  191. const std::int32_t ints[4] { -1, 10, 256, 2 };
  192. const float points[6] { 0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.3f };
  193. put (&flags, 4);
  194. put (&base, 4);
  195. put (&level, 4);
  196. put (ints, 16);
  197. put (points, 24);
  198. }
  199. const auto old = lynx::parseEnvelopeController (v1);
  200. CHECK (old.has_value());
  201. if (old)
  202. {
  203. CHECK ((*old)[3].bipolar && (*old)[3].envOn && (*old)[3].lowKey == 10 && (*old)[3].count == 2 && ec162::near ((*old)[3].tension[1], 0.3));
  204. CHECK (! (*old)[3].lfoOn && (*old)[3].maps[0].count == 0 && ! old->continuous && ec162::near (old->modX, 0.5));
  205. }
  206. // version 2 keeps everything
  207. auto d = lynx::defaultEnvelopeController();
  208. d.modX = 0.3f;
  209. d.modY = 0.8f;
  210. d.continuous = true;
  211. d[2].lfoOn = d[2].lfoTempo = d[2].lfoGlobal = true;
  212. d[2].lfoSpeed = 12345;
  213. d[2].lfoTension = -40;
  214. d[2].lfoSkew = 20;
  215. d[2].lfoPulseWidth = 100;
  216. d[2].lfoLevel = -0.25f;
  217. d[2].lfoAmp = ec162::line (0.0f, 1.0f);
  218. d[2].lfoAmp.sustain = 1;
  219. d[2].adsr = { 100, 200, -30, 50 };
  220. d[5].maps[lynx::mapRandom] = ec162::line (0.2f, 0.9f);
  221. d[5].maps[lynx::mapModY].tension[0] = 0.5f;
  222. d[5].maps[lynx::mapModY].count = 2;
  223. d[5].maps[lynx::mapModY].time = { 0.1f, 0.6f };
  224. const auto back = lynx::parseEnvelopeController (lynx::writeEnvelopeController (d));
  225. CHECK (back.has_value());
  226. if (back)
  227. {
  228. CHECK (ec162::near (back->modX, 0.3) && ec162::near (back->modY, 0.8) && back->continuous);
  229. for (std::size_t k = 0; k < 8; ++k)
  230. CHECK ((*back)[k] == d[k]);
  231. }
  232. }
  233. TEST (envelope_controller_fl_presets_import_lfo_and_maps)
  234. {
  235. if (realFlpDir.empty())
  236. {
  237. std::printf (" (no FL Studio data: skipped)\n");
  238. return;
  239. }
  240. // every preset loads
  241. int presets = 0, read = 0;
  242. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Generators" / "Fruity Envelope Controller";
  243. for (auto& entry : std::filesystem::recursive_directory_iterator (dir))
  244. if (entry.path().extension() == ".fst")
  245. {
  246. ++presets;
  247. const auto relative = std::filesystem::relative (entry.path(), dir).string();
  248. read += lynx::parseEnvelopeController (ec162::presetState (relative.c_str())).has_value();
  249. }
  250. std::printf (" %d Envelope Controller presets, %d read\n", presets, read);
  251. CHECK (presets >= 20 && read == presets);
  252. // "Smart X for 2 controls (inverted)": continuous, bipolar, Mod X maps 0→1 and 1→0
  253. if (const auto smart = lynx::parseEnvelopeController (ec162::presetState ("Smart controls/Smart X for 2 controls (inverted).fst")))
  254. {
  255. CHECK (smart->continuous && ec162::near (smart->modX, 0.5));
  256. const auto& a = (*smart)[0];
  257. CHECK (a.bipolar && a.maps[lynx::mapModX].count == 2 && a.maps[lynx::mapModY].count == 0 && a.maps[lynx::mapKeyboard].count == 0);
  258. CHECK (ec162::near (lynx::controllerRestingValue (a, 0.25f, 0.5f), 0.25, 1.0e-3));
  259. CHECK (ec162::near (lynx::controllerRestingValue ((*smart)[1], 0.25f, 0.5f), 0.75, 1.0e-3));
  260. }
  261. else
  262. CHECK (false);
  263. // "Note LFO": a tempo-synced global LFO at 2 beats
  264. if (const auto lfo = lynx::parseEnvelopeController (ec162::presetState ("Misc/Note LFO.fst")))
  265. {
  266. const auto& a = (*lfo)[0];
  267. CHECK (a.lfoOn && a.lfoTempo && a.lfoGlobal && a.lfoSpeed == 40898 && a.lfoAmp.count == 0 && ec162::near (a.lfoLevel, 0.5));
  268. CHECK (! (*lfo)[1].lfoOn);
  269. }
  270. else
  271. CHECK (false);
  272. // "Note gate (velocity)": unipolar, velocity map 0→1
  273. if (const auto vel = lynx::parseEnvelopeController (ec162::presetState ("Misc/Note gate (velocity).fst")))
  274. {
  275. const auto& a = (*vel)[0];
  276. CHECK (! a.bipolar && a.maps[lynx::mapVelocity].count == 2 && ec162::near (a.maps[lynx::mapVelocity].level[1], 1.0));
  277. lynx::ControllerNote note;
  278. note.velocity = 0.25f;
  279. CHECK (ec162::near (lynx::controllerValue (a, 0.1, -1.0, 0.0, note, 0.5f, 0.5f), lynx::controllerVelocityInput (0.25f), 1.0e-3));
  280. }
  281. else
  282. CHECK (false);
  283. // "Note switches (C2-G2 mono)": output k is 1 only on key 24 + k
  284. if (const auto sw = lynx::parseEnvelopeController (ec162::presetState ("Misc/Note switches (C2-G2 mono).fst")))
  285. {
  286. lynx::ControllerNote note;
  287. for (int k : { 0, 3, 7 })
  288. {
  289. note.key = 24.0f + (float) k;
  290. CHECK (ec162::near (lynx::controllerValue ((*sw)[(std::size_t) k], 0.0, -1.0, 0.0, note, 0.5f, 0.5f), 1.0, 1.0e-3));
  291. note.key = 24.0f + (float) k + 1.0f;
  292. CHECK (ec162::near (lynx::controllerValue ((*sw)[(std::size_t) k], 0.0, -1.0, 0.0, note, 0.5f, 0.5f), 0.0, 1.0e-3));
  293. }
  294. }
  295. else
  296. CHECK (false);
  297. // the unipolar default keeps its maps neutral (empty)
  298. if (const auto product = lynx::parseEnvelopeController (ec162::presetState ("Default (unipolar product).fst")))
  299. for (const auto& m : (*product)[0].maps)
  300. CHECK (m.count == 0);
  301. else
  302. CHECK (false);
  303. }
  304. TEST (envelope_controller_instrument_plays_maps_and_continuous_output)
  305. {
  306. auto d = lynx::defaultEnvelopeController();
  307. d.continuous = true;
  308. d.modX = 0.2f;
  309. d[0].bipolar = true;
  310. d[0].envOn = false;
  311. d[0].base = 0.5f;
  312. d[0].maps[lynx::mapModX] = ec162::line (0.0f, 1.0f);
  313. // output 1: velocity map, unipolar gate
  314. d[1].envOn = true;
  315. d[1].base = 0.0f;
  316. d[1].count = 2;
  317. d[1].time = { 0.0f, 1.0f };
  318. d[1].level = { 1.0f, 1.0f };
  319. d[1].sustain = 1;
  320. d[1].maps[lynx::mapVelocity] = ec162::line (0.0f, 1.0f);
  321. lynx::ChannelDef c;
  322. c.instrument = lynx::InstrumentKind::controller;
  323. c.controllerState = lynx::writeEnvelopeController (d);
  324. auto inst = lynx::makeInstrument (lynx::InstrumentKind::controller);
  325. inst->prepare (48000.0);
  326. inst->configure (c, 120.0);
  327. std::vector<float> l (480), r (480);
  328. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  329. CHECK (ec162::near (inst->controlOutput (0), 0.2, 1.0e-3)); // no note: follows Mod X
  330. inst->setNextNoteModulation (0.9f, 0.5f);
  331. inst->noteOn (60, 0.5f);
  332. inst->render (l.data(), r.data(), 480, 1.0f, 1.0f);
  333. CHECK (ec162::near (inst->controlOutput (0), 0.6, 1.0e-3)); // the note adds its own Mod X (+0.4)
  334. CHECK (ec162::near (inst->controlOutput (1), lynx::controllerVelocityInput (0.5f), 1.0e-3));
  335. }