LoopInfoShot190kx.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // --loopinfo190kxshot <folder> (round 190kx, loop analysis): LYNX's own synthetic loops (tests/LoopCorpus190kx.h)
  2. // written as WAVs with names that say nothing of their tempo or key, then drawn offscreen:
  3. // - the Simple layout's Browser rows with the bars, tempo and key found (BandLab's loop rows);
  4. // - the Pattern layout's Browser on the same folder (FL's rows: nothing added);
  5. // - the Sample browser's Loop Browser with its Tempo / Key columns filled by the analysis;
  6. // - an audio clip's menu with the LYNX extras (Follow the project key, Loop key with "Detect from the audio", Fit to
  7. // the project tempo).
  8. // <folder>/checks.txt lists what was checked, with the paint times. Included in the app class of Main.cpp.
  9. bool runLoopInfoShot190kx (const juce::File& folder)
  10. {
  11. using namespace lynx;
  12. juce::LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);
  13. folder.createDirectory();
  14. juce::StringArray report;
  15. bool allOk = true;
  16. const auto check = [&report, &allOk] (const juce::String& name, bool ok, const juce::String& detail = {}) {
  17. report.add (name + "=" + (ok ? "OK" : "FAIL") + (detail.isEmpty() ? juce::String() : " " + detail));
  18. allOk = allOk && ok;
  19. };
  20. // the user's own loops: names without a tempo or a key
  21. const auto library = folder.getChildFile ("library").getChildFile ("My Loops");
  22. library.createDirectory();
  23. struct Made
  24. {
  25. const char* name;
  26. loopcorpus::Item item;
  27. };
  28. const Made made[] { { "Groove 1", loopcorpus::drums ("house", 124.0, 4, 11) },
  29. { "Boom bap idea", loopcorpus::drums ("boombap", 90.0, 2, 12) },
  30. { "Late night keys", loopcorpus::tonal ("chords", 2, true, 96.0, 4, 13) },
  31. { "Bass idea", loopcorpus::tonal ("bass", 9, true, 120.0, 4, 14) },
  32. { "Synth arp", loopcorpus::tonal ("arp", 4, true, 128.0, 4, 15) },
  33. { "Band jam", loopcorpus::mixed ("rock", 7, false, 110.0, 4, 16) },
  34. { "Kick hit", loopcorpus::oneShot (false, 0, 17) } };
  35. juce::WavAudioFormat wav;
  36. for (const auto& m : made)
  37. {
  38. const auto f = library.getChildFile (juce::String (m.name) + ".wav");
  39. f.deleteFile();
  40. const auto& a = *m.item.audio;
  41. juce::AudioBuffer<float> buffer (a.isStereo() ? 2 : 1, (int) a.frames());
  42. buffer.copyFrom (0, 0, a.leftData(), (int) a.frames());
  43. if (a.isStereo())
  44. buffer.copyFrom (1, 0, a.rightData(), (int) a.frames());
  45. if (auto out = std::unique_ptr<juce::OutputStream> (f.createOutputStream()))
  46. if (auto writer = std::unique_ptr<juce::AudioFormatWriter> (
  47. wav.createWriterFor (out.get(), a.sampleRate, (unsigned int) buffer.getNumChannels(), 16, {}, 0)))
  48. {
  49. out.release();
  50. writer->writeFromAudioSampleBuffer (buffer, 0, buffer.getNumSamples());
  51. }
  52. }
  53. PluginHost::settingsFolder().createDirectory();
  54. PluginHost::settingsFolder().getChildFile ("browser-folders.txt").replaceWithText (library.getParentDirectory().getFullPathName());
  55. BrowserComponent::readPrefs = false;
  56. auto savePngOf = [&] (juce::Component& c, const juce::String& name) {
  57. const auto image = shot::paint (c, theme::colour::bg2);
  58. const auto f = shot::file (folder.getChildFile (name));
  59. f.deleteFile();
  60. juce::PNGImageFormat png;
  61. bool ok = false;
  62. if (auto out = f.createOutputStream())
  63. ok = png.writeImageToStream (image, *out);
  64. check (name, ok && image.getWidth() > 0);
  65. };
  66. const auto paintMs = [] (juce::Component& c) {
  67. juce::Image image (juce::Image::ARGB, c.getWidth(), c.getHeight(), true);
  68. double best = 1.0e9;
  69. for (int round = 0; round < 3; ++round)
  70. {
  71. const auto t0 = juce::Time::getMillisecondCounterHiRes();
  72. for (int k = 0; k < 40; ++k)
  73. {
  74. juce::Graphics g (image);
  75. c.paintEntireComponent (g, false);
  76. }
  77. best = juce::jmin (best, (juce::Time::getMillisecondCounterHiRes() - t0) / 40.0);
  78. }
  79. return best;
  80. };
  81. const auto before = layoutstyle::current();
  82. AudioEngine engine (false);
  83. engine.editSong ([] (Song& s) { s.bpm = 120.0; });
  84. // 1. the Pattern layout (FL's Browser): the folder's rows as before, nothing analysed
  85. {
  86. layoutstyle::setCurrent (layoutstyle::Style::pattern);
  87. BrowserComponent browser (engine, BrowserComponent::Role::main);
  88. browser.setSize (360, 520);
  89. check ("pattern_located", browser.locateFile (library.getChildFile ("Groove 1.wav")));
  90. browser.waitLoopInfoForSnapshot (1000);
  91. check ("pattern_rows_unchanged", browser.loopRowTextsForCheck().isEmpty(), browser.loopRowTextsForCheck().joinIntoString ("|"));
  92. savePngOf (browser, "kx-02-pattern-rows-unchanged.png");
  93. check ("pattern_paint_ms", true, juce::String (paintMs (browser), 3));
  94. }
  95. // 2. the Simple layout: the rows show the bars, tempo and key found (BandLab's loop rows)
  96. {
  97. layoutstyle::setCurrent (layoutstyle::Style::simple);
  98. BrowserComponent browser (engine, BrowserComponent::Role::main);
  99. browser.setSize (360, 640);
  100. check ("simple_located", browser.locateFile (library.getChildFile ("Groove 1.wav")));
  101. // (the rows before the answers: the same paint as before this round; then with the texts, the cost they add)
  102. const auto bareMs = paintMs (browser);
  103. const auto t0 = juce::Time::getMillisecondCounterHiRes();
  104. check ("simple_analysed", browser.waitLoopInfoForSnapshot (60000));
  105. const auto analysedMs = juce::Time::getMillisecondCounterHiRes() - t0;
  106. const auto texts = browser.loopRowTextsForCheck();
  107. report.add ("simple_rows " + texts.joinIntoString (" | "));
  108. check ("simple_analysis_ms", true, juce::String (analysedMs, 0) + " ms for " + juce::String ((int) std::size (made)) + " files");
  109. const auto has = [&texts] (const juce::String& row) { return texts.contains (row); };
  110. check ("groove_124", has (juce::String::fromUTF8 ("Groove 1: 4 bars \xc2\xb7 124 BPM")));
  111. check ("boombap_90", has (juce::String::fromUTF8 ("Boom bap idea: 2 bars \xc2\xb7 90 BPM")));
  112. check ("keys_d_minor", texts.joinIntoString ("|").contains (juce::String ("Late night keys: ")) && texts.joinIntoString ("|").contains ("D minor"));
  113. check ("kick_hit_nothing", ! texts.joinIntoString ("|").contains ("Kick hit"));
  114. savePngOf (browser, "kx-01-simple-rows.png");
  115. check ("simple_paint_ms", true, "rows without texts " + juce::String (bareMs, 3) + " with texts " + juce::String (paintMs (browser), 3));
  116. // idle: the worker waits on its condition (no polling): the process' CPU over 2 s with the Browser shown
  117. {
  118. const auto c0 = std::clock();
  119. juce::Thread::sleep (2000);
  120. const auto cpuMs = 1000.0 * (double) (std::clock() - c0) / CLOCKS_PER_SEC;
  121. check ("idle_cpu_ms_in_2s", cpuMs < 100.0, juce::String (cpuMs, 1) + " ms (worker queued " + juce::String ((int) browser.loopInfoQueuedForCheck()) + ")");
  122. }
  123. // the switch of the drop (on by default, as BandLab)
  124. check ("drop_detects_default_on", loopanalysis::dropDetects().load());
  125. }
  126. // 3. the Sample browser's Loop Browser: Tempo and Key columns filled where the names say nothing
  127. {
  128. layoutstyle::setCurrent (layoutstyle::Style::pattern);
  129. BrowserComponent browser (engine, BrowserComponent::Role::samples);
  130. browser.setSize (340, 620);
  131. browser.setLoopsShown (true);
  132. browser.loadLoopsForSnapshot();
  133. browser.waitLoopInfoForSnapshot (60000);
  134. const auto rows = browser.resultLabelsForCheck();
  135. report.add ("loop_browser_rows " + rows.joinIntoString (" | "));
  136. savePngOf (browser, "kx-03-loop-browser-columns.png");
  137. // Tempo 110 - 130 now finds the user's loops by their detected tempo
  138. browser.setLoopTempoChoice (4);
  139. check ("tempo_filter_finds_detected", browser.numLoopsShownForSnapshot() >= 3, juce::String (browser.numLoopsShownForSnapshot()));
  140. savePngOf (browser, "kx-04-loop-browser-tempo-110-130.png");
  141. browser.setLoopTempoChoice (0);
  142. browser.setLoopsShown (false);
  143. }
  144. // 4. an audio clip's menu: LYNX extras with the key items and the one-click fit (every layout)
  145. {
  146. layoutstyle::setCurrent (layoutstyle::Style::pattern);
  147. auto song = Song::makeDefault();
  148. song.bpm = 120.0;
  149. ChannelDef keys;
  150. keys.name = "Late night keys";
  151. keys.instrument = InstrumentKind::audioClip;
  152. keys.samplePath = library.getChildFile ("Late night keys.wav").getFullPathName().toStdString();
  153. song.channels.push_back (keys);
  154. Clip c;
  155. c.kind = ClipKind::audio;
  156. c.channel = (int) song.channels.size() - 1;
  157. c.track = 2;
  158. c.start = 0;
  159. c.length = 16 * ppq;
  160. c.followKey = true; // (the menu then says how far it moved)
  161. song.playlist.push_back (c);
  162. projectkey::set (song, keyfollow::projectKeyFor ({ 4, true })); // E minor
  163. keyfollow::setLoopKey (song, c.channel, keyfollow::LoopKey { 2, true }, 2); // D minor, detected
  164. engine.setSong (std::move (song));
  165. engine.dispatchPendingMessages();
  166. PlaylistComponent playlist (engine);
  167. playlist.setSize (900, 500);
  168. const auto index = (int) engine.getSong().playlist.size() - 1;
  169. const auto menu = playlist.getClipMenuForSnapshot (index);
  170. bool follow = false, fit = false, loopKey = false;
  171. for (juce::PopupMenu::MenuItemIterator it (menu, true); it.next();)
  172. {
  173. const auto& t = it.getItem().text;
  174. follow = follow || t.startsWith (tr ("Follow the project key"));
  175. fit = fit || t.startsWith (tr ("Fit to the project tempo ({0}, from {1} BPM)").upToFirstOccurrenceOf ("(", false, false));
  176. loopKey = loopKey || t.startsWith (tr ("Loop key"));
  177. }
  178. check ("menu_follow_key", follow);
  179. check ("menu_loop_key", loopKey);
  180. check ("menu_fit_project_tempo", fit);
  181. savePng (columnMenuImage (menu), folder.getChildFile ("kx-05-clip-menu-lynx-extras.png").getFullPathName());
  182. }
  183. layoutstyle::setCurrent (before);
  184. folder.getChildFile ("checks.txt").replaceWithText (report.joinIntoString ("\n") + "\n");
  185. return allOk;
  186. }