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