1998-08-13 17:43:39 +02:00
|
|
|
/*
|
1999-06-13 12:09:54 +02:00
|
|
|
* aplay.c - plays and records
|
1998-08-13 17:43:39 +02:00
|
|
|
*
|
2000-05-27 18:52:19 +02:00
|
|
|
* CREATIVE LABS CHANNEL-files
|
1998-11-27 16:17:45 +01:00
|
|
|
* Microsoft WAVE-files
|
1998-08-13 17:43:39 +02:00
|
|
|
* SPARC AUDIO .AU-files
|
|
|
|
* Raw Data
|
|
|
|
*
|
1999-05-12 00:18:49 +02:00
|
|
|
* Copyright (c) by Jaroslav Kysela <perex@suse.cz>
|
1998-08-13 17:43:39 +02:00
|
|
|
* Based on vplay program by Michael Beck
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
1999-11-30 10:26:00 +01:00
|
|
|
#include <errno.h>
|
1998-08-30 23:10:35 +02:00
|
|
|
#include <sys/asoundlib.h>
|
2000-04-12 22:44:14 +02:00
|
|
|
#include <assert.h>
|
2000-05-08 20:54:14 +02:00
|
|
|
#include <sys/poll.h>
|
2000-05-18 16:42:43 +02:00
|
|
|
#include <sys/uio.h>
|
1998-08-31 11:21:36 +02:00
|
|
|
#include "aconfig.h"
|
1998-08-13 17:43:39 +02:00
|
|
|
#include "formats.h"
|
1999-01-30 20:12:34 +01:00
|
|
|
#include "version.h"
|
1998-08-13 17:43:39 +02:00
|
|
|
|
|
|
|
#define DEFAULT_SPEED 8000
|
|
|
|
|
|
|
|
#define FORMAT_DEFAULT -1
|
|
|
|
#define FORMAT_RAW 0
|
|
|
|
#define FORMAT_VOC 1
|
|
|
|
#define FORMAT_WAVE 2
|
|
|
|
#define FORMAT_AU 3
|
|
|
|
|
|
|
|
/* global data */
|
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
static ssize_t (*readi_func)(snd_pcm_t *handle, void *buffer, size_t size);
|
|
|
|
static ssize_t (*writei_func)(snd_pcm_t *handle, const void *buffer, size_t size);
|
|
|
|
static ssize_t (*readn_func)(snd_pcm_t *handle, void **bufs, size_t size);
|
|
|
|
static ssize_t (*writen_func)(snd_pcm_t *handle, void **bufs, size_t size);
|
2000-05-08 20:54:14 +02:00
|
|
|
|
|
|
|
static char *command;
|
2000-06-21 17:05:06 +02:00
|
|
|
static snd_pcm_t *handle;
|
|
|
|
static snd_pcm_info_t cinfo;
|
2000-07-01 12:38:30 +02:00
|
|
|
static snd_pcm_params_info_t cpinfo;
|
2000-05-08 20:54:14 +02:00
|
|
|
static snd_pcm_format_t rformat, format;
|
2000-06-21 17:05:06 +02:00
|
|
|
static snd_pcm_setup_t setup;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int timelimit = 0;
|
|
|
|
static int quiet_mode = 0;
|
2000-06-21 17:05:06 +02:00
|
|
|
static int file_type = FORMAT_DEFAULT;
|
2000-09-24 11:57:50 +02:00
|
|
|
static int xrun_mode = SND_PCM_XRUN_FRAGMENT;
|
|
|
|
static int ready_mode = SND_PCM_READY_FRAGMENT;
|
2000-06-21 17:05:06 +02:00
|
|
|
static int open_mode = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
static int stream = SND_PCM_STREAM_PLAYBACK;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int mmap_flag = 0;
|
|
|
|
static char *mmap_data = NULL;
|
|
|
|
static int nonblock = 0;
|
|
|
|
static char *audiobuf = NULL;
|
|
|
|
static int buffer_size = -1;
|
2000-06-21 17:05:06 +02:00
|
|
|
static int frag_length = 125;
|
|
|
|
static int buffer_length = 500;
|
2000-09-24 11:57:50 +02:00
|
|
|
static int avail_min = 50;
|
|
|
|
static int xfer_mode = SND_PCM_XFER_INTERLEAVED;
|
|
|
|
static int xfer_min = 50;
|
|
|
|
static int verbose = 0;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int buffer_pos = 0;
|
2000-06-10 14:39:52 +02:00
|
|
|
static size_t bits_per_sample, bits_per_frame;
|
|
|
|
static size_t buffer_bytes;
|
2000-05-08 20:54:14 +02:00
|
|
|
|
|
|
|
static int count;
|
|
|
|
static int vocmajor, vocminor;
|
1999-11-07 00:47:10 +01:00
|
|
|
|
1998-08-13 17:43:39 +02:00
|
|
|
/* needed prototypes */
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void playback(char *filename);
|
1999-06-13 12:09:54 +02:00
|
|
|
static void capture(char *filename);
|
2000-05-18 16:42:43 +02:00
|
|
|
static void playbackv(char **filenames, unsigned int count);
|
|
|
|
static void capturev(char **filenames, unsigned int count);
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
static void begin_voc(int fd, size_t count);
|
1998-11-27 16:17:45 +01:00
|
|
|
static void end_voc(int fd);
|
2000-05-18 16:42:43 +02:00
|
|
|
static void begin_wave(int fd, size_t count);
|
1998-11-27 16:17:45 +01:00
|
|
|
static void end_wave(int fd);
|
2000-05-18 16:42:43 +02:00
|
|
|
static void begin_au(int fd, size_t count);
|
1998-08-13 17:43:39 +02:00
|
|
|
|
1999-06-13 12:09:54 +02:00
|
|
|
struct fmt_capture {
|
2000-05-18 16:42:43 +02:00
|
|
|
void (*start) (int fd, size_t count);
|
1998-11-27 16:17:45 +01:00
|
|
|
void (*end) (int fd);
|
|
|
|
char *what;
|
1998-08-13 17:43:39 +02:00
|
|
|
} fmt_rec_table[] = {
|
1998-11-27 16:17:45 +01:00
|
|
|
{ NULL, end_wave, "raw data" },
|
|
|
|
{ begin_voc, end_voc, "VOC" },
|
|
|
|
{ begin_wave, end_wave, "WAVE" },
|
|
|
|
{ begin_au, end_wave, "Sparc Audio" }
|
|
|
|
};
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-08-19 11:08:39 +02:00
|
|
|
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
|
|
|
|
#define error(...) do {\
|
|
|
|
fprintf(stderr, "%s: %s:%d: ", command, __FUNCTION__, __LINE__); \
|
|
|
|
fprintf(stderr, __VA_ARGS__); \
|
2000-08-25 16:36:58 +02:00
|
|
|
putc('\n', stderr); \
|
2000-08-19 11:08:39 +02:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define error(args...) do {\
|
|
|
|
fprintf(stderr, "%s: %s:%d: ", command, __FUNCTION__, __LINE__); \
|
|
|
|
fprintf(stderr, ##args); \
|
2000-08-25 16:36:58 +02:00
|
|
|
putc('\n', stderr); \
|
2000-08-19 11:08:39 +02:00
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void check_new_format(snd_pcm_format_t * format)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-07-01 12:38:30 +02:00
|
|
|
if (cpinfo.rates & (SND_PCM_RATE_CONTINUOUS|SND_PCM_RATE_KNOT)) {
|
|
|
|
if (format->rate < cpinfo.min_rate ||
|
|
|
|
format->rate > cpinfo.max_rate) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unsupported rate %iHz (valid range is %iHz-%iHz)", format->rate, cpinfo.min_rate, cpinfo.max_rate);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned int r;
|
|
|
|
switch (format->rate) {
|
|
|
|
case 8000: r = SND_PCM_RATE_8000; break;
|
|
|
|
case 11025: r = SND_PCM_RATE_11025; break;
|
|
|
|
case 16000: r = SND_PCM_RATE_16000; break;
|
|
|
|
case 22050: r = SND_PCM_RATE_22050; break;
|
|
|
|
case 32000: r = SND_PCM_RATE_32000; break;
|
|
|
|
case 44100: r = SND_PCM_RATE_44100; break;
|
|
|
|
case 48000: r = SND_PCM_RATE_48000; break;
|
|
|
|
case 88200: r = SND_PCM_RATE_88200; break;
|
|
|
|
case 96000: r = SND_PCM_RATE_96000; break;
|
|
|
|
case 176400: r = SND_PCM_RATE_176400; break;
|
|
|
|
case 192000: r = SND_PCM_RATE_192000; break;
|
|
|
|
default: r = 0; break;
|
|
|
|
}
|
2000-07-01 12:38:30 +02:00
|
|
|
if (!(cpinfo.rates & r)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unsupported rate %iHz", format->rate);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2000-07-01 12:38:30 +02:00
|
|
|
if (cpinfo.min_channels > format->channels || cpinfo.max_channels < format->channels) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unsupported number of channels %i (valid range is %i-%i)", format->channels, cpinfo.min_channels, cpinfo.max_channels);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
if (!(cpinfo.formats & (1 << format->sfmt))) {
|
|
|
|
error("unsupported format %s", snd_pcm_format_name(format->sfmt));
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2000-05-27 18:52:19 +02:00
|
|
|
if (format->channels > 1) {
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xfer_mode == SND_PCM_XFER_INTERLEAVED) {
|
|
|
|
if (!(cpinfo.flags & SND_PCM_INFO_INTERLEAVED)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unsupported interleaved format");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
} else if (!(cpinfo.flags & SND_PCM_INFO_NONINTERLEAVED)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unsupported non interleaved format");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void usage(char *command)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-05-25 10:36:59 +02:00
|
|
|
int k;
|
2000-06-21 17:05:06 +02:00
|
|
|
fprintf(stderr, "\
|
|
|
|
Usage: %s [OPTION]... [FILE]...
|
|
|
|
|
|
|
|
--help help
|
|
|
|
--version print current version
|
2000-08-24 19:59:21 +02:00
|
|
|
-l,--list-devices list all soundcards and digital audio devices
|
|
|
|
-L,--list-pcms list all PCMs defined
|
|
|
|
|
|
|
|
-P, --pcm=NAME select PCM by name
|
|
|
|
or
|
|
|
|
-C, --card=# select card # (0-%i) or id, defaults to 0
|
2000-06-21 17:05:06 +02:00
|
|
|
-D, --device=# select device #, defaults to 0
|
2000-08-24 19:59:21 +02:00
|
|
|
-S, --subdevice=# select subdevice #, defaults to first free
|
|
|
|
-i, --direct don't use plugins for this PCM
|
|
|
|
|
2000-06-21 17:05:06 +02:00
|
|
|
-q, --quiet quiet mode
|
|
|
|
-t, --file-type TYPE file type (voc, wav or raw)
|
|
|
|
-c, --channels=# channels
|
|
|
|
-f, --format=FORMAT sample format (case insensitive)
|
|
|
|
-r, --rate=# sample rate
|
|
|
|
-d, --duration=# interrupt after # seconds
|
|
|
|
-e, --frame-mode use frame mode instead of default fragment mode
|
|
|
|
-M, --mmap mmap stream
|
|
|
|
-N, --nonblock nonblocking mode
|
|
|
|
-F, --fragment-length=# fragment length is # milliseconds
|
|
|
|
-B, --buffer-length=# buffer length is # milliseconds
|
2000-09-24 11:57:50 +02:00
|
|
|
-A, --avail-min=# min available space for wakeup is # milliseconds
|
|
|
|
-X, --xfer-min=# min xfer size is # milliseconds
|
2000-07-17 17:33:29 +02:00
|
|
|
-v, --verbose show PCM structure and setup
|
2000-06-25 18:30:20 +02:00
|
|
|
-I, --separate-channels one file for each channel
|
2000-06-21 17:05:06 +02:00
|
|
|
", command, snd_cards()-1);
|
2000-06-25 18:30:20 +02:00
|
|
|
fprintf(stderr, "Recognized sample formats are:");
|
2000-05-25 10:36:59 +02:00
|
|
|
for (k = 0; k < 32; ++k) {
|
2000-07-17 17:33:29 +02:00
|
|
|
const char *s = snd_pcm_format_name(k);
|
2000-05-25 10:36:59 +02:00
|
|
|
if (s)
|
|
|
|
fprintf(stderr, " %s", s);
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
fprintf(stderr, "\nSome of these may not be available on selected hardware\n");
|
2000-06-25 18:30:20 +02:00
|
|
|
fprintf(stderr, "The availabled format shortcuts are:\n");
|
|
|
|
fprintf(stderr, "cd (16 bit little endian, 44100, stereo)\n");
|
|
|
|
fprintf(stderr, "dat (16 bit little endian, 48000, stereo)\n");
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void device_list(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1999-06-02 02:40:31 +02:00
|
|
|
snd_ctl_t *handle;
|
1998-11-27 16:17:45 +01:00
|
|
|
int card, err, dev, idx;
|
|
|
|
unsigned int mask;
|
2000-05-08 20:54:14 +02:00
|
|
|
snd_ctl_hw_info_t info;
|
1998-11-27 16:17:45 +01:00
|
|
|
snd_pcm_info_t pcminfo;
|
|
|
|
|
|
|
|
mask = snd_cards_mask();
|
|
|
|
if (!mask) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("no soundcards found...");
|
1998-11-27 16:17:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (card = 0; card < SND_CARDS; card++) {
|
|
|
|
if (!(mask & (1 << card)))
|
|
|
|
continue;
|
2000-09-11 17:49:33 +02:00
|
|
|
if ((err = snd_ctl_hw_open(&handle, card)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("control open (%i): %s", card, snd_strerror(err));
|
1998-11-27 16:17:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("control hardware info (%i): %s", card, snd_strerror(err));
|
1998-11-27 16:17:45 +01:00
|
|
|
snd_ctl_close(handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (dev = 0; dev < info.pcmdevs; dev++) {
|
2000-06-21 17:05:06 +02:00
|
|
|
pcminfo.device = dev;
|
2000-06-22 08:07:13 +02:00
|
|
|
pcminfo.stream = -stream - 1;
|
2000-06-21 17:05:06 +02:00
|
|
|
pcminfo.subdevice = -1;
|
|
|
|
if ((err = snd_ctl_pcm_info(handle, &pcminfo)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("control digital audio info (%i): %s", card, snd_strerror(err));
|
1998-11-27 16:17:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2000-06-22 08:07:13 +02:00
|
|
|
if (pcminfo.stream != stream)
|
|
|
|
continue;
|
2000-09-10 01:17:04 +02:00
|
|
|
fprintf(stderr, "%s: %i [%s] / #%i: %s\n",
|
1998-11-27 16:17:45 +01:00
|
|
|
info.name,
|
|
|
|
card + 1,
|
|
|
|
info.id,
|
|
|
|
dev,
|
|
|
|
pcminfo.name);
|
2000-09-10 01:17:04 +02:00
|
|
|
fprintf(stderr, " Subdevices: %i/%i\n", pcminfo.subdevices_avail, pcminfo.subdevices_count);
|
2000-06-21 17:05:06 +02:00
|
|
|
for (idx = 0; idx < pcminfo.subdevices_count; idx++) {
|
|
|
|
pcminfo.subdevice = idx;
|
|
|
|
if ((err = snd_ctl_pcm_info(handle, &pcminfo)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("control digital audio playback info (%i): %s", card, snd_strerror(err));
|
2000-06-21 17:05:06 +02:00
|
|
|
} else {
|
2000-09-10 01:17:04 +02:00
|
|
|
fprintf(stderr, " Subdevice #%i: %s\n", idx, pcminfo.subname);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2000-08-24 19:59:21 +02:00
|
|
|
static void pcm_list(void)
|
|
|
|
{
|
|
|
|
snd_config_t *conf;
|
|
|
|
int err = snd_config_update();
|
|
|
|
if (err < 0)
|
2000-08-25 16:36:58 +02:00
|
|
|
error("snd_pcm_update: %s", snd_strerror(err));
|
2000-08-24 19:59:21 +02:00
|
|
|
err = snd_config_search(snd_config, "pcm", &conf);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2000-08-25 16:36:58 +02:00
|
|
|
fprintf(stderr, "PCM list:");
|
2000-08-24 19:59:21 +02:00
|
|
|
snd_config_save(conf, stderr);
|
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void version(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-08-25 16:36:58 +02:00
|
|
|
fprintf(stderr, "%s: version " SND_UTIL_VERSION_STR " by Jaroslav Kysela <perex@suse.cz>", command);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2000-06-21 17:05:06 +02:00
|
|
|
#define OPT_HELP 1
|
|
|
|
#define OPT_VERSION 2
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
int main(int argc, char *argv[])
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-06-21 17:05:06 +02:00
|
|
|
int option_index;
|
2000-09-24 11:57:50 +02:00
|
|
|
char *short_options = "lLP:C:D:S:iqt:c:f:r:d:eMNF:A:X:B:vI";
|
2000-06-21 17:05:06 +02:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, OPT_HELP},
|
|
|
|
{"version", 0, 0, OPT_VERSION},
|
|
|
|
{"list-devices", 0, 0, 'l'},
|
2000-08-24 19:59:21 +02:00
|
|
|
{"list-pcms", 0, 0, 'L'},
|
|
|
|
{"pcm", 1, 0, 'P'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"card", 1, 0, 'C'},
|
|
|
|
{"device", 1, 0, 'D'},
|
|
|
|
{"subdevice", 1, 0, 'S'},
|
2000-08-24 19:59:21 +02:00
|
|
|
{"direct", 0, 0, 'i'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"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'},
|
2000-09-24 11:57:50 +02:00
|
|
|
{"asap-mode", 0, 0, 'e'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"mmap", 0, 0, 'M'},
|
|
|
|
{"nonblock", 0, 0, 'N'},
|
|
|
|
{"fragment-length", 1, 0, 'F'},
|
2000-09-24 11:57:50 +02:00
|
|
|
{"avail-min", 1, 0, 'A'},
|
|
|
|
{"xfer-min", 1, 0, 'X'},
|
2000-08-24 19:59:21 +02:00
|
|
|
{"buffer-length", 1, 0, 'B'},
|
2000-07-17 17:33:29 +02:00
|
|
|
{"verbose", 0, 0, 'v'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"separate-channels", 0, 0, 'I'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
int direct = 0;
|
2000-08-24 19:59:21 +02:00
|
|
|
char *pcm_name = NULL;
|
|
|
|
int pcm_card = -1, pcm_dev = -1, pcm_subdev = -1;
|
|
|
|
int tmp, err, c;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
|
|
|
command = argv[0];
|
2000-06-21 17:05:06 +02:00
|
|
|
file_type = FORMAT_DEFAULT;
|
1998-11-27 16:17:45 +01:00
|
|
|
if (strstr(argv[0], "arecord")) {
|
2000-05-27 18:52:19 +02:00
|
|
|
stream = SND_PCM_STREAM_CAPTURE;
|
2000-06-21 17:05:06 +02:00
|
|
|
file_type = FORMAT_WAVE;
|
|
|
|
command = "arecord";
|
1998-11-27 16:17:45 +01:00
|
|
|
} else if (strstr(argv[0], "aplay")) {
|
2000-05-27 18:52:19 +02:00
|
|
|
stream = SND_PCM_STREAM_PLAYBACK;
|
2000-06-21 17:05:06 +02:00
|
|
|
command = "aplay";
|
1998-11-27 16:17:45 +01:00
|
|
|
} else {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("command should be named either arecord or aplay");
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_size = -1;
|
|
|
|
memset(&rformat, 0, sizeof(rformat));
|
2000-09-24 11:57:50 +02:00
|
|
|
rformat.sfmt = SND_PCM_SFMT_U8;
|
1998-11-27 16:17:45 +01:00
|
|
|
rformat.rate = DEFAULT_SPEED;
|
2000-05-27 18:52:19 +02:00
|
|
|
rformat.channels = 1;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-06-21 17:05:06 +02:00
|
|
|
while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
|
1998-11-27 16:17:45 +01:00
|
|
|
switch (c) {
|
2000-06-21 17:05:06 +02:00
|
|
|
case OPT_HELP:
|
1998-11-27 16:17:45 +01:00
|
|
|
usage(command);
|
|
|
|
return 0;
|
2000-06-21 17:05:06 +02:00
|
|
|
case OPT_VERSION:
|
|
|
|
version();
|
|
|
|
return 0;
|
1998-11-27 16:17:45 +01:00
|
|
|
case 'l':
|
|
|
|
device_list();
|
|
|
|
return 0;
|
2000-08-24 19:59:21 +02:00
|
|
|
case 'L':
|
|
|
|
pcm_list();
|
|
|
|
return 0;
|
|
|
|
case 'P':
|
|
|
|
if (pcm_card >= 0 || pcm_dev >=0 || pcm_dev >=0 ||
|
|
|
|
direct) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("-P cannot be used together with -C, -D, -S or -i");
|
2000-08-24 19:59:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pcm_name = optarg;
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'C':
|
2000-08-24 19:59:21 +02:00
|
|
|
if (pcm_name) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("-P cannot be used together with -C, -D, -S or -i");
|
2000-08-24 19:59:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2000-08-25 16:36:58 +02:00
|
|
|
pcm_card = snd_card_get_index(optarg);
|
2000-06-21 17:05:06 +02:00
|
|
|
if (pcm_card < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("soundcard '%s' not found", optarg);
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'D':
|
2000-08-24 19:59:21 +02:00
|
|
|
if (pcm_name) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("-P cannot be used together with -C, -D, -S or -i");
|
2000-08-24 19:59:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
pcm_dev = atoi(optarg);
|
|
|
|
if (pcm_dev < 0 || pcm_dev > 32) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("device %i is invalid", pcm_dev);
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'S':
|
2000-08-24 19:59:21 +02:00
|
|
|
if (pcm_name) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("-P cannot be used together with -C, -D, -S or -i");
|
2000-08-24 19:59:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
pcm_subdev = atoi(optarg);
|
|
|
|
if (pcm_subdev < 0 || pcm_subdev > 32) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("subdevice %i is invalid", pcm_subdev);
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
break;
|
2000-08-24 19:59:21 +02:00
|
|
|
case 'i':
|
|
|
|
if (pcm_name) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("-P cannot be used together with -C, -D, -S or -i");
|
2000-08-24 19:59:21 +02:00
|
|
|
return -1;
|
2000-06-21 17:05:06 +02:00
|
|
|
}
|
2000-08-24 19:59:21 +02:00
|
|
|
direct = 1;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet_mode = 1;
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
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 {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unrecognized file format %s", optarg);
|
2000-06-21 17:05:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'c':
|
|
|
|
rformat.channels = atoi(optarg);
|
|
|
|
if (rformat.channels < 1 || rformat.channels > 32) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("value %i for channels is invalid", rformat.channels);
|
2000-06-21 17:05:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'f':
|
2000-06-25 18:30:20 +02:00
|
|
|
if (strcasecmp(optarg, "cd") == 0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
rformat.sfmt = SND_PCM_SFMT_S16_LE;
|
2000-06-25 18:30:20 +02:00
|
|
|
rformat.rate = 44100;
|
|
|
|
rformat.channels = 2;
|
|
|
|
} else if (strcasecmp(optarg, "dat") == 0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
rformat.sfmt = SND_PCM_SFMT_S16_LE;
|
2000-06-25 18:30:20 +02:00
|
|
|
rformat.rate = 48000;
|
|
|
|
rformat.channels = 2;
|
|
|
|
} else {
|
2000-09-24 11:57:50 +02:00
|
|
|
rformat.sfmt = snd_pcm_format_value(optarg);
|
|
|
|
if (rformat.sfmt < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("wrong extended format '%s'", optarg);
|
2000-06-25 18:30:20 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'r':
|
1998-11-27 16:17:45 +01:00
|
|
|
tmp = atoi(optarg);
|
|
|
|
if (tmp < 300)
|
|
|
|
tmp *= 1000;
|
|
|
|
rformat.rate = tmp;
|
|
|
|
if (tmp < 2000 || tmp > 128000) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("bad speed value %i", tmp);
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'd':
|
1998-11-27 16:17:45 +01:00
|
|
|
timelimit = atoi(optarg);
|
|
|
|
break;
|
1999-11-07 00:47:10 +01:00
|
|
|
case 'e':
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun_mode = SND_PCM_XRUN_ASAP;
|
|
|
|
ready_mode = SND_PCM_READY_ASAP;
|
1999-11-07 00:47:10 +01:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'M':
|
2000-05-08 20:54:14 +02:00
|
|
|
mmap_flag = 1;
|
1999-11-07 00:47:10 +01:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'N':
|
2000-05-08 20:54:14 +02:00
|
|
|
nonblock = 1;
|
2000-06-21 17:05:06 +02:00
|
|
|
open_mode |= SND_PCM_NONBLOCK;
|
2000-05-08 20:54:14 +02:00
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
frag_length = atoi(optarg);
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'B':
|
|
|
|
buffer_length = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'A':
|
2000-09-24 11:57:50 +02:00
|
|
|
avail_min = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
xfer_min = atoi(optarg);
|
2000-06-21 17:05:06 +02:00
|
|
|
break;
|
|
|
|
case 'v':
|
2000-09-24 11:57:50 +02:00
|
|
|
verbose = 1;
|
2000-05-08 20:54:14 +02:00
|
|
|
break;
|
2000-05-18 16:42:43 +02:00
|
|
|
case 'I':
|
2000-09-24 11:57:50 +02:00
|
|
|
xfer_mode = SND_PCM_XFER_NONINTERLEAVED;
|
2000-05-18 16:42:43 +02:00
|
|
|
break;
|
1998-11-27 16:17:45 +01:00
|
|
|
default:
|
2000-09-24 11:57:50 +02:00
|
|
|
fprintf(stderr, "Try `%s --help' for more information.\n", command);
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
}
|
|
|
|
|
2000-08-24 19:59:21 +02:00
|
|
|
if (pcm_name)
|
|
|
|
err = snd_pcm_open(&handle, pcm_name, stream, open_mode);
|
2000-06-21 17:05:06 +02:00
|
|
|
else {
|
2000-08-24 19:59:21 +02:00
|
|
|
if (pcm_card < 0)
|
|
|
|
pcm_card = 0;
|
|
|
|
if (pcm_dev < 0)
|
|
|
|
pcm_dev = 0;
|
|
|
|
if (direct)
|
|
|
|
err = snd_pcm_hw_open_subdevice(&handle, pcm_card, pcm_dev, pcm_subdev, stream, open_mode);
|
2000-09-24 11:57:50 +02:00
|
|
|
else {
|
2000-08-24 19:59:21 +02:00
|
|
|
err = snd_pcm_plug_open_subdevice(&handle, pcm_card, pcm_dev, pcm_subdev, stream, open_mode);
|
2000-09-24 11:57:50 +02:00
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
}
|
2000-08-24 19:59:21 +02:00
|
|
|
if (err < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("audio open error: %s", snd_strerror(err));
|
2000-08-24 19:59:21 +02:00
|
|
|
return 1;
|
1999-12-11 12:43:22 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
|
|
|
|
if (nonblock) {
|
2000-06-21 17:05:06 +02:00
|
|
|
err = snd_pcm_nonblock(handle, 1);
|
2000-05-08 20:54:14 +02:00
|
|
|
if (err < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("nonblock setting error: %s", snd_strerror(err));
|
2000-05-08 20:54:14 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
1999-11-07 00:47:10 +01:00
|
|
|
memset(&cinfo, 0, sizeof(cinfo));
|
2000-06-21 17:05:06 +02:00
|
|
|
if ((err = snd_pcm_info(handle, &cinfo)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("info error: %s", snd_strerror(err));
|
2000-07-01 12:38:30 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&cpinfo, 0, sizeof(cpinfo));
|
|
|
|
if ((err = snd_pcm_params_info(handle, &cpinfo)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("params info error: %s", snd_strerror(err));
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
1999-11-07 00:47:10 +01:00
|
|
|
|
|
|
|
buffer_size = 1024;
|
1998-11-27 16:17:45 +01:00
|
|
|
format = rformat;
|
|
|
|
|
1999-11-07 00:47:10 +01:00
|
|
|
audiobuf = (char *)malloc(1024);
|
|
|
|
if (audiobuf == NULL) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("not enough memory");
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
1999-11-07 00:47:10 +01:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
if (mmap_flag) {
|
2000-09-24 11:57:50 +02:00
|
|
|
writei_func = snd_pcm_mmap_writei;
|
|
|
|
readi_func = snd_pcm_mmap_readi;
|
|
|
|
writen_func = snd_pcm_mmap_writen;
|
|
|
|
readn_func = snd_pcm_mmap_readn;
|
2000-05-08 20:54:14 +02:00
|
|
|
} else {
|
2000-09-24 11:57:50 +02:00
|
|
|
writei_func = snd_pcm_writei;
|
|
|
|
readi_func = snd_pcm_readi;
|
|
|
|
writen_func = snd_pcm_writen;
|
|
|
|
readn_func = snd_pcm_readn;
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xfer_mode == SND_PCM_XFER_INTERLEAVED) {
|
2000-05-18 16:42:43 +02:00
|
|
|
if (optind > argc - 1) {
|
2000-05-27 18:52:19 +02:00
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
2000-05-18 16:42:43 +02:00
|
|
|
playback(NULL);
|
1998-11-27 16:17:45 +01:00
|
|
|
else
|
2000-05-18 16:42:43 +02:00
|
|
|
capture(NULL);
|
|
|
|
} else {
|
|
|
|
while (optind <= argc - 1) {
|
2000-05-27 18:52:19 +02:00
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
2000-05-18 16:42:43 +02:00
|
|
|
playback(argv[optind++]);
|
|
|
|
else
|
|
|
|
capture(argv[optind++]);
|
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
} else {
|
2000-05-27 18:52:19 +02:00
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
2000-05-18 16:42:43 +02:00
|
|
|
playbackv(&argv[optind], argc - optind);
|
|
|
|
else
|
|
|
|
capturev(&argv[optind], argc - optind);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
snd_pcm_close(handle);
|
2000-05-08 20:54:14 +02:00
|
|
|
return EXIT_SUCCESS;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2000-08-19 11:08:39 +02:00
|
|
|
/*
|
|
|
|
* Safe read (for pipes)
|
|
|
|
*/
|
|
|
|
|
|
|
|
ssize_t safe_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t result = 0, res;
|
|
|
|
|
|
|
|
while (count > 0) {
|
2000-08-29 10:04:19 +02:00
|
|
|
if ((res = read(fd, buf, count)) == 0)
|
|
|
|
break;
|
2000-08-19 11:08:39 +02:00
|
|
|
if (res < 0)
|
|
|
|
return result > 0 ? result : res;
|
|
|
|
count -= res;
|
|
|
|
result += res;
|
|
|
|
(char *)buf += res;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-08-13 17:43:39 +02:00
|
|
|
/*
|
|
|
|
* Test, if it is a .VOC file and return >=0 if ok (this is the length of rest)
|
|
|
|
* < 0 if not
|
|
|
|
*/
|
|
|
|
static int test_vocfile(void *buffer)
|
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
VocHeader *vp = buffer;
|
|
|
|
|
|
|
|
if (strstr(vp->magic, VOC_MAGIC_STRING)) {
|
|
|
|
vocminor = vp->version & 0xFF;
|
|
|
|
vocmajor = vp->version / 256;
|
|
|
|
if (vp->version != (0x1233 - vp->coded_ver))
|
|
|
|
return -2; /* coded version mismatch */
|
|
|
|
return vp->headerlen - sizeof(VocHeader); /* 0 mostly */
|
|
|
|
}
|
|
|
|
return -1; /* magic string fail */
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2000-09-04 11:18:13 +02:00
|
|
|
/*
|
|
|
|
* helper for test_wavefile
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_t test_wavefile_read(int fd, char *buffer, size_t *size, size_t reqsize, int line)
|
|
|
|
{
|
|
|
|
if (*size >= reqsize)
|
|
|
|
return *size;
|
|
|
|
if (safe_read(fd, buffer + *size, reqsize - *size) != reqsize - *size) {
|
|
|
|
error("read error (called from line %i)", line);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return *size = reqsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-13 17:43:39 +02:00
|
|
|
/*
|
2000-05-29 20:54:23 +02:00
|
|
|
* test, if it's a .WAV file, > 0 if ok (and set the speed, stereo etc.)
|
|
|
|
* == 0 if not
|
|
|
|
* Value returned is bytes to be discarded.
|
1998-08-13 17:43:39 +02:00
|
|
|
*/
|
2000-09-04 11:18:13 +02:00
|
|
|
static ssize_t test_wavefile(int fd, char *buffer, size_t size)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-09-04 11:18:13 +02:00
|
|
|
WaveHeader *h = (WaveHeader *)buffer;
|
|
|
|
WaveFmtBody *f;
|
2000-05-29 20:54:23 +02:00
|
|
|
WaveChunkHeader *c;
|
2000-09-04 11:18:13 +02:00
|
|
|
u_int type, len;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-09-04 11:18:13 +02:00
|
|
|
if (size < sizeof(WaveHeader))
|
|
|
|
return -1;
|
2000-05-29 20:54:23 +02:00
|
|
|
if (h->magic != WAV_RIFF || h->type != WAV_WAVE)
|
2000-09-04 11:18:13 +02:00
|
|
|
return -1;
|
|
|
|
if (size > sizeof(WaveHeader))
|
|
|
|
memmove(buffer, buffer + sizeof(WaveHeader), size - sizeof(WaveHeader));
|
|
|
|
size -= sizeof(WaveHeader);
|
|
|
|
while (1) {
|
|
|
|
test_wavefile_read(fd, buffer, &size, sizeof(WaveChunkHeader), __LINE__);
|
|
|
|
c = (WaveChunkHeader*)buffer;
|
|
|
|
type = c->type;
|
|
|
|
len = LE_INT(c->length);
|
|
|
|
if (size > sizeof(WaveChunkHeader))
|
|
|
|
memmove(buffer, buffer + sizeof(WaveChunkHeader), size - sizeof(WaveChunkHeader));
|
|
|
|
size -= sizeof(WaveChunkHeader);
|
|
|
|
if (type == WAV_FMT)
|
|
|
|
break;
|
|
|
|
test_wavefile_read(fd, buffer, &size, len, __LINE__);
|
|
|
|
if (size > len)
|
|
|
|
memmove(buffer, buffer + len, size - len);
|
|
|
|
size -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(WaveFmtBody)) {
|
|
|
|
error("unknown length of 'fmt ' chunk (read %u, should be %u at least)", len, (u_int)sizeof(WaveFmtBody));
|
|
|
|
exit(EXIT_FAILURE);
|
2000-05-29 20:54:23 +02:00
|
|
|
}
|
2000-09-04 11:18:13 +02:00
|
|
|
test_wavefile_read(fd, buffer, &size, len, __LINE__);
|
|
|
|
f = (WaveFmtBody*) buffer;
|
2000-05-29 20:54:23 +02:00
|
|
|
if (LE_SHORT(f->format) != WAV_PCM_CODE) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("can't play not PCM-coded WAVE-files");
|
2000-05-29 20:54:23 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (LE_SHORT(f->modus) < 1) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("can't play WAVE-files with %d tracks", LE_SHORT(f->modus));
|
2000-05-29 20:54:23 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
format.channels = LE_SHORT(f->modus);
|
|
|
|
switch (LE_SHORT(f->bit_p_spl)) {
|
|
|
|
case 8:
|
2000-09-24 11:57:50 +02:00
|
|
|
format.sfmt = SND_PCM_SFMT_U8;
|
2000-05-29 20:54:23 +02:00
|
|
|
break;
|
|
|
|
case 16:
|
2000-09-24 11:57:50 +02:00
|
|
|
format.sfmt = SND_PCM_SFMT_S16_LE;
|
2000-05-29 20:54:23 +02:00
|
|
|
break;
|
|
|
|
default:
|
2000-08-25 16:36:58 +02:00
|
|
|
error(" can't play WAVE-files with sample %d bits wide", LE_SHORT(f->bit_p_spl));
|
2000-05-29 20:54:23 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
format.rate = LE_INT(f->sample_fq);
|
2000-09-04 11:18:13 +02:00
|
|
|
|
|
|
|
if (size > len)
|
|
|
|
memmove(buffer, buffer + len, size - len);
|
|
|
|
size -= len;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
u_int type, len;
|
|
|
|
|
|
|
|
test_wavefile_read(fd, buffer, &size, sizeof(WaveChunkHeader), __LINE__);
|
|
|
|
c = (WaveChunkHeader*)buffer;
|
|
|
|
type = c->type;
|
|
|
|
len = LE_INT(c->length);
|
|
|
|
if (size > sizeof(WaveChunkHeader))
|
|
|
|
memmove(buffer, buffer + sizeof(WaveChunkHeader), size - sizeof(WaveChunkHeader));
|
|
|
|
size -= sizeof(WaveChunkHeader);
|
|
|
|
if (type == WAV_DATA) {
|
|
|
|
if (len < count)
|
|
|
|
count = len;
|
|
|
|
check_new_format(&format);
|
|
|
|
return size;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-09-04 11:18:13 +02:00
|
|
|
test_wavefile_read(fd, buffer, &size, len, __LINE__);
|
|
|
|
if (size > len)
|
|
|
|
memmove(buffer, buffer + len, size - len);
|
|
|
|
size -= len;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-29 20:54:23 +02:00
|
|
|
|
2000-09-04 11:18:13 +02:00
|
|
|
/* shouldn't be reached */
|
|
|
|
return -1;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-27 16:17:45 +01:00
|
|
|
|
1998-08-13 17:43:39 +02:00
|
|
|
*/
|
1998-11-27 16:17:45 +01:00
|
|
|
|
|
|
|
static int test_au(int fd, void *buffer)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
AuHeader *ap = buffer;
|
|
|
|
|
2000-06-01 13:37:43 +02:00
|
|
|
if (ap->magic != AU_MAGIC)
|
1998-11-27 16:17:45 +01:00
|
|
|
return -1;
|
2000-06-01 13:37:43 +02:00
|
|
|
if (BE_INT(ap->hdr_size) > 128 || BE_INT(ap->hdr_size) < 24)
|
1998-11-27 16:17:45 +01:00
|
|
|
return -1;
|
2000-06-01 13:37:43 +02:00
|
|
|
count = BE_INT(ap->data_size);
|
|
|
|
switch (BE_INT(ap->encoding)) {
|
1998-11-27 16:17:45 +01:00
|
|
|
case AU_FMT_ULAW:
|
2000-09-24 11:57:50 +02:00
|
|
|
format.sfmt = SND_PCM_SFMT_MU_LAW;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case AU_FMT_LIN8:
|
2000-09-24 11:57:50 +02:00
|
|
|
format.sfmt = SND_PCM_SFMT_U8;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case AU_FMT_LIN16:
|
2000-09-24 11:57:50 +02:00
|
|
|
format.sfmt = SND_PCM_SFMT_U16_LE;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-01 13:37:43 +02:00
|
|
|
format.rate = BE_INT(ap->sample_rate);
|
1998-11-27 16:17:45 +01:00
|
|
|
if (format.rate < 2000 || format.rate > 256000)
|
|
|
|
return -1;
|
2000-06-01 13:37:43 +02:00
|
|
|
format.channels = BE_INT(ap->channels);
|
2000-05-27 18:52:19 +02:00
|
|
|
if (format.channels < 1 || format.channels > 128)
|
1998-11-27 16:17:45 +01:00
|
|
|
return -1;
|
2000-08-19 11:08:39 +02:00
|
|
|
if (safe_read(fd, buffer + sizeof(AuHeader), BE_INT(ap->hdr_size) - sizeof(AuHeader)) != BE_INT(ap->hdr_size) - sizeof(AuHeader)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("read error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
check_new_format(&format);
|
|
|
|
return 0;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
static void set_params(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-06-21 17:05:06 +02:00
|
|
|
snd_pcm_params_t params;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-06-04 18:26:59 +02:00
|
|
|
#if 0
|
1999-11-07 00:47:10 +01:00
|
|
|
if (mmap_flag)
|
2000-06-21 17:05:06 +02:00
|
|
|
snd_pcm_munmap(handle, stream);
|
2000-06-04 18:26:59 +02:00
|
|
|
#endif
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle); /* to be in right state */
|
2000-05-08 20:54:14 +02:00
|
|
|
|
1999-11-07 00:47:10 +01:00
|
|
|
memset(¶ms, 0, sizeof(params));
|
2000-06-10 14:39:52 +02:00
|
|
|
params.format = format;
|
2000-09-24 11:57:50 +02:00
|
|
|
params.start_mode = SND_PCM_START_DATA;
|
|
|
|
params.xfer_mode = xfer_mode;
|
|
|
|
params.xrun_mode = xrun_mode;
|
2000-06-10 14:39:52 +02:00
|
|
|
params.frag_size = format.rate * frag_length / 1000;
|
2000-06-21 17:05:06 +02:00
|
|
|
params.buffer_size = format.rate * buffer_length / 1000;
|
2000-09-24 11:57:50 +02:00
|
|
|
params.avail_min = format.rate * avail_min / 1000;
|
|
|
|
if (xrun_mode == SND_PCM_XRUN_FRAGMENT)
|
|
|
|
params.xfer_align = params.frag_size;
|
|
|
|
params.xfer_min = format.rate * xfer_min / 1000;
|
2000-06-21 17:05:06 +02:00
|
|
|
if (snd_pcm_params(handle, ¶ms) < 0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
error("unable to set params (where=0x%x, why=0x%x)", params.fail_mask, params.fail_reason);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
|
|
|
if (mmap_flag) {
|
2000-09-24 11:57:50 +02:00
|
|
|
if (snd_pcm_mmap(handle, (void **)&mmap_data)<0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unable to mmap memory");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
if (snd_pcm_prepare(handle) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unable to prepare PCM");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
|
|
|
memset(&setup, 0, sizeof(setup));
|
2000-06-21 17:05:06 +02:00
|
|
|
if (snd_pcm_setup(handle, &setup) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unable to obtain setup");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (verbose)
|
2000-07-17 17:33:29 +02:00
|
|
|
snd_pcm_dump(handle, stderr);
|
2000-05-08 20:54:14 +02:00
|
|
|
|
|
|
|
buffer_size = setup.frag_size;
|
2000-09-24 11:57:50 +02:00
|
|
|
bits_per_sample = snd_pcm_format_physical_width(setup.format.sfmt);
|
2000-06-10 14:39:52 +02:00
|
|
|
bits_per_frame = bits_per_sample * setup.format.channels;
|
|
|
|
buffer_bytes = buffer_size * bits_per_frame / 8;
|
|
|
|
audiobuf = malloc(buffer_bytes);
|
1999-11-07 00:47:10 +01:00
|
|
|
if (audiobuf == NULL) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("not enough memory");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
// fprintf(stderr, "real buffer_size = %i, frags = %i, total = %i\n", buffer_size, setup.buf.block.frags, setup.buf.block.frags * buffer_size);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
/* playback write error hander */
|
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
void xrun(void)
|
2000-05-08 20:54:14 +02:00
|
|
|
{
|
2000-06-21 17:05:06 +02:00
|
|
|
snd_pcm_status_t status;
|
2000-08-19 11:08:39 +02:00
|
|
|
int res;
|
2000-05-08 20:54:14 +02:00
|
|
|
|
|
|
|
memset(&status, 0, sizeof(status));
|
2000-08-19 11:08:39 +02:00
|
|
|
if ((res = snd_pcm_status(handle, &status))<0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
error("status error: %s", snd_strerror(res));
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2000-06-05 13:38:02 +02:00
|
|
|
if (status.state == SND_PCM_STATE_XRUN) {
|
2000-09-24 11:57:50 +02:00
|
|
|
struct timeval now, diff;
|
|
|
|
gettimeofday(&now, 0);
|
|
|
|
timersub(&now, &status.trigger_time, &diff);
|
|
|
|
fprintf(stderr, "xrun at position %lu (at least %.3f ms long)!!!\n", (unsigned long)status.hw_ptr, diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
|
|
|
|
if (verbose) {
|
|
|
|
fprintf(stderr, "Status:\n");
|
|
|
|
snd_pcm_dump_status(&status, stderr);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-08-19 11:08:39 +02:00
|
|
|
if ((res = snd_pcm_prepare(handle))<0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
error("xrun: prepare error: %s", snd_strerror(res));
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return; /* ok, data should be accepted again */
|
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
error("read/write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write function
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t pcm_write(u_char *data, size_t count)
|
|
|
|
{
|
2000-05-18 16:42:43 +02:00
|
|
|
ssize_t r;
|
|
|
|
ssize_t result = 0;
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xrun_mode == SND_PCM_XRUN_FRAGMENT &&
|
2000-06-11 15:33:16 +02:00
|
|
|
count < buffer_size) {
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_format_set_silence(format.sfmt, data + count * bits_per_frame / 8, (buffer_size - count) * format.channels);
|
2000-05-18 16:42:43 +02:00
|
|
|
count = buffer_size;
|
|
|
|
}
|
|
|
|
while (count > 0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
r = writei_func(handle, data, count);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r == -EAGAIN || (r >= 0 && r < count)) {
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_wait(handle, 1000);
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error: %s", snd_strerror(r));
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r > 0) {
|
|
|
|
result += r;
|
|
|
|
count -= r;
|
2000-06-10 14:39:52 +02:00
|
|
|
data += r * bits_per_frame / 8;
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
static ssize_t pcm_writev(u_char **data, unsigned int channels, size_t count)
|
2000-05-18 16:42:43 +02:00
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
size_t result = 0;
|
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xrun_mode == SND_PCM_XRUN_FRAGMENT &&
|
2000-05-18 16:42:43 +02:00
|
|
|
count != buffer_size) {
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-06-10 14:39:52 +02:00
|
|
|
size_t offset = count;
|
|
|
|
size_t remaining = buffer_size - count;
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; channel++)
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_format_set_silence(format.sfmt, data[channel] + offset * bits_per_sample / 8, remaining);
|
2000-05-18 16:42:43 +02:00
|
|
|
count = buffer_size;
|
|
|
|
}
|
|
|
|
while (count > 0) {
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-09-24 11:57:50 +02:00
|
|
|
void *bufs[channels];
|
2000-06-10 14:39:52 +02:00
|
|
|
size_t offset = result;
|
2000-09-24 11:57:50 +02:00
|
|
|
for (channel = 0; channel < channels; channel++)
|
|
|
|
bufs[channel] = data[channel] + offset * bits_per_sample / 8;
|
|
|
|
r = writen_func(handle, bufs, count);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r == -EAGAIN || (r >= 0 && r < count)) {
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_wait(handle, 1000);
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("writev error: %s", snd_strerror(r));
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (r > 0) {
|
|
|
|
result += r;
|
|
|
|
count -= r;
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read function
|
|
|
|
*/
|
|
|
|
|
2000-07-06 19:20:49 +02:00
|
|
|
static ssize_t pcm_read(u_char *data, size_t rcount)
|
2000-05-08 20:54:14 +02:00
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
size_t result = 0;
|
2000-07-06 19:20:49 +02:00
|
|
|
size_t count = rcount;
|
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xrun_mode == SND_PCM_XRUN_FRAGMENT &&
|
2000-07-06 19:20:49 +02:00
|
|
|
count != buffer_size) {
|
|
|
|
count = buffer_size;
|
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
while (count > 0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
r = readi_func(handle, data, count);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r == -EAGAIN || (r >= 0 && r < count)) {
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_wait(handle, 1000);
|
2000-05-08 20:54:14 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2000-05-08 20:54:14 +02:00
|
|
|
} else if (r < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("read error: %s", snd_strerror(r));
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r > 0) {
|
|
|
|
result += r;
|
|
|
|
count -= r;
|
2000-06-10 14:39:52 +02:00
|
|
|
data += r * bits_per_frame / 8;
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
2000-07-06 19:20:49 +02:00
|
|
|
return rcount;
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
|
2000-07-06 19:20:49 +02:00
|
|
|
static ssize_t pcm_readv(u_char **data, unsigned int channels, size_t rcount)
|
2000-05-18 16:42:43 +02:00
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
size_t result = 0;
|
2000-07-06 19:20:49 +02:00
|
|
|
size_t count = rcount;
|
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xrun_mode == SND_PCM_XRUN_FRAGMENT &&
|
2000-07-06 19:20:49 +02:00
|
|
|
count != buffer_size) {
|
|
|
|
count = buffer_size;
|
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
while (count > 0) {
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-09-24 11:57:50 +02:00
|
|
|
void *bufs[channels];
|
2000-06-10 14:39:52 +02:00
|
|
|
size_t offset = result;
|
2000-09-24 11:57:50 +02:00
|
|
|
for (channel = 0; channel < channels; channel++)
|
|
|
|
bufs[channel] = data[channel] + offset * bits_per_sample / 8;
|
|
|
|
r = readn_func(handle, bufs, count);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r == -EAGAIN || (r >= 0 && r < count)) {
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_wait(handle, 1000);
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("readv error: %s", snd_strerror(r));
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (r > 0) {
|
2000-05-08 20:54:14 +02:00
|
|
|
result += r;
|
2000-05-18 16:42:43 +02:00
|
|
|
count -= r;
|
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-07-06 19:20:49 +02:00
|
|
|
return rcount;
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
1998-08-13 17:43:39 +02:00
|
|
|
/*
|
|
|
|
* ok, let's play a .voc file
|
1998-11-27 16:17:45 +01:00
|
|
|
*/
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
static ssize_t voc_pcm_write(u_char *data, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t result = count, r;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
size = count;
|
2000-06-10 14:39:52 +02:00
|
|
|
if (size > buffer_bytes - buffer_pos)
|
|
|
|
size = buffer_bytes - buffer_pos;
|
2000-05-08 20:54:14 +02:00
|
|
|
memcpy(audiobuf + buffer_pos, data, size);
|
|
|
|
data += size;
|
|
|
|
count -= size;
|
|
|
|
buffer_pos += size;
|
2000-06-10 14:39:52 +02:00
|
|
|
if (buffer_pos == buffer_bytes) {
|
2000-05-08 20:54:14 +02:00
|
|
|
if ((r = pcm_write(audiobuf, buffer_size)) != buffer_size)
|
|
|
|
return r;
|
|
|
|
buffer_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
static void voc_write_silence(unsigned x)
|
2000-05-08 20:54:14 +02:00
|
|
|
{
|
|
|
|
unsigned l;
|
|
|
|
char *buf;
|
|
|
|
|
2000-06-10 14:39:52 +02:00
|
|
|
buf = (char *) malloc(buffer_bytes);
|
2000-05-08 20:54:14 +02:00
|
|
|
if (buf == NULL) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("can't allocate buffer for silence");
|
2000-05-08 20:54:14 +02:00
|
|
|
return; /* not fatal error */
|
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
snd_pcm_format_set_silence(format.sfmt, buf, buffer_size * format.channels);
|
2000-05-08 20:54:14 +02:00
|
|
|
while (x > 0) {
|
|
|
|
l = x;
|
|
|
|
if (l > buffer_size)
|
|
|
|
l = buffer_size;
|
|
|
|
if (voc_pcm_write(buf, l) != l) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
x -= l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void voc_pcm_flush(void)
|
|
|
|
{
|
|
|
|
if (buffer_pos > 0) {
|
2000-06-10 14:39:52 +02:00
|
|
|
size_t b;
|
2000-09-24 11:57:50 +02:00
|
|
|
if (xrun_mode == SND_PCM_XRUN_FRAGMENT) {
|
|
|
|
if (snd_pcm_format_set_silence(format.sfmt, audiobuf + buffer_pos, buffer_bytes - buffer_pos * 8 / bits_per_sample) < 0)
|
2000-08-25 16:36:58 +02:00
|
|
|
fprintf(stderr, "voc_pcm_flush - silence error");
|
2000-06-10 14:39:52 +02:00
|
|
|
b = buffer_size;
|
|
|
|
} else {
|
|
|
|
b = buffer_pos * 8 / bits_per_frame;
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-06-10 14:39:52 +02:00
|
|
|
if (pcm_write(audiobuf, b) != b)
|
2000-08-25 16:36:58 +02:00
|
|
|
error("voc_pcm_flush error");
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void voc_play(int fd, int ofs, char *name)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
int l;
|
|
|
|
VocBlockType *bp;
|
|
|
|
VocVoiceData *vd;
|
|
|
|
VocExtBlock *eb;
|
2000-05-18 16:42:43 +02:00
|
|
|
size_t nextblock, in_buffer;
|
2000-05-08 20:54:14 +02:00
|
|
|
u_char *data, *buf;
|
1998-11-27 16:17:45 +01:00
|
|
|
char was_extended = 0, output = 0;
|
|
|
|
u_short *sp, repeat = 0;
|
2000-05-18 16:42:43 +02:00
|
|
|
size_t silence;
|
1998-11-27 16:17:45 +01:00
|
|
|
int filepos = 0;
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
#define COUNT(x) nextblock -= x; in_buffer -= x; data += x
|
|
|
|
#define COUNT1(x) in_buffer -= x; data += x
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
data = buf = (u_char *)malloc(64 * 1024);
|
|
|
|
buffer_pos = 0;
|
|
|
|
if (data == NULL) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("malloc error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
if (!quiet_mode) {
|
2000-05-27 18:52:19 +02:00
|
|
|
fprintf(stderr, "Playing Creative Labs Channel file '%s'...\n", name);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
/* first we waste the rest of header, ugly but we don't need seek */
|
2000-06-10 14:39:52 +02:00
|
|
|
while (ofs > buffer_bytes) {
|
2000-08-19 11:08:39 +02:00
|
|
|
if (safe_read(fd, buf, buffer_bytes) != buffer_bytes) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("read error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-06-10 14:39:52 +02:00
|
|
|
ofs -= buffer_bytes;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
if (ofs) {
|
2000-08-19 11:08:39 +02:00
|
|
|
if (safe_read(fd, buf, ofs) != ofs) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("read error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
format.sfmt = SND_PCM_SFMT_U8;
|
2000-05-27 18:52:19 +02:00
|
|
|
format.channels = 1;
|
1998-11-27 16:17:45 +01:00
|
|
|
format.rate = DEFAULT_SPEED;
|
2000-05-18 16:42:43 +02:00
|
|
|
set_params();
|
1998-11-27 16:17:45 +01:00
|
|
|
|
|
|
|
in_buffer = nextblock = 0;
|
|
|
|
while (1) {
|
|
|
|
Fill_the_buffer: /* need this for repeat */
|
|
|
|
if (in_buffer < 32) {
|
2000-05-08 20:54:14 +02:00
|
|
|
/* move the rest of buffer to pos 0 and fill the buf up */
|
1998-11-27 16:17:45 +01:00
|
|
|
if (in_buffer)
|
2000-05-08 20:54:14 +02:00
|
|
|
memcpy(buf, data, in_buffer);
|
|
|
|
data = buf;
|
2000-08-19 11:08:39 +02:00
|
|
|
if ((l = safe_read(fd, buf + in_buffer, buffer_bytes - in_buffer)) > 0)
|
1998-11-27 16:17:45 +01:00
|
|
|
in_buffer += l;
|
|
|
|
else if (!in_buffer) {
|
|
|
|
/* the file is truncated, so simulate 'Terminator'
|
2000-05-08 20:54:14 +02:00
|
|
|
and reduce the datablock for safe landing */
|
|
|
|
nextblock = buf[0] = 0;
|
1998-11-27 16:17:45 +01:00
|
|
|
if (l == -1) {
|
|
|
|
perror(name);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!nextblock) { /* this is a new block */
|
|
|
|
if (in_buffer < sizeof(VocBlockType))
|
2000-05-08 20:54:14 +02:00
|
|
|
goto __end;
|
1998-11-27 16:17:45 +01:00
|
|
|
bp = (VocBlockType *) data;
|
|
|
|
COUNT1(sizeof(VocBlockType));
|
|
|
|
nextblock = VOC_DATALEN(bp);
|
|
|
|
if (output && !quiet_mode)
|
|
|
|
fprintf(stderr, "\n"); /* write /n after ASCII-out */
|
|
|
|
output = 0;
|
|
|
|
switch (bp->type) {
|
|
|
|
case 0:
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("Terminator\n");
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
return; /* VOC-file stop */
|
|
|
|
case 1:
|
|
|
|
vd = (VocVoiceData *) data;
|
|
|
|
COUNT1(sizeof(VocVoiceData));
|
|
|
|
/* we need a SYNC, before we can set new SPEED, STEREO ... */
|
|
|
|
|
|
|
|
if (!was_extended) {
|
|
|
|
format.rate = (int) (vd->tc);
|
|
|
|
format.rate = 1000000 / (256 - format.rate);
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
2000-05-27 18:52:19 +02:00
|
|
|
d_printf("Channel data %d Hz\n", dsp_speed);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
if (vd->pack) { /* /dev/dsp can't it */
|
2000-08-25 16:36:58 +02:00
|
|
|
error("can't play packed .voc files");
|
1998-11-27 16:17:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2000-05-27 18:52:19 +02:00
|
|
|
if (format.channels == 2) /* if we are in Stereo-Mode, switch back */
|
|
|
|
format.channels = 1;
|
1998-11-27 16:17:45 +01:00
|
|
|
} else { /* there was extended block */
|
2000-05-27 18:52:19 +02:00
|
|
|
format.channels = 2;
|
1998-11-27 16:17:45 +01:00
|
|
|
was_extended = 0;
|
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
set_params();
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 2: /* nothing to do, pure data */
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
2000-05-27 18:52:19 +02:00
|
|
|
d_printf("Channel continuation\n");
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 3: /* a silence block, no data, only a count */
|
|
|
|
sp = (u_short *) data;
|
|
|
|
COUNT1(sizeof(u_short));
|
|
|
|
format.rate = (int) (*data);
|
|
|
|
COUNT1(1);
|
|
|
|
format.rate = 1000000 / (256 - format.rate);
|
2000-05-18 16:42:43 +02:00
|
|
|
set_params();
|
|
|
|
silence = (((size_t) * sp) * 1000) / format.rate;
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("Silence for %d ms\n", (int) silence);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
2000-05-18 16:42:43 +02:00
|
|
|
voc_write_silence(*sp);
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 4: /* a marker for syncronisation, no effect */
|
|
|
|
sp = (u_short *) data;
|
|
|
|
COUNT1(sizeof(u_short));
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("Marker %d\n", *sp);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 5: /* ASCII text, we copy to stderr */
|
|
|
|
output = 1;
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("ASCII - text :\n");
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 6: /* repeat marker, says repeatcount */
|
|
|
|
/* my specs don't say it: maybe this can be recursive, but
|
|
|
|
I don't think somebody use it */
|
|
|
|
repeat = *(u_short *) data;
|
|
|
|
COUNT1(sizeof(u_short));
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("Repeat loop %d times\n", repeat);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1999-03-09 14:54:32 +01:00
|
|
|
if (filepos >= 0) { /* if < 0, one seek fails, why test another */
|
1998-11-27 16:17:45 +01:00
|
|
|
if ((filepos = lseek(fd, 0, 1)) < 0) {
|
2000-08-19 11:08:39 +02:00
|
|
|
error("can't play loops; %s isn't seekable\n", name);
|
1998-11-27 16:17:45 +01:00
|
|
|
repeat = 0;
|
1999-03-09 14:54:32 +01:00
|
|
|
} else {
|
1998-11-27 16:17:45 +01:00
|
|
|
filepos -= in_buffer; /* set filepos after repeat */
|
1999-03-09 14:54:32 +01:00
|
|
|
}
|
|
|
|
} else {
|
1998-11-27 16:17:45 +01:00
|
|
|
repeat = 0;
|
1999-03-09 14:54:32 +01:00
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 7: /* ok, lets repeat that be rewinding tape */
|
|
|
|
if (repeat) {
|
|
|
|
if (repeat != 0xFFFF) {
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("Repeat loop %d\n", repeat);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
--repeat;
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
else
|
|
|
|
d_printf("Neverending loop\n");
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
lseek(fd, filepos, 0);
|
|
|
|
in_buffer = 0; /* clear the buffer */
|
|
|
|
goto Fill_the_buffer;
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
else
|
|
|
|
d_printf("End repeat loop\n");
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case 8: /* the extension to play Stereo, I have SB 1.0 :-( */
|
|
|
|
was_extended = 1;
|
|
|
|
eb = (VocExtBlock *) data;
|
|
|
|
COUNT1(sizeof(VocExtBlock));
|
|
|
|
format.rate = (int) (eb->tc);
|
|
|
|
format.rate = 256000000L / (65536 - format.rate);
|
2000-05-27 18:52:19 +02:00
|
|
|
format.channels = eb->mode == VOC_MODE_STEREO ? 2 : 1;
|
|
|
|
if (format.channels == 2)
|
1998-11-27 16:17:45 +01:00
|
|
|
format.rate = format.rate >> 1;
|
|
|
|
if (eb->pack) { /* /dev/dsp can't it */
|
2000-08-25 16:36:58 +02:00
|
|
|
error("can't play packed .voc files");
|
1998-11-27 16:17:45 +01:00
|
|
|
return;
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
1998-11-27 16:17:45 +01:00
|
|
|
d_printf("Extended block %s %d Hz\n",
|
|
|
|
(eb->mode ? "Stereo" : "Mono"), dsp_speed);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
2000-08-25 16:36:58 +02:00
|
|
|
error("unknown blocktype %d. terminate.", bp->type);
|
1998-11-27 16:17:45 +01:00
|
|
|
return;
|
|
|
|
} /* switch (bp->type) */
|
|
|
|
} /* while (! nextblock) */
|
|
|
|
/* put nextblock data bytes to dsp */
|
|
|
|
l = in_buffer;
|
|
|
|
if (nextblock < l)
|
|
|
|
l = nextblock;
|
|
|
|
if (l) {
|
|
|
|
if (output && !quiet_mode) {
|
|
|
|
if (write(2, data, l) != l) { /* to stderr */
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
} else {
|
2000-05-08 20:54:14 +02:00
|
|
|
if (voc_pcm_write(data, l) != l) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
COUNT(l);
|
|
|
|
}
|
|
|
|
} /* while(1) */
|
2000-05-08 20:54:14 +02:00
|
|
|
__end:
|
|
|
|
voc_pcm_flush();
|
|
|
|
free(buf);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
/* that was a big one, perhaps somebody split it :-) */
|
|
|
|
|
|
|
|
/* setting the globals for playing raw data */
|
|
|
|
static void init_raw_data(void)
|
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
format = rformat;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the data count to read from/to dsp */
|
2000-05-18 16:42:43 +02:00
|
|
|
static size_t calc_count(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-05-18 16:42:43 +02:00
|
|
|
size_t count;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
if (!timelimit) {
|
1998-11-27 16:17:45 +01:00
|
|
|
count = 0x7fffffff;
|
2000-05-08 20:54:14 +02:00
|
|
|
} else {
|
2000-09-24 11:57:50 +02:00
|
|
|
count = snd_pcm_format_size(format.sfmt,
|
2000-05-08 20:54:14 +02:00
|
|
|
timelimit * format.rate *
|
2000-05-27 18:52:19 +02:00
|
|
|
format.channels);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
return count;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
/* write a .VOC-header */
|
2000-05-18 16:42:43 +02:00
|
|
|
static void begin_voc(int fd, size_t cnt)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
VocHeader vh;
|
|
|
|
VocBlockType bt;
|
|
|
|
VocVoiceData vd;
|
|
|
|
VocExtBlock eb;
|
|
|
|
|
|
|
|
strncpy(vh.magic, VOC_MAGIC_STRING, 20);
|
|
|
|
vh.magic[19] = 0x1A;
|
|
|
|
vh.headerlen = sizeof(VocHeader);
|
|
|
|
vh.version = VOC_ACTUAL_VERSION;
|
|
|
|
vh.coded_ver = 0x1233 - VOC_ACTUAL_VERSION;
|
|
|
|
|
|
|
|
if (write(fd, &vh, sizeof(VocHeader)) != sizeof(VocHeader)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-27 18:52:19 +02:00
|
|
|
if (format.channels > 1) {
|
1998-11-27 16:17:45 +01:00
|
|
|
/* write a extended block */
|
|
|
|
bt.type = 8;
|
|
|
|
bt.datalen = 4;
|
|
|
|
bt.datalen_m = bt.datalen_h = 0;
|
|
|
|
if (write(fd, &bt, sizeof(VocBlockType)) != sizeof(VocBlockType)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
eb.tc = (u_short) (65536 - 256000000L / (format.rate << 1));
|
|
|
|
eb.pack = 0;
|
|
|
|
eb.mode = 1;
|
|
|
|
if (write(fd, &eb, sizeof(VocExtBlock)) != sizeof(VocExtBlock)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bt.type = 1;
|
2000-05-27 18:52:19 +02:00
|
|
|
cnt += sizeof(VocVoiceData); /* Channel_data block follows */
|
1998-11-27 16:17:45 +01:00
|
|
|
bt.datalen = (u_char) (cnt & 0xFF);
|
|
|
|
bt.datalen_m = (u_char) ((cnt & 0xFF00) >> 8);
|
|
|
|
bt.datalen_h = (u_char) ((cnt & 0xFF0000) >> 16);
|
|
|
|
if (write(fd, &bt, sizeof(VocBlockType)) != sizeof(VocBlockType)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
vd.tc = (u_char) (256 - (1000000 / format.rate));
|
|
|
|
vd.pack = 0;
|
|
|
|
if (write(fd, &vd, sizeof(VocVoiceData)) != sizeof(VocVoiceData)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
|
|
|
|
/* write a WAVE-header */
|
2000-05-18 16:42:43 +02:00
|
|
|
static void begin_wave(int fd, size_t cnt)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-05-29 20:54:23 +02:00
|
|
|
WaveHeader h;
|
2000-09-04 11:18:13 +02:00
|
|
|
WaveFmtBody f;
|
|
|
|
WaveChunkHeader cf, cd;
|
1998-11-27 16:17:45 +01:00
|
|
|
int bits;
|
2000-05-30 12:12:59 +02:00
|
|
|
u_int tmp;
|
|
|
|
u_short tmp2;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
1999-05-12 00:18:49 +02:00
|
|
|
bits = 8;
|
2000-09-24 11:57:50 +02:00
|
|
|
switch (format.sfmt) {
|
1998-11-27 16:17:45 +01:00
|
|
|
case SND_PCM_SFMT_U8:
|
|
|
|
bits = 8;
|
|
|
|
break;
|
|
|
|
case SND_PCM_SFMT_S16_LE:
|
|
|
|
bits = 16;
|
|
|
|
break;
|
|
|
|
default:
|
2000-09-24 11:57:50 +02:00
|
|
|
error("Wave doesn't support %s format...", snd_pcm_format_name(format.sfmt));
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-29 20:54:23 +02:00
|
|
|
h.magic = WAV_RIFF;
|
2000-09-04 11:18:13 +02:00
|
|
|
tmp = cnt + sizeof(WaveHeader) + sizeof(WaveChunkHeader) + sizeof(WaveFmtBody) + sizeof(WaveChunkHeader) - 8;
|
2000-05-30 12:12:59 +02:00
|
|
|
h.length = LE_INT(tmp);
|
2000-05-29 20:54:23 +02:00
|
|
|
h.type = WAV_WAVE;
|
|
|
|
|
2000-09-04 11:18:13 +02:00
|
|
|
cf.type = WAV_FMT;
|
|
|
|
cf.length = LE_INT(16);
|
|
|
|
|
2000-05-30 12:12:59 +02:00
|
|
|
f.format = LE_INT(WAV_PCM_CODE);
|
2000-06-01 13:37:43 +02:00
|
|
|
f.modus = LE_SHORT(format.channels);
|
2000-05-30 12:12:59 +02:00
|
|
|
f.sample_fq = LE_INT(format.rate);
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
2000-05-30 12:12:59 +02:00
|
|
|
tmp2 = (samplesize == 8) ? 1 : 2;
|
|
|
|
f.byte_p_spl = LE_SHORT(tmp2);
|
|
|
|
tmp2 = dsp_speed * format.channels * tmp2;
|
|
|
|
f.byte_p_sec = LE_SHORT(tmp2);
|
1998-08-13 17:43:39 +02:00
|
|
|
#else
|
2000-06-01 13:37:43 +02:00
|
|
|
tmp2 = format.channels * ((bits + 7) / 8);
|
2000-05-30 12:12:59 +02:00
|
|
|
f.byte_p_spl = LE_SHORT(tmp2);
|
|
|
|
tmp2 = tmp2 * format.rate;
|
|
|
|
f.byte_p_sec = LE_SHORT(tmp2);
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
2000-05-30 12:12:59 +02:00
|
|
|
f.bit_p_spl = LE_SHORT(bits);
|
2000-05-29 20:54:23 +02:00
|
|
|
|
2000-09-04 11:18:13 +02:00
|
|
|
cd.type = WAV_DATA;
|
|
|
|
cd.length = LE_INT(cnt);
|
2000-05-29 20:54:23 +02:00
|
|
|
|
|
|
|
if (write(fd, &h, sizeof(WaveHeader)) != sizeof(WaveHeader) ||
|
2000-09-04 11:18:13 +02:00
|
|
|
write(fd, &cf, sizeof(WaveChunkHeader)) != sizeof(WaveChunkHeader) ||
|
|
|
|
write(fd, &f, sizeof(WaveFmtBody)) != sizeof(WaveFmtBody) ||
|
|
|
|
write(fd, &cd, sizeof(WaveChunkHeader)) != sizeof(WaveChunkHeader)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write a Au-header */
|
2000-05-18 16:42:43 +02:00
|
|
|
static void begin_au(int fd, size_t cnt)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
AuHeader ah;
|
|
|
|
|
2000-06-01 13:37:43 +02:00
|
|
|
ah.magic = AU_MAGIC;
|
|
|
|
ah.hdr_size = BE_INT(24);
|
|
|
|
ah.data_size = BE_INT(cnt);
|
2000-09-24 11:57:50 +02:00
|
|
|
switch (format.sfmt) {
|
1998-11-27 16:17:45 +01:00
|
|
|
case SND_PCM_SFMT_MU_LAW:
|
2000-06-01 13:37:43 +02:00
|
|
|
ah.encoding = BE_INT(AU_FMT_ULAW);
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case SND_PCM_SFMT_U8:
|
2000-06-01 13:37:43 +02:00
|
|
|
ah.encoding = BE_INT(AU_FMT_LIN8);
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
1999-11-07 00:47:10 +01:00
|
|
|
case SND_PCM_SFMT_S16_LE:
|
2000-06-01 13:37:43 +02:00
|
|
|
ah.encoding = BE_INT(AU_FMT_LIN16);
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
2000-09-24 11:57:50 +02:00
|
|
|
error("Sparc Audio doesn't support %s format...", snd_pcm_format_name(format.sfmt));
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-06-01 13:37:43 +02:00
|
|
|
ah.sample_rate = BE_INT(format.rate);
|
|
|
|
ah.channels = BE_INT(format.channels);
|
1998-11-27 16:17:45 +01:00
|
|
|
if (write(fd, &ah, sizeof(AuHeader)) != sizeof(AuHeader)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* closing .VOC */
|
|
|
|
static void end_voc(int fd)
|
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
char dummy = 0; /* Write a Terminator */
|
|
|
|
if (write(fd, &dummy, 1) != 1) {
|
2000-08-19 11:08:39 +02:00
|
|
|
error("write error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
if (fd != 1)
|
|
|
|
close(fd);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void end_wave(int fd)
|
|
|
|
{ /* only close output */
|
1998-11-27 16:17:45 +01:00
|
|
|
if (fd != 1)
|
|
|
|
close(fd);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void header(int rtype, char *name)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
if (!quiet_mode) {
|
|
|
|
fprintf(stderr, "%s %s '%s' : ",
|
2000-05-27 18:52:19 +02:00
|
|
|
(stream == SND_PCM_STREAM_PLAYBACK) ? "Playing" : "Recording",
|
1998-11-27 16:17:45 +01:00
|
|
|
fmt_rec_table[rtype].what,
|
|
|
|
name);
|
2000-09-24 11:57:50 +02:00
|
|
|
fprintf(stderr, "%s, ", snd_pcm_format_description(format.sfmt));
|
1998-11-27 16:17:45 +01:00
|
|
|
fprintf(stderr, "Rate %d Hz, ", format.rate);
|
2000-05-27 18:52:19 +02:00
|
|
|
if (format.channels == 1)
|
1998-11-27 16:17:45 +01:00
|
|
|
fprintf(stderr, "Mono");
|
2000-05-27 18:52:19 +02:00
|
|
|
else if (format.channels == 2)
|
1998-11-27 16:17:45 +01:00
|
|
|
fprintf(stderr, "Stereo");
|
|
|
|
else
|
2000-05-27 18:52:19 +02:00
|
|
|
fprintf(stderr, "Channels %i", format.channels);
|
1998-11-27 16:17:45 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* playing raw data */
|
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
void playback_go(int fd, size_t loaded, size_t count, int rtype, char *name)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1999-11-07 00:47:10 +01:00
|
|
|
int l, r;
|
2000-05-18 16:42:43 +02:00
|
|
|
size_t written = 0;
|
|
|
|
size_t c;
|
1998-08-13 17:43:39 +02:00
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
header(rtype, name);
|
2000-05-18 16:42:43 +02:00
|
|
|
set_params();
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-06-10 14:39:52 +02:00
|
|
|
while (loaded > buffer_bytes && written < count) {
|
2000-05-18 16:42:43 +02:00
|
|
|
if (pcm_write(audiobuf + written, buffer_size) <= 0)
|
2000-05-08 20:54:14 +02:00
|
|
|
return;
|
2000-06-10 14:39:52 +02:00
|
|
|
written += buffer_bytes;
|
|
|
|
loaded -= buffer_bytes;
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
if (written > 0 && loaded > 0)
|
|
|
|
memmove(audiobuf, audiobuf + written, loaded);
|
2000-04-12 22:44:14 +02:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
l = loaded;
|
2000-05-18 16:42:43 +02:00
|
|
|
while (written < count) {
|
1999-11-07 00:47:10 +01:00
|
|
|
do {
|
2000-05-18 16:42:43 +02:00
|
|
|
c = count - written;
|
2000-06-10 14:39:52 +02:00
|
|
|
if (c > buffer_bytes)
|
|
|
|
c = buffer_bytes;
|
2000-05-18 16:42:43 +02:00
|
|
|
c -= l;
|
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
if (c == 0)
|
|
|
|
break;
|
2000-08-19 11:08:39 +02:00
|
|
|
r = safe_read(fd, audiobuf + l, c);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r < 0) {
|
|
|
|
perror(name);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (r == 0)
|
1999-11-07 00:47:10 +01:00
|
|
|
break;
|
|
|
|
l += r;
|
2000-09-24 11:57:50 +02:00
|
|
|
} while (xrun_mode != SND_PCM_XRUN_ASAP && l < buffer_bytes);
|
2000-06-10 14:39:52 +02:00
|
|
|
l = l * 8 / bits_per_frame;
|
2000-05-18 16:42:43 +02:00
|
|
|
r = pcm_write(audiobuf, l);
|
|
|
|
if (r != l)
|
2000-05-08 20:54:14 +02:00
|
|
|
break;
|
2000-06-10 14:39:52 +02:00
|
|
|
r = r * bits_per_frame / 8;
|
2000-05-18 16:42:43 +02:00
|
|
|
written += r;
|
2000-05-08 20:54:14 +02:00
|
|
|
l = 0;
|
|
|
|
}
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
1999-06-13 12:09:54 +02:00
|
|
|
/* captureing raw data, this proc handels WAVE files and .VOCs (as one block) */
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
void capture_go(int fd, size_t count, int rtype, char *name)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2000-05-08 20:54:14 +02:00
|
|
|
size_t c;
|
|
|
|
ssize_t r;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
|
|
|
header(rtype, name);
|
2000-05-18 16:42:43 +02:00
|
|
|
set_params();
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
while (count > 0) {
|
1998-11-27 16:17:45 +01:00
|
|
|
c = count;
|
2000-06-10 14:39:52 +02:00
|
|
|
if (c > buffer_bytes)
|
|
|
|
c = buffer_bytes;
|
|
|
|
c = c * 8 / bits_per_frame;
|
2000-05-18 16:42:43 +02:00
|
|
|
if ((r = pcm_read(audiobuf, c)) != c)
|
2000-05-08 20:54:14 +02:00
|
|
|
break;
|
2000-06-10 14:39:52 +02:00
|
|
|
r = r * bits_per_frame / 8;
|
2000-05-08 20:54:14 +02:00
|
|
|
if (write(fd, audiobuf, r) != r) {
|
|
|
|
perror(name);
|
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
count -= r;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-06-13 12:09:54 +02:00
|
|
|
* let's play or capture it (capture_type says VOC/WAVE/raw)
|
1998-08-13 17:43:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void playback(char *name)
|
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
int fd, ofs;
|
2000-09-12 14:40:09 +02:00
|
|
|
size_t dta;
|
|
|
|
ssize_t dtawave;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-07-12 18:25:21 +02:00
|
|
|
count = calc_count();
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
1999-02-10 11:06:34 +01:00
|
|
|
if (!name || !strcmp(name, "-")) {
|
1998-11-27 16:17:45 +01:00
|
|
|
fd = 0;
|
|
|
|
name = "stdin";
|
|
|
|
} else {
|
|
|
|
if ((fd = open(name, O_RDONLY, 0)) == -1) {
|
|
|
|
perror(name);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* read the file header */
|
2000-09-04 11:18:13 +02:00
|
|
|
dta = sizeof(AuHeader);
|
|
|
|
if (safe_read(fd, audiobuf, dta) != dta) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("read error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
if (test_au(fd, audiobuf) >= 0) {
|
2000-09-24 11:57:50 +02:00
|
|
|
rformat.sfmt = SND_PCM_SFMT_MU_LAW;
|
1998-11-27 16:17:45 +01:00
|
|
|
playback_go(fd, 0, count, FORMAT_AU, name);
|
|
|
|
goto __end;
|
|
|
|
}
|
2000-09-04 11:18:13 +02:00
|
|
|
dta = sizeof(VocHeader);
|
2000-08-19 11:08:39 +02:00
|
|
|
if (safe_read(fd, audiobuf + sizeof(AuHeader),
|
2000-09-04 11:18:13 +02:00
|
|
|
dta - sizeof(AuHeader)) != dta - sizeof(AuHeader)) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("read error");
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
if ((ofs = test_vocfile(audiobuf)) >= 0) {
|
|
|
|
voc_play(fd, ofs, name);
|
|
|
|
goto __end;
|
|
|
|
}
|
|
|
|
/* read bytes for WAVE-header */
|
2000-09-04 11:18:13 +02:00
|
|
|
if ((dtawave = test_wavefile(fd, audiobuf, dta)) >= 0) {
|
|
|
|
playback_go(fd, dtawave, count, FORMAT_WAVE, name);
|
1998-11-27 16:17:45 +01:00
|
|
|
} else {
|
|
|
|
/* should be raw data */
|
1999-11-07 00:47:10 +01:00
|
|
|
check_new_format(&rformat);
|
1998-11-27 16:17:45 +01:00
|
|
|
init_raw_data();
|
2000-09-04 11:18:13 +02:00
|
|
|
playback_go(fd, dta, count, FORMAT_RAW, name);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
__end:
|
|
|
|
if (fd != 0)
|
|
|
|
close(fd);
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
|
1999-06-13 12:09:54 +02:00
|
|
|
static void capture(char *name)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
1998-11-27 16:17:45 +01:00
|
|
|
int fd;
|
|
|
|
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
1999-02-10 11:06:34 +01:00
|
|
|
if (!name || !strcmp(name, "-")) {
|
1998-11-27 16:17:45 +01:00
|
|
|
fd = 1;
|
|
|
|
name = "stdout";
|
|
|
|
} else {
|
|
|
|
remove(name);
|
|
|
|
if ((fd = open(name, O_WRONLY | O_CREAT, 0644)) == -1) {
|
|
|
|
perror(name);
|
2000-05-08 20:54:14 +02:00
|
|
|
exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
2000-07-12 18:25:21 +02:00
|
|
|
count = calc_count();
|
|
|
|
count += count % 2;
|
1998-11-27 16:17:45 +01:00
|
|
|
/* 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) */
|
2000-06-21 17:05:06 +02:00
|
|
|
if (fmt_rec_table[file_type].start)
|
|
|
|
fmt_rec_table[file_type].start(fd, count);
|
1999-11-07 00:47:10 +01:00
|
|
|
check_new_format(&rformat);
|
2000-06-21 17:05:06 +02:00
|
|
|
capture_go(fd, count, file_type, name);
|
|
|
|
fmt_rec_table[file_type].end(fd);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
void playbackv_go(int* fds, unsigned int channels, size_t loaded, size_t count, int rtype, char **names)
|
2000-05-18 16:42:43 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
size_t vsize;
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
|
|
|
u_char *bufs[channels];
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
header(rtype, names[0]);
|
|
|
|
set_params();
|
|
|
|
|
2000-06-10 14:39:52 +02:00
|
|
|
vsize = buffer_bytes / channels;
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
// Not yet implemented
|
|
|
|
assert(loaded == 0);
|
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel)
|
|
|
|
bufs[channel] = audiobuf + vsize * channel;
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
size_t c = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
size_t expected = count / channels;
|
2000-05-18 16:42:43 +02:00
|
|
|
if (expected > vsize)
|
|
|
|
expected = vsize;
|
|
|
|
do {
|
2000-08-19 11:08:39 +02:00
|
|
|
r = safe_read(fds[0], bufs[0], expected);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r < 0) {
|
2000-05-27 18:52:19 +02:00
|
|
|
perror(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 1; channel < channels; ++channel) {
|
2000-08-19 11:08:39 +02:00
|
|
|
if (safe_read(fds[channel], bufs[channel], r) != r) {
|
2000-05-27 18:52:19 +02:00
|
|
|
perror(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
c += r;
|
2000-09-24 11:57:50 +02:00
|
|
|
} while (xrun_mode != SND_PCM_XRUN_ASAP && c < expected);
|
2000-06-10 14:39:52 +02:00
|
|
|
c = c * 8 / bits_per_sample;
|
|
|
|
r = pcm_writev(bufs, channels, c);
|
|
|
|
if (r != c)
|
2000-05-18 16:42:43 +02:00
|
|
|
break;
|
2000-06-10 14:39:52 +02:00
|
|
|
r = r * bits_per_frame / 8;
|
2000-05-18 16:42:43 +02:00
|
|
|
count -= r;
|
|
|
|
}
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
void capturev_go(int* fds, unsigned int channels, size_t count, int rtype, char **names)
|
2000-05-18 16:42:43 +02:00
|
|
|
{
|
|
|
|
size_t c;
|
|
|
|
ssize_t r;
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-05-18 16:42:43 +02:00
|
|
|
size_t vsize;
|
2000-05-27 18:52:19 +02:00
|
|
|
u_char *bufs[channels];
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
header(rtype, names[0]);
|
|
|
|
set_params();
|
|
|
|
|
2000-06-10 14:39:52 +02:00
|
|
|
vsize = buffer_bytes / channels;
|
2000-05-18 16:42:43 +02:00
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel)
|
|
|
|
bufs[channel] = audiobuf + vsize * channel;
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
size_t rv;
|
|
|
|
c = count;
|
2000-06-10 14:39:52 +02:00
|
|
|
if (c > buffer_bytes)
|
|
|
|
c = buffer_bytes;
|
|
|
|
c = c * 8 / bits_per_frame;
|
2000-05-27 18:52:19 +02:00
|
|
|
if ((r = pcm_readv(bufs, channels, c)) != c)
|
2000-05-18 16:42:43 +02:00
|
|
|
break;
|
2000-06-10 14:39:52 +02:00
|
|
|
rv = r * bits_per_sample / 8;
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
if (write(fds[channel], bufs[channel], rv) != rv) {
|
|
|
|
perror(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2000-06-10 14:39:52 +02:00
|
|
|
r = r * bits_per_frame / 8;
|
2000-05-18 16:42:43 +02:00
|
|
|
count -= r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void playbackv(char **names, unsigned int count)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
|
|
|
unsigned int channels = rformat.channels;
|
2000-05-18 16:42:43 +02:00
|
|
|
int alloced = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
int fds[channels];
|
|
|
|
for (channel = 0; channel < channels; ++channel)
|
|
|
|
fds[channel] = -1;
|
2000-05-18 16:42:43 +02:00
|
|
|
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (count == 1) {
|
|
|
|
size_t len = strlen(names[0]);
|
|
|
|
char format[1024];
|
|
|
|
memcpy(format, names[0], len);
|
|
|
|
strcpy(format + len, ".%d");
|
|
|
|
len += 4;
|
2000-05-27 18:52:19 +02:00
|
|
|
names = malloc(sizeof(*names) * channels);
|
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
names[channel] = malloc(len);
|
|
|
|
sprintf(names[channel], format, channel);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
alloced = 1;
|
2000-05-27 18:52:19 +02:00
|
|
|
} else if (count != channels) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("You need to specify %d files", channels);
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
fds[channel] = open(names[channel], O_RDONLY, 0);
|
|
|
|
if (fds[channel] < 0) {
|
|
|
|
perror(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto __end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* should be raw data */
|
|
|
|
check_new_format(&rformat);
|
|
|
|
init_raw_data();
|
|
|
|
count = calc_count();
|
2000-05-27 18:52:19 +02:00
|
|
|
playbackv_go(fds, channels, 0, count, FORMAT_RAW, names);
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
__end:
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
if (fds[channel] >= 0)
|
|
|
|
close(fds[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (alloced)
|
2000-05-27 18:52:19 +02:00
|
|
|
free(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
if (alloced)
|
|
|
|
free(names);
|
|
|
|
if (ret)
|
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void capturev(char **names, unsigned int count)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
|
|
|
unsigned int channels = rformat.channels;
|
2000-05-18 16:42:43 +02:00
|
|
|
int alloced = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
int fds[channels];
|
|
|
|
for (channel = 0; channel < channels; ++channel)
|
|
|
|
fds[channel] = -1;
|
2000-05-18 16:42:43 +02:00
|
|
|
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (count == 1) {
|
|
|
|
size_t len = strlen(names[0]);
|
|
|
|
char format[1024];
|
|
|
|
memcpy(format, names[0], len);
|
|
|
|
strcpy(format + len, ".%d");
|
|
|
|
len += 4;
|
2000-05-27 18:52:19 +02:00
|
|
|
names = malloc(sizeof(*names) * channels);
|
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
names[channel] = malloc(len);
|
|
|
|
sprintf(names[channel], format, channel);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
alloced = 1;
|
2000-05-27 18:52:19 +02:00
|
|
|
} else if (count != channels) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("You need to specify %d files", channels);
|
2000-05-18 16:42:43 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
fds[channel] = open(names[channel], O_WRONLY + O_CREAT, 0644);
|
|
|
|
if (fds[channel] < 0) {
|
|
|
|
perror(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto __end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* should be raw data */
|
|
|
|
check_new_format(&rformat);
|
|
|
|
init_raw_data();
|
|
|
|
count = calc_count();
|
2000-05-27 18:52:19 +02:00
|
|
|
capturev_go(fds, channels, count, FORMAT_RAW, names);
|
2000-05-18 16:42:43 +02:00
|
|
|
|
|
|
|
__end:
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; ++channel) {
|
|
|
|
if (fds[channel] >= 0)
|
|
|
|
close(fds[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
if (alloced)
|
2000-05-27 18:52:19 +02:00
|
|
|
free(names[channel]);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
if (alloced)
|
|
|
|
free(names);
|
|
|
|
if (ret)
|
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|