-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocessor.go
81 lines (63 loc) · 1.77 KB
/
preprocessor.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
72
73
74
75
76
77
78
79
80
81
package speexdsp
/*
#cgo pkg-config: speexdsp
#include <speex/speex_preprocess.h>
*/
import "C"
import "unsafe"
type Preprocessor struct {
state *C.SpeexPreprocessState
sampleRate int
frameSize int
}
func FrameSize(frameSizeMs, sampleRate int) int {
return frameSizeMs * sampleRate / 1000
}
func NewPreprocessor(sampleRate, frameSize int) *Preprocessor {
state := C.speex_preprocess_state_init(C.int(frameSize), C.int(sampleRate))
return &Preprocessor{
state: state,
sampleRate: sampleRate,
frameSize: frameSize,
}
}
func (p *Preprocessor) Close() error {
C.speex_preprocess_state_destroy(p.state)
return nil
}
func (p *Preprocessor) SampleRate() int {
return p.sampleRate
}
func (p *Preprocessor) FrameSize() int {
return p.frameSize
}
// Run returns true if voice detected (only if VAD enabled)
func (p *Preprocessor) Run(buf []int16) bool {
pbuf := (*C.spx_int16_t)(&buf[0])
return 1 == C.speex_preprocess_run(p.state, pbuf)
}
func (p *Preprocessor) SetEchoCanceller(ec *EchoCanceller) {
ecState := unsafe.Pointer(nil)
if ec != nil {
ecState = unsafe.Pointer(ec.state)
}
C.speex_preprocess_ctl(p.state, C.SPEEX_PREPROCESS_SET_ECHO_STATE, ecState)
}
func (p *Preprocessor) EnableDenoise(enable bool) {
denoise := 0
if enable {
denoise = 1
}
C.speex_preprocess_ctl(p.state, C.SPEEX_PREPROCESS_SET_DENOISE, unsafe.Pointer(&denoise))
}
func (p *Preprocessor) EnableVAD(enable bool) {
var vad int = 0
if enable {
vad = 1
}
C.speex_preprocess_ctl(p.state, C.SPEEX_PREPROCESS_SET_VAD, unsafe.Pointer(&vad))
}
// SetNoiceSupress set decibel value of the maximum attenuation of the noise (default is -15)
func (p *Preprocessor) SetNoiceSupress(value int32) {
C.speex_preprocess_ctl(p.state, C.SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, unsafe.Pointer(&value))
}