2021-09-06 14:27:54 +02:00
|
|
|
import type { IAnalyserNode, IAudioContext, IMediaStreamAudioSourceNode } from "standardized-audio-context";
|
2021-06-02 17:58:58 +02:00
|
|
|
|
2020-06-23 12:24:36 +02:00
|
|
|
/**
|
|
|
|
* Class to measure the sound volume of a media stream
|
|
|
|
*/
|
|
|
|
export class SoundMeter {
|
|
|
|
private instant: number;
|
|
|
|
private clip: number;
|
|
|
|
//private script: ScriptProcessorNode;
|
2021-09-06 14:27:54 +02:00
|
|
|
private analyser: IAnalyserNode<IAudioContext> | undefined;
|
|
|
|
private dataArray: Uint8Array | undefined;
|
|
|
|
private context: IAudioContext | undefined;
|
|
|
|
private source: IMediaStreamAudioSourceNode<IAudioContext> | undefined;
|
2020-06-23 12:24:36 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.instant = 0.0;
|
|
|
|
this.clip = 0.0;
|
|
|
|
//this.script = context.createScriptProcessor(2048, 1, 1);
|
|
|
|
}
|
|
|
|
|
2021-06-02 17:58:58 +02:00
|
|
|
private init(context: IAudioContext) {
|
2021-05-10 19:55:43 +02:00
|
|
|
this.context = context;
|
|
|
|
this.analyser = this.context.createAnalyser();
|
2020-06-23 12:24:36 +02:00
|
|
|
|
2021-05-10 19:55:43 +02:00
|
|
|
this.analyser.fftSize = 2048;
|
|
|
|
const bufferLength = this.analyser.fftSize;
|
|
|
|
this.dataArray = new Uint8Array(bufferLength);
|
2020-06-23 12:24:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:27:54 +02:00
|
|
|
public connectToSource(stream: MediaStream, context: IAudioContext): void {
|
2021-06-01 16:17:36 +02:00
|
|
|
if (this.source !== undefined) {
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:24:36 +02:00
|
|
|
this.init(context);
|
|
|
|
|
|
|
|
this.source = this.context?.createMediaStreamSource(stream);
|
|
|
|
if (this.analyser !== undefined) {
|
|
|
|
this.source?.connect(this.analyser);
|
|
|
|
}
|
|
|
|
//analyser.connect(distortion);
|
|
|
|
//distortion.connect(this.context.destination);
|
|
|
|
//this.analyser.connect(this.context.destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getVolume(): number {
|
|
|
|
if (this.context === undefined || this.dataArray === undefined || this.analyser === undefined) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
this.analyser.getByteFrequencyData(this.dataArray);
|
|
|
|
|
|
|
|
const input = this.dataArray;
|
|
|
|
let i;
|
|
|
|
let sum = 0.0;
|
|
|
|
//let clipcount = 0;
|
|
|
|
for (i = 0; i < input.length; ++i) {
|
|
|
|
sum += input[i] * input[i];
|
2021-09-06 14:27:54 +02:00
|
|
|
// if (Math.abs(input[i]) > 0.99) {
|
|
|
|
// clipcount += 1;
|
|
|
|
// }
|
2020-06-23 12:24:36 +02:00
|
|
|
}
|
|
|
|
this.instant = Math.sqrt(sum / input.length);
|
|
|
|
//this.slow = 0.95 * that.slow + 0.05 * that.instant;
|
|
|
|
//this.clip = clipcount / input.length;
|
|
|
|
|
|
|
|
//console.log('instant', this.instant, 'clip', this.clip);
|
|
|
|
|
|
|
|
return this.instant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public stop(): void {
|
|
|
|
if (this.context === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.source !== undefined) {
|
|
|
|
this.source.disconnect();
|
|
|
|
}
|
|
|
|
this.context = undefined;
|
|
|
|
this.analyser = undefined;
|
|
|
|
this.dataArray = undefined;
|
|
|
|
this.source = undefined;
|
|
|
|
}
|
|
|
|
}
|