-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsndio.c
50 lines (40 loc) · 1.34 KB
/
sndio.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
#include "sndio.h"
#include "common.h"
#include <sndio.h>
void *input_sndio(void *data) {
struct audio_data *audio = (struct audio_data *)data;
struct sio_par par;
struct sio_hdl *hdl;
int16_t buf[audio->input_buffer_size];
sio_initpar(&par);
par.sig = 1;
par.bits = 16;
par.le = 1;
par.rate = 44100;
par.rchan = 2;
par.appbufsz = sizeof(buf) / par.rchan;
if ((hdl = sio_open(audio->source, SIO_REC, 0)) == NULL) {
fprintf(stderr, __FILE__ ": Could not open sndio source: %s\n", audio->source);
exit(EXIT_FAILURE);
}
if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par) || par.sig != 1 || par.le != 1 ||
par.rate != 44100 || par.rchan != audio->channels) {
fprintf(stderr, __FILE__ ": Could not set required audio parameters\n");
exit(EXIT_FAILURE);
}
if (!sio_start(hdl)) {
fprintf(stderr, __FILE__ ": sio_start() failed\n");
exit(EXIT_FAILURE);
}
uint16_t frames = (sizeof(buf) / sizeof(buf[0])) / 2;
while (audio->terminate != 1) {
if (sio_read(hdl, buf, sizeof(buf)) == 0) {
fprintf(stderr, __FILE__ ": sio_read() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
write_to_input_buffers(frames * 2, buf, audio);
}
sio_stop(hdl);
sio_close(hdl);
return 0;
}