Home | History | Annotate | Line # | Download | only in play
play.c revision 1.3
      1 /*	$NetBSD: play.c,v 1.3 1999/03/26 15:46:22 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, int));
     51 
     52 audio_info_t	info;
     53 int	volume = 0;
     54 int	balance = 0;
     55 int	port = 0;
     56 char	const *play_errstring = NULL;
     57 
     58 int
     59 main(argc, argv)
     60 	int argc;
     61 	char *argv[];
     62 {
     63 	size_t	len, bufsize;
     64 	ssize_t	hdrlen;
     65 	off_t	filesize;
     66 	int	ch, audiofd, ctlfd;
     67 	int	iflag = 0;
     68 	int	qflag = 0;
     69 	int	verbose = 0;
     70 	char	*device = 0;
     71 	char	*ctldev = 0;
     72 
     73 	while ((ch = getopt(argc, argv, "b:C:d:hiqp:Vv:")) != -1) {
     74 		switch (ch) {
     75 		case 'b':
     76 			decode_int(optarg, &balance);
     77 			if (balance < 0 || balance > 64)
     78 				errx(1, "balance must be between 0 and 64\n");
     79 			break;
     80 		case 'C':
     81 			ctldev = optarg;
     82 			break;
     83 		case 'd':
     84 			device = optarg;
     85 			break;
     86 		case 'i':
     87 			iflag++;
     88 			break;
     89 		case 'q':
     90 			qflag++;
     91 			break;
     92 		case 'p':
     93 			len = strlen(optarg);
     94 
     95 			if (strncmp(optarg, "speaker", len) == 0)
     96 				port |= AUDIO_SPEAKER;
     97 			else if (strncmp(optarg, "headphone", len) == 0)
     98 				port |= AUDIO_HEADPHONE;
     99 			else if (strncmp(optarg, "line", len) == 0)
    100 				port |= AUDIO_LINE_OUT;
    101 			else
    102 				errx(1,
    103 			    "port must be `speaker', `headphone', or `line'");
    104 			break;
    105 		case 'V':
    106 			verbose++;
    107 			break;
    108 		case 'v':
    109 			volume = atoi(optarg);
    110 			if (volume < 0 || volume > 255)
    111 				errx(1, "volume must be between 0 and 255\n");
    112 			break;
    113 		/* case 'h': */
    114 		default:
    115 			usage();
    116 			/* NOTREACHED */
    117 		}
    118 	}
    119 	argc -= optind;
    120 	argv += optind;
    121 
    122 	if (device == NULL)
    123 		device = _PATH_AUDIO;
    124 	if (ctldev == NULL)
    125 		ctldev = _PATH_AUDIOCTL;
    126 
    127 	audiofd = open(device, O_WRONLY);
    128 #ifdef _PATH_OAUDIO
    129         /* Allow the non-unit device to be used. */
    130         if (audiofd < 0 && device == _PATH_AUDIO) {
    131         	device = _PATH_OAUDIO;
    132         	ctldev = _PATH_OAUDIOCTL;
    133                 audiofd = open(device, O_WRONLY);
    134 	}
    135 #endif
    136 	if (audiofd < 0)
    137 		err(1, "failed to open %s", device);
    138 	ctlfd = open(ctldev, O_RDWR);
    139 	if (ctlfd < 0)
    140 		err(1, "failed to open %s", ctldev);
    141 
    142 	if (ioctl(ctlfd, AUDIO_GETINFO, &info) < 0)
    143 		err(1, "failed to get audio info");
    144 	bufsize = info.play.buffer_size;
    145 	if (bufsize < 32 * 1024)
    146 		bufsize = 32 * 1024;
    147 
    148 	if (*argv) {
    149 		int fd;
    150 		struct stat sb;
    151 		void *addr, *oaddr;
    152 
    153 		do {
    154 			fd = open(*argv, O_RDONLY);
    155 			if (fd < 0)
    156 				err(1, "could not open %s", *argv);
    157 
    158 			if (fstat(fd, &sb) < 0)
    159 				err(1, "could not fstat %s", *argv);
    160 			filesize = sb.st_size;
    161 
    162 			oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
    163 			    MAP_SHARED, fd, 0);
    164 			if (addr == (void *)-1)
    165 				err(1, "could not mmap %s", *argv);
    166 
    167 			if ((hdrlen = audioctl_write_fromhdr(addr,
    168 			    (size_t)filesize, ctlfd, 1)) < 0) {
    169 				if (play_errstring)
    170 					errx(1, "%s: %s", play_errstring, *argv);
    171 				else
    172 					errx(1, "unknown audio file: %s", *argv);
    173 			}
    174 
    175 			filesize -= hdrlen;
    176 			(char *)addr += hdrlen;
    177 
    178 			while (filesize > bufsize) {
    179 				if (write(audiofd, addr, bufsize) != bufsize)
    180 					err(1, "write failed");
    181 				(char *)addr += bufsize;
    182 				filesize -= bufsize;
    183 			}
    184 			if (write(audiofd, addr, (size_t)filesize) != (ssize_t)filesize)
    185 				err(1, "final write failed");
    186 
    187 			if (munmap(oaddr, (size_t)filesize) < 0)
    188 				err(1, "munmap failed");
    189 
    190 			close(fd);
    191 
    192 		} while (*++argv);
    193 	} else {
    194 		/* ... handle stdin */
    195 	}
    196 
    197 	exit(0);
    198 }
    199 
    200 /*
    201  * only support sun and wav audio files so far ...
    202  *
    203  * XXX this should probably be mostly part of libaudio, but it
    204  * uses the local "info" variable. blah... fix me!
    205  */
    206 ssize_t
    207 audioctl_write_fromhdr(hdr, fsz, fd, unknown_ok)
    208 	void	*hdr;
    209 	size_t	fsz;
    210 	int	fd;
    211 	int	unknown_ok;
    212 {
    213 	sun_audioheader	*sunhdr;
    214 	ssize_t	hdr_len;
    215 
    216 	AUDIO_INITINFO(&info);
    217 	sunhdr = hdr;
    218 	if (ntohl(sunhdr->magic) == AUDIO_FILE_MAGIC) {
    219 		if (audio_get_sun_encoding(ntohl(sunhdr->encoding),
    220 		    &info.play.encoding, &info.play.precision)) {
    221 			warnx("unknown supported Sun audio encoding format %d",
    222 			    sunhdr->encoding);
    223 			return (-1);
    224 		}
    225 
    226 		info.play.sample_rate = ntohl(sunhdr->sample_rate);
    227 		info.play.channels = ntohl(sunhdr->channels);
    228 		if (port)
    229 			info.play.port = port;
    230 		if (volume)
    231 			info.play.gain = volume;
    232 		if (balance)
    233 			info.play.balance = balance;
    234 		info.mode = AUMODE_PLAY_ALL;
    235 
    236 		if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    237 			err(1, "failed to set audio info");
    238 
    239 		return (ntohl(sunhdr->hdr_size));
    240 	}
    241 
    242 	hdr_len = audio_parse_wav_hdr(hdr, fsz, &info.play.encoding,
    243 	    &info.play.precision, &info.play.sample_rate, &info.play.channels);
    244 
    245 	switch (hdr_len) {
    246 	case AUDIO_ESHORTHDR:
    247 	case AUDIO_EWAVUNSUPP:
    248 	case AUDIO_EWAVBADPCM:
    249 	case AUDIO_EWAVNODATA:
    250 		if (unknown_ok == 0)
    251 			play_errstring = audio_errstring(hdr_len);
    252 		/* FALL THROUGH */
    253 	case AUDIO_ENOENT:
    254 		break;
    255 	default:
    256 		if (hdr_len < 1)
    257 			break;
    258 		if (port)
    259 			info.play.port = port;
    260 		if (volume)
    261 			info.play.gain = volume;
    262 		if (balance)
    263 			info.play.balance = balance;
    264 		info.mode = AUMODE_PLAY_ALL;
    265 
    266 		if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
    267 			err(1, "failed to set audio info");
    268 
    269 		return (hdr_len);
    270 	}
    271 	return (unknown_ok ? 0 : -1);
    272 }
    273 
    274 void
    275 usage()
    276 {
    277 	extern char *__progname;
    278 
    279 	fprintf(stderr, "Usage: %s [-iqVh] [-v vol] [-b bal] [-p port] [-d dev]\n\t[-c ctl] [file ...]\n", __progname);
    280 	exit(0);
    281 }
    282