Issue97Tests.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Issue #97: several automation clips on one target. FL's rule, from FL's own renders (FL64 /R, REA_rodada177b.md): where
  2. // clips play, the one that started last (same start: the lowest automation); between clips, the end of the clip that
  3. // ended last; before the first clip, the control's own value. Not only one automation of a target plays.
  4. namespace issue97
  5. {
  6. const auto bar = (std::int64_t) lynx::ppq * 4;
  7. lynx::Song song()
  8. {
  9. auto s = lynx::Song::makeDefault();
  10. s.bpm = 120.0;
  11. s.mode = lynx::PlayMode::song;
  12. // (FL's clip rule, from FL renders without latency on the Master: LYNX's fader compensation of round 190hq would take
  13. // the Master fader's automation as late as the default Master's limiter look-ahead)
  14. s.compensateMixerLevels = false;
  15. return s;
  16. }
  17. // a flat automation of the Master's fader
  18. int addFlat (lynx::Song& s, float value)
  19. {
  20. lynx::Automation a;
  21. a.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  22. a.points = { { 0, value }, { bar * 32, value } };
  23. s.automations.push_back (a);
  24. return (int) s.automations.size() - 1;
  25. }
  26. void addClip (lynx::Song& s, int automation, int startBar, int bars, int track)
  27. {
  28. lynx::Clip c;
  29. c.kind = lynx::ClipKind::automation;
  30. c.automation = automation;
  31. c.track = track;
  32. c.start = bar * startBar;
  33. c.length = bar * bars;
  34. s.playlist.push_back (c);
  35. }
  36. // the Master's peak over a bar
  37. float levelAt (const lynx::Song& s, int atBar)
  38. {
  39. const double sr = 48000.0;
  40. auto state = lynx::RackState::build (s, nullptr, sr);
  41. lynx::RackRenderer r;
  42. r.prepare (sr, 512);
  43. std::vector<float> l (512), rr (512);
  44. const auto tps = lynx::ticksPerSample (s.bpm, sr);
  45. float peak = 0.0f;
  46. for (double tick = (double) bar * atBar; tick < (double) bar * (atBar + 1); tick += 512.0 * tps)
  47. {
  48. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  49. for (const auto v : l)
  50. peak = std::max (peak, std::abs (v));
  51. }
  52. return peak;
  53. }
  54. // the pattern in every bar, so every bar has signal
  55. lynx::Song withSound()
  56. {
  57. auto s = song();
  58. for (int b = 0; b < 32; ++b)
  59. s.playlist.push_back ({ 0, 0, bar * b, bar, 0 });
  60. return s;
  61. }
  62. } // namespace issue97
  63. TEST (issue97_one_target_shares_a_slot)
  64. {
  65. auto s = issue97::song();
  66. const auto a = issue97::addFlat (s, 0.2f), b = issue97::addFlat (s, 0.5f), c = issue97::addFlat (s, 0.8f);
  67. issue97::addClip (s, a, 4, 2, 1);
  68. issue97::addClip (s, b, 8, 2, 2);
  69. auto state = lynx::RackState::build (s, nullptr, 48000.0);
  70. CHECK (state->mixerVolumeAuto[0] == b); // (the third has no clip: it plays nothing)
  71. CHECK (state->automationGroups[(std::size_t) b].size() == 2 && state->automationFollowers[(std::size_t) a] == 1);
  72. CHECK (state->automationFollowers[(std::size_t) c] == 0 && state->automationGroups[(std::size_t) c].empty());
  73. // the second automation's clip goes: the first plays alone again
  74. s.playlist.pop_back();
  75. state = lynx::RackState::build (s, nullptr, 48000.0);
  76. CHECK (state->mixerVolumeAuto[0] == a && state->automationFollowers[(std::size_t) a] == 0 && state->automationGroups[(std::size_t) a].empty());
  77. }
  78. TEST (issue97_plugin_parameter_binds_once)
  79. {
  80. // a CC / plugin parameter binding was made per automation: both wrote every block and the last one stayed
  81. auto s = issue97::song();
  82. s.channels[0].instrument = lynx::InstrumentKind::midiOut;
  83. for (const auto value : { 0.2f, 0.5f })
  84. {
  85. lynx::Automation a;
  86. a.target = { lynx::AutomationTargetKind::channelParam, 0, 0, lynx::midictl::ccBase + 1 };
  87. a.points = { { 0, value }, { issue97::bar * 8, value } };
  88. s.automations.push_back (a);
  89. }
  90. issue97::addClip (s, 0, 0, 2, 1);
  91. issue97::addClip (s, 1, 2, 2, 2);
  92. const auto state = lynx::RackState::build (s, nullptr, 48000.0);
  93. CHECK (state->pluginParams.size() == 1 && state->pluginParams[0].automation == 1);
  94. }
  95. TEST (issue97_the_clip_that_plays_decides)
  96. {
  97. // FL's ra / rb renders: before = own value; A, held; B, held; and the channel order does not matter
  98. auto s = issue97::withSound();
  99. const auto own = issue97::levelAt (s, 1);
  100. const auto low = issue97::addFlat (s, 0.2f), high = issue97::addFlat (s, 0.5f);
  101. issue97::addClip (s, low, 4, 2, 1);
  102. issue97::addClip (s, high, 8, 2, 2);
  103. const auto before = issue97::levelAt (s, 1), first = issue97::levelAt (s, 4), between = issue97::levelAt (s, 6);
  104. const auto second = issue97::levelAt (s, 8), after = issue97::levelAt (s, 12);
  105. CHECK (std::abs (before - own) < 1e-3f);
  106. CHECK (first > 0.0f && first < second);
  107. CHECK (std::abs (between - first) < 1e-3f);
  108. CHECK (std::abs (after - second) < 1e-3f);
  109. auto swapped = issue97::withSound();
  110. const auto high2 = issue97::addFlat (swapped, 0.5f), low2 = issue97::addFlat (swapped, 0.2f);
  111. issue97::addClip (swapped, low2, 4, 2, 1);
  112. issue97::addClip (swapped, high2, 8, 2, 2);
  113. CHECK (std::abs (issue97::levelAt (swapped, 4) - first) < 1e-3f);
  114. CHECK (std::abs (issue97::levelAt (swapped, 8) - second) < 1e-3f);
  115. }
  116. TEST (issue97_overlap_the_later_start_wins)
  117. {
  118. // FL's rc / rd / re renders
  119. auto s = issue97::withSound();
  120. const auto a = issue97::addFlat (s, 0.2f), b = issue97::addFlat (s, 0.5f);
  121. issue97::addClip (s, a, 4, 6, 2); // A 4..10
  122. issue97::addClip (s, b, 6, 2, 1); // B 6..8, inside it
  123. const auto onlyA = issue97::levelAt (s, 4), inside = issue97::levelAt (s, 6), resumes = issue97::levelAt (s, 8);
  124. CHECK (onlyA < inside);
  125. CHECK (std::abs (resumes - onlyA) < 1e-3f);
  126. // the same start: the lowest automation (FL's rg / rh / ri, whichever the track or the file order)
  127. auto tie = issue97::withSound();
  128. const auto a2 = issue97::addFlat (tie, 0.2f), b2 = issue97::addFlat (tie, 0.5f);
  129. issue97::addClip (tie, b2, 4, 4, 1);
  130. issue97::addClip (tie, a2, 4, 4, 2);
  131. CHECK (std::abs (issue97::levelAt (tie, 5) - onlyA) < 1e-3f);
  132. // the same automation twice: the later clip restarts its curve (FL's rj)
  133. auto same = issue97::song();
  134. lynx::Automation ramp;
  135. ramp.target = { lynx::AutomationTargetKind::mixerVolume, 0 };
  136. ramp.points = { { 0, 0.0f }, { issue97::bar * 4, 1.0f } };
  137. same.automations.push_back (ramp);
  138. issue97::addClip (same, 0, 0, 4, 1);
  139. issue97::addClip (same, 0, 2, 4, 2);
  140. CHECK (std::abs (same.targetValueAt (ramp.target, (double) issue97::bar * 2) - 0.0f) < 1e-5f);
  141. CHECK (std::isnan (same.targetValueAt ({ lynx::AutomationTargetKind::mixerPan, 0 }, 0.0)));
  142. }
  143. TEST (issue97_two_tempo_clips)
  144. {
  145. auto s = issue97::song();
  146. for (const auto value : { 0.2f, 0.6f })
  147. {
  148. lynx::Automation a;
  149. a.target.kind = lynx::AutomationTargetKind::tempo;
  150. a.points = { { 0, value }, { issue97::bar * 8, value } };
  151. s.automations.push_back (a);
  152. }
  153. issue97::addClip (s, 0, 2, 2, 1);
  154. issue97::addClip (s, 1, 6, 2, 2);
  155. // before the first clip: the first clip's value (178a's rule for the tempo, unverified against FL)
  156. CHECK (std::abs (s.tempoAt ((double) issue97::bar) - lynx::tempoFromValue (0.2f)) < 1.0e-3);
  157. const auto first = s.tempoAt ((double) issue97::bar * 3), second = s.tempoAt ((double) issue97::bar * 7);
  158. CHECK (first < second);
  159. CHECK (s.tempoAt ((double) issue97::bar * 5) == first);
  160. CHECK (s.tempoAt ((double) issue97::bar * 9) == second);
  161. }