in lex-web-ui/src/lib/lex/recorder.js [458:504]
_initMicVolumeProcessor() {
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
// assumes a single channel - XXX does it need to handle 2 channels?
const processor = this._audioContext.createScriptProcessor(
this.bufferLength,
this.numChannels,
this.numChannels,
);
processor.onaudioprocess = (evt) => {
if (this._state === 'recording') {
// send buffers to worker
this._recordBuffers(evt.inputBuffer);
// stop recording if over the maximum time
if ((this._audioContext.currentTime - this._recordingStartTime)
> this.recordingTimeMax
) {
console.warn('stopped recording due to maximum time');
this.stop();
}
}
// XXX assumes mono channel
const input = evt.inputBuffer.getChannelData(0);
let sum = 0.0;
let clipCount = 0;
for (let i = 0; i < input.length; ++i) {
// square to calculate signal power
sum += input[i] * input[i];
if (Math.abs(input[i]) > 0.99) {
clipCount += 1;
}
}
this._instant = Math.sqrt(sum / input.length);
this._slow = (0.95 * this._slow) + (0.05 * this._instant);
this._clip = (input.length) ? clipCount / input.length : 0;
this._setIsMicMuted();
this._setIsMicQuiet();
this._analyser.getFloatFrequencyData(this._analyserData);
this._maxVolume = Math.max(...this._analyserData);
};
this._micVolumeProcessor = processor;
return Promise.resolve();
}