Issues173aTests.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Round 173a: issues #70, #72, #73 (docs/analise/REA_rodada173a.md)
  2. namespace i173a
  3. {
  4. // a dense piano part as in the #70 video: chords strummed over a bar, each chord note held to the bar's end, a key
  5. // played again right where its previous note ends, and more than 20 notes in the pattern
  6. std::vector<lynx::Note> videoPart()
  7. {
  8. using lynx::ppq;
  9. std::vector<lynx::Note> notes;
  10. const auto add = [&] (double beat, double beats, int key) { notes.push_back (i32d::Rig::note ((std::int64_t) (beat * ppq), (std::int64_t) (beats * ppq), key)); };
  11. for (int bar = 0; bar < 2; ++bar)
  12. {
  13. const auto b = bar * 4.0;
  14. add (b + 0.0, 4.0, 43); // G2 held the whole bar
  15. add (b + 0.5, 2.5, 50), add (b + 3.0, 1.0, 50); // D3, then D3 again where it ends
  16. add (b + 1.0, 3.0, 55), add (b + 1.0, 3.0, 59); // a chord
  17. add (b + 1.05, 2.95, 60), add (b + 1.5, 2.5, 66); // strummed: one sample block later
  18. add (b + 2.0, 2.0, 69), add (b + 3.0, 1.0, 62);
  19. add (b + 2.25, 0.75, 64), add (b + 3.0, 1.0, 64); // E3 twice, end to start
  20. add (b + 3.5, 0.5, 71);
  21. }
  22. return notes;
  23. }
  24. // FL's SoundFont Player state, version 6 (as tests/SoundFont158Tests.inc writes it)
  25. std::vector<std::uint8_t> flSoundfontState6 (const std::array<int, 13>& p, const std::string& path)
  26. {
  27. std::vector<std::uint8_t> s;
  28. const auto i32 = [&s] (int v) {
  29. for (int b = 0; b < 4; ++b)
  30. s.push_back ((std::uint8_t) ((std::uint32_t) v >> (8 * b)));
  31. };
  32. i32 (6);
  33. for (auto v : p)
  34. i32 (v);
  35. s.push_back ((std::uint8_t) path.size());
  36. s.insert (s.end(), path.begin(), path.end());
  37. i32 (-1), s.push_back (0); // reverb: send to none, not built in
  38. i32 (-1), s.push_back (0); // chorus
  39. s.push_back (1);
  40. s.push_back (1), s.push_back (1); // fix levels, velocity filter
  41. i32 (4), i32 (1), i32 (0); // standard resampling, (1), preset index 0
  42. s.push_back (1); // EMU envelopes
  43. s.push_back (1), s.push_back (1);
  44. i32 (0);
  45. return s;
  46. }
  47. } // namespace i173a
  48. // #70: "the notes keep on cutting off when two or more notes play". Every note of a dense, overlapping part reaches the
  49. // instrument as its own note on at its start and its own note off at its end: no note is ended early by another one,
  50. // and nothing releases every note (allNotesOff) while the pattern plays on, over the loop too.
  51. TEST (issues173a_overlapping_notes_each_end_at_their_own_end)
  52. {
  53. i32d::Rig rig;
  54. rig.song.patterns[0].notes = i173a::videoPart();
  55. rig.song.patterns[0].lengthTicks = 8 * lynx::ppq;
  56. rig.rebuild();
  57. rig.play (16.0); // two passes
  58. const auto& notes = rig.song.patterns[0].notes;
  59. int ons = 0, offs = 0;
  60. for (const auto& e : rig.spy->log)
  61. if (e.sample < rig.sampleOfBeat (16.0)) // (the last block runs into a third pass)
  62. (e.on ? ons : offs) += 1;
  63. CHECK (ons == 2 * (int) notes.size());
  64. int endsBefore16 = 0; // (the notes that end with the second pass end at beat 16 itself)
  65. for (int pass = 0; pass < 2; ++pass)
  66. for (const auto& n : notes)
  67. endsBefore16 += rig.sampleOfBeat (pass * 8.0 + (double) (n.start + n.length) / lynx::ppq) < rig.sampleOfBeat (16.0) ? 1 : 0;
  68. CHECK (offs >= endsBefore16);
  69. // each note: on at its start, and the key's next off at its end (never before)
  70. for (int pass = 0; pass < 2; ++pass)
  71. for (const auto& n : notes)
  72. {
  73. const auto start = rig.sampleOfBeat (pass * 8.0 + (double) n.start / lynx::ppq);
  74. const auto end = rig.sampleOfBeat (pass * 8.0 + (double) (n.start + n.length) / lynx::ppq);
  75. if (end >= rig.sampleOfBeat (15.9))
  76. continue;
  77. long long onAt = -1, offAt = -1;
  78. for (const auto& e : rig.spy->log)
  79. {
  80. if (e.key != n.key)
  81. continue;
  82. if (e.on && onAt < 0 && std::llabs (e.sample - start) <= 2)
  83. onAt = e.sample;
  84. else if (! e.on && onAt >= 0 && offAt < 0 && e.sample >= onAt)
  85. offAt = e.sample;
  86. }
  87. CHECK (onAt >= 0);
  88. if (std::llabs (offAt - end) > 2)
  89. std::printf (" #70: key %d at beat %.2f ends at sample %lld, not %lld\n", n.key, pass * 8.0 + (double) n.start / lynx::ppq, offAt, end);
  90. CHECK (std::llabs (offAt - end) <= 2);
  91. }
  92. CHECK (rig.spy->allOffs == 0);
  93. }
  94. // #73 evidence (not a check): LYNX_173A_WRITE_FLP=<folder> writes FL projects with FL's SoundFont Player (FL's own
  95. // Default.sf2) on one long C5: no detune, the channel PITCH knob at −400 cents (range 12) and Misc › Fine tune at −40.
  96. // FL renders them (FL64.exe /R /Ewav), LYNX imports the same files: REA_rodada173a.md #73.
  97. TEST (issues173a_write_fl_detune_probes)
  98. {
  99. const auto* dir = std::getenv ("LYNX_173A_WRITE_FLP");
  100. if (dir == nullptr)
  101. return;
  102. const std::string font = "C:\\Program Files\\Image-Line\\FL Studio 2026\\Plugins\\Fruity\\Generators\\Soundfont Player\\Data\\Default.sf2";
  103. for (const auto& [name, pitch, fine] : { std::tuple { "sf_pitch0", 0, 0 }, std::tuple { "sf_pitch-400", -400, 0 },
  104. std::tuple { "sf_fine-40", 0, -40 }, std::tuple { "sf_pitch-400_fine-40", -400, -40 } })
  105. {
  106. flp::Project p;
  107. p.tempo = 120.0;
  108. flp::Channel c;
  109. c.index = 0;
  110. c.type = flp::ChannelType::instrument;
  111. c.name = "SF";
  112. c.pluginName = "Fruity Soundfont Player";
  113. c.pluginState = i173a::flSoundfontState6 ({ 1, 0, 0, 0, 0, -1, -1, -1, -1, -1, -128, -1, -1 }, font);
  114. c.hasLevels = true;
  115. c.volume = 10000;
  116. c.pan = 6400;
  117. c.pitchCents = pitch;
  118. c.pitchRange = 12;
  119. c.fineTune = fine;
  120. p.channels.push_back (c);
  121. flp::Pattern pat;
  122. pat.index = 1;
  123. flp::Note n;
  124. n.position = 0;
  125. n.length = 96 * 6;
  126. n.key = 60;
  127. n.velocity = 100;
  128. pat.notes.push_back (n);
  129. p.patterns.push_back (pat);
  130. flp::Arrangement a;
  131. a.name = "Arrangement";
  132. flp::PlaylistItem item;
  133. item.position = 0;
  134. item.length = 96 * 8;
  135. item.index = 1;
  136. item.track = 0;
  137. a.items.push_back (item);
  138. p.arrangements.push_back (a);
  139. const auto bytes = flp::writeProject (p);
  140. std::ofstream out (std::string (dir) + "/" + name + ".flp", std::ios::binary);
  141. out.write ((const char*) bytes.data(), (std::streamsize) bytes.size());
  142. std::printf (" wrote %s/%s.flp\n", dir, name);
  143. }
  144. }
  145. // #73: "SoundFont Player detune not being imported correctly". The FL project's channel PITCH knob (−400 cents, its
  146. // range 12) reached LYNX's channel, but the LYNX Soundfont that plays FL's SoundFont Player ignored the channel's PITCH
  147. // knob (only the Misc fine tune and the main pitch reached it, through each note). It plays at FL's pitch now, and
  148. // follows a live knob move on a sounding note as FL's generators do.
  149. TEST (issues173a_soundfont_player_follows_the_channel_pitch_knob)
  150. {
  151. using namespace lynx;
  152. const auto folder = std::filesystem::temp_directory_path() / ("lynx-173a-sf-" + processTempTag()); // (per process: shards run at once)
  153. std::filesystem::create_directories (folder);
  154. const auto path = (folder / "font.sf2").string();
  155. {
  156. const auto bytes = sf::makeTestFont ({ { "Tone", 0, 0 } }, 1);
  157. std::ofstream out (path, std::ios::binary);
  158. out.write ((const char*) bytes.data(), (std::streamsize) bytes.size());
  159. }
  160. const auto keep = sf::FontLibrary::instance().getNow (path); // (loaded: the rack's channel takes it at once)
  161. const auto imported = [&] (int pitchCents, int fineTune) {
  162. flp::Project project;
  163. flp::Channel c;
  164. c.index = 0;
  165. c.type = flp::ChannelType::instrument;
  166. c.name = "SF";
  167. c.pluginName = "Fruity Soundfont Player"; // (FL 12's name)
  168. c.pluginState = i173a::flSoundfontState6 ({ 1, 0, 0, 0, 0, -1, -1, -1, -1, -1, -128, -1, -1 }, path);
  169. c.hasLevels = true;
  170. c.pitchCents = pitchCents;
  171. c.pitchRange = 12;
  172. c.fineTune = fineTune;
  173. project.channels = { c };
  174. const auto song = importFlp (project, {});
  175. return song.channels.empty() ? ChannelDef {} : song.channels[0];
  176. };
  177. Note held;
  178. held.key = 60;
  179. held.length = ppq * 8;
  180. held.velocity = 0.8f;
  181. const auto plain = imported (0, 0);
  182. I32D_REQUIRE (plain.instrument == InstrumentKind::soundfont);
  183. const auto base = issue14::pitchAt (issue14::renderPattern (plain, { held }, 0.0f, 1.0), 0.5);
  184. const auto detuned = imported (-400, 0);
  185. CHECK (std::abs (detuned.sampler.pitch + 4.0f) < 1.0e-4f);
  186. const auto down = issue14::cents (base, issue14::pitchAt (issue14::renderPattern (detuned, { held }, 0.0f, 1.0), 0.5));
  187. const auto fine = issue14::cents (base, issue14::pitchAt (issue14::renderPattern (imported (0, -40), { held }, 0.0f, 1.0), 0.5));
  188. const auto both = issue14::cents (base, issue14::pitchAt (issue14::renderPattern (imported (-400, -40), { held }, 0.0f, 1.0), 0.5));
  189. std::printf (" #73: base %.1f Hz, knob -400 -> %+.0f cents, fine -40 -> %+.0f, both -> %+.0f\n", base, down, fine, both);
  190. CHECK (base > 50.0);
  191. CHECK (std::abs (down + 400.0) < 15.0);
  192. CHECK (std::abs (fine + 40.0) < 15.0);
  193. CHECK (std::abs (both + 440.0) < 15.0);
  194. // a live PITCH knob move retunes the note that sounds (FL's generators, REA_rodada167a §1)
  195. const auto live = left167::renderLiveTweak (plain, 2.0f, 0.0f, 0.75, 2.0);
  196. CHECK (std::abs (issue14::cents (issue14::pitchAt (live, 0.3), issue14::pitchAt (live, 1.25)) - 200.0) < 20.0);
  197. // and a live main pitch move too, once (not counted twice: the note's own cents already held the old one)
  198. const auto main = left167::renderLiveTweak (plain, 0.0f, 100.0f, 0.75, 2.0);
  199. CHECK (std::abs (issue14::cents (issue14::pitchAt (main, 0.3), issue14::pitchAt (main, 1.25)) - 100.0) < 20.0);
  200. }