-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-alsa.c
250 lines (207 loc) · 8.36 KB
/
simple-alsa.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
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
//#define BUF_SIZE 1024
//#define BUF_SIZE 128
//#define BUF_SIZE 46080
#define PERIODSIZE 8192
#define PERIODS 2
#define BUF_SIZE (PERIODSIZE * PERIODS)
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
int main (int argc, char *argv[])
{
int err;
unsigned int rate;
signed short buf[PERIODSIZE * PERIODS];
unsigned int resample;
snd_pcm_hw_params_t *hw_params_capture;
snd_pcm_hw_params_t *hw_params_playback;
snd_pcm_uframes_t capBufferSize, playBufferSize;
int periods = PERIODS;
snd_pcm_uframes_t periodsize = PERIODSIZE;
int loops;
if (argc < 3) {
fprintf(stderr, "usage: %s <capture device> <playback device>\n", argv[0]);
exit(1);
}
////////// capture /////////////
if ((err = snd_pcm_open(&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "cannot open audio capture device %s (%s)\n",
argv[1],
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_malloc(&hw_params_capture)) < 0) {
fprintf(stderr, "cannot allocate capture hardware parameter structure (%s)\n",
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_any(capture_handle, hw_params_capture)) < 0) {
fprintf(stderr, "cannot initialize capture hardware parameter structure (%s)\n",
snd_strerror(err));
exit(1);
}
#if 0
resample = 1;
err = snd_pcm_hw_params_set_rate_resample(capture_handle, hw_params_capture, resample);
if (err < 0) {
fprintf(stderr, "resampling setup failed for capture device (%s)\n",
snd_strerror(err));
exit(1);
}
#endif
if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params_capture, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "cannot set capture access type (%s)\n",
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params_capture, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf(stderr, "cannot set capture sample format (%s)\n",
snd_strerror(err));
exit(1);
}
rate = 44100;
if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params_capture, &rate, 0)) < 0) {
fprintf(stderr, "cannot set capture sample rate (%s)\n",
snd_strerror(err));
exit(1);
}
printf("capture rate set to %d\n", rate);
if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params_capture, 2)) < 0) {
fprintf(stderr, "cannot set capture channel count (%s)\n",
snd_strerror(err));
exit(1);
}
/* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(capture_handle, hw_params_capture, periods, 0) < 0) {
fprintf(stderr, "Error setting capture periods.\n");
exit(1);
}
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(capture_handle, hw_params_capture, (periodsize * periods)>>2) < 0) {
fprintf(stderr, "Error setting capture buffersize.\n");
exit(1);
}
if ((err = snd_pcm_hw_params(capture_handle, hw_params_capture)) < 0) {
fprintf(stderr, "cannot set capture parameters (%s)\n",
snd_strerror(err));
exit(1);
}
snd_pcm_hw_params_get_buffer_size(hw_params_capture, &capBufferSize);
printf("capture buffer size %d\n", (int)capBufferSize);
printf("capture bits %d\n", snd_pcm_hw_params_get_sbits(hw_params_capture));
if (capBufferSize > BUF_SIZE) {
fprintf(stderr, "shit capture buffer size > BUF_SIZE (%d > %d)\n", (int)capBufferSize, BUF_SIZE);
exit(1);
}
if ((err = snd_pcm_prepare(capture_handle)) < 0) {
fprintf(stderr, "cannot prepare audio capture interface for use (%s)\n",
snd_strerror(err));
exit(1);
}
//////////////// playback ///////////////
if ((err = snd_pcm_open(&playback_handle, argv[2], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "cannot open audio playback device %s (%s)\n",
argv[2],
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_malloc(&hw_params_playback)) < 0) {
fprintf(stderr, "cannot allocate playback hardware parameter structure (%s)\n",
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_any(playback_handle, hw_params_playback)) < 0) {
fprintf(stderr, "cannot initialize playback hardware parameter structure (%s)\n",
snd_strerror(err));
exit(1);
}
#if 0
resample = 1;
err = snd_pcm_hw_params_set_rate_resample(playback_handle, hw_params_playback, resample);
if (err < 0) {
fprintf(stderr, "resampling setup failed for playback device (%s)\n",
snd_strerror(err));
exit(1);
}
#endif
if ((err = snd_pcm_hw_params_set_access(playback_handle, hw_params_playback, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "cannot set playback access type (%s)\n",
snd_strerror(err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format(playback_handle, hw_params_playback, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set playback sample format (%s)\n",
snd_strerror(err));
exit(1);
}
rate = 44100;
if ((err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params_playback, &rate, 0)) < 0) {
fprintf(stderr, "cannot set playback sample rate (%s)\n",
snd_strerror(err));
exit(1);
}
printf("playback rate set to %d\n", rate);
if ((err = snd_pcm_hw_params_set_channels(playback_handle, hw_params_playback, 2)) < 0) {
fprintf(stderr, "cannot set playback channel count (%s)\n",
snd_strerror(err));
exit(1);
}
/* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(playback_handle, hw_params_playback, periods, 0) < 0) {
fprintf(stderr, "Error setting playback periods.\n");
exit(1);
}
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(playback_handle, hw_params_playback, (periodsize * periods)>>2) < 0) {
fprintf(stderr, "Error setting playback buffersize.\n");
exit(1);
}
if ((err = snd_pcm_hw_params(playback_handle, hw_params_playback)) < 0) {
fprintf (stderr, "cannot set playback parameters (%s)\n",
snd_strerror(err));
exit(1);
}
snd_pcm_hw_params_get_buffer_size(hw_params_playback, &playBufferSize);
printf("playback buffer size %d\n", (int)playBufferSize);
printf("playback bits %d\n", snd_pcm_hw_params_get_sbits(hw_params_playback));
if (playBufferSize > BUF_SIZE) {
fprintf(stderr, "shit playback buffer size > BUF_SIZE (%d > %d)\n", (int)playBufferSize, BUF_SIZE);
exit(1);
}
if ((err = snd_pcm_prepare(playback_handle)) < 0) {
fprintf(stderr, "cannot prepare playback audio interface for use (%s)\n",
snd_strerror(err));
exit(1);
}
//////////////////////////////
if (playBufferSize != capBufferSize) {
fprintf(stderr, "playBufferSize != capBufferSize\n");
exit(1);
}
loops = 0;
while (1) {
loops++;
while ((err = snd_pcm_readi(capture_handle, buf, periodsize >> 2)) < 0) {
snd_pcm_prepare(capture_handle);
fprintf(stderr, "%d <<<<<<<<<<<<<<< Buffer Overrun >>>>>>>>>>>>>>>\n", loops);
}
while ((err = snd_pcm_writei(playback_handle, buf, periodsize >> 2)) < 0) {
snd_pcm_prepare(playback_handle);
fprintf(stderr, "%d playback xrun\n", loops);
}
}
printf("all done\n");
if (hw_params_capture)
snd_pcm_hw_params_free(hw_params_capture);
if (hw_params_playback)
snd_pcm_hw_params_free(hw_params_playback);
if (capture_handle)
snd_pcm_close(capture_handle);
if (playback_handle)
snd_pcm_close(playback_handle);
return 0;
}