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
|
|
|
|
*
|
2007-10-15 10:25:17 +02:00
|
|
|
* Copyright (c) by Jaroslav Kysela <perex@perex.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
|
2017-11-14 14:28:51 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
1998-08-13 17:43:39 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-02-17 19:07:24 +01:00
|
|
|
#define _GNU_SOURCE
|
2018-03-13 19:51:24 +01:00
|
|
|
#include "aconfig.h"
|
1998-08-13 17:43:39 +02:00
|
|
|
#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>
|
2004-03-09 14:18:29 +01:00
|
|
|
#include <limits.h>
|
2007-08-07 15:36:41 +02:00
|
|
|
#include <time.h>
|
2005-10-31 15:44:03 +01:00
|
|
|
#include <locale.h>
|
2001-09-11 11:34:14 +02:00
|
|
|
#include <alsa/asoundlib.h>
|
2000-04-12 22:44:14 +02:00
|
|
|
#include <assert.h>
|
2010-07-15 10:40:21 +02:00
|
|
|
#include <termios.h>
|
2012-07-17 12:15:33 +02:00
|
|
|
#include <signal.h>
|
2019-11-20 05:28:53 +01:00
|
|
|
#include <poll.h>
|
2000-05-18 16:42:43 +02:00
|
|
|
#include <sys/uio.h>
|
2001-08-22 08:41:16 +02:00
|
|
|
#include <sys/time.h>
|
2011-06-03 14:30:55 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2011-06-03 14:35:44 +02:00
|
|
|
#include <endian.h>
|
2005-06-23 12:38:06 +02:00
|
|
|
#include "gettext.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
|
|
|
|
2019-11-20 20:13:55 +01:00
|
|
|
#define ABS(a) (a) < 0 ? -(a) : (a)
|
|
|
|
|
2012-09-11 12:20:55 +02:00
|
|
|
#ifdef SND_CHMAP_API_VERSION
|
|
|
|
#define CONFIG_SUPPORT_CHMAP 1
|
|
|
|
#endif
|
|
|
|
|
2004-03-09 14:18:29 +01:00
|
|
|
#ifndef LLONG_MAX
|
|
|
|
#define LLONG_MAX 9223372036854775807LL
|
|
|
|
#endif
|
|
|
|
|
2012-01-25 10:40:25 +01:00
|
|
|
#ifndef le16toh
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#define le16toh(x) __le16_to_cpu(x)
|
|
|
|
#define be16toh(x) __be16_to_cpu(x)
|
|
|
|
#define le32toh(x) __le32_to_cpu(x)
|
|
|
|
#define be32toh(x) __be32_to_cpu(x)
|
|
|
|
#endif
|
|
|
|
|
2004-07-01 11:36:54 +02:00
|
|
|
#define DEFAULT_FORMAT SND_PCM_FORMAT_U8
|
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 */
|
|
|
|
|
2001-01-15 14:43:08 +01:00
|
|
|
static snd_pcm_sframes_t (*readi_func)(snd_pcm_t *handle, void *buffer, snd_pcm_uframes_t size);
|
|
|
|
static snd_pcm_sframes_t (*writei_func)(snd_pcm_t *handle, const void *buffer, snd_pcm_uframes_t size);
|
|
|
|
static snd_pcm_sframes_t (*readn_func)(snd_pcm_t *handle, void **bufs, snd_pcm_uframes_t size);
|
|
|
|
static snd_pcm_sframes_t (*writen_func)(snd_pcm_t *handle, void **bufs, snd_pcm_uframes_t size);
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2008-03-14 14:11:06 +01:00
|
|
|
enum {
|
|
|
|
VUMETER_NONE,
|
|
|
|
VUMETER_MONO,
|
|
|
|
VUMETER_STEREO
|
|
|
|
};
|
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
static char *command;
|
2000-06-21 17:05:06 +02:00
|
|
|
static snd_pcm_t *handle;
|
2000-12-21 21:44:11 +01:00
|
|
|
static struct {
|
2001-02-04 18:03:19 +01:00
|
|
|
snd_pcm_format_t format;
|
2000-12-21 21:44:11 +01:00
|
|
|
unsigned int channels;
|
|
|
|
unsigned int rate;
|
|
|
|
} hwparams, rhwparams;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int timelimit = 0;
|
2017-08-09 00:05:58 +02:00
|
|
|
static int sampleslimit = 0;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int quiet_mode = 0;
|
2000-06-21 17:05:06 +02:00
|
|
|
static int file_type = FORMAT_DEFAULT;
|
|
|
|
static int open_mode = 0;
|
2001-02-04 18:03:19 +01:00
|
|
|
static snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int mmap_flag = 0;
|
2000-11-20 21:11:13 +01:00
|
|
|
static int interleaved = 1;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int nonblock = 0;
|
2015-04-29 18:01:07 +02:00
|
|
|
static volatile sig_atomic_t in_aborting = 0;
|
2005-12-16 19:44:14 +01:00
|
|
|
static u_char *audiobuf = NULL;
|
2002-09-17 15:02:17 +02:00
|
|
|
static snd_pcm_uframes_t chunk_size = 0;
|
|
|
|
static unsigned period_time = 0;
|
|
|
|
static unsigned buffer_time = 0;
|
2003-08-07 16:48:17 +02:00
|
|
|
static snd_pcm_uframes_t period_frames = 0;
|
|
|
|
static snd_pcm_uframes_t buffer_frames = 0;
|
2001-01-15 12:06:55 +01:00
|
|
|
static int avail_min = -1;
|
2001-11-27 18:37:48 +01:00
|
|
|
static int start_delay = 0;
|
2001-04-19 23:18:24 +02:00
|
|
|
static int stop_delay = 0;
|
2009-04-08 09:22:16 +02:00
|
|
|
static int monotonic = 0;
|
2011-06-03 12:41:26 +02:00
|
|
|
static int interactive = 0;
|
2010-07-15 10:40:21 +02:00
|
|
|
static int can_pause = 0;
|
2012-03-16 04:51:02 +01:00
|
|
|
static int fatal_errors = 0;
|
2000-09-24 11:57:50 +02:00
|
|
|
static int verbose = 0;
|
2008-03-14 14:11:06 +01:00
|
|
|
static int vumeter = VUMETER_NONE;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int buffer_pos = 0;
|
2015-09-15 13:10:00 +02:00
|
|
|
static size_t significant_bits_per_sample, bits_per_sample, bits_per_frame;
|
2001-01-15 12:06:55 +01:00
|
|
|
static size_t chunk_bytes;
|
2008-10-23 11:13:24 +02:00
|
|
|
static int test_position = 0;
|
2009-04-09 09:48:27 +02:00
|
|
|
static int test_coef = 8;
|
2009-04-08 09:22:16 +02:00
|
|
|
static int test_nowait = 0;
|
2001-01-17 12:00:34 +01:00
|
|
|
static snd_output_t *log;
|
2010-01-08 22:24:33 +01:00
|
|
|
static long long max_file_size = 0;
|
|
|
|
static int max_file_time = 0;
|
|
|
|
static int use_strftime = 0;
|
|
|
|
volatile static int recycle_capture_file = 0;
|
2010-08-18 08:23:09 +02:00
|
|
|
static long term_c_lflag = -1;
|
2012-03-05 22:27:00 +01:00
|
|
|
static int dump_hw_params = 0;
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2001-09-11 10:39:48 +02:00
|
|
|
static int fd = -1;
|
2004-03-09 14:18:29 +01:00
|
|
|
static off64_t pbrec_count = LLONG_MAX, fdcount;
|
2000-05-08 20:54:14 +02:00
|
|
|
static int vocmajor, vocminor;
|
1999-11-07 00:47:10 +01:00
|
|
|
|
2010-01-08 22:21:07 +01:00
|
|
|
static char *pidfile_name = NULL;
|
|
|
|
FILE *pidf = NULL;
|
|
|
|
static int pidfile_written = 0;
|
|
|
|
|
2012-09-11 12:20:55 +02:00
|
|
|
#ifdef CONFIG_SUPPORT_CHMAP
|
2012-09-20 13:53:46 +02:00
|
|
|
static snd_pcm_chmap_t *channel_map = NULL; /* chmap to override */
|
|
|
|
static unsigned int *hw_map = NULL; /* chmap to follow */
|
2012-09-11 12:20:55 +02:00
|
|
|
#endif
|
|
|
|
|
1998-08-13 17:43:39 +02:00
|
|
|
/* needed prototypes */
|
|
|
|
|
2010-07-15 13:39:14 +02:00
|
|
|
static void done_stdin(void);
|
|
|
|
|
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);
|
2001-09-11 10:39:48 +02:00
|
|
|
static void end_au(int fd);
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2016-11-28 17:32:00 +01:00
|
|
|
static void suspend(void);
|
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static const 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;
|
2005-12-06 12:25:47 +01:00
|
|
|
long long max_filesize;
|
1998-08-13 17:43:39 +02:00
|
|
|
} fmt_rec_table[] = {
|
2006-05-17 14:59:29 +02:00
|
|
|
{ NULL, NULL, N_("raw data"), LLONG_MAX },
|
2006-05-10 17:26:53 +02:00
|
|
|
{ begin_voc, end_voc, N_("VOC"), 16000000LL },
|
2006-04-26 16:14:51 +02:00
|
|
|
/* FIXME: can WAV handle exactly 2GB or less than it? */
|
2006-05-10 17:26:53 +02:00
|
|
|
{ begin_wave, end_wave, N_("WAVE"), 2147483648LL },
|
2005-12-06 12:25:47 +01:00
|
|
|
{ begin_au, end_au, N_("Sparc Audio"), LLONG_MAX }
|
1998-11-27 16:17:45 +01:00
|
|
|
};
|
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 usage(char *command)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2001-02-04 18:03:19 +01:00
|
|
|
snd_pcm_format_t k;
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(
|
2005-06-23 12:38:06 +02:00
|
|
|
_("Usage: %s [OPTION]... [FILE]...\n"
|
2002-10-02 16:42:00 +02:00
|
|
|
"\n"
|
2003-08-07 16:48:17 +02:00
|
|
|
"-h, --help help\n"
|
|
|
|
" --version print current version\n"
|
2002-10-02 16:42:00 +02:00
|
|
|
"-l, --list-devices list all soundcards and digital audio devices\n"
|
2006-10-12 14:53:56 +02:00
|
|
|
"-L, --list-pcms list device names\n"
|
2002-10-02 16:42:00 +02:00
|
|
|
"-D, --device=NAME select PCM by name\n"
|
|
|
|
"-q, --quiet quiet mode\n"
|
|
|
|
"-t, --file-type TYPE file type (voc, wav, raw or au)\n"
|
|
|
|
"-c, --channels=# channels\n"
|
|
|
|
"-f, --format=FORMAT sample format (case insensitive)\n"
|
|
|
|
"-r, --rate=# sample rate\n"
|
|
|
|
"-d, --duration=# interrupt after # seconds\n"
|
2017-08-09 00:05:58 +02:00
|
|
|
"-s, --samples=# interrupt after # samples per channel\n"
|
2002-10-02 16:42:00 +02:00
|
|
|
"-M, --mmap mmap stream\n"
|
|
|
|
"-N, --nonblock nonblocking mode\n"
|
|
|
|
"-F, --period-time=# distance between interrupts is # microseconds\n"
|
|
|
|
"-B, --buffer-time=# buffer duration is # microseconds\n"
|
2003-08-07 16:48:17 +02:00
|
|
|
" --period-size=# distance between interrupts is # frames\n"
|
|
|
|
" --buffer-size=# buffer duration is # frames\n"
|
2002-10-02 16:42:00 +02:00
|
|
|
"-A, --avail-min=# min available space for wakeup is # microseconds\n"
|
|
|
|
"-R, --start-delay=# delay for automatic PCM start is # microseconds \n"
|
|
|
|
" (relative to buffer size if <= 0)\n"
|
|
|
|
"-T, --stop-delay=# delay for automatic PCM stop is # microseconds from xrun\n"
|
|
|
|
"-v, --verbose show PCM structure and setup (accumulative)\n"
|
2008-03-14 14:11:06 +01:00
|
|
|
"-V, --vumeter=TYPE enable VU meter (TYPE: mono or stereo)\n"
|
2008-01-21 10:42:48 +01:00
|
|
|
"-I, --separate-channels one file for each channel\n"
|
2011-06-03 12:41:26 +02:00
|
|
|
"-i, --interactive allow interactive operation from stdin\n"
|
2012-09-20 13:53:46 +02:00
|
|
|
"-m, --chmap=ch1,ch2,.. Give the channel map to override or follow\n"
|
2008-01-21 10:42:48 +01:00
|
|
|
" --disable-resample disable automatic rate resample\n"
|
|
|
|
" --disable-channels disable automatic channel conversions\n"
|
|
|
|
" --disable-format disable automatic format conversions\n"
|
2008-10-23 11:13:24 +02:00
|
|
|
" --disable-softvol disable software volume control (softvol)\n"
|
2009-04-08 09:22:16 +02:00
|
|
|
" --test-position test ring buffer position\n"
|
2011-10-12 19:37:57 +02:00
|
|
|
" --test-coef=# test coefficient for ring buffer position (default 8)\n"
|
2009-04-09 09:48:27 +02:00
|
|
|
" expression for validation is: coef * (buffer_size / 2)\n"
|
2010-01-08 22:21:07 +01:00
|
|
|
" --test-nowait do not wait for ring buffer - eats whole CPU\n"
|
2010-01-08 22:24:33 +01:00
|
|
|
" --max-file-time=# start another output file when the old file has recorded\n"
|
|
|
|
" for this many seconds\n"
|
|
|
|
" --process-id-file write the process ID here\n"
|
2012-03-05 22:27:00 +01:00
|
|
|
" --use-strftime apply the strftime facility to the output file name\n"
|
2012-03-16 04:51:02 +01:00
|
|
|
" --dump-hw-params dump hw_params of the device\n"
|
2012-09-11 12:20:55 +02:00
|
|
|
" --fatal-errors treat all errors as fatal\n"
|
|
|
|
)
|
2002-10-02 16:42:00 +02:00
|
|
|
, command);
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(_("Recognized sample formats are:"));
|
2013-09-09 12:02:29 +02:00
|
|
|
for (k = 0; k <= SND_PCM_FORMAT_LAST; ++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)
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(" %s", s);
|
2000-05-25 10:36:59 +02:00
|
|
|
}
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(_("\nSome of these may not be available on selected hardware\n"));
|
2013-05-15 19:12:54 +02:00
|
|
|
printf(_("The available format shortcuts are:\n"));
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(_("-f cd (16 bit little endian, 44100, stereo)\n"));
|
|
|
|
printf(_("-f cdr (16 bit big endian, 44100, stereo)\n"));
|
|
|
|
printf(_("-f 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;
|
2001-02-07 16:13:17 +01:00
|
|
|
snd_ctl_card_info_t *info;
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_info_t *pcminfo;
|
2001-02-07 16:13:17 +01:00
|
|
|
snd_ctl_card_info_alloca(&info);
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_info_alloca(&pcminfo);
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-11-30 20:25:50 +01:00
|
|
|
card = -1;
|
|
|
|
if (snd_card_next(&card) < 0 || card < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("no soundcards found..."));
|
1998-11-27 16:17:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(_("**** List of %s Hardware Devices ****\n"),
|
|
|
|
snd_pcm_stream_name(stream));
|
2000-11-30 20:25:50 +01:00
|
|
|
while (card >= 0) {
|
2000-11-20 21:11:13 +01:00
|
|
|
char name[32];
|
|
|
|
sprintf(name, "hw:%d", card);
|
2001-03-26 14:45:50 +02:00
|
|
|
if ((err = snd_ctl_open(&handle, name, 0)) < 0) {
|
2000-08-25 16:36:58 +02:00
|
|
|
error("control open (%i): %s", card, snd_strerror(err));
|
2003-11-27 12:07:25 +01:00
|
|
|
goto next_card;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2001-02-07 16:13:17 +01:00
|
|
|
if ((err = snd_ctl_card_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);
|
2003-11-27 12:07:25 +01:00
|
|
|
goto next_card;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-11-30 20:25:50 +01:00
|
|
|
dev = -1;
|
|
|
|
while (1) {
|
2001-01-31 18:26:59 +01:00
|
|
|
unsigned int count;
|
2000-11-30 20:25:50 +01:00
|
|
|
if (snd_ctl_pcm_next_device(handle, &dev)<0)
|
|
|
|
error("snd_ctl_pcm_next_device");
|
|
|
|
if (dev < 0)
|
|
|
|
break;
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_info_set_device(pcminfo, dev);
|
|
|
|
snd_pcm_info_set_subdevice(pcminfo, 0);
|
|
|
|
snd_pcm_info_set_stream(pcminfo, stream);
|
|
|
|
if ((err = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
|
|
|
|
if (err != -ENOENT)
|
|
|
|
error("control digital audio info (%i): %s", card, snd_strerror(err));
|
1998-11-27 16:17:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(_("card %i: %s [%s], device %i: %s [%s]\n"),
|
2001-02-07 16:13:17 +01:00
|
|
|
card, snd_ctl_card_info_get_id(info), snd_ctl_card_info_get_name(info),
|
2001-01-31 18:26:59 +01:00
|
|
|
dev,
|
2001-02-04 18:03:19 +01:00
|
|
|
snd_pcm_info_get_id(pcminfo),
|
|
|
|
snd_pcm_info_get_name(pcminfo));
|
|
|
|
count = snd_pcm_info_get_subdevices_count(pcminfo);
|
2005-06-23 12:39:16 +02:00
|
|
|
printf( _(" Subdevices: %i/%i\n"),
|
2005-06-23 12:38:06 +02:00
|
|
|
snd_pcm_info_get_subdevices_avail(pcminfo), count);
|
2003-02-04 17:03:12 +01:00
|
|
|
for (idx = 0; idx < (int)count; idx++) {
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_info_set_subdevice(pcminfo, 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 {
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(_(" Subdevice #%i: %s\n"),
|
2005-06-23 12:38:06 +02:00
|
|
|
idx, snd_pcm_info_get_subdevice_name(pcminfo));
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snd_ctl_close(handle);
|
2003-11-27 12:07:25 +01:00
|
|
|
next_card:
|
2000-11-30 20:25:50 +01:00
|
|
|
if (snd_card_next(&card) < 0) {
|
|
|
|
error("snd_card_next");
|
|
|
|
break;
|
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2000-08-24 19:59:21 +02:00
|
|
|
static void pcm_list(void)
|
|
|
|
{
|
2006-11-30 13:42:33 +01:00
|
|
|
void **hints, **n;
|
|
|
|
char *name, *descr, *descr1, *io;
|
|
|
|
const char *filter;
|
2006-10-11 13:31:47 +02:00
|
|
|
|
2006-11-06 17:27:50 +01:00
|
|
|
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
|
2001-03-08 15:20:00 +01:00
|
|
|
return;
|
2006-10-11 13:31:47 +02:00
|
|
|
n = hints;
|
2006-11-30 13:42:33 +01:00
|
|
|
filter = stream == SND_PCM_STREAM_CAPTURE ? "Input" : "Output";
|
2006-10-11 13:31:47 +02:00
|
|
|
while (*n != NULL) {
|
2006-11-30 13:42:33 +01:00
|
|
|
name = snd_device_name_get_hint(*n, "NAME");
|
|
|
|
descr = snd_device_name_get_hint(*n, "DESC");
|
|
|
|
io = snd_device_name_get_hint(*n, "IOID");
|
2009-01-12 12:29:41 +01:00
|
|
|
if (io != NULL && strcmp(io, filter) != 0)
|
2006-11-30 13:42:33 +01:00
|
|
|
goto __end;
|
2006-11-30 13:45:08 +01:00
|
|
|
printf("%s\n", name);
|
2006-11-30 13:42:33 +01:00
|
|
|
if ((descr1 = descr) != NULL) {
|
2006-10-12 11:04:38 +02:00
|
|
|
printf(" ");
|
2006-11-30 13:42:33 +01:00
|
|
|
while (*descr1) {
|
|
|
|
if (*descr1 == '\n')
|
2006-10-12 11:04:38 +02:00
|
|
|
printf("\n ");
|
|
|
|
else
|
2006-11-30 13:42:33 +01:00
|
|
|
putchar(*descr1);
|
|
|
|
descr1++;
|
2006-10-12 11:04:38 +02:00
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2006-10-11 13:36:37 +02:00
|
|
|
__end:
|
2006-11-30 13:42:33 +01:00
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
|
|
|
if (descr != NULL)
|
|
|
|
free(descr);
|
|
|
|
if (io != NULL)
|
|
|
|
free(io);
|
2006-10-11 13:31:47 +02:00
|
|
|
n++;
|
2001-03-08 15:20:00 +01:00
|
|
|
}
|
2006-10-11 13:31:47 +02:00
|
|
|
snd_device_name_free_hint(hints);
|
2000-08-24 19:59:21 +02:00
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
static void version(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2007-10-15 10:25:17 +02:00
|
|
|
printf("%s: version " SND_UTIL_VERSION_STR " by Jaroslav Kysela <perex@perex.cz>\n", command);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2010-01-08 22:21:07 +01:00
|
|
|
/*
|
|
|
|
* Subroutine to clean up before exit.
|
|
|
|
*/
|
|
|
|
static void prg_exit(int code)
|
|
|
|
{
|
2010-07-15 13:39:14 +02:00
|
|
|
done_stdin();
|
2010-01-08 22:21:07 +01:00
|
|
|
if (handle)
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
if (pidfile_written)
|
|
|
|
remove (pidfile_name);
|
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
2001-09-11 10:39:48 +02:00
|
|
|
static void signal_handler(int sig)
|
|
|
|
{
|
2011-06-03 12:44:17 +02:00
|
|
|
if (in_aborting)
|
|
|
|
return;
|
|
|
|
|
|
|
|
in_aborting = 1;
|
2005-12-06 12:23:00 +01:00
|
|
|
if (verbose==2)
|
|
|
|
putchar('\n');
|
2001-09-11 10:39:48 +02:00
|
|
|
if (!quiet_mode)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Aborted by signal %s...\n"), strsignal(sig));
|
2013-04-08 13:30:54 +02:00
|
|
|
if (handle)
|
|
|
|
snd_pcm_abort(handle);
|
|
|
|
if (sig == SIGABRT) {
|
|
|
|
/* do not call snd_pcm_close() and abort immediately */
|
2003-02-17 19:07:24 +01:00
|
|
|
handle = NULL;
|
2013-04-08 13:30:54 +02:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2003-02-17 19:07:24 +01:00
|
|
|
}
|
2015-04-29 18:01:58 +02:00
|
|
|
signal(sig, SIG_DFL);
|
2001-09-11 10:39:48 +02:00
|
|
|
}
|
|
|
|
|
2010-01-08 22:24:33 +01:00
|
|
|
/* call on SIGUSR1 signal. */
|
|
|
|
static void signal_handler_recycle (int sig)
|
|
|
|
{
|
|
|
|
/* flag the capture loop to start a new output file */
|
|
|
|
recycle_capture_file = 1;
|
|
|
|
}
|
|
|
|
|
2003-08-07 16:48:17 +02:00
|
|
|
enum {
|
|
|
|
OPT_VERSION = 1,
|
|
|
|
OPT_PERIOD_SIZE,
|
2008-01-21 10:42:48 +01:00
|
|
|
OPT_BUFFER_SIZE,
|
|
|
|
OPT_DISABLE_RESAMPLE,
|
|
|
|
OPT_DISABLE_CHANNELS,
|
|
|
|
OPT_DISABLE_FORMAT,
|
2008-10-23 11:13:24 +02:00
|
|
|
OPT_DISABLE_SOFTVOL,
|
2009-04-08 09:22:16 +02:00
|
|
|
OPT_TEST_POSITION,
|
2009-04-09 09:48:27 +02:00
|
|
|
OPT_TEST_COEF,
|
2010-01-08 22:21:07 +01:00
|
|
|
OPT_TEST_NOWAIT,
|
2010-01-08 22:24:33 +01:00
|
|
|
OPT_MAX_FILE_TIME,
|
|
|
|
OPT_PROCESS_ID_FILE,
|
2012-03-05 22:27:00 +01:00
|
|
|
OPT_USE_STRFTIME,
|
2012-03-16 04:51:02 +01:00
|
|
|
OPT_DUMP_HWPARAMS,
|
|
|
|
OPT_FATAL_ERRORS,
|
2003-08-07 16:48:17 +02:00
|
|
|
};
|
2000-06-21 17:05:06 +02:00
|
|
|
|
2017-04-10 09:04:33 +02:00
|
|
|
/*
|
|
|
|
* make sure we write all bytes or return an error
|
|
|
|
*/
|
|
|
|
static ssize_t xwrite(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
ssize_t written;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
while (offset < count) {
|
|
|
|
written = write(fd, buf + offset, count - offset);
|
|
|
|
if (written <= 0)
|
|
|
|
return written;
|
|
|
|
|
|
|
|
offset += written;
|
|
|
|
};
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2016-03-11 12:40:30 +01:00
|
|
|
static long parse_long(const char *str, int *err)
|
|
|
|
{
|
|
|
|
long val;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(str, &endptr, 0);
|
|
|
|
|
|
|
|
if (errno != 0 || *endptr != '\0')
|
|
|
|
*err = -1;
|
|
|
|
else
|
|
|
|
*err = 0;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
1998-11-27 16:17:45 +01:00
|
|
|
int main(int argc, char *argv[])
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2017-08-09 00:05:58 +02:00
|
|
|
int duration_or_sample = 0;
|
2000-06-21 17:05:06 +02:00
|
|
|
int option_index;
|
2017-08-09 00:05:58 +02:00
|
|
|
static const char short_options[] = "hnlLD:qt:c:f:r:d:s:MNF:A:R:T:B:vV:IPCi"
|
2012-09-11 12:20:55 +02:00
|
|
|
#ifdef CONFIG_SUPPORT_CHMAP
|
|
|
|
"m:"
|
|
|
|
#endif
|
|
|
|
;
|
2008-11-21 13:10:02 +01:00
|
|
|
static const struct option long_options[] = {
|
2003-08-07 16:48:17 +02:00
|
|
|
{"help", 0, 0, 'h'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"version", 0, 0, OPT_VERSION},
|
2005-05-10 12:53:40 +02:00
|
|
|
{"list-devnames", 0, 0, 'n'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"list-devices", 0, 0, 'l'},
|
2000-08-24 19:59:21 +02:00
|
|
|
{"list-pcms", 0, 0, 'L'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"device", 1, 0, 'D'},
|
|
|
|
{"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'},
|
2017-08-09 00:05:58 +02:00
|
|
|
{"samples", 1, 0, 's'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"mmap", 0, 0, 'M'},
|
|
|
|
{"nonblock", 0, 0, 'N'},
|
2002-04-29 18:00:28 +02:00
|
|
|
{"period-time", 1, 0, 'F'},
|
2003-08-07 16:48:17 +02:00
|
|
|
{"period-size", 1, 0, OPT_PERIOD_SIZE},
|
2000-09-24 11:57:50 +02:00
|
|
|
{"avail-min", 1, 0, 'A'},
|
2001-04-19 23:18:24 +02:00
|
|
|
{"start-delay", 1, 0, 'R'},
|
|
|
|
{"stop-delay", 1, 0, 'T'},
|
2001-01-15 12:06:55 +01:00
|
|
|
{"buffer-time", 1, 0, 'B'},
|
2003-08-07 16:48:17 +02:00
|
|
|
{"buffer-size", 1, 0, OPT_BUFFER_SIZE},
|
2000-07-17 17:33:29 +02:00
|
|
|
{"verbose", 0, 0, 'v'},
|
2008-03-14 14:11:06 +01:00
|
|
|
{"vumeter", 1, 0, 'V'},
|
2000-06-21 17:05:06 +02:00
|
|
|
{"separate-channels", 0, 0, 'I'},
|
2002-08-27 18:33:28 +02:00
|
|
|
{"playback", 0, 0, 'P'},
|
|
|
|
{"capture", 0, 0, 'C'},
|
2008-01-21 10:42:48 +01:00
|
|
|
{"disable-resample", 0, 0, OPT_DISABLE_RESAMPLE},
|
|
|
|
{"disable-channels", 0, 0, OPT_DISABLE_CHANNELS},
|
|
|
|
{"disable-format", 0, 0, OPT_DISABLE_FORMAT},
|
|
|
|
{"disable-softvol", 0, 0, OPT_DISABLE_SOFTVOL},
|
2008-10-23 11:13:24 +02:00
|
|
|
{"test-position", 0, 0, OPT_TEST_POSITION},
|
2009-04-09 09:48:27 +02:00
|
|
|
{"test-coef", 1, 0, OPT_TEST_COEF},
|
2009-04-08 09:22:16 +02:00
|
|
|
{"test-nowait", 0, 0, OPT_TEST_NOWAIT},
|
2010-01-08 22:24:33 +01:00
|
|
|
{"max-file-time", 1, 0, OPT_MAX_FILE_TIME},
|
2010-01-08 22:21:07 +01:00
|
|
|
{"process-id-file", 1, 0, OPT_PROCESS_ID_FILE},
|
2010-01-08 22:24:33 +01:00
|
|
|
{"use-strftime", 0, 0, OPT_USE_STRFTIME},
|
2011-06-03 12:41:26 +02:00
|
|
|
{"interactive", 0, 0, 'i'},
|
2012-03-05 22:27:00 +01:00
|
|
|
{"dump-hw-params", 0, 0, OPT_DUMP_HWPARAMS},
|
2012-03-16 04:51:02 +01:00
|
|
|
{"fatal-errors", 0, 0, OPT_FATAL_ERRORS},
|
2012-09-11 12:20:55 +02:00
|
|
|
#ifdef CONFIG_SUPPORT_CHMAP
|
|
|
|
{"chmap", 1, 0, 'm'},
|
|
|
|
#endif
|
2000-06-21 17:05:06 +02:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2001-05-22 11:19:44 +02:00
|
|
|
char *pcm_name = "default";
|
2000-08-24 19:59:21 +02:00
|
|
|
int tmp, err, c;
|
2006-10-12 14:53:56 +02:00
|
|
|
int do_device_list = 0, do_pcm_list = 0;
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_info_t *info;
|
2012-11-13 13:05:39 +01:00
|
|
|
FILE *direction;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2005-11-08 17:43:36 +01:00
|
|
|
#ifdef ENABLE_NLS
|
2005-06-23 12:38:06 +02:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
textdomain(PACKAGE);
|
2005-11-08 17:43:36 +01:00
|
|
|
#endif
|
2005-06-23 12:38:06 +02:00
|
|
|
|
2003-08-27 13:08:27 +02:00
|
|
|
snd_pcm_info_alloca(&info);
|
|
|
|
|
2001-01-17 12:00:34 +01:00
|
|
|
err = snd_output_stdio_attach(&log, stderr, 0);
|
|
|
|
assert(err >= 0);
|
|
|
|
|
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";
|
2001-12-01 21:26:46 +01:00
|
|
|
start_delay = 1;
|
2012-11-13 13:05:39 +01:00
|
|
|
direction = stdout;
|
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";
|
2012-11-13 13:05:39 +01:00
|
|
|
direction = stdin;
|
1998-11-27 16:17:45 +01:00
|
|
|
} else {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("command should be named either arecord or aplay"));
|
1998-11-27 16:17:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-13 13:05:39 +01:00
|
|
|
if (isatty(fileno(direction)) && (argc == 1)) {
|
|
|
|
usage(command);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-15 12:06:55 +01:00
|
|
|
chunk_size = -1;
|
2004-07-01 11:36:54 +02:00
|
|
|
rhwparams.format = DEFAULT_FORMAT;
|
2000-11-20 21:11:13 +01:00
|
|
|
rhwparams.rate = DEFAULT_SPEED;
|
|
|
|
rhwparams.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) {
|
2003-08-07 16:48:17 +02:00
|
|
|
case 'h':
|
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':
|
2003-02-24 14:51:15 +01:00
|
|
|
do_device_list = 1;
|
|
|
|
break;
|
2000-08-24 19:59:21 +02:00
|
|
|
case 'L':
|
2003-02-24 14:51:15 +01:00
|
|
|
do_pcm_list = 1;
|
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'D':
|
2000-11-20 21:11:13 +01:00
|
|
|
pcm_name = optarg;
|
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;
|
2001-04-15 11:50:17 +02:00
|
|
|
else if (strcasecmp(optarg, "au") == 0 || strcasecmp(optarg, "sparc") == 0)
|
|
|
|
file_type = FORMAT_AU;
|
2000-06-21 17:05:06 +02:00
|
|
|
else {
|
2005-06-23 12:38:06 +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':
|
2016-03-11 12:40:30 +01:00
|
|
|
rhwparams.channels = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid channels argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2011-06-29 09:44:06 +02:00
|
|
|
if (rhwparams.channels < 1 || rhwparams.channels > 256) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("value %i for channels is invalid"), rhwparams.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':
|
2003-06-04 09:31:50 +02:00
|
|
|
if (strcasecmp(optarg, "cd") == 0 || strcasecmp(optarg, "cdr") == 0) {
|
|
|
|
if (strcasecmp(optarg, "cdr") == 0)
|
|
|
|
rhwparams.format = SND_PCM_FORMAT_S16_BE;
|
|
|
|
else
|
|
|
|
rhwparams.format = file_type == FORMAT_AU ? SND_PCM_FORMAT_S16_BE : SND_PCM_FORMAT_S16_LE;
|
2000-11-20 21:11:13 +01:00
|
|
|
rhwparams.rate = 44100;
|
|
|
|
rhwparams.channels = 2;
|
2000-06-25 18:30:20 +02:00
|
|
|
} else if (strcasecmp(optarg, "dat") == 0) {
|
2003-02-24 13:44:05 +01:00
|
|
|
rhwparams.format = file_type == FORMAT_AU ? SND_PCM_FORMAT_S16_BE : SND_PCM_FORMAT_S16_LE;
|
2000-11-20 21:11:13 +01:00
|
|
|
rhwparams.rate = 48000;
|
|
|
|
rhwparams.channels = 2;
|
2000-06-25 18:30:20 +02:00
|
|
|
} else {
|
2000-11-20 21:11:13 +01:00
|
|
|
rhwparams.format = snd_pcm_format_value(optarg);
|
2001-02-05 16:44:45 +01:00
|
|
|
if (rhwparams.format == SND_PCM_FORMAT_UNKNOWN) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("wrong extended format '%s'"), optarg);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-06-25 18:30:20 +02:00
|
|
|
}
|
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':
|
2016-03-11 12:40:30 +01:00
|
|
|
tmp = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid rate argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-12-07 12:57:14 +01:00
|
|
|
if (tmp < 1000)
|
1998-11-27 16:17:45 +01:00
|
|
|
tmp *= 1000;
|
2000-11-20 21:11:13 +01:00
|
|
|
rhwparams.rate = tmp;
|
2017-12-07 12:57:14 +01:00
|
|
|
if (tmp < 2000 || tmp > 768000) {
|
2005-06-23 12:38:06 +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':
|
2017-08-09 00:05:58 +02:00
|
|
|
if (duration_or_sample) {
|
|
|
|
error(_("duration and samples arguments cannot be used together"));
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-11 12:40:30 +01:00
|
|
|
timelimit = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid duration argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-09 00:05:58 +02:00
|
|
|
duration_or_sample = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (duration_or_sample) {
|
|
|
|
error(_("samples and duration arguments cannot be used together"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
sampleslimit = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid samples argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
duration_or_sample = 1;
|
1998-11-27 16:17:45 +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':
|
2016-03-11 12:40:30 +01:00
|
|
|
period_time = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid period time argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'B':
|
2016-03-11 12:40:30 +01:00
|
|
|
buffer_time = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid buffer time argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2000-06-21 17:05:06 +02:00
|
|
|
break;
|
2003-08-07 16:48:17 +02:00
|
|
|
case OPT_PERIOD_SIZE:
|
2016-03-11 12:40:30 +01:00
|
|
|
period_frames = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid period size argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-08-07 16:48:17 +02:00
|
|
|
break;
|
|
|
|
case OPT_BUFFER_SIZE:
|
2016-03-11 12:40:30 +01:00
|
|
|
buffer_frames = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid buffer size argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-08-07 16:48:17 +02:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'A':
|
2016-03-11 12:40:30 +01:00
|
|
|
avail_min = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid min available space argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
break;
|
2001-04-19 23:18:24 +02:00
|
|
|
case 'R':
|
2016-03-11 12:40:30 +01:00
|
|
|
start_delay = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid start delay argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2001-04-19 23:18:24 +02:00
|
|
|
break;
|
|
|
|
case 'T':
|
2016-03-11 12:40:30 +01:00
|
|
|
stop_delay = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid stop delay argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2001-04-19 23:18:24 +02:00
|
|
|
break;
|
2000-06-21 17:05:06 +02:00
|
|
|
case 'v':
|
2002-10-02 16:42:00 +02:00
|
|
|
verbose++;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (verbose > 1 && !vumeter)
|
|
|
|
vumeter = VUMETER_MONO;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
if (*optarg == 's')
|
|
|
|
vumeter = VUMETER_STEREO;
|
|
|
|
else if (*optarg == 'm')
|
|
|
|
vumeter = VUMETER_MONO;
|
|
|
|
else
|
|
|
|
vumeter = VUMETER_NONE;
|
2000-05-08 20:54:14 +02:00
|
|
|
break;
|
2000-11-20 21:11:13 +01:00
|
|
|
case 'M':
|
|
|
|
mmap_flag = 1;
|
|
|
|
break;
|
2000-05-18 16:42:43 +02:00
|
|
|
case 'I':
|
2000-11-20 21:11:13 +01:00
|
|
|
interleaved = 0;
|
2000-05-18 16:42:43 +02:00
|
|
|
break;
|
2002-08-27 18:33:28 +02:00
|
|
|
case 'P':
|
|
|
|
stream = SND_PCM_STREAM_PLAYBACK;
|
|
|
|
command = "aplay";
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
stream = SND_PCM_STREAM_CAPTURE;
|
|
|
|
command = "arecord";
|
|
|
|
start_delay = 1;
|
|
|
|
if (file_type == FORMAT_DEFAULT)
|
|
|
|
file_type = FORMAT_WAVE;
|
|
|
|
break;
|
2011-06-03 12:41:26 +02:00
|
|
|
case 'i':
|
|
|
|
interactive = 1;
|
|
|
|
break;
|
2008-01-21 10:42:48 +01:00
|
|
|
case OPT_DISABLE_RESAMPLE:
|
|
|
|
open_mode |= SND_PCM_NO_AUTO_RESAMPLE;
|
|
|
|
break;
|
|
|
|
case OPT_DISABLE_CHANNELS:
|
|
|
|
open_mode |= SND_PCM_NO_AUTO_CHANNELS;
|
|
|
|
break;
|
|
|
|
case OPT_DISABLE_FORMAT:
|
|
|
|
open_mode |= SND_PCM_NO_AUTO_FORMAT;
|
|
|
|
break;
|
|
|
|
case OPT_DISABLE_SOFTVOL:
|
|
|
|
open_mode |= SND_PCM_NO_SOFTVOL;
|
|
|
|
break;
|
2008-10-23 11:13:24 +02:00
|
|
|
case OPT_TEST_POSITION:
|
|
|
|
test_position = 1;
|
|
|
|
break;
|
2009-04-09 09:48:27 +02:00
|
|
|
case OPT_TEST_COEF:
|
2016-03-11 12:40:30 +01:00
|
|
|
test_coef = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid test coef argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-04-09 09:48:27 +02:00
|
|
|
if (test_coef < 1)
|
|
|
|
test_coef = 1;
|
|
|
|
break;
|
2009-04-08 09:22:16 +02:00
|
|
|
case OPT_TEST_NOWAIT:
|
|
|
|
test_nowait = 1;
|
|
|
|
break;
|
2010-01-08 22:24:33 +01:00
|
|
|
case OPT_MAX_FILE_TIME:
|
2016-03-11 12:40:30 +01:00
|
|
|
max_file_time = parse_long(optarg, &err);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("invalid max file time argument '%s'"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-01-08 22:24:33 +01:00
|
|
|
break;
|
2010-01-08 22:21:07 +01:00
|
|
|
case OPT_PROCESS_ID_FILE:
|
|
|
|
pidfile_name = optarg;
|
|
|
|
break;
|
2010-01-08 22:24:33 +01:00
|
|
|
case OPT_USE_STRFTIME:
|
|
|
|
use_strftime = 1;
|
|
|
|
break;
|
2012-03-05 22:27:00 +01:00
|
|
|
case OPT_DUMP_HWPARAMS:
|
|
|
|
dump_hw_params = 1;
|
|
|
|
break;
|
2012-03-16 04:51:02 +01:00
|
|
|
case OPT_FATAL_ERRORS:
|
|
|
|
fatal_errors = 1;
|
|
|
|
break;
|
2012-09-11 12:20:55 +02:00
|
|
|
#ifdef CONFIG_SUPPORT_CHMAP
|
|
|
|
case 'm':
|
|
|
|
channel_map = snd_pcm_chmap_parse_string(optarg);
|
|
|
|
if (!channel_map) {
|
|
|
|
fprintf(stderr, _("Unable to parse channel map string: %s\n"), optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
1998-11-27 16:17:45 +01:00
|
|
|
default:
|
2005-06-23 12:38:06 +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
|
|
|
}
|
|
|
|
|
2006-10-12 14:53:56 +02:00
|
|
|
if (do_device_list) {
|
2003-02-24 14:51:15 +01:00
|
|
|
if (do_pcm_list) pcm_list();
|
|
|
|
device_list();
|
2006-10-12 14:53:56 +02:00
|
|
|
goto __end;
|
2003-02-24 14:51:15 +01:00
|
|
|
} else if (do_pcm_list) {
|
|
|
|
pcm_list();
|
2006-10-12 14:53:56 +02:00
|
|
|
goto __end;
|
2003-02-24 14:51:15 +01:00
|
|
|
}
|
|
|
|
|
2000-11-20 21:11:13 +01:00
|
|
|
err = snd_pcm_open(&handle, pcm_name, stream, open_mode);
|
2000-08-24 19:59:21 +02:00
|
|
|
if (err < 0) {
|
2005-06-23 12:38:06 +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
|
|
|
|
2001-01-31 18:26:59 +01:00
|
|
|
if ((err = snd_pcm_info(handle, info)) < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("info error: %s"), snd_strerror(err));
|
2000-12-22 15:03:15 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("nonblock setting error: %s"), snd_strerror(err));
|
2000-05-08 20:54:14 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2000-07-01 12:38:30 +02:00
|
|
|
|
2001-01-15 12:06:55 +01:00
|
|
|
chunk_size = 1024;
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams = rhwparams;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2005-12-16 19:44:14 +01:00
|
|
|
audiobuf = (u_char *)malloc(1024);
|
1999-11-07 00:47:10 +01:00
|
|
|
if (audiobuf == NULL) {
|
2005-06-23 12:38:06 +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
|
|
|
}
|
|
|
|
|
2010-01-08 22:21:07 +01:00
|
|
|
if (pidfile_name) {
|
|
|
|
errno = 0;
|
|
|
|
pidf = fopen (pidfile_name, "w");
|
|
|
|
if (pidf) {
|
|
|
|
(void)fprintf (pidf, "%d\n", getpid());
|
|
|
|
fclose(pidf);
|
|
|
|
pidfile_written = 1;
|
|
|
|
} else {
|
|
|
|
error(_("Cannot create process ID file %s: %s"),
|
|
|
|
pidfile_name, strerror (errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2001-09-11 10:39:48 +02:00
|
|
|
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGABRT, signal_handler);
|
2010-01-08 22:24:33 +01:00
|
|
|
signal(SIGUSR1, signal_handler_recycle);
|
2000-11-20 21:11:13 +01:00
|
|
|
if (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
|
|
|
}
|
2005-12-06 12:23:00 +01:00
|
|
|
if (verbose==2)
|
|
|
|
putchar('\n');
|
2000-06-21 17:05:06 +02:00
|
|
|
snd_pcm_close(handle);
|
2010-01-08 22:21:07 +01:00
|
|
|
handle = NULL;
|
2000-10-07 19:00:33 +02:00
|
|
|
free(audiobuf);
|
2006-10-12 14:53:56 +02:00
|
|
|
__end:
|
2001-01-17 12:00:34 +01:00
|
|
|
snd_output_close(log);
|
2003-08-27 13:08:27 +02:00
|
|
|
snd_config_update_free_global();
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_SUCCESS);
|
|
|
|
/* avoid warning */
|
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)
|
|
|
|
*/
|
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static ssize_t safe_read(int fd, void *buf, size_t count)
|
2000-08-19 11:08:39 +02:00
|
|
|
{
|
|
|
|
ssize_t result = 0, res;
|
|
|
|
|
2013-04-08 13:30:54 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
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;
|
2004-03-09 18:43:46 +01:00
|
|
|
buf = (char *)buf + res;
|
2000-08-19 11:08:39 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2004-09-13 09:04:53 +02:00
|
|
|
if (!memcmp(vp->magic, VOC_MAGIC_STRING, 20)) {
|
|
|
|
vocminor = LE_SHORT(vp->version) & 0xFF;
|
|
|
|
vocmajor = LE_SHORT(vp->version) / 256;
|
|
|
|
if (LE_SHORT(vp->version) != (0x1233 - LE_SHORT(vp->coded_ver)))
|
1998-11-27 16:17:45 +01:00
|
|
|
return -2; /* coded version mismatch */
|
2004-09-13 09:04:53 +02:00
|
|
|
return LE_SHORT(vp->headerlen) - sizeof(VocHeader); /* 0 mostly */
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static size_t test_wavefile_read(int fd, u_char *buffer, size_t *size, size_t reqsize, int line)
|
2000-09-04 11:18:13 +02:00
|
|
|
{
|
|
|
|
if (*size >= reqsize)
|
|
|
|
return *size;
|
2003-02-04 17:03:12 +01:00
|
|
|
if ((size_t)safe_read(fd, buffer + *size, reqsize - *size) != reqsize - *size) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("read error (called from line %i)"), line);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-09-04 11:18:13 +02:00
|
|
|
}
|
|
|
|
return *size = reqsize;
|
|
|
|
}
|
|
|
|
|
2004-09-13 09:04:53 +02:00
|
|
|
#define check_wavefile_space(buffer, len, blimit) \
|
|
|
|
if (len > blimit) { \
|
|
|
|
blimit = len; \
|
|
|
|
if ((buffer = realloc(buffer, blimit)) == NULL) { \
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("not enough memory")); \
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE); \
|
2004-09-13 09:04:53 +02:00
|
|
|
} \
|
2002-03-08 22:52:20 +01:00
|
|
|
}
|
2000-09-04 11:18:13 +02:00
|
|
|
|
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
|
|
|
*/
|
2005-12-16 19:44:14 +01:00
|
|
|
static ssize_t test_wavefile(int fd, u_char *_buffer, size_t size)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2002-03-08 22:52:20 +01:00
|
|
|
WaveHeader *h = (WaveHeader *)_buffer;
|
2005-12-16 19:44:14 +01:00
|
|
|
u_char *buffer = NULL;
|
2002-03-08 22:52:20 +01:00
|
|
|
size_t blimit = 0;
|
2000-09-04 11:18:13 +02:00
|
|
|
WaveFmtBody *f;
|
2000-05-29 20:54:23 +02:00
|
|
|
WaveChunkHeader *c;
|
2000-09-04 11:18:13 +02:00
|
|
|
u_int type, len;
|
2012-12-13 10:53:19 +01:00
|
|
|
unsigned short format, channels;
|
|
|
|
int big_endian, native_format;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2000-09-04 11:18:13 +02:00
|
|
|
if (size < sizeof(WaveHeader))
|
|
|
|
return -1;
|
2012-12-13 10:53:19 +01:00
|
|
|
if (h->magic == WAV_RIFF)
|
|
|
|
big_endian = 0;
|
|
|
|
else if (h->magic == WAV_RIFX)
|
|
|
|
big_endian = 1;
|
|
|
|
else
|
2000-09-04 11:18:13 +02:00
|
|
|
return -1;
|
2012-12-13 10:53:19 +01:00
|
|
|
if (h->type != WAV_WAVE)
|
|
|
|
return -1;
|
|
|
|
|
2002-03-08 22:52:20 +01:00
|
|
|
if (size > sizeof(WaveHeader)) {
|
2004-09-13 09:04:53 +02:00
|
|
|
check_wavefile_space(buffer, size - sizeof(WaveHeader), blimit);
|
2002-03-08 22:52:20 +01:00
|
|
|
memcpy(buffer, _buffer + sizeof(WaveHeader), size - sizeof(WaveHeader));
|
|
|
|
}
|
2000-09-04 11:18:13 +02:00
|
|
|
size -= sizeof(WaveHeader);
|
|
|
|
while (1) {
|
2004-09-13 09:04:53 +02:00
|
|
|
check_wavefile_space(buffer, sizeof(WaveChunkHeader), blimit);
|
2000-09-04 11:18:13 +02:00
|
|
|
test_wavefile_read(fd, buffer, &size, sizeof(WaveChunkHeader), __LINE__);
|
|
|
|
c = (WaveChunkHeader*)buffer;
|
|
|
|
type = c->type;
|
2012-12-13 10:53:19 +01:00
|
|
|
len = TO_CPU_INT(c->length, big_endian);
|
2004-09-13 09:04:53 +02:00
|
|
|
len += len % 2;
|
2000-09-04 11:18:13 +02:00
|
|
|
if (size > sizeof(WaveChunkHeader))
|
|
|
|
memmove(buffer, buffer + sizeof(WaveChunkHeader), size - sizeof(WaveChunkHeader));
|
|
|
|
size -= sizeof(WaveChunkHeader);
|
|
|
|
if (type == WAV_FMT)
|
|
|
|
break;
|
2004-09-13 09:04:53 +02:00
|
|
|
check_wavefile_space(buffer, len, blimit);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < sizeof(WaveFmtBody)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("unknown length of 'fmt ' chunk (read %u, should be %u at least)"),
|
|
|
|
len, (u_int)sizeof(WaveFmtBody));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-29 20:54:23 +02:00
|
|
|
}
|
2004-09-13 09:04:53 +02:00
|
|
|
check_wavefile_space(buffer, len, blimit);
|
2000-09-04 11:18:13 +02:00
|
|
|
test_wavefile_read(fd, buffer, &size, len, __LINE__);
|
|
|
|
f = (WaveFmtBody*) buffer;
|
2012-12-13 10:53:19 +01:00
|
|
|
format = TO_CPU_SHORT(f->format, big_endian);
|
|
|
|
if (format == WAV_FMT_EXTENSIBLE) {
|
2008-03-06 17:34:21 +01:00
|
|
|
WaveFmtExtensibleBody *fe = (WaveFmtExtensibleBody*)buffer;
|
|
|
|
if (len < sizeof(WaveFmtExtensibleBody)) {
|
|
|
|
error(_("unknown length of extensible 'fmt ' chunk (read %u, should be %u at least)"),
|
|
|
|
len, (u_int)sizeof(WaveFmtExtensibleBody));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2008-03-06 17:34:21 +01:00
|
|
|
}
|
|
|
|
if (memcmp(fe->guid_tag, WAV_GUID_TAG, 14) != 0) {
|
|
|
|
error(_("wrong format tag in extensible 'fmt ' chunk"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2008-03-06 17:34:21 +01:00
|
|
|
}
|
2012-12-13 10:53:19 +01:00
|
|
|
format = TO_CPU_SHORT(fe->guid_format, big_endian);
|
2008-03-06 17:34:21 +01:00
|
|
|
}
|
2012-12-13 10:53:19 +01:00
|
|
|
if (format != WAV_FMT_PCM &&
|
|
|
|
format != WAV_FMT_IEEE_FLOAT) {
|
|
|
|
error(_("can't play WAVE-file format 0x%04x which is not PCM or FLOAT encoded"), format);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-29 20:54:23 +02:00
|
|
|
}
|
2012-12-13 10:53:19 +01:00
|
|
|
channels = TO_CPU_SHORT(f->channels, big_endian);
|
|
|
|
if (channels < 1) {
|
|
|
|
error(_("can't play WAVE-files with %d tracks"), channels);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-29 20:54:23 +02:00
|
|
|
}
|
2012-12-13 10:53:19 +01:00
|
|
|
hwparams.channels = channels;
|
|
|
|
switch (TO_CPU_SHORT(f->bit_p_spl, big_endian)) {
|
2000-05-29 20:54:23 +02:00
|
|
|
case 8:
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
|
|
|
hwparams.format != SND_PCM_FORMAT_U8)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Warning: format is changed to U8\n"));
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.format = SND_PCM_FORMAT_U8;
|
2000-05-29 20:54:23 +02:00
|
|
|
break;
|
|
|
|
case 16:
|
2012-12-13 10:53:19 +01:00
|
|
|
if (big_endian)
|
|
|
|
native_format = SND_PCM_FORMAT_S16_BE;
|
|
|
|
else
|
|
|
|
native_format = SND_PCM_FORMAT_S16_LE;
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
2012-12-13 10:53:19 +01:00
|
|
|
hwparams.format != native_format)
|
|
|
|
fprintf(stderr, _("Warning: format is changed to %s\n"),
|
|
|
|
snd_pcm_format_name(native_format));
|
|
|
|
hwparams.format = native_format;
|
2000-05-29 20:54:23 +02:00
|
|
|
break;
|
2002-08-27 18:32:16 +02:00
|
|
|
case 24:
|
2012-12-13 10:53:19 +01:00
|
|
|
switch (TO_CPU_SHORT(f->byte_p_spl, big_endian) / hwparams.channels) {
|
2002-08-27 18:32:16 +02:00
|
|
|
case 3:
|
2012-12-13 10:53:19 +01:00
|
|
|
if (big_endian)
|
|
|
|
native_format = SND_PCM_FORMAT_S24_3BE;
|
|
|
|
else
|
|
|
|
native_format = SND_PCM_FORMAT_S24_3LE;
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
2012-12-13 10:53:19 +01:00
|
|
|
hwparams.format != native_format)
|
|
|
|
fprintf(stderr, _("Warning: format is changed to %s\n"),
|
|
|
|
snd_pcm_format_name(native_format));
|
|
|
|
hwparams.format = native_format;
|
2002-08-27 18:32:16 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2012-12-13 10:53:19 +01:00
|
|
|
if (big_endian)
|
|
|
|
native_format = SND_PCM_FORMAT_S24_BE;
|
|
|
|
else
|
|
|
|
native_format = SND_PCM_FORMAT_S24_LE;
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
2012-12-13 10:53:19 +01:00
|
|
|
hwparams.format != native_format)
|
|
|
|
fprintf(stderr, _("Warning: format is changed to %s\n"),
|
|
|
|
snd_pcm_format_name(native_format));
|
|
|
|
hwparams.format = native_format;
|
2002-08-27 18:32:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_(" can't play WAVE-files with sample %d bits in %d bytes wide (%d channels)"),
|
2012-12-13 10:53:19 +01:00
|
|
|
TO_CPU_SHORT(f->bit_p_spl, big_endian),
|
|
|
|
TO_CPU_SHORT(f->byte_p_spl, big_endian),
|
|
|
|
hwparams.channels);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2002-08-27 18:32:16 +02:00
|
|
|
}
|
2002-12-19 12:26:55 +01:00
|
|
|
break;
|
2001-09-11 10:39:48 +02:00
|
|
|
case 32:
|
2012-12-13 10:53:19 +01:00
|
|
|
if (format == WAV_FMT_PCM) {
|
|
|
|
if (big_endian)
|
|
|
|
native_format = SND_PCM_FORMAT_S32_BE;
|
|
|
|
else
|
|
|
|
native_format = SND_PCM_FORMAT_S32_LE;
|
|
|
|
hwparams.format = native_format;
|
|
|
|
} else if (format == WAV_FMT_IEEE_FLOAT) {
|
|
|
|
if (big_endian)
|
|
|
|
native_format = SND_PCM_FORMAT_FLOAT_BE;
|
|
|
|
else
|
|
|
|
native_format = SND_PCM_FORMAT_FLOAT_LE;
|
|
|
|
hwparams.format = native_format;
|
|
|
|
}
|
2001-09-11 10:39:48 +02:00
|
|
|
break;
|
2000-05-29 20:54:23 +02:00
|
|
|
default:
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_(" can't play WAVE-files with sample %d bits wide"),
|
2012-12-13 10:53:19 +01:00
|
|
|
TO_CPU_SHORT(f->bit_p_spl, big_endian));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-29 20:54:23 +02:00
|
|
|
}
|
2012-12-13 10:53:19 +01:00
|
|
|
hwparams.rate = TO_CPU_INT(f->sample_fq, big_endian);
|
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;
|
|
|
|
|
2004-09-13 09:04:53 +02:00
|
|
|
check_wavefile_space(buffer, sizeof(WaveChunkHeader), blimit);
|
2000-09-04 11:18:13 +02:00
|
|
|
test_wavefile_read(fd, buffer, &size, sizeof(WaveChunkHeader), __LINE__);
|
|
|
|
c = (WaveChunkHeader*)buffer;
|
|
|
|
type = c->type;
|
2012-12-13 10:53:19 +01:00
|
|
|
len = TO_CPU_INT(c->length, big_endian);
|
2000-09-04 11:18:13 +02:00
|
|
|
if (size > sizeof(WaveChunkHeader))
|
|
|
|
memmove(buffer, buffer + sizeof(WaveChunkHeader), size - sizeof(WaveChunkHeader));
|
|
|
|
size -= sizeof(WaveChunkHeader);
|
|
|
|
if (type == WAV_DATA) {
|
2004-09-13 09:04:53 +02:00
|
|
|
if (len < pbrec_count && len < 0x7ffffffe)
|
2003-02-04 17:03:12 +01:00
|
|
|
pbrec_count = len;
|
2002-03-08 22:52:20 +01:00
|
|
|
if (size > 0)
|
|
|
|
memcpy(_buffer, buffer, size);
|
|
|
|
free(buffer);
|
2000-09-04 11:18:13 +02:00
|
|
|
return size;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2004-09-13 09:04:53 +02:00
|
|
|
len += len % 2;
|
|
|
|
check_wavefile_space(buffer, len, blimit);
|
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;
|
2003-02-04 17:03:12 +01:00
|
|
|
pbrec_count = BE_INT(ap->data_size);
|
2000-06-01 13:37:43 +02:00
|
|
|
switch (BE_INT(ap->encoding)) {
|
1998-11-27 16:17:45 +01:00
|
|
|
case AU_FMT_ULAW:
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
|
|
|
hwparams.format != SND_PCM_FORMAT_MU_LAW)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Warning: format is changed to MU_LAW\n"));
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.format = SND_PCM_FORMAT_MU_LAW;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case AU_FMT_LIN8:
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
|
|
|
hwparams.format != SND_PCM_FORMAT_U8)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Warning: format is changed to U8\n"));
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.format = SND_PCM_FORMAT_U8;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
case AU_FMT_LIN16:
|
2004-07-01 11:36:54 +02:00
|
|
|
if (hwparams.format != DEFAULT_FORMAT &&
|
|
|
|
hwparams.format != SND_PCM_FORMAT_S16_BE)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Warning: format is changed to S16_BE\n"));
|
2002-05-07 14:20:19 +02:00
|
|
|
hwparams.format = SND_PCM_FORMAT_S16_BE;
|
1998-11-27 16:17:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.rate = BE_INT(ap->sample_rate);
|
|
|
|
if (hwparams.rate < 2000 || hwparams.rate > 256000)
|
1998-11-27 16:17:45 +01:00
|
|
|
return -1;
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.channels = BE_INT(ap->channels);
|
2011-06-29 09:44:06 +02:00
|
|
|
if (hwparams.channels < 1 || hwparams.channels > 256)
|
1998-11-27 16:17:45 +01:00
|
|
|
return -1;
|
2003-02-04 17:03:12 +01:00
|
|
|
if ((size_t)safe_read(fd, buffer + sizeof(AuHeader), BE_INT(ap->hdr_size) - sizeof(AuHeader)) != BE_INT(ap->hdr_size) - sizeof(AuHeader)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("read error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
return 0;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2009-10-18 21:34:30 +02:00
|
|
|
static void show_available_sample_formats(snd_pcm_hw_params_t* params)
|
|
|
|
{
|
|
|
|
snd_pcm_format_t format;
|
|
|
|
|
|
|
|
fprintf(stderr, "Available formats:\n");
|
2013-09-09 12:02:29 +02:00
|
|
|
for (format = 0; format <= SND_PCM_FORMAT_LAST; format++) {
|
2009-10-18 21:34:30 +02:00
|
|
|
if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
|
|
|
|
fprintf(stderr, "- %s\n", snd_pcm_format_name(format));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 13:53:46 +02:00
|
|
|
#ifdef CONFIG_SUPPORT_CHMAP
|
|
|
|
static int setup_chmap(void)
|
|
|
|
{
|
|
|
|
snd_pcm_chmap_t *chmap = channel_map;
|
|
|
|
char mapped[hwparams.channels];
|
|
|
|
snd_pcm_chmap_t *hw_chmap;
|
|
|
|
unsigned int ch, i;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!chmap)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (chmap->channels != hwparams.channels) {
|
|
|
|
error(_("Channel numbers don't match between hw_params and channel map"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = snd_pcm_set_chmap(handle, chmap);
|
|
|
|
if (!err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
hw_chmap = snd_pcm_get_chmap(handle);
|
|
|
|
if (!hw_chmap) {
|
|
|
|
fprintf(stderr, _("Warning: unable to get channel map\n"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hw_chmap->channels == chmap->channels &&
|
|
|
|
!memcmp(hw_chmap, chmap, 4 * (chmap->channels + 1))) {
|
|
|
|
/* maps are identical, so no need to convert */
|
|
|
|
free(hw_chmap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hw_map = calloc(hwparams.channels, sizeof(int));
|
|
|
|
if (!hw_map) {
|
|
|
|
error(_("not enough memory"));
|
2019-06-23 17:54:29 +02:00
|
|
|
free(hw_chmap);
|
2012-09-20 13:53:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(mapped, 0, sizeof(mapped));
|
|
|
|
for (ch = 0; ch < hw_chmap->channels; ch++) {
|
|
|
|
if (chmap->pos[ch] == hw_chmap->pos[ch]) {
|
|
|
|
mapped[ch] = 1;
|
|
|
|
hw_map[ch] = ch;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (i = 0; i < hw_chmap->channels; i++) {
|
|
|
|
if (!mapped[i] && chmap->pos[ch] == hw_chmap->pos[i]) {
|
|
|
|
mapped[i] = 1;
|
|
|
|
hw_map[ch] = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i >= hw_chmap->channels) {
|
|
|
|
char buf[256];
|
|
|
|
error(_("Channel %d doesn't match with hw_parmas"), ch);
|
|
|
|
snd_pcm_chmap_print(hw_chmap, sizeof(buf), buf);
|
|
|
|
fprintf(stderr, "hardware chmap = %s\n", buf);
|
2019-06-23 17:54:29 +02:00
|
|
|
free(hw_chmap);
|
2012-09-20 13:53:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(hw_chmap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define setup_chmap() 0
|
|
|
|
#endif
|
|
|
|
|
2000-05-18 16:42:43 +02:00
|
|
|
static void set_params(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_hw_params_t *params;
|
|
|
|
snd_pcm_sw_params_t *swparams;
|
2002-09-17 15:02:17 +02:00
|
|
|
snd_pcm_uframes_t buffer_size;
|
2000-11-29 09:32:36 +01:00
|
|
|
int err;
|
2000-12-29 16:00:55 +01:00
|
|
|
size_t n;
|
2001-04-19 23:18:24 +02:00
|
|
|
unsigned int rate;
|
|
|
|
snd_pcm_uframes_t start_threshold, stop_threshold;
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_hw_params_alloca(¶ms);
|
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
|
|
|
err = snd_pcm_hw_params_any(handle, params);
|
2000-12-21 21:44:11 +01:00
|
|
|
if (err < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Broken configuration for this PCM: no configurations available"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-12-21 21:44:11 +01:00
|
|
|
}
|
2012-03-05 22:27:00 +01:00
|
|
|
if (dump_hw_params) {
|
|
|
|
fprintf(stderr, _("HW Params of device \"%s\":\n"),
|
|
|
|
snd_pcm_name(handle));
|
|
|
|
fprintf(stderr, "--------------------\n");
|
|
|
|
snd_pcm_hw_params_dump(params, log);
|
|
|
|
fprintf(stderr, "--------------------\n");
|
|
|
|
}
|
2000-12-21 21:44:11 +01:00
|
|
|
if (mmap_flag) {
|
2001-02-04 18:03:19 +01:00
|
|
|
snd_pcm_access_mask_t *mask = alloca(snd_pcm_access_mask_sizeof());
|
|
|
|
snd_pcm_access_mask_none(mask);
|
|
|
|
snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
|
|
|
snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
|
|
|
|
snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_COMPLEX);
|
2001-02-07 00:48:29 +01:00
|
|
|
err = snd_pcm_hw_params_set_access_mask(handle, params, mask);
|
2000-12-21 21:44:11 +01:00
|
|
|
} else if (interleaved)
|
2001-02-07 00:48:29 +01:00
|
|
|
err = snd_pcm_hw_params_set_access(handle, params,
|
2001-02-04 18:03:19 +01:00
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED);
|
2000-11-20 21:11:13 +01:00
|
|
|
else
|
2001-02-07 00:48:29 +01:00
|
|
|
err = snd_pcm_hw_params_set_access(handle, params,
|
2001-02-04 18:03:19 +01:00
|
|
|
SND_PCM_ACCESS_RW_NONINTERLEAVED);
|
2000-12-21 21:44:11 +01:00
|
|
|
if (err < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Access type not available"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-12-21 21:44:11 +01:00
|
|
|
}
|
2001-02-07 00:48:29 +01:00
|
|
|
err = snd_pcm_hw_params_set_format(handle, params, hwparams.format);
|
2000-12-21 21:44:11 +01:00
|
|
|
if (err < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Sample format non available"));
|
2009-10-18 21:34:30 +02:00
|
|
|
show_available_sample_formats(params);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-12-21 21:44:11 +01:00
|
|
|
}
|
2001-02-07 00:48:29 +01:00
|
|
|
err = snd_pcm_hw_params_set_channels(handle, params, hwparams.channels);
|
2000-12-21 21:44:11 +01:00
|
|
|
if (err < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Channels count non available"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-12-21 21:44:11 +01:00
|
|
|
}
|
|
|
|
|
2001-01-15 12:06:55 +01:00
|
|
|
#if 0
|
2001-02-07 00:48:29 +01:00
|
|
|
err = snd_pcm_hw_params_set_periods_min(handle, params, 2);
|
2000-11-29 09:32:36 +01:00
|
|
|
assert(err >= 0);
|
2001-01-15 12:06:55 +01:00
|
|
|
#endif
|
2003-02-12 14:51:25 +01:00
|
|
|
rate = hwparams.rate;
|
2002-09-17 15:02:17 +02:00
|
|
|
err = snd_pcm_hw_params_set_rate_near(handle, params, &hwparams.rate, 0);
|
2000-11-29 09:32:36 +01:00
|
|
|
assert(err >= 0);
|
2003-02-12 14:51:25 +01:00
|
|
|
if ((float)rate * 1.05 < hwparams.rate || (float)rate * 0.95 > hwparams.rate) {
|
|
|
|
if (!quiet_mode) {
|
2006-04-18 15:14:43 +02:00
|
|
|
char plugex[64];
|
2006-08-03 17:27:35 +02:00
|
|
|
const char *pcmname = snd_pcm_name(handle);
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Warning: rate is not accurate (requested = %iHz, got = %iHz)\n"), rate, hwparams.rate);
|
2006-08-03 17:27:35 +02:00
|
|
|
if (! pcmname || strchr(snd_pcm_name(handle), ':'))
|
2006-04-18 15:14:43 +02:00
|
|
|
*plugex = 0;
|
|
|
|
else
|
|
|
|
snprintf(plugex, sizeof(plugex), "(-Dplug:%s)",
|
|
|
|
snd_pcm_name(handle));
|
|
|
|
fprintf(stderr, _(" please, try the plug plugin %s\n"),
|
|
|
|
plugex);
|
2003-02-12 14:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rate = hwparams.rate;
|
2004-05-03 12:03:57 +02:00
|
|
|
if (buffer_time == 0 && buffer_frames == 0) {
|
|
|
|
err = snd_pcm_hw_params_get_buffer_time_max(params,
|
|
|
|
&buffer_time, 0);
|
|
|
|
assert(err >= 0);
|
|
|
|
if (buffer_time > 500000)
|
|
|
|
buffer_time = 500000;
|
|
|
|
}
|
2003-08-07 16:48:17 +02:00
|
|
|
if (period_time == 0 && period_frames == 0) {
|
|
|
|
if (buffer_time > 0)
|
|
|
|
period_time = buffer_time / 4;
|
|
|
|
else
|
|
|
|
period_frames = buffer_frames / 4;
|
|
|
|
}
|
|
|
|
if (period_time > 0)
|
|
|
|
err = snd_pcm_hw_params_set_period_time_near(handle, params,
|
|
|
|
&period_time, 0);
|
|
|
|
else
|
|
|
|
err = snd_pcm_hw_params_set_period_size_near(handle, params,
|
|
|
|
&period_frames, 0);
|
2002-09-17 15:02:17 +02:00
|
|
|
assert(err >= 0);
|
2003-09-11 13:23:58 +02:00
|
|
|
if (buffer_time > 0) {
|
|
|
|
err = snd_pcm_hw_params_set_buffer_time_near(handle, params,
|
|
|
|
&buffer_time, 0);
|
|
|
|
} else {
|
|
|
|
err = snd_pcm_hw_params_set_buffer_size_near(handle, params,
|
|
|
|
&buffer_frames);
|
|
|
|
}
|
|
|
|
assert(err >= 0);
|
2009-04-08 09:22:16 +02:00
|
|
|
monotonic = snd_pcm_hw_params_is_monotonic(params);
|
2010-07-15 10:40:21 +02:00
|
|
|
can_pause = snd_pcm_hw_params_can_pause(params);
|
2001-01-31 18:26:59 +01:00
|
|
|
err = snd_pcm_hw_params(handle, params);
|
2000-11-29 09:32:36 +01:00
|
|
|
if (err < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Unable to install hw params:"));
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_hw_params_dump(params, log);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
2002-09-17 15:02:17 +02:00
|
|
|
snd_pcm_hw_params_get_period_size(params, &chunk_size, 0);
|
|
|
|
snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
|
2001-02-28 10:01:45 +01:00
|
|
|
if (chunk_size == buffer_size) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Can't use period equal to buffer size (%lu == %lu)"),
|
|
|
|
chunk_size, buffer_size);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2001-02-28 10:01:45 +01:00
|
|
|
}
|
2019-03-13 14:22:22 +01:00
|
|
|
err = snd_pcm_sw_params_current(handle, swparams);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("Unable to get current sw params."));
|
|
|
|
prg_exit(EXIT_FAILURE);
|
|
|
|
}
|
2001-01-15 12:06:55 +01:00
|
|
|
if (avail_min < 0)
|
|
|
|
n = chunk_size;
|
|
|
|
else
|
2001-04-19 23:18:24 +02:00
|
|
|
n = (double) rate * avail_min / 1000000;
|
2001-02-04 18:03:19 +01:00
|
|
|
err = snd_pcm_sw_params_set_avail_min(handle, swparams, n);
|
2001-04-19 23:18:24 +02:00
|
|
|
|
2002-02-02 20:29:04 +01:00
|
|
|
/* round up to closest transfer boundary */
|
2008-01-08 18:39:06 +01:00
|
|
|
n = buffer_size;
|
2002-02-02 20:29:04 +01:00
|
|
|
if (start_delay <= 0) {
|
|
|
|
start_threshold = n + (double) rate * start_delay / 1000000;
|
|
|
|
} else
|
2001-04-19 23:18:24 +02:00
|
|
|
start_threshold = (double) rate * start_delay / 1000000;
|
2002-02-02 10:47:19 +01:00
|
|
|
if (start_threshold < 1)
|
|
|
|
start_threshold = 1;
|
2002-02-02 20:29:04 +01:00
|
|
|
if (start_threshold > n)
|
|
|
|
start_threshold = n;
|
2001-04-19 23:18:24 +02:00
|
|
|
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
|
|
|
|
assert(err >= 0);
|
|
|
|
if (stop_delay <= 0)
|
|
|
|
stop_threshold = buffer_size + (double) rate * stop_delay / 1000000;
|
|
|
|
else
|
|
|
|
stop_threshold = (double) rate * stop_delay / 1000000;
|
|
|
|
err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
|
|
|
|
assert(err >= 0);
|
|
|
|
|
2001-01-31 18:26:59 +01:00
|
|
|
if (snd_pcm_sw_params(handle, swparams) < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("unable to install sw params:"));
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_sw_params_dump(swparams, log);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2012-09-20 13:53:46 +02:00
|
|
|
if (setup_chmap())
|
|
|
|
prg_exit(EXIT_FAILURE);
|
2012-09-11 12:20:55 +02:00
|
|
|
|
2000-09-24 11:57:50 +02:00
|
|
|
if (verbose)
|
2001-01-17 12:00:34 +01:00
|
|
|
snd_pcm_dump(handle, log);
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2000-11-20 21:11:13 +01:00
|
|
|
bits_per_sample = snd_pcm_format_physical_width(hwparams.format);
|
2015-09-15 13:10:00 +02:00
|
|
|
significant_bits_per_sample = snd_pcm_format_width(hwparams.format);
|
2000-11-20 21:11:13 +01:00
|
|
|
bits_per_frame = bits_per_sample * hwparams.channels;
|
2001-01-15 12:06:55 +01:00
|
|
|
chunk_bytes = chunk_size * bits_per_frame / 8;
|
|
|
|
audiobuf = realloc(audiobuf, chunk_bytes);
|
1999-11-07 00:47:10 +01:00
|
|
|
if (audiobuf == NULL) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("not enough memory"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1999-11-07 00:47:10 +01:00
|
|
|
}
|
2001-01-15 12:06:55 +01:00
|
|
|
// fprintf(stderr, "real chunk_size = %i, frags = %i, total = %i\n", chunk_size, setup.buf.block.frags, setup.buf.block.frags * chunk_size);
|
2008-03-14 14:11:06 +01:00
|
|
|
|
|
|
|
/* stereo VU-meter isn't always available... */
|
|
|
|
if (vumeter == VUMETER_STEREO) {
|
|
|
|
if (hwparams.channels != 2 || !interleaved || verbose > 2)
|
|
|
|
vumeter = VUMETER_MONO;
|
2008-09-09 12:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* show mmap buffer arragment */
|
|
|
|
if (mmap_flag && verbose) {
|
|
|
|
const snd_pcm_channel_area_t *areas;
|
2009-11-09 11:48:23 +01:00
|
|
|
snd_pcm_uframes_t offset, size = chunk_size;
|
2008-09-09 12:53:28 +02:00
|
|
|
int i;
|
2009-11-09 11:48:23 +01:00
|
|
|
err = snd_pcm_mmap_begin(handle, &areas, &offset, &size);
|
2008-09-09 12:53:28 +02:00
|
|
|
if (err < 0) {
|
2010-07-15 10:40:21 +02:00
|
|
|
error(_("snd_pcm_mmap_begin problem: %s"), snd_strerror(err));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2008-09-09 12:53:28 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < hwparams.channels; i++)
|
|
|
|
fprintf(stderr, "mmap_area[%i] = %p,%u,%u (%u)\n", i, areas[i].addr, areas[i].first, areas[i].step, snd_pcm_format_physical_width(hwparams.format));
|
|
|
|
/* not required, but for sure */
|
|
|
|
snd_pcm_mmap_commit(handle, offset, 0);
|
2008-03-14 14:11:06 +01:00
|
|
|
}
|
2008-10-23 11:13:24 +02:00
|
|
|
|
|
|
|
buffer_frames = buffer_size; /* for position test */
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2010-07-15 10:40:21 +02:00
|
|
|
static void init_stdin(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
long flags;
|
|
|
|
|
2011-06-03 12:41:26 +02:00
|
|
|
if (!interactive)
|
|
|
|
return;
|
2013-03-22 13:21:25 +01:00
|
|
|
if (!isatty(fileno(stdin))) {
|
|
|
|
interactive = 0;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-15 13:39:14 +02:00
|
|
|
tcgetattr(fileno(stdin), &term);
|
|
|
|
term_c_lflag = term.c_lflag;
|
2010-07-15 10:40:21 +02:00
|
|
|
if (fd == fileno(stdin))
|
|
|
|
return;
|
|
|
|
flags = fcntl(fileno(stdin), F_GETFL);
|
|
|
|
if (flags < 0 || fcntl(fileno(stdin), F_SETFL, flags|O_NONBLOCK) < 0)
|
|
|
|
fprintf(stderr, _("stdin O_NONBLOCK flag setup failed\n"));
|
|
|
|
term.c_lflag &= ~ICANON;
|
|
|
|
tcsetattr(fileno(stdin), TCSANOW, &term);
|
|
|
|
}
|
|
|
|
|
2010-07-15 13:39:14 +02:00
|
|
|
static void done_stdin(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
2011-06-03 12:41:26 +02:00
|
|
|
if (!interactive)
|
|
|
|
return;
|
2010-08-18 08:23:09 +02:00
|
|
|
if (fd == fileno(stdin) || term_c_lflag == -1)
|
2010-07-15 13:39:14 +02:00
|
|
|
return;
|
|
|
|
tcgetattr(fileno(stdin), &term);
|
|
|
|
term.c_lflag = term_c_lflag;
|
|
|
|
tcsetattr(fileno(stdin), TCSANOW, &term);
|
|
|
|
}
|
|
|
|
|
2010-07-15 10:40:21 +02:00
|
|
|
static void do_pause(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
unsigned char b;
|
|
|
|
|
|
|
|
if (!can_pause) {
|
|
|
|
fprintf(stderr, _("\rPAUSE command ignored (no hw support)\n"));
|
|
|
|
return;
|
|
|
|
}
|
2016-11-28 17:32:00 +01:00
|
|
|
if (snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED)
|
|
|
|
suspend();
|
|
|
|
|
2010-07-15 10:40:21 +02:00
|
|
|
err = snd_pcm_pause(handle, 1);
|
|
|
|
if (err < 0) {
|
|
|
|
error(_("pause push error: %s"), snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
while (read(fileno(stdin), &b, 1) != 1);
|
|
|
|
if (b == ' ' || b == '\r') {
|
|
|
|
while (read(fileno(stdin), &b, 1) == 1);
|
2016-11-28 17:32:00 +01:00
|
|
|
if (snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED)
|
|
|
|
suspend();
|
2010-07-15 10:40:21 +02:00
|
|
|
err = snd_pcm_pause(handle, 0);
|
|
|
|
if (err < 0)
|
|
|
|
error(_("pause release error: %s"), snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_stdin(void)
|
|
|
|
{
|
|
|
|
unsigned char b;
|
|
|
|
|
2011-06-03 12:41:26 +02:00
|
|
|
if (!interactive)
|
|
|
|
return;
|
2010-07-15 10:40:21 +02:00
|
|
|
if (fd != fileno(stdin)) {
|
|
|
|
while (read(fileno(stdin), &b, 1) == 1) {
|
|
|
|
if (b == ' ' || b == '\r') {
|
|
|
|
while (read(fileno(stdin), &b, 1) == 1);
|
|
|
|
fprintf(stderr, _("\r=== PAUSE === "));
|
|
|
|
fflush(stderr);
|
|
|
|
do_pause();
|
|
|
|
fprintf(stderr, " \r");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-25 12:54:05 +01:00
|
|
|
#ifndef timersub
|
|
|
|
#define timersub(a, b, result) \
|
|
|
|
do { \
|
|
|
|
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
|
|
|
|
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
|
|
|
|
if ((result)->tv_usec < 0) { \
|
|
|
|
--(result)->tv_sec; \
|
|
|
|
(result)->tv_usec += 1000000; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
2009-04-08 09:22:16 +02:00
|
|
|
#ifndef timermsub
|
|
|
|
#define timermsub(a, b, result) \
|
|
|
|
do { \
|
|
|
|
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
|
|
|
|
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
|
|
|
|
if ((result)->tv_nsec < 0) { \
|
|
|
|
--(result)->tv_sec; \
|
|
|
|
(result)->tv_nsec += 1000000000L; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
2001-10-04 17:04:44 +02:00
|
|
|
/* I/O error handler */
|
|
|
|
static void xrun(void)
|
2000-05-08 20:54:14 +02:00
|
|
|
{
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_status_t *status;
|
2000-08-19 11:08:39 +02:00
|
|
|
int res;
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_status_alloca(&status);
|
|
|
|
if ((res = snd_pcm_status(handle, status))<0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("status error: %s"), snd_strerror(res));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2001-02-04 18:03:19 +01:00
|
|
|
if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
|
2012-03-16 04:51:02 +01:00
|
|
|
if (fatal_errors) {
|
|
|
|
error(_("fatal %s: %s"),
|
|
|
|
stream == SND_PCM_STREAM_PLAYBACK ? _("underrun") : _("overrun"),
|
|
|
|
snd_strerror(res));
|
|
|
|
prg_exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-04-08 09:22:16 +02:00
|
|
|
if (monotonic) {
|
|
|
|
#ifdef HAVE_CLOCK_GETTIME
|
|
|
|
struct timespec now, diff, tstamp;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
snd_pcm_status_get_trigger_htstamp(status, &tstamp);
|
|
|
|
timermsub(&now, &tstamp, &diff);
|
|
|
|
fprintf(stderr, _("%s!!! (at least %.3f ms long)\n"),
|
|
|
|
stream == SND_PCM_STREAM_PLAYBACK ? _("underrun") : _("overrun"),
|
2014-01-21 21:25:12 +01:00
|
|
|
diff.tv_sec * 1000 + diff.tv_nsec / 1000000.0);
|
2009-04-08 09:22:16 +02:00
|
|
|
#else
|
|
|
|
fprintf(stderr, "%s !!!\n", _("underrun"));
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
struct timeval now, diff, tstamp;
|
|
|
|
gettimeofday(&now, 0);
|
|
|
|
snd_pcm_status_get_trigger_tstamp(status, &tstamp);
|
|
|
|
timersub(&now, &tstamp, &diff);
|
|
|
|
fprintf(stderr, _("%s!!! (at least %.3f ms long)\n"),
|
|
|
|
stream == SND_PCM_STREAM_PLAYBACK ? _("underrun") : _("overrun"),
|
|
|
|
diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
|
|
|
|
}
|
2000-09-24 11:57:50 +02:00
|
|
|
if (verbose) {
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Status:\n"));
|
2001-01-31 18:26:59 +01:00
|
|
|
snd_pcm_status_dump(status, log);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-08-19 11:08:39 +02:00
|
|
|
if ((res = snd_pcm_prepare(handle))<0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("xrun: prepare error: %s"), snd_strerror(res));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
return; /* ok, data should be accepted again */
|
2003-07-30 19:54:42 +02:00
|
|
|
} if (snd_pcm_status_get_state(status) == SND_PCM_STATE_DRAINING) {
|
|
|
|
if (verbose) {
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Status(DRAINING):\n"));
|
2003-07-30 19:54:42 +02:00
|
|
|
snd_pcm_status_dump(status, log);
|
|
|
|
}
|
|
|
|
if (stream == SND_PCM_STREAM_CAPTURE) {
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("capture stream format change? attempting recover...\n"));
|
2003-07-30 19:54:42 +02:00
|
|
|
if ((res = snd_pcm_prepare(handle))<0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("xrun(DRAINING): prepare error: %s"), snd_strerror(res));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2003-07-30 19:54:42 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (verbose) {
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Status(R/W):\n"));
|
2003-07-30 19:54:42 +02:00
|
|
|
snd_pcm_status_dump(status, log);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("read/write error, state = %s"), snd_pcm_state_name(snd_pcm_status_get_state(status)));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
2001-10-04 17:04:44 +02:00
|
|
|
/* I/O suspend handler */
|
|
|
|
static void suspend(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2018-10-14 17:00:26 +02:00
|
|
|
if (!quiet_mode) {
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Suspended. Trying resume. ")); fflush(stderr);
|
2018-10-14 17:00:26 +02:00
|
|
|
}
|
2001-10-04 18:41:45 +02:00
|
|
|
while ((res = snd_pcm_resume(handle)) == -EAGAIN)
|
2001-10-24 16:41:10 +02:00
|
|
|
sleep(1); /* wait until suspend flag is released */
|
2001-10-04 17:04:44 +02:00
|
|
|
if (res < 0) {
|
2018-10-14 17:00:26 +02:00
|
|
|
if (!quiet_mode) {
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Failed. Restarting stream. ")); fflush(stderr);
|
2018-10-14 17:00:26 +02:00
|
|
|
}
|
2001-10-04 17:04:44 +02:00
|
|
|
if ((res = snd_pcm_prepare(handle)) < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("suspend: prepare error: %s"), snd_strerror(res));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2001-10-04 17:04:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!quiet_mode)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Done.\n"));
|
2001-10-04 17:04:44 +02:00
|
|
|
}
|
|
|
|
|
2008-03-14 14:11:06 +01:00
|
|
|
static void print_vu_meter_mono(int perc, int maxperc)
|
|
|
|
{
|
|
|
|
const int bar_length = 50;
|
|
|
|
char line[80];
|
|
|
|
int val;
|
|
|
|
|
|
|
|
for (val = 0; val <= perc * bar_length / 100 && val < bar_length; val++)
|
|
|
|
line[val] = '#';
|
|
|
|
for (; val <= maxperc * bar_length / 100 && val < bar_length; val++)
|
|
|
|
line[val] = ' ';
|
|
|
|
line[val] = '+';
|
|
|
|
for (++val; val <= bar_length; val++)
|
|
|
|
line[val] = ' ';
|
|
|
|
if (maxperc > 99)
|
|
|
|
sprintf(line + val, "| MAX");
|
|
|
|
else
|
|
|
|
sprintf(line + val, "| %02i%%", maxperc);
|
2012-05-06 05:10:01 +02:00
|
|
|
fputs(line, stderr);
|
2008-03-14 14:11:06 +01:00
|
|
|
if (perc > 100)
|
2012-05-06 05:10:01 +02:00
|
|
|
fprintf(stderr, _(" !clip "));
|
2008-03-14 14:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_vu_meter_stereo(int *perc, int *maxperc)
|
|
|
|
{
|
|
|
|
const int bar_length = 35;
|
|
|
|
char line[80];
|
|
|
|
int c;
|
|
|
|
|
|
|
|
memset(line, ' ', sizeof(line) - 1);
|
|
|
|
line[bar_length + 3] = '|';
|
|
|
|
|
|
|
|
for (c = 0; c < 2; c++) {
|
|
|
|
int p = perc[c] * bar_length / 100;
|
|
|
|
char tmp[4];
|
|
|
|
if (p > bar_length)
|
|
|
|
p = bar_length;
|
|
|
|
if (c)
|
|
|
|
memset(line + bar_length + 6 + 1, '#', p);
|
|
|
|
else
|
|
|
|
memset(line + bar_length - p - 1, '#', p);
|
|
|
|
p = maxperc[c] * bar_length / 100;
|
|
|
|
if (p > bar_length)
|
|
|
|
p = bar_length;
|
|
|
|
if (c)
|
|
|
|
line[bar_length + 6 + 1 + p] = '+';
|
|
|
|
else
|
|
|
|
line[bar_length - p - 1] = '+';
|
2019-11-20 20:13:55 +01:00
|
|
|
if (ABS(maxperc[c]) > 99)
|
2008-03-14 14:11:06 +01:00
|
|
|
sprintf(tmp, "MAX");
|
|
|
|
else
|
|
|
|
sprintf(tmp, "%02d%%", maxperc[c]);
|
|
|
|
if (c)
|
|
|
|
memcpy(line + bar_length + 3 + 1, tmp, 3);
|
|
|
|
else
|
|
|
|
memcpy(line + bar_length, tmp, 3);
|
|
|
|
}
|
|
|
|
line[bar_length * 2 + 6 + 2] = 0;
|
2012-05-06 05:10:01 +02:00
|
|
|
fputs(line, stderr);
|
2008-03-14 14:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_vu_meter(signed int *perc, signed int *maxperc)
|
|
|
|
{
|
|
|
|
if (vumeter == VUMETER_STEREO)
|
|
|
|
print_vu_meter_stereo(perc, maxperc);
|
|
|
|
else
|
|
|
|
print_vu_meter_mono(*perc, *maxperc);
|
|
|
|
}
|
|
|
|
|
2001-09-06 10:57:52 +02:00
|
|
|
/* peak handler */
|
|
|
|
static void compute_max_peak(u_char *data, size_t count)
|
|
|
|
{
|
2008-03-14 14:11:06 +01:00
|
|
|
signed int val, max, perc[2], max_peak[2];
|
2005-10-10 14:52:28 +02:00
|
|
|
static int run = 0;
|
2005-01-03 14:33:06 +01:00
|
|
|
size_t ocount = count;
|
2005-10-10 14:52:28 +02:00
|
|
|
int format_little_endian = snd_pcm_format_little_endian(hwparams.format);
|
2008-03-14 14:11:06 +01:00
|
|
|
int ichans, c;
|
|
|
|
|
|
|
|
if (vumeter == VUMETER_STEREO)
|
|
|
|
ichans = 2;
|
|
|
|
else
|
|
|
|
ichans = 1;
|
2005-10-10 14:52:28 +02:00
|
|
|
|
2008-03-14 14:11:06 +01:00
|
|
|
memset(max_peak, 0, sizeof(max_peak));
|
2005-01-03 14:33:06 +01:00
|
|
|
switch (bits_per_sample) {
|
|
|
|
case 8: {
|
|
|
|
signed char *valp = (signed char *)data;
|
|
|
|
signed char mask = snd_pcm_format_silence(hwparams.format);
|
2008-03-14 14:11:06 +01:00
|
|
|
c = 0;
|
2005-01-03 14:33:06 +01:00
|
|
|
while (count-- > 0) {
|
|
|
|
val = *valp++ ^ mask;
|
|
|
|
val = abs(val);
|
2008-03-14 14:11:06 +01:00
|
|
|
if (max_peak[c] < val)
|
|
|
|
max_peak[c] = val;
|
|
|
|
if (vumeter == VUMETER_STEREO)
|
|
|
|
c = !c;
|
2005-01-03 14:33:06 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 16: {
|
|
|
|
signed short *valp = (signed short *)data;
|
|
|
|
signed short mask = snd_pcm_format_silence_16(hwparams.format);
|
2005-10-10 14:52:28 +02:00
|
|
|
signed short sval;
|
|
|
|
|
2005-01-03 14:33:06 +01:00
|
|
|
count /= 2;
|
2008-03-14 14:11:06 +01:00
|
|
|
c = 0;
|
2005-01-03 14:33:06 +01:00
|
|
|
while (count-- > 0) {
|
2005-10-10 14:52:28 +02:00
|
|
|
if (format_little_endian)
|
2011-06-03 14:35:44 +02:00
|
|
|
sval = le16toh(*valp);
|
2008-03-14 14:11:06 +01:00
|
|
|
else
|
2011-06-03 14:35:44 +02:00
|
|
|
sval = be16toh(*valp);
|
2005-10-10 14:52:28 +02:00
|
|
|
sval = abs(sval) ^ mask;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (max_peak[c] < sval)
|
|
|
|
max_peak[c] = sval;
|
2005-10-10 14:52:28 +02:00
|
|
|
valp++;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter == VUMETER_STEREO)
|
|
|
|
c = !c;
|
2005-10-10 14:52:28 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 24: {
|
|
|
|
unsigned char *valp = data;
|
|
|
|
signed int mask = snd_pcm_format_silence_32(hwparams.format);
|
|
|
|
|
|
|
|
count /= 3;
|
2008-03-14 14:11:06 +01:00
|
|
|
c = 0;
|
2005-10-10 14:52:28 +02:00
|
|
|
while (count-- > 0) {
|
|
|
|
if (format_little_endian) {
|
|
|
|
val = valp[0] | (valp[1]<<8) | (valp[2]<<16);
|
|
|
|
} else {
|
|
|
|
val = (valp[0]<<16) | (valp[1]<<8) | valp[2];
|
|
|
|
}
|
|
|
|
/* Correct signed bit in 32-bit value */
|
|
|
|
if (val & (1<<(bits_per_sample-1))) {
|
|
|
|
val |= 0xff<<24; /* Negate upper bits too */
|
|
|
|
}
|
|
|
|
val = abs(val) ^ mask;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (max_peak[c] < val)
|
|
|
|
max_peak[c] = val;
|
2005-10-10 14:52:28 +02:00
|
|
|
valp += 3;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter == VUMETER_STEREO)
|
|
|
|
c = !c;
|
2001-09-06 10:57:52 +02:00
|
|
|
}
|
2005-01-03 14:33:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 32: {
|
|
|
|
signed int *valp = (signed int *)data;
|
|
|
|
signed int mask = snd_pcm_format_silence_32(hwparams.format);
|
2008-03-14 14:11:06 +01:00
|
|
|
|
2005-01-03 14:33:06 +01:00
|
|
|
count /= 4;
|
2008-03-14 14:11:06 +01:00
|
|
|
c = 0;
|
2005-01-03 14:33:06 +01:00
|
|
|
while (count-- > 0) {
|
2005-10-10 14:52:28 +02:00
|
|
|
if (format_little_endian)
|
2011-06-03 14:35:44 +02:00
|
|
|
val = le32toh(*valp);
|
2008-03-14 14:11:06 +01:00
|
|
|
else
|
2011-06-03 14:35:44 +02:00
|
|
|
val = be32toh(*valp);
|
2005-10-10 14:52:28 +02:00
|
|
|
val = abs(val) ^ mask;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (max_peak[c] < val)
|
|
|
|
max_peak[c] = val;
|
2005-10-10 14:52:28 +02:00
|
|
|
valp++;
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter == VUMETER_STEREO)
|
|
|
|
c = !c;
|
2005-01-03 14:33:06 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2005-10-10 14:52:28 +02:00
|
|
|
if (run == 0) {
|
2008-04-16 14:43:01 +02:00
|
|
|
fprintf(stderr, _("Unsupported bit size %d.\n"), (int)bits_per_sample);
|
2005-10-10 14:52:28 +02:00
|
|
|
run = 1;
|
|
|
|
}
|
|
|
|
return;
|
2001-09-06 10:57:52 +02:00
|
|
|
}
|
2015-09-15 13:10:00 +02:00
|
|
|
max = 1 << (significant_bits_per_sample-1);
|
2002-10-02 16:42:00 +02:00
|
|
|
if (max <= 0)
|
2001-10-02 09:43:12 +02:00
|
|
|
max = 0x7fffffff;
|
2005-12-06 12:23:00 +01:00
|
|
|
|
2008-03-14 14:11:06 +01:00
|
|
|
for (c = 0; c < ichans; c++) {
|
|
|
|
if (bits_per_sample > 16)
|
|
|
|
perc[c] = max_peak[c] / (max / 100);
|
|
|
|
else
|
|
|
|
perc[c] = max_peak[c] * 100 / max;
|
|
|
|
}
|
2005-12-06 12:23:00 +01:00
|
|
|
|
2008-03-14 14:11:06 +01:00
|
|
|
if (interleaved && verbose <= 2) {
|
|
|
|
static int maxperc[2];
|
2005-12-06 12:23:00 +01:00
|
|
|
static time_t t=0;
|
|
|
|
const time_t tt=time(NULL);
|
|
|
|
if(tt>t) {
|
|
|
|
t=tt;
|
2008-03-14 14:11:06 +01:00
|
|
|
maxperc[0] = 0;
|
|
|
|
maxperc[1] = 0;
|
2005-12-06 12:23:00 +01:00
|
|
|
}
|
2008-03-14 14:11:06 +01:00
|
|
|
for (c = 0; c < ichans; c++)
|
|
|
|
if (perc[c] > maxperc[c])
|
|
|
|
maxperc[c] = perc[c];
|
2005-12-06 12:23:00 +01:00
|
|
|
|
2012-05-06 05:10:01 +02:00
|
|
|
putc('\r', stderr);
|
2008-03-14 14:11:06 +01:00
|
|
|
print_vu_meter(perc, maxperc);
|
2012-05-06 05:10:01 +02:00
|
|
|
fflush(stderr);
|
2005-12-06 12:23:00 +01:00
|
|
|
}
|
|
|
|
else if(verbose==3) {
|
2014-06-13 03:28:49 +02:00
|
|
|
fprintf(stderr, _("Max peak (%li samples): 0x%08x "), (long)ocount, max_peak[0]);
|
2005-12-06 12:23:00 +01:00
|
|
|
for (val = 0; val < 20; val++)
|
2008-03-14 14:11:06 +01:00
|
|
|
if (val <= perc[0] / 5)
|
2014-06-13 03:28:49 +02:00
|
|
|
putc('#', stderr);
|
2005-12-06 12:23:00 +01:00
|
|
|
else
|
2014-06-13 03:28:49 +02:00
|
|
|
putc(' ', stderr);
|
2012-05-06 05:10:01 +02:00
|
|
|
fprintf(stderr, " %i%%\n", perc[0]);
|
|
|
|
fflush(stderr);
|
2005-12-06 12:23:00 +01:00
|
|
|
}
|
2001-09-06 10:57:52 +02:00
|
|
|
}
|
|
|
|
|
2008-10-23 11:13:24 +02:00
|
|
|
static void do_test_position(void)
|
|
|
|
{
|
2009-04-09 09:48:27 +02:00
|
|
|
static long counter = 0;
|
|
|
|
static time_t tmr = -1;
|
|
|
|
time_t now;
|
|
|
|
static float availsum, delaysum, samples;
|
|
|
|
static snd_pcm_sframes_t maxavail, maxdelay;
|
|
|
|
static snd_pcm_sframes_t minavail, mindelay;
|
|
|
|
static snd_pcm_sframes_t badavail = 0, baddelay = 0;
|
|
|
|
snd_pcm_sframes_t outofrange;
|
2008-10-23 11:13:24 +02:00
|
|
|
snd_pcm_sframes_t avail, delay;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = snd_pcm_avail_delay(handle, &avail, &delay);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2009-04-09 09:48:27 +02:00
|
|
|
outofrange = (test_coef * (snd_pcm_sframes_t)buffer_frames) / 2;
|
|
|
|
if (avail > outofrange || avail < -outofrange ||
|
|
|
|
delay > outofrange || delay < -outofrange) {
|
|
|
|
badavail = avail; baddelay = delay;
|
|
|
|
availsum = delaysum = samples = 0;
|
|
|
|
maxavail = maxdelay = 0;
|
|
|
|
minavail = mindelay = buffer_frames * 16;
|
|
|
|
fprintf(stderr, _("Suspicious buffer position (%li total): "
|
|
|
|
"avail = %li, delay = %li, buffer = %li\n"),
|
|
|
|
++counter, (long)avail, (long)delay, (long)buffer_frames);
|
2008-10-23 11:13:24 +02:00
|
|
|
} else if (verbose) {
|
2009-04-09 09:48:27 +02:00
|
|
|
time(&now);
|
|
|
|
if (tmr == (time_t) -1) {
|
|
|
|
tmr = now;
|
|
|
|
availsum = delaysum = samples = 0;
|
|
|
|
maxavail = maxdelay = 0;
|
|
|
|
minavail = mindelay = buffer_frames * 16;
|
|
|
|
}
|
|
|
|
if (avail > maxavail)
|
|
|
|
maxavail = avail;
|
|
|
|
if (delay > maxdelay)
|
|
|
|
maxdelay = delay;
|
|
|
|
if (avail < minavail)
|
|
|
|
minavail = avail;
|
|
|
|
if (delay < mindelay)
|
|
|
|
mindelay = delay;
|
|
|
|
availsum += avail;
|
|
|
|
delaysum += delay;
|
|
|
|
samples++;
|
|
|
|
if (avail != 0 && now != tmr) {
|
|
|
|
fprintf(stderr, "BUFPOS: avg%li/%li "
|
|
|
|
"min%li/%li max%li/%li (%li) (%li:%li/%li)\n",
|
|
|
|
(long)(availsum / samples),
|
|
|
|
(long)(delaysum / samples),
|
|
|
|
(long)minavail, (long)mindelay,
|
|
|
|
(long)maxavail, (long)maxdelay,
|
|
|
|
(long)buffer_frames,
|
|
|
|
counter, badavail, baddelay);
|
|
|
|
tmr = now;
|
|
|
|
}
|
2008-10-23 11:13:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 13:53:46 +02:00
|
|
|
/*
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_SUPPORT_CHMAP
|
|
|
|
static u_char *remap_data(u_char *data, size_t count)
|
|
|
|
{
|
|
|
|
static u_char *tmp, *src, *dst;
|
|
|
|
static size_t tmp_size;
|
|
|
|
size_t sample_bytes = bits_per_sample / 8;
|
|
|
|
size_t step = bits_per_frame / 8;
|
|
|
|
size_t chunk_bytes;
|
|
|
|
unsigned int ch, i;
|
|
|
|
|
|
|
|
if (!hw_map)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
chunk_bytes = count * bits_per_frame / 8;
|
|
|
|
if (tmp_size < chunk_bytes) {
|
|
|
|
free(tmp);
|
|
|
|
tmp = malloc(chunk_bytes);
|
|
|
|
if (!tmp) {
|
|
|
|
error(_("not enough memory"));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
tmp_size = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = data;
|
|
|
|
dst = tmp;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
for (ch = 0; ch < hwparams.channels; ch++) {
|
|
|
|
memcpy(dst, src + sample_bytes * hw_map[ch],
|
|
|
|
sample_bytes);
|
|
|
|
dst += sample_bytes;
|
|
|
|
}
|
|
|
|
src += step;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_char **remap_datav(u_char **data, size_t count)
|
|
|
|
{
|
|
|
|
static u_char **tmp;
|
|
|
|
unsigned int ch;
|
|
|
|
|
|
|
|
if (!hw_map)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
if (!tmp) {
|
|
|
|
tmp = malloc(sizeof(*tmp) * hwparams.channels);
|
|
|
|
if (!tmp) {
|
|
|
|
error(_("not enough memory"));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (ch = 0; ch < hwparams.channels; ch++)
|
|
|
|
tmp[ch] = data[hw_map[ch]];
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define remap_data(data, count) (data)
|
2012-09-24 16:18:54 +02:00
|
|
|
#define remap_datav(data, count) (data)
|
2012-09-20 13:53:46 +02:00
|
|
|
#endif
|
|
|
|
|
2000-05-08 20:54:14 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2008-01-08 18:38:32 +01:00
|
|
|
if (count < chunk_size) {
|
2001-01-15 12:06:55 +01:00
|
|
|
snd_pcm_format_set_silence(hwparams.format, data + count * bits_per_frame / 8, (chunk_size - count) * hwparams.channels);
|
|
|
|
count = chunk_size;
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
2012-09-20 13:53:46 +02:00
|
|
|
data = remap_data(data, count);
|
2014-06-13 10:52:26 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2010-07-15 10:40:21 +02:00
|
|
|
check_stdin();
|
2000-09-24 11:57:50 +02:00
|
|
|
r = writei_func(handle, data, count);
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2003-02-04 17:03:12 +01:00
|
|
|
if (r == -EAGAIN || (r >= 0 && (size_t)r < count)) {
|
2009-04-08 09:22:16 +02:00
|
|
|
if (!test_nowait)
|
2010-07-15 10:40:21 +02:00
|
|
|
snd_pcm_wait(handle, 100);
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2001-10-04 17:04:44 +02:00
|
|
|
} else if (r == -ESTRPIPE) {
|
|
|
|
suspend();
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error: %s"), snd_strerror(r));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r > 0) {
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter)
|
2001-09-06 10:57:52 +02:00
|
|
|
compute_max_peak(data, r * hwparams.channels);
|
2000-05-18 16:42:43 +02:00
|
|
|
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;
|
|
|
|
|
2008-01-08 18:38:32 +01:00
|
|
|
if (count != chunk_size) {
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-06-10 14:39:52 +02:00
|
|
|
size_t offset = count;
|
2001-01-15 12:06:55 +01:00
|
|
|
size_t remaining = chunk_size - count;
|
2000-05-27 18:52:19 +02:00
|
|
|
for (channel = 0; channel < channels; channel++)
|
2000-11-20 21:11:13 +01:00
|
|
|
snd_pcm_format_set_silence(hwparams.format, data[channel] + offset * bits_per_sample / 8, remaining);
|
2001-01-15 12:06:55 +01:00
|
|
|
count = chunk_size;
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
2012-09-20 13:53:46 +02:00
|
|
|
data = remap_datav(data, count);
|
2014-06-13 10:52:26 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
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;
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2010-07-15 10:40:21 +02:00
|
|
|
check_stdin();
|
2000-09-24 11:57:50 +02:00
|
|
|
r = writen_func(handle, bufs, count);
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2003-02-04 17:03:12 +01:00
|
|
|
if (r == -EAGAIN || (r >= 0 && (size_t)r < count)) {
|
2009-04-08 09:22:16 +02:00
|
|
|
if (!test_nowait)
|
2010-07-15 10:40:21 +02:00
|
|
|
snd_pcm_wait(handle, 100);
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2001-10-04 17:04:44 +02:00
|
|
|
} else if (r == -ESTRPIPE) {
|
|
|
|
suspend();
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("writev error: %s"), snd_strerror(r));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
if (r > 0) {
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter) {
|
2001-09-06 10:57:52 +02:00
|
|
|
for (channel = 0; channel < channels; channel++)
|
|
|
|
compute_max_peak(data[channel], r);
|
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
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;
|
|
|
|
|
2008-01-08 18:38:32 +01:00
|
|
|
if (count != chunk_size) {
|
2001-01-15 12:06:55 +01:00
|
|
|
count = chunk_size;
|
2000-07-06 19:20:49 +02:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2014-06-13 10:52:26 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2010-07-15 10:40:21 +02:00
|
|
|
check_stdin();
|
2000-09-24 11:57:50 +02:00
|
|
|
r = readi_func(handle, data, count);
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2003-02-04 17:03:12 +01:00
|
|
|
if (r == -EAGAIN || (r >= 0 && (size_t)r < count)) {
|
2009-04-08 09:22:16 +02:00
|
|
|
if (!test_nowait)
|
2010-07-15 10:40:21 +02:00
|
|
|
snd_pcm_wait(handle, 100);
|
2000-05-08 20:54:14 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2001-10-04 17:04:44 +02:00
|
|
|
} else if (r == -ESTRPIPE) {
|
|
|
|
suspend();
|
2000-05-08 20:54:14 +02:00
|
|
|
} else if (r < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("read error: %s"), snd_strerror(r));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r > 0) {
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter)
|
2001-09-06 10:57:52 +02:00
|
|
|
compute_max_peak(data, r * hwparams.channels);
|
2000-05-18 16:42:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-10-01 15:43:57 +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;
|
|
|
|
|
2008-01-08 18:38:32 +01:00
|
|
|
if (count != chunk_size) {
|
2001-01-15 12:06:55 +01:00
|
|
|
count = chunk_size;
|
2000-07-06 19:20:49 +02:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
|
2014-06-13 10:52:26 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
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;
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2010-07-15 10:40:21 +02:00
|
|
|
check_stdin();
|
2000-09-24 11:57:50 +02:00
|
|
|
r = readn_func(handle, bufs, count);
|
2008-10-23 11:13:24 +02:00
|
|
|
if (test_position)
|
|
|
|
do_test_position();
|
2003-02-04 17:03:12 +01:00
|
|
|
if (r == -EAGAIN || (r >= 0 && (size_t)r < count)) {
|
2009-04-08 09:22:16 +02:00
|
|
|
if (!test_nowait)
|
2010-07-15 10:40:21 +02:00
|
|
|
snd_pcm_wait(handle, 100);
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r == -EPIPE) {
|
2000-09-24 11:57:50 +02:00
|
|
|
xrun();
|
2001-10-04 17:04:44 +02:00
|
|
|
} else if (r == -ESTRPIPE) {
|
|
|
|
suspend();
|
2000-05-18 16:42:43 +02:00
|
|
|
} else if (r < 0) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("readv error: %s"), snd_strerror(r));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
if (r > 0) {
|
2008-03-14 14:11:06 +01:00
|
|
|
if (vumeter) {
|
2001-09-06 10:57:52 +02:00
|
|
|
for (channel = 0; channel < channels; channel++)
|
|
|
|
compute_max_peak(data[channel], r);
|
|
|
|
}
|
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
|
|
|
}
|
2014-10-01 15:43:57 +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;
|
|
|
|
|
2016-12-19 09:34:39 +01:00
|
|
|
while (count > 0 && !in_aborting) {
|
2000-05-08 20:54:14 +02:00
|
|
|
size = count;
|
2001-01-15 12:06:55 +01:00
|
|
|
if (size > chunk_bytes - buffer_pos)
|
|
|
|
size = chunk_bytes - buffer_pos;
|
2000-05-08 20:54:14 +02:00
|
|
|
memcpy(audiobuf + buffer_pos, data, size);
|
|
|
|
data += size;
|
|
|
|
count -= size;
|
|
|
|
buffer_pos += size;
|
2003-02-04 17:03:12 +01:00
|
|
|
if ((size_t)buffer_pos == chunk_bytes) {
|
|
|
|
if ((size_t)(r = pcm_write(audiobuf, chunk_size)) != chunk_size)
|
2000-05-08 20:54:14 +02:00
|
|
|
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;
|
2005-12-16 19:44:14 +01:00
|
|
|
u_char *buf;
|
2000-05-08 20:54:14 +02:00
|
|
|
|
2005-12-16 19:44:14 +01:00
|
|
|
buf = (u_char *) malloc(chunk_bytes);
|
2000-05-08 20:54:14 +02:00
|
|
|
if (buf == NULL) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("can't allocate buffer for silence"));
|
2000-05-08 20:54:14 +02:00
|
|
|
return; /* not fatal error */
|
|
|
|
}
|
2001-01-15 12:06:55 +01:00
|
|
|
snd_pcm_format_set_silence(hwparams.format, buf, chunk_size * hwparams.channels);
|
2013-04-08 13:30:54 +02:00
|
|
|
while (x > 0 && !in_aborting) {
|
2000-05-08 20:54:14 +02:00
|
|
|
l = x;
|
2001-01-15 12:06:55 +01:00
|
|
|
if (l > chunk_size)
|
|
|
|
l = chunk_size;
|
2003-02-04 17:03:12 +01:00
|
|
|
if (voc_pcm_write(buf, l) != (ssize_t)l) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
x -= l;
|
|
|
|
}
|
2004-09-13 09:04:53 +02:00
|
|
|
free(buf);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void voc_pcm_flush(void)
|
|
|
|
{
|
|
|
|
if (buffer_pos > 0) {
|
2000-06-10 14:39:52 +02:00
|
|
|
size_t b;
|
2008-01-08 18:38:32 +01:00
|
|
|
if (snd_pcm_format_set_silence(hwparams.format, audiobuf + buffer_pos, chunk_bytes - buffer_pos * 8 / bits_per_sample) < 0)
|
|
|
|
fprintf(stderr, _("voc_pcm_flush - silence error"));
|
|
|
|
b = chunk_size;
|
2003-02-04 17:03:12 +01:00
|
|
|
if (pcm_write(audiobuf, b) != (ssize_t)b)
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("voc_pcm_flush error"));
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2007-12-14 15:29:51 +01:00
|
|
|
snd_pcm_nonblock(handle, 0);
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2007-12-14 15:29:51 +01:00
|
|
|
snd_pcm_nonblock(handle, nonblock);
|
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;
|
2003-08-27 13:08:27 +02:00
|
|
|
off64_t 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) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("malloc error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
1998-11-27 16:17:45 +01:00
|
|
|
if (!quiet_mode) {
|
2005-06-23 12:38:06 +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 */
|
2003-02-04 17:03:12 +01:00
|
|
|
while (ofs > (ssize_t)chunk_bytes) {
|
|
|
|
if ((size_t)safe_read(fd, buf, chunk_bytes) != chunk_bytes) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("read error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2001-01-15 12:06:55 +01:00
|
|
|
ofs -= chunk_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) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("read error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-08 20:54:14 +02:00
|
|
|
}
|
2004-07-01 11:36:54 +02:00
|
|
|
hwparams.format = DEFAULT_FORMAT;
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.channels = 1;
|
|
|
|
hwparams.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;
|
2013-04-08 13:30:54 +02:00
|
|
|
while (!in_aborting) {
|
1998-11-27 16:17:45 +01:00
|
|
|
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;
|
2001-01-15 12:06:55 +01:00
|
|
|
if ((l = safe_read(fd, buf + in_buffer, chunk_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);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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) {
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.rate = (int) (vd->tc);
|
|
|
|
hwparams.rate = 1000000 / (256 - hwparams.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 */
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("can't play packed .voc files"));
|
1998-11-27 16:17:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2000-11-20 21:11:13 +01:00
|
|
|
if (hwparams.channels == 2) /* if we are in Stereo-Mode, switch back */
|
|
|
|
hwparams.channels = 1;
|
1998-11-27 16:17:45 +01:00
|
|
|
} else { /* there was extended block */
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.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));
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.rate = (int) (*data);
|
1998-11-27 16:17:45 +01:00
|
|
|
COUNT1(1);
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.rate = 1000000 / (256 - hwparams.rate);
|
2000-05-18 16:42:43 +02:00
|
|
|
set_params();
|
1998-08-13 17:43:39 +02:00
|
|
|
#if 0
|
2012-09-24 16:18:54 +02:00
|
|
|
{
|
|
|
|
size_t silence;
|
|
|
|
silence = (((size_t) * sp) * 1000) / hwparams.rate;
|
|
|
|
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 */
|
2003-08-27 13:08:27 +02:00
|
|
|
if ((filepos = lseek64(fd, 0, 1)) < 0) {
|
2005-06-23 12:38:06 +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
|
2003-08-27 13:08:27 +02:00
|
|
|
lseek64(fd, filepos, 0);
|
1998-11-27 16:17:45 +01:00
|
|
|
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));
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams.rate = (int) (eb->tc);
|
|
|
|
hwparams.rate = 256000000L / (65536 - hwparams.rate);
|
|
|
|
hwparams.channels = eb->mode == VOC_MODE_STEREO ? 2 : 1;
|
|
|
|
if (hwparams.channels == 2)
|
|
|
|
hwparams.rate = hwparams.rate >> 1;
|
1998-11-27 16:17:45 +01:00
|
|
|
if (eb->pack) { /* /dev/dsp can't it */
|
2005-06-23 12:38:06 +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:
|
2005-06-23 12:38:06 +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;
|
2003-02-04 17:03:12 +01:00
|
|
|
if (nextblock < (size_t)l)
|
1998-11-27 16:17:45 +01:00
|
|
|
l = nextblock;
|
|
|
|
if (l) {
|
|
|
|
if (output && !quiet_mode) {
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(2, data, l) != l) { /* to stderr */
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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)
|
|
|
|
{
|
2000-11-20 21:11:13 +01:00
|
|
|
hwparams = rhwparams;
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the data count to read from/to dsp */
|
2003-08-27 13:12:50 +02:00
|
|
|
static off64_t calc_count(void)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2003-08-27 13:12:50 +02:00
|
|
|
off64_t count;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2017-08-09 00:05:58 +02:00
|
|
|
if (timelimit == 0)
|
|
|
|
if (sampleslimit == 0)
|
|
|
|
count = pbrec_count;
|
|
|
|
else
|
|
|
|
count = snd_pcm_format_size(hwparams.format, sampleslimit * hwparams.channels);
|
|
|
|
else {
|
2004-03-09 14:18:29 +01:00
|
|
|
count = snd_pcm_format_size(hwparams.format, hwparams.rate * hwparams.channels);
|
|
|
|
count *= (off64_t)timelimit;
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2003-02-04 17:03:12 +01:00
|
|
|
return count < pbrec_count ? count : pbrec_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;
|
|
|
|
|
2004-09-13 09:04:53 +02:00
|
|
|
memcpy(vh.magic, VOC_MAGIC_STRING, 20);
|
|
|
|
vh.headerlen = LE_SHORT(sizeof(VocHeader));
|
|
|
|
vh.version = LE_SHORT(VOC_ACTUAL_VERSION);
|
|
|
|
vh.coded_ver = LE_SHORT(0x1233 - VOC_ACTUAL_VERSION);
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &vh, sizeof(VocHeader)) != sizeof(VocHeader)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-11-20 21:11:13 +01:00
|
|
|
if (hwparams.channels > 1) {
|
2001-09-11 10:39:48 +02:00
|
|
|
/* write an extended block */
|
1998-11-27 16:17:45 +01:00
|
|
|
bt.type = 8;
|
|
|
|
bt.datalen = 4;
|
|
|
|
bt.datalen_m = bt.datalen_h = 0;
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &bt, sizeof(VocBlockType)) != sizeof(VocBlockType)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2004-09-13 09:04:53 +02:00
|
|
|
eb.tc = LE_SHORT(65536 - 256000000L / (hwparams.rate << 1));
|
1998-11-27 16:17:45 +01:00
|
|
|
eb.pack = 0;
|
|
|
|
eb.mode = 1;
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &eb, sizeof(VocExtBlock)) != sizeof(VocExtBlock)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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);
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &bt, sizeof(VocBlockType)) != sizeof(VocBlockType)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-11-20 21:11:13 +01:00
|
|
|
vd.tc = (u_char) (256 - (1000000 / hwparams.rate));
|
1998-11-27 16:17:45 +01:00
|
|
|
vd.pack = 0;
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &vd, sizeof(VocVoiceData)) != sizeof(VocVoiceData)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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
|
|
|
|
2003-03-19 15:42:00 +01:00
|
|
|
/* WAVE cannot handle greater than 32bit (signed?) int */
|
|
|
|
if (cnt == (size_t)-2)
|
|
|
|
cnt = 0x7fffff00;
|
|
|
|
|
1999-05-12 00:18:49 +02:00
|
|
|
bits = 8;
|
2001-02-04 18:03:19 +01:00
|
|
|
switch ((unsigned long) hwparams.format) {
|
2000-11-20 21:11:13 +01:00
|
|
|
case SND_PCM_FORMAT_U8:
|
1998-11-27 16:17:45 +01:00
|
|
|
bits = 8;
|
|
|
|
break;
|
2000-11-20 21:11:13 +01:00
|
|
|
case SND_PCM_FORMAT_S16_LE:
|
1998-11-27 16:17:45 +01:00
|
|
|
bits = 16;
|
|
|
|
break;
|
2001-09-11 10:39:48 +02:00
|
|
|
case SND_PCM_FORMAT_S32_LE:
|
2008-02-06 14:05:07 +01:00
|
|
|
case SND_PCM_FORMAT_FLOAT_LE:
|
2001-09-11 10:39:48 +02:00
|
|
|
bits = 32;
|
|
|
|
break;
|
2002-08-27 18:32:16 +02:00
|
|
|
case SND_PCM_FORMAT_S24_LE:
|
|
|
|
case SND_PCM_FORMAT_S24_3LE:
|
|
|
|
bits = 24;
|
|
|
|
break;
|
1998-11-27 16:17:45 +01:00
|
|
|
default:
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Wave doesn't support %s format..."), snd_pcm_format_name(hwparams.format));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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);
|
|
|
|
|
2008-02-06 14:05:07 +01:00
|
|
|
if (hwparams.format == SND_PCM_FORMAT_FLOAT_LE)
|
|
|
|
f.format = LE_SHORT(WAV_FMT_IEEE_FLOAT);
|
|
|
|
else
|
|
|
|
f.format = LE_SHORT(WAV_FMT_PCM);
|
2008-03-06 17:34:21 +01:00
|
|
|
f.channels = LE_SHORT(hwparams.channels);
|
2000-11-20 21:11:13 +01:00
|
|
|
f.sample_fq = LE_INT(hwparams.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);
|
2001-11-14 08:38:18 +01:00
|
|
|
tmp = dsp_speed * hwparams.channels * (u_int) tmp2;
|
1998-08-13 17:43:39 +02:00
|
|
|
#else
|
2002-08-27 18:32:16 +02:00
|
|
|
tmp2 = hwparams.channels * snd_pcm_format_physical_width(hwparams.format) / 8;
|
2000-05-30 12:12:59 +02:00
|
|
|
f.byte_p_spl = LE_SHORT(tmp2);
|
2001-11-14 08:38:18 +01:00
|
|
|
tmp = (u_int) tmp2 * hwparams.rate;
|
1998-08-13 17:43:39 +02:00
|
|
|
#endif
|
2001-11-14 08:38:18 +01:00
|
|
|
f.byte_p_sec = LE_INT(tmp);
|
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
|
|
|
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &h, sizeof(WaveHeader)) != sizeof(WaveHeader) ||
|
|
|
|
xwrite(fd, &cf, sizeof(WaveChunkHeader)) != sizeof(WaveChunkHeader) ||
|
|
|
|
xwrite(fd, &f, sizeof(WaveFmtBody)) != sizeof(WaveFmtBody) ||
|
|
|
|
xwrite(fd, &cd, sizeof(WaveChunkHeader)) != sizeof(WaveChunkHeader)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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);
|
2001-02-04 18:03:19 +01:00
|
|
|
switch ((unsigned long) hwparams.format) {
|
2000-11-20 21:11:13 +01:00
|
|
|
case SND_PCM_FORMAT_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;
|
2000-11-20 21:11:13 +01:00
|
|
|
case SND_PCM_FORMAT_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;
|
2003-02-24 13:44:05 +01:00
|
|
|
case SND_PCM_FORMAT_S16_BE:
|
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:
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("Sparc Audio doesn't support %s format..."), snd_pcm_format_name(hwparams.format));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-11-20 21:11:13 +01:00
|
|
|
ah.sample_rate = BE_INT(hwparams.rate);
|
|
|
|
ah.channels = BE_INT(hwparams.channels);
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &ah, sizeof(AuHeader)) != sizeof(AuHeader)) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_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)
|
|
|
|
{
|
2003-08-27 13:08:27 +02:00
|
|
|
off64_t length_seek;
|
2001-09-11 10:39:48 +02:00
|
|
|
VocBlockType bt;
|
|
|
|
size_t cnt;
|
1998-11-27 16:17:45 +01:00
|
|
|
char dummy = 0; /* Write a Terminator */
|
2001-09-11 10:39:48 +02:00
|
|
|
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, &dummy, 1) != 1) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("write error"));
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2001-09-11 10:39:48 +02:00
|
|
|
length_seek = sizeof(VocHeader);
|
|
|
|
if (hwparams.channels > 1)
|
|
|
|
length_seek += sizeof(VocBlockType) + sizeof(VocExtBlock);
|
|
|
|
bt.type = 1;
|
|
|
|
cnt = fdcount;
|
|
|
|
cnt += sizeof(VocVoiceData); /* Channel_data block follows */
|
2002-02-07 17:46:29 +01:00
|
|
|
if (cnt > 0x00ffffff)
|
|
|
|
cnt = 0x00ffffff;
|
2001-09-11 10:39:48 +02:00
|
|
|
bt.datalen = (u_char) (cnt & 0xFF);
|
|
|
|
bt.datalen_m = (u_char) ((cnt & 0xFF00) >> 8);
|
|
|
|
bt.datalen_h = (u_char) ((cnt & 0xFF0000) >> 16);
|
2003-08-27 13:08:27 +02:00
|
|
|
if (lseek64(fd, length_seek, SEEK_SET) == length_seek)
|
2017-04-10 09:04:33 +02:00
|
|
|
xwrite(fd, &bt, sizeof(VocBlockType));
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void end_wave(int fd)
|
|
|
|
{ /* only close output */
|
2001-09-11 10:39:48 +02:00
|
|
|
WaveChunkHeader cd;
|
2003-08-27 13:08:27 +02:00
|
|
|
off64_t length_seek;
|
2004-09-13 09:04:53 +02:00
|
|
|
off64_t filelen;
|
2002-12-19 12:10:05 +01:00
|
|
|
u_int rifflen;
|
2001-09-11 10:39:48 +02:00
|
|
|
|
|
|
|
length_seek = sizeof(WaveHeader) +
|
|
|
|
sizeof(WaveChunkHeader) +
|
|
|
|
sizeof(WaveFmtBody);
|
|
|
|
cd.type = WAV_DATA;
|
2004-09-13 09:04:53 +02:00
|
|
|
cd.length = fdcount > 0x7fffffff ? LE_INT(0x7fffffff) : LE_INT(fdcount);
|
|
|
|
filelen = fdcount + 2*sizeof(WaveChunkHeader) + sizeof(WaveFmtBody) + 4;
|
|
|
|
rifflen = filelen > 0x7fffffff ? LE_INT(0x7fffffff) : LE_INT(filelen);
|
2003-08-27 13:08:27 +02:00
|
|
|
if (lseek64(fd, 4, SEEK_SET) == 4)
|
2017-04-10 09:04:33 +02:00
|
|
|
xwrite(fd, &rifflen, 4);
|
2003-08-27 13:08:27 +02:00
|
|
|
if (lseek64(fd, length_seek, SEEK_SET) == length_seek)
|
2017-04-10 09:04:33 +02:00
|
|
|
xwrite(fd, &cd, sizeof(WaveChunkHeader));
|
2001-09-11 10:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void end_au(int fd)
|
|
|
|
{ /* only close output */
|
|
|
|
AuHeader ah;
|
2003-08-27 13:08:27 +02:00
|
|
|
off64_t length_seek;
|
2001-09-11 10:39:48 +02:00
|
|
|
|
|
|
|
length_seek = (char *)&ah.data_size - (char *)&ah;
|
2002-02-07 17:46:29 +01:00
|
|
|
ah.data_size = fdcount > 0xffffffff ? 0xffffffff : BE_INT(fdcount);
|
2003-08-27 13:08:27 +02:00
|
|
|
if (lseek64(fd, length_seek, SEEK_SET) == length_seek)
|
2017-04-10 09:04:33 +02:00
|
|
|
xwrite(fd, &ah.data_size, sizeof(ah.data_size));
|
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) {
|
2006-08-03 17:29:51 +02:00
|
|
|
if (! name)
|
|
|
|
name = (stream == SND_PCM_STREAM_PLAYBACK) ? "stdout" : "stdin";
|
1998-11-27 16:17:45 +01:00
|
|
|
fprintf(stderr, "%s %s '%s' : ",
|
2005-06-23 12:38:06 +02:00
|
|
|
(stream == SND_PCM_STREAM_PLAYBACK) ? _("Playing") : _("Recording"),
|
|
|
|
gettext(fmt_rec_table[rtype].what),
|
2006-08-03 17:29:51 +02:00
|
|
|
name);
|
2000-11-20 21:11:13 +01:00
|
|
|
fprintf(stderr, "%s, ", snd_pcm_format_description(hwparams.format));
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Rate %d Hz, "), hwparams.rate);
|
2000-11-20 21:11:13 +01:00
|
|
|
if (hwparams.channels == 1)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Mono"));
|
2000-11-20 21:11:13 +01:00
|
|
|
else if (hwparams.channels == 2)
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Stereo"));
|
1998-11-27 16:17:45 +01:00
|
|
|
else
|
2005-06-23 12:38:06 +02:00
|
|
|
fprintf(stderr, _("Channels %i"), hwparams.channels);
|
1998-11-27 16:17:45 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* playing raw data */
|
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static void playback_go(int fd, size_t loaded, off64_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;
|
2004-03-09 14:18:29 +01:00
|
|
|
off64_t written = 0;
|
|
|
|
off64_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
|
|
|
|
2013-04-08 13:30:54 +02:00
|
|
|
while (loaded > chunk_bytes && written < count && !in_aborting) {
|
2001-01-15 12:06:55 +01:00
|
|
|
if (pcm_write(audiobuf + written, chunk_size) <= 0)
|
2000-05-08 20:54:14 +02:00
|
|
|
return;
|
2001-01-15 12:06:55 +01:00
|
|
|
written += chunk_bytes;
|
|
|
|
loaded -= chunk_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;
|
2013-04-08 13:30:54 +02:00
|
|
|
while (written < count && !in_aborting) {
|
1999-11-07 00:47:10 +01:00
|
|
|
do {
|
2000-05-18 16:42:43 +02:00
|
|
|
c = count - written;
|
2001-01-15 12:06:55 +01:00
|
|
|
if (c > chunk_bytes)
|
|
|
|
c = chunk_bytes;
|
2017-08-09 00:06:00 +02:00
|
|
|
|
|
|
|
/* c < l, there is more data loaded
|
|
|
|
* then we actually need to write
|
|
|
|
*/
|
|
|
|
if (c < l)
|
|
|
|
l = c;
|
|
|
|
|
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);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
2001-09-11 10:39:48 +02:00
|
|
|
fdcount += r;
|
2000-05-18 16:42:43 +02:00
|
|
|
if (r == 0)
|
1999-11-07 00:47:10 +01:00
|
|
|
break;
|
|
|
|
l += r;
|
2008-01-08 18:38:32 +01:00
|
|
|
} while ((size_t)l < chunk_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;
|
|
|
|
}
|
2007-12-14 15:29:51 +01:00
|
|
|
snd_pcm_nonblock(handle, 0);
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2007-12-14 15:29:51 +01:00
|
|
|
snd_pcm_nonblock(handle, nonblock);
|
1998-08-13 17:43:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 00:05:59 +02:00
|
|
|
static int read_header(int *loaded, int header_size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
ret = fstat(fd, &buf);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("fstat");
|
|
|
|
prg_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't be adventurous, get out if file size is smaller than
|
|
|
|
* requested header size */
|
2018-05-15 22:17:01 +02:00
|
|
|
if ((buf.st_mode & S_IFMT) == S_IFREG &&
|
|
|
|
buf.st_size < header_size)
|
2017-08-09 00:05:59 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (*loaded < header_size) {
|
|
|
|
header_size -= *loaded;
|
|
|
|
ret = safe_read(fd, audiobuf + *loaded, header_size);
|
|
|
|
if (ret != header_size) {
|
|
|
|
error(_("read error"));
|
|
|
|
prg_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
*loaded += header_size;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int playback_au(char *name, int *loaded)
|
|
|
|
{
|
|
|
|
if (read_header(loaded, sizeof(AuHeader)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (test_au(fd, audiobuf) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rhwparams.format = hwparams.format;
|
|
|
|
pbrec_count = calc_count();
|
|
|
|
playback_go(fd, *loaded - sizeof(AuHeader), pbrec_count, FORMAT_AU, name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int playback_voc(char *name, int *loaded)
|
|
|
|
{
|
|
|
|
int ofs;
|
|
|
|
|
|
|
|
if (read_header(loaded, sizeof(VocHeader)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((ofs = test_vocfile(audiobuf)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pbrec_count = calc_count();
|
|
|
|
voc_play(fd, ofs, name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int playback_wave(char *name, int *loaded)
|
|
|
|
{
|
|
|
|
ssize_t dtawave;
|
|
|
|
|
|
|
|
if (read_header(loaded, sizeof(WaveHeader)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((dtawave = test_wavefile(fd, audiobuf, *loaded)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pbrec_count = calc_count();
|
|
|
|
playback_go(fd, dtawave, pbrec_count, FORMAT_WAVE, name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int playback_raw(char *name, int *loaded)
|
|
|
|
{
|
|
|
|
init_raw_data();
|
|
|
|
pbrec_count = calc_count();
|
|
|
|
playback_go(fd, *loaded, pbrec_count, FORMAT_RAW, name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2017-08-09 00:05:59 +02:00
|
|
|
int loaded = 0;
|
1998-11-27 16:17:45 +01:00
|
|
|
|
2004-03-09 14:18:29 +01:00
|
|
|
pbrec_count = LLONG_MAX;
|
2001-09-11 10:39:48 +02:00
|
|
|
fdcount = 0;
|
1999-02-10 11:06:34 +01:00
|
|
|
if (!name || !strcmp(name, "-")) {
|
2001-09-11 10:39:48 +02:00
|
|
|
fd = fileno(stdin);
|
1998-11-27 16:17:45 +01:00
|
|
|
name = "stdin";
|
|
|
|
} else {
|
2010-07-15 10:40:21 +02:00
|
|
|
init_stdin();
|
2012-07-17 12:15:34 +02:00
|
|
|
if ((fd = open(name, O_RDONLY, 0)) == -1) {
|
1998-11-27 16:17:45 +01:00
|
|
|
perror(name);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-09 00:05:59 +02:00
|
|
|
|
|
|
|
switch(file_type) {
|
|
|
|
case FORMAT_AU:
|
|
|
|
playback_au(name, &loaded);
|
|
|
|
break;
|
|
|
|
case FORMAT_VOC:
|
|
|
|
playback_voc(name, &loaded);
|
|
|
|
break;
|
|
|
|
case FORMAT_WAVE:
|
|
|
|
playback_wave(name, &loaded);
|
|
|
|
break;
|
|
|
|
case FORMAT_RAW:
|
|
|
|
playback_raw(name, &loaded);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* parse the file header */
|
|
|
|
if (playback_au(name, &loaded) < 0 &&
|
|
|
|
playback_voc(name, &loaded) < 0 &&
|
|
|
|
playback_wave(name, &loaded) < 0)
|
|
|
|
playback_raw(name, &loaded); /* should be raw data */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:14:50 +01:00
|
|
|
if (fd != fileno(stdin))
|
1998-11-27 16:17:45 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
1998-08-13 17:43:39 +02:00
|
|
|
|
2010-01-08 22:24:33 +01:00
|
|
|
/**
|
|
|
|
* mystrftime
|
|
|
|
*
|
|
|
|
* Variant of strftime(3) that supports additional format
|
|
|
|
* specifiers in the format string.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* s - destination string
|
|
|
|
* max - max number of bytes to write
|
|
|
|
* userformat - format string
|
|
|
|
* tm - time information
|
|
|
|
* filenumber - the number of the file, starting at 1
|
|
|
|
*
|
|
|
|
* Returns: number of bytes written to the string s
|
|
|
|
*/
|
|
|
|
size_t mystrftime(char *s, size_t max, const char *userformat,
|
|
|
|
const struct tm *tm, const int filenumber)
|
|
|
|
{
|
|
|
|
char formatstring[PATH_MAX] = "";
|
|
|
|
char tempstring[PATH_MAX] = "";
|
|
|
|
char *format, *tempstr;
|
|
|
|
const char *pos_userformat;
|
|
|
|
|
|
|
|
format = formatstring;
|
|
|
|
|
|
|
|
/* if mystrftime is called with userformat = NULL we return a zero length string */
|
|
|
|
if (userformat == NULL) {
|
|
|
|
*s = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pos_userformat = userformat; *pos_userformat; ++pos_userformat) {
|
|
|
|
if (*pos_userformat == '%') {
|
|
|
|
tempstr = tempstring;
|
|
|
|
tempstr[0] = '\0';
|
|
|
|
switch (*++pos_userformat) {
|
|
|
|
|
|
|
|
case '\0': // end of string
|
|
|
|
--pos_userformat;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': // file number
|
|
|
|
sprintf(tempstr, "%02d", filenumber);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // All other codes will be handled by strftime
|
|
|
|
*format++ = '%';
|
|
|
|
*format++ = *pos_userformat;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a format specifier was found and used, copy the result. */
|
|
|
|
if (tempstr[0]) {
|
|
|
|
while ((*format = *tempstr++) != '\0')
|
|
|
|
++format;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For any other character than % we simply copy the character */
|
|
|
|
*format++ = *pos_userformat;
|
|
|
|
}
|
|
|
|
|
|
|
|
*format = '\0';
|
|
|
|
format = formatstring;
|
|
|
|
return strftime(s, max, format, tm);
|
|
|
|
}
|
|
|
|
|
2006-05-17 14:59:29 +02:00
|
|
|
static int new_capture_file(char *name, char *namebuf, size_t namelen,
|
|
|
|
int filecount)
|
|
|
|
{
|
|
|
|
char *s;
|
aplay: Adjust buffer sizes to fix snprintf warnings
It also reduces compiled size slightly.
aplay.c: In function 'capture':
aplay.c:3055:34: error: '-01' directive output may be truncated writing 3
bytes into a region of size between 1 and 4097 [-Werror=format-truncation=
]
3055 | snprintf(namebuf, namelen, "%s-01", buf);
| ^~~
aplay.c:3055:4: note: 'snprintf' output between 4 and 4100 bytes into a
destination of size 4097
3055 | snprintf(namebuf, namelen, "%s-01", buf);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aplay.c:3053:34: error: '-01.' directive output may be truncated writing 4
bytes into a region of size between 1 and 4097 [-Werror=format-truncation=
]
3053 | snprintf(namebuf, namelen, "%s-01.%s", buf, s);
| ^~~~
aplay.c:3053:4: note: 'snprintf' output 5 or more bytes (assuming 4101)
into a destination of size 4097
3053 | snprintf(namebuf, namelen, "%s-01.%s", buf, s);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aplay.c:3065:34: error: '%02i' directive output may be truncated writing
between 2 and 10 bytes into a region of size between 0 and 4096 [-Werror=
format-truncation=]
3065 | snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
| ^~~~
aplay.c:3065:30: note: directive argument in the range [1, 2147483647]
3065 | snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
| ^~~~~~~~~
aplay.c:3065:3: note: 'snprintf' output between 4 and 4108 bytes into a
destination of size 4097
3065 | snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aplay.c:3063:34: error: '%02i' directive output may be truncated writing
between 2 and 10 bytes into a region of size between 0 and 4096 [-Werror=
format-truncation=]
3063 | snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
| ^~~~
aplay.c:3063:30: note: directive argument in the range [1, 2147483647]
3063 | snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
| ^~~~~~~~~~~~
aplay.c:3063:3: note: 'snprintf' output 5 or more bytes (assuming 4109)
into a destination of size 4097
3063 | snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2019-11-20 19:19:20 +01:00
|
|
|
char buf[PATH_MAX-10];
|
2010-01-08 22:24:33 +01:00
|
|
|
time_t t;
|
|
|
|
struct tm *tmp;
|
|
|
|
|
|
|
|
if (use_strftime) {
|
|
|
|
t = time(NULL);
|
|
|
|
tmp = localtime(&t);
|
|
|
|
if (tmp == NULL) {
|
|
|
|
perror("localtime");
|
|
|
|
prg_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (mystrftime(namebuf, namelen, name, tmp, filecount+1) == 0) {
|
|
|
|
fprintf(stderr, "mystrftime returned 0");
|
|
|
|
prg_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return filecount;
|
|
|
|
}
|
2006-05-17 14:59:29 +02:00
|
|
|
|
2010-01-08 22:24:33 +01:00
|
|
|
/* get a copy of the original filename */
|
2006-05-17 14:59:29 +02:00
|
|
|
strncpy(buf, name, sizeof(buf));
|
|
|
|
|
|
|
|
/* separate extension from filename */
|
|
|
|
s = buf + strlen(buf);
|
|
|
|
while (s > buf && *s != '.' && *s != '/')
|
|
|
|
--s;
|
|
|
|
if (*s == '.')
|
|
|
|
*s++ = 0;
|
|
|
|
else if (*s == '/')
|
|
|
|
s = buf + strlen(buf);
|
|
|
|
|
|
|
|
/* upon first jump to this if block rename the first file */
|
|
|
|
if (filecount == 1) {
|
|
|
|
if (*s)
|
|
|
|
snprintf(namebuf, namelen, "%s-01.%s", buf, s);
|
|
|
|
else
|
|
|
|
snprintf(namebuf, namelen, "%s-01", buf);
|
|
|
|
remove(namebuf);
|
|
|
|
rename(name, namebuf);
|
|
|
|
filecount = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* name of the current file */
|
|
|
|
if (*s)
|
|
|
|
snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
|
|
|
|
else
|
|
|
|
snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
|
|
|
|
|
|
|
|
return filecount;
|
|
|
|
}
|
|
|
|
|
2010-01-08 22:24:33 +01:00
|
|
|
/**
|
|
|
|
* create_path
|
|
|
|
*
|
|
|
|
* This function creates a file path, like mkdir -p.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* path - the path to create
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on failure
|
|
|
|
* On failure, a message has been printed to stderr.
|
|
|
|
*/
|
|
|
|
int create_path(const char *path)
|
|
|
|
{
|
|
|
|
char *start;
|
|
|
|
mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
|
|
|
|
|
|
|
if (path[0] == '/')
|
|
|
|
start = strchr(path + 1, '/');
|
|
|
|
else
|
|
|
|
start = strchr(path, '/');
|
|
|
|
|
|
|
|
while (start) {
|
|
|
|
char *buffer = strdup(path);
|
|
|
|
buffer[start-path] = 0x00;
|
|
|
|
|
|
|
|
if (mkdir(buffer, mode) == -1 && errno != EEXIST) {
|
|
|
|
fprintf(stderr, "Problem creating directory %s", buffer);
|
|
|
|
perror(" ");
|
|
|
|
free(buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(buffer);
|
|
|
|
start = strchr(start + 1, '/');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int safe_open(const char *name)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2012-07-17 12:15:34 +02:00
|
|
|
fd = open(name, O_WRONLY | O_CREAT, 0644);
|
2010-01-08 22:24:33 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
if (errno != ENOENT || !use_strftime)
|
|
|
|
return -1;
|
|
|
|
if (create_path(name) == 0)
|
2012-07-17 12:15:34 +02:00
|
|
|
fd = open(name, O_WRONLY | O_CREAT, 0644);
|
2010-01-08 22:24:33 +01:00
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2006-05-17 14:59:29 +02:00
|
|
|
static void capture(char *orig_name)
|
1998-08-13 17:43:39 +02:00
|
|
|
{
|
2005-12-06 12:25:47 +01:00
|
|
|
int tostdout=0; /* boolean which describes output stream */
|
|
|
|
int filecount=0; /* number of files written */
|
2006-05-17 14:59:29 +02:00
|
|
|
char *name = orig_name; /* current filename */
|
aplay: Adjust buffer sizes to fix snprintf warnings
It also reduces compiled size slightly.
aplay.c: In function 'capture':
aplay.c:3055:34: error: '-01' directive output may be truncated writing 3
bytes into a region of size between 1 and 4097 [-Werror=format-truncation=
]
3055 | snprintf(namebuf, namelen, "%s-01", buf);
| ^~~
aplay.c:3055:4: note: 'snprintf' output between 4 and 4100 bytes into a
destination of size 4097
3055 | snprintf(namebuf, namelen, "%s-01", buf);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aplay.c:3053:34: error: '-01.' directive output may be truncated writing 4
bytes into a region of size between 1 and 4097 [-Werror=format-truncation=
]
3053 | snprintf(namebuf, namelen, "%s-01.%s", buf, s);
| ^~~~
aplay.c:3053:4: note: 'snprintf' output 5 or more bytes (assuming 4101)
into a destination of size 4097
3053 | snprintf(namebuf, namelen, "%s-01.%s", buf, s);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aplay.c:3065:34: error: '%02i' directive output may be truncated writing
between 2 and 10 bytes into a region of size between 0 and 4096 [-Werror=
format-truncation=]
3065 | snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
| ^~~~
aplay.c:3065:30: note: directive argument in the range [1, 2147483647]
3065 | snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
| ^~~~~~~~~
aplay.c:3065:3: note: 'snprintf' output between 4 and 4108 bytes into a
destination of size 4097
3065 | snprintf(namebuf, namelen, "%s-%02i", buf, filecount);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aplay.c:3063:34: error: '%02i' directive output may be truncated writing
between 2 and 10 bytes into a region of size between 0 and 4096 [-Werror=
format-truncation=]
3063 | snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
| ^~~~
aplay.c:3063:30: note: directive argument in the range [1, 2147483647]
3063 | snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
| ^~~~~~~~~~~~
aplay.c:3063:3: note: 'snprintf' output 5 or more bytes (assuming 4109)
into a destination of size 4097
3063 | snprintf(namebuf, namelen, "%s-%02i.%s", buf, filecount, s);
Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2019-11-20 19:19:20 +01:00
|
|
|
char namebuf[PATH_MAX+2];
|
2006-05-17 14:59:29 +02:00
|
|
|
off64_t count, rest; /* number of bytes to capture */
|
2015-09-21 15:05:57 +02:00
|
|
|
struct stat statbuf;
|
2005-12-06 12:25:47 +01:00
|
|
|
|
|
|
|
/* get number of bytes to capture */
|
2006-05-17 14:59:29 +02:00
|
|
|
count = calc_count();
|
|
|
|
if (count == 0)
|
|
|
|
count = LLONG_MAX;
|
2010-01-08 22:24:33 +01:00
|
|
|
/* compute the number of bytes per file */
|
2017-06-23 20:35:03 +02:00
|
|
|
max_file_size = (long long) max_file_time *
|
2010-01-08 22:24:33 +01:00
|
|
|
snd_pcm_format_size(hwparams.format,
|
|
|
|
hwparams.rate * hwparams.channels);
|
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) */
|
2006-05-17 14:59:29 +02:00
|
|
|
if (count < LLONG_MAX)
|
|
|
|
count += count % 2;
|
2004-04-27 15:38:59 +02:00
|
|
|
else
|
2006-05-17 14:59:29 +02:00
|
|
|
count -= count % 2;
|
2005-12-06 12:25:47 +01:00
|
|
|
|
|
|
|
/* display verbose output to console */
|
|
|
|
header(file_type, name);
|
|
|
|
|
|
|
|
/* setup sound hardware */
|
|
|
|
set_params();
|
|
|
|
|
|
|
|
/* write to stdout? */
|
|
|
|
if (!name || !strcmp(name, "-")) {
|
|
|
|
fd = fileno(stdout);
|
|
|
|
name = "stdout";
|
2019-03-12 16:14:50 +01:00
|
|
|
tostdout = 1;
|
2006-05-18 12:33:35 +02:00
|
|
|
if (count > fmt_rec_table[file_type].max_filesize)
|
|
|
|
count = fmt_rec_table[file_type].max_filesize;
|
2005-12-06 12:25:47 +01:00
|
|
|
}
|
2010-07-15 10:40:21 +02:00
|
|
|
init_stdin();
|
2005-12-06 12:25:47 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* open a file to write */
|
2019-03-12 16:14:50 +01:00
|
|
|
if (!tostdout) {
|
2005-12-06 12:25:47 +01:00
|
|
|
/* upon the second file we start the numbering scheme */
|
2010-01-08 22:24:33 +01:00
|
|
|
if (filecount || use_strftime) {
|
2006-05-17 14:59:29 +02:00
|
|
|
filecount = new_capture_file(orig_name, namebuf,
|
|
|
|
sizeof(namebuf),
|
|
|
|
filecount);
|
|
|
|
name = namebuf;
|
2005-12-06 12:25:47 +01:00
|
|
|
}
|
2006-05-17 14:59:29 +02:00
|
|
|
|
2005-12-06 12:25:47 +01:00
|
|
|
/* open a new file */
|
2015-09-21 15:05:57 +02:00
|
|
|
if (!lstat(name, &statbuf)) {
|
|
|
|
if (S_ISREG(statbuf.st_mode))
|
|
|
|
remove(name);
|
|
|
|
}
|
2010-01-08 22:24:33 +01:00
|
|
|
fd = safe_open(name);
|
|
|
|
if (fd < 0) {
|
2005-12-06 12:25:47 +01:00
|
|
|
perror(name);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2005-12-06 12:25:47 +01:00
|
|
|
}
|
|
|
|
filecount++;
|
|
|
|
}
|
|
|
|
|
2006-05-17 14:59:29 +02:00
|
|
|
rest = count;
|
|
|
|
if (rest > fmt_rec_table[file_type].max_filesize)
|
|
|
|
rest = fmt_rec_table[file_type].max_filesize;
|
2010-01-08 22:24:33 +01:00
|
|
|
if (max_file_size && (rest > max_file_size))
|
|
|
|
rest = max_file_size;
|
2006-05-17 14:59:29 +02:00
|
|
|
|
2005-12-06 12:25:47 +01:00
|
|
|
/* setup sample header */
|
|
|
|
if (fmt_rec_table[file_type].start)
|
2006-05-17 14:59:29 +02:00
|
|
|
fmt_rec_table[file_type].start(fd, rest);
|
2005-12-06 12:25:47 +01:00
|
|
|
|
|
|
|
/* capture */
|
2006-05-18 12:33:35 +02:00
|
|
|
fdcount = 0;
|
2013-04-08 13:30:54 +02:00
|
|
|
while (rest > 0 && recycle_capture_file == 0 && !in_aborting) {
|
2006-05-18 12:33:35 +02:00
|
|
|
size_t c = (rest <= (off64_t)chunk_bytes) ?
|
2006-05-17 14:59:29 +02:00
|
|
|
(size_t)rest : chunk_bytes;
|
2006-05-18 12:33:35 +02:00
|
|
|
size_t f = c * 8 / bits_per_frame;
|
2016-04-14 06:32:46 +02:00
|
|
|
if (pcm_read(audiobuf, f) != f) {
|
|
|
|
in_aborting = 1;
|
2006-05-17 14:59:29 +02:00
|
|
|
break;
|
2016-04-14 06:32:46 +02:00
|
|
|
}
|
2017-04-10 09:04:33 +02:00
|
|
|
if (xwrite(fd, audiobuf, c) != c) {
|
2006-05-17 14:59:29 +02:00
|
|
|
perror(name);
|
2016-04-14 06:32:46 +02:00
|
|
|
in_aborting = 1;
|
|
|
|
break;
|
2005-12-06 12:25:47 +01:00
|
|
|
}
|
2006-05-18 12:33:35 +02:00
|
|
|
count -= c;
|
|
|
|
rest -= c;
|
|
|
|
fdcount += c;
|
2006-05-17 14:59:29 +02:00
|
|
|
}
|
2005-12-06 12:25:47 +01:00
|
|
|
|
2010-01-08 22:24:33 +01:00
|
|
|
/* re-enable SIGUSR1 signal */
|
|
|
|
if (recycle_capture_file) {
|
|
|
|
recycle_capture_file = 0;
|
|
|
|
signal(SIGUSR1, signal_handler_recycle);
|
|
|
|
}
|
|
|
|
|
2005-12-06 12:25:47 +01:00
|
|
|
/* finish sample container */
|
2019-03-12 16:14:50 +01:00
|
|
|
if (!tostdout) {
|
|
|
|
if (fmt_rec_table[file_type].end)
|
|
|
|
fmt_rec_table[file_type].end(fd);
|
|
|
|
close(fd);
|
2006-08-03 17:33:32 +02:00
|
|
|
fd = -1;
|
|
|
|
}
|
2005-12-06 12:25:47 +01:00
|
|
|
|
2013-04-17 08:34:34 +02:00
|
|
|
if (in_aborting)
|
2016-04-14 06:32:46 +02:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2013-04-17 08:34:34 +02:00
|
|
|
|
2006-05-17 14:59:29 +02:00
|
|
|
/* repeat the loop when format is raw without timelimit or
|
|
|
|
* requested counts of data are recorded
|
|
|
|
*/
|
2017-08-09 00:05:58 +02:00
|
|
|
} while ((file_type == FORMAT_RAW && !timelimit && !sampleslimit) || count > 0);
|
1998-11-27 16:17:45 +01:00
|
|
|
}
|
2000-05-18 16:42:43 +02:00
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static void playbackv_go(int* fds, unsigned int channels, size_t loaded, off64_t count, int rtype, char **names)
|
2000-05-18 16:42:43 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
size_t vsize;
|
2001-04-22 10:13:29 +02:00
|
|
|
|
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();
|
|
|
|
|
2001-01-15 12:06:55 +01:00
|
|
|
vsize = chunk_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
|
|
|
|
2013-04-08 13:30:54 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
2000-05-18 16:42:43 +02:00
|
|
|
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]);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
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]);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
c += r;
|
2008-01-08 18:38:32 +01:00
|
|
|
} while (c < expected);
|
2000-06-10 14:39:52 +02:00
|
|
|
c = c * 8 / bits_per_sample;
|
|
|
|
r = pcm_writev(bufs, channels, c);
|
2003-02-04 17:03:12 +01:00
|
|
|
if ((size_t)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;
|
|
|
|
}
|
2007-12-14 15:29:51 +01:00
|
|
|
snd_pcm_nonblock(handle, 0);
|
2000-09-29 22:49:20 +02:00
|
|
|
snd_pcm_drain(handle);
|
2007-12-14 15:29:51 +01:00
|
|
|
snd_pcm_nonblock(handle, nonblock);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static void capturev_go(int* fds, unsigned int channels, off64_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();
|
|
|
|
|
2001-01-15 12:06:55 +01:00
|
|
|
vsize = chunk_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
|
|
|
|
2013-04-08 13:30:54 +02:00
|
|
|
while (count > 0 && !in_aborting) {
|
2000-05-18 16:42:43 +02:00
|
|
|
size_t rv;
|
|
|
|
c = count;
|
2001-01-15 12:06:55 +01:00
|
|
|
if (c > chunk_bytes)
|
|
|
|
c = chunk_bytes;
|
2000-06-10 14:39:52 +02:00
|
|
|
c = c * 8 / bits_per_frame;
|
2003-02-04 17:03:12 +01:00
|
|
|
if ((size_t)(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) {
|
2017-04-10 09:04:33 +02:00
|
|
|
if ((size_t)xwrite(fds[channel], bufs[channel], rv) != rv) {
|
2000-05-27 18:52:19 +02:00
|
|
|
perror(names[channel]);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
2000-06-10 14:39:52 +02:00
|
|
|
r = r * bits_per_frame / 8;
|
2000-05-18 16:42:43 +02:00
|
|
|
count -= r;
|
2002-02-07 17:46:29 +01:00
|
|
|
fdcount += r;
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void playbackv(char **names, unsigned int count)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-11-20 21:11:13 +01:00
|
|
|
unsigned int channels = rhwparams.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-12-11 12:19:39 +01:00
|
|
|
if (count == 1 && channels > 1) {
|
2000-05-18 16:42:43 +02:00
|
|
|
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) {
|
2019-11-20 05:28:55 +01:00
|
|
|
error(_("You need to specify %u files"), channels);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
init_raw_data();
|
2003-02-04 17:03:12 +01:00
|
|
|
pbrec_count = calc_count();
|
|
|
|
playbackv_go(fds, channels, 0, pbrec_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)
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(ret);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void capturev(char **names, unsigned int count)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2000-05-27 18:52:19 +02:00
|
|
|
unsigned int channel;
|
2000-11-20 21:11:13 +01:00
|
|
|
unsigned int channels = rhwparams.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
|
|
|
|
|
|
|
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) {
|
2005-06-23 12:38:06 +02:00
|
|
|
error(_("You need to specify %d files"), channels);
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(EXIT_FAILURE);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
init_raw_data();
|
2003-02-04 17:03:12 +01:00
|
|
|
pbrec_count = calc_count();
|
|
|
|
capturev_go(fds, channels, pbrec_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)
|
2010-01-08 22:21:07 +01:00
|
|
|
prg_exit(ret);
|
2000-05-18 16:42:43 +02:00
|
|
|
}
|