Round190tyCheck.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Round 190ty (issue #369, esiowry): the Chord helper reads the key from the selected chord AND the chords before it,
  2. // with the scale the user set where the selection is as a strong hint (core/ModalHelper.h, "the key the chords
  3. // imply"). Through the real Piano roll and Chord helper panel, off screen; no window or menu is opened.
  4. // Included in the app class of Main.cpp; --round190tytest <report> [<shot prefix>] and --selftest "round190ty".
  5. // With a shot prefix: <prefix>-issue369-pair.png / -panel.png (the issue's progression, fixed) at 100 % and @150.
  6. juce::String round190tyCheck (juce::String& log, const juce::String& shots = {})
  7. {
  8. using namespace lynx;
  9. juce::String result = "OK";
  10. const auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) {
  11. log << (ok ? "ok " : "FAIL ") << what << (detail.isNotEmpty() ? " (" + detail + ")" : juce::String()) << "\n";
  12. if (! ok && result == "OK")
  13. result = "ERROR " + what;
  14. };
  15. const auto layoutBefore = layoutstyle::current();
  16. layoutstyle::setCurrent (layoutstyle::Style::pattern);
  17. const auto writeImage = [&] (const juce::Image& image, const juce::String& name) {
  18. if (shots.isEmpty())
  19. return;
  20. const juce::File file (shots + "-" + name + ".png");
  21. file.deleteFile();
  22. juce::FileOutputStream stream (file);
  23. check ("shot " + name, stream.openedOk() && juce::PNGImageFormat().writeImageToStream (image, stream));
  24. };
  25. // the Piano roll and the helper side by side (the editor left, the helper's window right), at 1x and 1.5x
  26. const auto shootPair = [&] (juce::Component& roll, juce::Component& panel, const juce::String& name) {
  27. for (const auto scale : { 1.0f, 1.5f })
  28. {
  29. const auto w = roll.getWidth() + panel.getWidth() + 12, h = juce::jmax (roll.getHeight(), panel.getHeight());
  30. juce::Image image (juce::Image::ARGB, juce::roundToInt ((float) w * scale), juce::roundToInt ((float) h * scale), true);
  31. {
  32. juce::Graphics g (image);
  33. g.fillAll (juce::Colour (0xff101114));
  34. g.drawImageAt (roll.createComponentSnapshot (roll.getLocalBounds(), true, scale), 0, 0);
  35. g.drawImageAt (panel.createComponentSnapshot (panel.getLocalBounds(), true, scale), juce::roundToInt ((float) (roll.getWidth() + 12) * scale), 0);
  36. }
  37. writeImage (image, name + (scale > 1.0f ? "@150" : ""));
  38. }
  39. };
  40. const auto shootPanel = [&] (juce::Component& panel, const juce::String& name) {
  41. writeImage (panel.createComponentSnapshot (panel.getLocalBounds(), true, 1.0f), name);
  42. writeImage (panel.createComponentSnapshot (panel.getLocalBounds(), true, 1.5f), name + "@150");
  43. };
  44. const auto bar = 4 * (std::int64_t) ppq;
  45. // a song with one pattern of chords (a bar each) on channel 0
  46. const auto makeSong = [bar] (const std::vector<std::vector<int>>& progression) {
  47. auto song = Song::makeDefault();
  48. song.currentPattern = 0;
  49. auto& p = song.patterns.front();
  50. p.notes.clear();
  51. for (std::size_t c = 0; c < progression.size(); ++c)
  52. for (const auto key : progression[c])
  53. {
  54. Note n;
  55. n.channel = 0;
  56. n.start = (std::int64_t) c * bar;
  57. n.length = bar;
  58. n.key = key;
  59. p.notes.push_back (n);
  60. }
  61. p.lengthTicks = (std::int64_t) (progression.size() + 2) * bar;
  62. return song;
  63. };
  64. const auto lastChord = [] (const Song& song) {
  65. std::vector<int> last;
  66. const auto& notes = song.patterns.front().notes;
  67. std::int64_t latest = -1;
  68. for (const auto& n : notes)
  69. latest = std::max (latest, n.start);
  70. for (int i = 0; i < (int) notes.size(); ++i)
  71. if (notes[(std::size_t) i].start == latest)
  72. last.push_back (i);
  73. return last;
  74. };
  75. const auto roman = [] (const modalhelp::Analysis& a) { return juce::String::fromUTF8 (modalhelp::numeralFigure (a.key, a.chord, a.quality).c_str()); };
  76. const auto keyName = [] (const modalhelp::Analysis& a) {
  77. return juce::String (a.key.tonic) + " " + juce::String (modalhelp::modeName (a.key.mode));
  78. };
  79. using Source = ChordHelperPanel::HintSource;
  80. // --- 1. the issue's progression: Cmaj9, Fmaj9, then Dm9 (the header's "Dm7") selected ---------------------------
  81. {
  82. AudioEngine engine (false);
  83. engine.setSong (makeSong ({ { 60, 64, 67, 71, 74 }, { 65, 69, 72, 76, 79 }, { 62, 65, 69, 72, 76 } }));
  84. engine.setSelectedChannel (0);
  85. engine.dispatchPendingMessages();
  86. PianoRollComponent roll (engine);
  87. roll.setBounds (0, 0, 820, 440);
  88. roll.setVisible (true);
  89. engine.sendChangeMessage(); // (the editor learns its pattern and channel)
  90. engine.dispatchPendingMessages();
  91. roll.selectMidiToolResult (lastChord (engine.getSong()));
  92. roll.scrollToNotes (true);
  93. engine.dispatchPendingMessages();
  94. const auto target = [&roll, &engine] {
  95. ChordHelperPanel::Target t;
  96. t.pattern = engine.getSong().currentPattern;
  97. t.channel = engine.getSelectedChannel();
  98. bool selected = false;
  99. t.selected = roll.midiToolNotes (selected);
  100. if (const auto [root, steps] = roll.chordHelperScale(); ! steps.empty())
  101. t.editorScale = modalhelp::KeyHint (root, steps);
  102. return t;
  103. };
  104. ChordHelperPanel panel (engine, target, [&roll] (const std::vector<int>& i) { roll.selectOwnNewNotes (i); }, ChordHelperPanel::Look::fl);
  105. const auto size = ChordHelperPanel::preferredSize (ChordHelperPanel::Look::fl);
  106. panel.setBounds (0, 0, size.x, size.y);
  107. panel.setVisible (true);
  108. panel.refresh();
  109. const auto& a = panel.analysis();
  110. check ("369 no scale: C Ionian, not D Dorian", a.valid && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian, keyName (a));
  111. check ("369 Dm7 reads ii7", roman (a) == "ii7", roman (a));
  112. check ("369 read with the 2 chords before it", a.contextChords == 2, juce::String (a.contextChords));
  113. check ("369 no hint without a scale", panel.hintSource() == Source::none && ! a.hinted);
  114. check ("369 related keys follow the key: C minor first", ! panel.relatedKeys().empty() && panel.relatedKeys().front().scale.tonic == 0
  115. && panel.relatedKeys().front().scale.mode == modalhelp::Mode::aeolian);
  116. check ("369 borrowed chords as C's: iv first, moved from D", ! panel.borrowedChords().empty() && panel.borrowedChords().front().numeral == "iv"
  117. && panel.borrowedChords().front().move.up == 3);
  118. // the Piano roll's scale C Major (the issue's toolbar): a strong hint, the same reading
  119. roll.setScaleForSnapshot (0, 1);
  120. panel.refresh();
  121. check ("369 the Piano roll's scale is the hint", panel.hintSource() == Source::pianoRollScale && panel.keyHint() && panel.keyHint()->tonic == 0
  122. && panel.keyHint()->steps == std::vector<int> { 0, 2, 4, 5, 7, 9, 11 });
  123. check ("369 with C Major set: C Ionian, ii7", a.hinted && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian && roman (a) == "ii7", keyName (a));
  124. roll.scrollToNotes (true);
  125. shootPair (roll, panel, "issue369-pair");
  126. shootPanel (panel, "issue369-panel");
  127. // the header's "The selected chord's root": the helper's first rule (D as the tonic, Dm7 = i7)
  128. panel.setTonicFromChord (true);
  129. check ("369 the selected chord's root: D, i7", a.key.tonic == 2 && a.forcedTonic && roman (a) == "i7", keyName (a) + " " + roman (a));
  130. shootPanel (panel, "issue369-panel-chord-root");
  131. panel.setTonicFromChord (false); // (the Key menu's "Auto: from the chords and the scale")
  132. check ("369 Auto again", a.key.tonic == 0 && ! a.forcedTonic);
  133. // Automatic is a guess, not a hint
  134. roll.setScaleForSnapshot (0, 0);
  135. check ("369 no scale: no toolbar hint", roll.chordHelperScale().second.empty());
  136. panel.refresh(); // (the hint went: one new reading, then none while nothing changes)
  137. check ("369 no scale: C Ionian still", a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian && panel.hintSource() == Source::none);
  138. const auto runs = panel.computations();
  139. for (int i = 0; i < 10; ++i)
  140. panel.refresh();
  141. check ("369 computes only on change", panel.computations() == runs);
  142. }
  143. // --- 2. where the hint comes from: a key marker, a Playlist key marker, the project key ---------------------------
  144. {
  145. // F then G (G selected): C major's IV V, or A minor's VI VII — the hint decides
  146. AudioEngine engine (false);
  147. auto song = makeSong ({ { 53, 57, 60 }, { 55, 59, 62 } });
  148. engine.setSong (std::move (song));
  149. engine.setSelectedChannel (0);
  150. engine.dispatchPendingMessages();
  151. const auto selection = lastChord (engine.getSong());
  152. std::optional<modalhelp::KeyHint> editorScale;
  153. const auto target = [&engine, &selection, &editorScale] {
  154. ChordHelperPanel::Target t;
  155. t.pattern = 0;
  156. t.channel = 0;
  157. t.selected = selection;
  158. t.editorScale = editorScale;
  159. return t;
  160. };
  161. ChordHelperPanel panel (engine, target, {}, ChordHelperPanel::Look::fl);
  162. panel.refresh();
  163. const auto& a = panel.analysis();
  164. check ("F G alone: C Ionian (IV V)", a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian && roman (a) == "V", keyName (a));
  165. const projectkey::Key aMinor { 9, keyregions::minorScale() };
  166. // the project key (FL's project scale): A minor
  167. engine.editSong ([aMinor] (Song& s) { projectkey::set (s, aMinor); });
  168. engine.dispatchPendingMessages();
  169. panel.refresh();
  170. check ("project key A minor: A Aeolian, G = VII", panel.hintSource() == Source::projectKey && a.key.tonic == 9 && a.key.mode == modalhelp::Mode::aeolian
  171. && roman (a) == "\xe2\x99\xad" "VII", keyName (a) + " " + roman (a));
  172. // the Piano roll's own scale wins over the project key: C Major
  173. editorScale = modalhelp::KeyHint (0, { 0, 2, 4, 5, 7, 9, 11 });
  174. panel.refresh();
  175. check ("Piano roll scale C Major over the project key", panel.hintSource() == Source::pianoRollScale && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian);
  176. // a Playlist key marker over the pattern's clip comes after the Piano roll's scale, before the project key
  177. engine.editSong ([bar, aMinor] (Song& s) {
  178. projectkey::set (s, std::nullopt);
  179. s.playlist.clear();
  180. Clip c;
  181. c.kind = ClipKind::pattern;
  182. c.pattern = 0;
  183. c.start = 8 * bar;
  184. c.offset = 0;
  185. c.length = 4 * bar;
  186. s.playlist.push_back (c);
  187. keyregions::addKeyMarker (s.markers(), 8 * bar, keyregions::State { keyregions::State::key, aMinor });
  188. });
  189. engine.dispatchPendingMessages();
  190. editorScale.reset();
  191. panel.refresh();
  192. check ("Playlist key marker over the clip: A Aeolian", panel.hintSource() == Source::playlistMarker && a.key.tonic == 9 && a.key.mode == modalhelp::Mode::aeolian,
  193. keyName (a));
  194. // a key marker of the pattern itself comes first (and a Black notes one means no hint at all)
  195. engine.editSong ([] (Song& s) {
  196. keyregions::addKeyMarker (s.patterns.front(), 0, keyregions::State { keyregions::State::key, projectkey::Key { 0, keyregions::majorScale() } });
  197. });
  198. engine.dispatchPendingMessages();
  199. editorScale = modalhelp::KeyHint (9, { 0, 2, 3, 5, 7, 8, 10 });
  200. panel.refresh();
  201. check ("pattern key marker C Major first", panel.hintSource() == Source::patternMarker && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian, keyName (a));
  202. engine.editSong ([] (Song& s) { keyregions::addKeyMarker (s.patterns.front(), 0, keyregions::State { keyregions::State::none, {} }); });
  203. engine.dispatchPendingMessages();
  204. panel.refresh();
  205. check ("Black notes key marker: no hint", panel.hintSource() == Source::none && ! a.hinted);
  206. }
  207. layoutstyle::setCurrent (layoutBefore);
  208. return result;
  209. }