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