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