Home | History | Annotate | Line # | Download | only in record
record.c revision 1.25
      1 /*	$NetBSD: record.c,v 1.25 2002/02/10 13:16:08 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 	case AUDIO_ENCODING_ALAW:
    488 		fmttag = WAVE_FORMAT_ALAW;
    489 		fmtsz = 18;
    490 		align = channels;
    491 		break;
    492 	/*
    493 	 * we could try to support RIFX but it seems to be more portable
    494 	 * to output little-endian data for WAV files.
    495 	 */
    496 	case AUDIO_ENCODING_ULINEAR_BE:
    497 		if (bps == 16)
    498 			conv_func = change_sign16_swap_bytes_be;
    499 		else if (bps == 32)
    500 			conv_func = change_sign32_swap_bytes_be;
    501 		goto fmt_pcm;
    502 	case AUDIO_ENCODING_SLINEAR_BE:
    503 		if (bps == 16)
    504 			conv_func = swap_bytes;
    505 		else if (bps == 32)
    506 			conv_func = swap_bytes32;
    507 		goto fmt_pcm;
    508 	case AUDIO_ENCODING_ULINEAR_LE:
    509 		if (bps == 16)
    510 			conv_func = change_sign16_le;
    511 		else if (bps == 32)
    512 			conv_func = change_sign32_le;
    513 		/* FALLTHROUGH */
    514 	case AUDIO_ENCODING_SLINEAR_LE:
    515 	case AUDIO_ENCODING_PCM16:
    516 fmt_pcm:
    517 		fmttag = WAVE_FORMAT_PCM;
    518 		fmtsz = 16;
    519 		align = channels * (bps / 8);
    520 		break;
    521 	default:
    522 		{
    523 			static int warned = 0;
    524 
    525 			if (warned == 0) {
    526 				warnx("can not support encoding of %s\n", wav_enc_from_val(encoding));
    527 				warned = 1;
    528 			}
    529 		}
    530 		return (-1);
    531 	}
    532 
    533 	nchan = channels;
    534 	sps = sample_rate;
    535 
    536 	/* data length */
    537 	if (outfd == STDOUT_FILENO)
    538 		datalen = 0;
    539 	else
    540 		datalen = total_size;
    541 
    542 	/* file length */
    543 	filelen = 4 + (8 + fmtsz) + (8 + datalen);
    544 	if (fmttag != WAVE_FORMAT_PCM)
    545 		filelen += 8 + factsz;
    546 
    547 	abps = (double)align*sample_rate / (double)1 + 0.5;
    548 
    549 	nsample = (datalen / bps) / sample_rate;
    550 
    551 	/*
    552 	 * now we've calculated the info, write it out!
    553 	 */
    554 #define put32(x) do { \
    555 	u_int32_t _f; \
    556 	putle32(_f, (x)); \
    557 	memcpy(p, &_f, 4); \
    558 } while (0)
    559 #define put16(x) do { \
    560 	u_int16_t _f; \
    561 	putle16(_f, (x)); \
    562 	memcpy(p, &_f, 2); \
    563 } while (0)
    564 	memcpy(p, riff, 4);
    565 	p += 4;				/* 4 */
    566 	put32(filelen);
    567 	p += 4;				/* 8 */
    568 	memcpy(p, wavefmt, 8);
    569 	p += 8;				/* 16 */
    570 	put32(fmtsz);
    571 	p += 4;				/* 20 */
    572 	put16(fmttag);
    573 	p += 2;				/* 22 */
    574 	put16(nchan);
    575 	p += 2;				/* 24 */
    576 	put32(sps);
    577 	p += 4;				/* 28 */
    578 	put32(abps);
    579 	p += 4;				/* 32 */
    580 	put16(align);
    581 	p += 2;				/* 34 */
    582 	put16(bps);
    583 	p += 2;				/* 36 */
    584 	/* NON PCM formats have an extended chunk; write it */
    585 	if (fmttag != WAVE_FORMAT_PCM) {
    586 		put16(extln);
    587 		p += 2;			/* 38 */
    588 		memcpy(p, fact, 4);
    589 		p += 4;			/* 42 */
    590 		put32(factsz);
    591 		p += 4;			/* 46 */
    592 		put32(nsample);
    593 		p += 4;			/* 50 */
    594 	}
    595 	memcpy(p, data, 4);
    596 	p += 4;				/* 40/54 */
    597 	put32(datalen);
    598 	p += 4;				/* 44/58 */
    599 #undef put32
    600 #undef put16
    601 
    602 	*hdrp = wavheaderbuf;
    603 	*lenp = (p - wavheaderbuf);
    604 
    605 	return 0;
    606 }
    607 
    608 void
    609 write_header()
    610 {
    611 	struct iovec iv[3];
    612 	int veclen, left, tlen;
    613 	void *hdr;
    614 	size_t hdrlen;
    615 
    616 	switch (format) {
    617 	case AUDIO_FORMAT_SUN:
    618 		if (write_header_sun(&hdr, &hdrlen, &left) != 0)
    619 			return;
    620 		break;
    621 	case AUDIO_FORMAT_WAV:
    622 		if (write_header_wav(&hdr, &hdrlen, &left) != 0)
    623 			return;
    624 		break;
    625 	case AUDIO_FORMAT_NONE:
    626 		return;
    627 	default:
    628 		errx(1, "unknown audio format");
    629 	}
    630 
    631 	veclen = 0;
    632 	tlen = 0;
    633 
    634 	if (hdrlen != 0) {
    635 		iv[veclen].iov_base = hdr;
    636 		iv[veclen].iov_len = hdrlen;
    637 		tlen += iv[veclen++].iov_len;
    638 	}
    639 	if (header_info) {
    640 		iv[veclen].iov_base = header_info;
    641 		iv[veclen].iov_len = (int)strlen(header_info) + 1;
    642 		tlen += iv[veclen++].iov_len;
    643 	}
    644 	if (left) {
    645 		iv[veclen].iov_base = default_info;
    646 		iv[veclen].iov_len = left;
    647 		tlen += iv[veclen++].iov_len;
    648 	}
    649 
    650 	if (tlen == 0)
    651 		return;
    652 
    653 	if (writev(outfd, iv, veclen) != tlen)
    654 		err(1, "could not write audio header");
    655 }
    656 
    657 void
    658 rewrite_header()
    659 {
    660 
    661 	/* can't do this here! */
    662 	if (outfd == STDOUT_FILENO)
    663 		return;
    664 
    665 	if (lseek(outfd, SEEK_SET, 0) < 0)
    666 		err(1, "could not seek to start of file for header rewrite");
    667 	write_header();
    668 }
    669 
    670 void
    671 usage()
    672 {
    673 
    674 	fprintf(stderr, "Usage: %s [-afhqV] [options] {files ...|-}\n",
    675 	    getprogname());
    676 	fprintf(stderr, "Options:\n\t"
    677 	    "-F format\n\t"
    678 	    "-b balance (0-63)\n\t"
    679 	    "-c channels\n\t"
    680 	    "-d audio device\n\t"
    681 	    "-e encoding\n\t"
    682 	    "-i header information\n\t"
    683 	    "-m monitor volume\n\t"
    684 	    "-P precision bits (4, 8, 16, 24 or 32)\n\t"
    685 	    "-p input port\n\t"
    686 	    "-s sample rate\n\t"
    687 	    "-t recording time\n\t"
    688 	    "-v volume\n");
    689 	exit(EXIT_FAILURE);
    690 }
    691