Round166eTests.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Round 166e (issue #16): FL 2026's automatic crossfades, the audio clip gain handle, "Disarm on stop" and the Piano
  2. // roll's Alt+wheel / note template (docs/analise/REA_rodada166e.md). Included at the end of tests.cpp.
  3. namespace round166e
  4. {
  5. inline lynx::Clip audioClip (int track, std::int64_t start, std::int64_t length)
  6. {
  7. lynx::Clip c;
  8. c.kind = lynx::ClipKind::audio;
  9. c.channel = 0;
  10. c.track = track;
  11. c.start = start;
  12. c.length = length;
  13. return c;
  14. }
  15. // one audio channel playing a constant 1.0 for 10 s; clips A (beats 0-4) and B (beats 2-6) on track 0
  16. inline lynx::Song overlapSong (bool crossfades)
  17. {
  18. lynx::Song s;
  19. s.bpm = 120.0; // 1 beat = 24000 samples at 48 kHz
  20. lynx::ChannelDef c;
  21. c.instrument = lynx::InstrumentKind::audioClip;
  22. c.samplePath = "ones";
  23. c.volume = 1.0f;
  24. s.channels.push_back (c);
  25. s.patterns.emplace_back();
  26. s.playlist.push_back (audioClip (0, 0, 4 * lynx::ppq));
  27. s.playlist.push_back (audioClip (0, 2 * lynx::ppq, 4 * lynx::ppq));
  28. s.mode = lynx::PlayMode::song;
  29. s.autoCrossfades = crossfades;
  30. lynx::clipops::updateCrossfades (s);
  31. return s;
  32. }
  33. // the left output at `beats` (rendered from the start in 256-sample blocks)
  34. inline float levelAt (const lynx::Song& s, double beats)
  35. {
  36. auto provider = [] (const std::string&) {
  37. auto d = std::make_shared<lynx::SampleData>();
  38. d->sampleRate = 48000.0;
  39. d->left.assign (48000 * 10, 1.0f);
  40. return std::shared_ptr<const lynx::SampleData> (d);
  41. };
  42. auto state = lynx::RackState::build (s, nullptr, 48000.0, provider);
  43. lynx::RackRenderer r;
  44. r.prepare (48000.0, 256);
  45. std::vector<float> l (256), rr (256);
  46. const auto tps = lynx::ticksPerSample (120.0, 48000.0);
  47. const auto blocks = (int) (beats * 24000.0 / 256.0);
  48. for (int b = 0; b <= blocks; ++b)
  49. r.process (state.get(), true, (double) b * 256.0 * tps, 256, l.data(), rr.data());
  50. return l[255];
  51. }
  52. } // namespace round166e
  53. TEST (round166e_automatic_crossfades_over_the_overlap)
  54. {
  55. using namespace lynx;
  56. auto s = round166e::overlapSong (true);
  57. const auto& a = s.playlist[0];
  58. const auto& b = s.playlist[1];
  59. CHECK (a.crossfadeOut == 2 * ppq && a.crossfadeIn == 0);
  60. CHECK (b.crossfadeIn == 2 * ppq && b.crossfadeOut == 0);
  61. CHECK (a.effectiveFadeOut() == 2 * ppq && b.effectiveFadeIn() == 2 * ppq);
  62. // off: none (and a clip's own fades come back)
  63. s.autoCrossfades = false;
  64. s.playlist[0].fadeOut = 10;
  65. clipops::updateCrossfades (s);
  66. CHECK (s.playlist[0].crossfadeOut == 0 && s.playlist[1].crossfadeIn == 0 && s.playlist[0].effectiveFadeOut() == 10);
  67. // a clip lying inside another, clips on other tracks and touching clips get none
  68. std::vector<Clip> clips { round166e::audioClip (0, 0, 8 * ppq), round166e::audioClip (0, 2 * ppq, 2 * ppq), round166e::audioClip (1, 4 * ppq, 4 * ppq),
  69. round166e::audioClip (2, 0, 4 * ppq), round166e::audioClip (2, 4 * ppq, 4 * ppq) };
  70. clipops::updateCrossfades (clips, true);
  71. for (const auto& c : clips)
  72. CHECK (c.crossfadeIn == 0 && c.crossfadeOut == 0);
  73. // a pattern clip doesn't take part
  74. std::vector<Clip> mixed { round166e::audioClip (0, 0, 4 * ppq), Clip { 0, 0, 2 * ppq, 4 * ppq, 0 } };
  75. clipops::updateCrossfades (mixed, true);
  76. CHECK (mixed[0].crossfadeOut == 0 && mixed[1].crossfadeIn == 0);
  77. // the order in the list doesn't matter (the later start fades in)
  78. std::vector<Clip> reversed { round166e::audioClip (0, 2 * ppq, 4 * ppq), round166e::audioClip (0, 0, 4 * ppq) };
  79. clipops::updateCrossfades (reversed, true);
  80. CHECK (reversed[0].crossfadeIn == 2 * ppq && reversed[1].crossfadeOut == 2 * ppq);
  81. }
  82. TEST (round166e_crossfade_curve_and_scheduler_shape)
  83. {
  84. using namespace lynx;
  85. CHECK (clipops::crossfadeCurve (0.0f) == 0.0f && std::abs (clipops::crossfadeCurve (1.0f) - 1.0f) < 1e-6f);
  86. for (float x = 0.0f; x <= 1.0f; x += 0.125f) // the two sides add up to 1
  87. CHECK (std::abs (clipops::crossfadeCurve (x) + clipops::crossfadeCurve (1.0f - x) - 1.0f) < 1e-5f);
  88. CHECK (std::abs (clipops::crossfadeCurve (0.5f) - 0.5f) < 1e-6f);
  89. const auto s = round166e::overlapSong (true);
  90. const auto tps = ticksPerSample (120.0, 48000.0);
  91. EventBuffer ev;
  92. scheduleSong (s, 0.0, 6 * 24000, tps, ev);
  93. int ons = 0;
  94. for (const auto& e : ev)
  95. if (e.type == NoteEvent::Type::on)
  96. {
  97. ++ons;
  98. if (e.sampleOffset == 0)
  99. CHECK (e.fadeShape == 2 && std::abs (e.fadeOut - 2.0f * 24000.0f) < 1.0f);
  100. else
  101. CHECK (e.fadeShape == 1 && std::abs (e.fadeIn - 2.0f * 24000.0f) < 1.0f);
  102. }
  103. CHECK (ons == 2);
  104. }
  105. TEST (round166e_crossfade_keeps_the_level_through_the_overlap)
  106. {
  107. const auto on = round166e::overlapSong (true);
  108. const auto off = round166e::overlapSong (false);
  109. const auto unity = round166e::levelAt (on, 1.0); // only clip A
  110. CHECK (unity > 0.1f);
  111. CHECK (std::abs (round166e::levelAt (off, 1.0) - unity) < 1e-4f);
  112. // without crossfades the overlap sums both clips; with them it stays at one clip's level (S curves adding to 1)
  113. CHECK (std::abs (round166e::levelAt (off, 3.0) - 2.0f * unity) < 0.01f * unity);
  114. CHECK (std::abs (round166e::levelAt (on, 3.0) - unity) < 0.01f * unity);
  115. CHECK (std::abs (round166e::levelAt (on, 2.5) - unity) < 0.01f * unity);
  116. CHECK (std::abs (round166e::levelAt (on, 4.5) - unity) < 0.01f * unity); // only clip B, past the overlap
  117. }
  118. TEST (round166e_gain_handle_range_and_gesture)
  119. {
  120. using namespace lynx;
  121. using namespace lynx::gesture;
  122. CHECK (std::abs (clipops::clipGainToDb (1.0f)) < 1e-9);
  123. CHECK (clipops::clipGainToDb (0.0f) == clipops::clipGainMinDb);
  124. CHECK (clipops::clipGainToDb (1.0e-9f) == clipops::clipGainMinDb);
  125. CHECK (std::abs (clipops::clipGainToDb (clipops::clipGainFromDb (12.5)) - 12.5) < 1e-4);
  126. CHECK (std::abs (clipops::clipGainToDb (clipops::clipGainFromDb (99.0)) - 36.0) < 1e-4); // FL's top: +36 dB
  127. CHECK (std::abs (clipops::clipGainToDb (clipops::clipGainFromDb (-200.0)) - clipops::clipGainMinDb) < 1e-3);
  128. // FL: 0.05 dB per pixel, linear in dB (100 px = 5 dB)
  129. CHECK (std::abs (100 * clipops::clipGainDbPerPixel - 5.0) < 1e-12);
  130. const Mods none {};
  131. CHECK (leftAction (EditTool::pencil, Target::gainHandle, none, true) == Action::clipGain);
  132. CHECK (cursorFor (Action::clipGain, EditTool::pencil) == Cursor::resizeV);
  133. }
  134. TEST (round166e_new_projects_get_automatic_crossfades)
  135. {
  136. using namespace lynx;
  137. NewProjectDefaults d;
  138. CHECK (d.autoCrossfades); // FL's "Enable automatic crossfades in new projects" is on by default
  139. auto song = Song::makeDefault();
  140. applyNewProjectDefaults (song, d);
  141. CHECK (song.autoCrossfades);
  142. d.autoCrossfades = false;
  143. applyNewProjectDefaults (song, d);
  144. CHECK (! song.autoCrossfades);
  145. CHECK (d.toText().find ("newAutoCrossfades=0") != std::string::npos);
  146. NewProjectDefaults read;
  147. read.readKey ("newAutoCrossfades", "0");
  148. CHECK (! read.autoCrossfades);
  149. read.readKey ("newAutoCrossfades", "1");
  150. CHECK (read.autoCrossfades);
  151. CHECK (! Song {}.autoCrossfades); // a project saved without the flag loads with it off (FL)
  152. }
  153. TEST (round166e_disarm_on_stop_switches_recording_off)
  154. {
  155. lynx::RecordOffOnStop sw;
  156. // on: recording while playing, then playback stops → the switch goes off once
  157. CHECK (! sw.update (false, true, true)); // armed, not playing yet
  158. CHECK (! sw.update (true, true, true));
  159. CHECK (! sw.update (true, true, true));
  160. CHECK (sw.update (false, true, true));
  161. CHECK (! sw.update (false, true, true));
  162. // off: nothing
  163. CHECK (! sw.update (true, true, false));
  164. CHECK (! sw.update (false, true, false));
  165. // playing without recording, then arming while stopped: nothing
  166. CHECK (! sw.update (true, false, true));
  167. CHECK (! sw.update (false, false, true));
  168. CHECK (! sw.update (false, true, true));
  169. }
  170. TEST (round166e_note_template_and_nearest_note)
  171. {
  172. using namespace lynx;
  173. Note clicked;
  174. clicked.velocity = 0.3f;
  175. clicked.pan = -0.5f;
  176. clicked.release = 0.9f;
  177. clicked.finePitch = 20.0f;
  178. clicked.modX = 0.1f;
  179. clicked.modY = 0.8f;
  180. noteedit::NoteTemplate t;
  181. Note fresh;
  182. t.applyTo (fresh); // the defaults before any click
  183. CHECK (fresh.velocity == Note {}.velocity && fresh.pan == 0.0f);
  184. t.takeFrom (clicked);
  185. t.applyTo (fresh);
  186. CHECK (fresh.velocity == 0.3f && fresh.pan == -0.5f && fresh.release == 0.9f && fresh.finePitch == 20.0f && fresh.modX == 0.1f && fresh.modY == 0.8f);
  187. std::vector<Note> notes (3);
  188. notes[0].start = 0, notes[0].length = 96, notes[0].key = 60;
  189. notes[1].start = 960, notes[1].length = 96, notes[1].key = 72;
  190. notes[2].start = 480, notes[2].length = 96, notes[2].key = 40;
  191. const std::vector<int> all { 0, 1, 2 };
  192. const auto ppt = 24.0 / ticksPerStep;
  193. CHECK (noteedit::nearestNote (notes, all, 30.0, 60, ppt, 14.0) == 0); // on it
  194. CHECK (noteedit::nearestNote (notes, all, 900.0, 70, ppt, 14.0) == 1); // near the second
  195. CHECK (noteedit::nearestNote (notes, all, 520.0, 45, ppt, 14.0) == 2);
  196. CHECK (noteedit::nearestNote (notes, { 0, 1 }, 520.0, 45, ppt, 14.0) >= 0); // only candidates count
  197. CHECK (noteedit::nearestNote (notes, { 1 }, 0.0, 60, ppt, 14.0) == 1);
  198. CHECK (noteedit::nearestNote (notes, {}, 0.0, 60, ppt, 14.0) == -1);
  199. CHECK (noteedit::nearestNote (notes, all, 500.0, 127, ppt, 0.0) == 2); // the velocity lane: by time only
  200. }