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