summaryrefslogtreecommitdiffstats
path: root/speex/pcm_speex.c
blob: 757a400e5b1bbd3512d31ffef7e7f6fde3854403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Speex DSP plugin
 *
 * Copyright (c) 2009 by Takashi Iwai <tiwai@suse.de>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>
#include <speex/speex_preprocess.h>
#include <speex/speex_echo.h>

/* DSP parameters */
struct spx_parms {
	int frames;
	int denoise;
	int agc;
	int echo;
	int filter_length;
	float agc_level;
	int dereverb;
	float dereverb_decay;
	float dereverb_level;
};

typedef struct {
	snd_pcm_extplug_t ext;
	struct spx_parms parms;
	/* instance and intermedate buffer */
	SpeexPreprocessState *state;
	SpeexEchoState *echo_state;
	short *buf;
	short *outbuf;
	/* running states */
	unsigned int filled;
	unsigned int processed;
} snd_pcm_speex_t;


static inline void *area_addr(const snd_pcm_channel_area_t *area,
			      snd_pcm_uframes_t offset)
{
	unsigned int bitofs = area->first + area->step * offset;
	return (char *) area->addr + bitofs / 8;
}

static snd_pcm_sframes_t
spx_transfer(snd_pcm_extplug_t *ext,
	     const snd_pcm_channel_area_t *dst_areas,
	     snd_pcm_uframes_t dst_offset,
	     const snd_pcm_channel_area_t *src_areas,
	     snd_pcm_uframes_t src_offset,
	     snd_pcm_uframes_t size)
{
	snd_pcm_speex_t *spx = (snd_pcm_speex_t *)ext;
	short *src = area_addr(src_areas, src_offset);
	short *dst = area_addr(dst_areas, dst_offset);
	unsigned int count = size;
	short *databuf;

	if (!spx->state && !spx->echo_state) {
		/* no DSP processing */
		memcpy(dst, src, count * 2);
		return size;
	}

	if (spx->echo_state)
		databuf = spx->outbuf;
	else
		databuf = spx->buf;

	while (count > 0) {
		unsigned int chunk;
		if (spx->filled + count > spx->parms.frames)
			chunk = spx->parms.frames - spx->filled;
		else
			chunk = count;
		if (spx->processed)
			memcpy(dst, databuf + spx->filled, chunk * 2);
		else
			memset(dst, 0, chunk * 2);
		dst += chunk;
		memcpy(spx->buf + spx->filled, src, chunk * 2);
		spx->filled += chunk;
		if (spx->filled == spx->parms.frames) {
			if (spx->echo_state)
				speex_echo_capture(spx->echo_state, spx->buf,
						   spx->outbuf);
			if (spx->state)
				speex_preprocess_run(spx->state, databuf);
			if (spx->echo_state)
				speex_echo_playback(spx->echo_state, databuf);
			spx->processed = 1;
			spx->filled = 0;
		}
		src += chunk;
		count -= chunk;
	}

	return size;
}

static int spx_init(snd_pcm_extplug_t *ext)
{
	snd_pcm_speex_t *spx = (snd_pcm_speex_t *)ext;

	spx->filled = 0;
	spx->processed = 0;

	if (!spx->buf) {
		spx->buf = malloc(spx->parms.frames * 2);
		if (!spx->buf)
			return -ENOMEM;
	}
	memset(spx->buf, 0, spx->parms.frames * 2);

	if (!spx->outbuf) {
		spx->outbuf = malloc(spx->parms.frames * 2);
		if (!spx->outbuf)
			return -ENOMEM;
	}
	memset(spx->outbuf, 0, spx->parms.frames * 2);

	if (spx->state) {
		speex_preprocess_state_destroy(spx->state);
		spx->state = NULL;
	}
	if (spx->echo_state) {
		speex_echo_state_destroy(spx->echo_state);
		spx->echo_state = NULL;
	}

	if (spx->parms.echo) {
		spx->echo_state = speex_echo_state_init(spx->parms.frames,
						spx->parms.filter_length);
		if (!spx->echo_state)
			return -EIO;
		speex_echo_ctl(spx->echo_state, SPEEX_ECHO_SET_SAMPLING_RATE,
			       &spx->ext.rate);
	}

	/* no preprocessor? */
	if (!spx->parms.denoise && !spx->parms.agc && !spx->parms.dereverb)
		return 0;

	spx->state = speex_preprocess_state_init(spx->parms.frames,
						 spx->ext.rate);
	if (!spx->state)
		return -EIO;
	if (spx->echo_state)
		speex_preprocess_ctl(spx->state,
				     SPEEX_PREPROCESS_SET_ECHO_STATE,
				     spx->echo_state);

	speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DENOISE,
			     &spx->parms.denoise);
	speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_AGC,
			     &spx->parms.agc);
	speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_AGC_LEVEL,
			     &spx->parms.agc_level);
	speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DEREVERB,
			     &spx->parms.dereverb);
	speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DEREVERB_DECAY,
			     &spx->parms.dereverb_decay);
	speex_preprocess_ctl(spx->state, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL,
			     &spx->parms.dereverb_level);
	return 0;
}

