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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With