wavey-wind/server.js

274 lines
6.0 KiB
JavaScript
Raw Normal View History

// Which port do I send OSC messages to? (SupperCollider, ...)
const OSCPORT = 57120;
// Which port do I listen to (for visuals, calibration)
const PORT = 6676
// Do we have a problem, shall we debug?
const DEBUG = true
const express = require('express')
const http = require('http')
const WebSocket = require('ws')
2021-08-11 14:39:34 +02:00
const osc = require('osc')
2022-03-03 11:11:38 +01:00
const readline = require('readline')
const fs = require('fs')
2021-08-11 14:39:34 +02:00
// Vzemi iz argumenta
const tty = process.argv[2]
2022-03-05 13:16:43 +01:00
let eulerRotation = [0, 0, 0]
let baudrate = parseInt(process.argv[3])
if (!baudrate) {
baudrate = 115200
}
const include_files = [
2021-08-14 15:15:16 +02:00
'/anim.js',
'/control.js',
'/osctl.js',
'/test.js',
'/node_modules/three/build/three.min.js',
'/node_modules/nouislider/distribute/nouislider.min.js',
'/node_modules/nouislider/distribute/nouislider.min.css',
'/node_modules/osc/dist/osc-browser.js'
];
const app = express();
const server = http.Server(app);
2021-08-11 14:39:34 +02:00
// Odprti serijski OSC link
2021-08-14 15:15:16 +02:00
let scon = null
2022-03-04 20:57:24 +01:00
function openSerial() {
console.log('opening ', tty, baudrate)
2022-03-03 21:51:02 +01:00
2021-08-14 15:15:16 +02:00
scon = new osc.SerialPort({
devicePath: tty,
2022-03-04 20:57:24 +01:00
bitrate: baudrate,
2022-03-03 21:51:02 +01:00
autoOpen: true,
useSLIP: true
2021-08-14 15:15:16 +02:00
})
2021-08-14 15:15:16 +02:00
scon.on('open', e => {
console.log('serial connection opened')
//console.log(scon)
2021-08-14 15:15:16 +02:00
})
scon.on('error', e => {
console.error('tty error', e)
//scon.close()
2021-08-14 15:15:16 +02:00
})
scon.on('close', e => {
console.warn('serial connection closed, restarting in 1 second')
2022-04-23 15:33:54 +02:00
setTimeout(openSerial, 1000)
2021-08-14 15:15:16 +02:00
})
// Arduino OSC gre v web
2022-03-03 21:51:02 +01:00
scon.on('message', msg => {
2021-08-14 15:15:16 +02:00
// Debug incoming osc
2022-03-04 20:57:24 +01:00
if (DEBUG) {
console.log('osc msg', msg)
2022-01-01 22:10:59 +01:00
}
2022-03-04 20:57:24 +01:00
2021-08-14 15:15:16 +02:00
sendAll(msg, null, null, osclients)
})
2022-04-23 15:33:54 +02:00
scon.open()
2021-08-14 15:15:16 +02:00
if (scon._closeCode) {
scon = null
console.log('restarting serial connection')
2021-08-14 15:15:16 +02:00
setTimeout(openSerial, 1000)
}
}
if (tty) {
2022-03-04 20:57:24 +01:00
openSerial(baudrate)
2022-03-03 21:51:02 +01:00
}
2021-08-11 14:39:34 +02:00
app.get('/', (req, res) => {
2017-10-06 23:20:52 +02:00
res.sendFile(__dirname + '/index.html');
});
2021-08-11 14:39:34 +02:00
app.get('/ctl', (req, res) => {
2017-10-06 23:20:52 +02:00
res.sendFile(__dirname + '/control.html');
});
2021-08-12 18:10:19 +02:00
app.get('/test', (req, res) => {
res.sendFile(__dirname + '/test.html');
});
// Hydra inclusion
app.get('/hydra', function(req, res) {
res.sendFile(__dirname + '/hydra-osc-main/index.html');
});
app.get('/lib/osc.min.js', function(req, res) {
res.sendFile(__dirname + '/hydra-osc-main/lib/osc.min.js');
});
let settings = {};
app.get('/settings', function(req, res) {
res.send(settings);
2017-10-06 23:20:52 +02:00
});
include_files.map(function(file) {
app.get(file, function(req, res){
res.sendFile(__dirname + file);
});
2017-10-06 23:20:52 +02:00
});
server.listen(PORT, () => console.log('listening on *:' + PORT))
2017-10-06 23:20:52 +02:00
// Websocket init
const wss = new WebSocket.Server({ server })
2017-10-06 23:20:52 +02:00
2022-03-04 20:57:24 +01:00
// Relay multicast to websockets
// @TODO still sends to supercollider? Do we need two sockets?
/*
2022-03-04 20:57:24 +01:00
var dgram = require('dgram');
var sss = dgram.createSocket('udp4');
sss.on('listening', () => {
sss.addMembership('224.0.1.9');
})
sss.bind(6696, '224.0.1.9');
*/
2022-03-04 20:57:24 +01:00
const scudp = new osc.UDPPort({
remotePort: OSCPORT
2022-03-05 15:18:01 +01:00
//socket: sss
})
2022-03-04 20:57:24 +01:00
scudp.on('open', () => {
console.log("UDP to OSC open")
})
2022-03-04 20:57:24 +01:00
scudp.on('message', (msg) => {
2022-03-05 15:18:01 +01:00
if (DEBUG) {
console.log('got UDP msg', msg);
}
osclients.forEach( client => {
if (client) {
//console.log("sending", msg, info)
client.send(msg)
}
})
})
scudp.on('error', (e) => {
console.log('UDP OSC error!', e)
})
scudp.open()
2022-03-05 12:44:07 +01:00
function eulerFromQuaternion(quaternion) {
// Quaternion to matrix.
const w = quaternion[0], x = quaternion[1], y = quaternion[2], z = quaternion[3];
const x2 = x + x, y2 = y + y, z2 = z + z;
const xx = x * x2, xy = x * y2, xz = x * z2;
const yy = y * y2, yz = y * z2, zz = z * z2;
const wx = w * x2, wy = w * y2, wz = w * z2;
const matrix = [
1 - ( yy + zz ), xy + wz, xz - wy, 0,
xy - wz, 1 - ( xx + zz ), yz + wx, 0,
xz + wy, yz - wx, 1 - ( xx + yy ), 0,
0, 0, 0, 1
];
// Matrix to euler
function clamp( value, min, max ) {
return Math.max( min, Math.min( max, value ) );
}
const m11 = matrix[ 0 ], m12 = matrix[ 4 ], m13 = matrix[ 8 ];
const m21 = matrix[ 1 ], m22 = matrix[ 5 ], m23 = matrix[ 9 ];
const m31 = matrix[ 2 ], m32 = matrix[ 6 ], m33 = matrix[ 10 ];
var euler = [ 0, 0, 0 ];
euler[1] = Math.asin( clamp( m13, - 1, 1 ) );
if ( Math.abs( m13 ) < 0.9999999 ) {
euler[0] = Math.atan2( - m23, m33 );
euler[2] = Math.atan2( - m12, m11 );
} else {
euler[0] = Math.atan2( m32, m22 );
euler[2] = 0;
}
return euler;
}
2021-08-14 15:15:16 +02:00
const sendAll = (msg, info, oscWS, osclients) => {
// Reset euler rotation to 0
if (msg.address == '/keys') {
if (msg.args[0] && msg.args[1] && msg.args[2] && msg.args[3]) {
eulerRotation = [0, 0, 0]
}
}
2022-03-05 12:44:07 +01:00
// Convert quaternion diff to euler angle diff
if (msg.address == '/quaternionDiff') {
const euler = eulerFromQuaternion(msg.args, 'XYZ');
sendAll({
address: '/eulerDiff',
args: euler
}, info, oscWS, osclients)
2022-03-05 13:16:43 +01:00
eulerRotation[0] += euler[0]
eulerRotation[1] += euler[1]
eulerRotation[2] += euler[2]
sendAll({
address: '/euler',
args: eulerRotation
}, info, oscWS, osclients)
2022-03-05 12:44:07 +01:00
}
2021-08-14 15:15:16 +02:00
osclients.forEach( client => {
if (client && oscWS != client) {
2022-03-05 12:44:07 +01:00
// console.log("sending", msg, info)
2021-08-11 14:39:34 +02:00
client.send(msg)
}
})
if (scudp) {
2022-03-05 15:18:01 +01:00
if (DEBUG) {
console.log("UDP SEND", msg)
}
scudp.send(msg)
}
2021-08-11 14:39:34 +02:00
}
2021-08-14 15:15:16 +02:00
const osclients = []
2017-10-06 23:20:52 +02:00
2021-08-11 14:39:34 +02:00
wss.on('connection', function (ws) {
2022-03-05 12:44:07 +01:00
console.log('new client connection')
let oscWS = new osc.WebSocketPort({
2021-08-14 15:15:16 +02:00
socket: ws,
metadata: false
2021-08-11 14:39:34 +02:00
});
2021-08-14 15:15:16 +02:00
// Vsi OSC sem grejo naprej na kliente OSC
oscWS.on('packet', (packet, info) => {
// Broadcast adjust msg
2021-08-14 15:15:16 +02:00
const [address, args] = packet
sendAll({ address, args}, info, oscWS, osclients)
})
oscWS.on('error', error => {
console.warn('Ignoring invalid OSC')
console.warn(error)
})
2021-08-14 15:15:16 +02:00
osclients.push(oscWS)
/*
oscWS.on('close', () => {
oscWS = null
})
*/
})
2022-03-03 21:51:02 +01:00