-
Notifications
You must be signed in to change notification settings - Fork 7
/
webmixer.js
227 lines (216 loc) · 8.24 KB
/
webmixer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// node-js file for the ovbox web mixer
var http = require('http');
var os = require('os');
var fs = require('fs');
var iolib = require('socket.io');
var osc = require('node-osc');
var path = require('path');
const homedir = require('os').homedir();
var deviceid = '';
{
var devname = os.hostname();
if( process.argv.length > 3 )
devname = process.argv[3];
try{
devname = fs.readFileSync('devicename');
}
catch(ee){
}
var devnames = devname.split(' ');
deviceid = devnames[0];
}
httpserver = http.createServer(function (req, res) {
// check if file is in local directory:
if( req.url.startsWith('/rec') & (
req.url.endsWith('.wav')||
req.url.endsWith('.aif')||
req.url.endsWith('.mat')||
req.url.endsWith('.flac')||
req.url.endsWith('.caf')
)){
// download from local directory:
if( fs.existsSync('.'+req.url) ){
var data = fs.readFileSync('.'+req.url);
res.writeHead(200);
res.end(data);
return;
}
// check in home directory:
if( fs.existsSync(homedir+req.url) ){
var data = fs.readFileSync(homedir+req.url);
res.writeHead(200);
res.end(data);
return;
}
}
var sdir = path.dirname(process.argv[1]);
console.log(sdir);
if( sdir.length > 0 )
sdir = sdir + '/';
var hosjs = fs.readFileSync(sdir+'ovclient.js');
var hoscss = fs.readFileSync(sdir+'ovclient.css');
var jackrec = fs.readFileSync(sdir+'jackrec.html');
var ipaddr = os.hostname();
if( process.argv.length > 2 )
ipaddr = process.argv[2];
var devname = os.hostname();
if( process.argv.length > 3 )
devname = process.argv[3];
try{
devname = fs.readFileSync('devicename');
}
catch(ee){
}
var devnames = devname.split(' ');
//deviceid = devnames[0];
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<!DOCTYPE HTML>\n');
res.write('<html><head><meta name="viewport" content="width=device-width, initial-scale=1"><style>');
res.write(hoscss);
res.write('</style><title>ov-client web mixer</title>\n</head><body>\n');
res.write('<h1>'+devname+'</h1>\n');
res.write(jackrec);
res.write('<script src="http://'+ipaddr+':8080/socket.io/socket.io.js"></script>\n');
res.write('<script>\n');
res.write('var socket = io("http://'+ipaddr+':8080");\n');
res.write(hosjs);
res.write('</script>\n');
res.end('</body></html>');
});
httpserver.listen(8080);
io = iolib(httpserver);
var oscServer, oscClient;
oscServer = new osc.Server( 9000, '0.0.0.0' );
oscClient = new osc.Client( 'localhost', 9871 );
function findOverlap(a, b) {
if (b.length === 0) {
return "";
}
if (a.endsWith(b)) {
return b;
}
if (a.indexOf(b) >= 0) {
return b;
}
return findOverlap(a, b.substring(0, b.length - 1));
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
io.on('connection', function (socket) {
socket.emit('deviceid',deviceid);
socket.on('objmixposcomplete', async function (obj) {
socket.emit('objmixredraw');
});
socket.on('config', function (obj) {
var varlist = {};
oscClient.send('/status', socket.id + ' connected');
oscServer.on('message', async function(msg, rinfo) {
if( msg[0] == '/touchosc/scene' ){
socket.emit('scene', 'scene');
}
if( msg[0].startsWith('/touchosc/label') && (!msg[0].endsWith('/color')) && (msg[1].length>1)){
socket.emit('newfader', msg[0].substr(15), msg[1] );
}
if( msg[0].startsWith('/touchosc/mute') && (!msg[0].endsWith('/color')) ){
socket.emit('updatemute', msg[0], msg[1] );
}
if( msg[0].startsWith('/touchosc/fader') && (!msg[0].endsWith('/color')) ){
socket.emit('updatefader', msg[0], msg[1] );
}
if( msg[0].startsWith('/touchosc/level') ){
socket.emit('updatefader', msg[0], msg[1] );
}
if( msg[0] == '/vertexpos' ){
const vpvars = msg[1].split('/');
var vpname = vpvars[2] + '.' + vpvars[3];
if( vpvars[2] == 'ego' )
vpname = vpvars[3];
socket.emit('vertexpos', vpname, msg[2], msg[3], msg[4], msg[1] );
}
if( msg[0] == '/jackrec/start' )
socket.emit('jackrecstart', '');
if( msg[0] == '/jackrec/stop' )
socket.emit('jackrecstop', '');
if( msg[0] == '/jackrec/portlist' )
socket.emit('jackrecportlist', '');
if( msg[0] == '/jackrec/port' )
socket.emit('jackrecaddport', msg[1] );
if( msg[0] == '/jackrec/filelist' )
socket.emit('jackrecfilelist', '');
if( msg[0] == '/jackrec/file' )
socket.emit('jackrecaddfile', msg[1] );
if( msg[0] == '/jackrec/rectime' )
socket.emit('jackrectime', msg[1] );
if( msg[0] == '/jackrec/error' )
socket.emit('jackrecerr', msg[1] );
if( msg[0] == '/varlist/getval' ){
if( varlist[msg[1]] !== null ){
socket.emit('updatevar',msg[1].replace(/[^a-zA-Z0-9]/g,''),msg[2]);
}
}
if( msg[0] == '/varlist/begin' )
varlist = {};
if( msg[0] == '/varlist' ){
if( (msg[2] == 'f') && (msg[3]>0) ){
var grps = msg[1].split('/');
if( grps.length > 3 )
varlist[msg[1]] = {'path':msg[1],'range':msg[4],'comment':msg[5],'label':msg[1]};
}
}
if( msg[0] == '/varlist/end' ){
var parents = [];
var sparents = [];
for(const key in varlist){
var grps = varlist[key].path.split('/');
varlist[key].id = varlist[key].path.replace(/[^a-zA-Z0-9]/g,'');
varlist[key].label = grps.pop();
varlist[key].parent = grps.join('');
while( grps.length > 0 ){
const level = grps.length;
var parent = grps.join('');
var grapa = null;
var grlab = grps.pop();
if( grlab ){
grapa = grps.join('');
grlab = grlab.replace('bus.','');
}
if( grlab && (grlab.length > 0) ){
if( sparents.indexOf(parent) < 0 ){
parents.unshift({'id':parent,'parent':grapa,'label':grlab,'level':level});
sparents.push(parent);
}
}
}
}
parents.sort((a, b) => {
if( a.level != b.level ) return a.level-b.level;
if( b.label < a.label ) return 1;
if( b.label > a.label ) return -1;
return 0;
} );
socket.emit('oscvarlist',parents,varlist);
for(const key in varlist){
const v = varlist[key];
oscClient.send(v.path+'/get','osc.udp://localhost:9000/','/varlist/getval');
}
}
});
oscClient.send('/touchosc/connect',16);
oscClient.send('/jackrec/listports');
oscClient.send('/jackrec/listfiles');
oscClient.send('/sendvarsto','osc.udp://localhost:9000/','/varlist','/bus.');
//oscClient.send('/*/ego/*/pos/get', 'osc.udp://localhost:9000/', '/vertexpos');
oscClient.send('/*/globalpos/get', 'osc.udp://localhost:9000/', '/vertexpos');
});
socket.on('message', function (obj) {
oscClient.send(obj);
});
socket.on('msg', function (obj) {
if( obj.hasOwnProperty('value') && (obj.value != null) ){
oscClient.send( obj.path, obj.value );
}else{
oscClient.send( obj.path );
}
});
});