play.c revision 1.16 1 /* $NetBSD: play.c,v 1.16 1999/11/08 10:21:20 kleink 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 sample_rate;
59 int encoding;
60 char *encoding_str;
61 int precision;
62 int channels;
63
64 char const *play_errstring = NULL;
65 size_t bufsize;
66 int audiofd, ctlfd;
67
68 int
69 main(argc, argv)
70 int argc;
71 char *argv[];
72 {
73 size_t len;
74 off_t filesize;
75 int ch;
76 int exitstatus = EXIT_SUCCESS;
77 int iflag = 0;
78 int qflag = 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 warn("madvise failed, ignoring");
226
227 /*
228 * get the header length and set up the audio device
229 */
230 if ((hdrlen = audioctl_write_fromhdr(addr,
231 (size_t)filesize, ctlfd)) < 0) {
232 if (play_errstring)
233 errx(1, "%s: %s", play_errstring, *argv);
234 else
235 errx(1, "unknown audio file: %s", *argv);
236 }
237
238 filesize -= hdrlen;
239 addr = (char *)addr + hdrlen;
240
241 while (filesize > bufsize) {
242 if (write(audiofd, addr, bufsize) != bufsize)
243 err(1, "write failed");
244 addr = (char *)addr + bufsize;
245 filesize -= bufsize;
246 }
247 if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
248 err(1, "final write failed");
249
250 if (ioctl(audiofd, AUDIO_DRAIN) < 0)
251 warn("audio drain ioctl failed");
252 if (munmap(oaddr, (size_t)filesize) < 0)
253 err(1, "munmap failed");
254
255 close(fd);
256
257 } while (*++argv);
258 } else {
259 play_fd(STDIN_FILENO, "standard input");
260 }
261
262 exit(exitstatus);
263 }
264
265 /*
266 * play the file on on the file descriptor fd
267 */
268 void
269 play_fd(fd, file)
270 int fd;
271 char *file;
272 {
273 char *buffer = malloc(bufsize);
274 ssize_t hdrlen;
275 int n, m;
276
277 if (buffer == NULL)
278 err(1, "malloc of read buffer failed");
279
280 n = read(fd, buffer, bufsize);
281
282 if (n < 0)
283 err(1, "read of standard input failed");
284 if (n == 0)
285 errx(1, "EOF on standard input");
286
287 hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd);
288 if (hdrlen < 0) {
289 if (play_errstring)
290 errx(1, "%s: %s", play_errstring, file);
291 else
292 errx(1, "unknown audio file: %s", file);
293 }
294
295 /* advance the buffer if we have to */
296 if (hdrlen > 0) {
297 /* shouldn't happen */
298 if (hdrlen > n)
299 err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
300
301 memmove(buffer, buffer + hdrlen, n - hdrlen);
302
303 m = read(fd, buffer + n, hdrlen);
304 n += m;
305 }
306 /* read until EOF or error */
307 do {
308 if (n == -1)
309 err(1, "read of standard input failed");
310 if (write(audiofd, buffer, n) != n)
311 err(1, "write failed");
312 } while ((n = read(fd, buffer, bufsize)));
313
314 if (ioctl(audiofd, AUDIO_DRAIN) < 0)
315 warn("audio drain ioctl failed");
316 }
317
318 /*
319 * only support sun and wav audio files so far ...
320 *
321 * XXX this should probably be mostly part of libaudio, but it
322 * uses the local "info" variable. blah... fix me!
323 */
324 ssize_t
325 audioctl_write_fromhdr(hdr, fsz, fd)
326 void *hdr;
327 size_t fsz;
328 int fd;
329 {
330 sun_audioheader *sunhdr;
331 ssize_t hdr_len;
332
333 AUDIO_INITINFO(&info);
334 sunhdr = hdr;
335 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
336 if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
337 &info.play.encoding, &info.play.precision)) {
338 warnx("unknown unsupported Sun audio encoding format %d",
339 ntohl(sunhdr->encoding));
340 if (fflag)
341 goto set_audio_mode;
342 return (-1);
343 }
344
345 info.play.sample_rate = ntohl(sunhdr->sample_rate);
346 info.play.channels = ntohl(sunhdr->channels);
347 hdr_len = ntohl(sunhdr->hdr_size);
348
349 goto set_audio_mode;
350 }
351
352 hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
353 &info.play.precision, &info.play.sample_rate, &info.play.channels);
354
355 switch (hdr_len) {
356 case AUDIO_ESHORTHDR:
357 case AUDIO_EWAVUNSUPP:
358 case AUDIO_EWAVBADPCM:
359 case AUDIO_EWAVNODATA:
360 play_errstring = audio_errstring(hdr_len);
361 /* FALL THROUGH */
362 case AUDIO_ENOENT:
363 break;
364 default:
365 if (hdr_len < 1)
366 break;
367 goto set_audio_mode;
368 }
369 /*
370 * if we don't know it, bail unless we are forcing.
371 */
372 if (fflag == 0)
373 return (-1);
374 set_audio_mode:
375 if (port)
376 info.play.port = port;
377 if (volume)
378 info.play.gain = volume;
379 if (balance)
380 info.play.balance = balance;
381 if (fflag) {
382 if (sample_rate)
383 info.play.sample_rate = sample_rate;
384 if (channels)
385 info.play.channels = channels;
386 if (encoding)
387 info.play.encoding = encoding;
388 if (precision)
389 info.play.precision = precision;
390 hdr_len = 0;
391 }
392 info.mode = AUMODE_PLAY_ALL;
393
394 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
395 err(1, "failed to set audio info");
396
397 return (hdr_len);
398 }
399
400 void
401 usage()
402 {
403 extern char *__progname;
404
405 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
406 fprintf(stderr, "Options:\n\t"
407 "-C audio control device\n\t"
408 "-b balance (0-63)\n\t"
409 "-d audio device\n\t"
410 "-f force settings\n\t"
411 "\t-c forced channels\n\t"
412 "\t-e forced encoding\n\t"
413 "\t-P forced precision\n\t"
414 "\t-s forced sample rate\n\t"
415 "-i header information\n\t"
416 "-m monitor volume\n\t"
417 "-p output port\n\t"
418 "-v volume\n");
419 exit(EXIT_FAILURE);
420 }
421