10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9484Sgarrett.damore@Sun.COM * Common Development and Distribution License (the "License").
6*9484Sgarrett.damore@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9484Sgarrett.damore@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Command-line audio play utility */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <signal.h>
350Sstevel@tonic-gate #include <locale.h>
360Sstevel@tonic-gate #include <limits.h> /* All occurances of INT_MAX used to be ~0 (by MCA) */
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <stropts.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/param.h>
430Sstevel@tonic-gate #include <sys/ioctl.h>
440Sstevel@tonic-gate #include <sys/mman.h>
450Sstevel@tonic-gate #include <netinet/in.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include <libaudio.h>
480Sstevel@tonic-gate #include <audio_device.h>
490Sstevel@tonic-gate #include <audio_encode.h>
500Sstevel@tonic-gate
510Sstevel@tonic-gate /* localization stuff */
520Sstevel@tonic-gate #define MGET(s) (char *)gettext(s)
530Sstevel@tonic-gate
540Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
550Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate
580Sstevel@tonic-gate #define Error (void) fprintf
590Sstevel@tonic-gate
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* Local variables */
620Sstevel@tonic-gate static char *prog;
630Sstevel@tonic-gate
64*9484Sgarrett.damore@Sun.COM static char prog_opts[] = "VEiv:d:?"; /* getopt() flags */
650Sstevel@tonic-gate
660Sstevel@tonic-gate static char *Stdin;
670Sstevel@tonic-gate
680Sstevel@tonic-gate #define MAX_GAIN (100) /* maximum gain */
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * This defines the tolerable sample rate error as a ratio between the
720Sstevel@tonic-gate * sample rates of the audio data and the audio device.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate #define SAMPLE_RATE_THRESHOLD (.01)
750Sstevel@tonic-gate
760Sstevel@tonic-gate #define BUFFER_LEN 10 /* seconds - for file i/o */
770Sstevel@tonic-gate #define ADPCM_SIZE (1000*8) /* adpcm conversion output buf size */
780Sstevel@tonic-gate #define SWAP_SIZE (8192)
790Sstevel@tonic-gate /* swap bytes conversion output buf size */
800Sstevel@tonic-gate
810Sstevel@tonic-gate static unsigned Volume = INT_MAX; /* output volume */
820Sstevel@tonic-gate static double Savevol; /* saved volume level */
83*9484Sgarrett.damore@Sun.COM
840Sstevel@tonic-gate static int Verbose = FALSE; /* verbose messages */
850Sstevel@tonic-gate static int Immediate = FALSE;
860Sstevel@tonic-gate /* don't hang waiting for device */
870Sstevel@tonic-gate static int Errdetect = FALSE; /* don't worry about underrun */
880Sstevel@tonic-gate static char *Audio_dev = "/dev/audio";
890Sstevel@tonic-gate
900Sstevel@tonic-gate static int NetEndian = TRUE; /* endian nature of the machine */
910Sstevel@tonic-gate
920Sstevel@tonic-gate static int Audio_fd = -1;
930Sstevel@tonic-gate /* file descriptor for audio device */
940Sstevel@tonic-gate static int Audio_ctlfd = -1;
950Sstevel@tonic-gate /* file descriptor for control device */
960Sstevel@tonic-gate static Audio_hdr Save_hdr;
970Sstevel@tonic-gate /* saved audio header for device */
980Sstevel@tonic-gate static Audio_hdr Dev_hdr; /* audio header for device */
990Sstevel@tonic-gate static char *Ifile; /* current filename */
1000Sstevel@tonic-gate static Audio_hdr File_hdr; /* audio header for file */
1010Sstevel@tonic-gate static unsigned Decode = AUDIO_ENCODING_NONE;
1020Sstevel@tonic-gate /* decode type, if any */
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static unsigned char *buf = NULL; /* dynamically alloc'd */
1050Sstevel@tonic-gate static unsigned bufsiz = 0; /* size of output buffer */
1060Sstevel@tonic-gate static unsigned char adpcm_buf[ADPCM_SIZE + 32];
1070Sstevel@tonic-gate /* for adpcm conversion */
1080Sstevel@tonic-gate static unsigned char swap_buf[SWAP_SIZE + 32];
1090Sstevel@tonic-gate /* for byte swap conversion */
1100Sstevel@tonic-gate static unsigned char *inbuf;
1110Sstevel@tonic-gate /* current input buffer pointer */
1120Sstevel@tonic-gate static unsigned insiz; /* current input buffer size */
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * The decode_g72x() function is capable of decoding only one channel
1160Sstevel@tonic-gate * at a time and so multichannel data must be decomposed (using demux()
1170Sstevel@tonic-gate * function below ) into its constituent channels and each passed
1180Sstevel@tonic-gate * separately to the decode_g72x() function. Encoded input channels are
1190Sstevel@tonic-gate * stored in **in_ch_data and decoded output channels in **out_ch_data.
1200Sstevel@tonic-gate * Once each channel has been decoded they are recombined (see mux()
1210Sstevel@tonic-gate * function below) before being written to the audio device. For each
1220Sstevel@tonic-gate * channel and adpcm state structure is created.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* adpcm state structures */
1260Sstevel@tonic-gate static struct audio_g72x_state *adpcm_state = NULL;
1270Sstevel@tonic-gate static unsigned char **in_ch_data = NULL; /* input channels */
1280Sstevel@tonic-gate static unsigned char **out_ch_data = NULL; /* output channels */
1290Sstevel@tonic-gate static int out_ch_size; /* output channel size */
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static char *Audio_path = NULL;
1320Sstevel@tonic-gate /* path to search for audio files */
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /* Global variables */
1350Sstevel@tonic-gate extern int getopt(int, char *const *, const char *);
1360Sstevel@tonic-gate extern int optind;
1370Sstevel@tonic-gate extern char *optarg;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /* Local functions */
1400Sstevel@tonic-gate static void usage(void);
1410Sstevel@tonic-gate static void sigint(int sig);
1420Sstevel@tonic-gate static void open_audio(void);
1430Sstevel@tonic-gate static int path_open(char *fname, int flags, mode_t mode, char *path);
1440Sstevel@tonic-gate static int parse_unsigned(char *str, unsigned *dst, char *flag);
1450Sstevel@tonic-gate static int reconfig(void);
1460Sstevel@tonic-gate static void initmux(int unitsz, int unitsp);
1470Sstevel@tonic-gate static void demux(int unitsz, int cnt);
1480Sstevel@tonic-gate static void mux(char *);
1490Sstevel@tonic-gate static void freemux(void);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate static void
usage(void)1530Sstevel@tonic-gate usage(void)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate Error(stderr, MGET("Play an audio file -- usage:\n"
156*9484Sgarrett.damore@Sun.COM "\t%s [-iV] [-v vol] [-d dev] [file ...]\n"
1570Sstevel@tonic-gate "where:\n"
1580Sstevel@tonic-gate "\t-i\tDon't hang if audio device is busy\n"
1590Sstevel@tonic-gate "\t-V\tPrint verbose warning messages\n"
1600Sstevel@tonic-gate "\t-v\tSet output volume (0 - %d)\n"
1610Sstevel@tonic-gate "\t-d\tSpecify audio device (default: /dev/audio)\n"
1620Sstevel@tonic-gate "\tfile\tList of files to play\n"
1630Sstevel@tonic-gate "\t\tIf no files specified, read stdin\n"),
164*9484Sgarrett.damore@Sun.COM prog, MAX_GAIN);
1650Sstevel@tonic-gate exit(1);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate static void
sigint(int sig)1690Sstevel@tonic-gate sigint(int sig)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate /* flush output queues before exiting */
1720Sstevel@tonic-gate if (Audio_fd >= 0) {
1730Sstevel@tonic-gate (void) audio_flush_play(Audio_fd);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* restore saved parameters */
1760Sstevel@tonic-gate if (Volume != INT_MAX)
1770Sstevel@tonic-gate (void) audio_set_play_gain(Audio_fd, &Savevol);
1780Sstevel@tonic-gate if ((Audio_ctlfd >= 0) &&
1790Sstevel@tonic-gate (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0)) {
1800Sstevel@tonic-gate (void) audio_set_play_config(Audio_fd, &Save_hdr);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate exit(1);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /* Open the audio device and initalize it. */
1870Sstevel@tonic-gate static void
open_audio(void)1880Sstevel@tonic-gate open_audio(void)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate int err;
1910Sstevel@tonic-gate double vol;
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /* Return if already open */
1940Sstevel@tonic-gate if (Audio_fd >= 0)
1950Sstevel@tonic-gate return;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /* Try opening without waiting, first */
1980Sstevel@tonic-gate Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK);
1990Sstevel@tonic-gate if ((Audio_fd < 0) && (errno == EBUSY)) {
2000Sstevel@tonic-gate if (Immediate) {
2010Sstevel@tonic-gate Error(stderr, MGET("%s: %s is busy\n"),
2020Sstevel@tonic-gate prog, Audio_dev);
2030Sstevel@tonic-gate exit(1);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate if (Verbose) {
2060Sstevel@tonic-gate Error(stderr, MGET("%s: waiting for %s..."),
2070Sstevel@tonic-gate prog, Audio_dev);
2080Sstevel@tonic-gate (void) fflush(stderr);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate /* Now hang until it's open */
2110Sstevel@tonic-gate Audio_fd = open(Audio_dev, O_WRONLY);
2120Sstevel@tonic-gate if (Verbose)
2130Sstevel@tonic-gate Error(stderr, (Audio_fd < 0) ? "\n" : MGET("open\n"));
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate if (Audio_fd < 0) {
2160Sstevel@tonic-gate Error(stderr, MGET("%s: error opening "), prog);
2170Sstevel@tonic-gate perror(Audio_dev);
2180Sstevel@tonic-gate exit(1);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /* Clear the non-blocking flag (in System V it persists after open) */
2220Sstevel@tonic-gate (void) fcntl(Audio_fd, F_SETFL,
2230Sstevel@tonic-gate (fcntl(Audio_fd, F_GETFL, 0) & ~(O_NDELAY | O_NONBLOCK)));
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /* Get the device output encoding configuration */
2260Sstevel@tonic-gate if (audio_get_play_config(Audio_fd, &Dev_hdr) != AUDIO_SUCCESS) {
2270Sstevel@tonic-gate Error(stderr, MGET("%s: %s is not an audio device\n"),
2280Sstevel@tonic-gate prog, Audio_dev);
2290Sstevel@tonic-gate exit(1);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /* If -v flag, set the output volume now */
2330Sstevel@tonic-gate if (Volume != INT_MAX) {
2340Sstevel@tonic-gate vol = (double)Volume / (double)MAX_GAIN;
2350Sstevel@tonic-gate (void) audio_get_play_gain(Audio_fd, &Savevol);
2360Sstevel@tonic-gate err = audio_set_play_gain(Audio_fd, &vol);
2370Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
2380Sstevel@tonic-gate Error(stderr,
2390Sstevel@tonic-gate MGET("%s: could not set output volume for %s\n"),
2400Sstevel@tonic-gate prog, Audio_dev);
2410Sstevel@tonic-gate exit(1);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /* Play a list of audio files. */
2470Sstevel@tonic-gate int
main(int argc,char ** argv)2480Sstevel@tonic-gate main(int argc, char **argv) {
2490Sstevel@tonic-gate int errorStatus = 0;
2500Sstevel@tonic-gate int i;
2510Sstevel@tonic-gate int c;
2520Sstevel@tonic-gate int cnt;
2530Sstevel@tonic-gate int file_type;
2540Sstevel@tonic-gate int rem;
2550Sstevel@tonic-gate int outsiz;
2560Sstevel@tonic-gate int tsize;
2570Sstevel@tonic-gate int len;
2580Sstevel@tonic-gate int err;
2590Sstevel@tonic-gate int ifd;
2600Sstevel@tonic-gate int stdinseen;
2610Sstevel@tonic-gate int regular;
2620Sstevel@tonic-gate int swapBytes;
2630Sstevel@tonic-gate int frame;
2640Sstevel@tonic-gate char *outbuf;
2650Sstevel@tonic-gate caddr_t mapaddr;
2660Sstevel@tonic-gate struct stat st;
2670Sstevel@tonic-gate char *cp;
2680Sstevel@tonic-gate char ctldev[MAXPATHLEN];
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2710Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /* Get the program name */
2740Sstevel@tonic-gate prog = strrchr(argv[0], '/');
2750Sstevel@tonic-gate if (prog == NULL)
2760Sstevel@tonic-gate prog = argv[0];
2770Sstevel@tonic-gate else
2780Sstevel@tonic-gate prog++;
2790Sstevel@tonic-gate Stdin = MGET("(stdin)");
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /* Check AUDIODEV environment for audio device name */
2820Sstevel@tonic-gate if (cp = getenv("AUDIODEV")) {
2830Sstevel@tonic-gate Audio_dev = cp;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate /* Parse the command line arguments */
2870Sstevel@tonic-gate err = 0;
2880Sstevel@tonic-gate while ((i = getopt(argc, argv, prog_opts)) != EOF) {
2890Sstevel@tonic-gate switch (i) {
2900Sstevel@tonic-gate case 'v':
2910Sstevel@tonic-gate if (parse_unsigned(optarg, &Volume, "-v")) {
2920Sstevel@tonic-gate err++;
2930Sstevel@tonic-gate } else if (Volume > MAX_GAIN) {
2940Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value "
2950Sstevel@tonic-gate "for -v\n"), prog);
2960Sstevel@tonic-gate err++;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate break;
2990Sstevel@tonic-gate case 'd':
3000Sstevel@tonic-gate Audio_dev = optarg;
3010Sstevel@tonic-gate break;
3020Sstevel@tonic-gate case 'V':
3030Sstevel@tonic-gate Verbose = TRUE;
3040Sstevel@tonic-gate break;
3050Sstevel@tonic-gate case 'E':
3060Sstevel@tonic-gate Errdetect = TRUE;
3070Sstevel@tonic-gate break;
3080Sstevel@tonic-gate case 'i':
3090Sstevel@tonic-gate Immediate = TRUE;
3100Sstevel@tonic-gate break;
3110Sstevel@tonic-gate case '?':
3120Sstevel@tonic-gate usage();
3130Sstevel@tonic-gate /*NOTREACHED*/
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate if (err > 0)
3170Sstevel@tonic-gate exit(1);
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate argc -= optind; /* update arg pointers */
3200Sstevel@tonic-gate argv += optind;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate /* Validate and open the audio device */
3230Sstevel@tonic-gate err = stat(Audio_dev, &st);
3240Sstevel@tonic-gate if (err < 0) {
3250Sstevel@tonic-gate Error(stderr, MGET("%s: cannot stat "), prog);
3260Sstevel@tonic-gate perror(Audio_dev);
3270Sstevel@tonic-gate exit(1);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate if (!S_ISCHR(st.st_mode)) {
3300Sstevel@tonic-gate Error(stderr, MGET("%s: %s is not an audio device\n"), prog,
3310Sstevel@tonic-gate Audio_dev);
3320Sstevel@tonic-gate exit(1);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /* This should probably use audio_cntl instead of open_audio */
3360Sstevel@tonic-gate if ((argc <= 0) && isatty(fileno(stdin))) {
337*9484Sgarrett.damore@Sun.COM Error(stderr, MGET("%s: No files and stdin is a tty.\n"), prog);
338*9484Sgarrett.damore@Sun.COM exit(1);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /* Check on the -i status now. */
3420Sstevel@tonic-gate Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK);
3430Sstevel@tonic-gate if ((Audio_fd < 0) && (errno == EBUSY)) {
3440Sstevel@tonic-gate if (Immediate) {
3450Sstevel@tonic-gate Error(stderr, MGET("%s: %s is busy\n"), prog,
3460Sstevel@tonic-gate Audio_dev);
3470Sstevel@tonic-gate exit(1);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate (void) close(Audio_fd);
3510Sstevel@tonic-gate Audio_fd = -1;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /* Try to open the control device and save the current format */
354*9484Sgarrett.damore@Sun.COM (void) snprintf(ctldev, sizeof (ctldev), "%sctl", Audio_dev);
3550Sstevel@tonic-gate Audio_ctlfd = open(ctldev, O_RDWR);
3560Sstevel@tonic-gate if (Audio_ctlfd >= 0) {
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate * wait for the device to become available then get the
3590Sstevel@tonic-gate * controls. We want to save the format that is left when the
3600Sstevel@tonic-gate * device is in a quiescent state. So wait until then.
3610Sstevel@tonic-gate */
3620Sstevel@tonic-gate Audio_fd = open(Audio_dev, O_WRONLY);
3630Sstevel@tonic-gate (void) close(Audio_fd);
3640Sstevel@tonic-gate Audio_fd = -1;
3650Sstevel@tonic-gate if (audio_get_play_config(Audio_ctlfd, &Save_hdr)
3660Sstevel@tonic-gate != AUDIO_SUCCESS) {
3670Sstevel@tonic-gate (void) close(Audio_ctlfd);
3680Sstevel@tonic-gate Audio_ctlfd = -1;
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate /* store AUDIOPATH so we don't keep doing getenv() */
3730Sstevel@tonic-gate Audio_path = getenv("AUDIOPATH");
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate /* Set up SIGINT handler to flush output */
3760Sstevel@tonic-gate (void) signal(SIGINT, sigint);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /* Set the endian nature of the machine. */
3790Sstevel@tonic-gate if ((ulong_t)1 != htonl((ulong_t)1)) {
3800Sstevel@tonic-gate NetEndian = FALSE;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate /* If no filenames, read stdin */
3840Sstevel@tonic-gate stdinseen = FALSE;
3850Sstevel@tonic-gate if (argc <= 0) {
3860Sstevel@tonic-gate Ifile = Stdin;
3870Sstevel@tonic-gate } else {
3880Sstevel@tonic-gate Ifile = *argv++;
3890Sstevel@tonic-gate argc--;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate /* Loop through all filenames */
3930Sstevel@tonic-gate do {
3940Sstevel@tonic-gate /* Interpret "-" filename to mean stdin */
3950Sstevel@tonic-gate if (strcmp(Ifile, "-") == 0)
3960Sstevel@tonic-gate Ifile = Stdin;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (Ifile == Stdin) {
3990Sstevel@tonic-gate if (stdinseen) {
4000Sstevel@tonic-gate Error(stderr,
4010Sstevel@tonic-gate MGET("%s: stdin already processed\n"),
4020Sstevel@tonic-gate prog);
4030Sstevel@tonic-gate goto nextfile;
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate stdinseen = TRUE;
4060Sstevel@tonic-gate ifd = fileno(stdin);
4070Sstevel@tonic-gate } else {
4080Sstevel@tonic-gate if ((ifd = path_open(Ifile, O_RDONLY, 0, Audio_path))
4090Sstevel@tonic-gate < 0) {
4100Sstevel@tonic-gate Error(stderr, MGET("%s: cannot open "), prog);
4110Sstevel@tonic-gate perror(Ifile);
4120Sstevel@tonic-gate errorStatus++;
4130Sstevel@tonic-gate goto nextfile;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate /* Check to make sure this is an audio file */
4180Sstevel@tonic-gate err = audio_read_filehdr(ifd, &File_hdr, &file_type,
4190Sstevel@tonic-gate (char *)NULL, 0);
4200Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
4210Sstevel@tonic-gate Error(stderr,
4220Sstevel@tonic-gate MGET("%s: %s is not a valid audio file\n"),
4230Sstevel@tonic-gate prog, Ifile);
4240Sstevel@tonic-gate errorStatus++;
4250Sstevel@tonic-gate goto closeinput;
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /* If G.72X adpcm, set flags for conversion */
4290Sstevel@tonic-gate if ((File_hdr.encoding == AUDIO_ENCODING_G721) &&
4300Sstevel@tonic-gate (File_hdr.samples_per_unit == 2) &&
4310Sstevel@tonic-gate (File_hdr.bytes_per_unit == 1)) {
4320Sstevel@tonic-gate Decode = AUDIO_ENCODING_G721;
4330Sstevel@tonic-gate File_hdr.encoding = AUDIO_ENCODING_ULAW;
4340Sstevel@tonic-gate File_hdr.samples_per_unit = 1;
4350Sstevel@tonic-gate File_hdr.bytes_per_unit = 1;
4360Sstevel@tonic-gate adpcm_state = (struct audio_g72x_state *)malloc
437*9484Sgarrett.damore@Sun.COM (sizeof (*adpcm_state) * File_hdr.channels);
4380Sstevel@tonic-gate for (i = 0; i < File_hdr.channels; i++) {
4390Sstevel@tonic-gate g721_init_state(&adpcm_state[i]);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate } else if ((File_hdr.encoding == AUDIO_ENCODING_G723) &&
4420Sstevel@tonic-gate (File_hdr.samples_per_unit == 8) &&
4430Sstevel@tonic-gate (File_hdr.bytes_per_unit == 3)) {
4440Sstevel@tonic-gate Decode = AUDIO_ENCODING_G723;
4450Sstevel@tonic-gate File_hdr.encoding = AUDIO_ENCODING_ULAW;
4460Sstevel@tonic-gate File_hdr.samples_per_unit = 1;
4470Sstevel@tonic-gate File_hdr.bytes_per_unit = 1;
4480Sstevel@tonic-gate adpcm_state = (struct audio_g72x_state *)malloc
449*9484Sgarrett.damore@Sun.COM (sizeof (*adpcm_state) * File_hdr.channels);
4500Sstevel@tonic-gate for (i = 0; i < File_hdr.channels; i++) {
4510Sstevel@tonic-gate g723_init_state(&adpcm_state[i]);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate } else {
4540Sstevel@tonic-gate Decode = AUDIO_ENCODING_NONE;
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /* Check the device configuration */
4580Sstevel@tonic-gate open_audio();
4590Sstevel@tonic-gate if (audio_cmp_hdr(&Dev_hdr, &File_hdr) != 0) {
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate * The device does not match the input file.
4620Sstevel@tonic-gate * Wait for any old output to drain, then attempt
4630Sstevel@tonic-gate * to reconfigure the audio device to match the
4640Sstevel@tonic-gate * input data.
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate if (audio_drain(Audio_fd, FALSE) != AUDIO_SUCCESS) {
4670Sstevel@tonic-gate /* Flush any remaining audio */
4680Sstevel@tonic-gate (void) ioctl(Audio_fd, I_FLUSH, FLUSHW);
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate Error(stderr, MGET("%s: "), prog);
4710Sstevel@tonic-gate perror(MGET("AUDIO_DRAIN error"));
4720Sstevel@tonic-gate exit(1);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate /* Flush any remaining audio */
4760Sstevel@tonic-gate (void) ioctl(Audio_fd, I_FLUSH, FLUSHW);
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate if (!reconfig()) {
4790Sstevel@tonic-gate errorStatus++;
4800Sstevel@tonic-gate goto closeinput;
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /* try to do the mmaping - for regular files only ... */
4860Sstevel@tonic-gate err = fstat(ifd, &st);
4870Sstevel@tonic-gate if (err < 0) {
4880Sstevel@tonic-gate Error(stderr, MGET("%s: cannot stat "), prog);
4890Sstevel@tonic-gate perror(Ifile);
4900Sstevel@tonic-gate exit(1);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate regular = (S_ISREG(st.st_mode));
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate /* If regular file, map it. Else, allocate a buffer */
4960Sstevel@tonic-gate mapaddr = 0;
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate * This should compare to MAP_FAILED not -1, can't
5000Sstevel@tonic-gate * find MAP_FAILED
5010Sstevel@tonic-gate */
5020Sstevel@tonic-gate if (regular && ((mapaddr = mmap(0, st.st_size, PROT_READ,
5030Sstevel@tonic-gate MAP_SHARED, ifd, 0)) != MAP_FAILED)) {
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate (void) madvise(mapaddr, st.st_size, MADV_SEQUENTIAL);
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /* Skip the file header and set the proper size */
5080Sstevel@tonic-gate cnt = lseek(ifd, 0, SEEK_CUR);
5090Sstevel@tonic-gate if (cnt < 0) {
5100Sstevel@tonic-gate perror("lseek");
5110Sstevel@tonic-gate exit(1);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate inbuf = (unsigned char *) mapaddr + cnt;
5140Sstevel@tonic-gate len = cnt = st.st_size - cnt;
5150Sstevel@tonic-gate } else { /* Not a regular file, or map failed */
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate /* mark is so. */
5180Sstevel@tonic-gate mapaddr = 0;
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate /* Allocate buffer to hold 10 seconds of data */
5210Sstevel@tonic-gate cnt = BUFFER_LEN * File_hdr.sample_rate *
5220Sstevel@tonic-gate File_hdr.bytes_per_unit * File_hdr.channels;
5230Sstevel@tonic-gate if (bufsiz != cnt) {
5240Sstevel@tonic-gate if (buf != NULL) {
5250Sstevel@tonic-gate (void) free(buf);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate buf = (unsigned char *) malloc(cnt);
5280Sstevel@tonic-gate if (buf == NULL) {
5290Sstevel@tonic-gate Error(stderr,
5300Sstevel@tonic-gate MGET("%s: couldn't allocate %dK "
5310Sstevel@tonic-gate "buf\n"), prog, bufsiz / 1000);
5320Sstevel@tonic-gate exit(1);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate inbuf = buf;
5350Sstevel@tonic-gate bufsiz = cnt;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate /* Set buffer sizes and pointers for conversion, if any */
5400Sstevel@tonic-gate switch (Decode) {
5410Sstevel@tonic-gate default:
5420Sstevel@tonic-gate case AUDIO_ENCODING_NONE:
5430Sstevel@tonic-gate insiz = bufsiz;
5440Sstevel@tonic-gate outbuf = (char *)buf;
5450Sstevel@tonic-gate break;
5460Sstevel@tonic-gate case AUDIO_ENCODING_G721:
5470Sstevel@tonic-gate insiz = ADPCM_SIZE / 2;
5480Sstevel@tonic-gate outbuf = (char *)adpcm_buf;
5490Sstevel@tonic-gate initmux(1, 2);
5500Sstevel@tonic-gate break;
5510Sstevel@tonic-gate case AUDIO_ENCODING_G723:
5520Sstevel@tonic-gate insiz = (ADPCM_SIZE * 3) / 8;
5530Sstevel@tonic-gate outbuf = (char *)adpcm_buf;
5540Sstevel@tonic-gate initmux(3, 8);
5550Sstevel@tonic-gate break;
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate * 8-bit audio isn't a problem, however 16-bit audio is.
5600Sstevel@tonic-gate * If the file is an endian that is different from the machine
5610Sstevel@tonic-gate * then the bytes will need to be swapped.
5620Sstevel@tonic-gate *
5630Sstevel@tonic-gate * Note: Because the G.72X conversions produce 8bit output,
5640Sstevel@tonic-gate * they don't require a byte swap before display and so
5650Sstevel@tonic-gate * this scheme works just fine. If a conversion is added
5660Sstevel@tonic-gate * that produces a 16 bit result and therefore requires
5670Sstevel@tonic-gate * byte swapping before output, then a mechanism
5680Sstevel@tonic-gate * for chaining the two conversions will have to be built.
5690Sstevel@tonic-gate *
5700Sstevel@tonic-gate * Note: The following if() could be simplified, but then
5710Sstevel@tonic-gate * it gets to be very hard to read. So it's left as is.
5720Sstevel@tonic-gate */
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate if (File_hdr.bytes_per_unit == 2 &&
5750Sstevel@tonic-gate ((!NetEndian && file_type == FILE_AIFF) ||
5760Sstevel@tonic-gate (!NetEndian && file_type == FILE_AU) ||
5770Sstevel@tonic-gate (NetEndian && file_type == FILE_WAV))) {
5780Sstevel@tonic-gate swapBytes = TRUE;
5790Sstevel@tonic-gate } else {
5800Sstevel@tonic-gate swapBytes = FALSE;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate if (swapBytes) {
5840Sstevel@tonic-gate /* Read in interal number of sample frames. */
5850Sstevel@tonic-gate frame = File_hdr.bytes_per_unit * File_hdr.channels;
5860Sstevel@tonic-gate insiz = (SWAP_SIZE / frame) * frame;
5870Sstevel@tonic-gate /* make the output buffer the swap buffer. */
5880Sstevel@tonic-gate outbuf = (char *)swap_buf;
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate * At this point, we're all ready to copy the data.
5930Sstevel@tonic-gate */
5940Sstevel@tonic-gate if (mapaddr == 0) { /* Not mmapped, do it a buffer at a time. */
5950Sstevel@tonic-gate inbuf = buf;
5960Sstevel@tonic-gate frame = File_hdr.bytes_per_unit * File_hdr.channels;
5970Sstevel@tonic-gate rem = 0;
5980Sstevel@tonic-gate while ((cnt = read(ifd, inbuf+rem, insiz-rem)) >= 0) {
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate * We need to ensure only an integral number of
6010Sstevel@tonic-gate * samples is ever written to the audio device.
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate cnt = cnt + rem;
6040Sstevel@tonic-gate rem = cnt % frame;
6050Sstevel@tonic-gate cnt = cnt - rem;
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate * If decoding adpcm, or swapping bytes do it
6090Sstevel@tonic-gate * now.
6100Sstevel@tonic-gate *
6110Sstevel@tonic-gate * We treat the swapping like a separate
6120Sstevel@tonic-gate * encoding here because the G.72X encodings
6130Sstevel@tonic-gate * decode to single byte output samples. If
6140Sstevel@tonic-gate * another encoding is added and it produces
6150Sstevel@tonic-gate * multi-byte output samples this will have to
6160Sstevel@tonic-gate * be changed.
6170Sstevel@tonic-gate */
6180Sstevel@tonic-gate if (Decode == AUDIO_ENCODING_G721) {
6190Sstevel@tonic-gate outsiz = 0;
6200Sstevel@tonic-gate demux(1, cnt / File_hdr.channels);
6210Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
6220Sstevel@tonic-gate err = g721_decode(in_ch_data[c],
6230Sstevel@tonic-gate cnt / File_hdr.channels,
6240Sstevel@tonic-gate &File_hdr,
6250Sstevel@tonic-gate (void*)out_ch_data[c],
6260Sstevel@tonic-gate &tsize,
6270Sstevel@tonic-gate &adpcm_state[c]);
6280Sstevel@tonic-gate outsiz = outsiz + tsize;
6290Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
6300Sstevel@tonic-gate Error(stderr, MGET(
6310Sstevel@tonic-gate "%s: error decoding g721\n"),
6320Sstevel@tonic-gate prog);
6330Sstevel@tonic-gate errorStatus++;
6340Sstevel@tonic-gate break;
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate mux(outbuf);
6380Sstevel@tonic-gate cnt = outsiz;
6390Sstevel@tonic-gate } else if (Decode == AUDIO_ENCODING_G723) {
6400Sstevel@tonic-gate outsiz = 0;
6410Sstevel@tonic-gate demux(3, cnt / File_hdr.channels);
6420Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
6430Sstevel@tonic-gate err = g723_decode(in_ch_data[c],
6440Sstevel@tonic-gate cnt / File_hdr.channels,
6450Sstevel@tonic-gate &File_hdr,
6460Sstevel@tonic-gate (void*)out_ch_data[c],
6470Sstevel@tonic-gate &tsize,
6480Sstevel@tonic-gate &adpcm_state[c]);
6490Sstevel@tonic-gate outsiz = outsiz + tsize;
6500Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
6510Sstevel@tonic-gate Error(stderr, MGET(
6520Sstevel@tonic-gate "%s: error decoding g723\n"),
6530Sstevel@tonic-gate prog);
6540Sstevel@tonic-gate errorStatus++;
6550Sstevel@tonic-gate break;
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate mux(outbuf);
6590Sstevel@tonic-gate cnt = outsiz;
6600Sstevel@tonic-gate } else if (swapBytes) {
6610Sstevel@tonic-gate swab((char *)inbuf, outbuf, cnt);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate /* If input EOF, write an eof marker */
6650Sstevel@tonic-gate err = write(Audio_fd, outbuf, cnt);
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate if (err < 0) {
6680Sstevel@tonic-gate perror("write");
6690Sstevel@tonic-gate errorStatus++;
6700Sstevel@tonic-gate break;
6710Sstevel@tonic-gate } else if (err != cnt) {
6720Sstevel@tonic-gate Error(stderr,
6730Sstevel@tonic-gate MGET("%s: output error: "), prog);
6740Sstevel@tonic-gate perror("");
6750Sstevel@tonic-gate errorStatus++;
6760Sstevel@tonic-gate break;
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate if (cnt == 0) {
6790Sstevel@tonic-gate break;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate /* Move remainder to the front of the buffer */
6820Sstevel@tonic-gate if (rem != 0) {
6830Sstevel@tonic-gate (void *)memcpy(inbuf, inbuf + cnt, rem);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate if (cnt < 0) {
6880Sstevel@tonic-gate Error(stderr, MGET("%s: error reading "), prog);
6890Sstevel@tonic-gate perror(Ifile);
6900Sstevel@tonic-gate errorStatus++;
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate } else { /* We're mmaped */
6930Sstevel@tonic-gate if ((Decode != AUDIO_ENCODING_NONE) || swapBytes) {
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate /* Transform data if we have to. */
6960Sstevel@tonic-gate for (i = 0; i <= len; i += cnt) {
6970Sstevel@tonic-gate cnt = insiz;
6980Sstevel@tonic-gate if ((i + cnt) > len) {
6990Sstevel@tonic-gate cnt = len - i;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate if (Decode == AUDIO_ENCODING_G721) {
7020Sstevel@tonic-gate outsiz = 0;
7030Sstevel@tonic-gate demux(1, cnt / File_hdr.channels);
7040Sstevel@tonic-gate for (c = 0; c < File_hdr.channels;
7050Sstevel@tonic-gate c++) {
7060Sstevel@tonic-gate err = g721_decode(
7070Sstevel@tonic-gate in_ch_data[c],
7080Sstevel@tonic-gate cnt / File_hdr.channels,
7090Sstevel@tonic-gate &File_hdr,
7100Sstevel@tonic-gate (void*)out_ch_data[c],
7110Sstevel@tonic-gate &tsize,
7120Sstevel@tonic-gate &adpcm_state[c]);
7130Sstevel@tonic-gate outsiz = outsiz + tsize;
7140Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
7150Sstevel@tonic-gate Error(stderr, MGET(
7160Sstevel@tonic-gate "%s: error decoding "
7170Sstevel@tonic-gate "g721\n"), prog);
7180Sstevel@tonic-gate errorStatus++;
7190Sstevel@tonic-gate break;
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate mux(outbuf);
7230Sstevel@tonic-gate } else if
7240Sstevel@tonic-gate (Decode == AUDIO_ENCODING_G723) {
7250Sstevel@tonic-gate outsiz = 0;
7260Sstevel@tonic-gate demux(3,
7270Sstevel@tonic-gate cnt / File_hdr.channels);
7280Sstevel@tonic-gate for (c = 0;
7290Sstevel@tonic-gate c < File_hdr.channels;
7300Sstevel@tonic-gate c++) {
7310Sstevel@tonic-gate err = g723_decode(
7320Sstevel@tonic-gate in_ch_data[c],
7330Sstevel@tonic-gate cnt /
7340Sstevel@tonic-gate File_hdr.channels,
7350Sstevel@tonic-gate &File_hdr,
7360Sstevel@tonic-gate (void*)out_ch_data[c],
7370Sstevel@tonic-gate &tsize,
7380Sstevel@tonic-gate &adpcm_state[c]);
7390Sstevel@tonic-gate outsiz = outsiz + tsize;
7400Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
7410Sstevel@tonic-gate Error(stderr, MGET(
7420Sstevel@tonic-gate "%s: error "
7430Sstevel@tonic-gate "decoding g723\n"),
7440Sstevel@tonic-gate prog);
7450Sstevel@tonic-gate errorStatus++;
7460Sstevel@tonic-gate break;
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate mux(outbuf);
7500Sstevel@tonic-gate } else if (swapBytes) {
7510Sstevel@tonic-gate swab((char *)inbuf, outbuf,
7520Sstevel@tonic-gate cnt);
7530Sstevel@tonic-gate outsiz = cnt;
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate inbuf += cnt;
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate /* If input EOF, write an eof marker */
7580Sstevel@tonic-gate err = write(Audio_fd, (char *)outbuf,
7590Sstevel@tonic-gate outsiz);
7600Sstevel@tonic-gate if (err < 0) {
7610Sstevel@tonic-gate perror("write");
7620Sstevel@tonic-gate errorStatus++;
7630Sstevel@tonic-gate } else if (outsiz == 0) {
7640Sstevel@tonic-gate break;
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate } else {
7690Sstevel@tonic-gate /* write the whole thing at once! */
7700Sstevel@tonic-gate err = write(Audio_fd, inbuf, len);
7710Sstevel@tonic-gate if (err < 0) {
7720Sstevel@tonic-gate perror("write");
7730Sstevel@tonic-gate errorStatus++;
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate if (err != len) {
7760Sstevel@tonic-gate Error(stderr,
7770Sstevel@tonic-gate MGET("%s: output error: "), prog);
7780Sstevel@tonic-gate perror("");
7790Sstevel@tonic-gate errorStatus++;
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate err = write(Audio_fd, inbuf, 0);
7820Sstevel@tonic-gate if (err < 0) {
7830Sstevel@tonic-gate perror("write");
7840Sstevel@tonic-gate errorStatus++;
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /* Free memory if decoding ADPCM */
7900Sstevel@tonic-gate switch (Decode) {
7910Sstevel@tonic-gate case AUDIO_ENCODING_G721:
7920Sstevel@tonic-gate case AUDIO_ENCODING_G723:
7930Sstevel@tonic-gate freemux();
7940Sstevel@tonic-gate break;
7950Sstevel@tonic-gate default:
7960Sstevel@tonic-gate break;
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate closeinput:;
8000Sstevel@tonic-gate if (mapaddr != 0)
8010Sstevel@tonic-gate (void) munmap(mapaddr, st.st_size);
8020Sstevel@tonic-gate (void) close(ifd); /* close input file */
8030Sstevel@tonic-gate if (Errdetect) {
8040Sstevel@tonic-gate cnt = 0;
8050Sstevel@tonic-gate audio_set_play_error(Audio_fd, (unsigned int *)&cnt);
8060Sstevel@tonic-gate if (cnt) {
8070Sstevel@tonic-gate Error(stderr,
8080Sstevel@tonic-gate MGET("%s: output underflow in %s\n"),
8090Sstevel@tonic-gate Ifile, prog);
8100Sstevel@tonic-gate errorStatus++;
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate nextfile:;
8140Sstevel@tonic-gate } while ((argc > 0) && (argc--, (Ifile = *argv++) != NULL));
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate * Though drain is implicit on close(), it's performed here
8180Sstevel@tonic-gate * to ensure that the volume is reset after all output is complete.
8190Sstevel@tonic-gate */
8200Sstevel@tonic-gate (void) audio_drain(Audio_fd, FALSE);
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate /* Flush any remaining audio */
8230Sstevel@tonic-gate (void) ioctl(Audio_fd, I_FLUSH, FLUSHW);
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate if (Volume != INT_MAX)
8260Sstevel@tonic-gate (void) audio_set_play_gain(Audio_fd, &Savevol);
8270Sstevel@tonic-gate if ((Audio_ctlfd >= 0) && (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0)) {
8280Sstevel@tonic-gate (void) audio_set_play_config(Audio_fd, &Save_hdr);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate (void) close(Audio_fd); /* close output */
8310Sstevel@tonic-gate return (errorStatus);
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate /*
8360Sstevel@tonic-gate * Try to reconfigure the audio device to match the file encoding.
8370Sstevel@tonic-gate * If this fails, we should attempt to make the input data match the
8380Sstevel@tonic-gate * device encoding. For now, we give up on this file.
8390Sstevel@tonic-gate *
8400Sstevel@tonic-gate * Returns TRUE if successful. Returns FALSE if not.
8410Sstevel@tonic-gate */
8420Sstevel@tonic-gate static int
reconfig(void)8430Sstevel@tonic-gate reconfig(void)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate int err;
8460Sstevel@tonic-gate char msg[AUDIO_MAX_ENCODE_INFO];
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate Dev_hdr = File_hdr;
8490Sstevel@tonic-gate err = audio_set_play_config(Audio_fd, &Dev_hdr);
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate switch (err) {
8520Sstevel@tonic-gate case AUDIO_SUCCESS:
8530Sstevel@tonic-gate return (TRUE);
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate case AUDIO_ERR_NOEFFECT:
8560Sstevel@tonic-gate /*
8570Sstevel@tonic-gate * Couldn't change the device.
8580Sstevel@tonic-gate * Check to see if we're nearly compatible.
8590Sstevel@tonic-gate * audio_cmp_hdr() returns >0 if only sample rate difference.
8600Sstevel@tonic-gate */
8610Sstevel@tonic-gate if (audio_cmp_hdr(&Dev_hdr, &File_hdr) > 0) {
8620Sstevel@tonic-gate double ratio;
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate ratio = (double)abs((int)
8650Sstevel@tonic-gate (Dev_hdr.sample_rate - File_hdr.sample_rate)) /
8660Sstevel@tonic-gate (double)File_hdr.sample_rate;
8670Sstevel@tonic-gate if (ratio <= SAMPLE_RATE_THRESHOLD) {
8680Sstevel@tonic-gate if (Verbose) {
8690Sstevel@tonic-gate Error(stderr,
870*9484Sgarrett.damore@Sun.COM MGET("%s: WARNING: %s sampled at "
871*9484Sgarrett.damore@Sun.COM "%d, playing at %d\n"),
8720Sstevel@tonic-gate prog, Ifile, File_hdr.sample_rate,
8730Sstevel@tonic-gate Dev_hdr.sample_rate);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate return (TRUE);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate Error(stderr,
8780Sstevel@tonic-gate MGET("%s: %s sample rate %d not available\n"),
8790Sstevel@tonic-gate prog, Ifile, File_hdr.sample_rate);
8800Sstevel@tonic-gate return (FALSE);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate (void) audio_enc_to_str(&File_hdr, msg);
8830Sstevel@tonic-gate Error(stderr, MGET("%s: %s encoding not available: %s\n"),
8840Sstevel@tonic-gate prog, Ifile, msg);
8850Sstevel@tonic-gate return (FALSE);
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate default:
8880Sstevel@tonic-gate Error(stderr,
8890Sstevel@tonic-gate MGET("%s: %s audio encoding type not available\n"),
8900Sstevel@tonic-gate prog, Ifile);
8910Sstevel@tonic-gate exit(1);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate return (TRUE);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate /* Parse an unsigned integer */
8980Sstevel@tonic-gate static int
parse_unsigned(char * str,unsigned * dst,char * flag)8990Sstevel@tonic-gate parse_unsigned(char *str, unsigned *dst, char *flag)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate char x;
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate if (sscanf(str, "%u%c", dst, &x) != 1) {
9040Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value for %s\n"), prog, flag);
9050Sstevel@tonic-gate return (1);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate return (0);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate * Search for fname in path and open. Ignore path not opened O_RDONLY.
9120Sstevel@tonic-gate * Note: in general path can be a list of ':' separated paths to search
9130Sstevel@tonic-gate * through.
9140Sstevel@tonic-gate */
9150Sstevel@tonic-gate static int
path_open(char * fname,int flags,mode_t mode,char * path)9160Sstevel@tonic-gate path_open(char *fname, int flags, mode_t mode, char *path)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate char fullpath[MAXPATHLEN]; /* full path of file */
9190Sstevel@tonic-gate char *buf; /* malloc off the tmp buff */
9200Sstevel@tonic-gate char *cp;
9210Sstevel@tonic-gate struct stat st;
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate if (!fname) { /* bogus */
9240Sstevel@tonic-gate return (-1);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate * cases where we don't bother checking path:
9290Sstevel@tonic-gate * - no path
9300Sstevel@tonic-gate * - file not opened O_RDONLY
9310Sstevel@tonic-gate * - not a relative path (i.e. starts with /, ./, or ../).
9320Sstevel@tonic-gate */
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate if ((!path) || (flags != O_RDONLY) || (*fname == '/') ||
9350Sstevel@tonic-gate (strncmp(fname, "./", strlen("./")) == 0) ||
9360Sstevel@tonic-gate (strncmp(fname, "../", strlen("../")) == 0)) {
9370Sstevel@tonic-gate return (open(fname, flags, mode));
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate /*
9410Sstevel@tonic-gate * Malloc off a buffer to hold the path variable.
9420Sstevel@tonic-gate * This is NOT limited to MAXPATHLEN characters as
9430Sstevel@tonic-gate * it may contain multiple paths.
9440Sstevel@tonic-gate */
9450Sstevel@tonic-gate buf = malloc(strlen(path) + 1);
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate * if first character is ':', but not the one following it,
9490Sstevel@tonic-gate * skip over it - or it'll be interpreted as "./". it's OK
9500Sstevel@tonic-gate * to have "::" since that does mean "./".
9510Sstevel@tonic-gate */
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate if ((path[0] == ':') && (path[1] != ':')) {
9540Sstevel@tonic-gate (void) strncpy(buf, path+1, strlen(path));
9550Sstevel@tonic-gate } else {
9560Sstevel@tonic-gate (void) strncpy(buf, path, strlen(path));
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate for (path = buf; path && *path; ) {
9600Sstevel@tonic-gate if (cp = strchr(path, ':')) {
9610Sstevel@tonic-gate *cp++ = NULL; /* now pts to next path element */
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate /* the safest way to create the path string :-) */
9650Sstevel@tonic-gate if (*path) {
9660Sstevel@tonic-gate (void) strncpy(fullpath, path, MAXPATHLEN);
9670Sstevel@tonic-gate (void) strncat(fullpath, "/", MAXPATHLEN);
9680Sstevel@tonic-gate } else {
9690Sstevel@tonic-gate /* a NULL path element means "./" */
9700Sstevel@tonic-gate (void) strncpy(fullpath, "./", MAXPATHLEN);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate (void) strncat(fullpath, fname, MAXPATHLEN);
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate /* see if there's a match */
9750Sstevel@tonic-gate if (stat(fullpath, &st) >= 0) {
9760Sstevel@tonic-gate if (S_ISREG(st.st_mode)) {
9770Sstevel@tonic-gate /* got a match! */
9780Sstevel@tonic-gate if (Verbose) {
9790Sstevel@tonic-gate Error(stderr,
980*9484Sgarrett.damore@Sun.COM MGET("%s: Found %s in path "
981*9484Sgarrett.damore@Sun.COM "at %s\n"),
9820Sstevel@tonic-gate prog, fname, fullpath);
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate return (open(fullpath, flags, mode));
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate /* go on to the next one */
9890Sstevel@tonic-gate path = cp;
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate
9920Sstevel@tonic-gate /*
9930Sstevel@tonic-gate * if we fall through with no match, just do a normal file open
9940Sstevel@tonic-gate */
9950Sstevel@tonic-gate return (open(fname, flags, mode));
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate /*
10000Sstevel@tonic-gate * initmux()
10010Sstevel@tonic-gate *
10020Sstevel@tonic-gate * Description:
10030Sstevel@tonic-gate * Allocates memory for carrying out demultiplexing/multiplexing.
10040Sstevel@tonic-gate *
10050Sstevel@tonic-gate * Arguments:
10060Sstevel@tonic-gate * int unitsz Bytes per unit
10070Sstevel@tonic-gate * int unitsp Samples per unit
10080Sstevel@tonic-gate *
10090Sstevel@tonic-gate * Returns:
10100Sstevel@tonic-gate * void
10110Sstevel@tonic-gate */
10120Sstevel@tonic-gate static void
initmux(int unitsz,int unitsp)10130Sstevel@tonic-gate initmux(int unitsz, int unitsp)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate int c; /* Channel */
10160Sstevel@tonic-gate int in_ch_size; /* Input channel size */
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate /* Size of each input channel */
10190Sstevel@tonic-gate in_ch_size = insiz / File_hdr.channels;
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate /* Size of each output channel */
10220Sstevel@tonic-gate out_ch_size = in_ch_size * unitsp / unitsz;
10230Sstevel@tonic-gate
10240Sstevel@tonic-gate /* Allocate pointers to input channels */
1025*9484Sgarrett.damore@Sun.COM in_ch_data = malloc(sizeof (unsigned char *) * File_hdr.channels);
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate if (in_ch_data == NULL) {
1028*9484Sgarrett.damore@Sun.COM Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1029*9484Sgarrett.damore@Sun.COM prog, sizeof (unsigned char *) * File_hdr.channels / 1000);
10300Sstevel@tonic-gate exit(1);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate /* Allocate input channels */
10340Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
1035*9484Sgarrett.damore@Sun.COM in_ch_data[c] = malloc(sizeof (unsigned char) * in_ch_size);
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate if (in_ch_data[c] == NULL) {
1038*9484Sgarrett.damore@Sun.COM Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1039*9484Sgarrett.damore@Sun.COM prog, in_ch_size / 1000);
10400Sstevel@tonic-gate exit(1);
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate /* Allocate pointers to output channels */
1045*9484Sgarrett.damore@Sun.COM out_ch_data = malloc(sizeof (unsigned char *) * File_hdr.channels);
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate if (out_ch_data == NULL) {
1048*9484Sgarrett.damore@Sun.COM Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1049*9484Sgarrett.damore@Sun.COM prog, sizeof (unsigned char *) * File_hdr.channels / 1000);
10500Sstevel@tonic-gate exit(1);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate /* Allocate output channels */
10540Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
1055*9484Sgarrett.damore@Sun.COM out_ch_data[c] = malloc(sizeof (unsigned char) * out_ch_size);
10560Sstevel@tonic-gate
10570Sstevel@tonic-gate if (out_ch_data[c] == NULL) {
1058*9484Sgarrett.damore@Sun.COM Error(stderr, MGET("%s: couldn't allocate %dK buf\n"),
1059*9484Sgarrett.damore@Sun.COM prog, out_ch_size / 1000);
10600Sstevel@tonic-gate exit(1);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate /*
10660Sstevel@tonic-gate * demux()
10670Sstevel@tonic-gate *
10680Sstevel@tonic-gate * Description:
10690Sstevel@tonic-gate * Split a multichannel signal into separate channels.
10700Sstevel@tonic-gate *
10710Sstevel@tonic-gate * Arguments:
10720Sstevel@tonic-gate * int unitsz Bytes per unit
10730Sstevel@tonic-gate * int cnt Bytes to process
10740Sstevel@tonic-gate *
10750Sstevel@tonic-gate * Returns:
10760Sstevel@tonic-gate * void
10770Sstevel@tonic-gate */
10780Sstevel@tonic-gate static void
demux(int unitsz,int cnt)10790Sstevel@tonic-gate demux(int unitsz, int cnt)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate int c; /* Channel */
10820Sstevel@tonic-gate int s; /* Sample */
10830Sstevel@tonic-gate int b; /* Byte */
10840Sstevel@tonic-gate int tp; /* Pointer into current data */
10850Sstevel@tonic-gate int dp; /* Pointer into target data */
10860Sstevel@tonic-gate
10870Sstevel@tonic-gate /* Split */
10880Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
10890Sstevel@tonic-gate for (s = 0; s < cnt / unitsz; s++) {
10900Sstevel@tonic-gate tp = s * unitsz;
10910Sstevel@tonic-gate dp = (s * File_hdr.channels + c) * unitsz;
10920Sstevel@tonic-gate for (b = 0; b < unitsz; b++) {
10930Sstevel@tonic-gate in_ch_data[c][tp + b] = inbuf[dp + b];
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate /*
11000Sstevel@tonic-gate * mux()
11010Sstevel@tonic-gate *
11020Sstevel@tonic-gate * Description:
11030Sstevel@tonic-gate * Combine separate channels to produce a multichannel signal.
11040Sstevel@tonic-gate *
11050Sstevel@tonic-gate * Arguments:
11060Sstevel@tonic-gate * char *outbuf Combined signal
11070Sstevel@tonic-gate *
11080Sstevel@tonic-gate * Returns:
11090Sstevel@tonic-gate * void
11100Sstevel@tonic-gate */
11110Sstevel@tonic-gate static void
mux(char * outbuf)11120Sstevel@tonic-gate mux(char *outbuf)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate int c; /* Channel */
11150Sstevel@tonic-gate int s; /* Sample */
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate /* Combine */
11180Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
11190Sstevel@tonic-gate for (s = 0; s < out_ch_size; s++) {
11200Sstevel@tonic-gate outbuf[File_hdr.channels * s + c] = out_ch_data[c][s];
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate * freemux()
11270Sstevel@tonic-gate *
11280Sstevel@tonic-gate * Description:
11290Sstevel@tonic-gate * Free memory used in multiplexing/demultiplexing.
11300Sstevel@tonic-gate *
11310Sstevel@tonic-gate * Arguments:
11320Sstevel@tonic-gate * void
11330Sstevel@tonic-gate *
11340Sstevel@tonic-gate * Returns:
11350Sstevel@tonic-gate * void
11360Sstevel@tonic-gate */
11370Sstevel@tonic-gate static void
freemux(void)11380Sstevel@tonic-gate freemux(void)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate int c; /* Channel */
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate /* Free */
11430Sstevel@tonic-gate for (c = 0; c < File_hdr.channels; c++) {
11440Sstevel@tonic-gate free(in_ch_data[c]);
11450Sstevel@tonic-gate free(out_ch_data[c]);
11460Sstevel@tonic-gate free(&adpcm_state[c]);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate free(in_ch_data);
11500Sstevel@tonic-gate free(out_ch_data);
11510Sstevel@tonic-gate }
1152