Workflow163cTests.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // --- Round 163c (user feedback: "pitching didn't work for me"): audio clip pitch, keep length, the Pitch... command ----
  2. namespace w163c
  3. {
  4. // An audio clip of `sample` (a 2-bar clip at bar 0, 120 BPM) on a channel with `pitch` semitones, `fine` cents and
  5. // "keep length"; rendered in song mode for `seconds` (the clip itself lasts 4 s). Returns the left channel.
  6. std::vector<float> renderClip (std::shared_ptr<lynx::SampleData> sample, float pitch, float fine, bool keepLength, double seconds = 3.0,
  7. std::int64_t clipLength = 0)
  8. {
  9. using namespace lynx;
  10. Song s;
  11. s.mixer = Song::defaultMixer (2);
  12. s.bpm = 120.0;
  13. s.mode = PlayMode::song;
  14. ChannelDef c;
  15. c.instrument = InstrumentKind::audioClip;
  16. c.samplePath = "sine.wav";
  17. c.sampler.pitch = pitch;
  18. c.sampler.keepPitch = keepLength;
  19. c.misc.fineTune = fine;
  20. c.mixerTrack = 1;
  21. s.channels.push_back (c);
  22. s.patterns.emplace_back();
  23. Clip clip;
  24. clip.kind = ClipKind::audio;
  25. clip.channel = 0;
  26. clip.start = 0;
  27. clip.length = clipLength > 0 ? clipLength : (std::int64_t) ppq * 4 * 4; // 4 bars = 8 s at 120 BPM
  28. s.playlist.push_back (clip);
  29. RackState::Factories f;
  30. f.samples = [sample] (const std::string&) { return std::shared_ptr<const SampleData> (sample); };
  31. constexpr double rate = 44100.0;
  32. auto state = RackState::build (s, nullptr, rate, f);
  33. RackRenderer r;
  34. r.prepare (rate, 441);
  35. std::vector<float> out, l (441), rr (441);
  36. double tick = 0.0;
  37. for (int b = 0; b < (int) (seconds * 100.0); ++b)
  38. {
  39. r.process (state.get(), true, tick, 441, l.data(), rr.data());
  40. tick += 441 * ticksPerSample (s.bpm, rate);
  41. out.insert (out.end(), l.begin(), l.end());
  42. }
  43. return out;
  44. }
  45. double cents (double heard, double expected)
  46. {
  47. return heard > 0.0 && expected > 0.0 ? 1200.0 * std::log2 (heard / expected) : 1.0e9;
  48. }
  49. } // namespace w163c
  50. TEST (w163c_an_audio_clip_follows_its_channel_pitch_by_resampling)
  51. {
  52. // 1 s of 440 Hz: the Clip properties' / Channel settings' pitch did nothing to audio clips
  53. const auto sine = s162::sine (440.0, 1.0);
  54. const auto up = s162::analyse (w163c::renderClip (sine, 12.0f, 0.0f, false), 44100.0);
  55. CHECK (std::abs (w163c::cents (up.freq, 880.0)) < 2.0);
  56. CHECK (std::abs (up.length - 22050) < 300); // like a tape: an octave up plays in half the time
  57. const auto fifth = s162::analyse (w163c::renderClip (sine, 7.0f, 0.0f, false), 44100.0);
  58. CHECK (std::abs (w163c::cents (fifth.freq, 440.0 * std::exp2 (7.0 / 12.0))) < 2.0);
  59. const auto down = s162::analyse (w163c::renderClip (sine, -12.0f, 0.0f, false), 44100.0);
  60. CHECK (std::abs (w163c::cents (down.freq, 220.0)) < 2.0);
  61. CHECK (std::abs (down.length - 88200) < 300);
  62. // 50 cents: on the pitch knob (fractions of a semitone) or in Misc › Fine tune
  63. const auto half = s162::analyse (w163c::renderClip (sine, 0.5f, 0.0f, false), 44100.0);
  64. CHECK (std::abs (w163c::cents (half.freq, 440.0 * std::exp2 (0.5 / 12.0))) < 2.0);
  65. const auto fine = s162::analyse (w163c::renderClip (sine, 0.0f, 50.0f, false), 44100.0);
  66. CHECK (std::abs (w163c::cents (fine.freq, 440.0 * std::exp2 (0.5 / 12.0))) < 2.0);
  67. }
  68. TEST (w163c_keep_length_transposes_an_audio_clip_without_changing_its_length)
  69. {
  70. const auto sine = s162::sine (440.0, 1.0);
  71. for (const auto semitones : { 7.0f, -12.0f, 0.5f })
  72. {
  73. const auto heard = s162::analyse (w163c::renderClip (sine, semitones, 0.0f, true), 44100.0);
  74. CHECK (std::abs (w163c::cents (heard.freq, 440.0 * std::exp2 (semitones / 12.0))) < 3.0);
  75. CHECK (std::abs (heard.length - 44100) < 2205); // within 50 ms of the second it lasts
  76. }
  77. }
  78. TEST (w163c_an_unpitched_clip_plays_exactly_as_before)
  79. {
  80. // pitch 0: the same samples with or without "keep length" (no shifter, no resampling), at the sample's rate
  81. const auto sine = s162::sine (440.0, 1.0);
  82. const auto plain = w163c::renderClip (sine, 0.0f, 0.0f, false, 1.5);
  83. const auto kept = w163c::renderClip (sine, 0.0f, 0.0f, true, 1.5);
  84. CHECK (plain == kept);
  85. const auto heard = s162::analyse (plain, 44100.0);
  86. CHECK (std::abs (w163c::cents (heard.freq, 440.0)) < 1.0 && std::abs (heard.length - 44100) < 50);
  87. }
  88. TEST (w163c_a_sampler_keeping_length_transposes_its_keys_without_stretch)
  89. {
  90. // "Keep length" with no Stretch: the keys transpose through the pitch shifter, the sample keeps its second
  91. lynx::SamplerSettings settings;
  92. settings.keepPitch = true;
  93. const auto sine = s162::sine (440.0, 1.0);
  94. const auto low = s162::playLive (settings, sine, 48, 0.0f, 2.0);
  95. CHECK (std::abs (w163c::cents (low.freq, 220.0)) < 3.0);
  96. CHECK (std::abs (low.length - 44100) < 2205);
  97. settings.keepPitch = false; // resampled: an octave down lasts twice as long
  98. const auto tape = s162::playLive (settings, sine, 48, 0.0f, 3.0);
  99. CHECK (std::abs (w163c::cents (tape.freq, 220.0)) < 2.0);
  100. CHECK (std::abs (tape.length - 88200) < 300);
  101. }
  102. TEST (w163c_the_pitch_command_sets_the_channel_and_rescales_its_clips)
  103. {
  104. using namespace lynx;
  105. Song s;
  106. s.bpm = 120.0;
  107. ChannelDef c;
  108. c.instrument = InstrumentKind::audioClip;
  109. c.samplePath = "x.wav";
  110. s.channels.push_back (c);
  111. Clip clip;
  112. clip.kind = ClipKind::audio;
  113. clip.channel = 0;
  114. clip.length = (std::int64_t) ppq * 4; // 2 s at 120 BPM = the whole 2 s file
  115. clip.offset = (std::int64_t) ppq;
  116. s.playlist.push_back (clip);
  117. const double frames = 88200.0, rate = 44100.0;
  118. CHECK (clipops::fileTicks (s.channels[0], frames, rate, s.bpm) == (std::int64_t) ppq * 4);
  119. // an octave up, resampled: the file (and every clip of it) plays in half the time
  120. clipops::setChannelPitch (s, 0, 12.0, 0.0, false, frames, rate);
  121. CHECK (s.channels[0].sampler.pitch == 12.0f && ! s.channels[0].sampler.keepPitch);
  122. CHECK (clipops::clipSpeed (s.channels[0]) == 2.0);
  123. CHECK (clipops::fileTicks (s.channels[0], frames, rate, s.bpm) == (std::int64_t) ppq * 2);
  124. CHECK (s.playlist[0].length == (std::int64_t) ppq * 2 && s.playlist[0].offset == (std::int64_t) ppq / 2);
  125. // back to 0 with keep length, then +7 st 25 cents keeping length: the clips stay as they are
  126. clipops::setChannelPitch (s, 0, 0.0, 0.0, true, frames, rate);
  127. CHECK (s.playlist[0].length == (std::int64_t) ppq * 4 && s.playlist[0].offset == (std::int64_t) ppq);
  128. clipops::setChannelPitch (s, 0, 7.0, 25.0, true, frames, rate);
  129. CHECK (std::abs (s.channels[0].sampler.pitch - 7.25f) < 1.0e-6f && s.channels[0].sampler.keepPitch);
  130. CHECK (clipops::clipSpeed (s.channels[0]) == 1.0);
  131. CHECK (s.playlist[0].length == (std::int64_t) ppq * 4);
  132. // a sample channel: only its pitch (its notes have no clips), clamped to ±24
  133. ChannelDef sampler;
  134. sampler.instrument = InstrumentKind::sampler;
  135. s.channels.push_back (sampler);
  136. clipops::setChannelPitch (s, 1, -30.0, 0.0, false);
  137. CHECK (s.channels[1].sampler.pitch == -24.0f);
  138. }
  139. TEST (w163c_clip_properties_keep_length_without_a_stretch)
  140. {
  141. using namespace lynx;
  142. Song s;
  143. ChannelDef c;
  144. c.instrument = InstrumentKind::audioClip;
  145. s.channels.push_back (c);
  146. Clip clip;
  147. clip.kind = ClipKind::audio;
  148. clip.channel = 0;
  149. clip.length = ppq;
  150. s.playlist.push_back (clip);
  151. clipprops::Props p;
  152. CHECK (clipprops::read (s, 0, p));
  153. p.pitch = 3.0f;
  154. p.keepPitch = true; // used to be dropped unless the clip was stretched
  155. CHECK (clipprops::apply (s, 0, p));
  156. CHECK (s.channels[0].sampler.pitch == 3.0f && s.channels[0].sampler.keepPitch);
  157. }