Home | History | Annotate | Line # | Download | only in common
audio.c revision 1.5
      1 /*	$NetBSD: audio.c,v 1.5 1999/03/29 04:49:49 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 #include <sys/types.h>
     32 #include <sys/audioio.h>
     33 #include <sys/ioctl.h>
     34 #include <sys/time.h>
     35 
     36 #include <ctype.h>
     37 #include <err.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 
     42 #include "libaudio.h"
     43 
     44 /* back and forth between encodings */
     45 struct {
     46 	char *ename;
     47 	int eno;
     48 } encs[] = {
     49 	{ AudioEmulaw,		AUDIO_ENCODING_ULAW },
     50 	{ "ulaw",		AUDIO_ENCODING_ULAW },
     51 	{ AudioEalaw, 		AUDIO_ENCODING_ALAW },
     52 	{ AudioEslinear,	AUDIO_ENCODING_SLINEAR },
     53 	{ "linear",		AUDIO_ENCODING_SLINEAR },
     54 	{ AudioEulinear,	AUDIO_ENCODING_ULINEAR },
     55 	{ AudioEadpcm,		AUDIO_ENCODING_ADPCM },
     56 	{ "ADPCM",		AUDIO_ENCODING_ADPCM },
     57 	{ AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE },
     58 	{ "linear_le",		AUDIO_ENCODING_SLINEAR_LE },
     59 	{ AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE },
     60 	{ AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE },
     61 	{ "linear_be",		AUDIO_ENCODING_SLINEAR_BE },
     62 	{ AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE },
     63 	{ AudioEmpeg_l1_stream,	AUDIO_ENCODING_MPEG_L1_STREAM },
     64 	{ AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
     65 	{ AudioEmpeg_l1_system,	AUDIO_ENCODING_MPEG_L1_SYSTEM },
     66 	{ AudioEmpeg_l2_stream,	AUDIO_ENCODING_MPEG_L2_STREAM },
     67 	{ AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
     68 	{ AudioEmpeg_l2_system,	AUDIO_ENCODING_MPEG_L2_SYSTEM },
     69 	{ 0, -1 }
     70 };
     71 
     72 
     73 char *
     74 audio_enc_from_val(val)
     75 	int	val;
     76 {
     77 	int	i;
     78 
     79 	for (i = 0; encs[i].ename; i++)
     80 		if (encs[i].eno == val)
     81 			break;
     82 	return (encs[i].ename);
     83 }
     84 
     85 int
     86 audio_enc_to_val(enc)
     87 	const	char *enc;
     88 {
     89 	int	i;
     90 
     91 	for (i = 0; encs[i].ename; i++)
     92 		if (strcmp(encs[i].ename, enc) == 0)
     93 			break;
     94 	if (encs[i].ename)
     95 		return (encs[i].eno);
     96 	else
     97 		return (-1);
     98 }
     99 
    100 /*
    101  * SunOS/NeXT .au format helpers
    102  */
    103 struct {
    104 	int	file_encoding;
    105 	int	encoding;
    106 	int	precision;
    107 } file2sw_encodings[] = {
    108 	{ AUDIO_FILE_ENCODING_MULAW_8,		AUDIO_ENCODING_ULAW,	8 },
    109 	{ AUDIO_FILE_ENCODING_LINEAR_8,		AUDIO_ENCODING_ULINEAR_BE, 8 },
    110 	{ AUDIO_FILE_ENCODING_LINEAR_16,	AUDIO_ENCODING_ULINEAR_BE, 16 },
    111 	{ AUDIO_FILE_ENCODING_LINEAR_24,	AUDIO_ENCODING_ULINEAR_BE, 24 },
    112 	{ AUDIO_FILE_ENCODING_LINEAR_32,	AUDIO_ENCODING_ULINEAR_BE, 32 },
    113 #if 0
    114 	{ AUDIO_FILE_ENCODING_FLOAT,		AUDIO_ENCODING_ULAW,	32 },
    115 	{ AUDIO_FILE_ENCODING_DOUBLE,		AUDIO_ENCODING_ULAW,	64 },
    116 	{ AUDIO_FILE_ENCODING_ADPCM_G721,	AUDIO_ENCODING_ULAW,	4 },
    117 	{ AUDIO_FILE_ENCODING_ADPCM_G722,	AUDIO_ENCODING_ULAW,	0 },
    118 	{ AUDIO_FILE_ENCODING_ADPCM_G723_3,	AUDIO_ENCODING_ULAW,	3 },
    119 	{ AUDIO_FILE_ENCODING_ADPCM_G723_5,	AUDIO_ENCODING_ULAW,	5 },
    120 #endif
    121 	{ AUDIO_FILE_ENCODING_ALAW_8,		AUDIO_ENCODING_ALAW,	8 },
    122 	{ -1, -1 }
    123 };
    124 
    125 int
    126 audio_get_sun_encoding(sun_encoding, encp, precp)
    127 	int	sun_encoding;
    128 	int	*encp;
    129 	int	*precp;
    130 {
    131 	int i;
    132 
    133 	for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
    134 		if (file2sw_encodings[i].file_encoding == sun_encoding) {
    135 			*precp = file2sw_encodings[i].precision;
    136 			*encp = file2sw_encodings[i].encoding;
    137 			return (0);
    138 		}
    139 	return (1);
    140 }
    141 
    142 /*
    143  * sample header is:
    144  *
    145  *   RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
    146  *
    147  */
    148 /*
    149  * WAV format helpers
    150  */
    151 /*
    152  * find a .wav header, etc. returns header length on success
    153  */
    154 size_t
    155 audio_parse_wav_hdr(hdr, sz, enc, prec, sample, channels)
    156 	void	*hdr;
    157 	size_t	sz;
    158 	int	*enc;
    159 	int	*prec;
    160 	int	*sample;
    161 	int	*channels;
    162 {
    163 	char	*where = hdr;
    164 	wav_audioheaderpart *part;
    165 	wav_audioheaderfmt *fmt;
    166 	char	*end = (((char *)hdr) + sz);
    167 	int	newenc, newprec;
    168 
    169 	if (sz < 32)
    170 		return (AUDIO_ENOENT);
    171 
    172 	if (strncmp(where, "RIFF", 4))
    173 		return (AUDIO_ENOENT);
    174 	where += 8;
    175 	if (strncmp(where,  "WAVE", 4))
    176 		return (AUDIO_ENOENT);
    177 	where += 4;
    178 
    179 	do {
    180 		part = (wav_audioheaderpart *)where;
    181 		where += getle32(part->len) + 8;
    182 	} while (where < end && strncmp(part->name, "fmt ", 4));
    183 
    184 	/* too short ? */
    185 	if (where + 16 > end)
    186 		return (AUDIO_ESHORTHDR);
    187 
    188 	fmt = (wav_audioheaderfmt *)(part + 1);
    189 
    190 #if 0
    191 printf("fmt header is:\n\t%d\ttag\n\t%d\tchannels\n\t%d\tsample rate\n\t%d\tavg_bps\n\t%d\talignment\n\t%d\tbits per sample\n", getle16(fmt->tag), getle16(fmt->channels), getle32(fmt->sample_rate), getle32(fmt->avg_bps), getle16(fmt->alignment), getle16(fmt->bits_per_sample));
    192 #endif
    193 
    194 	switch (getle16(fmt->tag)) {
    195 	case WAVE_FORMAT_UNKNOWN:
    196 	case WAVE_FORMAT_ADPCM:
    197 	case WAVE_FORMAT_OKI_ADPCM:
    198 	case WAVE_FORMAT_DIGISTD:
    199 	case WAVE_FORMAT_DIGIFIX:
    200 	case IBM_FORMAT_MULAW:
    201 	case IBM_FORMAT_ALAW:
    202 	case IBM_FORMAT_ADPCM:
    203 	default:
    204 		return (AUDIO_EWAVUNSUPP);
    205 
    206 	case WAVE_FORMAT_PCM:
    207 		switch (getle16(fmt->bits_per_sample)) {
    208 		case 8:
    209 			newprec = 8;
    210 			break;
    211 		case 16:
    212 			newprec = 16;
    213 			break;
    214 		case 24:
    215 			newprec = 24;
    216 			break;
    217 		case 32:
    218 			newprec = 32;
    219 			break;
    220 		default:
    221 			return (AUDIO_EWAVBADPCM);
    222 		}
    223 		newenc = AUDIO_ENCODING_ULINEAR_LE;
    224 		break;
    225 	case WAVE_FORMAT_ALAW:
    226 		newenc = AUDIO_ENCODING_ALAW;
    227 		newprec = 8;
    228 		break;
    229 	case WAVE_FORMAT_MULAW:
    230 		newenc = AUDIO_ENCODING_ULAW;
    231 		newprec = 8;
    232 		break;
    233 	}
    234 
    235 	do {
    236 		part = (wav_audioheaderpart *)where;
    237 #if 0
    238 printf("part `%c%c%c%c' len = %d\n", part->name[0], part->name[1], part->name[2], part->name[3], getle32(part->len));
    239 #endif
    240 		where += (getle32(part->len) + 8);
    241 	} while ((char *)where < end && strncmp(part->name, "data", 4));
    242 
    243 	if ((where - getle32(part->len)) <= end) {
    244 		*channels = getle16(fmt->channels);
    245 		*sample = getle32(fmt->sample_rate);
    246 		*enc = newenc;
    247 		*prec = newprec;
    248 		part++;
    249 		return ((char *)part - (char *)hdr);
    250 	}
    251 	return (AUDIO_EWAVNODATA);
    252 }
    253 
    254 /*
    255  * these belong elsewhere??
    256  */
    257 void
    258 decode_int(arg, intp)
    259 	const char *arg;
    260 	int *intp;
    261 {
    262 	char	*ep;
    263 	int	ret;
    264 
    265 	ret = strtoul(arg, &ep, 0);
    266 
    267 	if (ep[0] == '\0') {
    268 		*intp = ret;
    269 		return;
    270 	}
    271 	errx(1, "argument `%s' not a valid integer", arg);
    272 }
    273 
    274 #include <stdio.h>
    275 void
    276 decode_time(arg, tvp)
    277 	const char *arg;
    278 	struct timeval *tvp;
    279 {
    280 	char	*s, *colon, *dot;
    281 	char	*copy = strdup(arg);
    282 	int	first;
    283 
    284 	if (copy == NULL)
    285 		err(1, "could not allocate a copy of %s", arg);
    286 
    287 	tvp->tv_sec = tvp->tv_usec = 0;
    288 	s = copy;
    289 
    290 	/* handle [hh:]mm:ss.dd */
    291 	if ((colon = strchr(s, ':'))) {
    292 		*colon++ = '\0';
    293 		decode_int(s, &first);
    294 		tvp->tv_sec = first * 60;	/* minutes */
    295 		s = colon;
    296 
    297 		if ((colon = strchr(s, ':'))) {
    298 			*colon++ = '\0';
    299 			decode_int(s, &first);
    300 			tvp->tv_sec += first;
    301 			tvp->tv_sec *= 60;	/* minutes and hours */
    302 			s = colon;
    303 		}
    304 	}
    305 	if ((dot = strchr(s, '.'))) {
    306 		int 	i, base = 100000;
    307 
    308 		*dot++ = '\0';
    309 
    310 		for (i = 0; i < 6; i++, base /= 10) {
    311 			if (!dot[i])
    312 				break;
    313 			if (!isdigit(dot[i]))
    314 				errx(1, "argument `%s' is not a value time specification", arg);
    315 			tvp->tv_usec += base * (dot[i] - '0');
    316 		}
    317 	}
    318 	decode_int(s, &first);
    319 	tvp->tv_sec += first;
    320 #if 0
    321 printf("tvp->tv_sec = %ld, tvp->tv_usec = %ld\n", tvp->tv_sec, tvp->tv_usec);
    322 #endif
    323 
    324 	free(copy);
    325 }
    326 
    327 /*
    328  * decode a string into an encoding value.
    329  */
    330 void
    331 decode_encoding(arg, encp)
    332 	const char *arg;
    333 	int *encp;
    334 {
    335 	size_t	len;
    336 	int i;
    337 
    338 	len = strlen(arg);
    339 	for (i = 0; encs[i].ename; i++)
    340 		if (strncmp(encs[i].ename, arg, len) == 0) {
    341 			*encp = encs[i].eno;
    342 			return;
    343 		}
    344 	errx(1, "unknown encoding `%s'", arg);
    345 }
    346 
    347 const char *const audio_errlist[] = {
    348 	"no audio entry",
    349 	"short header",
    350 	"unsupported WAV format",
    351 	"bad (unsupported) WAV PCM format",
    352 	"no WAV audio data",
    353 };
    354 
    355 const char *
    356 audio_errstring(errval)
    357 	int	errval;
    358 {
    359 
    360 	errval = -errval;
    361 	if (errval < 1 || errval > AUDIO_MAXERRNO)
    362 		return "Invalid error";
    363 	return audio_errlist[errval];
    364 }
    365