LoopAnalysis190kxTests.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Round 190kx: loop analysis — the key and tempo of the user's own loops (core/LoopAnalysis.h). The laws measured on
  2. // LYNX's own synthetic corpus (tests/LoopCorpus190kx.h; the whole table: tools/loop_accuracy.cpp), the cache, its text
  3. // form, the worker, and what the Simple drop's switch defaults to.
  4. #include <atomic>
  5. #include <map>
  6. #include "LoopCorpus190kx.h"
  7. #include "core/LoopAnalysis.h"
  8. #include "core/TempoDetect.h"
  9. namespace loop190kx
  10. {
  11. inline bool sameTempo (double got, double want) { return got > 0.0 && std::abs (got / want - 1.0) < 0.01; }
  12. inline bool sameKey (const std::optional<lynx::keyfollow::LoopKey>& k, const loopcorpus::Item& it)
  13. {
  14. return k && k->root == it.keyRoot && k->minor == it.minor;
  15. }
  16. // "drums-house", "tonal-chords", "mixed-trap", "oneshot-drum"
  17. inline std::string group (const loopcorpus::Item& it) { return it.name.substr (0, it.name.find ('-', it.name.find ('-') + 1)); }
  18. } // namespace loop190kx
  19. TEST (loop190kx_tempo_beats_the_old_detector_in_every_class)
  20. {
  21. using namespace lynx;
  22. using namespace loop190kx;
  23. std::map<std::string, std::pair<int, int>> exact; // group -> (new, legacy)
  24. int fresh = 0, old = 0, wholeNew = 0, wholeOld = 0, loops = 0;
  25. for (const auto& it : loopcorpus::corpus (true))
  26. {
  27. if (! (it.bpm > 0.0))
  28. continue;
  29. ++loops;
  30. const auto a = loopanalysis::analyse (*it.audio, it.name + ".wav", false);
  31. const auto l = tempodetect::detect (it.audio->leftData(), it.audio->isStereo() ? it.audio->rightData() : nullptr, it.audio->frames(), it.audio->sampleRate);
  32. const auto legacyOk = sameTempo (l.bpm, it.bpm) && l.confidence >= 0.35;
  33. auto& e = exact[group (it)];
  34. e.first += sameTempo (a.bpm, it.bpm), e.second += legacyOk;
  35. fresh += sameTempo (a.bpm, it.bpm), old += legacyOk;
  36. // a loop of whole beats: its length in beats at the found tempo is whole (4 or more)
  37. wholeNew += a.wholeBeats && std::abs (a.beats - std::round (a.beats)) < 0.05;
  38. wholeOld += l.beats >= 3.9 && std::abs (l.beats - std::round (l.beats)) <= 0.04 && l.confidence >= 0.35;
  39. }
  40. for (const auto& [g, e] : exact)
  41. {
  42. CHECK (e.first >= e.second); // nothing gets worse
  43. if (e.first < e.second)
  44. std::printf (" loop190kx: %s tempo exact %d < legacy %d\n", g.c_str(), e.first, e.second);
  45. }
  46. std::printf (" loop190kx: tempo exact %d of %d (legacy %d), whole-beat loops %d (legacy %d)\n", fresh, loops, old, wholeNew, wholeOld);
  47. CHECK (fresh >= old + loops / 6); // (measured on the subset: a clear gain, see LOOPS-HANDOFF.md for the full table)
  48. CHECK (wholeNew >= wholeOld);
  49. }
  50. TEST (loop190kx_key_beats_the_old_detector_and_drums_get_no_key)
  51. {
  52. using namespace lynx;
  53. using namespace loop190kx;
  54. int fresh = 0, old = 0, keyed = 0, falseNew = 0, falseOld = 0, unkeyed = 0;
  55. std::map<std::string, std::pair<int, int>> exact;
  56. for (const auto& it : loopcorpus::corpus (true))
  57. {
  58. const auto d = loopanalysis::detectKey (*it.audio);
  59. const auto l = keyfollow::detectKey (*it.audio);
  60. if (it.keyRoot >= 0)
  61. {
  62. ++keyed;
  63. fresh += sameKey (d.key, it), old += sameKey (l.key, it);
  64. auto& e = exact[group (it)];
  65. e.first += sameKey (d.key, it), e.second += sameKey (l.key, it);
  66. }
  67. else
  68. {
  69. ++unkeyed;
  70. falseNew += d.key.has_value(), falseOld += l.key.has_value();
  71. }
  72. }
  73. // (per class on this small subset a class of 4-7 loops may lose one to chance — the arpeggios: short plucked notes
  74. // that the harmonic / percussive split thins out; on the whole corpus, tools/loop_accuracy.cpp, no class is worse)
  75. for (const auto& [g, e] : exact)
  76. {
  77. CHECK (e.first + 1 >= e.second);
  78. if (e.first < e.second)
  79. std::printf (" loop190kx: %s key exact %d < legacy %d\n", g.c_str(), e.first, e.second);
  80. }
  81. std::printf (" loop190kx: key exact %d of %d (legacy %d); a key given to %d of %d drum loops / hits (legacy %d)\n", fresh, keyed, old, falseNew, unkeyed,
  82. falseOld);
  83. CHECK (fresh > old);
  84. CHECK (falseNew <= falseOld && falseNew * 5 <= unkeyed); // percussion: at most one in five gets a key (the whole corpus: 6 of 130)
  85. }
  86. TEST (loop190kx_name_and_acid_come_first_and_long_files_are_left_alone)
  87. {
  88. using namespace lynx;
  89. const auto drums = loopcorpus::drums ("house", 124.0, 4, 3);
  90. // the audio says 124; the name says 120 and A minor, ACID says 16 beats: ACID, then the name, win (they are the
  91. // maker's own data)
  92. auto acid = *drums.audio;
  93. acid.acidBeats = 16.0f;
  94. auto r = loopanalysis::analyse (acid, "Loop 120 Am.wav");
  95. CHECK (r.tempoSource == loopanalysis::TempoSource::acid && std::abs (r.bpm - 124.0) < 0.01 && r.wholeBeats);
  96. CHECK (r.keySource == loopanalysis::KeySource::name && (r.key == keyfollow::LoopKey { 9, true }));
  97. r = loopanalysis::analyse (*drums.audio, "Groove 1.wav");
  98. CHECK (r.tempoSource == loopanalysis::TempoSource::detected && std::abs (r.bpm - 124.0) < 0.01 && r.wholeBeats && r.isLoop());
  99. CHECK (r.keySource == loopanalysis::KeySource::none && ! r.key); // (drums: no key)
  100. CHECK (loopanalysis::tempoText (r) == "124 BPM");
  101. // over 70 seconds: a song or a take, not analysed
  102. lynx::SampleData song;
  103. song.sampleRate = 44100.0;
  104. song.left.assign ((std::size_t) (71.0 * 44100.0), 0.0f);
  105. for (std::size_t i = 0; i < drums.audio->frames(); ++i)
  106. song.left[i] = drums.audio->leftData()[i];
  107. r = loopanalysis::analyse (song, "Song.wav");
  108. CHECK (r.bpm == 0.0 && ! r.wholeBeats && ! r.key && r.seconds > 70.0);
  109. // a take that is not a whole number of beats (a loop with 0.73 s more of itself): its tempo, but not a loop
  110. {
  111. auto take = *loopcorpus::drums ("rock", 100.0, 4, 6).audio;
  112. const auto extra = (std::size_t) (0.73 * take.sampleRate);
  113. for (std::size_t i = 0; i < extra; ++i)
  114. take.left.push_back (take.left[i]);
  115. take.right.clear();
  116. r = loopanalysis::analyse (take, "Take 3.wav");
  117. CHECK (! r.wholeBeats && std::abs (r.bpm - 100.0) < 1.0);
  118. if (r.wholeBeats || std::abs (r.bpm - 100.0) >= 1.0)
  119. std::printf (" loop190kx: take %.3f BPM, %.3f beats, whole %d\n", r.bpm, r.beats, (int) r.wholeBeats);
  120. }
  121. // a one-shot: no tempo, no loop
  122. const auto hit = loopcorpus::oneShot (false, 0, 4);
  123. r = loopanalysis::analyse (*hit.audio, "Kick.wav");
  124. CHECK (! r.isLoop() && ! r.key);
  125. }
  126. TEST (loop190kx_cache_is_per_file_stamp_and_round_trips_as_text)
  127. {
  128. using namespace lynx;
  129. loopanalysis::Cache cache;
  130. loopanalysis::Result r;
  131. r.seconds = 7.74, r.bpm = 124.0, r.tempoConfidence = 0.8, r.beats = 16.0, r.wholeBeats = true;
  132. r.tempoSource = loopanalysis::TempoSource::detected;
  133. r.key = keyfollow::LoopKey { 2, true };
  134. r.keyConfidence = 0.5f;
  135. r.keySource = loopanalysis::KeySource::detected;
  136. const loopanalysis::Stamp stamp { "/loops/Groove 1.wav", 1234, 99 };
  137. cache.put (stamp, r);
  138. CHECK (cache.find (stamp).has_value());
  139. CHECK (! cache.find ({ stamp.path, 1235, 99 }).has_value()); // the file changed: analysed again
  140. CHECK (! cache.find ({ stamp.path, 1234, 100 }).has_value());
  141. CHECK (cache.findPath (stamp.path).has_value());
  142. const auto line = loopanalysis::toText (stamp, r);
  143. loopanalysis::Stamp s2;
  144. loopanalysis::Result r2;
  145. CHECK (loopanalysis::fromText (line, s2, r2));
  146. CHECK (s2 == stamp && std::abs (r2.bpm - 124.0) < 1.0e-6 && r2.wholeBeats && r2.key == r.key && r2.keySource == r.keySource
  147. && r2.tempoSource == r.tempoSource && std::abs (r2.beats - 16.0) < 1.0e-6);
  148. CHECK (! loopanalysis::fromText ("garbage", s2, r2));
  149. CHECK (loopanalysis::toText ({ "/a\tb.wav", 1, 1 }, r).empty()); // (a tab in the path: not kept)
  150. // the oldest half goes beyond the capacity
  151. for (std::size_t i = 0; i <= loopanalysis::Cache::capacity; ++i)
  152. cache.put ({ "/f" + std::to_string (i), 1, 1 }, r);
  153. CHECK (cache.size() <= loopanalysis::Cache::capacity && cache.size() > loopanalysis::Cache::capacity / 3);
  154. CHECK (cache.find ({ "/f" + std::to_string (loopanalysis::Cache::capacity), 1, 1 }).has_value()); // the newest stays
  155. }
  156. TEST (loop190kx_worker_analyses_what_is_asked_once_and_answers_cached_files_at_once)
  157. {
  158. using namespace lynx;
  159. loopanalysis::Cache::shared().clear();
  160. const auto drums = loopcorpus::drums ("rock", 100.0, 4, 5);
  161. std::atomic<int> decoded { 0 }, answered { 0 };
  162. loopanalysis::Service::Callbacks cb;
  163. cb.stat = [] (const std::string& path, std::int64_t& size, std::int64_t& modified) {
  164. if (path.find ("missing") != std::string::npos)
  165. return false;
  166. size = 100, modified = 7;
  167. return true;
  168. };
  169. cb.decode = [&] (const std::string&, double, lynx::SampleData& out, double& total) {
  170. ++decoded;
  171. out = *drums.audio;
  172. total = (double) out.frames() / out.sampleRate;
  173. return true;
  174. };
  175. cb.analysed = [&] (const std::string&) { ++answered; };
  176. cb.throttle = false;
  177. {
  178. loopanalysis::Service service (std::move (cb));
  179. CHECK (service.request ({ "/l/a.wav", "/l/b.wav", "/l/missing.wav" }) == 3);
  180. CHECK (service.waitIdle (60000));
  181. CHECK (decoded == 2 && answered == 2 && service.done() == 2);
  182. const auto a = loopanalysis::Cache::shared().find ({ "/l/a.wav", 100, 7 });
  183. CHECK (a.has_value() && std::abs (a->bpm - 100.0) < 0.01 && a->isLoop());
  184. // asked again: answered from the cache, not decoded again
  185. service.request ({ "/l/a.wav" });
  186. CHECK (service.waitIdle (60000));
  187. CHECK (decoded == 2 && answered == 3);
  188. CHECK (service.find ("/l/b.wav").has_value());
  189. }
  190. loopanalysis::Cache::shared().clear();
  191. }
  192. TEST (loop190kx_simple_drop_switch_is_on_by_default)
  193. {
  194. // BandLab fits its loops to the project's tempo and key: the Simple layout's drops do it by default
  195. CHECK (lynx::loopanalysis::dropDetects().load());
  196. }