6c532f26745879d463c10a8fcf955f589b826b63
[platform/upstream/pulseaudio.git] / 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 *source_ss, pa_channel_map *source_map,
53                                     pa_sample_spec *sink_ss, pa_channel_map *sink_map)
54 {
55     source_ss->format = PA_SAMPLE_S16NE;
56
57     *sink_ss = *source_ss;
58     *sink_map = *source_map;
59 }
60
61 static pa_bool_t pa_speex_ec_preprocessor_init(pa_echo_canceller *ec, pa_sample_spec *source_ss, uint32_t nframes, pa_modargs *ma) {
62     pa_bool_t agc;
63     pa_bool_t denoise;
64     pa_bool_t echo_suppress;
65     int32_t echo_suppress_attenuation;
66     int32_t echo_suppress_attenuation_active;
67
68     agc = DEFAULT_AGC_ENABLED;
69     if (pa_modargs_get_value_boolean(ma, "agc", &agc) < 0) {
70         pa_log("Failed to parse agc value");
71         goto fail;
72     }
73
74     denoise = DEFAULT_DENOISE_ENABLED;
75     if (pa_modargs_get_value_boolean(ma, "denoise", &denoise) < 0) {
76         pa_log("Failed to parse denoise value");
77         goto fail;
78     }
79
80     echo_suppress = DEFAULT_ECHO_SUPPRESS_ENABLED;
81     if (pa_modargs_get_value_boolean(ma, "echo_suppress", &echo_suppress) < 0) {
82         pa_log("Failed to parse echo_suppress value");
83         goto fail;
84     }
85
86     echo_suppress_attenuation = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
87     if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation", &echo_suppress_attenuation) < 0) {
88         pa_log("Failed to parse echo_suppress_attenuation value");
89         goto fail;
90     }
91     if (echo_suppress_attenuation > 0) {
92         pa_log("echo_suppress_attenuation should be a negative dB value");
93         goto fail;
94     }
95
96     echo_suppress_attenuation_active = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
97     if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation_active", &echo_suppress_attenuation_active) < 0) {
98         pa_log("Failed to parse echo_suppress_attenuation_active value");
99         goto fail;
100     }
101     if (echo_suppress_attenuation_active > 0) {
102         pa_log("echo_suppress_attenuation_active should be a negative dB value");
103         goto fail;
104     }
105
106     if (agc || denoise || echo_suppress) {
107         spx_int32_t tmp;
108
109         if (source_ss->channels != 1) {
110             pa_log("AGC, denoising and echo suppression only work with channels=1");
111             goto fail;
112         }
113
114         ec->params.priv.speex.pp_state = speex_preprocess_state_init(nframes, source_ss->rate);
115
116         tmp = agc;
117         speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_AGC, &tmp);
118
119         tmp = denoise;
120         speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_DENOISE, &tmp);
121
122         if (echo_suppress) {
123             if (echo_suppress_attenuation)
124                 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS,
125                                      &echo_suppress_attenuation);
126
127             if (echo_suppress_attenuation_active) {
128                 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS_ACTIVE,
129                                      &echo_suppress_attenuation_active);
130             }
131
132             speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_STATE,
133                                  ec->params.priv.speex.state);
134         }
135
136         pa_log_info("Loaded speex preprocessor with params: agc=%s, denoise=%s, echo_suppress=%s", pa_yes_no(agc),
137                     pa_yes_no(denoise), pa_yes_no(echo_suppress));
138     } else
139         pa_log_info("All preprocessing options are disabled");
140
141     return TRUE;
142
143 fail:
144     return FALSE;
145 }
146
147
148 pa_bool_t pa_speex_ec_init(pa_core *c, pa_echo_canceller *ec,
149                            pa_sample_spec *source_ss, pa_channel_map *source_map,
150                            pa_sample_spec *sink_ss, pa_channel_map *sink_map,
151                            uint32_t *nframes, const char *args)
152 {
153     int rate;
154     uint32_t y, frame_size_ms, filter_size_ms;
155     pa_modargs *ma;
156
157     if (!(ma = pa_modargs_new(args, valid_modargs))) {
158         pa_log("Failed to parse submodule arguments.");
159         goto fail;
160     }
161
162     filter_size_ms = DEFAULT_FILTER_SIZE_MS;
163     if (pa_modargs_get_value_u32(ma, "filter_size_ms", &filter_size_ms) < 0 || filter_size_ms < 1 || filter_size_ms > 2000) {
164         pa_log("Invalid filter_size_ms specification");
165         goto fail;
166     }
167
168     frame_size_ms = DEFAULT_FRAME_SIZE_MS;
169     if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
170         pa_log("Invalid frame_size_ms specification");
171         goto fail;
172     }
173
174     pa_speex_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
175
176     rate = source_ss->rate;
177     *nframes = (rate * frame_size_ms) / 1000;
178     /* nframes should be a power of 2, round down to nearest power of two */
179     y = 1 << ((8 * sizeof (uint32_t)) - 2);
180     while (y > *nframes)
181       y >>= 1;
182     *nframes = y;
183
184     pa_log_debug ("Using nframes %d, channels %d, rate %d", *nframes, source_ss->channels, source_ss->rate);
185
186     ec->params.priv.speex.state = speex_echo_state_init_mc (*nframes, (rate * filter_size_ms) / 1000, source_ss->channels, source_ss->channels);
187
188     if (!ec->params.priv.speex.state)
189         goto fail;
190
191     speex_echo_ctl(ec->params.priv.speex.state, SPEEX_ECHO_SET_SAMPLING_RATE, &rate);
192
193     if (!pa_speex_ec_preprocessor_init(ec, source_ss, *nframes, ma))
194         goto fail;
195
196     pa_modargs_free(ma);
197     return TRUE;
198
199 fail:
200     if (ma)
201         pa_modargs_free(ma);
202     if (ec->params.priv.speex.pp_state) {
203         speex_preprocess_state_destroy(ec->params.priv.speex.pp_state);
204         ec->params.priv.speex.pp_state = NULL;
205     }
206     if (ec->params.priv.speex.state) {
207         speex_echo_state_destroy(ec->params.priv.speex.state);
208         ec->params.priv.speex.state = NULL;
209     }
210     return FALSE;
211 }
212
213 void pa_speex_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
214     speex_echo_cancellation(ec->params.priv.speex.state, (const spx_int16_t *) rec, (const spx_int16_t *) play,
215                             (spx_int16_t *) out);
216
217     /* preprecessor is run after AEC. This is not a mistake! */
218     if (ec->params.priv.speex.pp_state)
219         speex_preprocess_run(ec->params.priv.speex.pp_state, (spx_int16_t *) out);
220 }
221
222 void pa_speex_ec_done(pa_echo_canceller *ec) {
223     if (ec->params.priv.speex.pp_state) {
224         speex_preprocess_state_destroy(ec->params.priv.speex.pp_state);
225         ec->params.priv.speex.pp_state = NULL;
226     }
227
228     if (ec->params.priv.speex.state) {
229         speex_echo_state_destroy(ec->params.priv.speex.state);
230         ec->params.priv.speex.state = NULL;
231     }
232 }