4
0

LoopBrowser190kfTests.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Round gaps190kf (item 3): the Loop Browser's keywords and filters (core/LoopKeywords.h) — GarageBand's keyword
  2. // buttons (several picked = all of them), the Scale menu (Any / Minor / Major / Neither / Good for Both), Loop Types,
  3. // favourites, keys near the project key, LYNX's tempo filter and the keyword layout (swap / place / reset).
  4. // Evidence: /mnt/e/lynx-work/gaps190kf/loops/NOTES.md.
  5. #include "core/LoopKeywords.h"
  6. TEST (loops190kf_words_split_folders_names_camel_and_digits)
  7. {
  8. using namespace lynx::loopkeys;
  9. CHECK ((words ("/lib/Drum Loops/DarkPad90_Am.wav") == std::vector<std::string> { "lib", "drum", "loops", "dark", "pad", "90", "am" }));
  10. CHECK ((words ("C:\\Packs\\808s\\Hi-Hat 01.WAV") == std::vector<std::string> { "c", "packs", "808s", "hi", "hat", "01" }));
  11. CHECK (words ("").empty());
  12. }
  13. TEST (loops190kf_keywords_from_names_folders_and_implied)
  14. {
  15. using namespace lynx::loopkeys;
  16. // a kick in a Kicks folder: Kick and the family it implies (All Drums)
  17. auto k = describe ("/s/Drums/Kicks/Kick Hard 01.wav", "Drums/Kicks/Kick Hard 01.wav");
  18. CHECK (k.keywords.count ("kick") == 1);
  19. CHECK (k.keywords.count ("all drums") == 1);
  20. CHECK (k.keywords.count ("intense") == 1); // ("hard")
  21. CHECK (k.type == audio);
  22. CHECK (k.scale == Scale::neither); // drums have no scale
  23. // "bass drum" is a kick, not a bass
  24. k = describe ("/s/Bass Drum 2.wav", "Bass Drum 2.wav");
  25. CHECK (k.keywords.count ("kick") == 1);
  26. CHECK (k.keywords.count ("bass") == 0);
  27. // hi-hat written three ways
  28. for (const auto* n : { "Hi-Hat open.wav", "HiHat.wav", "closed hat 3.wav" })
  29. CHECK (describe (std::string ("/s/") + n, n).keywords.count ("hi-hat") == 1);
  30. // plurals and camelCase; a synth bass is a bass too
  31. k = describe ("/s/SynthBass Reese 140bpm Fm.wav", "SynthBass Reese 140bpm Fm.wav");
  32. CHECK (k.keywords.count ("synth bass") == 1);
  33. CHECK (k.keywords.count ("bass") == 1);
  34. CHECK (k.tempo == 140.0);
  35. CHECK (k.keyRoot == 5);
  36. CHECK (k.scale == Scale::minor);
  37. // the user's own folder names above the place don't count (wordsOf), the place's do
  38. k = describe ("/home/rock/Music/Pads/Warm Pad.wav", "Pads/Warm Pad.wav");
  39. CHECK (k.keywords.count ("rock") == 0);
  40. CHECK (k.keywords.count ("pads") == 1);
  41. CHECK (k.keywords.count ("synths") == 1); // (a pad implies Synths)
  42. }
  43. TEST (loops190kf_tags_and_factory_keywords)
  44. {
  45. using namespace lynx::loopkeys;
  46. // a user tag that names a keyword (by id or label, any case) gives it; other tags don't make keywords
  47. auto k = describe ("/s/take 7.wav", "take 7.wav", { "Dark", "my best", "ELEC PIANO" }, { "lo-fi" });
  48. CHECK (k.keywords.count ("dark") == 1);
  49. CHECK (k.keywords.count ("elec piano") == 1);
  50. CHECK (k.keywords.count ("lo-fi") == 1);
  51. CHECK (k.keywords.size() == 3);
  52. // a factory keyword's implied ones come too
  53. k = describe ("/f/a.mid", "a.mid", {}, { "kick" });
  54. CHECK (k.keywords.count ("all drums") == 1);
  55. CHECK (k.type == midi);
  56. CHECK (find ("Hi-Hat") != nullptr);
  57. CHECK (find ("nonsense") == nullptr);
  58. }
  59. TEST (loops190kf_loop_types_by_extension)
  60. {
  61. using namespace lynx::loopkeys;
  62. CHECK (typeOf ("a.wav") == audio);
  63. CHECK (typeOf ("a.AIFF") == audio);
  64. CHECK (typeOf ("a.flac") == audio);
  65. CHECK (typeOf ("a.mid") == midi);
  66. CHECK (typeOf ("a.fsc") == pattern);
  67. CHECK (typeOf ("a.fst") == 0);
  68. CHECK (typeOf ("noext") == 0);
  69. }
  70. TEST (loops190kf_scale_from_the_name)
  71. {
  72. using namespace lynx::loopkeys;
  73. CHECK (describe ("/s/Keys 120 Am.wav").scale == Scale::minor);
  74. CHECK (describe ("/s/Keys 120 Cmaj.wav").scale == Scale::major);
  75. CHECK (describe ("/s/Keys F# major.wav").scale == Scale::major);
  76. // a root alone (loop packs' "Bass_120_G"): good for both
  77. const auto g = describe ("/s/Bass_120_G.wav");
  78. CHECK (g.scale == Scale::both);
  79. CHECK (g.keyRoot == 7);
  80. CHECK (keyText (g) == "G");
  81. CHECK (keyText (describe ("/s/Keys 120 Am.wav")) == "Am");
  82. // a tonal loop with no key: unknown (shown under Any only)
  83. CHECK (describe ("/s/Piano Ballad.wav").scale == Scale::any);
  84. }
  85. TEST (loops190kf_tempo_from_the_name)
  86. {
  87. using namespace lynx::loopkeys;
  88. CHECK (describe ("/s/Beat 98 bpm.wav").tempo == 98.0);
  89. CHECK (describe ("/s/Drum Loop_128.wav").tempo == 128.0); // (a bare number in a loop's name)
  90. CHECK (describe ("/s/Snare 100.wav").tempo == 0.0); // (a bare number in a one-shot's name is not a tempo)
  91. CHECK (tempoText (describe ("/s/Beat 98 bpm.wav")) == "98");
  92. CHECK (tempoText (describe ("/s/Snare 100.wav")).empty());
  93. }
  94. TEST (loops190kf_keyword_buttons_narrow_to_all_picked)
  95. {
  96. using namespace lynx::loopkeys;
  97. // GarageBand: "Clicking multiple buttons narrows the results to those loops that match all of the selected keywords"
  98. const std::vector<LoopInfo> loops { describe ("/s/Dark Piano 90 Am.wav", "Dark Piano 90 Am.wav"), describe ("/s/Piano Happy 120 C maj.wav", "Piano Happy 120 C maj.wav"),
  99. describe ("/s/Dark Pad Loop 70 Dm.wav", "Dark Pad Loop 70 Dm.wav"), describe ("/s/Kick 1.wav", "Kick 1.wav"),
  100. describe ("/s/Trap Beat 140 bpm.wav", "Trap Beat 140 bpm.wav"), describe ("/s/Lead Arp.mid", "Lead Arp.mid") };
  101. const auto count = [&loops] (const Filter& f, std::vector<bool> fav = {}) {
  102. int n = 0;
  103. for (std::size_t i = 0; i < loops.size(); ++i)
  104. n += matches (loops[i], f, i < fav.size() && fav[i]) ? 1 : 0;
  105. return n;
  106. };
  107. Filter f;
  108. CHECK (! f.active());
  109. CHECK (count (f) == 6);
  110. f.keywords = { "piano" };
  111. CHECK (count (f) == 2);
  112. f.keywords = { "piano", "dark" };
  113. CHECK (count (f) == 1);
  114. f.keywords = { "dark" };
  115. CHECK (count (f) == 2);
  116. // the counts behind the buttons: with Dark picked, Piano still finds 1, Kick none (dimmed)
  117. const auto counts = keywordCounts (loops, f, [] (std::size_t) { return false; });
  118. CHECK (counts.at ("piano") == 1);
  119. CHECK (counts.at ("kick") == 0);
  120. CHECK (counts.at ("dark") == 2);
  121. // the Scale menu
  122. f = {};
  123. f.scale = Scale::minor;
  124. CHECK (count (f) == 2);
  125. f.scale = Scale::major;
  126. CHECK (count (f) == 1);
  127. f.scale = Scale::neither;
  128. CHECK (count (f) == 2); // (the kick and the trap beat)
  129. // Loop Types: MIDI only
  130. f = {};
  131. f.types = midi;
  132. CHECK (count (f) == 1);
  133. f.types = audio | pattern;
  134. CHECK (count (f) == 5);
  135. // favourites only (GarageBand's Favorites button)
  136. f = {};
  137. f.favouritesOnly = true;
  138. CHECK (count (f, { false, true, false, false, true }) == 2);
  139. // the search text (the Browser's own query language) on the name
  140. f = {};
  141. f.text = "dark NOT pad";
  142. CHECK (count (f) == 1);
  143. }
  144. TEST (loops190kf_tempo_and_near_key_filters)
  145. {
  146. using namespace lynx::loopkeys;
  147. const std::vector<LoopInfo> loops { describe ("/s/A 90 bpm Am.wav"), describe ("/s/B 120 bpm Em.wav"), describe ("/s/C 128 bpm.wav"), describe ("/s/Kick.wav") };
  148. Filter f;
  149. const auto [lo, hi] = tempoBounds (1, 125.0); // near the project tempo (±6 %)
  150. f.tempoMin = lo;
  151. f.tempoMax = hi;
  152. CHECK (matches (loops[1], f, false));
  153. CHECK (matches (loops[2], f, false));
  154. CHECK (! matches (loops[0], f, false));
  155. CHECK (! matches (loops[3], f, false)); // (no tempo known)
  156. CHECK ((tempoBounds (0, 120.0) == std::pair<double, double> { 0.0, 0.0 }));
  157. CHECK ((tempoBounds (3, 120.0) == std::pair<double, double> { 90.0, 110.0 }));
  158. // GarageBand's "within two semitones of the project key": project in G (7): Am (9) yes, Em (4) no; no key stays
  159. f = {};
  160. f.nearKey = 7;
  161. CHECK (matches (loops[0], f, false));
  162. CHECK (! matches (loops[1], f, false));
  163. CHECK (matches (loops[3], f, false));
  164. CHECK (rootDistance (11, 1) == 2);
  165. CHECK (rootDistance (0, 6) == 6);
  166. }
  167. TEST (loops190kf_vocabulary_grid_and_layout)
  168. {
  169. using namespace lynx::loopkeys;
  170. // three categories; ids unique; the instrument grid starts with GarageBand's first row's families
  171. std::set<std::string> ids;
  172. for (const auto& k : vocabulary())
  173. CHECK (ids.insert (k.id).second);
  174. const auto inst = ofCategory (Category::instrument);
  175. CHECK (inst.size() == 48);
  176. CHECK (std::string (inst[0]->id) == "all drums");
  177. CHECK (std::string (inst[1]->id) == "piano");
  178. CHECK (std::string (inst[2]->id) == "guitars");
  179. CHECK (std::string (inst[3]->id) == "brass");
  180. CHECK (ofCategory (Category::genre).size() == 20);
  181. CHECK (ofCategory (Category::descriptor).size() == 20);
  182. // every implied keyword exists
  183. for (const auto& k : vocabulary())
  184. for (const auto* a : k.also)
  185. CHECK (find (a) != nullptr);
  186. // the layout: swap two buttons, put another keyword on a place, save / load, reset
  187. auto l = Layout::defaults();
  188. CHECK (l.swap (Category::instrument, 0, 1));
  189. CHECK (l.order[0][0] == "piano");
  190. CHECK (l.place (Category::genre, 0, "jazz")); // (jazz was shown: the two swap)
  191. CHECK (l.order[1][0] == "jazz");
  192. CHECK (! l.swap (Category::genre, 0, 99));
  193. const auto back = Layout::fromText (l.toText());
  194. CHECK (back == l);
  195. CHECK (Layout::fromText ("instrument=nonsense,piano\n").order[0].size() == 48);
  196. CHECK (Layout::fromText ("instrument=nonsense,piano\n").order[0][0] == "piano");
  197. CHECK (Layout::fromText ("") == Layout::defaults());
  198. }
  199. TEST (loops190kf_factory_source_hook)
  200. {
  201. using namespace lynx::loopkeys;
  202. // round 190ke's factory tags plug in here (a thread-safe lookup by path); none by default
  203. CHECK (factoryKeywords ("/x/a.wav").empty());
  204. setFactorySource ([] (const std::string& path) { return path == "/lynx/Starter Beat.wav" ? std::vector<std::string> { "hip hop", "beats" } : std::vector<std::string> {}; });
  205. const auto k = describe ("/lynx/Starter Beat.wav", "Starter Beat.wav", {}, factoryKeywords ("/lynx/Starter Beat.wav"));
  206. CHECK (k.keywords.count ("hip hop") == 1);
  207. CHECK (k.keywords.count ("all drums") == 1); // (beats implies All Drums)
  208. CHECK (factoryKeywords ("/x/a.wav").empty());
  209. setFactorySource (nullptr);
  210. CHECK (factoryKeywords ("/lynx/Starter Beat.wav").empty());
  211. }