forked from oligau/xwax-1.5-osc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsa.c
442 lines (333 loc) · 10.3 KB
/
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
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
/*
* Copyright (C) 2014 Mark Hills <mark@xwax.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/poll.h>
#include <alsa/asoundlib.h>
#include "alsa.h"
/* This structure doesn't have corresponding functions to be an
* abstraction of the ALSA calls; it is merely a container for these
* variables. */
struct alsa_pcm {
snd_pcm_t *pcm;
struct pollfd *pe;
size_t pe_count; /* number of pollfd entries */
signed short *buf;
snd_pcm_uframes_t period;
int rate;
};
struct alsa {
struct alsa_pcm capture, playback;
};
static void alsa_error(const char *msg, int r)
{
fprintf(stderr, "ALSA %s: %s\n", msg, snd_strerror(r));
}
static bool chk(const char *s, int r)
{
if (r < 0) {
alsa_error(s, r);
return false;
} else {
return true;
}
}
static int pcm_open(struct alsa_pcm *alsa, const char *device_name,
snd_pcm_stream_t stream, int rate, int buffer_time)
{
int r, dir;
unsigned int p;
size_t bytes;
snd_pcm_hw_params_t *hw_params;
r = snd_pcm_open(&alsa->pcm, device_name, stream, SND_PCM_NONBLOCK);
if (!chk("open", r))
return -1;
snd_pcm_hw_params_alloca(&hw_params);
r = snd_pcm_hw_params_any(alsa->pcm, hw_params);
if (!chk("hw_params_any", r))
return -1;
r = snd_pcm_hw_params_set_access(alsa->pcm, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (!chk("hw_params_set_access", r))
return -1;
r = snd_pcm_hw_params_set_format(alsa->pcm, hw_params, SND_PCM_FORMAT_S16);
if (!chk("hw_params_set_format", r)) {
fprintf(stderr, "16-bit signed format is not available. "
"You may need to use a 'plughw' device.\n");
return -1;
}
r = snd_pcm_hw_params_set_rate(alsa->pcm, hw_params, rate, 0);
if (!chk("hw_params_set_rate", r )) {
fprintf(stderr, "%dHz sample rate not available. You may need to use "
"a 'plughw' device.\n", rate);
return -1;
}
alsa->rate = rate;
r = snd_pcm_hw_params_set_channels(alsa->pcm, hw_params, DEVICE_CHANNELS);
if (!chk("hw_params_set_channels", r)) {
fprintf(stderr, "%d channel audio not available on this device.\n",
DEVICE_CHANNELS);
return -1;
}
p = buffer_time * 1000; /* microseconds */
dir = -1;
r = snd_pcm_hw_params_set_buffer_time_max(alsa->pcm, hw_params, &p, &dir);
if (!chk("hw_params_set_buffer_time_max", r)) {
fprintf(stderr, "Buffer of %dms may be too small for this hardware.\n",
buffer_time);
return -1;
}
p = 2; /* double buffering */
dir = 1;
r = snd_pcm_hw_params_set_periods_min(alsa->pcm, hw_params, &p, &dir);
if (!chk("hw_params_set_periods_min", r)) {
fprintf(stderr, "Buffer of %dms may be too small for this hardware.\n",
buffer_time);
return -1;
}
r = snd_pcm_hw_params(alsa->pcm, hw_params);
if (!chk("hw_params", r))
return -1;
r = snd_pcm_hw_params_get_period_size(hw_params, &alsa->period, &dir);
if (!chk("get_period_size", r))
return -1;
bytes = alsa->period * DEVICE_CHANNELS * sizeof(signed short);
alsa->buf = malloc(bytes);
if (!alsa->buf) {
perror("malloc");
return -1;
}
/* snd_pcm_readi() returns uninitialised memory on first call,
* possibly caused by premature POLLIN. Keep valgrind happy. */
memset(alsa->buf, 0, bytes);
return 0;
}
static void pcm_close(struct alsa_pcm *alsa)
{
if (snd_pcm_close(alsa->pcm) < 0)
abort();
free(alsa->buf);
}
static ssize_t pcm_pollfds(struct alsa_pcm *alsa, struct pollfd *pe,
size_t z)
{
int r, count;
count = snd_pcm_poll_descriptors_count(alsa->pcm);
if (count > z)
return -1;
if (count == 0)
alsa->pe = NULL;
else {
r = snd_pcm_poll_descriptors(alsa->pcm, pe, count);
if (r < 0) {
alsa_error("poll_descriptors", r);
return -1;
}
alsa->pe = pe;
}
alsa->pe_count = count;
return count;
}
static int pcm_revents(struct alsa_pcm *alsa, unsigned short *revents) {
int r;
r = snd_pcm_poll_descriptors_revents(alsa->pcm, alsa->pe, alsa->pe_count,
revents);
if (r < 0) {
alsa_error("poll_descriptors_revents", r);
return -1;
}
return 0;
}
/* Start the audio device capture and playback */
static void start(struct device *dv)
{
struct alsa *alsa = (struct alsa*)dv->local;
if (snd_pcm_start(alsa->capture.pcm) < 0)
abort();
}
/* Register this device's interest in a set of pollfd file
* descriptors */
static ssize_t pollfds(struct device *dv, struct pollfd *pe, size_t z)
{
int total, r;
struct alsa *alsa = (struct alsa*)dv->local;
total = 0;
r = pcm_pollfds(&alsa->capture, pe, z);
if (r < 0)
return -1;
pe += r;
z -= r;
total += r;
r = pcm_pollfds(&alsa->playback, pe, z);
if (r < 0)
return -1;
total += r;
return total;
}
/* Collect audio from the player and push it into the device's buffer,
* for playback */
static int playback(struct device *dv)
{
int r;
struct alsa *alsa = (struct alsa*)dv->local;
device_collect(dv, alsa->playback.buf, alsa->playback.period);
r = snd_pcm_writei(alsa->playback.pcm, alsa->playback.buf,
alsa->playback.period);
if (r < 0)
return r;
if (r < alsa->playback.period) {
fprintf(stderr, "alsa: playback underrun %d/%ld.\n", r,
alsa->playback.period);
}
return 0;
}
/* Pull audio from the device's buffer for capture, and pass it
* through to the timecoder */
static int capture(struct device *dv)
{
int r;
struct alsa *alsa = (struct alsa*)dv->local;
r = snd_pcm_readi(alsa->capture.pcm, alsa->capture.buf,
alsa->capture.period);
if (r < 0)
return r;
if (r < alsa->capture.period) {
fprintf(stderr, "alsa: capture underrun %d/%ld.\n",
r, alsa->capture.period);
}
device_submit(dv, alsa->capture.buf, r);
return 0;
}
/* After poll() has returned, instruct a device to do all it can at
* the present time. Return zero if success, otherwise -1 */
static int handle(struct device *dv)
{
int r;
unsigned short revents;
struct alsa *alsa = (struct alsa*)dv->local;
/* Check input buffer for timecode capture */
r = pcm_revents(&alsa->capture, &revents);
if (r < 0)
return -1;
if (revents & POLLIN) {
r = capture(dv);
if (r < 0) {
if (r == -EPIPE) {
fputs("ALSA: capture xrun.\n", stderr);
r = snd_pcm_prepare(alsa->capture.pcm);
if (r < 0) {
alsa_error("prepare", r);
return -1;
}
r = snd_pcm_start(alsa->capture.pcm);
if (r < 0) {
alsa_error("start", r);
return -1;
}
} else {
alsa_error("capture", r);
return -1;
}
}
}
/* Check the output buffer for playback */
r = pcm_revents(&alsa->playback, &revents);
if (r < 0)
return -1;
if (revents & POLLOUT) {
r = playback(dv);
if (r < 0) {
if (r == -EPIPE) {
fputs("ALSA: playback xrun.\n", stderr);
r = snd_pcm_prepare(alsa->playback.pcm);
if (r < 0) {
alsa_error("prepare", r);
return -1;
}
/* The device starts when data is written. POLLOUT
* events are generated in prepared state. */
} else {
alsa_error("playback", r);
return -1;
}
}
}
return 0;
}
static unsigned int sample_rate(struct device *dv)
{
struct alsa *alsa = (struct alsa*)dv->local;
return alsa->capture.rate;
}
/* Close ALSA device and clear any allocations */
static void clear(struct device *dv)
{
struct alsa *alsa = (struct alsa*)dv->local;
pcm_close(&alsa->capture);
pcm_close(&alsa->playback);
free(dv->local);
}
static struct device_ops alsa_ops = {
.pollfds = pollfds,
.handle = handle,
.sample_rate = sample_rate,
.start = start,
.clear = clear
};
/* Open ALSA device. Do not operate on audio until device_start() */
int alsa_init(struct device *dv, const char *device_name,
int rate, int buffer_time)
{
struct alsa *alsa;
alsa = malloc(sizeof *alsa);
if (alsa == NULL) {
perror("malloc");
return -1;
}
if (pcm_open(&alsa->capture, device_name, SND_PCM_STREAM_CAPTURE,
rate, buffer_time) < 0)
{
fputs("Failed to open device for capture.\n", stderr);
goto fail;
}
if (pcm_open(&alsa->playback, device_name, SND_PCM_STREAM_PLAYBACK,
rate, buffer_time) < 0)
{
fputs("Failed to open device for playback.\n", stderr);
goto fail_capture;
}
device_init(dv, &alsa_ops);
dv->local = alsa;
return 0;
fail_capture:
pcm_close(&alsa->capture);
fail:
free(alsa);
return -1;
}
/* ALSA caches information when devices are open. Provide a call
* to clear these caches so that valgrind output is clean. */
void alsa_clear_config_cache(void)
{
int r;
r = snd_config_update_free_global();
if (r < 0)
alsa_error("config_update_free_global", r);
}