AudioToMidi189bTests.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // --- Convert to MIDI (round a2m189b): Basic Pitch's windows and note creation, melody, drums, the new pattern -------
  2. #include <random>
  3. #include "core/AudioToMidi.h"
  4. TEST (a2m189b_windows_are_stitched_on_an_exact_time_grid)
  5. {
  6. using namespace lynx::a2m;
  7. // a fake model that answers each frame with the input sample at that frame: the stitched output must be the ramp
  8. // at k * hop (within half a frame: the 141.27-frame stride is not whole)
  9. for (const std::int64_t samples : { (std::int64_t) 1000, (std::int64_t) windowSamples, (std::int64_t) windowStride * 3 + 517 })
  10. {
  11. std::vector<float> ramp ((std::size_t) samples);
  12. for (std::int64_t i = 0; i < samples; ++i)
  13. ramp[(std::size_t) i] = (float) i;
  14. constexpr int perWindow = 172;
  15. int calls = 0;
  16. double last = 0.0;
  17. Posteriorgram pg;
  18. const auto ok = runModel (ramp, perWindow, [&] (const float* in, float* note, float* onset) {
  19. ++calls;
  20. for (int f = 0; f < perWindow; ++f)
  21. for (int b = 0; b < keys; ++b)
  22. {
  23. const auto at = std::min (f * hop, windowSamples - 1);
  24. // (the padding before the audio reads 0; mark it so it can't pass for sample 0)
  25. note[f * keys + b] = in[at] == 0.0f && at < overlapSamples / 2 ? -1.0f : in[at];
  26. onset[f * keys + b] = (float) b;
  27. }
  28. return true;
  29. }, [&] (double p) { last = p; }, pg);
  30. CHECK (ok);
  31. CHECK (calls == numWindows (samples));
  32. CHECK (last == 1.0);
  33. CHECK (pg.frames == (int) ((samples + hop - 1) / hop));
  34. auto worst = 0.0;
  35. for (int k = 0; k < pg.frames; ++k)
  36. {
  37. const auto expected = std::min<double> ((double) k * hop, (double) samples - 1);
  38. const auto got = (double) pg.note[(std::size_t) k * keys];
  39. if ((double) k * hop < (double) samples - hop)
  40. worst = std::max (worst, std::abs (got - expected));
  41. CHECK (pg.onset[(std::size_t) k * keys + 7] == 7.0f);
  42. }
  43. CHECK (worst <= hop / 2.0);
  44. }
  45. }
  46. TEST (a2m189b_model_stop_ends_the_run)
  47. {
  48. using namespace lynx::a2m;
  49. std::vector<float> audio ((std::size_t) windowStride * 5, 0.1f);
  50. int calls = 0;
  51. Posteriorgram pg;
  52. CHECK (! runModel (audio, 172, [&] (const float*, float*, float*) { return ++calls < 2; }, {}, pg));
  53. CHECK (calls == 2);
  54. }
  55. namespace
  56. {
  57. lynx::a2m::Posteriorgram emptyPg (int frames)
  58. {
  59. lynx::a2m::Posteriorgram pg;
  60. pg.frames = frames;
  61. pg.note.assign ((std::size_t) frames * lynx::a2m::keys, 0.0f);
  62. pg.onset = pg.note;
  63. return pg;
  64. }
  65. void paint (lynx::a2m::Posteriorgram& pg, int key, int from, int to, float level, float onset)
  66. {
  67. const auto b = key - lynx::a2m::lowestKey;
  68. for (int t = from; t < to; ++t)
  69. pg.note[(std::size_t) t * lynx::a2m::keys + (std::size_t) b] = level;
  70. if (onset > 0.0f)
  71. pg.onset[(std::size_t) from * lynx::a2m::keys + (std::size_t) b] = onset;
  72. }
  73. } // namespace
  74. TEST (a2m189b_basic_pitch_note_creation)
  75. {
  76. using namespace lynx::a2m;
  77. const auto frameSeconds = hop / modelRate;
  78. auto pg = emptyPg (400);
  79. paint (pg, 60, 20, 80, 0.8f, 0.9f); // a clear note with an onset
  80. paint (pg, 64, 100, 105, 0.8f, 0.9f); // too short (5 frames < 11)
  81. paint (pg, 67, 150, 220, 0.6f, 0.0f); // no onset: only the melodia trick finds it
  82. paint (pg, 48, 250, 300, 0.2f, 0.9f); // below the frame threshold: no note
  83. NoteOptions options;
  84. options.inferOnsets = false;
  85. auto notes = notesFromPosteriorgram (pg, options);
  86. CHECK (notes.size() == 2);
  87. CHECK (notes[0].key == 60 && std::abs (notes[0].start - 20 * frameSeconds) < 1e-9 && std::abs (notes[0].end - 80 * frameSeconds) < 1e-9);
  88. CHECK (std::abs (notes[0].velocity - 0.8f) < 1e-6f);
  89. CHECK (notes[1].key == 67 && std::abs (notes[1].start - 150 * frameSeconds) < 1e-9);
  90. options.melodiaTrick = false;
  91. notes = notesFromPosteriorgram (pg, options);
  92. CHECK (notes.size() == 1 && notes[0].key == 60);
  93. // a key range leaves the rest out; the mode settings: melody keeps the melodia trick, harmony doesn't
  94. options.minKey = 61;
  95. CHECK (notesFromPosteriorgram (pg, options).empty());
  96. CHECK (optionsFor (Mode::melody).melodiaTrick && ! optionsFor (Mode::harmony).melodiaTrick);
  97. CHECK (optionsFor (Mode::melody).onsetThreshold == 0.7f && optionsFor (Mode::harmony).frameThreshold == 0.4f);
  98. }
  99. TEST (a2m189b_melody_keeps_one_note_at_a_time)
  100. {
  101. using namespace lynx::a2m;
  102. // a fundamental with a weaker octave above it, then a legato step to the next note while the first still sounds
  103. std::vector<Note> notes { { 0.00, 0.60, 40, 0.8f }, { 0.01, 0.55, 52, 0.4f }, { 0.50, 1.00, 43, 0.7f },
  104. { 0.70, 0.80, 55, 0.3f } };
  105. const auto mono = monophonic (notes, 0.05);
  106. CHECK (mono.size() == 2);
  107. CHECK (mono[0].key == 40 && std::abs (mono[0].end - 0.50) < 1e-9); // cut where the next one starts
  108. CHECK (mono[1].key == 43 && std::abs (mono[1].end - 1.00) < 1e-9); // the weak overtone inside it is left out
  109. for (std::size_t i = 1; i < mono.size(); ++i)
  110. CHECK (mono[i].start >= mono[i - 1].end);
  111. }
  112. TEST (a2m189b_drum_hits_are_found_and_told_apart)
  113. {
  114. using namespace lynx::a2m;
  115. constexpr double rate = 44100.0;
  116. std::vector<float> audio ((std::size_t) (rate * 8.0), 0.0f);
  117. std::mt19937 rng (189);
  118. std::uniform_real_distribution<float> noise (-1.0f, 1.0f);
  119. struct Hit { double time; int key; };
  120. std::vector<Hit> hits;
  121. const auto beat = 0.5;
  122. for (int i = 0; i < 16; ++i)
  123. {
  124. const auto t = 0.25 + i * beat;
  125. hits.push_back ({ t, i % 2 == 0 ? kickKey : snareKey });
  126. hits.push_back ({ t + beat / 2, hatKey });
  127. }
  128. for (const auto& h : hits)
  129. {
  130. const auto start = (std::size_t) (h.time * rate);
  131. float dull = 0.0f, duller = 0.0f, dullest = 0.0f; // the snare's noise, low-passed 3 times around 3 kHz (wires, not a cymbal)
  132. for (std::size_t i = 0; i < (std::size_t) (rate * 0.25) && start + i < audio.size(); ++i)
  133. {
  134. const auto s = (double) i / rate;
  135. float v = 0.0f;
  136. if (h.key == kickKey) // a falling 60 Hz sine
  137. v = (float) (std::sin (2.0 * 3.141592653589793 * 60.0 * s) * std::exp (-s * 12.0));
  138. else if (h.key == snareKey) // 200 Hz body and noise, ringing ~150 ms
  139. {
  140. dull += 0.35f * (noise (rng) - dull);
  141. duller += 0.35f * (dull - duller);
  142. dullest += 0.35f * (duller - dullest);
  143. v = (float) ((0.5 * std::sin (2.0 * 3.141592653589793 * 200.0 * s) + 2.0 * dullest) * std::exp (-s * 18.0) * std::min (1.0, s / 0.002));
  144. }
  145. else // a short burst of differentiated (bright) noise
  146. v = (float) (0.4 * (noise (rng) - noise (rng)) * std::exp (-s * 60.0));
  147. audio[start + i] += 0.5f * v;
  148. }
  149. }
  150. const auto found = drumHits (audio.data(), (std::int64_t) audio.size(), rate);
  151. for (const int key : { kickKey, snareKey, hatKey })
  152. {
  153. int expected = 0, matched = 0, got = 0;
  154. for (const auto& h : hits)
  155. if (h.key == key)
  156. {
  157. ++expected;
  158. matched += std::any_of (found.begin(), found.end(), [&] (const Note& n) { return n.key == key && std::abs (n.start - h.time) < 0.03; }) ? 1 : 0;
  159. }
  160. for (const auto& n : found)
  161. got += n.key == key ? 1 : 0;
  162. CHECK (matched >= expected - 1); // recall
  163. CHECK (got <= expected + 2); // precision
  164. }
  165. CHECK (drumHits (nullptr, 0, rate).empty());
  166. }
  167. TEST (a2m189b_the_pattern_lands_under_the_clip)
  168. {
  169. using namespace lynx;
  170. Song song;
  171. ChannelDef audioChannel;
  172. audioChannel.name = "loop";
  173. audioChannel.instrument = InstrumentKind::audioClip;
  174. audioChannel.samplePath = "/x/loop.wav";
  175. song.channels.push_back (audioChannel);
  176. Clip clip;
  177. clip.kind = ClipKind::audio;
  178. clip.channel = 0;
  179. clip.track = 2;
  180. clip.start = 4 * ppq;
  181. clip.length = 8 * ppq;
  182. clip.offset = ppq; // the clip starts one beat into the file
  183. song.playlist.push_back (clip);
  184. Clip other;
  185. other.track = 3;
  186. song.playlist.push_back (other);
  187. // 120 BPM, 44.1 kHz: one beat = 22050 frames
  188. a2m::ClipTiming timing;
  189. timing.sampleRate = 44100.0;
  190. timing.framesPerTick = 22050.0 / ppq;
  191. timing.spanFrames = 22050.0 * 16;
  192. const auto first = (std::int64_t) 22050; // the analysed audio starts at the clip's first frame
  193. const std::vector<a2m::Note> notes { { 0.0, 0.25, 60, 0.5f }, { 1.0, 1.5, 64, 0.9f }, { -0.6, -0.55, 70, 0.5f }, { 3.9, 9.0, 67, 0.5f } };
  194. a2m::Names names { "loop - Melody", "loop - Melody", "Kick", "Snare", "Hat" };
  195. CHECK (a2m::addClipPattern (song, 0, "/x/other.wav", a2m::Mode::melody, notes, first, timing, names) == -1);
  196. const auto pattern = a2m::addClipPattern (song, 0, "/x/loop.wav", a2m::Mode::melody, notes, first, timing, names);
  197. CHECK (pattern == (int) song.patterns.size() - 1);
  198. const auto& p = song.patterns[(std::size_t) pattern];
  199. CHECK (p.name == "loop - Melody");
  200. CHECK (p.notes.size() == 3); // (the one before the clip is left out)
  201. CHECK (p.notes[0].start == 0 && p.notes[0].length == ppq / 2 && p.notes[0].key == 60);
  202. CHECK (p.notes[1].start == 2 * ppq && p.notes[1].length == ppq);
  203. CHECK (p.notes[2].start == (std::int64_t) std::llround (3.9 * 2 * ppq) && p.notes[2].start + p.notes[2].length == 8 * ppq); // cut at the clip's end
  204. CHECK (song.channels.size() == 2 && song.channels[1].instrument == InstrumentKind::synth && song.channels[1].name == "loop - Melody");
  205. CHECK (p.notes[0].channel == 1);
  206. // a new track right under the audio clip; the clips below move down
  207. CHECK (song.playlist[1].track == 4);
  208. const auto& added = song.playlist.back();
  209. CHECK (added.kind == ClipKind::pattern && added.pattern == pattern && added.track == 3 && added.start == clip.start && added.length == clip.length);
  210. // drums: Kick, Snare and Hat channels, one step-long note per hit on the step grid's key
  211. const std::vector<a2m::Note> hits { { 0.0, 0.06, a2m::kickKey, 1.0f }, { 0.5, 0.56, a2m::snareKey, 0.8f }, { 0.75, 0.81, a2m::hatKey, 0.5f } };
  212. const auto drums = a2m::addClipPattern (song, 0, "/x/loop.wav", a2m::Mode::drums, hits, first, timing, names);
  213. CHECK (drums >= 0 && song.channels.size() == 5);
  214. CHECK (song.channels[2].instrument == InstrumentKind::kick && song.channels[3].instrument == InstrumentKind::snare && song.channels[4].instrument == InstrumentKind::hat);
  215. const auto& d = song.patterns[(std::size_t) drums];
  216. CHECK (d.notes.size() == 3 && d.notes[0].channel == 2 && d.notes[1].channel == 3 && d.notes[2].channel == 4);
  217. CHECK (d.notes[1].key == 60 && d.notes[1].length == ticksPerStep && d.notes[1].start == ppq);
  218. // a clip that plays backwards mirrors the times
  219. timing.backwards = true;
  220. const auto clipFirst = a2m::clipFrames (clip, timing, 22050 * 16);
  221. CHECK (clipFirst.first == 22050 * 16 - 9 * 22050 && clipFirst.count == 8 * 22050);
  222. }