-
Notifications
You must be signed in to change notification settings - Fork 7
/
ovclient.js
596 lines (559 loc) · 20.4 KB
/
ovclient.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
var deviceid = '';
var inchannelpos = [];
var objmix_sel = -1;
var objmix_drag = false;
/* accepts parameters
* h Object = {h:x, s:y, v:z}
* OR
* h, s, v
*/
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
if (arguments.length === 1) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}
function objmix_getscale( w, h )
{
return 0.22*Math.min(w,h);
}
function pos2scr( pos )
{
const canvas = document.getElementById("objmixer");
const scale = objmix_getscale(canvas.width, canvas.height);
return {x:(0.5*canvas.width-scale*pos[1]),y:0.9*canvas.height-scale*pos[0]};
}
function scr2pos( pos )
{
const canvas = document.getElementById("objmixer");
const scale = objmix_getscale(canvas.width, canvas.height);
return {y:-(pos.x-0.5*canvas.width)/scale,
x:(-pos.y+0.9*canvas.height)/scale};
}
function on_canvas_click( e )
{
const canvas = document.getElementById("objmixer");
var rect = canvas.getBoundingClientRect();
var ksel = -1;
for( var k=0; k<inchannelpos.length;k++){
const vertex = inchannelpos[k];
var pos = pos2scr([vertex.x,vertex.y,vertex.z]);
pos.x = pos.x - e.clientX + rect.left;
pos.y = pos.y - e.clientY + rect.top;
const d = Math.sqrt(pos.x*pos.x + pos.y*pos.y);
if( d < 24 )
ksel = k;
}
objmix_sel = ksel;
if( ksel >= 0 ){
objmix_drag = true;
}
}
function on_canvas_up( e )
{
if( objmix_drag ){
objmix_drag = false;
socket.emit('objmixposcomplete');
e.preventDefault();
}
}
function on_canvas_move( e )
{
if( objmix_drag ){
const canvas = document.getElementById("objmixer");
if( !canvas )
return;
var rect = canvas.getBoundingClientRect();
const np = scr2pos({x: e.clientX-rect.left, y: e.clientY-rect.top});
inchannelpos[objmix_sel].x = np.x;
inchannelpos[objmix_sel].y = np.y;
objmix_draw();
//console.log(inchannelpos[objmix_sel].path);
//socket.emit("msg",{path:'/'+deviceid+'/ego/'+inchannelpos[objmix_sel].name+'/pos',
// value:[inchannelpos[objmix_sel].x,inchannelpos[objmix_sel].y,inchannelpos[objmix_sel].z]});
socket.emit("msg",{path:inchannelpos[objmix_sel].path,
value:[inchannelpos[objmix_sel].x,inchannelpos[objmix_sel].y,inchannelpos[objmix_sel].z]});
e.preventDefault();
}
}
function objmix_draw()
{
const canvas = document.getElementById("objmixer");
if( !canvas )
return;
canvas.width = canvas.parentElement.clientWidth-2;
canvas.height = 0.5*canvas.width;
const ctx = canvas.getContext("2d");
//ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = '#153d17';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.strokeStyle = "rgba(200, 200, 200, 0.5)";
ctx.beginPath();
p0 = pos2scr( [0,0] );
p1 = pos2scr( [0,4] );
p2 = pos2scr( [0,-4] );
p3 = pos2scr( [4,0] );
ctx.moveTo(p1.x,p1.y);
ctx.lineTo(p2.x,p2.y);
ctx.moveTo(p0.x,p0.y);
ctx.lineTo(p3.x,p3.y);
ctx.moveTo(p0.x,p0.y);
ctx.stroke();
ctx.beginPath();
ctx.arc(p0.x,p0.y,Math.abs(p1.x-p0.x),0,Math.PI,true);
ctx.stroke();
ctx.beginPath();
ctx.arc(p0.x,p0.y,0.25*Math.abs(p1.x-p0.x),0,Math.PI,true);
ctx.stroke();
ctx.restore();
ctx.save();
ctx.font = "24px sans";
//canvas.width = canvas.width;
for( var k=0; k<inchannelpos.length;k++){
const vertex = inchannelpos[k];
const pos = pos2scr([vertex.x,vertex.y,vertex.z]);
const colrgb = HSVtoRGB(k/inchannelpos.length, 0.65, 0.8 );
ctx.fillStyle = `rgb(${colrgb.r},${colrgb.g},${colrgb.b})`;
ctx.beginPath();
ctx.arc(pos.x, pos.y, 20*Math.sqrt(canvas.width/1000), 0, Math.PI * 2, true); // Outer circle
ctx.fill();
ctx.fillText(vertex.name, pos.x+24, pos.y-5);
}
ctx.restore();
}
function update_objmix_sounds()
{
const canvas = document.getElementById("objmixer");
if( !canvas )
return;
canvas.width = canvas.parentElement.clientWidth;
canvas.height = 0.55*canvas.width;
canvas.addEventListener( 'pointerdown', on_canvas_click );
canvas.addEventListener( 'pointerup', on_canvas_up );
canvas.addEventListener( 'pointermove', on_canvas_move );
objmix_draw();
}
socket.on("connect", function() {
inchannelpos = [];
socket.emit("config",{});
});
socket.on('deviceid',function(id){deviceid=id;});
socket.on("scene", function(scene){
let el=document.getElementById("mixer");
if( el ){
while (el.firstChild) {el.removeChild(el.firstChild);}
let elheader=document.createElement("h2");
elheader.setAttribute("class","scene");
el.append(scene,elheader);
let elgainstore=document.createElement("p");
elgainstore.setAttribute("class","gainstore");
el.appendChild(elgainstore);
}
});
socket.on("vertexpos", function(name, x, y, z, path){
// add an input channel for object-based mixing.
// if the provided object is already in list, then clear list:
var needclear = false;
for( var k=0; k<inchannelpos.length;k++){
if( inchannelpos[k].name == name )
needclear = true;
}
if( needclear )
inchannelpos = [];
// append object:
inchannelpos.push({'name':name,'x':x, 'y':y, 'z': z, 'path' : path});
update_objmix_sounds();
});
socket.on("newfader", function(faderno,val){
// remove effect bus from mixer:
if( val.startsWith('bus.') ){
return;
}
fader="/touchosc/fader"+faderno;
levelid="/touchosc/level"+faderno;
muteid="/touchosc/mute"+faderno;
let el_mixer=document.getElementById("mixer");
if( el_mixer ){
orig_val = val;
let classname = "mixerstrip";
val = val.replace('.'+deviceid,'');
val = val.replace(deviceid+'.','');
val = val.replace('bus.','');
if( val.startsWith("ego.")||(val == "monitor") ){
classname = classname + " mixerego";
val = val.replace('ego.','');
}
if( (val == "main") || (val == "reverb") )
classname = classname + " mixerother";
// create main mixer strip element:
let el_mixerstrip = el_mixer.appendChild(document.createElement("div"));
el_mixerstrip.setAttribute("class",classname);
el_mixerstrip.setAttribute("id","mixerstrip_"+orig_val);
let el_row1 = el_mixerstrip.appendChild(document.createElement("div"));
el_row1.setAttribute("class","mixerrow");
let el_row2 = el_mixerstrip.appendChild(document.createElement("div"));
el_row2.setAttribute("class","mixerrow");
let el_row3 = el_mixerstrip.appendChild(document.createElement("div"));
el_row3.setAttribute("class","mixerrow");
let el_lab = el_row1.appendChild(document.createElement("label"));
el_lab.setAttribute("for",fader);
el_lab.setAttribute("class","mixerlabel");
el_lab.append(val);
let el_mutebuttondiv = el_row1.appendChild(document.createElement("div"));
el_mutebuttondiv.setAttribute("class", "mutebuttondiv");
let el_span = el_mutebuttondiv.appendChild(document.createElement("span"));
el_span.setAttribute("class", "mutebuttonlabel");
el_span.appendChild(document.createTextNode("mute "));
let el_mutebutton = el_mutebuttondiv.appendChild(document.createElement("input"));
el_mutebutton.setAttribute("class", "mutebutton");
el_mutebutton.setAttribute("type", "checkbox");
el_mutebutton.setAttribute("id", muteid);
//el_mutebutton.onchange = upload_session_gains;
let el_fader=el_row2.appendChild(document.createElement("input"));
el_fader.setAttribute("class","fader");
el_fader.setAttribute("type","range");
el_fader.setAttribute("min","-30");
el_fader.setAttribute("max","10");
el_fader.setAttribute("value",val);
el_fader.setAttribute("step","0.1");
el_fader.setAttribute("id",fader);
el_fader.onchange = upload_session_gains;
let el_gaintext=el_row2.appendChild(document.createElement("input"));
el_gaintext.setAttribute("type","number");
el_gaintext.setAttribute("class","gaintxtfader");
el_gaintext.setAttribute("min","-30");
el_gaintext.setAttribute("max","10");
el_gaintext.setAttribute("step","0.1");
el_gaintext.setAttribute("id","txt"+fader);
let el_meter = el_row3.appendChild(document.createElement("meter"));
el_meter.setAttribute("class","level");
el_meter.setAttribute("min","0");
el_meter.setAttribute("max","94");
el_meter.setAttribute("low","71");
el_meter.setAttribute("high","84");
el_meter.setAttribute("optimum","54");
el_meter.setAttribute("id",levelid);
let el_metertext = el_row3.appendChild(document.createElement("input"));
el_metertext.setAttribute("type","text");
el_metertext.setAttribute("readonly","true");
el_metertext.setAttribute("class","gaintxtfader");
el_metertext.setAttribute("id","txt"+levelid);
//el_mixerstrip.appendChild(el_lab);
//el_mixerstrip.appendChild(el_mutebuttondiv);
//el_mixerstrip.appendChild(document.createElement("br"));
//el_mixerstrip.appendChild(document.createElement("br"));
//el_mixerstrip.appendChild(el_meter);
//el_mixerstrip.appendChild(el_metertext);
}
});
socket.on("updatefader", function(fader,val){
let fad=document.getElementById(fader);
if( fad!=null ){
fad.value=val;
}
let fadt=document.getElementById("txt"+fader);
if( fadt!=null ){
fadt.value=val.toFixed(1);
}
});
socket.on("updatemute", function(fader,val){
let fad=document.getElementById(fader);
if( fad!=null ){
fad.checked=(val == 1);
}
});
socket.on('updatevar', function(id, val){
let el=document.getElementById(id);
if( el )
el.setAttribute('value',val);
let el2=document.getElementById(id+'.disp');
if( el2 )
el2.setAttribute('value',parseFloat(val.toPrecision(4)));
});
socket.on('oscvarlist', function(parents,varlist){
let el=document.getElementById('plugpars');
while (el.firstChild) {el.removeChild(el.firstChild);}
for( var k=0;k<parents.length;++k){
const p = parents[k];
var d;
var dp = null;
if( p.parent && (p.parent.length>0) )
dp = document.getElementById(p.parent);
if( dp )
d = dp.appendChild(document.createElement('div'));
else
d = el.appendChild(document.createElement('div'));
d.setAttribute('id',p.id);
d.setAttribute('class','parblock');
d.appendChild(document.createTextNode(p.label));
}
for( const key in varlist ){
const v = varlist[key];
let p=document.getElementById(v.parent);
if( p ){
var dplug = p.appendChild(document.createElement('div'));
dplug.setAttribute('class','parstrip');
var dpluglab = dplug.appendChild(document.createElement('label'));
//dpluglab.setAttribute('type','label');
dpluglab.setAttribute('class','parstriplabel');
dpluglab.appendChild(document.createTextNode(v.label));
//dpluglab.setAttribute('value',v.label);
dpluglab.setAttribute('title',v.comment);
var inp = dplug.appendChild(document.createElement('input'));
inp.setAttribute('class','parstripctl');
inp.setAttribute('id',v.id);
inp.setAttribute('title',v.comment);
var inp2 = dplug.appendChild(document.createElement('input'));
inp2.setAttribute('class','parstripdisp');
inp2.setAttribute('id',v.id+'.disp');
inp2.setAttribute('type','number');
inp2.setAttribute('step','any');
inp2.setAttribute('title',v.comment);
var rg = v.range.split(',');
if( rg.length == 2 ){
var vmin = parseFloat(rg[0].replace('[','').replace(']',''));
var vmax = parseFloat(rg[1].replace('[','').replace(']',''));
const step = (vmax-vmin)/256;
if( rg[0].startsWith(']') )
vmin += step;
if( rg[1].endsWith('[') )
vmax -= step;
inp.setAttribute('type','range');
inp.setAttribute('min',vmin);
inp.setAttribute('max',vmax);
inp.setAttribute('step',step);
}else{
inp.setAttribute('type','number');
inp.setAttribute('step','any');
}
inp.onchange = function(e){
socket.emit("msg",{"path":v.path,"value":e.target.valueAsNumber});
socket.emit("msg",{"path":"/uploadpluginsettings","value":null});
let inp = document.getElementById(v.id+'.disp');
if( inp )
inp.value = parseFloat(e.target.valueAsNumber.toPrecision(4));
};
inp2.onchange = function(e){
socket.emit("msg",{"path":v.path,"value":e.target.valueAsNumber});
socket.emit("msg",{"path":"/uploadpluginsettings","value":null});
let inp = document.getElementById(v.id);
if( inp )
inp.value = e.target.valueAsNumber;
};
}
}
});
function upload_session_gains()
{
socket.emit("msg",{"path":"/uploadsessiongains","value":null});
}
function str_pad_left(string,pad,length) {
return (new Array(length+1).join(pad)+string).slice(-length);
}
function sec2minsec( t ){
var minutes = Math.floor(t / 60);
var seconds = Math.floor(t-60*minutes);
var sec10 = Math.floor(10*(t-Math.floor(t)));
return str_pad_left(minutes,'0',2) + ':' + str_pad_left(seconds,'0',2) + '.' + sec10;
}
function recerror( e ){
let el=document.getElementById("recerr");
if( el.childNodes.length > 0 )
el.replaceChild(document.createTextNode(e),el.childNodes[0]);
else
el.appendChild(document.createTextNode(e));
}
function objmix_upload_posandgains(){
socket.emit("msg",{"path":"/uploadobjmix","value":null});
}
socket.on("jackrecerr", function(e){recerror(e)} );
socket.on("jackrectime", function(t){
let el=document.getElementById("rectime");
el.setAttribute("value",sec2minsec(t));
//el.set_attribute("value",t);
});
socket.on("jackrecportlist", function(t){
let el=document.getElementById("portlist");
if( el )
while (el.firstChild) {el.removeChild(el.firstChild);}
});
socket.on('jackrecaddport', function(p){
var labs = p;
var classes = 'mixerstrip jackrecsrcport';
var helps = '';
if( p.startsWith('n2j_') )
return;
if( p.startsWith('system:capture') ){
classes += ' mixerego';
helps = 'Hardware input';
}
if( p.startsWith('bus.') ){
classes += ' mixerego';
helps = 'My input (with effects)';
labs = labs.replace('bus.','').replace(':out.0','');
}
if( p == deviceid + '.metronome:out.0' ){
return;
}
if( p == deviceid + '.metronome:out.1' ){
classes += ' mixerother';
helps = 'Metronome';
labs = 'metronome';
}
if( p.startsWith('render.'+deviceid) ){
classes += ' mixerother';
helps = 'My headphone output';
labs = labs.replace('render.'+deviceid+':','');
}
labs = labs.replace('.'+deviceid+':out','');
let el=document.getElementById("portlist");
if( el ){
let div=el.appendChild(document.createElement('div'));
div.setAttribute('class',classes);
let inp=div.appendChild(document.createElement('input'));
inp.setAttribute('title',helps);
inp.setAttribute('type','checkbox');
inp.setAttribute('value',p);
inp.setAttribute('id',p);
inp.setAttribute('class','jackport checkbox');
let lab=div.appendChild(document.createElement('label'));
lab.setAttribute('for',p);
lab.setAttribute('title',helps);
lab.appendChild(document.createTextNode(labs));
}
});
socket.on("jackrecfilelist", function(t){
let el=document.getElementById("filelist");
if( el )
while (el.firstChild) {el.removeChild(el.firstChild);}
});
socket.on('jackrecaddfile', function(p){
let el=document.getElementById("filelist");
if( !el )
return;
let div=el.appendChild(document.createElement('div'));
let inp=div.appendChild(document.createElement('input'));
inp.setAttribute('type','checkbox');
inp.setAttribute('value',p);
inp.setAttribute('class','filename');
let lab=div.appendChild(document.createElement('a'));
lab.setAttribute('href',p);
lab.appendChild(document.createTextNode(p));
});
socket.on('jackrecstart', function(p){
let el=document.getElementById("recindicator");
el.style = 'display: inline-block;';
});
socket.on('jackrecstop', function(p){
let el=document.getElementById("recindicator");
el.style = 'display: none;';
});
socket.on('objmixredraw', function(p){
objmix_draw();
});
let form = document.getElementById("mixer");
if( form )
form.oninput = handleChange;
function handleChange(e) {
if( e.target.id.substr(0,3)=="txt" ){
socket.emit("msg", { path: e.target.id.substr(3), value: e.target.valueAsNumber } );
let fad=document.getElementById(e.target.id.substr(3));
if( fad!=null ){
fad.value=e.target.valueAsNumber;
}
}else{
if(e.target.type == "checkbox" ){
if(e.target.checked)
socket.emit("msg", { path: e.target.id, value: 1 } );
else
socket.emit("msg", { path: e.target.id, value: 0 } );
}else{
socket.emit("msg", { path: e.target.id, value: e.target.valueAsNumber } );
let fadt=document.getElementById(e.target.id);
if( fadt!=null ){
fadt.value=e.target.valueAsNumber.toFixed(1);
}
}
}
}
function jackrec_start() {
socket.emit("msg", {path: '/jackrec/clear',value: null});
let el=document.getElementById("portlist");
if( !el )
return;
let ports=el.getElementsByClassName('jackport');
for( var k=0;k<ports.length;k++){
if( ports[k].checked ){
socket.emit("msg", {path: '/jackrec/addport',value: ports[k].getAttribute('value')});
}
}
recerror('');
socket.emit("msg", {path: '/jackrec/start', value: null} );
}
function jackrec_delete() {
let el=document.getElementById("filelist");
if( !el )
return;
let ports=el.getElementsByClassName('filename');
for( var k=0;k<ports.length;k++){
if( ports[k].checked ){
socket.emit("msg", {path: '/jackrec/rmfile',value: ports[k].getAttribute('value')});
}
}
socket.emit('msg', {path: '/jackrec/listfiles', value: null});
document.getElementById("selectallfiles").checked = false;
}
function jackrec_stop() {
recerror('');
socket.emit("msg", {path: '/jackrec/stop', value: null} );
socket.emit('msg', {path: '/jackrec/listfiles', value: null});
}
function jackrec_selectallfiles() {
let ischecked=document.getElementById("selectallfiles").checked;
let el=document.getElementById("filelist");
if( !el )
return;
let ports=el.getElementsByClassName('filename');
for( var k=0;k<ports.length;k++){
ports[k].checked = ischecked;
}
}
function showtab( name ) {
names = ['mixer','plugpars','jackrec','objmix'];
for( var k=0;k<names.length;k++){
var el = document.getElementById(names[k]);
var elinp = document.getElementById('tabact'+names[k]);
if( names[k] == name ){
el.classList.add("tabshow");
el.classList.remove("tabhide");
elinp.classList.add("tabact");
}else{
el.classList.remove("tabshow");
el.classList.add("tabhide");
elinp.classList.remove("tabact");
}
}
objmix_draw();
}
window.addEventListener('resize', function(event) {
objmix_draw();
}, true);