]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/adrian.c
40e9654dc439545356b9cc7fd961b789e3530436
[pulseaudio] / src / modules / echo-cancel / adrian.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2010 Arun Raghavan <arun.raghavan@collabora.co.uk>
5
6 Contributor: Wim Taymans <wim.taymans@gmail.com>
7
8 The actual implementation is taken from the sources at
9 http://andreadrian.de/intercom/ - for the license, look for
10 adrian-license.txt in the same directory as this file.
11
12 PulseAudio is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published
14 by the Free Software Foundation; either version 2.1 of the License,
15 or (at your option) any later version.
16
17 PulseAudio is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU Lesser General Public License
23 along with PulseAudio; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 USA.
26 ***/
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/modargs.h>
35
36 #include "echo-cancel.h"
37
38 /* should be between 10-20 ms */
39 #define DEFAULT_FRAME_SIZE_MS 20
40
41 static const char* const valid_modargs[] = {
42 "frame_size_ms",
43 NULL
44 };
45
46 static void pa_adrian_ec_fixate_spec(pa_sample_spec *source_ss, pa_channel_map *source_map,
47 pa_sample_spec *sink_ss, pa_channel_map *sink_map)
48 {
49 source_ss->format = PA_SAMPLE_S16NE;
50 source_ss->channels = 1;
51 pa_channel_map_init_mono(source_map);
52
53 *sink_ss = *source_ss;
54 *sink_map = *source_map;
55 }
56
57 pa_bool_t pa_adrian_ec_init(pa_core *c, pa_echo_canceller *ec,
58 pa_sample_spec *source_ss, pa_channel_map *source_map,
59 pa_sample_spec *sink_ss, pa_channel_map *sink_map,
60 uint32_t *nframes, const char *args)
61 {
62 int rate, have_vector = 0;
63 uint32_t frame_size_ms;
64 pa_modargs *ma;
65
66 if (!(ma = pa_modargs_new(args, valid_modargs))) {
67 pa_log("Failed to parse submodule arguments.");
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_adrian_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
78
79 rate = source_ss->rate;
80 *nframes = (rate * frame_size_ms) / 1000;
81 ec->params.priv.adrian.blocksize = (*nframes) * pa_frame_size(source_ss);
82
83 pa_log_debug ("Using nframes %d, blocksize %u, channels %d, rate %d", *nframes, ec->params.priv.adrian.blocksize, source_ss->channels, source_ss->rate);
84
85 /* For now we only support SSE */
86 if (c->cpu_info.cpu_type == PA_CPU_X86 && (c->cpu_info.flags.x86 & PA_CPU_X86_SSE))
87 have_vector = 1;
88
89 ec->params.priv.adrian.aec = AEC_init(rate, have_vector);
90 if (!ec->params.priv.adrian.aec)
91 goto fail;
92
93 pa_modargs_free(ma);
94 return TRUE;
95
96 fail:
97 if (ma)
98 pa_modargs_free(ma);
99 return FALSE;
100 }
101
102 void pa_adrian_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
103 unsigned int i;
104
105 for (i = 0; i < ec->params.priv.adrian.blocksize; i += 2) {
106 /* We know it's S16NE mono data */
107 int r = *(int16_t *)(rec + i);
108 int p = *(int16_t *)(play + i);
109 *(int16_t *)(out + i) = (int16_t) AEC_doAEC(ec->params.priv.adrian.aec, r, p);
110 }
111 }
112
113 void pa_adrian_ec_done(pa_echo_canceller *ec) {
114 if (ec->params.priv.adrian.aec) {
115 AEC_done(ec->params.priv.adrian.aec);
116 ec->params.priv.adrian.aec = NULL;
117 }
118 }