-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer-source-processor.js
More file actions
52 lines (46 loc) · 1.43 KB
/
buffer-source-processor.js
File metadata and controls
52 lines (46 loc) · 1.43 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
class BufferSourceProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
this.sampleClock = 0;
this.playing = false;
this.bufferChannels = options.processorOptions.bufferChannels;
this.port.onmessage = event => {
const action = JSON.parse(event.data);
switch (action.type) {
case "start":
this.sampleClock = 0;
this.playing = true;
break;
case "stop":
this.playing = false;
break;
default:
console.warn("buffersource received unhandled action: ", action);
break;
}
};
}
process(inputs, outputs, parameters) {
const { sampleClock, bufferChannels } = this;
for (let o = 0; o < outputs.length; o += 1) {
const channels = outputs[o];
for (let c = 0; c < channels.length; c += 1) {
if (bufferChannels.length < c) {
continue;
}
const samples = channels[c];
const bufferSamples = bufferChannels[c];
for (let s = 0; s < samples.length; s += 1) {
const bufferIndex =
bufferSamples && bufferSamples.length
? (sampleClock + s) % bufferSamples.length
: 0;
outputs[o][c][s] = this.playing ? bufferSamples[bufferIndex] : 0;
}
}
}
this.sampleClock += outputs[0][0].length;
return true;
}
}
registerProcessor("buffer-source-processor", BufferSourceProcessor);