Issue81Tests.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Round 174b (issue #81, Francisc0422): "When moving notes up and down pitches while a pattern is playing it makes
  2. // 'ghost' notes that are sustained until the end of the pattern". Video (0.1.23): the default "Synth" channel, its
  3. // 16 notes all selected in the Piano roll and moved up / down with the keyboard while the song plays; the Piano
  4. // roll's keyboard shows more and more keys lit (voices whose note off never came). FL 2026 (r174b, 3x Osc, a C5
  5. // note of 3 beats, Pattern mode playing): a sounding note moved to another key (Shift+Up, or dragged) keeps its old
  6. // voice to the note's original end; the old key goes dark there; the new key plays from the next pass. LYNX 0.1.23
  7. // emitted each note off for the note's key as it is now, so the old key never got one. Round 169d's ScheduledNotes
  8. // (0.1.24) ends each started note where it was going to; these tests pin the issue's gestures.
  9. // Uses i32d::Rig (Issue32dTests.inc).
  10. namespace i81
  11. {
  12. const std::int64_t bar = 4 * lynx::ppq;
  13. // the video's melody: 16 sixteenths (staccato: an eighth of a beat each, so every sixteenth ends with a gap)
  14. inline void addMelody (lynx::Pattern& p)
  15. {
  16. const int keys[16] = { 69, 72, 74, 76, 79, 81, 79, 76, 74, 72, 69, 67, 64, 67, 69, 72 };
  17. for (int i = 0; i < 16; ++i)
  18. p.notes.push_back (i32d::Rig::note (i * lynx::ppq / 4, lynx::ppq / 8, keys[i]));
  19. }
  20. inline void transposeAll (lynx::Pattern& p, int keys)
  21. {
  22. for (auto& n : p.notes)
  23. n.key += keys;
  24. }
  25. inline void playTo (i32d::Rig& rig, double beat)
  26. {
  27. while (rig.tick < beat * lynx::ppq)
  28. {
  29. rig.renderer.process (rig.state.get(), true, rig.tick, 256, rig.l.data(), rig.r.data());
  30. rig.tick += 256 * rig.tps;
  31. }
  32. }
  33. inline int soundingTotal (const i32d::Rig& rig)
  34. {
  35. int total = 0;
  36. for (auto s : rig.spy->sounding)
  37. total += s;
  38. return total;
  39. }
  40. // plays to beat `to` and checks, in the middle of every sixteenth's gap, that nothing sounds
  41. inline int ghostsUpTo (i32d::Rig& rig, double from, double to)
  42. {
  43. int worst = 0;
  44. for (double gap = std::floor (from * 4.0) / 4.0 + 0.1875; gap < to; gap += 0.25)
  45. if (gap > from)
  46. {
  47. playTo (rig, gap);
  48. worst = std::max (worst, soundingTotal (rig));
  49. }
  50. playTo (rig, to);
  51. return worst;
  52. }
  53. } // namespace i81
  54. TEST (issue81_moving_the_whole_melody_up_and_down_while_the_song_plays_leaves_no_ghost_notes)
  55. {
  56. // the video: Song mode, the pattern in a Playlist clip, all its notes moved by a semitone at a time while they play
  57. i32d::Rig rig;
  58. i81::addMelody (rig.song.patterns[0]);
  59. lynx::Clip clip;
  60. clip.pattern = 0;
  61. clip.track = 1;
  62. clip.start = 0;
  63. clip.length = i81::bar;
  64. rig.song.playlist.push_back (clip);
  65. rig.song.mode = lynx::PlayMode::song;
  66. rig.rebuild();
  67. // each move lands inside a sounding sixteenth (1/32 beat after it starts), up and down as in the video
  68. const int moves[] = { +1, +1, +1, -1, +1, -1, -1, -1, +2, -2, +1, +1 };
  69. double at = 0.53;
  70. int worst = 0;
  71. for (const auto keys : moves)
  72. {
  73. worst = std::max (worst, i81::ghostsUpTo (rig, rig.tick / lynx::ppq, at));
  74. I32D_REQUIRE (i81::soundingTotal (rig) == 1); // a note sounds while it is moved
  75. i81::transposeAll (rig.song.patterns[0], keys);
  76. rig.rebuild();
  77. at += 0.75; // (a different sixteenth each time, across the loop of the 1-bar song)
  78. }
  79. worst = std::max (worst, i81::ghostsUpTo (rig, rig.tick / lynx::ppq, std::floor (at) + 8.1875)); // two more passes (to a gap)
  80. CHECK (worst == 0); // 0.1.23: the old keys hung (up to one per move)
  81. CHECK (rig.spy->allOffs == 0); // (nothing reset the instrument meanwhile)
  82. // every note on got exactly one note off, of its own key
  83. std::array<int, 128> balance {};
  84. for (const auto& e : rig.spy->log)
  85. balance[(std::size_t) e.key] += e.on ? 1 : -1;
  86. for (auto b : balance)
  87. CHECK (b == 0);
  88. CHECK (rig.renderer.scheduledNotesSounding() == 0);
  89. }
  90. TEST (issue81_a_sounding_note_moved_to_other_keys_keeps_its_voice_to_its_end)
  91. {
  92. // FL r174b b1–b7 / c1–c7: the old key stays lit until the note's original end, then goes dark; the new key plays
  93. // from the next pass; no note on for the new key meanwhile (the scheduler never restarts a note behind the play head)
  94. i32d::Rig rig;
  95. rig.song.patterns[0].notes.push_back (i32d::Rig::note (0, 3 * lynx::ppq, 72)); // C5, 3 beats of a 1-bar pattern
  96. rig.rebuild();
  97. i81::playTo (rig, 0.4);
  98. I32D_REQUIRE (rig.spy->sounding[72] == 1);
  99. for (const auto key : { 74, 73, 75, 72, 74 }) // up, down, up, back to C5, up to D5
  100. {
  101. rig.song.patterns[0].notes[0].key = key;
  102. rig.rebuild();
  103. i81::playTo (rig, rig.tick / lynx::ppq + 0.3);
  104. CHECK (rig.spy->sounding[72] == 1);
  105. CHECK (rig.spy->offs (72) == 0);
  106. }
  107. i81::playTo (rig, 3.1);
  108. CHECK (rig.spy->offs (72) == 1); // ended once, at its end
  109. CHECK (std::abs (rig.spy->lastOff (72) - rig.sampleOfBeat (3.0)) <= 3);
  110. int sounding = 0;
  111. for (auto s : rig.spy->sounding)
  112. sounding += s;
  113. CHECK (sounding == 0);
  114. int ons = 0;
  115. for (const auto& e : rig.spy->log)
  116. ons += e.on ? 1 : 0;
  117. CHECK (ons == 1); // the moved note waits for the next pass
  118. i81::playTo (rig, 4.5);
  119. CHECK (rig.spy->sounding[74] == 1 && rig.spy->sounding[72] == 0); // the next pass: D5
  120. i81::playTo (rig, 7.2);
  121. CHECK (rig.spy->sounding[74] == 0 && rig.spy->offs (74) == 1);
  122. }
  123. TEST (issue81_a_playlist_clip_moved_while_its_notes_sound_leaves_none_hanging)
  124. {
  125. // Playlist edits: the clip dragged to another track and later in the song while its note sounds; a second clip
  126. // of the same pattern stays. Each started note still ends where it was going to.
  127. i32d::Rig rig;
  128. rig.song.patterns[0].notes.push_back (i32d::Rig::note (0, 2 * lynx::ppq, 60));
  129. rig.song.patterns[0].notes.push_back (i32d::Rig::note (2 * lynx::ppq, lynx::ppq, 67));
  130. for (int i = 0; i < 2; ++i)
  131. {
  132. lynx::Clip clip;
  133. clip.pattern = 0;
  134. clip.track = i;
  135. clip.start = i * 2 * i81::bar;
  136. clip.length = i81::bar;
  137. rig.song.playlist.push_back (clip);
  138. }
  139. rig.song.mode = lynx::PlayMode::song;
  140. rig.rebuild();
  141. i81::playTo (rig, 1.0);
  142. I32D_REQUIRE (rig.spy->sounding[60] == 1);
  143. rig.song.playlist[0].track = 5;
  144. rig.song.playlist[0].start = i81::bar; // now starts at bar 2, after the play head
  145. rig.rebuild();
  146. i81::playTo (rig, 2.5);
  147. CHECK (rig.spy->offs (60) == 1);
  148. CHECK (std::abs (rig.spy->lastOff (60) - rig.sampleOfBeat (2.0)) <= 3);
  149. CHECK (rig.spy->sounding[60] == 0);
  150. i81::playTo (rig, 12.5); // the moved clip's pass (bar 2) and the other clip's (bar 3)
  151. CHECK (rig.spy->sounding[60] == 0 && rig.spy->sounding[67] == 0);
  152. CHECK (rig.spy->offs (60) == 3 && rig.spy->offs (67) == 2);
  153. CHECK (rig.renderer.scheduledNotesSounding() == 0);
  154. }
  155. TEST (issue81_render_the_native_synth_falls_silent_after_a_moved_note_s_end)
  156. {
  157. // the reporter's instrument (the default "Synth" channel), rendered: a long note moved up a fifth while it sounds.
  158. // The output is silent between the old note's end (+ its release) and the next pass; 0.1.23 kept the old key
  159. // sounding through it.
  160. lynx::Song song = lynx::Song::makeDefault();
  161. song.playlist.clear();
  162. song.mode = lynx::PlayMode::pattern;
  163. for (auto& p : song.patterns)
  164. p.notes.clear();
  165. song.currentPattern = 0;
  166. int synth = -1;
  167. for (std::size_t c = 0; c < song.channels.size(); ++c)
  168. if (song.channels[c].instrument == lynx::InstrumentKind::synth)
  169. synth = (int) c;
  170. I32D_REQUIRE (synth >= 0);
  171. auto n = i32d::Rig::note (0, 2 * lynx::ppq, 60);
  172. n.channel = synth;
  173. song.patterns[0].notes.push_back (n);
  174. const double rate = 48000.0;
  175. const auto tps = lynx::ticksPerSample (song.bpm, rate);
  176. lynx::RackRenderer renderer;
  177. renderer.prepare (rate, 256);
  178. renderer.setTransportOptions (true, false, 0);
  179. auto state = lynx::RackState::build (song, nullptr, rate);
  180. std::vector<float> l (256), r (256);
  181. double tick = 0.0;
  182. auto peakUntil = [&] (double beat) {
  183. float peak = 0.0f;
  184. while (tick < beat * lynx::ppq)
  185. {
  186. renderer.process (state.get(), true, tick, 256, l.data(), r.data());
  187. tick += 256 * tps;
  188. for (int i = 0; i < 256; ++i)
  189. peak = std::max ({ peak, std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]) });
  190. }
  191. return peak;
  192. };
  193. const auto playing = peakUntil (1.0);
  194. CHECK (playing > 0.01f);
  195. song.patterns[0].notes[0].key = 67; // moved while it sounds
  196. state = lynx::RackState::build (song, state.get(), rate);
  197. CHECK (peakUntil (1.9) > 0.01f); // the old voice plays on (FL)
  198. peakUntil (3.0); // its end at beat 2, then the release
  199. CHECK (peakUntil (3.7) < 1.0e-3f); // silent: no ghost
  200. CHECK (peakUntil (4.9) > 0.01f); // the next pass: the moved note
  201. }