-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudio-worklet.js
More file actions
55 lines (49 loc) · 1.22 KB
/
audio-worklet.js
File metadata and controls
55 lines (49 loc) · 1.22 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
class MyAudioWorklet extends AudioWorkletProcessor {
constructor() {
super();
this.FIFO_CAP = 5000;
this.fifo0 = new Int16Array(this.FIFO_CAP);
this.fifo1 = new Int16Array(this.FIFO_CAP);
this.fifoHead = 0;
this.fifoLen = 0;
this.port.onmessage = (e) => {
//console.log(this.fifoLen)
var buf = e.data;
var samplesReceived = buf.length / 2;
if (this.fifoLen + samplesReceived >= this.FIFO_CAP) {
//console.log('o')
return;
}
for (var i = 0; i < buf.length; i += 2) {
this.fifoEnqueue(buf[i], buf[i + 1]);
}
};
}
fifoDequeue() {
this.fifoHead += 1;
this.fifoHead %= this.FIFO_CAP;
this.fifoLen -= 1;
}
fifoEnqueue(a, b) {
const pos = (this.fifoHead + this.fifoLen) % this.FIFO_CAP;
this.fifo0[pos] = a;
this.fifo1[pos] = b;
this.fifoLen += 1;
}
process(inputs, outputs, parameters) {
const output = outputs[0];
const chan0 = output[0];
const chan1 = output[1];
for (var i = 0; i < chan0.length; i++) {
if (this.fifoLen < 1) {
//console.log("u")
break;
}
chan0[i] = this.fifo0[this.fifoHead] / 32768.0;
chan1[i] = this.fifo1[this.fifoHead] / 32768.0;
this.fifoDequeue();
}
return true;
}
}
registerProcessor('my-worklet', MyAudioWorklet);