Printing MIDI Notes in an Audio Application

I am trying to print MIDI notes in a label in a Juce sound application when pressed. Here is the code I have:

in the MainComponent header file:

class MainComponent   : public Component,
                        public MidiInputCallback

{
public:
    //==============================================================================
    MainComponent();
    ~MainComponent();

    void resized() override;
    void handleIncomingMidiMessage (MidiInput*, const MidiMessage&);


private:
    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
    AudioDeviceManager audioDeviceManager;
    Label midiLabel;
};

In the constructor of MainComponent.cpp:

MainComponent::MainComponent()
{
    setSize (500, 400);

    audioDeviceManager.setMidiInputEnabled("USB Axiom 49 Port 1", true);
    audioDeviceManager.addMidiInputCallback (String::empty, this);

    //midiLabel
    midiLabel.setText("midiText", sendNotification);
    addAndMakeVisible(midiLabel);
}

and finally in the handleIncomingMidiMessage function:

void MainComponent::handleIncomingMidiMessage(MidiInput*, const MidiMessage&)
{
    DBG("MIDI Message Recieved\n");


    //display label text
    String midiText;
    MidiMessage message;
    if (message.isNoteOnOrOff()) {
        midiText << "NoteOn: Channel " << message.getChannel();
        midiText << ":Number" << message.getNoteNumber();
        midiText << ":Velocity" << message.getVelocity();
    }
    midiLabel.getTextValue() = midiText;

}

When I run this, β€œmidiText” is displayed, and when I press a key on the midi keyboard, the text disappears. Any ideas?

+4
source share
1 answer

You create a new one MidiMessageinside the loop, instead of using the MidiMessagepassed to the callback. The result is midiTestempty, which is then used to set your label (hence why it is empty).

:

void MainComponent::handleIncomingMidiMessage(MidiInput*, const MidiMessage& message)

:

MidiMessage message;
+3

Source: https://habr.com/ru/post/1615050/


All Articles