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