| Rev 25 |
|
Rev 26 |
| Line 14... |
|
Line 14... |
| 14 |
#include <math.h> |
|
14 |
#include <math.h> |
| 15 |
#include <fftw3.h> |
|
15 |
#include <fftw3.h> |
| 16 |
|
|
16 |
|
| 17 |
static char *device = "plughw:0,0"; /* playback device */ |
|
17 |
static char *device = "plughw:0,0"; /* playback device */ |
| 18 |
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ |
|
18 |
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ |
| 19 |
static unsigned int rate = 48000; /* stream rate */ |
|
19 |
static unsigned int rate = 98000; /* stream rate */ |
| 20 |
static unsigned int buffer_time = 130000; /* ring buffer length in us */ |
|
20 |
static unsigned int buffer_time = 130000; /* ring buffer length in us */ |
| 21 |
static unsigned int period_time = 100000; /* period time in us */ |
|
21 |
static unsigned int period_time = 90000; /* period time in us */ |
| 22 |
static int verbose = 0; /* verbose flag */ |
|
22 |
static int verbose = 0; /* verbose flag */ |
| 23 |
static int resample = 1; /* enable alsa-lib resampling */ |
|
23 |
static int resample = 1; /* enable alsa-lib resampling */ |
| 24 |
static int period_event = 0; /* produce poll event after each period */ |
|
24 |
static int period_event = 0; /* produce poll event after each period */ |
| 25 |
|
|
25 |
|
| 26 |
static snd_pcm_sframes_t buffer_size; // size of buffer at sound card |
|
26 |
static snd_pcm_sframes_t buffer_size; // size of buffer at sound card |
| Line 31... |
|
Line 31... |
| 31 |
|
|
31 |
|
| 32 |
double *inl, *inr; |
|
32 |
double *inl, *inr; |
| 33 |
fftw_complex *outl, *outr; |
|
33 |
fftw_complex *outl, *outr; |
| 34 |
fftw_plan fft_plan_left, fft_plan_right; |
|
34 |
fftw_plan fft_plan_left, fft_plan_right; |
| 35 |
|
|
35 |
|
| 36 |
double *spect_avg; |
|
36 |
double *spect_avg_left, *spect_avg_right; |
| 37 |
unsigned int period; |
|
37 |
unsigned int period; |
| 38 |
|
|
38 |
|
| 39 |
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels) |
|
39 |
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels) |
| 40 |
{ |
|
40 |
{ |
| 41 |
unsigned int rrate; |
|
41 |
unsigned int rrate; |
| Line 167... |
|
Line 167... |
| 167 |
/////////// CALL BACK STUFF /////////////////// |
|
167 |
/////////// CALL BACK STUFF /////////////////// |
| 168 |
|
|
168 |
|
| 169 |
static void async_capture_callback(snd_async_handler_t *ahandler) |
|
169 |
static void async_capture_callback(snd_async_handler_t *ahandler) |
| 170 |
{ |
|
170 |
{ |
| 171 |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler); |
|
171 |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler); |
| 172 |
int err, n; |
|
172 |
int err; |
| 173 |
unsigned int i; |
|
173 |
unsigned int i, n; |
| 174 |
short signal[1000000]; |
|
174 |
short signal[100000]; |
| 175 |
|
|
175 |
|
| 176 |
/* signal = malloc(sizeof(short) * period_size); |
|
176 |
/*signal = calloc( (unsigned int) period_size, sizeof(short) ); |
| 177 |
if (signal = NULL) printf("memory allocation failed");*/ |
|
177 |
if (signal = NULL) printf("memory allocation failed");*/ |
| 178 |
|
|
178 |
|
| 179 |
while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer |
|
179 |
while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer |
| 180 |
|
|
180 |
|
| 181 |
err = snd_pcm_readi(handle, signal, period_size); |
|
181 |
err = snd_pcm_readi(handle, signal, period_size); |
| Line 188... |
|
Line 188... |
| 188 |
exit(EXIT_FAILURE); |
|
188 |
exit(EXIT_FAILURE); |
| 189 |
} |
|
189 |
} |
| 190 |
|
|
190 |
|
| 191 |
n=0; |
|
191 |
n=0; |
| 192 |
i=0; |
|
192 |
i=0; |
| 193 |
while(n < period_size){ |
|
193 |
do { |
| 194 |
inl[n]= signal[i]; |
|
194 |
inl[n]= signal[i]; |
| 195 |
// inr[n]=(double) signal[i+1]; |
|
195 |
inr[n]= signal[i+1]; |
| 196 |
n++; |
|
196 |
n++; |
| 197 |
i+=2; |
|
197 |
i+=2; |
| 198 |
} |
|
- |
|
| 199 |
/* fftw_execute(fft_plan_left); |
|
- |
|
| 200 |
// fftw_execute(fft_plan_right); |
|
198 |
} while (n < period_size); |
| 201 |
|
|
199 |
|
| - |
|
|
200 |
fftw_execute(fft_plan_left); |
| - |
|
|
201 |
fftw_execute(fft_plan_right); |
| - |
|
|
202 |
|
| 202 |
for(i=0; i<(period_size/2 +1); i++) spect_avg[i] += sqrt( (outl[i][0] * outl[i][0]) + (outl[i][1] * outl[i][1]) ); |
|
203 |
for(i=0; i< (period_size/2 +1); i++) spect_avg_left[i] += sqrt( (outl[i][0] * outl[i][0]) + (outl[i][1] * outl[i][1]) ); //acumulate average spectrum |
| - |
|
|
204 |
for(i=0; i< (period_size/2 +1); i++) spect_avg_right[i] += sqrt( (outr[i][0] * outr[i][0]) + (outr[i][1] * outr[i][1]) ); //acumulate average spectrum |
| 203 |
period++; |
|
205 |
period++; |
| 204 |
} |
|
206 |
} |
| - |
|
|
207 |
|
| 205 |
if (period > 1000){ |
|
208 |
if (period > 100){ |
| 206 |
for(i=0; i<(period_size/2 +1); i++) spect_avg[i] = spect_avg[i]/1000; |
|
209 |
for(i=0; i<(period_size/2 +1); i++) spect_avg_left[i] = spect_avg_left[i]/100; |
| - |
|
|
210 |
for(i=0; i<(period_size/2 +1); i++) spect_avg_right[i] = spect_avg_right[i]/100; |
| 207 |
out=fopen("./output.txt","w"); |
|
211 |
out=fopen("/tmp/sidspect","w"); |
| 208 |
for(i=0; i<(period_size/2 +1); i++) fprintf(out,"%6f\n",spect_avg[i]); |
|
212 |
for(i=0; i< (period_size/2 +1); i++) fprintf(out,"%u %6f %6f\n",i, spect_avg_left[i], spect_avg_right[i]); |
| 209 |
fclose(out); |
|
213 |
fclose(out); |
| 210 |
period=0;*/ |
|
214 |
period=0; |
| 211 |
} |
|
215 |
} |
| 212 |
free(signal); |
|
216 |
// free(signal); |
| 213 |
} |
|
217 |
} |
| 214 |
|
|
218 |
|
| 215 |
|
|
219 |
|
| 216 |
int main(int argc, char *argv[]) |
|
220 |
int main(int argc, char *argv[]) |
| 217 |
{ |
|
221 |
{ |
| Line 224... |
|
Line 228... |
| 224 |
snd_pcm_channel_area_t *areas; |
|
228 |
snd_pcm_channel_area_t *areas; |
| 225 |
|
|
229 |
|
| 226 |
struct async_private_data data; |
|
230 |
struct async_private_data data; |
| 227 |
snd_async_handler_t *chandler; |
|
231 |
snd_async_handler_t *chandler; |
| 228 |
|
|
232 |
|
| 229 |
int count; |
|
- |
|
| 230 |
unsigned int i,j; |
|
- |
|
| 231 |
|
|
- |
|
| 232 |
snd_pcm_hw_params_alloca(&hwparams); |
|
233 |
snd_pcm_hw_params_alloca(&hwparams); |
| 233 |
snd_pcm_sw_params_alloca(&swparams); |
|
234 |
snd_pcm_sw_params_alloca(&swparams); |
| 234 |
|
|
235 |
|
| 235 |
printf("SID monitor 2.0 starting work.. \n"); |
|
236 |
printf("SID monitor 2.0 starting work.. \n"); |
| 236 |
|
|
237 |
|
| Line 247... |
|
Line 248... |
| 247 |
if ((err = set_swparams(capture_handle, swparams)) < 0) { |
|
248 |
if ((err = set_swparams(capture_handle, swparams)) < 0) { |
| 248 |
printf("Setting of swparams failed: %s\n", snd_strerror(err)); |
|
249 |
printf("Setting of swparams failed: %s\n", snd_strerror(err)); |
| 249 |
exit(EXIT_FAILURE); |
|
250 |
exit(EXIT_FAILURE); |
| 250 |
} |
|
251 |
} |
| 251 |
|
|
252 |
|
| 252 |
spect_avg = calloc((unsigned int)(period_size/2 + 1), sizeof (double)); |
|
253 |
spect_avg_left = calloc((period_size/2 + 1), sizeof (double)); |
| - |
|
|
254 |
spect_avg_right = calloc((period_size/2 + 1), sizeof (double)); |
| 253 |
|
|
255 |
|
| 254 |
|
|
256 |
|
| 255 |
// register capture callback |
|
257 |
// register capture callback |
| 256 |
err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data |
|
258 |
err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data |
| 257 |
if (err < 0) { |
|
259 |
if (err < 0) { |
| 258 |
printf("Unable to register async handler\n"); |
|
260 |
printf("Unable to register async handler\n"); |
| 259 |
exit(EXIT_FAILURE); |
|
261 |
exit(EXIT_FAILURE); |
| 260 |
} |
|
262 |
} |
| 261 |
|
|
263 |
|
| 262 |
inl = fftw_malloc(sizeof(double) * 100000); // period_size); |
|
264 |
inl = fftw_malloc(sizeof(double) * period_size); // period_size); |
| 263 |
outl = fftw_malloc(sizeof(fftw_complex) * (period_size/2 +1) ); |
|
265 |
outl = fftw_malloc(sizeof(fftw_complex) * (period_size/2 + 1)); |
| - |
|
|
266 |
|
| - |
|
|
267 |
inr = fftw_malloc(sizeof(double) * period_size); // period_size); |
| - |
|
|
268 |
outr = fftw_malloc(sizeof(fftw_complex) * (period_size/2 + 1)); |
| 264 |
|
|
269 |
|
| - |
|
|
270 |
|
| 265 |
fft_plan_left = fftw_plan_dft_r2c_1d(period_size, inl, outr, FFTW_ESTIMATE); |
|
271 |
fft_plan_left = fftw_plan_dft_r2c_1d(period_size, inl, outl, FFTW_ESTIMATE); |
| 266 |
// fft_plan_right = fftw_plan_dft_r2c_1d(period_size, in, out, FFTW_ESTIMATE); |
|
272 |
fft_plan_right = fftw_plan_dft_r2c_1d(period_size, inr, outr, FFTW_ESTIMATE); |
| 267 |
period=0; |
|
273 |
period=0; |
| - |
|
|
274 |
|
| 268 |
//start capture |
|
275 |
//start capture |
| 269 |
if ((err = snd_pcm_prepare (capture_handle)) < 0) { |
|
276 |
if ((err = snd_pcm_prepare (capture_handle)) < 0) { |
| 270 |
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", |
|
277 |
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", |
| 271 |
snd_strerror (err)); |
|
278 |
snd_strerror (err)); |
| 272 |
exit (1); |
|
279 |
exit (1); |