8magsvn – Blame information for rev 35

Subversion Repositories:
Rev:
Rev Author Line No. Line
25 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>
15 #include <fftw3.h>
16  
35 kaklik 17 static char *device = "plughw:0,0"; /* playback device */
25 kaklik 18 static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */
35 kaklik 19 static unsigned int rate = 98000; /* stream rate */
25 kaklik 20 static int resample = 1; /* enable alsa-lib resampling */
21 static int period_event = 0; /* produce poll event after each period */
22  
35 kaklik 23 static snd_pcm_uframes_t buffer_size = 20004; //samples per frame
24 static snd_pcm_uframes_t period_size = 4000;
25 kaklik 25 static snd_output_t *output = NULL;
26  
27 FILE *out;
28  
28 kaklik 29 double df; //frequency resolution
30 unsigned int frequency_bins; // number of output frequency bins
31  
25 kaklik 32 double *inl, *inr;
33 fftw_complex *outl, *outr;
34 fftw_plan fft_plan_left, fft_plan_right;
35  
26 kaklik 36 double *spect_avg_left, *spect_avg_right;
25 kaklik 37 unsigned int period;
38  
32 kaklik 39 #define PERIODS 100 // number of periods to average
31 kaklik 40  
41  
25 kaklik 42 static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels)
43 {
44 unsigned int rrate;
45 int err, dir;
46  
47 /* choose all parameters */
48 err = snd_pcm_hw_params_any(handle, params);
49 if (err < 0) {
28 kaklik 50 printf("Broken configuration for capture: no configurations available: %s\n", snd_strerror(err));
25 kaklik 51 return err;
52 }
53 /* set hardware resampling */
54 err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);
55 if (err < 0) {
28 kaklik 56 printf("Resampling setup failed for capture: %s\n", snd_strerror(err));
25 kaklik 57 return err;
58 }
59 /* set the interleaved read/write format */
60 err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
61 if (err < 0) {
28 kaklik 62 printf("Access type not available for capture: %s\n", snd_strerror(err));
25 kaklik 63 return err;
64 }
65 /* set the sample format */
66 err = snd_pcm_hw_params_set_format(handle, params, format);
67 if (err < 0) {
32 kaklik 68 printf("Sample format not available for capture: %s\n", snd_strerror(err));
25 kaklik 69 return err;
70 }
71 /* set the count of channels */
72 err = snd_pcm_hw_params_set_channels(handle, params, channels);
73 if (err < 0) {
32 kaklik 74 printf("Channels count (%i) not available for capture: %s\n", channels, snd_strerror(err));
25 kaklik 75 return err;
76 }
77 /* set the stream rate */
78 rrate = rate;
79 err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
80 if (err < 0) {
32 kaklik 81 printf("Rate %iHz not available for capture: %s\n", rate, snd_strerror(err));
25 kaklik 82 return err;
83 }
84 if (rrate != rate) {
85 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
86 return -EINVAL;
87 }
29 kaklik 88 else printf("Sampling rate set to %i Hz\n", rate, err);
89  
90 /* set the buffer size */
35 kaklik 91 err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);
25 kaklik 92 if (err < 0) {
35 kaklik 93 printf("Unable to set maximal buffer size for capture: %s\n", snd_strerror(err));
25 kaklik 94 return err;
95 }
35 kaklik 96 else printf("Size of capture buffer set to %i\n", (int) buffer_size);
29 kaklik 97  
35 kaklik 98 snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, &dir);
25 kaklik 99 if (err < 0) {
35 kaklik 100 printf("Unable to set maximal period size %i for capture: %s\n", (int) period_size, snd_strerror(err));
25 kaklik 101 return err;
102 }
35 kaklik 103 else printf("Size of capture period (FFT width) set to %i\n", (int) period_size);
29 kaklik 104  
25 kaklik 105 /* write the parameters to device */
106 err = snd_pcm_hw_params(handle, params);
107 if (err < 0) {
32 kaklik 108 printf("Unable to set hw params for capture: %s\n", snd_strerror(err));
25 kaklik 109 return err;
110 }
111 return 0;
112 }
113  
114 static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams)
115 {
116 int err;
117  
118 /* get the current swparams */
119 err = snd_pcm_sw_params_current(handle, swparams);
120 if (err < 0) {
121 printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err));
122 return err;
123 }
124 /* start the transfer when the buffer is almost full: */
125 /* (buffer_size / avail_min) * avail_min */
126 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, (buffer_size / period_size) * period_size);
127 if (err < 0) {
128 printf("Unable to set start threshold mode for playback: %s\n", snd_strerror(err));
129 return err;
130 }
131 /* allow the transfer when at least period_size samples can be processed */
132 /* or disable this mechanism when period event is enabled (aka interrupt like style processing) */
133 err = snd_pcm_sw_params_set_avail_min(handle, swparams, period_event ? buffer_size : period_size);
134 if (err < 0) {
135 printf("Unable to set avail min for playback: %s\n", snd_strerror(err));
136 return err;
137 }
138 /* enable period events when requested */
139 if (period_event) {
140 err = snd_pcm_sw_params_set_period_event(handle, swparams, 1);
141 if (err < 0) {
142 printf("Unable to set period event: %s\n", snd_strerror(err));
143 return err;
144 }
145 }
146 /* write the parameters to the playback device */
147 err = snd_pcm_sw_params(handle, swparams);
148 if (err < 0) {
149 printf("Unable to set sw params for playback: %s\n", snd_strerror(err));
150 return err;
151 }
152 return 0;
153 }
154  
155 struct async_private_data {
156 signed short *samples;
157 snd_pcm_channel_area_t *areas;
158 unsigned int period;
159 };
160  
161 /////////// CALL BACK STUFF ///////////////////
162  
163 static void async_capture_callback(snd_async_handler_t *ahandler)
164 {
165 snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
26 kaklik 166 int err;
167 unsigned int i, n;
35 kaklik 168 short signal[100000];
25 kaklik 169  
26 kaklik 170 /*signal = calloc( (unsigned int) period_size, sizeof(short) );
25 kaklik 171 if (signal = NULL) printf("memory allocation failed");*/
172  
173 while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer
174  
175 err = snd_pcm_readi(handle, signal, period_size);
176 if (err < 0) {
177 printf("Read error: %s\n", snd_strerror(err));
178 exit(EXIT_FAILURE);
179 }
180 if (err != period_size) {
181 printf("Read error: red %i expected %li\n", err, period_size);
182 exit(EXIT_FAILURE);
183 }
184  
185 n=0;
186 i=0;
26 kaklik 187 do {
31 kaklik 188 inl[n]= signal[i]/32768.0;
189 inr[n]= signal[i+1]/32768.0;
25 kaklik 190 n++;
191 i+=2;
26 kaklik 192 } while (n < period_size);
25 kaklik 193  
26 kaklik 194 fftw_execute(fft_plan_left);
195 fftw_execute(fft_plan_right);
196  
28 kaklik 197 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
198 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
25 kaklik 199 period++;
200 }
26 kaklik 201  
31 kaklik 202 if (period > PERIODS)
28 kaklik 203 {
31 kaklik 204 for(i=0; i < frequency_bins; i++) spect_avg_left[i] = spect_avg_left[i]/PERIODS; // finally average spectrum data
205 for(i=0; i < frequency_bins; i++) spect_avg_right[i] = spect_avg_right[i]/PERIODS;
28 kaklik 206  
31 kaklik 207 out=fopen("/tmp/sidspec","w");
28 kaklik 208  
31 kaklik 209 for(i=0; i < frequency_bins; i++) //write spectrum to file
28 kaklik 210 {
31 kaklik 211 fprintf(out,"%.5e %.5e %.5e\n",(i+0.5)*df, spect_avg_left[i], spect_avg_right[i]);
28 kaklik 212 spect_avg_left[i]=0;
213 spect_avg_right[i]=0;
214 }
25 kaklik 215 fclose(out);
26 kaklik 216 period=0;
25 kaklik 217 }
26 kaklik 218 // free(signal);
25 kaklik 219 }
220  
221  
222 int main(int argc, char *argv[])
223 {
224 snd_pcm_t *playback_handle, *capture_handle;
225 int err;
226 snd_pcm_hw_params_t *hwparams;
227 snd_pcm_sw_params_t *swparams;
228 signed short *frame; // pointer to array of samples
229 unsigned int chn;
230 snd_pcm_channel_area_t *areas;
231  
232 struct async_private_data data;
233 snd_async_handler_t *chandler;
234  
235 snd_pcm_hw_params_alloca(&hwparams);
236 snd_pcm_sw_params_alloca(&swparams);
237  
238 printf("SID monitor 2.0 starting work.. \n");
239  
240 //open and set capture device
241 if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
242 printf("Playback open error: %s\n", snd_strerror(err));
243 return 0;
244 }
33 kaklik 245 else printf("Using audio device: %s\n", device);
25 kaklik 246  
247 if ((err = set_hwparams(capture_handle, hwparams, 2)) < 0) {
248 printf("Setting of hwparams failed: %s\n", snd_strerror(err));
249 exit(EXIT_FAILURE);
250 }
251 if ((err = set_swparams(capture_handle, swparams)) < 0) {
252 printf("Setting of swparams failed: %s\n", snd_strerror(err));
253 exit(EXIT_FAILURE);
254 }
255  
28 kaklik 256 frequency_bins = period_size/2 + 1;
25 kaklik 257  
28 kaklik 258 spect_avg_left = calloc(frequency_bins, sizeof (double)); //allocate space for frrequency spectrum
259 spect_avg_right = calloc(frequency_bins, sizeof (double));
25 kaklik 260  
28 kaklik 261  
25 kaklik 262 // register capture callback
263 err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data
264 if (err < 0) {
265 printf("Unable to register async handler\n");
266 exit(EXIT_FAILURE);
267 }
268  
28 kaklik 269 // setup fft
270  
31 kaklik 271 df = (double) rate/ (double) period_size;
28 kaklik 272  
26 kaklik 273 inl = fftw_malloc(sizeof(double) * period_size); // period_size);
28 kaklik 274 outl = fftw_malloc(sizeof(fftw_complex) * frequency_bins);
25 kaklik 275  
26 kaklik 276 inr = fftw_malloc(sizeof(double) * period_size); // period_size);
28 kaklik 277 outr = fftw_malloc(sizeof(fftw_complex) * frequency_bins);
26 kaklik 278  
279  
280 fft_plan_left = fftw_plan_dft_r2c_1d(period_size, inl, outl, FFTW_ESTIMATE);
281 fft_plan_right = fftw_plan_dft_r2c_1d(period_size, inr, outr, FFTW_ESTIMATE);
25 kaklik 282 period=0;
26 kaklik 283  
25 kaklik 284 //start capture
285 if ((err = snd_pcm_prepare (capture_handle)) < 0) {
286 fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
287 snd_strerror (err));
288 exit (1);
289 }
290  
291 err = snd_pcm_start(capture_handle);
292 if (err < 0) {
293 printf("Start error: %s\n", snd_strerror(err));
294 exit(EXIT_FAILURE);
295 }
296  
28 kaklik 297 // wait for interrupt
25 kaklik 298 printf("processing audio input.. \n");
299  
300 while(1) usleep(1000);
301  
302 snd_pcm_close(capture_handle);
303 return 0;
304 }
305