alsa-lib/src/pcm/pcm.c

466 lines
12 KiB
C
Raw Normal View History

1998-08-13 15:42:56 +00:00
/*
* PCM Interface - main file
* Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
2000-05-08 18:53:38 +00:00
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
1998-08-27 20:47:51 +00:00
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
1998-08-13 15:42:56 +00:00
*/
1998-08-13 15:42:56 +00:00
#include <stdio.h>
2000-05-26 17:58:24 +00:00
#include <string.h>
2000-05-08 18:53:38 +00:00
#include <malloc.h>
1998-08-13 15:42:56 +00:00
#include <errno.h>
2000-07-04 19:29:16 +00:00
#include <sys/ioctl.h>
2000-05-08 18:53:38 +00:00
#include <sys/poll.h>
#include <sys/uio.h>
#include "pcm_local.h"
1998-08-13 15:42:56 +00:00
2000-06-21 14:59:20 +00:00
snd_pcm_type_t snd_pcm_type(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
assert(handle);
2000-06-21 14:59:20 +00:00
return handle->type;
}
2000-06-21 14:59:20 +00:00
snd_pcm_type_t snd_pcm(snd_pcm_t *handle)
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->stream;
2000-05-08 18:53:38 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_close(snd_pcm_t *handle)
2000-05-08 18:53:38 +00:00
{
int ret = 0;
int err;
2000-06-21 14:59:20 +00:00
assert(handle);
if (handle->mmap_status) {
if ((err = snd_pcm_munmap_status(handle)) < 0)
ret = err;
}
2000-06-21 14:59:20 +00:00
if (handle->mmap_control) {
if ((err = snd_pcm_munmap_control(handle)) < 0)
2000-05-08 18:53:38 +00:00
ret = err;
}
2000-06-21 14:59:20 +00:00
if (handle->mmap_data) {
if ((err = snd_pcm_munmap_data(handle)) < 0)
2000-05-08 18:53:38 +00:00
ret = err;
1999-08-22 13:34:31 +00:00
}
2000-06-21 14:59:20 +00:00
if ((err = handle->ops->close(handle->op_arg)) < 0)
2000-05-08 18:53:38 +00:00
ret = err;
2000-06-21 14:59:20 +00:00
handle->valid_setup = 0;
free(handle);
2000-05-08 18:53:38 +00:00
return ret;
}
1998-08-13 15:42:56 +00:00
2000-06-21 14:59:20 +00:00
int snd_pcm_nonblock(snd_pcm_t *handle, int nonblock)
1998-08-13 15:42:56 +00:00
{
2000-05-08 18:53:38 +00:00
int err;
2000-06-21 14:59:20 +00:00
assert(handle);
if ((err = handle->ops->nonblock(handle->op_arg, nonblock)) < 0)
2000-05-08 18:53:38 +00:00
return err;
if (nonblock)
2000-06-21 14:59:20 +00:00
handle->mode |= SND_PCM_NONBLOCK;
2000-05-08 18:53:38 +00:00
else
2000-06-21 14:59:20 +00:00
handle->mode &= ~SND_PCM_NONBLOCK;
1999-11-07 16:43:13 +00:00
return 0;
}
2000-06-21 14:59:20 +00:00
int snd_pcm_info(snd_pcm_t *handle, snd_pcm_info_t *info)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle && info);
/* Here we pass private and not op_arg.
FIXME: find a better solution */
return handle->ops->info(handle->private, info);
1998-08-13 15:42:56 +00:00
}
int snd_pcm_params_info(snd_pcm_t *handle, snd_pcm_params_info_t *info)
{
assert(handle && info);
/* Here we pass private and not op_arg.
FIXME: find a better solution */
return handle->ops->params_info(handle->private, info);
}
2000-06-21 14:59:20 +00:00
int snd_pcm_setup(snd_pcm_t *handle, snd_pcm_setup_t *setup)
1998-08-13 15:42:56 +00:00
{
int err;
2000-06-21 14:59:20 +00:00
assert(handle && setup);
if (handle->valid_setup) {
*setup = handle->setup;
return 0;
}
2000-06-21 14:59:20 +00:00
/* Here we pass private and not op_arg.
FIXME: find a better solution */
if ((err = handle->ops->setup(handle->private, &handle->setup)) < 0)
2000-05-08 18:53:38 +00:00
return err;
2000-06-21 14:59:20 +00:00
*setup = handle->setup;
handle->bits_per_sample = snd_pcm_format_physical_width(setup->format.format);
handle->bits_per_frame = handle->bits_per_sample * setup->format.channels;
handle->valid_setup = 1;
1998-11-27 14:57:39 +00:00
return 0;
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_channel_setup(snd_pcm_t *handle, snd_pcm_channel_setup_t *setup)
2000-04-16 15:38:28 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle && setup);
assert(handle->valid_setup);
return handle->ops->channel_setup(handle->op_arg, setup);
2000-05-08 18:53:38 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_params(snd_pcm_t *handle, snd_pcm_params_t *params)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
int err;
snd_pcm_setup_t setup;
assert(handle && params);
assert(!handle->mmap_data);
/* Here we pass private and not op_arg.
FIXME: find a better solution */
if ((err = handle->ops->params(handle->private, params)) < 0)
return err;
handle->valid_setup = 0;
return snd_pcm_setup(handle, &setup);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_status(snd_pcm_t *handle, snd_pcm_status_t *status)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle && status);
return handle->ops->status(handle->op_arg, status);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_state(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
if (handle->mmap_status)
return handle->mmap_status->state;
return handle->ops->state(handle->op_arg);
}
1998-11-27 14:57:39 +00:00
2000-06-21 14:59:20 +00:00
int snd_pcm_frame_io(snd_pcm_t *handle, int update)
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
if (handle->mmap_status && !update)
return handle->mmap_status->frame_io;
return handle->ops->frame_io(handle->op_arg, update);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_prepare(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->ops->prepare(handle->op_arg);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_go(snd_pcm_t *handle)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->ops->go(handle->op_arg);
1998-08-13 15:42:56 +00:00
}
2000-07-04 19:29:16 +00:00
int snd_pcm_synchro(snd_pcm_synchro_cmd_t cmd,
unsigned int reqs_count, snd_pcm_synchro_request_t *reqs,
snd_pcm_synchro_mode_t mode)
{
snd_pcm_sync_request_t *sync_reqs;
snd_pcm_sync_t sync;
unsigned int k;
2000-07-18 10:09:47 +00:00
int ret;
2000-07-04 19:29:16 +00:00
assert(reqs_count > 0 && reqs);
sync_reqs = __builtin_alloca(sizeof(*sync_reqs) * reqs_count);
switch (cmd) {
case SND_PCM_SYNCHRO_GO:
2000-07-18 10:09:47 +00:00
sync.cmd = SND_PCM_IOCTL_GO;
2000-07-04 19:29:16 +00:00
break;
default:
assert(0);
return -EINVAL;
}
sync.mode = mode;
sync.requests_count = reqs_count;
sync.requests = sync_reqs;
for (k = 0; k < reqs_count; ++k) {
switch (snd_pcm_type(reqs[k].handle)) {
case SND_PCM_TYPE_HW:
case SND_PCM_TYPE_PLUG:
sync_reqs[k].fd = snd_pcm_file_descriptor(reqs[k].handle);
break;
default:
/* Not yet implemented */
assert(0);
return -ENOSYS;
}
}
if (ioctl(sync_reqs[0].fd, SND_PCM_IOCTL_SYNC, &sync) < 0)
2000-07-18 10:09:47 +00:00
ret = -errno;
else
ret = 0;
for (k = 0; k < reqs_count; ++k) {
reqs[k].tstamp = sync_reqs[k].tstamp;
reqs[k].result = sync_reqs[k].result;
}
return ret;
1998-08-25 15:38:50 +00:00
}
2000-07-04 19:29:16 +00:00
2000-06-21 14:59:20 +00:00
int snd_pcm_drain(snd_pcm_t *handle)
1998-08-25 15:38:50 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->ops->drain(handle->op_arg);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_flush(snd_pcm_t *handle)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->ops->flush(handle->op_arg);
2000-05-08 18:53:38 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_pause(snd_pcm_t *handle, int enable)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->ops->pause(handle->op_arg, enable);
1998-08-13 15:42:56 +00:00
}
2000-05-08 18:53:38 +00:00
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_frame_data(snd_pcm_t *handle, off_t offset)
2000-05-29 19:53:30 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
if (handle->mmap_control) {
if (offset == 0)
2000-06-21 14:59:20 +00:00
return handle->mmap_control->frame_data;
}
2000-06-21 14:59:20 +00:00
return handle->ops->frame_data(handle->op_arg, offset);
2000-05-29 19:53:30 +00:00
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_write(snd_pcm_t *handle, const void *buffer, size_t size)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(size == 0 || buffer);
2000-06-21 14:59:20 +00:00
assert(handle->valid_setup);
assert(size % handle->setup.frames_align == 0);
2000-07-18 10:09:47 +00:00
return handle->ops->write(handle->op_arg, 0, buffer, size);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_writev(snd_pcm_t *handle, const struct iovec *vector, unsigned long count)
2000-01-31 12:40:05 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(count == 0 || vector);
2000-06-21 14:59:20 +00:00
assert(handle->valid_setup);
assert(handle->setup.format.interleave ||
count % handle->setup.format.channels == 0);
2000-07-18 10:09:47 +00:00
return handle->ops->writev(handle->op_arg, 0, vector, count);
2000-01-31 12:40:05 +00:00
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_read(snd_pcm_t *handle, void *buffer, size_t size)
1998-08-13 15:42:56 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(size == 0 || buffer);
2000-06-21 14:59:20 +00:00
assert(handle->valid_setup);
assert(size % handle->setup.frames_align == 0);
2000-07-18 10:09:47 +00:00
return handle->ops->read(handle->op_arg, 0, buffer, size);
1998-08-13 15:42:56 +00:00
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_readv(snd_pcm_t *handle, const struct iovec *vector, unsigned long count)
2000-01-31 12:40:05 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(count == 0 || vector);
2000-06-21 14:59:20 +00:00
assert(handle->valid_setup);
2000-07-18 10:09:47 +00:00
return handle->ops->readv(handle->op_arg, 0, vector, count);
2000-05-08 18:53:38 +00:00
}
2000-01-31 12:40:05 +00:00
2000-06-21 14:59:20 +00:00
int snd_pcm_file_descriptor(snd_pcm_t *handle)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
assert(handle);
return handle->ops->file_descriptor(handle->op_arg);
2000-01-31 12:40:05 +00:00
}
2000-06-21 14:59:20 +00:00
int snd_pcm_channels_mask(snd_pcm_t *handle, bitset_t *client_vmask)
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
return handle->ops->channels_mask(handle->op_arg, client_vmask);
}
typedef struct {
int value;
const char* name;
const char* desc;
} assoc_t;
static assoc_t *assoc_value(int value, assoc_t *alist)
{
while (alist->desc) {
if (value == alist->value)
return alist;
alist++;
}
return 0;
}
static assoc_t *assoc_name(const char *name, assoc_t *alist)
{
while (alist->name) {
if (strcasecmp(name, alist->name) == 0)
return alist;
alist++;
}
return 0;
}
static const char *assoc(int value, assoc_t *alist)
{
assoc_t *a;
a = assoc_value(value, alist);
if (a)
return a->name;
return "UNKNOWN";
}
#define STREAM(v) { SND_PCM_STREAM_##v, #v, #v }
#define MODE(v) { SND_PCM_MODE_##v, #v, #v }
#define FMT(v, d) { SND_PCM_SFMT_##v, #v, d }
#define XRUN(v) { SND_PCM_XRUN_##v, #v, #v }
#define START(v) { SND_PCM_START_##v, #v, #v }
#define FILL(v) { SND_PCM_FILL_##v, #v, #v }
#define END { 0, NULL, NULL }
static assoc_t streams[] = { STREAM(PLAYBACK), STREAM(CAPTURE), END };
static assoc_t modes[] = { MODE(FRAME), MODE(FRAGMENT), END };
static assoc_t fmts[] = {
FMT(S8, "Signed 8-bit"),
FMT(U8, "Unsigned 8-bit"),
FMT(S16_LE, "Signed 16-bit Little Endian"),
FMT(S16_BE, "Signed 16-bit Big Endian"),
FMT(U16_LE, "Unsigned 16-bit Little Endian"),
FMT(U16_BE, "Unsigned 16-bit Big Endian"),
FMT(S24_LE, "Signed 24-bit Little Endian"),
FMT(S24_BE, "Signed 24-bit Big Endian"),
FMT(U24_LE, "Unsigned 24-bit Little Endian"),
FMT(U24_BE, "Unsigned 24-bit Big Endian"),
FMT(S32_LE, "Signed 32-bit Little Endian"),
FMT(S32_BE, "Signed 32-bit Big Endian"),
FMT(U32_LE, "Unsigned 32-bit Little Endian"),
FMT(U32_BE, "Unsigned 32-bit Big Endian"),
FMT(FLOAT_LE, "Float Little Endian"),
FMT(FLOAT_BE, "Float Big Endian"),
FMT(FLOAT64_LE, "Float64 Little Endian"),
FMT(FLOAT64_BE, "Float64 Big Endian"),
FMT(IEC958_SUBFRAME_LE, "IEC-958 Little Endian"),
FMT(IEC958_SUBFRAME_BE, "IEC-958 Big Endian"),
FMT(MU_LAW, "Mu-Law"),
FMT(A_LAW, "A-Law"),
FMT(IMA_ADPCM, "Ima-ADPCM"),
FMT(MPEG, "MPEG"),
FMT(GSM, "GSM"),
FMT(SPECIAL, "Special"),
END
};
static assoc_t starts[] = { START(GO), START(DATA), START(FULL), END };
static assoc_t xruns[] = { XRUN(FLUSH), XRUN(DRAIN), END };
static assoc_t fills[] = { FILL(NONE), FILL(SILENCE_WHOLE), FILL(SILENCE), END };
2000-05-26 17:58:24 +00:00
static assoc_t onoff[] = { {0, "OFF", NULL}, {1, "ON", NULL}, {-1, "ON", NULL}, END };
2000-06-21 14:59:20 +00:00
int snd_pcm_dump_setup(snd_pcm_t *handle, FILE *fp)
{
2000-06-21 14:59:20 +00:00
snd_pcm_setup_t *setup;
assert(handle);
assert(fp);
2000-06-21 14:59:20 +00:00
assert(handle->valid_setup);
setup = &handle->setup;
fprintf(fp, "stream: %s\n", assoc(handle->stream, streams));
fprintf(fp, "mode: %s\n", assoc(setup->mode, modes));
fprintf(fp, "format: %s\n", assoc(setup->format.format, fmts));
fprintf(fp, "channels: %d\n", setup->format.channels);
fprintf(fp, "rate: %d (%d/%d=%g)\n", setup->format.rate, setup->rate_master, setup->rate_divisor, (double) setup->rate_master / setup->rate_divisor);
// digital
fprintf(fp, "start_mode: %s\n", assoc(setup->start_mode, starts));
fprintf(fp, "xrun_mode: %s\n", assoc(setup->xrun_mode, xruns));
fprintf(fp, "time: %s\n", assoc(setup->time, onoff));
// ust_time
// sync
fprintf(fp, "buffer_size: %d\n", setup->buffer_size);
fprintf(fp, "frag_size: %d\n", setup->frag_size);
fprintf(fp, "frags: %d\n", setup->frags);
fprintf(fp, "frame_boundary: %d\n", setup->frame_boundary);
fprintf(fp, "msbits_per_sample: %d\n", setup->msbits_per_sample);
fprintf(fp, "frames_min: %d\n", setup->frames_min);
fprintf(fp, "frames_align: %d\n", setup->frames_align);
fprintf(fp, "frames_xrun_max: %d\n", setup->frames_xrun_max);
fprintf(fp, "fill_mode: %s\n", assoc(setup->fill_mode, fills));
fprintf(fp, "frames_fill_max: %d\n", setup->frames_fill_max);
return 0;
}
int snd_pcm_dump(snd_pcm_t *handle, FILE *fp)
{
assert(handle);
assert(fp);
handle->ops->dump(handle->op_arg, fp);
return 0;
}
const char *snd_pcm_format_name(int format)
{
assoc_t *a = assoc_value(format, fmts);
if (a)
return a->name;
return 0;
}
const char *snd_pcm_format_description(int format)
{
assoc_t *a = assoc_value(format, fmts);
if (a)
return a->desc;
return "Unknown";
}
int snd_pcm_format_value(const char* name)
{
assoc_t *a = assoc_name(name, fmts);
if (a)
return a->value;
return -1;
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_bytes_to_frames(snd_pcm_t *handle, int bytes)
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
return bytes * 8 / handle->bits_per_frame;
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_frames_to_bytes(snd_pcm_t *handle, int frames)
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
return frames * handle->bits_per_frame / 8;
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_bytes_to_samples(snd_pcm_t *handle, int bytes)
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
return bytes * 8 / handle->bits_per_sample;
}
2000-06-21 14:59:20 +00:00
ssize_t snd_pcm_samples_to_bytes(snd_pcm_t *handle, int samples)
{
2000-06-21 14:59:20 +00:00
assert(handle);
assert(handle->valid_setup);
return samples * handle->bits_per_sample / 8;
}
2000-06-21 14:59:20 +00:00