l@271: var dgram = require('dgram'); l@271: var osc = require('osc-min'); l@271: l@271: // port numbers l@271: var OSC_RECIEVE = 7563; l@271: var OSC_SEND = 7562; l@271: l@271: // socket to send and receive OSC messages from bela l@271: var socket = dgram.createSocket('udp4'); l@271: socket.bind(OSC_RECIEVE, '127.0.0.1'); l@271: l@271: socket.on('message', (message, info) => { l@271: l@271: var msg = osc.fromBuffer(message); l@271: l@271: if (msg.oscType === 'message'){ l@271: parseMessage(msg); l@271: } else if (msg.elements){ l@271: for (let element of msg.elements){ l@271: parseMessage(element); l@271: } l@271: } l@271: l@271: }); l@271: l@271: function parseMessage(msg){ l@271: l@271: var address = msg.address.split('/'); l@271: if (!address || !address.length || address.length <2){ l@271: console.log('bad OSC address', address); l@271: return; l@271: } l@271: l@271: // setup handshake l@271: if (address[1] === 'osc-setup'){ l@271: sendHandshakeReply(); l@271: l@271: // start sending OSC messages to Bela l@271: setInterval(sendOscTest, 1000); l@271: l@271: } else if (address[1] === 'osc-acknowledge'){ l@271: console.log('received osc-acknowledge', msg.args); l@271: } l@271: } l@271: l@271: function sendOscTest(){ l@271: var buffer = osc.toBuffer({ l@271: address : '/osc-test', l@271: args : [ l@271: {type: 'integer', value: 78}, l@271: {type: 'float', value: 3.14} l@271: ] l@271: }); l@271: socket.send(buffer, 0, buffer.length, OSC_SEND, '127.0.0.1', function(err) { l@271: if (err) console.log(err); l@271: }); l@271: } l@271: l@271: function sendHandshakeReply(){ l@271: var buffer = osc.toBuffer({ address : '/osc-setup-reply' }); l@271: socket.send(buffer, 0, buffer.length, OSC_SEND, '127.0.0.1', function(err) { l@271: if (err) console.log(err); l@271: }); l@271: }