2002-07-20 12:40:16 +04:00
|
|
|
/* $NetBSD: record.c,v 1.30 2002/07/20 08:40:16 grant Exp $ */
|
1999-03-26 18:46:22 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Matthew R. Green
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1999-03-26 17:02:39 +03:00
|
|
|
/*
|
|
|
|
* SunOS compatible audiorecord(1)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/audioio.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "libaudio.h"
|
2002-01-15 20:02:51 +03:00
|
|
|
#include "auconv.h"
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
audio_info_t info, oinfo;
|
|
|
|
ssize_t total_size = -1;
|
2002-01-16 02:48:51 +03:00
|
|
|
const char *device;
|
2002-02-10 18:30:53 +03:00
|
|
|
int format = AUDIO_FORMAT_DEFAULT;
|
1999-03-26 17:02:39 +03:00
|
|
|
char *header_info;
|
|
|
|
char default_info[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
|
2002-02-02 23:20:23 +03:00
|
|
|
int audiofd, outfd;
|
1999-03-26 17:02:39 +03:00
|
|
|
int qflag, aflag, fflag;
|
|
|
|
int verbose;
|
2002-01-15 11:19:36 +03:00
|
|
|
int monitor_gain, omonitor_gain;
|
|
|
|
int gain;
|
1999-03-26 17:02:39 +03:00
|
|
|
int balance;
|
|
|
|
int port;
|
|
|
|
int encoding;
|
|
|
|
char *encoding_str;
|
|
|
|
int precision;
|
|
|
|
int sample_rate;
|
|
|
|
int channels;
|
|
|
|
struct timeval record_time;
|
|
|
|
struct timeval start_time; /* XXX because that's what gettimeofday returns */
|
|
|
|
|
2002-01-15 20:02:51 +03:00
|
|
|
void (*conv_func) (u_char *, size_t);
|
2002-01-15 11:59:20 +03:00
|
|
|
|
2000-12-22 14:38:42 +03:00
|
|
|
void usage (void);
|
|
|
|
int main (int, char *[]);
|
|
|
|
int timeleft (struct timeval *, struct timeval *);
|
|
|
|
void cleanup (int) __attribute__((__noreturn__));
|
2002-01-15 11:19:36 +03:00
|
|
|
int write_header_sun (void **, size_t *, int *);
|
|
|
|
int write_header_wav (void **, size_t *, int *);
|
2000-12-22 14:38:42 +03:00
|
|
|
void write_header (void);
|
|
|
|
void rewrite_header (void);
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
size_t len, bufsize;
|
1999-09-27 05:58:07 +04:00
|
|
|
int ch, no_time_limit = 1;
|
2002-02-05 03:17:26 +03:00
|
|
|
const char *defdevice = _PATH_SOUND;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
while ((ch = getopt(argc, argv, "ab:C:F:c:d:e:fhi:m:P:p:qt:s:Vv:")) != -1) {
|
1999-03-26 17:02:39 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
decode_int(optarg, &balance);
|
|
|
|
if (balance < 0 || balance > 63)
|
2002-07-20 12:40:16 +04:00
|
|
|
errx(1, "balance must be between 0 and 63");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
|
|
|
case 'C':
|
2002-02-02 23:20:23 +03:00
|
|
|
/* Ignore, compatibility */
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
2002-01-15 11:19:36 +03:00
|
|
|
case 'F':
|
|
|
|
format = audio_format_from_str(optarg);
|
|
|
|
if (format < 0)
|
2002-02-10 18:30:53 +03:00
|
|
|
errx(1, "Unknown audio format; supported "
|
|
|
|
"formats: \"sun\", \"wav\", and \"none\"");
|
2002-01-15 11:19:36 +03:00
|
|
|
break;
|
1999-03-26 17:02:39 +03:00
|
|
|
case 'c':
|
|
|
|
decode_int(optarg, &channels);
|
|
|
|
if (channels < 0 || channels > 16)
|
2002-07-20 12:40:16 +04:00
|
|
|
errx(1, "channels must be between 0 and 16");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
device = optarg;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
encoding_str = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag++;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
header_info = optarg;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2002-01-15 11:19:36 +03:00
|
|
|
decode_int(optarg, &monitor_gain);
|
|
|
|
if (monitor_gain < 0 || monitor_gain > 255)
|
2002-07-20 12:40:16 +04:00
|
|
|
errx(1, "monitor volume must be between 0 and 255");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
decode_int(optarg, &precision);
|
2001-05-02 16:49:41 +04:00
|
|
|
if (precision != 4 && precision != 8 &&
|
|
|
|
precision != 16 && precision != 24 &&
|
|
|
|
precision != 32)
|
|
|
|
errx(1, "precision must be between 4, 8, 16, 24 or 32");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
len = strlen(optarg);
|
|
|
|
|
|
|
|
if (strncmp(optarg, "mic", len) == 0)
|
|
|
|
port |= AUDIO_MICROPHONE;
|
|
|
|
else if (strncmp(optarg, "cd", len) == 0 ||
|
|
|
|
strncmp(optarg, "internal-cd", len) == 0)
|
|
|
|
port |= AUDIO_CD;
|
|
|
|
else if (strncmp(optarg, "line", len) == 0)
|
|
|
|
port |= AUDIO_LINE_IN;
|
|
|
|
else
|
|
|
|
errx(1,
|
|
|
|
"port must be `cd', `internal-cd', `mic', or `line'");
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
qflag++;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
decode_int(optarg, &sample_rate);
|
|
|
|
if (sample_rate < 0 || sample_rate > 48000 * 2) /* XXX */
|
2002-07-20 12:40:16 +04:00
|
|
|
errx(1, "sample rate must be between 0 and 96000");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
|
|
|
case 't':
|
1999-09-27 05:58:07 +04:00
|
|
|
no_time_limit = 0;
|
1999-03-26 17:02:39 +03:00
|
|
|
decode_time(optarg, &record_time);
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'v':
|
2002-01-15 11:19:36 +03:00
|
|
|
decode_int(optarg, &gain);
|
|
|
|
if (gain < 0 || gain > 255)
|
2002-07-20 12:40:16 +04:00
|
|
|
errx(1, "volume must be between 0 and 255");
|
1999-03-26 17:02:39 +03:00
|
|
|
break;
|
|
|
|
/* case 'h': */
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open the audio device, and control device
|
|
|
|
*/
|
1999-07-13 18:02:33 +04:00
|
|
|
if (device == NULL && (device = getenv("AUDIODEVICE")) == NULL &&
|
|
|
|
(device = getenv("AUDIODEV")) == NULL) /* Sun compatibility */
|
2002-02-05 03:17:26 +03:00
|
|
|
device = defdevice;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
audiofd = open(device, O_RDONLY);
|
2002-02-05 03:17:26 +03:00
|
|
|
if (audiofd < 0 && device == defdevice) {
|
2002-01-31 03:03:23 +03:00
|
|
|
device = _PATH_SOUND0;
|
2002-03-21 06:48:24 +03:00
|
|
|
audiofd = open(device, O_RDONLY);
|
2002-01-31 03:03:23 +03:00
|
|
|
}
|
1999-03-26 17:02:39 +03:00
|
|
|
if (audiofd < 0)
|
|
|
|
err(1, "failed to open %s", device);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* work out the buffer size to use, and allocate it. also work out
|
|
|
|
* what the old monitor gain value is, so that we can reset it later.
|
|
|
|
*/
|
2002-02-02 23:20:23 +03:00
|
|
|
if (ioctl(audiofd, AUDIO_GETINFO, &oinfo) < 0)
|
1999-03-26 17:02:39 +03:00
|
|
|
err(1, "failed to get audio info");
|
|
|
|
bufsize = oinfo.record.buffer_size;
|
|
|
|
if (bufsize < 32 * 1024)
|
|
|
|
bufsize = 32 * 1024;
|
2002-01-15 11:19:36 +03:00
|
|
|
omonitor_gain = oinfo.monitor_gain;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
buffer = malloc(bufsize);
|
|
|
|
if (buffer == NULL)
|
|
|
|
err(1, "couldn't malloc buffer of %d size", (int)bufsize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open the output file
|
|
|
|
*/
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
if (argv[0][0] != '-' && argv[0][1] != '\0') {
|
|
|
|
outfd = open(*argv, O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY, 0666);
|
|
|
|
if (outfd < 0)
|
|
|
|
err(1, "could not open %s", *argv);
|
|
|
|
} else
|
|
|
|
outfd = STDOUT_FILENO;
|
|
|
|
|
|
|
|
/*
|
2002-01-15 11:19:36 +03:00
|
|
|
* convert the encoding string into a value.
|
1999-03-26 17:02:39 +03:00
|
|
|
*/
|
1999-03-27 20:46:00 +03:00
|
|
|
if (encoding_str) {
|
|
|
|
encoding = audio_enc_to_val(encoding_str);
|
|
|
|
if (encoding == -1)
|
1999-03-26 17:02:39 +03:00
|
|
|
errx(1, "unknown encoding, bailing...");
|
|
|
|
}
|
2000-02-02 08:02:25 +03:00
|
|
|
else
|
|
|
|
encoding = AUDIO_ENCODING_ULAW;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
/*
|
|
|
|
* set up audio device for recording with the speified parameters
|
|
|
|
*/
|
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* for these, get the current values for stuffing into the header
|
|
|
|
*/
|
|
|
|
#define SETINFO(x) if (x) info.record.x = x; else x = oinfo.record.x
|
|
|
|
SETINFO (sample_rate);
|
|
|
|
SETINFO (channels);
|
|
|
|
SETINFO (precision);
|
|
|
|
SETINFO (encoding);
|
|
|
|
SETINFO (gain);
|
|
|
|
SETINFO (port);
|
|
|
|
SETINFO (balance);
|
|
|
|
#undef SETINFO
|
|
|
|
|
|
|
|
if (monitor_gain)
|
|
|
|
info.monitor_gain = monitor_gain;
|
|
|
|
else
|
|
|
|
monitor_gain = oinfo.monitor_gain;
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
info.mode = AUMODE_RECORD;
|
2002-02-02 23:20:23 +03:00
|
|
|
if (ioctl(audiofd, AUDIO_SETINFO, &info) < 0)
|
2002-07-20 11:45:08 +04:00
|
|
|
err(1, "failed to set audio info");
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
signal(SIGINT, cleanup);
|
|
|
|
write_header();
|
|
|
|
total_size = 0;
|
|
|
|
|
2002-02-10 18:30:53 +03:00
|
|
|
if (verbose && conv_func) {
|
|
|
|
const char *s = NULL;
|
|
|
|
|
|
|
|
if (conv_func == swap_bytes)
|
|
|
|
s = "swap bytes (16 bit)";
|
|
|
|
else if (conv_func == swap_bytes32)
|
|
|
|
s = "swap bytes (32 bit)";
|
|
|
|
else if (conv_func == change_sign16_be)
|
|
|
|
s = "change sign (big-endian, 16 bit)";
|
|
|
|
else if (conv_func == change_sign16_le)
|
|
|
|
s = "change sign (little-endian, 16 bit)";
|
|
|
|
else if (conv_func == change_sign32_be)
|
|
|
|
s = "change sign (big-endian, 32 bit)";
|
|
|
|
else if (conv_func == change_sign32_le)
|
|
|
|
s = "change sign (little-endian, 32 bit)";
|
|
|
|
else if (conv_func == change_sign16_swap_bytes_be)
|
|
|
|
s = "change sign & swap bytes (big-endian, 16 bit)";
|
|
|
|
else if (conv_func == change_sign16_swap_bytes_le)
|
|
|
|
s = "change sign & swap bytes (little-endian, 16 bit)";
|
|
|
|
else if (conv_func == change_sign32_swap_bytes_be)
|
|
|
|
s = "change sign (big-endian, 32 bit)";
|
|
|
|
else if (conv_func == change_sign32_swap_bytes_le)
|
|
|
|
s = "change sign & swap bytes (little-endian, 32 bit)";
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
fprintf(stderr, "%s: converting, using function: %s\n",
|
|
|
|
getprogname(), s);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s: using unnamed conversion "
|
|
|
|
"function\n", getprogname());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr,
|
|
|
|
"sample_rate=%d channels=%d precision=%d encoding=%s\n",
|
|
|
|
info.record.sample_rate, info.record.channels,
|
|
|
|
info.record.precision,
|
|
|
|
audio_enc_from_val(info.record.encoding));
|
|
|
|
|
1999-03-26 17:02:39 +03:00
|
|
|
(void)gettimeofday(&start_time, NULL);
|
1999-09-27 05:58:07 +04:00
|
|
|
while (no_time_limit || timeleft(&start_time, &record_time)) {
|
1999-03-26 17:02:39 +03:00
|
|
|
if (read(audiofd, buffer, bufsize) != bufsize)
|
|
|
|
err(1, "read failed");
|
2002-01-15 11:59:20 +03:00
|
|
|
if (conv_func)
|
2002-01-15 20:02:51 +03:00
|
|
|
(*conv_func)(buffer, bufsize);
|
1999-03-26 17:02:39 +03:00
|
|
|
if (write(outfd, buffer, bufsize) != bufsize)
|
|
|
|
err(1, "write failed");
|
|
|
|
total_size += bufsize;
|
|
|
|
}
|
|
|
|
cleanup(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
timeleft(start_tvp, record_tvp)
|
|
|
|
struct timeval *start_tvp;
|
|
|
|
struct timeval *record_tvp;
|
|
|
|
{
|
|
|
|
struct timeval now, diff;
|
|
|
|
|
|
|
|
(void)gettimeofday(&now, NULL);
|
1999-09-23 19:41:31 +04:00
|
|
|
timersub(&now, start_tvp, &diff);
|
|
|
|
timersub(record_tvp, &diff, &now);
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(signo)
|
|
|
|
int signo;
|
|
|
|
{
|
|
|
|
|
|
|
|
rewrite_header();
|
|
|
|
close(outfd);
|
2002-01-15 11:19:36 +03:00
|
|
|
if (omonitor_gain) {
|
1999-03-26 17:02:39 +03:00
|
|
|
AUDIO_INITINFO(&info);
|
2002-01-15 11:19:36 +03:00
|
|
|
info.monitor_gain = omonitor_gain;
|
2002-02-02 23:20:23 +03:00
|
|
|
if (ioctl(audiofd, AUDIO_SETINFO, &info) < 0)
|
1999-03-26 17:02:39 +03:00
|
|
|
err(1, "failed to reset audio info");
|
|
|
|
}
|
2002-02-02 23:20:23 +03:00
|
|
|
close(audiofd);
|
1999-03-26 17:02:39 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
int
|
|
|
|
write_header_sun(hdrp, lenp, leftp)
|
|
|
|
void **hdrp;
|
|
|
|
size_t *lenp;
|
|
|
|
int *leftp;
|
1999-03-26 17:02:39 +03:00
|
|
|
{
|
2001-06-07 16:50:29 +04:00
|
|
|
static int warned = 0;
|
2002-01-15 11:19:36 +03:00
|
|
|
static sun_audioheader auh;
|
2002-01-15 20:02:51 +03:00
|
|
|
int sunenc, oencoding = encoding;
|
|
|
|
|
2002-02-10 18:30:53 +03:00
|
|
|
/* only perform conversions if we don't specify the encoding */
|
2002-02-10 16:16:08 +03:00
|
|
|
switch (encoding) {
|
|
|
|
case AUDIO_ENCODING_ULINEAR_LE:
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
case AUDIO_ENCODING_ULINEAR:
|
|
|
|
#endif
|
2002-01-15 20:02:51 +03:00
|
|
|
if (precision == 16)
|
2002-02-10 18:30:53 +03:00
|
|
|
conv_func = change_sign16_swap_bytes_le;
|
2002-01-15 20:02:51 +03:00
|
|
|
else if (precision == 32)
|
2002-02-10 18:30:53 +03:00
|
|
|
conv_func = change_sign32_swap_bytes_le;
|
2002-01-16 02:48:51 +03:00
|
|
|
if (conv_func)
|
|
|
|
encoding = AUDIO_ENCODING_SLINEAR_BE;
|
2002-02-10 16:16:08 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUDIO_ENCODING_ULINEAR_BE:
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
case AUDIO_ENCODING_ULINEAR:
|
|
|
|
#endif
|
2002-01-15 20:02:51 +03:00
|
|
|
if (precision == 16)
|
|
|
|
conv_func = change_sign16_be;
|
|
|
|
else if (precision == 32)
|
|
|
|
conv_func = change_sign32_be;
|
2002-01-16 02:48:51 +03:00
|
|
|
if (conv_func)
|
|
|
|
encoding = AUDIO_ENCODING_SLINEAR_BE;
|
2002-02-10 16:16:08 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUDIO_ENCODING_SLINEAR_LE:
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
case AUDIO_ENCODING_SLINEAR:
|
|
|
|
#endif
|
2002-01-15 20:02:51 +03:00
|
|
|
if (precision == 16)
|
2002-02-10 18:30:53 +03:00
|
|
|
conv_func = swap_bytes;
|
2002-01-15 20:02:51 +03:00
|
|
|
else if (precision == 32)
|
2002-02-10 18:30:53 +03:00
|
|
|
conv_func = swap_bytes32;
|
2002-01-16 02:48:51 +03:00
|
|
|
if (conv_func)
|
|
|
|
encoding = AUDIO_ENCODING_SLINEAR_BE;
|
2002-02-10 16:16:08 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
case AUDIO_ENCODING_SLINEAR:
|
|
|
|
encoding = AUDIO_ENCODING_SLINEAR_BE;
|
|
|
|
break;
|
|
|
|
#endif
|
2002-01-15 20:02:51 +03:00
|
|
|
}
|
|
|
|
|
2000-12-13 11:19:54 +03:00
|
|
|
/* if we can't express this as a Sun header, don't write any */
|
|
|
|
if (audio_encoding_to_sun(encoding, precision, &sunenc) != 0) {
|
2002-02-10 16:16:08 +03:00
|
|
|
if (!qflag && !warned) {
|
|
|
|
const char *s = audio_enc_from_val(oencoding);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
s = "(unknown)";
|
|
|
|
warnx("failed to convert to sun encoding from %s "
|
|
|
|
"(precision %d);\nSun audio header not written",
|
|
|
|
s, precision);
|
|
|
|
}
|
2002-02-10 18:30:53 +03:00
|
|
|
format = AUDIO_FORMAT_NONE;
|
2002-01-15 20:02:51 +03:00
|
|
|
conv_func = 0;
|
2001-06-07 16:50:29 +04:00
|
|
|
warned = 1;
|
2002-01-15 11:19:36 +03:00
|
|
|
return -1;
|
2000-12-13 11:19:54 +03:00
|
|
|
}
|
1999-03-26 17:02:39 +03:00
|
|
|
|
|
|
|
auh.magic = htonl(AUDIO_FILE_MAGIC);
|
|
|
|
if (outfd == STDOUT_FILENO)
|
|
|
|
auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
|
|
|
|
else
|
|
|
|
auh.data_size = htonl(total_size);
|
2000-12-13 11:19:54 +03:00
|
|
|
auh.encoding = htonl(sunenc);
|
1999-03-26 17:02:39 +03:00
|
|
|
auh.sample_rate = htonl(sample_rate);
|
|
|
|
auh.channels = htonl(channels);
|
|
|
|
if (header_info) {
|
|
|
|
int len, infolen;
|
|
|
|
|
|
|
|
infolen = ((len = strlen(header_info)) + 7) & 0xfffffff8;
|
2002-01-15 11:19:36 +03:00
|
|
|
*leftp = infolen - len;
|
1999-03-26 17:02:39 +03:00
|
|
|
auh.hdr_size = htonl(sizeof(auh) + infolen);
|
|
|
|
} else {
|
2002-01-15 11:19:36 +03:00
|
|
|
*leftp = sizeof(default_info);
|
|
|
|
auh.hdr_size = htonl(sizeof(auh) + *leftp);
|
|
|
|
}
|
|
|
|
*(sun_audioheader **)hdrp = &auh;
|
|
|
|
*lenp = sizeof auh;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
write_header_wav(hdrp, lenp, leftp)
|
|
|
|
void **hdrp;
|
|
|
|
size_t *lenp;
|
|
|
|
int *leftp;
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* WAV header we write looks like this:
|
|
|
|
*
|
|
|
|
* bytes purpose
|
|
|
|
* 0-3 "RIFF"
|
|
|
|
* 4-7 file length (minus 8)
|
|
|
|
* 8-15 "WAVEfmt "
|
|
|
|
* 16-19 format size
|
|
|
|
* 20-21 format tag
|
|
|
|
* 22-23 number of channels
|
|
|
|
* 24-27 sample rate
|
|
|
|
* 28-31 average bytes per second
|
|
|
|
* 32-33 block alignment
|
|
|
|
* 34-35 bits per sample
|
|
|
|
*
|
|
|
|
* then for ULAW and ALAW outputs, we have an extended chunk size
|
|
|
|
* and a WAV "fact" to add:
|
|
|
|
*
|
|
|
|
* 36-37 length of extension (== 0)
|
|
|
|
* 38-41 "fact"
|
|
|
|
* 42-45 fact size
|
|
|
|
* 46-49 number of samples written
|
|
|
|
* 50-53 "data"
|
|
|
|
* 54-57 data length
|
|
|
|
* 58- raw audio data
|
|
|
|
*
|
|
|
|
* for PCM outputs we have just the data remaining:
|
|
|
|
*
|
|
|
|
* 36-39 "data"
|
|
|
|
* 40-43 data length
|
|
|
|
* 44- raw audio data
|
|
|
|
*
|
|
|
|
* RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
|
|
|
|
*/
|
|
|
|
char wavheaderbuf[64], *p = wavheaderbuf;
|
2002-01-16 02:48:51 +03:00
|
|
|
const char *riff = "RIFF",
|
|
|
|
*wavefmt = "WAVEfmt ",
|
|
|
|
*fact = "fact",
|
|
|
|
*data = "data";
|
2002-01-15 11:19:36 +03:00
|
|
|
u_int32_t filelen, fmtsz, sps, abps, factsz = 4, nsample, datalen;
|
|
|
|
u_int16_t fmttag, nchan, align, bps, extln = 0;
|
|
|
|
|
|
|
|
if (header_info)
|
|
|
|
warnx("header information not supported for WAV");
|
|
|
|
*leftp = NULL;
|
|
|
|
|
|
|
|
switch (precision) {
|
|
|
|
case 8:
|
|
|
|
bps = 8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
bps = 16;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
bps = 32;
|
|
|
|
break;
|
|
|
|
default:
|
2002-01-15 11:59:20 +03:00
|
|
|
{
|
|
|
|
static int warned = 0;
|
|
|
|
|
|
|
|
if (warned == 0) {
|
2002-07-20 12:40:16 +04:00
|
|
|
warnx("can not support precision of %d", precision);
|
2002-01-15 11:59:20 +03:00
|
|
|
warned = 1;
|
|
|
|
}
|
2002-01-15 11:19:36 +03:00
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (encoding) {
|
|
|
|
case AUDIO_ENCODING_ULAW:
|
|
|
|
fmttag = WAVE_FORMAT_MULAW;
|
|
|
|
fmtsz = 18;
|
|
|
|
align = channels;
|
|
|
|
break;
|
2002-02-10 16:23:27 +03:00
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
case AUDIO_ENCODING_ALAW:
|
|
|
|
fmttag = WAVE_FORMAT_ALAW;
|
|
|
|
fmtsz = 18;
|
|
|
|
align = channels;
|
|
|
|
break;
|
2002-02-10 16:23:27 +03:00
|
|
|
|
2002-01-15 11:59:20 +03:00
|
|
|
/*
|
|
|
|
* we could try to support RIFX but it seems to be more portable
|
|
|
|
* to output little-endian data for WAV files.
|
|
|
|
*/
|
|
|
|
case AUDIO_ENCODING_ULINEAR_BE:
|
2002-02-10 16:23:27 +03:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
case AUDIO_ENCODING_ULINEAR:
|
|
|
|
#endif
|
2002-01-15 20:17:13 +03:00
|
|
|
if (bps == 16)
|
|
|
|
conv_func = change_sign16_swap_bytes_be;
|
|
|
|
else if (bps == 32)
|
|
|
|
conv_func = change_sign32_swap_bytes_be;
|
|
|
|
goto fmt_pcm;
|
2002-02-10 16:23:27 +03:00
|
|
|
|
2002-01-15 11:59:20 +03:00
|
|
|
case AUDIO_ENCODING_SLINEAR_BE:
|
2002-02-10 16:23:27 +03:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
case AUDIO_ENCODING_SLINEAR:
|
|
|
|
#endif
|
2002-01-15 11:59:20 +03:00
|
|
|
if (bps == 16)
|
2002-01-15 20:02:51 +03:00
|
|
|
conv_func = swap_bytes;
|
2002-01-15 11:59:20 +03:00
|
|
|
else if (bps == 32)
|
2002-01-15 20:02:51 +03:00
|
|
|
conv_func = swap_bytes32;
|
2002-01-15 20:17:13 +03:00
|
|
|
goto fmt_pcm;
|
2002-02-10 16:23:27 +03:00
|
|
|
|
2002-01-15 11:59:20 +03:00
|
|
|
case AUDIO_ENCODING_ULINEAR_LE:
|
2002-02-10 16:23:27 +03:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
case AUDIO_ENCODING_ULINEAR:
|
|
|
|
#endif
|
2002-01-15 20:17:13 +03:00
|
|
|
if (bps == 16)
|
|
|
|
conv_func = change_sign16_le;
|
|
|
|
else if (bps == 32)
|
|
|
|
conv_func = change_sign32_le;
|
|
|
|
/* FALLTHROUGH */
|
2002-02-10 16:23:27 +03:00
|
|
|
|
2002-01-15 11:59:20 +03:00
|
|
|
case AUDIO_ENCODING_SLINEAR_LE:
|
2002-01-15 20:17:13 +03:00
|
|
|
case AUDIO_ENCODING_PCM16:
|
2002-02-10 16:23:27 +03:00
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
case AUDIO_ENCODING_SLINEAR:
|
|
|
|
#endif
|
2002-01-15 20:17:13 +03:00
|
|
|
fmt_pcm:
|
2002-01-15 11:19:36 +03:00
|
|
|
fmttag = WAVE_FORMAT_PCM;
|
|
|
|
fmtsz = 16;
|
|
|
|
align = channels * (bps / 8);
|
|
|
|
break;
|
2002-02-10 16:23:27 +03:00
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
default:
|
2002-01-15 11:59:20 +03:00
|
|
|
{
|
|
|
|
static int warned = 0;
|
|
|
|
|
|
|
|
if (warned == 0) {
|
2002-02-10 16:23:27 +03:00
|
|
|
const char *s = wav_enc_from_val(encoding);
|
|
|
|
|
|
|
|
if (s == NULL)
|
2002-07-20 12:40:16 +04:00
|
|
|
warnx("can not support encoding of %s", s);
|
2002-02-10 16:23:27 +03:00
|
|
|
else
|
2002-07-20 12:40:16 +04:00
|
|
|
warnx("can not support encoding of %d", encoding);
|
2002-01-15 11:59:20 +03:00
|
|
|
warned = 1;
|
|
|
|
}
|
2002-01-15 11:19:36 +03:00
|
|
|
}
|
2002-02-10 18:30:53 +03:00
|
|
|
format = AUDIO_FORMAT_NONE;
|
2002-01-15 11:19:36 +03:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
nchan = channels;
|
|
|
|
sps = sample_rate;
|
|
|
|
|
|
|
|
/* data length */
|
|
|
|
if (outfd == STDOUT_FILENO)
|
|
|
|
datalen = 0;
|
|
|
|
else
|
|
|
|
datalen = total_size;
|
|
|
|
|
|
|
|
/* file length */
|
|
|
|
filelen = 4 + (8 + fmtsz) + (8 + datalen);
|
|
|
|
if (fmttag != WAVE_FORMAT_PCM)
|
|
|
|
filelen += 8 + factsz;
|
|
|
|
|
|
|
|
abps = (double)align*sample_rate / (double)1 + 0.5;
|
|
|
|
|
|
|
|
nsample = (datalen / bps) / sample_rate;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now we've calculated the info, write it out!
|
|
|
|
*/
|
|
|
|
#define put32(x) do { \
|
|
|
|
u_int32_t _f; \
|
|
|
|
putle32(_f, (x)); \
|
|
|
|
memcpy(p, &_f, 4); \
|
|
|
|
} while (0)
|
|
|
|
#define put16(x) do { \
|
|
|
|
u_int16_t _f; \
|
|
|
|
putle16(_f, (x)); \
|
|
|
|
memcpy(p, &_f, 2); \
|
|
|
|
} while (0)
|
|
|
|
memcpy(p, riff, 4);
|
|
|
|
p += 4; /* 4 */
|
|
|
|
put32(filelen);
|
|
|
|
p += 4; /* 8 */
|
|
|
|
memcpy(p, wavefmt, 8);
|
|
|
|
p += 8; /* 16 */
|
|
|
|
put32(fmtsz);
|
|
|
|
p += 4; /* 20 */
|
|
|
|
put16(fmttag);
|
|
|
|
p += 2; /* 22 */
|
|
|
|
put16(nchan);
|
|
|
|
p += 2; /* 24 */
|
|
|
|
put32(sps);
|
|
|
|
p += 4; /* 28 */
|
|
|
|
put32(abps);
|
|
|
|
p += 4; /* 32 */
|
|
|
|
put16(align);
|
|
|
|
p += 2; /* 34 */
|
|
|
|
put16(bps);
|
|
|
|
p += 2; /* 36 */
|
|
|
|
/* NON PCM formats have an extended chunk; write it */
|
|
|
|
if (fmttag != WAVE_FORMAT_PCM) {
|
|
|
|
put16(extln);
|
|
|
|
p += 2; /* 38 */
|
|
|
|
memcpy(p, fact, 4);
|
|
|
|
p += 4; /* 42 */
|
|
|
|
put32(factsz);
|
|
|
|
p += 4; /* 46 */
|
|
|
|
put32(nsample);
|
|
|
|
p += 4; /* 50 */
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|
2002-01-15 11:19:36 +03:00
|
|
|
memcpy(p, data, 4);
|
|
|
|
p += 4; /* 40/54 */
|
|
|
|
put32(datalen);
|
|
|
|
p += 4; /* 44/58 */
|
|
|
|
#undef put32
|
|
|
|
#undef put16
|
1999-03-26 17:02:39 +03:00
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
*hdrp = wavheaderbuf;
|
|
|
|
*lenp = (p - wavheaderbuf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
write_header()
|
|
|
|
{
|
|
|
|
struct iovec iv[3];
|
|
|
|
int veclen, left, tlen;
|
|
|
|
void *hdr;
|
|
|
|
size_t hdrlen;
|
|
|
|
|
|
|
|
switch (format) {
|
2002-02-10 18:30:53 +03:00
|
|
|
case AUDIO_FORMAT_DEFAULT:
|
2002-01-15 11:19:36 +03:00
|
|
|
case AUDIO_FORMAT_SUN:
|
|
|
|
if (write_header_sun(&hdr, &hdrlen, &left) != 0)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case AUDIO_FORMAT_WAV:
|
|
|
|
if (write_header_wav(&hdr, &hdrlen, &left) != 0)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case AUDIO_FORMAT_NONE:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
errx(1, "unknown audio format");
|
|
|
|
}
|
|
|
|
|
|
|
|
veclen = 0;
|
|
|
|
tlen = 0;
|
|
|
|
|
|
|
|
if (hdrlen != 0) {
|
|
|
|
iv[veclen].iov_base = hdr;
|
|
|
|
iv[veclen].iov_len = hdrlen;
|
|
|
|
tlen += iv[veclen++].iov_len;
|
|
|
|
}
|
1999-03-26 17:02:39 +03:00
|
|
|
if (header_info) {
|
|
|
|
iv[veclen].iov_base = header_info;
|
2002-01-15 11:19:36 +03:00
|
|
|
iv[veclen].iov_len = (int)strlen(header_info) + 1;
|
1999-03-26 17:02:39 +03:00
|
|
|
tlen += iv[veclen++].iov_len;
|
|
|
|
}
|
|
|
|
if (left) {
|
|
|
|
iv[veclen].iov_base = default_info;
|
|
|
|
iv[veclen].iov_len = left;
|
|
|
|
tlen += iv[veclen++].iov_len;
|
|
|
|
}
|
|
|
|
|
2002-01-15 11:19:36 +03:00
|
|
|
if (tlen == 0)
|
|
|
|
return;
|
|
|
|
|
1999-03-26 17:02:39 +03:00
|
|
|
if (writev(outfd, iv, veclen) != tlen)
|
|
|
|
err(1, "could not write audio header");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rewrite_header()
|
|
|
|
{
|
|
|
|
|
|
|
|
/* can't do this here! */
|
|
|
|
if (outfd == STDOUT_FILENO)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (lseek(outfd, SEEK_SET, 0) < 0)
|
|
|
|
err(1, "could not seek to start of file for header rewrite");
|
|
|
|
write_header();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2001-02-20 02:03:42 +03:00
|
|
|
|
|
|
|
fprintf(stderr, "Usage: %s [-afhqV] [options] {files ...|-}\n",
|
|
|
|
getprogname());
|
1999-03-27 21:16:23 +03:00
|
|
|
fprintf(stderr, "Options:\n\t"
|
2002-01-15 11:19:36 +03:00
|
|
|
"-F format\n\t"
|
1999-03-27 21:16:23 +03:00
|
|
|
"-b balance (0-63)\n\t"
|
|
|
|
"-c channels\n\t"
|
|
|
|
"-d audio device\n\t"
|
|
|
|
"-e encoding\n\t"
|
|
|
|
"-i header information\n\t"
|
|
|
|
"-m monitor volume\n\t"
|
2001-05-02 16:49:41 +04:00
|
|
|
"-P precision bits (4, 8, 16, 24 or 32)\n\t"
|
1999-03-27 21:16:23 +03:00
|
|
|
"-p input port\n\t"
|
|
|
|
"-s sample rate\n\t"
|
|
|
|
"-t recording time\n\t"
|
|
|
|
"-v volume\n");
|
1999-11-08 13:21:20 +03:00
|
|
|
exit(EXIT_FAILURE);
|
1999-03-26 17:02:39 +03:00
|
|
|
}
|