diff options
author | Jason Wright <jason@cvs.openbsd.org> | 2003-02-01 17:58:19 +0000 |
---|---|---|
committer | Jason Wright <jason@cvs.openbsd.org> | 2003-02-01 17:58:19 +0000 |
commit | 979f6a882bc0d78a80ff1c4c6a57f06b7027a2b0 (patch) | |
tree | 73862c583c1b9c2ee8f2b848ed23414553afe812 /regress/sys/dev | |
parent | 315df967df0885612fe70a456e0d3600c839490d (diff) |
audio encoding tester
Diffstat (limited to 'regress/sys/dev')
-rw-r--r-- | regress/sys/dev/audio/Makefile | 13 | ||||
-rw-r--r-- | regress/sys/dev/audio/adpcm.c | 259 | ||||
-rw-r--r-- | regress/sys/dev/audio/adpcm.h | 21 | ||||
-rw-r--r-- | regress/sys/dev/audio/autest.1 | 74 | ||||
-rw-r--r-- | regress/sys/dev/audio/autest.c | 725 | ||||
-rw-r--r-- | regress/sys/dev/audio/law.c | 286 | ||||
-rw-r--r-- | regress/sys/dev/audio/law.h | 38 |
7 files changed, 1416 insertions, 0 deletions
diff --git a/regress/sys/dev/audio/Makefile b/regress/sys/dev/audio/Makefile new file mode 100644 index 00000000000..22c970043f8 --- /dev/null +++ b/regress/sys/dev/audio/Makefile @@ -0,0 +1,13 @@ + +PROG=autest +SRCS=autest.c adpcm.c law.c +CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes +MAN1=autest.1 +LDADD=-lm + +.ifndef DO_AUTEST +REGRESS_SKIP= +REGRESS_SKIP_TARGETS=autest +.endif + +.include <bsd.regress.mk> diff --git a/regress/sys/dev/audio/adpcm.c b/regress/sys/dev/audio/adpcm.c new file mode 100644 index 00000000000..79cb7c1f25f --- /dev/null +++ b/regress/sys/dev/audio/adpcm.c @@ -0,0 +1,259 @@ +/* $OpenBSD: adpcm.c,v 1.1 2003/02/01 17:58:18 jason Exp $ */ + +/*********************************************************** +Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The +Netherlands. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the names of Stichting Mathematisch +Centrum or CWI not be used in advertising or publicity pertaining to +distribution of the software without specific, written prior permission. + +STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO +THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE +FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +******************************************************************/ + +/* +** Intel/DVI ADPCM coder/decoder. +** +** The algorithm for this coder was taken from the IMA Compatability Project +** proceedings, Vol 2, Number 2; May 1992. +** +** Version 1.2, 18-Dec-92. +** +** Change log: +** - Fixed a stupid bug, where the delta was computed as +** stepsize*code/4 in stead of stepsize*(code+0.5)/4. +** - There was an off-by-one error causing it to pick +** an incorrect delta once in a blue moon. +** - The NODIVMUL define has been removed. Computations are now always done +** using shifts, adds and subtracts. It turned out that, because the standard +** is defined using shift/add/subtract, you needed bits of fixup code +** (because the div/mul simulation using shift/add/sub made some rounding +** errors that real div/mul don't make) and all together the resultant code +** ran slower than just using the shifts all the time. +** - Changed some of the variable names to be more meaningful. +*/ + +#include <sys/types.h> + +#include "adpcm.h" +#include <stdio.h> /*DBG*/ + +#ifndef __STDC__ +#define signed +#endif + +/* Intel ADPCM step variation table */ +static int indexTable[16] = { + -1, -1, -1, -1, 2, 4, 6, 8, + -1, -1, -1, -1, 2, 4, 6, 8, +}; + +static int stepsizeTable[89] = { + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, + 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, + 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, + 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, + 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, + 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, + 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, + 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 +}; + +void +adpcm_coder(indata, outdata, len, state) + int16_t indata[]; + char outdata[]; + int len; + struct adpcm_state *state; +{ + int16_t *inp; /* Input buffer pointer */ + signed char *outp; /* output buffer pointer */ + int val; /* Current input sample value */ + int sign; /* Current adpcm sign bit */ + int delta; /* Current adpcm output value */ + int diff; /* Difference between val and valprev */ + int step; /* Stepsize */ + int valpred; /* Predicted output value */ + int vpdiff; /* Current change to valpred */ + int index; /* Current step change index */ + int outputbuffer; /* place to keep previous 4-bit value */ + int bufferstep; /* toggle between outputbuffer/output */ + + outputbuffer = 0; /* XXX gcc */ + + outp = (signed char *)outdata; + inp = indata; + + valpred = state->valprev; + index = state->index; + step = stepsizeTable[index]; + + bufferstep = 1; + + for ( ; len > 0 ; len-- ) { + val = *inp++; + + /* Step 1 - compute difference with previous value */ + diff = val - valpred; + sign = (diff < 0) ? 8 : 0; + if ( sign ) diff = (-diff); + + /* Step 2 - Divide and clamp */ + /* Note: + ** This code *approximately* computes: + ** delta = diff*4/step; + ** vpdiff = (delta+0.5)*step/4; + ** but in shift step bits are dropped. The net result of this is + ** that even if you have fast mul/div hardware you cannot put it to + ** good use since the fixup would be too expensive. + */ + delta = 0; + vpdiff = (step >> 3); + + if ( diff >= step ) { + delta = 4; + diff -= step; + vpdiff += step; + } + step >>= 1; + if ( diff >= step ) { + delta |= 2; + diff -= step; + vpdiff += step; + } + step >>= 1; + if ( diff >= step ) { + delta |= 1; + vpdiff += step; + } + + /* Step 3 - Update previous value */ + if ( sign ) + valpred -= vpdiff; + else + valpred += vpdiff; + + /* Step 4 - Clamp previous value to 16 bits */ + if ( valpred > 32767 ) + valpred = 32767; + else if ( valpred < -32768 ) + valpred = -32768; + + /* Step 5 - Assemble value, update index and step values */ + delta |= sign; + + index += indexTable[delta]; + if ( index < 0 ) index = 0; + if ( index > 88 ) index = 88; + step = stepsizeTable[index]; + + /* Step 6 - Output value */ + if ( bufferstep ) { + outputbuffer = (delta << 4) & 0xf0; + } else { + *outp++ = (delta & 0x0f) | outputbuffer; + } + bufferstep = !bufferstep; + } + + /* Output last step, if needed */ + if ( !bufferstep ) + *outp++ = outputbuffer; + + state->valprev = valpred; + state->index = index; +} + +void +adpcm_decoder(indata, outdata, len, state) + char indata[]; + int16_t outdata[]; + int len; + struct adpcm_state *state; +{ + signed char *inp; /* Input buffer pointer */ + int16_t *outp; /* output buffer pointer */ + int sign; /* Current adpcm sign bit */ + int delta; /* Current adpcm output value */ + int step; /* Stepsize */ + int valpred; /* Predicted value */ + int vpdiff; /* Current change to valpred */ + int index; /* Current step change index */ + int inputbuffer; /* place to keep next 4-bit value */ + int bufferstep; /* toggle between inputbuffer/input */ + + inputbuffer = 0; /* XXX gcc */ + outp = outdata; + inp = (signed char *)indata; + + valpred = state->valprev; + index = state->index; + step = stepsizeTable[index]; + + bufferstep = 0; + + for ( ; len > 0 ; len-- ) { + + /* Step 1 - get the delta value */ + if ( bufferstep ) { + delta = inputbuffer & 0xf; + } else { + inputbuffer = *inp++; + delta = (inputbuffer >> 4) & 0xf; + } + bufferstep = !bufferstep; + + /* Step 2 - Find new index value (for later) */ + index += indexTable[delta]; + if ( index < 0 ) index = 0; + if ( index > 88 ) index = 88; + + /* Step 3 - Separate sign and magnitude */ + sign = delta & 8; + delta = delta & 7; + + /* Step 4 - Compute difference and new predicted value */ + /* + ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment + ** in adpcm_coder. + */ + vpdiff = step >> 3; + if ( delta & 4 ) vpdiff += step; + if ( delta & 2 ) vpdiff += step>>1; + if ( delta & 1 ) vpdiff += step>>2; + + if ( sign ) + valpred -= vpdiff; + else + valpred += vpdiff; + + /* Step 5 - clamp output value */ + if ( valpred > 32767 ) + valpred = 32767; + else if ( valpred < -32768 ) + valpred = -32768; + + /* Step 6 - Update step value */ + step = stepsizeTable[index]; + + /* Step 7 - Output value */ + *outp++ = valpred; + } + + state->valprev = valpred; + state->index = index; +} diff --git a/regress/sys/dev/audio/adpcm.h b/regress/sys/dev/audio/adpcm.h new file mode 100644 index 00000000000..ee8ab459acd --- /dev/null +++ b/regress/sys/dev/audio/adpcm.h @@ -0,0 +1,21 @@ +/* $OpenBSD: adpcm.h,v 1.1.1.1 2003/02/01 17:58:18 jason Exp $ */ + +/* +** adpcm.h - include file for adpcm coder. +** +** Version 1.0, 7-Jul-92. +*/ + +struct adpcm_state { + int16_t valprev; /* Previous output value */ + char index; /* Index into stepsize table */ +}; + +#ifdef __STDC__ +#define ARGS(x) x +#else +#define ARGS(x) () +#endif + +void adpcm_coder ARGS((int16_t [], char [], int, struct adpcm_state *)); +void adpcm_decoder ARGS((char [], int16_t [], int, struct adpcm_state *)); diff --git a/regress/sys/dev/audio/autest.1 b/regress/sys/dev/audio/autest.1 new file mode 100644 index 00000000000..ba895d60eb9 --- /dev/null +++ b/regress/sys/dev/audio/autest.1 @@ -0,0 +1,74 @@ +.\" $OpenBSD: autest.1,v 1.1 2003/02/01 17:58:18 jason Exp $ +.\" +.\" Copyright (c) 2002 Jason L. Wright (jason@thought.net) +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Jason L. Wright +.\" 4. The name of the author may not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +.\" DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, +.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +.\" ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd September 10, 2002 +.Dt AUTEST 1 +.Os +.Sh NAME +.Nm autest +.Nd test audio encoding output +.Sh SYNOPSIS +.Nm autest +.Sh DESCRIPTION +The +.Nm +utility opens +.Ar /dev/sound +and iterates through all of the encodings supported by the device playing +a 440Hz tone in the proper format. +The tone should sound almost identical in each of the formats. +.Pp +.Nm +can produce tones in any of the following formats and will skip other +formats if supported by the device: +.Bl -tag -width XXXXXXXXXX +.It Cm mulaw +8bit U-Law companded +.It Cm alaw +8bit A-Law companded +.It Cm adpcm +4 bit adaptive differential pulse code modulation +.It Cm ulinear +8 bit unsigned linear. +.It Cm ulinear_le +16 bit unsigned linear little endian +.It Cm ulinear_be +16 bit unsigned linear big endian +.It Cm slinear +8 bit signed linear +.It Cm slinear_le +16 bit signed linear little endian +.It Cm slinear_be +16 bit signed linear big endian +.Sh SEE ALSO +.Xr audio 4 +.Sh BUGS +The ADPCM encoding sounds very noisy on CS4231 (it's probaly incorrect). diff --git a/regress/sys/dev/audio/autest.c b/regress/sys/dev/audio/autest.c new file mode 100644 index 00000000000..d3e1f4e1af3 --- /dev/null +++ b/regress/sys/dev/audio/autest.c @@ -0,0 +1,725 @@ +/* $OpenBSD: autest.c,v 1.1 2003/02/01 17:58:18 jason Exp $ */ + +/* + * Copyright (c) 2002 Jason L. Wright (jason@thought.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Jason L. Wright + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/ioctl.h> +#include <sys/audioio.h> +#include <string.h> +#include <fcntl.h> +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <unistd.h> + +#include "adpcm.h" +#include "law.h" + +int main(void); +void check_encoding(int, audio_encoding_t *); +void check_encoding_mono(int, audio_encoding_t *); +void check_encoding_stereo(int, audio_encoding_t *); +void enc_ulaw_8(int, audio_encoding_t *, int); +void enc_alaw_8(int, audio_encoding_t *, int); +void enc_ulinear_8(int, audio_encoding_t *, int); +void enc_ulinear_be_16(int, audio_encoding_t *, int); +void enc_ulinear_le_16(int, audio_encoding_t *, int); +void enc_slinear_8(int, audio_encoding_t *, int); +void enc_slinear_be_16(int, audio_encoding_t *, int); +void enc_slinear_le_16(int, audio_encoding_t *, int); +void enc_adpcm_8(int, audio_encoding_t *, int); +void audio_wait(int); + +#define PLAYSECS 2 + +int +main() +{ + int fd, i; + + fd = open("/dev/sound", O_RDWR, 0); + if (fd == -1) + err(1, "open"); + + + for (i = 0; ; i++) { + audio_encoding_t enc; + + enc.index = i; + if (ioctl(fd, AUDIO_GETENC, &enc) == -1) + break; + check_encoding(fd, &enc); + } + close(fd); + + return (0); +} + +void +check_encoding(int fd, audio_encoding_t *enc) +{ + printf("%s:%d%s", + enc->name, + enc->precision, + (enc->flags & AUDIO_ENCODINGFLAG_EMULATED) ? "*" : ""); + fflush(stdout); + check_encoding_mono(fd, enc); + check_encoding_stereo(fd, enc); + printf("\n"); +} + +void +check_encoding_stereo(int fd, audio_encoding_t *enc) +{ + printf("...stereo"); + fflush(stdout); + switch (enc->encoding) { + case AUDIO_ENCODING_ULAW: + if (enc->precision == 8) { + enc_ulaw_8(fd, enc, 2); + } + break; + case AUDIO_ENCODING_ALAW: + if (enc->precision == 8) { + enc_alaw_8(fd, enc, 2); + } + break; + case AUDIO_ENCODING_ULINEAR: + if (enc->precision == 8) { + enc_ulinear_8(fd, enc, 2); + } + break; + case AUDIO_ENCODING_ULINEAR_LE: + if (enc->precision == 8) + enc_ulinear_8(fd, enc, 2); + else if (enc->precision == 16) + enc_ulinear_le_16(fd, enc, 2); + break; + case AUDIO_ENCODING_ULINEAR_BE: + if (enc->precision == 8) + enc_ulinear_8(fd, enc, 2); + else if (enc->precision == 16) + enc_ulinear_be_16(fd, enc, 2); + break; + case AUDIO_ENCODING_SLINEAR: + if (enc->precision == 8) { + enc_slinear_8(fd, enc, 2); + } + break; + case AUDIO_ENCODING_SLINEAR_LE: + if (enc->precision == 8) + enc_slinear_8(fd, enc, 2); + else if (enc->precision == 16) + enc_slinear_le_16(fd, enc, 2); + break; + case AUDIO_ENCODING_SLINEAR_BE: + if (enc->precision == 8) + enc_slinear_8(fd, enc, 2); + else if (enc->precision == 16) + enc_slinear_be_16(fd, enc, 2); + break; + default: + printf("[skip]"); + } +} + +void +check_encoding_mono(int fd, audio_encoding_t *enc) +{ + printf("...mono"); + fflush(stdout); + switch (enc->encoding) { + case AUDIO_ENCODING_ULAW: + if (enc->precision == 8) { + enc_ulaw_8(fd, enc, 1); + } + break; + case AUDIO_ENCODING_ALAW: + if (enc->precision == 8) { + enc_alaw_8(fd, enc, 1); + } + break; + case AUDIO_ENCODING_ULINEAR: + if (enc->precision == 8) { + enc_ulinear_8(fd, enc, 1); + } + break; + case AUDIO_ENCODING_ULINEAR_LE: + if (enc->precision == 8) + enc_ulinear_8(fd, enc, 1); + else if (enc->precision == 16) + enc_ulinear_le_16(fd, enc, 1); + break; + case AUDIO_ENCODING_ULINEAR_BE: + if (enc->precision == 8) + enc_ulinear_8(fd, enc, 1); + else if (enc->precision == 16) + enc_ulinear_be_16(fd, enc, 1); + break; + case AUDIO_ENCODING_SLINEAR: + if (enc->precision == 8) { + enc_slinear_8(fd, enc, 1); + } + break; + case AUDIO_ENCODING_SLINEAR_LE: + if (enc->precision == 8) + enc_slinear_8(fd, enc, 1); + else if (enc->precision == 16) + enc_slinear_le_16(fd, enc, 1); + break; + case AUDIO_ENCODING_SLINEAR_BE: + if (enc->precision == 8) + enc_slinear_8(fd, enc, 1); + else if (enc->precision == 16) + enc_slinear_be_16(fd, enc, 1); + break; +#if 0 + case AUDIO_ENCODING_ADPCM: + if (enc->precision == 8) + enc_adpcm_8(fd, enc, 1); + break; +#endif + default: + printf("[skip]"); + } +} + +void +enc_ulinear_8(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + u_int8_t *samples = NULL, *p; + int i, j; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (u_int8_t *)malloc(inf.play.sample_rate * chans); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0, p = samples; i < inf.play.sample_rate; i++) { + double d; + u_int8_t v; + + d = 127.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + d = rint(d + 127.0); + v = d; + + for (j = 0; j < chans; j++) { + *p = v; + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) + write(fd, samples, inf.play.sample_rate * chans); + audio_wait(fd); + +out: + if (samples != NULL) + free(samples); +} + +void +enc_slinear_8(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + int8_t *samples = NULL, *p; + int i, j; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (int8_t *)malloc(inf.play.sample_rate * chans); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0, p = samples; i < inf.play.sample_rate; i++) { + double d; + int8_t v; + + d = 127.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + d = rint(d); + v = d; + + for (j = 0; j < chans; j++) { + *p = v; + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) + write(fd, samples, inf.play.sample_rate * chans); + audio_wait(fd); + +out: + if (samples != NULL) + free(samples); +} + +void +enc_slinear_be_16(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + u_int8_t *samples = NULL, *p; + int i, j; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (int8_t *)malloc(inf.play.sample_rate * chans * 2); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0, p = samples; i < inf.play.sample_rate; i++) { + double d; + int16_t v; + + d = 32767.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + d = rint(d); + v = d; + + for (j = 0; j < chans; j++) { + *p = (v & 0xff00) >> 8; + p++; + *p = (v & 0x00ff) >> 0; + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) + write(fd, samples, inf.play.sample_rate * chans * 2); + audio_wait(fd); + +out: + if (samples != NULL) + free(samples); +} + +void +enc_slinear_le_16(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + u_int8_t *samples = NULL, *p; + int i, j; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (int8_t *)malloc(inf.play.sample_rate * chans * 2); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0, p = samples; i < inf.play.sample_rate; i++) { + double d; + int16_t v; + + d = 32767.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + d = rint(d); + v = d; + + for (j = 0; j < chans; j++) { + *p = (v & 0x00ff) >> 0; + p++; + *p = (v & 0xff00) >> 8; + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) + write(fd, samples, inf.play.sample_rate * chans * 2); + audio_wait(fd); + +out: + if (samples != NULL) + free(samples); +} + +void +enc_ulinear_le_16(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + u_int8_t *samples = NULL, *p; + int i, j; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (u_int8_t *)malloc(inf.play.sample_rate * chans * 2); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0, p = samples; i < inf.play.sample_rate; i++) { + double d; + u_int16_t v; + + d = 32767.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + d = rint(d + 32767.0); + v = d; + + for (j = 0; j < chans; j++) { + *p = (v >> 0) & 0xff; + p++; + *p = (v >> 8) & 0xff; + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) + write(fd, samples, inf.play.sample_rate * chans * 2); + audio_wait(fd); + +out: + if (samples != NULL) + free(samples); +} + +void +enc_ulinear_be_16(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + u_int8_t *samples = NULL, *p; + int i, j; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (u_int8_t *)malloc(inf.play.sample_rate * chans * 2); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0, p = samples; i < inf.play.sample_rate; i++) { + double d; + u_int16_t v; + + d = 32767.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + d = rint(d + 32767.0); + v = d; + + for (j = 0; j < chans; j++) { + *p = (v >> 8) & 0xff; + p++; + *p = (v >> 0) & 0xff; + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) + write(fd, samples, inf.play.sample_rate * chans * 2); + audio_wait(fd); + +out: + if (samples != NULL) + free(samples); +} + +void +enc_adpcm_8(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + struct adpcm_state adsts; + int16_t *samples = NULL; + int i; + char *outbuf = NULL; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + bzero(&adsts, sizeof(adsts)); + + samples = (int16_t *)malloc(inf.play.sample_rate * sizeof(*samples)); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + outbuf = (char *)malloc(inf.play.sample_rate / 2); + if (outbuf == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0; i < inf.play.sample_rate; i++) { + double d; + + d = 32767.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + samples[i] = rint(d); + } + + for (i = 0; i < PLAYSECS; i++) { + adpcm_coder(samples, outbuf, inf.play.sample_rate, &adsts); + write(fd, outbuf, inf.play.sample_rate / 2); + } + audio_wait(fd); + +out: + if (samples == NULL) + free(samples); + if (outbuf == NULL) + free(outbuf); +} + +void +enc_ulaw_8(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + int16_t *samples = NULL; + int i, j; + u_int8_t *outbuf = NULL, *p; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (int16_t *)calloc(inf.play.sample_rate, sizeof(*samples)); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + outbuf = (u_int8_t *)malloc(inf.play.sample_rate * chans); + if (outbuf == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0; i < inf.play.sample_rate; i++) { + float x; + + x = 32765.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + samples[i] = x; + } + + for (i = 0, p = outbuf; i < inf.play.sample_rate; i++) { + for (j = 0; j < chans; j++) { + *p = linear2ulaw(samples[i]); + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) { + write(fd, outbuf, inf.play.sample_rate * chans); + } + audio_wait(fd); + +out: + if (samples == NULL) + free(samples); + if (outbuf == NULL) + free(outbuf); +} + +void +enc_alaw_8(int fd, audio_encoding_t *enc, int chans) +{ + audio_info_t inf; + int16_t *samples = NULL; + int i, j; + u_int8_t *outbuf = NULL, *p; + + AUDIO_INITINFO(&inf); + inf.play.precision = enc->precision; + inf.play.encoding = enc->encoding; + inf.play.channels = chans; + + if (ioctl(fd, AUDIO_SETINFO, &inf) == -1) { + warn("setinfo"); + goto out; + } + + if (ioctl(fd, AUDIO_GETINFO, &inf) == -1) { + warn("getinfo"); + goto out; + } + + samples = (int16_t *)calloc(inf.play.sample_rate, sizeof(*samples)); + if (samples == NULL) { + warn("malloc"); + goto out; + } + + outbuf = (u_int8_t *)malloc(inf.play.sample_rate * chans); + if (outbuf == NULL) { + warn("malloc"); + goto out; + } + + for (i = 0; i < inf.play.sample_rate; i++) { + float x; + + x = 32767.0 * sin(((double)i / (double)inf.play.sample_rate) * + (2 * M_PI * 440.0)); + samples[i] = x; + } + + for (i = 0, p = outbuf; i < inf.play.sample_rate; i++) { + for (j = 0; j < chans; j++) { + *p = linear2alaw(samples[i]); + p++; + } + } + + for (i = 0; i < PLAYSECS; i++) { + write(fd, outbuf, inf.play.sample_rate * chans); + } + audio_wait(fd); + +out: + if (samples == NULL) + free(samples); + if (outbuf == NULL) + free(outbuf); +} + +void +audio_wait(int fd) +{ + if (ioctl(fd, AUDIO_DRAIN, NULL) == -1) + warn("drain"); +} diff --git a/regress/sys/dev/audio/law.c b/regress/sys/dev/audio/law.c new file mode 100644 index 00000000000..0b79c651eb6 --- /dev/null +++ b/regress/sys/dev/audio/law.c @@ -0,0 +1,286 @@ +/* $OpenBSD: law.c,v 1.1.1.1 2003/02/01 17:58:18 jason Exp $ */ + +/* + * This source code is a product of Sun Microsystems, Inc. and is provided + * for unrestricted use. Users may copy or modify this source code without + * charge. + * + * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING + * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR + * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. + * + * Sun source code is provided with no support and without any obligation on + * the part of Sun Microsystems, Inc. to assist in its use, correction, + * modification or enhancement. + * + * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE + * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE + * OR ANY PART THEREOF. + * + * In no event will Sun Microsystems, Inc. be liable for any lost revenue + * or profits or other special, indirect and consequential damages, even if + * Sun has been advised of the possibility of such damages. + * + * Sun Microsystems, Inc. + * 2550 Garcia Avenue + * Mountain View, California 94043 + */ + +#include <sys/types.h> +#include "law.h" + +/* + * g711.c + * + * u-law, A-law and linear PCM conversions. + */ +#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ +#define QUANT_MASK (0xf) /* Quantization field mask. */ +#define NSEGS (8) /* Number of A-law segments. */ +#define SEG_SHIFT (4) /* Left shift for segment number. */ +#define SEG_MASK (0x70) /* Segment field mask. */ + +static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF, + 0x1FF, 0x3FF, 0x7FF, 0xFFF}; +static short seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF, + 0x3FF, 0x7FF, 0xFFF, 0x1FFF}; + +/* copy from CCITT G.711 specifications */ +u_int8_t _u2a[128] = { /* u- to A-law conversions */ + 1, 1, 2, 2, 3, 3, 4, 4, + 5, 5, 6, 6, 7, 7, 8, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 27, 29, 31, 33, 34, 35, 36, + 37, 38, 39, 40, 41, 42, 43, 44, + 46, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, + 64, 65, 66, 67, 68, 69, 70, 71, + 72, 73, 74, 75, 76, 77, 78, 79, + 80, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 91, 92, 93, 94, 95, 96, + 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, + 113, 114, 115, 116, 117, 118, 119, 120, + 121, 122, 123, 124, 125, 126, 127, 128}; + +u_int8_t _a2u[128] = { /* A- to u-law conversions */ + 1, 3, 5, 7, 9, 11, 13, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + 32, 32, 33, 33, 34, 34, 35, 35, + 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 48, 49, 49, + 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 64, + 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, + 80, 81, 82, 83, 84, 85, 86, 87, + 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, + 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127}; + +static int +search(int val, short *table, int size) +{ + int i; + + for (i = 0; i < size; i++) + if (val <= *table++) + return (i); + return (size); +} + +/* + * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law + * + * linear2alaw() accepts an 16-bit integer and encodes it as A-law data. + * + * Linear Input Code Compressed Code + * ------------------------ --------------- + * 0000000wxyza 000wxyz + * 0000001wxyza 001wxyz + * 000001wxyzab 010wxyz + * 00001wxyzabc 011wxyz + * 0001wxyzabcd 100wxyz + * 001wxyzabcde 101wxyz + * 01wxyzabcdef 110wxyz + * 1wxyzabcdefg 111wxyz + * + * For further information see John C. Bellamy's Digital Telephony, 1982, + * John Wiley & Sons, pps 98-111 and 472-476. + */ +u_int8_t +linear2alaw(int pcm_val) /* 2's complement (16-bit range) */ +{ + int mask; + int seg; + u_int8_t aval; + + pcm_val = pcm_val >> 3; + + if (pcm_val >= 0) { + mask = 0xD5; /* sign (7th) bit = 1 */ + } else { + mask = 0x55; /* sign bit = 0 */ + pcm_val = -pcm_val - 1; + } + + /* Convert the scaled magnitude to segment number. */ + seg = search(pcm_val, seg_aend, 8); + + /* Combine the sign, segment, and quantization bits. */ + + if (seg >= 8) /* out of range, return maximum value. */ + return (0x7F ^ mask); + else { + aval = seg << SEG_SHIFT; + if (seg < 2) + aval |= (pcm_val >> 4) & QUANT_MASK; + else + aval |= (pcm_val >> seg) & QUANT_MASK; + return (aval ^ mask); + } +} + +/* + * alaw2linear() - Convert an A-law value to 16-bit linear PCM + * + */ +int +alaw2linear(u_int8_t a_val) +{ + int t; + int seg; + + a_val ^= 0x55; + + t = (a_val & QUANT_MASK) << 4; + seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; + switch (seg) { + case 0: + t += 8; + break; + case 1: + t += 0x108; + break; + default: + t += 0x108; + t <<= seg - 1; + } + return ((a_val & SIGN_BIT) ? t : -t); +} + +#define BIAS (0x84) /* Bias for linear code. */ +#define CLIP 8159 + +/* + * linear2ulaw() - Convert a linear PCM value to u-law + * + * In order to simplify the encoding process, the original linear magnitude + * is biased by adding 33 which shifts the encoding range from (0 - 8158) to + * (33 - 8191). The result can be seen in the following encoding table: + * + * Biased Linear Input Code Compressed Code + * ------------------------ --------------- + * 00000001wxyza 000wxyz + * 0000001wxyzab 001wxyz + * 000001wxyzabc 010wxyz + * 00001wxyzabcd 011wxyz + * 0001wxyzabcde 100wxyz + * 001wxyzabcdef 101wxyz + * 01wxyzabcdefg 110wxyz + * 1wxyzabcdefgh 111wxyz + * + * Each biased linear code has a leading 1 which identifies the segment + * number. The value of the segment number is equal to 7 minus the number + * of leading 0's. The quantization interval is directly available as the + * four bits wxyz. * The trailing bits (a - h) are ignored. + * + * Ordinarily the complement of the resulting code word is used for + * transmission, and so the code word is complemented before it is returned. + * + * For further information see John C. Bellamy's Digital Telephony, 1982, + * John Wiley & Sons, pps 98-111 and 472-476. + */ +u_int8_t +linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */ +{ + int mask; + int seg; + u_int8_t uval; + + /* Get the sign and the magnitude of the value. */ + pcm_val = pcm_val >> 2; + if (pcm_val < 0) { + pcm_val = -pcm_val; + mask = 0x7F; + } else { + mask = 0xFF; + } + if (pcm_val > CLIP) + pcm_val = CLIP; + pcm_val += (BIAS >> 2); + + /* Convert the scaled magnitude to segment number. */ + seg = search(pcm_val, seg_uend, 8); + + /* + * Combine the sign, segment, quantization bits; + * and complement the code word. + */ + if (seg >= 8) /* out of range, return maximum value. */ + return (0x7F ^ mask); + else { + uval = (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF); + return (uval ^ mask); + } + +} + +/* + * ulaw2linear() - Convert a u-law value to 16-bit linear PCM + * + * First, a biased linear code is derived from the code word. An unbiased + * output can then be obtained by subtracting 33 from the biased code. + * + * Note that this function expects to be passed the complement of the + * original code word. This is in keeping with ISDN conventions. + */ +int +ulaw2linear(u_int8_t u_val) +{ + int t; + + /* Complement to obtain normal u-law value. */ + u_val = ~u_val; + + /* + * Extract and bias the quantization bits. Then + * shift up by the segment number and subtract out the bias. + */ + t = ((u_val & QUANT_MASK) << 3) + BIAS; + t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; + + return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); +} + +/* A-law to u-law conversion */ +u_int8_t +alaw2ulaw(u_int8_t aval) +{ + aval &= 0xff; + return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : + (0x7F ^ _a2u[aval ^ 0x55])); +} + +/* u-law to A-law conversion */ +u_int8_t +ulaw2alaw(u_int8_t uval) +{ + uval &= 0xff; + return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : + (0x55 ^ (_u2a[0x7F ^ uval] - 1))); +} diff --git a/regress/sys/dev/audio/law.h b/regress/sys/dev/audio/law.h new file mode 100644 index 00000000000..cc4809e9a3e --- /dev/null +++ b/regress/sys/dev/audio/law.h @@ -0,0 +1,38 @@ +/* $OpenBSD: law.h,v 1.1 2003/02/01 17:58:18 jason Exp $ */ + +/* + * Copyright (c) 2003 Jason L. Wright (jason@thought.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Jason L. Wright + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +u_int8_t linear2alaw(int); +u_int8_t linear2ulaw(int); +int alaw2linear(u_int8_t); +int ulaw2linear(u_int8_t); +u_int8_t alaw2ulaw(u_int8_t); +u_int8_t ulaw2alaw(u_int8_t); |