Home | History | Annotate | Line # | Download | only in common
wav.c revision 1.7
      1  1.7  mrg /*	$NetBSD: wav.c,v 1.7 2004/01/21 11:55:07 mrg Exp $	*/
      2  1.3  mrg 
      3  1.1  mrg /*
      4  1.1  mrg  * Copyright (c) 2002 Matthew R. Green
      5  1.1  mrg  * All rights reserved.
      6  1.1  mrg  *
      7  1.1  mrg  * Redistribution and use in source and binary forms, with or without
      8  1.1  mrg  * modification, are permitted provided that the following conditions
      9  1.1  mrg  * are met:
     10  1.1  mrg  * 1. Redistributions of source code must retain the above copyright
     11  1.1  mrg  *    notice, this list of conditions and the following disclaimer.
     12  1.1  mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  mrg  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  mrg  *    documentation and/or other materials provided with the distribution.
     15  1.1  mrg  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  mrg  *    derived from this software without specific prior written permission.
     17  1.1  mrg  *
     18  1.1  mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.1  mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.1  mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.1  mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.1  mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  mrg  * SUCH DAMAGE.
     29  1.1  mrg  */
     30  1.1  mrg 
     31  1.1  mrg /*
     32  1.1  mrg  * WAV support for the audio tools; thanks go to the sox utility for
     33  1.1  mrg  * clearing up issues with WAV files.
     34  1.1  mrg  */
     35  1.6  agc #include <sys/cdefs.h>
     36  1.6  agc 
     37  1.6  agc #ifndef lint
     38  1.7  mrg __RCSID("$NetBSD: wav.c,v 1.7 2004/01/21 11:55:07 mrg Exp $");
     39  1.6  agc #endif
     40  1.6  agc 
     41  1.1  mrg 
     42  1.1  mrg #include <sys/types.h>
     43  1.1  mrg #include <sys/audioio.h>
     44  1.1  mrg #include <sys/ioctl.h>
     45  1.1  mrg #include <sys/time.h>
     46  1.1  mrg 
     47  1.1  mrg #include <ctype.h>
     48  1.1  mrg #include <err.h>
     49  1.1  mrg #include <stdio.h>
     50  1.1  mrg #include <stdlib.h>
     51  1.1  mrg #include <string.h>
     52  1.1  mrg 
     53  1.1  mrg #include "libaudio.h"
     54  1.1  mrg 
     55  1.1  mrg struct {
     56  1.1  mrg 	int	wenc;
     57  1.2  mrg 	const char *wname;
     58  1.1  mrg } wavencs[] = {
     59  1.1  mrg 	{ WAVE_FORMAT_UNKNOWN, 	"Microsoft Official Unknown" },
     60  1.1  mrg 	{ WAVE_FORMAT_PCM,	"Microsoft PCM" },
     61  1.1  mrg 	{ WAVE_FORMAT_ADPCM,	"Microsoft ADPCM" },
     62  1.1  mrg 	{ WAVE_FORMAT_ALAW,	"Microsoft A-law" },
     63  1.5  wiz 	{ WAVE_FORMAT_MULAW,	"Microsoft mu-law" },
     64  1.1  mrg 	{ WAVE_FORMAT_OKI_ADPCM,"OKI ADPCM" },
     65  1.1  mrg 	{ WAVE_FORMAT_DIGISTD,	"Digistd format" },
     66  1.1  mrg 	{ WAVE_FORMAT_DIGIFIX,	"Digifix format" },
     67  1.1  mrg 	{ -1, 			"?Unknown?" },
     68  1.1  mrg };
     69  1.1  mrg 
     70  1.2  mrg const char *
     71  1.2  mrg wav_enc_from_val(int encoding)
     72  1.1  mrg {
     73  1.1  mrg 	int	i;
     74  1.1  mrg 
     75  1.1  mrg 	for (i = 0; wavencs[i].wenc != -1; i++)
     76  1.1  mrg 		if (wavencs[i].wenc == encoding)
     77  1.1  mrg 			break;
     78  1.1  mrg 	return (wavencs[i].wname);
     79  1.1  mrg }
     80  1.1  mrg 
     81  1.1  mrg /*
     82  1.1  mrg  * sample header is:
     83  1.1  mrg  *
     84  1.1  mrg  *   RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
     85  1.1  mrg  *
     86  1.1  mrg  */
     87  1.1  mrg /*
     88  1.1  mrg  * WAV format helpers
     89  1.1  mrg  */
     90  1.1  mrg /*
     91  1.1  mrg  * find a .wav header, etc. returns header length on success
     92  1.1  mrg  */
     93  1.1  mrg ssize_t
     94  1.1  mrg audio_wav_parse_hdr(hdr, sz, enc, prec, sample, channels, datasize)
     95  1.1  mrg 	void	*hdr;
     96  1.1  mrg 	size_t	sz;
     97  1.7  mrg 	u_int	*enc;
     98  1.7  mrg 	u_int	*prec;
     99  1.7  mrg 	u_int	*sample;
    100  1.7  mrg 	u_int	*channels;
    101  1.1  mrg 	size_t *datasize;
    102  1.1  mrg {
    103  1.1  mrg 	char	*where = hdr, *owhere;
    104  1.1  mrg 	wav_audioheaderpart part;
    105  1.1  mrg 	wav_audioheaderfmt fmt;
    106  1.1  mrg 	char	*end = (((char *)hdr) + sz);
    107  1.7  mrg 	u_int	newenc, newprec;
    108  1.2  mrg 	static const char
    109  1.2  mrg 	    strfmt[4] = "fmt ",
    110  1.2  mrg 	    strRIFF[4] = "RIFF",
    111  1.2  mrg 	    strWAVE[4] = "WAVE",
    112  1.2  mrg 	    strdata[4] = "data";
    113  1.2  mrg 
    114  1.1  mrg 	if (sz < 32)
    115  1.1  mrg 		return (AUDIO_ENOENT);
    116  1.1  mrg 
    117  1.2  mrg 	if (strncmp(where, strRIFF, sizeof strRIFF))
    118  1.1  mrg 		return (AUDIO_ENOENT);
    119  1.1  mrg 	where += 8;
    120  1.2  mrg 	if (strncmp(where, strWAVE, sizeof strWAVE))
    121  1.1  mrg 		return (AUDIO_ENOENT);
    122  1.1  mrg 	where += 4;
    123  1.1  mrg 
    124  1.1  mrg 	do {
    125  1.1  mrg 		memcpy(&part, where, sizeof part);
    126  1.1  mrg 		owhere = where;
    127  1.1  mrg 		where += getle32(part.len) + 8;
    128  1.2  mrg 	} while (where < end && strncmp(part.name, strfmt, sizeof strfmt));
    129  1.1  mrg 
    130  1.1  mrg 	/* too short ? */
    131  1.1  mrg 	if (where + sizeof fmt > end)
    132  1.1  mrg 		return (AUDIO_ESHORTHDR);
    133  1.1  mrg 
    134  1.1  mrg 	memcpy(&fmt, (owhere + 8), sizeof fmt);
    135  1.1  mrg 
    136  1.1  mrg 	switch (getle16(fmt.tag)) {
    137  1.1  mrg 	case WAVE_FORMAT_UNKNOWN:
    138  1.1  mrg 	case WAVE_FORMAT_ADPCM:
    139  1.1  mrg 	case WAVE_FORMAT_OKI_ADPCM:
    140  1.1  mrg 	case WAVE_FORMAT_DIGISTD:
    141  1.1  mrg 	case WAVE_FORMAT_DIGIFIX:
    142  1.1  mrg 	case IBM_FORMAT_MULAW:
    143  1.1  mrg 	case IBM_FORMAT_ALAW:
    144  1.1  mrg 	case IBM_FORMAT_ADPCM:
    145  1.1  mrg 	default:
    146  1.1  mrg 		return (AUDIO_EWAVUNSUPP);
    147  1.1  mrg 
    148  1.1  mrg 	case WAVE_FORMAT_PCM:
    149  1.1  mrg 		switch (getle16(fmt.bits_per_sample)) {
    150  1.1  mrg 		case 8:
    151  1.1  mrg 			newprec = 8;
    152  1.1  mrg 			break;
    153  1.1  mrg 		case 16:
    154  1.1  mrg 			newprec = 16;
    155  1.1  mrg 			break;
    156  1.1  mrg 		case 24:
    157  1.1  mrg 			newprec = 24;
    158  1.1  mrg 			break;
    159  1.1  mrg 		case 32:
    160  1.1  mrg 			newprec = 32;
    161  1.1  mrg 			break;
    162  1.1  mrg 		default:
    163  1.1  mrg 			return (AUDIO_EWAVBADPCM);
    164  1.1  mrg 		}
    165  1.1  mrg 		if (newprec == 8)
    166  1.1  mrg 			newenc = AUDIO_ENCODING_ULINEAR_LE;
    167  1.1  mrg 		else
    168  1.1  mrg 			newenc = AUDIO_ENCODING_SLINEAR_LE;
    169  1.1  mrg 		break;
    170  1.1  mrg 	case WAVE_FORMAT_ALAW:
    171  1.1  mrg 		newenc = AUDIO_ENCODING_ALAW;
    172  1.1  mrg 		newprec = 8;
    173  1.1  mrg 		break;
    174  1.1  mrg 	case WAVE_FORMAT_MULAW:
    175  1.1  mrg 		newenc = AUDIO_ENCODING_ULAW;
    176  1.1  mrg 		newprec = 8;
    177  1.1  mrg 		break;
    178  1.1  mrg 	}
    179  1.1  mrg 
    180  1.1  mrg 	do {
    181  1.1  mrg 		memcpy(&part, where, sizeof part);
    182  1.1  mrg 		owhere = where;
    183  1.1  mrg 		where += (getle32(part.len) + 8);
    184  1.2  mrg 	} while (where < end && strncmp(part.name, strdata, sizeof strdata));
    185  1.1  mrg 
    186  1.1  mrg 	if ((where - getle32(part.len)) <= end) {
    187  1.1  mrg 		if (channels)
    188  1.7  mrg 			*channels = (u_int)getle16(fmt.channels);
    189  1.1  mrg 		if (sample)
    190  1.1  mrg 			*sample = getle32(fmt.sample_rate);
    191  1.1  mrg 		if (enc)
    192  1.1  mrg 			*enc = newenc;
    193  1.1  mrg 		if (prec)
    194  1.1  mrg 			*prec = newprec;
    195  1.1  mrg 		if (datasize)
    196  1.1  mrg 			*datasize = (size_t)getle32(part.len);
    197  1.1  mrg 		return (owhere - (char *)hdr + 8);
    198  1.1  mrg 	}
    199  1.1  mrg 	return (AUDIO_EWAVNODATA);
    200  1.1  mrg }
    201