xref: /titanic_52/usr/src/cmd/audio/audioplay/audioplay.c (revision 88447a05f537aabe9a1bc3d5313f22581ec992a7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*88447a05SGarrett D'Amore  * Common Development and Distribution License (the "License").
6*88447a05SGarrett D'Amore  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*88447a05SGarrett D'Amore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* Command-line audio play utility */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <locale.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>	/* All occurances of INT_MAX used to be ~0  (by MCA) */
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <stropts.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/file.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
447c478bd9Sstevel@tonic-gate #include <sys/mman.h>
457c478bd9Sstevel@tonic-gate #include <netinet/in.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <libaudio.h>
487c478bd9Sstevel@tonic-gate #include <audio_device.h>
497c478bd9Sstevel@tonic-gate #include <audio_encode.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* localization stuff */
527c478bd9Sstevel@tonic-gate #define	MGET(s)		(char *)gettext(s)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
557c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	Error		(void) fprintf
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* Local variables */
627c478bd9Sstevel@tonic-gate static char *prog;
637c478bd9Sstevel@tonic-gate 
64*88447a05SGarrett D'Amore static char prog_opts[] =	"VEiv:d:?";	/* getopt() flags */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static char			*Stdin;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	MAX_GAIN		(100)		/* maximum gain */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * This defines the tolerable sample rate error as a ratio between the
727c478bd9Sstevel@tonic-gate  * sample rates of the audio data and the audio device.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #define	SAMPLE_RATE_THRESHOLD	(.01)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define		BUFFER_LEN	10	/* seconds - for file i/o */
777c478bd9Sstevel@tonic-gate #define		ADPCM_SIZE	(1000*8) /* adpcm conversion output buf size */
787c478bd9Sstevel@tonic-gate #define		SWAP_SIZE	(8192)
797c478bd9Sstevel@tonic-gate 			/* swap bytes conversion output buf size */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static unsigned		Volume = INT_MAX;	/* output volume */
827c478bd9Sstevel@tonic-gate static double		Savevol;		/* saved volume level */
83*88447a05SGarrett D'Amore 
847c478bd9Sstevel@tonic-gate static int		Verbose = FALSE;	/* verbose messages */
857c478bd9Sstevel@tonic-gate static int		Immediate = FALSE;
867c478bd9Sstevel@tonic-gate 			/* don't hang waiting for device */
877c478bd9Sstevel@tonic-gate static int		Errdetect = FALSE;	/* don't worry about underrun */
887c478bd9Sstevel@tonic-gate static char		*Audio_dev = "/dev/audio";
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int NetEndian = TRUE;		/* endian nature of the machine */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static int		Audio_fd = -1;
937c478bd9Sstevel@tonic-gate 			/* file descriptor for audio device */
947c478bd9Sstevel@tonic-gate static int		Audio_ctlfd = -1;
957c478bd9Sstevel@tonic-gate 			/* file descriptor for control device */
967c478bd9Sstevel@tonic-gate static Audio_hdr	Save_hdr;
977c478bd9Sstevel@tonic-gate 			/* saved audio header for device */
987c478bd9Sstevel@tonic-gate static Audio_hdr	Dev_hdr;		/* audio header for device */
997c478bd9Sstevel@tonic-gate static char		*Ifile;			/* current filename */
1007c478bd9Sstevel@tonic-gate static Audio_hdr	File_hdr;		/* audio header for file */
1017c478bd9Sstevel@tonic-gate static unsigned		Decode = AUDIO_ENCODING_NONE;
1027c478bd9Sstevel@tonic-gate 			/* decode type, if any */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static unsigned char	*buf = NULL;		/* dynamically alloc'd */
1057c478bd9Sstevel@tonic-gate static unsigned		bufsiz = 0;		/* size of output buffer */
1067c478bd9Sstevel@tonic-gate static unsigned char	adpcm_buf[ADPCM_SIZE + 32];
1077c478bd9Sstevel@tonic-gate 			/* for adpcm conversion */
1087c478bd9Sstevel@tonic-gate static unsigned char	swap_buf[SWAP_SIZE + 32];
1097c478bd9Sstevel@tonic-gate 			/* for byte swap conversion */
1107c478bd9Sstevel@tonic-gate static unsigned char	*inbuf;
1117c478bd9Sstevel@tonic-gate 			/* current input buffer pointer */
1127c478bd9Sstevel@tonic-gate static unsigned		insiz;			/* current input buffer size */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * The decode_g72x() function is capable of decoding only one channel
1167c478bd9Sstevel@tonic-gate  * at a time and so multichannel data must be decomposed (using demux()
1177c478bd9Sstevel@tonic-gate  * function below ) into its constituent channels and each passed
1187c478bd9Sstevel@tonic-gate  * separately to the decode_g72x() function. Encoded input channels are
1197c478bd9Sstevel@tonic-gate  * stored in **in_ch_data and decoded output channels in **out_ch_data.
1207c478bd9Sstevel@tonic-gate  * Once each channel has been decoded they are recombined (see mux()
1217c478bd9Sstevel@tonic-gate  * function below) before being written to the audio device. For each
1227c478bd9Sstevel@tonic-gate  * channel and adpcm state structure is created.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* adpcm state structures */
1267c478bd9Sstevel@tonic-gate static struct audio_g72x_state *adpcm_state = NULL;
1277c478bd9Sstevel@tonic-gate static unsigned char	**in_ch_data = NULL;	/* input channels */
1287c478bd9Sstevel@tonic-gate static unsigned char	**out_ch_data = NULL;	/* output channels */
1297c478bd9Sstevel@tonic-gate static int		out_ch_size;		/* output channel size */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static char		*Audio_path = NULL;
1327c478bd9Sstevel@tonic-gate 			/* path to search for audio files */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* Global variables */
1357c478bd9Sstevel@tonic-gate extern int	getopt(int, char *const *, const char *);
1367c478bd9Sstevel@tonic-gate extern int	optind;
1377c478bd9Sstevel@tonic-gate extern char	*optarg;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* Local functions  */
1407c478bd9Sstevel@tonic-gate static void usage(void);
1417c478bd9Sstevel@tonic-gate static void sigint(int sig);
1427c478bd9Sstevel@tonic-gate static void open_audio(void);
1437c478bd9Sstevel@tonic-gate static int path_open(char *fname, int flags, mode_t mode, char *path);
1447c478bd9Sstevel@tonic-gate static int parse_unsigned(char *str, unsigned *dst, char *flag);
1457c478bd9Sstevel@tonic-gate static int reconfig(void);
1467c478bd9Sstevel@tonic-gate static void initmux(int unitsz, int unitsp);
1477c478bd9Sstevel@tonic-gate static void demux(int unitsz, int cnt);
1487c478bd9Sstevel@tonic-gate static void mux(char *);
1497c478bd9Sstevel@tonic-gate static void freemux(void);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static void
1537c478bd9Sstevel@tonic-gate usage(void)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	Error(stderr, MGET("Play an audio file -- usage:\n"
156*88447a05SGarrett D'Amore 	    "\t%s [-iV] [-v vol] [-d dev] [file ...]\n"
1577c478bd9Sstevel@tonic-gate 	    "where:\n"
1587c478bd9Sstevel@tonic-gate 	    "\t-i\tDon't hang if audio device is busy\n"
1597c478bd9Sstevel@tonic-gate 	    "\t-V\tPrint verbose warning messages\n"
1607c478bd9Sstevel@tonic-gate 	    "\t-v\tSet output volume (0 - %d)\n"
1617c478bd9Sstevel@tonic-gate 	    "\t-d\tSpecify audio device (default: /dev/audio)\n"
1627c478bd9Sstevel@tonic-gate 	    "\tfile\tList of files to play\n"
1637c478bd9Sstevel@tonic-gate 	    "\t\tIf no files specified, read stdin\n"),
164*88447a05SGarrett D'Amore 	    prog, MAX_GAIN);
1657c478bd9Sstevel@tonic-gate 	exit(1);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static void
1697c478bd9Sstevel@tonic-gate sigint(int sig)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	/* flush output queues before exiting */
1727c478bd9Sstevel@tonic-gate 	if (Audio_fd >= 0) {
1737c478bd9Sstevel@tonic-gate 		(void) audio_flush_play(Audio_fd);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		/* restore saved parameters */
1767c478bd9Sstevel@tonic-gate 		if (Volume != INT_MAX)
1777c478bd9Sstevel@tonic-gate 			(void) audio_set_play_gain(Audio_fd, &Savevol);
1787c478bd9Sstevel@tonic-gate 		if ((Audio_ctlfd >= 0) &&
1797c478bd9Sstevel@tonic-gate 		    (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0)) {
1807c478bd9Sstevel@tonic-gate 			(void) audio_set_play_config(Audio_fd, &Save_hdr);
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 	exit(1);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* Open the audio device and initalize it. */
1877c478bd9Sstevel@tonic-gate static void
1887c478bd9Sstevel@tonic-gate open_audio(void)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	int		err;
1917c478bd9Sstevel@tonic-gate 	double		vol;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* Return if already open */
1947c478bd9Sstevel@tonic-gate 	if (Audio_fd >= 0)
1957c478bd9Sstevel@tonic-gate 		return;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Try opening without waiting, first */
1987c478bd9Sstevel@tonic-gate 	Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK);
1997c478bd9Sstevel@tonic-gate 	if ((Audio_fd < 0) && (errno == EBUSY)) {
2007c478bd9Sstevel@tonic-gate 		if (Immediate) {
2017c478bd9Sstevel@tonic-gate 			Error(stderr, MGET("%s: %s is busy\n"),
2027c478bd9Sstevel@tonic-gate 			    prog, Audio_dev);
2037c478bd9Sstevel@tonic-gate 			exit(1);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		if (Verbose) {
2067c478bd9Sstevel@tonic-gate 			Error(stderr, MGET("%s: waiting for %s..."),
2077c478bd9Sstevel@tonic-gate 			    prog, Audio_dev);
2087c478bd9Sstevel@tonic-gate 			(void) fflush(stderr);
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 		/* Now hang until it's open */
2117c478bd9Sstevel@tonic-gate 		Audio_fd = open(Audio_dev, O_WRONLY);
2127c478bd9Sstevel@tonic-gate 		if (Verbose)
2137c478bd9Sstevel@tonic-gate 			Error(stderr, (Audio_fd < 0) ? "\n" : MGET("open\n"));
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	if (Audio_fd < 0) {
2167c478bd9Sstevel@tonic-gate 		Error(stderr, MGET("%s: error opening "), prog);
2177c478bd9Sstevel@tonic-gate 		perror(Audio_dev);
2187c478bd9Sstevel@tonic-gate 		exit(1);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/* Clear the non-blocking flag (in System V it persists after open) */
2227c478bd9Sstevel@tonic-gate 	(void) fcntl(Audio_fd, F_SETFL,
2237c478bd9Sstevel@tonic-gate 	    (fcntl(Audio_fd, F_GETFL, 0) & ~(O_NDELAY | O_NONBLOCK)));
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* Get the device output encoding configuration */
2267c478bd9Sstevel@tonic-gate 	if (audio_get_play_config(Audio_fd, &Dev_hdr) != AUDIO_SUCCESS) {
2277c478bd9Sstevel@tonic-gate 		Error(stderr, MGET("%s: %s is not an audio device\n"),
2287c478bd9Sstevel@tonic-gate 		    prog, Audio_dev);
2297c478bd9Sstevel@tonic-gate 		exit(1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* If -v flag, set the output volume now */
2337c478bd9Sstevel@tonic-gate 	if (Volume != INT_MAX) {
2347c478bd9Sstevel@tonic-gate 		vol = (double)Volume / (double)MAX_GAIN;
2357c478bd9Sstevel@tonic-gate 		(void) audio_get_play_gain(Audio_fd, &Savevol);
2367c478bd9Sstevel@tonic-gate 		err = audio_set_play_gain(Audio_fd, &vol);
2377c478bd9Sstevel@tonic-gate 		if (err != AUDIO_SUCCESS) {
2387c478bd9Sstevel@tonic-gate 			Error(stderr,
2397c478bd9Sstevel@tonic-gate 			    MGET("%s: could not set output volume for %s\n"),
2407c478bd9Sstevel@tonic-gate 			    prog, Audio_dev);
2417c478bd9Sstevel@tonic-gate 			exit(1);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /* Play a list of audio files. */
2477c478bd9Sstevel@tonic-gate int
2487c478bd9Sstevel@tonic-gate main(int argc, char **argv) {
2497c478bd9Sstevel@tonic-gate 	int		errorStatus = 0;
2507c478bd9Sstevel@tonic-gate 	int		i;
2517c478bd9Sstevel@tonic-gate 	int		c;
2527c478bd9Sstevel@tonic-gate 	int		cnt;
2537c478bd9Sstevel@tonic-gate 	int		file_type;
2547c478bd9Sstevel@tonic-gate 	int		rem;
2557c478bd9Sstevel@tonic-gate 	int		outsiz;
2567c478bd9Sstevel@tonic-gate 	int		tsize;
2577c478bd9Sstevel@tonic-gate 	int		len;
2587c478bd9Sstevel@tonic-gate 	int		err;
2597c478bd9Sstevel@tonic-gate 	int		ifd;
2607c478bd9Sstevel@tonic-gate 	int		stdinseen;
2617c478bd9Sstevel@tonic-gate 	int		regular;
2627c478bd9Sstevel@tonic-gate 	int		swapBytes;
2637c478bd9Sstevel@tonic-gate 	int		frame;
2647c478bd9Sstevel@tonic-gate 	char		*outbuf;
2657c478bd9Sstevel@tonic-gate 	caddr_t		mapaddr;
2667c478bd9Sstevel@tonic-gate 	struct stat	st;
2677c478bd9Sstevel@tonic-gate 	char		*cp;
2687c478bd9Sstevel@tonic-gate 	char		ctldev[MAXPATHLEN];
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2717c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/* Get the program name */
2747c478bd9Sstevel@tonic-gate 	prog = strrchr(argv[0], '/');
2757c478bd9Sstevel@tonic-gate 	if (prog == NULL)
2767c478bd9Sstevel@tonic-gate 		prog = argv[0];
2777c478bd9Sstevel@tonic-gate 	else
2787c478bd9Sstevel@tonic-gate 		prog++;
2797c478bd9Sstevel@tonic-gate 	Stdin = MGET("(stdin)");
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/* Check AUDIODEV environment for audio device name */
2827c478bd9Sstevel@tonic-gate 	if (cp = getenv("AUDIODEV")) {
2837c478bd9Sstevel@tonic-gate 		Audio_dev = cp;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* Parse the command line arguments */
2877c478bd9Sstevel@tonic-gate 	err = 0;
2887c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, prog_opts)) != EOF) {
2897c478bd9Sstevel@tonic-gate 		switch (i) {
2907c478bd9Sstevel@tonic-gate 			case 'v':
2917c478bd9Sstevel@tonic-gate 				if (parse_unsigned(optarg, &Volume, "-v")) {
2927c478bd9Sstevel@tonic-gate 					err++;
2937c478bd9Sstevel@tonic-gate 				} else if (Volume > MAX_GAIN) {
2947c478bd9Sstevel@tonic-gate 					Error(stderr, MGET("%s: invalid value "
2957c478bd9Sstevel@tonic-gate 					    "for -v\n"), prog);
2967c478bd9Sstevel@tonic-gate 					err++;
2977c478bd9Sstevel@tonic-gate 				}
2987c478bd9Sstevel@tonic-gate 				break;
2997c478bd9Sstevel@tonic-gate 			case 'd':
3007c478bd9Sstevel@tonic-gate 				Audio_dev = optarg;
3017c478bd9Sstevel@tonic-gate 				break;
3027c478bd9Sstevel@tonic-gate 			case 'V':
3037c478bd9Sstevel@tonic-gate 				Verbose = TRUE;
3047c478bd9Sstevel@tonic-gate 				break;
3057c478bd9Sstevel@tonic-gate 			case 'E':
3067c478bd9Sstevel@tonic-gate 				Errdetect = TRUE;
3077c478bd9Sstevel@tonic-gate 				break;
3087c478bd9Sstevel@tonic-gate 			case 'i':
3097c478bd9Sstevel@tonic-gate 				Immediate = TRUE;
3107c478bd9Sstevel@tonic-gate 				break;
3117c478bd9Sstevel@tonic-gate 			case '?':
3127c478bd9Sstevel@tonic-gate 				usage();
3137c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	if (err > 0)
3177c478bd9Sstevel@tonic-gate 		exit(1);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	argc -= optind;		/* update arg pointers */
3207c478bd9Sstevel@tonic-gate 	argv += optind;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* Validate and open the audio device */
3237c478bd9Sstevel@tonic-gate 	err = stat(Audio_dev, &st);
3247c478bd9Sstevel@tonic-gate 	if (err < 0) {
3257c478bd9Sstevel@tonic-gate 		Error(stderr, MGET("%s: cannot stat "), prog);
3267c478bd9Sstevel@tonic-gate 		perror(Audio_dev);
3277c478bd9Sstevel@tonic-gate 		exit(1);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	if (!S_ISCHR(st.st_mode)) {
3307c478bd9Sstevel@tonic-gate 		Error(stderr, MGET("%s: %s is not an audio device\n"), prog,
3317c478bd9Sstevel@tonic-gate 		    Audio_dev);
3327c478bd9Sstevel@tonic-gate 		exit(1);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* This should probably use audio_cntl instead of open_audio */
3367c478bd9Sstevel@tonic-gate 	if ((argc <= 0) && isatty(fileno(stdin))) {
337*88447a05SGarrett D'Amore 		Error(stderr, MGET("%s: No files and stdin is a tty.\n"), prog);
338*88447a05SGarrett D'Amore 		exit(1);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* Check on the -i status now. */
3427c478bd9Sstevel@tonic-gate 	Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK);
3437c478bd9Sstevel@tonic-gate 	if ((Audio_fd < 0) && (errno == EBUSY)) {
3447c478bd9Sstevel@tonic-gate 		if (Immediate) {
3457c478bd9Sstevel@tonic-gate 			Error(stderr, MGET("%s: %s is busy\n"), prog,
3467c478bd9Sstevel@tonic-gate 			    Audio_dev);
3477c478bd9Sstevel@tonic-gate 			exit(1);
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	(void) close(Audio_fd);
3517c478bd9Sstevel@tonic-gate 	Audio_fd = -1;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* Try to open the control device and save the current format */
354*88447a05SGarrett D'Amore 	(void) snprintf(ctldev, sizeof (ctldev), "%sctl", Audio_dev);
3557c478bd9Sstevel@tonic-gate 	Audio_ctlfd = open(ctldev, O_RDWR);
3567c478bd9Sstevel@tonic-gate 	if (Audio_ctlfd >= 0) {
3577c478bd9Sstevel@tonic-gate 		/*
3587c478bd9Sstevel@tonic-gate 		 * wait for the device to become available then get the
3597c478bd9Sstevel@tonic-gate 		 * controls. We want to save the format that is left when the
3607c478bd9Sstevel@tonic-gate 		 * device is in a quiescent state. So wait until then.
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		Audio_fd = open(Audio_dev, O_WRONLY);
3637c478bd9Sstevel@tonic-gate 		(void) close(Audio_fd);
3647c478bd9Sstevel@tonic-gate 		Audio_fd = -1;
3657c478bd9Sstevel@tonic-gate 		if (audio_get_play_config(Audio_ctlfd, &Save_hdr)
3667c478bd9Sstevel@tonic-gate 		    != AUDIO_SUCCESS) {
3677c478bd9Sstevel@tonic-gate 			(void) close(Audio_ctlfd);
3687c478bd9Sstevel@tonic-gate 			Audio_ctlfd = -1;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/* store AUDIOPATH so we don't keep doing getenv() */
3737c478bd9Sstevel@tonic-gate 	Audio_path = getenv("AUDIOPATH");
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* Set up SIGINT handler to flush output */
3767c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, sigint);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* Set the endian nature of the machine. */
3797c478bd9Sstevel@tonic-gate 	if ((ulong_t)1 != htonl((ulong_t)1)) {
3807c478bd9Sstevel@tonic-gate 		NetEndian = FALSE;
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/* If no filenames, read stdin */
3847c478bd9Sstevel@tonic-gate 	stdinseen = FALSE;
3857c478bd9Sstevel@tonic-gate 	if (argc <= 0) {
3867c478bd9Sstevel@tonic-gate 		Ifile = Stdin;
3877c478bd9Sstevel@tonic-gate 	} else {
3887c478bd9Sstevel@tonic-gate 		Ifile = *argv++;
3897c478bd9Sstevel@tonic-gate 		argc--;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* Loop through all filenames */
3937c478bd9Sstevel@tonic-gate 	do {
3947c478bd9Sstevel@tonic-gate 		/* Interpret "-" filename to mean stdin */
3957c478bd9Sstevel@tonic-gate 		if (strcmp(Ifile, "-") == 0)
3967c478bd9Sstevel@tonic-gate 			Ifile = Stdin;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (Ifile == Stdin) {
3997c478bd9Sstevel@tonic-gate 			if (stdinseen) {
4007c478bd9Sstevel@tonic-gate 				Error(stderr,
4017c478bd9Sstevel@tonic-gate 				    MGET("%s: stdin already processed\n"),
4027c478bd9Sstevel@tonic-gate 				    prog);
4037c478bd9Sstevel@tonic-gate 				goto nextfile;
4047c478bd9Sstevel@tonic-gate 			}
4057c478bd9Sstevel@tonic-gate 			stdinseen = TRUE;
4067c478bd9Sstevel@tonic-gate 			ifd = fileno(stdin);
4077c478bd9Sstevel@tonic-gate 		} else {
4087c478bd9Sstevel@tonic-gate 			if ((ifd = path_open(Ifile, O_RDONLY, 0, Audio_path))
4097c478bd9Sstevel@tonic-gate 			    < 0) {
4107c478bd9Sstevel@tonic-gate 				Error(stderr, MGET("%s: cannot open "), prog);
4117c478bd9Sstevel@tonic-gate 				perror(Ifile);
4127c478bd9Sstevel@tonic-gate 				errorStatus++;
4137c478bd9Sstevel@tonic-gate 				goto nextfile;
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		/* Check to make sure this is an audio file */
4187c478bd9Sstevel@tonic-gate 		err = audio_read_filehdr(ifd, &File_hdr, &file_type,
4197c478bd9Sstevel@tonic-gate 		    (char *)NULL, 0);
4207c478bd9Sstevel@tonic-gate 		if (err != AUDIO_SUCCESS) {
4217c478bd9Sstevel@tonic-gate 			Error(stderr,
4227c478bd9Sstevel@tonic-gate 			    MGET("%s: %s is not a valid audio file\n"),
4237c478bd9Sstevel@tonic-gate 			    prog, Ifile);
4247c478bd9Sstevel@tonic-gate 			errorStatus++;
4257c478bd9Sstevel@tonic-gate 			goto closeinput;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		/* If G.72X adpcm, set flags for conversion */
4297c478bd9Sstevel@tonic-gate 		if ((File_hdr.encoding == AUDIO_ENCODING_G721) &&
4307c478bd9Sstevel@tonic-gate 		    (File_hdr.samples_per_unit == 2) &&
4317c478bd9Sstevel@tonic-gate 		    (File_hdr.bytes_per_unit == 1)) {
4327c478bd9Sstevel@tonic-gate 			Decode = AUDIO_ENCODING_G721;
4337c478bd9Sstevel@tonic-gate 			File_hdr.encoding = AUDIO_ENCODING_ULAW;
4347c478bd9Sstevel@tonic-gate 			File_hdr.samples_per_unit = 1;
4357c478bd9Sstevel@tonic-gate 			File_hdr.bytes_per_unit = 1;
4367c478bd9Sstevel@tonic-gate 			adpcm_state = (struct audio_g72x_state *)malloc
4377c478bd9Sstevel@tonic-gate 			    (sizeof (*adpcm_state) * File_hdr.channels);
4387c478bd9Sstevel@tonic-gate 			for (i = 0; i < File_hdr.channels; i++) {
4397c478bd9Sstevel@tonic-gate 				g721_init_state(&adpcm_state[i]);
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 		} else if ((File_hdr.encoding == AUDIO_ENCODING_G723) &&
4427c478bd9Sstevel@tonic-gate 		    (File_hdr.samples_per_unit == 8) &&
4437c478bd9Sstevel@tonic-gate 		    (File_hdr.bytes_per_unit == 3)) {
4447c478bd9Sstevel@tonic-gate 			Decode = AUDIO_ENCODING_G723;
4457c478bd9Sstevel@tonic-gate 			File_hdr.encoding = AUDIO_ENCODING_ULAW;
4467c478bd9Sstevel@tonic-gate 			File_hdr.samples_per_unit = 1;
4477c478bd9Sstevel@tonic-gate 			File_hdr.bytes_per_unit = 1;
4487c478bd9Sstevel@tonic-gate 			adpcm_state = (struct audio_g72x_state *)malloc
4497c478bd9Sstevel@tonic-gate 			    (sizeof (*adpcm_state) * File_hdr.channels);
4507c478bd9Sstevel@tonic-gate 			for (i = 0; i < File_hdr.channels; i++) {
4517c478bd9Sstevel@tonic-gate 				g723_init_state(&adpcm_state[i]);
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 		} else {
4547c478bd9Sstevel@tonic-gate 			Decode = AUDIO_ENCODING_NONE;
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		/* Check the device configuration */
4587c478bd9Sstevel@tonic-gate 		open_audio();
4597c478bd9Sstevel@tonic-gate 		if (audio_cmp_hdr(&Dev_hdr, &File_hdr) != 0) {
4607c478bd9Sstevel@tonic-gate 			/*
4617c478bd9Sstevel@tonic-gate 			 * The device does not match the input file.
4627c478bd9Sstevel@tonic-gate 			 * Wait for any old output to drain, then attempt
4637c478bd9Sstevel@tonic-gate 			 * to reconfigure the audio device to match the
4647c478bd9Sstevel@tonic-gate 			 * input data.
4657c478bd9Sstevel@tonic-gate 			 */
4667c478bd9Sstevel@tonic-gate 			if (audio_drain(Audio_fd, FALSE) != AUDIO_SUCCESS) {
4677c478bd9Sstevel@tonic-gate 				/* Flush any remaining audio */
4687c478bd9Sstevel@tonic-gate 				(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 				Error(stderr, MGET("%s: "), prog);
4717c478bd9Sstevel@tonic-gate 				perror(MGET("AUDIO_DRAIN error"));
4727c478bd9Sstevel@tonic-gate 				exit(1);
4737c478bd9Sstevel@tonic-gate 			}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			/* Flush any remaining audio */
4767c478bd9Sstevel@tonic-gate 			(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 			if (!reconfig()) {
4797c478bd9Sstevel@tonic-gate 				errorStatus++;
4807c478bd9Sstevel@tonic-gate 				goto closeinput;
4817c478bd9Sstevel@tonic-gate 			}
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		/* try to do the mmaping - for regular files only ... */
4867c478bd9Sstevel@tonic-gate 		err = fstat(ifd, &st);
4877c478bd9Sstevel@tonic-gate 		if (err < 0) {
4887c478bd9Sstevel@tonic-gate 			Error(stderr, MGET("%s: cannot stat "), prog);
4897c478bd9Sstevel@tonic-gate 			perror(Ifile);
4907c478bd9Sstevel@tonic-gate 			exit(1);
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		regular = (S_ISREG(st.st_mode));
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		/* If regular file, map it.  Else, allocate a buffer */
4967c478bd9Sstevel@tonic-gate 		mapaddr = 0;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		/*
4997c478bd9Sstevel@tonic-gate 		 * This should compare to MAP_FAILED not -1, can't
5007c478bd9Sstevel@tonic-gate 		 * find MAP_FAILED
5017c478bd9Sstevel@tonic-gate 		 */
5027c478bd9Sstevel@tonic-gate 		if (regular && ((mapaddr = mmap(0, st.st_size, PROT_READ,
5037c478bd9Sstevel@tonic-gate 		    MAP_SHARED, ifd, 0)) != MAP_FAILED)) {
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 			(void) madvise(mapaddr, st.st_size, MADV_SEQUENTIAL);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 			/* Skip the file header and set the proper size */
5087c478bd9Sstevel@tonic-gate 			cnt = lseek(ifd, 0, SEEK_CUR);
5097c478bd9Sstevel@tonic-gate 			if (cnt < 0) {
5107c478bd9Sstevel@tonic-gate 			    perror("lseek");
5117c478bd9Sstevel@tonic-gate 			    exit(1);
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 			inbuf = (unsigned char *) mapaddr + cnt;
5147c478bd9Sstevel@tonic-gate 			len = cnt = st.st_size - cnt;
5157c478bd9Sstevel@tonic-gate 		} else {		/* Not a regular file, or map failed */
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 			/* mark is so. */
5187c478bd9Sstevel@tonic-gate 			mapaddr = 0;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 			/* Allocate buffer to hold 10 seconds of data */
5217c478bd9Sstevel@tonic-gate 			cnt = BUFFER_LEN * File_hdr.sample_rate *
5227c478bd9Sstevel@tonic-gate 			    File_hdr.bytes_per_unit * File_hdr.channels;
5237c478bd9Sstevel@tonic-gate 			if (bufsiz != cnt) {
5247c478bd9Sstevel@tonic-gate 				if (buf != NULL) {
5257c478bd9Sstevel@tonic-gate 					(void) free(buf);
5267c478bd9Sstevel@tonic-gate 				}
5277c478bd9Sstevel@tonic-gate 				buf = (unsigned char *) malloc(cnt);
5287c478bd9Sstevel@tonic-gate 				if (buf == NULL) {
5297c478bd9Sstevel@tonic-gate 					Error(stderr,
5307c478bd9Sstevel@tonic-gate 					    MGET("%s: couldn't allocate %dK "
5317c478bd9Sstevel@tonic-gate 					    "buf\n"), prog, bufsiz / 1000);
5327c478bd9Sstevel@tonic-gate 					exit(1);
5337c478bd9Sstevel@tonic-gate 				}
5347c478bd9Sstevel@tonic-gate 				inbuf = buf;
5357c478bd9Sstevel@tonic-gate 				bufsiz = cnt;
5367c478bd9Sstevel@tonic-gate 			}
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		/* Set buffer sizes and pointers for conversion, if any */
5407c478bd9Sstevel@tonic-gate 		switch (Decode) {
5417c478bd9Sstevel@tonic-gate 		default:
5427c478bd9Sstevel@tonic-gate 		case AUDIO_ENCODING_NONE:
5437c478bd9Sstevel@tonic-gate 			insiz = bufsiz;
5447c478bd9Sstevel@tonic-gate 			outbuf = (char *)buf;
5457c478bd9Sstevel@tonic-gate 			break;
5467c478bd9Sstevel@tonic-gate 		case AUDIO_ENCODING_G721:
5477c478bd9Sstevel@tonic-gate 			insiz = ADPCM_SIZE / 2;
5487c478bd9Sstevel@tonic-gate 			outbuf = (char *)adpcm_buf;
5497c478bd9Sstevel@tonic-gate 			initmux(1, 2);
5507c478bd9Sstevel@tonic-gate 			break;
5517c478bd9Sstevel@tonic-gate 		case AUDIO_ENCODING_G723:
5527c478bd9Sstevel@tonic-gate 			insiz = (ADPCM_SIZE * 3) / 8;
5537c478bd9Sstevel@tonic-gate 			outbuf = (char *)adpcm_buf;
5547c478bd9Sstevel@tonic-gate 			initmux(3, 8);
5557c478bd9Sstevel@tonic-gate 			break;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		/*
5597c478bd9Sstevel@tonic-gate 		 * 8-bit audio isn't a problem, however 16-bit audio is.
5607c478bd9Sstevel@tonic-gate 		 * If the file is an endian that is different from the machine
5617c478bd9Sstevel@tonic-gate 		 * then the bytes will need to be swapped.
5627c478bd9Sstevel@tonic-gate 		 *
5637c478bd9Sstevel@tonic-gate 		 * Note: Because the G.72X conversions produce 8bit output,
5647c478bd9Sstevel@tonic-gate 		 * they don't require a byte swap before display and so
5657c478bd9Sstevel@tonic-gate 		 * this scheme works just fine. If a conversion is added
5667c478bd9Sstevel@tonic-gate 		 * that produces a 16 bit result and therefore requires
5677c478bd9Sstevel@tonic-gate 		 * byte swapping before output, then a mechanism
5687c478bd9Sstevel@tonic-gate 		 * for chaining the two conversions will have to be built.
5697c478bd9Sstevel@tonic-gate 		 *
5707c478bd9Sstevel@tonic-gate 		 * Note: The following if() could be simplified, but then
5717c478bd9Sstevel@tonic-gate 		 * it gets to be very hard to read. So it's left as is.
5727c478bd9Sstevel@tonic-gate 		 */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		if (File_hdr.bytes_per_unit == 2 &&
5757c478bd9Sstevel@tonic-gate 		    ((!NetEndian && file_type == FILE_AIFF) ||
5767c478bd9Sstevel@tonic-gate 		    (!NetEndian && file_type == FILE_AU) ||
5777c478bd9Sstevel@tonic-gate 		    (NetEndian && file_type == FILE_WAV))) {
5787c478bd9Sstevel@tonic-gate 			swapBytes = TRUE;
5797c478bd9Sstevel@tonic-gate 		} else {
5807c478bd9Sstevel@tonic-gate 			swapBytes = FALSE;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		if (swapBytes) {
5847c478bd9Sstevel@tonic-gate 			/* Read in interal number of sample frames. */
5857c478bd9Sstevel@tonic-gate 			frame = File_hdr.bytes_per_unit * File_hdr.channels;
5867c478bd9Sstevel@tonic-gate 			insiz = (SWAP_SIZE / frame) * frame;
5877c478bd9Sstevel@tonic-gate 			/* make the output buffer  the swap buffer. */
5887c478bd9Sstevel@tonic-gate 			outbuf = (char *)swap_buf;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * At this point, we're all ready to copy the data.
5937c478bd9Sstevel@tonic-gate 		 */
5947c478bd9Sstevel@tonic-gate 		if (mapaddr == 0) { /* Not mmapped, do it a buffer at a time. */
5957c478bd9Sstevel@tonic-gate 			inbuf = buf;
5967c478bd9Sstevel@tonic-gate 			frame = File_hdr.bytes_per_unit * File_hdr.channels;
5977c478bd9Sstevel@tonic-gate 			rem = 0;
5987c478bd9Sstevel@tonic-gate 			while ((cnt = read(ifd, inbuf+rem, insiz-rem)) >= 0) {
5997c478bd9Sstevel@tonic-gate 				/*
6007c478bd9Sstevel@tonic-gate 				 * We need to ensure only an integral number of
6017c478bd9Sstevel@tonic-gate 				 * samples is ever written to the audio device.
6027c478bd9Sstevel@tonic-gate 				 */
6037c478bd9Sstevel@tonic-gate 				cnt = cnt + rem;
6047c478bd9Sstevel@tonic-gate 				rem = cnt % frame;
6057c478bd9Sstevel@tonic-gate 				cnt = cnt - rem;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 				/*
6087c478bd9Sstevel@tonic-gate 				 * If decoding adpcm, or swapping bytes do it
6097c478bd9Sstevel@tonic-gate 				 * now.
6107c478bd9Sstevel@tonic-gate 				 *
6117c478bd9Sstevel@tonic-gate 				 * We treat the swapping like a separate
6127c478bd9Sstevel@tonic-gate 				 * encoding here because the G.72X encodings
6137c478bd9Sstevel@tonic-gate 				 * decode to single byte output samples. If
6147c478bd9Sstevel@tonic-gate 				 * another encoding is added and it produces
6157c478bd9Sstevel@tonic-gate 				 * multi-byte output samples this will have to
6167c478bd9Sstevel@tonic-gate 				 * be changed.
6177c478bd9Sstevel@tonic-gate 				 */
6187c478bd9Sstevel@tonic-gate 				if (Decode == AUDIO_ENCODING_G721) {
6197c478bd9Sstevel@tonic-gate 				    outsiz = 0;
6207c478bd9Sstevel@tonic-gate 				    demux(1, cnt / File_hdr.channels);
6217c478bd9Sstevel@tonic-gate 				    for (c = 0; c < File_hdr.channels; c++) {
6227c478bd9Sstevel@tonic-gate 					err = g721_decode(in_ch_data[c],
6237c478bd9Sstevel@tonic-gate 					    cnt / File_hdr.channels,
6247c478bd9Sstevel@tonic-gate 					    &File_hdr,
6257c478bd9Sstevel@tonic-gate 					    (void*)out_ch_data[c],
6267c478bd9Sstevel@tonic-gate 					    &tsize,
6277c478bd9Sstevel@tonic-gate 					    &adpcm_state[c]);
6287c478bd9Sstevel@tonic-gate 					outsiz = outsiz + tsize;
6297c478bd9Sstevel@tonic-gate 					if (err != AUDIO_SUCCESS) {
6307c478bd9Sstevel@tonic-gate 					    Error(stderr, MGET(
6317c478bd9Sstevel@tonic-gate 						"%s: error decoding g721\n"),
6327c478bd9Sstevel@tonic-gate 						prog);
6337c478bd9Sstevel@tonic-gate 						errorStatus++;
6347c478bd9Sstevel@tonic-gate 						break;
6357c478bd9Sstevel@tonic-gate 					}
6367c478bd9Sstevel@tonic-gate 				    }
6377c478bd9Sstevel@tonic-gate 				    mux(outbuf);
6387c478bd9Sstevel@tonic-gate 				    cnt = outsiz;
6397c478bd9Sstevel@tonic-gate 				} else if (Decode == AUDIO_ENCODING_G723) {
6407c478bd9Sstevel@tonic-gate 				    outsiz = 0;
6417c478bd9Sstevel@tonic-gate 				    demux(3, cnt / File_hdr.channels);
6427c478bd9Sstevel@tonic-gate 				    for (c = 0; c < File_hdr.channels; c++) {
6437c478bd9Sstevel@tonic-gate 					err = g723_decode(in_ch_data[c],
6447c478bd9Sstevel@tonic-gate 					    cnt / File_hdr.channels,
6457c478bd9Sstevel@tonic-gate 					    &File_hdr,
6467c478bd9Sstevel@tonic-gate 					    (void*)out_ch_data[c],
6477c478bd9Sstevel@tonic-gate 					    &tsize,
6487c478bd9Sstevel@tonic-gate 					    &adpcm_state[c]);
6497c478bd9Sstevel@tonic-gate 					outsiz = outsiz + tsize;
6507c478bd9Sstevel@tonic-gate 					if (err != AUDIO_SUCCESS) {
6517c478bd9Sstevel@tonic-gate 					    Error(stderr, MGET(
6527c478bd9Sstevel@tonic-gate 						"%s: error decoding g723\n"),
6537c478bd9Sstevel@tonic-gate 						prog);
6547c478bd9Sstevel@tonic-gate 					    errorStatus++;
6557c478bd9Sstevel@tonic-gate 					    break;
6567c478bd9Sstevel@tonic-gate 					}
6577c478bd9Sstevel@tonic-gate 				    }
6587c478bd9Sstevel@tonic-gate 				    mux(outbuf);
6597c478bd9Sstevel@tonic-gate 				    cnt = outsiz;
6607c478bd9Sstevel@tonic-gate 				} else if (swapBytes) {
6617c478bd9Sstevel@tonic-gate 					swab((char *)inbuf, outbuf, cnt);
6627c478bd9Sstevel@tonic-gate 				}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 				/* If input EOF, write an eof marker */
6657c478bd9Sstevel@tonic-gate 				err = write(Audio_fd, outbuf, cnt);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 				if (err < 0) {
6687c478bd9Sstevel@tonic-gate 					perror("write");
6697c478bd9Sstevel@tonic-gate 					errorStatus++;
6707c478bd9Sstevel@tonic-gate 					break;
6717c478bd9Sstevel@tonic-gate 				} else if (err != cnt) {
6727c478bd9Sstevel@tonic-gate 					Error(stderr,
6737c478bd9Sstevel@tonic-gate 					    MGET("%s: output error: "), prog);
6747c478bd9Sstevel@tonic-gate 					perror("");
6757c478bd9Sstevel@tonic-gate 					errorStatus++;
6767c478bd9Sstevel@tonic-gate 					break;
6777c478bd9Sstevel@tonic-gate 				}
6787c478bd9Sstevel@tonic-gate 				if (cnt == 0) {
6797c478bd9Sstevel@tonic-gate 					break;
6807c478bd9Sstevel@tonic-gate 				}
6817c478bd9Sstevel@tonic-gate 				/* Move remainder to the front of the buffer */
6827c478bd9Sstevel@tonic-gate 				if (rem != 0) {
6837c478bd9Sstevel@tonic-gate 					(void *)memcpy(inbuf, inbuf + cnt, rem);
6847c478bd9Sstevel@tonic-gate 				}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 			if (cnt < 0) {
6887c478bd9Sstevel@tonic-gate 				Error(stderr, MGET("%s: error reading "), prog);
6897c478bd9Sstevel@tonic-gate 				perror(Ifile);
6907c478bd9Sstevel@tonic-gate 				errorStatus++;
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 		} else {	/* We're mmaped */
6937c478bd9Sstevel@tonic-gate 			if ((Decode != AUDIO_ENCODING_NONE) || swapBytes) {
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 				/* Transform data if we have to. */
6967c478bd9Sstevel@tonic-gate 				for (i = 0; i <= len; i += cnt) {
6977c478bd9Sstevel@tonic-gate 					cnt = insiz;
6987c478bd9Sstevel@tonic-gate 					if ((i + cnt) > len) {
6997c478bd9Sstevel@tonic-gate 						cnt = len - i;
7007c478bd9Sstevel@tonic-gate 					}
7017c478bd9Sstevel@tonic-gate 					if (Decode == AUDIO_ENCODING_G721) {
7027c478bd9Sstevel@tonic-gate 					    outsiz = 0;
7037c478bd9Sstevel@tonic-gate 					    demux(1, cnt / File_hdr.channels);
7047c478bd9Sstevel@tonic-gate 					    for (c = 0; c < File_hdr.channels;
7057c478bd9Sstevel@tonic-gate 						c++) {
7067c478bd9Sstevel@tonic-gate 						err = g721_decode(
7077c478bd9Sstevel@tonic-gate 						    in_ch_data[c],
7087c478bd9Sstevel@tonic-gate 						    cnt / File_hdr.channels,
7097c478bd9Sstevel@tonic-gate 						    &File_hdr,
7107c478bd9Sstevel@tonic-gate 						    (void*)out_ch_data[c],
7117c478bd9Sstevel@tonic-gate 						    &tsize,
7127c478bd9Sstevel@tonic-gate 						    &adpcm_state[c]);
7137c478bd9Sstevel@tonic-gate 						outsiz = outsiz + tsize;
7147c478bd9Sstevel@tonic-gate 						if (err != AUDIO_SUCCESS) {
7157c478bd9Sstevel@tonic-gate 						    Error(stderr, MGET(
7167c478bd9Sstevel@tonic-gate 							"%s: error decoding "
7177c478bd9Sstevel@tonic-gate 							"g721\n"), prog);
7187c478bd9Sstevel@tonic-gate 						    errorStatus++;
7197c478bd9Sstevel@tonic-gate 						    break;
7207c478bd9Sstevel@tonic-gate 						}
7217c478bd9Sstevel@tonic-gate 					    }
7227c478bd9Sstevel@tonic-gate 					    mux(outbuf);
7237c478bd9Sstevel@tonic-gate 					} else if
7247c478bd9Sstevel@tonic-gate 					    (Decode == AUDIO_ENCODING_G723) {
7257c478bd9Sstevel@tonic-gate 						outsiz = 0;
7267c478bd9Sstevel@tonic-gate 						demux(3,
7277c478bd9Sstevel@tonic-gate 						    cnt / File_hdr.channels);
7287c478bd9Sstevel@tonic-gate 						for (c = 0;
7297c478bd9Sstevel@tonic-gate 							c < File_hdr.channels;
7307c478bd9Sstevel@tonic-gate 							c++) {
7317c478bd9Sstevel@tonic-gate 						    err = g723_decode(
7327c478bd9Sstevel@tonic-gate 							in_ch_data[c],
7337c478bd9Sstevel@tonic-gate 							cnt /
7347c478bd9Sstevel@tonic-gate 							    File_hdr.channels,
7357c478bd9Sstevel@tonic-gate 							&File_hdr,
7367c478bd9Sstevel@tonic-gate 							(void*)out_ch_data[c],
7377c478bd9Sstevel@tonic-gate 							&tsize,
7387c478bd9Sstevel@tonic-gate 							&adpcm_state[c]);
7397c478bd9Sstevel@tonic-gate 						    outsiz = outsiz + tsize;
7407c478bd9Sstevel@tonic-gate 						    if (err != AUDIO_SUCCESS) {
7417c478bd9Sstevel@tonic-gate 							Error(stderr, MGET(
7427c478bd9Sstevel@tonic-gate 							    "%s: error "
7437c478bd9Sstevel@tonic-gate 							    "decoding g723\n"),
7447c478bd9Sstevel@tonic-gate 							    prog);
7457c478bd9Sstevel@tonic-gate 							errorStatus++;
7467c478bd9Sstevel@tonic-gate 							break;
7477c478bd9Sstevel@tonic-gate 						    }
7487c478bd9Sstevel@tonic-gate 						}
7497c478bd9Sstevel@tonic-gate 						mux(outbuf);
7507c478bd9Sstevel@tonic-gate 					} else if (swapBytes) {
7517c478bd9Sstevel@tonic-gate 						swab((char *)inbuf, outbuf,
7527c478bd9Sstevel@tonic-gate 						    cnt);
7537c478bd9Sstevel@tonic-gate 						outsiz = cnt;
7547c478bd9Sstevel@tonic-gate 					}
7557c478bd9Sstevel@tonic-gate 					inbuf += cnt;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 					/* If input EOF, write an eof marker */
7587c478bd9Sstevel@tonic-gate 					err = write(Audio_fd, (char *)outbuf,
7597c478bd9Sstevel@tonic-gate 					    outsiz);
7607c478bd9Sstevel@tonic-gate 					if (err < 0) {
7617c478bd9Sstevel@tonic-gate 						perror("write");
7627c478bd9Sstevel@tonic-gate 						errorStatus++;
7637c478bd9Sstevel@tonic-gate 					} else if (outsiz == 0) {
7647c478bd9Sstevel@tonic-gate 						break;
7657c478bd9Sstevel@tonic-gate 					}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 				}
7687c478bd9Sstevel@tonic-gate 			} else {
7697c478bd9Sstevel@tonic-gate 				/* write the whole thing at once!  */
7707c478bd9Sstevel@tonic-gate 				err = write(Audio_fd, inbuf, len);
7717c478bd9Sstevel@tonic-gate 				if (err < 0) {
7727c478bd9Sstevel@tonic-gate 					perror("write");
7737c478bd9Sstevel@tonic-gate 					errorStatus++;
7747c478bd9Sstevel@tonic-gate 				}
7757c478bd9Sstevel@tonic-gate 				if (err != len) {
7767c478bd9Sstevel@tonic-gate 					Error(stderr,
7777c478bd9Sstevel@tonic-gate 					    MGET("%s: output error: "), prog);
7787c478bd9Sstevel@tonic-gate 					perror("");
7797c478bd9Sstevel@tonic-gate 					errorStatus++;
7807c478bd9Sstevel@tonic-gate 				}
7817c478bd9Sstevel@tonic-gate 				err = write(Audio_fd, inbuf, 0);
7827c478bd9Sstevel@tonic-gate 				if (err < 0) {
7837c478bd9Sstevel@tonic-gate 					perror("write");
7847c478bd9Sstevel@tonic-gate 					errorStatus++;
7857c478bd9Sstevel@tonic-gate 				}
7867c478bd9Sstevel@tonic-gate 			}
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		/* Free memory if decoding ADPCM */
7907c478bd9Sstevel@tonic-gate 		switch (Decode) {
7917c478bd9Sstevel@tonic-gate 		case AUDIO_ENCODING_G721:
7927c478bd9Sstevel@tonic-gate 		case AUDIO_ENCODING_G723:
7937c478bd9Sstevel@tonic-gate 			freemux();
7947c478bd9Sstevel@tonic-gate 			break;
7957c478bd9Sstevel@tonic-gate 		default:
7967c478bd9Sstevel@tonic-gate 			break;
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate closeinput:;
8007c478bd9Sstevel@tonic-gate 		if (mapaddr != 0)
8017c478bd9Sstevel@tonic-gate 			(void) munmap(mapaddr, st.st_size);
8027c478bd9Sstevel@tonic-gate 		(void) close(ifd);		/* close input file */
8037c478bd9Sstevel@tonic-gate 		if (Errdetect) {
8047c478bd9Sstevel@tonic-gate 			cnt = 0;
8057c478bd9Sstevel@tonic-gate 			audio_set_play_error(Audio_fd, (unsigned int *)&cnt);
8067c478bd9Sstevel@tonic-gate 			if (cnt) {
8077c478bd9Sstevel@tonic-gate 				Error(stderr,
8087c478bd9Sstevel@tonic-gate 				    MGET("%s: output underflow in %s\n"),
8097c478bd9Sstevel@tonic-gate 				    Ifile, prog);
8107c478bd9Sstevel@tonic-gate 				errorStatus++;
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate nextfile:;
8147c478bd9Sstevel@tonic-gate 	} while ((argc > 0) && (argc--, (Ifile = *argv++) != NULL));
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	/*
8177c478bd9Sstevel@tonic-gate 	 * Though drain is implicit on close(), it's performed here
8187c478bd9Sstevel@tonic-gate 	 * to ensure that the volume is reset after all output is complete.
8197c478bd9Sstevel@tonic-gate 	 */
8207c478bd9Sstevel@tonic-gate 	(void) audio_drain(Audio_fd, FALSE);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/* Flush any remaining audio */
8237c478bd9Sstevel@tonic-gate 	(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (Volume != INT_MAX)
8267c478bd9Sstevel@tonic-gate 		(void) audio_set_play_gain(Audio_fd, &Savevol);
8277c478bd9Sstevel@tonic-gate 	if ((Audio_ctlfd >= 0) && (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0)) {
8287c478bd9Sstevel@tonic-gate 		(void) audio_set_play_config(Audio_fd, &Save_hdr);
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 	(void) close(Audio_fd);			/* close output */
8317c478bd9Sstevel@tonic-gate 	return (errorStatus);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate  * Try to reconfigure the audio device to match the file encoding.
8377c478bd9Sstevel@tonic-gate  * If this fails, we should attempt to make the input data match the
8387c478bd9Sstevel@tonic-gate  * device encoding.  For now, we give up on this file.
8397c478bd9Sstevel@tonic-gate  *
8407c478bd9Sstevel@tonic-gate  * Returns TRUE if successful.  Returns FALSE if not.
8417c478bd9Sstevel@tonic-gate  */
8427c478bd9Sstevel@tonic-gate static int
8437c478bd9Sstevel@tonic-gate reconfig(void)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	int	err;
8467c478bd9Sstevel@tonic-gate 	char	msg[AUDIO_MAX_ENCODE_INFO];
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	Dev_hdr = File_hdr;
8497c478bd9Sstevel@tonic-gate 	err = audio_set_play_config(Audio_fd, &Dev_hdr);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	switch (err) {
8527c478bd9Sstevel@tonic-gate 	case AUDIO_SUCCESS:
8537c478bd9Sstevel@tonic-gate 		return (TRUE);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	case AUDIO_ERR_NOEFFECT:
8567c478bd9Sstevel@tonic-gate 		/*
8577c478bd9Sstevel@tonic-gate 		 * Couldn't change the device.
8587c478bd9Sstevel@tonic-gate 		 * Check to see if we're nearly compatible.
8597c478bd9Sstevel@tonic-gate 		 * audio_cmp_hdr() returns >0 if only sample rate difference.
8607c478bd9Sstevel@tonic-gate 		 */
8617c478bd9Sstevel@tonic-gate 		if (audio_cmp_hdr(&Dev_hdr, &File_hdr) > 0) {
8627c478bd9Sstevel@tonic-gate 			double	ratio;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 			ratio = (double)abs((int)
8657c478bd9Sstevel@tonic-gate 			    (Dev_hdr.sample_rate - File_hdr.sample_rate)) /
8667c478bd9Sstevel@tonic-gate 			    (double)File_hdr.sample_rate;
8677c478bd9Sstevel@tonic-gate 			if (ratio <= SAMPLE_RATE_THRESHOLD) {
8687c478bd9Sstevel@tonic-gate 				if (Verbose) {
8697c478bd9Sstevel@tonic-gate 					Error(stderr,
870*88447a05SGarrett D'Amore 					    MGET("%s: WARNING: %s sampled at "
871*88447a05SGarrett D'Amore 					    "%d, playing at %d\n"),
8727c478bd9Sstevel@tonic-gate 					    prog, Ifile, File_hdr.sample_rate,
8737c478bd9Sstevel@tonic-gate 					    Dev_hdr.sample_rate);
8747c478bd9Sstevel@tonic-gate 				}
8757c478bd9Sstevel@tonic-gate 				return (TRUE);
8767c478bd9Sstevel@tonic-gate 			}
8777c478bd9Sstevel@tonic-gate 			Error(stderr,
8787c478bd9Sstevel@tonic-gate 			    MGET("%s: %s sample rate %d not available\n"),
8797c478bd9Sstevel@tonic-gate 			    prog, Ifile, File_hdr.sample_rate);
8807c478bd9Sstevel@tonic-gate 			return (FALSE);
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 		(void) audio_enc_to_str(&File_hdr, msg);
8837c478bd9Sstevel@tonic-gate 		Error(stderr, MGET("%s: %s encoding not available: %s\n"),
8847c478bd9Sstevel@tonic-gate 		    prog, Ifile, msg);
8857c478bd9Sstevel@tonic-gate 		return (FALSE);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	default:
8887c478bd9Sstevel@tonic-gate 		Error(stderr,
8897c478bd9Sstevel@tonic-gate 		    MGET("%s: %s audio encoding type not available\n"),
8907c478bd9Sstevel@tonic-gate 		    prog, Ifile);
8917c478bd9Sstevel@tonic-gate 		exit(1);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	return (TRUE);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /* Parse an unsigned integer */
8987c478bd9Sstevel@tonic-gate static int
8997c478bd9Sstevel@tonic-gate parse_unsigned(char *str, unsigned *dst, char *flag)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	char	x;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (sscanf(str, "%u%c", dst, &x) != 1) {
9047c478bd9Sstevel@tonic-gate 		Error(stderr, MGET("%s: invalid value for %s\n"), prog, flag);
9057c478bd9Sstevel@tonic-gate 		return (1);
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 	return (0);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * Search for fname in path and open. Ignore path not opened O_RDONLY.
9127c478bd9Sstevel@tonic-gate  * Note: in general path can be a list of ':' separated paths to search
9137c478bd9Sstevel@tonic-gate  * through.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate static int
9167c478bd9Sstevel@tonic-gate path_open(char *fname, int flags, mode_t mode, char *path)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	char		fullpath[MAXPATHLEN]; 	/* full path of file */
9197c478bd9Sstevel@tonic-gate 	char 		*buf;			/* malloc off the tmp buff */
9207c478bd9Sstevel@tonic-gate 	char		*cp;
9217c478bd9Sstevel@tonic-gate 	struct stat	st;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (!fname) {		/* bogus */
9247c478bd9Sstevel@tonic-gate 		return (-1);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * cases where we don't bother checking path:
9297c478bd9Sstevel@tonic-gate 	 *	- no path
9307c478bd9Sstevel@tonic-gate 	 *	- file not opened O_RDONLY
9317c478bd9Sstevel@tonic-gate 	 *	- not a relative path (i.e. starts with /, ./, or ../).
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if ((!path) || (flags != O_RDONLY) || (*fname == '/') ||
9357c478bd9Sstevel@tonic-gate 	    (strncmp(fname, "./", strlen("./")) == 0) ||
9367c478bd9Sstevel@tonic-gate 	    (strncmp(fname, "../", strlen("../")) == 0)) {
9377c478bd9Sstevel@tonic-gate 		return (open(fname, flags, mode));
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * Malloc off a buffer to hold the path variable.
9427c478bd9Sstevel@tonic-gate 	 * This is NOT limited to MAXPATHLEN characters as
9437c478bd9Sstevel@tonic-gate 	 * it may contain multiple paths.
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	buf = malloc(strlen(path) + 1);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * if first character is ':', but not the one following it,
9497c478bd9Sstevel@tonic-gate 	 * skip over it - or it'll be interpreted as "./". it's OK
9507c478bd9Sstevel@tonic-gate 	 * to have "::" since that does mean "./".
9517c478bd9Sstevel@tonic-gate 	 */
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if ((path[0] == ':') && (path[1] != ':')) {
9547c478bd9Sstevel@tonic-gate 		(void) strncpy(buf, path+1, strlen(path));
9557c478bd9Sstevel@tonic-gate 	} else {
9567c478bd9Sstevel@tonic-gate 		(void) strncpy(buf, path, strlen(path));
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	for (path = buf; path && *path; ) {
9607c478bd9Sstevel@tonic-gate 		if (cp = strchr(path, ':')) {
9617c478bd9Sstevel@tonic-gate 			*cp++ = NULL; /* now pts to next path element */
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 		/* the safest way to create the path string :-) */
9657c478bd9Sstevel@tonic-gate 		if (*path) {
9667c478bd9Sstevel@tonic-gate 			(void) strncpy(fullpath, path, MAXPATHLEN);
9677c478bd9Sstevel@tonic-gate 			(void) strncat(fullpath, "/", MAXPATHLEN);
9687c478bd9Sstevel@tonic-gate 		} else {
9697c478bd9Sstevel@tonic-gate 			/* a NULL path element means "./" */
9707c478bd9Sstevel@tonic-gate 			(void) strncpy(fullpath, "./", MAXPATHLEN);
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 		(void) strncat(fullpath, fname, MAXPATHLEN);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		/* see if there's a match */
9757c478bd9Sstevel@tonic-gate 		if (stat(fullpath, &st) >= 0) {
9767c478bd9Sstevel@tonic-gate 			if (S_ISREG(st.st_mode)) {
9777c478bd9Sstevel@tonic-gate 				/* got a match! */
9787c478bd9Sstevel@tonic-gate 				if (Verbose) {
9797c478bd9Sstevel@tonic-gate 					Error(stderr,
980*88447a05SGarrett D'Amore 					    MGET("%s: Found %s in path "
981*88447a05SGarrett D'Amore 					    "at %s\n"),
9827c478bd9Sstevel@tonic-gate 					    prog, fname, fullpath);
9837c478bd9Sstevel@tonic-gate 				}
9847c478bd9Sstevel@tonic-gate 				return (open(fullpath, flags, mode));
9857c478bd9Sstevel@tonic-gate 			}
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		/* go on to the next one */
9897c478bd9Sstevel@tonic-gate 		path = cp;
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/*
9937c478bd9Sstevel@tonic-gate 	 * if we fall through with no match, just do a normal file open
9947c478bd9Sstevel@tonic-gate 	 */
9957c478bd9Sstevel@tonic-gate 	return (open(fname, flags, mode));
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate /*
10007c478bd9Sstevel@tonic-gate  * initmux()
10017c478bd9Sstevel@tonic-gate  *
10027c478bd9Sstevel@tonic-gate  * Description:
10037c478bd9Sstevel@tonic-gate  *      Allocates memory for carrying out demultiplexing/multiplexing.
10047c478bd9Sstevel@tonic-gate  *
10057c478bd9Sstevel@tonic-gate  * Arguments:
10067c478bd9Sstevel@tonic-gate  *      int		unitsz		Bytes per unit
10077c478bd9Sstevel@tonic-gate  *	int		unitsp		Samples per unit
10087c478bd9Sstevel@tonic-gate  *
10097c478bd9Sstevel@tonic-gate  * Returns:
10107c478bd9Sstevel@tonic-gate  *      void
10117c478bd9Sstevel@tonic-gate  */
10127c478bd9Sstevel@tonic-gate static void
10137c478bd9Sstevel@tonic-gate initmux(int unitsz, int unitsp)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	int	c;		/* Channel */
10167c478bd9Sstevel@tonic-gate 	int	in_ch_size;	/* Input channel size */
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/* Size of each input channel */
10197c478bd9Sstevel@tonic-gate 	in_ch_size = insiz / File_hdr.channels;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/* Size of each output channel */
10227c478bd9Sstevel@tonic-gate 	out_ch_size = in_ch_size * unitsp / unitsz;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	/* Allocate pointers to input channels */
1025*88447a05SGarrett D'Amore 	in_ch_data = malloc(sizeof (unsigned char *) * File_hdr.channels);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	if (in_ch_data == NULL) {
1028*88447a05SGarrett D'Amore 		Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1029*88447a05SGarrett D'Amore 		    prog, sizeof (unsigned char *) * File_hdr.channels / 1000);
10307c478bd9Sstevel@tonic-gate 		exit(1);
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/* Allocate input channels */
10347c478bd9Sstevel@tonic-gate 	for (c = 0; c < File_hdr.channels; c++) {
1035*88447a05SGarrett D'Amore 		in_ch_data[c] = malloc(sizeof (unsigned char) * in_ch_size);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		if (in_ch_data[c] == NULL) {
1038*88447a05SGarrett D'Amore 			Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1039*88447a05SGarrett D'Amore 			    prog, in_ch_size / 1000);
10407c478bd9Sstevel@tonic-gate 			exit(1);
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/* Allocate pointers to output channels */
1045*88447a05SGarrett D'Amore 	out_ch_data = malloc(sizeof (unsigned char *) * File_hdr.channels);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	if (out_ch_data == NULL) {
1048*88447a05SGarrett D'Amore 		Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1049*88447a05SGarrett D'Amore 		    prog, sizeof (unsigned char *) * File_hdr.channels / 1000);
10507c478bd9Sstevel@tonic-gate 		exit(1);
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	/* Allocate output channels */
10547c478bd9Sstevel@tonic-gate 	for (c = 0; c < File_hdr.channels; c++) {
1055*88447a05SGarrett D'Amore 		out_ch_data[c] = malloc(sizeof (unsigned char) * out_ch_size);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		if (out_ch_data[c] == NULL) {
1058*88447a05SGarrett D'Amore 			Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1059*88447a05SGarrett D'Amore 			    prog, out_ch_size / 1000);
10607c478bd9Sstevel@tonic-gate 			exit(1);
10617c478bd9Sstevel@tonic-gate 		}
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * demux()
10677c478bd9Sstevel@tonic-gate  *
10687c478bd9Sstevel@tonic-gate  * Description:
10697c478bd9Sstevel@tonic-gate  *      Split a multichannel signal into separate channels.
10707c478bd9Sstevel@tonic-gate  *
10717c478bd9Sstevel@tonic-gate  * Arguments:
10727c478bd9Sstevel@tonic-gate  *      int		unitsz		Bytes per unit
10737c478bd9Sstevel@tonic-gate  *	int		cnt		Bytes to process
10747c478bd9Sstevel@tonic-gate  *
10757c478bd9Sstevel@tonic-gate  * Returns:
10767c478bd9Sstevel@tonic-gate  *      void
10777c478bd9Sstevel@tonic-gate  */
10787c478bd9Sstevel@tonic-gate static void
10797c478bd9Sstevel@tonic-gate demux(int unitsz, int cnt)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	int	c;		/* Channel */
10827c478bd9Sstevel@tonic-gate 	int	s;		/* Sample */
10837c478bd9Sstevel@tonic-gate 	int	b;		/* Byte */
10847c478bd9Sstevel@tonic-gate 	int	tp;		/* Pointer into current data */
10857c478bd9Sstevel@tonic-gate 	int	dp;		/* Pointer into target data */
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	/* Split */
10887c478bd9Sstevel@tonic-gate 	for (c = 0; c < File_hdr.channels; c++) {
10897c478bd9Sstevel@tonic-gate 		for (s = 0; s < cnt / unitsz; s++) {
10907c478bd9Sstevel@tonic-gate 			tp = s * unitsz;
10917c478bd9Sstevel@tonic-gate 			dp = (s * File_hdr.channels + c) * unitsz;
10927c478bd9Sstevel@tonic-gate 			for (b = 0; b < unitsz; b++) {
10937c478bd9Sstevel@tonic-gate 				in_ch_data[c][tp + b] = inbuf[dp + b];
10947c478bd9Sstevel@tonic-gate 			}
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * mux()
11017c478bd9Sstevel@tonic-gate  *
11027c478bd9Sstevel@tonic-gate  * Description:
11037c478bd9Sstevel@tonic-gate  *      Combine separate channels to produce a multichannel signal.
11047c478bd9Sstevel@tonic-gate  *
11057c478bd9Sstevel@tonic-gate  * Arguments:
11067c478bd9Sstevel@tonic-gate  *      char		*outbuf		Combined signal
11077c478bd9Sstevel@tonic-gate  *
11087c478bd9Sstevel@tonic-gate  * Returns:
11097c478bd9Sstevel@tonic-gate  *      void
11107c478bd9Sstevel@tonic-gate  */
11117c478bd9Sstevel@tonic-gate static void
11127c478bd9Sstevel@tonic-gate mux(char *outbuf)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	int	c;		/* Channel */
11157c478bd9Sstevel@tonic-gate 	int	s;		/* Sample */
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	/* Combine */
11187c478bd9Sstevel@tonic-gate 	for (c = 0; c < File_hdr.channels; c++) {
11197c478bd9Sstevel@tonic-gate 		for (s = 0; s < out_ch_size; s++) {
11207c478bd9Sstevel@tonic-gate 			outbuf[File_hdr.channels * s + c] = out_ch_data[c][s];
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate /*
11267c478bd9Sstevel@tonic-gate  * freemux()
11277c478bd9Sstevel@tonic-gate  *
11287c478bd9Sstevel@tonic-gate  * Description:
11297c478bd9Sstevel@tonic-gate  *      Free memory used in multiplexing/demultiplexing.
11307c478bd9Sstevel@tonic-gate  *
11317c478bd9Sstevel@tonic-gate  * Arguments:
11327c478bd9Sstevel@tonic-gate  *      void
11337c478bd9Sstevel@tonic-gate  *
11347c478bd9Sstevel@tonic-gate  * Returns:
11357c478bd9Sstevel@tonic-gate  *      void
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate static void
11387c478bd9Sstevel@tonic-gate freemux(void)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	int	c;		/* Channel */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/* Free */
11437c478bd9Sstevel@tonic-gate 	for (c = 0; c < File_hdr.channels; c++) {
11447c478bd9Sstevel@tonic-gate 		free(in_ch_data[c]);
11457c478bd9Sstevel@tonic-gate 		free(out_ch_data[c]);
11467c478bd9Sstevel@tonic-gate 		free(&adpcm_state[c]);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	free(in_ch_data);
11507c478bd9Sstevel@tonic-gate 	free(out_ch_data);
11517c478bd9Sstevel@tonic-gate }
1152