Merged pcm-v2 branch into main CVS tree.

This commit is contained in:
Jaroslav Kysela 1999-11-06 23:47:07 +00:00
parent 4b8fda3997
commit 600dc6ae32
22 changed files with 11135 additions and 347 deletions

View file

@ -30,7 +30,7 @@ SAVE_LIBRARY_VERSION
AC_OUTPUT(Makefile doc/Makefile include/Makefile src/Makefile \
src/control/Makefile src/mixer/Makefile src/pcm/Makefile \
src/rawmidi/Makefile src/timer/Makefile src/seq/Makefile \
src/instr/Makefile \
src/pcm/plugin/Makefile src/rawmidi/Makefile src/timer/Makefile \
src/seq/Makefile src/instr/Makefile \
test/Makefile utils/Makefile \
utils/alsa-lib.spec)

View file

@ -40,15 +40,14 @@ int snd_ctl_switch_read(snd_ctl_t *handle, snd_switch_t * sw);
int snd_ctl_switch_write(snd_ctl_t *handle, snd_switch_t * sw);
int snd_ctl_hwdep_info(snd_ctl_t *handle, int dev, snd_hwdep_info_t * info);
int snd_ctl_pcm_info(snd_ctl_t *handle, int dev, snd_pcm_info_t * info);
int snd_ctl_pcm_playback_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_playback_info_t * info);
int snd_ctl_pcm_capture_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_capture_info_t * info);
int snd_ctl_pcm_channel_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_channel_info_t * info);
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *handle, int dev, int subdev);
int snd_ctl_pcm_playback_switch_list(snd_ctl_t *handle, int dev, snd_switch_list_t * list);
int snd_ctl_pcm_playback_switch_read(snd_ctl_t *handle, int dev, snd_switch_t * sw);
int snd_ctl_pcm_playback_switch_write(snd_ctl_t *handle, int dev, snd_switch_t * sw);
int snd_ctl_pcm_capture_switch_list(snd_ctl_t *handle, int dev, snd_switch_list_t * list);
int snd_ctl_pcm_capture_switch_read(snd_ctl_t *handle, int dev, snd_switch_t * sw);
int snd_ctl_pcm_capture_switch_write(snd_ctl_t *handle, int dev, snd_switch_t * sw);
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *handle, int dev, int subdev);
int snd_ctl_mixer_info(snd_ctl_t *handle, int dev, snd_mixer_info_t * info);
int snd_ctl_mixer_switch_list(snd_ctl_t *handle, int dev, snd_switch_list_t *list);
int snd_ctl_mixer_switch_read(snd_ctl_t *handle, int dev, snd_switch_t * sw);

View file

@ -5,9 +5,13 @@
* *
****************************************************************************/
#define SND_PCM_OPEN_PLAYBACK (O_WRONLY)
#define SND_PCM_OPEN_CAPTURE (O_RDONLY)
#define SND_PCM_OPEN_DUPLEX (O_RDWR)
#define SND_PCM_OPEN_PLAYBACK 0x0001
#define SND_PCM_OPEN_CAPTURE 0x0002
#define SND_PCM_OPEN_DUPLEX 0x0003
#define SND_PCM_OPEN_STREAM 0x1000
#define SND_PCM_OPEN_STREAM_PLAYBACK 0x1001
#define SND_PCM_OPEN_STREAM_CAPTURE 0x1002
#define SND_PCM_OPEN_STREAM_DUPLEX 0x1003
#ifdef __cplusplus
extern "C" {
@ -20,29 +24,99 @@ int snd_pcm_open(snd_pcm_t **handle, int card, int device, int mode);
int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevice, int mode);
int snd_pcm_close(snd_pcm_t *handle);
int snd_pcm_file_descriptor(snd_pcm_t *handle);
int snd_pcm_block_mode(snd_pcm_t *handle, int enable);
int snd_pcm_info(snd_pcm_t *handle, snd_pcm_info_t * info);
int snd_pcm_playback_info(snd_pcm_t *handle, snd_pcm_playback_info_t * info);
int snd_pcm_capture_info(snd_pcm_t *handle, snd_pcm_capture_info_t * info);
int snd_pcm_playback_format(snd_pcm_t *handle, snd_pcm_format_t * format);
int snd_pcm_capture_format(snd_pcm_t *handle, snd_pcm_format_t * format);
int snd_pcm_playback_params(snd_pcm_t *handle, snd_pcm_playback_params_t * params);
int snd_pcm_capture_params(snd_pcm_t *handle, snd_pcm_capture_params_t * params);
int snd_pcm_playback_status(snd_pcm_t *handle, snd_pcm_playback_status_t * status);
int snd_pcm_capture_status(snd_pcm_t *handle, snd_pcm_capture_status_t * status);
int snd_pcm_channel_info(snd_pcm_t *handle, snd_pcm_channel_info_t * info);
int snd_pcm_channel_params(snd_pcm_t *handle, snd_pcm_channel_params_t * params);
int snd_pcm_channel_setup(snd_pcm_t *handle, snd_pcm_channel_setup_t * setup);
int snd_pcm_channel_status(snd_pcm_t *handle, snd_pcm_channel_status_t * status);
int snd_pcm_playback_prepare(snd_pcm_t *handle);
int snd_pcm_capture_prepare(snd_pcm_t *handle);
int snd_pcm_channel_prepare(snd_pcm_t *handle, int channel);
int snd_pcm_playback_go(snd_pcm_t *handle);
int snd_pcm_capture_go(snd_pcm_t *handle);
int snd_pcm_channel_go(snd_pcm_t *handle, int channel);
int snd_pcm_sync_go(snd_pcm_t *handle, snd_pcm_sync_t *sync);
int snd_pcm_drain_playback(snd_pcm_t *handle);
int snd_pcm_flush_playback(snd_pcm_t *handle);
int snd_pcm_flush_capture(snd_pcm_t *handle);
int snd_pcm_flush_channel(snd_pcm_t *handle, int channel);
int snd_pcm_playback_pause(snd_pcm_t *handle, int enable);
int snd_pcm_playback_time(snd_pcm_t *handle, int enable);
int snd_pcm_capture_time(snd_pcm_t *handle, int enable);
ssize_t snd_pcm_transfer_size(snd_pcm_t *handle, int channel);
ssize_t snd_pcm_write(snd_pcm_t *handle, const void *buffer, size_t size);
ssize_t snd_pcm_read(snd_pcm_t *handle, void *buffer, size_t size);
int snd_pcm_mmap(snd_pcm_t *handle, int channel, snd_pcm_mmap_control_t **control, void **buffer);
int snd_pcm_munmap(snd_pcm_t *handle, int channel);
#ifdef __cplusplus
}
#endif
/*
* Plug-In interface (ala C++)
*/
typedef struct snd_stru_pcm_plugin snd_pcm_plugin_t;
typedef enum {
INIT = 0,
DRAIN = 1,
FLUSH = 2
} snd_pcm_plugin_action_t;
#define snd_pcm_plugin_extra_data(plugin) (((char *)plugin) + sizeof(*plugin))
struct snd_stru_pcm_plugin {
char *name; /* plug-in name */
int (*transfer_src_ptr)(snd_pcm_plugin_t *plugin, char **src_ptr, size_t *src_size);
ssize_t (*transfer)(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size);
ssize_t (*src_size)(snd_pcm_plugin_t *plugin, size_t dst_size);
ssize_t (*dst_size)(snd_pcm_plugin_t *plugin, size_t src_size);
int (*action)(snd_pcm_plugin_t *plugin, snd_pcm_plugin_action_t action);
snd_pcm_plugin_t *prev;
snd_pcm_plugin_t *next;
void *private_data;
void (*private_free)(snd_pcm_plugin_t *plugin, void *private_data);
};
snd_pcm_plugin_t *snd_pcm_plugin_build(const char *name, int extra);
int snd_pcm_plugin_free(snd_pcm_plugin_t *plugin);
int snd_pcm_plugin_clear(snd_pcm_t *handle, int channel);
int snd_pcm_plugin_insert(snd_pcm_t *handle, int channel, snd_pcm_plugin_t *plugin);
int snd_pcm_plugin_remove_to(snd_pcm_t *handle, int channel, snd_pcm_plugin_t *plugin);
int snd_pcm_plugin_remove_first(snd_pcm_t *handle, int channel);
snd_pcm_plugin_t *snd_pcm_plugin_first(snd_pcm_t *handle, int channel);
snd_pcm_plugin_t *snd_pcm_plugin_last(snd_pcm_t *handle, int channel);
ssize_t snd_pcm_plugin_transfer_size(snd_pcm_t *handle, int channel, size_t drv_size);
ssize_t snd_pcm_plugin_hardware_size(snd_pcm_t *handle, int channel, size_t trf_size);
int snd_pcm_plugin_info(snd_pcm_t *handle, snd_pcm_channel_info_t *info);
int snd_pcm_plugin_params(snd_pcm_t *handle, snd_pcm_channel_params_t *params);
int snd_pcm_plugin_setup(snd_pcm_t *handle, snd_pcm_channel_setup_t *setup);
int snd_pcm_plugin_status(snd_pcm_t *handle, snd_pcm_channel_status_t *status);
int snd_pcm_plugin_drain_playback(snd_pcm_t *handle);
int snd_pcm_plugin_flush(snd_pcm_t *handle, int channel);
int snd_pcm_plugin_pointer(snd_pcm_t *pcm, int channel, void **ptr, size_t *size);
ssize_t snd_pcm_plugin_write(snd_pcm_t *handle, const void *buffer, size_t size);
ssize_t snd_pcm_plugin_read(snd_pcm_t *handle, void *bufer, size_t size);
/*
* Plug-In constructors
*/
/* basic I/O */
int snd_pcm_plugin_build_stream(snd_pcm_t *handle, int channel, snd_pcm_plugin_t **r_plugin);
int snd_pcm_plugin_build_block(snd_pcm_t *handle, int channel, snd_pcm_plugin_t **r_plugin);
int snd_pcm_plugin_build_mmap(snd_pcm_t *handle, int channel, snd_pcm_plugin_t **r_plugin);
/* conversion plugins */
int snd_pcm_plugin_build_interleave(int src_interleave, int dst_interleave, int format, snd_pcm_plugin_t **r_plugin);
int snd_pcm_plugin_build_linear(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin);
int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin);
/*
* Loopback interface
*/
#define SND_PCM_LB_OPEN_PLAYBACK 0
#define SND_PCM_LB_OPEN_CAPTURE 1

View file

