Remove libatomic_ops dependency
[profile/ivi/pulseaudio-panda.git] / 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 *blocksize, const char *args)
61 {
62     int framelen, 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     framelen = (rate * frame_size_ms) / 1000;
81
82     *blocksize = ec->params.priv.adrian.blocksize = framelen * pa_frame_size (source_ss);
83
84     pa_log_debug ("Using framelen %d, blocksize %u, channels %d, rate %d", framelen, ec->params.priv.adrian.blocksize, source_ss->channels, source_ss->rate);
85
86     /* For now we only support SSE */
87     if (c->cpu_info.cpu_type == PA_CPU_X86 && (c->cpu_info.flags.x86 & PA_CPU_X86_SSE))
88         have_vector = 1;
89
90     ec->params.priv.adrian.aec = AEC_init(rate, have_vector);
91     if (!ec->params.priv.adrian.aec)
92         goto fail;
93
94     pa_modargs_free(ma);
95     return TRUE;
96
97 fail:
98     if (ma)
99         pa_modargs_free(ma);
100     return FALSE;
101 }
102
103 void pa_adrian_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
104     unsigned int i;
105
106     for (i = 0; i < ec->params.priv.adrian.blocksize; i += 2) {
107         /* We know it's S16NE mono data */
108         int r = *(int16_t *)(rec + i);
109         int p = *(int16_t *)(play + i);
110         *(int16_t *)(out + i) = (int16_t) AEC_doAEC(ec->params.priv.adrian.aec, r, p);
111     }
112 }
113
114 void pa_adrian_ec_done(pa_echo_canceller *ec) {
115     pa_xfree(ec->params.priv.adrian.aec);
116     ec->params.priv.adrian.aec = NULL;
117 }