2 * PCM - A-Law conversion
3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pcm_local.h"
24 #include "pcm_plugin.h"
26 typedef void (*alaw_f)(snd_pcm_channel_area_t *src_areas,
28 snd_pcm_channel_area_t *dst_areas,
30 size_t frames, size_t channels, int getputidx);
33 /* This field need to be the first */
34 snd_pcm_plugin_t plug;
40 static inline int val_seg(int val)
58 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
60 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
62 * Linear Input Code Compressed Code
63 * ------------------------ ---------------
64 * 0000000wxyza 000wxyz
65 * 0000001wxyza 001wxyz
66 * 000001wxyzab 010wxyz
67 * 00001wxyzabc 011wxyz
68 * 0001wxyzabcd 100wxyz
69 * 001wxyzabcde 101wxyz
70 * 01wxyzabcdef 110wxyz
71 * 1wxyzabcdefg 111wxyz
73 * For further information see John C. Bellamy's Digital Telephony, 1982,
74 * John Wiley & Sons, pps 98-111 and 472-476.
77 static unsigned char s16_to_alaw(int pcm_val)
95 /* Convert the scaled magnitude to segment number. */
96 seg = val_seg(pcm_val);
97 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
103 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
106 static int alaw_to_s16(unsigned char a_val)
116 seg = (t >> 4) & 0x07;
117 t = ((t & 0x0f) << 4) + 0x108;
120 return ((a_val & 0x80) ? t : -t);
123 static void alaw_decode(snd_pcm_channel_area_t *src_areas,
125 snd_pcm_channel_area_t *dst_areas,
127 size_t frames, size_t channels, int putidx)
130 #include "plugin_ops.h"
132 void *put = put16_labels[putidx];
134 for (channel = 0; channel < channels; ++channel) {
137 int src_step, dst_step;
139 snd_pcm_channel_area_t *src_area = &src_areas[channel];
140 snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
142 if (!src_area->enabled) {
143 if (dst_area->wanted)
144 snd_pcm_area_silence(&dst_areas[channel], dst_offset, frames, dst_sfmt);
145 dst_area->enabled = 0;
148 dst_area->enabled = 1;
150 src = snd_pcm_channel_area_addr(src_area, src_offset);
151 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
152 src_step = snd_pcm_channel_area_step(src_area);
153 dst_step = snd_pcm_channel_area_step(dst_area);
155 while (frames1-- > 0) {
156 int16_t sample = alaw_to_s16(*src);
158 #define PUT16_END after
159 #include "plugin_ops.h"
168 static void alaw_encode(snd_pcm_channel_area_t *src_areas,
170 snd_pcm_channel_area_t *dst_areas,
172 size_t frames, size_t channels, int getidx)
175 #include "plugin_ops.h"
177 void *get = get16_labels[getidx];
180 for (channel = 0; channel < channels; ++channel) {
183 int src_step, dst_step;
185 snd_pcm_channel_area_t *src_area = &src_areas[channel];
186 snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
188 if (!src_area->enabled) {
189 if (dst_area->wanted)
190 snd_pcm_area_silence(&dst_area->area, 0, frames, dst_sfmt);
191 dst_area->enabled = 0;
194 dst_area->enabled = 1;
196 src = snd_pcm_channel_area_addr(src_area, src_offset);
197 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
198 src_step = snd_pcm_channel_area_step(src_area);
199 dst_step = snd_pcm_channel_area_step(dst_area);
201 while (frames1-- > 0) {
203 #define GET16_END after
204 #include "plugin_ops.h"
207 *dst = s16_to_alaw(sample);
214 static int snd_pcm_alaw_hw_info(snd_pcm_t *pcm, snd_pcm_hw_info_t * info)
216 snd_pcm_alaw_t *alaw = pcm->private;
217 unsigned int format_mask, access_mask;
219 info->access_mask &= (SND_PCM_ACCBIT_MMAP_INTERLEAVED |
220 SND_PCM_ACCBIT_RW_INTERLEAVED |
221 SND_PCM_ACCBIT_MMAP_NONINTERLEAVED |
222 SND_PCM_ACCBIT_RW_NONINTERLEAVED);
223 access_mask = info->access_mask;
224 if (access_mask == 0)
226 if (alaw->sformat == SND_PCM_FORMAT_MU_LAW)
227 info->format_mask &= SND_PCM_FMTBIT_LINEAR;
229 info->format_mask &= SND_PCM_FMTBIT_MU_LAW;
230 format_mask = info->format_mask;
231 if (format_mask == 0)
234 info->format_mask = 1U << alaw->sformat;
235 info->access_mask = SND_PCM_ACCBIT_MMAP;
236 err = snd_pcm_hw_info(alaw->plug.slave, info);
237 info->format_mask = format_mask;
238 info->access_mask = access_mask;
241 info->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
242 snd_pcm_hw_info_complete(info);
246 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
248 snd_pcm_alaw_t *alaw = pcm->private;
249 snd_pcm_t *slave = alaw->plug.slave;
250 snd_pcm_hw_info_t sinfo;
251 snd_pcm_hw_params_t sparams;
253 snd_pcm_hw_params_to_info(params, &sinfo);
254 sinfo.format_mask = 1 << alaw->sformat;
255 sinfo.access_mask = SND_PCM_ACCBIT_MMAP;
256 err = snd_pcm_hw_params_info(slave, &sparams, &sinfo);
257 params->fail_mask = sparams.fail_mask;
260 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
261 if (alaw->sformat == SND_PCM_FORMAT_MU_LAW) {
262 alaw->getput_idx = get_index(params->format, SND_PCM_FORMAT_S16);
263 alaw->func = alaw_encode;
265 alaw->getput_idx = put_index(SND_PCM_FORMAT_S16, alaw->sformat);
266 alaw->func = alaw_decode;
269 if (alaw->sformat == SND_PCM_FORMAT_MU_LAW) {
270 alaw->getput_idx = put_index(SND_PCM_FORMAT_S16, params->format);
271 alaw->func = alaw_decode;
273 alaw->getput_idx = get_index(alaw->sformat, SND_PCM_FORMAT_S16);
274 alaw->func = alaw_encode;
280 static ssize_t snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
281 snd_pcm_channel_area_t *areas,
286 snd_pcm_alaw_t *alaw = pcm->private;
287 snd_pcm_t *slave = alaw->plug.slave;
290 if (slave_sizep && *slave_sizep < size)
293 while (xfer < size) {
294 size_t frames = snd_pcm_mmap_playback_xfer(slave, size - xfer);
295 alaw->func(areas, offset,
296 snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
297 frames, pcm->channels,
299 err = snd_pcm_mmap_forward(slave, frames);
302 assert((size_t)err == frames);
305 snd_pcm_mmap_hw_forward(pcm, err);
315 static ssize_t snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
316 snd_pcm_channel_area_t *areas,
321 snd_pcm_alaw_t *alaw = pcm->private;
322 snd_pcm_t *slave = alaw->plug.slave;
325 if (slave_sizep && *slave_sizep < size)
328 while (xfer < size) {
329 size_t frames = snd_pcm_mmap_capture_xfer(slave, size - xfer);
330 alaw->func(snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
332 frames, pcm->channels,
334 err = snd_pcm_mmap_forward(slave, frames);
337 assert((size_t)err == frames);
340 snd_pcm_mmap_hw_forward(pcm, err);
350 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, FILE *fp)
352 snd_pcm_alaw_t *alaw = pcm->private;
353 fprintf(fp, "A-Law conversion PCM (%s)\n",
354 snd_pcm_format_name(alaw->sformat));
356 fprintf(fp, "Its setup is:\n");
357 snd_pcm_dump_setup(pcm, fp);
359 fprintf(fp, "Slave: ");
360 snd_pcm_dump(alaw->plug.slave, fp);
363 snd_pcm_ops_t snd_pcm_alaw_ops = {
364 close: snd_pcm_plugin_close,
365 info: snd_pcm_plugin_info,
366 hw_info: snd_pcm_alaw_hw_info,
367 hw_params: snd_pcm_alaw_hw_params,
368 sw_params: snd_pcm_plugin_sw_params,
369 dig_info: snd_pcm_plugin_dig_info,
370 dig_params: snd_pcm_plugin_dig_params,
371 channel_info: snd_pcm_plugin_channel_info,
372 dump: snd_pcm_alaw_dump,
373 nonblock: snd_pcm_plugin_nonblock,
374 async: snd_pcm_plugin_async,
375 mmap: snd_pcm_plugin_mmap,
376 munmap: snd_pcm_plugin_munmap,
379 int snd_pcm_alaw_open(snd_pcm_t **pcmp, char *name, int sformat, snd_pcm_t *slave, int close_slave)
382 snd_pcm_alaw_t *alaw;
383 assert(pcmp && slave);
384 if (snd_pcm_format_linear(sformat) != 1 &&
385 sformat != SND_PCM_FORMAT_A_LAW)
387 alaw = calloc(1, sizeof(snd_pcm_alaw_t));
391 alaw->sformat = sformat;
392 alaw->plug.read = snd_pcm_alaw_read_areas;
393 alaw->plug.write = snd_pcm_alaw_write_areas;
394 alaw->plug.slave = slave;
395 alaw->plug.close_slave = close_slave;
397 pcm = calloc(1, sizeof(snd_pcm_t));
403 pcm->name = strdup(name);
404 pcm->type = SND_PCM_TYPE_ALAW;
405 pcm->stream = slave->stream;
406 pcm->mode = slave->mode;
407 pcm->ops = &snd_pcm_alaw_ops;
409 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
410 pcm->fast_op_arg = pcm;
412 pcm->poll_fd = slave->poll_fd;
413 pcm->hw_ptr = &alaw->plug.hw_ptr;
414 pcm->appl_ptr = &alaw->plug.appl_ptr;
420 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, char *name,
422 int stream, int mode)
424 snd_config_iterator_t i;
429 snd_config_foreach(i, conf) {
430 snd_config_t *n = snd_config_entry(i);
431 if (strcmp(n->id, "comment") == 0)
433 if (strcmp(n->id, "type") == 0)
435 if (strcmp(n->id, "stream") == 0)
437 if (strcmp(n->id, "sname") == 0) {
438 err = snd_config_string_get(n, &sname);
440 ERR("Invalid type for %s", n->id);
445 if (strcmp(n->id, "sformat") == 0) {
447 err = snd_config_string_get(n, &f);
449 ERR("Invalid type for %s", n->id);
452 sformat = snd_pcm_format_value(f);
454 ERR("Unknown sformat");
457 if (snd_pcm_format_linear(sformat) != 1 &&
458 sformat != SND_PCM_FORMAT_A_LAW) {
459 ERR("Invalid sformat");
464 ERR("Unknown field %s", n->id);
468 ERR("sname is not defined");
472 ERR("sformat is not defined");
475 /* This is needed cause snd_config_update may destroy config */
476 sname = strdup(sname);
479 err = snd_pcm_open(&spcm, sname, stream, mode);
483 err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);