Reorganized options and added support for pcm_multi

This commit is contained in:
Abramo Bagnara 2000-06-21 15:05:06 +00:00
parent 2c3d1a0639
commit c410bf9a62

View file

@ -59,28 +59,27 @@ static ssize_t (*readv_func)(snd_pcm_t *handle, const struct iovec *vector, unsi
static ssize_t (*writev_func)(snd_pcm_t *handle, const struct iovec *vector, unsigned long count); static ssize_t (*writev_func)(snd_pcm_t *handle, const struct iovec *vector, unsigned long count);
static char *command; static char *command;
static snd_pcm_t *pcm_handle; static snd_pcm_t *handle;
static snd_pcm_stream_info_t cinfo; static snd_pcm_info_t cinfo;
static snd_pcm_format_t rformat, format; static snd_pcm_format_t rformat, format;
static snd_pcm_stream_setup_t setup; static snd_pcm_setup_t setup;
static int timelimit = 0; static int timelimit = 0;
static int quiet_mode = 0; static int quiet_mode = 0;
static int verbose_mode = 0; static int file_type = FORMAT_DEFAULT;
static int active_format = FORMAT_DEFAULT;
static int mode = SND_PCM_MODE_FRAGMENT; static int mode = SND_PCM_MODE_FRAGMENT;
static int direction = SND_PCM_OPEN_PLAYBACK; static int open_mode = 0;
static int stream = SND_PCM_STREAM_PLAYBACK; static int stream = SND_PCM_STREAM_PLAYBACK;
static int mmap_flag = 0; static int mmap_flag = 0;
static snd_pcm_mmap_control_t *mmap_control = NULL; static snd_pcm_mmap_control_t *mmap_control = NULL;
static snd_pcm_mmap_status_t *mmap_status = NULL; static snd_pcm_mmap_status_t *mmap_status = NULL;
static char *mmap_data = NULL; static char *mmap_data = NULL;
static int noplugin = 0;
static int nonblock = 0; static int nonblock = 0;
static char *audiobuf = NULL; static char *audiobuf = NULL;
static int align = 1; static int align = 1;
static int buffer_size = -1; static int buffer_size = -1;
/* 250 ms */ static int frag_length = 125;
static int frag_length = 250; static int buffer_length = 500;
static int min_avail = 50;
static int show_setup = 0; static int show_setup = 0;
static int buffer_pos = 0; static int buffer_pos = 0;
static size_t bits_per_sample, bits_per_frame; static size_t bits_per_sample, bits_per_frame;
@ -152,11 +151,11 @@ static void check_new_format(snd_pcm_format_t * format)
} }
if (format->channels > 1) { if (format->channels > 1) {
if (format->interleave) { if (format->interleave) {
if (!(cinfo.flags & SND_PCM_STREAM_INFO_INTERLEAVE)) { if (!(cinfo.flags & SND_PCM_INFO_INTERLEAVE)) {
fprintf(stderr, "%s: unsupported interleaved format\n", command); fprintf(stderr, "%s: unsupported interleaved format\n", command);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else if (!(cinfo.flags & SND_PCM_STREAM_INFO_NONINTERLEAVE)) { } else if (!(cinfo.flags & SND_PCM_INFO_NONINTERLEAVE)) {
fprintf(stderr, "%s: unsupported non interleaved format\n", command); fprintf(stderr, "%s: unsupported non interleaved format\n", command);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -166,40 +165,41 @@ static void check_new_format(snd_pcm_format_t * format)
static void usage(char *command) static void usage(char *command)
{ {
int k; int k;
fprintf(stderr, fprintf(stderr, "\
"Usage: %s [switches] [filename] <filename> ...\n" Usage: %s [OPTION]... [FILE]...
"Available switches:\n"
"\n" --help help
" -h,--help help\n" --version print current version
" -V,--version print current version\n" --list-devices list all soundcards and digital audio devices
" -l list all soundcards and digital audio devices\n" -C, --card=# select card # or card id (0-%i), defaults to 0
" -c <card> select card # or card id (0-%i), defaults to 0\n" -D, --device=# select device #, defaults to 0
" -d <device> select device #, defaults to 0\n" -S, --subdevice=# select subdevice #, defaults to first available
" -q quiet mode\n" -P, --direct don't use plugins for this PCM
" -v file format Voc\n" -H, --pcm-channels=# channels for last specified PCM
" -w file format Wave\n" -h, --bind-channel=C,S bind stream channel C to PCM channel S
" -r file format Raw\n" -q, --quiet quiet mode
" -o <channels> channels (1-N)\n" -t, --file-type TYPE file type (voc, wav or raw)
" -s <Hz> speed (Hz)\n" -c, --channels=# channels
" -f <format> data format\n" -f, --format=FORMAT sample format (case insensitive)
" -F <msec> fragment length\n" -r, --rate=# sample rate
" -m set CD-ROM quality (44100Hz,stereo,16-bit linear,little endian)\n" -d, --duration=# interrupt after # seconds
" -M set DAT quality (48000Hz,stereo,16-bit linear,little endian)\n" -e, --frame-mode use frame mode instead of default fragment mode
" -t <secs> timelimit (seconds)\n" -M, --mmap mmap stream
" -e frames mode\n" -Q, --multi-direct don't use plugins on top of multi
" -E mmap mode\n" -N, --nonblock nonblocking mode
" -N Don't use plugins\n" -F, --fragment-length=# fragment length is # milliseconds
" -B Nonblocking mode\n" -B, --buffer-length=# buffer length is # milliseconds
" -I Noninterleaved mode\n" -A, --min-avail=# min available space for wakeup is # milliseconds
" -S Show setup\n" -v, --show-setup show actual setup
,command, snd_cards()-1); -I, --separate-channels one file for each channel\
fprintf(stderr, "\nRecognized data formats are:"); ", command, snd_cards()-1);
fprintf(stderr, "\nRecognized sample formats are:");
for (k = 0; k < 32; ++k) { for (k = 0; k < 32; ++k) {
const char *s = snd_pcm_get_format_name(k); const char *s = snd_pcm_get_format_name(k);
if (s) if (s)
fprintf(stderr, " %s", s); fprintf(stderr, " %s", s);
} }
fprintf(stderr, " (some of these may not be available on selected hardware)\n"); fprintf(stderr, "\nSome of these may not be available on selected hardware\n");
} }
static void device_list(void) static void device_list(void)
@ -209,7 +209,6 @@ static void device_list(void)
unsigned int mask; unsigned int mask;
snd_ctl_hw_info_t info; snd_ctl_hw_info_t info;
snd_pcm_info_t pcminfo; snd_pcm_info_t pcminfo;
snd_pcm_stream_info_t stream_info;
mask = snd_cards_mask(); mask = snd_cards_mask();
if (!mask) { if (!mask) {
@ -229,7 +228,10 @@ static void device_list(void)
continue; continue;
} }
for (dev = 0; dev < info.pcmdevs; dev++) { for (dev = 0; dev < info.pcmdevs; dev++) {
if ((err = snd_ctl_pcm_info(handle, dev, &pcminfo)) < 0) { pcminfo.device = dev;
pcminfo.stream = stream;
pcminfo.subdevice = -1;
if ((err = snd_ctl_pcm_info(handle, &pcminfo)) < 0) {
fprintf(stderr, "Error: control digital audio info (%i): %s\n", card, snd_strerror(err)); fprintf(stderr, "Error: control digital audio info (%i): %s\n", card, snd_strerror(err));
continue; continue;
} }
@ -239,32 +241,13 @@ static void device_list(void)
info.id, info.id,
dev, dev,
pcminfo.name); pcminfo.name);
fprintf(stderr, " Directions: %s%s%s\n", fprintf(stderr, " Subdevices: %i/%i\n", pcminfo.subdevices_avail, pcminfo.subdevices_count);
pcminfo.flags & SND_PCM_INFO_PLAYBACK ? "playback " : "", for (idx = 0; idx < pcminfo.subdevices_count; idx++) {
pcminfo.flags & SND_PCM_INFO_CAPTURE ? "capture " : "", pcminfo.subdevice = idx;
pcminfo.flags & SND_PCM_INFO_DUPLEX ? "duplex " : ""); if ((err = snd_ctl_pcm_info(handle, &pcminfo)) < 0) {
fprintf(stderr, " Playback subdevices: %i\n", pcminfo.playback + 1);
fprintf(stderr, " Capture subdevices: %i\n", pcminfo.capture + 1);
if (pcminfo.flags & SND_PCM_INFO_PLAYBACK) {
for (idx = 0; idx <= pcminfo.playback; idx++) {
memset(&stream_info, 0, sizeof(stream_info));
stream_info.stream = SND_PCM_STREAM_PLAYBACK;
if ((err = snd_ctl_pcm_stream_info(handle, dev, SND_PCM_STREAM_PLAYBACK, idx, &stream_info)) < 0) {
fprintf(stderr, "Error: control digital audio playback info (%i): %s\n", card, snd_strerror(err)); fprintf(stderr, "Error: control digital audio playback info (%i): %s\n", card, snd_strerror(err));
} else { } else {
fprintf(stderr, " Playback subdevice #%i: %s\n", idx, stream_info.subname); fprintf(stderr, " Subdevice #%i: %s\n", idx, pcminfo.subname);
}
}
}
if (pcminfo.flags & SND_PCM_INFO_CAPTURE) {
for (idx = 0; idx <= pcminfo.capture; idx++) {
memset(&stream_info, 0, sizeof(stream_info));
stream_info.stream = SND_PCM_STREAM_CAPTURE;
if ((err = snd_ctl_pcm_stream_info(handle, dev, SND_PCM_STREAM_CAPTURE, 0, &stream_info)) < 0) {
fprintf(stderr, "Error: control digital audio capture info (%i): %s\n", card, snd_strerror(err));
} else {
fprintf(stderr, " Capture subdevice #%i: %s\n", idx, stream_info.subname);
}
} }
} }
} }
@ -277,23 +260,92 @@ static void version(void)
fprintf(stderr, "%s: version " SND_UTIL_VERSION_STR " by Jaroslav Kysela <perex@suse.cz>\n", command); fprintf(stderr, "%s: version " SND_UTIL_VERSION_STR " by Jaroslav Kysela <perex@suse.cz>\n", command);
} }
#define OPT_HELP 1
#define OPT_VERSION 2
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int card, dev, tmp, err, c; int option_index;
char *short_options = "lC:D:S:H:h:qt:c:f:r:d:eMPQA:B:F:NvI";
static struct option long_options[] = {
{"help", 0, 0, OPT_HELP},
{"version", 0, 0, OPT_VERSION},
{"list-devices", 0, 0, 'l'},
{"card", 1, 0, 'C'},
{"device", 1, 0, 'D'},
{"subdevice", 1, 0, 'S'},
{"pcm-channels", 1, 0, 'H'},
{"bind-channel", 1, 0, 'h'},
{"quiet", 0, 0, 'q'},
{"file-type", 1, 0, 't'},
{"channels", 1, 0, 'c'},
{"format", 1, 0, 'f'},
{"rate", 1, 0, 'r'},
{"duration", 1, 0 ,'d'},
{"frame-mode", 0, 0, 'e'},
{"mmap", 0, 0, 'M'},
{"direct", 0, 0, 'P'},
{"multi-direct", 0, 0, 'Q'},
{"nonblock", 0, 0, 'N'},
{"fragment-length", 1, 0, 'F'},
{"buffer-length", 1, 0, 'B'},
{"min-avail", 1, 0, 'A'},
{"show-setup", 0, 0, 'v'},
{"separate-channels", 0, 0, 'I'},
{0, 0, 0, 0}
};
int binds_pcm[32];
int binds_client_channel[32];
int binds_slave_channel[32];
int pcms_card[32];
int pcms_dev[32];
int pcms_subdev[32];
int pcms_channels[32];
int pcms_direct[32];
int direct = 0;
int multi_direct = 0;
int pcm_card = 0, pcm_dev = 0, pcm_subdev = -1, pcm_channels = -1;
int tmp, err, c, client_channel, slave_channel;
int pcm;
int pcms_count = 0, binds_count = 0;
int multi;
char *ptr, *beg;
int get_pcm() {
int pcm;
if (pcm_channels < 0)
pcm_channels = rformat.channels;
for (pcm = 0; pcm < pcms_count; ++pcm) {
if (pcms_card[pcm] == pcm_card &&
pcms_dev[pcm] == pcm_dev &&
pcms_subdev[pcm] == pcm_subdev)
break;
}
if (pcm == pcms_count) {
pcms_card[pcm] = pcm_card;
pcms_dev[pcm] = pcm_dev;
pcms_subdev[pcm] = pcm_subdev;
pcms_channels[pcm] = pcm_channels;
pcms_direct[pcm] = direct;
direct = 0;
pcms_count++;
} else if (pcm_channels != pcms_channels[pcm]) {
fprintf(stderr, "Error: different channels count specified for the same pcm\n");
exit(1);
}
return pcm;
}
card = 0;
dev = 0;
command = argv[0]; command = argv[0];
active_format = FORMAT_DEFAULT; file_type = FORMAT_DEFAULT;
if (strstr(argv[0], "arecord")) { if (strstr(argv[0], "arecord")) {
direction = SND_PCM_OPEN_CAPTURE;
stream = SND_PCM_STREAM_CAPTURE; stream = SND_PCM_STREAM_CAPTURE;
active_format = FORMAT_WAVE; file_type = FORMAT_WAVE;
command = "Arecord"; command = "arecord";
} else if (strstr(argv[0], "aplay")) { } else if (strstr(argv[0], "aplay")) {
direction = SND_PCM_OPEN_PLAYBACK;
stream = SND_PCM_STREAM_PLAYBACK; stream = SND_PCM_STREAM_PLAYBACK;
command = "Aplay"; command = "aplay";
} else { } else {
fprintf(stderr, "Error: command should be named either arecord or aplay\n"); fprintf(stderr, "Error: command should be named either arecord or aplay\n");
return 1; return 1;
@ -306,57 +358,101 @@ int main(int argc, char *argv[])
rformat.rate = DEFAULT_SPEED; rformat.rate = DEFAULT_SPEED;
rformat.channels = 1; rformat.channels = 1;
if (argc > 1 && !strcmp(argv[1], "--help")) { while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
usage(command);
return 0;
}
if (argc > 1 && !strcmp(argv[1], "--version")) {
version();
return 0;
}
while ((c = getopt(argc, argv, "hlc:d:qs:o:t:vrwxc:p:mMVeEf:NBF:SI")) != EOF)
switch (c) { switch (c) {
case 'h': case OPT_HELP:
usage(command); usage(command);
return 0; return 0;
case OPT_VERSION:
version();
return 0;
case 'l': case 'l':
device_list(); device_list();
return 0; return 0;
case 'c': case 'C':
card = snd_card_name(optarg); pcm_card = snd_card_name(optarg);
if (card < 0) { if (pcm_card < 0) {
fprintf(stderr, "Error: soundcard '%s' not found\n", optarg); fprintf(stderr, "Error: soundcard '%s' not found\n", optarg);
return 1; return 1;
} }
break; break;
case 'd': case 'D':
dev = atoi(optarg); pcm_dev = atoi(optarg);
if (dev < 0 || dev > 32) { if (pcm_dev < 0 || pcm_dev > 32) {
fprintf(stderr, "Error: device %i is invalid\n", dev); fprintf(stderr, "Error: device %i is invalid\n", pcm_dev);
return 1; return 1;
} }
break; break;
case 'o': case 'S':
tmp = atoi(optarg); pcm_subdev = atoi(optarg);
if (tmp < 1 || tmp > 32) { if (pcm_subdev < 0 || pcm_subdev > 32) {
fprintf(stderr, "Error: value %i for channels is invalid\n", tmp); fprintf(stderr, "Error: subdevice %i is invalid\n", pcm_subdev);
return 1; return 1;
} }
rformat.channels = tmp; break;
case 'H':
pcm_channels = atoi(optarg);
if (pcm_channels < 1 || pcm_channels > 32) {
fprintf(stderr, "Error: value %i for channels is invalid\n", pcm_channels);
return 1;
}
break;
case 'h':
client_channel = strtol(optarg, &ptr, 10);
if (*ptr != ',' || ptr == optarg) {
fprintf(stderr, "Error: invalid channel binding syntax\n");
return 1;
}
beg = ptr + 1;
slave_channel = strtol(beg, &ptr, 10);
if (*ptr || ptr == optarg) {
fprintf(stderr, "Error: invalid channel binding syntax\n");
return 1;
}
if (client_channel >= rformat.channels) {
fprintf(stderr, "Error: attempt to bind unavailable stream channel %d\n", client_channel);
return 1;
}
if (slave_channel >= pcm_channels) {
fprintf(stderr, "Error: attempt to bind to an unavailable PCM channel %d\n", slave_channel);
return 1;
}
pcm = get_pcm();
binds_pcm[binds_count] = pcm;
binds_client_channel[binds_count] = client_channel;
binds_slave_channel[binds_count] = slave_channel;
++binds_count;
break; break;
case 'q': case 'q':
quiet_mode = 1; quiet_mode = 1;
break; break;
case 't':
if (strcasecmp(optarg, "raw") == 0)
file_type = FORMAT_RAW;
else if (strcasecmp(optarg, "voc") == 0)
file_type = FORMAT_VOC;
else if (strcasecmp(optarg, "wav") == 0)
file_type = FORMAT_WAVE;
else {
fprintf(stderr, "Error: unrecognized file format %s\n", optarg);
return 1;
}
break;
case 'c':
rformat.channels = atoi(optarg);
if (rformat.channels < 1 || rformat.channels > 32) {
fprintf(stderr, "Error: value %i for channels is invalid\n", rformat.channels);
return 1;
}
break;
case 'f':
rformat.format = snd_pcm_get_format_value(optarg);
if (rformat.format < 0) {
fprintf(stderr, "Error: wrong extended format '%s'\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'r': case 'r':
active_format = FORMAT_RAW;
break;
case 'v':
active_format = FORMAT_VOC;
break;
case 'w':
active_format = FORMAT_WAVE;
break;
case 's':
tmp = atoi(optarg); tmp = atoi(optarg);
if (tmp < 300) if (tmp < 300)
tmp *= 1000; tmp *= 1000;
@ -366,79 +462,128 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
break; break;
case 't': case 'd':
timelimit = atoi(optarg); timelimit = atoi(optarg);
break; break;
case 'x':
verbose_mode = 1;
quiet_mode = 0;
break;
case 'f':
rformat.format = snd_pcm_get_format_value(optarg);
if (rformat.format < 0) {
fprintf(stderr, "Error: wrong extended format '%s'\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'm':
case 'M':
rformat.format = SND_PCM_SFMT_S16_LE;
rformat.rate = c == 'M' ? 48000 : 44100;
rformat.channels = 2;
break;
case 'V':
version();
return 0;
case 'e': case 'e':
mode = SND_PCM_MODE_FRAME; mode = SND_PCM_MODE_FRAME;
break; break;
case 'E': case 'M':
mmap_flag = 1; mmap_flag = 1;
break; break;
case 'N': case 'P':
noplugin = 1; direct = 1;
break; break;
case 'B': case 'Q':
multi_direct = 1;
break;
case 'N':
nonblock = 1; nonblock = 1;
open_mode |= SND_PCM_NONBLOCK;
break; break;
case 'F': case 'F':
frag_length = atoi(optarg); frag_length = atoi(optarg);
break; break;
case 'S': case 'B':
buffer_length = atoi(optarg);
break;
case 'A':
min_avail = atoi(optarg);
break;
case 'v':
show_setup = 1; show_setup = 1;
break; break;
case 'I': case 'I':
rformat.interleave = 0; rformat.interleave = 0;
break; break;
default: default:
usage(command); fprintf(stderr, "Try `%s --help' for more information.\n", command);
return 1; return 1;
} }
}
if (binds_count == 0) {
pcm = get_pcm();
for (c = 0; c < rformat.channels; ++c) {
if (c > pcm_channels) {
fprintf(stderr, "Error: attempt to bind to an unavailable PCM channel %d\n", c);
return 1;
}
binds_pcm[binds_count] = pcm;
binds_client_channel[binds_count] = c;
binds_slave_channel[binds_count] = c;
binds_count++;
}
}
if (!quiet_mode) if (!quiet_mode)
version(); version();
if (!quiet_mode) { assert(pcms_count > 0);
char *cardname;
if ((err = snd_card_get_longname(card, &cardname)) < 0) { multi = 0;
if (pcms_count != 1 || rformat.channels != binds_count ||
pcm_channels != binds_count)
multi = 1;
else {
char mask[binds_count];
memset(mask, 0, sizeof(mask));
for (c = 0; c < binds_count; ++c) {
if (binds_client_channel[c] != binds_slave_channel[c]) {
multi = 1;
break;
}
if (mask[c]) {
multi = 1;
break;
}
mask[c] = 1;
}
}
if (!quiet_mode) {
for (pcm = 0; pcm < pcms_count; ++pcm) {
char *cardname;
if ((err = snd_card_get_longname(pcms_card[pcm], &cardname)) < 0) {
fprintf(stderr, "Error: unable to obtain longname: %s\n", snd_strerror(err)); fprintf(stderr, "Error: unable to obtain longname: %s\n", snd_strerror(err));
return 1; return 1;
} }
fprintf(stderr, "Using soundcard '%s'\n", cardname); fprintf(stderr, "Using soundcard '%s'\n", cardname);
free(cardname); free(cardname);
} }
}
if (noplugin) {
err = snd_pcm_open(&pcm_handle, card, dev, direction); snd_pcm_t *handles[pcms_count];
for (pcm = 0; pcm < pcms_count; ++pcm) {
if (pcms_direct[pcm])
err = snd_pcm_hw_open_subdevice(&handles[pcm], pcms_card[pcm], pcms_dev[pcm], pcms_subdev[pcm], stream, open_mode);
else else
err = snd_pcm_plug_open(&pcm_handle, card, dev, direction); err = snd_pcm_plug_open_subdevice(&handles[pcm], pcms_card[pcm], pcms_dev[pcm], pcms_subdev[pcm], stream, open_mode);
if (err < 0) { if (err < 0) {
fprintf(stderr, "Error: audio open error: %s\n", snd_strerror(err)); fprintf(stderr, "Error: audio open error: %s\n", snd_strerror(err));
return 1; return 1;
} }
}
if (multi) {
err = snd_pcm_multi_create(&handle, pcms_count, handles, pcms_channels, binds_count, binds_client_channel, binds_pcm, binds_slave_channel, 1);
if (err < 0) {
fprintf(stderr, "Error: audio open error: %s\n", snd_strerror(err));
return 1;
}
if (!multi_direct) {
snd_pcm_t *h = handle;
err = snd_pcm_plug_create(&handle, h, 1);
if (err < 0) {
fprintf(stderr, "Error: audio open error: %s\n", snd_strerror(err));
return 1;
}
}
} else {
handle = handles[0];
}
}
if (nonblock) { if (nonblock) {
err = snd_pcm_stream_nonblock(pcm_handle, stream, 1); err = snd_pcm_nonblock(handle, 1);
if (err < 0) { if (err < 0) {
fprintf(stderr, "nonblock setting error: %s\n", snd_strerror(err)); fprintf(stderr, "nonblock setting error: %s\n", snd_strerror(err));
return 1; return 1;
@ -446,7 +591,7 @@ int main(int argc, char *argv[])
} }
memset(&cinfo, 0, sizeof(cinfo)); memset(&cinfo, 0, sizeof(cinfo));
cinfo.stream = stream; cinfo.stream = stream;
if ((err = snd_pcm_stream_info(pcm_handle, &cinfo)) < 0) { if ((err = snd_pcm_info(handle, &cinfo)) < 0) {
fprintf(stderr, "Error: stream info error: %s\n", snd_strerror(err)); fprintf(stderr, "Error: stream info error: %s\n", snd_strerror(err));
return 1; return 1;
} }
@ -492,7 +637,7 @@ int main(int argc, char *argv[])
else else
capturev(&argv[optind], argc - optind); capturev(&argv[optind], argc - optind);
} }
snd_pcm_close(pcm_handle); snd_pcm_close(handle);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -614,19 +759,18 @@ static int test_au(int fd, void *buffer)
static void set_params(void) static void set_params(void)
{ {
snd_pcm_stream_params_t params; snd_pcm_params_t params;
align = (snd_pcm_format_physical_width(format.format) + 7) / 8; align = (snd_pcm_format_physical_width(format.format) + 7) / 8;
#if 0 #if 0
if (mmap_flag) if (mmap_flag)
snd_pcm_munmap(pcm_handle, stream); snd_pcm_munmap(handle, stream);
#endif #endif
snd_pcm_stream_flush(pcm_handle, stream); /* to be in right state */ snd_pcm_flush(handle); /* to be in right state */
memset(&params, 0, sizeof(params)); memset(&params, 0, sizeof(params));
params.mode = mode; params.mode = mode;
params.stream = stream;
params.format = format; params.format = format;
if (stream == SND_PCM_STREAM_PLAYBACK) { if (stream == SND_PCM_STREAM_PLAYBACK) {
params.start_mode = SND_PCM_START_FULL; params.start_mode = SND_PCM_START_FULL;
@ -635,35 +779,34 @@ static void set_params(void)
} }
params.xrun_mode = SND_PCM_XRUN_FLUSH; params.xrun_mode = SND_PCM_XRUN_FLUSH;
params.frag_size = format.rate * frag_length / 1000; params.frag_size = format.rate * frag_length / 1000;
params.buffer_size = params.frag_size * 4; params.buffer_size = format.rate * buffer_length / 1000;
params.frames_min = format.rate / 16; params.frames_min = format.rate * min_avail / 1000;
params.frames_xrun_max = 0; params.frames_xrun_max = 0;
params.fill_mode = SND_PCM_FILL_SILENCE; params.fill_mode = SND_PCM_FILL_SILENCE;
params.frames_fill_max = 1024; params.frames_fill_max = 1024;
params.frames_xrun_max = 0; params.frames_xrun_max = 0;
if (snd_pcm_stream_params(pcm_handle, &params) < 0) { if (snd_pcm_params(handle, &params) < 0) {
fprintf(stderr, "%s: unable to set stream params\n", command); fprintf(stderr, "%s: unable to set stream params\n", command);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (mmap_flag) { if (mmap_flag) {
if (snd_pcm_mmap(pcm_handle, stream, &mmap_status, &mmap_control, (void **)&mmap_data)<0) { if (snd_pcm_mmap(handle, &mmap_status, &mmap_control, (void **)&mmap_data)<0) {
fprintf(stderr, "%s: unable to mmap memory\n", command); fprintf(stderr, "%s: unable to mmap memory\n", command);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (snd_pcm_stream_prepare(pcm_handle, stream) < 0) { if (snd_pcm_prepare(handle) < 0) {
fprintf(stderr, "%s: unable to prepare stream\n", command); fprintf(stderr, "%s: unable to prepare stream\n", command);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
memset(&setup, 0, sizeof(setup)); memset(&setup, 0, sizeof(setup));
setup.stream = stream; if (snd_pcm_setup(handle, &setup) < 0) {
if (snd_pcm_stream_setup(pcm_handle, &setup) < 0) {
fprintf(stderr, "%s: unable to obtain setup\n", command); fprintf(stderr, "%s: unable to obtain setup\n", command);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (show_setup) if (show_setup)
snd_pcm_dump_setup(pcm_handle, stream, stderr); snd_pcm_dump_setup(handle, stderr);
buffer_size = setup.frag_size; buffer_size = setup.frag_size;
bits_per_sample = snd_pcm_format_physical_width(setup.format.format); bits_per_sample = snd_pcm_format_physical_width(setup.format.format);
@ -681,17 +824,16 @@ static void set_params(void)
void playback_underrun(void) void playback_underrun(void)
{ {
snd_pcm_stream_status_t status; snd_pcm_status_t status;
memset(&status, 0, sizeof(status)); memset(&status, 0, sizeof(status));
status.stream = stream; if (snd_pcm_status(handle, &status)<0) {
if (snd_pcm_stream_status(pcm_handle, &status)<0) {
fprintf(stderr, "playback stream status error\n"); fprintf(stderr, "playback stream status error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (status.state == SND_PCM_STATE_XRUN) { if (status.state == SND_PCM_STATE_XRUN) {
fprintf(stderr, "underrun at position %u!!!\n", status.frame_io); fprintf(stderr, "underrun at position %u!!!\n", status.frame_io);
if (snd_pcm_stream_prepare(pcm_handle, SND_PCM_STREAM_PLAYBACK)<0) { if (snd_pcm_prepare(handle)<0) {
fprintf(stderr, "underrun: playback stream prepare error\n"); fprintf(stderr, "underrun: playback stream prepare error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -705,11 +847,10 @@ void playback_underrun(void)
void capture_overrun(void) void capture_overrun(void)
{ {
snd_pcm_stream_status_t status; snd_pcm_status_t status;
memset(&status, 0, sizeof(status)); memset(&status, 0, sizeof(status));
status.stream = stream; if (snd_pcm_status(handle, &status)<0) {
if (snd_pcm_stream_status(pcm_handle, &status)<0) {
fprintf(stderr, "capture stream status error\n"); fprintf(stderr, "capture stream status error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -717,7 +858,7 @@ void capture_overrun(void)
return; /* everything is ok, but the driver is waiting for data */ return; /* everything is ok, but the driver is waiting for data */
if (status.state == SND_PCM_STATE_XRUN) { if (status.state == SND_PCM_STATE_XRUN) {
fprintf(stderr, "overrun at position %u!!!\n", status.frame_io); fprintf(stderr, "overrun at position %u!!!\n", status.frame_io);
if (snd_pcm_stream_prepare(pcm_handle, SND_PCM_STREAM_CAPTURE)<0) { if (snd_pcm_prepare(handle)<0) {
fprintf(stderr, "overrun: capture stream prepare error\n"); fprintf(stderr, "overrun: capture stream prepare error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -742,10 +883,10 @@ static ssize_t pcm_write(u_char *data, size_t count)
count = buffer_size; count = buffer_size;
} }
while (count > 0) { while (count > 0) {
r = write_func(pcm_handle, data, count); r = write_func(handle, data, count);
if (r == -EAGAIN || (r >= 0 && r < count)) { if (r == -EAGAIN || (r >= 0 && r < count)) {
struct pollfd pfd; struct pollfd pfd;
pfd.fd = snd_pcm_file_descriptor(pcm_handle, SND_PCM_STREAM_PLAYBACK); pfd.fd = snd_pcm_file_descriptor(handle);
pfd.events = POLLOUT | POLLERR; pfd.events = POLLOUT | POLLERR;
poll(&pfd, 1, 1000); poll(&pfd, 1, 1000);
} else if (r == -EPIPE) { } else if (r == -EPIPE) {
@ -786,10 +927,10 @@ static ssize_t pcm_writev(u_char **data, unsigned int channels, size_t count)
vec[channel].iov_base = data[channel] + offset * bits_per_sample / 8; vec[channel].iov_base = data[channel] + offset * bits_per_sample / 8;
vec[channel].iov_len = remaining; vec[channel].iov_len = remaining;
} }
r = writev_func(pcm_handle, vec, channels); r = writev_func(handle, vec, channels);
if (r == -EAGAIN || (r >= 0 && r < count)) { if (r == -EAGAIN || (r >= 0 && r < count)) {
struct pollfd pfd; struct pollfd pfd;
pfd.fd = snd_pcm_file_descriptor(pcm_handle, SND_PCM_STREAM_PLAYBACK); pfd.fd = snd_pcm_file_descriptor(handle);
pfd.events = POLLOUT | POLLERR; pfd.events = POLLOUT | POLLERR;
poll(&pfd, 1, 1000); poll(&pfd, 1, 1000);
} else if (r == -EPIPE) { } else if (r == -EPIPE) {
@ -816,10 +957,10 @@ static ssize_t pcm_read(u_char *data, size_t count)
size_t result = 0; size_t result = 0;
while (count > 0) { while (count > 0) {
r = read_func(pcm_handle, data, count); r = read_func(handle, data, count);
if (r == -EAGAIN || (r >= 0 && r < count)) { if (r == -EAGAIN || (r >= 0 && r < count)) {
struct pollfd pfd; struct pollfd pfd;
pfd.fd = snd_pcm_file_descriptor(pcm_handle, SND_PCM_STREAM_CAPTURE); pfd.fd = snd_pcm_file_descriptor(handle);
pfd.events = POLLIN | POLLERR; pfd.events = POLLIN | POLLERR;
poll(&pfd, 1, 1000); poll(&pfd, 1, 1000);
} else if (r == -EPIPE) { } else if (r == -EPIPE) {
@ -851,10 +992,10 @@ static ssize_t pcm_readv(u_char **data, unsigned int channels, size_t count)
vec[channel].iov_base = data[channel] + offset * bits_per_sample / 8; vec[channel].iov_base = data[channel] + offset * bits_per_sample / 8;
vec[channel].iov_len = remaining; vec[channel].iov_len = remaining;
} }
r = readv_func(pcm_handle, vec, channels); r = readv_func(handle, vec, channels);
if (r == -EAGAIN || (r >= 0 && r < count)) { if (r == -EAGAIN || (r >= 0 && r < count)) {
struct pollfd pfd; struct pollfd pfd;
pfd.fd = snd_pcm_file_descriptor(pcm_handle, SND_PCM_STREAM_CAPTURE); pfd.fd = snd_pcm_file_descriptor(handle);
pfd.events = POLLIN | POLLERR; pfd.events = POLLIN | POLLERR;
poll(&pfd, 1, 1000); poll(&pfd, 1, 1000);
} else if (r == -EPIPE) { } else if (r == -EPIPE) {
@ -934,7 +1075,7 @@ static void voc_pcm_flush(void)
if (pcm_write(audiobuf, b) != b) if (pcm_write(audiobuf, b) != b)
fprintf(stderr, "voc_pcm_flush error\n"); fprintf(stderr, "voc_pcm_flush error\n");
} }
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_PLAYBACK); snd_pcm_flush(handle);
} }
static void voc_play(int fd, int ofs, char *name) static void voc_play(int fd, int ofs, char *name)
@ -1403,7 +1544,7 @@ void playback_go(int fd, size_t loaded, size_t count, int rtype, char *name)
written += r; written += r;
l = 0; l = 0;
} }
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_PLAYBACK); snd_pcm_flush(handle);
} }
/* captureing raw data, this proc handels WAVE files and .VOCs (as one block) */ /* captureing raw data, this proc handels WAVE files and .VOCs (as one block) */
@ -1440,7 +1581,7 @@ static void playback(char *name)
{ {
int fd, ofs; int fd, ofs;
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_PLAYBACK); snd_pcm_flush(handle);
if (!name || !strcmp(name, "-")) { if (!name || !strcmp(name, "-")) {
fd = 0; fd = 0;
name = "stdin"; name = "stdin";
@ -1496,7 +1637,7 @@ static void capture(char *name)
{ {
int fd; int fd;
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_CAPTURE); snd_pcm_flush(handle);
if (!name || !strcmp(name, "-")) { if (!name || !strcmp(name, "-")) {
fd = 1; fd = 1;
name = "stdout"; name = "stdout";
@ -1510,11 +1651,11 @@ static void capture(char *name)
count = calc_count() & 0xFFFFFFFE; count = calc_count() & 0xFFFFFFFE;
/* WAVE-file should be even (I'm not sure), but wasting one byte /* WAVE-file should be even (I'm not sure), but wasting one byte
isn't a problem (this can only be in 8 bit mono) */ isn't a problem (this can only be in 8 bit mono) */
if (fmt_rec_table[active_format].start) if (fmt_rec_table[file_type].start)
fmt_rec_table[active_format].start(fd, count); fmt_rec_table[file_type].start(fd, count);
check_new_format(&rformat); check_new_format(&rformat);
capture_go(fd, count, active_format, name); capture_go(fd, count, file_type, name);
fmt_rec_table[active_format].end(fd); fmt_rec_table[file_type].end(fd);
} }
void playbackv_go(int* fds, unsigned int channels, size_t loaded, size_t count, int rtype, char **names) void playbackv_go(int* fds, unsigned int channels, size_t loaded, size_t count, int rtype, char **names)
@ -1563,7 +1704,7 @@ void playbackv_go(int* fds, unsigned int channels, size_t loaded, size_t count,
r = r * bits_per_frame / 8; r = r * bits_per_frame / 8;
count -= r; count -= r;
} }
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_PLAYBACK); snd_pcm_flush(handle);
} }
void capturev_go(int* fds, unsigned int channels, size_t count, int rtype, char **names) void capturev_go(int* fds, unsigned int channels, size_t count, int rtype, char **names)
@ -1612,7 +1753,7 @@ static void playbackv(char **names, unsigned int count)
for (channel = 0; channel < channels; ++channel) for (channel = 0; channel < channels; ++channel)
fds[channel] = -1; fds[channel] = -1;
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_PLAYBACK); snd_pcm_flush(handle);
if (count == 1) { if (count == 1) {
size_t len = strlen(names[0]); size_t len = strlen(names[0]);
char format[1024]; char format[1024];
@ -1667,7 +1808,7 @@ static void capturev(char **names, unsigned int count)
for (channel = 0; channel < channels; ++channel) for (channel = 0; channel < channels; ++channel)
fds[channel] = -1; fds[channel] = -1;
snd_pcm_stream_flush(pcm_handle, SND_PCM_STREAM_CAPTURE); snd_pcm_flush(handle);
if (count == 1) { if (count == 1) {
size_t len = strlen(names[0]); size_t len = strlen(names[0]);
char format[1024]; char format[1024];