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, see <http://www.gnu.org/licenses/>.
24***/
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30#include <pulse/xmalloc.h>
31
32#include <pulsecore/modargs.h>
33
34#include "echo-cancel.h"
35
36/* should be between 10-20 ms */
37#define DEFAULT_FRAME_SIZE_MS 20
38
39static const char* const valid_modargs[] = {
40    "frame_size_ms",
41    NULL
42};
43
44static void pa_adrian_ec_fixate_spec(pa_sample_spec *rec_ss, pa_channel_map *rec_map,
45                                     pa_sample_spec *play_ss, pa_channel_map *play_map,
46                                     pa_sample_spec *out_ss, pa_channel_map *out_map) {
47    out_ss->format = PA_SAMPLE_S16NE;
48    out_ss->channels = 1;
49    pa_channel_map_init_mono(out_map);
50
51    *play_ss = *out_ss;
52    *play_map = *out_map;
53    *rec_ss = *out_ss;
54    *rec_map = *out_map;
55}
56
57bool pa_adrian_ec_init(pa_core *c, pa_echo_canceller *ec,
58                       pa_sample_spec *rec_ss, pa_channel_map *rec_map,
59                       pa_sample_spec *play_ss, pa_channel_map *play_map,
60                       pa_sample_spec *out_ss, pa_channel_map *out_map,
61                       uint32_t *nframes, const char *args) {
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(rec_ss, rec_map, play_ss, play_map, out_ss, out_map);
78
79    rate = out_ss->rate;
80    *nframes = (rate * frame_size_ms) / 1000;
81    ec->params.adrian.blocksize = (*nframes) * pa_frame_size(out_ss);
82
83    pa_log_debug ("Using nframes %d, blocksize %u, channels %d, rate %d", *nframes, ec->params.adrian.blocksize, out_ss->channels, out_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.adrian.aec = AEC_init(rate, have_vector);
90    if (!ec->params.adrian.aec)
91        goto fail;
92
93    pa_modargs_free(ma);
94    return true;
95
96fail:
97    if (ma)
98        pa_modargs_free(ma);
99    return false;
100}
101
102void 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.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.adrian.aec, r, p);
110    }
111}
112
113void pa_adrian_ec_done(pa_echo_canceller *ec) {
114    if (ec->params.adrian.aec) {
115        AEC_done(ec->params.adrian.aec);
116        ec->params.adrian.aec = NULL;
117    }
118}
119