-
Notifications
You must be signed in to change notification settings - Fork 139
/
aplay.c
3564 lines (3289 loc) · 90.1 KB
/
aplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* aplay.c - plays and records
*
* CREATIVE LABS CHANNEL-files
* Microsoft WAVE-files
* SPARC AUDIO .AU-files
* Raw Data
*
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
* Based on vplay program by Michael Beck
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define _GNU_SOURCE
#include "aconfig.h"
#include <stdio.h>
#include <stdint.h>
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <locale.h>
#include <alsa/asoundlib.h>
#include <assert.h>
#include <termios.h>
#include <signal.h>
#include <poll.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <endian.h>
#include "gettext.h"
#include "formats.h"
#include "version.h"
#include "os_compat.h"
#define ABS(a) (a) < 0 ? -(a) : (a)
#ifdef SND_CHMAP_API_VERSION
#define CONFIG_SUPPORT_CHMAP 1
#endif
#ifndef LLONG_MAX
#define LLONG_MAX 9223372036854775807LL
#endif
#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
#define DEFAULT_FORMAT SND_PCM_FORMAT_U8
#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 */
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);
enum {
VUMETER_NONE,
VUMETER_MONO,
VUMETER_STEREO
};
static char *command;
static snd_pcm_t *handle;
static struct {
snd_pcm_format_t format;
snd_pcm_subformat_t subformat;
unsigned int channels;
unsigned int rate;
} hwparams, rhwparams;
static int timelimit = 0;
static int sampleslimit = 0;
static int quiet_mode = 0;
static int file_type = FORMAT_DEFAULT;
static int open_mode = 0;
static snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
static int mmap_flag = 0;
static int interleaved = 1;
static int nonblock = 0;
static volatile sig_atomic_t in_aborting = 0;
static uint8_t *audiobuf = NULL;
static snd_pcm_uframes_t chunk_size = 0;
static unsigned period_time = 0;
static unsigned buffer_time = 0;
static snd_pcm_uframes_t period_frames = 0;
static snd_pcm_uframes_t buffer_frames = 0;
static int avail_min = -1;
static int start_delay = 0;
static int stop_delay = 0;
static int monotonic = 0;
static int interactive = 0;
static int can_pause = 0;
static int fatal_errors = 0;
static int verbose = 0;
static int vumeter = VUMETER_NONE;
static int buffer_pos = 0;
static size_t significant_bits_per_sample, bits_per_sample, bits_per_frame;
static size_t chunk_bytes;
static int test_position = 0;
static int test_coef = 8;
static int test_nowait = 0;
static snd_output_t *log;
static long long max_file_size = 0;
static int max_file_time = 0;
static int use_strftime = 0;
static volatile int recycle_capture_file = 0;
static long term_c_lflag = -1;
static int dump_hw_params = 0;
static int fd = -1;
static off_t pbrec_count = LLONG_MAX, fdcount;
static int vocmajor, vocminor;
static char *pidfile_name = NULL;
FILE *pidf = NULL;
static int pidfile_written = 0;
#ifdef CONFIG_SUPPORT_CHMAP
static snd_pcm_chmap_t *channel_map = NULL; /* chmap to override */
static unsigned int *hw_map = NULL; /* chmap to follow */
#endif
/* needed prototypes */
static void done_stdin(void);
static void playback(char *filename);
static void capture(char *filename);
static void playbackv(char **filenames, unsigned int count);
static void capturev(char **filenames, unsigned int count);
static void begin_voc(int fd, size_t count);
static void end_voc(int fd);
static void begin_wave(int fd, size_t count);
static void end_wave(int fd);
static void begin_au(int fd, size_t count);
static void end_au(int fd);
static void suspend(void);
static const struct fmt_capture {
void (*start) (int fd, size_t count);
void (*end) (int fd);
char *what;
long long max_filesize;
} fmt_rec_table[] = {
{ NULL, NULL, N_("raw data"), LLONG_MAX },
{ begin_voc, end_voc, N_("VOC"), 16000000LL },
/* FIXME: can WAV handle exactly 2GB or less than it? */
{ begin_wave, end_wave, N_("WAVE"), 2147483648LL },
{ begin_au, end_au, N_("Sparc Audio"), LLONG_MAX }
};
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
#define error(...) do {\
fprintf(stderr, "%s: %s:%d: ", command, __func__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
putc('\n', stderr); \
} while (0)
#else
#define error(args...) do {\
fprintf(stderr, "%s: %s:%d: ", command, __func__, __LINE__); \
fprintf(stderr, ##args); \
putc('\n', stderr); \
} while (0)
#endif
static void usage(char *command)
{
snd_pcm_format_t k;
printf(
_("Usage: %s [OPTION]... [FILE]...\n"
"\n"
"-h, --help help\n"
" --version print current version\n"
"-l, --list-devices list all soundcards and digital audio devices\n"
"-L, --list-pcms list device names\n"
"-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"
" --subformat=SUBFORMAT sample subformat (case insensitive)\n"
"-r, --rate=# sample rate\n"
"-d, --duration=# interrupt after # seconds\n"
"-s, --samples=# interrupt after # samples per channel\n"
"-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"
" --period-size=# distance between interrupts is # frames\n"
" --buffer-size=# buffer duration is # frames\n"
"-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"
"-V, --vumeter=TYPE enable VU meter (TYPE: mono or stereo)\n"
"-I, --separate-channels one file for each channel\n"
"-i, --interactive allow interactive operation from stdin\n"
"-m, --chmap=ch1,ch2,.. Give the channel map to override or follow\n"
" --disable-resample disable automatic rate resample\n"
" --disable-channels disable automatic channel conversions\n"
" --disable-format disable automatic format conversions\n"
" --disable-softvol disable software volume control (softvol)\n"
" --test-position test ring buffer position\n"
" --test-coef=# test coefficient for ring buffer position (default 8)\n"
" expression for validation is: coef * (buffer_size / 2)\n"
" --test-nowait do not wait for ring buffer - eats whole CPU\n"
" --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"
" --use-strftime apply the strftime facility to the output file name\n"
" --dump-hw-params dump hw_params of the device\n"
" --fatal-errors treat all errors as fatal\n"
)
, command);
printf(_("Recognized sample formats are:"));
for (k = 0; k <= SND_PCM_FORMAT_LAST; ++k) {
const char *s = snd_pcm_format_name(k);
if (s)
printf(" %s", s);
}
printf(_("\nSome of these may not be available on selected hardware\n"));
printf(_("The available format shortcuts are:\n"));
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"));
}
static void device_list(void)
{
snd_ctl_t *handle;
int card, err, dev, idx;
snd_ctl_card_info_t *info;
snd_pcm_info_t *pcminfo;
snd_ctl_card_info_alloca(&info);
snd_pcm_info_alloca(&pcminfo);
card = -1;
if (snd_card_next(&card) < 0 || card < 0) {
error(_("no soundcards found..."));
return;
}
printf(_("**** List of %s Hardware Devices ****\n"),
snd_pcm_stream_name(stream));
while (card >= 0) {
char name[32];
sprintf(name, "hw:%d", card);
if ((err = snd_ctl_open(&handle, name, 0)) < 0) {
error("control open (%i): %s", card, snd_strerror(err));
goto next_card;
}
if ((err = snd_ctl_card_info(handle, info)) < 0) {
error("control hardware info (%i): %s", card, snd_strerror(err));
snd_ctl_close(handle);
goto next_card;
}
dev = -1;
while (1) {
unsigned int count;
if (snd_ctl_pcm_next_device(handle, &dev)<0)
error("snd_ctl_pcm_next_device");
if (dev < 0)
break;
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));
continue;
}
printf(_("card %i: %s [%s], device %i: %s [%s]\n"),
card, snd_ctl_card_info_get_id(info), snd_ctl_card_info_get_name(info),
dev,
snd_pcm_info_get_id(pcminfo),
snd_pcm_info_get_name(pcminfo));
count = snd_pcm_info_get_subdevices_count(pcminfo);
printf( _(" Subdevices: %i/%i\n"),
snd_pcm_info_get_subdevices_avail(pcminfo), count);
for (idx = 0; idx < (int)count; idx++) {
snd_pcm_info_set_subdevice(pcminfo, idx);
if ((err = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
error("control digital audio playback info (%i): %s", card, snd_strerror(err));
} else {
printf(_(" Subdevice #%i: %s\n"),
idx, snd_pcm_info_get_subdevice_name(pcminfo));
}
}
}
snd_ctl_close(handle);
next_card:
if (snd_card_next(&card) < 0) {
error("snd_card_next");
break;
}
}
}
static void pcm_list(void)
{
void **hints, **n;
char *name, *descr, *descr1, *io;
const char *filter;
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
return;
n = hints;
filter = stream == SND_PCM_STREAM_CAPTURE ? "Input" : "Output";
while (*n != NULL) {
name = snd_device_name_get_hint(*n, "NAME");
descr = snd_device_name_get_hint(*n, "DESC");
io = snd_device_name_get_hint(*n, "IOID");
if (io != NULL && strcmp(io, filter) != 0)
goto __end;
printf("%s\n", name);
if ((descr1 = descr) != NULL) {
printf(" ");
while (*descr1) {
if (*descr1 == '\n')
printf("\n ");
else
putchar(*descr1);
descr1++;
}
putchar('\n');
}
__end:
if (name != NULL)
free(name);
if (descr != NULL)
free(descr);
if (io != NULL)
free(io);
n++;
}
snd_device_name_free_hint(hints);
}
static void version(void)
{
printf("%s: version " SND_UTIL_VERSION_STR " by Jaroslav Kysela <perex@perex.cz>\n", command);
}
/*
* Subroutine to clean up before exit.
*/
static void prg_exit(int code)
{
done_stdin();
if (handle)
snd_pcm_close(handle);
if (pidfile_written)
remove (pidfile_name);
exit(code);
}
static void signal_handler(int sig)
{
if (in_aborting)
return;
in_aborting = 1;
if (verbose==2)
putchar('\n');
if (!quiet_mode)
fprintf(stderr, _("Aborted by signal %s...\n"), strsignal(sig));
if (handle)
snd_pcm_abort(handle);
if (sig == SIGABRT) {
/* do not call snd_pcm_close() and abort immediately */
handle = NULL;
prg_exit(EXIT_FAILURE);
}
signal(sig, SIG_DFL);
}
/* call on SIGUSR1 signal. */
static void signal_handler_recycle(int sig ATTRIBUTE_UNUSED)
{
/* flag the capture loop to start a new output file */
recycle_capture_file = 1;
}
enum {
OPT_VERSION = 1,
OPT_PERIOD_SIZE,
OPT_BUFFER_SIZE,
OPT_DISABLE_RESAMPLE,
OPT_DISABLE_CHANNELS,
OPT_DISABLE_FORMAT,
OPT_DISABLE_SOFTVOL,
OPT_TEST_POSITION,
OPT_TEST_COEF,
OPT_TEST_NOWAIT,
OPT_MAX_FILE_TIME,
OPT_PROCESS_ID_FILE,
OPT_USE_STRFTIME,
OPT_DUMP_HWPARAMS,
OPT_FATAL_ERRORS,
OPT_SUBFORMAT,
};
/*
* 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, (char *)buf + offset, count - offset);
if (written <= 0)
return written;
offset += written;
};
return offset;
}
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;
}
int main(int argc, char *argv[])
{
int duration_or_sample = 0;
int option_index;
static const char short_options[] = "hnlLD:qt:c:f:r:d:s:MNF:A:R:T:B:vV:IPCi"
#ifdef CONFIG_SUPPORT_CHMAP
"m:"
#endif
;
static const struct option long_options[] = {
{"help", 0, 0, 'h'},
{"version", 0, 0, OPT_VERSION},
{"list-devnames", 0, 0, 'n'},
{"list-devices", 0, 0, 'l'},
{"list-pcms", 0, 0, 'L'},
{"device", 1, 0, 'D'},
{"quiet", 0, 0, 'q'},
{"file-type", 1, 0, 't'},
{"channels", 1, 0, 'c'},
{"format", 1, 0, 'f'},
{"subformat", 1, 0, OPT_SUBFORMAT},
{"rate", 1, 0, 'r'},
{"duration", 1, 0 ,'d'},
{"samples", 1, 0, 's'},
{"mmap", 0, 0, 'M'},
{"nonblock", 0, 0, 'N'},
{"period-time", 1, 0, 'F'},
{"period-size", 1, 0, OPT_PERIOD_SIZE},
{"avail-min", 1, 0, 'A'},
{"start-delay", 1, 0, 'R'},
{"stop-delay", 1, 0, 'T'},
{"buffer-time", 1, 0, 'B'},
{"buffer-size", 1, 0, OPT_BUFFER_SIZE},
{"verbose", 0, 0, 'v'},
{"vumeter", 1, 0, 'V'},
{"separate-channels", 0, 0, 'I'},
{"playback", 0, 0, 'P'},
{"capture", 0, 0, 'C'},
{"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},
{"test-position", 0, 0, OPT_TEST_POSITION},
{"test-coef", 1, 0, OPT_TEST_COEF},
{"test-nowait", 0, 0, OPT_TEST_NOWAIT},
{"max-file-time", 1, 0, OPT_MAX_FILE_TIME},
{"process-id-file", 1, 0, OPT_PROCESS_ID_FILE},
{"use-strftime", 0, 0, OPT_USE_STRFTIME},
{"interactive", 0, 0, 'i'},
{"dump-hw-params", 0, 0, OPT_DUMP_HWPARAMS},
{"fatal-errors", 0, 0, OPT_FATAL_ERRORS},
#ifdef CONFIG_SUPPORT_CHMAP
{"chmap", 1, 0, 'm'},
#endif
{0, 0, 0, 0}
};
char *pcm_name = "default";
int tmp, err, c;
int do_device_list = 0, do_pcm_list = 0, force_sample_format = 0;
snd_pcm_info_t *info;
FILE *direction;
#ifdef ENABLE_NLS
setlocale(LC_ALL, "");
textdomain(PACKAGE);
#endif
snd_pcm_info_alloca(&info);
err = snd_output_stdio_attach(&log, stderr, 0);
assert(err >= 0);
command = argv[0];
file_type = FORMAT_DEFAULT;
if (strstr(argv[0], "arecord")) {
stream = SND_PCM_STREAM_CAPTURE;
file_type = FORMAT_WAVE;
command = "arecord";
start_delay = 1;
direction = stdout;
} else if (strstr(argv[0], "aplay")) {
stream = SND_PCM_STREAM_PLAYBACK;
command = "aplay";
direction = stdin;
} else {
error(_("command should be named either arecord or aplay"));
return 1;
}
if (isatty(fileno(direction)) && (argc == 1)) {
usage(command);
return 1;
}
chunk_size = -1;
rhwparams.format = DEFAULT_FORMAT;
rhwparams.subformat = SND_PCM_SUBFORMAT_STD;
rhwparams.rate = DEFAULT_SPEED;
rhwparams.channels = 1;
while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage(command);
return 0;
case OPT_VERSION:
version();
return 0;
case 'l':
do_device_list = 1;
break;
case 'L':
do_pcm_list = 1;
break;
case 'D':
pcm_name = optarg;
break;
case 'q':
quiet_mode = 1;
break;
case 't':
if (strcasecmp(optarg, "raw") == 0)
file_type = FORMAT_RAW;
else if (strcasecmp(optarg, "voc") == 0)
file_type = FORMAT_VOC;
else if (strcasecmp(optarg, "wav") == 0)
file_type = FORMAT_WAVE;
else if (strcasecmp(optarg, "au") == 0 || strcasecmp(optarg, "sparc") == 0)
file_type = FORMAT_AU;
else {
error(_("unrecognized file format %s"), optarg);
return 1;
}
break;
case 'c':
rhwparams.channels = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid channels argument '%s'"), optarg);
return 1;
}
if (rhwparams.channels < 1 || rhwparams.channels > 256) {
error(_("value %i for channels is invalid"), rhwparams.channels);
return 1;
}
break;
case 'f':
force_sample_format = 1;
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;
rhwparams.rate = 44100;
rhwparams.channels = 2;
} else if (strcasecmp(optarg, "dat") == 0) {
rhwparams.format = file_type == FORMAT_AU ? SND_PCM_FORMAT_S16_BE : SND_PCM_FORMAT_S16_LE;
rhwparams.rate = 48000;
rhwparams.channels = 2;
} else {
rhwparams.format = snd_pcm_format_value(optarg);
if (rhwparams.format == SND_PCM_FORMAT_UNKNOWN) {
error(_("wrong extended format '%s'"), optarg);
prg_exit(EXIT_FAILURE);
}
}
break;
case OPT_SUBFORMAT:
#if SND_LIB_VER(1, 2, 10) < SND_LIB_VERSION
rhwparams.subformat = snd_pcm_subformat_value(optarg);
if (rhwparams.subformat == SND_PCM_SUBFORMAT_UNKNOWN) {
#else
if (strcasecmp(optarg, "std") != 0 && strcasecmp(optarg, "standard") != 0) {
#endif
error(_("wrong extended subformat '%s'"), optarg);
prg_exit(EXIT_FAILURE);
}
break;
case 'r':
tmp = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid rate argument '%s'"), optarg);
return 1;
}
if (tmp < 1000)
tmp *= 1000;
rhwparams.rate = tmp;
if (tmp < 2000 || tmp > 768000) {
error(_("bad speed value %i"), tmp);
return 1;
}
break;
case 'd':
if (duration_or_sample) {
error(_("duration and samples arguments cannot be used together"));
return 1;
}
timelimit = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid duration argument '%s'"), optarg);
return 1;
}
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;
break;
case 'N':
nonblock = 1;
open_mode |= SND_PCM_NONBLOCK;
break;
case 'F':
period_time = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid period time argument '%s'"), optarg);
return 1;
}
break;
case 'B':
buffer_time = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid buffer time argument '%s'"), optarg);
return 1;
}
break;
case OPT_PERIOD_SIZE:
period_frames = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid period size argument '%s'"), optarg);
return 1;
}
break;
case OPT_BUFFER_SIZE:
buffer_frames = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid buffer size argument '%s'"), optarg);
return 1;
}
break;
case 'A':
avail_min = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid min available space argument '%s'"), optarg);
return 1;
}
break;
case 'R':
start_delay = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid start delay argument '%s'"), optarg);
return 1;
}
break;
case 'T':
stop_delay = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid stop delay argument '%s'"), optarg);
return 1;
}
break;
case 'v':
verbose++;
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;
break;
case 'M':
mmap_flag = 1;
break;
case 'I':
interleaved = 0;
break;
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;
case 'i':
interactive = 1;
break;
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;
case OPT_TEST_POSITION:
test_position = 1;
break;
case OPT_TEST_COEF:
test_coef = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid test coef argument '%s'"), optarg);
return 1;
}
if (test_coef < 1)
test_coef = 1;
break;
case OPT_TEST_NOWAIT:
test_nowait = 1;
break;
case OPT_MAX_FILE_TIME:
max_file_time = parse_long(optarg, &err);
if (err < 0) {
error(_("invalid max file time argument '%s'"), optarg);
return 1;
}
break;
case OPT_PROCESS_ID_FILE:
pidfile_name = optarg;
break;
case OPT_USE_STRFTIME:
use_strftime = 1;
break;
case OPT_DUMP_HWPARAMS:
dump_hw_params = 1;
break;
case OPT_FATAL_ERRORS:
fatal_errors = 1;
break;
#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
default:
fprintf(stderr, _("Try `%s --help' for more information.\n"), command);
return 1;
}
}
if (do_device_list) {
if (do_pcm_list) pcm_list();
device_list();
goto __end;
} else if (do_pcm_list) {
pcm_list();
goto __end;
}
err = snd_pcm_open(&handle, pcm_name, stream, open_mode);
if (err < 0) {
error(_("audio open error: %s"), snd_strerror(err));
return 1;
}
if ((err = snd_pcm_info(handle, info)) < 0) {
error(_("info error: %s"), snd_strerror(err));
return 1;
}
if (nonblock) {
err = snd_pcm_nonblock(handle, 1);
if (err < 0) {
error(_("nonblock setting error: %s"), snd_strerror(err));
return 1;
}
}
if (!force_sample_format &&
isatty(fileno(stdin)) &&
stream == SND_PCM_STREAM_CAPTURE &&
snd_pcm_format_width(rhwparams.format) <= 8)
fprintf(stderr, "Warning: Some sources (like microphones) may produce inaudible results\n"
" with 8-bit sampling. Use '-f' argument to increase resolution\n"
" e.g. '-f S16_LE'.\n");
chunk_size = 1024;
hwparams = rhwparams;
audiobuf = (uint8_t *)malloc(1024);
if (audiobuf == NULL) {
error(_("not enough memory"));
return 1;
}
if (mmap_flag) {
writei_func = snd_pcm_mmap_writei;
readi_func = snd_pcm_mmap_readi;
writen_func = snd_pcm_mmap_writen;
readn_func = snd_pcm_mmap_readn;
} else {
writei_func = snd_pcm_writei;
readi_func = snd_pcm_readi;
writen_func = snd_pcm_writen;
readn_func = snd_pcm_readn;
}
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;
}
}
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGABRT, signal_handler);
signal(SIGUSR1, signal_handler_recycle);
if (interleaved) {
if (optind > argc - 1) {
if (stream == SND_PCM_STREAM_PLAYBACK)
playback(NULL);
else
capture(NULL);
} else {
while (optind <= argc - 1) {
if (stream == SND_PCM_STREAM_PLAYBACK)
playback(argv[optind++]);
else
capture(argv[optind++]);
}
}
} else {
if (stream == SND_PCM_STREAM_PLAYBACK)
playbackv(&argv[optind], argc - optind);
else
capturev(&argv[optind], argc - optind);
}
if (verbose==2)
putchar('\n');
snd_pcm_close(handle);
handle = NULL;
free(audiobuf);
__end:
snd_output_close(log);
snd_config_update_free_global();
prg_exit(EXIT_SUCCESS);
/* avoid warning */
return EXIT_SUCCESS;
}
/*
* Safe read (for pipes)
*/
static ssize_t safe_read(int fd, void *buf, size_t count)
{
ssize_t result = 0, res;
while (count > 0 && !in_aborting) {
if ((res = read(fd, buf, count)) == 0)
break;
if (res < 0)
return result > 0 ? result : res;
count -= res;
result += res;
buf = (char *)buf + res;
}
return result;
}
/*
* 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)
{
VocHeader *vp = buffer;
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)))
return -2; /* coded version mismatch */
return LE_SHORT(vp->headerlen) - sizeof(VocHeader); /* 0 mostly */
}
return -1; /* magic string fail */
}
/*
* helper for test_wavefile
*/
static size_t test_wavefile_read(int fd, uint8_t *buffer, size_t *size, size_t reqsize, int line)
{
if (*size >= reqsize)
return *size;
if ((size_t)safe_read(fd, buffer + *size, reqsize - *size) != reqsize - *size) {
error(_("read error (called from line %i)"), line);
prg_exit(EXIT_FAILURE);
}
return *size = reqsize;
}
#define check_wavefile_space(buffer, len, blimit) \