-
Notifications
You must be signed in to change notification settings - Fork 2
/
vad.go
58 lines (46 loc) · 1.24 KB
/
vad.go
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
47
48
49
50
51
52
53
54
55
56
57
58
// Package goEagi of vad.go provides functionality on
// detecting voice/speech activity based on audio bytes.
package goEagi
const (
defaultAmplitudeDetectionThreshold = -27.5
)
type VadResult struct {
Error error
Detected bool
Amplitude float64
Frame []byte
}
type Vad struct {
AmplitudeDetectionThreshold float64
}
// NewVad is a constructor of Vad.
// The initialization will use the defaultAmplitudeDetectionThreshold.
func NewVad(amplitudeThreshold float64) *Vad {
if amplitudeThreshold != 0 {
return &Vad{AmplitudeDetectionThreshold: amplitudeThreshold}
}
return &Vad{AmplitudeDetectionThreshold: defaultAmplitudeDetectionThreshold}
}
// Detect analyzes voice activity for a given slice of bytes.
func (v *Vad) Detect(done <-chan interface{}, stream <-chan []byte) <-chan VadResult {
vadResultStream := make(chan VadResult)
go func() {
defer close(vadResultStream)
for {
select {
case <-done:
return
case buf := <-stream:
amp, err := ComputeAmplitude(buf)
if err != nil {
vadResultStream <- VadResult{Error: err}
return
}
if v.AmplitudeDetectionThreshold < amp {
vadResultStream <- VadResult{Detected: true, Amplitude: amp, Frame: buf}
}
}
}
}()
return vadResultStream
}