|
| 1 | +/* |
| 2 | + ____ _____ _ _ |
| 3 | +| __ )| ____| | / \ |
| 4 | +| _ \| _| | | / _ \ |
| 5 | +| |_) | |___| |___ / ___ \ |
| 6 | +|____/|_____|_____/_/ \_\ |
| 7 | +http://bela.io |
| 8 | +*/ |
| 9 | +/** |
| 10 | +\example Communication/OSC/render.cpp |
| 11 | +
|
| 12 | +Open Sound Control |
| 13 | +------------------ |
| 14 | +
|
| 15 | +This example shows an implementation of OSC (Open Sound Control) which was |
| 16 | +developed at UC Berkeley Center for New Music and Audio Technology (CNMAT). |
| 17 | +
|
| 18 | +It is designed to be run alongside resources/osc/osc.js. |
| 19 | +For the example to work, run in a terminal on the board |
| 20 | +``` |
| 21 | +node /root/Bela/resources/osc/osc.js |
| 22 | +``` |
| 23 | +
|
| 24 | +In `setup()` an OSC message to address `/osc-setup`, it then waits |
| 25 | +1 second for a reply on `/osc-setup-reply`. |
| 26 | +
|
| 27 | +After that, OSC communication takes place in the on_receive() callback, |
| 28 | +which is called every time a new message comes in. |
| 29 | +*/ |
| 30 | + |
| 31 | +#include <Bela.h> |
| 32 | +#include <libraries/OscSender/OscSender.h> |
| 33 | +#include <libraries/OscReceiver/OscReceiver.h> |
| 34 | + |
| 35 | +OscReceiver oscReceiver; |
| 36 | +OscSender oscSender; |
| 37 | +int localPort = 8888; |
| 38 | +int remotePort = 9999; |
| 39 | +const char* remoteIp = "127.0.0.1"; |
| 40 | + |
| 41 | +// parse messages received by the OSC receiver |
| 42 | +// msg is Message class of oscpkt: http://gruntthepeon.free.fr/oscpkt/ |
| 43 | +bool handshakeReceived; |
| 44 | +void on_receive(oscpkt::Message* msg, void* arg) |
| 45 | +{ |
| 46 | + if(msg->match("/bela/osc-setup-reply")) |
| 47 | + handshakeReceived = true; |
| 48 | + else if(msg->match("/bela/osc-test")){ |
| 49 | + int intArg; |
| 50 | + float floatArg; |
| 51 | + msg->match("/bela/osc-test").popInt32(intArg).popFloat(floatArg).isOkNoMoreArgs(); |
| 52 | + printf("received a message with int %i and float %f\n", intArg, floatArg); |
| 53 | + oscSender.newMessage("/bela/osc-acknowledge").add(intArg).add(4.2f).add(std::string("OSC message received")).send(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +bool setup(BelaContext *context, void *userData) |
| 58 | +{ |
| 59 | + oscReceiver.setup(localPort, on_receive); |
| 60 | + oscSender.setup(remotePort, remoteIp); |
| 61 | + |
| 62 | + // the following code sends an OSC message to address /osc-setup |
| 63 | + // then waits 1 second for a reply on /osc-setup-reply |
| 64 | + oscSender.newMessage("/bela/osc-setup").send(); |
| 65 | + int count = 0; |
| 66 | + int timeoutCount = 10; |
| 67 | + printf("Waiting for handshake ....\n"); |
| 68 | + while(!handshakeReceived && ++count != timeoutCount) |
| 69 | + { |
| 70 | + usleep(100000); |
| 71 | + } |
| 72 | + if (handshakeReceived) { |
| 73 | + printf("handshake received!\n"); |
| 74 | + } else { |
| 75 | + printf("timeout! : did you start the iipyper server? `python server.py`\n"); |
| 76 | + return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +void render(BelaContext *context, void *userData) |
| 82 | +{ |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +void cleanup(BelaContext *context, void *userData) |
| 87 | +{ |
| 88 | + |
| 89 | +} |
0 commit comments