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