Sampler162Tests.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // --- Round 162 (issue #9): channel routing past insert 16, sampler fine tune / stretch / pitch wheel ---------------
  2. namespace s162
  3. {
  4. // a mono sine sample
  5. std::shared_ptr<lynx::SampleData> sine (double freq, double seconds, double rate = 44100.0)
  6. {
  7. auto s = std::make_shared<lynx::SampleData>();
  8. s->sampleRate = rate;
  9. const auto n = (std::size_t) (seconds * rate);
  10. for (std::size_t i = 0; i < n; ++i)
  11. s->left.push_back ((float) (0.5 * std::sin (2.0 * 3.141592653589793 * freq * (double) i / rate)));
  12. return s;
  13. }
  14. // frequency of the steady part of `x` (positive-going zero crossings between 30 % and 70 % of the sounding part) and
  15. // the number of sounding samples (until the signal stays below -60 dB, or at the value an audio clip holds after its
  16. // file's end like FL, round 168j)
  17. struct Heard
  18. {
  19. double freq = 0.0;
  20. long length = 0;
  21. };
  22. Heard analyse (const std::vector<float>& x, double rate)
  23. {
  24. Heard h;
  25. long last = -1;
  26. const float tail = x.empty() ? 0.0f : x.back();
  27. for (long i = 0; i < (long) x.size(); ++i)
  28. if (std::abs (x[(std::size_t) i]) > 0.001f && std::abs (x[(std::size_t) i] - tail) > 0.001f)
  29. last = i;
  30. h.length = last + 1;
  31. const auto from = (long) (h.length * 0.3), to = (long) (h.length * 0.7);
  32. long first = -1, end = -1;
  33. int crossings = 0;
  34. for (long i = from + 1; i < to; ++i)
  35. if (x[(std::size_t) i - 1] <= 0.0f && x[(std::size_t) i] > 0.0f)
  36. {
  37. if (first < 0)
  38. first = i;
  39. else
  40. ++crossings, end = i;
  41. }
  42. if (crossings > 0)
  43. h.freq = crossings * rate / (double) (end - first);
  44. return h;
  45. }
  46. // a sampler channel playing `sample` with `settings`: one live note (key, cents of fine tune), the transport
  47. // stopped, rendered until it ends (at most `seconds`)
  48. Heard playLive (const lynx::SamplerSettings& settings, std::shared_ptr<lynx::SampleData> sample, int key, float fineCents = 0.0f,
  49. double seconds = 6.0, float bend = 0.0f, int range = 2)
  50. {
  51. lynx::Song s;
  52. s.mixer = lynx::Song::defaultMixer (2);
  53. s.bpm = 120.0;
  54. lynx::ChannelDef c;
  55. c.instrument = lynx::InstrumentKind::sampler;
  56. c.samplePath = "sine.wav";
  57. c.sampler = settings;
  58. c.misc.fineTune = fineCents;
  59. c.misc.pitchRange = range;
  60. c.mixerTrack = 1;
  61. s.channels.push_back (c);
  62. s.patterns.emplace_back();
  63. lynx::RackState::Factories f;
  64. f.samples = [sample] (const std::string&) { return std::shared_ptr<const lynx::SampleData> (sample); };
  65. f.stretchedSamples = [sample] (const std::string&, double ratio, lynx::StretchMethod m, double, const lynx::StretchMap&, const lynx::StretchOptions&) {
  66. return std::shared_ptr<const lynx::SampleData> (lynx::timeStretch (*sample, ratio, m));
  67. };
  68. constexpr double rate = 44100.0;
  69. auto state = lynx::RackState::build (s, nullptr, rate, f);
  70. lynx::RackRenderer r;
  71. r.prepare (rate, 441);
  72. lynx::LiveEventQueue live;
  73. if (bend != 0.0f)
  74. {
  75. lynx::NoteEvent b;
  76. b.type = lynx::NoteEvent::Type::control;
  77. b.channel = 0;
  78. b.key = lynx::NoteEvent::controlPitchBend;
  79. b.velocity = bend;
  80. live.push (b);
  81. }
  82. lynx::NoteEvent e;
  83. e.channel = 0;
  84. e.key = key;
  85. e.velocity = 0.8f;
  86. live.push (e);
  87. std::vector<float> out, l (441), rr (441);
  88. for (int b = 0; b < (int) (seconds * 100.0); ++b)
  89. {
  90. r.process (state.get(), false, 0.0, 441, l.data(), rr.data(), &live, nullptr);
  91. out.insert (out.end(), l.begin(), l.end());
  92. }
  93. return analyse (out, rate);
  94. }
  95. } // namespace s162
  96. TEST (s162_a_channel_routes_to_any_insert)
  97. {
  98. using namespace lynx;
  99. auto s = Song::makeDefault();
  100. CHECK (s.mixer.size() == 17); // Master + 16, as before
  101. s.channels.resize (std::max<std::size_t> (s.channels.size(), 1));
  102. // issue #9: past insert 16 the route box stopped; now the inserts up to the target are added
  103. mixerops::routeChannel (s, 0, 64);
  104. CHECK (s.channels[0].mixerTrack == 64);
  105. CHECK (s.mixer.size() == 65);
  106. CHECK (s.mixer[40].name == "Insert 40" && s.mixer[64].name == "Insert 64");
  107. mixerops::routeChannel (s, 0, 500); // at most FL's last insert
  108. CHECK (s.channels[0].mixerTrack == maxMixerTracks - 1);
  109. CHECK ((int) s.mixer.size() == maxMixerTracks);
  110. mixerops::routeChannel (s, 0, 3); // routing lower never removes inserts
  111. CHECK (s.channels[0].mixerTrack == 3 && (int) s.mixer.size() == maxMixerTracks);
  112. // a new channel takes the next free insert, past the 16th when they are all in use (it used to fall to Master)
  113. auto t = Song::makeDefault();
  114. t.channels.clear();
  115. for (int i = 0; i < 20; ++i)
  116. {
  117. ChannelDef c;
  118. c.mixerTrack = t.claimFreeInsert();
  119. t.channels.push_back (c);
  120. }
  121. CHECK (t.channels[15].mixerTrack == 16);
  122. CHECK (t.channels[16].mixerTrack == 17 && t.channels[19].mixerTrack == 20);
  123. CHECK (t.mixer.size() == 21);
  124. CHECK (t.nextFreeInsert() == 0); // (the const query still says "none free")
  125. }
  126. TEST (s162_the_rack_mixes_a_channel_routed_past_insert_16_into_that_insert)
  127. {
  128. using namespace lynx;
  129. Song s;
  130. s.mixer = Song::defaultMixer (16);
  131. ChannelDef c;
  132. c.instrument = InstrumentKind::synth;
  133. s.channels.push_back (c);
  134. mixerops::routeChannel (s, 0, 40);
  135. CHECK (s.mixer.size() == 41);
  136. // the insert gets the channel (not insert 16, where the renderer used to clamp it)
  137. CHECK (mixerops::channelsOn (s, 40).size() == 1);
  138. CHECK (mixerops::channelsOn (s, 16).empty());
  139. }
  140. TEST (s162_sampler_fine_tune_changes_live_notes)
  141. {
  142. using namespace lynx;
  143. const auto sample = s162::sine (440.0, 1.0);
  144. SamplerSettings st;
  145. const auto plain = s162::playLive (st, sample, 60);
  146. CHECK (std::abs (plain.freq - 440.0) < 1.0);
  147. // Misc › Fine tune +50 cents on a key played live: 440 · 2^(50/1200) = 452.9 Hz (it was ignored)
  148. const auto fine = s162::playLive (st, sample, 60, 50.0f);
  149. CHECK (std::abs (fine.freq - 452.9) < 1.5);
  150. // the sampler's pitch knob, in cents (-0.25 st)
  151. st.pitch = -0.25f;
  152. CHECK (std::abs (s162::playLive (st, sample, 60).freq - 440.0 * std::exp2 (-0.25 / 12.0)) < 1.0);
  153. }
  154. TEST (s162_resample_stretch_follows_the_keys)
  155. {
  156. using namespace lynx;
  157. // a 1 s sample stretched (Resample) to 4 beats = 2 s at 120 BPM: an octave down at the root key
  158. const auto sample = s162::sine (440.0, 1.0);
  159. SamplerSettings st;
  160. st.stretchBeats = 4.0f;
  161. const auto root = s162::playLive (st, sample, 60);
  162. CHECK (std::abs (root.freq - 220.0) < 1.0);
  163. CHECK (std::abs (root.length - 88200) < 400);
  164. // FL's Resample: pitch and length change together, like tape (LYNX played every key at the root's pitch)
  165. const auto fifth = s162::playLive (st, sample, 67);
  166. CHECK (std::abs (fifth.freq - 220.0 * std::exp2 (7.0 / 12.0)) < 1.5);
  167. CHECK (std::abs (fifth.length - 88200.0 / std::exp2 (7.0 / 12.0)) < 400);
  168. // fine tune and pitch knob too
  169. CHECK (std::abs (s162::playLive (st, sample, 60, 100.0f).freq - 220.0 * std::exp2 (1.0 / 12.0)) < 1.0);
  170. }
  171. TEST (s162_stretch_keeping_pitch_transposes_and_keeps_the_length)
  172. {
  173. using namespace lynx;
  174. const auto sample = s162::sine (440.0, 1.0);
  175. SamplerSettings st;
  176. st.stretchBeats = 4.0f; // 2 s at 120 BPM
  177. st.keepPitch = true;
  178. const auto root = s162::playLive (st, sample, 60);
  179. CHECK (std::abs (root.freq - 440.0) < 1.5);
  180. CHECK (std::abs (root.length - 88200) < 2000);
  181. // FL's Stretch modes: "pitch changes keep the original duration" — the key transposes, the length stays
  182. for (const int key : { 67, 55, 72 })
  183. {
  184. const auto heard = s162::playLive (st, sample, key);
  185. const auto expected = 440.0 * std::exp2 ((key - 60) / 12.0);
  186. CHECK (std::abs (heard.freq - expected) < expected * 0.004);
  187. CHECK (std::abs (heard.length - root.length) < 4000);
  188. }
  189. // the fine tune moves it too
  190. const auto fine = s162::playLive (st, sample, 60, 100.0f);
  191. CHECK (std::abs (fine.freq - 440.0 * std::exp2 (1.0 / 12.0)) < 2.5);
  192. }
  193. TEST (s162_pitch_wheel_bends_native_instruments_by_the_pitch_range)
  194. {
  195. using namespace lynx;
  196. const auto sample = s162::sine (440.0, 1.0);
  197. SamplerSettings st;
  198. // full wheel up with FL's default range (2 semitones): 440 · 2^(2/12)
  199. CHECK (std::abs (s162::playLive (st, sample, 60, 0.0f, 3.0, 1.0f).freq - 440.0 * std::exp2 (2.0 / 12.0)) < 1.5);
  200. // half down with a 12 semitone range: -6 st
  201. CHECK (std::abs (s162::playLive (st, sample, 60, 0.0f, 3.0, -0.5f, 12).freq - 440.0 * std::exp2 (-6.0 / 12.0)) < 1.0);
  202. // stretch keeping pitch: the wheel bends the shifter (the length stays)
  203. st.stretchBeats = 4.0f;
  204. st.keepPitch = true;
  205. const auto bent = s162::playLive (st, sample, 64, 0.0f, 4.0, 1.0f);
  206. CHECK (std::abs (bent.freq - 440.0 * std::exp2 (6.0 / 12.0)) < 3.0);
  207. CHECK (std::abs (bent.length - 88200) < 4000);
  208. // the built-in synth: a sine at C5 bent up 2 semitones
  209. auto synth = makeInstrument (InstrumentKind::synth);
  210. synth->prepare (44100.0);
  211. ChannelDef c;
  212. c.synth.osc[0] = { Waveform::sine, 0, 0.0f, 1.0f };
  213. c.synth.osc[1].level = c.synth.osc[2].level = 0.0f;
  214. synth->configure (c, 120.0);
  215. synth->pitchBend (2.0f);
  216. synth->noteOn (69, 0.8f);
  217. std::vector<float> l (44100, 0.0f), r (44100, 0.0f);
  218. synth->render (l.data(), r.data(), 44100, 1.0f, 1.0f);
  219. CHECK (std::abs (s162::analyse (l, 44100.0).freq - 440.0 * std::exp2 (2.0 / 12.0)) < 2.0);
  220. }
  221. namespace s162
  222. {
  223. // a fake hosted plugin: records the MIDI messages it gets
  224. struct MidiSpy final : lynx::Instrument
  225. {
  226. std::vector<std::vector<std::uint8_t>> got;
  227. void prepare (double) noexcept override {}
  228. void noteOn (int, float) noexcept override {}
  229. void noteOff (int) noexcept override {}
  230. void allNotesOff() noexcept override {}
  231. void render (float*, float*, int, float, float) noexcept override {}
  232. void midiMessage (const std::uint8_t* bytes, int size) noexcept override { got.emplace_back (bytes, bytes + size); }
  233. };
  234. } // namespace s162
  235. TEST (s162_pitch_and_mod_wheel_reach_a_hosted_plugin)
  236. {
  237. using namespace lynx;
  238. Song s;
  239. s.mixer = Song::defaultMixer (2);
  240. ChannelDef c;
  241. c.instrument = InstrumentKind::plugin;
  242. c.plugin.id = "spy";
  243. s.channels.push_back (c);
  244. s.patterns.emplace_back();
  245. auto spy = std::make_shared<s162::MidiSpy>();
  246. RackState::Factories f;
  247. f.pluginInstruments = [spy] (const PluginRef&, double) { return std::shared_ptr<Instrument> (spy); };
  248. auto state = RackState::build (s, nullptr, 44100.0, f);
  249. RackRenderer r;
  250. r.prepare (44100.0, 256);
  251. LiveEventQueue live;
  252. NoteEvent e;
  253. e.type = NoteEvent::Type::control;
  254. e.channel = 0;
  255. e.key = NoteEvent::controlPitchBend;
  256. e.velocity = 1.0f;
  257. live.push (e);
  258. e.velocity = 0.0f;
  259. live.push (e);
  260. e.key = 1; // mod wheel
  261. e.velocity = 64.0f / 127.0f;
  262. live.push (e);
  263. std::vector<float> l (256), rr (256);
  264. r.process (state.get(), false, 0.0, 256, l.data(), rr.data(), &live, nullptr);
  265. CHECK (spy->got.size() == 3);
  266. if (spy->got.size() == 3)
  267. {
  268. CHECK ((spy->got[0] == std::vector<std::uint8_t> { 0xE0, 127, 127 }));
  269. CHECK ((spy->got[1] == std::vector<std::uint8_t> { 0xE0, 0, 64 }));
  270. CHECK ((spy->got[2] == std::vector<std::uint8_t> { 0xB0, 1, 64 }));
  271. }
  272. }