// Round 190ty (issue #369, esiowry): the Chord helper reads the key from the selected chord AND the chords before it, // with the scale the user set where the selection is as a strong hint (core/ModalHelper.h, "the key the chords // imply"). Through the real Piano roll and Chord helper panel, off screen; no window or menu is opened. // Included in the app class of Main.cpp; --round190tytest [] and --selftest "round190ty". // With a shot prefix: -issue369-pair.png / -panel.png (the issue's progression, fixed) at 100 % and @150. juce::String round190tyCheck (juce::String& log, const juce::String& shots = {}) { using namespace lynx; juce::String result = "OK"; const auto check = [&] (const juce::String& what, bool ok, const juce::String& detail = {}) { log << (ok ? "ok " : "FAIL ") << what << (detail.isNotEmpty() ? " (" + detail + ")" : juce::String()) << "\n"; if (! ok && result == "OK") result = "ERROR " + what; }; const auto layoutBefore = layoutstyle::current(); layoutstyle::setCurrent (layoutstyle::Style::pattern); const auto writeImage = [&] (const juce::Image& image, const juce::String& name) { if (shots.isEmpty()) return; const juce::File file (shots + "-" + name + ".png"); file.deleteFile(); juce::FileOutputStream stream (file); check ("shot " + name, stream.openedOk() && juce::PNGImageFormat().writeImageToStream (image, stream)); }; // the Piano roll and the helper side by side (the editor left, the helper's window right), at 1x and 1.5x const auto shootPair = [&] (juce::Component& roll, juce::Component& panel, const juce::String& name) { for (const auto scale : { 1.0f, 1.5f }) { const auto w = roll.getWidth() + panel.getWidth() + 12, h = juce::jmax (roll.getHeight(), panel.getHeight()); juce::Image image (juce::Image::ARGB, juce::roundToInt ((float) w * scale), juce::roundToInt ((float) h * scale), true); { juce::Graphics g (image); g.fillAll (juce::Colour (0xff101114)); g.drawImageAt (roll.createComponentSnapshot (roll.getLocalBounds(), true, scale), 0, 0); g.drawImageAt (panel.createComponentSnapshot (panel.getLocalBounds(), true, scale), juce::roundToInt ((float) (roll.getWidth() + 12) * scale), 0); } writeImage (image, name + (scale > 1.0f ? "@150" : "")); } }; const auto shootPanel = [&] (juce::Component& panel, const juce::String& name) { writeImage (panel.createComponentSnapshot (panel.getLocalBounds(), true, 1.0f), name); writeImage (panel.createComponentSnapshot (panel.getLocalBounds(), true, 1.5f), name + "@150"); }; const auto bar = 4 * (std::int64_t) ppq; // a song with one pattern of chords (a bar each) on channel 0 const auto makeSong = [bar] (const std::vector>& progression) { auto song = Song::makeDefault(); song.currentPattern = 0; auto& p = song.patterns.front(); p.notes.clear(); for (std::size_t c = 0; c < progression.size(); ++c) for (const auto key : progression[c]) { Note n; n.channel = 0; n.start = (std::int64_t) c * bar; n.length = bar; n.key = key; p.notes.push_back (n); } p.lengthTicks = (std::int64_t) (progression.size() + 2) * bar; return song; }; const auto lastChord = [] (const Song& song) { std::vector last; const auto& notes = song.patterns.front().notes; std::int64_t latest = -1; for (const auto& n : notes) latest = std::max (latest, n.start); for (int i = 0; i < (int) notes.size(); ++i) if (notes[(std::size_t) i].start == latest) last.push_back (i); return last; }; const auto roman = [] (const modalhelp::Analysis& a) { return juce::String::fromUTF8 (modalhelp::numeralFigure (a.key, a.chord, a.quality).c_str()); }; const auto keyName = [] (const modalhelp::Analysis& a) { return juce::String (a.key.tonic) + " " + juce::String (modalhelp::modeName (a.key.mode)); }; using Source = ChordHelperPanel::HintSource; // --- 1. the issue's progression: Cmaj9, Fmaj9, then Dm9 (the header's "Dm7") selected --------------------------- { AudioEngine engine (false); engine.setSong (makeSong ({ { 60, 64, 67, 71, 74 }, { 65, 69, 72, 76, 79 }, { 62, 65, 69, 72, 76 } })); engine.setSelectedChannel (0); engine.dispatchPendingMessages(); PianoRollComponent roll (engine); roll.setBounds (0, 0, 820, 440); roll.setVisible (true); engine.sendChangeMessage(); // (the editor learns its pattern and channel) engine.dispatchPendingMessages(); roll.selectMidiToolResult (lastChord (engine.getSong())); roll.scrollToNotes (true); engine.dispatchPendingMessages(); const auto target = [&roll, &engine] { ChordHelperPanel::Target t; t.pattern = engine.getSong().currentPattern; t.channel = engine.getSelectedChannel(); bool selected = false; t.selected = roll.midiToolNotes (selected); if (const auto [root, steps] = roll.chordHelperScale(); ! steps.empty()) t.editorScale = modalhelp::KeyHint (root, steps); return t; }; ChordHelperPanel panel (engine, target, [&roll] (const std::vector& i) { roll.selectOwnNewNotes (i); }, ChordHelperPanel::Look::fl); const auto size = ChordHelperPanel::preferredSize (ChordHelperPanel::Look::fl); panel.setBounds (0, 0, size.x, size.y); panel.setVisible (true); panel.refresh(); const auto& a = panel.analysis(); check ("369 no scale: C Ionian, not D Dorian", a.valid && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian, keyName (a)); check ("369 Dm7 reads ii7", roman (a) == "ii7", roman (a)); check ("369 read with the 2 chords before it", a.contextChords == 2, juce::String (a.contextChords)); check ("369 no hint without a scale", panel.hintSource() == Source::none && ! a.hinted); check ("369 related keys follow the key: C minor first", ! panel.relatedKeys().empty() && panel.relatedKeys().front().scale.tonic == 0 && panel.relatedKeys().front().scale.mode == modalhelp::Mode::aeolian); check ("369 borrowed chords as C's: iv first, moved from D", ! panel.borrowedChords().empty() && panel.borrowedChords().front().numeral == "iv" && panel.borrowedChords().front().move.up == 3); // the Piano roll's scale C Major (the issue's toolbar): a strong hint, the same reading roll.setScaleForSnapshot (0, 1); panel.refresh(); check ("369 the Piano roll's scale is the hint", panel.hintSource() == Source::pianoRollScale && panel.keyHint() && panel.keyHint()->tonic == 0 && panel.keyHint()->steps == std::vector { 0, 2, 4, 5, 7, 9, 11 }); 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)); roll.scrollToNotes (true); shootPair (roll, panel, "issue369-pair"); shootPanel (panel, "issue369-panel"); // the header's "The selected chord's root": the helper's first rule (D as the tonic, Dm7 = i7) panel.setTonicFromChord (true); check ("369 the selected chord's root: D, i7", a.key.tonic == 2 && a.forcedTonic && roman (a) == "i7", keyName (a) + " " + roman (a)); shootPanel (panel, "issue369-panel-chord-root"); panel.setTonicFromChord (false); // (the Key menu's "Auto: from the chords and the scale") check ("369 Auto again", a.key.tonic == 0 && ! a.forcedTonic); // Automatic is a guess, not a hint roll.setScaleForSnapshot (0, 0); check ("369 no scale: no toolbar hint", roll.chordHelperScale().second.empty()); panel.refresh(); // (the hint went: one new reading, then none while nothing changes) check ("369 no scale: C Ionian still", a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian && panel.hintSource() == Source::none); const auto runs = panel.computations(); for (int i = 0; i < 10; ++i) panel.refresh(); check ("369 computes only on change", panel.computations() == runs); } // --- 2. where the hint comes from: a key marker, a Playlist key marker, the project key --------------------------- { // F then G (G selected): C major's IV V, or A minor's VI VII — the hint decides AudioEngine engine (false); auto song = makeSong ({ { 53, 57, 60 }, { 55, 59, 62 } }); engine.setSong (std::move (song)); engine.setSelectedChannel (0); engine.dispatchPendingMessages(); const auto selection = lastChord (engine.getSong()); std::optional editorScale; const auto target = [&engine, &selection, &editorScale] { ChordHelperPanel::Target t; t.pattern = 0; t.channel = 0; t.selected = selection; t.editorScale = editorScale; return t; }; ChordHelperPanel panel (engine, target, {}, ChordHelperPanel::Look::fl); panel.refresh(); const auto& a = panel.analysis(); check ("F G alone: C Ionian (IV V)", a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian && roman (a) == "V", keyName (a)); const projectkey::Key aMinor { 9, keyregions::minorScale() }; // the project key (FL's project scale): A minor engine.editSong ([aMinor] (Song& s) { projectkey::set (s, aMinor); }); engine.dispatchPendingMessages(); panel.refresh(); check ("project key A minor: A Aeolian, G = VII", panel.hintSource() == Source::projectKey && a.key.tonic == 9 && a.key.mode == modalhelp::Mode::aeolian && roman (a) == "\xe2\x99\xad" "VII", keyName (a) + " " + roman (a)); // the Piano roll's own scale wins over the project key: C Major editorScale = modalhelp::KeyHint (0, { 0, 2, 4, 5, 7, 9, 11 }); panel.refresh(); check ("Piano roll scale C Major over the project key", panel.hintSource() == Source::pianoRollScale && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian); // a Playlist key marker over the pattern's clip comes after the Piano roll's scale, before the project key engine.editSong ([bar, aMinor] (Song& s) { projectkey::set (s, std::nullopt); s.playlist.clear(); Clip c; c.kind = ClipKind::pattern; c.pattern = 0; c.start = 8 * bar; c.offset = 0; c.length = 4 * bar; s.playlist.push_back (c); keyregions::addKeyMarker (s.markers(), 8 * bar, keyregions::State { keyregions::State::key, aMinor }); }); engine.dispatchPendingMessages(); editorScale.reset(); panel.refresh(); check ("Playlist key marker over the clip: A Aeolian", panel.hintSource() == Source::playlistMarker && a.key.tonic == 9 && a.key.mode == modalhelp::Mode::aeolian, keyName (a)); // a key marker of the pattern itself comes first (and a Black notes one means no hint at all) engine.editSong ([] (Song& s) { keyregions::addKeyMarker (s.patterns.front(), 0, keyregions::State { keyregions::State::key, projectkey::Key { 0, keyregions::majorScale() } }); }); engine.dispatchPendingMessages(); editorScale = modalhelp::KeyHint (9, { 0, 2, 3, 5, 7, 8, 10 }); panel.refresh(); check ("pattern key marker C Major first", panel.hintSource() == Source::patternMarker && a.key.tonic == 0 && a.key.mode == modalhelp::Mode::ionian, keyName (a)); engine.editSong ([] (Song& s) { keyregions::addKeyMarker (s.patterns.front(), 0, keyregions::State { keyregions::State::none, {} }); }); engine.dispatchPendingMessages(); panel.refresh(); check ("Black notes key marker: no hint", panel.hintSource() == Source::none && ! a.hinted); } layoutstyle::setCurrent (layoutBefore); return result; }