-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (64 loc) · 2.32 KB
/
index.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
import Clocks from 'dvbcss-clocks';
import { rendering, synchronisation } from '@bbc/audio-orchestration-core';
const { Sequence, SequenceRenderer } = rendering;
const { AudioContextClock } = synchronisation;
const audioContext = new AudioContext();
const sysClock = new AudioContextClock({}, audioContext);
const clock = new Clocks.CorrelatedClock(sysClock, { correlation: [0, 0], speed: 0, tickRate: 1 });
function initRenderer(sequenceData) {
const sequence = new Sequence(sequenceData);
const renderer = new SequenceRenderer(audioContext, clock, sequence);
renderer.start(0);
renderer.output.connect(audioContext.destination);
return renderer;
}
function getSelectedObjects() {
const objectIds = [];
document.querySelectorAll('input[type=checkbox]:checked').forEach(input => {
objectIds.push({
objectGain: 1.0,
objectId: input.value,
});
});
return objectIds;
}
function initControls(renderer) {
renderer.sequence.objectIds.forEach((objectId, i) => {
const input = document.createElement('input');
input.type = 'checkbox';
input.checked = (i === 0);
input.value = objectId;
const label = document.createElement('label');
label.appendChild(input);
label.appendChild(document.createTextNode(objectId));
document.getElementById('object-ids').appendChild(label);
});
const clickHandlers = {
'btn-play': () => {
audioContext.resume();
clock.setCorrelationAndSpeed([sysClock.now(), clock.now()], 1);
},
'btn-pause': () => clock.setCorrelationAndSpeed([sysClock.now(), clock.now()], 0),
'btn-reset': () => clock.setCorrelationAndSpeed([sysClock.now(), 0], 1),
'btn-skip': () => clock.setCorrelationAndSpeed([sysClock.now(), clock.now() + 10], 1),
'btn-allocate': () => renderer.setActiveObjects(getSelectedObjects()),
};
Object.entries(clickHandlers).forEach(([id, cb]) => {
document.getElementById(id).addEventListener('click', cb);
});
}
function init() {
fetch('./sequence.json')
.then((response) => {
if (!response.ok) {
throw new Error('could not download sequence.json');
}
return response.json();
})
.then(data => initRenderer(data))
.then(renderer => initControls(renderer));
setInterval(() => {
document.getElementById('clock').innerText = clock.now().toFixed(1);
}, 100);
}
init();