Issue204Tests.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Round 190lq (issue #204): an audio clip channel played from the Piano roll / steps, as FL 2026 plays it (FL64 /R renders of
  2. // the same notes: /mnt/e/lynx-work/issue204_190lq, docs in the round's HANDOFF): the whole clip whatever the note's length,
  3. // overlapping notes layer (the second note no longer ends where the first one did), and the key transposes the clip from
  4. // C5. Projects saved before the law (SamplerSettings::clipNoteLegacy) keep their earlier sound: each note cut at its end,
  5. // at the clip's own pitch — but only that note's voice.
  6. namespace i204
  7. {
  8. constexpr double rate = 48000.0;
  9. // a 1 s sine (0.25, 440 Hz)
  10. std::shared_ptr<lynx::SampleData> sine()
  11. {
  12. auto d = std::make_shared<lynx::SampleData>();
  13. d->sampleRate = rate;
  14. d->left.resize ((std::size_t) rate);
  15. for (std::size_t i = 0; i < d->left.size(); ++i)
  16. d->left[i] = 0.25f * (float) std::sin (6.283185307179586 * 440.0 * (double) i / rate);
  17. d->right = d->left;
  18. return d;
  19. }
  20. struct N
  21. {
  22. double beat, beats;
  23. int key;
  24. float velocity = 100.0f / 128.0f;
  25. };
  26. // Song mode, 120 BPM (a beat = 0.5 s): channel 0 = an audio clip channel on the sine playing `notes` once
  27. lynx::Song song (const std::vector<N>& notes, bool legacy, lynx::InstrumentKind kind = lynx::InstrumentKind::audioClip, bool flVoice = false)
  28. {
  29. auto s = lynx::Song::makeDefault();
  30. s.bpm = 120.0;
  31. s.mode = lynx::PlayMode::song;
  32. lynx::ChannelDef c;
  33. c.instrument = kind;
  34. c.name = "Sine clip";
  35. c.samplePath = "sine";
  36. c.volume = lynx::audioClipUnityVolume;
  37. c.sampler.clipNoteLegacy = legacy;
  38. if (flVoice) // FL's native voice as the .flp import stores it (the plugins' default records, Mod X / Y, PPQ 96, pan law)
  39. {
  40. const auto defaults = lynx::flvp::defaultSnapshot();
  41. c.sampler.flChannel.assign (90, 0);
  42. std::memcpy (c.sampler.flChannel.data(), defaults.data(), 85 * 4);
  43. std::memcpy (&c.sampler.flChannel[85], defaults.data() + 0x15c, 4);
  44. std::memcpy (&c.sampler.flChannel[86], defaults.data() + 0x160, 4);
  45. std::memcpy (&c.sampler.flChannel[87], defaults.data() + 0x168, 4);
  46. c.sampler.flChannel[88] = 96;
  47. }
  48. s.channels = { c };
  49. lynx::Pattern p;
  50. p.name = "Notes";
  51. for (const auto& n : notes)
  52. {
  53. lynx::Note note;
  54. note.start = (std::int64_t) std::llround (n.beat * lynx::ppq);
  55. note.length = (std::int64_t) std::llround (n.beats * lynx::ppq);
  56. note.key = n.key;
  57. note.velocity = n.velocity;
  58. p.notes.push_back (note);
  59. }
  60. p.sortNotes();
  61. p.lengthTicks = 16 * lynx::ppq; // (8 s: the pattern does not loop inside the render)
  62. s.patterns = { p };
  63. lynx::Clip clip;
  64. clip.kind = lynx::ClipKind::pattern;
  65. clip.pattern = 0;
  66. clip.length = 16 * lynx::ppq; // (longer than the render: the pattern does not loop inside it)
  67. s.playlist = { clip };
  68. return s;
  69. }
  70. std::vector<float> render (const lynx::Song& s, double seconds)
  71. {
  72. const auto data = sine();
  73. auto state = lynx::RackState::build (s, nullptr, rate, [data] (const std::string&) -> std::shared_ptr<const lynx::SampleData> { return data; });
  74. lynx::RackRenderer rr;
  75. rr.prepare (rate, 480);
  76. const auto total = (int) (seconds * rate);
  77. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  78. double tick = 0.0;
  79. const auto tps = lynx::ticksPerSample (s.bpm, rate);
  80. for (int at = 0; at < total; at += 480)
  81. {
  82. const auto n = std::min (480, total - at);
  83. rr.process (state.get(), true, tick, n, l.data() + at, r.data() + at);
  84. tick += n * tps;
  85. }
  86. return l;
  87. }
  88. // the last 10 ms window above -60 dBFS (seconds)
  89. double soundsUntil (const std::vector<float>& x)
  90. {
  91. std::size_t last = 0;
  92. for (std::size_t w = 0; w + 480 <= x.size(); w += 480)
  93. {
  94. float peak = 0.0f;
  95. for (std::size_t i = w; i < w + 480; ++i)
  96. peak = std::max (peak, std::abs (x[i]));
  97. if (peak > 0.001f)
  98. last = w + 480;
  99. }
  100. return (double) last / rate;
  101. }
  102. float peak (const std::vector<float>& x, double from, double to)
  103. {
  104. float p = 0.0f;
  105. for (auto i = (std::size_t) (from * rate); i < std::min (x.size(), (std::size_t) (to * rate)); ++i)
  106. p = std::max (p, std::abs (x[i]));
  107. return p;
  108. }
  109. // frequency from the zero crossings between `from` and `to` seconds
  110. double hz (const std::vector<float>& x, double from, double to)
  111. {
  112. int crossings = 0;
  113. double first = -1.0, last = -1.0;
  114. for (auto i = (std::size_t) (from * rate) + 1; i < std::min (x.size(), (std::size_t) (to * rate)); ++i)
  115. if ((x[i - 1] < 0.0f) != (x[i] < 0.0f))
  116. {
  117. const auto t = (double) i / rate;
  118. if (first < 0.0)
  119. first = t;
  120. last = t;
  121. ++crossings;
  122. }
  123. return crossings > 2 ? (crossings - 1) / 2.0 / (last - first) : 0.0;
  124. }
  125. } // namespace i204
  126. // #204 item 1: a note shorter than the clip plays the whole clip (FL: 1.0 s of a 1 s clip for a 1/16 step and for a 3 s note)
  127. TEST (issue204_clip_note_plays_the_whole_clip)
  128. {
  129. using namespace i204;
  130. const auto shortNote = render (song ({ { 0.0, 0.25, 60 } }, false), 2.5);
  131. const auto longNote = render (song ({ { 0.0, 6.0, 60 } }, false), 2.5);
  132. std::printf ("issue204: a 1/16 note sounds %.3f s, a 3 s note %.3f s (clip 1 s)\n", soundsUntil (shortNote), soundsUntil (longNote));
  133. CHECK (soundsUntil (shortNote) > 0.98 && soundsUntil (shortNote) < 1.05);
  134. CHECK (soundsUntil (longNote) > 0.98 && soundsUntil (longNote) < 1.05); // (no loop, no DC hold past the sample)
  135. CHECK (std::abs (hz (shortNote, 0.1, 0.9) - 440.0) < 1.0);
  136. }
  137. // #204 item 2: two overlapping notes of one key layer; the second one plays its whole clip (it ended where the first did)
  138. TEST (issue204_overlapping_clip_notes_layer)
  139. {
  140. using namespace i204;
  141. // n1 at 0 for 0.5 s, n2 at 0.25 s for 1 s: n1 sounds 0…1 s, n2 0.25…1.25 s (in phase: 110 cycles later)
  142. const auto x = render (song ({ { 0.0, 1.0, 60 }, { 0.5, 2.0, 60 } }, false), 2.5);
  143. std::printf ("issue204: overlap peaks %.3f / %.3f / %.3f, sounds until %.3f s\n", peak (x, 0.05, 0.2), peak (x, 0.6, 0.9),
  144. peak (x, 1.05, 1.2), soundsUntil (x));
  145. const auto one = peak (x, 0.05, 0.2);
  146. CHECK (one > 0.05f);
  147. CHECK (std::abs (peak (x, 0.6, 0.9) - 2.0f * one) < 0.05f * one); // both voices
  148. CHECK (std::abs (peak (x, 1.05, 1.2) - one) < 0.03f * one); // the second voice alone, past the first note's end
  149. CHECK (soundsUntil (x) > 1.23 && soundsUntil (x) < 1.30);
  150. // the legacy law (projects saved before): each note cut at its own end, and only its own voice — n1 0…0.5 s, n2
  151. // 0.25…0.75 s: 0.1.29 ended both at 0.5 s (its note-off stopped every voice of the channel)
  152. const auto old = render (song ({ { 0.0, 1.0, 60 }, { 0.5, 1.0, 60 } }, true), 2.5);
  153. std::printf ("issue204: legacy overlap: %.3f after the first note's end, sounds until %.3f s (second note ends at 0.75 s)\n",
  154. peak (old, 0.55, 0.7), soundsUntil (old));
  155. CHECK (std::abs (peak (old, 0.55, 0.7) - peak (old, 0.05, 0.2)) < 0.03f * peak (old, 0.05, 0.2)); // the second voice alone
  156. CHECK (soundsUntil (old) > 0.74 && soundsUntil (old) < 0.80);
  157. }
  158. // #204 item 3: the key transposes the clip from C5, resampled (FL: A5 = 740 Hz for 0.595 s, C4 = 220 Hz for 2 s)
  159. TEST (issue204_clip_note_key_transposes)
  160. {
  161. using namespace i204;
  162. const auto a5 = render (song ({ { 0.0, 0.5, 69 } }, false), 2.5);
  163. const auto c6 = render (song ({ { 0.0, 0.5, 72 } }, false), 2.5);
  164. const auto c4 = render (song ({ { 0.0, 0.5, 48 } }, false), 2.5);
  165. std::printf ("issue204: A5 %.1f Hz %.3f s, C6 %.1f Hz %.3f s, C4 %.1f Hz %.3f s\n", hz (a5, 0.05, 0.5), soundsUntil (a5),
  166. hz (c6, 0.05, 0.45), soundsUntil (c6), hz (c4, 0.1, 1.5), soundsUntil (c4));
  167. CHECK (std::abs (hz (a5, 0.05, 0.5) - 739.99) < 1.5);
  168. CHECK (std::abs (hz (c6, 0.05, 0.45) - 880.0) < 1.5);
  169. CHECK (std::abs (hz (c4, 0.1, 1.5) - 220.0) < 1.0);
  170. CHECK (soundsUntil (c6) > 0.49 && soundsUntil (c6) < 0.53);
  171. CHECK (soundsUntil (c4) > 1.98 && soundsUntil (c4) < 2.05);
  172. // the legacy law: every key at the clip's own pitch
  173. const auto old = render (song ({ { 0.0, 0.5, 69 } }, true), 2.5);
  174. CHECK (std::abs (hz (old, 0.05, 0.2) - 440.0) < 1.0);
  175. }
  176. // FL 2026's renders of the same notes on an audio clip channel and on a Sampler of the same file are bit-identical (round
  177. // 190lq: lengths, overlaps, keys, velocities 50 / 100 / 128). LYNX: the same, with FL's voice (.flp imports) and without
  178. TEST (issue204_clip_notes_play_as_the_sampler)
  179. {
  180. using namespace i204;
  181. using lynx::InstrumentKind;
  182. const std::vector<N> notes { { 0.0, 0.25, 60 }, { 2.0, 1.0, 60 }, { 2.5, 2.0, 60 }, { 4.0, 1.0, 60 }, { 4.5, 2.0, 67 },
  183. { 6.0, 0.5, 69 }, { 7.0, 0.5, 48 }, { 8.0, 0.5, 60, 50.0f / 128.0f }, { 9.0, 0.5, 61, 1.0f } };
  184. for (const auto flVoice : { false, true })
  185. {
  186. const auto clip = render (song (notes, false, InstrumentKind::audioClip, flVoice), 6.0);
  187. const auto sampler = render (song (notes, false, InstrumentKind::sampler, flVoice), 6.0);
  188. float diff = 0.0f, level = 0.0f;
  189. for (std::size_t i = 0; i < clip.size(); ++i)
  190. {
  191. diff = std::max (diff, std::abs (clip[i] - sampler[i]));
  192. level = std::max (level, std::abs (sampler[i]));
  193. }
  194. std::printf ("issue204: audio clip channel vs Sampler (%s voice): max difference %.3g, peak %.3f\n", flVoice ? "FL's" : "LYNX's",
  195. (double) diff, (double) level);
  196. CHECK (level > 0.05f);
  197. CHECK (diff < 1.0e-6f);
  198. }
  199. // FL's voice: velocity 50 / 100 / 128 = 0.023 / 0.097 / 0.200 of a 0.25 sine at the channel's 10000 (FL's renders)
  200. const auto fl = render (song ({ { 0.0, 0.5, 60, 50.0f / 128.0f }, { 2.0, 0.5, 60 }, { 4.0, 0.5, 60, 1.0f } }, false, InstrumentKind::audioClip, true), 2.5);
  201. std::printf ("issue204: FL voice velocity 50 / 100 / 128: %.4f / %.4f / %.4f\n", (double) peak (fl, 0.1, 0.4), (double) peak (fl, 1.1, 1.4),
  202. (double) peak (fl, 2.1, 2.4));
  203. }