34 |
kaklik |
1 |
/////////////////////////////////////////////////////////////////////////////////// |
|
|
2 |
// |
|
|
3 |
// |
|
|
4 |
/////////////////////////////////////////////////////////////////////////////////// |
|
|
5 |
|
|
|
6 |
#include <stdio.h> |
|
|
7 |
#include <stdlib.h> |
|
|
8 |
#include <string.h> |
|
|
9 |
#include <sched.h> |
|
|
10 |
#include <errno.h> |
|
|
11 |
#include <getopt.h> |
|
|
12 |
#include <alsa/asoundlib.h> |
|
|
13 |
#include <sys/time.h> |
|
|
14 |
#include <math.h> |
36 |
kaklik |
15 |
#include <time.h> |
34 |
kaklik |
16 |
|
|
|
17 |
static char *device = "plughw:0,0"; /* playback device */ |
|
|
18 |
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ |
|
|
19 |
static unsigned int rate = 48000; /* stream rate */ |
|
|
20 |
static int resample = 1; /* enable alsa-lib resampling */ |
|
|
21 |
static int period_event = 0; /* produce poll event after each period */ |
|
|
22 |
|
37 |
kaklik |
23 |
static snd_pcm_uframes_t buffer_size = 30000; // size of buffer at sound card |
|
|
24 |
static snd_pcm_uframes_t period_size = 8000; //samples per frame |
34 |
kaklik |
25 |
static snd_output_t *output = NULL; |
|
|
26 |
|
37 |
kaklik |
27 |
#define MAX_BINS 8 //define nomber of recorded channels |
34 |
kaklik |
28 |
|
|
|
29 |
int sample_count; |
|
|
30 |
double q0; |
|
|
31 |
double q1[MAX_BINS]; |
|
|
32 |
double q2[MAX_BINS]; |
|
|
33 |
double r[MAX_BINS]; |
|
|
34 |
double coefs[MAX_BINS] ; |
|
|
35 |
|
|
|
36 |
FILE *out; |
|
|
37 |
|
37 |
kaklik |
38 |
double freqs[MAX_BINS] = // define recorded frequencies |
34 |
kaklik |
39 |
{ |
|
|
40 |
697, |
|
|
41 |
770, |
|
|
42 |
852, |
|
|
43 |
941, |
|
|
44 |
1209, |
|
|
45 |
1336, |
|
|
46 |
1477, |
|
|
47 |
1633 |
|
|
48 |
}; |
|
|
49 |
|
|
|
50 |
/*---------------------------------------------------------------------------- |
|
|
51 |
* calc_coeffs |
|
|
52 |
*---------------------------------------------------------------------------- |
|
|
53 |
* This is where we calculate the correct co-efficients. |
|
|
54 |
*/ |
|
|
55 |
|
|
|
56 |
/* |
|
|
57 |
* coef = 2.0 * cos( (2.0 * PI * k) / (float)GOERTZEL_N)) ; |
|
|
58 |
* Where k = (int) (0.5 + ((float)GOERTZEL_N * target_freq) / SAMPLING_RATE)); |
|
|
59 |
* |
|
|
60 |
* More simply: coef = 2.0 * cos( (2.0 * PI * target_freq) / SAMPLING_RATE ); |
|
|
61 |
*/ |
|
|
62 |
|
37 |
kaklik |
63 |
void calc_coeffs() // calculate GOERTZEL coefficients |
34 |
kaklik |
64 |
{ |
|
|
65 |
int n; |
|
|
66 |
|
|
|
67 |
for(n = 0; n < MAX_BINS; n++) |
|
|
68 |
{ |
|
|
69 |
coefs[n] = 2.0 * cos(2.0 * M_PI * freqs[n] / rate); |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels) |
|
|
74 |
{ |
|
|
75 |
unsigned int rrate; |
|
|
76 |
int err, dir; |
|
|
77 |
|
|
|
78 |
/* choose all parameters */ |
|
|
79 |
err = snd_pcm_hw_params_any(handle, params); |
|
|
80 |
if (err < 0) { |
|
|
81 |
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err)); |
|
|
82 |
return err; |
|
|
83 |
} |
|
|
84 |
/* set hardware resampling */ |
|
|
85 |
err = snd_pcm_hw_params_set_rate_resample(handle, params, resample); |
|
|
86 |
if (err < 0) { |
|
|
87 |
printf("Resampling setup failed for playback: %s\n", snd_strerror(err)); |
|
|
88 |
return err; |
|
|
89 |
} |
|
|
90 |
/* set the interleaved read/write format */ |
|
|
91 |
err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); |
|
|
92 |
if (err < 0) { |
|
|
93 |
printf("Access type not available for playback: %s\n", snd_strerror(err)); |
|
|
94 |
return err; |
|
|
95 |
} |
|
|
96 |
/* set the sample format */ |
|
|
97 |
err = snd_pcm_hw_params_set_format(handle, params, format); |
|
|
98 |
if (err < 0) { |
|
|
99 |
printf("Sample format not available for playback: %s\n", snd_strerror(err)); |
|
|
100 |
return err; |
|
|
101 |
} |
|
|
102 |
/* set the count of channels */ |
|
|
103 |
err = snd_pcm_hw_params_set_channels(handle, params, channels); |
|
|
104 |
if (err < 0) { |
|
|
105 |
printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err)); |
|
|
106 |
return err; |
|
|
107 |
} |
|
|
108 |
/* set the stream rate */ |
|
|
109 |
rrate = rate; |
|
|
110 |
err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0); |
|
|
111 |
if (err < 0) { |
|
|
112 |
printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err)); |
|
|
113 |
return err; |
|
|
114 |
} |
|
|
115 |
if (rrate != rate) { |
|
|
116 |
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err); |
|
|
117 |
return -EINVAL; |
|
|
118 |
} |
|
|
119 |
else printf("Rate set to %i Hz\n", rate, err); |
36 |
kaklik |
120 |
|
|
|
121 |
/* set the buffer size */ |
|
|
122 |
err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size); |
34 |
kaklik |
123 |
if (err < 0) { |
36 |
kaklik |
124 |
printf("Unable to set buffer size %i for playback: %s\n", (int) buffer_size, snd_strerror(err)); |
34 |
kaklik |
125 |
return err; |
|
|
126 |
} |
36 |
kaklik |
127 |
else printf("Size of capture buffer set to %i\n", (int) buffer_size); |
|
|
128 |
|
34 |
kaklik |
129 |
/* set the period time */ |
36 |
kaklik |
130 |
err = snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, &dir); |
34 |
kaklik |
131 |
if (err < 0) { |
36 |
kaklik |
132 |
printf("Unable to set period time %i for playback: %s\n", (int) period_size, snd_strerror(err)); |
34 |
kaklik |
133 |
return err; |
|
|
134 |
} |
36 |
kaklik |
135 |
else printf("Size of capture period set to %i\n", (int) period_size); |
|
|
136 |
|
34 |
kaklik |
137 |
/* write the parameters to device */ |
|
|
138 |
err = snd_pcm_hw_params(handle, params); |
|
|
139 |
if (err < 0) { |
|
|
140 |
printf("Unable to set hw params for playback: %s\n", snd_strerror(err)); |
|
|
141 |
return err; |
|
|
142 |
} |
|
|
143 |
return 0; |
|
|
144 |
} |
|
|
145 |
|
|
|
146 |
static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams) |
|
|
147 |
{ |
|
|
148 |
int err; |
|
|
149 |
|
|
|
150 |
/* get the current swparams */ |
|
|
151 |
err = snd_pcm_sw_params_current(handle, swparams); |
|
|
152 |
if (err < 0) { |
|
|
153 |
printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err)); |
|
|
154 |
return err; |
|
|
155 |
} |
|
|
156 |
/* start the transfer when the buffer is almost full: */ |
|
|
157 |
/* (buffer_size / avail_min) * avail_min */ |
|
|
158 |
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size); |
|
|
159 |
if (err < 0) { |
|
|
160 |
printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err)); |
|
|
161 |
return err; |
|
|
162 |
} |
|
|
163 |
/* allow the transfer when at least period_size samples can be processed */ |
|
|
164 |
/* or disable this mechanism when period event is enabled (aka interrupt like style processing) */ |
|
|
165 |
err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size); |
|
|
166 |
if (err < 0) { |
|
|
167 |
printf("Unable to set avail min for playback: %s\n", snd_strerror(err)); |
|
|
168 |
return err; |
|
|
169 |
} |
|
|
170 |
/* enable period events when requested */ |
|
|
171 |
if (period_event) { |
|
|
172 |
err = snd_pcm_sw_params_set_period_event(handle, swparams, 1); |
|
|
173 |
if (err < 0) { |
|
|
174 |
printf("Unable to set period event: %s\n", snd_strerror(err)); |
|
|
175 |
return err; |
|
|
176 |
} |
|
|
177 |
} |
|
|
178 |
/* write the parameters to the playback device */ |
|
|
179 |
err = snd_pcm_sw_params(handle, swparams); |
|
|
180 |
if (err < 0) { |
|
|
181 |
printf("Unable to set sw params for playback: %s\n", snd_strerror(err)); |
|
|
182 |
return err; |
|
|
183 |
} |
|
|
184 |
return 0; |
|
|
185 |
} |
|
|
186 |
|
|
|
187 |
struct async_private_data { |
|
|
188 |
signed short *samples; |
|
|
189 |
snd_pcm_channel_area_t *areas; |
|
|
190 |
unsigned int period; |
|
|
191 |
}; |
|
|
192 |
|
|
|
193 |
/////////// CALL BACK STUFF /////////////////// |
|
|
194 |
static void async_capture_callback(snd_async_handler_t *ahandler) |
|
|
195 |
{ |
|
|
196 |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler); |
37 |
kaklik |
197 |
struct timeval tv; |
|
|
198 |
struct tm *tm; |
|
|
199 |
double fsecs; |
|
|
200 |
time_t ud; |
|
|
201 |
|
34 |
kaklik |
202 |
int err; |
|
|
203 |
short signal[100000]; |
37 |
kaklik |
204 |
char fname[100]; |
34 |
kaklik |
205 |
unsigned int n,i; |
|
|
206 |
|
|
|
207 |
while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer |
37 |
kaklik |
208 |
|
|
|
209 |
gettimeofday( &tv, NULL); //get microsecond precise epoch time |
|
|
210 |
fsecs = tv.tv_sec + 1e-6 * tv.tv_usec; // split seconds and microseconds to seconds |
|
|
211 |
ud = fsecs; //tv.tv_sec - tv.tv_sec % 86400; // convert to time_t format?? |
|
|
212 |
tm = gmtime( &ud); // decode epoch time to UTC time |
34 |
kaklik |
213 |
|
37 |
kaklik |
214 |
sprintf(fname, "%s/%02d%02d%02d.txt", "./", tm->tm_year - 100, tm->tm_mon+1, tm->tm_mday); |
|
|
215 |
|
36 |
kaklik |
216 |
err = snd_pcm_readi(handle, signal, period_size); |
37 |
kaklik |
217 |
|
34 |
kaklik |
218 |
if (err < 0) { |
|
|
219 |
printf("Read error: %s\n", snd_strerror(err)); |
|
|
220 |
exit(EXIT_FAILURE); |
|
|
221 |
} |
|
|
222 |
if (err != period_size) { |
|
|
223 |
printf("Read error: red %i expected %li\n", err, period_size); |
|
|
224 |
exit(EXIT_FAILURE); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
for ( n=0; n<period_size; n+=2 ){ |
|
|
228 |
for ( i=0; i<MAX_BINS; i++ ) |
|
|
229 |
{ |
36 |
kaklik |
230 |
q0 = coefs[i] * q1[i] - q2[i] + (double) signal[n]/32768.0; |
34 |
kaklik |
231 |
q2[i] = q1[i]; |
|
|
232 |
q1[i] = q0; |
|
|
233 |
} |
|
|
234 |
} |
|
|
235 |
|
37 |
kaklik |
236 |
for ( i=0; i<MAX_BINS; i++ ) |
|
|
237 |
{ |
|
|
238 |
r[i] = (q1[i] * q1[i]) + (q2[i] * q2[i]) - (coefs[i] * q1[i] * q2[i]); |
|
|
239 |
q1[i] = 0.0; |
|
|
240 |
q2[i] = 0.0; |
|
|
241 |
} |
|
|
242 |
|
|
|
243 |
out=fopen(fname,"a+"); |
|
|
244 |
fprintf(out,"%02d:%02d:%2.5f ", tm->tm_hour, tm->tm_min, tm->tm_sec + 1e-6 * tv.tv_usec); |
|
|
245 |
for(i=0;i<MAX_BINS;i++) fprintf(out,"%.e5 ",r[i]); |
|
|
246 |
fprintf(out,"\n"); |
|
|
247 |
fclose(out); |
34 |
kaklik |
248 |
} |
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
int main(int argc, char *argv[]) |
|
|
253 |
{ |
36 |
kaklik |
254 |
snd_pcm_t *capture_handle; |
34 |
kaklik |
255 |
int err; |
|
|
256 |
snd_pcm_hw_params_t *hwparams; |
|
|
257 |
snd_pcm_sw_params_t *swparams; |
36 |
kaklik |
258 |
// signed short *frame; // pointer to array of samples |
|
|
259 |
// unsigned int chn; |
34 |
kaklik |
260 |
snd_pcm_channel_area_t *areas; |
|
|
261 |
|
|
|
262 |
struct async_private_data data; |
36 |
kaklik |
263 |
snd_async_handler_t *chandler; |
34 |
kaklik |
264 |
int count; |
|
|
265 |
unsigned int i,j; |
|
|
266 |
|
|
|
267 |
snd_pcm_hw_params_alloca(&hwparams); |
|
|
268 |
snd_pcm_sw_params_alloca(&swparams); |
|
|
269 |
|
|
|
270 |
printf("SID monitor 2.0 starting work.. \n"); |
|
|
271 |
|
|
|
272 |
//open and set capture device |
|
|
273 |
if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) { |
|
|
274 |
printf("Playback open error: %s\n", snd_strerror(err)); |
|
|
275 |
return 0; |
|
|
276 |
} |
|
|
277 |
|
|
|
278 |
if ((err = set_hwparams(capture_handle, hwparams, 2)) < 0) { |
|
|
279 |
printf("Setting of hwparams failed: %s\n", snd_strerror(err)); |
|
|
280 |
exit(EXIT_FAILURE); |
|
|
281 |
} |
|
|
282 |
if ((err = set_swparams(capture_handle, swparams)) < 0) { |
|
|
283 |
printf("Setting of swparams failed: %s\n", snd_strerror(err)); |
|
|
284 |
exit(EXIT_FAILURE); |
|
|
285 |
} |
|
|
286 |
|
|
|
287 |
// register capture callback |
|
|
288 |
err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data |
|
|
289 |
if (err < 0) { |
|
|
290 |
printf("Unable to register async handler\n"); |
|
|
291 |
exit(EXIT_FAILURE); |
|
|
292 |
} |
|
|
293 |
|
|
|
294 |
calc_coeffs(); |
|
|
295 |
|
|
|
296 |
//start capture |
|
|
297 |
if ((err = snd_pcm_prepare (capture_handle)) < 0) { |
|
|
298 |
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", |
|
|
299 |
snd_strerror (err)); |
|
|
300 |
exit (1); |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
err = snd_pcm_start(capture_handle); |
|
|
304 |
if (err < 0) { |
|
|
305 |
printf("Start error: %s\n", snd_strerror(err)); |
|
|
306 |
exit(EXIT_FAILURE); |
|
|
307 |
} |
|
|
308 |
|
|
|
309 |
//wait until all samples aren't transmitted |
|
|
310 |
printf("processing audio input.. \n"); |
|
|
311 |
|
36 |
kaklik |
312 |
while (1) usleep(1000); |
34 |
kaklik |
313 |
|
|
|
314 |
snd_pcm_close(capture_handle); |
|
|
315 |
return 0; |
|
|
316 |
} |
|
|
317 |
|