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

@ -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;
}