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