annotate alsacap.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents 9cadc470e3da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
1 /*
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
2 * ALSA parameter test program
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
3 *
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
4 * compile with: gcc -o alsacap alsacap.c -lasound
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
5 */
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
6
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
7
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
8 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
9 Includes
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
10 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
11
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
12 #include <stdlib.h>
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
13 #include <stdio.h>
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
14 #include <alsa/asoundlib.h>
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
15 #include <errno.h>
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
16 #include <string.h>
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
17 #include <ctype.h>
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
18
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
19 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
20 Constant and type definitions
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
21 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
22
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
23 #define RATE_KHZ_LIMIT 200
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
24
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
25 #define HWP_END 0
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
26 #define HWP_RATE 1
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
27 #define HWP_NCH 2
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
28 #define HWP_FORMAT 3
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
29 #define SIZE_HWP 7
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
30
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
31 typedef struct {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
32 int recdevices, verbose, card, dev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
33 char *device;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
34 int hwparams[SIZE_HWP];
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
35 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
36 aiopts;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
37
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
38
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
39 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
40 Global variables
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
41 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
42
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
43 static snd_ctl_t *handle= NULL;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
44 static snd_pcm_t *pcm= NULL;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
45 static snd_ctl_card_info_t *info;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
46 static snd_pcm_info_t *pcminfo;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
47 static snd_pcm_hw_params_t *pars;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
48 static snd_pcm_format_mask_t *fmask;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
49
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
50
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
51 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
52 Prototypes
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
53 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
54
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
55 void usagemsg(int code);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
56 void errnumarg(char optchar);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
57 void errarg(char optchar);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
58 void errtoomany();
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
59
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
60 void scancards(snd_pcm_stream_t stream, int thecard, int thedev);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
61 int sc_errcheck(int retval, const char *doingwhat, int cardnr, int devnr);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
62
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
63 void testconfig(snd_pcm_stream_t stream, const char *device, const int *hwpars);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
64 void tc_errcheck(int retval, const char *doingwhat);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
65
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
66 const char *alsaerrstr(const int errcode);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
67 const char *dirstr(int dir);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
68
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
69 int parse_alsaformat(const char *fmtstr);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
70 const char *alsafmtstr(int fmtnum);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
71
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
72 void printfmtmask(const snd_pcm_format_mask_t *fmask);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
73
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
74
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
75 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
76 Main program
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
77 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
78
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
79 int main(int argc, char **argv)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
80 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
81 aiopts options= { 0, 1, -1, -1, NULL };
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
82 snd_pcm_stream_t stream;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
83 char *argpar;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
84 int argind, hwpind;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
85
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
86 snd_ctl_card_info_alloca(&info);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
87 snd_pcm_info_alloca(&pcminfo);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
88 snd_pcm_hw_params_alloca(&pars);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
89 snd_pcm_format_mask_alloca(&fmask);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
90
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
91 hwpind= 0;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
92 for( argind= 1; argind< argc; ++argind )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
93 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
94 if( argv[argind][0]!='-' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
95 fprintf(stderr, "Unrecognised command-line argument `%s'.\n", argv[argind]);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
96 usagemsg(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
97 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
98 if( argv[argind][2] ) argpar= argv[argind]+2;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
99 else {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
100 if( argind+1 >= argc ) argpar= NULL;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
101 else argpar= argv[argind+1];
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
102 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
103 if( argv[argind][1]=='h' || !strcmp(argv[argind]+1, "-help") )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
104 usagemsg(0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
105 else if( argv[argind][1]=='R' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
106 options.recdevices= 1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
107 argpar= NULL; // set to NULL if unused to keep track of next arg index
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
108 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
109 else if( argv[argind][1]=='C' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
110 if( !argpar || !isdigit(*argpar) ) errnumarg('C');
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
111 options.card= strtol(argpar, NULL, 0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
112 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
113 else if( argv[argind][1]=='D' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
114 if( !argpar || !isdigit(*argpar) ) errnumarg('D');
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
115 options.dev= strtol(argpar, NULL, 0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
116 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
117 else if( argv[argind][1]=='d' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
118 if( !argpar ) errarg('d');
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
119 options.device= argpar;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
120 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
121 else if( argv[argind][1]=='r' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
122 if( !argpar || !isdigit(*argpar) ) errnumarg('r');
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
123 if( hwpind+3 > SIZE_HWP ) errtoomany();
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
124 options.hwparams[hwpind++]= HWP_RATE;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
125 options.hwparams[hwpind]= strtol(argpar, NULL, 0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
126 if( options.hwparams[hwpind] <= RATE_KHZ_LIMIT )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
127 options.hwparams[hwpind] *= 1000; // sanity check: Hz or kHz ?
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
128 ++hwpind;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
129 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
130 else if( argv[argind][1]=='c' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
131 if( !argpar || !isdigit(*argpar) ) errnumarg('c');
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
132 if( hwpind+3 > SIZE_HWP ) errtoomany();
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
133 options.hwparams[hwpind++]= HWP_NCH;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
134 options.hwparams[hwpind++]= strtol(argpar, NULL, 0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
135 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
136 else if( argv[argind][1]=='f' ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
137 if( !argpar ) errarg('f');
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
138 if( hwpind+3 > SIZE_HWP ) errtoomany();
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
139 options.hwparams[hwpind++]= HWP_FORMAT;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
140 options.hwparams[hwpind++]= parse_alsaformat(argpar);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
141 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
142 else {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
143 fprintf(stderr, "Unrecognised command-line option `%s'.\n", argv[argind]);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
144 usagemsg(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
145 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
146 if( argpar && !argv[argind][2] )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
147 ++argind; // additional increment if separate parameter argument was used
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
148 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
149 options.hwparams[hwpind]= HWP_END;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
150 if( options.dev >= 0 && options.card < 0 ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
151 fprintf(stderr, "The card has to be specified with -C if a device number is given (-D).\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
152 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
153 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
154 if( options.device && (options.card>=0 || options.dev>=0) ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
155 fprintf(stderr, "Specifying a device name (-d) and a card and possibly device number (-C, -D) is mutually exclusive.\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
156 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
157 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
158 stream= options.recdevices? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
159
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
160 if( !options.device )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
161 scancards(stream, options.card, options.dev);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
162 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
163 testconfig(stream, options.device, options.hwparams);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
164
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
165 exit(0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
166 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
167
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
168
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
169
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
170 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
171 Usage message and command-line argument error functions
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
172 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
173
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
174 void usagemsg(int code)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
175 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
176 fprintf(stderr, "Usage: alsacap [-R] [-C <card #> [-D <device #>]]\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
177 " alsacap [-R] -d <device name> [-r <rate>|-c <# of channels>|-f <sample format>]...\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
178 "ALSA capability lister.\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
179 "First form: Scans one or all soundcards known to ALSA for devices, \n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
180 "subdevices and parameter ranges. -R causes a scan for recording\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
181 "rather than playback devices. The other options specify the sound\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
182 "card and possibly the device by number.\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
183 "Second form: Displays ranges of configuration parameters for the given\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
184 "ALSA device. Unlike with the first form, a non-hardware device may be\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
185 "given. Up to three optional command-line arguments fix the rate,\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
186 "number of channels and sample format in the order in which they are\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
187 "given. The remaining parameter ranges are output. If unique, the\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
188 "number of significant bits of the sample values is output. (Some\n"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
189 "sound cards ignore some of the bits.)\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
190 exit(code);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
191 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
192
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
193 void errnumarg(char optchar)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
194 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
195 fprintf(stderr, "The -%c option requires a numerical argument! Aborting.\n", optchar);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
196 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
197 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
198
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
199 void errarg(char optchar)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
200 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
201 fprintf(stderr, "The -%c option requires an argument! Aborting.\n", optchar);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
202 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
203 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
204
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
205 void errtoomany()
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
206 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
207 fprintf(stderr, "Too many -r/-c/-f options given! (Maximum is %d.) Aborting.\n", (SIZE_HWP-1)/2);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
208 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
209 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
210
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
211
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
212 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
213 Function for scanning all cards
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
214 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
215
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
216 #define HWCARDTEMPL "hw:%d"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
217 #define HWDEVTEMPL "hw:%d,%d"
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
218 #define HWDEVLEN 32
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
219
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
220 void scancards(snd_pcm_stream_t stream, int thecard, int thedev)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
221 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
222 char hwdev[HWDEVLEN+1];
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
223 unsigned min, max;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
224 int card, err, dev, subd, nsubd;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
225
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
226 printf("*** Scanning for %s devices", stream==SND_PCM_STREAM_CAPTURE? "recording" : "playback");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
227 if( thecard >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
228 printf(" on card %d", thecard);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
229 if( thedev >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
230 printf(", device %d", thedev);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
231 printf(" ***\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
232 hwdev[HWDEVLEN]= 0;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
233 if( thecard >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
234 card= thecard;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
235 else {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
236 card= -1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
237 if( snd_card_next(&card) < 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
238 return;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
239 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
240 while( card >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
241 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
242 snprintf(hwdev, HWDEVLEN, HWCARDTEMPL, card);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
243 err= snd_ctl_open(&handle, hwdev, 0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
244 if( sc_errcheck(err, "opening control interface", card, -1) ) goto nextcard;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
245 err= snd_ctl_card_info(handle, info);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
246 if( sc_errcheck(err, "obtaining card info", card, -1) ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
247 snd_ctl_close(handle);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
248 goto nextcard;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
249 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
250 printf("Card %d, ID `%s', name `%s'\n", card, snd_ctl_card_info_get_id(info),
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
251 snd_ctl_card_info_get_name(info));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
252 if( thedev >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
253 dev= thedev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
254 else {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
255 dev= -1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
256 if( snd_ctl_pcm_next_device(handle, &dev) < 0 ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
257 snd_ctl_close(handle);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
258 goto nextcard;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
259 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
260 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
261 while( dev >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
262 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
263 snd_pcm_info_set_device(pcminfo, dev);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
264 snd_pcm_info_set_subdevice(pcminfo, 0);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
265 snd_pcm_info_set_stream(pcminfo, stream);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
266 err= snd_ctl_pcm_info(handle, pcminfo);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
267 if( thedev<0 && err == -ENOENT ) goto nextdev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
268 if( sc_errcheck(err, "obtaining device info", card, dev) ) goto nextdev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
269 nsubd= snd_pcm_info_get_subdevices_count(pcminfo);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
270 if( sc_errcheck(nsubd, "obtaining device info", card, dev) ) goto nextdev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
271 printf(" Device %d, ID `%s', name `%s', %d subdevices (%d available)\n",
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
272 dev, snd_pcm_info_get_id(pcminfo), snd_pcm_info_get_name(pcminfo),
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
273 nsubd, snd_pcm_info_get_subdevices_avail(pcminfo));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
274 snprintf(hwdev, HWDEVLEN, HWDEVTEMPL, card, dev);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
275 err= snd_pcm_open(&pcm, hwdev, stream, SND_PCM_NONBLOCK);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
276 if( sc_errcheck(err, "opening sound device", card, dev) ) goto nextdev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
277 err= snd_pcm_hw_params_any(pcm, pars);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
278 if( sc_errcheck(err, "obtaining hardware parameters", card, dev) ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
279 snd_pcm_close(pcm);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
280 goto nextdev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
281 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
282 snd_pcm_hw_params_get_channels_min(pars, &min);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
283 snd_pcm_hw_params_get_channels_max(pars, &max);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
284 if( min == max )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
285 if( min == 1 ) printf(" 1 channel, ");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
286 else printf(" %d channels, ", min);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
287 else printf(" %u..%u channels, ", min, max);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
288 snd_pcm_hw_params_get_rate_min(pars, &min, NULL);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
289 snd_pcm_hw_params_get_rate_max(pars, &max, NULL);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
290 printf("sampling rate %u..%u Hz\n Sample formats: ", min, max);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
291 snd_pcm_hw_params_get_format_mask(pars, fmask);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
292 printfmtmask(fmask);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
293 snd_pcm_close(pcm);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
294 printf("\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
295 pcm= NULL;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
296 for( subd= 0; subd< nsubd; ++subd ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
297 snd_pcm_info_set_subdevice(pcminfo, subd);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
298 err= snd_ctl_pcm_info(handle, pcminfo);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
299 if( sc_errcheck(err, "obtaining subdevice info", card, dev) ) goto nextdev;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
300 printf(" Subdevice %d, name `%s'\n", subd, snd_pcm_info_get_subdevice_name(pcminfo));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
301 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
302 nextdev:
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
303 if( thedev >= 0 || snd_ctl_pcm_next_device(handle, &dev) < 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
304 break;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
305 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
306 snd_ctl_close(handle);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
307 nextcard:
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
308 if( thecard >= 0 || snd_card_next(&card) < 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
309 break;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
310 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
311 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
312
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
313
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
314 int sc_errcheck(int retval, const char *doingwhat, int cardnr, int devnr)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
315 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
316 if( retval<0 ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
317 if( devnr>= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
318 fprintf(stderr, "Error %s for card %d, device %d: %s. Skipping.\n", doingwhat, cardnr, devnr, alsaerrstr(retval));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
319 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
320 fprintf(stderr, "Error %s for card %d: %s. Skipping.\n", doingwhat, cardnr, alsaerrstr(retval));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
321 return 1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
322 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
323 return 0;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
324 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
325
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
326
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
327
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
328 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
329 Function for investigating device configurations
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
330 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
331
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
332 void testconfig(snd_pcm_stream_t stream, const char *device, const int *hwpars)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
333 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
334 unsigned min, max, param;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
335 int err, count, dir, result;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
336
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
337 printf("*** Exploring configuration space of device `%s' for %s ***\n", device,
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
338 stream==SND_PCM_STREAM_CAPTURE? "recording" : "playback");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
339
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
340 if (stream==SND_PCM_STREAM_CAPTURE && stream==SND_PCM_STREAM_PLAYBACK)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
341 dir = 0;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
342 else if (stream==SND_PCM_STREAM_CAPTURE)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
343 dir = -1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
344 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
345 dir = 1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
346
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
347 err= snd_pcm_open(&pcm, device, stream, SND_PCM_NONBLOCK);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
348 tc_errcheck(err, "opening sound device");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
349 err= snd_pcm_hw_params_any(pcm, pars);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
350 tc_errcheck(err, "initialising hardware parameters");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
351 for( count= 0; hwpars[count]!=HWP_END; count += 2 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
352
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
353 switch(hwpars[count])
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
354 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
355 case HWP_RATE:param= hwpars[count+1];
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
356 err= snd_pcm_hw_params_set_rate_near(pcm, pars, &param, &result);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
357 if( err<0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
358 fprintf(stderr, "Could not set sampling rate to %d Hz: %s. "
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
359 "Continuing regardless.\n", hwpars[count+1], alsaerrstr(err));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
360 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
361 printf("Set sampling rate %d Hz --> got %u Hz, %s requested.\n", hwpars[count+1], param, dirstr(dir));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
362 break;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
363 case HWP_NCH:err= snd_pcm_hw_params_set_channels(pcm, pars, hwpars[count+1]);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
364 if( err<0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
365 fprintf(stderr, "Could not set # of channels to %d: %s. "
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
366 "Continuing regardless.\n", hwpars[count+1], alsaerrstr(err));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
367 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
368 printf("Set number of channels to %d.\n", hwpars[count+1]);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
369 break;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
370 case HWP_FORMAT:err= snd_pcm_hw_params_set_format(pcm, pars, hwpars[count+1]);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
371 if( err<0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
372 fprintf(stderr, "Could not set sample format to %s: %s."
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
373 " Continuing regardless.\n", alsafmtstr(hwpars[count+1]), alsaerrstr(err));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
374 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
375 printf("Set sample format to %s.\n", alsafmtstr(hwpars[count+1]));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
376 break;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
377 default:
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
378 break;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
379 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
380 if( count>0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
381 printf("Parameter ranges remaining after these settings:\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
382 snd_pcm_hw_params_get_channels_min(pars, &min);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
383 snd_pcm_hw_params_get_channels_max(pars, &max);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
384 if( min==max )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
385 if( min==1 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
386 printf("1 channel\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
387 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
388 printf("%u channels\n", min);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
389 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
390 printf("%u..%u channels\n", min, max);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
391 snd_pcm_hw_params_get_rate_min(pars, &min, NULL);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
392 snd_pcm_hw_params_get_rate_max(pars, &max, NULL);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
393 if( min==max )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
394 printf("Sampling rate %u Hz\nSample formats: ", min);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
395 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
396 printf("Sampling rate %u..%u Hz\nSample formats: ", min, max);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
397 snd_pcm_hw_params_get_format_mask(pars, fmask);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
398 printfmtmask(fmask);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
399 printf("\n");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
400 result= snd_pcm_hw_params_get_sbits(pars);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
401 if( result >= 0 ) // only available if bit width of all formats is the same
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
402 printf("Significant bits: %d\n", result);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
403 snd_pcm_close(pcm);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
404 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
405
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
406
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
407 void tc_errcheck(int retval, const char *doingwhat)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
408 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
409 if( retval<0 ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
410 fprintf(stderr, "Error %s: %s. Aborting.\n", doingwhat, alsaerrstr(retval));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
411 if( pcm )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
412 snd_pcm_close(pcm);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
413 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
414 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
415 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
416
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
417
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
418 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
419 String-building functions
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
420 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
421
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
422 struct alsaerr { int err; char *msg; };
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
423 struct alsaerr aelist[]= {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
424 {-EBADFD, "PCM device is in a bad state"},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
425 {-EPIPE, "An underrun occurred"},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
426 {-ESTRPIPE, "A suspend event occurred"},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
427 {-ENOTTY, "Hotplug device has been removed"},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
428 {-ENODEV, "Hotplug device has been removed"},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
429 {-ENOENT, "Device does not exist"},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
430 {0, NULL}
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
431 };
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
432 const char *alsaerrstr(const int errcode)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
433 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
434 struct alsaerr *search;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
435
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
436 if( errcode >= 0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
437 return "No error";
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
438 for( search= aelist; search->msg && search->err!=errcode; ++search);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
439 if( search->msg )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
440 return search->msg;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
441 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
442 return strerror(-errcode);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
443 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
444
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
445
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
446 const char *dirstr(int dir)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
447 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
448 if( !dir )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
449 return "=";
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
450 else if( dir<0 )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
451 return "<";
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
452 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
453 return ">";
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
454 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
455
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
456
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
457 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
458 Functions for parsing and string output of ALSA sample formats
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
459 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
460
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
461 struct fmtdef { char *fmtname; int format; };
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
462 static struct fmtdef fmtlist[]= {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
463 {"S8", SND_PCM_FORMAT_S8},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
464 {"U8", SND_PCM_FORMAT_U8},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
465 {"S16_LE", SND_PCM_FORMAT_S16_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
466 {"S16_BE", SND_PCM_FORMAT_S16_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
467 {"U16_LE", SND_PCM_FORMAT_U16_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
468 {"U16_BE", SND_PCM_FORMAT_U16_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
469 {"S24_LE", SND_PCM_FORMAT_S24_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
470 {"S24_BE", SND_PCM_FORMAT_S24_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
471 {"U24_LE", SND_PCM_FORMAT_U24_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
472 {"U24_BE", SND_PCM_FORMAT_U24_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
473 {"S32_LE", SND_PCM_FORMAT_S32_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
474 {"S32_BE", SND_PCM_FORMAT_S32_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
475 {"U32_LE", SND_PCM_FORMAT_U32_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
476 {"U32_BE", SND_PCM_FORMAT_U32_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
477 {"FLOAT_LE", SND_PCM_FORMAT_FLOAT_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
478 {"FLOAT_BE", SND_PCM_FORMAT_FLOAT_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
479 {"FLOAT64_LE", SND_PCM_FORMAT_FLOAT64_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
480 {"FLOAT64_BE", SND_PCM_FORMAT_FLOAT64_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
481 {"IEC958_SUBFRAME_LE", SND_PCM_FORMAT_IEC958_SUBFRAME_LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
482 {"IEC958_SUBFRAME_BE", SND_PCM_FORMAT_IEC958_SUBFRAME_BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
483 {"MU_LAW", SND_PCM_FORMAT_MU_LAW},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
484 {"A_LAW", SND_PCM_FORMAT_A_LAW},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
485 {"IMA_ADPCM", SND_PCM_FORMAT_IMA_ADPCM},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
486 {"MPEG", SND_PCM_FORMAT_MPEG},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
487 {"GSM", SND_PCM_FORMAT_GSM},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
488 {"SPECIAL", SND_PCM_FORMAT_SPECIAL},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
489 {"S24_3LE", SND_PCM_FORMAT_S24_3LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
490 {"S24_3BE", SND_PCM_FORMAT_S24_3BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
491 {"U24_3LE", SND_PCM_FORMAT_U24_3LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
492 {"U24_3BE", SND_PCM_FORMAT_U24_3BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
493 {"S20_3LE", SND_PCM_FORMAT_S20_3LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
494 {"S20_3BE", SND_PCM_FORMAT_S20_3BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
495 {"U20_3LE", SND_PCM_FORMAT_U20_3LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
496 {"U20_3BE", SND_PCM_FORMAT_U20_3BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
497 {"S18_3LE", SND_PCM_FORMAT_S18_3LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
498 {"S18_3BE", SND_PCM_FORMAT_S18_3BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
499 {"U18_3LE", SND_PCM_FORMAT_U18_3LE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
500 {"U18_3BE", SND_PCM_FORMAT_U18_3BE},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
501 {"S16", SND_PCM_FORMAT_S16},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
502 {"U16", SND_PCM_FORMAT_U16},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
503 {"S24", SND_PCM_FORMAT_S24},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
504 {"U24", SND_PCM_FORMAT_U24},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
505 {"S32", SND_PCM_FORMAT_S32},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
506 {"U32", SND_PCM_FORMAT_U32},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
507 {"FLOAT", SND_PCM_FORMAT_FLOAT},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
508 {"FLOAT64", SND_PCM_FORMAT_FLOAT64},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
509 {"IEC958_SUBFRAME", SND_PCM_FORMAT_IEC958_SUBFRAME},
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
510 {NULL, 0}
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
511 };
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
512
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
513 int parse_alsaformat(const char *fmtstr)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
514 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
515 struct fmtdef *search;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
516
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
517 for( search= fmtlist; search->fmtname && strcmp(search->fmtname, fmtstr); ++search );
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
518 if( !search->fmtname ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
519 fprintf(stderr, "Unknown sample format `%s'. Aborting.\n", fmtstr);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
520 exit(1);
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
521 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
522 return search->format;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
523 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
524
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
525 const char *alsafmtstr(int fmtnum)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
526 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
527 struct fmtdef *search;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
528
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
529 for( search= fmtlist; search->fmtname && search->format!=fmtnum; ++search );
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
530 if( !search->fmtname )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
531 return "(unknown)";
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
532 else
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
533 return search->fmtname;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
534 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
535
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
536
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
537 /*============================================================================
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
538 Printout functions
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
539 ============================================================================*/
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
540
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
541 void printfmtmask(const snd_pcm_format_mask_t *fmask)
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
542 {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
543 int fmt, prevformat= 0;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
544
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
545 for( fmt= 0; fmt <= SND_PCM_FORMAT_LAST; ++fmt )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
546 if( snd_pcm_format_mask_test(fmask, (snd_pcm_format_t)fmt) ) {
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
547 if( prevformat )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
548 printf(", ");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
549 printf("%s", snd_pcm_format_name((snd_pcm_format_t)fmt));
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
550 prevformat= 1;
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
551 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
552 if( !prevformat )
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
553 printf("(none)");
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
554 }
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
555
9cadc470e3da reorganize, add more
pmeerw@pan
parents:
diff changeset
556

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.