Debian Codegraph @

speex/pcm_speex.c

raw symbols SCIP
1 /*
2 * Speex DSP plugin
3 *
4 * Copyright (c) 2009 by Takashi Iwai <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22 #include <alsa/asoundlib.h>
23 #include <alsa/pcm_external.h>
24 #include <speex/speex_preprocess.h>
25 #include <speex/speex_echo.h>
26
27 /* DSP parameters */
28 struct spx_parms {
29 int frames;
30 int denoise;
31 int agc;
32 int echo;
33 int filter_length;
34 float agc_level;
35 int dereverb;
36 float dereverb_decay;
37 float dereverb_level;
38 };
39
40 typedef struct {
41 snd_pcm_extplug_t ext;
42 struct spx_parms parms;
43 /* instance and intermedate buffer */
44 SpeexPreprocessState *state;
45 SpeexEchoState *echo_state;
46 short *buf;
47 short *outbuf;
48 /* running states */
49 unsigned int filled;
50 unsigned int processed;
51 } snd_pcm_speex_t;
52
53
54 static inline void *area_addr(const snd_pcm_channel_area_t *area,
55 snd_pcm_uframes_t offset)
56 {
57 unsigned int bitofs = area->first + area->step * offset;
58 return (char *) area->addr + bitofs / 8;
59 }
60
61 static snd_pcm_sframes_t
62 spx_transfer(snd_pcm_extplug_t *ext,
63 const snd_pcm_channel_area_t *dst_areas,
64 snd_pcm_uframes_t dst_offset,
65 const snd_pcm_channel_area_t *src_areas,
66 snd_pcm_uframes_t src_offset,
67 snd_pcm_uframes_t size)
68 {
69 snd_pcm_speex_t *spx = (snd_pcm_speex_t *)ext;
70 short *src = area_addr(src_areas, src_offset);
71 short *dst = area_addr(dst_areas, dst_offset);
72 unsigned int count = size;
73 short *databuf;
74
75 if (!spx->state && !spx->echo_state) {
76 /* no DSP processing */
77 memcpy(dst, src, count * 2);
78 return size;
79 }
80
81 if (spx->echo_state)
82 databuf = spx->outbuf;
83 else
84 databuf = spx->buf;
85
86 while (count > 0) {
87 unsigned int chunk;
88 if (spx->filled + count > spx->parms.frames)
89 chunk = spx->parms.frames - spx->filled;
90 else
91 chunk = count;
92 if (spx->processed)
93 memcpy(dst, databuf + spx->filled, chunk * 2);
94 else
95 memset(dst, 0, chunk * 2);
96 dst += chunk;
97 memcpy(spx->buf + spx->filled, src, chunk * 2);
98 spx->filled += chunk;
99 if (spx->filled == spx->parms.frames) {
100 if (spx->echo_state)
101 speex_echo_capture(spx->echo_state, spx->buf,
102 spx->outbuf);
103 if (spx->state)
104 speex_preprocess_run(spx->state, databuf);
105 if (spx->echo_state)
106 speex_echo_playback(spx->echo_state, databuf);
107 spx->processed = 1;
108 spx->filled = 0;
109 }
110 src += chunk;
111 count -= chunk;
112 }
113
114 return size;
115 }
116
117 static int spx_init(snd_pcm_extplug_t *ext)
118 {
119 snd_pcm_speex_t *spx = (snd_pcm_speex_t *)ext;
120
121 spx->filled = 0;
122 spx->processed = 0;
123
124 if (!spx->buf) {
125 spx->buf = malloc(spx->parms.frames * 2);
126 if (!spx->buf)
127 return -ENOMEM;
128 }
129 memset(spx->buf, 0, spx->parms.frames * 2);
130
131 if (!spx->outbuf) {
132 spx->outbuf = malloc(spx->parms.frames * 2);
133 if (!spx->outbuf)
134 return -ENOMEM;
135 }
136 memset(spx->outbuf, 0, spx->parms.frames * 2);
137
138 if (spx->state) {
139 speex_preprocess_state_destroy(spx->state);
140 spx->state = NULL;
141 }
142 if (spx->echo_state) {
143 speex_echo_state_destroy(spx->echo_state);
144 spx->echo_state = NULL;
145 }
146
147 if (spx->parms.echo) {
148 spx->echo_state = speex_echo_state_init(spx->parms.frames,
149 spx->parms.filter_length);
150 if (!spx->echo_state)
151 return -EIO;
152 speex_echo_ctl(spx->echo_state, SPEEX_ECHO_SET_SAMPLING_RATE,
153 &spx->ext.rate);
154 }
155
156 /* no preprocessor? */
157 if (!spx->parms.denoise && !spx->parms.agc && !spx->parms.dereverb)
158 return 0;
159
160 spx->state = speex_preprocess_state_init(spx->parms.frames,
161 spx->ext.rate);
162 if (!spx->state)
163 return -EIO;
164 if (spx->echo_state)
165 speex_preprocess_ctl(spx->state,
166 SPEEX_PREPROCESS_SET_ECHO_STATE,
167 spx->echo_state);
168
169 speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DENOISE,
170 &spx->parms.denoise);
171 speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_AGC,
172 &spx->parms.agc);
173 speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_AGC_LEVEL,
174 &spx->parms.agc_level);
175 speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DEREVERB,
176 &spx->parms.dereverb);
177 speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DEREVERB_DECAY,
178 &spx->parms.dereverb_decay);
179 speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL,
180 &spx->parms.dereverb_level);
181 return 0;
182 }
183
184 static int spx_close(snd_pcm_extplug_t *ext)
185 {
186 snd_pcm_speex_t *spx = (snd_pcm_speex_t *)ext;
187 free(spx->outbuf);
188 free(spx->buf);
189 if (spx->state)
190 speex_preprocess_state_destroy(spx->state);
191 if (spx->echo_state)
192 speex_echo_state_destroy(spx->echo_state);
193 return 0;
194 }
195
196 static const snd_pcm_extplug_callback_t speex_callback = {
197 .transfer = spx_transfer,
198 .init = spx_init,
199 .close = spx_close,
200 };
201
202 static int get_bool_parm(snd_config_t *n, const char *id, const char *str,
203 int *val_ret)
204 {
205 int val;
206 if (strcmp(id, str))
207 return 0;
208
209 val = snd_config_get_bool(n);
210 if (val < 0) {
211 SNDERR("Invalid value for %s", id);
212 return val;
213 }
214 *val_ret = val;
215 return 1;
216 }
217
218 static int get_int_parm(snd_config_t *n, const char *id, const char *str,
219 int *val_ret)
220 {
221 long val;
222 int err;
223
224 if (strcmp(id, str))
225 return 0;
226 err = snd_config_get_integer(n, &val);
227 if (err < 0) {
228 SNDERR("Invalid value for %s parameter", id);
229 return err;
230 }
231 *val_ret = val;
232 return 1;
233 }
234
235 static int get_float_parm(snd_config_t *n, const char *id, const char *str,
236 float *val_ret)
237 {
238 double val;
239 int err;
240
241 if (strcmp(id, str))
242 return 0;
243 err = snd_config_get_ireal(n, &val);
244 if (err < 0) {
245 SNDERR("Invalid value for %s", id);
246 return err;
247 }
248 *val_ret = val;
249 return 1;
250 }
251
252 SND_PCM_PLUGIN_DEFINE_FUNC(speex)
253 {
254 snd_config_iterator_t i, next;
255 snd_pcm_speex_t *spx;
256 snd_config_t *sconf = NULL;
257 int err;
258 struct spx_parms parms = {
259 .frames = 64,
260 .denoise = 1,
261 .agc = 0,
262 .agc_level = 8000,
263 .dereverb = 0,
264 .dereverb_decay = 0,
265 .dereverb_level = 0,
266 .echo = 0,
267 .filter_length = 256,
268 };
269
270 snd_config_for_each(i, next, conf) {
271 snd_config_t *n = snd_config_iterator_entry(i);
272 const char *id;
273 if (snd_config_get_id(n, &id) < 0)
274 continue;
275 if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 ||
276 strcmp(id, "hint") == 0)
277 continue;
278 if (strcmp(id, "slave") == 0) {
279 sconf = n;
280 continue;
281 }
282 err = get_int_parm(n, id, "frames", &parms.frames);
283 if (err)
284 goto ok;
285 err = get_bool_parm(n, id, "denoise", &parms.denoise);
286 if (err)
287 goto ok;
288 err = get_bool_parm(n, id, "agc", &parms.agc);
289 if (err)
290 goto ok;
291 err = get_float_parm(n, id, "agc_level", &parms.agc_level);
292 if (err)
293 goto ok;
294 err = get_bool_parm(n, id, "dereverb", &parms.dereverb);
295 if (err)
296 goto ok;
297 err = get_float_parm(n, id, "dereverb_decay",
298 &parms.dereverb_decay);
299 if (err)
300 goto ok;
301 err = get_float_parm(n, id, "dereverb_level",
302 &parms.dereverb_level);
303 if (err)
304 goto ok;
305 err = get_bool_parm(n, id, "echo", &parms.echo);
306 if (err)
307 goto ok;
308 err = get_int_parm(n, id, "filter_length",
309 &parms.filter_length);
310 if (err)
311 goto ok;
312 SNDERR("Unknown field %s", id);
313 err = -EINVAL;
314 ok:
315 if (err < 0)
316 return err;
317 }
318
319 if (!sconf) {
320 SNDERR("No slave configuration for speex pcm");
321 return -EINVAL;
322 }
323
324 spx = calloc(1, sizeof(*spx));
325 if (!spx)
326 return -ENOMEM;
327
328 spx->ext.version = SND_PCM_EXTPLUG_VERSION;
329 spx->ext.name = "Speex DSP Plugin";
330 spx->ext.callback = &speex_callback;
331 spx->ext.private_data = spx;
332 spx->parms = parms;
333
334 err = snd_pcm_extplug_create(&spx->ext, name, root, sconf,
335 stream, mode);
336 if (err < 0) {
337 free(spx);
338 return err;
339 }
340
341 snd_pcm_extplug_set_param(&spx->ext, SND_PCM_EXTPLUG_HW_CHANNELS, 1);
342 snd_pcm_extplug_set_slave_param(&spx->ext,
343 SND_PCM_EXTPLUG_HW_CHANNELS, 1);
344 snd_pcm_extplug_set_param(&spx->ext, SND_PCM_EXTPLUG_HW_FORMAT,
345 SND_PCM_FORMAT_S16);
346 snd_pcm_extplug_set_slave_param(&spx->ext, SND_PCM_EXTPLUG_HW_FORMAT,
347 SND_PCM_FORMAT_S16);
348
349 *pcmp = spx->ext.pcm;
350 return 0;
351 }
352
353 SND_PCM_PLUGIN_SYMBOL(speex);