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