]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/speex.c
7851510b4743a48fee08c8f0eba206703faf1d8b
[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 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)
44 {
45 source_ss->format = PA_SAMPLE_S16NE;
46
47 *sink_ss = *source_ss;
48 *sink_map = *source_map;
49 }
50
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)
55 {
56 int framelen, y, rate;
57 uint32_t frame_size_ms, filter_size_ms;
58 pa_modargs *ma;
59
60 if (!(ma = pa_modargs_new(args, valid_modargs))) {
61 pa_log("Failed to parse submodule arguments.");
62 goto fail;
63 }
64
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");
68 goto fail;
69 }
70
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");
74 goto fail;
75 }
76
77 pa_speex_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
78
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);
83 while (y > framelen)
84 y >>= 1;
85 framelen = y;
86
87 *blocksize = framelen * pa_frame_size (source_ss);
88
89 pa_log_debug ("Using framelen %d, blocksize %u, channels %d, rate %d", framelen, *blocksize, source_ss->channels, source_ss->rate);
90
91 ec->params.priv.speex.state = speex_echo_state_init_mc (framelen, (rate * filter_size_ms) / 1000, source_ss->channels, source_ss->channels);
92
93 if (!ec->params.priv.speex.state)
94 goto fail;
95
96 speex_echo_ctl(ec->params.priv.speex.state, SPEEX_ECHO_SET_SAMPLING_RATE, &rate);
97
98 pa_modargs_free(ma);
99 return TRUE;
100
101 fail:
102 if (ma)
103 pa_modargs_free(ma);
104 return FALSE;
105 }
106
107 void pa_speex_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out)
108 {
109 speex_echo_cancellation(ec->params.priv.speex.state, (const spx_int16_t *) rec, (const spx_int16_t *) play, (spx_int16_t *) out);
110 }
111
112 void pa_speex_ec_done(pa_echo_canceller *ec)
113 {
114 speex_echo_state_destroy (ec->params.priv.speex.state);
115 ec->params.priv.speex.state = NULL;
116 }