Home | History | Annotate | Line # | Download | only in common
wav.c revision 1.14.6.2
      1 /*	$NetBSD: wav.c,v 1.14.6.2 2024/03/25 15:11:33 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002, 2009, 2013, 2015, 2019, 2024 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * WAV support for the audio tools; thanks go to the sox utility for
     31  * clearing up issues with WAV files.
     32  */
     33 #include <sys/cdefs.h>
     34 
     35 #ifndef lint
     36 __RCSID("$NetBSD: wav.c,v 1.14.6.2 2024/03/25 15:11:33 martin Exp $");
     37 #endif
     38 
     39 
     40 #include <sys/types.h>
     41 #include <sys/audioio.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/time.h>
     44 
     45 #include <ctype.h>
     46 #include <err.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <stdint.h>
     51 #include <unistd.h>
     52 #include <stdbool.h>
     53 
     54 #include "libaudio.h"
     55 #include "auconv.h"
     56 
     57 static const struct {
     58 	int	wenc;
     59 	const char *wname;
     60 } wavencs[] = {
     61 	{ WAVE_FORMAT_UNKNOWN, 	"Microsoft Official Unknown" },
     62 	{ WAVE_FORMAT_PCM,	"Microsoft PCM" },
     63 	{ WAVE_FORMAT_ADPCM,	"Microsoft ADPCM" },
     64 	{ WAVE_FORMAT_IEEE_FLOAT,"Microsoft IEEE Floating-Point" },
     65 	{ WAVE_FORMAT_ALAW,	"Microsoft A-law" },
     66 	{ WAVE_FORMAT_MULAW,	"Microsoft mu-law" },
     67 	{ WAVE_FORMAT_OKI_ADPCM,"OKI ADPCM" },
     68 	{ WAVE_FORMAT_DIGISTD,	"Digistd format" },
     69 	{ WAVE_FORMAT_DIGIFIX,	"Digifix format" },
     70 	{ -1, 			"?Unknown?" },
     71 };
     72 
     73 const char *
     74 wav_enc_from_val(int encoding)
     75 {
     76 	int	i;
     77 
     78 	for (i = 0; wavencs[i].wenc != -1; i++)
     79 		if (wavencs[i].wenc == encoding)
     80 			break;
     81 	return (wavencs[i].wname);
     82 }
     83 
     84 /*
     85  * sample header is:
     86  *
     87  *   RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
     88  *
     89  */
     90 /*
     91  * WAV format helpers
     92  */
     93 
     94 static bool
     95 find_riff_chunk(const char search[4], size_t *remainp, char **wherep, uint32_t *partlen)
     96 {
     97 	wav_audioheaderpart part;
     98 
     99 	*partlen = 0;
    100 
    101 #define ADJUST(l) do {				\
    102 	if (l > *(remainp))			\
    103 		return false;			\
    104 	*(wherep) += (l);			\
    105 	*(remainp) -= (l);			\
    106 } while (0)
    107 
    108 	while (*remainp >= sizeof part) {
    109 		const char *emsg = "";
    110 		uint32_t len;
    111 
    112 		memcpy(&part, *wherep, sizeof part);
    113 		ADJUST(sizeof part);
    114 		len = getle32(part.len);
    115 		if (len % 2) {
    116 			emsg = " (odd length, adjusted)";
    117 			len += 1;
    118 		}
    119 		if (strncmp(part.name, search, sizeof *search) == 0) {
    120 			*partlen = len;
    121 			if (verbose > 1)
    122 				fprintf(stderr, "Found part %.04s length %d%s\n",
    123 				    part.name, len, emsg);
    124 			return true;
    125 		}
    126 		ADJUST(len);
    127 		if (verbose > 1)
    128 			fprintf(stderr, "Skipping part %.04s length %d%s\n",
    129 			    part.name, len, emsg);
    130 	}
    131 #undef ADJUST
    132 
    133 	return false;
    134 }
    135 
    136 /*
    137  * find a .wav header, etc. returns header length on success
    138  */
    139 ssize_t
    140 audio_wav_parse_hdr(void *hdr, size_t sz, u_int *enc, u_int *prec,
    141     u_int *sample, u_int *channels, off_t *datasize)
    142 {
    143 	char	*where = hdr;
    144 	wav_audioheaderfmt fmt;
    145 	wav_audiohdrextensible ext;
    146 	size_t remain = sz;
    147 	u_int	newenc, newprec;
    148 	uint32_t len = 0;
    149 	u_int16_t fmttag;
    150 	static const char
    151 	    strfmt[4] = "fmt ",
    152 	    strRIFF[4] = "RIFF",
    153 	    strWAVE[4] = "WAVE",
    154 	    strdata[4] = "data";
    155 	bool found;
    156 
    157 	if (sz < 32)
    158 		return (AUDIO_ENOENT);
    159 
    160 #define ADJUST(l) do {				\
    161 	if ((l) > remain)			\
    162 		return (AUDIO_ESHORTHDR);	\
    163 	where += (l);				\
    164 	remain -= (l);				\
    165 } while (0)
    166 
    167 	if (memcmp(where, strRIFF, sizeof strRIFF) != 0)
    168 		return (AUDIO_ENOENT);
    169 	ADJUST(sizeof strRIFF);
    170 	/* XXX we ignore the RIFF length here */
    171 	ADJUST(4);
    172 	if (memcmp(where, strWAVE, sizeof strWAVE) != 0)
    173 		return (AUDIO_ENOENT);
    174 	ADJUST(sizeof strWAVE);
    175 
    176 	found = find_riff_chunk(strfmt, &remain, &where, &len);
    177 
    178 	/* too short ? */
    179 	if (!found || remain <= sizeof fmt)
    180 		return (AUDIO_ESHORTHDR);
    181 
    182 	memcpy(&fmt, where, sizeof fmt);
    183 	fmttag = getle16(fmt.tag);
    184 	if (verbose)
    185 		printf("WAVE format tag/len: %04x/%u\n", fmttag, len);
    186 
    187 	if (fmttag == WAVE_FORMAT_EXTENSIBLE) {
    188 		if (len < sizeof(fmt) + sizeof(ext)) {
    189 			if (verbose)
    190 				fprintf(stderr, "short WAVE ext fmt\n");
    191 			return (AUDIO_ESHORTHDR);
    192 		}
    193 		if (remain <= sizeof ext + sizeof fmt) {
    194 			if (verbose)
    195 				fprintf(stderr, "WAVE ext truncated\n");
    196 			return (AUDIO_ESHORTHDR);
    197 		}
    198 		memcpy(&ext, where + sizeof fmt, sizeof ext);
    199 		fmttag = getle16(ext.sub_tag);
    200 		uint16_t sublen = getle16(ext.len);
    201 		if (verbose)
    202 			printf("WAVE extensible tag/len: %04x/%u\n", fmttag, sublen);
    203 
    204 		/*
    205 		 * XXXMRG: it may be that part.len (aka sizeof fmt + sizeof ext)
    206 		 * should equal sizeof fmt + sizeof ext.len + sublen?  this block
    207 		 * is only entered for part.len == 40, where ext.len is expected
    208 		 * to be 22 (sizeof ext.len = 2, sizeof fmt = 16).
    209 		 *
    210 		 * warn about this, but don't consider it an error.
    211 		 */
    212 		if (getle16(ext.len) != 22 && verbose) {
    213 			fprintf(stderr, "warning: WAVE ext.len %u not 22\n",
    214 			    getle16(ext.len));
    215 		}
    216 	} else if (len < sizeof(fmt)) {
    217 		if (verbose)
    218 			fprintf(stderr, "WAVE fmt unsupported size %u\n", len);
    219 		return (AUDIO_EWAVUNSUPP);
    220 	}
    221 	ADJUST(len);
    222 
    223 	switch (fmttag) {
    224 	default:
    225 		return (AUDIO_EWAVUNSUPP);
    226 
    227 	case WAVE_FORMAT_PCM:
    228 	case WAVE_FORMAT_ADPCM:
    229 	case WAVE_FORMAT_OKI_ADPCM:
    230 	case WAVE_FORMAT_IMA_ADPCM:
    231 	case WAVE_FORMAT_DIGIFIX:
    232 	case WAVE_FORMAT_DIGISTD:
    233 		switch (getle16(fmt.bits_per_sample)) {
    234 		case 8:
    235 			newprec = 8;
    236 			break;
    237 		case 16:
    238 			newprec = 16;
    239 			break;
    240 		case 24:
    241 			newprec = 24;
    242 			break;
    243 		case 32:
    244 			newprec = 32;
    245 			break;
    246 		default:
    247 			return (AUDIO_EWAVBADPCM);
    248 		}
    249 		if (newprec == 8)
    250 			newenc = AUDIO_ENCODING_ULINEAR_LE;
    251 		else
    252 			newenc = AUDIO_ENCODING_SLINEAR_LE;
    253 		break;
    254 	case WAVE_FORMAT_ALAW:
    255 		newenc = AUDIO_ENCODING_ALAW;
    256 		newprec = 8;
    257 		break;
    258 	case WAVE_FORMAT_MULAW:
    259 		newenc = AUDIO_ENCODING_ULAW;
    260 		newprec = 8;
    261 		break;
    262 	case WAVE_FORMAT_IEEE_FLOAT:
    263 		switch (getle16(fmt.bits_per_sample)) {
    264 		case 32:
    265 			newenc = AUDIO_ENCODING_LIBAUDIO_FLOAT32;
    266 			newprec = 32;
    267 			break;
    268 		case 64:
    269 			newenc = AUDIO_ENCODING_LIBAUDIO_FLOAT64;
    270 			newprec = 32;
    271 			break;
    272 		default:
    273 			return (AUDIO_EWAVBADPCM);
    274 		}
    275 		break;
    276 	}
    277 
    278 	found = find_riff_chunk(strdata, &remain, &where, &len);
    279 	if (!found)
    280 		return (AUDIO_EWAVNODATA);
    281 
    282 	if (channels)
    283 		*channels = (u_int)getle16(fmt.channels);
    284 	if (sample)
    285 		*sample = getle32(fmt.sample_rate);
    286 	if (enc)
    287 		*enc = newenc;
    288 	if (prec)
    289 		*prec = newprec;
    290 	if (datasize)
    291 		*datasize = (off_t)len;
    292 	return (where - (char *)hdr);
    293 
    294 #undef ADJUST
    295 }
    296 
    297 
    298 /*
    299  * prepare a WAV header for writing; we fill in hdrp, lenp and leftp,
    300  * and expect our caller (wav_write_header()) to use them.
    301  */
    302 int
    303 wav_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp)
    304 {
    305 	/*
    306 	 * WAV header we write looks like this:
    307 	 *
    308 	 *      bytes   purpose
    309 	 *      0-3     "RIFF"
    310 	 *      4-7     RIFF chunk length (file length minus 8)
    311 	 *      8-15    "WAVEfmt "
    312 	 *      16-19   format size
    313 	 *      20-21   format tag
    314 	 *      22-23   number of channels
    315 	 *      24-27   sample rate
    316 	 *      28-31   average bytes per second
    317 	 *      32-33   block alignment
    318 	 *      34-35   bits per sample
    319 	 *
    320 	 * then for ULAW and ALAW outputs, we have an extended chunk size
    321 	 * and a WAV "fact" to add:
    322 	 *
    323 	 *      36-37   length of extension (== 0)
    324 	 *      38-41   "fact"
    325 	 *      42-45   fact size
    326 	 *      46-49   number of samples written
    327 	 *      50-53   "data"
    328 	 *      54-57   data length
    329 	 *      58-     raw audio data
    330 	 *
    331 	 * for PCM outputs we have just the data remaining:
    332 	 *
    333 	 *      36-39   "data"
    334 	 *      40-43   data length
    335 	 *      44-     raw audio data
    336 	 *
    337 	 *	RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
    338 	 */
    339 	static char wavheaderbuf[64];
    340 	char	*p = wavheaderbuf;
    341 	const char *riff = "RIFF",
    342 	    *wavefmt = "WAVEfmt ",
    343 	    *fact = "fact",
    344 	    *data = "data";
    345 	u_int32_t filelen, fmtsz, sps, abps, factsz = 4, nsample, datalen;
    346 	u_int16_t fmttag, nchan, align, extln = 0;
    347 
    348 	if (ti->header_info)
    349 		warnx("header information not supported for WAV");
    350 	*leftp = 0;
    351 
    352 	switch (ti->precision) {
    353 	case 8:
    354 		break;
    355 	case 16:
    356 		break;
    357 	case 24:
    358 		break;
    359 	case 32:
    360 		break;
    361 	default:
    362 		{
    363 			static int warned = 0;
    364 
    365 			if (warned == 0) {
    366 				warnx("can not support precision of %d", ti->precision);
    367 				warned = 1;
    368 			}
    369 		}
    370 		return (-1);
    371 	}
    372 
    373 	switch (ti->encoding) {
    374 	case AUDIO_ENCODING_ULAW:
    375 		fmttag = WAVE_FORMAT_MULAW;
    376 		fmtsz = 18;
    377 		align = ti->channels;
    378 		break;
    379 
    380 	case AUDIO_ENCODING_ALAW:
    381 		fmttag = WAVE_FORMAT_ALAW;
    382 		fmtsz = 18;
    383 		align = ti->channels;
    384 		break;
    385 
    386 	/*
    387 	 * we could try to support RIFX but it seems to be more portable
    388 	 * to output little-endian data for WAV files.
    389 	 */
    390 	case AUDIO_ENCODING_ULINEAR_BE:
    391 	case AUDIO_ENCODING_SLINEAR_BE:
    392 	case AUDIO_ENCODING_ULINEAR_LE:
    393 	case AUDIO_ENCODING_SLINEAR_LE:
    394 	case AUDIO_ENCODING_PCM16:
    395 
    396 #if BYTE_ORDER == LITTLE_ENDIAN
    397 	case AUDIO_ENCODING_ULINEAR:
    398 	case AUDIO_ENCODING_SLINEAR:
    399 #endif
    400 		fmttag = WAVE_FORMAT_PCM;
    401 		fmtsz = 16;
    402 		align = ti->channels * (ti->precision / 8);
    403 		break;
    404 
    405 	default:
    406 #if 0 // move into record.c, and maybe merge.c
    407 		{
    408 			static int warned = 0;
    409 
    410 			if (warned == 0) {
    411 				const char *s = wav_enc_from_val(ti->encoding);
    412 
    413 				if (s == NULL)
    414 					warnx("can not support encoding of %s", s);
    415 				else
    416 					warnx("can not support encoding of %d", ti->encoding);
    417 				warned = 1;
    418 			}
    419 		}
    420 #endif
    421 		ti->format = AUDIO_FORMAT_NONE;
    422 		return (-1);
    423 	}
    424 
    425 	nchan = ti->channels;
    426 	sps = ti->sample_rate;
    427 
    428 	/* data length */
    429 	if (ti->outfd == STDOUT_FILENO)
    430 		datalen = 0;
    431 	else if (ti->total_size != -1)
    432 		datalen = ti->total_size;
    433 	else
    434 		datalen = 0;
    435 
    436 	/* file length */
    437 	filelen = 4 + (8 + fmtsz) + (8 + datalen);
    438 	if (fmttag != WAVE_FORMAT_PCM)
    439 		filelen += 8 + factsz;
    440 
    441 	abps = (double)align*ti->sample_rate / (double)1 + 0.5;
    442 
    443 	nsample = (datalen / ti->precision) / ti->sample_rate;
    444 
    445 	/*
    446 	 * now we've calculated the info, write it out!
    447 	 */
    448 #define put32(x) do { \
    449 	u_int32_t _f; \
    450 	putle32(_f, (x)); \
    451 	memcpy(p, &_f, 4); \
    452 } while (0)
    453 #define put16(x) do { \
    454 	u_int16_t _f; \
    455 	putle16(_f, (x)); \
    456 	memcpy(p, &_f, 2); \
    457 } while (0)
    458 	memcpy(p, riff, 4);
    459 	p += 4;				/* 4 */
    460 	put32(filelen);
    461 	p += 4;				/* 8 */
    462 	memcpy(p, wavefmt, 8);
    463 	p += 8;				/* 16 */
    464 	put32(fmtsz);
    465 	p += 4;				/* 20 */
    466 	put16(fmttag);
    467 	p += 2;				/* 22 */
    468 	put16(nchan);
    469 	p += 2;				/* 24 */
    470 	put32(sps);
    471 	p += 4;				/* 28 */
    472 	put32(abps);
    473 	p += 4;				/* 32 */
    474 	put16(align);
    475 	p += 2;				/* 34 */
    476 	put16(ti->precision);
    477 	p += 2;				/* 36 */
    478 	/* NON PCM formats have an extended chunk; write it */
    479 	if (fmttag != WAVE_FORMAT_PCM) {
    480 		put16(extln);
    481 		p += 2;			/* 38 */
    482 		memcpy(p, fact, 4);
    483 		p += 4;			/* 42 */
    484 		put32(factsz);
    485 		p += 4;			/* 46 */
    486 		put32(nsample);
    487 		p += 4;			/* 50 */
    488 	}
    489 	memcpy(p, data, 4);
    490 	p += 4;				/* 40/54 */
    491 	put32(datalen);
    492 	p += 4;				/* 44/58 */
    493 #undef put32
    494 #undef put16
    495 
    496 	*hdrp = wavheaderbuf;
    497 	*lenp = (p - wavheaderbuf);
    498 
    499 	return 0;
    500 }
    501 
    502 write_conv_func
    503 wav_write_get_conv_func(struct track_info *ti)
    504 {
    505 	write_conv_func conv_func = NULL;
    506 
    507 	switch (ti->encoding) {
    508 
    509 	/*
    510 	 * we could try to support RIFX but it seems to be more portable
    511 	 * to output little-endian data for WAV files.
    512 	 */
    513 	case AUDIO_ENCODING_ULINEAR_BE:
    514 #if BYTE_ORDER == BIG_ENDIAN
    515 	case AUDIO_ENCODING_ULINEAR:
    516 #endif
    517 		if (ti->precision == 16)
    518 			conv_func = change_sign16_swap_bytes_be;
    519 		else if (ti->precision == 32)
    520 			conv_func = change_sign32_swap_bytes_be;
    521 		break;
    522 
    523 	case AUDIO_ENCODING_SLINEAR_BE:
    524 #if BYTE_ORDER == BIG_ENDIAN
    525 	case AUDIO_ENCODING_SLINEAR:
    526 #endif
    527 		if (ti->precision == 8)
    528 			conv_func = change_sign8;
    529 		else if (ti->precision == 16)
    530 			conv_func = swap_bytes;
    531 		else if (ti->precision == 32)
    532 			conv_func = swap_bytes32;
    533 		break;
    534 
    535 	case AUDIO_ENCODING_ULINEAR_LE:
    536 #if BYTE_ORDER == LITTLE_ENDIAN
    537 	case AUDIO_ENCODING_ULINEAR:
    538 #endif
    539 		if (ti->precision == 16)
    540 			conv_func = change_sign16_le;
    541 		else if (ti->precision == 32)
    542 			conv_func = change_sign32_le;
    543 		break;
    544 
    545 	case AUDIO_ENCODING_SLINEAR_LE:
    546 	case AUDIO_ENCODING_PCM16:
    547 #if BYTE_ORDER == LITTLE_ENDIAN
    548 	case AUDIO_ENCODING_SLINEAR:
    549 #endif
    550 		if (ti->precision == 8)
    551 			conv_func = change_sign8;
    552 		break;
    553 
    554 	default:
    555 		ti->format = AUDIO_FORMAT_NONE;
    556 	}
    557 
    558 	return conv_func;
    559 }
    560