play.c revision 1.46.10.1 1 /* $NetBSD: play.c,v 1.46.10.1 2007/11/06 23:35:40 matt 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 #include <sys/cdefs.h>
31
32 #ifndef lint
33 __RCSID("$NetBSD: play.c,v 1.46.10.1 2007/11/06 23:35:40 matt Exp $");
34 #endif
35
36
37 #include <sys/param.h>
38 #include <sys/audioio.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42
43 #include <err.h>
44 #include <fcntl.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <util.h>
51
52 #include <paths.h>
53
54 #include "libaudio.h"
55
56 int main(int, char *[]);
57 void usage(void);
58 void play(char *);
59 void play_fd(const char *, int);
60 ssize_t audioctl_write_fromhdr(void *, size_t, int, size_t *, const char *);
61 void cleanup(int) __attribute__((__noreturn__));
62
63 audio_info_t info;
64 int volume;
65 int balance;
66 int port;
67 int fflag;
68 int qflag;
69 int verbose;
70 int sample_rate;
71 int encoding;
72 char *encoding_str;
73 int precision;
74 int channels;
75
76 char const *play_errstring = NULL;
77 size_t bufsize;
78 int audiofd;
79 int exitstatus = EXIT_SUCCESS;
80
81 int
82 main(argc, argv)
83 int argc;
84 char *argv[];
85 {
86 size_t len;
87 int ch;
88 int iflag = 0;
89 const char *defdevice = _PATH_SOUND;
90 const char *device = NULL;
91
92 while ((ch = getopt(argc, argv, "b:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
93 switch (ch) {
94 case 'b':
95 decode_int(optarg, &balance);
96 if (balance < 0 || balance > 64)
97 errx(1, "balance must be between 0 and 63");
98 break;
99 case 'c':
100 decode_int(optarg, &channels);
101 if (channels < 0)
102 errx(1, "channels must be positive");
103 break;
104 case 'C':
105 /* Ignore, compatibility */
106 break;
107 case 'd':
108 device = optarg;
109 break;
110 case 'e':
111 encoding_str = optarg;
112 break;
113 case 'f':
114 fflag = 1;
115 break;
116 case 'i':
117 iflag++;
118 break;
119 case 'q':
120 qflag++;
121 break;
122 case 'P':
123 decode_int(optarg, &precision);
124 if (precision != 4 && precision != 8 &&
125 precision != 16 && precision != 24 &&
126 precision != 32)
127 errx(1, "precision must be between 4, 8, 16, 24 or 32");
128 break;
129 case 'p':
130 len = strlen(optarg);
131
132 if (strncmp(optarg, "speaker", len) == 0)
133 port |= AUDIO_SPEAKER;
134 else if (strncmp(optarg, "headphone", len) == 0)
135 port |= AUDIO_HEADPHONE;
136 else if (strncmp(optarg, "line", len) == 0)
137 port |= AUDIO_LINE_OUT;
138 else
139 errx(1,
140 "port must be `speaker', `headphone', or `line'");
141 break;
142 case 's':
143 decode_int(optarg, &sample_rate);
144 if (sample_rate < 0 || sample_rate > 48000 * 2) /* XXX */
145 errx(1, "sample rate must be between 0 and 96000");
146 break;
147 case 'V':
148 verbose++;
149 break;
150 case 'v':
151 volume = atoi(optarg);
152 if (volume < 0 || volume > 255)
153 errx(1, "volume must be between 0 and 255");
154 break;
155 /* case 'h': */
156 default:
157 usage();
158 /* NOTREACHED */
159 }
160 }
161 argc -= optind;
162 argv += optind;
163
164 if (encoding_str) {
165 encoding = audio_enc_to_val(encoding_str);
166 if (encoding == -1)
167 errx(1, "unknown encoding, bailing...");
168 }
169
170 if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
171 (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
172 device = defdevice;
173
174 audiofd = open(device, O_WRONLY);
175 if (audiofd < 0 && device == defdevice) {
176 device = _PATH_SOUND0;
177 audiofd = open(device, O_WRONLY);
178 }
179
180 if (audiofd < 0)
181 err(1, "failed to open %s", device);
182
183 if (ioctl(audiofd, AUDIO_GETINFO, &info) < 0)
184 err(1, "failed to get audio info");
185 bufsize = info.play.buffer_size;
186 if (bufsize < 32 * 1024)
187 bufsize = 32 * 1024;
188
189 signal(SIGINT, cleanup);
190 signal(SIGTERM, cleanup);
191 signal(SIGHUP, cleanup);
192
193 if (*argv)
194 do
195 play(*argv++);
196 while (*argv);
197 else
198 play_fd("standard input", STDIN_FILENO);
199
200 cleanup(0);
201 }
202
203 void
204 cleanup(signo)
205 int signo;
206 {
207
208 (void)ioctl(audiofd, AUDIO_FLUSH, NULL);
209 (void)ioctl(audiofd, AUDIO_SETINFO, &info);
210 close(audiofd);
211 if (signo != 0) {
212 (void)raise_default_signal(signo);
213 }
214 exit(exitstatus);
215 }
216
217 void
218 play(file)
219 char *file;
220 {
221 struct stat sb;
222 void *addr, *oaddr;
223 off_t filesize;
224 size_t sizet_filesize;
225 size_t datasize = 0;
226 ssize_t hdrlen;
227 int fd;
228
229 if (file[0] == '-' && file[1] == 0) {
230 play_fd("standard input", STDIN_FILENO);
231 return;
232 }
233
234 fd = open(file, O_RDONLY);
235 if (fd < 0) {
236 if (!qflag)
237 warn("could not open %s", file);
238 exitstatus = EXIT_FAILURE;
239 return;
240 }
241
242 if (fstat(fd, &sb) < 0)
243 err(1, "could not fstat %s", file);
244 filesize = sb.st_size;
245 sizet_filesize = (size_t)filesize;
246
247 /*
248 * if the file is not a regular file, doesn't fit in a size_t,
249 * or if we failed to mmap the file, try to read it instead, so
250 * that filesystems, etc, that do not support mmap() work
251 */
252 if (S_ISREG(sb.st_rdev & S_IFMT) == 0 ||
253 ((off_t)sizet_filesize != filesize) ||
254 (oaddr = addr = mmap(0, sizet_filesize, PROT_READ,
255 MAP_SHARED, fd, 0)) == MAP_FAILED) {
256 play_fd(file, fd);
257 close(fd);
258 return;
259 }
260
261 /*
262 * give the VM system a bit of a hint about the type
263 * of accesses we will make.
264 */
265 if (madvise(addr, sizet_filesize, MADV_SEQUENTIAL) < 0 &&
266 !qflag)
267 warn("madvise failed, ignoring");
268
269 /*
270 * get the header length and set up the audio device
271 */
272 if ((hdrlen = audioctl_write_fromhdr(addr,
273 sizet_filesize, audiofd, &datasize, file)) < 0) {
274 if (play_errstring)
275 errx(1, "%s: %s", play_errstring, file);
276 else
277 errx(1, "unknown audio file: %s", file);
278 }
279
280 filesize -= hdrlen;
281 addr = (char *)addr + hdrlen;
282 if (filesize < datasize || datasize == 0) {
283 if (filesize < datasize)
284 warnx("bogus datasize: %ld", (u_long)datasize);
285 datasize = filesize;
286 }
287
288 while (datasize > bufsize) {
289 if (write(audiofd, addr, bufsize) != bufsize)
290 err(1, "write failed");
291 addr = (char *)addr + bufsize;
292 datasize -= bufsize;
293 }
294 if (write(audiofd, addr, (size_t)datasize) != (ssize_t)datasize)
295 err(1, "final write failed");
296
297 if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
298 warn("audio drain ioctl failed");
299 if (munmap(oaddr, sizet_filesize) < 0)
300 err(1, "munmap failed");
301
302 close(fd);
303 }
304
305 /*
306 * play the file on the file descriptor fd
307 */
308 void
309 play_fd(file, fd)
310 const char *file;
311 int fd;
312 {
313 char *buffer = malloc(bufsize);
314 ssize_t hdrlen;
315 int nr, nw;
316 size_t datasize = 0;
317 size_t dataout = 0;
318
319 if (buffer == NULL)
320 err(1, "malloc of read buffer failed");
321
322 nr = read(fd, buffer, bufsize);
323 if (nr < 0)
324 goto read_error;
325 if (nr == 0) {
326 if (fflag) {
327 free(buffer);
328 return;
329 }
330 err(1, "unexpected EOF");
331 }
332 hdrlen = audioctl_write_fromhdr(buffer, nr, audiofd, &datasize, file);
333 if (hdrlen < 0) {
334 if (play_errstring)
335 errx(1, "%s: %s", play_errstring, file);
336 else
337 errx(1, "unknown audio file: %s", file);
338 }
339 if (hdrlen > 0) {
340 if (hdrlen > nr) /* shouldn't happen */
341 errx(1, "header seems really large: %lld", (long long)hdrlen);
342 memmove(buffer, buffer + hdrlen, nr - hdrlen);
343 nr -= hdrlen;
344 }
345 while (datasize == 0 || dataout < datasize) {
346 if (datasize != 0 && dataout + nr > datasize)
347 nr = datasize - dataout;
348 nw = write(audiofd, buffer, nr);
349 if (nw != nr)
350 goto write_error;
351 dataout += nw;
352 nr = read(fd, buffer, bufsize);
353 if (nr == -1)
354 goto read_error;
355 if (nr == 0)
356 break;
357 }
358 /* something to think about: no message given for dataout < datasize */
359 if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
360 warn("audio drain ioctl failed");
361 return;
362 read_error:
363 err(1, "read of standard input failed");
364 write_error:
365 err(1, "audio device write failed");
366 }
367
368 /*
369 * only support sun and wav audio files so far ...
370 *
371 * XXX this should probably be mostly part of libaudio, but it
372 * uses the local "info" variable. blah... fix me!
373 */
374 ssize_t
375 audioctl_write_fromhdr(hdr, fsz, fd, datasize, file)
376 void *hdr;
377 size_t fsz;
378 int fd;
379 size_t *datasize;
380 const char *file;
381 {
382 sun_audioheader *sunhdr;
383 ssize_t hdr_len = 0;
384
385 AUDIO_INITINFO(&info);
386 sunhdr = hdr;
387 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
388 if (audio_sun_to_encoding(ntohl(sunhdr->encoding),
389 &info.play.encoding, &info.play.precision)) {
390 if (!qflag)
391 warnx("unknown unsupported Sun audio encoding"
392 " format %d", ntohl(sunhdr->encoding));
393 if (fflag)
394 goto set_audio_mode;
395 return (-1);
396 }
397
398 info.play.sample_rate = ntohl(sunhdr->sample_rate);
399 info.play.channels = ntohl(sunhdr->channels);
400 hdr_len = ntohl(sunhdr->hdr_size);
401
402 *datasize = ntohl(sunhdr->data_size);
403 goto set_audio_mode;
404 }
405
406 hdr_len = audio_wav_parse_hdr(hdr, fsz, &info.play.encoding,
407 &info.play.precision, &info.play.sample_rate, &info.play.channels,
408 datasize);
409
410 switch (hdr_len) {
411 case AUDIO_ESHORTHDR:
412 case AUDIO_EWAVUNSUPP:
413 case AUDIO_EWAVBADPCM:
414 case AUDIO_EWAVNODATA:
415 play_errstring = audio_errstring(hdr_len);
416 /* FALL THROUGH */
417 case AUDIO_ENOENT:
418 break;
419 default:
420 if (hdr_len < 1)
421 break;
422 goto set_audio_mode;
423 }
424 /*
425 * if we don't know it, bail unless we are forcing.
426 */
427 if (fflag == 0)
428 return (-1);
429 set_audio_mode:
430 if (port)
431 info.play.port = port;
432 if (volume)
433 info.play.gain = volume;
434 if (balance)
435 info.play.balance = balance;
436 if (fflag) {
437 if (sample_rate)
438 info.play.sample_rate = sample_rate;
439 if (channels)
440 info.play.channels = channels;
441 if (encoding)
442 info.play.encoding = encoding;
443 if (precision)
444 info.play.precision = precision;
445 hdr_len = 0;
446 }
447 info.mode = AUMODE_PLAY_ALL;
448
449 if (verbose) {
450 const char *enc = audio_enc_from_val(info.play.encoding);
451
452 printf("%s: sample_rate=%d channels=%d "
453 "datasize=%lld "
454 "precision=%d%s%s\n", file,
455 info.play.sample_rate,
456 info.play.channels,
457 (long long)*datasize,
458 info.play.precision,
459 enc ? " encoding=" : "",
460 enc ? enc : "");
461 }
462
463 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
464 err(1, "failed to set audio info");
465
466 return (hdr_len);
467 }
468
469 void
470 usage()
471 {
472
473 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", getprogname());
474 fprintf(stderr, "Options:\n\t"
475 "-C audio control device\n\t"
476 "-b balance (0-63)\n\t"
477 "-d audio device\n\t"
478 "-f force settings\n\t"
479 "\t-c forced channels\n\t"
480 "\t-e forced encoding\n\t"
481 "\t-P forced precision\n\t"
482 "\t-s forced sample rate\n\t"
483 "-i header information\n\t"
484 "-p output port\n\t"
485 "-v volume\n");
486 exit(EXIT_FAILURE);
487 }
488