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