8magsvn – Blame information for rev 26

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