Why in this tutorial, the source must be”On-Screen Keyboard” in the handleNoteOn override function? Why not like handleIncomingMidiMessage function?


void handleNoteOn (juce::MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override
//Called when one of the MidiKeyboardState's keys is pressed.
    if (! isAddingFromMidiInput)
        auto m = juce::MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity);
        m.setTimeStamp (juce::Time::getMillisecondCounterHiRes() * 0.001);
        postMessageToList (m, "On-Screen Keyboard");

void postMessageToList (const juce::MidiMessage& message, const juce::String& source)
        (new IncomingMessageCallback (this, message, source))->post();

void handleIncomingMidiMessage (juce::MidiInput* source, const juce::MidiMessage& message) override //MidiInputCallback::handleIncomingMidiMessage()
        const juce::ScopedValueSetter<bool> scopedInputFlag (isAddingFromMidiInput, true);
        keyboardState.processNextMidiEvent (message);
//Looks at a key-up/down event and uses it to update the state of this object.
        postMessageToList (message, source->getName());

The handleNoteOn is called from MidiKeyboardState. handleIncomingMidiMessage would be called from the MidiInput.

  • in a plugin you should not talk to the MidiInput directly, because they are managed from the host.
  • the host needs to add the timestamps according to the timeline. MidiInput is asynchronous to the audio playback.
  • The preferred way is to send the MidiBuffer from the processBlock() call to the MidiKeyboardState, which takes also care of thread safety between GUI thread and the audio thread.

While this tutorial is not a plugin, the MidiKeyboardComponent relies on the MidiKeyboardState, so it makes sense to use its interface for the handleNoteOn and handleNoteOff.

Why should not talk to the MidiInput directly because they are managed from the host ?

If a note is played by a midi keyboard, it will send 2 lines to log?(one is velocity, source, and the other one is the played note of on-screen keyboard)

a) because the host wants to record the note and b) because the user selects in the host to which instrument he/she wants to send the midi signal.

Why can’t the host record the note using handleIncomingMidiMessage()?

The host will use that method internally. But then first thing is to add a timestamp and route it to where the user wants it fed to.

I guess your questions are aiming for something? What do you want to achieve?

I just want to know why not let handleIncomingMidiMessage do handleNoteOn’s work? Why must do work with on-screen keyboard?