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