@ -29,7 +29,7 @@
#include "asoundlib.h"
#define SND_FILE_CONTROL "/dev/snd/controlC%i"
#define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 1)
#define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION(2, 1, 0)
struct snd_ctl {
int card;
@ -171,7 +171,7 @@ int snd_ctl_pcm_info(snd_ctl_t *handle, int dev, snd_pcm_info_t * info)
return 0;
}
int snd_ctl_pcm_playback_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_playback_info_t * info)
int snd_ctl_pcm_channel_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_channel_info_t * info)
{
snd_ctl_t *ctl;
@ -182,23 +182,21 @@ int snd_ctl_pcm_playback_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_pl
return -errno;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_SUBDEVICE, &subdev) < 0)
return -errno;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PLAYBACK_INFO, info) < 0)
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_CHANNEL_INFO, info) < 0)
return -errno;
return 0;
}
int snd_ctl_pcm_capture_info(snd_ctl_t *handle, int dev, int subdev, snd_pcm_capture_info_t * info)
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *handle, int dev, int subdev)
{
snd_ctl_t *ctl;
ctl = handle;
if (!ctl || !info || dev < 0 || subdev < 0)
if (!ctl || dev < 0)
return -EINVAL;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
return -errno;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_SUBDEVICE, &subdev) < 0)
return -errno;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_CAPTURE_INFO, info) < 0)
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
return -errno;
return 0;
}
@ -287,20 +285,6 @@ int snd_ctl_pcm_capture_switch_write(snd_ctl_t *handle, int dev, snd_switch_t *
return 0;
}
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *handle, int dev, int subdev)
{
snd_ctl_t *ctl;
ctl = handle;
if (!ctl || dev < 0)
return -EINVAL;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_DEVICE, &dev) < 0)
return -errno;
if (ioctl(ctl->fd, SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
return -errno;
return 0;
}
int snd_ctl_mixer_info(snd_ctl_t *handle, int dev, snd_mixer_info_t * info)
{
snd_ctl_t *ctl;

View file

@ -29,7 +29,7 @@
#include "asoundlib.h"
#define SND_FILE_MIXER "/dev/snd/mixerC%iD%i"
#define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION(2, 1, 0)
#define SND_MIXER_VERSION_MAX SND_PROTOCOL_VERSION(2, 2, 0)
struct snd_mixer {
int card;

View file

@ -1,6 +1,9 @@
EXTRA_LTLIBRARIES=libpcm.la
SUBDIRS = plugin
libpcm_la_SOURCES = pcm.c pcm_loopback.c
EXTRA_LTLIBRARIES = libpcm.la
libpcm_la_SOURCES = pcm.c pcm_plugin.c pcm_loopback.c
libpcm_la_LIBADD = plugin/libpcmplugin.la
all: libpcm.la

View file

@ -26,31 +26,31 @@
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "asoundlib.h"
#include <sys/mman.h>
#include "pcm_local.h"
#define SND_FILE_PCM "/dev/snd/pcmC%iD%i"
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 1 )
struct snd_pcm {
int card;
int device;
int fd;
int mode;
};
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
int snd_pcm_open(snd_pcm_t **handle, int card, int device, int mode)
{
return snd_pcm_open_subdevice(handle, card, device, -1, mode);
}
#ifndef O_WRITEFLG
#define O_WRITEFLG 0x10000000
#endif
#ifndef O_READFLG
#define O_READFLG 0x20000000
#endif
int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevice, int mode)
{
int fd, ver, err, attempt = 0;
int fd, fmode, ver, err, attempt = 0;
char filename[32];
snd_pcm_t *pcm;
snd_ctl_t *ctl;
snd_pcm_playback_info_t pinfo;
snd_pcm_capture_info_t cinfo;
snd_pcm_channel_info_t info;
*handle = NULL;
@ -58,6 +58,17 @@ int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevi
return -EINVAL;
if ((err = snd_ctl_open(&ctl, card)) < 0)
return err;
fmode = O_RDWR;
if (mode & SND_PCM_OPEN_PLAYBACK)
fmode |= O_WRITEFLG;
if (mode & SND_PCM_OPEN_CAPTURE)
fmode |= O_READFLG;
if (fmode == O_RDWR) {
snd_ctl_close(ctl);
return -EINVAL;
}
if (mode & SND_PCM_OPEN_STREAM)
fmode |= O_NONBLOCK;
__again:
if (attempt++ > 3) {
snd_ctl_close(ctl);
@ -68,7 +79,7 @@ int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevi
return err;
}
sprintf(filename, SND_FILE_PCM, card, device);
if ((fd = open(filename, mode)) < 0) {
if ((fd = open(filename, fmode)) < 0) {
err = -errno;
snd_ctl_close(ctl);
return err;
@ -84,26 +95,30 @@ int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevi
snd_ctl_close(ctl);
return -SND_ERROR_INCOMPATIBLE_VERSION;
}
if (subdevice >= 0 && (mode == SND_PCM_OPEN_PLAYBACK || mode == SND_PCM_OPEN_DUPLEX)) {
if (ioctl(fd, SND_PCM_IOCTL_PLAYBACK_INFO, &pinfo) < 0) {
if (subdevice >= 0 && (mode & SND_PCM_OPEN_PLAYBACK) != 0) {
memset(&info, 0, sizeof(info));
info.channel = SND_PCM_CHANNEL_PLAYBACK;
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_INFO, &info) < 0) {
err = -errno;
close(fd);
snd_ctl_close(ctl);
return err;
}
if (pinfo.subdevice != subdevice) {
if (info.subdevice != subdevice) {
close(fd);
goto __again;
}
}
if (subdevice >= 0 && (mode == SND_PCM_OPEN_CAPTURE || mode == SND_PCM_OPEN_DUPLEX)) {
if (ioctl(fd, SND_PCM_IOCTL_CAPTURE_INFO, &cinfo) < 0) {
if (subdevice >= 0 && (mode & SND_PCM_OPEN_CAPTURE) != 0) {
memset(&info, 0, sizeof(info));
info.channel = SND_PCM_CHANNEL_CAPTURE;
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_INFO, &info) < 0) {
err = -errno;
close(fd);
snd_ctl_close(ctl);
return err;
}
if (cinfo.subdevice != subdevice) {
if (info.subdevice != subdevice) {
close(fd);
goto __again;
}
@ -122,53 +137,30 @@ int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevi
return 0;
}
int snd_pcm_close(snd_pcm_t *handle)
int snd_pcm_close(snd_pcm_t *pcm)
{
snd_pcm_t *pcm;
int res;
pcm = handle;
if (!pcm)
return -EINVAL;
snd_pcm_munmap(pcm, SND_PCM_CHANNEL_PLAYBACK);
snd_pcm_munmap(pcm, SND_PCM_CHANNEL_CAPTURE);
snd_pcm_plugin_clear(pcm, SND_PCM_CHANNEL_PLAYBACK);
snd_pcm_plugin_clear(pcm, SND_PCM_CHANNEL_CAPTURE);
res = close(pcm->fd) < 0 ? -errno : 0;
free(pcm);
return res;
}
int snd_pcm_file_descriptor(snd_pcm_t *handle)
int snd_pcm_file_descriptor(snd_pcm_t *pcm)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm)
return -EINVAL;
return pcm->fd;
}
int snd_pcm_block_mode(snd_pcm_t *handle, int enable)
int snd_pcm_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
{
snd_pcm_t *pcm;
long flags;
pcm = handle;
if (!pcm)
return -EINVAL;
if ((flags = fcntl(pcm->fd, F_GETFL)) < 0)
return -errno;
if (enable)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
if (fcntl(pcm->fd, F_SETFL, flags) < 0)
return -errno;
return 0;
}
int snd_pcm_info(snd_pcm_t *handle, snd_pcm_info_t * info)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !info)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_INFO, info) < 0)
@ -176,107 +168,131 @@ int snd_pcm_info(snd_pcm_t *handle, snd_pcm_info_t * info)
return 0;
}
int snd_pcm_playback_info(snd_pcm_t *handle, snd_pcm_playback_info_t * info)
int snd_pcm_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !info)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_INFO, info) < 0)
if (ioctl(pcm->fd, SND_PCM_IOCTL_CHANNEL_INFO, info) < 0)
return -errno;
return 0;
}
int snd_pcm_capture_info(snd_pcm_t *handle, snd_pcm_capture_info_t * info)
int snd_pcm_channel_params(snd_pcm_t *pcm, snd_pcm_channel_params_t * params)
{
snd_pcm_t *pcm;
int err;
pcm = handle;
if (!pcm || !info)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_INFO, info) < 0)
return -errno;
return 0;
}
int snd_pcm_playback_format(snd_pcm_t *handle, snd_pcm_format_t * format)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !format)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_FORMAT, format) < 0)
return -errno;
return 0;
}
int snd_pcm_capture_format(snd_pcm_t *handle, snd_pcm_format_t * format)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !format)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_FORMAT, format) < 0)
return -errno;
return 0;
}
int snd_pcm_playback_params(snd_pcm_t *handle, snd_pcm_playback_params_t * params)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !params)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PARAMS, params) < 0)
return -errno;
return 0;
}
int snd_pcm_capture_params(snd_pcm_t *handle, snd_pcm_capture_params_t * params)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !params)
if (params->channel < 0 || params->channel > 1)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_PARAMS, params) < 0)
if (ioctl(pcm->fd, SND_PCM_IOCTL_CHANNEL_PARAMS, params) < 0)
return -errno;
pcm->setup_is_valid[params->channel] = 0;
memset(&pcm->setup[params->channel], 0, sizeof(snd_pcm_channel_setup_t));
pcm->setup[params->channel].channel = params->channel;
if ((err = snd_pcm_channel_setup(pcm, &pcm->setup[params->channel]))<0)
return err;
pcm->setup_is_valid[params->channel] = 1;
return 0;
}
int snd_pcm_playback_status(snd_pcm_t *handle, snd_pcm_playback_status_t * status)
int snd_pcm_channel_setup(snd_pcm_t *pcm, snd_pcm_channel_setup_t * setup)
{
snd_pcm_t *pcm;
if (!pcm || !setup)
return -EINVAL;
if (setup->channel < 0 || setup->channel > 1)
return -EINVAL;
if (pcm->setup_is_valid[setup->channel]) {
memcpy(setup, &pcm->setup[setup->channel], sizeof(*setup));
} else {
if (ioctl(pcm->fd, SND_PCM_IOCTL_CHANNEL_SETUP, setup) < 0)
return -errno;
memcpy(&pcm->setup[setup->channel], setup, sizeof(*setup));
pcm->setup_is_valid[setup->channel] = 1;
}
return 0;
}
pcm = handle;
int snd_pcm_channel_status(snd_pcm_t *pcm, snd_pcm_channel_status_t * status)
{
if (!pcm || !status)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_STATUS, status) < 0)
if (ioctl(pcm->fd, SND_PCM_IOCTL_CHANNEL_STATUS, status) < 0)
return -errno;
return 0;
}
int snd_pcm_capture_status(snd_pcm_t *handle, snd_pcm_capture_status_t * status)
int snd_pcm_playback_prepare(snd_pcm_t *pcm)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm || !status)
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_STATUS, status) < 0)
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PREPARE) < 0)
return -errno;
return 0;
}
int snd_pcm_drain_playback(snd_pcm_t *handle)
int snd_pcm_capture_prepare(snd_pcm_t *pcm)
{
snd_pcm_t *pcm;
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_PREPARE) < 0)
return -errno;
return 0;
}
pcm = handle;
int snd_pcm_channel_prepare(snd_pcm_t *pcm, int channel)
{
switch (channel) {
case SND_PCM_CHANNEL_PLAYBACK:
return snd_pcm_playback_prepare(pcm);
case SND_PCM_CHANNEL_CAPTURE:
return snd_pcm_capture_prepare(pcm);
default:
return -EIO;
}
}
int snd_pcm_playback_go(snd_pcm_t *pcm)
{
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_GO) < 0)
return -errno;
return 0;
}
int snd_pcm_capture_go(snd_pcm_t *pcm)
{
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_GO) < 0)
return -errno;
return 0;
}
int snd_pcm_channel_go(snd_pcm_t *pcm, int channel)
{
switch (channel) {
case SND_PCM_CHANNEL_PLAYBACK:
return snd_pcm_playback_go(pcm);
case SND_PCM_CHANNEL_CAPTURE:
return snd_pcm_capture_go(pcm);
default:
return -EIO;
}
}
int snd_pcm_sync_go(snd_pcm_t *pcm, snd_pcm_sync_t *sync)
{
if (!pcm || !sync)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_SYNC_GO, sync) < 0)
return -errno;
return 0;
}
int snd_pcm_drain_playback(snd_pcm_t *pcm)
{
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_DRAIN_PLAYBACK) < 0)
@ -284,11 +300,8 @@ int snd_pcm_drain_playback(snd_pcm_t *handle)
return 0;
}
int snd_pcm_flush_playback(snd_pcm_t *handle)
int snd_pcm_flush_playback(snd_pcm_t *pcm)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_FLUSH_PLAYBACK) < 0)
@ -296,11 +309,8 @@ int snd_pcm_flush_playback(snd_pcm_t *handle)
return 0;
}
int snd_pcm_flush_capture(snd_pcm_t *handle)
int snd_pcm_flush_capture(snd_pcm_t *pcm)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_FLUSH_CAPTURE) < 0)
@ -308,11 +318,20 @@ int snd_pcm_flush_capture(snd_pcm_t *handle)
return 0;
}
int snd_pcm_playback_pause(snd_pcm_t *handle, int enable)
int snd_pcm_flush_channel(snd_pcm_t *pcm, int channel)
{
snd_pcm_t *pcm;
switch (channel) {
case SND_PCM_CHANNEL_PLAYBACK:
return snd_pcm_flush_playback(pcm);
case SND_PCM_CHANNEL_CAPTURE:
return snd_pcm_flush_capture(pcm);
default:
return -EIO;
}
}
pcm = handle;
int snd_pcm_playback_pause(snd_pcm_t *pcm, int enable)
{
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_PAUSE, &enable) < 0)
@ -320,36 +339,21 @@ int snd_pcm_playback_pause(snd_pcm_t *handle, int enable)
return 0;
}
int snd_pcm_playback_time(snd_pcm_t *handle, int enable)
ssize_t snd_pcm_transfer_size(snd_pcm_t *pcm, int channel)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm)
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_PLAYBACK_TIME, &enable) < 0)
return -errno;
return 0;
if (!pcm->setup_is_valid[channel])
return -EBADFD;
if (pcm->setup[channel].mode != SND_PCM_MODE_BLOCK)
return -EBADFD;
return pcm->setup[channel].buf.block.frag_size;
}
int snd_pcm_capture_time(snd_pcm_t *handle, int enable)
ssize_t snd_pcm_write(snd_pcm_t *pcm, const void *buffer, size_t size)
{
snd_pcm_t *pcm;
pcm = handle;
if (!pcm)
return -EINVAL;
if (ioctl(pcm->fd, SND_PCM_IOCTL_CAPTURE_TIME, &enable) < 0)
return -errno;
return 0;
}
ssize_t snd_pcm_write(snd_pcm_t *handle, const void *buffer, size_t size)
{
snd_pcm_t *pcm;
ssize_t result;
pcm = handle;
if (!pcm || (!buffer && size > 0) || size < 0)
return -EINVAL;
result = write(pcm->fd, buffer, size);
@ -358,12 +362,10 @@ ssize_t snd_pcm_write(snd_pcm_t *handle, const void *buffer, size_t size)
return result;
}
ssize_t snd_pcm_read(snd_pcm_t *handle, void *buffer, size_t size)
ssize_t snd_pcm_read(snd_pcm_t *pcm, void *buffer, size_t size)
{
snd_pcm_t *pcm;
ssize_t result;
pcm = handle;
if (!pcm || (!buffer && size > 0) || size < 0)
return -EINVAL;
result = read(pcm->fd, buffer, size);
@ -371,3 +373,57 @@ ssize_t snd_pcm_read(snd_pcm_t *handle, void *buffer, size_t size)
return -errno;
return result;
}
int snd_pcm_mmap(snd_pcm_t *pcm, int channel, snd_pcm_mmap_control_t **control, void **buffer)
{
snd_pcm_channel_info_t info;
int err;
void *caddr, *daddr;
off_t offset;
if (control)
*control = NULL;
if (buffer)
*buffer = NULL;
if (!pcm || channel < 0 || channel > 1 || !control || !buffer)
return -EINVAL;
memset(&info, 0, sizeof(info));
info.channel = channel;
if ((err = snd_pcm_channel_info(pcm, &info))<0)
return err;
offset = channel == SND_PCM_CHANNEL_PLAYBACK ?
SND_PCM_MMAP_OFFSET_PLAYBACK_CONTROL :
SND_PCM_MMAP_OFFSET_CAPTURE_CONTROL;
caddr = mmap(NULL, sizeof(snd_pcm_mmap_control_t), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, pcm->fd, offset);
if (caddr == (caddr_t)-1 || caddr == NULL)
return -errno;
offset = channel == SND_PCM_CHANNEL_PLAYBACK ?
SND_PCM_MMAP_OFFSET_PLAYBACK :
SND_PCM_MMAP_OFFSET_CAPTURE;
daddr = mmap(NULL, info.mmap_size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, pcm->fd, offset);
if (daddr == (caddr_t)-1 || daddr == NULL) {
err = -errno;
munmap(caddr, sizeof(snd_pcm_mmap_control_t));
return err;
}
*control = pcm->mmap_caddr[channel] = caddr;
*buffer = pcm->mmap_daddr[channel] = daddr;
pcm->mmap_size[channel] = info.mmap_size;
return 0;
}
int snd_pcm_munmap(snd_pcm_t *pcm, int channel)
{
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
if (pcm->mmap_caddr[channel]) {
munmap(pcm->mmap_caddr[channel], sizeof(snd_pcm_mmap_control_t));
pcm->mmap_caddr[channel] = NULL;
}
if (pcm->mmap_daddr[channel]) {
munmap(pcm->mmap_daddr[channel], pcm->mmap_size[channel]);
pcm->mmap_daddr[channel] = NULL;
pcm->mmap_size[channel] = 0;
}
return 0;
}

