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/modargs.h>
29 #include "echo-cancel.h"
31 /* should be between 10-20 ms */
32 #define DEFAULT_FRAME_SIZE_MS 20
33 /* should be between 100-500 ms */
34 #define DEFAULT_FILTER_SIZE_MS 200
36 static const char* const valid_modargs
[] = {
42 static void pa_speex_ec_fixate_spec(pa_sample_spec
*source_ss
, pa_channel_map
*source_map
,
43 pa_sample_spec
*sink_ss
, pa_channel_map
*sink_map
)
45 source_ss
->format
= PA_SAMPLE_S16NE
;
47 *sink_ss
= *source_ss
;
48 *sink_map
= *source_map
;
51 pa_bool_t
pa_speex_ec_init(pa_core
*c
, pa_echo_canceller
*ec
,
52 pa_sample_spec
*source_ss
, pa_channel_map
*source_map
,
53 pa_sample_spec
*sink_ss
, pa_channel_map
*sink_map
,
54 uint32_t *blocksize
, const char *args
)
56 int framelen
, y
, rate
;
57 uint32_t frame_size_ms
, filter_size_ms
;
60 if (!(ma
= pa_modargs_new(args
, valid_modargs
))) {
61 pa_log("Failed to parse submodule arguments.");
65 filter_size_ms
= DEFAULT_FILTER_SIZE_MS
;
66 if (pa_modargs_get_value_u32(ma
, "filter_size_ms", &filter_size_ms
) < 0 || filter_size_ms
< 1 || filter_size_ms
> 2000) {
67 pa_log("Invalid filter_size_ms specification");
71 frame_size_ms
= DEFAULT_FRAME_SIZE_MS
;
72 if (pa_modargs_get_value_u32(ma
, "frame_size_ms", &frame_size_ms
) < 0 || frame_size_ms
< 1 || frame_size_ms
> 200) {
73 pa_log("Invalid frame_size_ms specification");
77 pa_speex_ec_fixate_spec(source_ss
, source_map
, sink_ss
, sink_map
);
79 rate
= source_ss
->rate
;
80 framelen
= (rate
* frame_size_ms
) / 1000;
81 /* framelen should be a power of 2, round down to nearest power of two */
82 y
= 1 << ((8 * sizeof (int)) - 2);
87 *blocksize
= framelen
* pa_frame_size (source_ss
);
89 pa_log_debug ("Using framelen %d, blocksize %u, channels %d, rate %d", framelen
, *blocksize
, source_ss
->channels
, source_ss
->rate
);
91 ec
->params
.priv
.speex
.state
= speex_echo_state_init_mc (framelen
, (rate
* filter_size_ms
) / 1000, source_ss
->channels
, source_ss
->channels
);
93 if (!ec
->params
.priv
.speex
.state
)
96 speex_echo_ctl(ec
->params
.priv
.speex
.state
, SPEEX_ECHO_SET_SAMPLING_RATE
, &rate
);
107 void pa_speex_ec_run(pa_echo_canceller
*ec
, const uint8_t *rec
, const uint8_t *play
, uint8_t *out
) {
108 speex_echo_cancellation(ec
->params
.priv
.speex
.state
, (const spx_int16_t
*) rec
, (const spx_int16_t
*) play
, (spx_int16_t
*) out
);
111 void pa_speex_ec_done(pa_echo_canceller
*ec
) {
112 speex_echo_state_destroy(ec
->params
.priv
.speex
.state
);
113 ec
->params
.priv
.speex
.state
= NULL
;