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 |
|
36 |
kaklik |
23 |
static snd_pcm_uframes_t buffer_size = 20000; // size of buffer at sound card |
|
|
24 |
static snd_pcm_uframes_t period_size = 4000; //samples per frame |
34 |
kaklik |
25 |
static snd_output_t *output = NULL; |
|
|
26 |
|
|
|
27 |
#define MAX_BINS 8 |
|
|
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 |
|
|
|
38 |
double freqs[MAX_BINS] = |
|
|
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 |
|
|
|
63 |
void calc_coeffs() |
|
|
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); |
|
|
197 |
int err; |
|
|
198 |
short signal[100000]; |
|
|
199 |
unsigned int n,i; |
|
|
200 |
|
|
|
201 |
while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer |
|
|
202 |
|
36 |
kaklik |
203 |
err = snd_pcm_readi(handle, signal, period_size); |
34 |
kaklik |
204 |
if (err < 0) { |
|
|
205 |
printf("Read error: %s\n", snd_strerror(err)); |
|
|
206 |
exit(EXIT_FAILURE); |
|
|
207 |
} |
|
|
208 |
if (err != period_size) { |
|
|
209 |
printf("Read error: red %i expected %li\n", err, period_size); |
|
|
210 |
exit(EXIT_FAILURE); |
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
for ( n=0; n<period_size; n+=2 ){ |
|
|
214 |
for ( i=0; i<MAX_BINS; i++ ) |
|
|
215 |
{ |
36 |
kaklik |
216 |
q0 = coefs[i] * q1[i] - q2[i] + (double) signal[n]/32768.0; |
34 |
kaklik |
217 |
q2[i] = q1[i]; |
|
|
218 |
q1[i] = q0; |
|
|
219 |
} |
|
|
220 |
} |
|
|
221 |
|
|
|
222 |
for ( i=0; i<MAX_BINS; i++ ) |
|
|
223 |
{ |
|
|
224 |
r[i] = (q1[i] * q1[i]) + (q2[i] * q2[i]) - (coefs[i] * q1[i] * q2[i]); |
|
|
225 |
q1[i] = 0.0; |
|
|
226 |
q2[i] = 0.0; |
|
|
227 |
} |
|
|
228 |
out=fopen("./output.txt","a"); |
36 |
kaklik |
229 |
for(i=0;i<MAX_BINS;i++) fprintf(out,"%.e5 ",r[i]); |
34 |
kaklik |
230 |
fprintf(out,"\n"); |
|
|
231 |
fclose(out); |
|
|
232 |
} |
|
|
233 |
} |
|
|
234 |
|
|
|
235 |
|
|
|
236 |
int main(int argc, char *argv[]) |
|
|
237 |
{ |
36 |
kaklik |
238 |
snd_pcm_t *capture_handle; |
34 |
kaklik |
239 |
int err; |
|
|
240 |
snd_pcm_hw_params_t *hwparams; |
|
|
241 |
snd_pcm_sw_params_t *swparams; |
36 |
kaklik |
242 |
// signed short *frame; // pointer to array of samples |
|
|
243 |
// unsigned int chn; |
34 |
kaklik |
244 |
snd_pcm_channel_area_t *areas; |
|
|
245 |
|
|
|
246 |
struct async_private_data data; |
36 |
kaklik |
247 |
snd_async_handler_t *chandler; |
34 |
kaklik |
248 |
int count; |
|
|
249 |
unsigned int i,j; |
|
|
250 |
|
|
|
251 |
snd_pcm_hw_params_alloca(&hwparams); |
|
|
252 |
snd_pcm_sw_params_alloca(&swparams); |
|
|
253 |
|
|
|
254 |
printf("SID monitor 2.0 starting work.. \n"); |
|
|
255 |
|
|
|
256 |
//open and set capture device |
|
|
257 |
if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) { |
|
|
258 |
printf("Playback open error: %s\n", snd_strerror(err)); |
|
|
259 |
return 0; |
|
|
260 |
} |
|
|
261 |
|
|
|
262 |
if ((err = set_hwparams(capture_handle, hwparams, 2)) < 0) { |
|
|
263 |
printf("Setting of hwparams failed: %s\n", snd_strerror(err)); |
|
|
264 |
exit(EXIT_FAILURE); |
|
|
265 |
} |
|
|
266 |
if ((err = set_swparams(capture_handle, swparams)) < 0) { |
|
|
267 |
printf("Setting of swparams failed: %s\n", snd_strerror(err)); |
|
|
268 |
exit(EXIT_FAILURE); |
|
|
269 |
} |
|
|
270 |
|
|
|
271 |
// register capture callback |
|
|
272 |
err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data |
|
|
273 |
if (err < 0) { |
|
|
274 |
printf("Unable to register async handler\n"); |
|
|
275 |
exit(EXIT_FAILURE); |
|
|
276 |
} |
|
|
277 |
|
|
|
278 |
calc_coeffs(); |
|
|
279 |
|
|
|
280 |
//start capture |
|
|
281 |
if ((err = snd_pcm_prepare (capture_handle)) < 0) { |
|
|
282 |
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", |
|
|
283 |
snd_strerror (err)); |
|
|
284 |
exit (1); |
|
|
285 |
} |
|
|
286 |
|
|
|
287 |
err = snd_pcm_start(capture_handle); |
|
|
288 |
if (err < 0) { |
|
|
289 |
printf("Start error: %s\n", snd_strerror(err)); |
|
|
290 |
exit(EXIT_FAILURE); |
|
|
291 |
} |
|
|
292 |
|
|
|
293 |
//wait until all samples aren't transmitted |
|
|
294 |
printf("processing audio input.. \n"); |
|
|
295 |
|
36 |
kaklik |
296 |
while (1) usleep(1000); |
34 |
kaklik |
297 |
|
|
|
298 |
snd_pcm_close(capture_handle); |
|
|
299 |
return 0; |
|
|
300 |
} |
|
|
301 |
|