-
Notifications
You must be signed in to change notification settings - Fork 3
/
vad.go
67 lines (59 loc) · 1.7 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
package webrtcvad
// #cgo linux CPPFLAGS: -DWEBRTC_POSIX
// #cgo darwin CPPFLAGS: -DWEBRTC_POSIX
// #cgo windows CPPFLAGS: -DWEBRTC_WIN
// #cgo CPPFLAGS: -I${SRCDIR}/webrtc_lkgr
// #cgo CXXFLAGS: -std=c++11
// #include "webrtc_lkgr/common_audio/vad/webrtc_vad.c"
import "C"
import (
"errors"
"unsafe"
)
type VadInst *C.struct_WebRtcVadInst
// Create Creates an instance to the VAD structure.
func Create() VadInst {
return VadInst(C.WebRtcVad_Create())
}
// Free Frees the dynamic memory of a specified VAD instance.
func Free(vadInst VadInst) {
C.WebRtcVad_Free(vadInst)
}
// Init Initializes a VAD instance.
func Init(vadInst VadInst) (err error) {
result := C.WebRtcVad_Init(vadInst)
if result == -1 {
err = errors.New("null pointer or Default mode could not be set")
}
return
}
// SetMode Sets the VAD operating mode.
func SetMode(vadInst VadInst, mode int) (err error) {
result := C.WebRtcVad_set_mode(vadInst, C.int(mode))
if result == -1 {
err = errors.New("mode could not be set or the VAD instance has not been initialized")
}
return
}
// Process Sets the VAD operating mode.
func Process(vadInst VadInst, fs int, audioFrame []byte, frameLength int) (active bool, err error) {
result := C.WebRtcVad_Process(vadInst, C.int(fs), (*C.short)(unsafe.Pointer(&audioFrame[0])), C.size_t(frameLength))
if result == 1 {
active = true
} else if result == 0 {
active = false
} else {
err = errors.New("process fail")
}
return
}
// ValidRateAndFrameLength Sets the VAD operating mode.
func ValidRateAndFrameLength(rate int, frameLength int) (valid bool) {
result := C.WebRtcVad_ValidRateAndFrameLength(C.int(rate), C.size_t(frameLength))
if result == 0 {
valid = true
} else {
valid = false
}
return
}