]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/adrian.c
08df2edd4c869bf19657142284a7d67c126b02bf
[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 <pulsecore/modargs.h>
33 #include <pulsecore/endianmacros.h>
34 #include "echo-cancel.h"
35
36 /* should be between 10-20 ms */
37 #define DEFAULT_FRAME_SIZE_MS 20
38
39 static const char* const valid_modargs[] = {
40 "frame_size_ms",
41 NULL
42 };
43
44 static void pa_adrian_ec_fixate_spec(pa_sample_spec *source_ss, pa_channel_map *source_map,
45 pa_sample_spec *sink_ss, pa_channel_map *sink_map)
46 {
47 source_ss->format = PA_SAMPLE_S16NE;
48 source_ss->channels = 1;
49 pa_channel_map_init_mono(source_map);
50
51 *sink_ss = *source_ss;
52 *sink_map = *source_map;
53 }
54
55 pa_bool_t pa_adrian_ec_init(pa_core *c, pa_echo_canceller *ec,
56 pa_sample_spec *source_ss, pa_channel_map *source_map,
57 pa_sample_spec *sink_ss, pa_channel_map *sink_map,
58 uint32_t *blocksize, const char *args)
59 {
60 int framelen, rate, have_vector = 0;
61 uint32_t frame_size_ms;
62 pa_modargs *ma;
63
64 if (!(ma = pa_modargs_new(args, valid_modargs))) {
65 pa_log("Failed to parse submodule arguments.");
66 goto fail;
67 }
68
69 frame_size_ms = DEFAULT_FRAME_SIZE_MS;
70 if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
71 pa_log("Invalid frame_size_ms specification");
72 goto fail;
73 }
74
75 pa_adrian_ec_fixate_spec(source_ss, source_map, sink_ss, sink_map);
76
77 rate = source_ss->rate;
78 framelen = (rate * frame_size_ms) / 1000;
79
80 *blocksize = ec->params.priv.adrian.blocksize = framelen * pa_frame_size (source_ss);
81
82 pa_log_debug ("Using framelen %d, blocksize %u, channels %d, rate %d", framelen, ec->params.priv.adrian.blocksize, source_ss->channels, source_ss->rate);
83
84 /* For now we only support SSE */
85 if (c->cpu_info.cpu_type == PA_CPU_X86 && (c->cpu_info.flags.x86 & PA_CPU_X86_SSE))
86 have_vector = 1;
87
88 ec->params.priv.adrian.aec = AEC_init(rate, have_vector);
89 if (!ec->params.priv.adrian.aec)
90 goto fail;
91
92 pa_modargs_free(ma);
93 return TRUE;
94
95 fail:
96 if (ma)
97 pa_modargs_free(ma);
98 return FALSE;
99 }
100
101 void pa_adrian_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
102 unsigned int i;
103
104 for (i = 0; i < ec->params.priv.adrian.blocksize; i += 2) {
105 /* We know it's S16NE mono data */
106 int r = *(int16_t *)(rec + i);
107 int p = *(int16_t *)(play + i);
108 *(int16_t *)(out + i) = (int16_t) AEC_doAEC(ec->params.priv.adrian.aec, r, p);
109 }
110 }
111
112 void pa_adrian_ec_done(pa_echo_canceller *ec) {
113 pa_xfree(ec->params.priv.adrian.aec);
114 ec->params.priv.adrian.aec = NULL;
115 }