comparison resources/osc/osc.js @ 271:fb9c28a4676b prerelease

Added osc example project and node script for testing
author Liam Donovan <l.b.donovan@qmul.ac.uk>
date Tue, 17 May 2016 16:01:06 +0100
parents
children
comparison
equal deleted inserted replaced
270:de37582ce6f3 271:fb9c28a4676b
1 var dgram = require('dgram');
2 var osc = require('osc-min');
3
4 // port numbers
5 var OSC_RECIEVE = 7563;
6 var OSC_SEND = 7562;
7
8 // socket to send and receive OSC messages from bela
9 var socket = dgram.createSocket('udp4');
10 socket.bind(OSC_RECIEVE, '127.0.0.1');
11
12 socket.on('message', (message, info) => {
13
14 var msg = osc.fromBuffer(message);
15
16 if (msg.oscType === 'message'){
17 parseMessage(msg);
18 } else if (msg.elements){
19 for (let element of msg.elements){
20 parseMessage(element);
21 }
22 }
23
24 });
25
26 function parseMessage(msg){
27
28 var address = msg.address.split('/');
29 if (!address || !address.length || address.length <2){
30 console.log('bad OSC address', address);
31 return;
32 }
33
34 // setup handshake
35 if (address[1] === 'osc-setup'){
36 sendHandshakeReply();
37
38 // start sending OSC messages to Bela
39 setInterval(sendOscTest, 1000);
40
41 } else if (address[1] === 'osc-acknowledge'){
42 console.log('received osc-acknowledge', msg.args);
43 }
44 }
45
46 function sendOscTest(){
47 var buffer = osc.toBuffer({
48 address : '/osc-test',
49 args : [
50 {type: 'integer', value: 78},
51 {type: 'float', value: 3.14}
52 ]
53 });
54 socket.send(buffer, 0, buffer.length, OSC_SEND, '127.0.0.1', function(err) {
55 if (err) console.log(err);
56 });
57 }
58
59 function sendHandshakeReply(){
60 var buffer = osc.toBuffer({ address : '/osc-setup-reply' });
61 socket.send(buffer, 0, buffer.length, OSC_SEND, '127.0.0.1', function(err) {
62 if (err) console.log(err);
63 });
64 }