3
* @brief Alsa External plugin: I/O plugin
5
* Copyright (C) 2006 Nokia Corporation
7
* Contact: Eduardo Bezerra Valentin <eduardo.valentin@indt.org.br>
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Library General Public
11
* License as published by the Free Software Foundation; either
12
* version 2 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Library General Public License for more details.
19
* You should have received a copy of the GNU Library General Public
20
* License along with this library; if not, write to the
21
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
* Boston, MA 02111-1307, USA.
25
#include <sys/ioctl.h>
26
#include <alsa/asoundlib.h>
27
#include <alsa/pcm_external.h>
30
#include "dsp-protocol.h"
31
#include "constants.h"
33
#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
35
* Device node file name list.
39
struct list_head list;
43
* Holds the need information: list of playback and recording devices,
44
* current format, sample_rate, bytes per frame and pointer to ring
47
typedef struct snd_pcm_alsa_dsp {
49
dsp_protocol_t *dsp_protocol;
53
snd_pcm_sframes_t hw_pointer;
54
device_list_t playback_devices;
55
device_list_t recording_devices;
58
static snd_pcm_alsa_dsp_t *free_ref;
60
* @param io pcm io plugin configured to Alsa libs.
62
* It starts the playback sending a DSP_CMD_PLAY.
64
* @return zero if success, otherwise a negative error code.
66
static int alsa_dsp_start(snd_pcm_ioplug_t * io)
68
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
71
DPRINT("IO_STREAM %d == SND_PCM_STREAM_PLAYBACK %d\n", io->stream,
72
io->stream == SND_PCM_STREAM_PLAYBACK);
73
if (io->stream != SND_PCM_STREAM_PLAYBACK)
74
dsp_protocol_set_mic_enabled(alsa_dsp->dsp_protocol, 1);
75
ret = dsp_protocol_send_play(alsa_dsp->dsp_protocol);
81
* @param io the pcm io plugin we configured to Alsa libs.
83
* It starts the playback sending a DSP_CMD_STOP.
85
* @return zero if success, otherwise a negative error code.
87
static int alsa_dsp_stop(snd_pcm_ioplug_t * io)
89
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
92
ret = dsp_protocol_send_stop(alsa_dsp->dsp_protocol);
93
if (io->stream != SND_PCM_STREAM_PLAYBACK)
94
dsp_protocol_set_mic_enabled(alsa_dsp->dsp_protocol, 0);
101
* @param io the pcm io plugin we configured to Alsa libs.
103
* It returns the position of current period consuming.
105
* @return on success, returns current position, otherwise a negative
108
static snd_pcm_sframes_t alsa_dsp_pointer(snd_pcm_ioplug_t * io)
110
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
111
snd_pcm_sframes_t ret;
113
ret = alsa_dsp->hw_pointer;
114
if (alsa_dsp->hw_pointer == 0)
115
alsa_dsp->hw_pointer =
116
io->period_size * alsa_dsp->bytes_per_frame;
118
alsa_dsp->hw_pointer = 0;
124
* @param io the pcm io plugin we configured to Alsa libs.
126
* It transfers the audio data to dsp side.
128
* @return on success, returns amount of data transfered,
129
* otherwise a negative error code.
131
static snd_pcm_sframes_t alsa_dsp_transfer(snd_pcm_ioplug_t * io,
132
const snd_pcm_channel_area_t * areas,
133
snd_pcm_uframes_t offset,
134
snd_pcm_uframes_t size)
136
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
142
words = size * alsa_dsp->bytes_per_frame;
144
DPRINT("***** Info: words %d size %lu bpf: %d\n", words, size,
145
alsa_dsp->bytes_per_frame);
146
if (words > alsa_dsp->dsp_protocol->mmap_buffer_size) {
147
DERROR("Requested too much data transfer (playing only %d)\n",
148
alsa_dsp->dsp_protocol->mmap_buffer_size);
149
words = alsa_dsp->dsp_protocol->mmap_buffer_size;
151
if (alsa_dsp->dsp_protocol->state != STATE_PLAYING) {
152
DPRINT("I did nothing - No start sent\n");
155
/* we handle only an interleaved buffer */
156
buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
157
if (io->stream == SND_PCM_STREAM_PLAYBACK)
159
dsp_protocol_send_audio_data(alsa_dsp->dsp_protocol, buf,
163
dsp_protocol_receive_audio_data(alsa_dsp->dsp_protocol, buf,
166
result /= alsa_dsp->bytes_per_frame;
167
alsa_dsp->hw_pointer += result;
173
* @param device_list a list of device names to be freed.
175
* It passes a list of device names and frees each node.
177
* @return zero (success).
179
static int free_device_list(device_list_t * device_list)
181
struct list_head *pos, *q;
183
list_for_each_safe(pos, q, &device_list->list) {
184
tmp = list_entry(pos, device_list_t, list);
193
* @param io the pcm io plugin we configured to Alsa libs.
195
* Closes the connection with the pcm dsp task. It
196
* destroies all allocated data.
198
* @return zero if success, otherwise a negative error code.
200
static int alsa_dsp_close(snd_pcm_ioplug_t * io)
202
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
205
ret = dsp_protocol_close_node(alsa_dsp->dsp_protocol);
206
dsp_protocol_destroy(&(alsa_dsp->dsp_protocol));
207
free_device_list(&(alsa_dsp->playback_devices));
208
free_device_list(&(alsa_dsp->recording_devices));
214
* @param map the values to be mapped
215
* @param value the search key
216
* @param steps how many keys should be checked
218
* Maps a value to another.
220
* @return on success, returns mapped value, otherwise a negative error code.
222
static int map_value(int *map, int value, int steps)
225
for (i = 0; i < steps; i++)
226
if (map[i * 2] == value)
227
return map[i * 2 + 1];
232
* @param io the pcm io plugin we configured to Alsa libs.
235
* It checks if the pcm format and rate are supported.
237
* @return zero if success, otherwise a negative error code.
239
static int alsa_dsp_hw_params(snd_pcm_ioplug_t * io,
240
snd_pcm_hw_params_t * params)
242
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
244
int map_sample_rates[] = {
245
8000, SAMPLE_RATE_8KHZ,
246
11025, SAMPLE_RATE_11_025KHZ,
247
12000, SAMPLE_RATE_12KHZ,
248
16000, SAMPLE_RATE_16KHZ,
249
22050, SAMPLE_RATE_22_05KHZ,
250
24000, SAMPLE_RATE_24KHZ,
251
32000, SAMPLE_RATE_32KHZ,
252
44100, SAMPLE_RATE_44_1KHZ,
253
48000, SAMPLE_RATE_48KHZ
255
int map_formats[] = {
256
SND_PCM_FORMAT_A_LAW, DSP_AFMT_ALAW,
257
SND_PCM_FORMAT_MU_LAW, DSP_AFMT_ULAW,
258
SND_PCM_FORMAT_S16_LE, DSP_AFMT_S16_LE,
259
SND_PCM_FORMAT_U8, DSP_AFMT_U8,
260
SND_PCM_FORMAT_S8, DSP_AFMT_S8,
261
SND_PCM_FORMAT_S16_BE, DSP_AFMT_S16_BE,
262
SND_PCM_FORMAT_U16_LE, DSP_AFMT_U16_LE,
263
SND_PCM_FORMAT_U16_BE, DSP_AFMT_U16_BE
266
DPRINT("Checking Format- Ret %d\n", ret);
267
alsa_dsp->format = map_value(map_formats, io->format,
269
SND_PCM_STREAM_PLAYBACK ?
270
ARRAY_SIZE(map_formats) : 3);
271
if (alsa_dsp->format < 0) {
272
DERROR("*** ALSA-DSP: unsupported format %s\n",
273
snd_pcm_format_name(io->format));
276
DPRINT("Format is Ok. Checking rate. Ret %d\n", ret);
278
alsa_dsp->sample_rate = map_value(map_sample_rates, io->rate,
280
SND_PCM_STREAM_PLAYBACK ?
281
ARRAY_SIZE(map_sample_rates) : 1);
282
if (alsa_dsp->sample_rate < 0) {
284
DERROR("** ALSA - DSP - Unsuported Sample Rate! **\n");
286
DPRINT("Rate is ok. Calculating WPF. Ret %d\n", ret);
288
alsa_dsp->bytes_per_frame =
289
((snd_pcm_format_physical_width(io->format) * io->channels) / 8);
290
DPRINT("WPF: %d width %d channels %d\n", alsa_dsp->bytes_per_frame,
291
snd_pcm_format_physical_width(io->format), io->channels);
298
* @param io the pcm io plugin we configured to Alsa libs.
300
* It sends the audio parameters to pcm task node (formats, channels,
301
* access, rates). It is assumed that everything is proper set.
303
* @return zero if success, otherwise a negative error code.
305
static int alsa_dsp_prepare(snd_pcm_ioplug_t * io)
307
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
308
audio_params_data_t params;
309
speech_params_data_t sparams;
314
alsa_dsp->hw_pointer = 0;
315
if (alsa_dsp->dsp_protocol->state != STATE_INITIALISED) {
316
tmp = strdup(alsa_dsp->dsp_protocol->device);
317
ret = dsp_protocol_close_node(alsa_dsp->dsp_protocol);
319
dsp_protocol_open_node(alsa_dsp->dsp_protocol, tmp);
323
if (io->stream == SND_PCM_STREAM_PLAYBACK) {
324
params.dsp_cmd = DSP_CMD_SET_PARAMS;
325
params.dsp_audio_fmt = alsa_dsp->format;
326
params.sample_rate = alsa_dsp->sample_rate;
327
params.number_channels = io->channels;
328
params.ds_stream_id = 0;
329
params.stream_priority = 0;
330
if (dsp_protocol_send_audio_params
331
(alsa_dsp->dsp_protocol, ¶ms) < 0) {
333
DERROR("Error in send params data\n");
335
DPRINT("Sending params data is ok\n");
337
sparams.dsp_cmd = DSP_CMD_SET_SPEECH_PARAMS;
338
sparams.audio_fmt = alsa_dsp->format;
339
sparams.sample_rate = alsa_dsp->sample_rate;
340
sparams.ds_stream_id = 0;
341
sparams.stream_priority = 0;
342
sparams.frame_size = io->period_size;
343
DPRINT("frame size %u\n", sparams.frame_size);
344
if (dsp_protocol_send_speech_params
345
(alsa_dsp->dsp_protocol, &sparams) < 0) {
347
DERROR("Error in send speech params data\n");
349
DPRINT("Sending speech params data is ok\n");
358
* @param io the pcm io plugin we configured to Alsa libs.
360
* It pauses the playback sending a DSP_CMD_PAUSE.
362
* @return zero if success, otherwise a negative error code.
364
static int alsa_dsp_pause(snd_pcm_ioplug_t * io, int enable)
366
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
369
ret = dsp_protocol_send_pause(alsa_dsp->dsp_protocol);
375
* @param io the pcm io plugin we configured to Alsa libs.
377
* It starts the playback sending a DSP_CMD_PLAY.
379
* @return zero if success, otherwise a negative error code.
381
static int alsa_dsp_resume(snd_pcm_ioplug_t * io)
383
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data;
386
ret = dsp_protocol_send_play(alsa_dsp->dsp_protocol);
392
* @param alsa_dsp the structure to be configured.
394
* It configures constraints about formats, channels, access, rates,
395
* periods and buffer size. It exports the supported constraints by the
396
* dsp task node to the alsa plugin library.
398
* @return zero if success, otherwise a negative error code.
400
static int alsa_dsp_configure_constraints(snd_pcm_alsa_dsp_t * alsa_dsp)
402
snd_pcm_ioplug_t *io = &alsa_dsp->io;
403
static snd_pcm_access_t access_list[] = {
404
SND_PCM_ACCESS_RW_INTERLEAVED
406
const unsigned int formats[] = {
407
SND_PCM_FORMAT_U8, /* DSP_AFMT_U8 */
408
SND_PCM_FORMAT_S16_LE, /* DSP_AFMT_S16_LE */
409
SND_PCM_FORMAT_S16_BE, /* DSP_AFMT_S16_BE */
410
SND_PCM_FORMAT_S8, /* DSP_AFMT_S8 */
411
SND_PCM_FORMAT_U16_LE, /* DSP_AFMT_U16_LE */
412
SND_PCM_FORMAT_U16_BE, /* DSP_AFMT_U16_BE */
413
SND_PCM_FORMAT_A_LAW, /* DSP_AFMT_ALAW */
414
SND_PCM_FORMAT_MU_LAW /* DSP_AFMT_ULAW */
416
const unsigned int formats_recor[] = {
417
SND_PCM_FORMAT_S16_LE, /* DSP_AFMT_S16_LE */
418
SND_PCM_FORMAT_A_LAW, /* DSP_AFMT_ALAW */
419
SND_PCM_FORMAT_MU_LAW /* DSP_AFMT_ULAW */
421
static unsigned int bytes_list[] = {
424
static unsigned int bytes_list_rec_8bit[] = {
425
/* It must be multiple of 80... less than or equal to 800 */
426
80, 160, 240, 320, 400, 480, 560, 640, 720, 800
431
/* Configuring access */
432
if ((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
433
ARRAY_SIZE(access_list),
438
if (io->stream == SND_PCM_STREAM_PLAYBACK) {
439
/* Configuring formats */
441
snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
447
/* Configuring channels */
449
snd_pcm_ioplug_set_param_minmax(io,
450
SND_PCM_IOPLUG_HW_CHANNELS,
456
/* Configuring rates */
458
snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE,
463
/* Configuring periods */
465
snd_pcm_ioplug_set_param_list(io,
466
SND_PCM_IOPLUG_HW_PERIOD_BYTES,
467
ARRAY_SIZE(bytes_list),
472
/* Configuring buffer size */
474
snd_pcm_ioplug_set_param_list(io,
475
SND_PCM_IOPLUG_HW_BUFFER_BYTES,
476
ARRAY_SIZE(bytes_list),
483
/* Configuring formats */
485
snd_pcm_ioplug_set_param_list(io,
486
SND_PCM_IOPLUG_HW_FORMAT,
487
ARRAY_SIZE(formats_recor),
488
formats_recor)) < 0) {
492
/* Configuring channels */
493
if ((err = snd_pcm_ioplug_set_param_minmax(io,
494
SND_PCM_IOPLUG_HW_CHANNELS,
500
/* Configuring rates */
502
snd_pcm_ioplug_set_param_minmax(io,
503
SND_PCM_IOPLUG_HW_RATE,
508
/* Configuring periods */
510
snd_pcm_ioplug_set_param_list(io,
511
SND_PCM_IOPLUG_HW_PERIOD_BYTES,
513
(bytes_list_rec_8bit),
514
bytes_list_rec_8bit)) < 0) {
518
/* Configuring buffer size */
520
snd_pcm_ioplug_set_param_list(io,
521
SND_PCM_IOPLUG_HW_BUFFER_BYTES,
523
(bytes_list_rec_8bit),
524
bytes_list_rec_8bit)) < 0) {
531
if ((err = snd_pcm_ioplug_set_param_minmax(io,
532
SND_PCM_IOPLUG_HW_PERIODS,
544
* Alsa-lib callback structure.
546
static snd_pcm_ioplug_callback_t alsa_dsp_callback = {
547
.start = alsa_dsp_start,
548
.stop = alsa_dsp_stop,
549
.pointer = alsa_dsp_pointer,
550
.transfer = alsa_dsp_transfer,
551
.close = alsa_dsp_close,
552
.hw_params = alsa_dsp_hw_params,
553
.prepare = alsa_dsp_prepare,
554
.pause = alsa_dsp_pause,
555
.resume = alsa_dsp_resume,
559
* @param alsa_dsp the structure to be configured.
561
* It probes all configured dsp task devices to be available for
562
* this plugin. It will use first dsp task device whose is in
563
* UNINITIALISED state.
565
* @return zero if success, otherwise a negative error code.
567
static int alsa_dsp_open_dsp_task(snd_pcm_alsa_dsp_t * alsa_dsp,
568
device_list_t * device_list)
573
DPRINT("Looking for a dsp device node \n");
574
list_for_each_entry(tmp, &(device_list->list), list) {
575
DPRINT("Trying to use %s\n", tmp->device);
577
dsp_protocol_open_node(alsa_dsp->dsp_protocol,
579
DPRINT("%s is not available now\n", tmp->device);
580
dsp_protocol_close_node(alsa_dsp->dsp_protocol);
585
DPRINT("No valid dsp task nodes for now. Exiting.\n");
592
* @param n configuration file parse tree.
593
* @param device_list list of device files to be filled.
595
* It searches for device file names in given configuration parse
596
* tree. When one device file name is found, it is filled into device_list.
598
* @return zero if success, otherwise a negative error code.
600
static int fill_string_list(snd_config_t * n, device_list_t * device_list)
602
snd_config_iterator_t j, nextj;
607
INIT_LIST_HEAD(&device_list->list);
608
snd_config_for_each(j, nextj, n) {
609
snd_config_t *s = snd_config_iterator_entry(j);
610
const char *id_number;
612
if (snd_config_get_id(s, &id_number) < 0)
614
if (safe_strtol(id_number, &k) < 0) {
615
SNDERR("id of field %s is not an integer", id_number);
621
/* add to available dsp task nodes */
622
tmp = (device_list_t *) malloc(sizeof(device_list_t));
623
if (snd_config_get_ascii(s, &(tmp->device)) < 0) {
624
SNDERR("invalid ascii string for id %s\n",
630
list_add(&(tmp->list), &(device_list->list));
641
* It initializes the alsa plugin. It reads the parameters and creates the
642
* connection with the pcm device file.
644
* @return zero if success, otherwise a negative error code.
646
SND_PCM_PLUGIN_DEFINE_FUNC(alsa_dsp)
648
snd_config_iterator_t i, next;
649
snd_pcm_alsa_dsp_t *alsa_dsp;
654
/* Allocate the structure */
655
alsa_dsp = calloc(1, sizeof(snd_pcm_alsa_dsp_t));
656
if (alsa_dsp == NULL) {
661
/* Read the configuration searching for configurated devices */
662
snd_config_for_each(i, next, conf) {
663
snd_config_t *n = snd_config_iterator_entry(i);
665
if (snd_config_get_id(n, &id) < 0)
667
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0)
669
if (strcmp(id, "playback_device_file") == 0) {
670
if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND){
673
&(alsa_dsp->playback_devices))) < 0) {
674
SNDERR("Could not fill string"
675
" list for playback devices\n");
679
SNDERR("Invalid type for %s", id);
686
if (strcmp(id, "recording_device_file") == 0) {
687
if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND){
690
&(alsa_dsp->recording_devices))) < 0){
691
SNDERR("Could not fill string"
692
" list for recording devices\n");
696
SNDERR("Invalid type for %s", id);
703
SNDERR("Unknown field %s", id);
707
/* Initialise the dsp_protocol and create connection */
708
if ((err = dsp_protocol_create(&(alsa_dsp->dsp_protocol))) < 0)
710
if ((err = alsa_dsp_open_dsp_task(alsa_dsp,
711
(stream == SND_PCM_STREAM_PLAYBACK) ?
712
&(alsa_dsp->playback_devices) :
713
&(alsa_dsp->recording_devices))) < 0)
715
/* Initialise the snd_pcm_ioplug_t */
716
alsa_dsp->io.version = SND_PCM_IOPLUG_VERSION;
717
alsa_dsp->io.name = "Alsa - DSP PCM Plugin";
718
alsa_dsp->io.mmap_rw = 0;
719
alsa_dsp->io.callback = &alsa_dsp_callback;
720
alsa_dsp->io.poll_fd = alsa_dsp->dsp_protocol->fd;
721
alsa_dsp->io.poll_events = stream == SND_PCM_STREAM_PLAYBACK ?
724
alsa_dsp->io.private_data = alsa_dsp;
727
if ((err = snd_pcm_ioplug_create(&alsa_dsp->io, name,
731
/* Configure the plugin */
732
if ((err = alsa_dsp_configure_constraints(alsa_dsp)) < 0) {
733
snd_pcm_ioplug_delete(&alsa_dsp->io);
736
*pcmp = alsa_dsp->io.pcm;
748
void alsa_dsp_descructor(void) __attribute__ ((destructor));
750
void alsa_dsp_descructor(void)
753
DPRINT("alsa dsp destructor\n");
754
DPRINT("checking for memories leaks and releasing resources\n");
756
if (free_ref->dsp_protocol) {
757
dsp_protocol_close_node(free_ref->dsp_protocol);
758
dsp_protocol_destroy(&(free_ref->dsp_protocol));
760
free_device_list(&(free_ref->playback_devices));
762
free_device_list(&(free_ref->recording_devices));
771
SND_PCM_PLUGIN_SYMBOL(alsa_dsp);