This repository was archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.js
More file actions
588 lines (486 loc) · 18.3 KB
/
control.js
File metadata and controls
588 lines (486 loc) · 18.3 KB
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
import TangleMsgBox from "./lib/webcomponents/dialog-component.js";
import { SpectodaSound } from "./lib/tangle-js/SpectodaSound.js";
// Just to make blockly interactive first and let libraries load in the background
window.onload = function () {
window.alert = TangleMsgBox.alert;
window.confirm = TangleMsgBox.confirm;
window.prompt = TangleMsgBox.prompt;
const spectodaSoundMic = new SpectodaSound();
spectodaSoundMic.on('loudness', handleControlSend)
const spectodaSoundMusic = new SpectodaSound();
spectodaSoundMusic.on('loudness', handleControlSend)
const content_control = document.querySelector("#content_control");
const control_percentage_range = document.querySelector("#control_percentage_range");
const control_destination = document.querySelector("#control_destination");
const control_send = document.querySelector("#control_send");
const control_sound = document.querySelector("#control_sound");
const control_sound_music = document.querySelector("#control_sound_music");
control_sound.onclick = async e => {
if (!spectodaSoundMic.running) {
await spectodaSoundMic.connect("microphone");
spectodaSoundMic.start();
e.target.textContent = "Mic ON"
} else {
spectodaSoundMic.stop();
e.target.textContent = "Mic OFF"
}
}
control_sound_music.onclick = async e => {
if (!spectodaSoundMusic.running) {
await spectodaSoundMusic.connect(window.myAudioElement.captureStream())
spectodaSoundMusic.start()
e.target.textContent = "Music ON"
} else {
spectodaSoundMusic.stop();
e.target.textContent = "Music OFF"
}
}
const event_logs = document.querySelector("#event_logs");
const control_label = document.querySelector("#control_label");
const control_percentage_value = document.querySelector("#control_percentage_value");
const control_timestamp_value = document.querySelector("#control_timestamp_value");
const control_color_value = /** @type {HTMLInputElement} */ document.querySelector("#control_color_value");
const control_color_picker = document.querySelector("#control_color_picker");
const control_connector_select = document.querySelector("#control_connector_select");
// CONTROL TYPE HANDLER
let currentControlType = "percentage_control";
document.querySelector("#control_type").onchange = e => {
const controlType = e.target.options[e.target.selectedIndex].value;
// Hide other controls
document.querySelector(`#${currentControlType}`).style.display = "none";
document.querySelector(`#${controlType}`).style.display = "block";
currentControlType = controlType;
};
control_label.onchange = e => {
control_label.value = control_label.value.replace(/\W/g, "");
control_label.value = control_label.value.substring(0, 5);
};
control_color_picker.oninput = e => {
control_color_value.value = control_color_picker.value;
control_color_value.style.backgroundColor = control_color_value.value;
control_color_value.style.color = lum(control_color_value.value);
};
control_color_value.oninput = e => {
control_color_picker.value = getHexColor(control_color_value.value);
control_color_value.style.backgroundColor = control_color_value.value;
control_color_value.style.color = lum(control_color_value.value);
};
control_connector_select.onchange = e => {
console.log(`Assigning ${control_connector_select.value} connector to the device`);
Code.device.assignConnector(control_connector_select.value);
};
// Code.device.on("event", event => {
// control_label.value = event.label;
// control_destination.value = event.id;
// if (event.value === null) {
// return;
// }
// if (event.value.toString().match(/#[\dabcdefABCDEF]{6}/g)) {
// control_color_value.value = event.value;
// control_color_picker.value = event.value;
// } else {
// if (event.value >= -100.0 && event.value <= 100.0) {
// control_percentage_value.value = event.value;
// control_percentage_range.value = event.value;
// control_timestamp_value.value = event.value;
// }
// if (event.value >= -2147483648 && event.value <= 2147483647) {
// control_timestamp_value.value = event.value;
// }
// }
// });
// const timeline_toggle = document.querySelector("#timeline_toggle");
const timeline_container = document.querySelector("#timeline_container");
const wavesurfer_container = document.querySelector("#waveform_container");
// timeline_toggle.addEventListener("click", function () {
// timeline_container.classList.toggle("openned");
// wavesurfer_container.classList.toggle("hidden");
// });
// !! problems with layout, so we need to do this somehow manually
// TODO make it responsive on resize
// TODO make wavesurfer rerenders only when openned
// TODO add Zoom and time-line functionality like in Tangler
window.wavesurfer = WaveSurfer.create({
container: "#waveform",
height: 60,
backend: 'MediaElementWebAudio',
plugins: [
// WaveSurfer.regions.create({
// // regions: [
// // {
// // start: 0,
// // end: 5,
// // color: 'hsla(400, 100%, 30%, 0.1)'
// // },
// // {
// // start: 10,
// // end: 20,
// // color: 'hsla(200, 50%, 70%, 0.1)'
// // }
// // ]
// }),
WaveSurfer.timeline.create({
container: "#timeline",
}),
WaveSurfer.cursor.create({
showTime: true,
opacity: 1,
customShowTimeStyle: {
"background-color": "#000",
color: "#fff",
padding: "2px",
"font-size": "10px",
},
}),
],
});
// wavesurfer.setMute(true);
//window.musicDebounce = false;
// const playPause = document.querySelector("#playPause");
// playPause.onclick = function () {
// if (wavesurfer.isPlaying()) {
// Code.device.timeline.pause();
// wavesurfer.pause();
// playPause.innerHTML = "Play";
// } else {
// Code.device.timeline.unpause();
// wavesurfer.play();
// playPause.innerHTML = "Pause";
// }
// Code.device.setTimeline();
// };
wavesurfer.on("interaction", function () {
setTimeout(() => {
if (wavesurfer.getDuration()) {
const playing = wavesurfer.isPlaying();
if (playing != !Code.device.timeline.paused()) {
if (playing) {
Code.device.timeline.unpause();
} else {
Code.device.timeline.pause();
}
}
Code.device.timeline.setMillis(wavesurfer.getCurrentTime() * 1000);
}
Code.device.syncTimeline().catch(() => {
console.log("Device Disconnected");
});
}, 1);
});
// wavesurfer.load('./elevator.mp3');
wavesurfer.load('./timeline.mp3');
document.addEventListener("keypress", function onPress(event) {
if (event.key === " ") {
wavesurfer.playPause();
const dur = wavesurfer.getDuration();
const pos = dur ? wavesurfer.getCurrentTime() / wavesurfer.getDuration() : 0;
wavesurfer.seekAndCenter(pos);
}
});
let count = 100;
let debounce = true;
function handleAltZoom(e) {
if (!e.altKey) {
return;
} else {
e.preventDefault();
}
// TODO - implement debouncing to prevent render twice when zooming fast on long song
debounce = false;
if (!debounce) {
// setTimeout(_ => {
// setZoom((zoom) => {
// debounce = false;
// count = zoom;
// });
// }, 100)
if (count - e.deltaY < 10) {
count = 10;
} else {
count -= e.deltaY;
}
if (count > 1500) {
count = 1500;
}
if (count <= 10) {
count = 10;
}
console.log("zoom count", count);
if (count >= 10 && count <= 1500) {
wavesurfer.zoom(count);
debounce = true;
}
}
}
document.querySelector("#waveform").addEventListener("wheel", handleAltZoom);
function handlePercentageValueChange(e) {
const value = e.target.value;
handleControlSend(value);
}
function handleColorValueChange(e) {
const value = e.target.value;
handleControlSend(value);
}
function handleControlSend(value = null) {
let log_value = "";
if (currentControlType === "percentage_control") {
if (value === null) {
log_value = control_percentage_value.value + "%";
Code.device.emitPercentageEvent(control_label.value, parseFloat(control_percentage_value.value), [control_destination.value], true, false).then(() => {
console.log("Sent!");
});
} else {
log_value = value + "%";
Code.device.emitPercentageEvent(control_label.value, parseFloat(value), [control_destination.value], false, false);
}
} else if (currentControlType === "color_control") {
// if (!value) {
const hexColor = getHexColor(document.querySelector("#control_color_value").value);
log_value = `<span style="color:${hexColor}">` + hexColor + `</span>`;
Code.device.emitColorEvent(control_label.value, hexColor, [control_destination.value], true, false);
// } else {
// Code.device.bluetoothDevice.emitColorEvent(control_label.value, value, [control_destination.value]);
// }
} else if (currentControlType === "timestamp_control") {
log_value = control_timestamp_value.value + " ms";
// TODO parse timeparams (x seconds, x minutes, x hours, x days), like in block
Code.device.emitTimestampEvent(control_label.value, control_timestamp_value.value, [control_destination.value], true, false);
}
}
const sliders = [
"sli_0",
"sli_1",
"sli_2",
"sli_3",
"sli_4",
"sli_5",
"sli_6",
"sli_7",
"sli_8",
"sli_9"
];
for (let i = 0; i < sliders.length; i++) {
const slider = document.querySelector(`#slider_${sliders[i]}`);
slider.oninput = (e) => {
const value = e.target.value;
Code.device.emitPercentageEvent(sliders[i], value);
};
}
Code.device.on("event", event => {
const logmessageDOM = document.createElement("li");
// TODO edit this message accordingly to each control type
logmessageDOM.innerHTML = `${new Date().toString().slice(15, 24)} : $${event.label} = ${event.value} [${event.type}] -> ${event.id}`;
event_logs.appendChild(logmessageDOM);
event_logs.scrollTop = -999999999;
if (event_logs.childElementCount > 100) {
event_logs.removeChild(event_logs.firstChild)
}
for (let i = 0; i < sliders.length; i++) {
if (event.label == sliders[i]) {
const feedback = document.querySelector(`#feedback_${sliders[i]}`);
feedback.innerHTML = event.value;
const slider = document.querySelector(`#slider_${sliders[i]}`);
slider.value = event.value;
}
}
});
const apply_button = document.querySelector(`#apply_button`);
apply_button.onclick = e => {
for (let i = 0; i < sliders.length; i++) {
const slider = document.querySelector(`#slider_${sliders[i]}`);
Code.device.emitPercentageEvent(sliders[i], slider.value);
}
Code.device.emitEvent("apply");
};
const apply_events = document.querySelector(`#apply_events`);
apply_events.onclick = e => {
Code.device.resendAll()
}
control_percentage_range.oninput = handlePercentageValueChange;
control_color_picker.onchange = handleColorValueChange;
control_send.onclick = e => handleControlSend();
const saveFile = document.querySelector("#saveFile");
const loadFile = document.querySelector("#loadFile");
saveFile.onclick = _ => {
const zip = new JSZip();
const data = Blockly.Xml.domToPrettyText(Blockly.Xml.workspaceToDom(Code.workspace));
zip.file("version", "0.6.1");
zip.file("content.xml", data);
if (window.blockly_music) {
zip.file("music.mp3", window.blockly_music);
}
// if (window.blockly_metronome) {
// zip.file("metronome.mp3", window.blockly_metronome);
// }
zip.generateAsync({ type: "blob", compression: "STORE" }).then(function (content) {
saveAs(content, document.querySelector("#filename").value.replace(/\.tgbl$/, "") + ".tgbl");
});
};
loadFile.onclick = _ => {
// Create once invisible browse button with event listener, and click it
var selectFile = document.getElementById("select_file");
if (selectFile === null) {
var selectFileDom = document.createElement("INPUT");
selectFileDom.type = "file";
selectFileDom.id = "select_file";
var selectFileWrapperDom = document.createElement("DIV");
selectFileWrapperDom.id = "select_file_wrapper";
selectFileWrapperDom.style.display = "none";
selectFileWrapperDom.appendChild(selectFileDom);
document.body.appendChild(selectFileWrapperDom);
selectFile = document.getElementById("select_file");
selectFile.addEventListener("change", e => { parseInputXMLfile(e.target.files[0]); e.target.value = "" }, false);
}
selectFile.click();
};
// // MUSIC controls
// wavesurfer.on('interaction', e => {
// if (!musicDebounce) {
// //Code.music.currentTime = wavesurfer.getCurrentTime()
// musicDebounce = true;
// setTimeout(_ => musicDebounce = false, 20)
// }
// })
// Code.music.onplay = _ => wavesurfer.play();
// Code.music.onpause = _ => wavesurfer.pause();
// Code.music.onstop = _ => wavesurfer.stop();
setupOwnership();
};
function setupOwnership() {
const owner_identifier = /** @type {HTMLInputElement} */ (document.querySelector("#owner_identifier"));
const owner_signature = /** @type {HTMLInputElement} */ (document.querySelector("#owner_signature"));
const owner_key = /** @type {HTMLInputElement} */ (document.querySelector("#owner_key"));
owner_identifier.onchange = async e => {
const encoder = new TextEncoder();
const data = encoder.encode(owner_identifier.value);
const hash = await crypto.subtle.digest("SHA-1", data);
owner_signature.value = uint8ArrayToHexString(hash).slice(0, 32);
owner_signature.onchange(null);
};
owner_signature.onchange = e => {
try {
Code.device.setOwnerSignature(owner_signature.value);
// if event is not null, then the owner_signature was changed from the html input.
// then erase owner_identifier
if (e != null) {
owner_identifier.value = "";
}
} catch { }
owner_signature.value = Code.device.getOwnerSignature();
console.log(`owner_signature: ${owner_signature.value}`);
};
owner_key.onchange = e => {
try {
Code.device.setOwnerKey(owner_key.value);
} catch { }
owner_key.value = Code.device.getOwnerKey();
console.log(`owner_key: ${owner_key.value}`);
};
owner_identifier.onchange(null);
owner_key.onchange(null);
}
function loadBlocksfromXmlDom(blocksXmlDom) {
try {
Blockly.Xml.domToWorkspace(blocksXmlDom, Code.workspace);
} catch (e) {
return false;
}
return true;
}
function replaceBlocksfromXml(blocksXml) {
var xmlDom = null;
try {
xmlDom = Blockly.Xml.textToDom(blocksXml);
} catch (e) {
return false;
}
Code.workspace.clear();
var sucess = false;
if (xmlDom) {
sucess = loadBlocksfromXmlDom(xmlDom);
}
return true;
}
// Create File Reader event listener function
const parseInputXMLfile = function (file) {
const filename = file.name;
document.querySelector("#filename").value = filename;
JSZip.loadAsync(file).then(async function (zip) {
const xmlContent = await zip.file("content.xml").async("text");
const success = replaceBlocksfromXml(xmlContent);
if (zip.file("music.mp3")) {
window.blockly_music = await zip.file("music.mp3").async("blob");
const music_url = URL.createObjectURL(window.blockly_music);
// Code.music.setAttribute("src", music_url);
wavesurfer.load(music_url);
}
if (zip.file("metronome.mp3")) {
window.blockly_metronome = await zip.file("metronome.mp3").async("blob");
// Code.music.setAttribute("src", URL.createObjectURL(window.blockly_music));
}
console.log("File " + filename + " is loaded");
// console.log("Trying to upload tngl...");
// Code.device.writeTngl();
if (success) {
Code.renderContent();
} else {
alert("Ooops something went wrong during load. Try again with another file.");
}
});
};
// Code.music.addEventListener("timeupdate", () => {
// console.log("timeupdate");
// if (!musicDebounce && Code.music.paused) {
// Code.device.timeline.setMillis(Code.music.currentTime * 1000);
// wavesurfer.setCurrentTime(Code.music.currentTime);
// Code.device.syncTimeline();
// musicDebounce = true
// setTimeout(_ => musicDebounce = false, 20)
// }
// });
document.getElementById("music").addEventListener("change", function () {
var url = URL.createObjectURL(this.files[0]);
window.blockly_music = this.files[0];
// Code.music.setAttribute("src", url);
wavesurfer.load(url);
Code.device.timeline.pause();
Code.device.timeline.setMillis(wavesurfer.getCurrentTime() * 1000);
Code.device.syncTimeline();
});
function getHexColor(colorStr) {
const a = document.createElement("div");
a.style.color = colorStr;
const colors = window
.getComputedStyle(document.body.appendChild(a))
.color.match(/\d+/g)
.map(function (a) {
return parseInt(a, 10);
});
document.body.removeChild(a);
return colors.length >= 3 ? "#" + ((1 << 24) + (colors[0] << 16) + (colors[1] << 8) + colors[2]).toString(16).substr(1) : false;
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : [0, 0, 0];
}
function lum(hex) {
var rgb = hexToRgb(hex);
if ((rgb[0] > 200 && rgb[1] > 200 && rgb[2] > 200) || (rgb[0] > 140 && rgb[1] > 215 && rgb[2] > 140) || (rgb[1] > 220 && (rgb[0] > 120 || rgb[2] > 120))) {
return "#000000";
}
return "#ffffff";
}
document.body.ondrop = function (e) {
console.log("ondrop", e);
e.preventDefault();
};
function handleFileDrop(e) {
console.log("ondrop", e);
console.log(e.dataTransfer.files);
parseInputXMLfile(e.dataTransfer.files[0]);
e.preventDefault();
}
document.querySelector("#filename").ondrop = handleFileDrop;
document.querySelector("#loadFile").ondrop = handleFileDrop;
document.querySelector("#saveFile").ondrop = handleFileDrop;
//document.querySelector(".blocklySvg").ondrop = handleFileDrop; // This doesn't work