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