Timing180dTests.inc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // --- Round 180d: notes start on their exact frame (180c: Fruity Slicer notes 1 frame late from 2.25 s on) -----------------
  2. namespace r180dtiming
  3. {
  4. // Renders `notes` eighth notes of a one-frame click on `instrument` in song mode, in blocks of `block` frames with the
  5. // transport's own tick accumulation (AudioEngine: position += n · ticksPerSample), and returns each note's first frame.
  6. inline std::vector<long> onsets (lynx::InstrumentKind instrument, int notes, int block, double rate, double bpm)
  7. {
  8. using namespace lynx;
  9. auto click = std::make_shared<SampleData>();
  10. click->sampleRate = rate;
  11. click->left.assign (4000, 0.0f);
  12. click->left[0] = 1.0f;
  13. Song s;
  14. s.bpm = bpm;
  15. s.mode = PlayMode::song;
  16. s.mixer = Song::defaultMixer (1);
  17. ChannelDef c;
  18. c.instrument = instrument;
  19. c.samplePath = "click.wav";
  20. c.mixerTrack = 1;
  21. s.channels.push_back (c);
  22. Pattern p;
  23. for (int i = 0; i < notes; ++i)
  24. {
  25. Note n;
  26. n.start = (std::int64_t) i * ppq / 2;
  27. n.length = ppq / 4;
  28. p.notes.push_back (n);
  29. }
  30. s.patterns.push_back (p);
  31. Clip clip;
  32. clip.kind = ClipKind::pattern;
  33. clip.pattern = 0;
  34. clip.length = (std::int64_t) notes * ppq / 2;
  35. s.playlist = { clip };
  36. RackState::Factories f;
  37. f.samples = [click] (const std::string&) { return std::shared_ptr<const SampleData> (click); };
  38. auto state = RackState::build (s, nullptr, rate, f);
  39. RackRenderer r;
  40. r.prepare (rate, block);
  41. std::vector<float> out, l ((std::size_t) block), rr ((std::size_t) block);
  42. const auto tps = ticksPerSample (bpm, rate);
  43. const auto total = (double) notes * ppq / 2 + ppq;
  44. for (double tick = 0.0; tick < total; tick += block * tps)
  45. {
  46. r.process (state.get(), true, tick, block, l.data(), rr.data());
  47. out.insert (out.end(), l.begin(), l.end());
  48. }
  49. std::vector<long> at;
  50. bool inside = false;
  51. for (std::size_t i = 0; i < out.size(); ++i)
  52. {
  53. const auto loud = std::abs (out[i]) > 1.0e-3f;
  54. if (loud && ! inside)
  55. at.push_back ((long) i);
  56. inside = loud && (at.empty() || (long) i - at.back() < 50);
  57. }
  58. return at;
  59. }
  60. } // namespace r180dtiming
  61. TEST (render180d_notes_start_on_their_exact_frame)
  62. {
  63. using namespace lynx;
  64. // 120 BPM: an eighth note is 11025 frames at 44.1 kHz; 140 BPM at 48 kHz: 10285.714…, FL rounds each to its frame
  65. for (const auto& [rate, bpm] : { std::pair { 44100.0, 120.0 }, std::pair { 48000.0, 140.0 } })
  66. for (const auto block : { 512, 441, 64 })
  67. for (const auto kind : { InstrumentKind::sampler, InstrumentKind::audioClip })
  68. {
  69. const auto at = r180dtiming::onsets (kind, 64, block, rate, bpm);
  70. const auto eighth = rate * 60.0 / bpm / 2.0;
  71. int late = 0, first = -1;
  72. for (std::size_t i = 0; i < std::min<std::size_t> (at.size(), 64); ++i)
  73. if (at[i] != (long) std::nearbyint ((double) i * eighth))
  74. late += 1, first = first < 0 ? (int) i : first;
  75. if (late > 0 || at.size() < 64)
  76. std::fprintf (stderr, " [r180d] %s %.0f Hz %.0f BPM block %d: %zu onsets, %d off (first: note %d at %ld, exact %ld)\n",
  77. kind == InstrumentKind::sampler ? "sampler" : "audio clip", rate, bpm, block, at.size(), late, first,
  78. first >= 0 ? at[(std::size_t) first] : -1L, first >= 0 ? (long) std::nearbyint (first * eighth) : -1L);
  79. CHECK (at.size() >= 64 && late == 0); // (the render runs a beat past the last note)
  80. }
  81. }