-
Notifications
You must be signed in to change notification settings - Fork 0
/
vad.go
71 lines (57 loc) · 1.4 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
59
60
61
62
63
64
65
66
67
68
69
70
71
package webrtcvad
import "C"
//#cgo darwin CFLAGS: -I.
//#include "webrtc/common_audio/vad/include/webrtc_vad.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
)
func New() (*VAD, error) {
var inst *C.struct_WebRtcVadInst
inst = C.WebRtcVad_Create()
vad := &VAD{inst}
runtime.SetFinalizer(vad, free)
ret := C.WebRtcVad_Init(inst)
if ret != 0 {
return nil, errors.New("default mode could not be set")
}
return vad, nil
}
func free(vad *VAD) {
C.WebRtcVad_Free(vad.inst)
}
type VAD struct {
inst *C.struct_WebRtcVadInst
}
func (v *VAD) SetMode(mode int) error {
ret := C.WebRtcVad_set_mode(v.inst, C.int(mode))
if ret != 0 {
return errors.New("mode could not be set")
}
return nil
}
func (v *VAD) Process(fs int, audioFrame []byte) (activeVoice bool, err error) {
if len(audioFrame)%2 != 0 {
return false, errors.New("audio frames must be 16bit little endian unsigned integers")
}
audioFramePtr := (*C.int16_t)(unsafe.Pointer(&audioFrame[0]))
frameLen := (C.size_t)(len(audioFrame) / 2)
ret := C.WebRtcVad_Process(v.inst, C.int(fs), audioFramePtr, frameLen)
switch ret {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, errors.New("processing error")
}
}
func (v *VAD) ValidRateAndFrameLength(rate int, frameLength int) bool {
ret := C.WebRtcVad_ValidRateAndFrameLength(C.int(rate), C.size_t(frameLength))
if ret < 0 {
return false
}
return true
}