Blame src/pcm/pcm_alaw.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file pcm/pcm_alaw.c
Packit 4a16fb
 * \ingroup PCM_Plugins
Packit 4a16fb
 * \brief PCM A-Law Conversion Plugin Interface
Packit 4a16fb
 * \author Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 * \date 2000-2001
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  PCM - A-Law conversion
Packit 4a16fb
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
  
Packit 4a16fb
#include "bswap.h"
Packit 4a16fb
#include "pcm_local.h"
Packit 4a16fb
#include "pcm_plugin.h"
Packit 4a16fb
Packit 4a16fb
#include "plugin_ops.h"
Packit 4a16fb
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
/* entry for static linking */
Packit 4a16fb
const char *_snd_module_pcm_alaw = "";
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
Packit 4a16fb
typedef void (*alaw_f)(const snd_pcm_channel_area_t *dst_areas,
Packit 4a16fb
		       snd_pcm_uframes_t dst_offset,
Packit 4a16fb
		       const snd_pcm_channel_area_t *src_areas,
Packit 4a16fb
		       snd_pcm_uframes_t src_offset,
Packit 4a16fb
		       unsigned int channels, snd_pcm_uframes_t frames,
Packit 4a16fb
		       unsigned int getputidx);
Packit 4a16fb
Packit 4a16fb
typedef struct {
Packit 4a16fb
	/* This field need to be the first */
Packit 4a16fb
	snd_pcm_plugin_t plug;
Packit 4a16fb
	unsigned int getput_idx;
Packit 4a16fb
	alaw_f func;
Packit 4a16fb
	snd_pcm_format_t sformat;
Packit 4a16fb
} snd_pcm_alaw_t;
Packit 4a16fb
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static inline int val_seg(int val)
Packit 4a16fb
{
Packit 4a16fb
	int r = 1;
Packit 4a16fb
	val >>= 8;
Packit 4a16fb
	if (val & 0xf0) {
Packit 4a16fb
		val >>= 4;
Packit 4a16fb
		r += 4;
Packit 4a16fb
	}
Packit 4a16fb
	if (val & 0x0c) {
Packit 4a16fb
		val >>= 2;
Packit 4a16fb
		r += 2;
Packit 4a16fb
	}
Packit 4a16fb
	if (val & 0x02)
Packit 4a16fb
		r += 1;
Packit 4a16fb
	return r;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
Packit 4a16fb
 *
Packit 4a16fb
 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
Packit 4a16fb
 *
Packit 4a16fb
 *		Linear Input Code	Compressed Code
Packit 4a16fb
 *	------------------------	---------------
Packit 4a16fb
 *	0000000wxyza			000wxyz
Packit 4a16fb
 *	0000001wxyza			001wxyz
Packit 4a16fb
 *	000001wxyzab			010wxyz
Packit 4a16fb
 *	00001wxyzabc			011wxyz
Packit 4a16fb
 *	0001wxyzabcd			100wxyz
Packit 4a16fb
 *	001wxyzabcde			101wxyz
Packit 4a16fb
 *	01wxyzabcdef			110wxyz
Packit 4a16fb
 *	1wxyzabcdefg			111wxyz
Packit 4a16fb
 *
Packit 4a16fb
 * For further information see John C. Bellamy's Digital Telephony, 1982,
Packit 4a16fb
 * John Wiley & Sons, pps 98-111 and 472-476.
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static unsigned char s16_to_alaw(int pcm_val)
Packit 4a16fb
{
Packit 4a16fb
	int		mask;
Packit 4a16fb
	int		seg;
Packit 4a16fb
	unsigned char	aval;
Packit 4a16fb
Packit 4a16fb
	if (pcm_val >= 0) {
Packit 4a16fb
		mask = 0xD5;
Packit 4a16fb
	} else {
Packit 4a16fb
		mask = 0x55;
Packit 4a16fb
		pcm_val = -pcm_val;
Packit 4a16fb
		if (pcm_val > 0x7fff)
Packit 4a16fb
			pcm_val = 0x7fff;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (pcm_val < 256)
Packit 4a16fb
		aval = pcm_val >> 4;
Packit 4a16fb
	else {
Packit 4a16fb
		/* Convert the scaled magnitude to segment number. */
Packit 4a16fb
		seg = val_seg(pcm_val);
Packit 4a16fb
		aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
Packit 4a16fb
	}
Packit 4a16fb
	return aval ^ mask;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
static int alaw_to_s16(unsigned char a_val)
Packit 4a16fb
{
Packit 4a16fb
	int		t;
Packit 4a16fb
	int		seg;
Packit 4a16fb
Packit 4a16fb
	a_val ^= 0x55;
Packit 4a16fb
	t = a_val & 0x7f;
Packit 4a16fb
	if (t < 16)
Packit 4a16fb
		t = (t << 4) + 8;
Packit 4a16fb
	else {
Packit 4a16fb
		seg = (t >> 4) & 0x07;
Packit 4a16fb
		t = ((t & 0x0f) << 4) + 0x108;
Packit 4a16fb
		t <<= seg -1;
Packit 4a16fb
	}
Packit 4a16fb
	return ((a_val & 0x80) ? t : -t);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
Packit 4a16fb
void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
Packit 4a16fb
			 snd_pcm_uframes_t dst_offset,
Packit 4a16fb
			 const snd_pcm_channel_area_t *src_areas,
Packit 4a16fb
			 snd_pcm_uframes_t src_offset,
Packit 4a16fb
			 unsigned int channels, snd_pcm_uframes_t frames,
Packit 4a16fb
			 unsigned int putidx)
Packit 4a16fb
{
Packit 4a16fb
#define PUT16_LABELS
Packit 4a16fb
#include "plugin_ops.h"
Packit 4a16fb
#undef PUT16_LABELS
Packit 4a16fb
	void *put = put16_labels[putidx];
Packit 4a16fb
	unsigned int channel;
Packit 4a16fb
	for (channel = 0; channel < channels; ++channel) {
Packit 4a16fb
		const unsigned char *src;
Packit 4a16fb
		char *dst;
Packit 4a16fb
		int src_step, dst_step;
Packit 4a16fb
		snd_pcm_uframes_t frames1;
Packit 4a16fb
		const snd_pcm_channel_area_t *src_area = &src_areas[channel];
Packit 4a16fb
		const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
Packit 4a16fb
		src = snd_pcm_channel_area_addr(src_area, src_offset);
Packit 4a16fb
		dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
Packit 4a16fb
		src_step = snd_pcm_channel_area_step(src_area);
Packit 4a16fb
		dst_step = snd_pcm_channel_area_step(dst_area);
Packit 4a16fb
		frames1 = frames;
Packit 4a16fb
		while (frames1-- > 0) {
Packit 4a16fb
			int16_t sample = alaw_to_s16(*src);
Packit 4a16fb
			goto *put;
Packit 4a16fb
#define PUT16_END after
Packit 4a16fb
#include "plugin_ops.h"
Packit 4a16fb
#undef PUT16_END
Packit 4a16fb
		after:
Packit 4a16fb
			src += src_step;
Packit 4a16fb
			dst += dst_step;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
Packit 4a16fb
			 snd_pcm_uframes_t dst_offset,
Packit 4a16fb
			 const snd_pcm_channel_area_t *src_areas,
Packit 4a16fb
			 snd_pcm_uframes_t src_offset,
Packit 4a16fb
			 unsigned int channels, snd_pcm_uframes_t frames,
Packit 4a16fb
			 unsigned int getidx)
Packit 4a16fb
{
Packit 4a16fb
#define GET16_LABELS
Packit 4a16fb
#include "plugin_ops.h"
Packit 4a16fb
#undef GET16_LABELS
Packit 4a16fb
	void *get = get16_labels[getidx];
Packit 4a16fb
	unsigned int channel;
Packit 4a16fb
	int16_t sample = 0;
Packit 4a16fb
	for (channel = 0; channel < channels; ++channel) {
Packit 4a16fb
		const char *src;
Packit 4a16fb
		char *dst;
Packit 4a16fb
		int src_step, dst_step;
Packit 4a16fb
		snd_pcm_uframes_t frames1;
Packit 4a16fb
		const snd_pcm_channel_area_t *src_area = &src_areas[channel];
Packit 4a16fb
		const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
Packit 4a16fb
		src = snd_pcm_channel_area_addr(src_area, src_offset);
Packit 4a16fb
		dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
Packit 4a16fb
		src_step = snd_pcm_channel_area_step(src_area);
Packit 4a16fb
		dst_step = snd_pcm_channel_area_step(dst_area);
Packit 4a16fb
		frames1 = frames;
Packit 4a16fb
		while (frames1-- > 0) {
Packit 4a16fb
			goto *get;
Packit 4a16fb
#define GET16_END after
Packit 4a16fb
#include "plugin_ops.h"
Packit 4a16fb
#undef GET16_END
Packit 4a16fb
		after:
Packit 4a16fb
			*dst = s16_to_alaw(sample);
Packit 4a16fb
			src += src_step;
Packit 4a16fb
			dst += dst_step;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#endif /* DOC_HIDDEN */
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_alaw_t *alaw = pcm->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
Packit 4a16fb
	err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
Packit 4a16fb
					 &access_mask);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
Packit 4a16fb
		snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
Packit 4a16fb
		err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
Packit 4a16fb
						 &format_mask);
Packit 4a16fb
	} else {
Packit 4a16fb
		err = _snd_pcm_hw_params_set_format(params, 
Packit 4a16fb
						   SND_PCM_FORMAT_A_LAW);
Packit 4a16fb
	}
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_alaw_t *alaw = pcm->private_data;
Packit 4a16fb
	snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
Packit 4a16fb
	_snd_pcm_hw_params_any(sparams);
Packit 4a16fb
	_snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
Packit 4a16fb
				   &saccess_mask);
Packit 4a16fb
	_snd_pcm_hw_params_set_format(sparams, alaw->sformat);
Packit 4a16fb
	_snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
Packit 4a16fb
					    snd_pcm_hw_params_t *sparams)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_RATE |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_PERIOD_SIZE |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_BUFFER_SIZE |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_PERIODS |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_PERIOD_TIME |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_BUFFER_TIME |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_TICK_TIME);
Packit 4a16fb
	err = _snd_pcm_hw_params_refine(sparams, links, params);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
	
Packit 4a16fb
static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
Packit 4a16fb
					    snd_pcm_hw_params_t *sparams)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_RATE |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_PERIOD_SIZE |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_BUFFER_SIZE |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_PERIODS |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_PERIOD_TIME |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_BUFFER_TIME |
Packit 4a16fb
			      SND_PCM_HW_PARBIT_TICK_TIME);
