main/osc: first implementation of user custom module support

This commit is contained in:
jean-emmanuel 2016-11-01 18:38:11 +01:00
parent 589a5ed648
commit 5cdbf53f6a
4 changed files with 109 additions and 16 deletions

View file

@ -5,7 +5,9 @@ var osc = require('osc'),
ipc = require('./server').ipc,
settings = require('./settings'),
oscInPort = settings.read('oscInPort'),
debug = settings.read('debug')
debug = settings.read('debug'),
vm = require('vm'),
fs = require('fs')
var oscServer = new osc.UDPPort({
@ -13,19 +15,37 @@ var oscServer = new osc.UDPPort({
localPort: oscInPort
});
var customModule = (function(){
var file = (function(){try {return fs.readFileSync(settings.read('customModule'),'utf8')} catch(err) {console.log('File not found: ' + settings.read('customModule'));return false}})(),
mod,
context = {
console:console,
sendOsc:sendOsc
}
try {
mod = vm.runInContext(file, vm.createContext(context))
} catch(err) {
console.log(err)
}
return mod
})()
oscServer.on('message', function (msg, bundle, info) {
var data = {path: msg.address, args: msg.args}
if (data.args.length==1) data.args = data.args[0]
ipc.send('receiveOsc',data)
if (debug) console.log('OSC received: ',data, 'From : ' + info.address + ':' + info.port)
})
var oscInFilter = function(data){
if (customModule.oscInFilter) {
return customModule.oscInFilter(data)
} else {
return data
}
}
var oscOutFilter = function(data){
if (customModule.oscOutFilter) {
return customModule.oscOutFilter(data)
} else {
return data
}
}
oscServer.on('error', function (error) {
console.log(error)
})
oscServer.open()
var parseType = function(type){
@ -53,6 +73,44 @@ var parseArg = function(arg,precision){
}
}
var sendOsc = function(data){
if (!data) return
oscServer.send({
address: data.address,
args: data.args
}, data.host, data.port);
if (debug) console.log('OSC sent: ',{path:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port)
}
var receiveOsc = function(data, info){
if (!data) return
if (data.args.length==1) data.args = data.args[0]
ipc.send('receiveOsc',data)
if (debug) console.log('OSC received: ', {path:data.address, args: data.args}, 'From : ' + data.address + ':' + data.port)
}
oscServer.on('message', function (msg, bundle, info) {
var data = oscInFilter({path: msg.address, args: msg.args, host: info.address, port: info.port})
receiveOsc(data)
})
oscServer.on('error', function (error) {
console.log(error)
})
oscServer.open()
module.exports = {
send: function(host,port,path,args,precision) {
@ -69,12 +127,9 @@ module.exports = {
if (arg!=null) message.push(arg)
}
oscServer.send({
address: path,
args: message
}, host, port);
var data = oscOutFilter({address:path, args: message, host: host, port: port})
if (debug) console.log('OSC sent: ',{path:path,args:message}, 'To : ' + host + ':' + port)
sendOsc(data)
}
}

View file

@ -11,6 +11,7 @@ var argv = require('yargs')
.options({
's':{alias:'sync',type:'array',describe:'synchronized hosts (ip:port pairs)'},
'l':{alias:'load',type:'string',describe:'session file to load'},
'c':{alias:'custom-module',type:'string',describe:'custom module file to load'},
'p':{alias:'port',describe:'http port of the server (default to 8080)'},
'o':{alias:'osc-port',describe:'osc input port'},
'd':{alias:'debug',describe:'log received osc messages in the console'},
@ -38,6 +39,7 @@ var config = function(){try {return JSON.parse(fs.readFileSync(configFile,'utf-8
httpPort: argv.p || 8080,
debug: argv.d || false,
sessionFile: argv.l || false,
customModule: argv.c || false,
noGui: argv.n || false,
guiOnly: argv.g || false,
appAddresses:function(){

View file

@ -0,0 +1,35 @@
# Custom module
Using the `-c / --custom-module` command-line switch, users can load a javascript file to tune the way Open Stage Control behaves regarding osc.
It must be of the following form:
```js
(function(){
// Do whatever you want, initialize some variables, declare some functions, ...
return {
oscInFilter:function(data){
var {address, args, host, port} = data
// do what you want
// return data if you want the message to be processed (and sent)
return data
},
oscOutFilter:function(data){
// same as oscInFilter
}
}
})()
```
The module is executed in a restricted context, only a few globals are available :
- `console` object
- `sendOsc` function
- `receiveOsc` function

View file

@ -10,6 +10,7 @@ Prebuilt binaries for Linux, Windows and OS X can be found on the [release](http
Options:
-s, --sync synchronized hosts (ip:port pairs)
-l, --load session file to load
-c, --custom-module custom module file to load
-p, --port http port of the server (default to 8080)
-o, --osc-port osc input port
-d, --debug log received osc messages in the console