play.c revision 1.7.2.4 1 /* $NetBSD: play.c,v 1.7.2.4 1999/10/22 09:37:04 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 iflag = 0;
75 int qflag = 0;
76 int verbose = 0;
77 char *device = 0;
78 char *ctldev = 0;
79
80 while ((ch = getopt(argc, argv, "b:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
81 switch (ch) {
82 case 'b':
83 decode_int(optarg, &balance);
84 if (balance < 0 || balance > 64)
85 errx(1, "balance must be between 0 and 64");
86 break;
87 case 'c':
88 decode_int(optarg, &channels);
89 if (channels < 0)
90 errx(1, "channels must be positive");
91 break;
92 case 'C':
93 ctldev = optarg;
94 break;
95 case 'd':
96 device = optarg;
97 break;
98 case 'e':
99 encoding_str = optarg;
100 break;
101 case 'f':
102 fflag = 1;
103 break;
104 case 'i':
105 iflag++;
106 break;
107 case 'q':
108 qflag++;
109 break;
110 case 'P':
111 decode_int(optarg, &precision);
112 if (precision != 8 && precision != 16 &&
113 precision != 24 && precision != 32)
114 errx(1, "precision must be between 8, 16, 24 or 32");
115 break;
116 case 'p':
117 len = strlen(optarg);
118
119 if (strncmp(optarg, "speaker", len) == 0)
120 port |= AUDIO_SPEAKER;
121 else if (strncmp(optarg, "headphone", len) == 0)
122 port |= AUDIO_HEADPHONE;
123 else if (strncmp(optarg, "line", len) == 0)
124 port |= AUDIO_LINE_OUT;
125 else
126 errx(1,
127 "port must be `speaker', `headphone', or `line'");
128 break;
129 case 's':
130 decode_int(optarg, &sample_rate);
131 if (sample_rate < 0 || sample_rate > 48000 * 2) /* XXX */
132 errx(1, "sample rate must be between 0 and 96000\n");
133 break;
134 case 'V':
135 verbose++;
136 break;
137 case 'v':
138 volume = atoi(optarg);
139 if (volume < 0 || volume > 255)
140 errx(1, "volume must be between 0 and 255\n");
141 break;
142 /* case 'h': */
143 default:
144 usage();
145 /* NOTREACHED */
146 }
147 }
148 argc -= optind;
149 argv += optind;
150
151 if (encoding_str) {
152 encoding = audio_enc_to_val(encoding_str);
153 if (encoding == -1)
154 errx(1, "unknown encoding, bailing...");
155 }
156
157 if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
158 (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
159 device = _PATH_AUDIO;
160 if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == 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 fd = open(*argv, O_RDONLY);
191 if (fd < 0)
192 err(1, "could not open %s", *argv);
193
194 if (fstat(fd, &sb) < 0)
195 err(1, "could not fstat %s", *argv);
196 filesize = sb.st_size;
197
198 oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
199 MAP_SHARED, fd, 0);
200 if (addr == (void *)-1)
201 err(1, "could not mmap %s", *argv);
202
203 if ((hdrlen = audioctl_write_fromhdr(addr,
204 (size_t)filesize, ctlfd)) < 0) {
205 play_error:
206 if (play_errstring)
207 errx(1, "%s: %s", play_errstring, *argv);
208 else
209 errx(1, "unknown audio file: %s", *argv);
210 }
211
212 filesize -= hdrlen;
213 (char *)addr += hdrlen;
214
215 while (filesize > bufsize) {
216 if (write(audiofd, addr, bufsize) != bufsize)
217 err(1, "write failed");
218 (char *)addr += bufsize;
219 filesize -= bufsize;
220 }
221 if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
222 err(1, "final write failed");
223
224 ioctl(audiofd, AUDIO_DRAIN);
225 if (munmap(oaddr, (size_t)filesize) < 0)
226 err(1, "munmap failed");
227
228 close(fd);
229
230 } while (*++argv);
231 } else {
232 char *buffer = malloc(bufsize);
233 int n, m;
234
235 if (buffer == NULL)
236 err(1, "malloc of read buffer failed");
237
238 n = read(STDIN_FILENO, buffer, bufsize);
239
240 if (n < 0)
241 err(1, "read of standard input failed");
242 if (n == 0)
243 errx(1, "EOF on standard input");
244
245 hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd);
246 if (hdrlen < 0)
247 goto play_error;
248
249 /* advance the buffer if we have to */
250 if (hdrlen > 0) {
251 /* shouldn't happen */
252 if (hdrlen > n)
253 err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
254
255 memmove(buffer, buffer + hdrlen, n - hdrlen);
256
257 m = read(STDIN_FILENO, buffer + n, hdrlen);
258 n += m;
259 }
260 /* read until EOF or error */
261 do {
262 if (n == -1)
263 err(1, "read of standard input failed");
264 if (write(audiofd, buffer, n) != n)
265 err(1, "write failed");
266 } while ((n = read(STDIN_FILENO, buffer, bufsize)));
267 ioctl(audiofd, AUDIO_DRAIN);
268 }
269
270 exit(0);
271 }
272
273 /*
274 * only support sun and wav audio files so far ...
275 *
276 * XXX this should probably be mostly part of libaudio, but it
277 * uses the local "info" variable. blah... fix me!
278 */
279 ssize_t
280 audioctl_write_fromhdr(hdr, fsz, fd)
281 void *hdr;
282 size_t fsz;
283 int fd;
284 {
285 sun_audioheader *sunhdr;
286 ssize_t hdr_len;
287
288 AUDIO_INITINFO(&info);
289 sunhdr = hdr;
290 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
291 if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
292 &info.play.encoding, &info.play.precision)) {
293 warnx("unknown unsupported Sun audio encoding format %d",
294 ntohl(sunhdr->encoding));
295 if (fflag)
296 goto set_audio_mode;
297 return (-1);
298 }
299
300 info.play.sample_rate = ntohl(sunhdr->sample_rate);
301 info.play.channels = ntohl(sunhdr->channels);
302 hdr_len = ntohl(sunhdr->hdr_size);
303
304 goto set_audio_mode;
305 }
306
307 hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
308 &info.play.precision, &info.play.sample_rate, &info.play.channels);
309
310 switch (hdr_len) {
311 case AUDIO_ESHORTHDR:
312 case AUDIO_EWAVUNSUPP:
313 case AUDIO_EWAVBADPCM:
314 case AUDIO_EWAVNODATA:
315 play_errstring = audio_errstring(hdr_len);
316 /* FALL THROUGH */
317 case AUDIO_ENOENT:
318 break;
319 default:
320 if (hdr_len < 1)
321 break;
322 goto set_audio_mode;
323 }
324 /*
325 * if we don't know it, bail unless we are forcing.
326 */
327 if (fflag == 0)
328 return (-1);
329 set_audio_mode:
330 if (port)
331 info.play.port = port;
332 if (volume)
333 info.play.gain = volume;
334 if (balance)
335 info.play.balance = balance;
336 if (fflag) {
337 if (sample_rate)
338 info.play.sample_rate = sample_rate;
339 if (channels)
340 info.play.channels = channels;
341 if (encoding)
342 info.play.encoding = encoding;
343 if (precision)
344 info.play.encoding = precision;
345 }
346 info.mode = AUMODE_PLAY_ALL;
347
348 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
349 err(1, "failed to set audio info");
350
351 return (hdr_len);
352 }
353
354 void
355 usage()
356 {
357 extern char *__progname;
358
359 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
360 fprintf(stderr, "Options:\n\t"
361 "-C audio control device\n\t"
362 "-b balance (0-63)\n\t"
363 "-d audio device\n\t"
364 "-f force settings\n\t"
365 "\t-c forced channels\n\t"
366 "\t-e forced encoding\n\t"
367 "\t-P forced precision\n\t"
368 "\t-s forced sample rate\n\t"
369 "-i header information\n\t"
370 "-m monitor volume\n\t"
371 "-p output port\n\t"
372 "-v volume\n");
373 exit(0);
374 }
375