robert@372
|
1 /*
|
robert@372
|
2 ____ _____ _ _
|
robert@372
|
3 | __ )| ____| | / \
|
robert@372
|
4 | _ \| _| | | / _ \
|
robert@372
|
5 | |_) | |___| |___ / ___ \
|
robert@372
|
6 |____/|_____|_____/_/ \_\.io
|
robert@372
|
7
|
robert@372
|
8 */
|
robert@372
|
9
|
robert@372
|
10 /**
|
robert@372
|
11 \example 5_osc
|
robert@372
|
12
|
robert@372
|
13 Open Sound Control
|
robert@372
|
14 ------------------
|
robert@372
|
15
|
robert@372
|
16 This example shows an implementation of OSC (Open Sound Control) which was
|
robert@372
|
17 developed at UC Berkeley Center for New Music and Audio Technology (CNMAT).
|
robert@372
|
18
|
robert@372
|
19 It is designed to be run alongside resources/osc/osc.js
|
robert@372
|
20
|
robert@372
|
21 The OSC server port on which to receive is set in `setup()`
|
robert@372
|
22 via `oscServer.setup()`. Likewise the OSC client port on which to
|
robert@372
|
23 send is set in `oscClient.setup()`.
|
robert@372
|
24
|
robert@372
|
25 In `setup()` an OSC message to address `/osc-setup`, it then waits
|
robert@372
|
26 1 second for a reply on `/osc-setup-reply`.
|
robert@372
|
27
|
robert@372
|
28 in `render()` the code receives OSC messages, parses them, and sends
|
robert@372
|
29 back an acknowledgment.
|
robert@372
|
30 */
|
robert@372
|
31
|
giuliomoro@301
|
32 #include <Bela.h>
|
l@271
|
33 #include <OSCServer.h>
|
l@271
|
34 #include <OSCClient.h>
|
l@271
|
35
|
l@271
|
36 OSCServer oscServer;
|
l@271
|
37 OSCClient oscClient;
|
l@271
|
38
|
l@271
|
39 // this example is designed to be run alongside resources/osc/osc.js
|
l@271
|
40
|
l@271
|
41 // parse messages recieved by OSC Server
|
l@271
|
42 // msg is Message class of oscpkt: http://gruntthepeon.free.fr/oscpkt/
|
l@271
|
43 void parseMessage(oscpkt::Message msg){
|
l@271
|
44
|
l@271
|
45 rt_printf("recieved message to: %s\n", msg.addressPattern().c_str());
|
l@271
|
46
|
l@271
|
47 int intArg;
|
l@271
|
48 float floatArg;
|
l@271
|
49 if (msg.match("/osc-test").popInt32(intArg).popFloat(floatArg).isOkNoMoreArgs()){
|
l@271
|
50 rt_printf("recieved int %i and float %f\n", intArg, floatArg);
|
l@271
|
51 }
|
l@271
|
52
|
l@271
|
53 }
|
l@271
|
54
|
giuliomoro@301
|
55 bool setup(BelaContext *context, void *userData)
|
l@271
|
56 {
|
l@271
|
57 // setup the OSC server to recieve on port 7562
|
l@271
|
58 oscServer.setup(7562);
|
l@271
|
59 // setup the OSC client to send on port 7563
|
l@271
|
60 oscClient.setup(7563);
|
l@271
|
61
|
l@271
|
62 // the following code sends an OSC message to address /osc-setup
|
l@271
|
63 // then waits 1 second for a reply on /osc-setup-reply
|
l@271
|
64 bool handshakeRecieved = false;
|
l@271
|
65 oscClient.sendMessageNow(oscClient.newMessage.to("/osc-setup").end());
|
l@271
|
66 oscServer.recieveMessageNow(1000);
|
l@271
|
67 while (oscServer.messageWaiting()){
|
l@271
|
68 if (oscServer.popMessage().match("/osc-setup-reply")){
|
l@271
|
69 handshakeRecieved = true;
|
l@271
|
70 }
|
l@271
|
71 }
|
l@271
|
72
|
l@271
|
73 if (handshakeRecieved){
|
l@271
|
74 rt_printf("handshake recieved!\n");
|
l@271
|
75 } else {
|
l@271
|
76 rt_printf("timeout!\n");
|
l@271
|
77 }
|
l@271
|
78
|
l@271
|
79 return true;
|
l@271
|
80 }
|
l@271
|
81
|
giuliomoro@301
|
82 void render(BelaContext *context, void *userData)
|
l@271
|
83 {
|
l@271
|
84 // recieve OSC messages, parse them, and send back an acknowledgment
|
l@271
|
85 while (oscServer.messageWaiting()){
|
l@271
|
86 parseMessage(oscServer.popMessage());
|
l@271
|
87 oscClient.queueMessage(oscClient.newMessage.to("/osc-acknowledge").add(5).add(4.2f).add(std::string("OSC message recieved")).end());
|
l@271
|
88 }
|
l@271
|
89 }
|
l@271
|
90
|
giuliomoro@301
|
91 void cleanup(BelaContext *context, void *userData)
|
l@271
|
92 {
|
l@271
|
93
|
l@271
|
94 }
|