41
src/pcm/pcm_local.h Normal file
View file

@ -0,0 +1,41 @@
/*
* PCM Interface - local header file
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include "asoundlib.h"
struct snd_pcm {
int card;
int device;
int fd;
int mode;
int setup_is_valid[2];
snd_pcm_channel_setup_t setup[2];
snd_pcm_mmap_control_t *mmap_caddr[2];
char *mmap_daddr[2];
long mmap_size[2];
snd_pcm_plugin_t *plugin_first[2];
snd_pcm_plugin_t *plugin_last[2];
void *plugin_alloc_ptr[4];
long plugin_alloc_size[4];
int plugin_alloc_lock[4];
void *plugin_alloc_xptr[2];
long plugin_alloc_xsize[2];
};

739
src/pcm/pcm_plugin.c Normal file
View file

@ -0,0 +1,739 @@
/*
* PCM Plug-In Interface
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "pcm_local.h"
snd_pcm_plugin_t *snd_pcm_plugin_build(const char *name, int extra)
{
snd_pcm_plugin_t *plugin;
if (extra < 0)
return NULL;
plugin = (snd_pcm_plugin_t *)calloc(1, sizeof(*plugin) + extra);
if (plugin == NULL)
return NULL;
plugin->name = name ? strdup(name) : NULL;
return plugin;
}
int snd_pcm_plugin_free(snd_pcm_plugin_t *plugin)
{
if (plugin) {
if (plugin->private_free)
plugin->private_free(plugin, plugin->private_data);
if (plugin->name)
free(plugin->name);
free(plugin);
}
return 0;
}
int snd_pcm_plugin_clear(snd_pcm_t *pcm, int channel)
{
snd_pcm_plugin_t *plugin, *plugin_next;
int idx;
if (!pcm)
return -EINVAL;
plugin = pcm->plugin_first[channel];
pcm->plugin_first[channel] = NULL;
pcm->plugin_last[channel] = NULL;
while (plugin) {
plugin_next = plugin->next;
snd_pcm_plugin_free(plugin);
plugin = plugin_next;
}
for (idx = 0; idx < 4; idx++) {
if (pcm->plugin_alloc_ptr[idx])
free(pcm->plugin_alloc_ptr[idx]);
pcm->plugin_alloc_ptr[idx] = 0;
pcm->plugin_alloc_size[idx] = 0;
pcm->plugin_alloc_lock[idx] = 0;
}
if (pcm->plugin_alloc_xptr[channel])
free(pcm->plugin_alloc_xptr[channel]);
pcm->plugin_alloc_xptr[channel] = NULL;
pcm->plugin_alloc_xsize[channel] = 0;
return 0;
}
int snd_pcm_plugin_insert(snd_pcm_t *pcm, int channel, snd_pcm_plugin_t *plugin)
{
if (!pcm || channel < 0 || channel > 1 || !plugin)
return -EINVAL;
plugin->next = pcm->plugin_first[channel];
plugin->prev = NULL;
if (pcm->plugin_first[channel]) {
pcm->plugin_first[channel]->prev = plugin;
pcm->plugin_first[channel] = plugin;
} else {
pcm->plugin_last[channel] =
pcm->plugin_first[channel] = plugin;
}
return 0;
}
int snd_pcm_plugin_remove_to(snd_pcm_t *pcm, int channel, snd_pcm_plugin_t *plugin)
{
snd_pcm_plugin_t *plugin1, *plugin1_prev;
if (!pcm || channel < 0 || channel > 1 || !plugin || !plugin->prev)
return -EINVAL;
plugin1 = plugin;
while (plugin1->prev)
plugin1 = plugin1->prev;
if (pcm->plugin_first[channel] != plugin1)
return -EINVAL;
pcm->plugin_first[channel] = plugin;
plugin1 = plugin->prev;
plugin->prev = NULL;
while (plugin1) {
plugin1_prev = plugin1->prev;
snd_pcm_plugin_free(plugin1);
plugin1 = plugin1_prev;
}
return 0;
}
int snd_pcm_plugin_remove_first(snd_pcm_t *pcm, int channel)
{
snd_pcm_plugin_t *plugin;
plugin = snd_pcm_plugin_first(pcm, channel);
if (plugin->next) {
plugin = plugin->next;
} else {
return snd_pcm_plugin_clear(pcm, channel);
}
return snd_pcm_plugin_remove_to(pcm, channel, plugin);
}
snd_pcm_plugin_t *snd_pcm_plugin_first(snd_pcm_t *pcm, int channel)
{
if (!pcm || channel < 0 || channel > 1)
return NULL;
return pcm->plugin_first[channel];
}
snd_pcm_plugin_t *snd_pcm_plugin_last(snd_pcm_t *pcm, int channel)
{
if (!pcm || channel < 0 || channel > 1)
return NULL;
return pcm->plugin_last[channel];
}
ssize_t snd_pcm_plugin_transfer_size(snd_pcm_t *pcm, int channel, size_t drv_size)
{
snd_pcm_plugin_t *plugin, *plugin_prev, *plugin_next;
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
if (drv_size == 0)
return 0;
if (drv_size < 0)
return -EINVAL;
if (channel == SND_PCM_CHANNEL_PLAYBACK) {
plugin = snd_pcm_plugin_last(pcm, channel);
while (plugin) {
plugin_prev = plugin->prev;
if (plugin->src_size)
drv_size = plugin->src_size(plugin, drv_size);
plugin = plugin_prev;
}
} else if (channel == SND_PCM_CHANNEL_CAPTURE) {
plugin = snd_pcm_plugin_first(pcm, channel);
while (plugin) {
plugin_next = plugin->next;
if (plugin->dst_size)
drv_size = plugin->dst_size(plugin, drv_size);
plugin = plugin_next;
}
}
return drv_size;
}
ssize_t snd_pcm_plugin_hardware_size(snd_pcm_t *pcm, int channel, size_t trf_size)
{
snd_pcm_plugin_t *plugin, *plugin_prev, *plugin_next;
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
if (trf_size == 0)
return 0;
if (trf_size < 0)
return -EINVAL;
if (channel == SND_PCM_CHANNEL_PLAYBACK) {
plugin = snd_pcm_plugin_first(pcm, channel);
while (plugin) {
plugin_next = plugin->next;
if (plugin->dst_size)
trf_size = plugin->dst_size(plugin, trf_size);
plugin = plugin_next;
}
} else if (channel == SND_PCM_CHANNEL_CAPTURE) {
plugin = snd_pcm_plugin_first(pcm, channel);
while (plugin) {
plugin_prev = plugin->prev;
if (plugin->src_size)
trf_size = plugin->src_size(plugin, trf_size);
plugin = plugin_prev;
}
}
return trf_size;
}
double snd_pcm_plugin_transfer_ratio(snd_pcm_t *pcm, int channel)
{
ssize_t transfer;
transfer = snd_pcm_plugin_transfer_size(pcm, channel, 100000);
if (transfer < 0)
return 0;
return (double)transfer / (double)100000;
}
double snd_pcm_plugin_hardware_ratio(snd_pcm_t *pcm, int channel)
{
ssize_t hardware;
hardware = snd_pcm_plugin_hardware_size(pcm, channel, 100000);
if (hardware < 0)
return 0;
return (double)hardware / (double)100000;
}
/*
*
*/
static unsigned int snd_pcm_plugin_formats(snd_pcm_t *pcm, unsigned int formats)
{
formats |= SND_PCM_FMT_MU_LAW;
if (formats & (SND_PCM_SFMT_U8|SND_PCM_SFMT_S8|
SND_PCM_SFMT_U16_LE|SND_PCM_SFMT_S16_LE))
formats |= SND_PCM_SFMT_U8|SND_PCM_SFMT_S8|
SND_PCM_SFMT_U16_LE|SND_PCM_SFMT_S16_LE;
return formats;
}
int snd_pcm_plugin_info(snd_pcm_t *pcm, snd_pcm_channel_info_t *info)
{
int err;
if ((err = snd_pcm_channel_info(pcm, info)) < 0)
return err;
info->formats = snd_pcm_plugin_formats(pcm, info->formats);
return 0;
}
static int snd_pcm_plugin_action(snd_pcm_t *pcm, int channel, int action)
{
snd_pcm_plugin_t *plugin;
int err;
plugin = pcm->plugin_first[channel];
while (plugin) {
if (plugin->action) {
if ((err = plugin->action(plugin, action))<0)
return err;
}
plugin = plugin->next;
}
return 0;
}
static void swap_formats(int channel, int *src, int *dst)
{
int tmp;
if (channel == SND_PCM_CHANNEL_PLAYBACK)
return;
tmp = *src;
*src = *dst;
*dst = tmp;
}
#define CONVERT_RATIO(dest, ratio) dest = (int)((double)dest * ratio)
int snd_pcm_plugin_params(snd_pcm_t *pcm, snd_pcm_channel_params_t *params)
{
double ratio;
snd_pcm_channel_params_t sparams;
snd_pcm_channel_info_t sinfo;
snd_pcm_plugin_t *plugin;
int err, srcfmt, dstfmt;
if (!pcm || !params || params->channel < 0 || params->channel > 1)
return -EINVAL;
memcpy(&sparams, params, sizeof(sparams));
/*
* try to decide, if a conversion is required
*/
memset(&sinfo, 0, sizeof(sinfo));
sinfo.channel = params->channel;
if ((err = snd_pcm_channel_info(pcm, &sinfo)) < 0) {
snd_pcm_plugin_clear(pcm, params->channel);
return err;
}
if ((sinfo.formats & (1 << sparams.format.format)) == 0) {
if ((snd_pcm_plugin_formats(pcm, sinfo.formats) & (1 << sparams.format.format)) == 0)
return -EINVAL;
switch (sparams.format.format) {
case SND_PCM_SFMT_U8:
if (sinfo.formats & SND_PCM_SFMT_S8) {
sparams.format.format = SND_PCM_SFMT_S8;
} else if (sinfo.formats & SND_PCM_SFMT_U16_LE) {
sparams.format.format = SND_PCM_SFMT_U16_LE;
} else if (sinfo.formats & SND_PCM_SFMT_S16_LE) {
sparams.format.format = SND_PCM_SFMT_S16_LE;
} else {
return -EINVAL;
}
break;
case SND_PCM_SFMT_S8:
if (sinfo.formats & SND_PCM_SFMT_U8) {
sparams.format.format = SND_PCM_SFMT_U8;
} else if (sinfo.formats & SND_PCM_SFMT_S16_LE) {
sparams.format.format = SND_PCM_SFMT_S16_LE;
} else if (sinfo.formats & SND_PCM_SFMT_U16_LE) {
sparams.format.format = SND_PCM_SFMT_U16_LE;
} else {
return -EINVAL;
}
break;
case SND_PCM_SFMT_S16_LE:
if (sinfo.formats & SND_PCM_SFMT_U16_LE) {
sparams.format.format = SND_PCM_SFMT_U16_LE;
} else if (sinfo.formats & SND_PCM_SFMT_S8) {
sparams.format.format = SND_PCM_SFMT_S8;
} else if (sinfo.formats & SND_PCM_SFMT_U8) {
sparams.format.format = SND_PCM_SFMT_U8;
} else {
return -EINVAL;
}
break;
case SND_PCM_SFMT_U16_LE:
if (sinfo.formats & SND_PCM_SFMT_S16_LE) {
sparams.format.format = SND_PCM_SFMT_S16_LE;
} else if (sinfo.formats & SND_PCM_SFMT_U8) {
sparams.format.format = SND_PCM_SFMT_U8;
} else if (sinfo.formats & SND_PCM_SFMT_S8) {
sparams.format.format = SND_PCM_SFMT_S8;
} else {
return -EINVAL;
}
break;
case SND_PCM_SFMT_MU_LAW:
if (sinfo.formats & SND_PCM_SFMT_S16_LE) {
sparams.format.format = SND_PCM_SFMT_S16_LE;
} else if (sinfo.formats & SND_PCM_SFMT_U16_LE) {
sparams.format.format = SND_PCM_SFMT_U16_LE;
} else if (sinfo.formats & SND_PCM_SFMT_S8) {
sparams.format.format = SND_PCM_SFMT_S8;
} else if (sinfo.formats & SND_PCM_SFMT_U8) {
sparams.format.format = SND_PCM_SFMT_U8;
} else {
return -EINVAL;
}
break;
default:
return -EINVAL;
}
}
/*
* add necessary plugins
*/
snd_pcm_plugin_clear(pcm, params->channel);
if (sinfo.mode == SND_PCM_MODE_STREAM) {
err = snd_pcm_plugin_build_stream(pcm, params->channel, &plugin);
} else if (sinfo.mode == SND_PCM_MODE_BLOCK) {
if (sinfo.flags & SND_PCM_CHNINFO_MMAP) {
err = snd_pcm_plugin_build_mmap(pcm, params->channel, &plugin);
} else {
err = snd_pcm_plugin_build_block(pcm, params->channel, &plugin);
}
} else {
return -EINVAL;
}
if (err < 0)
return err;
err = snd_pcm_plugin_insert(pcm, params->channel, plugin);
if (err < 0) {
snd_pcm_plugin_free(plugin);
return err;
}
if (params->format.voices > 1 && sinfo.mode == SND_PCM_MODE_BLOCK) {
int src_interleave, dst_interleave;
if (params->format.interleave) {
src_interleave = dst_interleave = 1;
if (!(sinfo.flags & SND_PCM_CHNINFO_INTERLEAVE))
dst_interleave = 0;
} else {
src_interleave = dst_interleave = 0;
if (!(sinfo.flags & SND_PCM_CHNINFO_NONINTERLEAVE))
dst_interleave = 1;
}
if (src_interleave != dst_interleave) {
sparams.format.interleave = dst_interleave;
swap_formats(params->channel, &src_interleave, &dst_interleave);
err = snd_pcm_plugin_build_interleave(src_interleave, dst_interleave, sparams.format.format, &plugin);
if (err < 0)
return err;
err = snd_pcm_plugin_insert(pcm, params->channel, plugin);
if (err < 0) {
snd_pcm_plugin_free(plugin);
return err;
}
}
}
if (params->format.format == sparams.format.format)
goto __skip;
/* build additional plugins for conversion */
switch (params->format.format) {
case SND_PCM_SFMT_MU_LAW:
srcfmt = SND_PCM_SFMT_MU_LAW;
dstfmt = sparams.format.format;
swap_formats(params->channel, &srcfmt, &dstfmt);
err = snd_pcm_plugin_build_mulaw(srcfmt, dstfmt, &plugin);
break;
default:
srcfmt = params->format.format;
dstfmt = sparams.format.format;
swap_formats(params->channel, &srcfmt, &dstfmt);
err = snd_pcm_plugin_build_linear(srcfmt, dstfmt, &plugin);
}
if (err < 0)
return err;
err = snd_pcm_plugin_insert(pcm, params->channel, plugin);
if (err < 0) {
snd_pcm_plugin_free(plugin);
return err;
}
__skip:
ratio = snd_pcm_plugin_hardware_ratio(pcm, params->channel);
if (ratio <= 0)
return -EINVAL;
if (params->mode == SND_PCM_MODE_STREAM) {
CONVERT_RATIO(sparams.buf.stream.queue_size, ratio);
CONVERT_RATIO(sparams.buf.stream.max_fill, ratio);
} else if (params->mode == SND_PCM_MODE_BLOCK) {
CONVERT_RATIO(sparams.buf.block.frag_size, ratio);
} else {
return -EINVAL;
}
err = snd_pcm_channel_params(pcm, &sparams);
if (err < 0)
return err;
err = snd_pcm_plugin_action(pcm, sparams.channel, INIT);
if (err < 0)
return err;
return 0;
}
int snd_pcm_plugin_setup(snd_pcm_t *pcm, snd_pcm_channel_setup_t *setup)
{
double ratio;
int err;
if (!pcm || !setup || setup->channel < 0 || setup->channel > 1)
return -EINVAL;
err = snd_pcm_channel_setup(pcm, setup);
if (err < 0)
return err;
ratio = snd_pcm_plugin_transfer_ratio(pcm, setup->channel);
if (ratio <= 0)
return -EINVAL;
if (setup->mode == SND_PCM_MODE_STREAM) {
CONVERT_RATIO(setup->buf.stream.queue_size, ratio);
} else if (setup->mode == SND_PCM_MODE_BLOCK) {
CONVERT_RATIO(setup->buf.block.frag_size, ratio);
} else {
return -EINVAL;
}
return 0;
}
int snd_pcm_plugin_status(snd_pcm_t *pcm, snd_pcm_channel_status_t *status)
{
double ratio;
int err;
if (!pcm || !status || status->channel < 0 || status->channel > 1)
return -EINVAL;
err = snd_pcm_channel_status(pcm, status);
if (err < 0)
return err;
ratio = snd_pcm_plugin_transfer_ratio(pcm, status->channel);
if (ratio <= 0)
return -EINVAL;
CONVERT_RATIO(status->scount, ratio);
CONVERT_RATIO(status->count, ratio);
CONVERT_RATIO(status->free, ratio);
return 0;
}
int snd_pcm_plugin_drain_playback(snd_pcm_t *pcm)
{
int err;
if ((err = snd_pcm_plugin_action(pcm, SND_PCM_CHANNEL_PLAYBACK, DRAIN))<0)
return err;
return snd_pcm_drain_playback(pcm);
}
int snd_pcm_plugin_flush(snd_pcm_t *pcm, int channel)
{
int err;
if ((err = snd_pcm_plugin_action(pcm, channel, FLUSH))<0)
return err;
return snd_pcm_flush_channel(pcm, channel);
}
int snd_pcm_plugin_pointer(snd_pcm_t *pcm, int channel, void **ptr, size_t *size)
{
snd_pcm_plugin_t *plugin;
int err;
if (!ptr || !size)
return -EINVAL;
*ptr = NULL;
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
plugin = pcm->plugin_first[channel];
if (!plugin)
return -EINVAL;
if (plugin->transfer_src_ptr) {
err = plugin->transfer_src_ptr(plugin, (char **)ptr, size);
if (err >= 0)
return 0;
}
if (pcm->plugin_alloc_xptr[channel]) {
if (pcm->plugin_alloc_xsize[channel] >= *size) {
*ptr = (char *)pcm->plugin_alloc_xptr[channel];
return 0;
}
*ptr = (char *)realloc(pcm->plugin_alloc_xptr[channel], *size);
} else {
*ptr = (char *)malloc(*size);
if (*ptr != NULL)
pcm->plugin_alloc_xsize[channel] = *size;
}
if (*ptr == NULL)
return -ENOMEM;
pcm->plugin_alloc_xptr[channel] = *ptr;
return 0;
}
static void *snd_pcm_plugin_malloc(snd_pcm_t *pcm, long size)
{
int idx;
void *ptr;
for (idx = 0; idx < 4; idx++) {
if (pcm->plugin_alloc_lock[idx])
continue;
if (pcm->plugin_alloc_ptr[idx] == NULL)
continue;
if (pcm->plugin_alloc_size[idx] >= size) {
pcm->plugin_alloc_lock[idx] = 1;
return pcm->plugin_alloc_ptr[idx];
}
}
for (idx = 0; idx < 4; idx++) {
if (pcm->plugin_alloc_lock[idx])
continue;
if (pcm->plugin_alloc_ptr[idx] == NULL)
continue;
ptr = realloc(pcm->plugin_alloc_ptr[idx], size);
if (ptr == NULL)
continue;
pcm->plugin_alloc_size[idx] = size;
pcm->plugin_alloc_lock[idx] = 1;
return pcm->plugin_alloc_ptr[idx] = ptr;
}
for (idx = 0; idx < 4; idx++) {
if (pcm->plugin_alloc_ptr[idx] != NULL)
continue;
ptr = malloc(size);
if (ptr == NULL)
continue;
pcm->plugin_alloc_size[idx] = size;
pcm->plugin_alloc_lock[idx] = 1;
return pcm->plugin_alloc_ptr[idx] = ptr;
}
return NULL;
}
static int snd_pcm_plugin_alloc_unlock(snd_pcm_t *pcm, void *ptr)
{
int idx;
for (idx = 0; idx < 4; idx++) {
if (pcm->plugin_alloc_ptr[idx] == ptr) {
pcm->plugin_alloc_lock[idx] = 0;
return 0;
}
}
return -ENOENT;
}
ssize_t snd_pcm_plugin_write(snd_pcm_t *pcm, const void *buffer, size_t count)
{
snd_pcm_plugin_t *plugin, *next;
char *dst_ptr, *dst_ptr1 = NULL, *src_ptr, *src_ptr1 = NULL;
size_t dst_size, src_size;
ssize_t size = 0, result = 0;
int err;
if ((plugin = snd_pcm_plugin_first(pcm, SND_PCM_CHANNEL_PLAYBACK)) == NULL)
return snd_pcm_write(pcm, buffer, count);
src_ptr = (char *)buffer;
dst_size = src_size = count;
while (plugin) {
next = plugin->next;
if (plugin->dst_size) {
dst_size = plugin->dst_size(plugin, dst_size);
if (dst_size < 0) {
result = dst_size;
goto __free;
}
}
if (next != NULL) {
if (next->transfer_src_ptr) {
if ((err = next->transfer_src_ptr(next, &dst_ptr, &dst_size)) < 0) {
if (dst_ptr == NULL)
goto __alloc;
result = err;
goto __free;
}
} else {
__alloc:
dst_ptr = dst_ptr1 = (char *)snd_pcm_plugin_malloc(pcm, dst_size);
if (dst_ptr == NULL) {
result = -ENOMEM;
goto __free;
}
}
} else {
dst_ptr = src_ptr;
dst_size = src_size;
}
if ((size = plugin->transfer(plugin, src_ptr, src_size,
dst_ptr, dst_size))<0) {
result = size;
goto __free;
}
if (src_ptr1)
snd_pcm_plugin_alloc_unlock(pcm, src_ptr1);
plugin = plugin->next;
src_ptr = dst_ptr;
src_ptr1 = dst_ptr1;
dst_ptr1 = NULL;
src_size = size;
}
result = snd_pcm_plugin_transfer_size(pcm, SND_PCM_CHANNEL_PLAYBACK, size);
__free:
if (dst_ptr1)
snd_pcm_plugin_alloc_unlock(pcm, dst_ptr1);
if (src_ptr1)
snd_pcm_plugin_alloc_unlock(pcm, src_ptr1);
return result;
}
ssize_t snd_pcm_plugin_read(snd_pcm_t *pcm, void *buffer, size_t count)
{
snd_pcm_plugin_t *plugin, *prev;
char *dst_ptr, *dst_ptr1 = NULL, *src_ptr, *src_ptr1 = NULL;
size_t dst_size, src_size;
ssize_t size = 0, result = 0;
int err;
if ((plugin = snd_pcm_plugin_last(pcm, SND_PCM_CHANNEL_CAPTURE)) == NULL)
return snd_pcm_read(pcm, buffer, count);
src_ptr = NULL;
src_size = 0;
dst_size = snd_pcm_plugin_hardware_size(pcm, SND_PCM_CHANNEL_CAPTURE, count);
if (dst_size < 0)
return dst_size;
while (plugin) {
prev = plugin->prev;
if (plugin->dst_size) {
dst_size = plugin->dst_size(plugin, dst_size);
if (dst_size < 0) {
result = dst_size;
goto __free;
}
}
if (prev != NULL) {
if (prev->transfer_src_ptr) {
if ((err = prev->transfer_src_ptr(prev, &dst_ptr, &dst_size)) < 0) {
if (dst_ptr == NULL)
goto __alloc;
result = err;
goto __free;
}
} else {
__alloc:
dst_ptr = dst_ptr1 = (char *)snd_pcm_plugin_malloc(pcm, dst_size);
if (dst_ptr == NULL) {
result = -ENOMEM;
goto __free;
}
}
} else {
dst_ptr = buffer;
}
if ((size = plugin->transfer(plugin, src_ptr, src_size,
dst_ptr, dst_size))<0) {
result = size;
goto __free;
}
if (dst_ptr1)
snd_pcm_plugin_alloc_unlock(pcm, dst_ptr1);
plugin = plugin->prev;
src_ptr = dst_ptr;
src_ptr1 = dst_ptr1;
dst_ptr1 = NULL;
src_size = size;
}
result = size;
__free:
if (dst_ptr1)
snd_pcm_plugin_alloc_unlock(pcm, dst_ptr1);
if (src_ptr1)
snd_pcm_plugin_alloc_unlock(pcm, src_ptr1);
return result;
}

