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 */ |
|
|
19 |
static unsigned int rate = 48000; /* stream rate */ |
|
|
20 |
static unsigned int buffer_time = 130000; /* ring buffer length in us */ |
|
|
21 |
static unsigned int period_time = 100000; /* period time in us */ |
|
|
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 |
|
|
|
36 |
double *spect_avg; |
|
|
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); |
|
|
172 |
int err, n; |
|
|
173 |
unsigned int i; |
|
|
174 |
short signal[1000000]; |
|
|
175 |
|
|
|
176 |
/* signal = malloc(sizeof(short) * period_size); |
|
|
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; |
|
|
193 |
while(n < period_size){ |
|
|
194 |
inl[n]= signal[i]; |
|
|
195 |
// inr[n]=(double) signal[i+1]; |
|
|
196 |
n++; |
|
|
197 |
i+=2; |
|
|
198 |
} |
|
|
199 |
/* fftw_execute(fft_plan_left); |
|
|
200 |
// fftw_execute(fft_plan_right); |
|
|
201 |
|
|
|
202 |
for(i=0; i<(period_size/2 +1); i++) spect_avg[i] += sqrt( (outl[i][0] * outl[i][0]) + (outl[i][1] * outl[i][1]) ); |
|
|
203 |
period++; |
|
|
204 |
} |
|
|
205 |
if (period > 1000){ |
|
|
206 |
for(i=0; i<(period_size/2 +1); i++) spect_avg[i] = spect_avg[i]/1000; |
|
|
207 |
out=fopen("./output.txt","w"); |
|
|
208 |
for(i=0; i<(period_size/2 +1); i++) fprintf(out,"%6f\n",spect_avg[i]); |
|
|
209 |
fclose(out); |
|
|
210 |
period=0;*/ |
|
|
211 |
} |
|
|
212 |
free(signal); |
|
|
213 |
} |
|
|
214 |
|
|
|
215 |
|
|
|
216 |
int main(int argc, char *argv[]) |
|
|
217 |
{ |
|
|
218 |
snd_pcm_t *playback_handle, *capture_handle; |
|
|
219 |
int err; |
|
|
220 |
snd_pcm_hw_params_t *hwparams; |
|
|
221 |
snd_pcm_sw_params_t *swparams; |
|
|
222 |
signed short *frame; // pointer to array of samples |
|
|
223 |
unsigned int chn; |
|
|
224 |
snd_pcm_channel_area_t *areas; |
|
|
225 |
|
|
|
226 |
struct async_private_data data; |
|
|
227 |
snd_async_handler_t *chandler; |
|
|
228 |
|
|
|
229 |
int count; |
|
|
230 |
unsigned int i,j; |
|
|
231 |
|
|
|
232 |
snd_pcm_hw_params_alloca(&hwparams); |
|
|
233 |
snd_pcm_sw_params_alloca(&swparams); |
|
|
234 |
|
|
|
235 |
printf("SID monitor 2.0 starting work.. \n"); |
|
|
236 |
|
|
|
237 |
//open and set capture device |
|
|
238 |
if ((err = snd_pcm_open(&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) { |
|
|
239 |
printf("Playback open error: %s\n", snd_strerror(err)); |
|
|
240 |
return 0; |
|
|
241 |
} |
|
|
242 |
|
|
|
243 |
if ((err = set_hwparams(capture_handle, hwparams, 2)) < 0) { |
|
|
244 |
printf("Setting of hwparams failed: %s\n", snd_strerror(err)); |
|
|
245 |
exit(EXIT_FAILURE); |
|
|
246 |
} |
|
|
247 |
if ((err = set_swparams(capture_handle, swparams)) < 0) { |
|
|
248 |
printf("Setting of swparams failed: %s\n", snd_strerror(err)); |
|
|
249 |
exit(EXIT_FAILURE); |
|
|
250 |
} |
|
|
251 |
|
|
|
252 |
spect_avg = calloc(period_size/2 + 1, sizeof (double)); |
|
|
253 |
|
|
|
254 |
|
|
|
255 |
// register capture callback |
|
|
256 |
err = snd_async_add_pcm_handler(&chandler, capture_handle, async_capture_callback, &data); // fill by dummy &data |
|
|
257 |
if (err < 0) { |
|
|
258 |
printf("Unable to register async handler\n"); |
|
|
259 |
exit(EXIT_FAILURE); |
|
|
260 |
} |
|
|
261 |
|
|
|
262 |
inl = fftw_malloc(sizeof(double) * period_size); |
|
|
263 |
outl = fftw_malloc(sizeof(fftw_complex) * (period_size/2 +1) ); |
|
|
264 |
|
|
|
265 |
fft_plan_left = fftw_plan_dft_r2c_1d(period_size, inl, outr, FFTW_ESTIMATE); |
|
|
266 |
// fft_plan_right = fftw_plan_dft_r2c_1d(period_size, in, out, FFTW_ESTIMATE); |
|
|
267 |
period=0; |
|
|
268 |
//start capture |
|
|
269 |
if ((err = snd_pcm_prepare (capture_handle)) < 0) { |
|
|
270 |
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", |
|
|
271 |
snd_strerror (err)); |
|
|
272 |
exit (1); |
|
|
273 |
} |
|
|
274 |
|
|
|
275 |
err = snd_pcm_start(capture_handle); |
|
|
276 |
if (err < 0) { |
|
|
277 |
printf("Start error: %s\n", snd_strerror(err)); |
|
|
278 |
exit(EXIT_FAILURE); |
|
|
279 |
} |
|
|
280 |
|
|
|
281 |
//wait until all samples aren't transmitted |
|
|
282 |
printf("processing audio input.. \n"); |
|
|
283 |
|
|
|
284 |
while(1) usleep(1000); |
|
|
285 |
|
|
|
286 |
snd_pcm_close(capture_handle); |
|
|
287 |
return 0; |
|
|
288 |
} |
|
|
289 |
|