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