Packit 4a16fb
	err = _snd_pcm_hw_params_refine(params, links, sparams);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
Packit 4a16fb
{
Packit 4a16fb
	return snd_pcm_hw_refine_slave(pcm, params,
Packit 4a16fb
				       snd_pcm_alaw_hw_refine_cprepare,
Packit 4a16fb
				       snd_pcm_alaw_hw_refine_cchange,
Packit 4a16fb
				       snd_pcm_alaw_hw_refine_sprepare,
Packit 4a16fb
				       snd_pcm_alaw_hw_refine_schange,
Packit 4a16fb
				       snd_pcm_generic_hw_refine);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_alaw_t *alaw = pcm->private_data;
Packit 4a16fb
	snd_pcm_format_t format;
Packit 4a16fb
	int err = snd_pcm_hw_params_slave(pcm, params,
Packit 4a16fb
					  snd_pcm_alaw_hw_refine_cchange,
Packit 4a16fb
					  snd_pcm_alaw_hw_refine_sprepare,
Packit 4a16fb
					  snd_pcm_alaw_hw_refine_schange,
Packit 4a16fb
					  snd_pcm_generic_hw_params);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
Packit 4a16fb
	err = INTERNAL(snd_pcm_hw_params_get_format)(params, &format);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
		
Packit 4a16fb
	if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
Packit 4a16fb
		if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
Packit 4a16fb
			alaw->getput_idx = snd_pcm_linear_get_index(format, SND_PCM_FORMAT_S16);
Packit 4a16fb
			alaw->func = snd_pcm_alaw_encode;
Packit 4a16fb
		} else {
Packit 4a16fb
			alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
Packit 4a16fb
			alaw->func = snd_pcm_alaw_decode;
Packit 4a16fb
		}
