Home | History | Annotate | Line # | Download | only in play
play.c revision 1.10
      1 /*	$NetBSD: play.c,v 1.10 1999/05/17 02:03:36 jtk 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)
    160 		device = _PATH_AUDIO;
    161 	if (ctldev == NULL)
    162 		ctldev = _PATH_AUDIOCTL;
    163 
    164 	audiofd = open(device, O_WRONLY);
    165 #ifdef _PATH_OAUDIO
    166 	/* Allow the non-unit device to be used. */
    167 	if (audiofd < 0 && device == _PATH_AUDIO) {
    168 		device = _PATH_OAUDIO;
    169 		ctldev = _PATH_OAUDIOCTL;
    170 		audiofd = open(device, O_WRONLY);
    171 	}
    172 #endif
    173 	if (audiofd < 0)
    174 		err(1, "failed to open %s", device);
    175 	ctlfd = open(ctldev, O_RDWR);
    176 	if (ctlfd < 0)
    177 		err(1, "failed to open %s", ctldev);
    178 
    179 	if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
    180 		err(1, "failed to get audio info");
    181 	bufsize = info.play.buffer_size;
    182 	if (bufsize < 32 * 1024)
    183 		bufsize = 32 * 1024;
    184 
    185 	if (*argv) {
    186 		int fd;
    187 		struct stat sb;
    188 		void *addr, *oaddr;
    189 
    190 		do {
    191 			ssize_t	hdrlen;
    192 
    193 			fd = open(*argv, O_RDONLY);
    194 			if (fd < 0)
    195 				err(1, "could not open %s", *argv);
    196 
    197 			if (fstat(fd, &sb) < 0)
    198 				err(1, "could not fstat %s", *argv);
    199 			filesize = sb.st_size;
    200 
    201 			oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
    202 			    MAP_SHARED, fd, 0);
    203 
    204 			/*
    205 			 * if we failed to mmap the file, try to read it
    206 			 * instead, so that filesystems, etc, that do not
    207 			 * support mmap() work
    208 			 */
    209 			if (addr == (void *)-1) {
    210 				play_fd(fd, *argv);
    211 				close(fd);
    212 				continue;
    213 			}
    214 
    215 			/*
    216 			 * give the VM system a bit of a hint about the type
    217 			 * of accesses we will make.
    218 			 */
    219 			if (madvise(addr, filesize, MADV_SEQUENTIAL) < 0)
    220 				warn("madvise failed, ignoring");
    221 
    222 			/*
    223 			 * get the header length and set up the audio device
    224 			 */
    225 			if ((hdrlen = audioctl_write_fromhdr(addr,
    226 			    (size_t)filesize, ctlfd)) < 0) {
    227 				if (play_errstring)
    228 					errx(1, "%s: %s", play_errstring, *argv);
    229 				else
    230 					errx(1, "unknown audio file: %s", *argv);
    231 			}
    232 
    233 			filesize -= hdrlen;
    234 			(char *)addr += hdrlen;
    235 
    236 			while (filesize > bufsize) {
    237 				if (write(audiofd, addr, bufsize) != bufsize)
    238 					err(1, "write failed");
    239 				(char *)addr += bufsize;
    240 				filesize -= bufsize;
    241 			}
    242 			if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
    243 				err(1, "final write failed");
    244 
    245 			if (ioctl(audiofd, AUDIO_DRAIN) < 0)
    246 				warn("audio drain ioctl failed");
    247 			if (munmap(oaddr, (size_t)filesize) < 0)
    248 				err(1, "munmap failed");
    249 
    250 			close(fd);
    251 
    252 		} while (*++argv);
    253 	} else {
    254 		play_fd(STDIN_FILENO, "standard input");
    255 	}
    256 
    257 	exit(0);
    258 }
    259 
    260 /*
    261  * play the file on on the file descriptor fd
    262  */
    263 void
    264 play_fd(fd, file)
    265 	int     fd;
    266 	char    *file;
    267 {
    268 	char    *buffer = malloc(bufsize);
    269 	ssize_t hdrlen;
    270 	int     n, m;
    271 
    272 	if (buffer == NULL)
    273 		err(1, "malloc of read buffer failed");
    274 
    275 	n = read(fd, buffer, bufsize);
    276 
    277 	if (n < 0)
    278 		err(1, "read of standard input failed");
    279 	if (n == 0)
    280 		errx(1, "EOF on standard input");
    281 
    282 	hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd);
    283 	if (hdrlen < 0) {
    284 		if (play_errstring)
    285 			errx(1, "%s: %s", play_errstring, file);
    286 		else
    287 			errx(1, "unknown audio file: %s", file);
    288 	}
    289 
    290 	/* advance the buffer if we have to */
    291 	if (hdrlen > 0) {
    292 		/* shouldn't happen */
    293 		if (hdrlen > n)
    294 			err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
    295 
    296 		memmove(buffer, buffer + hdrlen, n - hdrlen);
    297 
    298 		m = read(fd, buffer + n, hdrlen);
    299 		n += m;
    300 	}
    301 	/* read until EOF or error */
    302 	do {
    303 		if (n == -1)
    304 			err(1, "read of standard input failed");
    305 		if (write(audiofd, buffer, n) != n)
    306 			err(1, "write failed");
    307 	} while ((n = read(fd, buffer, bufsize)));
    308 
    309 	if (ioctl(audiofd, AUDIO_DRAIN) < 0)
    310 		warn("audio drain ioctl failed");
    311 }
    312 
    313 /*
    314  * only support sun and wav audio files so far ...
    315  *
    316  * XXX this should probably be mostly part of libaudio, but it
    317  * uses the local "info" variable. blah... fix me!
    318  */
    319 ssize_t
    320 audioctl_write_fromhdr(hdr, fsz, fd)
    321 	void	*hdr;
    322 	size_t	fsz;
    323 	int	fd;
    324 {
    325 	sun_audioheader	*sunhdr;
    326 	ssize_t	hdr_len;
    327 
    328 	AUDIO_INITINFO(&info);
    329 	sunhdr = hdr;
    330 	if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
    331 		if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
    332 		    &info.play.encoding, &info.play.precision)) {
    333 			warnx("unknown supported Sun audio encoding format %d",
    334 			    sunhdr->encoding);
    335 			return (-1);
    336 		}
    337 
    338 		info.play.sample_rate = ntohl(sunhdr->sample_rate);
    339 		info.play.channels = ntohl(sunhdr->channels);
    340 		hdr_len = ntohl(sunhdr->hdr_size);
    341 
    342 		goto set_audio_mode;
    343 	}
    344 
    345 	hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
    346 	    &info.play.precision, &info.play.sample_rate, &info.play.channels);
    347 
    348 	switch (hdr_len) {
    349 	case AUDIO_ESHORTHDR:
    350 	case AUDIO_EWAVUNSUPP:
    351 	case AUDIO_EWAVBADPCM:
    352 	case AUDIO_EWAVNODATA:
    353 		play_errstring = audio_errstring(hdr_len);
    354 		/* FALL THROUGH */
    355 	case AUDIO_ENOENT:
    356 		break;
    357 	default:
    358 		if (hdr_len < 1)
    359 			break;
    360 		goto set_audio_mode;
    361 	}
    362 	/*
    363 	 * if we don't know it, bail unless we are forcing.
    364 	 */
    365 	if (fflag == 0)
    366 		return (-1);
    367 set_audio_mode:
    368 	if (port)
    369 		info.play.port = port;
    370 	if (volume)
    371 		info.play.gain = volume;
    372 	if (balance)
    373 		info.play.balance = balance;
    374 	if (fflag) {
    375 		if (sample_rate)
    376 			info.play.sample_rate = sample_rate;
    377 		if (channels)
    378 			info.play.channels = channels;
    379 		if (encoding)
    380 			info.play.encoding = encoding;
    381 		if (precision)
    382 			info.play.encoding = precision;
    383 	}
    384 	info.mode = AUMODE_PLAY_ALL;
    385 
    386 	if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    387 		err(1, "failed to set audio info");
    388 
    389 	return (hdr_len);
    390 }
    391 
    392 void
    393 usage()
    394 {
    395 	extern char *__progname;
    396 
    397 	fprintf(stderr, "Usage: %s [-hiqV] [options] files\n", __progname);
    398 	fprintf(stderr, "Options:\n\t"
    399 	    "-C audio control device\n\t"
    400 	    "-b balance (0-63)\n\t"
    401 	    "-d audio device\n\t"
    402 	    "-f force settings\n\t"
    403 	    "\t-c forced channels\n\t"
    404 	    "\t-e forced encoding\n\t"
    405 	    "\t-P forced precision\n\t"
    406 	    "\t-s forced sample rate\n\t"
    407 	    "-i header information\n\t"
    408 	    "-m monitor volume\n\t"
    409 	    "-p output port\n\t"
    410 	    "-v volume\n");
    411 	exit(0);
    412 }
    413