record.c revision 1.9 1 /* $NetBSD: record.c,v 1.9 1999/11/08 10:21:21 kleink 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, no_time_limit = 1;
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 no_time_limit = 0;
154 decode_time(optarg, &record_time);
155 break;
156 case 'V':
157 verbose++;
158 break;
159 case 'v':
160 decode_int(optarg, &volume);
161 if (volume < 0 || volume > 255)
162 errx(1, "volume must be between 0 and 255\n");
163 break;
164 /* case 'h': */
165 default:
166 usage();
167 /* NOTREACHED */
168 }
169 }
170 argc -= optind;
171 argv += optind;
172
173 /*
174 * open the audio device, and control device
175 */
176 if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
177 (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
178 device = _PATH_AUDIO;
179 if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == NULL)
180 ctldev = _PATH_AUDIOCTL;
181
182 audiofd = open(device, O_RDONLY);
183 if (audiofd < 0)
184 err(1, "failed to open %s", device);
185 ctlfd = open(ctldev, O_RDWR);
186 if (ctlfd < 0)
187 err(1, "failed to open %s", ctldev);
188
189 /*
190 * work out the buffer size to use, and allocate it. also work out
191 * what the old monitor gain value is, so that we can reset it later.
192 */
193 if (ioctl(ctlfd, AUDIO_GETINFO, &oinfo) < 0)
194 err(1, "failed to get audio info");
195 bufsize = oinfo.record.buffer_size;
196 if (bufsize < 32 * 1024)
197 bufsize = 32 * 1024;
198 omonvol = oinfo.monitor_gain;
199
200 buffer = malloc(bufsize);
201 if (buffer == NULL)
202 err(1, "couldn't malloc buffer of %d size", (int)bufsize);
203
204 /*
205 * open the output file
206 */
207 if (argc != 1)
208 usage();
209 if (argv[0][0] != '-' && argv[0][1] != '\0') {
210 outfd = open(*argv, O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY, 0666);
211 if (outfd < 0)
212 err(1, "could not open %s", *argv);
213 } else
214 outfd = STDOUT_FILENO;
215
216 /*
217 * set up audio device for recording with the speified parameters
218 */
219 AUDIO_INITINFO(&info);
220
221 /*
222 * for these, get the current values for stuffing into the header
223 **/
224 if (sample_rate)
225 info.record.sample_rate = sample_rate;
226 else
227 sample_rate = oinfo.record.sample_rate;
228 if (channels)
229 info.record.channels = channels;
230 else
231 channels = oinfo.record.channels;
232
233 if (encoding_str) {
234 encoding = audio_enc_to_val(encoding_str);
235 if (encoding == -1)
236 errx(1, "unknown encoding, bailing...");
237 }
238
239 if (precision)
240 info.record.precision = precision;
241 if (encoding)
242 info.record.encoding = encoding;
243 if (volume)
244 info.record.gain = volume;
245 if (port)
246 info.record.port = port;
247 if (balance)
248 info.record.balance = (u_char)balance;
249 if (monvol)
250 info.monitor_gain = monvol;
251
252 info.mode = AUMODE_RECORD;
253 if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
254 err(1, "failed to reset audio info");
255
256 signal(SIGINT, cleanup);
257 write_header();
258 total_size = 0;
259
260 (void)gettimeofday(&start_time, NULL);
261 while (no_time_limit || timeleft(&start_time, &record_time)) {
262 if (read(audiofd, buffer, bufsize) != bufsize)
263 err(1, "read failed");
264 if (write(outfd, buffer, bufsize) != bufsize)
265 err(1, "write failed");
266 total_size += bufsize;
267 }
268 cleanup(0);
269 }
270
271 int
272 timeleft(start_tvp, record_tvp)
273 struct timeval *start_tvp;
274 struct timeval *record_tvp;
275 {
276 struct timeval now, diff;
277
278 (void)gettimeofday(&now, NULL);
279 timersub(&now, start_tvp, &diff);
280 timersub(record_tvp, &diff, &now);
281
282 return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
283 }
284
285 void
286 cleanup(signo)
287 int signo;
288 {
289
290 close(audiofd);
291 rewrite_header();
292 close(outfd);
293 if (omonvol) {
294 AUDIO_INITINFO(&info);
295 info.monitor_gain = omonvol;
296 if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
297 err(1, "failed to reset audio info");
298 }
299 close(ctlfd);
300 exit(0);
301 }
302
303 void
304 write_header()
305 {
306 sun_audioheader auh;
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(EXIT_FAILURE);
380 }
381