robert@372: /* robert@372: ____ _____ _ _ robert@372: | __ )| ____| | / \ robert@372: | _ \| _| | | / _ \ robert@372: | |_) | |___| |___ / ___ \ robert@372: |____/|_____|_____/_/ \_\.io robert@372: robert@372: */ robert@372: robert@372: /** robert@372: \example 5_osc robert@372: robert@372: Open Sound Control robert@372: ------------------ robert@372: robert@372: This example shows an implementation of OSC (Open Sound Control) which was robert@372: developed at UC Berkeley Center for New Music and Audio Technology (CNMAT). robert@372: robert@372: It is designed to be run alongside resources/osc/osc.js robert@372: robert@372: The OSC server port on which to receive is set in `setup()` robert@372: via `oscServer.setup()`. Likewise the OSC client port on which to robert@372: send is set in `oscClient.setup()`. robert@372: robert@372: In `setup()` an OSC message to address `/osc-setup`, it then waits robert@372: 1 second for a reply on `/osc-setup-reply`. robert@372: robert@372: in `render()` the code receives OSC messages, parses them, and sends robert@372: back an acknowledgment. robert@372: */ robert@372: giuliomoro@301: #include l@271: #include l@271: #include l@271: l@271: OSCServer oscServer; l@271: OSCClient oscClient; l@271: l@271: // this example is designed to be run alongside resources/osc/osc.js l@271: l@271: // parse messages recieved by OSC Server l@271: // msg is Message class of oscpkt: http://gruntthepeon.free.fr/oscpkt/ l@271: void parseMessage(oscpkt::Message msg){ l@271: l@271: rt_printf("recieved message to: %s\n", msg.addressPattern().c_str()); l@271: l@271: int intArg; l@271: float floatArg; l@271: if (msg.match("/osc-test").popInt32(intArg).popFloat(floatArg).isOkNoMoreArgs()){ l@271: rt_printf("recieved int %i and float %f\n", intArg, floatArg); l@271: } l@271: l@271: } l@271: giuliomoro@301: bool setup(BelaContext *context, void *userData) l@271: { l@271: // setup the OSC server to recieve on port 7562 l@271: oscServer.setup(7562); l@271: // setup the OSC client to send on port 7563 l@271: oscClient.setup(7563); l@271: l@271: // the following code sends an OSC message to address /osc-setup l@271: // then waits 1 second for a reply on /osc-setup-reply l@271: bool handshakeRecieved = false; l@271: oscClient.sendMessageNow(oscClient.newMessage.to("/osc-setup").end()); l@271: oscServer.recieveMessageNow(1000); l@271: while (oscServer.messageWaiting()){ l@271: if (oscServer.popMessage().match("/osc-setup-reply")){ l@271: handshakeRecieved = true; l@271: } l@271: } l@271: l@271: if (handshakeRecieved){ l@271: rt_printf("handshake recieved!\n"); l@271: } else { l@271: rt_printf("timeout!\n"); l@271: } l@271: l@271: return true; l@271: } l@271: giuliomoro@301: void render(BelaContext *context, void *userData) l@271: { l@271: // recieve OSC messages, parse them, and send back an acknowledgment l@271: while (oscServer.messageWaiting()){ l@271: parseMessage(oscServer.popMessage()); l@271: oscClient.queueMessage(oscClient.newMessage.to("/osc-acknowledge").add(5).add(4.2f).add(std::string("OSC message recieved")).end()); l@271: } l@271: } l@271: giuliomoro@301: void cleanup(BelaContext *context, void *userData) l@271: { l@271: l@271: }