this example should have most of the code you need, although admittedly it is a bit confusing, as it tried to illustrate three different approaches. See where it says alternatively, you can use the built-in parser (only processes channel messages at the moment)
.
Ultimately, you will need something like this:
#include <Bela.h>
#include <Midi.h>
Midi midi;
char gMidiPort0[] = "hw:0,0,0";
bool setup(BelaContext *context, void *userData)
{
midi.readFrom(gMidiPort0);
midi.writeTo(gMidiPort0);
midi.enableParser(true);
// note that we do no set a parserCallback
return true;
}
void render(BelaContext *context, void *userData)
{
while(midi.getParser()->numAvailableMessages() > 0)
{
MidiChannelMessage message;
message = midi.getParser()->getNextChannelMessage();
message.prettyPrint();
int channel = message.getChannel();
if(message.getType() == kmmNoteOff){
int note = message.getDataByte(0);
int velocity = message.getDataByte(1);
rt_printf("noteOff: channel: %d, note %d, velocity: %d\n", channel, note, velocity);
/// do something useful with the message
}
if(message.getType() == kmmNoteOn){
int note = message.getDataByte(0);
int velocity = message.getDataByte(1);
int noteOn = velocity > 0;
if(noteOn)
rt_printf("noteOn: channel: %d, note %d, velocity: %d\n", channel, note, velocity);
else
rt_printf("noteOff: channel: %d, note %d, velocity: %d\n", channel, note, velocity);
/// do something useful with the message
}
if(message.getType() == kmmControlChange){
int controller = message.getDataByte(0);
int value = message.getDataByte(1);
rt_printf("control change: channel: %d, controller %d, value: %d\n", channel, controller, value);
/// do something useful with the message
}
}
}
void cleanup(BelaContext *context, void *userData)
{}
does it make sense? You can see the other message types available here.