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