-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrophone.c
250 lines (209 loc) · 6.06 KB
/
microphone.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
/*
* microphone.c
*
* Copyright (C) 2019 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* read data from microphone
*
* todo:
* - work on frequency allowed by the soundcard
* - set digital mixer control to 0dB
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <alsa/asoundlib.h>
#include "filters.h"
/*
* set mixer to maximal capture level
*/
int maxmixer(char *name) {
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
long int min, max;
enum _snd_mixer_selem_channel_id channel;
int res;
snd_mixer_open(&handle, 0);
res = snd_mixer_attach(handle, name);
if (res < 0) {
fprintf(stderr, "snd_mixer_attach: %s\n", strerror(-res));
return -1;
}
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, "Capture");
elem = snd_mixer_find_selem(handle, sid);
if (elem == NULL) {
snd_mixer_selem_id_set_name(sid, "Mic");
elem = snd_mixer_find_selem(handle, sid);
if (elem == NULL) {
printf("cannot find mixer element\n");
return -1;
}
}
snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
snd_mixer_selem_set_capture_volume_all(elem, max);
for (channel = 0; channel <= SND_MIXER_SCHN_LAST; channel++)
snd_mixer_selem_set_capture_switch(elem, channel, 1);
snd_mixer_detach(handle, name);
snd_mixer_close(handle);
return 0;
}
/*
* open and configure sound output
*/
snd_pcm_t *audio(char *name, int frequency, int *channels) {
int res;
snd_pcm_t *handle;
snd_pcm_info_t *info;
snd_pcm_hw_params_t *params;
unsigned int num, c;
snd_pcm_access_t a;
int den, dir;
snd_pcm_uframes_t frames = 32;
res = snd_pcm_open(&handle, name, SND_PCM_STREAM_CAPTURE, 0);
if (res < 0) {
fprintf(stderr, "snd_pcm_open: %s\n", strerror(-res));
return NULL;
}
snd_pcm_info_malloc(&info);
res = snd_pcm_info(handle, info);
if (res < 0) {
fprintf(stderr, "snd_pcm_info: %s\n", strerror(-res));
return NULL;
}
fprintf(stderr, "name: %s\n", snd_pcm_info_get_name(info));
snd_pcm_info_free(info);
snd_pcm_hw_params_malloc(¶ms);
snd_pcm_hw_params_any(handle, params);
res = snd_pcm_hw_params_set_rate(handle, params, frequency, 0);
if (res < 0)
fprintf(stderr, "set sample rate: %s\n", strerror(-res));
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16);
res = snd_pcm_hw_params_set_period_size_near(handle, params,
&frames, &dir);
if (res < 0)
fprintf(stderr, "set period size: %s\n", strerror(-res));
res = snd_pcm_hw_params_set_channels(handle, params, 1);
if (res < 0)
fprintf(stderr, "set channels: %s\n", strerror(-res));
res = snd_pcm_hw_params(handle, params);
if (res < 0) {
fprintf(stderr, "set hw parameters: %s\n", strerror(-res));
return NULL;
}
snd_pcm_hw_params_current(handle, params);
snd_pcm_hw_params_get_rate(params, &num, &den);
fprintf(stderr, "sample rate: %d/%d\n", num, den);
if (num != (unsigned) frequency) {
fprintf(stderr, "ERROR: actual sample rate %d, ", num);
fprintf(stderr, "requested %d\n", frequency);
}
snd_pcm_hw_params_get_channels(params, &c);
fprintf(stderr, "channels: %d\n", c);
if (c != 1)
fprintf(stderr, "WARNING: using channel 1 of %d\n", c);
*channels = c;
snd_pcm_hw_params_get_access(params, &a);
if (a != SND_PCM_ACCESS_RW_INTERLEAVED)
fprintf(stderr,
"ERROR: interleaved access not allowed (%d)\n", a);
snd_pcm_hw_params_free(params);
res = snd_pcm_prepare(handle);
if (res < 0) {
fprintf(stderr, "prepare: %s\n", strerror(-res));
return NULL;
}
return handle;
}
/*
* microphone filter
*/
#define NFRAMES (32*256)
struct audiobuffer {
snd_pcm_t *handle;
int channels;
int16_t buffer[NFRAMES * sizeof(int16_t)];
int pos;
};
snd_pcm_t *microphone_handle(void *internal) {
struct audiobuffer *buffer;
buffer = (struct audiobuffer *) internal;
return buffer->handle;
}
void *microphone_init(char *device, struct status *status) {
struct audiobuffer *buffer;
int frequency;
status->ended = 1;
buffer = malloc(sizeof(struct audiobuffer));
/* set mixer */
if (maxmixer(device))
fprintf(stderr, "WARNING: cannot maximize capture volume\n");
/* set pcm */
frequency = 44100;
buffer->handle = audio(device, frequency, &buffer->channels);
if (buffer->handle == NULL)
exit(EXIT_FAILURE);
buffer->pos = NFRAMES;
status->ended = 0;
return buffer;
}
int microphone_value(int value, void *internal, struct status *status) {
struct audiobuffer *buffer;
int res;
int16_t v;
int channel = 0;
(void) value;
(void) status;
buffer = (struct audiobuffer *) internal;
if (buffer->pos < NFRAMES * buffer->channels) {
v = buffer->buffer[buffer->pos + channel];
buffer->pos += buffer->channels;
return v;
}
res = snd_pcm_readi(buffer->handle, buffer->buffer, NFRAMES);
if (res == -EPIPE) {
snd_pcm_recover(buffer->handle, res, 0);
return microphone_value(value, internal, status);
}
else if (res < 0) {
fprintf(stderr, "readi: %s\n", strerror(-res));
return -1;
}
buffer->pos = 0;
v = buffer->buffer[buffer->pos + channel];
buffer->pos += buffer->channels;
return v;
}
int microphone_end(void *internal, struct status *status) {
struct audiobuffer *buffer;
int res;
(void) status;
buffer = (struct audiobuffer *) internal;
res = snd_pcm_close(buffer->handle);
if (res < 0) {
printf("close: %s\n", strerror(-res));
exit(EXIT_FAILURE);
}
free(buffer);
return 0;
}