Notes188gTests.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Round 188g: the rest of GitHub issue #63 (docs/analise/REA_rodada188g.md): a note's Mod X / Y on LYNX's own Sampler,
  2. // its fine pitch on hosted plugins (FL's wrapper: the key it rounds to, the VST3 tuning), release velocity on the
  3. // Sampler (FL's leaves it alone) and the Fruity Slicer / Slicex slices of a sample's second half one frame early.
  4. #include "core/HostNotePitch.h"
  5. namespace notes188g
  6. {
  7. std::shared_ptr<lynx::SampleData> noise (int frames, std::uint32_t seed = 11)
  8. {
  9. auto s = std::make_shared<lynx::SampleData>();
  10. s->sampleRate = 44100.0;
  11. for (int i = 0; i < frames; ++i)
  12. {
  13. seed = (seed * 1103515245u + 12345u) & 0x7fffffffu;
  14. s->left.push_back ((float) (((double) seed / 2147483647.0 * 2.0 - 1.0) * 0.3));
  15. }
  16. return s;
  17. }
  18. // FL's 12 dB filter law (X → Hz at 44.1 kHz, as FlpImport's channelFilterHz for types 1…5) and its inverse
  19. double hzOfX (double x) { return x >= 1.0 ? 20000.0 : std::clamp (((std::pow (51.0, x) - 1.0) * 0.0596 + 0.02) * 44100.0 / 6.283185307179586, 20.0, 20000.0); }
  20. double xOfHz (double hz) { return std::log ((hz * 6.283185307179586 / 44100.0 - 0.02) / 0.0596 + 1.0) / std::log (51.0); }
  21. // the energy of the first differences (a crude "brightness")
  22. double brightness (const std::vector<float>& v)
  23. {
  24. double e = 0.0;
  25. for (std::size_t i = 1; i < v.size(); ++i)
  26. e += (double) (v[i] - v[i - 1]) * (v[i] - v[i - 1]);
  27. return e;
  28. }
  29. } // namespace notes188g
  30. // FL's wrapper (Fruity Wrapper 0x879a00 → the format's note on): pitch = key + fine / 100; VST2 / MIDI and VST3 round
  31. // it to a key (ties to even), VST3 sends the rest as tuning, CLAP truncates and sends nothing else.
  32. TEST (notes188g_hosted_note_pitch_follows_fl_wrapper)
  33. {
  34. using namespace lynx::hostpitch;
  35. struct Case
  36. {
  37. int key;
  38. float cents;
  39. int vst3Key;
  40. float tuning;
  41. int clapKey;
  42. };
  43. const Case cases[] = {
  44. { 60, 0.0f, 60, 0.0f, 60 }, { 60, 25.0f, 60, 25.0f, 60 }, { 60, -40.0f, 60, -40.0f, 59 },
  45. { 60, 70.0f, 61, -30.0f, 60 }, { 64, 50.0f, 64, 50.0f, 64 }, { 65, 50.0f, 66, -50.0f, 65 }, // (ties to even)
  46. { 64, 150.0f, 66, -50.0f, 65 }, { 67, -1200.0f, 55, 0.0f, 55 }, { 72, 1350.0f, 86, -50.0f, 85 },
  47. { 127, 90.0f, 127, 0.0f, 127 }, { 0, -80.0f, 0, 0.0f, 0 },
  48. };
  49. for (const auto& c : cases)
  50. {
  51. const auto v = of (c.key, c.cents, Format::vst3);
  52. const auto m = of (c.key, c.cents, Format::midi);
  53. const auto k = of (c.key, c.cents, Format::clap);
  54. const auto clampedEnds = c.key == 127 || c.key == 0; // (the key is clamped, the tuning keeps the rest)
  55. CHECK (v.key == c.vst3Key && m.key == c.vst3Key && k.key == c.clapKey);
  56. if (! clampedEnds)
  57. CHECK (std::abs (v.tuning - c.tuning) < 1.0e-3f);
  58. CHECK (m.tuning == 0.0f && k.tuning == 0.0f);
  59. if (! clampedEnds)
  60. CHECK (std::abs ((float) (v.key - c.key) * 100.0f + v.tuning - c.cents) < 1.0e-3f); // nothing lost
  61. }
  62. CHECK (of (60, std::numeric_limits<float>::quiet_NaN()).key == 60);
  63. }
  64. // The note off goes to the key its note on went out on (FL keeps it in the voice), oldest first; the VST3 tunings wait
  65. // for their note ons in the order they were played.
  66. TEST (notes188g_sent_keys_end_notes_on_their_own_key)
  67. {
  68. using namespace lynx::hostpitch;
  69. SentKeys keys;
  70. keys.on (60, of (60, 70.0f)); // → 61, −30
  71. keys.on (60, of (60, -20.0f)); // → 60, −20
  72. keys.on (61, of (61, 0.0f)); // → 61, 0
  73. CHECK (keys.takeTuning (61) == of (60, 70.0f).tuning);
  74. CHECK (keys.takeTuning (60) == of (60, -20.0f).tuning);
  75. CHECK (keys.takeTuning (61) == 0.0f);
  76. CHECK (keys.takeTuning (61) == 0.0f); // (none waits: 0)
  77. CHECK (keys.off (60) == 61);
  78. CHECK (keys.off (60) == 60);
  79. CHECK (keys.off (60) == 60); // (no note sounding: its own key)
  80. CHECK (keys.off (61) == 61);
  81. for (int i = 0; i < 6; ++i) // more notes than a key keeps: the oldest are forgotten, nothing overflows
  82. keys.on (62, of (62, 60.0f));
  83. CHECK (keys.off (62) == 63);
  84. }
  85. // #63: a note's Mod X / Y on LYNX's own Sampler move its filter as FL's voice moves the channel filter (REA_rodada76 §1:
  86. // X + (b − 128) / 128): a note at Mod X 0.25 on a channel at cutoff F sounds like a centred note on the channel at the
  87. // cutoff half of FL's X range lower. A centred note is unchanged, bit for bit; Mod Y moves the resonance; an open
  88. // channel closes for a note with Mod X below the centre.
  89. TEST (notes188g_note_mod_x_moves_lynx_sampler_filter)
  90. {
  91. using namespace lynx;
  92. const auto source = notes188g::noise (22050);
  93. auto render = [&] (float cutoffHz, float resonance, std::optional<std::pair<float, float>> mod) {
  94. ChannelDef c;
  95. c.instrument = InstrumentKind::sampler;
  96. c.sampler.cutoff = cutoffHz;
  97. c.sampler.resonance = resonance;
  98. auto inst = makeInstrument (InstrumentKind::sampler, source);
  99. inst->prepare (44100.0);
  100. inst->configure (c, 120.0);
  101. if (mod)
  102. inst->setNextNoteModulation (mod->first, mod->second);
  103. inst->noteOn (60, 0.8f);
  104. std::vector<float> l (8000), r (8000);
  105. inst->render (l.data(), r.data(), 8000, 1.0f, 1.0f);
  106. return l;
  107. };
  108. const auto centred = render (3000.0f, 0.2f, std::nullopt);
  109. CHECK (render (3000.0f, 0.2f, std::pair { 0.5f, 0.5f }) == centred); // centre: nothing changes
  110. const auto darker = render (3000.0f, 0.2f, std::pair { 0.25f, 0.5f });
  111. const auto brighter = render (3000.0f, 0.2f, std::pair { 0.75f, 0.5f });
  112. std::printf (" brightness: Mod X 0.25 %.4g, centred %.4g, Mod X 0.75 %.4g\n", notes188g::brightness (darker), notes188g::brightness (centred),
  113. notes188g::brightness (brighter));
  114. CHECK (notes188g::brightness (darker) < 0.5 * notes188g::brightness (centred));
  115. CHECK (notes188g::brightness (brighter) > 1.5 * notes188g::brightness (centred));
  116. // the same as the channel's cutoff moved by half of FL's X range (Mod X 0.25 = −0.5)
  117. const auto moved = (float) notes188g::hzOfX (notes188g::xOfHz (3000.0) - 0.5);
  118. const auto same = render (moved, 0.2f, std::nullopt);
  119. auto diff = 0.0;
  120. for (std::size_t i = 0; i < same.size(); ++i)
  121. diff = std::max (diff, (double) std::abs (same[i] - darker[i]));
  122. std::printf (" cutoff 3000 Hz + Mod X 0.25 vs cutoff %.1f Hz: max diff %.3g\n", moved, diff);
  123. CHECK (diff < 1.0e-4);
  124. CHECK (render (3000.0f, 0.2f, std::pair { 0.5f, 0.9f }) != centred); // Mod Y: the resonance
  125. // an open channel: untouched by a centred note, filtered by a note below the centre
  126. const auto open = render (20000.0f, 0.0f, std::nullopt);
  127. CHECK (open == render (20000.0f, 0.0f, std::pair { 0.5f, 0.5f }));
  128. CHECK (notes188g::brightness (render (20000.0f, 0.0f, std::pair { 0.2f, 0.5f })) < 0.5 * notes188g::brightness (open));
  129. }
  130. // A VFX (VFX Envelope / Color Mapper) moving a sounding note's Mod X reaches the same filter.
  131. TEST (notes188g_vfx_mod_x_moves_a_sounding_sampler_note)
  132. {
  133. using namespace lynx;
  134. const auto source = notes188g::noise (22050, 5);
  135. auto render = [&] (bool move) {
  136. ChannelDef c;
  137. c.instrument = InstrumentKind::sampler;
  138. c.sampler.cutoff = 5000.0f;
  139. auto inst = makeInstrument (InstrumentKind::sampler, source);
  140. inst->prepare (44100.0);
  141. inst->configure (c, 120.0);
  142. inst->noteOn (60, 0.8f);
  143. std::vector<float> l (4000), r (4000);
  144. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  145. if (move)
  146. inst->voiceProperty (60, (int) vfx::Prop::modX, 0.1f);
  147. inst->render (l.data() + 2000, r.data() + 2000, 2000, 1.0f, 1.0f);
  148. return std::vector<float> (l.begin() + 2000, l.end());
  149. };
  150. const auto still = render (false), moved = render (true);
  151. CHECK (notes188g::brightness (moved) < 0.5 * notes188g::brightness (still));
  152. }
  153. // FL's Sampler ignores a note's release velocity: its note off (FLEngine FUN_00d241e0) releases the voice's articulators
  154. // with the channel's envelope records only (REA_rodada188g §3); LYNX's own Sampler does the same.
  155. TEST (notes188g_release_velocity_leaves_the_sampler_alone)
  156. {
  157. using namespace lynx;
  158. const auto source = notes188g::noise (44100, 3);
  159. auto render = [&] (std::optional<float> release) {
  160. ChannelDef c;
  161. c.instrument = InstrumentKind::sampler;
  162. c.sampler.release = 0.2f;
  163. auto inst = makeInstrument (InstrumentKind::sampler, source);
  164. inst->prepare (44100.0);
  165. inst->configure (c, 120.0);
  166. inst->noteOn (60, 0.8f);
  167. std::vector<float> l (12000), r (12000);
  168. inst->render (l.data(), r.data(), 2000, 1.0f, 1.0f);
  169. if (release)
  170. inst->setNextNoteRelease (*release);
  171. inst->noteOff (60);
  172. inst->render (l.data() + 2000, r.data() + 2000, 10000, 1.0f, 1.0f);
  173. return l;
  174. };
  175. const auto plain = render (std::nullopt);
  176. CHECK (render (0.0f) == plain);
  177. CHECK (render (1.0f) == plain);
  178. }
  179. // Round 180c's "notes 1 frame late from 2.25 s" (/mnt/e/lynx-work/issues180c/note-timing.md): the Fruity Slicer project
  180. // played slice k at 2.25 s + … one frame late against FL because the sampler scaled the slice starts (p / frames) by the
  181. // last frame's index (frames − 1): every slice in the sample's second half started one frame early in the sample. FL's
  182. // render starts each slice on its own frame.
  183. TEST (notes188g_slices_in_the_second_half_start_on_their_frame)
  184. {
  185. using namespace lynx;
  186. constexpr int frames = 176400; // round 180c's loop.wav: 4 s at 44.1 kHz, 16 slices of 11025 frames
  187. auto ramp = std::make_shared<SampleData>();
  188. ramp->sampleRate = 44100.0;
  189. for (int i = 0; i < frames; ++i)
  190. ramp->left.push_back ((float) i);
  191. ChannelDef c;
  192. for (int k = 0; k < 16; ++k)
  193. c.sampler.slices.push_back ((float) ((double) (k * 11025) / (double) frames));
  194. auto wrong = 0;
  195. for (int k = 0; k < 16; ++k)
  196. {
  197. auto inst = makeInstrument (InstrumentKind::sampler, ramp);
  198. inst->prepare (44100.0);
  199. inst->configure (c, 120.0);
  200. inst->noteOn (60 + k, 1.0f);
  201. std::vector<float> l (16), r (16);
  202. inst->render (l.data(), r.data(), 16, 1.0f, 1.0f);
  203. if (std::abs ((double) l[0] - (double) (k * 11025)) > 0.25) // (one frame early reads p − 1)
  204. {
  205. ++wrong;
  206. std::printf (" slice %d starts at frame %.2f (exact %d)\n", k, (double) l[0], k * 11025);
  207. }
  208. }
  209. CHECK (wrong == 0);
  210. }