-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocessor.js
42 lines (37 loc) · 1.55 KB
/
processor.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
import { latencyMeasurer } from "./latencyMeasurer.js";
class MyProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
this.samplerate = options.processorOptions.samplerate;
this.latencyMeasurer = new latencyMeasurer();
this.latencyMeasurer.toggle();
this.lastState = 0;
this.port.postMessage('___ready___');
}
process(inputs, outputs, parameters) {
let inBufferL = null, inBufferR = null, outBufferL = null, outBufferR = null;
if (typeof inputs.getChannelData === 'function') {
inBufferL = inputs.getChannelData(0);
inBufferR = inputs.getChannelData(1);
} else {
inBufferL = inputs[0][0];
inBufferR = inputs[0][1];
}
if (typeof outputs.getChannelData === 'function') {
outBufferL = outputs.getChannelData(0);
outBufferR = outputs.getChannelData(1);
} else {
outBufferL = outputs[0][0];
outBufferR = outputs[0][1];
}
this.latencyMeasurer.processInput(inBufferL, inBufferR, this.samplerate, 128);
this.latencyMeasurer.processOutput(outBufferL, outBufferR);
if (this.lastState != this.latencyMeasurer.state) {
this.lastState = this.latencyMeasurer.state;
this.port.postMessage({ state: this.lastState, latency: this.latencyMeasurer.latencyMs });
}
return true;
}
}
if (typeof AudioWorkletProcessor === 'function') registerProcessor('MyProcessor', MyProcessor);
export default MyProcessor;