Iss158Tests.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Issue #158 (round 190eh): overlapping pattern clips. FL 2026, 25 command-line renders (/mnt/e/lynx-work/iss158/,
  2. // HANDOFF.md): a pattern plays from one Playlist clip at a time — a later clip of the SAME pattern (any track; the same
  3. // start: the one later in the list) takes over at its start, the earlier clip starts no more notes from there (also after
  4. // the later clip has ended), the notes it already started play to their ends. Clips of different patterns never cut each
  5. // other; muted clips and clips on muted tracks take nothing over (Song::patternClipTakeover, Scheduler).
  6. #include "core/ExportOptions.h"
  7. namespace iss158
  8. {
  9. constexpr std::int64_t bar = 4 * lynx::ppq;
  10. // Patterns as in the FL renders: P1 = channel 0 key 60 held 8 bars + channel 1 key 72 a short note on every beat; P2 = the
  11. // same channels with keys 67 / 79; P3 = channels 2 / 3, keys 64 / 76; P4 = channel 1 key 72 on every beat + channel 2 key
  12. // 76 on every beat of bars 4-8 only.
  13. inline lynx::Song song (bool takeover = true)
  14. {
  15. using namespace lynx;
  16. Song s;
  17. s.bpm = 120.0;
  18. s.mode = PlayMode::song;
  19. s.channels.resize (4);
  20. s.patterns.resize (4);
  21. s.playlistTracks.resize (4);
  22. s.patternClipTakeover = takeover;
  23. const auto make = [] (Pattern& p, int heldChannel, int heldKey, int pulseChannel, int pulseKey) {
  24. Note h;
  25. h.start = 0;
  26. h.length = 8 * bar;
  27. h.channel = heldChannel;
  28. h.key = heldKey;
  29. p.notes.push_back (h);
  30. for (int b = 0; b < 32; ++b)
  31. {
  32. Note n;
  33. n.start = b * ppq;
  34. n.length = ppq / 4;
  35. n.channel = pulseChannel;
  36. n.key = pulseKey;
  37. p.notes.push_back (n);
  38. }
  39. };
  40. make (s.patterns[0], 0, 60, 1, 72);
  41. make (s.patterns[1], 0, 67, 1, 79);
  42. make (s.patterns[2], 2, 64, 3, 76);
  43. for (int b = 0; b < 32; ++b)
  44. {
  45. Note n;
  46. n.start = b * ppq;
  47. n.length = ppq / 4;
  48. n.channel = 1;
  49. n.key = 72;
  50. s.patterns[3].notes.push_back (n);
  51. if (b >= 16)
  52. {
  53. n.channel = 2;
  54. n.key = 76;
  55. s.patterns[3].notes.push_back (n);
  56. }
  57. }
  58. for (auto& p : s.patterns)
  59. std::stable_sort (p.notes.begin(), p.notes.end(), [] (const Note& a, const Note& b) { return a.start < b.start; });
  60. return s;
  61. }
  62. inline lynx::Clip clip (int pattern, std::int64_t startBar, std::int64_t bars, int track = 0, std::int64_t offset = 0)
  63. {
  64. lynx::Clip c;
  65. c.kind = lynx::ClipKind::pattern;
  66. c.pattern = pattern;
  67. c.start = startBar * bar;
  68. c.length = bars * bar;
  69. c.track = track;
  70. c.offset = offset;
  71. return c;
  72. }
  73. struct Played
  74. {
  75. lynx::NoteEvent::Type type;
  76. int channel, key;
  77. std::int64_t tick;
  78. };
  79. // The whole song played once in 512-sample blocks at 48 kHz; each event at its tick. With or without the index.
  80. inline std::vector<Played> play (const lynx::Song& s, bool withIndex = true)
  81. {
  82. using namespace lynx;
  83. const auto tps = ticksPerSample (s.bpm, 48000.0);
  84. const auto index = makeScheduleIndex (s);
  85. std::vector<Played> out;
  86. const auto end = (double) (s.songLength() + bar);
  87. for (double t = 0.0; t < end; t += 512 * tps)
  88. {
  89. EventBuffer ev;
  90. scheduleSong (s, t, 512, tps, ev, t == 0.0, withIndex ? &index : nullptr, false);
  91. for (const auto& e : ev)
  92. out.push_back ({ e.type, e.channel, e.key, (std::int64_t) std::llround (t + e.sampleOffset * tps) });
  93. }
  94. return out;
  95. }
  96. inline int count (const std::vector<Played>& p, lynx::NoteEvent::Type type, int key, std::int64_t from = 0,
  97. std::int64_t to = std::numeric_limits<std::int64_t>::max())
  98. {
  99. return (int) std::count_if (p.begin(), p.end(), [&] (const Played& e) { return e.type == type && e.key == key && e.tick >= from && e.tick < to; });
  100. }
  101. inline int ons (const std::vector<Played>& p, int key, std::int64_t from = 0, std::int64_t to = std::numeric_limits<std::int64_t>::max())
  102. {
  103. return count (p, lynx::NoteEvent::Type::on, key, from, to);
  104. }
  105. inline std::vector<std::int64_t> offsOf (const std::vector<Played>& p, int key)
  106. {
  107. std::vector<std::int64_t> t;
  108. for (const auto& e : p)
  109. if (e.type == lynx::NoteEvent::Type::off && e.key == key)
  110. t.push_back (e.tick);
  111. std::sort (t.begin(), t.end());
  112. return t;
  113. }
  114. } // namespace iss158
  115. TEST (iss158_a_later_clip_of_the_same_pattern_takes_over_on_its_track)
  116. {
  117. // FL e_same_pat_same_track: P1 0-4 bars and P1 2-6 bars on one track. The beat notes play once per beat (-14.2 dB, not
  118. // +6 dB), from bar 2 the later clip's; the earlier clip's held note goes on to its own end (bar 4) beside the later one's
  119. using namespace iss158;
  120. auto s = song();
  121. s.playlist = { clip (0, 0, 4), clip (0, 2, 4) };
  122. const auto p = play (s);
  123. CHECK (ons (p, 72) == 8 + 16); // the earlier clip's beats 0-7, the later clip's 16
  124. CHECK (ons (p, 72, 2 * bar, 4 * bar) == 8); // one note per beat where both clips lie
  125. CHECK (ons (p, 60) == 2);
  126. CHECK ((offsOf (p, 60) == std::vector<std::int64_t> { 4 * bar, 6 * bar }));
  127. // LYNX before (and projects saved before, the option off): both clips play every note
  128. auto legacy = song (false);
  129. legacy.playlist = s.playlist;
  130. CHECK (ons (play (legacy), 72) == 32);
  131. }
  132. TEST (iss158_the_earlier_clip_does_not_come_back_after_the_later_one_ends)
  133. {
  134. // FL h_inside_same_pat / m (other track) / v (offset): P1 0-8 bars with P1 2-4 bars inside it — after bar 4 nothing of
  135. // P1 starts any more (silence where the outer clip still lies); the outer held note ends at its own end (bar 8)
  136. using namespace iss158;
  137. for (const auto track : { 0, 1 })
  138. for (const auto offset : { (std::int64_t) 0, bar })
  139. {
  140. auto s = song();
  141. s.playlist = { clip (0, 0, 8), clip (0, 2, 2, track, offset) };
  142. const auto p = play (s);
  143. CHECK (ons (p, 72, 0, 2 * bar) == 8);
  144. CHECK (ons (p, 72, 2 * bar, 4 * bar) == 8);
  145. CHECK (ons (p, 72, 4 * bar) == 0);
  146. CHECK (ons (p, 60) == (offset == 0 ? 2 : 1)); // (the inner clip with an offset starts after the held note)
  147. CHECK (offsOf (p, 60).back() == 8 * bar);
  148. }
  149. }
  150. TEST (iss158_different_patterns_never_cut_each_other)
  151. {
  152. // FL a / b / c / d / g / i / j / k / l: overlapping clips of different patterns (same or other channels, same or other
  153. // track, inside, the same start, either list order) all play every note
  154. using namespace iss158;
  155. const std::vector<std::vector<lynx::Clip>> cases {
  156. { clip (0, 0, 4), clip (1, 2, 4) }, { clip (0, 0, 4), clip (1, 2, 4, 1) }, { clip (0, 0, 4), clip (2, 2, 4) },
  157. { clip (0, 0, 8), clip (1, 2, 2) }, { clip (1, 2, 4), clip (0, 0, 4) }, { clip (0, 0, 4), clip (1, 0, 4) },
  158. { clip (0, 0, 4, 1), clip (1, 2, 4) }, { clip (0, 0, 8), clip (2, 2, 2) },
  159. };
  160. for (const auto& clips : cases)
  161. {
  162. auto s = song();
  163. s.playlist = clips;
  164. auto legacy = song (false);
  165. legacy.playlist = clips;
  166. const auto p = play (s);
  167. const auto q = play (legacy);
  168. for (const auto key : { 60, 67, 64, 72, 79, 76 })
  169. {
  170. CHECK (ons (p, key) == ons (q, key));
  171. CHECK (offsOf (p, key) == offsOf (q, key));
  172. }
  173. CHECK (ons (p, 72) == (int) (clips[clips[0].pattern == 0 ? 0 : 1].length / lynx::ppq));
  174. }
  175. }
  176. TEST (iss158_the_same_start_goes_to_the_clip_later_in_the_list)
  177. {
  178. // FL n1-n6: two clips of P1 at the same start (4 and 8 bars, same or other tracks): only the one later in the item list
  179. // plays (n1 / n3 / n6: the 8-bar one, beats to 16 s; n2 / n4 / n5: the 4-bar one, all silent from 8 s), one held note
  180. using namespace iss158;
  181. for (const auto tracks : { std::pair { 0, 0 }, std::pair { 0, 1 }, std::pair { 1, 0 } })
  182. {
  183. auto s = song();
  184. s.playlist = { clip (0, 0, 4, tracks.first), clip (0, 0, 8, tracks.second) };
  185. auto p = play (s);
  186. CHECK (ons (p, 72) == 32);
  187. CHECK (ons (p, 60) == 1);
  188. CHECK ((offsOf (p, 60) == std::vector<std::int64_t> { 8 * bar }));
  189. std::swap (s.playlist[0], s.playlist[1]);
  190. p = play (s);
  191. CHECK (ons (p, 72) == 16);
  192. CHECK (ons (p, 60) == 1);
  193. CHECK ((offsOf (p, 60) == std::vector<std::int64_t> { 4 * bar }));
  194. }
  195. }
  196. TEST (iss158_a_muted_clip_or_track_takes_nothing_over)
  197. {
  198. // FL q_inner_on_muted_track / x_inner_clip_muted: the outer clip plays all its beats
  199. using namespace iss158;
  200. for (const auto viaTrack : { false, true })
  201. {
  202. auto s = song();
  203. s.playlist = { clip (0, 0, 8), clip (0, 2, 2, 1) };
  204. if (viaTrack)
  205. s.playlistTracks[1].muted = true;
  206. else
  207. s.playlist[1].muted = true;
  208. const auto p = play (s);
  209. CHECK (ons (p, 72) == 32);
  210. CHECK (ons (p, 60) == 1);
  211. // unmuted again: the inner clip takes over (the index keeps the chain, the mute is read live)
  212. const auto index = lynx::makeScheduleIndex (s);
  213. s.playlistTracks[1].muted = false;
  214. s.playlist[1].muted = false;
  215. CHECK (lynx::patternClipStopsAt (s, 0, &index) == 2 * bar);
  216. CHECK (lynx::patternClipStopsAt (s, 0) == 2 * bar);
  217. }
  218. }
  219. TEST (iss158_takeover_is_per_pattern_and_the_boundary_is_inclusive)
  220. {
  221. using namespace iss158;
  222. // FL u_per_channel: P4's channel-2 notes (bars 4-8 of the pattern) never play: the outer clip stopped at bar 2, the
  223. // inner clip (pattern bars 0-2) has none
  224. {
  225. auto s = song();
  226. s.playlist = { clip (3, 0, 8), clip (3, 2, 2) };
  227. const auto p = play (s);
  228. CHECK (ons (p, 76) == 0);
  229. CHECK (ons (p, 72) == 16);
  230. }
  231. // FL y_boundary: the inner clip starts at bar 2 half a beat into the pattern (its first note half a beat later); the
  232. // outer clip's note exactly at bar 2 does not play
  233. {
  234. auto s = song();
  235. s.playlist = { clip (0, 0, 8), clip (0, 2, 2, 0, lynx::ppq / 2) };
  236. const auto p = play (s);
  237. CHECK (ons (p, 72, 2 * bar, 2 * bar + 1) == 0);
  238. CHECK (ons (p, 72, 2 * bar - lynx::ppq, 2 * bar) == 1);
  239. CHECK (ons (p, 72, 2 * bar + lynx::ppq / 2, 2 * bar + lynx::ppq / 2 + 1) == 1);
  240. }
  241. // FL w_earlier_ends_first: P1 0-3 bars, P1 2-6 bars: the earlier held note ends at bar 3, the later one at bar 6
  242. {
  243. auto s = song();
  244. s.playlist = { clip (0, 0, 3), clip (0, 2, 4) };
  245. const auto p = play (s);
  246. CHECK ((offsOf (p, 60) == std::vector<std::int64_t> { 3 * bar, 6 * bar }));
  247. CHECK (ons (p, 72) == 8 + 16);
  248. }
  249. }
  250. TEST (iss158_the_index_and_the_scan_agree)
  251. {
  252. using namespace iss158;
  253. auto s = song();
  254. s.playlist = { clip (0, 0, 8), clip (1, 1, 4, 1), clip (0, 2, 2, 2), clip (0, 2, 4, 1), clip (0, 6, 2), clip (2, 0, 8) };
  255. const auto index = lynx::makeScheduleIndex (s);
  256. for (std::size_t i = 0; i < s.playlist.size(); ++i)
  257. CHECK (lynx::patternClipStopsAt (s, i, &index) == lynx::patternClipStopsAt (s, i));
  258. CHECK (lynx::patternClipStopsAt (s, 0, &index) == 2 * iss158::bar);
  259. CHECK (lynx::patternClipStopsAt (s, 2, &index) == 2 * iss158::bar); // (the same start: the clip later in the list)
  260. CHECK (lynx::patternClipStopsAt (s, 3, &index) == 6 * iss158::bar);
  261. CHECK (lynx::patternClipStopsAt (s, 4, &index) == std::numeric_limits<std::int64_t>::max());
  262. CHECK (lynx::patternClipStopsAt (s, 1, &index) == std::numeric_limits<std::int64_t>::max());
  263. const auto a = play (s, true), b = play (s, false);
  264. CHECK (a.size() == b.size());
  265. for (const auto key : { 60, 67, 64, 72, 79, 76 })
  266. CHECK (ons (a, key) == ons (b, key) && offsOf (a, key) == offsOf (b, key));
  267. s.patternClipTakeover = false;
  268. CHECK (lynx::patternClipStopsAt (s, 0, &index) == std::numeric_limits<std::int64_t>::max());
  269. }
  270. TEST (iss158_midi_export_writes_what_plays)
  271. {
  272. using namespace iss158;
  273. auto s = song();
  274. s.playlist = { clip (0, 0, 8), clip (0, 2, 2) };
  275. const auto notes = lynx::exportopts::midiExportNotes (s);
  276. const auto beats = std::count_if (notes.begin(), notes.end(), [] (const lynx::Note& n) { return n.key == 72; });
  277. CHECK (beats == 16);
  278. s.patternClipTakeover = false;
  279. const auto all = lynx::exportopts::midiExportNotes (s);
  280. CHECK (std::count_if (all.begin(), all.end(), [] (const lynx::Note& n) { return n.key == 72; }) == 32 + 8);
  281. }
  282. TEST (iss158_flp_import_turns_it_on_and_keeps_the_file_order)
  283. {
  284. // an .flp plays as in FL: the option is on, and two clips of a pattern at the same start keep the file's order (the
  285. // later one wins)
  286. using namespace lynx;
  287. flp::Project p;
  288. p.tempo = 120.0;
  289. flp::Channel c;
  290. c.index = 0;
  291. c.type = flp::ChannelType::sampler;
  292. c.name = "Tone";
  293. c.samplePath = "C:/t.wav";
  294. p.channels.push_back (c);
  295. flp::Pattern pat;
  296. pat.index = 1;
  297. for (int k = 0; k < 16; ++k)
  298. {
  299. flp::Note n;
  300. n.position = 96 * k;
  301. n.length = 24;
  302. n.key = 72;
  303. n.velocity = 100;
  304. pat.notes.push_back (n);
  305. }
  306. p.patterns.push_back (pat);
  307. flp::Arrangement a;
  308. for (const auto bars : { 4u, 2u, 1u })
  309. {
  310. flp::PlaylistItem item;
  311. item.position = 0;
  312. item.length = 384 * bars;
  313. item.index = 1;
  314. item.track = (int) bars;
  315. a.items.push_back (item);
  316. }
  317. p.arrangements.push_back (a);
  318. FlpImportOptions fo;
  319. fo.fileExists = [] (const std::string&) { return true; };
  320. const auto s = importFlp (flp::parseProject (flp::writeProject (p)), fo);
  321. CHECK (s.patternClipTakeover);
  322. CHECK (s.playlist.size() == 3);
  323. if (s.playlist.size() == 3)
  324. {
  325. CHECK (s.playlist[0].length > s.playlist[1].length && s.playlist[1].length > s.playlist[2].length);
  326. CHECK (patternClipStopsAt (s, 0) == 0 && patternClipStopsAt (s, 1) == 0);
  327. CHECK (patternClipStopsAt (s, 2) == std::numeric_limits<std::int64_t>::max());
  328. }
  329. }