2 This file is part of PulseAudio.
4 Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
6 Contributor: Arun Raghavan <arun.raghavan@collabora.co.uk>
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.
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.
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
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/modargs.h>
30 #include "echo-cancel.h"
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
41 static const char* const valid_modargs[] = {
47 "echo_suppress_attenuation",
48 "echo_suppress_attenuation_active",
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)
55 source_ss->format = PA_SAMPLE_S16NE;
57 *sink_ss = *source_ss;
58 *sink_map = *source_map;
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) {
64 pa_bool_t echo_suppress;
65 int32_t echo_suppress_attenuation;
66 int32_t echo_suppress_attenuation_active;
68 agc = DEFAULT_AGC_ENABLED;
69 if (pa_modargs_get_value_boolean(ma, "agc", &agc) < 0) {
70 pa_log("Failed to parse agc value");
74 denoise = DEFAULT_DENOISE_ENABLED;
75 if (pa_modargs_get_value_boolean(ma, "denoise", &denoise) < 0) {
76 pa_log("Failed to parse denoise value");
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");
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");
91 if (echo_suppress_attenuation > 0) {
92 pa_log("echo_suppress_attenuation should be a negative dB value");
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");
101 if (echo_suppress_attenuation_active > 0) {
102 pa_log("echo_suppress_attenuation_active should be a negative dB value");
106 if (agc || denoise || echo_suppress) {
109 if (source_ss->channels != 1) {
110 pa_log("AGC, denoising and echo suppression only work with channels=1");
114 ec->params.priv.speex.pp_state = speex_preprocess_state_init(nframes, source_ss->rate);
117 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_AGC, &tmp);
120 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_DENOISE, &tmp);
123 if (echo_suppress_attenuation)
124 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS,
125 &echo_suppress_attenuation);
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);
132 speex_preprocess_ctl(ec->params.priv.speex.pp_state, SPEEX_PREPROCESS_SET_ECHO_STATE,
133 ec->params.priv.speex.state);
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));
139 pa_log_info("All preprocessing options are disabled");
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)
154 uint32_t frame_size_ms, filter_size_ms;
157 if (!(ma = pa_modargs_new(args, valid_modargs))) {
158 pa_log("Failed to parse submodule arguments.");
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");
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");
174 pa_speex_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
176 rate = source_ss->rate;
177 *nframes = pa_echo_canceller_blocksize_power2(rate, frame_size_ms);
179 pa_log_debug ("Using nframes %d, channels %d, rate %d", *nframes, source_ss->channels, source_ss->rate);
180 ec->params.priv.speex.state = speex_echo_state_init_mc(*nframes, (rate * filter_size_ms) / 1000, source_ss->channels, source_ss->channels);
182 if (!ec->params.priv.speex.state)
185 speex_echo_ctl(ec->params.priv.speex.state, SPEEX_ECHO_SET_SAMPLING_RATE, &rate);
187 if (!pa_speex_ec_preprocessor_init(ec, source_ss, *nframes, ma))
196 if (ec->params.priv.speex.pp_state) {
197 speex_preprocess_state_destroy(ec->params.priv.speex.pp_state);
198 ec->params.priv.speex.pp_state = NULL;
200 if (ec->params.priv.speex.state) {
201 speex_echo_state_destroy(ec->params.priv.speex.state);
202 ec->params.priv.speex.state = NULL;
207 void pa_speex_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
208 speex_echo_cancellation(ec->params.priv.speex.state, (const spx_int16_t *) rec, (const spx_int16_t *) play,
209 (spx_int16_t *) out);
211 /* preprecessor is run after AEC. This is not a mistake! */
212 if (ec->params.priv.speex.pp_state)
213 speex_preprocess_run(ec->params.priv.speex.pp_state, (spx_int16_t *) out);
216 void pa_speex_ec_done(pa_echo_canceller *ec) {
217 if (ec->params.priv.speex.pp_state) {
218 speex_preprocess_state_destroy(ec->params.priv.speex.pp_state);
219 ec->params.priv.speex.pp_state = NULL;
222 if (ec->params.priv.speex.state) {
223 speex_echo_state_destroy(ec->params.priv.speex.state);
224 ec->params.priv.speex.state = NULL;