comparison examples/05-Communication/OSC/render.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children 5779ed0562ac
comparison
equal deleted inserted replaced
463:c47709e8b5c9 464:8fcfbfb32aa0
1 /*
2 ____ _____ _ _
3 | __ )| ____| | / \
4 | _ \| _| | | / _ \
5 | |_) | |___| |___ / ___ \
6 |____/|_____|_____/_/ \_\
7
8 The platform for ultra-low latency audio and sensor processing
9
10 http://bela.io
11
12 A project of the Augmented Instruments Laboratory within the
13 Centre for Digital Music at Queen Mary University of London.
14 http://www.eecs.qmul.ac.uk/~andrewm
15
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
19
20 The Bela software is distributed under the GNU Lesser General Public License
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */
23
24
25 #include <Bela.h>
26 #include <OSCServer.h>
27 #include <OSCClient.h>
28
29 OSCServer oscServer;
30 OSCClient oscClient;
31
32 // this example is designed to be run alongside resources/osc/osc.js
33
34 // parse messages recieved by OSC Server
35 // msg is Message class of oscpkt: http://gruntthepeon.free.fr/oscpkt/
36 void parseMessage(oscpkt::Message msg){
37
38 rt_printf("recieved message to: %s\n", msg.addressPattern().c_str());
39
40 int intArg;
41 float floatArg;
42 if (msg.match("/osc-test").popInt32(intArg).popFloat(floatArg).isOkNoMoreArgs()){
43 rt_printf("recieved int %i and float %f\n", intArg, floatArg);
44 }
45
46 }
47
48 bool setup(BelaContext *context, void *userData)
49 {
50 // setup the OSC server to recieve on port 7562
51 oscServer.setup(7562);
52 // setup the OSC client to send on port 7563
53 oscClient.setup(7563);
54
55 // the following code sends an OSC message to address /osc-setup
56 // then waits 1 second for a reply on /osc-setup-reply
57 bool handshakeRecieved = false;
58 oscClient.sendMessageNow(oscClient.newMessage.to("/osc-setup").end());
59 oscServer.recieveMessageNow(1000);
60 while (oscServer.messageWaiting()){
61 if (oscServer.popMessage().match("/osc-setup-reply")){
62 handshakeRecieved = true;
63 }
64 }
65
66 if (handshakeRecieved){
67 rt_printf("handshake recieved!\n");
68 } else {
69 rt_printf("timeout!\n");
70 }
71
72 return true;
73 }
74
75 void render(BelaContext *context, void *userData)
76 {
77 // recieve OSC messages, parse them, and send back an acknowledgment
78 while (oscServer.messageWaiting()){
79 parseMessage(oscServer.popMessage());
80 oscClient.queueMessage(oscClient.newMessage.to("/osc-acknowledge").add(5).add(4.2f).add(std::string("OSC message recieved")).end());
81 }
82 }
83
84 void cleanup(BelaContext *context, void *userData)
85 {
86
87 }
88
89 /* ------------ Project Explantation ------------ */
90
91 /**
92 \example 05-OSC
93
94 Open Sound Control
95 ------------------
96
97 This example shows an implementation of OSC (Open Sound Control) which was
98 developed at UC Berkeley Center for New Music and Audio Technology (CNMAT).
99
100 It is designed to be run alongside resources/osc/osc.js
101
102 The OSC server port on which to receive is set in `setup()`
103 via `oscServer.setup()`. Likewise the OSC client port on which to
104 send is set in `oscClient.setup()`.
105
106 In `setup()` an OSC message to address `/osc-setup`, it then waits
107 1 second for a reply on `/osc-setup-reply`.
108
109 in `render()` the code receives OSC messages, parses them, and sends
110 back an acknowledgment.
111 */