36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
import {AcousticDetector} from './acoustic-detector.js';
|
|
|
|
class AcousticProbe extends AudioWorkletProcessor {
|
|
constructor(options) {
|
|
super();
|
|
this.detector = new AcousticDetector(sampleRate, options.processorOptions.minutes);
|
|
this.next = currentFrame;
|
|
this.deadline = currentFrame + sampleRate * (this.detector.minutes * 60 + 60);
|
|
this.ack = currentFrame;
|
|
this.credit = true;
|
|
this.stopped = false;
|
|
this.port.onmessage = ({data}) => {
|
|
if (data === 'stop') this.stopped = true;
|
|
if (data === 'ack') { this.credit = true; this.ack = currentFrame; }
|
|
};
|
|
}
|
|
process(inputs, outputs) {
|
|
if (this.stopped) return false;
|
|
if (currentFrame >= this.deadline || currentFrame - this.ack > 10 * sampleRate) {
|
|
this.port.postMessage({expired:true});
|
|
return false;
|
|
}
|
|
const size = outputs[0][0].length;
|
|
this.detector.process(inputs[0]?.[0], currentFrame, size);
|
|
if (this.credit && currentFrame >= this.next) {
|
|
const sample = this.detector.summary((currentFrame + size) * 1000 / sampleRate);
|
|
this.port.postMessage(sample);
|
|
if (sample.complete) return false;
|
|
this.next = currentFrame + sampleRate;
|
|
this.credit = false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
registerProcessor('acoustic-probe', AcousticProbe);
|