-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathapm.go
More file actions
182 lines (165 loc) · 4.37 KB
/
apm.go
File metadata and controls
182 lines (165 loc) · 4.37 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//go:build console
// Package apm provides Go bindings for the WebRTC Audio Processing Module (APM).
// It supports echo cancellation (AEC3), noise suppression, automatic gain control,
// and high-pass filtering. Audio must be 48kHz int16 PCM in 10ms frames (480 samples/channel).
package apm
// #include "bridge.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
)
type APMConfig struct {
EchoCanceller bool
GainController bool
HighPassFilter bool
NoiseSuppressor bool
CaptureChannels int
RenderChannels int
}
func DefaultConfig() APMConfig {
return APMConfig{
EchoCanceller: true,
GainController: true,
HighPassFilter: true,
NoiseSuppressor: true,
CaptureChannels: 1,
RenderChannels: 1,
}
}
type APM struct {
handle C.ApmHandle
}
func NewAPM(config APMConfig) (*APM, error) {
capCh := config.CaptureChannels
if capCh == 0 {
capCh = 1
}
renCh := config.RenderChannels
if renCh == 0 {
renCh = 1
}
var cerr C.int
handle := C.apm_create(
boolToInt(config.EchoCanceller),
boolToInt(config.GainController),
boolToInt(config.HighPassFilter),
boolToInt(config.NoiseSuppressor),
C.int(capCh),
C.int(renCh),
&cerr,
)
if handle == nil {
return nil, errors.New("apm: failed to create audio processing module")
}
a := &APM{handle: handle}
runtime.SetFinalizer(a, func(a *APM) { a.Close() })
return a, nil
}
// ProcessCapture processes a 10ms capture (microphone) frame in-place.
// samples must contain exactly 480 * numChannels int16 values.
func (a *APM) ProcessCapture(samples []int16) error {
if a.handle == nil {
return errors.New("apm: closed")
}
if len(samples) == 0 {
return nil
}
numChannels := len(samples) / 480
if numChannels == 0 {
numChannels = 1
}
ret := C.apm_process_capture(
a.handle,
(*C.int16_t)(unsafe.Pointer(&samples[0])),
C.int(numChannels),
)
if ret != 0 {
return errors.New("apm: ProcessCapture failed")
}
return nil
}
// ProcessRender processes a 10ms render (speaker/far-end) frame in-place.
// This feeds the echo canceller with the signal being played back.
// samples must contain exactly 480 * numChannels int16 values.
func (a *APM) ProcessRender(samples []int16) error {
if a.handle == nil {
return errors.New("apm: closed")
}
if len(samples) == 0 {
return nil
}
numChannels := len(samples) / 480
if numChannels == 0 {
numChannels = 1
}
ret := C.apm_process_render(
a.handle,
(*C.int16_t)(unsafe.Pointer(&samples[0])),
C.int(numChannels),
)
if ret != 0 {
return errors.New("apm: ProcessRender failed")
}
return nil
}
// SetStreamDelayMs sets the delay in milliseconds between the far-end signal
// being rendered and arriving at the near-end microphone.
func (a *APM) SetStreamDelayMs(ms int) {
if a.handle == nil {
return
}
C.apm_set_stream_delay_ms(a.handle, C.int(ms))
}
func (a *APM) StreamDelayMs() int {
if a.handle == nil {
return 0
}
return int(C.apm_stream_delay_ms(a.handle))
}
// Stats holds AEC statistics from the WebRTC APM.
type Stats struct {
EchoReturnLoss float64 // ERL in dB (higher = more echo removed)
EchoReturnLossEnhancement float64 // ERLE in dB (higher = better cancellation)
DivergentFilterFraction float64 // 0-1, fraction of time filter is divergent
DelayMs int // Estimated echo path delay
ResidualEchoLikelihood float64 // 0-1, likelihood of residual echo
HasERL bool
HasERLE bool
HasDelay bool
HasResidualEcho bool
HasDivergent bool
}
// GetStats returns the current AEC statistics.
func (a *APM) GetStats() Stats {
if a.handle == nil {
return Stats{}
}
var cs C.ApmStats
C.apm_get_stats(a.handle, &cs)
return Stats{
EchoReturnLoss: float64(cs.echo_return_loss),
EchoReturnLossEnhancement: float64(cs.echo_return_loss_enhancement),
DivergentFilterFraction: float64(cs.divergent_filter_fraction),
DelayMs: int(cs.delay_ms),
ResidualEchoLikelihood: float64(cs.residual_echo_likelihood),
HasERL: cs.has_erl != 0,
HasERLE: cs.has_erle != 0,
HasDelay: cs.has_delay != 0,
HasResidualEcho: cs.has_residual_echo != 0,
HasDivergent: cs.has_divergent != 0,
}
}
func (a *APM) Close() {
if a.handle != nil {
C.apm_destroy(a.handle)
a.handle = nil
}
}
func boolToInt(b bool) C.int {
if b {
return 1
}
return 0
}