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