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