Issue12cTests.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Issue #12 (follow-up): knobs and faders follow their automation on screen while the song plays, as in FL.
  2. // core/AutomationDisplay evaluates on the UI thread the value each automated target has at the playhead. Included at
  3. // the end of tests.cpp.
  4. namespace issue12c
  5. {
  6. // a song in Song mode with an automation clip (0 → 1 over 4 bars) on `target`, at bar 1
  7. inline lynx::Song songWithClip (const lynx::AutomationTarget& target)
  8. {
  9. auto song = lynx::Song::makeDefault();
  10. song.mode = lynx::PlayMode::song;
  11. lynx::Automation a;
  12. a.target = target;
  13. a.points = { { 0, 0.0f, 0.0f }, { 16 * lynx::ppq, 1.0f, 0.0f } };
  14. song.automations.push_back (a);
  15. lynx::Clip c;
  16. c.kind = lynx::ClipKind::automation;
  17. c.automation = (int) song.automations.size() - 1;
  18. c.track = 3;
  19. c.start = 0;
  20. c.length = 16 * lynx::ppq;
  21. song.playlist.push_back (c);
  22. return song;
  23. }
  24. } // namespace issue12c
  25. TEST (issue12c_automated_value_at_the_playhead)
  26. {
  27. using namespace lynx;
  28. const AutomationTarget send { AutomationTargetKind::mixerSend, 1, 0 };
  29. const AutomationTarget volume { AutomationTargetKind::mixerVolume, 2 };
  30. auto song = issue12c::songWithClip (send);
  31. AutomationDisplay shown;
  32. // stopped: nothing is shown (the controls show the Song's value, which a hand move edits)
  33. CHECK (! shown.update (song, 1, 4.0 * ppq, false));
  34. CHECK (! shown.valueOf (send) && ! shown.any());
  35. CHECK (shown.automatedTargets().size() == 1 && shown.automatedTargets().front() == send);
  36. // playing: the clip's value at the playhead, the same one playback uses
  37. const auto g0 = shown.getGeneration();
  38. CHECK (shown.update (song, 1, 4.0 * ppq, true));
  39. CHECK (shown.getGeneration() != g0);
  40. CHECK (shown.valueOf (send) && std::abs (*shown.valueOf (send) - song.targetValueAt (song.automations[0].target, 4.0 * ppq)) < 1.0e-5f);
  41. CHECK (std::abs (*shown.valueOf (send) - 0.25f) < 0.01f);
  42. CHECK (! shown.valueOf (volume));
  43. CHECK (shown.shown (volume, 0.7f) == 0.7f);
  44. // the same position again: no change, no repaint
  45. const auto g1 = shown.getGeneration();
  46. CHECK (! shown.update (song, 1, 4.0 * ppq, true));
  47. CHECK (shown.getGeneration() == g1);
  48. CHECK (shown.update (song, 1, 12.0 * ppq, true));
  49. CHECK (std::abs (*shown.valueOf (send) - 0.75f) < 0.01f);
  50. // after the clip: its last value; a muted clip: nothing
  51. CHECK (shown.update (song, 1, 40.0 * ppq, true) || true);
  52. CHECK (std::abs (*shown.valueOf (send) - 1.0f) < 0.01f);
  53. song.playlist.back().muted = true;
  54. shown.update (song, 2, 4.0 * ppq, true);
  55. CHECK (! shown.valueOf (send));
  56. // pattern mode: the Playlist does not play; the current pattern's event lane does
  57. song.playlist.back().muted = false;
  58. song.mode = PlayMode::pattern;
  59. auto& p = song.patterns[(std::size_t) song.currentPattern];
  60. PatternEvents lane;
  61. lane.target = volume;
  62. lane.events = { { 0, 0.2f }, { 2 * ppq, 0.9f } };
  63. p.events.push_back (lane);
  64. shown.update (song, 3, 3.0 * ppq, true);
  65. CHECK (! shown.valueOf (send));
  66. CHECK (shown.valueOf (volume) && std::abs (*shown.valueOf (volume) - 0.9f) < 1.0e-5f);
  67. shown.update (song, 3, 1.0 * ppq, true);
  68. CHECK (std::abs (*shown.valueOf (volume) - 0.2f) < 1.0e-5f);
  69. // stopping clears it (generation moves, so the editors repaint back to the Song's values)
  70. const auto g2 = shown.getGeneration();
  71. CHECK (shown.update (song, 3, 1.0 * ppq, false));
  72. CHECK (shown.getGeneration() != g2 && ! shown.any());
  73. }
  74. // The refresh runs on the UI thread once per frame: measure it on a big song (300 automated targets, 3000 clips)
  75. TEST (issue12c_display_refresh_is_cheap)
  76. {
  77. using namespace lynx;
  78. auto song = Song::makeDefault();
  79. song.mode = PlayMode::song;
  80. while (song.mixer.size() < 100)
  81. song.mixer.emplace_back();
  82. for (int a = 0; a < 300; ++a)
  83. {
  84. Automation automation;
  85. automation.target = { a % 3 == 0 ? AutomationTargetKind::mixerVolume : a % 3 == 1 ? AutomationTargetKind::mixerPan
  86. : AutomationTargetKind::channelVolume,
  87. a / 3 };
  88. for (int k = 0; k < 64; ++k)
  89. automation.points.push_back ({ (std::int64_t) k * ppq, (float) ((k * 7 + a) % 11) / 10.0f, 0.0f });
  90. song.automations.push_back (automation);
  91. for (int c = 0; c < 10; ++c)
  92. {
  93. Clip clip;
  94. clip.kind = ClipKind::automation;
  95. clip.automation = a;
  96. clip.track = a % 50;
  97. clip.start = (std::int64_t) c * 64 * ppq;
  98. clip.length = 64 * ppq;
  99. song.playlist.push_back (clip);
  100. }
  101. }
  102. AutomationDisplay shown;
  103. shown.update (song, 1, 0.0, true); // (builds the tables once)
  104. const auto start = std::chrono::steady_clock::now();
  105. constexpr int frames = 300;
  106. for (int f = 0; f < frames; ++f)
  107. shown.update (song, 1, (double) f * 37.0 * ppq / 10.0, true);
  108. const auto us = std::chrono::duration<double, std::micro> (std::chrono::steady_clock::now() - start).count() / frames;
  109. std::printf (" automation display: %.1f us per refresh (300 automated targets, 3000 clips)\n", us);
  110. CHECK (shown.automatedTargets().size() == 300);
  111. CHECK (shown.valueOf ({ AutomationTargetKind::mixerPan, 0 }).has_value());
  112. CHECK (us < 2000.0); // well under a frame (33 ms) even in a debug build
  113. }