Issue14bugsTests.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Round 166a, issue #14 (kubapl2004gmail, Linux): the three bugs — snap to scale moves every note, the Piano roll's
  2. // keyboard lights the sounding keys, master / channel pitch on every instrument (REA_rodada166a §2-§4)
  3. TEST (issue14_snap_to_scale_moves_every_note_onto_the_scale)
  4. {
  5. using namespace lynx;
  6. int major = 0;
  7. for (int i = 0; i < (int) notetools::scales().size(); ++i)
  8. if (std::string (notetools::scales()[(std::size_t) i].name) == "Major")
  9. major = i;
  10. CHECK (major > 0);
  11. Pattern p;
  12. for (const auto key : { 60, 64, 67 }) // C E G, C major
  13. {
  14. Note n;
  15. n.key = key;
  16. p.notes.push_back (n);
  17. }
  18. const auto originals = p.notes;
  19. // the mouse moved two semitones up: each note goes to the scale near its own key + 2 (F# → F: a tie goes down)
  20. notetools::moveKeysOnScale (p, { 0, 1, 2 }, originals, 2, 0, major);
  21. CHECK (p.notes[0].key == 62 && p.notes[1].key == 65 && p.notes[2].key == 69);
  22. for (const auto& n : p.notes)
  23. CHECK (notetools::inScale (n.key, 0, major));
  24. notetools::moveKeysOnScale (p, { 0, 1, 2 }, originals, 0, 0, major); // no vertical move: left alone
  25. CHECK (p.notes[0].key == 62);
  26. // the keyboard's up / down: one scale step
  27. CHECK (notetools::stepOnScale (64, 1, 0, major) == 65 && notetools::stepOnScale (65, -1, 0, major) == 64);
  28. CHECK (notetools::stepOnScale (60, 1, 0, major) == 62 && notetools::stepOnScale (61, -1, 0, major) == 60);
  29. CHECK (notetools::stepOnScale (60, 1, 0, 0) == 61 && notetools::stepOnScale (127, 1, 0, major) == 127);
  30. }
  31. TEST (issue14_renderer_publishes_the_sounding_keys)
  32. {
  33. layer164::Rig rig (1); // channel 0 = a Layer, channel 1 = a plugin
  34. CHECK (rig.renderer.soundingKeys (1) == (lynx::NoteMask {}));
  35. rig.play (1, 60);
  36. rig.play (1, 100);
  37. auto k = rig.renderer.soundingKeys (1);
  38. CHECK ((k[0] >> 60 & 1u) == 1u && (k[1] >> (100 - 64) & 1u) == 1u);
  39. rig.play (1, 60, false);
  40. k = rig.renderer.soundingKeys (1);
  41. CHECK ((k[0] >> 60 & 1u) == 0u && (k[1] >> (100 - 64) & 1u) == 1u);
  42. CHECK (rig.renderer.soundingKeys (-1) == (lynx::NoteMask {}) && rig.renderer.soundingKeys (5000) == (lynx::NoteMask {}));
  43. }
  44. namespace issue14
  45. {
  46. // a pattern of one channel played by the renderer (Pattern mode, 120 BPM, 48 kHz): the output
  47. std::vector<float> renderPattern (const lynx::ChannelDef& channel, const std::vector<lynx::Note>& notes, float masterPitch, double seconds)
  48. {
  49. lynx::Song s;
  50. s.mixer = lynx::Song::defaultMixer (2);
  51. s.bpm = 120.0;
  52. s.masterPitch = masterPitch;
  53. auto c = channel;
  54. c.mixerTrack = 1;
  55. s.channels.push_back (c);
  56. s.patterns.emplace_back();
  57. s.patterns[0].notes = notes;
  58. s.patterns[0].lengthTicks = (std::int64_t) (lynx::ppq * 16);
  59. s.mode = lynx::PlayMode::pattern;
  60. auto samples = [] (const std::string& path) -> std::shared_ptr<const lynx::SampleData> {
  61. auto p = path;
  62. std::replace (p.begin(), p.end(), '\\', '/');
  63. return lynx::tools::readWav (lynx::existingPathIgnoringCase (p));
  64. };
  65. auto state = lynx::RackState::build (s, nullptr, 48000.0, samples);
  66. lynx::RackRenderer r;
  67. r.prepare (48000.0, 512);
  68. const auto n = (int) (seconds * 48000.0);
  69. std::vector<float> out;
  70. const auto tps = 120.0 / 60.0 * lynx::ppq / 48000.0;
  71. for (int done = 0; done < n; done += 512)
  72. {
  73. std::vector<float> l (512), rr (512);
  74. r.process (state.get(), true, done * tps, 512, l.data(), rr.data(), nullptr);
  75. out.insert (out.end(), l.begin(), l.end());
  76. }
  77. return out;
  78. }
  79. // the fundamental (Hz) of out[from, from + 4096) by normalised autocorrelation, 60…1200 Hz; 0 when silent
  80. double pitchAt (const std::vector<float>& x, double seconds)
  81. {
  82. const auto from = (std::size_t) (seconds * 48000.0);
  83. const std::size_t w = 4096;
  84. if (from + w + 1000 > x.size())
  85. return 0.0;
  86. double e = 0.0;
  87. for (std::size_t i = 0; i < w; ++i)
  88. e += (double) x[from + i] * x[from + i];
  89. if (e < 1e-6)
  90. return 0.0;
  91. std::vector<double> ac (801);
  92. for (int lag = 40; lag <= 800; ++lag)
  93. {
  94. double s = 0.0;
  95. for (std::size_t i = 0; i < w; ++i)
  96. s += (double) x[from + i] * x[from + i + (std::size_t) lag];
  97. ac[(std::size_t) lag] = s / e;
  98. }
  99. const auto best = *std::max_element (ac.begin() + 40, ac.end());
  100. for (int lag = 41; lag < 800; ++lag) // the first peak near the best (no octave-down picks)
  101. if (ac[(std::size_t) lag] > 0.9 * best && ac[(std::size_t) lag] >= ac[(std::size_t) lag - 1] && ac[(std::size_t) lag] >= ac[(std::size_t) lag + 1])
  102. {
  103. const auto a = ac[(std::size_t) lag - 1], b = ac[(std::size_t) lag], c = ac[(std::size_t) lag + 1];
  104. const auto d = a - 2.0 * b + c;
  105. return 48000.0 / (lag + (d != 0.0 ? 0.5 * (a - c) / d : 0.0));
  106. }
  107. return 0.0;
  108. }
  109. double cents (double a, double b) { return a > 0.0 && b > 0.0 ? 1200.0 * std::log2 (b / a) : 9999.0; }
  110. } // namespace issue14
  111. // An audit of every instrument engine (prints a table): the master pitch, the channel's PITCH knob and slide notes
  112. // Every instrument follows the main pitch, its channel's PITCH knob and slide notes (issue #14 C). Needs FL's data
  113. // (the FL engines' factory presets); LYNX_PITCH_AUDIT=1 also prints the table.
  114. TEST (issue14_pitch_audit_of_the_instruments)
  115. {
  116. using namespace lynx;
  117. if (realFlpDir.empty())
  118. return;
  119. const auto print = std::getenv ("LYNX_PITCH_AUDIT") != nullptr;
  120. // the pitch of a guitar / string model and of drums can't be read by the autocorrelation estimate (Sakura: C4 reads as
  121. // 870 Hz and G4 lower than C4; Ogun's held C4 reads 2 octaves apart at 0.3 s and 1.0 s, its slide +726…+740 cents at
  122. // 0.6…0.65 s with LYNX_PITCH_TRACE; Drumpad doesn't follow the key)
  123. // Wave Traveller plays the channel's sample (none here: silent, as FL) and its pitch changes the curve's speed (REA_rodada167f)
  124. // Fruity Slicer (round 180c) plays its own sample's slices: a new one has none, silent as FL
  125. // Slicex (in the list since round 190r) likewise: a new one has empty decks, and its keys pick regions, not pitches
  126. const std::set<std::string> unreadable { "FL Slayer", "Fruity Granulizer", "Wave Traveller", "Fruity Slicer", "Slicex", "Drumaxx", "Fruit Kick", "BassDrum", "Drumpad", "FPC", "Sakura", "Ogun", "DirectWave" }; // (round 190hz: a new DirectWave has no zones, silent as FL's)
  127. // Plucked! reads Final.Pitch only when a voice starts (REA_rodada65 §1.2: it sizes the delay line): FL can't glide it
  128. // (rollup 0.1.31: LYNX Model, a struck bar / tine model, follows neither slides nor the channel porta: the rack retriggers it, as Plucked!)
  129. const std::set<std::string> noSlide { "Plucked!", "LYNX Model" };
  130. const auto fl = natives142::flAccess();
  131. std::vector<std::pair<std::string, ChannelDef>> list;
  132. {
  133. ChannelDef synth;
  134. synth.name = "LYNX Synth";
  135. synth.instrument = InstrumentKind::synth;
  136. list.emplace_back ("LYNX Synth", synth);
  137. }
  138. // (round 169l2: the Default preset the audit was written with, not the Plugin database patch of a new channel)
  139. for (const auto& e : natives::instrumentEngines())
  140. if (const auto c = natives142::writtenWith (e.kind, fl))
  141. list.emplace_back (e.name, *c);
  142. Note held;
  143. held.key = 60;
  144. held.length = ppq * 8;
  145. held.velocity = 0.8f;
  146. Note slide;
  147. slide.key = 67;
  148. slide.start = ppq;
  149. slide.length = ppq / 4;
  150. slide.slide = true;
  151. if (print)
  152. std::printf (" %-28s %9s %9s %9s %9s\n", "engine", "base Hz", "master", "pitch", "slide");
  153. for (const auto& [name, c] : list)
  154. {
  155. const auto base = issue14::pitchAt (issue14::renderPattern (c, { held }, 0.0f, 1.5), 1.0);
  156. auto fifth = held; // the reference: the same note played 7 semitones up (the engine's own timbre at that pitch)
  157. fifth.key = 67;
  158. const auto ref = issue14::pitchAt (issue14::renderPattern (c, { fifth }, 0.0f, 1.5), 1.0);
  159. const auto master = issue14::pitchAt (issue14::renderPattern (c, { held }, 700.0f, 1.5), 1.0);
  160. auto bent = c;
  161. bent.misc.pitchRange = 12;
  162. bent.sampler.pitch += 7.0f;
  163. const auto knob = issue14::pitchAt (issue14::renderPattern (bent, { held }, 0.0f, 1.5), 1.0);
  164. const auto slidOut = issue14::renderPattern (c, { held, slide }, 0.0f, 1.5);
  165. const auto slid = issue14::pitchAt (slidOut, 1.0);
  166. if (print && std::getenv ("LYNX_PITCH_TRACE") != nullptr)
  167. for (const auto t : { 0.3, 0.45, 0.55, 0.6, 0.65, 0.7, 0.8, 0.9, 1.0 })
  168. std::printf (" %s slide t=%.2f %+.0f cents\n", name.c_str(), t, issue14::cents (base, issue14::pitchAt (slidOut, t)));
  169. if (print)
  170. std::printf (" %-28s %9.1f %+9.0f %+9.0f %+9.0f\n", name.c_str(), base, issue14::cents (base, master), issue14::cents (base, knob),
  171. issue14::cents (base, slid));
  172. if (unreadable.count (name) == 0)
  173. {
  174. // within 40 cents of the key played 7 semitones up, or (an engine whose key doesn't move its pitch, like a
  175. // Transistor Bass preset running its own sequence) of the base pitch + 700 cents
  176. const auto same = [ref, base] (double hz) {
  177. return std::abs (issue14::cents (ref, hz)) < 40.0 || std::abs (issue14::cents (base, hz) - 700.0) < 40.0;
  178. };
  179. if (! same (master) || ! same (knob) || (noSlide.count (name) == 0 && ! same (slid)))
  180. std::printf (" pitch: %s doesn't follow (key +7 %+.0f; master %+.0f, knob %+.0f, slide %+.0f cents)\n", name.c_str(),
  181. issue14::cents (base, ref), issue14::cents (base, master), issue14::cents (base, knob), issue14::cents (base, slid));
  182. CHECK (same (master));
  183. CHECK (same (knob));
  184. CHECK (noSlide.count (name) != 0 || same (slid));
  185. }
  186. }
  187. }
  188. namespace issue14
  189. {
  190. // a hosted "plugin" that records the live pitch the renderer gives it and the slides it gets
  191. struct PitchSpy final : lynx::Instrument
  192. {
  193. float live = -99999.0f;
  194. int slides = 0, slideKey = -1;
  195. void prepare (double) noexcept override {}
  196. void noteOn (int, float) noexcept override {}
  197. void noteOff (int) noexcept override {}
  198. void allNotesOff() noexcept override {}
  199. void render (float*, float*, int, float, float) noexcept override {}
  200. void setLivePitch (float cents) noexcept override { live = cents; }
  201. void slideTo (int key, float, float) noexcept override
  202. {
  203. ++slides;
  204. slideKey = key;
  205. }
  206. };
  207. } // namespace issue14
  208. TEST (issue14_plugin_channels_get_the_pitch_knob_main_pitch_and_slides)
  209. {
  210. using namespace lynx;
  211. Song song;
  212. song.mixer = Song::defaultMixer (2);
  213. song.masterPitch = 50.0f;
  214. ChannelDef c;
  215. c.instrument = InstrumentKind::plugin;
  216. c.plugin.id = "spy";
  217. c.sampler.pitch = 3.0f;
  218. c.misc.fineTune = 10.0f;
  219. song.channels.push_back (c);
  220. song.patterns.emplace_back();
  221. auto spy = std::make_shared<issue14::PitchSpy>();
  222. RackState::Factories f;
  223. f.pluginInstruments = [spy] (const PluginRef&, double) { return std::shared_ptr<Instrument> (spy); };
  224. auto state = RackState::build (song, nullptr, 44100.0, f);
  225. RackRenderer renderer;
  226. renderer.prepare (44100.0, 256);
  227. LiveEventQueue live;
  228. auto play = [&] (NoteEvent::Type type, int key) {
  229. NoteEvent e;
  230. e.type = type;
  231. e.channel = 0;
  232. e.key = key;
  233. e.velocity = 0.8f;
  234. e.slideSamples = 1000.0f;
  235. live.push (e);
  236. std::vector<float> l (256), r (256);
  237. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live, nullptr);
  238. };
  239. play (NoteEvent::Type::on, 60);
  240. CHECK (std::abs (spy->live - 360.0f) < 0.01f); // 3 semitones + 10 cents fine + 50 cents main pitch
  241. // "Main pitch" off on the channel: the toolbar knob leaves it alone
  242. song.channels[0].misc.mainPitch = false;
  243. state = RackState::build (song, state.get(), 44100.0, f);
  244. play (NoteEvent::Type::off, 60);
  245. CHECK (std::abs (spy->live - 310.0f) < 0.01f);
  246. // a slide note of the pattern reaches the plugin (PluginInstrument turns it into a pitch-bend glide)
  247. Note on;
  248. on.key = 60;
  249. on.length = ppq * 2;
  250. Note slide;
  251. slide.key = 64;
  252. slide.start = ppq / 2;
  253. slide.length = ppq / 4;
  254. slide.slide = true;
  255. song.patterns[0].notes = { on, slide };
  256. song.patterns[0].lengthTicks = (std::int64_t) (ppq * 4);
  257. song.mode = PlayMode::pattern;
  258. state = RackState::build (song, state.get(), 44100.0, f);
  259. const auto tps = song.bpm / 60.0 * ppq / 44100.0;
  260. std::vector<float> l (256), r (256);
  261. for (int done = 0; done < 44100; done += 256)
  262. renderer.process (state.get(), true, done * tps, 256, l.data(), r.data(), nullptr);
  263. CHECK (spy->slides == 1 && spy->slideKey == 64);
  264. }