Home | History | Annotate | Line # | Download | only in record
record.c revision 1.7
      1 /*	$NetBSD: record.c,v 1.7 1999/09/23 15:41:31 dmcmahill 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 
     52 audio_info_t info, oinfo;
     53 ssize_t	total_size = -1;
     54 char	*device;
     55 char	*ctldev;
     56 char	*header_info;
     57 char	default_info[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
     58 int	audiofd, ctlfd, outfd;
     59 int	qflag, aflag, fflag;
     60 int	verbose;
     61 int	monvol, omonvol;
     62 int	volume;
     63 int	balance;
     64 int	port;
     65 int	encoding;
     66 char	*encoding_str;
     67 int	precision;
     68 int	sample_rate;
     69 int	channels;
     70 struct timeval record_time;
     71 struct timeval start_time;	/* XXX because that's what gettimeofday returns */
     72 
     73 void usage __P((void));
     74 int main __P((int, char *[]));
     75 int timeleft __P((struct timeval *, struct timeval *));
     76 void cleanup __P((int)) __attribute__((__noreturn__));
     77 void write_header __P((void));
     78 void rewrite_header __P((void));
     79 
     80 int
     81 main(argc, argv)
     82 	int argc;
     83 	char *argv[];
     84 {
     85 	char	*buffer;
     86 	size_t	len, bufsize;
     87 	int	ch;
     88 
     89 	while ((ch = getopt(argc, argv, "ab:C:c:d:e:fhi:m:P:p:qt:s:Vv:")) != -1) {
     90 		switch (ch) {
     91 		case 'a':
     92 			aflag++;
     93 			break;
     94 		case 'b':
     95 			decode_int(optarg, &balance);
     96 			if (balance < 0 || balance > 63)
     97 				errx(1, "balance must be between 0 and 63\n");
     98 			break;
     99 		case 'C':
    100 			ctldev = optarg;
    101 			break;
    102 		case 'c':
    103 			decode_int(optarg, &channels);
    104 			if (channels < 0 || channels > 16)
    105 				errx(1, "channels must be between 0 and 16\n");
    106 			break;
    107 		case 'd':
    108 			device = optarg;
    109 			break;
    110 		case 'e':
    111 			encoding_str = optarg;
    112 			break;
    113 		case 'f':
    114 			fflag++;
    115 			break;
    116 		case 'i':
    117 			header_info = optarg;
    118 			break;
    119 		case 'm':
    120 			decode_int(optarg, &monvol);
    121 			if (monvol < 0 || monvol > 255)
    122 				errx(1, "monitor volume must be between 0 and 255\n");
    123 			break;
    124 		case 'P':
    125 			decode_int(optarg, &precision);
    126 			if (precision != 8 && precision != 16 &&
    127 			    precision != 24 && precision != 32)
    128 				errx(1, "precision must be between 8, 16, 24 or 32");
    129 			break;
    130 		case 'p':
    131 			len = strlen(optarg);
    132 
    133 			if (strncmp(optarg, "mic", len) == 0)
    134 				port |= AUDIO_MICROPHONE;
    135 			else if (strncmp(optarg, "cd", len) == 0 ||
    136 			           strncmp(optarg, "internal-cd", len) == 0)
    137 				port |= AUDIO_CD;
    138 			else if (strncmp(optarg, "line", len) == 0)
    139 				port |= AUDIO_LINE_IN;
    140 			else
    141 				errx(1,
    142 			    "port must be `cd', `internal-cd', `mic', or `line'");
    143 			break;
    144 		case 'q':
    145 			qflag++;
    146 			break;
    147 		case 's':
    148 			decode_int(optarg, &sample_rate);
    149 			if (sample_rate < 0 || sample_rate > 48000 * 2)	/* XXX */
    150 				errx(1, "sample rate must be between 0 and 96000\n");
    151 			break;
    152 		case 't':
    153 			decode_time(optarg, &record_time);
    154 			break;
    155 		case 'V':
    156 			verbose++;
    157 			break;
    158 		case 'v':
    159 			decode_int(optarg, &volume);
    160 			if (volume < 0 || volume > 255)
    161 				errx(1, "volume must be between 0 and 255\n");
    162 			break;
    163 		/* case 'h': */
    164 		default:
    165 			usage();
    166 			/* NOTREACHED */
    167 		}
    168 	}
    169 	argc -= optind;
    170 	argv += optind;
    171 
    172 	/*
    173 	 * open the audio device, and control device
    174 	 */
    175 	if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
    176 	    (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
    177 		device = _PATH_AUDIO;
    178 	if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == NULL)
    179 		ctldev = _PATH_AUDIOCTL;
    180 
    181 	audiofd = open(device, O_RDONLY);
    182 	if (audiofd < 0)
    183 		err(1, "failed to open %s", device);
    184 	ctlfd = open(ctldev, O_RDWR);
    185 	if (ctlfd < 0)
    186 		err(1, "failed to open %s", ctldev);
    187 
    188 	/*
    189 	 * work out the buffer size to use, and allocate it.  also work out
    190 	 * what the old monitor gain value is, so that we can reset it later.
    191 	 */
    192 	if (ioctl(ctlfd, AUDIO_GETINFO, &oinfo) < 0)
    193 		err(1, "failed to get audio info");
    194 	bufsize = oinfo.record.buffer_size;
    195 	if (bufsize < 32 * 1024)
    196 		bufsize = 32 * 1024;
    197 	omonvol = oinfo.monitor_gain;
    198 
    199 	buffer = malloc(bufsize);
    200 	if (buffer == NULL)
    201 		err(1, "couldn't malloc buffer of %d size", (int)bufsize);
    202 
    203 	/*
    204 	 * open the output file
    205 	 */
    206 	if (argc != 1)
    207 		usage();
    208 	if (argv[0][0] != '-' && argv[0][1] != '\0') {
    209 		outfd = open(*argv, O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY, 0666);
    210 		if (outfd < 0)
    211 			err(1, "could not open %s", *argv);
    212 	} else
    213 		outfd = STDOUT_FILENO;
    214 
    215 	/*
    216 	 * set up audio device for recording with the speified parameters
    217 	 */
    218 	AUDIO_INITINFO(&info);
    219 
    220 	/*
    221 	 * for these, get the current values for stuffing into the header
    222 	 **/
    223 	if (sample_rate)
    224 		info.record.sample_rate = sample_rate;
    225 	else
    226 		sample_rate = oinfo.record.sample_rate;
    227 	if (channels)
    228 		info.record.channels = channels;
    229 	else
    230 		channels = oinfo.record.channels;
    231 
    232 	if (encoding_str) {
    233 		encoding = audio_enc_to_val(encoding_str);
    234 		if (encoding == -1)
    235 			errx(1, "unknown encoding, bailing...");
    236 	}
    237 
    238 	if (precision)
    239 		info.record.precision = precision;
    240 	if (encoding)
    241 		info.record.encoding = encoding;
    242 	if (volume)
    243 		info.record.gain = volume;
    244 	if (port)
    245 		info.record.port = port;
    246 	if (balance)
    247 		info.record.balance = (u_char)balance;
    248 	if (monvol)
    249 		info.monitor_gain = monvol;
    250 
    251 	info.mode = AUMODE_RECORD;
    252 	if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
    253 		err(1, "failed to reset audio info");
    254 
    255 	signal(SIGINT, cleanup);
    256 	write_header();
    257 	total_size = 0;
    258 
    259 	(void)gettimeofday(&start_time, NULL);
    260 	while (timeleft(&start_time, &record_time)) {
    261 		if (read(audiofd, buffer, bufsize) != bufsize)
    262 			err(1, "read failed");
    263 		if (write(outfd, buffer, bufsize) != bufsize)
    264 			err(1, "write failed");
    265 		total_size += bufsize;
    266 	}
    267 	cleanup(0);
    268 }
    269 
    270 int
    271 timeleft(start_tvp, record_tvp)
    272 	struct timeval *start_tvp;
    273 	struct timeval *record_tvp;
    274 {
    275 	struct timeval now, diff;
    276 
    277 	(void)gettimeofday(&now, NULL);
    278 	timersub(&now, start_tvp, &diff);
    279 	timersub(record_tvp, &diff, &now);
    280 
    281 	return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
    282 }
    283 
    284 void
    285 cleanup(signo)
    286 	int signo;
    287 {
    288 
    289 	close(audiofd);
    290 	rewrite_header();
    291 	close(outfd);
    292 	if (omonvol) {
    293 		AUDIO_INITINFO(&info);
    294 		info.monitor_gain = omonvol;
    295 		if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
    296 			err(1, "failed to reset audio info");
    297 	}
    298 	close(ctlfd);
    299 	exit(0);
    300 }
    301 
    302 sun_audioheader auh;
    303 
    304 void
    305 write_header()
    306 {
    307 	struct iovec iv[3];
    308 	int veclen = 0, left, tlen = 0;
    309 
    310 	auh.magic = htonl(AUDIO_FILE_MAGIC);
    311 	if (outfd == STDOUT_FILENO)
    312 		auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
    313 	else
    314 		auh.data_size = htonl(total_size);
    315 	auh.encoding = htonl(encoding);
    316 	auh.sample_rate = htonl(sample_rate);
    317 	auh.channels = htonl(channels);
    318 	if (header_info) {
    319 		int 	len, infolen;
    320 
    321 		infolen = ((len = strlen(header_info)) + 7) & 0xfffffff8;
    322 		left = infolen - len;
    323 		auh.hdr_size = htonl(sizeof(auh) + infolen);
    324 	} else {
    325 		left = sizeof(default_info);
    326 		auh.hdr_size = htonl(sizeof(auh) + left);
    327 	}
    328 
    329 	iv[veclen].iov_base = &auh;
    330 	iv[veclen].iov_len = sizeof(auh);
    331 	tlen = iv[veclen++].iov_len;
    332 	if (header_info) {
    333 		iv[veclen].iov_base = header_info;
    334 		iv[veclen].iov_len = (int)strlen(header_info);
    335 		tlen += iv[veclen++].iov_len;
    336 	}
    337 	if (left) {
    338 		iv[veclen].iov_base = default_info;
    339 		iv[veclen].iov_len = left;
    340 		tlen += iv[veclen++].iov_len;
    341 	}
    342 
    343 	if (writev(outfd, iv, veclen) != tlen)
    344 		err(1, "could not write audio header");
    345 }
    346 
    347 void
    348 rewrite_header()
    349 {
    350 
    351 	/* can't do this here! */
    352 	if (outfd == STDOUT_FILENO)
    353 		return;
    354 
    355 	if (lseek(outfd, SEEK_SET, 0) < 0)
    356 		err(1, "could not seek to start of file for header rewrite");
    357 	write_header();
    358 }
    359 
    360 void
    361 usage()
    362 {
    363 	extern char *__progname;
    364 
    365 	fprintf(stderr, "Usage: %s [-afhqV] [options] {files ...|-}\n", __progname);
    366 	fprintf(stderr, "Options:\n\t"
    367 	    "-C audio control device\n\t"
    368 	    "-b balance (0-63)\n\t"
    369 	    "-c channels\n\t"
    370 	    "-d audio device\n\t"
    371 	    "-e encoding\n\t"
    372 	    "-i header information\n\t"
    373 	    "-m monitor volume\n\t"
    374 	    "-P precision bits (8, 16, 24 or 32)\n\t"
    375 	    "-p input port\n\t"
    376 	    "-s sample rate\n\t"
    377 	    "-t recording time\n\t"
    378 	    "-v volume\n");
    379 	exit(0);
    380 }
    381