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