I’m trying to draw different key colours on a MidiKeyboardComponent depending on which channel the incoming notes are.
What I have so far (simplified):
A CustomMidiKeyboardComponent, derived from MidiKeybOardComponent with overriden drawBlackNote(), drawWhiteNote() and handleNoteOn() methods.
void CustomMidiKeyboardComponent::handleNoteOn(MidiKeyboardState* state, int midiChannel, int midiNoteNumber, float velocity)
{
curMidiChannel = midiChannel;
MidiKeyboardComponent::handleNoteOn(state, midiChannel, midiNoteNumber, velocity);
}
void CustomMidiKeyboardComponent::drawWhiteNote(int midiNoteNumber, Graphics & g, int x, int y, int w, int h, bool isDown, bool isOver, const Colour & lineColour, const Colour & textColour)
{
Colour c(Colours::transparentWhite);
if (isDown) {
if (curMidiChannel == 1) {
c = c.overlaidWith(chan1Colour);
}
if (curMidiChannel == 2) {
c = c.overlaidWith(chan2Colour);
}
}
// ...
}
void CustomMidiKeyboardComponent::drawBlackNote(int midiNoteNumber, Graphics & g, int x, int y, int w, int h, bool isDown, bool isOver, const Colour & noteFillColour)
{
Colour c(noteFillColour);
if (isDown) {
if (curMidiChannel == 1) {
c = c.overlaidWith(chan1Colour);
}
if (curMidiChannel == 2) {
c = c.overlaidWith(chan2Colour);
}
}
// ...
}
This basically kind of works, but it breaks (= sometimes drawn notes have the wrong colour) if there are more than just a few notes incoming on each channel.
I think I kinda get why this happens (setting the channel and notes drawing can overlap ?) but what would be a better (correct) approach to do this ?