Home | History | Annotate | Line # | Download | only in common
wav.c revision 1.10.8.1
      1  1.10.8.1    tls /*	$NetBSD: wav.c,v 1.10.8.1 2014/08/20 00:04:56 tls Exp $	*/
      2       1.3    mrg 
      3       1.1    mrg /*
      4       1.9    mrg  * Copyright (c) 2002, 2009 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.10.8.1    tls __RCSID("$NetBSD: wav.c,v 1.10.8.1 2014/08/20 00:04:56 tls 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.9    mrg #include <stdint.h>
     51  1.10.8.1    tls #include <unistd.h>
     52       1.1    mrg 
     53       1.1    mrg #include "libaudio.h"
     54  1.10.8.1    tls #include "auconv.h"
     55       1.1    mrg 
     56      1.10  joerg static const struct {
     57       1.1    mrg 	int	wenc;
     58       1.2    mrg 	const char *wname;
     59       1.1    mrg } wavencs[] = {
     60       1.1    mrg 	{ WAVE_FORMAT_UNKNOWN, 	"Microsoft Official Unknown" },
     61       1.1    mrg 	{ WAVE_FORMAT_PCM,	"Microsoft PCM" },
     62       1.1    mrg 	{ WAVE_FORMAT_ADPCM,	"Microsoft ADPCM" },
     63       1.1    mrg 	{ WAVE_FORMAT_ALAW,	"Microsoft A-law" },
     64       1.5    wiz 	{ WAVE_FORMAT_MULAW,	"Microsoft mu-law" },
     65       1.1    mrg 	{ WAVE_FORMAT_OKI_ADPCM,"OKI ADPCM" },
     66       1.1    mrg 	{ WAVE_FORMAT_DIGISTD,	"Digistd format" },
     67       1.1    mrg 	{ WAVE_FORMAT_DIGIFIX,	"Digifix format" },
     68       1.1    mrg 	{ -1, 			"?Unknown?" },
     69       1.1    mrg };
     70       1.1    mrg 
     71       1.2    mrg const char *
     72       1.2    mrg wav_enc_from_val(int encoding)
     73       1.1    mrg {
     74       1.1    mrg 	int	i;
     75       1.1    mrg 
     76       1.1    mrg 	for (i = 0; wavencs[i].wenc != -1; i++)
     77       1.1    mrg 		if (wavencs[i].wenc == encoding)
     78       1.1    mrg 			break;
     79       1.1    mrg 	return (wavencs[i].wname);
     80       1.1    mrg }
     81       1.1    mrg 
     82       1.1    mrg /*
     83       1.1    mrg  * sample header is:
     84       1.1    mrg  *
     85       1.1    mrg  *   RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
     86       1.1    mrg  *
     87       1.1    mrg  */
     88       1.1    mrg /*
     89       1.1    mrg  * WAV format helpers
     90       1.1    mrg  */
     91       1.1    mrg /*
     92       1.1    mrg  * find a .wav header, etc. returns header length on success
     93       1.1    mrg  */
     94       1.1    mrg ssize_t
     95      1.10  joerg audio_wav_parse_hdr(void *hdr, size_t sz, u_int *enc, u_int *prec,
     96      1.10  joerg     u_int *sample, u_int *channels, size_t *datasize)
     97       1.1    mrg {
     98       1.1    mrg 	char	*where = hdr, *owhere;
     99       1.1    mrg 	wav_audioheaderpart part;
    100       1.1    mrg 	wav_audioheaderfmt fmt;
    101       1.9    mrg 	wav_audiohdrextensible ext;
    102       1.1    mrg 	char	*end = (((char *)hdr) + sz);
    103       1.7    mrg 	u_int	newenc, newprec;
    104       1.9    mrg 	u_int16_t fmttag;
    105       1.2    mrg 	static const char
    106       1.2    mrg 	    strfmt[4] = "fmt ",
    107       1.2    mrg 	    strRIFF[4] = "RIFF",
    108       1.2    mrg 	    strWAVE[4] = "WAVE",
    109       1.2    mrg 	    strdata[4] = "data";
    110       1.2    mrg 
    111       1.1    mrg 	if (sz < 32)
    112       1.1    mrg 		return (AUDIO_ENOENT);
    113       1.1    mrg 
    114       1.2    mrg 	if (strncmp(where, strRIFF, sizeof strRIFF))
    115       1.1    mrg 		return (AUDIO_ENOENT);
    116       1.1    mrg 	where += 8;
    117       1.2    mrg 	if (strncmp(where, strWAVE, sizeof strWAVE))
    118       1.1    mrg 		return (AUDIO_ENOENT);
    119       1.1    mrg 	where += 4;
    120       1.1    mrg 
    121       1.1    mrg 	do {
    122       1.1    mrg 		memcpy(&part, where, sizeof part);
    123       1.1    mrg 		owhere = where;
    124       1.1    mrg 		where += getle32(part.len) + 8;
    125       1.2    mrg 	} while (where < end && strncmp(part.name, strfmt, sizeof strfmt));
    126       1.1    mrg 
    127       1.1    mrg 	/* too short ? */
    128       1.1    mrg 	if (where + sizeof fmt > end)
    129       1.1    mrg 		return (AUDIO_ESHORTHDR);
    130       1.1    mrg 
    131       1.1    mrg 	memcpy(&fmt, (owhere + 8), sizeof fmt);
    132       1.1    mrg 
    133       1.9    mrg 	fmttag = getle16(fmt.tag);
    134       1.9    mrg 	if (verbose)
    135       1.9    mrg 		printf("WAVE format tag: %x\n", fmttag);
    136       1.9    mrg 
    137       1.9    mrg 	if (fmttag == WAVE_FORMAT_EXTENSIBLE) {
    138       1.9    mrg 		if ((uintptr_t)(where - owhere) < sizeof(fmt) + sizeof(ext))
    139       1.9    mrg 			return (AUDIO_ESHORTHDR);
    140       1.9    mrg 		memcpy(&ext, owhere + sizeof fmt, sizeof ext);
    141       1.9    mrg 		if (getle16(ext.len) < sizeof(ext) - sizeof(ext.len))
    142       1.9    mrg 			return (AUDIO_ESHORTHDR);
    143       1.9    mrg 		fmttag = ext.sub_tag;
    144       1.9    mrg 		if (verbose)
    145       1.9    mrg 			printf("WAVE extensible sub tag: %x\n", fmttag);
    146       1.9    mrg 	}
    147       1.9    mrg 
    148       1.9    mrg 	switch (fmttag) {
    149       1.1    mrg 	case WAVE_FORMAT_UNKNOWN:
    150       1.1    mrg 	case IBM_FORMAT_MULAW:
    151       1.1    mrg 	case IBM_FORMAT_ALAW:
    152       1.1    mrg 	case IBM_FORMAT_ADPCM:
    153       1.1    mrg 	default:
    154       1.1    mrg 		return (AUDIO_EWAVUNSUPP);
    155       1.1    mrg 
    156       1.1    mrg 	case WAVE_FORMAT_PCM:
    157       1.9    mrg 	case WAVE_FORMAT_ADPCM:
    158       1.9    mrg 	case WAVE_FORMAT_OKI_ADPCM:
    159       1.9    mrg 	case WAVE_FORMAT_IMA_ADPCM:
    160       1.9    mrg 	case WAVE_FORMAT_DIGIFIX:
    161       1.9    mrg 	case WAVE_FORMAT_DIGISTD:
    162       1.1    mrg 		switch (getle16(fmt.bits_per_sample)) {
    163       1.1    mrg 		case 8:
    164       1.1    mrg 			newprec = 8;
    165       1.1    mrg 			break;
    166       1.1    mrg 		case 16:
    167       1.1    mrg 			newprec = 16;
    168       1.1    mrg 			break;
    169       1.1    mrg 		case 24:
    170       1.1    mrg 			newprec = 24;
    171       1.1    mrg 			break;
    172       1.1    mrg 		case 32:
    173       1.1    mrg 			newprec = 32;
    174       1.1    mrg 			break;
    175       1.1    mrg 		default:
    176       1.1    mrg 			return (AUDIO_EWAVBADPCM);
    177       1.1    mrg 		}
    178       1.1    mrg 		if (newprec == 8)
    179       1.1    mrg 			newenc = AUDIO_ENCODING_ULINEAR_LE;
    180       1.1    mrg 		else
    181       1.1    mrg 			newenc = AUDIO_ENCODING_SLINEAR_LE;
    182       1.1    mrg 		break;
    183       1.1    mrg 	case WAVE_FORMAT_ALAW:
    184       1.1    mrg 		newenc = AUDIO_ENCODING_ALAW;
    185       1.1    mrg 		newprec = 8;
    186       1.1    mrg 		break;
    187       1.1    mrg 	case WAVE_FORMAT_MULAW:
    188       1.1    mrg 		newenc = AUDIO_ENCODING_ULAW;
    189       1.1    mrg 		newprec = 8;
    190       1.1    mrg 		break;
    191       1.1    mrg 	}
    192       1.1    mrg 
    193       1.1    mrg 	do {
    194       1.1    mrg 		memcpy(&part, where, sizeof part);
    195       1.1    mrg 		owhere = where;
    196       1.1    mrg 		where += (getle32(part.len) + 8);
    197       1.2    mrg 	} while (where < end && strncmp(part.name, strdata, sizeof strdata));
    198       1.1    mrg 
    199       1.1    mrg 	if ((where - getle32(part.len)) <= end) {
    200       1.1    mrg 		if (channels)
    201       1.7    mrg 			*channels = (u_int)getle16(fmt.channels);
    202       1.1    mrg 		if (sample)
    203       1.1    mrg 			*sample = getle32(fmt.sample_rate);
    204       1.1    mrg 		if (enc)
    205       1.1    mrg 			*enc = newenc;
    206       1.1    mrg 		if (prec)
    207       1.1    mrg 			*prec = newprec;
    208       1.1    mrg 		if (datasize)
    209       1.1    mrg 			*datasize = (size_t)getle32(part.len);
    210       1.1    mrg 		return (owhere - (char *)hdr + 8);
    211       1.1    mrg 	}
    212       1.1    mrg 	return (AUDIO_EWAVNODATA);
    213       1.1    mrg }
    214  1.10.8.1    tls 
    215  1.10.8.1    tls 
    216  1.10.8.1    tls /*
    217  1.10.8.1    tls  * prepare a WAV header for writing; we fill in hdrp, lenp and leftp,
    218  1.10.8.1    tls  * and expect our caller (wav_write_header()) to use them.
    219  1.10.8.1    tls  */
    220  1.10.8.1    tls int
    221  1.10.8.1    tls wav_prepare_header(struct write_info *wi, void **hdrp, size_t *lenp, int *leftp)
    222  1.10.8.1    tls {
    223  1.10.8.1    tls 	/*
    224  1.10.8.1    tls 	 * WAV header we write looks like this:
    225  1.10.8.1    tls 	 *
    226  1.10.8.1    tls 	 *      bytes   purpose
    227  1.10.8.1    tls 	 *      0-3     "RIFF"
    228  1.10.8.1    tls 	 *      4-7     file length (minus 8)
    229  1.10.8.1    tls 	 *      8-15    "WAVEfmt "
    230  1.10.8.1    tls 	 *      16-19   format size
    231  1.10.8.1    tls 	 *      20-21   format tag
    232  1.10.8.1    tls 	 *      22-23   number of channels
    233  1.10.8.1    tls 	 *      24-27   sample rate
    234  1.10.8.1    tls 	 *      28-31   average bytes per second
    235  1.10.8.1    tls 	 *      32-33   block alignment
    236  1.10.8.1    tls 	 *      34-35   bits per sample
    237  1.10.8.1    tls 	 *
    238  1.10.8.1    tls 	 * then for ULAW and ALAW outputs, we have an extended chunk size
    239  1.10.8.1    tls 	 * and a WAV "fact" to add:
    240  1.10.8.1    tls 	 *
    241  1.10.8.1    tls 	 *      36-37   length of extension (== 0)
    242  1.10.8.1    tls 	 *      38-41   "fact"
    243  1.10.8.1    tls 	 *      42-45   fact size
    244  1.10.8.1    tls 	 *      46-49   number of samples written
    245  1.10.8.1    tls 	 *      50-53   "data"
    246  1.10.8.1    tls 	 *      54-57   data length
    247  1.10.8.1    tls 	 *      58-     raw audio data
    248  1.10.8.1    tls 	 *
    249  1.10.8.1    tls 	 * for PCM outputs we have just the data remaining:
    250  1.10.8.1    tls 	 *
    251  1.10.8.1    tls 	 *      36-39   "data"
    252  1.10.8.1    tls 	 *      40-43   data length
    253  1.10.8.1    tls 	 *      44-     raw audio data
    254  1.10.8.1    tls 	 *
    255  1.10.8.1    tls 	 *	RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
    256  1.10.8.1    tls 	 */
    257  1.10.8.1    tls 	static char wavheaderbuf[64];
    258  1.10.8.1    tls 	char	*p = wavheaderbuf;
    259  1.10.8.1    tls 	const char *riff = "RIFF",
    260  1.10.8.1    tls 	    *wavefmt = "WAVEfmt ",
    261  1.10.8.1    tls 	    *fact = "fact",
    262  1.10.8.1    tls 	    *data = "data";
    263  1.10.8.1    tls 	u_int32_t filelen, fmtsz, sps, abps, factsz = 4, nsample, datalen;
    264  1.10.8.1    tls 	u_int16_t fmttag, nchan, align, extln = 0;
    265  1.10.8.1    tls 
    266  1.10.8.1    tls 	if (wi->header_info)
    267  1.10.8.1    tls 		warnx("header information not supported for WAV");
    268  1.10.8.1    tls 	*leftp = 0;
    269  1.10.8.1    tls 
    270  1.10.8.1    tls 	switch (wi->precision) {
    271  1.10.8.1    tls 	case 8:
    272  1.10.8.1    tls 		break;
    273  1.10.8.1    tls 	case 16:
    274  1.10.8.1    tls 		break;
    275  1.10.8.1    tls 	case 32:
    276  1.10.8.1    tls 		break;
    277  1.10.8.1    tls 	default:
    278  1.10.8.1    tls 		{
    279  1.10.8.1    tls 			static int warned = 0;
    280  1.10.8.1    tls 
    281  1.10.8.1    tls 			if (warned == 0) {
    282  1.10.8.1    tls 				warnx("can not support precision of %d", wi->precision);
    283  1.10.8.1    tls 				warned = 1;
    284  1.10.8.1    tls 			}
    285  1.10.8.1    tls 		}
    286  1.10.8.1    tls 		return (-1);
    287  1.10.8.1    tls 	}
    288  1.10.8.1    tls 
    289  1.10.8.1    tls 	switch (wi->encoding) {
    290  1.10.8.1    tls 	case AUDIO_ENCODING_ULAW:
    291  1.10.8.1    tls 		fmttag = WAVE_FORMAT_MULAW;
    292  1.10.8.1    tls 		fmtsz = 18;
    293  1.10.8.1    tls 		align = wi->channels;
    294  1.10.8.1    tls 		break;
    295  1.10.8.1    tls 
    296  1.10.8.1    tls 	case AUDIO_ENCODING_ALAW:
    297  1.10.8.1    tls 		fmttag = WAVE_FORMAT_ALAW;
    298  1.10.8.1    tls 		fmtsz = 18;
    299  1.10.8.1    tls 		align = wi->channels;
    300  1.10.8.1    tls 		break;
    301  1.10.8.1    tls 
    302  1.10.8.1    tls 	/*
    303  1.10.8.1    tls 	 * we could try to support RIFX but it seems to be more portable
    304  1.10.8.1    tls 	 * to output little-endian data for WAV files.
    305  1.10.8.1    tls 	 */
    306  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR_BE:
    307  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR_BE:
    308  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR_LE:
    309  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR_LE:
    310  1.10.8.1    tls 	case AUDIO_ENCODING_PCM16:
    311  1.10.8.1    tls 
    312  1.10.8.1    tls #if BYTE_ORDER == LITTLE_ENDIAN
    313  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR:
    314  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR:
    315  1.10.8.1    tls #endif
    316  1.10.8.1    tls 		fmttag = WAVE_FORMAT_PCM;
    317  1.10.8.1    tls 		fmtsz = 16;
    318  1.10.8.1    tls 		align = wi->channels * (wi->precision / 8);
    319  1.10.8.1    tls 		break;
    320  1.10.8.1    tls 
    321  1.10.8.1    tls 	default:
    322  1.10.8.1    tls #if 0 // move into record.c, and maybe merge.c
    323  1.10.8.1    tls 		{
    324  1.10.8.1    tls 			static int warned = 0;
    325  1.10.8.1    tls 
    326  1.10.8.1    tls 			if (warned == 0) {
    327  1.10.8.1    tls 				const char *s = wav_enc_from_val(wi->encoding);
    328  1.10.8.1    tls 
    329  1.10.8.1    tls 				if (s == NULL)
    330  1.10.8.1    tls 					warnx("can not support encoding of %s", s);
    331  1.10.8.1    tls 				else
    332  1.10.8.1    tls 					warnx("can not support encoding of %d", wi->encoding);
    333  1.10.8.1    tls 				warned = 1;
    334  1.10.8.1    tls 			}
    335  1.10.8.1    tls 		}
    336  1.10.8.1    tls #endif
    337  1.10.8.1    tls 		wi->format = AUDIO_FORMAT_NONE;
    338  1.10.8.1    tls 		return (-1);
    339  1.10.8.1    tls 	}
    340  1.10.8.1    tls 
    341  1.10.8.1    tls 	nchan = wi->channels;
    342  1.10.8.1    tls 	sps = wi->sample_rate;
    343  1.10.8.1    tls 
    344  1.10.8.1    tls 	/* data length */
    345  1.10.8.1    tls 	if (wi->outfd == STDOUT_FILENO)
    346  1.10.8.1    tls 		datalen = 0;
    347  1.10.8.1    tls 	else if (wi->total_size != -1)
    348  1.10.8.1    tls 		datalen = wi->total_size;
    349  1.10.8.1    tls 	else
    350  1.10.8.1    tls 		datalen = 0;
    351  1.10.8.1    tls 
    352  1.10.8.1    tls 	/* file length */
    353  1.10.8.1    tls 	filelen = 4 + (8 + fmtsz) + (8 + datalen);
    354  1.10.8.1    tls 	if (fmttag != WAVE_FORMAT_PCM)
    355  1.10.8.1    tls 		filelen += 8 + factsz;
    356  1.10.8.1    tls 
    357  1.10.8.1    tls 	abps = (double)align*wi->sample_rate / (double)1 + 0.5;
    358  1.10.8.1    tls 
    359  1.10.8.1    tls 	nsample = (datalen / wi->precision) / wi->sample_rate;
    360  1.10.8.1    tls 
    361  1.10.8.1    tls 	/*
    362  1.10.8.1    tls 	 * now we've calculated the info, write it out!
    363  1.10.8.1    tls 	 */
    364  1.10.8.1    tls #define put32(x) do { \
    365  1.10.8.1    tls 	u_int32_t _f; \
    366  1.10.8.1    tls 	putle32(_f, (x)); \
    367  1.10.8.1    tls 	memcpy(p, &_f, 4); \
    368  1.10.8.1    tls } while (0)
    369  1.10.8.1    tls #define put16(x) do { \
    370  1.10.8.1    tls 	u_int16_t _f; \
    371  1.10.8.1    tls 	putle16(_f, (x)); \
    372  1.10.8.1    tls 	memcpy(p, &_f, 2); \
    373  1.10.8.1    tls } while (0)
    374  1.10.8.1    tls 	memcpy(p, riff, 4);
    375  1.10.8.1    tls 	p += 4;				/* 4 */
    376  1.10.8.1    tls 	put32(filelen);
    377  1.10.8.1    tls 	p += 4;				/* 8 */
    378  1.10.8.1    tls 	memcpy(p, wavefmt, 8);
    379  1.10.8.1    tls 	p += 8;				/* 16 */
    380  1.10.8.1    tls 	put32(fmtsz);
    381  1.10.8.1    tls 	p += 4;				/* 20 */
    382  1.10.8.1    tls 	put16(fmttag);
    383  1.10.8.1    tls 	p += 2;				/* 22 */
    384  1.10.8.1    tls 	put16(nchan);
    385  1.10.8.1    tls 	p += 2;				/* 24 */
    386  1.10.8.1    tls 	put32(sps);
    387  1.10.8.1    tls 	p += 4;				/* 28 */
    388  1.10.8.1    tls 	put32(abps);
    389  1.10.8.1    tls 	p += 4;				/* 32 */
    390  1.10.8.1    tls 	put16(align);
    391  1.10.8.1    tls 	p += 2;				/* 34 */
    392  1.10.8.1    tls 	put16(wi->precision);
    393  1.10.8.1    tls 	p += 2;				/* 36 */
    394  1.10.8.1    tls 	/* NON PCM formats have an extended chunk; write it */
    395  1.10.8.1    tls 	if (fmttag != WAVE_FORMAT_PCM) {
    396  1.10.8.1    tls 		put16(extln);
    397  1.10.8.1    tls 		p += 2;			/* 38 */
    398  1.10.8.1    tls 		memcpy(p, fact, 4);
    399  1.10.8.1    tls 		p += 4;			/* 42 */
    400  1.10.8.1    tls 		put32(factsz);
    401  1.10.8.1    tls 		p += 4;			/* 46 */
    402  1.10.8.1    tls 		put32(nsample);
    403  1.10.8.1    tls 		p += 4;			/* 50 */
    404  1.10.8.1    tls 	}
    405  1.10.8.1    tls 	memcpy(p, data, 4);
    406  1.10.8.1    tls 	p += 4;				/* 40/54 */
    407  1.10.8.1    tls 	put32(datalen);
    408  1.10.8.1    tls 	p += 4;				/* 44/58 */
    409  1.10.8.1    tls #undef put32
    410  1.10.8.1    tls #undef put16
    411  1.10.8.1    tls 
    412  1.10.8.1    tls 	*hdrp = wavheaderbuf;
    413  1.10.8.1    tls 	*lenp = (p - wavheaderbuf);
    414  1.10.8.1    tls 
    415  1.10.8.1    tls 	return 0;
    416  1.10.8.1    tls }
    417  1.10.8.1    tls 
    418  1.10.8.1    tls write_conv_func
    419  1.10.8.1    tls wav_write_get_conv_func(struct write_info *wi)
    420  1.10.8.1    tls {
    421  1.10.8.1    tls 	write_conv_func conv_func = NULL;
    422  1.10.8.1    tls 
    423  1.10.8.1    tls 	switch (wi->encoding) {
    424  1.10.8.1    tls 
    425  1.10.8.1    tls 	/*
    426  1.10.8.1    tls 	 * we could try to support RIFX but it seems to be more portable
    427  1.10.8.1    tls 	 * to output little-endian data for WAV files.
    428  1.10.8.1    tls 	 */
    429  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR_BE:
    430  1.10.8.1    tls #if BYTE_ORDER == BIG_ENDIAN
    431  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR:
    432  1.10.8.1    tls #endif
    433  1.10.8.1    tls 		if (wi->precision == 16)
    434  1.10.8.1    tls 			conv_func = change_sign16_swap_bytes_be;
    435  1.10.8.1    tls 		else if (wi->precision == 32)
    436  1.10.8.1    tls 			conv_func = change_sign32_swap_bytes_be;
    437  1.10.8.1    tls 		break;
    438  1.10.8.1    tls 
    439  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR_BE:
    440  1.10.8.1    tls #if BYTE_ORDER == BIG_ENDIAN
    441  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR:
    442  1.10.8.1    tls #endif
    443  1.10.8.1    tls 		if (wi->precision == 8)
    444  1.10.8.1    tls 			conv_func = change_sign8;
    445  1.10.8.1    tls 		else if (wi->precision == 16)
    446  1.10.8.1    tls 			conv_func = swap_bytes;
    447  1.10.8.1    tls 		else if (wi->precision == 32)
    448  1.10.8.1    tls 			conv_func = swap_bytes32;
    449  1.10.8.1    tls 		break;
    450  1.10.8.1    tls 
    451  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR_LE:
    452  1.10.8.1    tls #if BYTE_ORDER == LITTLE_ENDIAN
    453  1.10.8.1    tls 	case AUDIO_ENCODING_ULINEAR:
    454  1.10.8.1    tls #endif
    455  1.10.8.1    tls 		if (wi->precision == 16)
    456  1.10.8.1    tls 			conv_func = change_sign16_le;
    457  1.10.8.1    tls 		else if (wi->precision == 32)
    458  1.10.8.1    tls 			conv_func = change_sign32_le;
    459  1.10.8.1    tls 		break;
    460  1.10.8.1    tls 
    461  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR_LE:
    462  1.10.8.1    tls 	case AUDIO_ENCODING_PCM16:
    463  1.10.8.1    tls #if BYTE_ORDER == LITTLE_ENDIAN
    464  1.10.8.1    tls 	case AUDIO_ENCODING_SLINEAR:
    465  1.10.8.1    tls #endif
    466  1.10.8.1    tls 		if (wi->precision == 8)
    467  1.10.8.1    tls 			conv_func = change_sign8;
    468  1.10.8.1    tls 		break;
    469  1.10.8.1    tls 
    470  1.10.8.1    tls 	default:
    471  1.10.8.1    tls 		wi->format = AUDIO_FORMAT_NONE;
    472  1.10.8.1    tls 	}
    473  1.10.8.1    tls 
    474  1.10.8.1    tls 	return conv_func;
    475  1.10.8.1    tls }
    476