I'm trying to use MIDI with Pyo, but I'm getting tons of drop outs printed in Bela's terminal. Here's the render.cpp file:
#include <Bela.h>
#include <libraries/Midi/Midi.h>
#include <cmath>
#include <iostream>
#include <Utilities.h>
#include <string>
#include "PyoClass.h"
Pyo pyo;
bool gotMidi = false;
int statusByte;
int dataByte1, dataByte2;
void Bela_userSettings(BelaInitSettings *settings) {
settings->periodSize = 32;
settings->uniformSampleRate = 1;
settings->numAnalogInChannels = 8;
//settings->numAnalogOutChannels = 8;
//settings->analogOutputsPersist = 0;
}
/*
* This callback is called every time a new input Midi message is available
*
* Note that this is called in a different thread than the audio processing one.
*
*/
void midiMessageCallback(MidiChannelMessage message, void* arg){
//if ((message.getType() >= 128 && message.getType() <= 159) || (message.getType() >= 176 && message.getType() <= 239)) {
//}
message.prettyPrint();
statusByte = message.getType();
dataByte1 = message.getDataByte(0);
dataByte2 = message.getDataByte(1);
gotMidi = true;
}
Midi midi;
const char* gMidiPort = "hw:0,0,0";
bool setup(BelaContext *context, void *userData) {
// Initialize a pyo server.
pyo.setup(context->audioInChannels, context->audioOutChannels, context->audioFrames,
context->audioSampleRate, context->analogInChannels);
// initialize MIDI
// copied from the C++ MIDI example that comes with the Bela
int ret = midi.readFrom(gMidiPort);
if(ret <= 0)
{
fprintf(stderr, "Error %d when reading from device %s\n", ret, gMidiPort);
return false;
}
ret = midi.writeTo(gMidiPort);
if(ret <= 0)
{
fprintf(stderr, "Error %d when writing to device %s\n", ret, gMidiPort);
return false;
}
midi.enableParser(true);
midi.getParser()->setCallback(midiMessageCallback, (void*)gMidiPort);
// Load a python file.
char filename[] = "main.py";
ret = pyo.loadfile(filename, 0);
if (ret != 0) {
if (ret == 1) {
printf("Error: file \"%s\" not found", filename);
}
else {
std::string s = pyo.getErrorMsg();
std::cout << s << std::endl;
}
return false;
}
return true;
}
void render(BelaContext *context, void *userData) {
// First check if we have received a MIDI message
if (gotMidi) {
pyo.midievent(statusByte, dataByte1, dataByte2);
gotMidi = false;
}
// Fill pyo input buffer (channels 0-1) with audio samples.
pyo.fillin(context->audioIn);
// Fill pyo input buffer (channels 2+) with analog inputs.
pyo.analogin(context->analogIn);
// Call pyo processing function and retrieve back stereo outputs.
pyo.process(context->audioOut);
// Get back pyo output channels 2+ as analog outputs.
//if (context->analogOut != NULL) {
// pyo.analogout(context->analogOut);
//}
}
void cleanup(BelaContext *context, void *userData) {}
Can anyone spot something that seems to be wrong? Or is it just how Pyo handles MIDI when embedded in other platforms?