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