Home | History | Annotate | Line # | Download | only in play
play.c revision 1.21
      1 /*	$NetBSD: play.c,v 1.21 2001/02/05 01:23:24 christos 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 (int, char *[]);
     49 void usage (void);
     50 void play (char *);
     51 void play_fd (char *, int);
     52 ssize_t audioctl_write_fromhdr (void *, size_t, int);
     53 
     54 audio_info_t	info;
     55 int	volume;
     56 int	balance;
     57 int	port;
     58 int	fflag;
     59 int	qflag;
     60 int	sample_rate;
     61 int	encoding;
     62 char	*encoding_str;
     63 int	precision;
     64 int	channels;
     65 
     66 char	const *play_errstring = NULL;
     67 size_t	bufsize;
     68 int	audiofd, ctlfd;
     69 int	exitstatus = EXIT_SUCCESS;
     70 
     71 extern char *__progname;
     72 
     73 int
     74 main(argc, argv)
     75 	int argc;
     76 	char *argv[];
     77 {
     78 	size_t	len;
     79 	int	ch;
     80 	int	iflag = 0;
     81 	int	verbose = 0;
     82 	char	*device = 0;
     83 	char	*ctldev = 0;
     84 
     85 	while ((ch = getopt(argc, argv, "b:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
     86 		switch (ch) {
     87 		case 'b':
     88 			decode_int(optarg, &balance);
     89 			if (balance < 0 || balance > 64)
     90 				errx(1, "balance must be between 0 and 64");
     91 			break;
     92 		case 'c':
     93 			decode_int(optarg, &channels);
     94 			if (channels < 0)
     95 				errx(1, "channels must be positive");
     96 			break;
     97 		case 'C':
     98 			ctldev = optarg;
     99 			break;
    100 		case 'd':
    101 			device = optarg;
    102 			break;
    103 		case 'e':
    104 			encoding_str = optarg;
    105 			break;
    106 		case 'f':
    107 			fflag = 1;
    108 			break;
    109 		case 'i':
    110 			iflag++;
    111 			break;
    112 		case 'q':
    113 			qflag++;
    114 			break;
    115 		case 'P':
    116 			decode_int(optarg, &precision);
    117 			if (precision != 8 && precision != 16 &&
    118 			    precision != 24 && precision != 32)
    119 				errx(1, "precision must be between 8, 16, 24 or 32");
    120 			break;
    121 		case 'p':
    122 			len = strlen(optarg);
    123 
    124 			if (strncmp(optarg, "speaker", len) == 0)
    125 				port |= AUDIO_SPEAKER;
    126 			else if (strncmp(optarg, "headphone", len) == 0)
    127 				port |= AUDIO_HEADPHONE;
    128 			else if (strncmp(optarg, "line", len) == 0)
    129 				port |= AUDIO_LINE_OUT;
    130 			else
    131 				errx(1,
    132 			    "port must be `speaker', `headphone', or `line'");
    133 			break;
    134 		case 's':
    135 			decode_int(optarg, &sample_rate);
    136 			if (sample_rate < 0 || sample_rate > 48000 * 2)	/* XXX */
    137 				errx(1, "sample rate must be between 0 and 96000\n");
    138 			break;
    139 		case 'V':
    140 			verbose++;
    141 			break;
    142 		case 'v':
    143 			volume = atoi(optarg);
    144 			if (volume < 0 || volume > 255)
    145 				errx(1, "volume must be between 0 and 255\n");
    146 			break;
    147 		/* case 'h': */
    148 		default:
    149 			usage();
    150 			/* NOTREACHED */
    151 		}
    152 	}
    153 	argc -= optind;
    154 	argv += optind;
    155 
    156 	if (encoding_str) {
    157 		encoding = audio_enc_to_val(encoding_str);
    158 		if (encoding == -1)
    159 			errx(1, "unknown encoding, bailing...");
    160 	}
    161 
    162 	if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
    163 	    (device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
    164 		device = _PATH_AUDIO;
    165 	if (ctldev == NULL && (ctldev = getenv("AUDIOCTLDEVICE")) == NULL)
    166 		ctldev = _PATH_AUDIOCTL;
    167 
    168 	audiofd = open(device, O_WRONLY);
    169 #ifdef _PATH_OAUDIO
    170 	/* Allow the non-unit device to be used. */
    171 	if (audiofd < 0 && device == _PATH_AUDIO) {
    172 		device = _PATH_OAUDIO;
    173 		ctldev = _PATH_OAUDIOCTL;
    174 		audiofd = open(device, O_WRONLY);
    175 	}
    176 #endif
    177 	if (audiofd < 0)
    178 		err(1, "failed to open %s", device);
    179 	ctlfd = open(ctldev, O_RDWR);
    180 	if (ctlfd < 0)
    181 		err(1, "failed to open %s", ctldev);
    182 
    183 	if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
    184 		err(1, "failed to get audio info");
    185 	bufsize = info.play.buffer_size;
    186 	if (bufsize < 32 * 1024)
    187 		bufsize = 32 * 1024;
    188 
    189 	if (*argv)
    190 		do
    191 			play(*argv++);
    192 		while (*argv);
    193 	else
    194 		play_fd("standard input", STDIN_FILENO);
    195 
    196 	exit(exitstatus);
    197 }
    198 
    199 void
    200 play(file)
    201 	char *file;
    202 {
    203 	struct stat sb;
    204 	void *addr, *oaddr;
    205 	off_t	filesize;
    206 	ssize_t	hdrlen;
    207 	int fd;
    208 
    209 	fd = open(file, O_RDONLY);
    210 	if (fd < 0) {
    211 		if (!qflag)
    212 			warn("could not open %s", file);
    213 		exitstatus = EXIT_FAILURE;
    214 		return;
    215 	}
    216 
    217 	if (fstat(fd, &sb) < 0)
    218 		err(1, "could not fstat %s", file);
    219 	filesize = sb.st_size;
    220 
    221 	oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
    222 	    MAP_SHARED, fd, 0);
    223 
    224 	/*
    225 	 * if we failed to mmap the file, try to read it
    226 	 * instead, so that filesystems, etc, that do not
    227 	 * support mmap() work
    228 	 */
    229 	if (addr == MAP_FAILED) {
    230 		play_fd(file, fd);
    231 		close(fd);
    232 		return;
    233 	}
    234 
    235 	/*
    236 	 * give the VM system a bit of a hint about the type
    237 	 * of accesses we will make.
    238 	 */
    239 	if (madvise(addr, filesize, MADV_SEQUENTIAL) < 0 &&
    240 	    !qflag)
    241 		warn("madvise failed, ignoring");
    242 
    243 	/*
    244 	 * get the header length and set up the audio device
    245 	 */
    246 	if ((hdrlen = audioctl_write_fromhdr(addr,
    247 	    (size_t)filesize, ctlfd)) < 0) {
    248 		if (play_errstring)
    249 			errx(1, "%s: %s", play_errstring, file);
    250 		else
    251 			errx(1, "unknown audio file: %s", file);
    252 	}
    253 
    254 	filesize -= hdrlen;
    255 	addr = (char *)addr + hdrlen;
    256 
    257 	while (filesize > bufsize) {
    258 		if (write(audiofd, addr, bufsize) != bufsize)
    259 			err(1, "write failed");
    260 		addr = (char *)addr + bufsize;
    261 		filesize -= bufsize;
    262 	}
    263 	if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
    264 		err(1, "final write failed");
    265 
    266 	if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
    267 		warn("audio drain ioctl failed");
    268 	if (munmap(oaddr, (size_t)filesize) < 0)
    269 		err(1, "munmap failed");
    270 
    271 	close(fd);
    272 
    273 }
    274 /*
    275  * play the file on on the file descriptor fd
    276  */
    277 void
    278 play_fd(file, fd)
    279 	char    *file;
    280 	int     fd;
    281 {
    282 	char    *buffer = malloc(bufsize);
    283 	ssize_t hdrlen;
    284 	int     n, m;
    285 
    286 	if (buffer == NULL)
    287 		err(1, "malloc of read buffer failed");
    288 
    289 	n = read(fd, buffer, bufsize);
    290 
    291 	if (n < 0)
    292 		err(1, "read of standard input failed");
    293 	if (n == 0)
    294 		errx(1, "EOF on standard input");
    295 
    296 	hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd);
    297 	if (hdrlen < 0) {
    298 		if (play_errstring)
    299 			errx(1, "%s: %s", play_errstring, file);
    300 		else
    301 			errx(1, "unknown audio file: %s", file);
    302 	}
    303 
    304 	/* advance the buffer if we have to */
    305 	if (hdrlen > 0) {
    306 		/* shouldn't happen */
    307 		if (hdrlen > n)
    308 			err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
    309 
    310 		memmove(buffer, buffer + hdrlen, n - hdrlen);
    311 
    312 		m = read(fd, buffer + n, hdrlen);
    313 		n += m;
    314 	}
    315 	/* read until EOF or error */
    316 	do {
    317 		if (n == -1)
    318 			err(1, "read of standard input failed");
    319 		if (write(audiofd, buffer, n) != n)
    320 			err(1, "write failed");
    321 	} while ((n = read(fd, buffer, bufsize)));
    322 
    323 	if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
    324 		warn("audio drain ioctl failed");
    325 }
    326 
    327 /*
    328  * only support sun and wav audio files so far ...
    329  *
    330  * XXX this should probably be mostly part of libaudio, but it
    331  * uses the local "info" variable. blah... fix me!
    332  */
    333 ssize_t
    334 audioctl_write_fromhdr(hdr, fsz, fd)
    335 	void	*hdr;
    336 	size_t	fsz;
    337 	int	fd;
    338 {
    339 	sun_audioheader	*sunhdr;
    340 	ssize_t	hdr_len;
    341 
    342 	AUDIO_INITINFO(&info);
    343 	sunhdr = hdr;
    344 	if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
    345 		if (audio_sun_to_encoding(ntohl(sunhdr->encoding),
    346 		    &info.play.encoding, &info.play.precision)) {
    347 			if (!qflag)
    348 				warnx("unknown unsupported Sun audio encoding"
    349 				      " format %d", ntohl(sunhdr->encoding));
    350 			if (fflag)
    351 				goto set_audio_mode;
    352 			return (-1);
    353 		}
    354 
    355 		info.play.sample_rate = ntohl(sunhdr->sample_rate);
    356 		info.play.channels = ntohl(sunhdr->channels);
    357 		hdr_len = ntohl(sunhdr->hdr_size);
    358 
    359 		goto set_audio_mode;
    360 	}
    361 
    362 	hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
    363 	    &info.play.precision, &info.play.sample_rate, &info.play.channels);
    364 
    365 	switch (hdr_len) {
    366 	case AUDIO_ESHORTHDR:
    367 	case AUDIO_EWAVUNSUPP:
    368 	case AUDIO_EWAVBADPCM:
    369 	case AUDIO_EWAVNODATA:
    370 		play_errstring = audio_errstring(hdr_len);
    371 		/* FALL THROUGH */
    372 	case AUDIO_ENOENT:
    373 		break;
    374 	default:
    375 		if (hdr_len < 1)
    376 			break;
    377 		goto set_audio_mode;
    378 	}
    379 	/*
    380 	 * if we don't know it, bail unless we are forcing.
    381 	 */
    382 	if (fflag == 0)
    383 		return (-1);
    384 set_audio_mode:
    385 	if (port)
    386 		info.play.port = port;
    387 	if (volume)
    388 		info.play.gain = volume;
    389 	if (balance)
    390 		info.play.balance = balance;
    391 	if (fflag) {
    392 		if (sample_rate)
    393 			info.play.sample_rate = sample_rate;
    394 		if (channels)
    395 			info.play.channels = channels;
    396 		if (encoding)
    397 			info.play.encoding = encoding;
    398 		if (precision)
    399 			info.play.precision = precision;
    400 		hdr_len = 0;
    401 	}
    402 	info.mode = AUMODE_PLAY_ALL;
    403 
    404 	if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    405 		err(1, "failed to set audio info");
    406 
    407 	return (hdr_len);
    408 }
    409 
    410 void
    411 usage()
    412 {
    413 	fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
    414 	fprintf(stderr, "Options:\n\t"
    415 	    "-C audio control device\n\t"
    416 	    "-b balance (0-63)\n\t"
    417 	    "-d audio device\n\t"
    418 	    "-f force settings\n\t"
    419 	    "\t-c forced channels\n\t"
    420 	    "\t-e forced encoding\n\t"
    421 	    "\t-P forced precision\n\t"
    422 	    "\t-s forced sample rate\n\t"
    423 	    "-i header information\n\t"
    424 	    "-m monitor volume\n\t"
    425 	    "-p output port\n\t"
    426 	    "-v volume\n");
    427 	exit(EXIT_FAILURE);
    428 }
    429