Packit 4a16fb
	} else {
Packit 4a16fb
		if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
Packit 4a16fb
			alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, format);
Packit 4a16fb
			alaw->func = snd_pcm_alaw_decode;
Packit 4a16fb
		} else {
Packit 4a16fb
			alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
Packit 4a16fb
			alaw->func = snd_pcm_alaw_encode;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_uframes_t
Packit 4a16fb
snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
Packit 4a16fb
			 const snd_pcm_channel_area_t *areas,
Packit 4a16fb
			 snd_pcm_uframes_t offset,
Packit 4a16fb
			 snd_pcm_uframes_t size,
Packit 4a16fb
			 const snd_pcm_channel_area_t *slave_areas,
Packit 4a16fb
			 snd_pcm_uframes_t slave_offset,
Packit 4a16fb
			 snd_pcm_uframes_t *slave_sizep)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_alaw_t *alaw = pcm->private_data;
Packit 4a16fb
	if (size > *slave_sizep)
Packit 4a16fb
		size = *slave_sizep;
Packit 4a16fb
	alaw->func(slave_areas, slave_offset,
Packit 4a16fb
		   areas, offset, 
Packit 4a16fb
		   pcm->channels, size,
Packit 4a16fb
		   alaw->getput_idx);
Packit 4a16fb
	*slave_sizep = size;
