play.c revision 1.7.2.5 1 /* $NetBSD: play.c,v 1.7.2.5 1999/11/11 21:18:14 he 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 ssize_t audioctl_write_fromhdr __P((void *, size_t, int));
51
52 audio_info_t info;
53 int volume;
54 int balance;
55 int port;
56 int fflag;
57 int sample_rate;
58 int encoding;
59 char *encoding_str;
60 int precision;
61 int channels;
62
63 char const *play_errstring = NULL;
64
65 int
66 main(argc, argv)
67 int argc;
68 char *argv[];
69 {
70 size_t len, bufsize;
71 ssize_t hdrlen;
72 off_t filesize;
73 int ch, audiofd, ctlfd;
74 int exitstatus = EXIT_SUCCESS;
75 int iflag = 0;
76 int qflag = 0;
77 int verbose = 0;
78 char *device = 0;
79 char *ctldev = 0;
80
81 while ((ch = getopt(argc, argv, "b:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
82 switch (ch) {
83 case 'b':
84 decode_int(optarg, &balance);
85 if (balance < 0 || balance > 64)
86 errx(1, "balance must be between 0 and 64");
87 break;
88 case 'c':
89 decode_int(optarg, &channels);
90 if (channels < 0)
91 errx(1, "channels must be positive");
92 break;
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 && (device = getenv("AUDIODEVICE")) == NULL &&
159 (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
160 device = _PATH_AUDIO;
161 if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == NULL)
162 ctldev = _PATH_AUDIOCTL;
163
164 audiofd = open(device, O_WRONLY);
165 #ifdef _PATH_OAUDIO
166 /* Allow the non-unit device to be used. */
167 if (audiofd < 0 && device == _PATH_AUDIO) {
168 device = _PATH_OAUDIO;
169 ctldev = _PATH_OAUDIOCTL;
170 audiofd = open(device, O_WRONLY);
171 }
172 #endif
173 if (audiofd < 0)
174 err(1, "failed to open %s", device);
175 ctlfd = open(ctldev, O_RDWR);
176 if (ctlfd < 0)
177 err(1, "failed to open %s", ctldev);
178
179 if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
180 err(1, "failed to get audio info");
181 bufsize = info.play.buffer_size;
182 if (bufsize < 32 * 1024)
183 bufsize = 32 * 1024;
184
185 if (*argv) {
186 int fd;
187 struct stat sb;
188 void *addr, *oaddr;
189
190 do {
191 fd = open(*argv, O_RDONLY);
192 if (fd < 0) {
193 warn("could not open %s", *argv);
194 exitstatus = EXIT_FAILURE;
195 continue;
196 }
197
198 if (fstat(fd, &sb) < 0)
199 err(1, "could not fstat %s", *argv);
200 filesize = sb.st_size;
201
202 oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
203 MAP_SHARED, fd, 0);
204 if (addr == MAP_FAILED)
205 err(1, "could not mmap %s", *argv);
206
207 if ((hdrlen = audioctl_write_fromhdr(addr,
208 (size_t)filesize, ctlfd)) < 0) {
209 play_error:
210 if (play_errstring)
211 errx(1, "%s: %s", play_errstring, *argv);
212 else
213 errx(1, "unknown audio file: %s", *argv);
214 }
215
216 filesize -= hdrlen;
217 addr = (char *)addr + hdrlen;
218
219 while (filesize > bufsize) {
220 if (write(audiofd, addr, bufsize) != bufsize)
221 err(1, "write failed");
222 addr = (char *)addr + bufsize;
223 filesize -= bufsize;
224 }
225 if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
226 err(1, "final write failed");
227
228 ioctl(audiofd, AUDIO_DRAIN);
229 if (munmap(oaddr, (size_t)filesize) < 0)
230 err(1, "munmap failed");
231
232 close(fd);
233
234 } while (*++argv);
235 } else {
236 char *buffer = malloc(bufsize);
237 int n, m;
238
239 if (buffer == NULL)
240 err(1, "malloc of read buffer failed");
241
242 n = read(STDIN_FILENO, buffer, bufsize);
243
244 if (n < 0)
245 err(1, "read of standard input failed");
246 if (n == 0)
247 errx(1, "EOF on standard input");
248
249 hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd);
250 if (hdrlen < 0)
251 goto play_error;
252
253 /* advance the buffer if we have to */
254 if (hdrlen > 0) {
255 /* shouldn't happen */
256 if (hdrlen > n)
257 err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
258
259 memmove(buffer, buffer + hdrlen, n - hdrlen);
260
261 m = read(STDIN_FILENO, buffer + n, hdrlen);
262 n += m;
263 }
264 /* read until EOF or error */
265 do {
266 if (n == -1)
267 err(1, "read of standard input failed");
268 if (write(audiofd, buffer, n) != n)
269 err(1, "write failed");
270 } while ((n = read(STDIN_FILENO, buffer, bufsize)));
271 ioctl(audiofd, AUDIO_DRAIN);
272 }
273
274 exit(exitstatus);
275 }
276
277 /*
278 * only support sun and wav audio files so far ...
279 *
280 * XXX this should probably be mostly part of libaudio, but it
281 * uses the local "info" variable. blah... fix me!
282 */
283 ssize_t
284 audioctl_write_fromhdr(hdr, fsz, fd)
285 void *hdr;
286 size_t fsz;
287 int fd;
288 {
289 sun_audioheader *sunhdr;
290 ssize_t hdr_len;
291
292 AUDIO_INITINFO(&info);
293 sunhdr = hdr;
294 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
295 if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
296 &info.play.encoding, &info.play.precision)) {
297 warnx("unknown unsupported Sun audio encoding format %d",
298 ntohl(sunhdr->encoding));
299 if (fflag)
300 goto set_audio_mode;
301 return (-1);
302 }
303
304 info.play.sample_rate = ntohl(sunhdr->sample_rate);
305 info.play.channels = ntohl(sunhdr->channels);
306 hdr_len = ntohl(sunhdr->hdr_size);
307
308 goto set_audio_mode;
309 }
310
311 hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
312 &info.play.precision, &info.play.sample_rate, &info.play.channels);
313
314 switch (hdr_len) {
315 case AUDIO_ESHORTHDR:
316 case AUDIO_EWAVUNSUPP:
317 case AUDIO_EWAVBADPCM:
318 case AUDIO_EWAVNODATA:
319 play_errstring = audio_errstring(hdr_len);
320 /* FALL THROUGH */
321 case AUDIO_ENOENT:
322 break;
323 default:
324 if (hdr_len < 1)
325 break;
326 goto set_audio_mode;
327 }
328 /*
329 * if we don't know it, bail unless we are forcing.
330 */
331 if (fflag == 0)
332 return (-1);
333 set_audio_mode:
334 if (port)
335 info.play.port = port;
336 if (volume)
337 info.play.gain = volume;
338 if (balance)
339 info.play.balance = balance;
340 if (fflag) {
341 if (sample_rate)
342 info.play.sample_rate = sample_rate;
343 if (channels)
344 info.play.channels = channels;
345 if (encoding)
346 info.play.encoding = encoding;
347 if (precision)
348 info.play.encoding = precision;
349 }
350 info.mode = AUMODE_PLAY_ALL;
351
352 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
353 err(1, "failed to set audio info");
354
355 return (hdr_len);
356 }
357
358 void
359 usage()
360 {
361 extern char *__progname;
362
363 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
364 fprintf(stderr, "Options:\n\t"
365 "-C audio control device\n\t"
366 "-b balance (0-63)\n\t"
367 "-d audio device\n\t"
368 "-f force settings\n\t"
369 "\t-c forced channels\n\t"
370 "\t-e forced encoding\n\t"
371 "\t-P forced precision\n\t"
372 "\t-s forced sample rate\n\t"
373 "-i header information\n\t"
374 "-m monitor volume\n\t"
375 "-p output port\n\t"
376 "-v volume\n");
377 exit(EXIT_FAILURE);
378 }
379