5

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?

willfo
  • 241
  • 1
  • 2
  • 12
  • 2
    Not knowing about Juce, [`setText()`](http://learn.juce.com/doc/classLabel.php#a3f0ca22cb63e924d3db23da48c210790) may be a better way to set the text than `getTextValue()`. – MikeCAT Nov 09 '15 at 13:46
  • When you say the text disappears, do you mean all of it or you just see `MIDI Message Received` instead? – OMGtechy Nov 09 '15 at 16:12
  • MIDI Message received only prints to the console, so that can be ignored (but yes that works fine, If that's what you're asking.) The issue here is what's being printed on the application window. It begins with "midiText", then when a key is pressed, the note number etc should be displayed. However all text in the window just disappears. – willfo Nov 09 '15 at 16:36

1 Answers1

4

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;
OMGtechy
  • 7,935
  • 8
  • 48
  • 83