-
Notifications
You must be signed in to change notification settings - Fork 306
/
worklet.js
33 lines (27 loc) · 936 Bytes
/
worklet.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
class MegaWorkletMessenger extends AudioWorkletProcessor {
constructor() {
super();
this.threshold = false;
this.currentTime = currentTime;
this.port.onmessage = (ev) => this.handleMessage(ev);
}
handleMessage(ev) {
const {message, threshold} = ev.data;
// console.debug('worklet-messenger', ev.data);
if (message === 'schedule') {
this.threshold = threshold / 1e3;
}
else if (message === 'sleep') {
this.threshold = false;
}
}
process() {
if (this.threshold && currentTime - this.currentTime > this.threshold) {
// console.debug('worklet-processor', currentTime);
this.currentTime = currentTime;
this.port.postMessage({message: 'dispatch', currentTime});
}
return true;
}
}
registerProcessor('mega-worklet-messenger', MegaWorkletMessenger);