Issue208Tests.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // --- Round 190ma: issue #208, the Channel Rack graph editor's Note pitch, Shift and Repeat (FL Studio 2026) -----------
  2. #include "core/ChannelBurn.h"
  3. #include "core/FlpExport.h"
  4. #include "core/FlpImport.h"
  5. #include "core/NoteTools.h"
  6. #include "core/RackOps.h"
  7. #include "core/Scheduler.h"
  8. #include "core/NoteRepeat.h"
  9. namespace i208
  10. {
  11. lynx::Note step (int index, int repeat = 0, std::int64_t length = lynx::ticksPerStep)
  12. {
  13. lynx::Note n;
  14. n.start = (std::int64_t) index * lynx::ticksPerStep;
  15. n.length = length;
  16. n.repeat = repeat;
  17. return n;
  18. }
  19. std::vector<std::int64_t> strikes (const lynx::Note& n)
  20. {
  21. std::vector<std::int64_t> out;
  22. lynx::noterepeat::forEachStrike (n, n.start, n.length, [&] (std::int64_t at, std::int64_t, int) { out.push_back (at); });
  23. return out;
  24. }
  25. } // namespace i208
  26. TEST (i208_repeat_rates_are_fls_table)
  27. {
  28. using namespace lynx::noterepeat;
  29. // FL's table (FLEngine 0x154a138): index 0 = none, then 1/4 beat ... 1/64 triplet
  30. CHECK (std::string (rateName (0)) == "No repeat" && std::string (rateName (1)) == "1/4 (Beat)");
  31. CHECK (std::string (rateName (7)) == "1/16 (Step)" && std::string (rateName (10)) == "1/32 (Half step)");
  32. CHECK (std::string (rateName (14)) == "1/64 triplet" && std::string (rateName (15)).empty());
  33. CHECK (interval (0) == 0 && interval (1) == lynx::ppq && interval (4) == lynx::ppq / 2);
  34. CHECK (interval (7) == lynx::ticksPerStep && interval (10) == lynx::ticksPerStep / 2 && interval (13) == lynx::ticksPerStep / 4);
  35. CHECK (interval (2) == lynx::ppq * 3 / 4 && interval (3) == lynx::ppq * 2 / 3 && interval (14) == lynx::ppq / 24);
  36. for (int i = 2; i <= rateCount; ++i)
  37. CHECK (interval (i) < interval (i - 1)); // (faster and faster)
  38. }
  39. TEST (i208_a_repeated_note_is_struck_at_its_rate_while_it_lasts)
  40. {
  41. using namespace lynx;
  42. // one beat at 1/16: four strikes, each to the next
  43. auto n = i208::step (0, 7, ppq);
  44. CHECK ((i208::strikes (n) == std::vector<std::int64_t> { 0, 240, 480, 720 }));
  45. // a one-step note at 1/32: two; at 1/64: four
  46. CHECK (i208::strikes (i208::step (2, 10)).size() == 2 && i208::strikes (i208::step (2, 13)).size() == 4);
  47. // no repeat, or a rate slower than the note: the note itself
  48. CHECK ((i208::strikes (i208::step (3, 0)) == std::vector<std::int64_t> { 720 }));
  49. CHECK ((i208::strikes (i208::step (3, 1)) == std::vector<std::int64_t> { 720 }));
  50. CHECK (noterepeat::strikeCount (n, n.length) == 4 && noterepeat::strikeCount (i208::step (0, 0), ticksPerStep) == 1);
  51. // the strikes' lengths add up to the note's
  52. std::int64_t total = 0;
  53. noterepeat::forEachStrike (n, 0, 1000, [&] (std::int64_t, std::int64_t length, int) { total += length; });
  54. CHECK (total == 1000);
  55. }
  56. TEST (i208_scheduler_plays_every_strike)
  57. {
  58. using namespace lynx;
  59. Pattern p;
  60. p.notes.push_back (i208::step (0, 7, ppq)); // one beat at 1/16
  61. const auto tps = 960.0 * 2.0 / 44100.0; // 120 BPM, 44.1 kHz
  62. EventBuffer out;
  63. schedulePatternLoop (p, 3840, 0.0, 24000, tps, out); // (past the note's end: its last "off" too)
  64. std::vector<int> ons, offs;
  65. for (const auto& e : out)
  66. (e.type == NoteEvent::Type::on ? ons : offs).push_back (e.sampleOffset);
  67. CHECK (ons.size() == 4 && offs.size() == 4);
  68. if (ons.size() == 4)
  69. for (int k = 0; k < 4; ++k)
  70. CHECK (std::abs (ons[(std::size_t) k] - (int) std::lround (k * 240 / tps)) <= 1);
  71. // an "off" and the next "on" on the same sample: the off first (the voice is cut, then struck again)
  72. for (int i = 1; i < out.size(); ++i)
  73. if (out[i].sampleOffset == out[i - 1].sampleOffset && out[i].type == NoteEvent::Type::off)
  74. CHECK (out[i - 1].type != NoteEvent::Type::on);
  75. }
  76. TEST (i208_scheduler_plays_strikes_of_a_playlist_clip)
  77. {
  78. using namespace lynx;
  79. auto song = Song::makeDefault();
  80. song.patterns.clear();
  81. Pattern p;
  82. p.notes.push_back (i208::step (0, 10)); // one step at 1/32: two strikes
  83. song.patterns.push_back (p);
  84. song.playlist.clear();
  85. Clip c;
  86. c.kind = ClipKind::pattern;
  87. c.pattern = 0;
  88. c.start = 0;
  89. c.length = ppq * 4;
  90. song.playlist.push_back (c);
  91. const auto tps = 960.0 * 2.0 / 44100.0;
  92. EventBuffer out;
  93. scheduleSong (song, 0.0, 8192, tps, out);
  94. int ons = 0;
  95. for (const auto& e : out)
  96. ons += e.type == NoteEvent::Type::on ? 1 : 0;
  97. CHECK (ons == 2);
  98. }
  99. TEST (i208_graph_lanes_in_fls_order)
  100. {
  101. using namespace lynx::notetools;
  102. const Level fl[] { Level::notePitch, Level::velocity, Level::release, Level::finePitch, Level::pan, Level::modX, Level::modY, Level::shift, Level::repeat };
  103. for (int tab = 0; tab < stepLevelCount; ++tab)
  104. CHECK (stepLevel (tab) == fl[tab]);
  105. CHECK (std::string (stepLevelName (0)) == "Note pitch" && std::string (stepLevelName (2)) == "Release velocity");
  106. CHECK (std::string (stepLevelName (4)) == "Panning" && std::string (stepLevelName (8)) == "Repeat");
  107. CHECK (std::string (stepLevelName (2, 1)) == "Release" && std::string (stepLevelName (8, 1)) == "Repeats");
  108. CHECK (std::string (stepLevelName (0, 0)) == "Note" && std::string (stepLevelName (8, 0)) == "Rep" && std::string (stepLevelName (5, 0)) == "X");
  109. // round 190lx's lane ids and order (its Channel Rack code reads these)
  110. CHECK (graphNote == 6 && graphShift == 7 && graphRepeat == 8 && graphLaneCount == 9);
  111. for (int tab = 0; tab < graphLaneCount; ++tab)
  112. CHECK (graphTabOrder[tab] == (int) stepLevel (tab));
  113. lynx::Note n;
  114. setGraphValue (n, graphShift, 0.5f);
  115. CHECK (std::abs (graphValue (n, graphShift) - 0.5f) < 0.01f && graphDefault (graphShift) == 0.0f && ! graphIsBipolar (graphShift));
  116. CHECK (std::string (graphLaneName (graphRepeat)) == "Repeat");
  117. }
  118. TEST (i208_note_pitch_shift_and_repeat_levels)
  119. {
  120. using namespace lynx;
  121. using namespace lynx::notetools;
  122. auto n = i208::step (5);
  123. // Note pitch: the key
  124. setLevelValue (n, Level::notePitch, 72.0f / 127.0f);
  125. CHECK (n.key == 72 && std::abs (levelValue (n, Level::notePitch) - 72.0f / 127.0f) < 1e-6f);
  126. CHECK (levelText (Level::notePitch, 60.0f / 127.0f) == "C5"); // (FL's C5 = 60)
  127. // Shift: the note moves inside its step, never into the next one
  128. // (FL's manual: 100 % shift is 99 % of a step; round 190lx's mapping too)
  129. setLevelValue (n, Level::shift, 0.5f);
  130. CHECK (n.start == 5 * ticksPerStep + std::llround (0.5 * 0.99 * ticksPerStep));
  131. setLevelValue (n, Level::shift, 1.0f);
  132. CHECK (n.start == 5 * ticksPerStep + std::llround (0.99 * ticksPerStep) && n.start < 6 * ticksPerStep);
  133. CHECK (std::abs (levelValue (n, Level::shift) - 1.0f) < 1e-3f);
  134. setLevelValue (n, Level::shift, 0.0f);
  135. CHECK (n.start == 5 * ticksPerStep && levelDefault (Level::shift) == 0.0f);
  136. CHECK (levelText (Level::shift, 0.25f) == "0.25");
  137. // Repeat: FL's rate index
  138. setLevelValue (n, Level::repeat, 12.0f / 14.0f);
  139. CHECK (n.repeat == 12 && levelText (Level::repeat, 12.0f / 14.0f) == "1/32 triplet");
  140. CHECK (levelDefault (Level::repeat) == 0.0f && levelText (Level::repeat, 0.0f) == "No repeat");
  141. CHECK (levelText (Level::velocity, 0.5f).empty()); // (the other lanes: the caller's own text)
  142. // a shifted step is still that step's note (the Channel Rack finds it there)
  143. Pattern p;
  144. p.notes.push_back (n);
  145. setLevelValue (p.notes[0], Level::shift, 0.9f);
  146. CHECK (notesInStep (p, 0, 5).size() == 1 && notesInStep (p, 0, 6).empty());
  147. }
  148. TEST (i208_flp_round_trip_of_repeat_and_levels)
  149. {
  150. using namespace lynx;
  151. auto song = Song::makeDefault();
  152. song.patterns.clear();
  153. Pattern p;
  154. auto a = i208::step (0, 12);
  155. a.release = 0.25f;
  156. a.modX = 0.75f;
  157. a.modY = 0.125f;
  158. auto b = i208::step (1, 4);
  159. b.start += 60; // (shifted: a quarter step)
  160. b.key = 67;
  161. p.notes = { a, b };
  162. song.patterns.push_back (p);
  163. flpexport::Options options;
  164. flpexport::Report report;
  165. const auto project = flp::parseProject (flpexport::exportFlp (song, options, report));
  166. bool found12 = false, found4 = false;
  167. for (const auto& fp : project.patterns)
  168. for (const auto& n : fp.notes)
  169. {
  170. found12 = found12 || n.repeats == 12;
  171. found4 = found4 || (n.repeats == 4 && n.key == 67);
  172. }
  173. CHECK (found12 && found4);
  174. FlpImportOptions io;
  175. io.fileExists = [] (const std::string&) { return true; };
  176. const auto back = importFlp (project, io);
  177. bool ok12 = false, ok4 = false;
  178. for (const auto& bp : back.patterns)
  179. for (const auto& n : bp.notes)
  180. {
  181. if (n.repeat == 12)
  182. ok12 = std::abs (n.release - 0.25f) < 0.01f && std::abs (n.modX - 0.75f) < 0.01f && std::abs (n.modY - 0.125f) < 0.01f;
  183. if (n.repeat == 4)
  184. ok4 = n.key == 67 && n.start == ticksPerStep + 60;
  185. }
  186. CHECK (ok12 && ok4);
  187. }
  188. TEST (i208_burn_to_midi_writes_each_strike)
  189. {
  190. using namespace lynx;
  191. auto song = Song::makeDefault();
  192. song.patterns.clear();
  193. Pattern p;
  194. p.notes.push_back (i208::step (0, 7, ppq)); // four strikes
  195. song.patterns.push_back (p);
  196. const auto played = channelburn::playedNotes (song, 0, 0);
  197. CHECK (played.size() == 4);
  198. for (const auto& n : played)
  199. CHECK (n.repeat == 0 && n.length == ticksPerStep);
  200. }