Home | History | Annotate | Line # | Download | only in record
record.c revision 1.40
      1 /*	$NetBSD: record.c,v 1.40 2005/07/05 22:01:42 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2002 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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * SunOS compatible audiorecord(1)
     33  */
     34 #include <sys/cdefs.h>
     35 
     36 #ifndef lint
     37 __RCSID("$NetBSD: record.c,v 1.40 2005/07/05 22:01:42 mrg Exp $");
     38 #endif
     39 
     40 
     41 #include <sys/types.h>
     42 #include <sys/audioio.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/time.h>
     45 #include <sys/uio.h>
     46 
     47 #include <err.h>
     48 #include <fcntl.h>
     49 #include <paths.h>
     50 #include <signal.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "libaudio.h"
     57 #include "auconv.h"
     58 
     59 audio_info_t info, oinfo;
     60 ssize_t	total_size = -1;
     61 const char *device;
     62 int	format = AUDIO_FORMAT_DEFAULT;
     63 char	*header_info;
     64 char	default_info[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
     65 int	audiofd, outfd;
     66 int	qflag, aflag, fflag;
     67 int	verbose;
     68 int	monitor_gain, omonitor_gain;
     69 int	gain;
     70 int	balance;
     71 int	port;
     72 int	encoding;
     73 char	*encoding_str;
     74 int	precision;
     75 int	sample_rate;
     76 int	channels;
     77 struct timeval record_time;
     78 struct timeval start_time;
     79 
     80 void (*conv_func) (u_char *, int);
     81 
     82 void usage (void);
     83 int main (int, char *[]);
     84 int timeleft (struct timeval *, struct timeval *);
     85 void cleanup (int) __attribute__((__noreturn__));
     86 int write_header_sun (void **, size_t *, int *);
     87 int write_header_wav (void **, size_t *, int *);
     88 void write_header (void);
     89 void rewrite_header (void);
     90 
     91 int
     92 main(argc, argv)
     93 	int argc;
     94 	char *argv[];
     95 {
     96 	char	*buffer;
     97 	size_t	len, bufsize;
     98 	int	ch, no_time_limit = 1;
     99 	const char *defdevice = _PATH_SOUND;
    100 
    101 	while ((ch = getopt(argc, argv, "ab:C:F:c:d:e:fhi:m:P:p:qt:s:Vv:")) != -1) {
    102 		switch (ch) {
    103 		case 'a':
    104 			aflag++;
    105 			break;
    106 		case 'b':
    107 			decode_int(optarg, &balance);
    108 			if (balance < 0 || balance > 63)
    109 				errx(1, "balance must be between 0 and 63");
    110 			break;
    111 		case 'C':
    112 			/* Ignore, compatibility */
    113 			break;
    114 		case 'F':
    115 			format = audio_format_from_str(optarg);
    116 			if (format < 0)
    117 				errx(1, "Unknown audio format; supported "
    118 				    "formats: \"sun\", \"wav\", and \"none\"");
    119 			break;
    120 		case 'c':
    121 			decode_int(optarg, &channels);
    122 			if (channels < 0 || channels > 16)
    123 				errx(1, "channels must be between 0 and 16");
    124 			break;
    125 		case 'd':
    126 			device = optarg;
    127 			break;
    128 		case 'e':
    129 			encoding_str = optarg;
    130 			break;
    131 		case 'f':
    132 			fflag++;
    133 			break;
    134 		case 'i':
    135 			header_info = optarg;
    136 			break;
    137 		case 'm':
    138 			decode_int(optarg, &monitor_gain);
    139 			if (monitor_gain < 0 || monitor_gain > 255)
    140 				errx(1, "monitor volume must be between 0 and 255");
    141 			break;
    142 		case 'P':
    143 			decode_int(optarg, &precision);
    144 			if (precision != 4 && precision != 8 &&
    145 			    precision != 16 && precision != 24 &&
    146 			    precision != 32)
    147 				errx(1, "precision must be between 4, 8, 16, 24 or 32");
    148 			break;
    149 		case 'p':
    150 			len = strlen(optarg);
    151 
    152 			if (strncmp(optarg, "mic", len) == 0)
    153 				port |= AUDIO_MICROPHONE;
    154 			else if (strncmp(optarg, "cd", len) == 0 ||
    155 			           strncmp(optarg, "internal-cd", len) == 0)
    156 				port |= AUDIO_CD;
    157 			else if (strncmp(optarg, "line", len) == 0)
    158 				port |= AUDIO_LINE_IN;
    159 			else
    160 				errx(1,
    161 			    "port must be `cd', `internal-cd', `mic', or `line'");
    162 			break;
    163 		case 'q':
    164 			qflag++;
    165 			break;
    166 		case 's':
    167 			decode_int(optarg, &sample_rate);
    168 			if (sample_rate < 0 || sample_rate > 48000 * 2)	/* XXX */
    169 				errx(1, "sample rate must be between 0 and 96000");
    170 			break;
    171 		case 't':
    172 			no_time_limit = 0;
    173 			decode_time(optarg, &record_time);
    174 			break;
    175 		case 'V':
    176 			verbose++;
    177 			break;
    178 		case 'v':
    179 			decode_int(optarg, &gain);
    180 			if (gain < 0 || gain > 255)
    181 				errx(1, "volume must be between 0 and 255");
    182 			break;
    183 		/* case 'h': */
    184 		default:
    185 			usage();
    186 			/* NOTREACHED */
    187 		}
    188 	}
    189 	argc -= optind;
    190 	argv += optind;
    191 
    192 	if (argc != 1)
    193 		usage();
    194 
    195 	/*
    196 	 * convert the encoding string into a value.
    197 	 */
    198 	if (encoding_str) {
    199 		encoding = audio_enc_to_val(encoding_str);
    200 		if (encoding == -1)
    201 			errx(1, "unknown encoding, bailing...");
    202 	}
    203 #if 0
    204 	else
    205 		encoding = AUDIO_ENCODING_ULAW;
    206 #endif
    207 
    208 	/*
    209 	 * open the output file
    210 	 */
    211 	if (argv[0][0] != '-' && argv[0][1] != '\0') {
    212 		/* intuit the file type from the name */
    213 		if (format == AUDIO_FORMAT_DEFAULT)
    214 		{
    215 			size_t flen = strlen(*argv);
    216 			const char *arg = *argv;
    217 
    218 			if (strcasecmp(arg + flen - 3, ".au") == 0)
    219 				format = AUDIO_FORMAT_SUN;
    220 			else if (strcasecmp(arg + flen - 4, ".wav") == 0)
    221 				format = AUDIO_FORMAT_WAV;
    222 		}
    223 		outfd = open(*argv, O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY, 0666);
    224 		if (outfd < 0)
    225 			err(1, "could not open %s", *argv);
    226 	} else
    227 		outfd = STDOUT_FILENO;
    228 
    229 	/*
    230 	 * open the audio device
    231 	 */
    232 	if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
    233 	    (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
    234 		device = defdevice;
    235 
    236 	audiofd = open(device, O_RDONLY);
    237 	if (audiofd < 0 && device == defdevice) {
    238 		device = _PATH_SOUND0;
    239 		audiofd = open(device, O_RDONLY);
    240 	}
    241 	if (audiofd < 0)
    242 		err(1, "failed to open %s", device);
    243 
    244 	/*
    245 	 * work out the buffer size to use, and allocate it.  also work out
    246 	 * what the old monitor gain value is, so that we can reset it later.
    247 	 */
    248 	if (ioctl(audiofd, AUDIO_GETINFO, &oinfo) < 0)
    249 		err(1, "failed to get audio info");
    250 	bufsize = oinfo.record.buffer_size;
    251 	if (bufsize < 32 * 1024)
    252 		bufsize = 32 * 1024;
    253 	omonitor_gain = oinfo.monitor_gain;
    254 
    255 	buffer = malloc(bufsize);
    256 	if (buffer == NULL)
    257 		err(1, "couldn't malloc buffer of %d size", (int)bufsize);
    258 
    259 	/*
    260 	 * set up audio device for recording with the speified parameters
    261 	 */
    262 	AUDIO_INITINFO(&info);
    263 
    264 	/*
    265 	 * for these, get the current values for stuffing into the header
    266 	 */
    267 #define SETINFO(x)	if (x) \
    268 				info.record.x = x; \
    269 			else \
    270 				info.record.x = x = oinfo.record.x;
    271 	SETINFO (sample_rate)
    272 	SETINFO (channels)
    273 	SETINFO (precision)
    274 	SETINFO (encoding)
    275 	SETINFO (gain)
    276 	SETINFO (port)
    277 	SETINFO (balance)
    278 #undef SETINFO
    279 
    280 	if (monitor_gain)
    281 		info.monitor_gain = monitor_gain;
    282 	else
    283 		monitor_gain = oinfo.monitor_gain;
    284 
    285 	info.mode = AUMODE_RECORD;
    286 	if (ioctl(audiofd, AUDIO_SETINFO, &info) < 0)
    287 		err(1, "failed to set audio info");
    288 
    289 	signal(SIGINT, cleanup);
    290 	write_header();
    291 	total_size = 0;
    292 
    293 	if (verbose && conv_func) {
    294 		const char *s = NULL;
    295 
    296 		if (conv_func == swap_bytes)
    297 			s = "swap bytes (16 bit)";
    298 		else if (conv_func == swap_bytes32)
    299 			s = "swap bytes (32 bit)";
    300 		else if (conv_func == change_sign16_be)
    301 			s = "change sign (big-endian, 16 bit)";
    302 		else if (conv_func == change_sign16_le)
    303 			s = "change sign (little-endian, 16 bit)";
    304 		else if (conv_func == change_sign32_be)
    305 			s = "change sign (big-endian, 32 bit)";
    306 		else if (conv_func == change_sign32_le)
    307 			s = "change sign (little-endian, 32 bit)";
    308 		else if (conv_func == change_sign16_swap_bytes_be)
    309 			s = "change sign & swap bytes (big-endian, 16 bit)";
    310 		else if (conv_func == change_sign16_swap_bytes_le)
    311 			s = "change sign & swap bytes (little-endian, 16 bit)";
    312 		else if (conv_func == change_sign32_swap_bytes_be)
    313 			s = "change sign (big-endian, 32 bit)";
    314 		else if (conv_func == change_sign32_swap_bytes_le)
    315 			s = "change sign & swap bytes (little-endian, 32 bit)";
    316 
    317 		if (s)
    318 			fprintf(stderr, "%s: converting, using function: %s\n",
    319 			    getprogname(), s);
    320 		else
    321 			fprintf(stderr, "%s: using unnamed conversion "
    322 					"function\n", getprogname());
    323 	}
    324 
    325 	if (verbose)
    326 		fprintf(stderr,
    327 		   "sample_rate=%d channels=%d precision=%d encoding=%s\n",
    328 		   info.record.sample_rate, info.record.channels,
    329 		   info.record.precision,
    330 		   audio_enc_from_val(info.record.encoding));
    331 
    332 	if (!no_time_limit && verbose)
    333 		fprintf(stderr, "recording for %lu seconds, %lu microseconds\n",
    334 		    (u_long)record_time.tv_sec, (u_long)record_time.tv_usec);
    335 
    336 	(void)gettimeofday(&start_time, NULL);
    337 	while (no_time_limit || timeleft(&start_time, &record_time)) {
    338 		if (read(audiofd, buffer, bufsize) != bufsize)
    339 			err(1, "read failed");
    340 		if (conv_func)
    341 			(*conv_func)(buffer, bufsize);
    342 		if (write(outfd, buffer, bufsize) != bufsize)
    343 			err(1, "write failed");
    344 		total_size += bufsize;
    345 	}
    346 	cleanup(0);
    347 }
    348 
    349 int
    350 timeleft(start_tvp, record_tvp)
    351 	struct timeval *start_tvp;
    352 	struct timeval *record_tvp;
    353 {
    354 	struct timeval now, diff;
    355 
    356 	(void)gettimeofday(&now, NULL);
    357 	timersub(&now, start_tvp, &diff);
    358 	timersub(record_tvp, &diff, &now);
    359 
    360 	return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
    361 }
    362 
    363 void
    364 cleanup(signo)
    365 	int signo;
    366 {
    367 
    368 	rewrite_header();
    369 	close(outfd);
    370 	if (omonitor_gain) {
    371 		AUDIO_INITINFO(&info);
    372 		info.monitor_gain = omonitor_gain;
    373 		if (ioctl(audiofd, AUDIO_SETINFO, &info) < 0)
    374 			err(1, "failed to reset audio info");
    375 	}
    376 	close(audiofd);
    377 	exit(0);
    378 }
    379 
    380 int
    381 write_header_sun(hdrp, lenp, leftp)
    382 	void **hdrp;
    383 	size_t *lenp;
    384 	int *leftp;
    385 {
    386 	static int warned = 0;
    387 	static sun_audioheader auh;
    388 	int sunenc, oencoding = encoding;
    389 
    390 	/* only perform conversions if we don't specify the encoding */
    391 	switch (encoding) {
    392 	case AUDIO_ENCODING_ULINEAR_LE:
    393 #if BYTE_ORDER == LITTLE_ENDIAN
    394 	case AUDIO_ENCODING_ULINEAR:
    395 #endif
    396 		if (precision == 16)
    397 			conv_func = change_sign16_swap_bytes_le;
    398 		else if (precision == 32)
    399 			conv_func = change_sign32_swap_bytes_le;
    400 		if (conv_func)
    401 			encoding = AUDIO_ENCODING_SLINEAR_BE;
    402 		break;
    403 
    404 	case AUDIO_ENCODING_ULINEAR_BE:
    405 #if BYTE_ORDER == BIG_ENDIAN
    406 	case AUDIO_ENCODING_ULINEAR:
    407 #endif
    408 		if (precision == 16)
    409 			conv_func = change_sign16_be;
    410 		else if (precision == 32)
    411 			conv_func = change_sign32_be;
    412 		if (conv_func)
    413 			encoding = AUDIO_ENCODING_SLINEAR_BE;
    414 		break;
    415 
    416 	case AUDIO_ENCODING_SLINEAR_LE:
    417 #if BYTE_ORDER == LITTLE_ENDIAN
    418 	case AUDIO_ENCODING_SLINEAR:
    419 #endif
    420 		if (precision == 16)
    421 			conv_func = swap_bytes;
    422 		else if (precision == 32)
    423 			conv_func = swap_bytes32;
    424 		if (conv_func)
    425 			encoding = AUDIO_ENCODING_SLINEAR_BE;
    426 		break;
    427 
    428 #if BYTE_ORDER == BIG_ENDIAN
    429 	case AUDIO_ENCODING_SLINEAR:
    430 		encoding = AUDIO_ENCODING_SLINEAR_BE;
    431 		break;
    432 #endif
    433 	}
    434 
    435 	/* if we can't express this as a Sun header, don't write any */
    436 	if (audio_encoding_to_sun(encoding, precision, &sunenc) != 0) {
    437 		if (!qflag && !warned) {
    438 			const char *s = audio_enc_from_val(oencoding);
    439 
    440 			if (s == NULL)
    441 				s = "(unknown)";
    442 			warnx("failed to convert to sun encoding from %s "
    443 			      "(precision %d);\nSun audio header not written",
    444 			      s, precision);
    445 		}
    446 		format = AUDIO_FORMAT_NONE;
    447 		conv_func = 0;
    448 		warned = 1;
    449 		return -1;
    450 	}
    451 
    452 	auh.magic = htonl(AUDIO_FILE_MAGIC);
    453 	if (outfd == STDOUT_FILENO)
    454 		auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
    455 	else
    456 		auh.data_size = htonl(total_size);
    457 	auh.encoding = htonl(sunenc);
    458 	auh.sample_rate = htonl(sample_rate);
    459 	auh.channels = htonl(channels);
    460 	if (header_info) {
    461 		int 	len, infolen;
    462 
    463 		infolen = ((len = strlen(header_info)) + 7) & 0xfffffff8;
    464 		*leftp = infolen - len;
    465 		auh.hdr_size = htonl(sizeof(auh) + infolen);
    466 	} else {
    467 		*leftp = sizeof(default_info);
    468 		auh.hdr_size = htonl(sizeof(auh) + *leftp);
    469 	}
    470 	*(sun_audioheader **)hdrp = &auh;
    471 	*lenp = sizeof auh;
    472 	return 0;
    473 }
    474 
    475 int
    476 write_header_wav(hdrp, lenp, leftp)
    477 	void **hdrp;
    478 	size_t *lenp;
    479 	int *leftp;
    480 {
    481 	/*
    482 	 * WAV header we write looks like this:
    483 	 *
    484 	 *      bytes   purpose
    485 	 *      0-3     "RIFF"
    486 	 *      4-7     file length (minus 8)
    487 	 *      8-15    "WAVEfmt "
    488 	 *      16-19   format size
    489 	 *      20-21   format tag
    490 	 *      22-23   number of channels
    491 	 *      24-27   sample rate
    492 	 *      28-31   average bytes per second
    493 	 *      32-33   block alignment
    494 	 *      34-35   bits per sample
    495 	 *
    496 	 * then for ULAW and ALAW outputs, we have an extended chunk size
    497 	 * and a WAV "fact" to add:
    498 	 *
    499 	 *      36-37   length of extension (== 0)
    500 	 *      38-41   "fact"
    501 	 *      42-45   fact size
    502 	 *      46-49   number of samples written
    503 	 *      50-53   "data"
    504 	 *      54-57   data length
    505 	 *      58-     raw audio data
    506 	 *
    507 	 * for PCM outputs we have just the data remaining:
    508 	 *
    509 	 *      36-39   "data"
    510 	 *      40-43   data length
    511 	 *      44-     raw audio data
    512 	 *
    513 	 *	RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
    514 	 */
    515 	char	wavheaderbuf[64], *p = wavheaderbuf;
    516 	const char *riff = "RIFF",
    517 	    *wavefmt = "WAVEfmt ",
    518 	    *fact = "fact",
    519 	    *data = "data";
    520 	u_int32_t filelen, fmtsz, sps, abps, factsz = 4, nsample, datalen;
    521 	u_int16_t fmttag, nchan, align, bps, extln = 0;
    522 
    523 	if (header_info)
    524 		warnx("header information not supported for WAV");
    525 	*leftp = 0;
    526 
    527 	switch (precision) {
    528 	case 8:
    529 		bps = 8;
    530 		break;
    531 	case 16:
    532 		bps = 16;
    533 		break;
    534 	case 32:
    535 		bps = 32;
    536 		break;
    537 	default:
    538 		{
    539 			static int warned = 0;
    540 
    541 			if (warned == 0) {
    542 				warnx("can not support precision of %d", precision);
    543 				warned = 1;
    544 			}
    545 		}
    546 		return (-1);
    547 	}
    548 
    549 	switch (encoding) {
    550 	case AUDIO_ENCODING_ULAW:
    551 		fmttag = WAVE_FORMAT_MULAW;
    552 		fmtsz = 18;
    553 		align = channels;
    554 		break;
    555 
    556 	case AUDIO_ENCODING_ALAW:
    557 		fmttag = WAVE_FORMAT_ALAW;
    558 		fmtsz = 18;
    559 		align = channels;
    560 		break;
    561 
    562 	/*
    563 	 * we could try to support RIFX but it seems to be more portable
    564 	 * to output little-endian data for WAV files.
    565 	 */
    566 	case AUDIO_ENCODING_ULINEAR_BE:
    567 #if BYTE_ORDER == BIG_ENDIAN
    568 	case AUDIO_ENCODING_ULINEAR:
    569 #endif
    570 		if (bps == 16)
    571 			conv_func = change_sign16_swap_bytes_be;
    572 		else if (bps == 32)
    573 			conv_func = change_sign32_swap_bytes_be;
    574 		goto fmt_pcm;
    575 
    576 	case AUDIO_ENCODING_SLINEAR_BE:
    577 #if BYTE_ORDER == BIG_ENDIAN
    578 	case AUDIO_ENCODING_SLINEAR:
    579 #endif
    580 		if (bps == 8)
    581 			conv_func = change_sign8;
    582 		else if (bps == 16)
    583 			conv_func = swap_bytes;
    584 		else if (bps == 32)
    585 			conv_func = swap_bytes32;
    586 		goto fmt_pcm;
    587 
    588 	case AUDIO_ENCODING_ULINEAR_LE:
    589 #if BYTE_ORDER == LITTLE_ENDIAN
    590 	case AUDIO_ENCODING_ULINEAR:
    591 #endif
    592 		if (bps == 16)
    593 			conv_func = change_sign16_le;
    594 		else if (bps == 32)
    595 			conv_func = change_sign32_le;
    596 		/* FALLTHROUGH */
    597 
    598 	case AUDIO_ENCODING_SLINEAR_LE:
    599 	case AUDIO_ENCODING_PCM16:
    600 #if BYTE_ORDER == LITTLE_ENDIAN
    601 	case AUDIO_ENCODING_SLINEAR:
    602 #endif
    603 		if (bps == 8)
    604 			conv_func = change_sign8;
    605 fmt_pcm:
    606 		fmttag = WAVE_FORMAT_PCM;
    607 		fmtsz = 16;
    608 		align = channels * (bps / 8);
    609 		break;
    610 
    611 	default:
    612 		{
    613 			static int warned = 0;
    614 
    615 			if (warned == 0) {
    616 				const char *s = wav_enc_from_val(encoding);
    617 
    618 				if (s == NULL)
    619 					warnx("can not support encoding of %s", s);
    620 				else
    621 					warnx("can not support encoding of %d", encoding);
    622 				warned = 1;
    623 			}
    624 		}
    625 		format = AUDIO_FORMAT_NONE;
    626 		return (-1);
    627 	}
    628 
    629 	nchan = channels;
    630 	sps = sample_rate;
    631 
    632 	/* data length */
    633 	if (outfd == STDOUT_FILENO)
    634 		datalen = 0;
    635 	else
    636 		datalen = total_size;
    637 
    638 	/* file length */
    639 	filelen = 4 + (8 + fmtsz) + (8 + datalen);
    640 	if (fmttag != WAVE_FORMAT_PCM)
    641 		filelen += 8 + factsz;
    642 
    643 	abps = (double)align*sample_rate / (double)1 + 0.5;
    644 
    645 	nsample = (datalen / bps) / sample_rate;
    646 
    647 	/*
    648 	 * now we've calculated the info, write it out!
    649 	 */
    650 #define put32(x) do { \
    651 	u_int32_t _f; \
    652 	putle32(_f, (x)); \
    653 	memcpy(p, &_f, 4); \
    654 } while (0)
    655 #define put16(x) do { \
    656 	u_int16_t _f; \
    657 	putle16(_f, (x)); \
    658 	memcpy(p, &_f, 2); \
    659 } while (0)
    660 	memcpy(p, riff, 4);
    661 	p += 4;				/* 4 */
    662 	put32(filelen);
    663 	p += 4;				/* 8 */
    664 	memcpy(p, wavefmt, 8);
    665 	p += 8;				/* 16 */
    666 	put32(fmtsz);
    667 	p += 4;				/* 20 */
    668 	put16(fmttag);
    669 	p += 2;				/* 22 */
    670 	put16(nchan);
    671 	p += 2;				/* 24 */
    672 	put32(sps);
    673 	p += 4;				/* 28 */
    674 	put32(abps);
    675 	p += 4;				/* 32 */
    676 	put16(align);
    677 	p += 2;				/* 34 */
    678 	put16(bps);
    679 	p += 2;				/* 36 */
    680 	/* NON PCM formats have an extended chunk; write it */
    681 	if (fmttag != WAVE_FORMAT_PCM) {
    682 		put16(extln);
    683 		p += 2;			/* 38 */
    684 		memcpy(p, fact, 4);
    685 		p += 4;			/* 42 */
    686 		put32(factsz);
    687 		p += 4;			/* 46 */
    688 		put32(nsample);
    689 		p += 4;			/* 50 */
    690 	}
    691 	memcpy(p, data, 4);
    692 	p += 4;				/* 40/54 */
    693 	put32(datalen);
    694 	p += 4;				/* 44/58 */
    695 #undef put32
    696 #undef put16
    697 
    698 	*hdrp = wavheaderbuf;
    699 	*lenp = (p - wavheaderbuf);
    700 
    701 	return 0;
    702 }
    703 
    704 void
    705 write_header()
    706 {
    707 	struct iovec iv[3];
    708 	int veclen, left, tlen;
    709 	void *hdr;
    710 	size_t hdrlen;
    711 
    712 	switch (format) {
    713 	case AUDIO_FORMAT_DEFAULT:
    714 	case AUDIO_FORMAT_SUN:
    715 		if (write_header_sun(&hdr, &hdrlen, &left) != 0)
    716 			return;
    717 		break;
    718 	case AUDIO_FORMAT_WAV:
    719 		if (write_header_wav(&hdr, &hdrlen, &left) != 0)
    720 			return;
    721 		break;
    722 	case AUDIO_FORMAT_NONE:
    723 		return;
    724 	default:
    725 		errx(1, "unknown audio format");
    726 	}
    727 
    728 	veclen = 0;
    729 	tlen = 0;
    730 
    731 	if (hdrlen != 0) {
    732 		iv[veclen].iov_base = hdr;
    733 		iv[veclen].iov_len = hdrlen;
    734 		tlen += iv[veclen++].iov_len;
    735 	}
    736 	if (header_info) {
    737 		iv[veclen].iov_base = header_info;
    738 		iv[veclen].iov_len = (int)strlen(header_info) + 1;
    739 		tlen += iv[veclen++].iov_len;
    740 	}
    741 	if (left) {
    742 		iv[veclen].iov_base = default_info;
    743 		iv[veclen].iov_len = left;
    744 		tlen += iv[veclen++].iov_len;
    745 	}
    746 
    747 	if (tlen == 0)
    748 		return;
    749 
    750 	if (writev(outfd, iv, veclen) != tlen)
    751 		err(1, "could not write audio header");
    752 }
    753 
    754 void
    755 rewrite_header()
    756 {
    757 
    758 	/* can't do this here! */
    759 	if (outfd == STDOUT_FILENO)
    760 		return;
    761 
    762 	if (lseek(outfd, SEEK_SET, 0) < 0)
    763 		err(1, "could not seek to start of file for header rewrite");
    764 	write_header();
    765 }
    766 
    767 void
    768 usage()
    769 {
    770 
    771 	fprintf(stderr, "Usage: %s [-afhqV] [options] {files ...|-}\n",
    772 	    getprogname());
    773 	fprintf(stderr, "Options:\n\t"
    774 	    "-b balance (0-63)\n\t"
    775 	    "-c channels\n\t"
    776 	    "-d audio device\n\t"
    777 	    "-e encoding\n\t"
    778 	    "-F format\n\t"
    779 	    "-i header information\n\t"
    780 	    "-m monitor volume\n\t"
    781 	    "-P precision (4, 8, 16, 24, or 32 bits)\n\t"
    782 	    "-p input port\n\t"
    783 	    "-s sample rate\n\t"
    784 	    "-t recording time\n\t"
    785 	    "-v volume\n");
    786 	exit(EXIT_FAILURE);
    787 }
    788