Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing midi note numbers in juce audio application

Tags:

c++

midi

juce

I am trying to print midi note numbers in to a label in a Juce audio application as they are pressed. Here is the code I currently 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 MainComponent.cpp constructer:

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, a label saying "midiText" is visible, and when I press a key on the midi keyboard, the text disappears. Any ideas?

like image 830
willfo Avatar asked Sep 27 '22 06:09

willfo


1 Answers

You're creating a new MidiMessage inside the loop, rather than using the MidiMessage passed into the callback. As a result of this, midiTest is empty, which is then used to set your label (hence why it goes blank).

You need to change your function signature to:

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

Then remove the line:

MidiMessage message;
like image 121
OMGtechy Avatar answered Sep 30 '22 07:09

OMGtechy