]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/speex.c
echo-cancel: Pass arguments to the specific canceller module
[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/modargs.h>
29 #include "echo-cancel.h"
30
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
35
36 static const char* const valid_modargs[] = {
37 "frame_size_ms",
38 "filter_size_ms",
39 NULL
40 };
41
42 pa_bool_t pa_speex_ec_init(pa_echo_canceller *ec, pa_sample_spec ss, pa_channel_map map, const char *args)
43 {
44 int framelen, y, rate = ss.rate;
45 uint32_t frame_size_ms, filter_size_ms;
46 pa_modargs *ma;
47
48 if (!(ma = pa_modargs_new(args, valid_modargs))) {
49 pa_log("Failed to parse submodule arguments.");
50 goto fail;
51 }
52
53 filter_size_ms = DEFAULT_FILTER_SIZE_MS;
54 if (pa_modargs_get_value_u32(ma, "filter_size_ms", &filter_size_ms) < 0 || filter_size_ms < 1 || filter_size_ms > 2000) {
55 pa_log("Invalid filter_size_ms specification");
56 goto fail;
57 }
58
59 frame_size_ms = DEFAULT_FRAME_SIZE_MS;
60 if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
61 pa_log("Invalid frame_size_ms specification");
62 goto fail;
63 }
64
65 framelen = (rate * frame_size_ms) / 1000;
66 /* framelen should be a power of 2, round down to nearest power of two */
67 y = 1 << ((8 * sizeof (int)) - 2);
68 while (y > framelen)
69 y >>= 1;
70 framelen = y;
71
72 ec->params.priv.speex.blocksize = framelen * pa_frame_size (&ss);
73
74 pa_log_debug ("Using framelen %d, blocksize %lld, channels %d, rate %d", framelen, (long long) ec->params.priv.speex.blocksize, ss.channels, ss.rate);
75
76 ec->params.priv.speex.state = speex_echo_state_init_mc (framelen, (rate * filter_size_ms) / 1000, ss.channels, ss.channels);
77
78 if (!ec->params.priv.speex.state)
79 goto fail;
80
81 speex_echo_ctl(ec->params.priv.speex.state, SPEEX_ECHO_SET_SAMPLING_RATE, &rate);
82
83 pa_modargs_free(ma);
84 return TRUE;
85
86 fail:
87 if (ma)
88 pa_modargs_free(ma);
89 return FALSE;
90 }
91
92 void pa_speex_ec_run(pa_echo_canceller *ec, uint8_t *rec, uint8_t *play, uint8_t *out)
93 {
94 speex_echo_cancellation(ec->params.priv.speex.state, (const spx_int16_t *) rec, (const spx_int16_t *) play, (spx_int16_t *) out);
95 }
96
97 void pa_speex_ec_done(pa_echo_canceller *ec)
98 {
99 speex_echo_state_destroy (ec->params.priv.speex.state);
100 ec->params.priv.speex.state = NULL;
101 }
102
103 uint32_t pa_speex_ec_get_block_size(pa_echo_canceller *ec)
104 {
105 return ec->params.priv.speex.blocksize;
106 }