play.c revision 1.5 1 /* $NetBSD: play.c,v 1.5 1999/03/27 18:16:23 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 ssize_t audioctl_write_fromhdr __P((void *, size_t, int, int));
51
52 audio_info_t info;
53 int volume = 0;
54 int balance = 0;
55 int port = 0;
56 char const *play_errstring = NULL;
57
58 int
59 main(argc, argv)
60 int argc;
61 char *argv[];
62 {
63 size_t len, bufsize;
64 ssize_t hdrlen;
65 off_t filesize;
66 int ch, audiofd, ctlfd;
67 int iflag = 0;
68 int qflag = 0;
69 int verbose = 0;
70 char *device = 0;
71 char *ctldev = 0;
72
73 while ((ch = getopt(argc, argv, "b:C:d:hiqp:Vv:")) != -1) {
74 switch (ch) {
75 case 'b':
76 decode_int(optarg, &balance);
77 if (balance < 0 || balance > 64)
78 errx(1, "balance must be between 0 and 64\n");
79 break;
80 case 'C':
81 ctldev = optarg;
82 break;
83 case 'd':
84 device = optarg;
85 break;
86 case 'i':
87 iflag++;
88 break;
89 case 'q':
90 qflag++;
91 break;
92 case 'p':
93 len = strlen(optarg);
94
95 if (strncmp(optarg, "speaker", len) == 0)
96 port |= AUDIO_SPEAKER;
97 else if (strncmp(optarg, "headphone", len) == 0)
98 port |= AUDIO_HEADPHONE;
99 else if (strncmp(optarg, "line", len) == 0)
100 port |= AUDIO_LINE_OUT;
101 else
102 errx(1,
103 "port must be `speaker', `headphone', or `line'");
104 break;
105 case 'V':
106 verbose++;
107 break;
108 case 'v':
109 volume = atoi(optarg);
110 if (volume < 0 || volume > 255)
111 errx(1, "volume must be between 0 and 255\n");
112 break;
113 /* case 'h': */
114 default:
115 usage();
116 /* NOTREACHED */
117 }
118 }
119 argc -= optind;
120 argv += optind;
121
122 if (device == NULL)
123 device = _PATH_AUDIO;
124 if (ctldev == NULL)
125 ctldev = _PATH_AUDIOCTL;
126
127 audiofd = open(device, O_WRONLY);
128 #ifdef _PATH_OAUDIO
129 /* Allow the non-unit device to be used. */
130 if (audiofd < 0 && device == _PATH_AUDIO) {
131 device = _PATH_OAUDIO;
132 ctldev = _PATH_OAUDIOCTL;
133 audiofd = open(device, O_WRONLY);
134 }
135 #endif
136 if (audiofd < 0)
137 err(1, "failed to open %s", device);
138 ctlfd = open(ctldev, O_RDWR);
139 if (ctlfd < 0)
140 err(1, "failed to open %s", ctldev);
141
142 if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
143 err(1, "failed to get audio info");
144 bufsize = info.play.buffer_size;
145 if (bufsize < 32 * 1024)
146 bufsize = 32 * 1024;
147
148 if (*argv) {
149 int fd;
150 struct stat sb;
151 void *addr, *oaddr;
152
153 do {
154 fd = open(*argv, O_RDONLY);
155 if (fd < 0)
156 err(1, "could not open %s", *argv);
157
158 if (fstat(fd, &sb) < 0)
159 err(1, "could not fstat %s", *argv);
160 filesize = sb.st_size;
161
162 oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
163 MAP_SHARED, fd, 0);
164 if (addr == (void *)-1)
165 err(1, "could not mmap %s", *argv);
166
167 if ((hdrlen = audioctl_write_fromhdr(addr,
168 (size_t)filesize, ctlfd, 1)) < 0) {
169 play_error:
170 if (play_errstring)
171 errx(1, "%s: %s", play_errstring, *argv);
172 else
173 errx(1, "unknown audio file: %s", *argv);
174 }
175
176 filesize -= hdrlen;
177 (char *)addr += hdrlen;
178
179 while (filesize > bufsize) {
180 if (write(audiofd, addr, bufsize) != bufsize)
181 err(1, "write failed");
182 (char *)addr += bufsize;
183 filesize -= bufsize;
184 }
185 if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
186 err(1, "final write failed");
187
188 if (munmap(oaddr, (size_t)filesize) < 0)
189 err(1, "munmap failed");
190
191 close(fd);
192
193 } while (*++argv);
194 } else {
195 char *buffer = malloc(bufsize);
196 int n, m;
197
198 if (buffer == NULL)
199 err(1, "malloc of read buffer failed");
200
201 n = read(STDIN_FILENO, buffer, bufsize);
202
203 if (n < 0)
204 err(1, "read of standard input failed");
205 if (n == 0)
206 errx(1, "EOF on standard input");
207
208 hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd, 1);
209 if (hdrlen < 0)
210 goto play_error;
211
212 /* advance the buffer if we have to */
213 if (hdrlen > 0) {
214 /* shouldn't happen */
215 if (hdrlen > n)
216 err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
217
218 memmove(buffer, buffer + hdrlen, n - hdrlen);
219
220 m = read(STDIN_FILENO, buffer + n, hdrlen);
221 n += m;
222 }
223 /* read until EOF or error */
224 do {
225 if (n == -1)
226 err(1, "read of standard input failed");
227 if (write(audiofd, buffer, n) != n)
228 err(1, "write failed");
229 } while ((n = read(STDIN_FILENO, buffer, bufsize)));
230 }
231
232 exit(0);
233 }
234
235 /*
236 * only support sun and wav audio files so far ...
237 *
238 * XXX this should probably be mostly part of libaudio, but it
239 * uses the local "info" variable. blah... fix me!
240 */
241 ssize_t
242 audioctl_write_fromhdr(hdr, fsz, fd, unknown_ok)
243 void *hdr;
244 size_t fsz;
245 int fd;
246 int unknown_ok;
247 {
248 sun_audioheader *sunhdr;
249 ssize_t hdr_len;
250
251 AUDIO_INITINFO(&info);
252 sunhdr = hdr;
253 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
254 if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
255 &info.play.encoding, &info.play.precision)) {
256 warnx("unknown supported Sun audio encoding format %d",
257 sunhdr->encoding);
258 return (-1);
259 }
260
261 info.play.sample_rate = ntohl(sunhdr->sample_rate);
262 info.play.channels = ntohl(sunhdr->channels);
263 if (port)
264 info.play.port = port;
265 if (volume)
266 info.play.gain = volume;
267 if (balance)
268 info.play.balance = balance;
269 info.mode = AUMODE_PLAY_ALL;
270
271 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
272 err(1, "failed to set audio info");
273
274 return (ntohl(sunhdr->hdr_size));
275 }
276
277 hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
278 &info.play.precision, &info.play.sample_rate, &info.play.channels);
279
280 switch (hdr_len) {
281 case AUDIO_ESHORTHDR:
282 case AUDIO_EWAVUNSUPP:
283 case AUDIO_EWAVBADPCM:
284 case AUDIO_EWAVNODATA:
285 if (unknown_ok == 0)
286 play_errstring = audio_errstring(hdr_len);
287 /* FALL THROUGH */
288 case AUDIO_ENOENT:
289 break;
290 default:
291 if (hdr_len < 1)
292 break;
293 if (port)
294 info.play.port = port;
295 if (volume)
296 info.play.gain = volume;
297 if (balance)
298 info.play.balance = balance;
299 info.mode = AUMODE_PLAY_ALL;
300
301 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
302 err(1, "failed to set audio info");
303
304 return (hdr_len);
305 }
306 return (unknown_ok ? 0 : -1);
307 }
308
309 void
310 usage()
311 {
312 extern char *__progname;
313
314 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
315 fprintf(stderr, "Options:\n\t"
316 "-C audio control device\n\t"
317 "-b balance (0-63)\n\t"
318 "-d audio device\n\t"
319 "-i header information\n\t"
320 "-m monitor volume\n\t"
321 "-p output port\n\t"
322 "-v volume\n");
323 exit(0);
324 }
325