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

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