-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
32 lines (27 loc) · 933 Bytes
/
index.ts
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
import bindings from "bindings";
const vadBindings = bindings("vad.node");
export default class VAD {
private sampleRate: number;
private instance: any;
constructor(sampleRate: number = 16000, level: number = 3) {
this.sampleRate = sampleRate;
this.instance = new vadBindings.VAD(sampleRate, level);
}
private valid(audio: Buffer): boolean {
return (
audio.length / 2 == this.sampleRate / 100 ||
audio.length / 2 == (2 * this.sampleRate) / 100 ||
audio.length / 2 == (3 * this.sampleRate) / 100
);
}
process(audio: Buffer): boolean {
if (!this.valid(audio)) {
throw new Error(
`Invalid audio length. For a sample rate of ${this.sampleRate}, audio length must be ${(2 *
this.sampleRate) /
100}, ${(4 * this.sampleRate) / 100}, or ${(6 * this.sampleRate) / 100}.`
);
}
return this.instance.process(audio, audio.length / 2);
}
}