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