Home | History | Annotate | Line # | Download | only in common
wav.c revision 1.22
      1 /*	$NetBSD: wav.c,v 1.22 2024/03/12 00:34:38 mrg 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.22 2024/03/12 00:34:38 mrg 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 (len) {
    283 		if (channels)
    284 			*channels = (u_int)getle16(fmt.channels);
    285 		if (sample)
    286 			*sample = getle32(fmt.sample_rate);
    287 		if (enc)
    288 			*enc = newenc;
    289 		if (prec)
    290 			*prec = newprec;
    291 		if (datasize)
    292 			*datasize = (off_t)len;
    293 		return (where - (char *)hdr);
    294 	}
    295 	return (AUDIO_EWAVNODATA);
    296 
    297 #undef ADJUST
    298 }
    299 
    300 
    301 /*
    302  * prepare a WAV header for writing; we fill in hdrp, lenp and leftp,
    303  * and expect our caller (wav_write_header()) to use them.
    304  */
    305 int
    306 wav_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp)
    307 {
    308 	/*
    309 	 * WAV header we write looks like this:
    310 	 *
    311 	 *      bytes   purpose
    312 	 *      0-3     "RIFF"
    313 	 *      4-7     RIFF chunk length (file length minus 8)
    314 	 *      8-15    "WAVEfmt "
    315 	 *      16-19   format size
    316 	 *      20-21   format tag
    317 	 *      22-23   number of channels
    318 	 *      24-27   sample rate
    319 	 *      28-31   average bytes per second
    320 	 *      32-33   block alignment
    321 	 *      34-35   bits per sample
    322 	 *
    323 	 * then for ULAW and ALAW outputs, we have an extended chunk size
    324 	 * and a WAV "fact" to add:
    325 	 *
    326 	 *      36-37   length of extension (== 0)
    327 	 *      38-41   "fact"
    328 	 *      42-45   fact size
    329 	 *      46-49   number of samples written
    330 	 *      50-53   "data"
    331 	 *      54-57   data length
    332 	 *      58-     raw audio data
    333 	 *
    334 	 * for PCM outputs we have just the data remaining:
    335 	 *
    336 	 *      36-39   "data"
    337 	 *      40-43   data length
    338 	 *      44-     raw audio data
    339 	 *
    340 	 *	RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
    341 	 */
    342 	static char wavheaderbuf[64];
    343 	char	*p = wavheaderbuf;
    344 	const char *riff = "RIFF",
    345 	    *wavefmt = "WAVEfmt ",
    346 	    *fact = "fact",
    347 	    *data = "data";
    348 	u_int32_t filelen, fmtsz, sps, abps, factsz = 4, nsample, datalen;
    349 	u_int16_t fmttag, nchan, align, extln = 0;
    350 
    351 	if (ti->header_info)
    352 		warnx("header information not supported for WAV");
    353 	*leftp = 0;
    354 
    355 	switch (ti->precision) {
    356 	case 8:
    357 		break;
    358 	case 16:
    359 		break;
    360 	case 24:
    361 		break;
    362 	case 32:
    363 		break;
    364 	default:
    365 		{
    366 			static int warned = 0;
    367 
    368 			if (warned == 0) {
    369 				warnx("can not support precision of %d", ti->precision);
    370 				warned = 1;
    371 			}
    372 		}
    373 		return (-1);
    374 	}
    375 
    376 	switch (ti->encoding) {
    377 	case AUDIO_ENCODING_ULAW:
    378 		fmttag = WAVE_FORMAT_MULAW;
    379 		fmtsz = 18;
    380 		align = ti->channels;
    381 		break;
    382 
    383 	case AUDIO_ENCODING_ALAW:
    384 		fmttag = WAVE_FORMAT_ALAW;
    385 		fmtsz = 18;
    386 		align = ti->channels;
    387 		break;
    388 
    389 	/*
    390 	 * we could try to support RIFX but it seems to be more portable
    391 	 * to output little-endian data for WAV files.
    392 	 */
    393 	case AUDIO_ENCODING_ULINEAR_BE:
    394 	case AUDIO_ENCODING_SLINEAR_BE:
    395 	case AUDIO_ENCODING_ULINEAR_LE:
    396 	case AUDIO_ENCODING_SLINEAR_LE:
    397 	case AUDIO_ENCODING_PCM16:
    398 
    399 #if BYTE_ORDER == LITTLE_ENDIAN
    400 	case AUDIO_ENCODING_ULINEAR:
    401 	case AUDIO_ENCODING_SLINEAR:
    402 #endif
    403 		fmttag = WAVE_FORMAT_PCM;
    404 		fmtsz = 16;
    405 		align = ti->channels * (ti->precision / 8);
    406 		break;
    407 
    408 	default:
    409 #if 0 // move into record.c, and maybe merge.c
    410 		{
    411 			static int warned = 0;
    412 
    413 			if (warned == 0) {
    414 				const char *s = wav_enc_from_val(ti->encoding);
    415 
    416 				if (s == NULL)
    417 					warnx("can not support encoding of %s", s);
    418 				else
    419 					warnx("can not support encoding of %d", ti->encoding);
    420 				warned = 1;
    421 			}
    422 		}
    423 #endif
    424 		ti->format = AUDIO_FORMAT_NONE;
    425 		return (-1);
    426 	}
    427 
    428 	nchan = ti->channels;
    429 	sps = ti->sample_rate;
    430 
    431 	/* data length */
    432 	if (ti->outfd == STDOUT_FILENO)
    433 		datalen = 0;
    434 	else if (ti->total_size != -1)
    435 		datalen = ti->total_size;
    436 	else
    437 		datalen = 0;
    438 
    439 	/* file length */
    440 	filelen = 4 + (8 + fmtsz) + (8 + datalen);
    441 	if (fmttag != WAVE_FORMAT_PCM)
    442 		filelen += 8 + factsz;
    443 
    444 	abps = (double)align*ti->sample_rate / (double)1 + 0.5;
    445 
    446 	nsample = (datalen / ti->precision) / ti->sample_rate;
    447 
    448 	/*
    449 	 * now we've calculated the info, write it out!
    450 	 */
    451 #define put32(x) do { \
    452 	u_int32_t _f; \
    453 	putle32(_f, (x)); \
    454 	memcpy(p, &_f, 4); \
    455 } while (0)
    456 #define put16(x) do { \
    457 	u_int16_t _f; \
    458 	putle16(_f, (x)); \
    459 	memcpy(p, &_f, 2); \
    460 } while (0)
    461 	memcpy(p, riff, 4);
    462 	p += 4;				/* 4 */
    463 	put32(filelen);
    464 	p += 4;				/* 8 */
    465 	memcpy(p, wavefmt, 8);
    466 	p += 8;				/* 16 */
    467 	put32(fmtsz);
    468 	p += 4;				/* 20 */
    469 	put16(fmttag);
    470 	p += 2;				/* 22 */
    471 	put16(nchan);
    472 	p += 2;				/* 24 */
    473 	put32(sps);
    474 	p += 4;				/* 28 */
    475 	put32(abps);
    476 	p += 4;				/* 32 */
    477 	put16(align);
    478 	p += 2;				/* 34 */
    479 	put16(ti->precision);
    480 	p += 2;				/* 36 */
    481 	/* NON PCM formats have an extended chunk; write it */
    482 	if (fmttag != WAVE_FORMAT_PCM) {
    483 		put16(extln);
    484 		p += 2;			/* 38 */
    485 		memcpy(p, fact, 4);
    486 		p += 4;			/* 42 */
    487 		put32(factsz);
    488 		p += 4;			/* 46 */
    489 		put32(nsample);
    490 		p += 4;			/* 50 */
    491 	}
    492 	memcpy(p, data, 4);
    493 	p += 4;				/* 40/54 */
    494 	put32(datalen);
    495 	p += 4;				/* 44/58 */
    496 #undef put32
    497 #undef put16
    498 
    499 	*hdrp = wavheaderbuf;
    500 	*lenp = (p - wavheaderbuf);
    501 
    502 	return 0;
    503 }
    504 
    505 write_conv_func
    506 wav_write_get_conv_func(struct track_info *ti)
    507 {
    508 	write_conv_func conv_func = NULL;
    509 
    510 	switch (ti->encoding) {
    511 
    512 	/*
    513 	 * we could try to support RIFX but it seems to be more portable
    514 	 * to output little-endian data for WAV files.
    515 	 */
    516 	case AUDIO_ENCODING_ULINEAR_BE:
    517 #if BYTE_ORDER == BIG_ENDIAN
    518 	case AUDIO_ENCODING_ULINEAR:
    519 #endif
    520 		if (ti->precision == 16)
    521 			conv_func = change_sign16_swap_bytes_be;
    522 		else if (ti->precision == 32)
    523 			conv_func = change_sign32_swap_bytes_be;
    524 		break;
    525 
    526 	case AUDIO_ENCODING_SLINEAR_BE:
    527 #if BYTE_ORDER == BIG_ENDIAN
    528 	case AUDIO_ENCODING_SLINEAR:
    529 #endif
    530 		if (ti->precision == 8)
    531 			conv_func = change_sign8;
    532 		else if (ti->precision == 16)
    533 			conv_func = swap_bytes;
    534 		else if (ti->precision == 32)
    535 			conv_func = swap_bytes32;
    536 		break;
    537 
    538 	case AUDIO_ENCODING_ULINEAR_LE:
    539 #if BYTE_ORDER == LITTLE_ENDIAN
    540 	case AUDIO_ENCODING_ULINEAR:
    541 #endif
    542 		if (ti->precision == 16)
    543 			conv_func = change_sign16_le;
    544 		else if (ti->precision == 32)
    545 			conv_func = change_sign32_le;
    546 		break;
    547 
    548 	case AUDIO_ENCODING_SLINEAR_LE:
    549 	case AUDIO_ENCODING_PCM16:
    550 #if BYTE_ORDER == LITTLE_ENDIAN
    551 	case AUDIO_ENCODING_SLINEAR:
    552 #endif
    553 		if (ti->precision == 8)
    554 			conv_func = change_sign8;
    555 		break;
    556 
    557 	default:
    558 		ti->format = AUDIO_FORMAT_NONE;
    559 	}
    560 
    561 	return conv_func;
    562 }
    563