-
Notifications
You must be signed in to change notification settings - Fork 7
/
vad.js
46 lines (37 loc) · 928 Bytes
/
vad.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const VAD = require('node-vad')
class VoiceActivityFilter {
constructor(options) {
this.options = options || {}
this._debouncing = this.debounce
this._vad = new VAD(this.vadMode)
}
get sampleRate() {
return this.options.sampleRate || 16000
}
get vadMode() {
return this.options.vadMode || VAD.Mode.VERY_AGGRESSIVE
}
get vadDebounceTime() {
return this.options.vadDebounceTime || 1000
}
get debounce() {
return this.options.debounce || 20
}
async processAudio(audioBuffer) {
if ( this._debouncing > 0 ) {
this._debouncing--
return true
}
const res = await this._vad.processAudio(audioBuffer, this.sampleRate)
if ( res === VAD.Event.VOICE ) {
this._debouncing = this.debounce
return true
}
return false
}
destroy(err) {
this._vad = null
}
}
VoiceActivityFilter.Mode = VAD.Mode
module.exports = VoiceActivityFilter