Issue32dTests.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Round 169d (issue #32 follow-up 2, medievilmusic): docs/analise/REA_rodada169d.md
  2. #define I32D_REQUIRE(x) \
  3. do { if (! (x)) { CHECK (x); return; } } while (false)
  4. namespace i32d
  5. {
  6. // records each note on / off with the sample it reached the instrument at (the renderer's block clock)
  7. struct NoteSpy final : lynx::Instrument
  8. {
  9. struct Event
  10. {
  11. bool on;
  12. int key;
  13. long long sample;
  14. };
  15. std::vector<Event> log;
  16. long long rendered = 0, position = 0, chunk = 0;
  17. std::array<int, 128> sounding {};
  18. void prepare (double) noexcept override {}
  19. void noteOn (int key, float) noexcept override
  20. {
  21. log.push_back ({ true, key, rendered + position });
  22. ++sounding[(std::size_t) key];
  23. }
  24. void noteOff (int key) noexcept override
  25. {
  26. log.push_back ({ false, key, rendered + position });
  27. sounding[(std::size_t) key] = std::max (0, sounding[(std::size_t) key] - 1);
  28. }
  29. int allOffs = 0;
  30. void allNotesOff() noexcept override
  31. {
  32. sounding.fill (0);
  33. ++allOffs;
  34. }
  35. void render (float*, float*, int n, float, float) noexcept override { position += n; }
  36. bool idle() const noexcept override { return false; }
  37. void beginChunk (int n) noexcept override // (a chunk's notes split its render calls: position = within it)
  38. {
  39. rendered += chunk;
  40. chunk = n;
  41. position = 0;
  42. }
  43. int offs (int key) const
  44. {
  45. int c = 0;
  46. for (const auto& e : log)
  47. c += (! e.on && e.key == key) ? 1 : 0;
  48. return c;
  49. }
  50. long long lastOff (int key) const
  51. {
  52. long long at = -1;
  53. for (const auto& e : log)
  54. if (! e.on && e.key == key)
  55. at = e.sample;
  56. return at;
  57. }
  58. };
  59. struct Rig
  60. {
  61. lynx::Song song = lynx::Song::makeDefault();
  62. double rate = 48000.0, tps = 0.0, tick = 0.0;
  63. std::shared_ptr<NoteSpy> spy = std::make_shared<NoteSpy>();
  64. std::shared_ptr<const lynx::RackState> state;
  65. lynx::RackRenderer renderer;
  66. std::vector<float> l = std::vector<float> (256), r = std::vector<float> (256);
  67. Rig()
  68. {
  69. song.playlist.clear();
  70. song.mode = lynx::PlayMode::pattern;
  71. for (auto& p : song.patterns)
  72. p.notes.clear();
  73. song.currentPattern = 0;
  74. tps = lynx::ticksPerSample (song.bpm, rate);
  75. renderer.prepare (rate, 256);
  76. renderer.setTransportOptions (true, false, 0);
  77. }
  78. static lynx::Note note (std::int64_t start, std::int64_t length, int key)
  79. {
  80. lynx::Note n;
  81. n.start = start;
  82. n.length = length;
  83. n.key = key;
  84. n.channel = 1;
  85. return n;
  86. }
  87. void rebuild() // the song was edited: a new snapshot, as the engine publishes it (same instrument)
  88. {
  89. state = lynx::RackState::build (song, state.get(), rate);
  90. const_cast<lynx::RackState&> (*state).instruments[1] = spy;
  91. }
  92. void play (double beats)
  93. {
  94. const auto blocks = (int) std::ceil (beats * lynx::ppq / (256 * tps));
  95. for (int b = 0; b < blocks; ++b, tick += 256 * tps)
  96. renderer.process (state.get(), true, tick, 256, l.data(), r.data());
  97. }
  98. long long sampleOfBeat (double beat) const { return (long long) std::llround (beat * lynx::ppq / tps); }
  99. };
  100. } // namespace i32d
  101. TEST (issue32d_scheduled_notes_ledger_keeps_each_note_s_end)
  102. {
  103. lynx::ScheduledNotes ledger;
  104. const double tps = 0.01;
  105. lynx::EventBuffer ev;
  106. auto add = [&] (lynx::NoteEvent::Type type, int key, int offset, float length) {
  107. lynx::NoteEvent e;
  108. e.type = type;
  109. e.channel = 2;
  110. e.key = key;
  111. e.sampleOffset = offset;
  112. e.lengthTicks = length;
  113. ev.add (e);
  114. };
  115. // block 1 (100 samples = 1 tick): key 60 starts at sample 10, 3 ticks long (ends at sample 310)
  116. add (lynx::NoteEvent::Type::on, 60, 10, 3.0f);
  117. add (lynx::NoteEvent::Type::off, 61, 50, 0.0f); // nothing of key 61 sounds: dropped
  118. ledger.process (ev, 100, tps);
  119. I32D_REQUIRE (ev.size() == 1);
  120. CHECK (ev[0].type == lynx::NoteEvent::Type::on && ledger.sounding() == 1);
  121. // the note was deleted: no "off" comes from the scheduler. Blocks 2 and 3: nothing yet; block 4: its own off
  122. for (int b = 1; b < 3; ++b)
  123. {
  124. ev.clear();
  125. ledger.process (ev, 100, tps);
  126. CHECK (ev.size() == 0);
  127. }
  128. ev.clear();
  129. ledger.process (ev, 100, tps);
  130. I32D_REQUIRE (ev.size() == 1);
  131. CHECK (ev[0].type == lynx::NoteEvent::Type::off && ev[0].key == 60 && ev[0].channel == 2);
  132. CHECK (ev[0].sampleOffset == 12); // its end (sample 310) + the 2-sample allowance
  133. CHECK (ledger.sounding() == 0);
  134. // an "off" at a note's end ends it (and passes); one far from it (the note was edited) is dropped
  135. ev.clear();
  136. add (lynx::NoteEvent::Type::on, 64, 0, 0.5f); // ends at sample 50
  137. add (lynx::NoteEvent::Type::off, 64, 30, 0.0f);
  138. add (lynx::NoteEvent::Type::off, 64, 51, 0.0f);
  139. ledger.process (ev, 100, tps);
  140. I32D_REQUIRE (ev.size() == 2);
  141. CHECK (ev[1].type == lynx::NoteEvent::Type::off && ev[1].sampleOffset == 51);
  142. CHECK (ledger.sounding() == 0);
  143. // audio clips (keys from 128) and notes of unknown length pass as before
  144. ev.clear();
  145. add (lynx::NoteEvent::Type::on, lynx::audioClipKey (3), 0, 1.0f);
  146. add (lynx::NoteEvent::Type::on, 70, 0, 0.0f);
  147. add (lynx::NoteEvent::Type::off, 70, 90, 0.0f);
  148. add (lynx::NoteEvent::Type::off, lynx::audioClipKey (3), 95, 0.0f);
  149. ledger.process (ev, 100, tps);
  150. CHECK (ev.size() == 4 && ledger.sounding() == 0);
  151. // channels moved: the note goes with its channel; a removed channel's note is forgotten
  152. ev.clear();
  153. add (lynx::NoteEvent::Type::on, 40, 0, 50.0f);
  154. ledger.process (ev, 100, tps);
  155. ledger.moveChannels ({ 0, 2, 1 }); // old channel 2 is now 1
  156. ev.clear();
  157. for (int b = 0; b < 60 && ev.size() == 0; ++b)
  158. ledger.process (ev, 100, tps);
  159. I32D_REQUIRE (ev.size() == 1);
  160. CHECK (ev[0].channel == 1 && ev[0].key == 40);
  161. ev.clear();
  162. add (lynx::NoteEvent::Type::on, 41, 0, 50.0f);
  163. ledger.process (ev, 100, tps);
  164. ledger.moveChannels ({ 0, 1 }); // channel 2 removed
  165. CHECK (ledger.sounding() == 0);
  166. }
  167. TEST (issue32d_a_note_deleted_while_it_sounds_ends_where_it_was_going_to)
  168. {
  169. // "while editing notes during playback some notes randomly start voicing": a note deleted (or moved) while it
  170. // sounds never got its note off and hung until Stop. FL plays it to its end (r169d f13–f16).
  171. i32d::Rig rig;
  172. rig.song.patterns[0].notes.push_back (i32d::Rig::note (0, 2 * lynx::ppq, 60));
  173. rig.song.patterns[0].notes.push_back (i32d::Rig::note (3 * lynx::ppq, lynx::ppq / 2, 62)); // (keeps the pattern 1 bar)
  174. rig.rebuild();
  175. rig.play (1.0);
  176. I32D_REQUIRE (rig.spy->sounding[60] == 1);
  177. rig.song.patterns[0].notes.erase (rig.song.patterns[0].notes.begin());
  178. rig.rebuild();
  179. rig.play (0.9);
  180. CHECK (rig.spy->offs (60) == 0); // not cut: it plays on...
  181. rig.play (0.2);
  182. CHECK (rig.spy->offs (60) == 1); // ...to its end
  183. CHECK (rig.spy->sounding[60] == 0);
  184. CHECK (std::abs (rig.spy->lastOff (60) - rig.sampleOfBeat (2.0)) <= 3);
  185. CHECK (rig.renderer.scheduledNotesSounding() == 0);
  186. }
  187. TEST (issue32d_a_new_pattern_while_playing_leaves_no_note_hanging)
  188. {
  189. // "same when adding patterns while playing": Pattern mode switches to the new, empty pattern; the old pattern's
  190. // sounding notes still end (FL r169d f17–f20)
  191. i32d::Rig rig;
  192. rig.song.patterns[0].notes.push_back (i32d::Rig::note (0, 3 * lynx::ppq, 57));
  193. rig.song.patterns[0].notes.push_back (i32d::Rig::note (0, 3 * lynx::ppq, 64));
  194. rig.song.patterns.push_back (rig.song.patterns[0]);
  195. rig.song.patterns.back().notes.clear(); // the new pattern
  196. rig.rebuild();
  197. rig.play (1.0);
  198. I32D_REQUIRE (rig.spy->sounding[57] == 1 && rig.spy->sounding[64] == 1);
  199. rig.song.currentPattern = 1;
  200. rig.rebuild();
  201. rig.play (2.5);
  202. CHECK (rig.spy->sounding[57] == 0 && rig.spy->sounding[64] == 0);
  203. CHECK (rig.spy->offs (57) == 1 && rig.spy->offs (64) == 1);
  204. }
  205. TEST (issue32d_an_edited_note_s_off_cannot_cut_another_voice)
  206. {
  207. // a note drawn in behind the play head (its "on" already passed) has an "off" ahead; it must not end the long
  208. // note of the same key that is sounding. And a sounding note made longer still ends where it started to end.
  209. i32d::Rig rig;
  210. rig.song.patterns[0].notes.push_back (i32d::Rig::note (0, 3 * lynx::ppq, 60));
  211. rig.rebuild();
  212. rig.play (1.0);
  213. rig.song.patterns[0].notes.push_back (i32d::Rig::note (lynx::ppq / 2, lynx::ppq, 60)); // ends at beat 1.5
  214. rig.rebuild();
  215. rig.play (1.0);
  216. CHECK (rig.spy->offs (60) == 0);
  217. CHECK (rig.spy->sounding[60] == 1);
  218. rig.song.patterns[0].notes[0].length = 4 * lynx::ppq - 10; // longer while it sounds
  219. rig.rebuild();
  220. rig.play (1.5);
  221. CHECK (rig.spy->offs (60) == 1); // one off, at the length it started with
  222. CHECK (std::abs (rig.spy->lastOff (60) - rig.sampleOfBeat (3.0)) <= 3);
  223. CHECK (rig.spy->sounding[60] == 0);
  224. }
  225. TEST (issue32d_unedited_notes_keep_their_exact_offs)
  226. {
  227. // nothing changes for notes nobody touches: each off is the scheduler's own, on its sample
  228. i32d::Rig rig;
  229. for (int i = 0; i < 16; ++i)
  230. rig.song.patterns[0].notes.push_back (i32d::Rig::note (i * lynx::ppq / 4, lynx::ppq / 8 + i, 48 + (i % 5)));
  231. rig.rebuild();
  232. rig.play (7.95); // two passes of the pattern (the last note ends at beat 7.89)
  233. int ons = 0, offs = 0;
  234. for (const auto& e : rig.spy->log)
  235. (e.on ? ons : offs) += 1;
  236. CHECK (ons == 32 && offs == 32);
  237. // each note's length is the scheduler's (± its rounding of on and off)
  238. for (std::size_t i = 0; i < rig.spy->log.size(); ++i)
  239. if (rig.spy->log[i].on)
  240. for (std::size_t j = i + 1; j < rig.spy->log.size(); ++j)
  241. if (! rig.spy->log[j].on && rig.spy->log[j].key == rig.spy->log[i].key)
  242. {
  243. const auto n = (int) ((rig.spy->log[i].sample - rig.sampleOfBeat (0)) * rig.tps / (lynx::ppq / 4.0) + 0.5) % 16;
  244. const auto expected = (double) (lynx::ppq / 8 + n) / rig.tps;
  245. CHECK (std::abs ((double) (rig.spy->log[j].sample - rig.spy->log[i].sample) - expected) <= 1.0);
  246. break;
  247. }
  248. CHECK (rig.renderer.scheduledNotesSounding() == 0);
  249. }
  250. TEST (issue32d_a_ruler_click_goes_to_the_nearest_grid_line)
  251. {
  252. // "the cursor in the sequencer is clicking on one place but the playback triangle is reacting on an earlier spot":
  253. // the ruler put the song position on the grid line before the mouse (up to a whole grid step earlier; "Line
  254. // (follows zoom)" zoomed out is two bars or more). FL takes the nearest line (r169d f32–f34, Bar snap).
  255. const std::int64_t bar = 4 * lynx::ppq;
  256. CHECK (lynx::noteedit::snapRound (4.33 * (double) bar, bar) == 4 * bar); // bar 5.33 → 5:01
  257. CHECK (lynx::noteedit::snapRound (4.5 * (double) bar, bar) == 5 * bar); // bar 5.5 → 6:01
  258. CHECK (lynx::noteedit::snapRound (4.83 * (double) bar, bar) == 5 * bar); // bar 5.83 → 6:01
  259. CHECK (lynx::noteedit::snapDown (4.83 * (double) bar, bar) == 4 * bar); // (what it did)
  260. CHECK (lynx::noteedit::snapRound (0.2 * (double) bar, bar) == 0);
  261. CHECK (lynx::noteedit::snapRound (10.6, 1) == 11);
  262. }
  263. TEST (issue32d_the_brush_does_not_stack_a_copy_on_the_same_clip)
  264. {
  265. // "when I use the brush or pen tool this can cause problems sometimes by adding some patterns in places I didn't
  266. // want and later on playback they sound doubled (louder)": a brush stroke over clips of the same pattern put a
  267. // copy on each of them. FL's brush adds none there (r169d f37–f39).
  268. std::vector<lynx::Clip> playlist;
  269. lynx::Clip a;
  270. a.kind = lynx::ClipKind::pattern;
  271. a.pattern = 3;
  272. a.track = 2;
  273. a.start = 4 * lynx::ppq;
  274. a.length = 4 * lynx::ppq;
  275. playlist.push_back (a);
  276. auto c = a;
  277. CHECK (lynx::noteedit::paintWouldStack (playlist, c)); // the same place
  278. c.start = 6 * lynx::ppq;
  279. CHECK (lynx::noteedit::paintWouldStack (playlist, c)); // half over it
  280. c.start = 8 * lynx::ppq;
  281. CHECK (! lynx::noteedit::paintWouldStack (playlist, c)); // right after it: painted
  282. c.start = a.start;
  283. c.track = 3;
  284. CHECK (! lynx::noteedit::paintWouldStack (playlist, c)); // another track
  285. c.track = 2;
  286. c.pattern = 4;
  287. CHECK (! lynx::noteedit::paintWouldStack (playlist, c)); // another pattern (FL lets clips overlap)
  288. lynx::Clip audio;
  289. audio.kind = lynx::ClipKind::audio;
  290. audio.channel = 7;
  291. audio.track = 2;
  292. audio.start = a.start;
  293. audio.length = lynx::ppq;
  294. CHECK (! lynx::noteedit::paintWouldStack (playlist, audio));
  295. playlist.push_back (audio);
  296. audio.start += lynx::ppq / 2;
  297. CHECK (lynx::noteedit::paintWouldStack (playlist, audio));
  298. }