Home | History | Annotate | Line # | Download | only in record
record.c revision 1.5
      1 /*	$NetBSD: record.c,v 1.5 1999/06/19 05:20:17 itohy 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)
    176 		device = _PATH_AUDIO;
    177 	if (ctldev == NULL)
    178 		ctldev = _PATH_AUDIOCTL;
    179 
    180 	audiofd = open(device, O_RDONLY);
    181 	if (audiofd < 0)
    182 		err(1, "failed to open %s", device);
    183 	ctlfd = open(ctldev, O_RDWR);
    184 	if (ctlfd < 0)
    185 		err(1, "failed to open %s", ctldev);
    186 
    187 	/*
    188 	 * work out the buffer size to use, and allocate it.  also work out
    189 	 * what the old monitor gain value is, so that we can reset it later.
    190 	 */
    191 	if (ioctl(ctlfd, AUDIO_GETINFO, &oinfo) < 0)
    192 		err(1, "failed to get audio info");
    193 	bufsize = oinfo.record.buffer_size;
    194 	if (bufsize < 32 * 1024)
    195 		bufsize = 32 * 1024;
    196 	omonvol = oinfo.monitor_gain;
    197 
    198 	buffer = malloc(bufsize);
    199 	if (buffer == NULL)
    200 		err(1, "couldn't malloc buffer of %d size", (int)bufsize);
    201 
    202 	/*
    203 	 * open the output file
    204 	 */
    205 	if (argc != 1)
    206 		usage();
    207 	if (argv[0][0] != '-' && argv[0][1] != '\0') {
    208 		outfd = open(*argv, O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY, 0666);
    209 		if (outfd < 0)
    210 			err(1, "could not open %s", *argv);
    211 	} else
    212 		outfd = STDOUT_FILENO;
    213 
    214 	/*
    215 	 * set up audio device for recording with the speified parameters
    216 	 */
    217 	AUDIO_INITINFO(&info);
    218 
    219 	/*
    220 	 * for these, get the current values for stuffing into the header
    221 	 **/
    222 	if (sample_rate)
    223 		info.record.sample_rate = sample_rate;
    224 	else
    225 		sample_rate = oinfo.record.sample_rate;
    226 	if (channels)
    227 		info.record.channels = channels;
    228 	else
    229 		channels = oinfo.record.channels;
    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 
    237 	if (precision)
    238 		info.record.precision = precision;
    239 	if (encoding)
    240 		info.record.encoding = encoding;
    241 	if (volume)
    242 		info.record.gain = volume;
    243 	if (port)
    244 		info.record.port = port;
    245 	if (balance)
    246 		info.record.balance = (u_char)balance;
    247 	if (monvol)
    248 		info.monitor_gain = monvol;
    249 
    250 	info.mode = AUMODE_RECORD;
    251 	if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
    252 		err(1, "failed to reset audio info");
    253 
    254 	signal(SIGINT, cleanup);
    255 	write_header();
    256 	total_size = 0;
    257 
    258 	(void)gettimeofday(&start_time, NULL);
    259 	while (timeleft(&start_time, &record_time)) {
    260 		if (read(audiofd, buffer, bufsize) != bufsize)
    261 			err(1, "read failed");
    262 		if (write(outfd, buffer, bufsize) != bufsize)
    263 			err(1, "write failed");
    264 		total_size += bufsize;
    265 	}
    266 	cleanup(0);
    267 }
    268 
    269 int
    270 timeleft(start_tvp, record_tvp)
    271 	struct timeval *start_tvp;
    272 	struct timeval *record_tvp;
    273 {
    274 	struct timeval now, diff;
    275 
    276 	(void)gettimeofday(&now, NULL);
    277 	timersub(&diff, &now, start_tvp);
    278 	timersub(&now, record_tvp, &diff);
    279 
    280 	return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
    281 }
    282 
    283 void
    284 cleanup(signo)
    285 	int signo;
    286 {
    287 
    288 	close(audiofd);
    289 	rewrite_header();
    290 	close(outfd);
    291 	if (omonvol) {
    292 		AUDIO_INITINFO(&info);
    293 		info.monitor_gain = omonvol;
    294 		if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
    295 			err(1, "failed to reset audio info");
    296 	}
    297 	close(ctlfd);
    298 	exit(0);
    299 }
    300 
    301 sun_audioheader auh;
    302 
    303 void
    304 write_header()
    305 {
    306 	struct iovec iv[3];
    307 	int veclen = 0, left, tlen = 0;
    308 
    309 	auh.magic = htonl(AUDIO_FILE_MAGIC);
    310 	if (outfd == STDOUT_FILENO)
    311 		auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
    312 	else
    313 		auh.data_size = htonl(total_size);
    314 	auh.encoding = htonl(encoding);
    315 	auh.sample_rate = htonl(sample_rate);
    316 	auh.channels = htonl(channels);
    317 	if (header_info) {
    318 		int 	len, infolen;
    319 
    320 		infolen = ((len = strlen(header_info)) + 7) & 0xfffffff8;
    321 		left = infolen - len;
    322 		auh.hdr_size = htonl(sizeof(auh) + infolen);
    323 	} else {
    324 		left = sizeof(default_info);
    325 		auh.hdr_size = htonl(sizeof(auh) + left);
    326 	}
    327 
    328 	iv[veclen].iov_base = &auh;
    329 	iv[veclen].iov_len = sizeof(auh);
    330 	tlen = iv[veclen++].iov_len;
    331 	if (header_info) {
    332 		iv[veclen].iov_base = header_info;
    333 		iv[veclen].iov_len = (int)strlen(header_info);
    334 		tlen += iv[veclen++].iov_len;
    335 	}
    336 	if (left) {
    337 		iv[veclen].iov_base = default_info;
    338 		iv[veclen].iov_len = left;
    339 		tlen += iv[veclen++].iov_len;
    340 	}
    341 
    342 	if (writev(outfd, iv, veclen) != tlen)
    343 		err(1, "could not write audio header");
    344 }
    345 
    346 void
    347 rewrite_header()
    348 {
    349 
    350 	/* can't do this here! */
    351 	if (outfd == STDOUT_FILENO)
    352 		return;
    353 
    354 	if (lseek(outfd, SEEK_SET, 0) < 0)
    355 		err(1, "could not seek to start of file for header rewrite");
    356 	write_header();
    357 }
    358 
    359 void
    360 usage()
    361 {
    362 	extern char *__progname;
    363 
    364 	fprintf(stderr, "Usage: %s [-afhqV] [options] {files ...|-}\n", __progname);
    365 	fprintf(stderr, "Options:\n\t"
    366 	    "-C audio control device\n\t"
    367 	    "-b balance (0-63)\n\t"
    368 	    "-c channels\n\t"
    369 	    "-d audio device\n\t"
    370 	    "-e encoding\n\t"
    371 	    "-i header information\n\t"
    372 	    "-m monitor volume\n\t"
    373 	    "-P precision bits (8, 16, 24 or 32)\n\t"
    374 	    "-p input port\n\t"
    375 	    "-s sample rate\n\t"
    376 	    "-t recording time\n\t"
    377 	    "-v volume\n");
    378 	exit(0);
    379 }
    380