]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/speex.c
cef0aad0f3eb54359114321f7b8454e818f55c67
[pulseaudio] / src / modules / echo-cancel / speex.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
5
6 Contributor: Arun Raghavan <arun.raghavan@collabora.co.uk>
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2.1 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/modargs.h>
30 #include "echo-cancel.h"
31
32 /* should be between 10-20 ms */
33 #define DEFAULT_FRAME_SIZE_MS 20
34 /* should be between 100-500 ms */
35 #define DEFAULT_FILTER_SIZE_MS 200
36 #define DEFAULT_AGC_ENABLED TRUE
37 #define DEFAULT_DENOISE_ENABLED TRUE
38 #define DEFAULT_ECHO_SUPPRESS_ENABLED TRUE
39 #define DEFAULT_ECHO_SUPPRESS_ATTENUATION 0
40
41 static const char* const valid_modargs[] = {
42 "frame_size_ms",
43 "filter_size_ms",
44 "agc",
45 "denoise",
46 "echo_suppress",
47 "echo_suppress_attenuation",
48 "echo_suppress_attenuation_active",
49 NULL
50 };
51
52 static void pa_speex_ec_fixate_spec(pa_sample_spec *rec_ss, pa_channel_map *rec_map,
53 pa_sample_spec *play_ss, pa_channel_map *play_map,
54 pa_sample_spec *out_ss, pa_channel_map *out_map) {
55 out_ss->format = PA_SAMPLE_S16NE;
56
57 *play_ss = *out_ss;
58 *play_map = *out_map;
59 *rec_ss = *out_ss;
60 *rec_map = *out_map;
61 }
62
63 static pa_bool_t pa_speex_ec_preprocessor_init(pa_echo_canceller *ec, pa_sample_spec *out_ss, uint32_t nframes, pa_modargs *ma) {
64 pa_bool_t agc;
65 pa_bool_t denoise;
66 pa_bool_t echo_suppress;
67 int32_t echo_suppress_attenuation;
68 int32_t echo_suppress_attenuation_active;
69
70 agc = DEFAULT_AGC_ENABLED;
71 if (pa_modargs_get_value_boolean(ma, "agc", &agc) < 0) {
72 pa_log("Failed to parse agc value");
73 goto fail;
74 }
75
76 denoise = DEFAULT_DENOISE_ENABLED;
77 if (pa_modargs_get_value_boolean(ma, "denoise", &denoise) < 0) {
78 pa_log("Failed to parse denoise value");
79 goto fail;
80 }
81
82 echo_suppress = DEFAULT_ECHO_SUPPRESS_ENABLED;
83 if (pa_modargs_get_value_boolean(ma, "echo_suppress", &echo_suppress) < 0) {
84 pa_log("Failed to parse echo_suppress value");
85 goto fail;
86 }
87
88 echo_suppress_attenuation = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
89 if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation", &echo_suppress_attenuation) < 0) {
90 pa_log("Failed to parse echo_suppress_attenuation value");
91 goto fail;
92 }
93 if (echo_suppress_attenuation > 0) {
94 pa_log("echo_suppress_attenuation should be a negative dB value");
95 goto fail;
96 }
97
98 echo_suppress_attenuation_active = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
99 if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation_active", &echo_suppress_attenuation_active) < 0) {
100 pa_log("Failed to parse echo_suppress_attenuation_active value");
101 goto fail;
102 }
103 if (echo_suppress_attenuation_active > 0) {
104 pa_log("echo_suppress_attenuation_active should be a negative dB value");
105 goto fail;
106 }
107
108 if (agc || denoise || echo_suppress) {
109 spx_int32_t tmp;
110
111 if (out_ss->channels != 1) {
112 pa_log("AGC, denoising and echo suppression only work with channels=1");
113 goto fail;
114 }
115
116 ec->params.priv.speex.pp_state = speex_preprocess_state_init(nframes, out_ss->rate);
117
118 tmp = agc;
119 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_AGC, &tmp);
120
121 tmp = denoise;
122 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_DENOISE, &tmp);
123
124 if (echo_suppress) {
125 if (echo_suppress_attenuation)
126 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS,
127 &echo_suppress_attenuation);
128
129 if (echo_suppress_attenuation_active) {
130 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS_ACTIVE,
131 &echo_suppress_attenuation_active);
132 }
133
134 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_STATE,
135 ec->params.priv.speex.state);
136 }
137
138 pa_log_info("Loaded speex preprocessor with params: agc=%s, denoise=%s, echo_suppress=%s", pa_yes_no(agc),
139 pa_yes_no(denoise), pa_yes_no(echo_suppress));
140 } else
141 pa_log_info("All preprocessing options are disabled");
142
143 return TRUE;
144
145 fail:
146 return FALSE;
147 }
148
149
150 pa_bool_t pa_speex_ec_init(pa_core *c, pa_echo_canceller *ec,
151 pa_sample_spec *rec_ss, pa_channel_map *rec_map,
152 pa_sample_spec *play_ss, pa_channel_map *play_map,
153 pa_sample_spec *out_ss, pa_channel_map *out_map,
154 uint32_t *nframes, const char *args) {
155 int rate;
156 uint32_t frame_size_ms, filter_size_ms;
157 pa_modargs *ma;
158
159 if (!(ma = pa_modargs_new(args, valid_modargs))) {
160 pa_log("Failed to parse submodule arguments.");
161 goto fail;
162 }
163
164 filter_size_ms = DEFAULT_FILTER_SIZE_MS;
165 if (pa_modargs_get_value_u32(ma, "filter_size_ms", &filter_size_ms) < 0 || filter_size_ms < 1 || filter_size_ms > 2000) {
166 pa_log("Invalid filter_size_ms specification");
167 goto fail;
168 }
169
170 frame_size_ms = DEFAULT_FRAME_SIZE_MS;
171 if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
172 pa_log("Invalid frame_size_ms specification");
173 goto fail;
174 }
175
176 pa_speex_ec_fixate_spec(rec_ss, rec_map, play_ss, play_map, out_ss, out_map);
177
178 rate = out_ss->rate;
179 *nframes = pa_echo_canceller_blocksize_power2(rate, frame_size_ms);
180
181 pa_log_debug ("Using nframes %d, channels %d, rate %d", *nframes, out_ss->channels, out_ss->rate);
182 ec->params.priv.speex.state = speex_echo_state_init_mc(*nframes, (rate * filter_size_ms) / 1000, out_ss->channels, out_ss->channels);
183
184 if (!ec->params.priv.speex.state)
185 goto fail;
186
187 speex_echo_ctl(ec->params.priv.speex.state, SPEEX_ECHO_SET_SAMPLING_RATE, &rate);
188
189 if (!pa_speex_ec_preprocessor_init(ec, out_ss, *nframes, ma))
190 goto fail;
191
192 pa_modargs_free(ma);
193 return TRUE;
194
195 fail:
196 if (ma)
197 pa_modargs_free(ma);
198 if (ec->params.priv.speex.pp_state) {
199 speex_preprocess_state_destroy(ec->params.priv.speex.pp_state);
200 ec->params.priv.speex.pp_state = NULL;
201 }
202 if (ec->params.priv.speex.state) {
203 speex_echo_state_destroy(ec->params.priv.speex.state);
204 ec->params.priv.speex.state = NULL;
205 }
206 return FALSE;
207 }
208
209 void pa_speex_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
210 speex_echo_cancellation(ec->params.priv.speex.state, (const spx_int16_t *) rec, (const spx_int16_t *) play,
211 (spx_int16_t *) out);
212
213 /* preprecessor is run after AEC. This is not a mistake! */
214 if (ec->params.priv.speex.pp_state)
215 speex_preprocess_run(ec->params.priv.speex.pp_state, (spx_int16_t *) out);
216 }
217
218 void pa_speex_ec_done(pa_echo_canceller *ec) {
219 if (ec->params.priv.speex.pp_state) {
220 speex_preprocess_state_destroy(ec->params.priv.speex.pp_state);
221 ec->params.priv.speex.pp_state = NULL;
222 }
223
224 if (ec->params.priv.speex.state) {
225 speex_echo_state_destroy(ec->params.priv.speex.state);
226 ec->params.priv.speex.state = NULL;
227 }
228 }