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 record utility */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <libgen.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <math.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <strings.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <fcntl.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <limits.h> /* All occurances of INT_MAX used to be ~0 (by MCA) */
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <stropts.h>
507c478bd9Sstevel@tonic-gate #include <poll.h>
517c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
527c478bd9Sstevel@tonic-gate #include <netinet/in.h>
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #include <libaudio.h>
557c478bd9Sstevel@tonic-gate #include <audio_device.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #define irint(d) ((int)d)
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /* localization stuff */
607c478bd9Sstevel@tonic-gate #define MGET(s) (char *)gettext(s)
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
637c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #define Error (void) fprintf
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate /* Local variables */
697c478bd9Sstevel@tonic-gate static char *prog;
7088447a05SGarrett D'Amore static char prog_opts[] = "aft:v:d:i:e:s:c:T:?"; /* getopt() flags */
717c478bd9Sstevel@tonic-gate static char *Stdout;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /* XXX - the input buffer size should depend on sample_rate */
747c478bd9Sstevel@tonic-gate #define AUDIO_BUFSIZ (1024 * 64)
757c478bd9Sstevel@tonic-gate static unsigned char buf[AUDIO_BUFSIZ];
76*64731133SToomas Soome static char swapBuf[AUDIO_BUFSIZ]; /* for byte swapping */
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #define MAX_GAIN (100) /* maximum gain */
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static char *Info = NULL; /* pointer to info data */
827c478bd9Sstevel@tonic-gate static unsigned Ilen = 0; /* length of info data */
837c478bd9Sstevel@tonic-gate static unsigned Volume = INT_MAX; /* record volume */
847c478bd9Sstevel@tonic-gate static double Savevol; /* saved volume */
857c478bd9Sstevel@tonic-gate static unsigned Sample_rate = 0;
867c478bd9Sstevel@tonic-gate static unsigned Channels = 0;
877c478bd9Sstevel@tonic-gate static unsigned Precision = 0; /* based on encoding */
887c478bd9Sstevel@tonic-gate static unsigned Encoding = 0;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate static int NetEndian = TRUE; /* endian nature of the machines */
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate static int Append = FALSE; /* append to output file */
937c478bd9Sstevel@tonic-gate static int Force = FALSE; /* ignore rate differences on append */
947c478bd9Sstevel@tonic-gate static double Time = -1.; /* recording time */
957c478bd9Sstevel@tonic-gate static unsigned Limit = AUDIO_UNKNOWN_SIZE; /* recording limit */
967c478bd9Sstevel@tonic-gate static char *Audio_dev = "/dev/audio";
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate static int Audio_fd = -1;
997c478bd9Sstevel@tonic-gate /* file descriptor for audio device */
1007c478bd9Sstevel@tonic-gate static Audio_hdr Dev_hdr; /* audio header for device */
1017c478bd9Sstevel@tonic-gate static Audio_hdr Save_hdr; /* saved audio device header */
1027c478bd9Sstevel@tonic-gate static char *Ofile; /* current filename */
1037c478bd9Sstevel@tonic-gate static int File_type = FILE_AU; /* audio file type */
1047c478bd9Sstevel@tonic-gate static int File_type_set = FALSE; /* file type specified as arg */
1057c478bd9Sstevel@tonic-gate static Audio_hdr File_hdr; /* audio header for file */
1067c478bd9Sstevel@tonic-gate static int Cleanup = FALSE; /* SIGINT sets this flag */
1077c478bd9Sstevel@tonic-gate static unsigned Size = 0; /* Size of output file */
1087c478bd9Sstevel@tonic-gate static unsigned Oldsize = 0;
1097c478bd9Sstevel@tonic-gate /* Size of input file, if append */
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /* Global variables */
1127c478bd9Sstevel@tonic-gate extern int getopt();
1137c478bd9Sstevel@tonic-gate extern int optind;
1147c478bd9Sstevel@tonic-gate extern char *optarg;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* Local Functions */
1177c478bd9Sstevel@tonic-gate static void usage(void);
1187c478bd9Sstevel@tonic-gate static void sigint(int sig);
1197c478bd9Sstevel@tonic-gate static int parse_unsigned(char *str, unsigned *dst, char *flag);
1207c478bd9Sstevel@tonic-gate static int parse_sample_rate(char *s, unsigned *rate);
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate static void
usage(void)1247c478bd9Sstevel@tonic-gate usage(void)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate Error(stderr, MGET("Record an audio file -- usage:\n"
12788447a05SGarrett D'Amore "\t%s [-af] [-v vol]\n"
1287c478bd9Sstevel@tonic-gate "\t%.*s [-c channels] [-s rate] [-e encoding]\n"
1297c478bd9Sstevel@tonic-gate "\t%.*s [-t time] [-i info] [-d dev] [-T au|wav|aif[f]] [file]\n"
1307c478bd9Sstevel@tonic-gate "where:\n"
1317c478bd9Sstevel@tonic-gate "\t-a\tAppend to output file\n"
1327c478bd9Sstevel@tonic-gate "\t-f\tIgnore sample rate differences on append\n"
1337c478bd9Sstevel@tonic-gate "\t-v\tSet record volume (0 - %d)\n"
1347c478bd9Sstevel@tonic-gate "\t-c\tSpecify number of channels to record\n"
1357c478bd9Sstevel@tonic-gate "\t-s\tSpecify rate in samples per second\n"
1367c478bd9Sstevel@tonic-gate "\t-e\tSpecify encoding (ulaw | alaw | [u]linear | linear8 )\n"
1377c478bd9Sstevel@tonic-gate "\t-t\tSpecify record time (hh:mm:ss.dd)\n"
1387c478bd9Sstevel@tonic-gate "\t-i\tSpecify a file header information string\n"
1397c478bd9Sstevel@tonic-gate "\t-d\tSpecify audio device (default: /dev/audio)\n"
1407c478bd9Sstevel@tonic-gate "\t-T\tSpecify the audio file type (default: au)\n"
1417c478bd9Sstevel@tonic-gate "\tfile\tRecord to named file\n"
1427c478bd9Sstevel@tonic-gate "\t\tIf no file specified, write to stdout\n"
1437c478bd9Sstevel@tonic-gate "\t\tDefault audio encoding is ulaw, 8khz, mono\n"
1447c478bd9Sstevel@tonic-gate "\t\tIf -t is not specified, record until ^C\n"),
1457c478bd9Sstevel@tonic-gate prog,
1467c478bd9Sstevel@tonic-gate strlen(prog), " ",
1477c478bd9Sstevel@tonic-gate strlen(prog), " ",
14888447a05SGarrett D'Amore MAX_GAIN);
1497c478bd9Sstevel@tonic-gate exit(1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate static void
sigint(int sig)1537c478bd9Sstevel@tonic-gate sigint(int sig)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate /* If this is the first ^C, set a flag for the main loop */
1567c478bd9Sstevel@tonic-gate if (!Cleanup && (Audio_fd >= 0)) {
1577c478bd9Sstevel@tonic-gate /* flush input queues before exiting */
1587c478bd9Sstevel@tonic-gate Cleanup = TRUE;
1597c478bd9Sstevel@tonic-gate if (audio_pause_record(Audio_fd) == AUDIO_SUCCESS)
1607c478bd9Sstevel@tonic-gate return;
1617c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: could not flush input buffer\n"), prog);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /* If double ^C, really quit */
1657c478bd9Sstevel@tonic-gate if (Audio_fd >= 0) {
1667c478bd9Sstevel@tonic-gate if (Volume != INT_MAX)
1677c478bd9Sstevel@tonic-gate (void) audio_set_record_gain(Audio_fd, &Savevol);
1687c478bd9Sstevel@tonic-gate if (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0) {
1697c478bd9Sstevel@tonic-gate (void) audio_set_record_config(Audio_fd, &Save_hdr);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate exit(1);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * Record from the audio device to a file.
1777c478bd9Sstevel@tonic-gate */
1787c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1797c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate int i;
1827c478bd9Sstevel@tonic-gate int cnt;
1837c478bd9Sstevel@tonic-gate int err;
1847c478bd9Sstevel@tonic-gate int file_type;
1857c478bd9Sstevel@tonic-gate int ofd;
186*64731133SToomas Soome int swapBytes = FALSE;
1877c478bd9Sstevel@tonic-gate double vol;
1887c478bd9Sstevel@tonic-gate struct stat st;
1897c478bd9Sstevel@tonic-gate struct pollfd pfd;
1907c478bd9Sstevel@tonic-gate char *cp;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1937c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate /* Get the program name */
1967c478bd9Sstevel@tonic-gate prog = strrchr(argv[0], '/');
1977c478bd9Sstevel@tonic-gate if (prog == NULL)
1987c478bd9Sstevel@tonic-gate prog = argv[0];
1997c478bd9Sstevel@tonic-gate else
2007c478bd9Sstevel@tonic-gate prog++;
2017c478bd9Sstevel@tonic-gate Stdout = MGET("(stdout)");
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* first check AUDIODEV environment for audio device name */
2047c478bd9Sstevel@tonic-gate if (cp = getenv("AUDIODEV")) {
2057c478bd9Sstevel@tonic-gate Audio_dev = cp;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /* Set the endian nature of the machine */
2097c478bd9Sstevel@tonic-gate if ((ulong_t)1 != htonl((ulong_t)1)) {
2107c478bd9Sstevel@tonic-gate NetEndian = FALSE;
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate err = 0;
2147c478bd9Sstevel@tonic-gate while ((i = getopt(argc, argv, prog_opts)) != EOF) {
2157c478bd9Sstevel@tonic-gate switch (i) {
2167c478bd9Sstevel@tonic-gate case 'v':
2177c478bd9Sstevel@tonic-gate if (parse_unsigned(optarg, &Volume, "-v")) {
2187c478bd9Sstevel@tonic-gate err++;
2197c478bd9Sstevel@tonic-gate } else if (Volume > MAX_GAIN) {
2207c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value for "
2217c478bd9Sstevel@tonic-gate "-v\n"), prog);
2227c478bd9Sstevel@tonic-gate err++;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate case 't':
2267c478bd9Sstevel@tonic-gate Time = audio_str_to_secs(optarg);
2277c478bd9Sstevel@tonic-gate if ((Time == HUGE_VAL) || (Time < 0.)) {
2287c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value for "
2297c478bd9Sstevel@tonic-gate "-t\n"), prog);
2307c478bd9Sstevel@tonic-gate err++;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate break;
2337c478bd9Sstevel@tonic-gate case 'd':
2347c478bd9Sstevel@tonic-gate Audio_dev = optarg;
2357c478bd9Sstevel@tonic-gate break;
2367c478bd9Sstevel@tonic-gate case 'f':
2377c478bd9Sstevel@tonic-gate Force = TRUE;
2387c478bd9Sstevel@tonic-gate break;
2397c478bd9Sstevel@tonic-gate case 'a':
2407c478bd9Sstevel@tonic-gate Append = TRUE;
2417c478bd9Sstevel@tonic-gate break;
2427c478bd9Sstevel@tonic-gate case 'i':
2437c478bd9Sstevel@tonic-gate Info = optarg; /* set information string */
2447c478bd9Sstevel@tonic-gate Ilen = strlen(Info);
2457c478bd9Sstevel@tonic-gate break;
2467c478bd9Sstevel@tonic-gate case 's':
2477c478bd9Sstevel@tonic-gate if (parse_sample_rate(optarg, &Sample_rate)) {
2487c478bd9Sstevel@tonic-gate err++;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate break;
2517c478bd9Sstevel@tonic-gate case 'c':
2527c478bd9Sstevel@tonic-gate if (strncmp(optarg, "mono", strlen(optarg)) == 0) {
2537c478bd9Sstevel@tonic-gate Channels = 1;
2547c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "stereo",
2557c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) {
2567c478bd9Sstevel@tonic-gate Channels = 2;
2577c478bd9Sstevel@tonic-gate } else if (parse_unsigned(optarg, &Channels, "-c")) {
2587c478bd9Sstevel@tonic-gate err++;
2597c478bd9Sstevel@tonic-gate } else if ((Channels != 1) && (Channels != 2)) {
2607c478bd9Sstevel@tonic-gate Error(stderr, "%s: invalid value for -c\n",
2617c478bd9Sstevel@tonic-gate prog);
2627c478bd9Sstevel@tonic-gate err++;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate break;
2657c478bd9Sstevel@tonic-gate case 'e':
2667c478bd9Sstevel@tonic-gate if (strncmp(optarg, "ulinear", strlen(optarg)) == 0) {
2677c478bd9Sstevel@tonic-gate Encoding = AUDIO_ENCODING_LINEAR8;
2687c478bd9Sstevel@tonic-gate Precision = 8;
2697c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "linear8",
2707c478bd9Sstevel@tonic-gate strlen("linear8")) == 0) {
2717c478bd9Sstevel@tonic-gate Encoding = AUDIO_ENCODING_LINEAR;
2727c478bd9Sstevel@tonic-gate Precision = 8;
2737c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "ulaw",
2747c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) {
2757c478bd9Sstevel@tonic-gate Encoding = AUDIO_ENCODING_ULAW;
2767c478bd9Sstevel@tonic-gate Precision = 8;
2777c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "alaw",
2787c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) {
2797c478bd9Sstevel@tonic-gate Encoding = AUDIO_ENCODING_ALAW;
2807c478bd9Sstevel@tonic-gate Precision = 8;
2817c478bd9Sstevel@tonic-gate } else if ((strncmp(optarg, "linear",
2827c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) || (strncmp(optarg, "pcm",
2837c478bd9Sstevel@tonic-gate strlen(optarg)) == 0)) {
2847c478bd9Sstevel@tonic-gate Encoding = AUDIO_ENCODING_LINEAR;
2857c478bd9Sstevel@tonic-gate Precision = 16;
2867c478bd9Sstevel@tonic-gate } else {
2877c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value for "
2887c478bd9Sstevel@tonic-gate "-e\n"), prog);
2897c478bd9Sstevel@tonic-gate err++;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate break;
2927c478bd9Sstevel@tonic-gate case 'T':
2937c478bd9Sstevel@tonic-gate if (strncmp(optarg, "au", strlen(optarg)) == 0) {
2947c478bd9Sstevel@tonic-gate File_type = FILE_AU;
2957c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "wav",
2967c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) {
2977c478bd9Sstevel@tonic-gate File_type = FILE_WAV;
2987c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "aif",
2997c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) {
3007c478bd9Sstevel@tonic-gate File_type = FILE_AIFF;
3017c478bd9Sstevel@tonic-gate } else if (strncmp(optarg, "aiff",
3027c478bd9Sstevel@tonic-gate strlen(optarg)) == 0) {
3037c478bd9Sstevel@tonic-gate File_type = FILE_AIFF;
3047c478bd9Sstevel@tonic-gate } else {
3057c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value for "
3067c478bd9Sstevel@tonic-gate "-T\n"), prog);
3077c478bd9Sstevel@tonic-gate err++;
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate File_type_set = TRUE;
3107c478bd9Sstevel@tonic-gate break;
3117c478bd9Sstevel@tonic-gate case '?':
3127c478bd9Sstevel@tonic-gate usage();
3137c478bd9Sstevel@tonic-gate /*NOTREACHED*/
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate if (Append && (Info != NULL)) {
3177c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: cannot specify -a and -i\n"), prog);
3187c478bd9Sstevel@tonic-gate err++;
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate if (err > 0)
3217c478bd9Sstevel@tonic-gate exit(1);
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate argc -= optind; /* update arg pointers */
3247c478bd9Sstevel@tonic-gate argv += optind;
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate /* Open the output file */
3277c478bd9Sstevel@tonic-gate if (argc <= 0) {
3287c478bd9Sstevel@tonic-gate Ofile = Stdout;
3297c478bd9Sstevel@tonic-gate } else {
3307c478bd9Sstevel@tonic-gate Ofile = *argv++;
3317c478bd9Sstevel@tonic-gate argc--;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /* Interpret "-" filename to mean stdout */
3347c478bd9Sstevel@tonic-gate if (strcmp(Ofile, "-") == 0)
3357c478bd9Sstevel@tonic-gate Ofile = Stdout;
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate /* if -T not set then we use the file suffix */
3387c478bd9Sstevel@tonic-gate if (File_type_set == FALSE) {
3397c478bd9Sstevel@tonic-gate char *file_name;
3407c478bd9Sstevel@tonic-gate char *start;
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /* get the file name without the path */
3437c478bd9Sstevel@tonic-gate file_name = basename(Ofile);
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate /* get the true suffix */
3467c478bd9Sstevel@tonic-gate start = strrchr(file_name, '.');
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate /* if no '.' then there's no suffix */
3497c478bd9Sstevel@tonic-gate if (start) {
3507c478bd9Sstevel@tonic-gate /* is this a .au file? */
3517c478bd9Sstevel@tonic-gate if (strcasecmp(start, ".au") == 0) {
3527c478bd9Sstevel@tonic-gate File_type = FILE_AU;
3537c478bd9Sstevel@tonic-gate } else if (strcasecmp(start, ".wav") == 0) {
3547c478bd9Sstevel@tonic-gate File_type = FILE_WAV;
3557c478bd9Sstevel@tonic-gate } else if (strcasecmp(start, ".aif") == 0) {
3567c478bd9Sstevel@tonic-gate File_type = FILE_AIFF;
3577c478bd9Sstevel@tonic-gate } else if (strcasecmp(start, ".aiff") == 0) {
3587c478bd9Sstevel@tonic-gate File_type = FILE_AIFF;
3597c478bd9Sstevel@tonic-gate } else {
3607c478bd9Sstevel@tonic-gate /* the default is .au */
3617c478bd9Sstevel@tonic-gate File_type = FILE_AU;
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate } else {
3647c478bd9Sstevel@tonic-gate /* no suffix, so default to .au */
3657c478bd9Sstevel@tonic-gate File_type = FILE_AU;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate if (Ofile == Stdout) {
3717c478bd9Sstevel@tonic-gate ofd = fileno(stdout);
3727c478bd9Sstevel@tonic-gate Append = FALSE;
3737c478bd9Sstevel@tonic-gate } else {
3747c478bd9Sstevel@tonic-gate ofd = open(Ofile,
3757c478bd9Sstevel@tonic-gate (O_RDWR | O_CREAT | (Append ? 0 : O_TRUNC)), 0666);
3767c478bd9Sstevel@tonic-gate if (ofd < 0) {
3777c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: cannot open "), prog);
3787c478bd9Sstevel@tonic-gate perror(Ofile);
3797c478bd9Sstevel@tonic-gate exit(1);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate if (Append) {
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * Check to make sure we're appending to an audio file.
3847c478bd9Sstevel@tonic-gate * It must be a regular file (if zero-length, simply
3857c478bd9Sstevel@tonic-gate * write it from scratch). Also, its file header
3867c478bd9Sstevel@tonic-gate * must match the input device configuration.
3877c478bd9Sstevel@tonic-gate */
3887c478bd9Sstevel@tonic-gate if ((fstat(ofd, &st) < 0) || (!S_ISREG(st.st_mode))) {
3897c478bd9Sstevel@tonic-gate Error(stderr,
3907c478bd9Sstevel@tonic-gate MGET("%s: %s is not a regular file\n"),
3917c478bd9Sstevel@tonic-gate prog, Ofile);
3927c478bd9Sstevel@tonic-gate exit(1);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate if (st.st_size == 0) {
3957c478bd9Sstevel@tonic-gate Append = FALSE;
3967c478bd9Sstevel@tonic-gate goto openinput;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate err = audio_read_filehdr(ofd, &File_hdr, &file_type,
4007c478bd9Sstevel@tonic-gate (char *)NULL, 0);
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
4037c478bd9Sstevel@tonic-gate Error(stderr,
4047c478bd9Sstevel@tonic-gate MGET("%s: %s is not a valid audio file\n"),
4057c478bd9Sstevel@tonic-gate prog, Ofile);
4067c478bd9Sstevel@tonic-gate exit(1);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate /* we need to make sure file types match */
4107c478bd9Sstevel@tonic-gate if (File_type_set == TRUE) {
4117c478bd9Sstevel@tonic-gate /* specified by the command line, must match */
4127c478bd9Sstevel@tonic-gate if (File_type != file_type) {
4137c478bd9Sstevel@tonic-gate Error(stderr,
4147c478bd9Sstevel@tonic-gate MGET("%s: file types must match\n"),
4157c478bd9Sstevel@tonic-gate prog);
4167c478bd9Sstevel@tonic-gate exit(1);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate } else {
4197c478bd9Sstevel@tonic-gate /* not specified, so force */
4207c478bd9Sstevel@tonic-gate File_type = file_type;
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate * Set the format state to the format
4257c478bd9Sstevel@tonic-gate * in the file header.
4267c478bd9Sstevel@tonic-gate */
4277c478bd9Sstevel@tonic-gate Sample_rate = File_hdr.sample_rate;
4287c478bd9Sstevel@tonic-gate Channels = File_hdr.channels;
4297c478bd9Sstevel@tonic-gate Encoding = File_hdr.encoding;
4307c478bd9Sstevel@tonic-gate Precision = File_hdr.bytes_per_unit * 8;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /* make sure we support the encoding method */
4337c478bd9Sstevel@tonic-gate switch (Encoding) {
4347c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR8:
4357c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ULAW:
4367c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ALAW:
4377c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR:
4387c478bd9Sstevel@tonic-gate break;
4397c478bd9Sstevel@tonic-gate default: {
4407c478bd9Sstevel@tonic-gate char msg[AUDIO_MAX_ENCODE_INFO];
4417c478bd9Sstevel@tonic-gate (void) audio_enc_to_str(&File_hdr, msg);
4427c478bd9Sstevel@tonic-gate Error(stderr,
4437c478bd9Sstevel@tonic-gate MGET("%s: Append is not supported "
4447c478bd9Sstevel@tonic-gate "for "), prog);
4457c478bd9Sstevel@tonic-gate Error(stderr,
4467c478bd9Sstevel@tonic-gate MGET("this file encoding:\n\t"
4477c478bd9Sstevel@tonic-gate "[%s]\n"), msg);
4487c478bd9Sstevel@tonic-gate exit(1);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate /* Get the current size, if possible */
4537c478bd9Sstevel@tonic-gate Oldsize = File_hdr.data_size;
4547c478bd9Sstevel@tonic-gate if ((Oldsize == AUDIO_UNKNOWN_SIZE) &&
4557c478bd9Sstevel@tonic-gate ((err = (int)lseek(ofd, 0L, SEEK_CUR)) >= 0)) {
4567c478bd9Sstevel@tonic-gate if (err < 0) {
4577c478bd9Sstevel@tonic-gate Error(stderr,
4587c478bd9Sstevel@tonic-gate MGET("%s: %s is not a valid audio "
4597c478bd9Sstevel@tonic-gate "file\n"), prog, Ofile);
4607c478bd9Sstevel@tonic-gate exit(1);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate Oldsize = st.st_size - err;
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate /* Seek to end to start append */
4657c478bd9Sstevel@tonic-gate if ((int)lseek(ofd, st.st_size, SEEK_SET) < 0) {
4667c478bd9Sstevel@tonic-gate Error(stderr,
4677c478bd9Sstevel@tonic-gate MGET("%s: cannot find end of %s\n"),
4687c478bd9Sstevel@tonic-gate prog, Ofile);
4697c478bd9Sstevel@tonic-gate exit(1);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate openinput:
4747c478bd9Sstevel@tonic-gate /* Validate and open the audio device */
4757c478bd9Sstevel@tonic-gate err = stat(Audio_dev, &st);
4767c478bd9Sstevel@tonic-gate if (err < 0) {
4777c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: cannot open "), prog);
4787c478bd9Sstevel@tonic-gate perror(Audio_dev);
4797c478bd9Sstevel@tonic-gate exit(1);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate if (!S_ISCHR(st.st_mode)) {
4827c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: %s is not an audio device\n"), prog,
4837c478bd9Sstevel@tonic-gate Audio_dev);
4847c478bd9Sstevel@tonic-gate exit(1);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate * For the mixer environment we need to open the audio device before
4897c478bd9Sstevel@tonic-gate * the control device. If successful we pause right away to keep
4907c478bd9Sstevel@tonic-gate * from queueing up a bunch of useless data.
4917c478bd9Sstevel@tonic-gate */
4927c478bd9Sstevel@tonic-gate Audio_fd = open(Audio_dev, O_RDONLY | O_NONBLOCK);
4937c478bd9Sstevel@tonic-gate if (Audio_fd < 0) {
4947c478bd9Sstevel@tonic-gate if (errno == EBUSY) {
4957c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: %s is busy\n"),
4967c478bd9Sstevel@tonic-gate prog, Audio_dev);
4977c478bd9Sstevel@tonic-gate } else {
4987c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: error opening "), prog);
4997c478bd9Sstevel@tonic-gate perror(Audio_dev);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate exit(1);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate if (audio_pause_record(Audio_fd) != AUDIO_SUCCESS) {
5047c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: not able to pause recording\n"), prog);
5057c478bd9Sstevel@tonic-gate exit(1);
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate /* get the current settings */
5097c478bd9Sstevel@tonic-gate if (audio_get_record_config(Audio_fd, &Save_hdr) != AUDIO_SUCCESS) {
5107c478bd9Sstevel@tonic-gate (void) close(Audio_fd);
5117c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: %s is not an audio device\n"),
5127c478bd9Sstevel@tonic-gate prog, Audio_dev);
5137c478bd9Sstevel@tonic-gate exit(1);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate /* make a copy into the working data structure */
5167c478bd9Sstevel@tonic-gate bcopy(&Save_hdr, &Dev_hdr, sizeof (Save_hdr));
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate /* flush any queued audio data */
5197c478bd9Sstevel@tonic-gate if (audio_flush_record(Audio_fd) != AUDIO_SUCCESS) {
5207c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: not able to flush recording\n"), prog);
5217c478bd9Sstevel@tonic-gate exit(1);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate if (Sample_rate != 0) {
5257c478bd9Sstevel@tonic-gate Dev_hdr.sample_rate = Sample_rate;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate if (Channels != 0) {
5287c478bd9Sstevel@tonic-gate Dev_hdr.channels = Channels;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate if (Precision != 0) {
5317c478bd9Sstevel@tonic-gate Dev_hdr.bytes_per_unit = Precision / 8;
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate if (Encoding != 0) {
5347c478bd9Sstevel@tonic-gate Dev_hdr.encoding = Encoding;
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate * For .wav we always record 8-bit linear as unsigned. Thus we
5397c478bd9Sstevel@tonic-gate * force unsigned linear to make life a lot easier on the user.
5407c478bd9Sstevel@tonic-gate *
5417c478bd9Sstevel@tonic-gate * For .aiff we set the default to 8-bit signed linear, not
5427c478bd9Sstevel@tonic-gate * u-law, if Encoding isn't already set.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate if (File_type == FILE_WAV &&
5457c478bd9Sstevel@tonic-gate Dev_hdr.encoding == AUDIO_ENCODING_LINEAR &&
5467c478bd9Sstevel@tonic-gate Dev_hdr.bytes_per_unit == 1) {
5477c478bd9Sstevel@tonic-gate /* force to unsigned */
5487c478bd9Sstevel@tonic-gate Dev_hdr.encoding = AUDIO_ENCODING_LINEAR8;
5497c478bd9Sstevel@tonic-gate } else if (File_type == FILE_AIFF && Encoding == 0) {
5507c478bd9Sstevel@tonic-gate Dev_hdr.encoding = AUDIO_ENCODING_LINEAR;
5517c478bd9Sstevel@tonic-gate if (Precision == 0) {
5527c478bd9Sstevel@tonic-gate Dev_hdr.bytes_per_unit = AUDIO_PRECISION_8 / 8;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate if (audio_set_record_config(Audio_fd, &Dev_hdr) != AUDIO_SUCCESS) {
5577c478bd9Sstevel@tonic-gate Error(stderr, MGET(
5587c478bd9Sstevel@tonic-gate "%s: Audio format not supported by the audio device\n"),
5597c478bd9Sstevel@tonic-gate prog);
5607c478bd9Sstevel@tonic-gate exit(1);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate if (audio_resume_record(Audio_fd) != AUDIO_SUCCESS) {
5647c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: not able to resume recording\n"), prog);
5657c478bd9Sstevel@tonic-gate exit(1);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate /* If appending to an existing file, check the configuration */
5697c478bd9Sstevel@tonic-gate if (Append) {
5707c478bd9Sstevel@tonic-gate char msg[AUDIO_MAX_ENCODE_INFO];
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate switch (audio_cmp_hdr(&Dev_hdr, &File_hdr)) {
5737c478bd9Sstevel@tonic-gate case 0: /* configuration matches */
5747c478bd9Sstevel@tonic-gate break;
5757c478bd9Sstevel@tonic-gate case 1: /* all but sample rate matches */
5767c478bd9Sstevel@tonic-gate if (Force) {
5777c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: WARNING: appending "
5787c478bd9Sstevel@tonic-gate "%.3fkHz data to %s (%.3fkHz)\n"), prog,
5797c478bd9Sstevel@tonic-gate ((double)Dev_hdr.sample_rate / 1000.),
5807c478bd9Sstevel@tonic-gate Ofile,
5817c478bd9Sstevel@tonic-gate ((double)File_hdr.sample_rate / 1000.));
5827c478bd9Sstevel@tonic-gate break;
5837c478bd9Sstevel@tonic-gate } /* if not -f, fall through */
5843e1e9f9bSToomas Soome /* FALLTHROUGH */
5857c478bd9Sstevel@tonic-gate default: /* encoding mismatch */
5867c478bd9Sstevel@tonic-gate (void) audio_enc_to_str(&Dev_hdr, msg);
5877c478bd9Sstevel@tonic-gate Error(stderr,
5887c478bd9Sstevel@tonic-gate MGET("%s: device encoding [%s]\n"), prog, msg);
5897c478bd9Sstevel@tonic-gate (void) audio_enc_to_str(&File_hdr, msg);
5907c478bd9Sstevel@tonic-gate Error(stderr,
5917c478bd9Sstevel@tonic-gate MGET("\tdoes not match file encoding [%s]\n"), msg);
5927c478bd9Sstevel@tonic-gate exit(1);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate } else if (!isatty(ofd)) {
5957c478bd9Sstevel@tonic-gate if (audio_write_filehdr(ofd, &Dev_hdr, File_type, Info,
5967c478bd9Sstevel@tonic-gate Ilen) != AUDIO_SUCCESS) {
5977c478bd9Sstevel@tonic-gate Error(stderr,
5987c478bd9Sstevel@tonic-gate MGET("%s: error writing header for %s\n"), prog,
5997c478bd9Sstevel@tonic-gate Ofile);
6007c478bd9Sstevel@tonic-gate exit(1);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate * 8-bit audio isn't a problem, however 16-bit audio is. If the file
6067c478bd9Sstevel@tonic-gate * is an endian that is different from the machine then the bytes
6077c478bd9Sstevel@tonic-gate * will need to be swapped.
6087c478bd9Sstevel@tonic-gate *
6097c478bd9Sstevel@tonic-gate * Note: The following if() could be simplified, but then it gets
6107c478bd9Sstevel@tonic-gate * to be very hard to read. So it's left as is.
6117c478bd9Sstevel@tonic-gate */
6127c478bd9Sstevel@tonic-gate if (Dev_hdr.bytes_per_unit == 2 &&
6137c478bd9Sstevel@tonic-gate ((!NetEndian && File_type == FILE_AIFF) ||
6147c478bd9Sstevel@tonic-gate (!NetEndian && File_type == FILE_AU) ||
6157c478bd9Sstevel@tonic-gate (NetEndian && File_type == FILE_WAV))) {
6167c478bd9Sstevel@tonic-gate swapBytes = TRUE;
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate /* If -v flag, set the record volume now */
6207c478bd9Sstevel@tonic-gate if (Volume != INT_MAX) {
6217c478bd9Sstevel@tonic-gate vol = (double)Volume / (double)MAX_GAIN;
6227c478bd9Sstevel@tonic-gate (void) audio_get_record_gain(Audio_fd, &Savevol);
6237c478bd9Sstevel@tonic-gate err = audio_set_record_gain(Audio_fd, &vol);
6247c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
6257c478bd9Sstevel@tonic-gate Error(stderr,
6267c478bd9Sstevel@tonic-gate MGET("%s: could not set record volume for %s\n"),
6277c478bd9Sstevel@tonic-gate prog, Audio_dev);
6287c478bd9Sstevel@tonic-gate exit(1);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate if (isatty(ofd)) {
63388447a05SGarrett D'Amore Error(stderr, MGET("%s: No files and stdout is a tty\n"),
63488447a05SGarrett D'Amore prog);
63588447a05SGarrett D'Amore exit(1);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate /* Set up SIGINT handler so that final buffers may be flushed */
6397c478bd9Sstevel@tonic-gate (void) signal(SIGINT, sigint);
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate * At this point, we're (finally) ready to copy the data.
6437c478bd9Sstevel@tonic-gate * Init a poll() structure, to use when there's nothing to read.
6447c478bd9Sstevel@tonic-gate */
6457c478bd9Sstevel@tonic-gate if (Time > 0)
6467c478bd9Sstevel@tonic-gate Limit = audio_secs_to_bytes(&Dev_hdr, Time);
6477c478bd9Sstevel@tonic-gate pfd.fd = Audio_fd;
6487c478bd9Sstevel@tonic-gate pfd.events = POLLIN;
6497c478bd9Sstevel@tonic-gate while ((Limit == AUDIO_UNKNOWN_SIZE) || (Limit != 0)) {
6507c478bd9Sstevel@tonic-gate /* Fill the buffer or read to the time limit */
6517c478bd9Sstevel@tonic-gate cnt = read(Audio_fd, (char *)buf,
6527c478bd9Sstevel@tonic-gate ((Limit != AUDIO_UNKNOWN_SIZE) && (Limit < sizeof (buf)) ?
6537c478bd9Sstevel@tonic-gate (int)Limit : sizeof (buf)));
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate if (cnt == 0) /* normally, eof can't happen */
6567c478bd9Sstevel@tonic-gate break;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate /* If error, probably have to wait for input */
6597c478bd9Sstevel@tonic-gate if (cnt < 0) {
6607c478bd9Sstevel@tonic-gate if (Cleanup)
6617c478bd9Sstevel@tonic-gate break; /* done if ^C seen */
6627c478bd9Sstevel@tonic-gate switch (errno) {
6637c478bd9Sstevel@tonic-gate case EAGAIN:
6647c478bd9Sstevel@tonic-gate (void) poll(&pfd, 1L, -1);
6657c478bd9Sstevel@tonic-gate break;
6667c478bd9Sstevel@tonic-gate case EOVERFLOW: /* Possibly a Large File */
6677c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: error reading"), prog);
6687c478bd9Sstevel@tonic-gate perror("Large File");
6697c478bd9Sstevel@tonic-gate exit(1);
6707c478bd9Sstevel@tonic-gate default:
6717c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: error reading"), prog);
6727c478bd9Sstevel@tonic-gate perror(Audio_dev);
6737c478bd9Sstevel@tonic-gate exit(1);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate continue;
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate /* Swab the output if required. */
6797c478bd9Sstevel@tonic-gate if (swapBytes) {
6807c478bd9Sstevel@tonic-gate swab((char *)buf, swapBuf, cnt);
6817c478bd9Sstevel@tonic-gate err = write(ofd, swapBuf, cnt);
6827c478bd9Sstevel@tonic-gate } else {
6837c478bd9Sstevel@tonic-gate err = write(ofd, (char *)buf, cnt);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate if (err < 0) {
6867c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: error writing "), prog);
6877c478bd9Sstevel@tonic-gate perror(Ofile);
6887c478bd9Sstevel@tonic-gate exit(1);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate if (err != cnt) {
6917c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: error writing "), prog);
6927c478bd9Sstevel@tonic-gate perror(Ofile);
6937c478bd9Sstevel@tonic-gate break;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate Size += cnt;
6967c478bd9Sstevel@tonic-gate if (Limit != AUDIO_UNKNOWN_SIZE)
6977c478bd9Sstevel@tonic-gate Limit -= cnt;
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate
7007c478bd9Sstevel@tonic-gate /* Attempt to rewrite the data_size field of the file header */
7017c478bd9Sstevel@tonic-gate if (!Append || (Oldsize != AUDIO_UNKNOWN_SIZE)) {
7027c478bd9Sstevel@tonic-gate if (Append)
7037c478bd9Sstevel@tonic-gate Size += Oldsize;
7047c478bd9Sstevel@tonic-gate (void) audio_rewrite_filesize(ofd, File_type, Size,
7057c478bd9Sstevel@tonic-gate Dev_hdr.channels, Dev_hdr.bytes_per_unit);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate (void) close(ofd); /* close input file */
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate
7117c478bd9Sstevel@tonic-gate /* Check for error during record */
7127c478bd9Sstevel@tonic-gate if (audio_get_record_error(Audio_fd, (unsigned *)&err) != AUDIO_SUCCESS)
7137c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: error reading device status\n"), prog);
7147c478bd9Sstevel@tonic-gate else if (err)
7157c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: WARNING: Data overflow occurred\n"),
7167c478bd9Sstevel@tonic-gate prog);
7177c478bd9Sstevel@tonic-gate
71888447a05SGarrett D'Amore /* Reset record volume, encoding */
7197c478bd9Sstevel@tonic-gate if (Volume != INT_MAX)
7207c478bd9Sstevel@tonic-gate (void) audio_set_record_gain(Audio_fd, &Savevol);
7217c478bd9Sstevel@tonic-gate if (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0) {
7227c478bd9Sstevel@tonic-gate (void) audio_set_record_config(Audio_fd, &Save_hdr);
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate (void) close(Audio_fd);
7257c478bd9Sstevel@tonic-gate return (0);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate /* Parse an unsigned integer */
7297c478bd9Sstevel@tonic-gate static int
parse_unsigned(char * str,unsigned * dst,char * flag)7307c478bd9Sstevel@tonic-gate parse_unsigned(char *str, unsigned *dst, char *flag)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate char x;
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate if (sscanf(str, "%u%c", dst, &x) != 1) {
7357c478bd9Sstevel@tonic-gate Error(stderr, MGET("%s: invalid value for %s\n"), prog, flag);
7367c478bd9Sstevel@tonic-gate return (1);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate return (0);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate * set the sample rate. assume anything is ok. check later on to make sure
7437c478bd9Sstevel@tonic-gate * the sample rate is valid.
7447c478bd9Sstevel@tonic-gate */
7457c478bd9Sstevel@tonic-gate static int
parse_sample_rate(char * s,unsigned * rate)7467c478bd9Sstevel@tonic-gate parse_sample_rate(char *s, unsigned *rate)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate char *cp;
7497c478bd9Sstevel@tonic-gate double drate;
7507c478bd9Sstevel@tonic-gate
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate * check if it's "cd" or "dat" or "voice". these also set
7537c478bd9Sstevel@tonic-gate * the precision and encoding, etc.
7547c478bd9Sstevel@tonic-gate */
7557c478bd9Sstevel@tonic-gate if (strcasecmp(s, "dat") == 0) {
7567c478bd9Sstevel@tonic-gate drate = 48000.0;
7577c478bd9Sstevel@tonic-gate } else if (strcasecmp(s, "cd") == 0) {
7587c478bd9Sstevel@tonic-gate drate = 44100.0;
7597c478bd9Sstevel@tonic-gate } else if (strcasecmp(s, "voice") == 0) {
7607c478bd9Sstevel@tonic-gate drate = 8000.0;
7617c478bd9Sstevel@tonic-gate } else {
7627c478bd9Sstevel@tonic-gate /* just do an atof */
7637c478bd9Sstevel@tonic-gate drate = atof(s);
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate * if the first non-digit is a "k" multiply by 1000,
7677c478bd9Sstevel@tonic-gate * if it's an "h", leave it alone. anything else,
7687c478bd9Sstevel@tonic-gate * return an error.
7697c478bd9Sstevel@tonic-gate */
7707c478bd9Sstevel@tonic-gate
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate * XXX bug alert: could have multiple "." in string
7737c478bd9Sstevel@tonic-gate * and mess things up.
7747c478bd9Sstevel@tonic-gate */
77588447a05SGarrett D'Amore for (cp = s; *cp && (isdigit(*cp) || (*cp == '.')); cp++)
77688447a05SGarrett D'Amore /* NOP */;
777*64731133SToomas Soome if (*cp != '\0') {
7787c478bd9Sstevel@tonic-gate if ((*cp == 'k') || (*cp == 'K')) {
7797c478bd9Sstevel@tonic-gate drate *= 1000.0;
780fefb716aSJohn Levon } else if ((*cp != 'h') && (*cp != 'H')) {
7817c478bd9Sstevel@tonic-gate /* bogus! */
7827c478bd9Sstevel@tonic-gate Error(stderr,
7837c478bd9Sstevel@tonic-gate MGET("invalid sample rate: %s\n"), s);
7847c478bd9Sstevel@tonic-gate return (1);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate *rate = irint(drate);
7917c478bd9Sstevel@tonic-gate return (0);
7927c478bd9Sstevel@tonic-gate }
793