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