Midi keyboard lag (low timer freq)

the midiKeyboardComponent is laggy because the update of the down notes relies on a simple bool+timer mechanism (search for shouldCheckState) and the timer frequency is 20hz only.
As a result we can have up to 50ms between the moment the noteOn is received and the repaint call.

Could we have the frequency increased to 200hz? or perhaps you could make the Timer inheritance protected, so we could call startTimer() with a higher freq in our child classes.

shouldCheckState could be made atomic btw

Are you sure that’s the root of the problem? I’ve actually removed that timer callback in a recent change as mouseDown and mouseDrag call repaint directly.

Thanks Tom. It’s not about notes played with the mouse, but about notes received via midi buffer through the midiKeyboardState.

I still see that in the latest tip :

void MidiKeyboardComponent::handleNoteOn (MidiKeyboardState*, int /*midiChannel*/, int /*midiNoteNumber*/, float /*velocity*/)
    shouldCheckState = true; // (probably being called from the audio thread, so avoid blocking in here)

void MidiKeyboardComponent::timerCallback()
    if (shouldCheckState)
        shouldCheckState = false;

           repaintNote (i);

I think the timer is the root of the problem yes. But just increasing the frequency won’t fix it.
Worst than the lag, if a midi note goes ON and back Off between 2 callbacks of the timer, then the note will never be painted ON.
To reproduce, just send short midi notes to the AudioPluginDemo. you will hear the notes, but the keyboard will not paint them.

To display very short MIDI notes in the keyboard component, I would suggest the following:

Each note should store timetag (eg. a sample counter) of last MIDI note on and off events. Keyboard key will be drawn pressed down if the MIDI note is currently pressed down or if there is less than eg. 50 ms from the last MIDI note off event.