-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalsa-capture.c
289 lines (216 loc) · 9.1 KB
/
alsa-capture.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
/*
A Minimal Capture Program
This program opens an audio interface for capture, configures it for
stereo, 16 bit, 44.1kHz, interleaved conventional read/write
access. Then its reads a chunk of random data from it, and exits. It
isn't meant to be a real program.
From on Paul David's tutorial : http://equalarea.com/paul/alsa-audio.html
Fixes rate and buffer problems
sudo apt-get install libasound2-dev
gcc -o alsa-record-example -lasound alsa-record-example.c && ./alsa-record-example hw:0
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <alsa/asoundlib.h>
// latency = periodsize * periods / (rate * bytes_per_frame)
int main (int argc, char *argv[])
{
int i;
int err;
char *buffer;
uint64_t period_size = 256; // in frames
uint64_t n_periods = 4; // number of periods
unsigned int rate = 96000;
uint32_t channels = 2;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_format_t format = SND_PCM_FORMAT_S32_LE;
if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
argv[1],
snd_strerror (err));
exit (1);
}
fprintf(stdout, "audio interface opened\n");
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params allocated\n");
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params initialized\n");
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params access setted\n");
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params format setted\n");
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params rate setted\n");
if ((err = snd_pcm_hw_params_test_period_size (capture_handle, hw_params, period_size, 0)) < 0) {
fprintf (stderr, "period size of %lu not good (%s)\n", period_size,
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_period_size_near(capture_handle, hw_params, &period_size, 0)) < 0) {
fprintf (stderr, "cannot set period size (%s)\n",
snd_strerror (err));
exit (1);
}
/* Set number of periods. Periods used to be called fragments. */
if ((err = snd_pcm_hw_params_set_periods(capture_handle, hw_params, n_periods, 0)) < 0){
fprintf(stderr, "Error setting periods.\n");
return(-1);
}
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params channels setted\n");
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "hw_params setted\n");
/* Display information about the PCM interface */
unsigned int val, val2;
printf("PCM capture_handle name = '%s'\n",
snd_pcm_name(capture_handle));
printf("PCM state = %s\n",
snd_pcm_state_name(snd_pcm_state(capture_handle)));
snd_pcm_hw_params_get_access(hw_params,
(snd_pcm_access_t *) &val);
printf("access type = %s\n",
snd_pcm_access_name((snd_pcm_access_t)val));
snd_pcm_hw_params_get_format(hw_params, (snd_pcm_format_t *) &val);
printf("format = '%s' (%s)\n",
snd_pcm_format_name((snd_pcm_format_t)val),
snd_pcm_format_description(
(snd_pcm_format_t)val));
snd_pcm_hw_params_get_subformat(hw_params,
(snd_pcm_subformat_t *)&val);
printf("subformat = '%s' (%s)\n",
snd_pcm_subformat_name((snd_pcm_subformat_t)val),
snd_pcm_subformat_description(
(snd_pcm_subformat_t)val));
snd_pcm_hw_params_get_channels(hw_params, &val);
printf("channels = %d\n", val);
snd_pcm_hw_params_get_rate(hw_params, &val, 0);
printf("rate = %d bps\n", val);
snd_pcm_hw_params_get_period_time(hw_params,
&val, 0);
printf("period time = %d us\n", val);
snd_pcm_uframes_t frames;
snd_pcm_hw_params_get_period_size(hw_params,
&frames, 0);
printf("period size = %d frames\n", (int)frames);
snd_pcm_hw_params_get_buffer_time(hw_params,
&val, 0);
printf("buffer time = %d us\n", val);
snd_pcm_hw_params_get_buffer_size(hw_params,
(snd_pcm_uframes_t *) &val);
printf("buffer size = %d frames\n", val);
snd_pcm_hw_params_get_periods(hw_params, &val, 0);
printf("periods per buffer = %d frames\n", val);
snd_pcm_hw_params_get_rate_numden(hw_params,
&val, &val2);
printf("exact rate = %d/%d bps\n", val, val2);
val = snd_pcm_hw_params_get_sbits(hw_params);
printf("significant bits = %d\n", val);
snd_pcm_hw_params_get_tick_time(hw_params,
&val, 0);
printf("tick time = %d us\n", val);
val = snd_pcm_hw_params_is_batch(hw_params);
printf("is batch = %d\n", val);
val = snd_pcm_hw_params_is_block_transfer(hw_params);
printf("is block transfer = %d\n", val);
val = snd_pcm_hw_params_is_double(hw_params);
printf("is double = %d\n", val);
val = snd_pcm_hw_params_is_half_duplex(hw_params);
printf("is half duplex = %d\n", val);
val = snd_pcm_hw_params_is_joint_duplex(hw_params);
printf("is joint duplex = %d\n", val);
val = snd_pcm_hw_params_can_overrange(hw_params);
printf("can overrange = %d\n", val);
val = snd_pcm_hw_params_can_mmap_sample_resolution(hw_params);
printf("can mmap = %d\n", val);
val = snd_pcm_hw_params_can_pause(hw_params);
printf("can pause = %d\n", val);
val = snd_pcm_hw_params_can_resume(hw_params);
printf("can resume = %d\n", val);
val = snd_pcm_hw_params_can_sync_start(hw_params);
printf("can sync start = %d\n", val);
snd_pcm_hw_params_free (hw_params);
fprintf(stdout, "hw_params freed\n");
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stdout, "audio interface prepared\n");
int sample_size = snd_pcm_format_width(format) / 8;
uint32_t buffer_size = period_size * sample_size * channels;
fprintf(stdout, "buffer size %u\n", buffer_size);
buffer = malloc(buffer_size);
fprintf(stdout, "buffer allocated\n");
uint8_t *left = (uint8_t *) malloc(buffer_size/2);
uint8_t *right = (uint8_t *) malloc(buffer_size/2);
FILE *left_fp = fopen("left.raw", "w");
FILE *right_fp = fopen("right.raw", "w");
for (i = 0; i < 1000000; ++i) {
if ((err = snd_pcm_mmap_readi(capture_handle, buffer, period_size)) != period_size) {
fprintf (stderr, "read from audio interface failed (%s)\n",
snd_strerror (err));
if (err == -EPIPE)
{
fprintf(stdout, "overrun\n");
snd_pcm_prepare (capture_handle);
}
else if (err < 0) {
fprintf(stderr,
"error from readi: %s\n",
snd_strerror(err));
exit (1);
} else if (err != period_size) {
fprintf(stderr,
"short read, read %d frames\n", err);
}
}
for (int j = 0; j < period_size; j++)
{
memcpy(&left[j*sample_size], &buffer[j * sample_size * channels], sample_size);
memcpy(&right[j*sample_size], &buffer[j * sample_size * channels + sample_size], sample_size);
}
// fprintf(stdout, "read %d done\n", i);
fwrite(left, buffer_size/2, 1, left_fp);
fwrite(right, buffer_size/2, 1, right_fp);
}
free(buffer);
free(left);
free(right);
fclose(left_fp);
fclose(right_fp);
fprintf(stdout, "buffer freed\n");
snd_pcm_drain(capture_handle);
snd_pcm_close (capture_handle);
fprintf(stdout, "audio interface closed\n");
exit (0);
}