-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsa-record-example.cc
169 lines (128 loc) · 5.01 KB
/
alsa-record-example.cc
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
/*
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
[X] /usr/bin/aarch64-linux-gnu-gcc -o alsa-record-example -std=c11 -I/usr/include/ -L/usr/lib/aarch64-linux-gnu/libasound.so.2.0.0 alsa-record-example.c
g++ alsa-record-example.cc -I/usr/include/ -o alsa-record-example -lm -ldl -lasound
gcc alsa-record-example.c -I/usr/include/ -o alsa-record-example -lm -ldl -lasound
./alsa-record-example hw:2,0
- More information: https://vovkos.github.io/doxyrest/samples/alsa/page_pcm.html#doxid-pcm
*/
#include <signal.h>
#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <alsa/asoundlib.h>
static bool running = true;
static snd_pcm_t* capture_handle = NULL;
/* Signals handling */
static void handle_sigterm(int signo) { running = false; }
void init_signal() {
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = handle_sigterm;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
}
void snd_param_init(snd_pcm_t **capture_handle,
const char* name,
int buffer_frames,
unsigned int rate,
snd_pcm_hw_params_t *hw_params,
snd_pcm_format_t format) {
int err;
if ((err = snd_pcm_open (capture_handle, name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
name,
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_RW_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_set_channels (*capture_handle, hw_params, 1)) < 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");
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);
}
}
int main (int argc, char *argv[])
{
int i;
int err;
char *buffer;
int buffer_frames = 22050;
unsigned int rate = 44100;
snd_pcm_hw_params_t *hw_params;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
init_signal();
snd_param_init(&capture_handle, argv[1], buffer_frames, rate, hw_params, format);
fprintf(stdout, "audio interface prepared\n");
buffer = (char*) malloc(128 * snd_pcm_format_width(format) / 8 * 2);
fprintf(stdout, "buffer allocated\n");
while(running){
auto start = std::chrono::high_resolution_clock::now();
if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
fprintf (stderr, "read from audio interface failed (%s)\n",
err, snd_strerror (err));
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
fprintf(stdout, "read %d done %ld ms \n", buffer[0], duration);
}
free(buffer);
fprintf(stdout, "buffer freed\n");
snd_pcm_close (capture_handle);
fprintf(stdout, "audio interface closed\n");
return 0;
}