play.c revision 1.2 1 #include <sys/param.h>
2 #include <sys/audioio.h>
3 #include <sys/ioctl.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6
7 #include <err.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <paths.h>
15
16 #include "libaudio.h"
17
18 int main __P((int, char *[]));
19 void usage __P((void));
20 ssize_t audioctl_write_fromhdr __P((void *, size_t, int, int));
21
22 audio_info_t info;
23 int volume = 0;
24 int balance = 0;
25 int port = 0;
26 char const *play_errstring = NULL;
27
28 int
29 main(argc, argv)
30 int argc;
31 char *argv[];
32 {
33 size_t len, bufsize;
34 ssize_t hdrlen;
35 off_t filesize;
36 int ch, audiofd, ctlfd;
37 int iflag = 0;
38 int qflag = 0;
39 int verbose = 0;
40 char *device = 0;
41 char *ctldev = 0;
42
43 while ((ch = getopt(argc, argv, "b:C:d:hiqp:Vv:")) != -1) {
44 switch (ch) {
45 case 'b':
46 decode_int(optarg, &balance);
47 if (balance < 0 || balance > 64)
48 errx(1, "balance must be between 0 and 64\n");
49 break;
50 case 'C':
51 ctldev = optarg;
52 break;
53 case 'd':
54 device = optarg;
55 break;
56 case 'i':
57 iflag++;
58 break;
59 case 'q':
60 qflag++;
61 break;
62 case 'p':
63 len = strlen(optarg);
64
65 if (strncmp(optarg, "speaker", len) == 0)
66 port |= AUDIO_SPEAKER;
67 else if (strncmp(optarg, "headphone", len) == 0)
68 port |= AUDIO_HEADPHONE;
69 else if (strncmp(optarg, "line", len) == 0)
70 port |= AUDIO_LINE_OUT;
71 else
72 errx(1,
73 "port must be `speaker', `headphone', or `line'");
74 break;
75 case 'V':
76 verbose++;
77 break;
78 case 'v':
79 volume = atoi(optarg);
80 if (volume < 0 || volume > 255)
81 errx(1, "volume must be between 0 and 255\n");
82 break;
83 /* case 'h': */
84 default:
85 usage();
86 /* NOTREACHED */
87 }
88 }
89 argc -= optind;
90 argv += optind;
91
92 if (device == NULL)
93 device = _PATH_AUDIO;
94 if (ctldev == NULL)
95 ctldev = _PATH_AUDIOCTL;
96
97 audiofd = open(device, O_WRONLY);
98 #ifdef _PATH_OAUDIO
99 /* Allow the non-unit device to be used. */
100 if (audiofd < 0 && device == _PATH_AUDIO) {
101 device = _PATH_OAUDIO;
102 ctldev = _PATH_OAUDIOCTL;
103 audiofd = open(device, O_WRONLY);
104 }
105 #endif
106 if (audiofd < 0)
107 err(1, "failed to open %s", device);
108 ctlfd = open(ctldev, O_RDWR);
109 if (ctlfd < 0)
110 err(1, "failed to open %s", ctldev);
111
112 if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
113 err(1, "failed to get audio info");
114 bufsize = info.play.buffer_size;
115 if (bufsize < 32 * 1024)
116 bufsize = 32 * 1024;
117
118 if (*argv) {
119 int fd;
120 struct stat sb;
121 void *addr, *oaddr;
122
123 do {
124 fd = open(*argv, O_RDONLY);
125 if (fd < 0)
126 err(1, "could not open %s", *argv);
127
128 if (fstat(fd, &sb) < 0)
129 err(1, "could not fstat %s", *argv);
130 filesize = sb.st_size;
131
132 oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
133 MAP_SHARED, fd, 0);
134 if (addr == (void *)-1)
135 err(1, "could not mmap %s", *argv);
136
137 if ((hdrlen = audioctl_write_fromhdr(addr,
138 (size_t)filesize, ctlfd, 1)) < 0) {
139 if (play_errstring)
140 errx(1, "%s: %s", play_errstring, *argv);
141 else
142 errx(1, "unknown audio file: %s", *argv);
143 }
144
145 filesize -= hdrlen;
146 (char *)addr += hdrlen;
147
148 while (filesize > bufsize) {
149 if (write(audiofd, addr, bufsize) != bufsize)
150 err(1, "write failed");
151 (char *)addr += bufsize;
152 filesize -= bufsize;
153 }
154 if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
155 err(1, "final write failed");
156
157 if (munmap(oaddr, (size_t)filesize) < 0)
158 err(1, "munmap failed");
159
160 close(fd);
161
162 } while (*++argv);
163 } else {
164 /* ... handle stdin */
165 }
166
167 exit(0);
168 }
169
170 /*
171 * only support sun and wav audio files so far ...
172 *
173 * XXX this should probably be mostly part of libaudio, but it
174 * uses the local "info" variable. blah... fix me!
175 */
176 ssize_t
177 audioctl_write_fromhdr(hdr, fsz, fd, unknown_ok)
178 void *hdr;
179 size_t fsz;
180 int fd;
181 int unknown_ok;
182 {
183 sun_audioheader *sunhdr;
184 ssize_t hdr_len;
185
186 AUDIO_INITINFO(&info);
187 sunhdr = hdr;
188 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
189 if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
190 &info.play.encoding, &info.play.precision)) {
191 warnx("unknown supported Sun audio encoding format %d",
192 sunhdr->encoding);
193 return (-1);
194 }
195
196 info.play.sample_rate = ntohl(sunhdr->sample_rate);
197 info.play.channels = ntohl(sunhdr->channels);
198 if (port)
199 info.play.port = port;
200 if (volume)
201 info.play.gain = volume;
202 if (balance)
203 info.play.balance = balance;
204 info.mode = AUMODE_PLAY_ALL;
205
206 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
207 err(1, "failed to set audio info");
208
209 return (ntohl(sunhdr->hdr_size));
210 }
211
212 hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
213 &info.play.precision, &info.play.sample_rate, &info.play.channels);
214
215 switch (hdr_len) {
216 case AUDIO_ESHORTHDR:
217 case AUDIO_EWAVUNSUPP:
218 case AUDIO_EWAVBADPCM:
219 case AUDIO_EWAVNODATA:
220 if (unknown_ok == 0)
221 play_errstring = audio_errstring(hdr_len);
222 /* FALL THROUGH */
223 case AUDIO_ENOENT:
224 break;
225 default:
226 if (hdr_len < 1)
227 break;
228 if (port)
229 info.play.port = port;
230 if (volume)
231 info.play.gain = volume;
232 if (balance)
233 info.play.balance = balance;
234 info.mode = AUMODE_PLAY_ALL;
235
236 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
237 err(1, "failed to set audio info");
238
239 return (hdr_len);
240 }
241 return (unknown_ok ? 0 : -1);
242 }
243
244 void
245 usage()
246 {
247 extern char *__progname;
248
249 fprintf(stderr, "Usage: %s [-iqVh] [-v vol] [-b bal] [-p port] [-d dev]\n\t[-c ctl] [file ...]\n", __progname);
250 exit(0);
251 }
252