Layer164Tests.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // Round 164a: FL's Layer channel (core/Layer.h) and the channels' key region
  2. namespace layer164
  3. {
  4. // a hosted "plugin" that records the notes it gets
  5. struct NoteSpy final : lynx::Instrument
  6. {
  7. struct Got
  8. {
  9. bool on = true;
  10. int key = 0;
  11. float velocity = 0.0f;
  12. };
  13. std::vector<Got> got;
  14. void prepare (double) noexcept override {}
  15. void noteOn (int key, float velocity) noexcept override { got.push_back ({ true, key, velocity }); }
  16. void noteOff (int key) noexcept override { got.push_back ({ false, key, 0.0f }); }
  17. void allNotesOff() noexcept override {}
  18. void render (float*, float*, int, float, float) noexcept override {}
  19. int ons() const
  20. {
  21. int n = 0;
  22. for (const auto& g : got)
  23. n += g.on ? 1 : 0;
  24. return n;
  25. }
  26. };
  27. struct Rig
  28. {
  29. lynx::Song song;
  30. std::vector<std::shared_ptr<NoteSpy>> spies; // [channel] (the Layer's own is unused)
  31. std::unique_ptr<const lynx::RackState> state;
  32. lynx::RackRenderer renderer;
  33. lynx::LiveEventQueue live;
  34. // channel 0 = the Layer, channels 1…children = spies
  35. explicit Rig (int children, std::function<void (lynx::Song&)> setup = {})
  36. {
  37. song.mixer = lynx::Song::defaultMixer (2);
  38. lynx::ChannelDef l;
  39. l.name = "Layer";
  40. l.instrument = lynx::InstrumentKind::layer;
  41. song.channels.push_back (l);
  42. for (int k = 0; k < children; ++k)
  43. {
  44. lynx::ChannelDef c;
  45. c.instrument = lynx::InstrumentKind::plugin;
  46. c.plugin.id = "spy" + std::to_string (k + 1);
  47. song.channels.push_back (c);
  48. song.channels[0].layer.children.push_back (k + 1);
  49. }
  50. song.patterns.emplace_back();
  51. if (setup)
  52. setup (song);
  53. spies.resize (song.channels.size());
  54. lynx::RackState::Factories f;
  55. f.pluginInstruments = [this] (const lynx::PluginRef& ref, double) {
  56. const auto k = std::stoi (ref.id.substr (3));
  57. spies[(std::size_t) k] = std::make_shared<NoteSpy>();
  58. return std::shared_ptr<lynx::Instrument> (spies[(std::size_t) k]);
  59. };
  60. state = lynx::RackState::build (song, nullptr, 44100.0, f);
  61. renderer.prepare (44100.0, 256);
  62. }
  63. void play (int channel, int key, bool on = true)
  64. {
  65. lynx::NoteEvent e;
  66. e.type = on ? lynx::NoteEvent::Type::on : lynx::NoteEvent::Type::off;
  67. e.channel = channel;
  68. e.key = key;
  69. e.velocity = on ? 0.8f : 0.0f;
  70. live.push (e);
  71. std::vector<float> l (256), r (256);
  72. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live, nullptr);
  73. }
  74. };
  75. } // namespace layer164
  76. TEST (layer164_every_child_plays_the_layers_notes)
  77. {
  78. layer164::Rig rig (3);
  79. rig.play (0, 60);
  80. rig.play (0, 60, false);
  81. for (int k = 1; k <= 3; ++k)
  82. {
  83. CHECK (rig.spies[(std::size_t) k]->ons() == 1);
  84. CHECK (rig.spies[(std::size_t) k]->got.size() == 2);
  85. CHECK (rig.spies[(std::size_t) k]->got[0].key == 60);
  86. CHECK (! rig.spies[(std::size_t) k]->got[1].on);
  87. }
  88. // a child's own notes don't go to its siblings
  89. rig.play (2, 64);
  90. CHECK (rig.spies[2]->ons() == 2);
  91. CHECK (rig.spies[1]->ons() == 1 && rig.spies[3]->ons() == 1);
  92. }
  93. TEST (layer164_random_plays_one_child_and_its_off_reaches_it)
  94. {
  95. layer164::Rig rig (4, [] (lynx::Song& s) { s.channels[0].layer.random = true; });
  96. std::array<int, 5> count {};
  97. for (int n = 0; n < 200; ++n)
  98. {
  99. rig.play (0, 60);
  100. rig.play (0, 60, false);
  101. }
  102. int total = 0;
  103. for (int k = 1; k <= 4; ++k)
  104. {
  105. count[(std::size_t) k] = rig.spies[(std::size_t) k]->ons();
  106. total += count[(std::size_t) k];
  107. CHECK (count[(std::size_t) k] > 20); // every child gets picked
  108. // each child got as many offs as ons
  109. CHECK ((int) rig.spies[(std::size_t) k]->got.size() == 2 * count[(std::size_t) k]);
  110. }
  111. CHECK (total == 200);
  112. }
  113. TEST (layer164_sequential_plays_the_children_in_turn)
  114. {
  115. layer164::Rig rig (3, [] (lynx::Song& s) { s.channels[0].layer.sequential = true; });
  116. std::vector<int> order;
  117. for (int n = 0; n < 6; ++n)
  118. {
  119. rig.play (0, 60);
  120. for (int k = 1; k <= 3; ++k)
  121. if (rig.spies[(std::size_t) k]->ons() > (int) std::count (order.begin(), order.end(), k))
  122. order.push_back (k);
  123. rig.play (0, 60, false);
  124. }
  125. CHECK (order == std::vector<int> ({ 1, 2, 3, 1, 2, 3 }));
  126. }
  127. TEST (layer164_crossfade_gains)
  128. {
  129. using lynx::layer::crossfadeGain;
  130. // three children: child 1 at 0, 1+2 at 0.25, child 2 at 0.5, 2+3 at 0.75, child 3 at 1
  131. CHECK (crossfadeGain (0, 3, 0.0f) == 1.0f && crossfadeGain (1, 3, 0.0f) == 0.0f);
  132. CHECK (std::abs (crossfadeGain (1, 3, 0.5f) - 1.0f) < 1e-6f && crossfadeGain (0, 3, 0.5f) == 0.0f && crossfadeGain (2, 3, 0.5f) == 0.0f);
  133. CHECK (std::abs (crossfadeGain (0, 3, 0.25f) - crossfadeGain (1, 3, 0.25f)) < 1e-6f);
  134. CHECK (std::abs (crossfadeGain (0, 1, 0.7f) - 0.7f) < 1e-6f); // (one child: the Fade itself, as FL)
  135. CHECK (std::abs (crossfadeGain (0, 3, 0.25f) - 0.5f) < 1e-6f); // FL's linear triangle
  136. // a crossfaded Layer only plays the children with a level
  137. layer164::Rig rig (3, [] (lynx::Song& s) { s.channels[0].layer.crossfade = true; s.channels[0].layer.fade = 1.0f; });
  138. rig.play (0, 60);
  139. CHECK (rig.spies[1]->ons() == 0 && rig.spies[2]->ons() == 0 && rig.spies[3]->ons() == 1);
  140. // FL scales the child by the Layer's channel gain (a plugin has no voice gain: the velocity carries it)
  141. CHECK (std::abs (rig.spies[3]->got[0].velocity - 0.8f * lynx::channelGain (0.78f)) < 1e-5f);
  142. }
  143. TEST (layer164_key_region_and_split_children)
  144. {
  145. layer164::Rig rig (3, [] (lynx::Song& s) {
  146. s.channels[0].misc.rootNote = 36;
  147. lynx::layer::splitChildren (s, 0);
  148. });
  149. CHECK (rig.song.channels[1].keyLow == 36 && rig.song.channels[1].keyHigh == 36);
  150. CHECK (rig.song.channels[3].keyLow == 38 && rig.song.channels[3].keyHigh == 38);
  151. CHECK (rig.song.channels[2].misc.rootNote == 37 && rig.song.channels[2].misc.addToKey);
  152. rig.play (0, 37);
  153. CHECK (rig.spies[1]->ons() == 0 && rig.spies[2]->ons() == 1 && rig.spies[3]->ons() == 0);
  154. // the key region holds for the channel's own notes too
  155. rig.play (3, 50);
  156. CHECK (rig.spies[3]->ons() == 0);
  157. rig.play (3, 38);
  158. CHECK (rig.spies[3]->ons() == 1);
  159. // reset: the whole keyboard, root back on C5
  160. lynx::layer::resetChildren (rig.song, 0);
  161. CHECK (rig.song.channels[2].keyLow == 0 && rig.song.channels[2].keyHigh == 127 && rig.song.channels[2].misc.rootNote == 60
  162. && ! rig.song.channels[2].misc.addToKey);
  163. }
  164. TEST (layer164_split_keeps_a_samplers_pitch_on_its_key)
  165. {
  166. lynx::Song s;
  167. lynx::ChannelDef l, smp;
  168. l.instrument = lynx::InstrumentKind::layer;
  169. l.misc.rootNote = 48;
  170. smp.instrument = lynx::InstrumentKind::sampler;
  171. s.channels = { l, smp, smp };
  172. lynx::layer::setChildren (s, 0, { 2, 1, 0, 1, 7 });
  173. CHECK (s.channels[0].layer.children == std::vector<int> ({ 1, 2 }));
  174. lynx::layer::splitChildren (s, 0);
  175. // key 49 plays the second sampler at its own pitch: root 49 → pitch +11 semitones over the C5 default
  176. CHECK (s.channels[2].misc.rootNote == 49 && std::abs (s.channels[2].sampler.pitch - 11.0f) < 1e-6f);
  177. lynx::layer::resetChildren (s, 0);
  178. CHECK (s.channels[2].misc.rootNote == 60 && std::abs (s.channels[2].sampler.pitch) < 1e-6f);
  179. }
  180. TEST (layer164_children_follow_channel_edits)
  181. {
  182. lynx::Song s;
  183. lynx::ChannelDef l, c;
  184. l.instrument = lynx::InstrumentKind::layer;
  185. s.channels = { c, l, c, c, c };
  186. s.channels[1].layer.children = { 0, 2, 4 };
  187. s.removeChannel (2);
  188. CHECK (s.channels[1].layer.children == std::vector<int> ({ 0, 3 }));
  189. CHECK (lynx::layer::parentsOf (s, 3) == std::vector<int> { 1 });
  190. lynx::permuteChannels (s, { 3, 0, 1, 2 }); // channel 3 first
  191. CHECK (s.channels[2].instrument == lynx::InstrumentKind::layer);
  192. CHECK (s.channels[2].layer.children == std::vector<int> ({ 1, 0 }));
  193. }
  194. // ===== round 164: X-Y / X-Y-Z Controller, Control Surface, Dashboard =====
  195. TEST (xy164_shape_and_output)
  196. {
  197. // straight at 0.5, FL's tension bends it either way and keeps the ends
  198. CHECK (std::abs (lynx::xy::shape (0.3, 0.5f) - 0.3) < 1e-9);
  199. for (const auto t : { 0.0f, 0.25f, 0.75f, 1.0f })
  200. CHECK (std::abs (lynx::xy::shape (0.0, t)) < 1e-9 && std::abs (lynx::xy::shape (1.0, t) - 1.0) < 1e-9);
  201. CHECK (lynx::xy::shape (0.5, 1.0f) > 0.5 && lynx::xy::shape (0.5, 0.0f) < 0.5);
  202. auto p = lynx::xy::defaults (lynx::EffectKind::xyController);
  203. p[(std::size_t) lynx::xy::minimum (1)] = 0.2f;
  204. p[(std::size_t) lynx::xy::maximum (1)] = 0.6f;
  205. CHECK (std::abs (lynx::xy::output (lynx::EffectKind::xyController, p, 1, 0.5) - 0.4f) < 1e-6f);
  206. // min above max inverts the axis
  207. p[(std::size_t) lynx::xy::minimum (1)] = 1.0f;
  208. p[(std::size_t) lynx::xy::maximum (1)] = 0.0f;
  209. CHECK (std::abs (lynx::xy::output (lynx::EffectKind::xyController, p, 1, 0.25) - 0.75f) < 1e-6f);
  210. }
  211. TEST (xy164_effect_follows_goal)
  212. {
  213. for (const auto kind : { lynx::EffectKind::xyController, lynx::EffectKind::xyzController })
  214. {
  215. auto slot = lynx::makeEffectSlot (kind);
  216. CHECK ((int) lynx::effectInfo (kind).params.size() == lynx::xy::paramCount (kind));
  217. CHECK (slot.params[(std::size_t) lynx::xy::level (0)] == 0.5f);
  218. auto fx = lynx::makeEffect (slot);
  219. fx->prepare (44100.0);
  220. std::vector<float> l (512, 0.25f), r (512, -0.25f);
  221. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  222. CHECK (l[0] == 0.25f && r[0] == -0.25f); // no change to the sound
  223. CHECK (std::abs (fx->controlOutput (0) - 0.5f) < 1e-6f && fx->controlOutput (7) == 1.0f);
  224. // the goal jumps: the target moves there smoothly (never past it) and arrives
  225. slot.params[(std::size_t) lynx::xy::level (0)] = 1.0f;
  226. float last = 0.5f;
  227. int blocks = 0;
  228. bool monotonic = true;
  229. while (fx->controlOutput (4) < 1.0f && blocks < 2000)
  230. {
  231. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  232. monotonic = monotonic && fx->controlOutput (4) >= last && fx->controlOutput (4) <= 1.0f;
  233. last = fx->controlOutput (4);
  234. ++blocks;
  235. }
  236. CHECK (monotonic && blocks > 2 && blocks < 2000);
  237. CHECK (std::abs (fx->controlOutput (0) - 1.0f) < 1e-6f);
  238. CHECK (std::abs (fx->controlOutput (1) - 0.5f) < 1e-6f); // Y untouched
  239. }
  240. // faster speed arrives sooner
  241. auto time = [] (float speed) {
  242. auto slot = lynx::makeEffectSlot (lynx::EffectKind::xyController);
  243. slot.params[(std::size_t) lynx::xy::speed (slot.kind)] = speed;
  244. auto fx = lynx::makeEffect (slot);
  245. fx->prepare (44100.0);
  246. std::vector<float> l (256), r (256);
  247. fx->process (l.data(), r.data(), 256, slot.params, 120.0);
  248. slot.params[0] = 0.0f;
  249. int blocks = 0;
  250. while (fx->controlOutput (4) > 0.0f && blocks < 100000)
  251. fx->process (l.data(), r.data(), 256, slot.params, 120.0), ++blocks;
  252. return blocks;
  253. };
  254. CHECK (time (0.9f) < time (0.2f));
  255. }
  256. TEST (xy164_fl_state)
  257. {
  258. // FL's X-Y state: version, then X level/min/max/tension, Y …, speed 1024…2048, accel 256…1024
  259. std::vector<std::uint8_t> d (44, 0);
  260. const std::int32_t values[] = { 1, 16384, 0, 65536, 64, 65536, 6553, 58982, 0, 1536, 640 };
  261. std::memcpy (d.data(), values, sizeof (values));
  262. const auto slot = lynx::xy::fromFl ("Fruity X-Y Controller", d);
  263. CHECK (slot.has_value());
  264. if (! slot)
  265. return;
  266. CHECK (slot->kind == lynx::EffectKind::xyController);
  267. CHECK (std::abs (slot->params[0] - 0.25f) < 1e-4f && std::abs (slot->params[3] - 0.75f) < 1e-4f);
  268. CHECK (std::abs (slot->params[4] - 1.0f) < 1e-4f && std::abs (slot->params[5] - 0.1f) < 1e-3f);
  269. CHECK (std::abs (slot->params[8] - 0.5f) < 1e-4f && std::abs (slot->params[9] - 0.5f) < 1e-4f);
  270. // the output equals the constant value the old import applied (core/FlEffects.h controllerConstantOutput)
  271. for (int o = 0; o < 2; ++o)
  272. {
  273. const auto constant = lynx::controllerConstantOutput ("Fruity X-Y Controller", d, o);
  274. CHECK (constant && std::abs (*constant - lynx::xy::output (slot->kind, slot->params, o, slot->params[(std::size_t) lynx::xy::level (o)])) < 1e-3f);
  275. }
  276. }
  277. TEST (surface164_outputs_and_params)
  278. {
  279. auto slot = lynx::makeEffectSlot (lynx::EffectKind::controlSurface);
  280. lynx::SurfaceControl knob, pad, button;
  281. knob.name = "Cutoff";
  282. knob.value = 0.3f;
  283. pad.kind = lynx::SurfaceControl::Kind::xy;
  284. pad.name = "Pad";
  285. pad.value = 0.1f;
  286. pad.valueY = 0.9f;
  287. button.kind = lynx::SurfaceControl::Kind::button;
  288. button.name = "Go";
  289. CHECK (lynx::surface::add (slot, knob) == 0 && lynx::surface::add (slot, pad) == 1 && lynx::surface::add (slot, button) == 2);
  290. CHECK (lynx::surface::firstOutput (slot.patcher.surface, 2) == 3);
  291. CHECK (lynx::surface::outputName (slot.patcher.surface, 2) == "Pad Y" && lynx::surface::outputName (slot.patcher.surface, 3) == "Go");
  292. CHECK (slot.params[0] == 0.3f && slot.params[1] == 0.1f && slot.params[2] == 0.9f);
  293. lynx::surface::setValue (slot, 2, 1.0f, 0.0f);
  294. CHECK (slot.params[3] == 1.0f);
  295. lynx::surface::remove (slot, 0); // the others move up
  296. CHECK (slot.params[0] == 0.1f && slot.params[1] == 0.9f && slot.params[2] == 1.0f);
  297. auto fx = lynx::makeEffect (slot);
  298. fx->prepare (44100.0);
  299. std::vector<float> l (64), r (64);
  300. fx->process (l.data(), r.data(), 64, slot.params, 120.0);
  301. CHECK (fx->controlOutput (1) == 0.9f);
  302. // its Patcher node publishes one output per value
  303. lynx::PatchNode node;
  304. node.effect = slot;
  305. CHECK (lynx::patcher::nodePorts (node).controlOuts == 3);
  306. // a Dashboard channel: the same from the channel's controls
  307. lynx::ChannelDef dash;
  308. dash.instrument = lynx::InstrumentKind::dashboard;
  309. dash.patcher.surface = slot.patcher.surface;
  310. auto instrument = lynx::makeInstrument (lynx::InstrumentKind::dashboard, nullptr);
  311. instrument->configure (dash, 120.0);
  312. CHECK (instrument->controlOutput (2) == 1.0f && instrument->controlOutput (0) == 0.1f);
  313. }
  314. TEST (surface164_fl_demos)
  315. {
  316. if (realFlpDir.empty())
  317. return;
  318. const auto path = std::filesystem::path (realFlpDir) / "Templates" / "Advanced" / "Electronic" / "Trap.flp";
  319. if (! std::filesystem::exists (path))
  320. return;
  321. std::ifstream in (path, std::ios::binary);
  322. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  323. lynx::FlpImportReport report;
  324. lynx::FlpImportOptions options;
  325. options.report = &report;
  326. const auto song = lynx::importFlp (flp::parseProject (file), options);
  327. int surfaces = 0, controls = 0, links = 0;
  328. for (const auto& track : song.mixer)
  329. for (const auto& fx : track.effects)
  330. if (fx.kind == lynx::EffectKind::controlSurface)
  331. ++surfaces, controls += (int) fx.patcher.surface.size();
  332. for (const auto& a : song.automations)
  333. if (a.isLive() && a.source.channel < 0 && a.source.track >= 0
  334. && song.mixer[(std::size_t) a.source.track].effects[(std::size_t) a.source.slot].kind == lynx::EffectKind::controlSurface)
  335. ++links;
  336. std::printf (" Trap template: %d Control Surface(s), %d controls, %d live links\n", surfaces, controls, links);
  337. CHECK (surfaces >= 1 && controls >= 3 && links >= 1);
  338. }
  339. TEST (layer164_fl_flags_and_pitch)
  340. {
  341. lynx::LayerSettings l;
  342. lynx::applyFlLayerFlags (1, l);
  343. CHECK (l.random && ! l.crossfade && ! l.sequential);
  344. lynx::applyFlLayerFlags (2 | 4, l);
  345. CHECK (! l.random && l.crossfade && l.sequential);
  346. lynx::applyFlLayerFlags (1 | 4, l); // (FL checks Random first)
  347. CHECK (l.random && ! l.sequential);
  348. lynx::ChannelDef layer;
  349. layer.misc.rootNote = 56; // RawFL's layer: its children play 4 semitones up
  350. CHECK (std::abs (lynx::layer::pitchCents (layer) - 400.0f) < 1e-3f);
  351. layer.sampler.pitch = -1.0f;
  352. CHECK (std::abs (lynx::layer::pitchCents (layer) - 300.0f) < 1e-3f);
  353. }
  354. namespace volt164
  355. {
  356. // FL's state of the Voltage Controller in the render test (REA_rodada164a §6): version 2, on, main L = pitch,
  357. // main R = gate × 0.5 (scale 49152), clock 24 PPQ, pulses of 15 samples
  358. const std::vector<std::uint8_t> flState {
  359. 0x02,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,
  360. 0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0xc0,0x00,0x00,0x00,0x00,0x01,0x00,
  361. 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x03,0x05,
  362. 0x06,0x07,0x09,0x09,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  363. 0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x3f };
  364. struct Rig
  365. {
  366. std::unique_ptr<lynx::Instrument> instrument = lynx::voltage::makeInstrument();
  367. std::vector<float> left, right;
  368. explicit Rig (const lynx::voltage::Settings& s, double bpm = 120.0)
  369. {
  370. lynx::ChannelDef c;
  371. c.instrument = lynx::InstrumentKind::voltage;
  372. c.pluginState = lynx::voltage::write (s);
  373. instrument->prepare (44100.0);
  374. instrument->configure (c, bpm);
  375. }
  376. void run (int n, double ticks = 0.0)
  377. {
  378. left.assign ((std::size_t) n, 0.0f);
  379. right.assign ((std::size_t) n, 0.0f);
  380. instrument->setSongPosition (ticks);
  381. instrument->beginChunk (n);
  382. instrument->render (left.data(), right.data(), n, 1.0f, 1.0f);
  383. }
  384. };
  385. } // namespace volt164
  386. TEST (voltage164_fl_state)
  387. {
  388. const auto s = lynx::voltage::parse (volt164::flState);
  389. CHECK (s.has_value());
  390. if (! s)
  391. return;
  392. CHECK (s->enabled && s->range == 0 && s->pitchStandard == 0 && s->clockPpq == 24 && s->pulseLength == 15);
  393. CHECK (s->source[0] == lynx::voltage::Source::pitch && s->source[1] == lynx::voltage::Source::gate);
  394. CHECK (s->source[2] == lynx::voltage::Source::velocity && s->source[7] == lynx::voltage::Source::none);
  395. CHECK (s->params[9] == 49152 && s->params[8] == 65536 && s->params[0] == 32768);
  396. CHECK (std::abs (lynx::voltage::scaleOf (*s, 1) - 0.5f) < 1e-6f && std::abs (lynx::voltage::offsetOf (*s, 0)) < 1e-6f);
  397. // LYNX writes version 3, which reads back the same
  398. const auto again = lynx::voltage::parse (lynx::voltage::write (*s));
  399. CHECK (again.has_value() && *again == *s);
  400. CHECK (! lynx::voltage::parse ({ 0x07, 0x00 }).has_value());
  401. }
  402. TEST (voltage164_matches_fl_render)
  403. {
  404. // FL's render of the test project: C0, C1 … C10 one after the other, then C5 — main L steps −0.5 … +0.5 by 0.1
  405. // (1 V / octave at 10 V full scale) and holds after the note ends; main R is the gate × 0.5
  406. volt164::Rig rig (*lynx::voltage::parse (volt164::flState));
  407. for (int octave = 0; octave <= 10; ++octave)
  408. {
  409. rig.instrument->noteOn (12 * octave, 0.8f);
  410. rig.run (64);
  411. CHECK (std::abs (rig.left[63] - (-0.5f + 0.1f * (float) octave)) < 1e-5f);
  412. CHECK (std::abs (rig.right[63] - 0.5f) < 1e-6f);
  413. rig.instrument->noteOff (12 * octave);
  414. rig.run (64);
  415. CHECK (std::abs (rig.left[63] - (-0.5f + 0.1f * (float) octave)) < 1e-5f); // (pitch holds)
  416. CHECK (rig.right[63] == 0.0f);
  417. }
  418. rig.instrument->noteOn (60, 0.8f);
  419. rig.run (16);
  420. CHECK (std::abs (rig.left[15]) < 1e-6f);
  421. // the channel's volume does not scale the voltages (FL's test channel was at 78 %); muted = silent
  422. rig.instrument->noteOn (72, 0.8f);
  423. std::vector<float> l (16, 0.0f), r (16, 0.0f);
  424. rig.instrument->beginChunk (16);
  425. rig.instrument->render (l.data(), r.data(), 16, 0.4f, 0.3f);
  426. CHECK (std::abs (l[15] - 0.1f) < 1e-5f && std::abs (r[15] - 0.5f) < 1e-6f);
  427. std::fill (l.begin(), l.end(), 0.0f);
  428. rig.instrument->beginChunk (16);
  429. rig.instrument->render (l.data(), r.data(), 16, 0.0f, 0.0f);
  430. CHECK (l[15] == 0.0f);
  431. }
  432. TEST (voltage164_pulses_ranges_and_outputs)
  433. {
  434. using namespace lynx::voltage;
  435. Settings s;
  436. s.enabled = true;
  437. s.source = { Source::trigger, Source::clock, Source::velocity, Source::none, Source::none, Source::none, Source::none, Source::none };
  438. s.pulseLength = 15;
  439. s.clockPpq = 4;
  440. s.tracks = { 3, 0, 0 };
  441. volt164::Rig rig (s);
  442. // a trigger: one pulse of 15 samples at the note's start
  443. rig.instrument->noteOn (60, 0.5f);
  444. rig.run (100);
  445. int high = 0;
  446. for (const auto v : rig.left)
  447. high += v > 0.5f ? 1 : 0;
  448. CHECK (high == 15 && rig.left[0] > 0.5f && rig.left[15] < 0.5f);
  449. // velocity on Out 2 L, fed to insert 3 (the rack adds that output there)
  450. CHECK (rig.instrument->auxOutputCount() == 3 && rig.instrument->auxOutputTrack (0) == 3 && rig.instrument->auxOutputTrack (1) == -1);
  451. CHECK (std::abs (rig.instrument->auxOutputData (0, 0)[50] - 0.5f) < 1e-6f);
  452. // the clock: 4 pulses per quarter note while the song plays (120 BPM: 22050 samples per beat → 4 pulses)
  453. int pulses = 0;
  454. double ticks = 960.0;
  455. for (int chunk = 0; chunk < 44; ++chunk) // 44 × 512 ≈ one beat past the first chunk
  456. {
  457. rig.run (512, ticks);
  458. for (int i = 0; i < 512; ++i)
  459. pulses += rig.right[(std::size_t) i] > 0.5f && (i == 0 ? false : rig.right[(std::size_t) i - 1] <= 0.5f) ? 1 : 0;
  460. ticks += 512.0 * 960.0 * 120.0 / (60.0 * 44100.0);
  461. }
  462. CHECK (pulses >= 3 && pulses <= 5);
  463. // the ranges (full scale 1.0 = 10 V)
  464. CHECK (applyRange (0, 1.5f) == 1.0f && applyRange (1, 1.0f) == 0.5f && applyRange (2, -1.0f) == 0.0f && applyRange (2, 1.0f) == 1.0f);
  465. // off (FL's default for a new one): silence and idle
  466. volt164::Rig off (Settings {});
  467. off.instrument->noteOn (72, 1.0f);
  468. off.run (32);
  469. CHECK (off.instrument->idle() && off.left[31] == 0.0f && off.right[31] == 0.0f);
  470. }