Packit 4a16fb
	return size;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static snd_pcm_uframes_t
Packit 4a16fb
snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
Packit 4a16fb
			const snd_pcm_channel_area_t *areas,
Packit 4a16fb
			snd_pcm_uframes_t offset,
Packit 4a16fb
			snd_pcm_uframes_t size,
Packit 4a16fb
			const snd_pcm_channel_area_t *slave_areas,
Packit 4a16fb
			snd_pcm_uframes_t slave_offset,
Packit 4a16fb
			snd_pcm_uframes_t *slave_sizep)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_alaw_t *alaw = pcm->private_data;
Packit 4a16fb
	if (size > *slave_sizep)
Packit 4a16fb
		size = *slave_sizep;
Packit 4a16fb
	alaw->func(areas, offset, 
Packit 4a16fb
		   slave_areas, slave_offset,
Packit 4a16fb
		   pcm->channels, size,
Packit 4a16fb
		   alaw->getput_idx);
Packit 4a16fb
	*slave_sizep = size;
Packit 4a16fb
	return size;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_alaw_t *alaw = pcm->private_data;
Packit 4a16fb
	snd_output_printf(out, "A-Law conversion PCM (%s)\n", 
Packit 4a16fb
		snd_pcm_format_name(alaw->sformat));
Packit 4a16fb
	if (pcm->setup) {
Packit 4a16fb
		snd_output_printf(out, "Its setup is:\n");
Packit 4a16fb
		snd_pcm_dump_setup(pcm, out);
Packit 4a16fb
	}
Packit 4a16fb
	snd_output_printf(out, "Slave: ");
