diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp index 700469f11d..3365c45f1d 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp @@ -194,21 +194,19 @@ Range MidiKeyboardComponent::getKeyPosition (int midiNoteNumber, float ta { jassert (midiNoteNumber >= 0 && midiNoteNumber < 128); - static const float blackNoteWidth = 0.7f; - - static const float notePos[] = { 0.0f, 1 - blackNoteWidth * 0.6f, - 1.0f, 2 - blackNoteWidth * 0.4f, + static const float notePos[] = { 0.0f, 1 - blackNoteWidthRatio * 0.6f, + 1.0f, 2 - blackNoteWidthRatio * 0.4f, 2.0f, - 3.0f, 4 - blackNoteWidth * 0.7f, - 4.0f, 5 - blackNoteWidth * 0.5f, - 5.0f, 6 - blackNoteWidth * 0.3f, + 3.0f, 4 - blackNoteWidthRatio * 0.7f, + 4.0f, 5 - blackNoteWidthRatio * 0.5f, + 5.0f, 6 - blackNoteWidthRatio * 0.3f, 6.0f }; auto octave = midiNoteNumber / 12; auto note = midiNoteNumber % 12; auto start = octave * 7.0f * targetKeyWidth + notePos[note] * targetKeyWidth; - auto width = MidiMessage::isMidiNoteBlack (note) ? blackNoteWidth * targetKeyWidth : targetKeyWidth; + auto width = MidiMessage::isMidiNoteBlack (note) ? blackNoteWidthRatio * targetKeyWidth : targetKeyWidth; return { start, start + width }; } @@ -568,6 +566,17 @@ float MidiKeyboardComponent::getBlackNoteLength() const noexcept return whiteNoteLength * blackNoteLengthRatio; } +void MidiKeyboardComponent::setBlackNoteWidthProportion (float ratio) noexcept +{ + jassert (ratio >= 0.0f && ratio <= 1.0f); + + if (blackNoteWidthRatio != ratio) + { + blackNoteWidthRatio = ratio; + resized(); + } +} + void MidiKeyboardComponent::resized() { auto w = getWidth(); diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h index 87865035dd..981da4216e 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h @@ -177,6 +177,17 @@ public: */ float getBlackNoteLength() const noexcept; + /** Sets the width of the black notes as a proportion of the white note width. */ + void setBlackNoteWidthProportion (float ratio) noexcept; + + /** Returns the width of the black notes as a proportion of the white note width. */ + float getBlackNoteWidthProportion() const noexcept { return blackNoteWidthRatio; } + + /** Returns the absolute width of the black notes. + This will be their vertical or horizontal width, depending on the keyboard's orientation. + */ + float getBlackNoteWidth() const noexcept { return keyWidth * blackNoteWidthRatio; } + /** If set to true, then scroll buttons will appear at either end of the keyboard if there are too many notes to fit them all in the component at once. */ @@ -380,6 +391,7 @@ private: MidiKeyboardState& state; float blackNoteLengthRatio = 0.7f; + float blackNoteWidthRatio = 0.7f; float xOffset = 0; float keyWidth = 16.0f; Orientation orientation;