static int spx_close(snd_pcm_extplug_t *ext)
{
	snd_pcm_speex_t *spx = (snd_pcm_speex_t *)ext;
	free(spx->outbuf);
	free(spx->buf);
	if (spx->state)
		speex_preprocess_state_destroy(spx->state);
	if (spx->echo_state)
		speex_echo_state_destroy(spx->echo_state);	
	return 0;
}

static const snd_pcm_extplug_callback_t speex_callback = {
	.transfer = spx_transfer,
	.init = spx_init,
	.close = spx_close,
};

static int get_bool_parm(snd_config_t *n, const char *id, const char *str,
			 int *val_ret)
{
	int val;
	if (strcmp(id, str))
		return 0;

	val = snd_config_get_bool(n);
	if (val < 0) {
		SNDERR("Invalid value for %s", id);
		return val;
	}
	*val_ret = val;
	return 1;
}

static int get_int_parm(snd_config_t *n, const char *id, const char *str,
			int *val_ret)
{
	long val;
	int err;

	if (strcmp(id, str))
		return 0;
	err = snd_config_get_integer(n, &val);
	if (err < 0) {
		SNDERR("Invalid value for %s parameter", id);
		return err;
	}
	*val_ret = val;
	return 1;
}

static int get_float_parm(snd_config_t *n, const char *id, const char *str,
			  float *val_ret)
{
	double val;
	int err;

	if (strcmp(id, str))
		return 0;
	err = snd_config_get_ireal(n, &val);
	if (err < 0) {
		SNDERR("Invalid value for %s", id);
		return err;
	}
	*val_ret = val;
	return 1;
}

SND_PCM_PLUGIN_DEFINE_FUNC(speex)
{
	snd_config_iterator_t i, next;
	snd_pcm_speex_t *spx;
	snd_config_t *sconf = NULL;
	int err;
	struct spx_parms parms = {
		.frames = 64,
		.denoise = 1,
		.agc = 0,
		.agc_level = 8000,
		.dereverb = 0,
		.dereverb_decay = 0,
		.dereverb_level = 0,
		.echo = 0,
		.filter_length = 256,
	};

	snd_config_for_each(i, next, conf) {
		snd_config_t *n = snd_config_iterator_entry(i);
		const char *id;
		if (snd_config_get_id(n, &id) < 0)
			continue;
		if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 ||
		    strcmp(id, "hint") == 0)
			continue;
		if (strcmp(id, "slave") == 0) {
			sconf = n;
			continue;
		}
		err = get_int_parm(n, id, "frames", &parms.frames);
		if (err)
			goto ok;
		err = get_bool_parm(n, id, "denoise", &parms.denoise);
		if (err)
			goto ok;
		err = get_bool_parm(n, id, "agc", &parms.agc);
		if (err)
			goto ok;
		err = get_float_parm(n, id, "agc_level", &parms.agc_level);
		if (err)
			goto ok;
		err = get_bool_parm(n, id, "dereverb", &parms.dereverb);
		if (err)
			goto ok;
		err = get_float_parm(n, id, "dereverb_decay",
				     &parms.dereverb_decay);
		if (err)
			goto ok;
		err = get_float_parm(n, id, "dereverb_level",
				     &parms.dereverb_level);
		if (err)
			goto ok;
		err = get_bool_parm(n, id, "echo", &parms.echo);
              	if (err)
                	goto ok;
		err = get_int_parm(n, id, "filter_length",
				   &parms.filter_length);
            	if (err)
			goto ok;	
		SNDERR("Unknown field %s", id);
		err = -EINVAL;
	ok:
		if (err < 0)
			return err;
	}

	if (!sconf) {
		SNDERR("No slave configuration for speex pcm");
		return -EINVAL;
	}

	spx = calloc(1, sizeof(*spx));
	if (!spx)
		return -ENOMEM;

	spx->ext.version = SND_PCM_EXTPLUG_VERSION;
	spx->ext.name = "Speex DSP Plugin";
	spx->ext.callback = &speex_callback;
	spx->ext.private_data = spx;
	spx->parms = parms;

	err = snd_pcm_extplug_create(&spx->ext, name, root, sconf,
				     stream, mode);
	if (err < 0) {
		free(spx);
		return err;
	}

	snd_pcm_extplug_set_param(&spx->ext, SND_PCM_EXTPLUG_HW_CHANNELS, 1);
	snd_pcm_extplug_set_slave_param(&spx->ext,
					SND_PCM_EXTPLUG_HW_CHANNELS, 1);
	snd_pcm_extplug_set_param(&spx->ext, SND_PCM_EXTPLUG_HW_FORMAT,
				  SND_PCM_FORMAT_S16);
	snd_pcm_extplug_set_slave_param(&spx->ext, SND_PCM_EXTPLUG_HW_FORMAT,
					SND_PCM_FORMAT_S16);

	*pcmp = spx->ext.pcm;
	return 0;
}

SND_PCM_PLUGIN_SYMBOL(speex);