Home | History | Annotate | Line # | Download | only in record
record.c revision 1.2
      1 /*	$NetBSD: record.c,v 1.2 1999/03/26 15:46:22 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 
     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 			device = 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 #if 0
    231 	/* XXX ??? */
    232 	if (precision)
    233 		info.record.precision = precision;
    234 	else
    235 		precision = oinfo.record.precision;
    236 #endif
    237 	if (encoding) {
    238 		if (audio_parse_encoding(encoding_str, ctlfd, &info.record.encoding, &info.record.precision))
    239 			errx(1, "unknown encoding, bailing...");
    240 	} else {
    241 		encoding = oinfo.record.encoding;
    242 		precision = oinfo.record.precision;
    243 	}
    244 
    245 	if (volume)
    246 		info.record.gain = volume;
    247 	if (port)
    248 		info.record.port = port;
    249 	if (balance)
    250 		info.record.balance = (u_char)balance;
    251 	if (monvol)
    252 		info.monitor_gain = monvol;
    253 
    254 	info.mode = AUMODE_RECORD;
    255 	if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
    256 		err(1, "failed to reset audio info");
    257 
    258 	signal(SIGINT, cleanup);
    259 	write_header();
    260 	total_size = 0;
    261 
    262 	(void)gettimeofday(&start_time, NULL);
    263 	while (timeleft(&start_time, &record_time)) {
    264 		if (read(audiofd, buffer, bufsize) != bufsize)
    265 			err(1, "read failed");
    266 		if (write(outfd, buffer, bufsize) != bufsize)
    267 			err(1, "write failed");
    268 		total_size += bufsize;
    269 	}
    270 	cleanup(0);
    271 }
    272 
    273 int
    274 timeleft(start_tvp, record_tvp)
    275 	struct timeval *start_tvp;
    276 	struct timeval *record_tvp;
    277 {
    278 	struct timeval now, diff;
    279 
    280 	(void)gettimeofday(&now, NULL);
    281 	timersub(&diff, &now, start_tvp);
    282 	timersub(&now, record_tvp, &diff);
    283 
    284 	return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
    285 }
    286 
    287 void
    288 cleanup(signo)
    289 	int signo;
    290 {
    291 
    292 	close(audiofd);
    293 	rewrite_header();
    294 	close(outfd);
    295 	if (omonvol) {
    296 		AUDIO_INITINFO(&info);
    297 		info.monitor_gain = omonvol;
    298 		if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
    299 			err(1, "failed to reset audio info");
    300 	}
    301 	close(ctlfd);
    302 	exit(0);
    303 }
    304 
    305 sun_audioheader auh;
    306 
    307 void
    308 write_header()
    309 {
    310 	struct iovec iv[3];
    311 	int veclen = 0, left, tlen = 0;
    312 
    313 	auh.magic = htonl(AUDIO_FILE_MAGIC);
    314 	if (outfd == STDOUT_FILENO)
    315 		auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
    316 	else
    317 		auh.data_size = htonl(total_size);
    318 	auh.encoding = htonl(encoding);
    319 	auh.sample_rate = htonl(sample_rate);
    320 	auh.channels = htonl(channels);
    321 	if (header_info) {
    322 		int 	len, infolen;
    323 
    324 		infolen = ((len = strlen(header_info)) + 7) & 0xfffffff8;
    325 		left = infolen - len;
    326 		auh.hdr_size = htonl(sizeof(auh) + infolen);
    327 	} else {
    328 		left = sizeof(default_info);
    329 		auh.hdr_size = htonl(sizeof(auh) + left);
    330 	}
    331 
    332 	iv[veclen].iov_base = &auh;
    333 	iv[veclen].iov_len = sizeof(auh);
    334 	tlen = iv[veclen++].iov_len;
    335 	if (header_info) {
    336 		iv[veclen].iov_base = header_info;
    337 		iv[veclen].iov_len = (int)strlen(header_info);
    338 		tlen += iv[veclen++].iov_len;
    339 	}
    340 	if (left) {
    341 		iv[veclen].iov_base = default_info;
    342 		iv[veclen].iov_len = left;
    343 		tlen += iv[veclen++].iov_len;
    344 	}
    345 
    346 	if (writev(outfd, iv, veclen) != tlen)
    347 		err(1, "could not write audio header");
    348 }
    349 
    350 void
    351 rewrite_header()
    352 {
    353 
    354 	/* can't do this here! */
    355 	if (outfd == STDOUT_FILENO)
    356 		return;
    357 
    358 	if (lseek(outfd, SEEK_SET, 0) < 0)
    359 		err(1, "could not seek to start of file for header rewrite");
    360 	write_header();
    361 }
    362 
    363 void
    364 usage()
    365 {
    366 	extern char *__progname;
    367 
    368 	fprintf(stderr, "Usage: %s [-iaqVh] [-v vol] [-b bal] [-c channels] [-p port] [-d dev] [-C ctl]\n\t{file|-}\n", __progname);
    369 	exit(0);
    370 }
    371