View file

@ -0,0 +1,8 @@
EXTRA_LTLIBRARIES = libpcmplugin.la
libpcmplugin_la_SOURCES = block.c mmap.c stream.c linear.c interleave.c \
mulaw.c
all: libpcmplugin.la
INCLUDES=-I$(top_srcdir)/include

96
src/pcm/plugin/block.c Normal file
View file

@ -0,0 +1,96 @@
/*
* PCM Block Plug-In Interface
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "../pcm_local.h"
/*
* Basic block plugin
*/
struct block_private_data {
snd_pcm_t *pcm;
int channel;
};
static ssize_t block_transfer(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size)
{
struct block_private_data *data;
if (plugin == NULL || dst_ptr == NULL || dst_size <= 0)
return -EINVAL;
data = (struct block_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
if (data->channel == SND_PCM_CHANNEL_PLAYBACK) {
return snd_pcm_write(data->pcm, dst_ptr, dst_size);
} else if (data->channel == SND_PCM_CHANNEL_CAPTURE) {
return snd_pcm_read(data->pcm, dst_ptr, dst_size);
} else {
return -EINVAL;
}
}
static int block_action(snd_pcm_plugin_t *plugin, snd_pcm_plugin_action_t action)
{
struct block_private_data *data;
if (plugin == NULL)
return -EINVAL;
data = (struct block_private_data *)snd_pcm_plugin_extra_data(plugin);
if (action == DRAIN && data->channel == SND_PCM_CHANNEL_PLAYBACK) {
return snd_pcm_drain_playback(data->pcm);
} else if (action == FLUSH) {
return snd_pcm_flush_channel(data->pcm, data->channel);
}
return 0; /* silenty ignore other actions */
}
int snd_pcm_plugin_build_block(snd_pcm_t *pcm, int channel, snd_pcm_plugin_t **r_plugin)
{
struct block_private_data *data;
snd_pcm_plugin_t *plugin;
if (r_plugin == NULL)
return -EINVAL;
*r_plugin = NULL;
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
plugin = snd_pcm_plugin_build(channel == SND_PCM_CHANNEL_PLAYBACK ?
"I/O block playback" :
"I/O block capture",
sizeof(struct block_private_data));
if (plugin == NULL)
return -ENOMEM;
data = (struct block_private_data *)snd_pcm_plugin_extra_data(plugin);
data->pcm = pcm;
data->channel = channel;
plugin->transfer = block_transfer;
plugin->action = block_action;
*r_plugin = plugin;
return 0;
}

229
src/pcm/plugin/interleave.c Normal file
View file

@ -0,0 +1,229 @@
/*
* Interleave / non-interleave conversion Plug-In
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <endian.h>
#include <byteswap.h>
#include "../pcm_local.h"
/*
* Basic interleave / non-interleave conversion plugin
*/
typedef enum {
_INTERLEAVE_NON,
_NON_INTERLEAVE
} combination_t;
struct interleave_private_data {
combination_t cmd;
int size;
};
static void separate_8bit(unsigned char *src_ptr,
unsigned char *dst_ptr,
unsigned int size)
{
unsigned char *dst1, *dst2;
dst1 = dst_ptr;
dst2 = dst_ptr + (size / 2);
size /= 2;
while (size--) {
*dst1++ = *src_ptr++;
*dst2++ = *src_ptr++;
}
}
static void separate_16bit(unsigned char *src_ptr,
unsigned char *dst_ptr,
unsigned int size)
{
unsigned short *src, *dst1, *dst2;
src = (short *)src_ptr;
dst1 = (short *)dst_ptr;
dst2 = (short *)(dst_ptr + (size / 2));
size /= 4;
while (size--) {
*dst1++ = *src++;
*dst2++ = *src++;
}
}
static void separate_32bit(unsigned char *src_ptr,
unsigned char *dst_ptr,
unsigned int size)
{
unsigned int *src, *dst1, *dst2;
src = (int *)src_ptr;
dst1 = (int *)dst_ptr;
dst2 = (int *)(dst_ptr + (size / 2));
size /= 8;
while (size--) {
*dst1++ = *src++;
*dst2++ = *src++;
}
}
static void interleave_8bit(unsigned char *src_ptr,
unsigned char *dst_ptr,
unsigned int size)
{
unsigned char *src1, *src2;
src1 = src_ptr;
src2 = src_ptr + (size / 2);
size /= 2;
while (size--) {
*dst_ptr++ = *src1++;
*dst_ptr++ = *src2++;
}
}
static void interleave_16bit(unsigned char *src_ptr,
unsigned char *dst_ptr,
unsigned int size)
{
unsigned short *src1, *src2, *dst;
src1 = (short *)src_ptr;
src2 = (short *)(src_ptr + (size / 2));
dst = (short *)dst_ptr;
size /= 4;
while (size--) {
*dst++ = *src1++;
*dst++ = *src2++;
}
}
static void interleave_32bit(unsigned char *src_ptr,
unsigned char *dst_ptr,
unsigned int size)
{
unsigned int *src1, *src2, *dst;
src1 = (int *)src_ptr;
src2 = (int *)(src_ptr + (size / 2));
dst = (int *)dst_ptr;
size /= 8;
while (size--) {
*dst++ = *src1++;
*dst++ = *src2++;
}
}
static ssize_t interleave_transfer(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size)
{
struct interleave_private_data *data;
if (plugin == NULL || src_ptr == NULL || src_size < 0 ||
dst_ptr == NULL || dst_size < 0)
return -EINVAL;
if (src_size == 0)
return 0;
if (src_size != dst_size)
return -EINVAL;
data = (struct interleave_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
switch (data->cmd) {
case _INTERLEAVE_NON:
switch (data->size) {
case 1: separate_8bit(src_ptr, dst_ptr, src_size); break;
case 2: separate_16bit(src_ptr, dst_ptr, src_size); break;
case 4: separate_32bit(src_ptr, dst_ptr, src_size); break;
default:
return -EINVAL;
}
break;
case _NON_INTERLEAVE:
switch (data->size) {
case 1: interleave_8bit(src_ptr, dst_ptr, src_size); break;
case 2: interleave_16bit(src_ptr, dst_ptr, src_size); break;
case 4: interleave_32bit(src_ptr, dst_ptr, src_size); break;
default:
return -EINVAL;
}
break;
default:
return -EINVAL;
}
return src_size;
}
int snd_pcm_plugin_build_interleave(int src_interleave, int dst_interleave, int format, snd_pcm_plugin_t **r_plugin)
{
struct interleave_private_data *data;
snd_pcm_plugin_t *plugin;
combination_t cmd;
int size;
if (!r_plugin)
return -EINVAL;
if (src_interleave && !dst_interleave) {
cmd = _INTERLEAVE_NON;
} else if (!src_interleave && dst_interleave) {
cmd = _NON_INTERLEAVE;
} else {
return -EINVAL;
}
switch (format) {
case SND_PCM_SFMT_S8:
case SND_PCM_SFMT_U8: size = 1; break;
case SND_PCM_SFMT_S16_LE:
case SND_PCM_SFMT_S16_BE:
case SND_PCM_SFMT_U16_LE: size = 2; break;
case SND_PCM_SFMT_S24_LE:
case SND_PCM_SFMT_S24_BE:
case SND_PCM_SFMT_U24_LE:
case SND_PCM_SFMT_U24_BE:
case SND_PCM_SFMT_S32_LE:
case SND_PCM_SFMT_S32_BE:
case SND_PCM_SFMT_U32_LE:
case SND_PCM_SFMT_U32_BE:
case SND_PCM_SFMT_FLOAT: size = 4; break;
case SND_PCM_SFMT_FLOAT64: size = 8; break;
case SND_PCM_SFMT_IEC958_SUBFRAME_LE:
case SND_PCM_SFMT_IEC958_SUBFRAME_BE: size = 4; break;
case SND_PCM_SFMT_MU_LAW:
case SND_PCM_SFMT_A_LAW: size = 1; break;
default:
return -EINVAL;
}
plugin = snd_pcm_plugin_build("interleave conversion",
sizeof(struct interleave_private_data));
if (plugin == NULL)
return -ENOMEM;
data = (struct interleave_private_data *)snd_pcm_plugin_extra_data(plugin);
data->cmd = cmd;
data->size = size;
plugin->transfer = interleave_transfer;
*r_plugin = plugin;
return 0;
}

393
src/pcm/plugin/linear.c Normal file
View file

@ -0,0 +1,393 @@
/*
* Linear conversion Plug-In
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <endian.h>
#include <byteswap.h>
#include "../pcm_local.h"
/*
* Basic linear conversion plugin
*/
typedef enum {
_8BIT_16BIT,
_8BIT_24BIT,
_8BIT_32BIT,
_16BIT_8BIT,
_16BIT_24BIT,
_16BIT_32BIT,
_24BIT_8BIT,
_24BIT_16BIT,
_24BIT_32BIT,
_32BIT_8BIT,
_32BIT_16BIT,
_32BIT_24BIT
} combination_t;
typedef enum {
NONE,
SOURCE,
DESTINATION,
BOTH,
SIGN_NONE,
SIGN_SOURCE,
SIGN_DESTINATION,
SIGN_BOTH,
} endian_t;
struct linear_private_data {
combination_t cmd;
endian_t endian;
};
static void linear_conv_8bit_16bit(unsigned char *src_ptr,
unsigned short *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = ((unsigned short)*src_ptr++) << 8;
}
static void linear_conv_8bit_16bit_swap(unsigned char *src_ptr,
unsigned short *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = (unsigned short)*src_ptr++;
}
static void linear_conv_sign_8bit_16bit(unsigned char *src_ptr,
unsigned short *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = (((unsigned short)*src_ptr++) << 8) ^ 0x8000;
}
static void linear_conv_sign_8bit_16bit_swap(unsigned char *src_ptr,
unsigned short *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = ((unsigned short)*src_ptr++) ^ 0x80;
}
static void linear_conv_16bit_8bit(unsigned short *src_ptr,
unsigned char *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = (*src_ptr++) >> 8;
}
static void linear_conv_16bit_8bit_swap(unsigned short *src_ptr,
unsigned char *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = (unsigned char)*src_ptr++;
}
static void linear_conv_sign_16bit_8bit(unsigned short *src_ptr,
unsigned char *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = (((unsigned short)*src_ptr++) >> 8) ^ 0x80;
}
static void linear_conv_sign_16bit_8bit_swap(unsigned short *src_ptr,
unsigned char *dst_ptr,
size_t size)
{
while (size--)
*dst_ptr++ = ((unsigned char)*src_ptr++) ^ 0x80;
}
static ssize_t linear_transfer(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size)
{
struct linear_private_data *data;
if (plugin == NULL || src_ptr == NULL || src_size < 0 ||
dst_ptr == NULL || dst_size < 0)
return -EINVAL;
if (src_size == 0)
return 0;
data = (struct linear_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
switch (data->cmd) {
case _8BIT_16BIT:
if ((dst_size >> 1) < src_size)
return -EINVAL;
switch (data->endian) {
case NONE:
linear_conv_8bit_16bit(src_ptr, (short *)dst_ptr, src_size);
break;
case DESTINATION:
linear_conv_8bit_16bit_swap(src_ptr, (short *)dst_ptr, src_size);
break;
case SIGN_NONE:
linear_conv_sign_8bit_16bit(src_ptr, (short *)dst_ptr, src_size);
break;
case SIGN_DESTINATION:
linear_conv_sign_8bit_16bit_swap(src_ptr, (short *)dst_ptr, src_size);
break;
default:
return -EINVAL;
}
return src_size << 1;
case _16BIT_8BIT:
if (dst_size < (src_size >> 1))
return -EINVAL;
switch (data->endian) {
case NONE:
linear_conv_16bit_8bit((short *)src_ptr, dst_ptr, src_size);
break;
case DESTINATION:
linear_conv_16bit_8bit_swap((short *)src_ptr, dst_ptr, src_size);
break;
case SIGN_NONE:
linear_conv_sign_16bit_8bit((short *)src_ptr, dst_ptr, src_size);
break;
case SIGN_DESTINATION:
linear_conv_sign_16bit_8bit_swap((short *)src_ptr, dst_ptr, src_size);
break;
default:
return -EINVAL;
}
return src_size >> 1;
default:
return -EIO;
}
}
static ssize_t linear_src_size(snd_pcm_plugin_t *plugin, size_t size)
{
struct linear_private_data *data;
if (!plugin || size <= 0)
return -EINVAL;
data = (struct linear_private_data *)snd_pcm_plugin_extra_data(plugin);
switch (data->cmd) {
case _8BIT_16BIT:
case _16BIT_24BIT:
case _16BIT_32BIT:
return size / 2;
case _8BIT_24BIT:
case _8BIT_32BIT:
return size / 4;
case _16BIT_8BIT:
case _24BIT_16BIT:
case _32BIT_16BIT:
return size * 2;
case _24BIT_8BIT:
case _32BIT_8BIT:
return size * 4;
case _24BIT_32BIT:
case _32BIT_24BIT:
return size;
default:
return -EIO;
}
}
static ssize_t linear_dst_size(snd_pcm_plugin_t *plugin, size_t size)
{
struct linear_private_data *data;
if (!plugin || size <= 0)
return -EINVAL;
data = (struct linear_private_data *)snd_pcm_plugin_extra_data(plugin);
switch (data->cmd) {
case _8BIT_16BIT:
case _16BIT_24BIT:
case _16BIT_32BIT:
return size * 2;
case _8BIT_24BIT:
case _8BIT_32BIT:
return size * 4;
case _16BIT_8BIT:
case _24BIT_16BIT:
case _32BIT_16BIT:
return size / 2;
case _24BIT_8BIT:
case _32BIT_8BIT:
return size / 4;
case _24BIT_32BIT:
case _32BIT_24BIT:
return size;
default:
return -EIO;
}
}
static int linear_wide(int format)
{
if (format >= 0 && format <= 1)
return 8;
if (format >= 2 && format <= 5)
return 16;
if (format >= 6 && format <= 9)
return 24;
if (format >= 10 && format <= 13)
return 32;
return -1;
}
static int linear_endian(int format)
{
switch (format) {
case SND_PCM_SFMT_S8:
case SND_PCM_SFMT_U8:
return 0;
case SND_PCM_SFMT_S16_LE:
case SND_PCM_SFMT_U16_LE:
case SND_PCM_SFMT_S24_LE:
case SND_PCM_SFMT_U24_LE:
case SND_PCM_SFMT_S32_LE:
case SND_PCM_SFMT_U32_LE:
return __LITTLE_ENDIAN;
case SND_PCM_SFMT_S16_BE:
case SND_PCM_SFMT_U16_BE:
case SND_PCM_SFMT_S24_BE:
case SND_PCM_SFMT_U24_BE:
case SND_PCM_SFMT_S32_BE:
case SND_PCM_SFMT_U32_BE:
return __BIG_ENDIAN;
default:
return -1;
}
}
static int linear_sign(int format)
{
switch (format) {
case SND_PCM_SFMT_S8:
case SND_PCM_SFMT_S16_LE:
case SND_PCM_SFMT_S16_BE:
case SND_PCM_SFMT_S24_LE:
case SND_PCM_SFMT_S24_BE:
case SND_PCM_SFMT_S32_LE:
case SND_PCM_SFMT_S32_BE:
return 1;
case SND_PCM_SFMT_U8:
case SND_PCM_SFMT_U16_LE:
case SND_PCM_SFMT_U24_LE:
case SND_PCM_SFMT_U32_LE:
case SND_PCM_SFMT_U16_BE:
case SND_PCM_SFMT_U24_BE:
case SND_PCM_SFMT_U32_BE:
return 0;
default:
return -1;
}
}
int snd_pcm_plugin_build_linear(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
{
struct linear_private_data *data;
snd_pcm_plugin_t *plugin;
combination_t cmd;
int wide1, wide2, endian1, endian2, sign1, sign2;
if (!r_plugin)
return -EINVAL;
*r_plugin = NULL;
wide1 = linear_wide(src_format);
endian1 = linear_endian(src_format);
sign1 = linear_sign(src_format);
wide2 = linear_wide(dst_format);
endian2 = linear_endian(dst_format);
sign2 = linear_sign(dst_format);
if (wide1 < 0 || wide2 < 0 || endian1 < 0 || endian2 < 0 || sign1 < 0 || sign2 < 0)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
endian1 = endian1 == __BIG_ENDIAN ? 1 : 0;
endian1 = endian2 == __BIG_ENDIAN ? 1 : 0;
#elif __BYTE_ORDER == __BIG_ENDIAN
endian1 = endian1 == __LITTLE_ENDIAN ? 1 : 0;
endian1 = endian2 == __LITTLE_ENDIAN ? 1 : 0;
#else
#error "Unsupported endian..."
#endif
cmd = _8BIT_16BIT;
switch (wide1) {
case 8:
switch (wide2) {
case 16: cmd = _8BIT_16BIT; break;
case 24: cmd = _8BIT_24BIT; break;
case 32: cmd = _8BIT_32BIT; break;
default: return -EINVAL;
}
break;
case 16:
switch (wide2) {
case 8: cmd = _16BIT_8BIT; break;
case 24: cmd = _16BIT_24BIT; break;
case 32: cmd = _16BIT_32BIT; break;
default: return -EINVAL;
}
case 24:
switch (wide2) {
case 8: cmd = _24BIT_8BIT; break;
case 16: cmd = _24BIT_16BIT; break;
case 32: cmd = _24BIT_32BIT; break;
default: return -EINVAL;
}
case 32:
switch (wide2) {
case 8: cmd = _32BIT_8BIT; break;
case 16: cmd = _32BIT_16BIT; break;
case 24: cmd = _32BIT_24BIT; break;
default: return -EINVAL;
}
}
plugin = snd_pcm_plugin_build("wide conversion",
sizeof(struct linear_private_data));
if (plugin == NULL)
return -ENOMEM;
data = (struct linear_private_data *)snd_pcm_plugin_extra_data(plugin);
data->cmd = cmd;
if (!endian1 && !endian2) {
data->endian = NONE;
} else if (endian1 && !endian2) {
data->endian = SOURCE;
} else if (!endian1 && endian2) {
data->endian = DESTINATION;
} else {
data->endian = BOTH;
}
if (sign1 != sign2)
data->endian += 4;
plugin->transfer = linear_transfer;
plugin->src_size = linear_src_size;
plugin->dst_size = linear_dst_size;
*r_plugin = plugin;
return 0;
}

268
src/pcm/plugin/mmap.c Normal file
View file

@ -0,0 +1,268 @@
/*
* PCM MMAP Plug-In Interface
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "../pcm_local.h"
/*
* Basic mmap plugin
*/
struct mmap_private_data {
snd_pcm_t *pcm;
int channel;
snd_pcm_mmap_control_t *control;
char *buffer;
int frag;
};
static int mmap_transfer_src_ptr(snd_pcm_plugin_t *plugin, char **buffer, size_t *size)
{
struct mmap_private_data *data;
snd_pcm_mmap_control_t *control;
int interleave, err;
if (plugin == NULL || buffer == NULL || size == NULL)
return -EINVAL;
data = (struct mmap_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
control = data->control;
if (control == NULL)
return -EINVAL;
interleave = control->status.voices < 0;
if (interleave) {
*buffer = data->buffer + control->fragments[data->frag].addr;
if (data->channel == SND_PCM_CHANNEL_PLAYBACK) {
/* wait until the block is not free */
while (control->fragments[data->frag].data) {
switch (control->status.status) {
case SND_PCM_STATUS_PREPARED:
err = snd_pcm_channel_go(data->pcm, data->channel);
if (err < 0)
return err;
break;
case SND_PCM_STATUS_RUNNING:
usleep(10000);
break;
default:
return -EIO;
}
}
}
*size = control->status.frag_size;
} else {
*buffer = NULL; /* use another buffer */
}
return 0;
}
static ssize_t mmap_transfer(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size)
{
struct mmap_private_data *data;
snd_pcm_mmap_control_t *control;
int interleave, voice, err;
char *addr;
if (plugin == NULL || dst_ptr == NULL || dst_size <= 0)
return -EINVAL;
data = (struct mmap_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
control = data->control;
if (control == NULL)
return -EINVAL;
interleave = control->status.voices < 0;
if (interleave) {
if (dst_size != control->status.frag_size)
return -EINVAL;
} else {
if (dst_size != control->status.frag_size * control->status.voices)
return -EINVAL;
}
if (data->channel == SND_PCM_CHANNEL_PLAYBACK) {
if (interleave) {
while (control->fragments[data->frag].data) {
switch (control->status.status) {
case SND_PCM_STATUS_PREPARED:
err = snd_pcm_channel_go(data->pcm, data->channel);
if (err < 0)
return err;
break;
case SND_PCM_STATUS_RUNNING:
usleep(10000);
break;
default:
return -EIO;
}
}
addr = data->buffer + control->fragments[data->frag].addr;
if (dst_ptr != addr)
memcpy(addr, dst_ptr, dst_size);
control->fragments[data->frag++].data = 1;
data->frag %= control->status.frags;
} else {
int frag;
for (voice = 0; voice < control->status.voices; voice++) {
frag = data->frag + (voice * (control->status.frags / control->status.voices));
while (control->fragments[frag].data) {
switch (control->status.status) {
case SND_PCM_STATUS_PREPARED:
err = snd_pcm_channel_go(data->pcm, data->channel);
if (err < 0)
return err;
break;
case SND_PCM_STATUS_RUNNING:
usleep(10000);
break;
default:
return -EIO;
}
}
addr = data->buffer + control->fragments[frag].addr;
if (dst_ptr != addr)
memcpy(addr, dst_ptr, control->status.frag_size);
control->fragments[frag].data = 1;
dst_ptr += control->status.frag_size;
}
data->frag++;
data->frag %= control->status.frags;
}
return dst_size;
} else if (data->channel == SND_PCM_CHANNEL_CAPTURE) {
if (interleave) {
while (!control->fragments[data->frag].data) {
switch (control->status.status) {
case SND_PCM_STATUS_PREPARED:
err = snd_pcm_channel_go(data->pcm, data->channel);
if (err < 0)
return err;
break;
case SND_PCM_STATUS_RUNNING:
usleep(10000);
break;
default:
return -EIO;
}
}
addr = data->buffer + control->fragments[data->frag].addr;
if (dst_ptr != addr)
memcpy(dst_ptr, addr, dst_size);
control->fragments[data->frag++].data = 0;
data->frag %= control->status.frags;
} else {
for (voice = 0; voice < control->status.voices; voice++) {
while (!control->fragments[data->frag].data) {
switch (control->status.status) {
case SND_PCM_STATUS_PREPARED:
err = snd_pcm_channel_go(data->pcm, data->channel);
if (err < 0)
return err;
break;
case SND_PCM_STATUS_RUNNING:
usleep(10000);
break;
default:
return -EIO;
}
}
addr = data->buffer + control->fragments[data->frag].addr;
if (dst_ptr != addr)
memcpy(dst_ptr, addr, control->status.frag_size);
control->fragments[data->frag++].data = 0;
data->frag %= control->status.frags;
dst_ptr += control->status.frag_size;
}
}
return dst_size;
} else {
return -EINVAL;
}
}
static int mmap_action(snd_pcm_plugin_t *plugin, snd_pcm_plugin_action_t action)
{
struct mmap_private_data *data;
int res;
if (plugin == NULL)
return -EINVAL;
data = (struct mmap_private_data *)snd_pcm_plugin_extra_data(plugin);
if (action == INIT) {
if (data->control)
snd_pcm_munmap(data->pcm, data->channel);
return snd_pcm_mmap(data->pcm, data->channel, &data->control, (void **)&data->buffer);
} else if (action == DRAIN && data->channel == SND_PCM_CHANNEL_PLAYBACK) {
res = snd_pcm_drain_playback(data->pcm);
data->frag = 0;
return res;
} else if (action == FLUSH) {
res = snd_pcm_flush_channel(data->pcm, data->channel);
data->frag = 0;
return res;
}
return 0; /* silenty ignore other actions */
}
static void mmap_free(snd_pcm_plugin_t *plugin, void *private_data)
{
struct mmap_private_data *data;
if (plugin == NULL)
return;
data = (struct mmap_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data->control)
snd_pcm_munmap(data->pcm, data->channel);
}
int snd_pcm_plugin_build_mmap(snd_pcm_t *pcm, int channel, snd_pcm_plugin_t **r_plugin)
{
struct mmap_private_data *data;
snd_pcm_plugin_t *plugin;
if (r_plugin == NULL)
return -EINVAL;
*r_plugin = NULL;
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
plugin = snd_pcm_plugin_build(channel == SND_PCM_CHANNEL_PLAYBACK ?
"I/O mmap playback" :
"I/O mmap capture",
sizeof(struct mmap_private_data));
if (plugin == NULL)
return -ENOMEM;
data = (struct mmap_private_data *)snd_pcm_plugin_extra_data(plugin);
data->pcm = pcm;
data->channel = channel;
plugin->transfer_src_ptr = mmap_transfer_src_ptr;
plugin->transfer = mmap_transfer;
plugin->action = mmap_action;
plugin->private_free = mmap_free;
*r_plugin = plugin;
return 0;
}

364
src/pcm/plugin/mulaw.c Normal file
View file

@ -0,0 +1,364 @@
/*
* muLaw conversion Plug-In Interface
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <endian.h>
#include <byteswap.h>
#include "../pcm_local.h"
#include "mulaw.h"
/*
* Basic muLaw plugin
*/
typedef enum {
_S8_MULAW,
_U8_MULAW,
_S16LE_MULAW,
_U16LE_MULAW,
_S16BE_MULAW,
_U16BE_MULAW,
_MULAW_S8,
_MULAW_U8,
_MULAW_S16LE,
_MULAW_U16LE,
_MULAW_S16BE,
_MULAW_U16BE
} combination_t;
struct mulaw_private_data {
combination_t cmd;
};
static void mulaw_conv_u8bit_mulaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
{
unsigned int idx;
while (size-- > 0) {
idx = ((*src_ptr++) ^ 0x80) << 8;
*dst_ptr++ = lintomulaw[idx];
}
}
static void mulaw_conv_s8bit_mulaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
{
unsigned int idx;
while (size-- > 0) {
idx = *src_ptr++ << 8;
*dst_ptr++ = lintomulaw[idx];
}
}
static void mulaw_conv_s16bit_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = lintomulaw[*src_ptr++];
}
static void mulaw_conv_s16bit_swap_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = lintomulaw[bswap_16(*src_ptr++)];
}
static void mulaw_conv_u16bit_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = lintomulaw[(*src_ptr++) ^ 0x8000];
}
static void mulaw_conv_u16bit_swap_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = lintomulaw[bswap_16(*src_ptr++) ^ 0x8000];
}
static void mulaw_conv_mulaw_u8bit(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = (mulawtolin[*src_ptr++] >> 8) ^ 0x80;
}
static void mulaw_conv_mulaw_s8bit(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = mulawtolin[*src_ptr++] >> 8;
}
static void mulaw_conv_mulaw_s16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = mulawtolin[*src_ptr++];
}
static void mulaw_conv_mulaw_swap_s16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = bswap_16(mulawtolin[*src_ptr++]);
}
static void mulaw_conv_mulaw_u16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = mulawtolin[*src_ptr++] ^ 0x8000;
}
static void mulaw_conv_mulaw_swap_u16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
{
while (size-- > 0)
*dst_ptr++ = bswap_16(mulawtolin[*src_ptr++] ^ 0x8000);
}
static ssize_t mulaw_transfer(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size)
{
struct mulaw_private_data *data;
if (plugin == NULL || src_ptr == NULL || src_size < 0 ||
dst_ptr == NULL || dst_size < 0)
return -EINVAL;
if (src_size == 0)
return 0;
data = (struct mulaw_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
switch (data->cmd) {
case _U8_MULAW:
if (dst_size < src_size)
return -EINVAL;
mulaw_conv_u8bit_mulaw(src_ptr, dst_ptr, src_size);
return src_size;
case _S8_MULAW:
if (dst_size < src_size)
return -EINVAL;
mulaw_conv_s8bit_mulaw(src_ptr, dst_ptr, src_size);
return src_size;
case _S16LE_MULAW:
if ((dst_size << 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_s16bit_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_s16bit_swap_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#else
#error "Have to be coded..."
#endif
return src_size >> 1;
case _U16LE_MULAW:
if ((dst_size << 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_u16bit_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_u16bit_swap_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#else
#error "Have to be coded..."
#endif
return src_size >> 1;
case _S16BE_MULAW:
if ((dst_size << 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_s16bit_swap_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_s16bit_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#else
#error "Have to be coded..."
#endif
return src_size >> 1;
case _U16BE_MULAW:
if ((dst_size << 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_u16bit_swap_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_u16bit_mulaw((short *)src_ptr, dst_ptr, src_size >> 1);
#else
#error "Have to be coded..."
#endif
return src_size >> 1;
case _MULAW_U8:
if (dst_size < src_size)
return -EINVAL;
mulaw_conv_mulaw_u8bit(src_ptr, dst_ptr, src_size);
return src_size;
case _MULAW_S8:
if (dst_size < src_size)
return -EINVAL;
mulaw_conv_mulaw_s8bit(src_ptr, dst_ptr, src_size);
return src_size;
case _MULAW_S16LE:
if ((dst_size >> 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_mulaw_s16bit(src_ptr, (short *)dst_ptr, src_size);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_mulaw_swap_s16bit(src_ptr, (short *)dst_ptr, src_size);
#else
#error "Have to be coded..."
#endif
return src_size << 1;
case _MULAW_U16LE:
if ((dst_size >> 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_mulaw_u16bit(src_ptr, (short *)dst_ptr, src_size);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_mulaw_swap_u16bit(src_ptr, (short *)dst_ptr, src_size);
#else
#error "Have to be coded..."
#endif
return src_size << 1;
case _MULAW_S16BE:
if ((dst_size >> 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_mulaw_swap_s16bit(src_ptr, (short *)dst_ptr, src_size);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_mulaw_s16bit(src_ptr, (short *)dst_ptr, src_size);
#else
#error "Have to be coded..."
#endif
return src_size << 1;
case _MULAW_U16BE:
if ((dst_size >> 1) < src_size)
return -EINVAL;
#if __BYTE_ORDER == __LITTLE_ENDIAN
mulaw_conv_mulaw_swap_u16bit(src_ptr, (short *)dst_ptr, src_size);
#elif __BYTE_ORDER == __BIG_ENDIAN
mulaw_conv_mulaw_u16bit(src_ptr, (short *)dst_ptr, src_size);
#else
#error "Have to be coded..."
#endif
return dst_size << 1;
default:
return -EIO;
}
}
static ssize_t mulaw_src_size(snd_pcm_plugin_t *plugin, size_t size)
{
struct mulaw_private_data *data;
if (!plugin || size <= 0)
return -EINVAL;
data = (struct mulaw_private_data *)snd_pcm_plugin_extra_data(plugin);
switch (data->cmd) {
case _U8_MULAW:
case _S8_MULAW:
case _MULAW_U8:
case _MULAW_S8:
return size;
case _U16LE_MULAW:
case _S16LE_MULAW:
case _U16BE_MULAW:
case _S16BE_MULAW:
return size * 2;
case _MULAW_U16LE:
case _MULAW_S16LE:
case _MULAW_U16BE:
case _MULAW_S16BE:
return size / 2;
default:
return -EIO;
}
}
static ssize_t mulaw_dst_size(snd_pcm_plugin_t *plugin, size_t size)
{
struct mulaw_private_data *data;
if (!plugin || size <= 0)
return -EINVAL;
data = (struct mulaw_private_data *)snd_pcm_plugin_extra_data(plugin);
switch (data->cmd) {
case _U8_MULAW:
case _S8_MULAW:
case _MULAW_U8:
case _MULAW_S8:
return size;
case _U16LE_MULAW:
case _S16LE_MULAW:
case _U16BE_MULAW:
case _S16BE_MULAW:
return size / 2;
case _MULAW_U16LE:
case _MULAW_S16LE:
case _MULAW_U16BE:
case _MULAW_S16BE:
return size * 2;
default:
return -EIO;
}
}
int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
{
struct mulaw_private_data *data;
snd_pcm_plugin_t *plugin;
combination_t cmd;
if (!r_plugin)
return -EINVAL;
*r_plugin = NULL;
if (dst_format == SND_PCM_SFMT_MU_LAW) {
switch (src_format) {
case SND_PCM_SFMT_U8: cmd = _U8_MULAW; break;
case SND_PCM_SFMT_S8: cmd = _S8_MULAW; break;
case SND_PCM_SFMT_U16_LE: cmd = _U16LE_MULAW; break;
case SND_PCM_SFMT_S16_LE: cmd = _S16LE_MULAW; break;
case SND_PCM_SFMT_U16_BE: cmd = _U16BE_MULAW; break;
case SND_PCM_SFMT_S16_BE: cmd = _S16BE_MULAW; break;
default:
return -EINVAL;
}
} else if (src_format == SND_PCM_SFMT_MU_LAW) {
switch (dst_format) {
case SND_PCM_SFMT_U8: cmd = _MULAW_U8; break;
case SND_PCM_SFMT_S8: cmd = _MULAW_S8; break;
case SND_PCM_SFMT_U16_LE: cmd = _MULAW_U16LE; break;
case SND_PCM_SFMT_S16_LE: cmd = _MULAW_S16LE; break;
case SND_PCM_SFMT_U16_BE: cmd = _MULAW_U16BE; break;
case SND_PCM_SFMT_S16_BE: cmd = _MULAW_S16BE; break;
default:
return -EINVAL;
}
} else {
return -EINVAL;
}
plugin = snd_pcm_plugin_build("muLaw<->linear conversion",
sizeof(struct mulaw_private_data));
if (plugin == NULL)
return -ENOMEM;
data = (struct mulaw_private_data *)snd_pcm_plugin_extra_data(plugin);
data->cmd = cmd;
plugin->transfer = mulaw_transfer;
plugin->src_size = mulaw_src_size;
plugin->dst_size = mulaw_dst_size;
*r_plugin = plugin;
return 0;
}

8228
src/pcm/plugin/mulaw.h Normal file

File diff suppressed because it is too large Load diff

96
src/pcm/plugin/stream.c Normal file
View file

@ -0,0 +1,96 @@
/*
* PCM Stream Plug-In Interface
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
*
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "../pcm_local.h"
/*
* Basic stream plugin
*/
struct stream_private_data {
snd_pcm_t *pcm;
int channel;
};
static ssize_t stream_transfer(snd_pcm_plugin_t *plugin,
char *src_ptr, size_t src_size,
char *dst_ptr, size_t dst_size)
{
struct stream_private_data *data;
if (plugin == NULL || dst_ptr == NULL || dst_size <= 0)
return -EINVAL;
data = (struct stream_private_data *)snd_pcm_plugin_extra_data(plugin);
if (data == NULL)
return -EINVAL;
if (data->channel == SND_PCM_CHANNEL_PLAYBACK) {
return snd_pcm_write(data->pcm, dst_ptr, dst_size);
} else if (data->channel == SND_PCM_CHANNEL_CAPTURE) {
return snd_pcm_read(data->pcm, dst_ptr, dst_size);
} else {
return -EINVAL;
}
}
static int stream_action(snd_pcm_plugin_t *plugin, snd_pcm_plugin_action_t action)
{
struct stream_private_data *data;
if (plugin == NULL)
return -EINVAL;
data = (struct stream_private_data *)snd_pcm_plugin_extra_data(plugin);
if (action == DRAIN && data->channel == SND_PCM_CHANNEL_PLAYBACK) {
return snd_pcm_drain_playback(data->pcm);
} else if (action == FLUSH) {
return snd_pcm_flush_channel(data->pcm, data->channel);
}
return 0; /* silenty ignore other actions */
}
int snd_pcm_plugin_build_stream(snd_pcm_t *pcm, int channel, snd_pcm_plugin_t **r_plugin)
{
struct stream_private_data *data;
snd_pcm_plugin_t *plugin;
if (!r_plugin)
return -EINVAL;
*r_plugin = NULL;
if (!pcm || channel < 0 || channel > 1)
return -EINVAL;
plugin = snd_pcm_plugin_build(channel == SND_PCM_CHANNEL_PLAYBACK ?
"I/O stream playback" :
"I/O stream capture",
sizeof(struct stream_private_data));
if (plugin == NULL)
return -ENOMEM;
data = (struct stream_private_data *)snd_pcm_plugin_extra_data(plugin);
data->pcm = pcm;
data->channel = channel;
plugin->transfer = stream_transfer;
plugin->action = stream_action;
*r_plugin = plugin;
return 0;
}

View file

@ -29,7 +29,7 @@
#include "asoundlib.h"
#define SND_FILE_RAWMIDI "/dev/snd/midiC%iD%i"
#define SND_RAWMIDI_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 )
#define SND_RAWMIDI_VERSION_MAX SND_PROTOCOL_VERSION(1, 1, 0)
struct snd_rawmidi {
int card;

View file

@ -29,7 +29,7 @@
#include "asoundlib.h"
#define SND_FILE_TIMER "/dev/snd/timer"
#define SND_TIMER_VERSION_MAX SND_PROTOCOL_VERSION( 1, 0, 0 )
#define SND_TIMER_VERSION_MAX SND_PROTOCOL_VERSION(1, 1, 0)
struct snd_timer {
int fd;

View file

@ -1,4 +1,4 @@
check_PROGRAMS=control mixer switches pause pcm latency seq \
check_PROGRAMS=control mixer switches pause pcm pcmtest latency seq \
playmidi1 timer loopback aconnect aseqnet
control_LDADD=../src/libasound.la
@ -6,6 +6,7 @@ mixer_LDADD=../src/libasound.la
switches_LDADD=../src/libasound.la
pause_LDADD=../src/libasound.la
pcm_LDADD=../src/libasound.la
pcmtest_LDADD=../src/libasound.la
latency_LDADD=../src/libasound.la
seq_LDADD=../src/libasound.la
playmidi1_LDADD=../src/libasound.la

View file

@ -4,6 +4,9 @@
#include <sched.h>
#include "../include/asoundlib.h"
#define LATENCY_LIMIT 8192 /* in bytes */
#define LOOP_LIMIT (30 * 176400) /* 30 seconds */
static char *xitoa(int aaa)
{
static char str[12];
@ -12,136 +15,138 @@ static char *xitoa(int aaa)
return str;
}
static int syncro(snd_pcm_t *phandle, snd_pcm_t *chandle)
{
snd_pcm_channel_info_t pinfo, cinfo;
int err;
bzero(&pinfo, sizeof(pinfo));
bzero(&cinfo, sizeof(cinfo));
pinfo.channel = SND_PCM_CHANNEL_PLAYBACK;
cinfo.channel = SND_PCM_CHANNEL_CAPTURE;
pinfo.mode = cinfo.mode = SND_PCM_MODE_STREAM;
if ((err = snd_pcm_channel_info(phandle, &pinfo)) < 0) {
printf("Playback info error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_channel_info(chandle, &cinfo)) < 0) {
printf("Capture info error: %s\n", snd_strerror(err));
exit(0);
}
if (pinfo.sync.id32[0] == 0 && pinfo.sync.id32[1] == 0 &&
pinfo.sync.id32[2] == 0 && pinfo.sync.id32[3] == 0)
return 0;
if (memcmp(&pinfo.sync, &cinfo.sync, sizeof(pinfo.sync)))
return 0;
return 1;
}
static void syncro_id(snd_pcm_sync_t *sync)
{
sync->id32[0] = 0; /* application */
sync->id32[1] = getpid();
sync->id32[2] = 0x89abcdef;
sync->id32[3] = 0xaaaaaaaa;
}
/*
* This small demo program can be used for measuring latency between
* capture and playback. This latency is measured from driver (diff when
* playback and capture was started). Scheduler is set to SCHED_RR.
*
* Used format is 44100Hz, Signed Little Endian 16-bit, Stereo.
*
* Program begins with 128-byte fragment (about 726us) and step is 32-byte
* until playback without underruns is reached. This program starts playback
* after two fragments are captureed (teoretical latency is 1452us by 128-byte
* fragment).
*/
void setformat(void *phandle, void *rhandle)
int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int sync, int *queue)
{
int err;
snd_pcm_format_t format;
int err, again;
snd_pcm_channel_params_t params;
snd_pcm_channel_setup_t psetup, csetup;
bzero(&format, sizeof(format));
format.format = SND_PCM_SFMT_S16_LE;
format.channels = 2;
format.rate = 44100;
if ((err = snd_pcm_playback_format(phandle, &format)) < 0) {
printf("Playback format error: %s\n", snd_strerror(err));
bzero(&params, sizeof(params));
params.channel = SND_PCM_CHANNEL_PLAYBACK;
params.mode = SND_PCM_MODE_STREAM;
params.format.interleave = 1;
params.format.format = SND_PCM_SFMT_S16_LE;
params.format.voices = 2;
params.format.rate = 44100;
params.start_mode = SND_PCM_START_GO;
params.stop_mode = SND_PCM_STOP_STOP;
params.time = 1;
*queue += 16;
params.buf.stream.fill = SND_PCM_FILL_SILENCE;
params.buf.stream.max_fill = 1024;
if (sync)
syncro_id(&params.sync);
__again:
if (*queue > LATENCY_LIMIT)
return -1;
again = 0;
params.buf.stream.queue_size = *queue;
if ((err = snd_pcm_plugin_params(phandle, &params)) < 0) {
printf("Playback params error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_capture_format(rhandle, &format)) < 0) {
printf("Record format error: %s\n", snd_strerror(err));
params.channel = SND_PCM_CHANNEL_CAPTURE;
if ((err = snd_pcm_plugin_params(chandle, &params)) < 0) {
printf("Capture params error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_playback_time(phandle, 1)) < 0) {
printf("Playback time error: %s\n", snd_strerror(err));
bzero(&psetup, sizeof(psetup));
psetup.mode = SND_PCM_MODE_STREAM;
psetup.channel = SND_PCM_CHANNEL_PLAYBACK;
if ((err = snd_pcm_plugin_setup(phandle, &psetup)) < 0) {
printf("Playback params error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_capture_time(rhandle, 1)) < 0) {
printf("Record time error: %s\n", snd_strerror(err));
bzero(&csetup, sizeof(csetup));
csetup.mode = SND_PCM_MODE_STREAM;
csetup.channel = SND_PCM_CHANNEL_CAPTURE;
if ((err = snd_pcm_plugin_setup(chandle, &csetup)) < 0) {
printf("Capture params error: %s\n", snd_strerror(err));
exit(0);
}
if (psetup.buf.stream.queue_size > *queue) {
*queue = psetup.buf.stream.queue_size;
again++;
}
if (csetup.buf.stream.queue_size > *queue) {
*queue = csetup.buf.stream.queue_size;
again++;
}
if (again)
goto __again;
if ((err = snd_pcm_playback_prepare(phandle)) < 0) {
printf("Playback prepare error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_capture_prepare(chandle)) < 0) {
printf("Capture prepare error: %s\n", snd_strerror(err));
exit(0);
}
printf("Trying latency %i...\n", *queue);
fflush(stdout);
return 0;
}
int setparams(void *phandle, void *rhandle, int *fragmentsize)
{
int err, step = 4;
snd_pcm_playback_info_t pinfo;
snd_pcm_capture_info_t rinfo;
snd_pcm_playback_params_t pparams;
snd_pcm_capture_params_t rparams;
if ((err = snd_pcm_playback_info(phandle, &pinfo)) < 0) {
printf("Playback info error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_capture_info(rhandle, &rinfo)) < 0) {
printf("Record info error: %s\n", snd_strerror(err));
exit(0);
}
if (step < pinfo.min_fragment_size)
step = pinfo.min_fragment_size;
if (step < rinfo.min_fragment_size)
step = rinfo.min_fragment_size;
while (step < 4096 &&
(step & pinfo.fragment_align) != 0 &&
(step & rinfo.fragment_align) != 0)
step++;
if (*fragmentsize) {
*fragmentsize += step;
} else {
if (step < 128)
*fragmentsize = 128;
else
*fragmentsize = step;
}
while (*fragmentsize < 4096) {
bzero(&pparams, sizeof(pparams));
pparams.fragment_size = *fragmentsize;
pparams.fragments_max = -1; /* maximum */
pparams.fragments_room = 1;
if ((err = snd_pcm_playback_params(phandle, &pparams)) < 0) {
*fragmentsize += step;
continue;
}
bzero(&rparams, sizeof(rparams));
rparams.fragment_size = *fragmentsize;
rparams.fragments_min = 1; /* wakeup if at least one fragment is ready */
if ((err = snd_pcm_capture_params(rhandle, &rparams)) < 0) {
*fragmentsize += step;
continue;
}
break;
}
if (*fragmentsize < 4096) {
printf("Trying fragment size %i...\n", *fragmentsize);
fflush(stdout);
return 0;
}
return -1;
}
int playbackunderrun(void *phandle)
void showstat(snd_pcm_t *handle, int channel, snd_pcm_channel_status_t *rstatus)
{
int err;
snd_pcm_playback_status_t pstatus;
snd_pcm_channel_status_t status;
char *str;
if ((err = snd_pcm_playback_status(phandle, &pstatus)) < 0) {
printf("Playback status error: %s\n", snd_strerror(err));
exit(0);
}
return pstatus.underrun;
}
void capturefragment(void *rhandle, char *buffer, int index, int fragmentsize)
{
int err;
buffer += index * fragmentsize;
if ((err = snd_pcm_read(rhandle, buffer, fragmentsize)) != fragmentsize) {
printf("Read error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
exit(0);
}
}
void playfragment(void *phandle, char *buffer, int index, int fragmentsize)
{
int err;
buffer += index * fragmentsize;
if ((err = snd_pcm_write(phandle, buffer, fragmentsize)) != fragmentsize) {
printf("Write error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
str = channel == SND_PCM_CHANNEL_CAPTURE ? "Capture" : "Playback";
bzero(&status, sizeof(status));
status.channel = channel;
status.mode = SND_PCM_MODE_STREAM;
if ((err = snd_pcm_plugin_status(handle, &status)) < 0) {
printf("Channel %s status error: %s\n", str, snd_strerror(err));
exit(0);
}
printf("%s:\n", str);
printf(" status = %i\n", status.status);
printf(" position = %u\n", status.scount);
*rstatus = status;
}
void setscheduler(void)
@ -175,70 +180,99 @@ long timediff(struct timeval t1, struct timeval t2)
return (t1.tv_sec * 1000000) + l;
}
long readbuf(snd_pcm_t *handle, char *buf, long len)
{
long r;
r = snd_pcm_plugin_read(handle, buf, len);
return r;
}
long writebuf(snd_pcm_t *handle, char *buf, long len)
{
long r;
while (len > 0) {
r = snd_pcm_plugin_write(handle, buf, len);
if (r < 0)
return r;
buf += r;
len -= r;
}
return 0;
}
int main(void)
{
snd_pcm_t *phandle, *rhandle;
snd_pcm_t *phandle, *chandle;
char buffer[4096 * 2]; /* max two fragments by 4096 bytes */
int pcard = 0, pdevice = 0;
int rcard = 0, rdevice = 0;
int err, fragmentsize = 0;
int ridx, pidx, size, ok;
snd_pcm_playback_status_t pstatus;
snd_pcm_capture_status_t rstatus;
int ccard = 0, cdevice = 0;
int err, latency = 16;
int size, ok;
int sync;
snd_pcm_sync_t ssync;
snd_pcm_channel_status_t pstatus, cstatus;
long r;
setscheduler();
if ((err = snd_pcm_open(&phandle, pcard, pdevice, SND_PCM_OPEN_PLAYBACK)) < 0) {
if ((err = snd_pcm_open(&phandle, pcard, pdevice, SND_PCM_OPEN_STREAM_PLAYBACK)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
return 0;
}
if ((err = snd_pcm_open(&rhandle, rcard, rdevice, SND_PCM_OPEN_CAPTURE)) < 0) {
if ((err = snd_pcm_open(&chandle, ccard, cdevice, SND_PCM_OPEN_STREAM_CAPTURE)) < 0) {
printf("Record open error: %s\n", snd_strerror(err));
return 0;
}
setformat(phandle, rhandle);
sync = syncro(phandle, chandle);
if (sync)
printf("Using hardware synchronization mode\n");
while (1) {
if (setparams(phandle, rhandle, &fragmentsize) < 0)
if (setparams(phandle, chandle, sync, &latency) < 0)
break;
memset(buffer, 0, latency);
if (writebuf(phandle, buffer, latency) < 0)
break;
if (sync) {
syncro_id(&ssync);
if ((err = snd_pcm_sync_go(phandle, &ssync)) < 0) {
printf("Sync go error: %s\n", snd_strerror(err));
exit(0);
}
} else {
if ((err = snd_pcm_capture_go(chandle)) < 0) {
printf("Capture go error: %s\n", snd_strerror(err));
exit(0);
}
if ((err = snd_pcm_playback_go(phandle)) < 0) {
printf("Playback go error: %s\n", snd_strerror(err));
exit(0);
}
}
ok = 1;
ridx = pidx = size = 0;
capturefragment(rhandle, buffer, ridx++, fragmentsize);
capturefragment(rhandle, buffer, ridx++, fragmentsize);
if ((err = snd_pcm_capture_status(rhandle, &rstatus)) < 0) {
printf("Record status error: %s\n", snd_strerror(err));
exit(0);
}
ridx %= 2;
playfragment(phandle, buffer, 0, 2 * fragmentsize);
size += 2 * fragmentsize;
pidx += 2;
pidx %= 2;
while (ok && size < 3 * 176400) { /* 30 seconds */
capturefragment(rhandle, buffer, ridx++, fragmentsize);
ridx %= 2;
playfragment(phandle, buffer, pidx++, fragmentsize);
size += fragmentsize;
pidx %= 2;
if (playbackunderrun(phandle) > 0)
size = 0;
while (ok && size < LOOP_LIMIT) {
if ((r = readbuf(chandle, buffer, latency)) < 0)
ok = 0;
if (writebuf(phandle, buffer, r) < 0)
ok = 0;
size += r;
}
if ((err = snd_pcm_playback_status(phandle, &pstatus)) < 0) {
printf("Playback status error: %s\n", snd_strerror(err));
exit(0);
}
snd_pcm_flush_capture(rhandle);
showstat(phandle, SND_PCM_CHANNEL_PLAYBACK, &pstatus);
showstat(chandle, SND_PCM_CHANNEL_CAPTURE, &cstatus);
snd_pcm_flush_capture(chandle);
snd_pcm_flush_playback(phandle);
if (ok && !playbackunderrun(phandle) > 0) {
printf("Playback OK!!!\n");
if (ok) {
printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
pstatus.stime.tv_sec,
(int)pstatus.stime.tv_usec,
rstatus.stime.tv_sec,
(int)rstatus.stime.tv_usec,
timediff(pstatus.stime, rstatus.stime));
cstatus.stime.tv_sec,
(int)cstatus.stime.tv_usec,
timediff(pstatus.stime, cstatus.stime));
break;
}
}
snd_pcm_close(phandle);
snd_pcm_close(rhandle);
snd_pcm_close(chandle);
return 0;
}

175
test/pcmtest.c Normal file
View file

@ -0,0 +1,175 @@
#include <stdio.h>
#include <string.h>
#include "../include/asoundlib.h"
void info_channel(snd_pcm_t *handle, char *id)
{
snd_pcm_channel_info_t chninfo;
int err;
if ((err = snd_pcm_channel_info(handle, &chninfo))<0) {
fprintf(stderr, "channel info error: %s\n", snd_strerror(err));
return;
}
printf("%s INFO:\n", id);
printf(" subdevice : %i\n", chninfo.subdevice);
printf(" subname : '%s'\n", chninfo.subname);
printf(" channel : %i\n", chninfo.channel);
printf(" mode : ");
switch (chninfo.mode) {
case SND_PCM_MODE_STREAM:
printf("stream\n");
break;
case SND_PCM_MODE_BLOCK:
printf("block\n");
break;
default:
printf("unknown\n");
}
printf(" sync : 0x%x, 0x%x, 0x%x, 0x%x\n",
chninfo.sync.id32[0],
chninfo.sync.id32[1],
chninfo.sync.id32[2],
chninfo.sync.id32[3]);
printf(" flags :");
if (chninfo.flags & SND_PCM_CHNINFO_MMAP)
printf(" mmap");
if (chninfo.flags & SND_PCM_CHNINFO_STREAM)
printf(" stream");
if (chninfo.flags & SND_PCM_CHNINFO_BLOCK)
printf(" block");
if (chninfo.flags & SND_PCM_CHNINFO_BATCH)
printf(" batch");
if (chninfo.flags & SND_PCM_CHNINFO_INTERLEAVE)
printf(" interleave");
if (chninfo.flags & SND_PCM_CHNINFO_NONINTERLEAVE)
printf(" noninterleave");
if (chninfo.flags & SND_PCM_CHNINFO_BLOCK_TRANSFER)
printf(" block_transfer");
if (chninfo.flags & SND_PCM_CHNINFO_OVERRANGE)
printf(" overrange");
printf("\n");
printf(" formats :");
if (chninfo.formats & SND_PCM_FMT_MU_LAW)
printf(" mu-Law");
if (chninfo.formats & SND_PCM_FMT_A_LAW)
printf(" a-Law");
if (chninfo.formats & SND_PCM_FMT_IMA_ADPCM)
printf(" IMA-ADPCM");
if (chninfo.formats & SND_PCM_FMT_U8)
printf(" U8");
if (chninfo.formats & SND_PCM_FMT_S16_LE)
printf(" S16-LE");
if (chninfo.formats & SND_PCM_FMT_S16_BE)
printf(" S16-BE");
if (chninfo.formats & SND_PCM_FMT_S8)
printf(" S8");
if (chninfo.formats & SND_PCM_FMT_U16_LE)
printf(" U16-LE");
if (chninfo.formats & SND_PCM_FMT_U16_BE)
printf(" U16-BE");
if (chninfo.formats & SND_PCM_FMT_MPEG)
printf(" MPEG");
if (chninfo.formats & SND_PCM_FMT_GSM)
printf(" GSM");
if (chninfo.formats & SND_PCM_FMT_S24_LE)
printf(" S24-LE");
if (chninfo.formats & SND_PCM_FMT_S24_BE)
printf(" S24-BE");
if (chninfo.formats & SND_PCM_FMT_U24_LE)
printf(" U24-LE");
if (chninfo.formats & SND_PCM_FMT_U24_BE)
printf(" U24-BE");
if (chninfo.formats & SND_PCM_FMT_S32_LE)
printf(" S32-LE");
if (chninfo.formats & SND_PCM_FMT_S32_BE)
printf(" S32-BE");
if (chninfo.formats & SND_PCM_FMT_U32_LE)
printf(" U32-LE");
if (chninfo.formats & SND_PCM_FMT_U32_BE)
printf(" U32-BE");
if (chninfo.formats & SND_PCM_FMT_FLOAT)
printf(" Float");
if (chninfo.formats & SND_PCM_FMT_FLOAT64)
printf(" Float64");
if (chninfo.formats & SND_PCM_FMT_IEC958_SUBFRAME_LE)
printf(" IEC958-LE");
if (chninfo.formats & SND_PCM_FMT_IEC958_SUBFRAME_BE)
printf(" IEC958-BE");
if (chninfo.formats & SND_PCM_FMT_SPECIAL)
printf(" Special");
printf("\n");
printf(" rates :");
if (chninfo.rates & SND_PCM_RATE_PLL)
printf(" PLL");
if (chninfo.rates & SND_PCM_RATE_8000)
printf(" 8000");
if (chninfo.rates & SND_PCM_RATE_11025)
printf(" 11025");
if (chninfo.rates & SND_PCM_RATE_16000)
printf(" 16000");
if (chninfo.rates & SND_PCM_RATE_22050)
printf(" 22050");
if (chninfo.rates & SND_PCM_RATE_32000)
printf(" 32000");
if (chninfo.rates & SND_PCM_RATE_44100)
printf(" 44100");
if (chninfo.rates & SND_PCM_RATE_48000)
printf(" 48000");
if (chninfo.rates & SND_PCM_RATE_88200)
printf(" 88200");
if (chninfo.rates & SND_PCM_RATE_96000)
printf(" 96000");
if (chninfo.rates & SND_PCM_RATE_176400)
printf(" 176400");
if (chninfo.rates & SND_PCM_RATE_192000)
printf(" 192000");
printf("\n");
printf(" min_rate : %i\n", chninfo.min_rate);
printf(" max_rate : %i\n", chninfo.max_rate);
printf(" min_voices : %i\n", chninfo.min_voices);
printf(" max_voices : %i\n", chninfo.max_voices);
printf(" buffer_size : %i\n", chninfo.buffer_size);
printf(" min_frag_size : %i\n", chninfo.min_fragment_size);
printf(" max_frag_size : %i\n", chninfo.max_fragment_size);
printf(" fragment_align : %i\n", chninfo.fragment_align);
printf(" fifo_size : %i\n", chninfo.fifo_size);
printf(" TBS : %i\n", chninfo.transfer_block_size);
printf(" mmap_size : %li\n", chninfo.mmap_size);
printf(" mixer_device : %i\n", chninfo.mixer_device);
printf(" mixer_eid : '%s',%i,%i\n", chninfo.mixer_eid.name, chninfo.mixer_eid.index, chninfo.mixer_eid.type);
}
void info(void)
{
snd_pcm_t *handle;
snd_pcm_info_t info;
int err;
if ((err = snd_pcm_open(&handle, 0, 0, SND_PCM_OPEN_PLAYBACK))<0) {
fprintf(stderr, "open error: %s\n", snd_strerror(err));
return;
}
if ((err = snd_pcm_info(handle, &info))<0) {
fprintf(stderr, "pcm info error: %s\n", snd_strerror(err));
return;
}
printf("INFO:\n");
printf(" type : 0x%x\n", info.type);
printf(" flags : 0x%x\n", info.flags);
printf(" id : '%s'\n", info.id);
printf(" name : '%s'\n", info.name);
printf(" playback : %i\n", info.playback);
printf(" capture : %i\n", info.capture);
if (info.flags & SND_PCM_INFO_PLAYBACK)
info_channel(handle, "Playback");
if (info.flags & SND_PCM_INFO_CAPTURE)
info_channel(handle, "Capture");
snd_pcm_close(handle);
}
int main(void )
{
info();
return 0;
}