record.c revision 1.10.4.1 1 1.10.4.1 he /* $NetBSD: record.c,v 1.10.4.1 2002/01/29 23:18:12 he Exp $ */
2 1.2 mrg
3 1.2 mrg /*
4 1.2 mrg * Copyright (c) 1999 Matthew R. Green
5 1.2 mrg * All rights reserved.
6 1.2 mrg *
7 1.2 mrg * Redistribution and use in source and binary forms, with or without
8 1.2 mrg * modification, are permitted provided that the following conditions
9 1.2 mrg * are met:
10 1.2 mrg * 1. Redistributions of source code must retain the above copyright
11 1.2 mrg * notice, this list of conditions and the following disclaimer.
12 1.2 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 mrg * notice, this list of conditions and the following disclaimer in the
14 1.2 mrg * documentation and/or other materials provided with the distribution.
15 1.2 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.2 mrg * derived from this software without specific prior written permission.
17 1.2 mrg *
18 1.2 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.2 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.2 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.2 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.2 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.2 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.2 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.2 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.2 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.2 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.2 mrg * SUCH DAMAGE.
29 1.2 mrg */
30 1.2 mrg
31 1.1 mrg /*
32 1.1 mrg * SunOS compatible audiorecord(1)
33 1.1 mrg */
34 1.1 mrg
35 1.1 mrg #include <sys/types.h>
36 1.1 mrg #include <sys/audioio.h>
37 1.1 mrg #include <sys/ioctl.h>
38 1.1 mrg #include <sys/time.h>
39 1.1 mrg #include <sys/uio.h>
40 1.1 mrg
41 1.1 mrg #include <err.h>
42 1.1 mrg #include <fcntl.h>
43 1.1 mrg #include <paths.h>
44 1.1 mrg #include <signal.h>
45 1.1 mrg #include <stdio.h>
46 1.1 mrg #include <stdlib.h>
47 1.1 mrg #include <string.h>
48 1.1 mrg #include <unistd.h>
49 1.1 mrg
50 1.1 mrg #include "libaudio.h"
51 1.10.4.1 he #include "auconv.h"
52 1.1 mrg
53 1.1 mrg audio_info_t info, oinfo;
54 1.1 mrg ssize_t total_size = -1;
55 1.10.4.1 he const char *device;
56 1.10.4.1 he const char *ctldev;
57 1.10.4.1 he int format = AUDIO_FORMAT_SUN;
58 1.1 mrg char *header_info;
59 1.1 mrg char default_info[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
60 1.1 mrg int audiofd, ctlfd, outfd;
61 1.1 mrg int qflag, aflag, fflag;
62 1.1 mrg int verbose;
63 1.10.4.1 he int monitor_gain, omonitor_gain;
64 1.10.4.1 he int gain;
65 1.1 mrg int balance;
66 1.1 mrg int port;
67 1.1 mrg int encoding;
68 1.1 mrg char *encoding_str;
69 1.1 mrg int precision;
70 1.1 mrg int sample_rate;
71 1.1 mrg int channels;
72 1.1 mrg struct timeval record_time;
73 1.1 mrg struct timeval start_time; /* XXX because that's what gettimeofday returns */
74 1.1 mrg
75 1.10.4.1 he void (*conv_func) (u_char *, size_t);
76 1.10.4.1 he
77 1.10.4.1 he void usage (void);
78 1.10.4.1 he int main (int, char *[]);
79 1.10.4.1 he int timeleft (struct timeval *, struct timeval *);
80 1.10.4.1 he void cleanup (int) __attribute__((__noreturn__));
81 1.10.4.1 he int write_header_sun (void **, size_t *, int *);
82 1.10.4.1 he int write_header_wav (void **, size_t *, int *);
83 1.10.4.1 he void write_header (void);
84 1.10.4.1 he void rewrite_header (void);
85 1.10.4.1 he
86 1.10.4.1 he extern char *__progname;
87 1.1 mrg
88 1.1 mrg int
89 1.1 mrg main(argc, argv)
90 1.1 mrg int argc;
91 1.1 mrg char *argv[];
92 1.1 mrg {
93 1.1 mrg char *buffer;
94 1.1 mrg size_t len, bufsize;
95 1.8 mrg int ch, no_time_limit = 1;
96 1.1 mrg
97 1.10.4.1 he while ((ch = getopt(argc, argv, "ab:C:F:c:d:e:fhi:m:P:p:qt:s:Vv:")) != -1) {
98 1.1 mrg switch (ch) {
99 1.1 mrg case 'a':
100 1.1 mrg aflag++;
101 1.1 mrg break;
102 1.1 mrg case 'b':
103 1.1 mrg decode_int(optarg, &balance);
104 1.1 mrg if (balance < 0 || balance > 63)
105 1.1 mrg errx(1, "balance must be between 0 and 63\n");
106 1.1 mrg break;
107 1.1 mrg case 'C':
108 1.5 itohy ctldev = optarg;
109 1.1 mrg break;
110 1.10.4.1 he case 'F':
111 1.10.4.1 he format = audio_format_from_str(optarg);
112 1.10.4.1 he if (format < 0)
113 1.10.4.1 he errx(1, "Unknown audio format; "
114 1.10.4.1 he "supported formats: \"sun\" and \"wav\"");
115 1.10.4.1 he break;
116 1.1 mrg case 'c':
117 1.1 mrg decode_int(optarg, &channels);
118 1.1 mrg if (channels < 0 || channels > 16)
119 1.1 mrg errx(1, "channels must be between 0 and 16\n");
120 1.1 mrg break;
121 1.1 mrg case 'd':
122 1.1 mrg device = optarg;
123 1.1 mrg break;
124 1.1 mrg case 'e':
125 1.1 mrg encoding_str = optarg;
126 1.1 mrg break;
127 1.1 mrg case 'f':
128 1.1 mrg fflag++;
129 1.1 mrg break;
130 1.1 mrg case 'i':
131 1.1 mrg header_info = optarg;
132 1.1 mrg break;
133 1.1 mrg case 'm':
134 1.10.4.1 he decode_int(optarg, &monitor_gain);
135 1.10.4.1 he if (monitor_gain < 0 || monitor_gain > 255)
136 1.1 mrg errx(1, "monitor volume must be between 0 and 255\n");
137 1.1 mrg break;
138 1.1 mrg case 'P':
139 1.1 mrg decode_int(optarg, &precision);
140 1.10.4.1 he if (precision != 4 && precision != 8 &&
141 1.10.4.1 he precision != 16 && precision != 24 &&
142 1.10.4.1 he precision != 32)
143 1.10.4.1 he errx(1, "precision must be between 4, 8, 16, 24 or 32");
144 1.1 mrg break;
145 1.1 mrg case 'p':
146 1.1 mrg len = strlen(optarg);
147 1.1 mrg
148 1.1 mrg if (strncmp(optarg, "mic", len) == 0)
149 1.1 mrg port |= AUDIO_MICROPHONE;
150 1.1 mrg else if (strncmp(optarg, "cd", len) == 0 ||
151 1.1 mrg strncmp(optarg, "internal-cd", len) == 0)
152 1.1 mrg port |= AUDIO_CD;
153 1.1 mrg else if (strncmp(optarg, "line", len) == 0)
154 1.1 mrg port |= AUDIO_LINE_IN;
155 1.1 mrg else
156 1.1 mrg errx(1,
157 1.1 mrg "port must be `cd', `internal-cd', `mic', or `line'");
158 1.1 mrg break;
159 1.1 mrg case 'q':
160 1.1 mrg qflag++;
161 1.1 mrg break;
162 1.1 mrg case 's':
163 1.1 mrg decode_int(optarg, &sample_rate);
164 1.1 mrg if (sample_rate < 0 || sample_rate > 48000 * 2) /* XXX */
165 1.1 mrg errx(1, "sample rate must be between 0 and 96000\n");
166 1.1 mrg break;
167 1.1 mrg case 't':
168 1.8 mrg no_time_limit = 0;
169 1.1 mrg decode_time(optarg, &record_time);
170 1.1 mrg break;
171 1.1 mrg case 'V':
172 1.1 mrg verbose++;
173 1.1 mrg break;
174 1.1 mrg case 'v':
175 1.10.4.1 he decode_int(optarg, &gain);
176 1.10.4.1 he if (gain < 0 || gain > 255)
177 1.1 mrg errx(1, "volume must be between 0 and 255\n");
178 1.1 mrg break;
179 1.1 mrg /* case 'h': */
180 1.1 mrg default:
181 1.1 mrg usage();
182 1.1 mrg /* NOTREACHED */
183 1.1 mrg }
184 1.1 mrg }
185 1.1 mrg argc -= optind;
186 1.1 mrg argv += optind;
187 1.1 mrg
188 1.1 mrg /*
189 1.1 mrg * open the audio device, and control device
190 1.1 mrg */
191 1.6 kleink if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
192 1.6 kleink (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
193 1.1 mrg device = _PATH_AUDIO;
194 1.6 kleink if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == NULL)
195 1.1 mrg ctldev = _PATH_AUDIOCTL;
196 1.1 mrg
197 1.1 mrg audiofd = open(device, O_RDONLY);
198 1.1 mrg if (audiofd < 0)
199 1.1 mrg err(1, "failed to open %s", device);
200 1.1 mrg ctlfd = open(ctldev, O_RDWR);
201 1.1 mrg if (ctlfd < 0)
202 1.1 mrg err(1, "failed to open %s", ctldev);
203 1.1 mrg
204 1.1 mrg /*
205 1.1 mrg * work out the buffer size to use, and allocate it. also work out
206 1.1 mrg * what the old monitor gain value is, so that we can reset it later.
207 1.1 mrg */
208 1.1 mrg if (ioctl(ctlfd, AUDIO_GETINFO, &oinfo) < 0)
209 1.1 mrg err(1, "failed to get audio info");
210 1.1 mrg bufsize = oinfo.record.buffer_size;
211 1.1 mrg if (bufsize < 32 * 1024)
212 1.1 mrg bufsize = 32 * 1024;
213 1.10.4.1 he omonitor_gain = oinfo.monitor_gain;
214 1.1 mrg
215 1.1 mrg buffer = malloc(bufsize);
216 1.1 mrg if (buffer == NULL)
217 1.1 mrg err(1, "couldn't malloc buffer of %d size", (int)bufsize);
218 1.1 mrg
219 1.1 mrg /*
220 1.1 mrg * open the output file
221 1.1 mrg */
222 1.1 mrg if (argc != 1)
223 1.1 mrg usage();
224 1.1 mrg if (argv[0][0] != '-' && argv[0][1] != '\0') {
225 1.1 mrg outfd = open(*argv, O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY, 0666);
226 1.1 mrg if (outfd < 0)
227 1.1 mrg err(1, "could not open %s", *argv);
228 1.1 mrg } else
229 1.1 mrg outfd = STDOUT_FILENO;
230 1.1 mrg
231 1.1 mrg /*
232 1.10.4.1 he * convert the encoding string into a value.
233 1.1 mrg */
234 1.3 mrg if (encoding_str) {
235 1.3 mrg encoding = audio_enc_to_val(encoding_str);
236 1.3 mrg if (encoding == -1)
237 1.1 mrg errx(1, "unknown encoding, bailing...");
238 1.1 mrg }
239 1.10 dmcmahil else
240 1.10 dmcmahil encoding = AUDIO_ENCODING_ULAW;
241 1.1 mrg
242 1.10.4.1 he /*
243 1.10.4.1 he * set up audio device for recording with the speified parameters
244 1.10.4.1 he */
245 1.10.4.1 he AUDIO_INITINFO(&info);
246 1.10.4.1 he
247 1.10.4.1 he /*
248 1.10.4.1 he * for these, get the current values for stuffing into the header
249 1.10.4.1 he */
250 1.10.4.1 he #define SETINFO(x) if (x) info.record.x = x; else x = oinfo.record.x
251 1.10.4.1 he SETINFO (sample_rate);
252 1.10.4.1 he SETINFO (channels);
253 1.10.4.1 he SETINFO (precision);
254 1.10.4.1 he SETINFO (encoding);
255 1.10.4.1 he SETINFO (gain);
256 1.10.4.1 he SETINFO (port);
257 1.10.4.1 he SETINFO (balance);
258 1.10.4.1 he #undef SETINFO
259 1.10.4.1 he
260 1.10.4.1 he if (monitor_gain)
261 1.10.4.1 he info.monitor_gain = monitor_gain;
262 1.10.4.1 he else
263 1.10.4.1 he monitor_gain = oinfo.monitor_gain;
264 1.1 mrg
265 1.1 mrg info.mode = AUMODE_RECORD;
266 1.1 mrg if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
267 1.1 mrg err(1, "failed to reset audio info");
268 1.1 mrg
269 1.1 mrg signal(SIGINT, cleanup);
270 1.1 mrg write_header();
271 1.1 mrg total_size = 0;
272 1.1 mrg
273 1.1 mrg (void)gettimeofday(&start_time, NULL);
274 1.8 mrg while (no_time_limit || timeleft(&start_time, &record_time)) {
275 1.1 mrg if (read(audiofd, buffer, bufsize) != bufsize)
276 1.1 mrg err(1, "read failed");
277 1.10.4.1 he if (conv_func)
278 1.10.4.1 he (*conv_func)(buffer, bufsize);
279 1.1 mrg if (write(outfd, buffer, bufsize) != bufsize)
280 1.1 mrg err(1, "write failed");
281 1.1 mrg total_size += bufsize;
282 1.1 mrg }
283 1.1 mrg cleanup(0);
284 1.1 mrg }
285 1.1 mrg
286 1.1 mrg int
287 1.1 mrg timeleft(start_tvp, record_tvp)
288 1.1 mrg struct timeval *start_tvp;
289 1.1 mrg struct timeval *record_tvp;
290 1.1 mrg {
291 1.1 mrg struct timeval now, diff;
292 1.1 mrg
293 1.1 mrg (void)gettimeofday(&now, NULL);
294 1.7 dmcmahil timersub(&now, start_tvp, &diff);
295 1.7 dmcmahil timersub(record_tvp, &diff, &now);
296 1.1 mrg
297 1.1 mrg return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
298 1.1 mrg }
299 1.1 mrg
300 1.1 mrg void
301 1.1 mrg cleanup(signo)
302 1.1 mrg int signo;
303 1.1 mrg {
304 1.1 mrg
305 1.1 mrg close(audiofd);
306 1.1 mrg rewrite_header();
307 1.1 mrg close(outfd);
308 1.10.4.1 he if (omonitor_gain) {
309 1.1 mrg AUDIO_INITINFO(&info);
310 1.10.4.1 he info.monitor_gain = omonitor_gain;
311 1.1 mrg if (ioctl(ctlfd, AUDIO_SETINFO, &info) < 0)
312 1.1 mrg err(1, "failed to reset audio info");
313 1.1 mrg }
314 1.1 mrg close(ctlfd);
315 1.1 mrg exit(0);
316 1.1 mrg }
317 1.1 mrg
318 1.10.4.1 he int
319 1.10.4.1 he write_header_sun(hdrp, lenp, leftp)
320 1.10.4.1 he void **hdrp;
321 1.10.4.1 he size_t *lenp;
322 1.10.4.1 he int *leftp;
323 1.1 mrg {
324 1.10.4.1 he static int warned = 0;
325 1.10.4.1 he static sun_audioheader auh;
326 1.10.4.1 he int sunenc, oencoding = encoding;
327 1.10.4.1 he
328 1.10.4.1 he if (encoding == AUDIO_ENCODING_ULINEAR_LE) {
329 1.10.4.1 he if (precision == 16)
330 1.10.4.1 he conv_func = swap_bytes;
331 1.10.4.1 he else if (precision == 32)
332 1.10.4.1 he conv_func = swap_bytes32;
333 1.10.4.1 he if (conv_func)
334 1.10.4.1 he encoding = AUDIO_ENCODING_SLINEAR_BE;
335 1.10.4.1 he } else if (encoding == AUDIO_ENCODING_ULINEAR_BE) {
336 1.10.4.1 he if (precision == 16)
337 1.10.4.1 he conv_func = change_sign16_be;
338 1.10.4.1 he else if (precision == 32)
339 1.10.4.1 he conv_func = change_sign32_be;
340 1.10.4.1 he if (conv_func)
341 1.10.4.1 he encoding = AUDIO_ENCODING_SLINEAR_BE;
342 1.10.4.1 he } else if (encoding == AUDIO_ENCODING_SLINEAR_LE) {
343 1.10.4.1 he if (precision == 16)
344 1.10.4.1 he conv_func = change_sign16_swap_bytes_le;
345 1.10.4.1 he else if (precision == 32)
346 1.10.4.1 he conv_func = change_sign32_swap_bytes_le;
347 1.10.4.1 he if (conv_func)
348 1.10.4.1 he encoding = AUDIO_ENCODING_SLINEAR_BE;
349 1.10.4.1 he }
350 1.10.4.1 he
351 1.10.4.1 he /* if we can't express this as a Sun header, don't write any */
352 1.10.4.1 he if (audio_encoding_to_sun(encoding, precision, &sunenc) != 0) {
353 1.10.4.1 he if (!qflag && !warned)
354 1.10.4.1 he warnx("failed to convert to sun encoding from %d:%d; "
355 1.10.4.1 he "Sun audio header not written",
356 1.10.4.1 he oencoding, precision);
357 1.10.4.1 he conv_func = 0;
358 1.10.4.1 he warned = 1;
359 1.10.4.1 he return -1;
360 1.10.4.1 he }
361 1.1 mrg
362 1.1 mrg auh.magic = htonl(AUDIO_FILE_MAGIC);
363 1.1 mrg if (outfd == STDOUT_FILENO)
364 1.1 mrg auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
365 1.1 mrg else
366 1.1 mrg auh.data_size = htonl(total_size);
367 1.10.4.1 he auh.encoding = htonl(sunenc);
368 1.1 mrg auh.sample_rate = htonl(sample_rate);
369 1.1 mrg auh.channels = htonl(channels);
370 1.1 mrg if (header_info) {
371 1.1 mrg int len, infolen;
372 1.1 mrg
373 1.1 mrg infolen = ((len = strlen(header_info)) + 7) & 0xfffffff8;
374 1.10.4.1 he *leftp = infolen - len;
375 1.1 mrg auh.hdr_size = htonl(sizeof(auh) + infolen);
376 1.1 mrg } else {
377 1.10.4.1 he *leftp = sizeof(default_info);
378 1.10.4.1 he auh.hdr_size = htonl(sizeof(auh) + *leftp);
379 1.1 mrg }
380 1.10.4.1 he *(sun_audioheader **)hdrp = &auh;
381 1.10.4.1 he *lenp = sizeof auh;
382 1.10.4.1 he return 0;
383 1.10.4.1 he }
384 1.1 mrg
385 1.10.4.1 he int
386 1.10.4.1 he write_header_wav(hdrp, lenp, leftp)
387 1.10.4.1 he void **hdrp;
388 1.10.4.1 he size_t *lenp;
389 1.10.4.1 he int *leftp;
390 1.10.4.1 he {
391 1.10.4.1 he /*
392 1.10.4.1 he * WAV header we write looks like this:
393 1.10.4.1 he *
394 1.10.4.1 he * bytes purpose
395 1.10.4.1 he * 0-3 "RIFF"
396 1.10.4.1 he * 4-7 file length (minus 8)
397 1.10.4.1 he * 8-15 "WAVEfmt "
398 1.10.4.1 he * 16-19 format size
399 1.10.4.1 he * 20-21 format tag
400 1.10.4.1 he * 22-23 number of channels
401 1.10.4.1 he * 24-27 sample rate
402 1.10.4.1 he * 28-31 average bytes per second
403 1.10.4.1 he * 32-33 block alignment
404 1.10.4.1 he * 34-35 bits per sample
405 1.10.4.1 he *
406 1.10.4.1 he * then for ULAW and ALAW outputs, we have an extended chunk size
407 1.10.4.1 he * and a WAV "fact" to add:
408 1.10.4.1 he *
409 1.10.4.1 he * 36-37 length of extension (== 0)
410 1.10.4.1 he * 38-41 "fact"
411 1.10.4.1 he * 42-45 fact size
412 1.10.4.1 he * 46-49 number of samples written
413 1.10.4.1 he * 50-53 "data"
414 1.10.4.1 he * 54-57 data length
415 1.10.4.1 he * 58- raw audio data
416 1.10.4.1 he *
417 1.10.4.1 he * for PCM outputs we have just the data remaining:
418 1.10.4.1 he *
419 1.10.4.1 he * 36-39 "data"
420 1.10.4.1 he * 40-43 data length
421 1.10.4.1 he * 44- raw audio data
422 1.10.4.1 he *
423 1.10.4.1 he * RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
424 1.10.4.1 he */
425 1.10.4.1 he char wavheaderbuf[64], *p = wavheaderbuf;
426 1.10.4.1 he const char *riff = "RIFF",
427 1.10.4.1 he *wavefmt = "WAVEfmt ",
428 1.10.4.1 he *fact = "fact",
429 1.10.4.1 he *data = "data";
430 1.10.4.1 he u_int32_t filelen, fmtsz, sps, abps, factsz = 4, nsample, datalen;
431 1.10.4.1 he u_int16_t fmttag, nchan, align, bps, extln = 0;
432 1.10.4.1 he
433 1.10.4.1 he if (header_info)
434 1.10.4.1 he warnx("header information not supported for WAV");
435 1.10.4.1 he *leftp = NULL;
436 1.10.4.1 he
437 1.10.4.1 he switch (precision) {
438 1.10.4.1 he case 8:
439 1.10.4.1 he bps = 8;
440 1.10.4.1 he break;
441 1.10.4.1 he case 16:
442 1.10.4.1 he bps = 16;
443 1.10.4.1 he break;
444 1.10.4.1 he case 32:
445 1.10.4.1 he bps = 32;
446 1.10.4.1 he break;
447 1.10.4.1 he default:
448 1.10.4.1 he {
449 1.10.4.1 he static int warned = 0;
450 1.10.4.1 he
451 1.10.4.1 he if (warned == 0) {
452 1.10.4.1 he warnx("can not support precision of %d\n", precision);
453 1.10.4.1 he warned = 1;
454 1.10.4.1 he }
455 1.10.4.1 he }
456 1.10.4.1 he return (-1);
457 1.10.4.1 he }
458 1.10.4.1 he
459 1.10.4.1 he switch (encoding) {
460 1.10.4.1 he case AUDIO_ENCODING_ULAW:
461 1.10.4.1 he fmttag = WAVE_FORMAT_MULAW;
462 1.10.4.1 he fmtsz = 18;
463 1.10.4.1 he align = channels;
464 1.10.4.1 he break;
465 1.10.4.1 he case AUDIO_ENCODING_ALAW:
466 1.10.4.1 he fmttag = WAVE_FORMAT_ALAW;
467 1.10.4.1 he fmtsz = 18;
468 1.10.4.1 he align = channels;
469 1.10.4.1 he break;
470 1.10.4.1 he /*
471 1.10.4.1 he * we could try to support RIFX but it seems to be more portable
472 1.10.4.1 he * to output little-endian data for WAV files.
473 1.10.4.1 he */
474 1.10.4.1 he case AUDIO_ENCODING_ULINEAR_BE:
475 1.10.4.1 he if (bps == 16)
476 1.10.4.1 he conv_func = change_sign16_swap_bytes_be;
477 1.10.4.1 he else if (bps == 32)
478 1.10.4.1 he conv_func = change_sign32_swap_bytes_be;
479 1.10.4.1 he goto fmt_pcm;
480 1.10.4.1 he case AUDIO_ENCODING_SLINEAR_BE:
481 1.10.4.1 he if (bps == 16)
482 1.10.4.1 he conv_func = swap_bytes;
483 1.10.4.1 he else if (bps == 32)
484 1.10.4.1 he conv_func = swap_bytes32;
485 1.10.4.1 he goto fmt_pcm;
486 1.10.4.1 he case AUDIO_ENCODING_ULINEAR_LE:
487 1.10.4.1 he if (bps == 16)
488 1.10.4.1 he conv_func = change_sign16_le;
489 1.10.4.1 he else if (bps == 32)
490 1.10.4.1 he conv_func = change_sign32_le;
491 1.10.4.1 he /* FALLTHROUGH */
492 1.10.4.1 he case AUDIO_ENCODING_SLINEAR_LE:
493 1.10.4.1 he case AUDIO_ENCODING_PCM16:
494 1.10.4.1 he fmt_pcm:
495 1.10.4.1 he fmttag = WAVE_FORMAT_PCM;
496 1.10.4.1 he fmtsz = 16;
497 1.10.4.1 he align = channels * (bps / 8);
498 1.10.4.1 he break;
499 1.10.4.1 he default:
500 1.10.4.1 he {
501 1.10.4.1 he static int warned = 0;
502 1.10.4.1 he
503 1.10.4.1 he if (warned == 0) {
504 1.10.4.1 he warnx("can not support encoding of %s\n", wav_enc_from_val(encoding));
505 1.10.4.1 he warned = 1;
506 1.10.4.1 he }
507 1.10.4.1 he }
508 1.10.4.1 he return (-1);
509 1.10.4.1 he }
510 1.10.4.1 he
511 1.10.4.1 he nchan = channels;
512 1.10.4.1 he sps = sample_rate;
513 1.10.4.1 he
514 1.10.4.1 he /* data length */
515 1.10.4.1 he if (outfd == STDOUT_FILENO)
516 1.10.4.1 he datalen = 0;
517 1.10.4.1 he else
518 1.10.4.1 he datalen = total_size;
519 1.10.4.1 he
520 1.10.4.1 he /* file length */
521 1.10.4.1 he filelen = 4 + (8 + fmtsz) + (8 + datalen);
522 1.10.4.1 he if (fmttag != WAVE_FORMAT_PCM)
523 1.10.4.1 he filelen += 8 + factsz;
524 1.10.4.1 he
525 1.10.4.1 he abps = (double)align*sample_rate / (double)1 + 0.5;
526 1.10.4.1 he
527 1.10.4.1 he nsample = (datalen / bps) / sample_rate;
528 1.10.4.1 he
529 1.10.4.1 he /*
530 1.10.4.1 he * now we've calculated the info, write it out!
531 1.10.4.1 he */
532 1.10.4.1 he #define put32(x) do { \
533 1.10.4.1 he u_int32_t _f; \
534 1.10.4.1 he putle32(_f, (x)); \
535 1.10.4.1 he memcpy(p, &_f, 4); \
536 1.10.4.1 he } while (0)
537 1.10.4.1 he #define put16(x) do { \
538 1.10.4.1 he u_int16_t _f; \
539 1.10.4.1 he putle16(_f, (x)); \
540 1.10.4.1 he memcpy(p, &_f, 2); \
541 1.10.4.1 he } while (0)
542 1.10.4.1 he memcpy(p, riff, 4);
543 1.10.4.1 he p += 4; /* 4 */
544 1.10.4.1 he put32(filelen);
545 1.10.4.1 he p += 4; /* 8 */
546 1.10.4.1 he memcpy(p, wavefmt, 8);
547 1.10.4.1 he p += 8; /* 16 */
548 1.10.4.1 he put32(fmtsz);
549 1.10.4.1 he p += 4; /* 20 */
550 1.10.4.1 he put16(fmttag);
551 1.10.4.1 he p += 2; /* 22 */
552 1.10.4.1 he put16(nchan);
553 1.10.4.1 he p += 2; /* 24 */
554 1.10.4.1 he put32(sps);
555 1.10.4.1 he p += 4; /* 28 */
556 1.10.4.1 he put32(abps);
557 1.10.4.1 he p += 4; /* 32 */
558 1.10.4.1 he put16(align);
559 1.10.4.1 he p += 2; /* 34 */
560 1.10.4.1 he put16(bps);
561 1.10.4.1 he p += 2; /* 36 */
562 1.10.4.1 he /* NON PCM formats have an extended chunk; write it */
563 1.10.4.1 he if (fmttag != WAVE_FORMAT_PCM) {
564 1.10.4.1 he put16(extln);
565 1.10.4.1 he p += 2; /* 38 */
566 1.10.4.1 he memcpy(p, fact, 4);
567 1.10.4.1 he p += 4; /* 42 */
568 1.10.4.1 he put32(factsz);
569 1.10.4.1 he p += 4; /* 46 */
570 1.10.4.1 he put32(nsample);
571 1.10.4.1 he p += 4; /* 50 */
572 1.10.4.1 he }
573 1.10.4.1 he memcpy(p, data, 4);
574 1.10.4.1 he p += 4; /* 40/54 */
575 1.10.4.1 he put32(datalen);
576 1.10.4.1 he p += 4; /* 44/58 */
577 1.10.4.1 he #undef put32
578 1.10.4.1 he #undef put16
579 1.10.4.1 he
580 1.10.4.1 he *hdrp = wavheaderbuf;
581 1.10.4.1 he *lenp = (p - wavheaderbuf);
582 1.10.4.1 he
583 1.10.4.1 he return 0;
584 1.10.4.1 he }
585 1.10.4.1 he
586 1.10.4.1 he void
587 1.10.4.1 he write_header()
588 1.10.4.1 he {
589 1.10.4.1 he struct iovec iv[3];
590 1.10.4.1 he int veclen, left, tlen;
591 1.10.4.1 he void *hdr;
592 1.10.4.1 he size_t hdrlen;
593 1.10.4.1 he
594 1.10.4.1 he switch (format) {
595 1.10.4.1 he case AUDIO_FORMAT_SUN:
596 1.10.4.1 he if (write_header_sun(&hdr, &hdrlen, &left) != 0)
597 1.10.4.1 he return;
598 1.10.4.1 he break;
599 1.10.4.1 he case AUDIO_FORMAT_WAV:
600 1.10.4.1 he if (write_header_wav(&hdr, &hdrlen, &left) != 0)
601 1.10.4.1 he return;
602 1.10.4.1 he break;
603 1.10.4.1 he case AUDIO_FORMAT_NONE:
604 1.10.4.1 he return;
605 1.10.4.1 he default:
606 1.10.4.1 he errx(1, "unknown audio format");
607 1.10.4.1 he }
608 1.10.4.1 he
609 1.10.4.1 he veclen = 0;
610 1.10.4.1 he tlen = 0;
611 1.10.4.1 he
612 1.10.4.1 he if (hdrlen != 0) {
613 1.10.4.1 he iv[veclen].iov_base = hdr;
614 1.10.4.1 he iv[veclen].iov_len = hdrlen;
615 1.10.4.1 he tlen += iv[veclen++].iov_len;
616 1.10.4.1 he }
617 1.1 mrg if (header_info) {
618 1.1 mrg iv[veclen].iov_base = header_info;
619 1.10.4.1 he iv[veclen].iov_len = (int)strlen(header_info) + 1;
620 1.1 mrg tlen += iv[veclen++].iov_len;
621 1.1 mrg }
622 1.1 mrg if (left) {
623 1.1 mrg iv[veclen].iov_base = default_info;
624 1.1 mrg iv[veclen].iov_len = left;
625 1.1 mrg tlen += iv[veclen++].iov_len;
626 1.1 mrg }
627 1.1 mrg
628 1.10.4.1 he if (tlen == 0)
629 1.10.4.1 he return;
630 1.10.4.1 he
631 1.1 mrg if (writev(outfd, iv, veclen) != tlen)
632 1.1 mrg err(1, "could not write audio header");
633 1.1 mrg }
634 1.1 mrg
635 1.1 mrg void
636 1.1 mrg rewrite_header()
637 1.1 mrg {
638 1.1 mrg
639 1.1 mrg /* can't do this here! */
640 1.1 mrg if (outfd == STDOUT_FILENO)
641 1.1 mrg return;
642 1.1 mrg
643 1.1 mrg if (lseek(outfd, SEEK_SET, 0) < 0)
644 1.1 mrg err(1, "could not seek to start of file for header rewrite");
645 1.1 mrg write_header();
646 1.1 mrg }
647 1.1 mrg
648 1.1 mrg void
649 1.1 mrg usage()
650 1.1 mrg {
651 1.4 mrg fprintf(stderr, "Usage: %s [-afhqV] [options] {files ...|-}\n", __progname);
652 1.4 mrg fprintf(stderr, "Options:\n\t"
653 1.4 mrg "-C audio control device\n\t"
654 1.10.4.1 he "-F format\n\t"
655 1.4 mrg "-b balance (0-63)\n\t"
656 1.4 mrg "-c channels\n\t"
657 1.4 mrg "-d audio device\n\t"
658 1.4 mrg "-e encoding\n\t"
659 1.4 mrg "-i header information\n\t"
660 1.4 mrg "-m monitor volume\n\t"
661 1.10.4.1 he "-P precision bits (4, 8, 16, 24 or 32)\n\t"
662 1.4 mrg "-p input port\n\t"
663 1.4 mrg "-s sample rate\n\t"
664 1.4 mrg "-t recording time\n\t"
665 1.4 mrg "-v volume\n");
666 1.9 kleink exit(EXIT_FAILURE);
667 1.1 mrg }
668