43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
import {SpeechEnvelope} from './speech-envelope.js';
|
|
|
|
class SpeechProbe extends AudioWorkletProcessor {
|
|
constructor({processorOptions:{minutes}}) {
|
|
super();
|
|
this.limit = 6000 * minutes;
|
|
this.ticks = 0;
|
|
this.batch = [];
|
|
this.lastAck = currentFrame;
|
|
this.previousEnd = null;
|
|
this.contiguous = true;
|
|
this.port.onmessage = ({data}) => {
|
|
if (data === 'stop') this.stopped = true;
|
|
if (data === 'ack') this.lastAck = currentFrame;
|
|
};
|
|
this.envelope = new SpeechEnvelope(sampleRate, (level, high, present) => {
|
|
if (this.ticks >= this.limit) return;
|
|
this.batch.push([level, high, present && this.contiguous]);
|
|
this.contiguous = true;
|
|
this.ticks++;
|
|
});
|
|
}
|
|
process(inputs, outputs) {
|
|
if (this.stopped) return false;
|
|
if (currentFrame - this.lastAck > 10 * sampleRate) {
|
|
this.port.postMessage({expired:true}); return false;
|
|
}
|
|
const count = outputs[0][0].length;
|
|
if (this.previousEnd !== null && this.previousEnd !== currentFrame) this.contiguous = false;
|
|
this.previousEnd = currentFrame + count;
|
|
const before = this.envelope.frames;
|
|
this.envelope.push(inputs[0]?.[0], count);
|
|
const complete = this.ticks === this.limit;
|
|
if (this.batch.length >= 25 || complete) {
|
|
const end = currentFrame + Math.round(this.ticks * sampleRate / 100) - before;
|
|
this.port.postMessage({batch:this.batch, endMs:end * 1000 / sampleRate, complete});
|
|
this.batch = [];
|
|
}
|
|
return !complete;
|
|
}
|
|
}
|
|
registerProcessor('speech-probe', SpeechProbe);
|