Here is the code I am using, modified from the examples:
#include <Bela.h>
#include <libraries/OscSender/OscSender.h>
#include <libraries/OscReceiver/OscReceiver.h>
#include <libraries/Pipe/Pipe.h>
Pipe oscPipe;
OscReceiver oscReceiver;
int localPort = 7562;
void on_receive(oscpkt::Message* msg, void*)
{
rt_printf("received a message\n");
// we make a copy of the incoming message and we send it down the pipe to the real-time thread
oscpkt::Message* incomingMsg = new oscpkt::Message(msg);
oscPipe.writeNonRt(incomingMsg);
// the real-time thread sends back to us the pointer once it is done with it
oscpkt::Message* returnedMsg;
while(oscPipe.readNonRt(returnedMsg) > 0)
{
delete returnedMsg;
}
}
bool setup(BelaContext *context, void *userData)
{
oscPipe.setup("incomingOsc");
oscReceiver.setup(localPort, on_receive);
// in the remainder of the program, we will be calling readRt() from render(), and we want it
// to return immediately if there are no new messages available. We therefore set the
// pipe to non-blocking mode
oscPipe.setBlockingRt(false);
return true;
}
void render(BelaContext *context, void *userData)
{
oscpkt::Message* msg;
// read incoming messages from the pipe
while(oscPipe.readRt(msg) > 0)
{
if (msg){
int intArg;
float floatArg;
msg->arg().popInt32(intArg).popFloat(floatArg).isOkNoMoreArgs();
rt_printf("received a message with int %i and float %f\n", intArg, floatArg);
}
oscPipe.writeRt(msg); // return the pointer to the other thread, where it will be destroyed
}
}
void cleanup(BelaContext *context, void *userData)
{
oscpkt::Message* returnedMsg;
// drain the pipes, so that any objects trapped in there can be appropriately destroyed
while(oscPipe.readRt(returnedMsg) > 0)
{
delete returnedMsg;
}
while(oscPipe.readNonRt(returnedMsg) > 0)
{
delete returnedMsg;
}
}