play.c revision 1.55.16.1 1 /* $NetBSD: play.c,v 1.55.16.1 2019/06/10 22:10:17 christos Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2000, 2001, 2002, 2010 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28 #include <sys/cdefs.h>
29
30 #ifndef lint
31 __RCSID("$NetBSD: play.c,v 1.55.16.1 2019/06/10 22:10:17 christos Exp $");
32 #endif
33
34
35 #include <sys/param.h>
36 #include <sys/audioio.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40
41 #include <err.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <util.h>
49
50 #include <paths.h>
51
52 #include "libaudio.h"
53
54 static void usage(void) __dead;
55 static void play(char *);
56 static void play_fd(const char *, int);
57 static ssize_t audioctl_write_fromhdr(void *, size_t, int, off_t *, const char *);
58 static void cleanup(int) __dead;
59
60 static audio_info_t info;
61 static int volume;
62 static int balance;
63 static int port;
64 static int fflag;
65 static int qflag;
66 int verbose;
67 static int sample_rate;
68 static int encoding;
69 static char *encoding_str;
70 static int precision;
71 static int channels;
72
73 static char const *play_errstring = NULL;
74 static size_t bufsize;
75 static int audiofd;
76 static int exitstatus = EXIT_SUCCESS;
77
78 int
79 main(int argc, char *argv[])
80 {
81 size_t len;
82 int ch;
83 int iflag = 0;
84 const char *defdevice = _PATH_SOUND;
85 const char *device = NULL;
86
87 while ((ch = getopt(argc, argv, "b:B:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
88 switch (ch) {
89 case 'b':
90 decode_int(optarg, &balance);
91 if (balance < 0 || balance > 64)
92 errx(1, "balance must be between 0 and 63");
93 break;
94 case 'B':
95 bufsize = strsuftoll("write buffer size", optarg,
96 1, UINT_MAX);
97 break;
98 case 'c':
99 decode_int(optarg, &channels);
100 if (channels < 0)
101 errx(1, "channels must be positive");
102 break;
103 case 'C':
104 /* Ignore, compatibility */
105 break;
106 case 'd':
107 device = optarg;
108 break;
109 case 'e':
110 encoding_str = optarg;
111 break;
112 case 'f':
113 fflag = 1;
114 break;
115 case 'i':
116 iflag++;
117 break;
118 case 'q':
119 qflag++;
120 break;
121 case 'P':
122 decode_int(optarg, &precision);
123 if (precision != 4 && precision != 8 &&
124 precision != 16 && precision != 24 &&
125 precision != 32)
126 errx(1, "precision must be between 4, 8, 16, 24 or 32");
127 break;
128 case 'p':
129 len = strlen(optarg);
130
131 if (strncmp(optarg, "speaker", len) == 0)
132 port |= AUDIO_SPEAKER;
133 else if (strncmp(optarg, "headphone", len) == 0)
134 port |= AUDIO_HEADPHONE;
135 else if (strncmp(optarg, "line", len) == 0)
136 port |= AUDIO_LINE_OUT;
137 else
138 errx(1,
139 "port must be `speaker', `headphone', or `line'");
140 break;
141 case 's':
142 decode_int(optarg, &sample_rate);
143 if (sample_rate < 0 || sample_rate > 48000 * 2) /* XXX */
144 errx(1, "sample rate must be between 0 and 96000");
145 break;
146 case 'V':
147 verbose++;
148 break;
149 case 'v':
150 volume = atoi(optarg);
151 if (volume < 0 || volume > 255)
152 errx(1, "volume must be between 0 and 255");
153 break;
154 /* case 'h': */
155 default:
156 usage();
157 /* NOTREACHED */
158 }
159 }
160 argc -= optind;
161 argv += optind;
162
163 if (encoding_str) {
164 encoding = audio_enc_to_val(encoding_str);
165 if (encoding == -1)
166 errx(1, "unknown encoding, bailing...");
167 }
168
169 if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
170 (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
171 device = defdevice;
172
173 audiofd = open(device, O_WRONLY);
174 if (audiofd < 0 && device == defdevice) {
175 device = _PATH_SOUND0;
176 audiofd = open(device, O_WRONLY);
177 }
178
179 if (audiofd < 0)
180 err(1, "failed to open %s", device);
181
182 if (ioctl(audiofd, AUDIO_GETINFO, &info) < 0)
183 err(1, "failed to get audio info");
184 if (bufsize == 0) {
185 bufsize = info.play.buffer_size;
186 if (bufsize < 32 * 1024)
187 bufsize = 32 * 1024;
188 }
189
190 signal(SIGINT, cleanup);
191 signal(SIGTERM, cleanup);
192 signal(SIGHUP, cleanup);
193
194 if (*argv)
195 do
196 play(*argv++);
197 while (*argv);
198 else
199 play_fd("standard input", STDIN_FILENO);
200
201 cleanup(0);
202 }
203
204 static void
205 cleanup(int signo)
206 {
207
208 (void)ioctl(audiofd, AUDIO_FLUSH, NULL);
209 (void)ioctl(audiofd, AUDIO_SETINFO, &info);
210 close(audiofd);
211 if (signo != 0) {
212 (void)raise_default_signal(signo);
213 }
214 exit(exitstatus);
215 }
216
217 static void
218 play(char *file)
219 {
220 struct stat sb;
221 void *addr, *oaddr;
222 off_t filesize;
223 size_t sizet_filesize;
224 off_t datasize = 0;
225 ssize_t hdrlen;
226 int fd;
227 int nw;
228
229 if (file[0] == '-' && file[1] == 0) {
230 play_fd("standard input", STDIN_FILENO);
231 return;
232 }
233
234 fd = open(file, O_RDONLY);
235 if (fd < 0) {
236 if (!qflag)
237 warn("could not open %s", file);
238 exitstatus = EXIT_FAILURE;
239 return;
240 }
241
242 if (fstat(fd, &sb) < 0)
243 err(1, "could not fstat %s", file);
244 filesize = sb.st_size;
245 sizet_filesize = (size_t)filesize;
246
247 /*
248 * if the file is not a regular file, doesn't fit in a size_t,
249 * or if we failed to mmap the file, try to read it instead, so
250 * that filesystems, etc, that do not support mmap() work
251 */
252 if (!S_ISREG(sb.st_mode) ||
253 ((off_t)sizet_filesize != filesize) ||
254 (oaddr = addr = mmap(0, sizet_filesize, PROT_READ,
255 MAP_SHARED, fd, 0)) == MAP_FAILED) {
256 play_fd(file, fd);
257 close(fd);
258 return;
259 }
260
261 /*
262 * give the VM system a bit of a hint about the type
263 * of accesses we will make. we don't care about errors.
264 */
265 madvise(addr, sizet_filesize, MADV_SEQUENTIAL);
266
267 /*
268 * get the header length and set up the audio device
269 */
270 if ((hdrlen = audioctl_write_fromhdr(addr,
271 sizet_filesize, audiofd, &datasize, file)) < 0) {
272 if (play_errstring)
273 errx(1, "%s: %s", play_errstring, file);
274 else
275 errx(1, "unknown audio file: %s", file);
276 }
277
278 filesize -= hdrlen;
279 addr = (char *)addr + hdrlen;
280 if (filesize < datasize || datasize == 0) {
281 if (filesize < datasize)
282 warnx("bogus datasize: %ld", (u_long)datasize);
283 datasize = filesize;
284 }
285
286 while ((uint64_t)datasize > bufsize) {
287 nw = write(audiofd, addr, bufsize);
288 if (nw == -1)
289 err(1, "write failed");
290 if ((size_t)nw != bufsize)
291 errx(1, "write failed");
292 addr = (char *)addr + bufsize;
293 datasize -= bufsize;
294 }
295 nw = write(audiofd, addr, datasize);
296 if (nw == -1)
297 err(1, "final write failed");
298 if ((off_t)nw != datasize)
299 errx(1, "final write failed");
300
301 if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
302 warn("audio drain ioctl failed");
303 if (munmap(oaddr, sizet_filesize) < 0)
304 err(1, "munmap failed");
305
306 close(fd);
307 }
308
309 /*
310 * play the file on the file descriptor fd
311 */
312 static void
313 play_fd(const char *file, int fd)
314 {
315 char *buffer = malloc(bufsize);
316 ssize_t hdrlen;
317 int nr, nw;
318 off_t datasize = 0;
319 off_t dataout = 0;
320
321 if (buffer == NULL)
322 err(1, "malloc of read buffer failed");
323
324 nr = read(fd, buffer, bufsize);
325 if (nr < 0)
326 goto read_error;
327 if (nr == 0) {
328 if (fflag) {
329 free(buffer);
330 return;
331 }
332 err(1, "unexpected EOF");
333 }
334 hdrlen = audioctl_write_fromhdr(buffer, nr, audiofd, &datasize, file);
335 if (hdrlen < 0) {
336 if (play_errstring)
337 errx(1, "%s: %s", play_errstring, file);
338 else
339 errx(1, "unknown audio file: %s", file);
340 }
341 if (hdrlen > 0) {
342 if (hdrlen > nr) /* shouldn't happen */
343 errx(1, "header seems really large: %lld", (long long)hdrlen);
344 memmove(buffer, buffer + hdrlen, nr - hdrlen);
345 nr -= hdrlen;
346 }
347 while (datasize == 0 || dataout < datasize) {
348 if (datasize != 0 && dataout + nr > datasize)
349 nr = datasize - dataout;
350 nw = write(audiofd, buffer, nr);
351 if (nw == -1)
352 err(1, "audio device write failed");
353 if (nw != nr)
354 errx(1, "audio device write failed");
355 dataout += nw;
356 nr = read(fd, buffer, bufsize);
357 if (nr == -1)
358 goto read_error;
359 if (nr == 0)
360 break;
361 }
362 /* something to think about: no message given for dataout < datasize */
363 if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
364 warn("audio drain ioctl failed");
365 return;
366 read_error:
367 err(1, "read of standard input failed");
368 }
369
370 /*
371 * only support sun and wav audio files so far ...
372 *
373 * XXX this should probably be mostly part of libaudio, but it
374 * uses the local "info" variable. blah... fix me!
375 */
376 static ssize_t
377 audioctl_write_fromhdr(void *hdr, size_t fsz, int fd, off_t *datasize, const char *file)
378 {
379 sun_audioheader *sunhdr;
380 ssize_t hdr_len = 0;
381
382 AUDIO_INITINFO(&info);
383 sunhdr = hdr;
384 if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
385 if (audio_sun_to_encoding(ntohl(sunhdr->encoding),
386 &info.play.encoding, &info.play.precision)) {
387 if (!qflag)
388 warnx("unknown unsupported Sun audio encoding"
389 " format %d", ntohl(sunhdr->encoding));
390 if (fflag)
391 goto set_audio_mode;
392 return (-1);
393 }
394
395 info.play.sample_rate = ntohl(sunhdr->sample_rate);
396 info.play.channels = ntohl(sunhdr->channels);
397 hdr_len = ntohl(sunhdr->hdr_size);
398
399 *datasize = (off_t)ntohl(sunhdr->data_size);
400 goto set_audio_mode;
401 }
402
403 hdr_len = audio_wav_parse_hdr(hdr, fsz, &info.play.encoding,
404 &info.play.precision, &info.play.sample_rate, &info.play.channels,
405 datasize);
406
407 switch (hdr_len) {
408 case AUDIO_ESHORTHDR:
409 case AUDIO_EWAVUNSUPP:
410 case AUDIO_EWAVBADPCM:
411 case AUDIO_EWAVNODATA:
412 play_errstring = audio_errstring(hdr_len);
413 /* FALL THROUGH */
414 case AUDIO_ENOENT:
415 break;
416 default:
417 if (hdr_len < 1)
418 break;
419 goto set_audio_mode;
420 }
421 /*
422 * if we don't know it, bail unless we are forcing.
423 */
424 if (fflag == 0)
425 return (-1);
426 set_audio_mode:
427 if (port)
428 info.play.port = port;
429 if (volume)
430 info.play.gain = volume;
431 if (balance)
432 info.play.balance = balance;
433 if (fflag) {
434 if (sample_rate)
435 info.play.sample_rate = sample_rate;
436 if (channels)
437 info.play.channels = channels;
438 if (encoding)
439 info.play.encoding = encoding;
440 if (precision)
441 info.play.precision = precision;
442 hdr_len = 0;
443 }
444 info.mode = AUMODE_PLAY_ALL;
445
446 if (verbose) {
447 const char *enc = audio_enc_from_val(info.play.encoding);
448
449 printf("%s: sample_rate=%d channels=%d "
450 "datasize=%lld "
451 "precision=%d%s%s\n", file,
452 info.play.sample_rate,
453 info.play.channels,
454 (long long)*datasize,
455 info.play.precision,
456 enc ? " encoding=" : "",
457 enc ? enc : "");
458 }
459
460 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
461 err(1, "failed to set audio info");
462
463 return (hdr_len);
464 }
465
466 static void
467 usage(void)
468 {
469
470 fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", getprogname());
471 fprintf(stderr, "Options:\n\t"
472 "-B buffer size\n\t"
473 "-b balance (0-63)\n\t"
474 "-d audio device\n\t"
475 "-f force settings\n\t"
476 "\t-c forced channels\n\t"
477 "\t-e forced encoding\n\t"
478 "\t-P forced precision\n\t"
479 "\t-s forced sample rate\n\t"
480 "-i header information\n\t"
481 "-p output port\n\t"
482 "-v volume\n");
483 exit(EXIT_FAILURE);
484 }
485