4
0

Issue32cTests.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Round 169c (issue #32 follow-up, medievilmusic on 0.1.22): docs/analise/REA_rodada169c.md
  2. #define I32C_REQUIRE(x) \
  3. do { if (! (x)) { CHECK (x); return; } } while (false)
  4. namespace i32c
  5. {
  6. // A hosted generator stand-in: keeps the keys it was sent like PluginInstrument (HostedKeys) and, for every block it
  7. // is asked to render, the position its play head would report (HostBlockClock + HostTimeline::tickAt).
  8. struct HostedSpy final : lynx::Instrument
  9. {
  10. lynx::HostedKeys keys;
  11. lynx::HostBlockClock clock;
  12. std::vector<int> offs; // keys released by allNotesOff (the note offs sent)
  13. std::vector<std::pair<double, int>> blocks; // (play head tick, samples) per block
  14. void prepare (double) noexcept override {}
  15. void noteOn (int key, float) noexcept override { keys.set (0, key, true); }
  16. void noteOff (int key) noexcept override { keys.set (0, key, false); }
  17. void allNotesOff() noexcept override
  18. {
  19. keys.releaseAll ([this] (int, int key) { offs.push_back (key); });
  20. }
  21. void render (float*, float*, int n, float, float) noexcept override
  22. {
  23. const auto& t = lynx::hostTimeline();
  24. const auto at = clock.start (t.chunk.load());
  25. clock.advance (n);
  26. if (t.playing.load())
  27. blocks.emplace_back (t.tickAt (at), n);
  28. }
  29. };
  30. } // namespace i32c
  31. TEST (issue32c_hosted_keys_release_each_held_key_once)
  32. {
  33. // a VST3 synth only stops a note on its own note off (no CC 123): every held key on every MIDI channel gets one
  34. lynx::HostedKeys keys;
  35. keys.set (0, 60, true);
  36. keys.set (0, 64, true);
  37. keys.set (3, 127, true);
  38. keys.set (0, 64, false);
  39. keys.set (16, 10, true); // (out of range: ignored)
  40. keys.set (0, 128, true);
  41. CHECK (keys.held (0, 60) && ! keys.held (0, 64) && keys.held (3, 127) && keys.any());
  42. std::vector<std::pair<int, int>> offs;
  43. keys.releaseAll ([&] (int ch, int key) { offs.emplace_back (ch, key); });
  44. I32C_REQUIRE (offs.size() == 2);
  45. CHECK (offs[0] == std::make_pair (0, 60));
  46. CHECK (offs[1] == std::make_pair (3, 127));
  47. CHECK (! keys.any());
  48. offs.clear();
  49. keys.releaseAll ([&] (int ch, int key) { offs.emplace_back (ch, key); });
  50. CHECK (offs.empty()); // nothing twice
  51. }
  52. TEST (issue32c_a_jump_while_playing_sends_the_sounding_notes_their_note_offs)
  53. {
  54. // #1 "when moving inside the sequencer while the playback is on, some synths double their sound": a long note of a
  55. // hosted generator is sounding, the user clicks the ruler; the old note must get its note off (the chased copy of
  56. // it starts again at the new position, as FL's "Play truncated notes")
  57. auto song = lynx::Song::makeDefault();
  58. song.playlist.clear();
  59. song.mode = lynx::PlayMode::pattern;
  60. I32C_REQUIRE (song.channels.size() > 1 && ! song.patterns.empty());
  61. song.patterns[0].notes.clear();
  62. lynx::Note n;
  63. n.start = 0;
  64. n.length = 16 * lynx::ppq;
  65. n.key = 55;
  66. n.channel = 1;
  67. song.patterns[0].notes.push_back (n);
  68. song.currentPattern = 0;
  69. const double rate = 48000.0, tps = lynx::ticksPerSample (song.bpm, rate);
  70. auto state = lynx::RackState::build (song, nullptr, rate);
  71. auto spy = std::make_shared<i32c::HostedSpy>();
  72. const_cast<lynx::RackState&> (*state).instruments[1] = spy;
  73. lynx::RackRenderer renderer;
  74. renderer.prepare (rate, 512);
  75. unsigned jumps = 0;
  76. renderer.setTransportOptions (true, false, jumps);
  77. std::vector<float> l (512), r (512);
  78. double tick = 0.0;
  79. for (int b = 0; b < 4; ++b, tick += 512 * tps)
  80. renderer.process (state.get(), true, tick, 512, l.data(), r.data());
  81. CHECK (spy->keys.held (0, 55));
  82. renderer.setTransportOptions (true, false, ++jumps);
  83. renderer.process (state.get(), true, 4.0 * lynx::ppq, 512, l.data(), r.data());
  84. I32C_REQUIRE (spy->offs.size() == 1);
  85. CHECK (spy->offs[0] == 55); // the old note was stopped...
  86. CHECK (spy->keys.held (0, 55)); // ...and the chased one plays (held once, not twice)
  87. }
  88. TEST (issue32c_each_block_of_a_generator_reports_where_it_starts)
  89. {
  90. // #1 "...or go out of tempo" / #3: a generator's chunk is split at its notes, and each piece is one plugin block.
  91. // Every block reported the chunk's start, so a synced sequencer saw the song stand still and jump. Now the play
  92. // head moves on with each block: the positions are continuous across notes, chunks and blocks.
  93. auto song = lynx::Song::makeDefault();
  94. song.playlist.clear();
  95. song.mode = lynx::PlayMode::pattern;
  96. I32C_REQUIRE (song.channels.size() > 1 && ! song.patterns.empty());
  97. song.patterns[0].notes.clear();
  98. for (int i = 0; i < 16; ++i) // sixteenth notes: several notes inside each block
  99. {
  100. lynx::Note n;
  101. n.start = i * lynx::ppq / 4;
  102. n.length = lynx::ppq / 8;
  103. n.key = 48 + i;
  104. n.channel = 1;
  105. song.patterns[0].notes.push_back (n);
  106. }
  107. song.currentPattern = 0;
  108. const double rate = 44100.0, tps = lynx::ticksPerSample (song.bpm, rate);
  109. auto state = lynx::RackState::build (song, nullptr, rate);
  110. auto spy = std::make_shared<i32c::HostedSpy>();
  111. const_cast<lynx::RackState&> (*state).instruments[1] = spy;
  112. lynx::RackRenderer renderer;
  113. renderer.prepare (rate, 1024);
  114. std::vector<float> l (1024), r (1024);
  115. double tick = 0.0;
  116. for (int b = 0; b < 40; ++b, tick += 1024 * tps)
  117. renderer.process (state.get(), true, tick, 1024, l.data(), r.data());
  118. I32C_REQUIRE (spy->blocks.size() > 60); // (split at the notes: more blocks than device blocks)
  119. int splitInsideChunk = 0;
  120. double worst = 0.0;
  121. for (std::size_t i = 1; i < spy->blocks.size(); ++i)
  122. {
  123. const auto expected = spy->blocks[i - 1].first + spy->blocks[i - 1].second * tps;
  124. const auto at = spy->blocks[i].first;
  125. if (at < 4.0 * lynx::ppq - 1.0 && expected < 4.0 * lynx::ppq - 1.0) // (the pattern's wrap is a jump)
  126. worst = std::max (worst, std::abs (at - expected));
  127. if (spy->blocks[i].first != spy->blocks[i - 1].first && spy->blocks[i - 1].second < 1024)
  128. ++splitInsideChunk;
  129. }
  130. CHECK (worst < 1.0e-6);
  131. CHECK (splitInsideChunk > 0);
  132. // stopped: no tempo, the play head stays where it is
  133. renderer.process (state.get(), false, tick, 1024, l.data(), r.data());
  134. CHECK (lynx::hostTimeline().ticksPerSample.load() == 0.0);
  135. }
  136. TEST (issue32c_a_wheel_scrolling_the_mixer_keeps_scrolling_over_its_controls)
  137. {
  138. // #5 "all the faders on the mixer can react accidentally on the mouse wheel": the spin that scrolls the strips goes
  139. // on scrolling while the wheel keeps turning; after a pause the wheel over a fader moves it again (FL)
  140. lynx::controls::WheelScrollLatch latch;
  141. CHECK (! latch.scrolling (1000.0)); // nothing scrolled yet: the wheel over a fader changes it
  142. latch.scrolled (1000.0);
  143. CHECK (latch.scrolling (1000.0));
  144. CHECK (latch.scrolling (1060.0)); // the next notch (~60 ms later) is still the scroll
  145. latch.scrolled (1060.0); // (it scrolled again: the hold starts over)
  146. CHECK (latch.scrolling (1400.0));
  147. CHECK (! latch.scrolling (1460.0)); // a pause of 400 ms ends it
  148. CHECK (! latch.scrolling (900.0)); // (a clock that went back is no scroll)
  149. }