8magsvn – Diff between revs 26 and 28

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 26 Rev 28
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 = 98000; /* stream rate */ 19 static unsigned int rate = 24000; /* stream rate */
20 static unsigned int buffer_time = 130000; /* ring buffer length in us */ 20 static unsigned int buffer_time = 200000; /* ring buffer length in us */
21 static unsigned int period_time = 90000; /* period time in us */ 21 static unsigned int period_time = 10000; /* 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
27 static snd_pcm_sframes_t period_size; //samples per frame 27 static snd_pcm_sframes_t period_size; //samples per frame
28 static snd_output_t *output = NULL; 28 static snd_output_t *output = NULL;
29 29
30 FILE *out; 30 FILE *out;
31   31  
-   32 double df; //frequency resolution
-   33 unsigned int frequency_bins; // number of output frequency bins
-   34  
32 double *inl, *inr; 35 double *inl, *inr;
33 fftw_complex *outl, *outr; 36 fftw_complex *outl, *outr;
34 fftw_plan fft_plan_left, fft_plan_right; 37 fftw_plan fft_plan_left, fft_plan_right;
35   38  
36 double *spect_avg_left, *spect_avg_right; 39 double *spect_avg_left, *spect_avg_right;
Line 43... Line 46...
43 int err, dir; 46 int err, dir;
44   47  
45 /* choose all parameters */ 48 /* choose all parameters */
46 err = snd_pcm_hw_params_any(handle, params); 49 err = snd_pcm_hw_params_any(handle, params);
47 if (err < 0) { 50 if (err < 0) {
48 printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err)); 51 printf("Broken configuration for capture: no configurations available: %s\n", snd_strerror(err));
49 return err; 52 return err;
50 } 53 }
51 /* set hardware resampling */ 54 /* set hardware resampling */
52 err = snd_pcm_hw_params_set_rate_resample(handle, params, resample); 55 err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
53 if (err < 0) { 56 if (err < 0) {
54 printf("Resampling setup failed for playback: %s\n", snd_strerror(err)); 57 printf("Resampling setup failed for capture: %s\n", snd_strerror(err));
55 return err; 58 return err;
56 } 59 }
57 /* set the interleaved read/write format */ 60 /* set the interleaved read/write format */
58 err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); 61 err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
59 if (err < 0) { 62 if (err < 0) {
60 printf("Access type not available for playback: %s\n", snd_strerror(err)); 63 printf("Access type not available for capture: %s\n", snd_strerror(err));
61 return err; 64 return err;
62 } 65 }
63 /* set the sample format */ 66 /* set the sample format */
64 err = snd_pcm_hw_params_set_format(handle, params, format); 67 err = snd_pcm_hw_params_set_format(handle, params, format);
65 if (err < 0) { 68 if (err < 0) {
Line 169... Line 172...
169 static void async_capture_callback(snd_async_handler_t *ahandler) 172 static void async_capture_callback(snd_async_handler_t *ahandler)
170 { 173 {
171 snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler); 174 snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
172 int err; 175 int err;
173 unsigned int i, n; 176 unsigned int i, n;
174 short signal[100000]; 177 short signal[300000];
175   178  
176 /*signal = calloc( (unsigned int) period_size, sizeof(short) ); 179 /*signal = calloc( (unsigned int) period_size, sizeof(short) );
177 if (signal = NULL) printf("memory allocation failed");*/ 180 if (signal = NULL) printf("memory allocation failed");*/
178   181  
179 while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer 182 while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer
Line 198... Line 201...
198 } while (n < period_size); 201 } while (n < period_size);
199   202  
200 fftw_execute(fft_plan_left); 203 fftw_execute(fft_plan_left);
201 fftw_execute(fft_plan_right); 204 fftw_execute(fft_plan_right);
202   205  
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 206 for(i=0; i < frequency_bins; 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 207 for(i=0; i < frequency_bins; i++) spect_avg_right[i] += sqrt( (outr[i][0] * outr[i][0]) + (outr[i][1] * outr[i][1]) ); //acumulate average spectrum
205 period++; 208 period++;
206 } 209 }
207   210  
208 if (period > 100){ 211 if (period > 100)
-   212 {
209 for(i=0; i<(period_size/2 +1); i++) spect_avg_left[i] = spect_avg_left[i]/100; 213 for(i=0; i < frequency_bins; 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; 214 for(i=0; i < frequency_bins; i++) spect_avg_right[i] = spect_avg_right[i]/100;
-   215  
211 out=fopen("/tmp/sidspect","w"); 216 out=fopen("/tmp/sidspect","w");
-   217  
-   218 for(i=0; i < frequency_bins; i++)
-   219 {
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]); 220 fprintf(out,"%6f %6f %6f\n",(i+0.5)*df, spect_avg_left[i], spect_avg_right[i]);
-   221 spect_avg_left[i]=0;
-   222 spect_avg_right[i]=0;
-   223 }
213 fclose(out); 224 fclose(out);
214 period=0; 225 period=0;
215 } 226 }
216 // free(signal); 227 // free(signal);
217 } 228 }
Line 248... Line 259...
248 if ((err = set_swparams(capture_handle, swparams)) < 0) { 259 if ((err = set_swparams(capture_handle, swparams)) < 0) {
249 printf("Setting of swparams failed: %s\n", snd_strerror(err)); 260 printf("Setting of swparams failed: %s\n", snd_strerror(err));
250 exit(EXIT_FAILURE); 261 exit(EXIT_FAILURE);
251 } 262 }
252   263  
-   264 frequency_bins = period_size/2 + 1;
-   265  
253 spect_avg_left = calloc((period_size/2 + 1), sizeof (double)); 266 spect_avg_left = calloc(frequency_bins, sizeof (double)); //allocate space for frrequency spectrum
254 spect_avg_right = calloc((period_size/2 + 1), sizeof (double)); 267 spect_avg_right = calloc(frequency_bins, sizeof (double));
255   268  
256   269  
257 // register capture callback 270 // register capture callback
258 err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data 271 err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
259 if (err < 0) { 272 if (err < 0) {
260 printf("Unable to register async handler\n"); 273 printf("Unable to register async handler\n");
261 exit(EXIT_FAILURE); 274 exit(EXIT_FAILURE);
262 } 275 }
263   276  
-   277 // setup fft
-   278  
-   279 df = (double) rate/ (double) frequency_bins * 2.0;
-   280  
264 inl = fftw_malloc(sizeof(double) * period_size); // period_size); 281 inl = fftw_malloc(sizeof(double) * period_size); // period_size);
265 outl = fftw_malloc(sizeof(fftw_complex) * (period_size/2 + 1)); 282 outl = fftw_malloc(sizeof(fftw_complex) * frequency_bins);
266   283  
267 inr = fftw_malloc(sizeof(double) * period_size); // period_size); 284 inr = fftw_malloc(sizeof(double) * period_size); // period_size);
268 outr = fftw_malloc(sizeof(fftw_complex) * (period_size/2 + 1)); 285 outr = fftw_malloc(sizeof(fftw_complex) * frequency_bins);
269   286  
270   287  
271 fft_plan_left = fftw_plan_dft_r2c_1d(period_size, inl, outl, FFTW_ESTIMATE); 288 fft_plan_left = fftw_plan_dft_r2c_1d(period_size, inl, outl, FFTW_ESTIMATE);
272 fft_plan_right = fftw_plan_dft_r2c_1d(period_size, inr, outr, FFTW_ESTIMATE); 289 fft_plan_right = fftw_plan_dft_r2c_1d(period_size, inr, outr, FFTW_ESTIMATE);
273 period=0; 290 period=0;
Line 283... Line 300...
283 if (err < 0) { 300 if (err < 0) {
284 printf("Start error: %s\n", snd_strerror(err)); 301 printf("Start error: %s\n", snd_strerror(err));
285 exit(EXIT_FAILURE); 302 exit(EXIT_FAILURE);
286 } 303 }
287 304
288 //wait until all samples aren't transmitted 305 // wait for interrupt
289 printf("processing audio input.. \n"); 306 printf("processing audio input.. \n");
290   307  
291 while(1) usleep(1000); 308 while(1) usleep(1000);
292   309  
293 snd_pcm_close(capture_handle); 310 snd_pcm_close(capture_handle);