Packit 4a16fb
	snd_pcm_dump(alaw->plug.gen.slave, out);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_pcm_ops_t snd_pcm_alaw_ops = {
Packit 4a16fb
	.close = snd_pcm_generic_close,
Packit 4a16fb
	.info = snd_pcm_generic_info,
Packit 4a16fb
	.hw_refine = snd_pcm_alaw_hw_refine,
Packit 4a16fb
	.hw_params = snd_pcm_alaw_hw_params,
Packit 4a16fb
	.hw_free = snd_pcm_generic_hw_free,
Packit 4a16fb
	.sw_params = snd_pcm_generic_sw_params,
Packit 4a16fb
	.channel_info = snd_pcm_generic_channel_info,
Packit 4a16fb
	.dump = snd_pcm_alaw_dump,
Packit 4a16fb
	.nonblock = snd_pcm_generic_nonblock,
Packit 4a16fb
	.async = snd_pcm_generic_async,
Packit 4a16fb
	.mmap = snd_pcm_generic_mmap,
Packit 4a16fb
	.munmap = snd_pcm_generic_munmap,
Packit 4a16fb
	.query_chmaps = snd_pcm_generic_query_chmaps,
Packit 4a16fb
	.get_chmap = snd_pcm_generic_get_chmap,
Packit 4a16fb
	.set_chmap = snd_pcm_generic_set_chmap,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new A-Law conversion PCM
Packit 4a16fb
 * \param pcmp Returns created PCM handle
Packit 4a16fb
 * \param name Name of PCM
Packit 4a16fb
 * \param sformat Slave (destination) format
Packit 4a16fb
 * \param slave Slave PCM handle
Packit 4a16fb
 * \param close_slave When set, the slave PCM handle is closed with copy PCM
Packit 4a16fb
 * \retval zero on success otherwise a negative error code
Packit 4a16fb
 * \warning Using of this function might be dangerous in the sense
Packit 4a16fb
 *          of compatibility reasons. The prototype might be freely
Packit 4a16fb
 *          changed in future.
Packit 4a16fb
 */           
Packit 4a16fb
int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
Packit 4a16fb
{
Packit 4a16fb
	snd_pcm_t *pcm;
Packit 4a16fb
	snd_pcm_alaw_t *alaw;
Packit 4a16fb
	int err;
Packit 4a16fb
	assert(pcmp && slave);
Packit 4a16fb
	if (snd_pcm_format_linear(sformat) != 1 &&
Packit 4a16fb
	    sformat != SND_PCM_FORMAT_A_LAW)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	alaw = calloc(1, sizeof(snd_pcm_alaw_t));
Packit 4a16fb
	if (!alaw) {
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	snd_pcm_plugin_init(&alaw->plug);
Packit 4a16fb
	alaw->sformat = sformat;
Packit 4a16fb
	alaw->plug.read = snd_pcm_alaw_read_areas;
Packit 4a16fb
	alaw->plug.write = snd_pcm_alaw_write_areas;
Packit 4a16fb
	alaw->plug.undo_read = snd_pcm_plugin_undo_read_generic;
Packit 4a16fb
	alaw->plug.undo_write = snd_pcm_plugin_undo_write_generic;
Packit 4a16fb
	alaw->plug.gen.slave = slave;
Packit 4a16fb
	alaw->plug.gen.close_slave = close_slave;
Packit 4a16fb
Packit 4a16fb
	err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		free(alaw);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	pcm->ops = &snd_pcm_alaw_ops;
Packit 4a16fb
	pcm->fast_ops = &snd_pcm_plugin_fast_ops;
Packit 4a16fb
	pcm->private_data = alaw;
Packit 4a16fb
	pcm->poll_fd = slave->poll_fd;
Packit 4a16fb
	pcm->poll_events = slave->poll_events;
Packit 4a16fb
	pcm->tstamp_type = slave->tstamp_type;
Packit 4a16fb
	snd_pcm_set_hw_ptr(pcm, &alaw->plug.hw_ptr, -1, 0);
Packit 4a16fb
	snd_pcm_set_appl_ptr(pcm, &alaw->plug.appl_ptr, -1, 0);
Packit 4a16fb
	*pcmp = pcm;
Packit 4a16fb
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*! \page pcm_plugins
Packit 4a16fb
Packit 4a16fb
\section pcm_plugins_alaw Plugin: A-Law
Packit 4a16fb
Packit 4a16fb
This plugin converts A-Law samples to linear or linear to A-Law samples
Packit 4a16fb
from master A-Law conversion PCM to given slave PCM. The channel count,
Packit 4a16fb
format and rate must match for both of them.
Packit 4a16fb
Packit 4a16fb
\code
Packit 4a16fb
pcm.name {
Packit 4a16fb
        type alaw               # A-Law conversion PCM
Packit 4a16fb
        slave STR               # Slave name
Packit 4a16fb
        # or
Packit 4a16fb
        slave {                 # Slave definition
Packit 4a16fb
                pcm STR         # Slave PCM name
Packit 4a16fb
                # or
Packit 4a16fb
                pcm { }         # Slave PCM definition
Packit 4a16fb
                format STR      # Slave format
Packit 4a16fb
        }
Packit 4a16fb
}
Packit 4a16fb
\endcode
Packit 4a16fb
Packit 4a16fb
\subsection pcm_plugins_alaw_funcref Function reference
Packit 4a16fb
Packit 4a16fb
    Packit 4a16fb
      
  • snd_pcm_alaw_open()
  • Packit 4a16fb
      
  • _snd_pcm_alaw_open()
  • Packit 4a16fb
    Packit 4a16fb
    Packit 4a16fb
    */
    Packit 4a16fb
    Packit 4a16fb
    /**
    Packit 4a16fb
     * \brief Creates a new A-Law conversion PCM
    Packit 4a16fb
     * \param pcmp Returns created PCM handle
    Packit 4a16fb
     * \param name Name of PCM
    Packit 4a16fb
     * \param root Root configuration node
    Packit 4a16fb
     * \param conf Configuration node with copy PCM description
    Packit 4a16fb
     * \param stream Stream type
    Packit 4a16fb
     * \param mode Stream mode
    Packit 4a16fb
     * \retval zero on success otherwise a negative error code
    Packit 4a16fb
     * \warning Using of this function might be dangerous in the sense
    Packit 4a16fb
     *          of compatibility reasons. The prototype might be freely
    Packit 4a16fb
     *          changed in future.
    Packit 4a16fb
     */
    Packit 4a16fb
    int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
    Packit 4a16fb
    		       snd_config_t *root, snd_config_t *conf, 
    Packit 4a16fb
    		       snd_pcm_stream_t stream, int mode)
    Packit 4a16fb
    {
    Packit 4a16fb
    	snd_config_iterator_t i, next;
    Packit 4a16fb
    	int err;
    Packit 4a16fb
    	snd_pcm_t *spcm;
    Packit 4a16fb
    	snd_config_t *slave = NULL, *sconf;
    Packit 4a16fb
    	snd_pcm_format_t sformat;
    Packit 4a16fb
    	snd_config_for_each(i, next, conf) {
    Packit 4a16fb
    		snd_config_t *n = snd_config_iterator_entry(i);
    Packit 4a16fb
    		const char *id;
    Packit 4a16fb
    		if (snd_config_get_id(n, &id) < 0)
    Packit 4a16fb
    			continue;
    Packit 4a16fb
    		if (snd_pcm_conf_generic_id(id))
    Packit 4a16fb
    			continue;
    Packit 4a16fb
    		if (strcmp(id, "slave") == 0) {
    Packit 4a16fb
    			slave = n;
    Packit 4a16fb
    			continue;
    Packit 4a16fb
    		}
    Packit 4a16fb
    		SNDERR("Unknown field %s", id);
    Packit 4a16fb
    		return -EINVAL;
    Packit 4a16fb
    	}
    Packit 4a16fb
    	if (!slave) {
    Packit 4a16fb
    		SNDERR("slave is not defined");
    Packit 4a16fb
    		return -EINVAL;
    Packit 4a16fb
    	}
    Packit 4a16fb
    	err = snd_pcm_slave_conf(root, slave, &sconf, 1,
    Packit 4a16fb
    				 SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
    Packit 4a16fb
    	if (err < 0)
    Packit 4a16fb
    		return err;
    Packit 4a16fb
    	if (snd_pcm_format_linear(sformat) != 1 &&
    Packit 4a16fb
    	    sformat != SND_PCM_FORMAT_A_LAW) {
    Packit 4a16fb
    	    	snd_config_delete(sconf);
    Packit 4a16fb
    		SNDERR("invalid slave format");
    Packit 4a16fb
    		return -EINVAL;
    Packit 4a16fb
    	}
    Packit 4a16fb
    	err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);
    Packit 4a16fb
    	snd_config_delete(sconf);
    Packit 4a16fb
    	if (err < 0)
    Packit 4a16fb
    		return err;
    Packit 4a16fb
    	err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
    Packit 4a16fb
    	if (err < 0)
    Packit 4a16fb
    		snd_pcm_close(spcm);
    Packit 4a16fb
    	return err;
    Packit 4a16fb
    }
    Packit 4a16fb
    #ifndef DOC_HIDDEN
    Packit 4a16fb
    SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);
    Packit 4a16fb
    #endif