play.c revision 1.29 1 /* $NetBSD: play.c,v 1.29 2002/01/15 08:19:38 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 <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 (const 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 const char *device = 0;
81 const 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 if (file[0] == '-' && file[1] == 0) {
210 play_fd("standard input", STDIN_FILENO);
211 return;
212 }
213
214 fd = open(file, O_RDONLY);
215 if (fd < 0) {
216 if (!qflag)
217 warn("could not open %s", file);
218 exitstatus = EXIT_FAILURE;
219 return;
220 }
221
222 if (fstat(fd, &sb) < 0)
223 err(1, "could not fstat %s", file);
224 filesize = sb.st_size;
225
226 oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
227 MAP_SHARED, fd, 0);
228
229 /*
230 * if we failed to mmap the file, try to read it
231 * instead, so that filesystems, etc, that do not
232 * support mmap() work
233 */
234 if (addr == MAP_FAILED) {
235 play_fd(file, fd);
236 close(fd);
237 return;
238 }
239
240 /*
241 * give the VM system a bit of a hint about the type
242 * of accesses we will make.
243 */
244 if (madvise(addr, filesize, MADV_SEQUENTIAL) < 0 &&
245 !qflag)
246 warn("madvise failed, ignoring");
247
248 /*
249 * get the header length and set up the audio device
250 */
251 if ((hdrlen = audioctl_write_fromhdr(addr,
252 (size_t)filesize, ctlfd, &datasize)) < 0) {
253 if (play_errstring)
254 errx(1, "%s: %s", play_errstring, file);
255 else
256 errx(1, "unknown audio file: %s", file);
257 }
258
259 filesize -= hdrlen;
260 addr = (char *)addr + hdrlen;
261 if (filesize < datasize || datasize == 0) {
262 warn("bogus datasize: %lu", (long)datasize);
263 datasize = filesize;
264 }
265
266 while (datasize > bufsize) {
267 if (write(audiofd, addr, bufsize) != bufsize)
268 err(1, "write failed");
269 addr = (char *)addr + bufsize;
270 datasize -= bufsize;
271 }
272 if (write(audiofd, addr, (size_t)datasize) != (ssize_t)datasize)
273 err(1, "final write failed");
274
275 if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
276 warn("audio drain ioctl failed");
277 if (munmap(oaddr, (size_t)filesize) < 0)
278 err(1, "munmap failed");
279
280 close(fd);
281 }
282
283 /*
284 * play the file on on the file descriptor fd
285 */
286 void
287 play_fd(file, fd)
288 const char *file;
289 int fd;
290 {
291 char *buffer = malloc(bufsize);
292 ssize_t hdrlen;
293 int nr, nw;
294 size_t datasize = 0;
295 size_t dataout = 0;
296
297 if (buffer == NULL)
298 err(1, "malloc of read buffer failed");
299
300 nr = read(fd, buffer, bufsize);
301 if (nr < 0)
302 goto read_error;
303 if (nr == 0) {
304 if (fflag)
305 return;
306 else errx(1, "unexpected EOF");
307 }
308 hdrlen = audioctl_write_fromhdr(buffer, nr, ctlfd, &datasize);
309 if (hdrlen < 0) {
310 if (play_errstring)
311 errx(1, "%s: %s", play_errstring, file);
312 else
313 errx(1, "unknown audio file: %s", file);
314 }
315 if (hdrlen > 0) {
316 if (hdrlen >= nr) /* shouldn't happen */
317 errx(1, "header seems really large");
318 memmove(buffer, buffer + hdrlen, nr - hdrlen);
319 nr -= hdrlen;
320 }
321 while (datasize == 0 || dataout < datasize) {
322 if (datasize != 0 && dataout + nr > datasize)
323 nr = datasize - dataout;
324 nw = write(audiofd, buffer, nr);
325 if (nw != nr)
326 goto write_error;
327 dataout += nw;
328 nr = read(fd, buffer, bufsize);
329 if (nr == -1)
330 goto read_error;
331 if (nr == 0)
332 break;
333 }
334 /* something to think about: no message given for dataout < datasize */
335 if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
336 warn("audio drain ioctl failed");
337 return;
338 read_error:
339 err(1, "read of standard input failed");
340 write_error:
341 err(1, "audio device write failed");
342 }
343
344 /*
345 * only support sun and wav audio files so far ...
346 *
347 * XXX this should probably be mostly part of libaudio, but it
348 * uses the local "info" variable. blah... fix me!
349 */
350 ssize_t
351 audioctl_write_fromhdr(hdr, fsz, fd, datasize)
352 void *hdr;
353 size_t fsz;
354 int fd;
355 size_t *datasize;
356 {
357 sun_audioheader *sunhdr;
358 ssize_t hdr_len;
359
360 AUDIO_INITINFO(&info);
361 sunhdr = hdr;
362 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
363 if (audio_sun_to_encoding(ntohl(sunhdr->encoding),
364 &info.play.encoding, &info.play.precision)) {
365 if (!qflag)
366 warnx("unknown unsupported Sun audio encoding"
367 " format %d", ntohl(sunhdr->encoding));
368 if (fflag)
369 goto set_audio_mode;
370 return (-1);
371 }
372
373 info.play.sample_rate = ntohl(sunhdr->sample_rate);
374 info.play.channels = ntohl(sunhdr->channels);
375 hdr_len = ntohl(sunhdr->hdr_size);
376
377 *datasize = ntohl(sunhdr->data_size);
378 goto set_audio_mode;
379 }
380
381 hdr_len = audio_wav_parse_hdr(hdr, fsz, &info.play.encoding,
382 &info.play.precision, &info.play.sample_rate, &info.play.channels,
383 datasize);
384
385 switch (hdr_len) {
386 case AUDIO_ESHORTHDR:
387 case AUDIO_EWAVUNSUPP:
388 case AUDIO_EWAVBADPCM:
389 case AUDIO_EWAVNODATA:
390 play_errstring = audio_errstring(hdr_len);
391 /* FALL THROUGH */
392 case AUDIO_ENOENT:
393 break;
394 default:
395 if (hdr_len < 1)
396 break;
397 goto set_audio_mode;
398 }
399 /*
400 * if we don't know it, bail unless we are forcing.
401 */
402 if (fflag == 0)
403 return (-1);
404 set_audio_mode:
405 if (port)
406 info.play.port = port;
407 if (volume)
408 info.play.gain = volume;
409 if (balance)
410 info.play.balance = balance;
411 if (fflag) {
412 if (sample_rate)
413 info.play.sample_rate = sample_rate;
414 if (channels)
415 info.play.channels = channels;
416 if (encoding)
417 info.play.encoding = encoding;
418 if (precision)
419 info.play.precision = precision;
420 hdr_len = 0;
421 }
422 info.mode = AUMODE_PLAY_ALL;
423
424 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
425 err(1, "failed to set audio info");
426
427 return (hdr_len);
428 }
429
430 void
431 usage()
432 {
433
434 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", getprogname());
435 fprintf(stderr, "Options:\n\t"
436 "-C audio control device\n\t"
437 "-b balance (0-63)\n\t"
438 "-d audio device\n\t"
439 "-f force settings\n\t"
440 "\t-c forced channels\n\t"
441 "\t-e forced encoding\n\t"
442 "\t-P forced precision\n\t"
443 "\t-s forced sample rate\n\t"
444 "-i header information\n\t"
445 "-m monitor volume\n\t"
446 "-p output port\n\t"
447 "-v volume\n");
448 exit(EXIT_FAILURE);
449 }
450