Home | History | Annotate | Line # | Download | only in common
audio.c revision 1.15
      1 /*	$NetBSD: audio.c,v 1.15 2002/01/15 23:48:52 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * XXX this is slightly icky in places...
     33  */
     34 
     35 #include <sys/types.h>
     36 #include <sys/audioio.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/time.h>
     39 
     40 #include <ctype.h>
     41 #include <err.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 
     46 #include "libaudio.h"
     47 
     48 /* what format am i? */
     49 
     50 struct {
     51 	const char *fname;
     52 	int fno;
     53 } formats[] = {
     54 	{ "sunau",		AUDIO_FORMAT_SUN },
     55 	{ "au",			AUDIO_FORMAT_SUN },
     56 	{ "sun",		AUDIO_FORMAT_SUN },
     57 	{ "wav",		AUDIO_FORMAT_WAV },
     58 	{ "wave",		AUDIO_FORMAT_WAV },
     59 	{ "riff",		AUDIO_FORMAT_WAV },
     60 	{ "no",			AUDIO_FORMAT_NONE },
     61 	{ "none",		AUDIO_FORMAT_NONE },
     62 	{ NULL, -1 }
     63 };
     64 
     65 int
     66 audio_format_from_str(str)
     67 	char *str;
     68 {
     69 	int	i;
     70 
     71 	for (i = 0; formats[i].fname; i++)
     72 		if (strcasecmp(formats[i].fname, str) == 0)
     73 			break;
     74 	return (formats[i].fno);
     75 }
     76 
     77 
     78 
     79 /* back and forth between encodings */
     80 struct {
     81 	const char *ename;
     82 	int eno;
     83 } encs[] = {
     84 	{ AudioEmulaw,		AUDIO_ENCODING_ULAW },
     85 	{ "ulaw",		AUDIO_ENCODING_ULAW },
     86 	{ AudioEalaw, 		AUDIO_ENCODING_ALAW },
     87 	{ AudioEslinear,	AUDIO_ENCODING_SLINEAR },
     88 	{ "linear",		AUDIO_ENCODING_SLINEAR },
     89 	{ AudioEulinear,	AUDIO_ENCODING_ULINEAR },
     90 	{ AudioEadpcm,		AUDIO_ENCODING_ADPCM },
     91 	{ "ADPCM",		AUDIO_ENCODING_ADPCM },
     92 	{ AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE },
     93 	{ "linear_le",		AUDIO_ENCODING_SLINEAR_LE },
     94 	{ AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE },
     95 	{ AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE },
     96 	{ "linear_be",		AUDIO_ENCODING_SLINEAR_BE },
     97 	{ AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE },
     98 	{ AudioEmpeg_l1_stream,	AUDIO_ENCODING_MPEG_L1_STREAM },
     99 	{ AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
    100 	{ AudioEmpeg_l1_system,	AUDIO_ENCODING_MPEG_L1_SYSTEM },
    101 	{ AudioEmpeg_l2_stream,	AUDIO_ENCODING_MPEG_L2_STREAM },
    102 	{ AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
    103 	{ AudioEmpeg_l2_system,	AUDIO_ENCODING_MPEG_L2_SYSTEM },
    104 	{ NULL, -1 }
    105 };
    106 
    107 
    108 const char *
    109 audio_enc_from_val(val)
    110 	int	val;
    111 {
    112 	int	i;
    113 
    114 	for (i = 0; encs[i].ename; i++)
    115 		if (encs[i].eno == val)
    116 			break;
    117 	return (encs[i].ename);
    118 }
    119 
    120 int
    121 audio_enc_to_val(enc)
    122 	const	char *enc;
    123 {
    124 	int	i;
    125 
    126 	for (i = 0; encs[i].ename; i++)
    127 		if (strcmp(encs[i].ename, enc) == 0)
    128 			break;
    129 	if (encs[i].ename)
    130 		return (encs[i].eno);
    131 	else
    132 		return (AUDIO_ENOENT);
    133 }
    134 
    135 void
    136 decode_int(arg, intp)
    137 	const char *arg;
    138 	int *intp;
    139 {
    140 	char	*ep;
    141 	int	ret;
    142 
    143 	ret = (int)strtoul(arg, &ep, 0);
    144 
    145 	if (ep[0] == '\0') {
    146 		*intp = ret;
    147 		return;
    148 	}
    149 	errx(1, "argument `%s' not a valid integer", arg);
    150 }
    151 
    152 void
    153 decode_time(arg, tvp)
    154 	const char *arg;
    155 	struct timeval *tvp;
    156 {
    157 	char	*s, *colon, *dot;
    158 	char	*copy = strdup(arg);
    159 	int	first;
    160 
    161 	if (copy == NULL)
    162 		err(1, "could not allocate a copy of %s", arg);
    163 
    164 	tvp->tv_sec = tvp->tv_usec = 0;
    165 	s = copy;
    166 
    167 	/* handle [hh:]mm:ss.dd */
    168 	if ((colon = strchr(s, ':')) != NULL) {
    169 		*colon++ = '\0';
    170 		decode_int(s, &first);
    171 		tvp->tv_sec = first * 60;	/* minutes */
    172 		s = colon;
    173 
    174 		if ((colon = strchr(s, ':')) != NULL) {
    175 			*colon++ = '\0';
    176 			decode_int(s, &first);
    177 			tvp->tv_sec *= 60;
    178 			tvp->tv_sec += first;	/* minutes and hours */
    179 			s = colon;
    180 		}
    181 	}
    182 	if ((dot = strchr(s, '.')) != NULL) {
    183 		int 	i, base = 100000;
    184 
    185 		*dot++ = '\0';
    186 
    187 		for (i = 0; i < 6; i++, base /= 10) {
    188 			if (!dot[i])
    189 				break;
    190 			if (!isdigit(dot[i]))
    191 				errx(1, "argument `%s' is not a value time specification", arg);
    192 			tvp->tv_usec += base * (dot[i] - '0');
    193 		}
    194 	}
    195 	decode_int(s, &first);
    196 	tvp->tv_sec += first;
    197 #if 0
    198 printf("tvp->tv_sec = %ld, tvp->tv_usec = %ld\n", tvp->tv_sec, tvp->tv_usec);
    199 #endif
    200 
    201 	free(copy);
    202 }
    203 
    204 /*
    205  * decode a string into an encoding value.
    206  */
    207 void
    208 decode_encoding(arg, encp)
    209 	const char *arg;
    210 	int *encp;
    211 {
    212 	size_t	len;
    213 	int i;
    214 
    215 	len = strlen(arg);
    216 	for (i = 0; encs[i].ename; i++)
    217 		if (strncmp(encs[i].ename, arg, len) == 0) {
    218 			*encp = encs[i].eno;
    219 			return;
    220 		}
    221 	errx(1, "unknown encoding `%s'", arg);
    222 }
    223 
    224 const char *const audio_errlist[] = {
    225 	"error zero",				/* nothing? */
    226 	"no audio entry",			/* AUDIO_ENOENT */
    227 	"short header",				/* AUDIO_ESHORTHDR */
    228 	"unsupported WAV format",		/* AUDIO_EWAVUNSUPP */
    229 	"bad (unsupported) WAV PCM format",	/* AUDIO_EWAVBADPCM */
    230 	"no WAV audio data",			/* AUDIO_EWAVNODATA */
    231 	"internal error",			/* AUDIO_EINTERNAL */
    232 };
    233 
    234 const char *
    235 audio_errstring(errval)
    236 	int	errval;
    237 {
    238 
    239 	errval = -errval;
    240 	if (errval < 1 || errval > AUDIO_MAXERRNO)
    241 		return "Invalid error";
    242 	return audio_errlist[errval];
    243 }
    244