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