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