Rev 34 |
|
Rev 36 |
Line 10... |
|
Line 10... |
10 |
#include <errno.h> |
|
10 |
#include <errno.h> |
11 |
#include <getopt.h> |
|
11 |
#include <getopt.h> |
12 |
#include <alsa/asoundlib.h> |
|
12 |
#include <alsa/asoundlib.h> |
13 |
#include <sys/time.h> |
|
13 |
#include <sys/time.h> |
14 |
#include <math.h> |
|
14 |
#include <math.h> |
- |
|
|
15 |
#include <time.h> |
15 |
|
|
16 |
|
16 |
static char *device = "plughw:0,0"; /* playback device */ |
|
17 |
static char *device = "plughw:0,0"; /* playback device */ |
17 |
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ |
|
18 |
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */ |
18 |
static unsigned int rate = 48000; /* stream rate */ |
|
19 |
static unsigned int rate = 48000; /* stream rate */ |
19 |
static unsigned int buffer_time = 130000; /* ring buffer length in us */ |
|
- |
|
20 |
static unsigned int period_time = 100000; /* period time in us */ |
|
- |
|
21 |
static int verbose = 0; /* verbose flag */ |
|
- |
|
22 |
static int resample = 1; /* enable alsa-lib resampling */ |
|
20 |
static int resample = 1; /* enable alsa-lib resampling */ |
23 |
static int period_event = 0; /* produce poll event after each period */ |
|
21 |
static int period_event = 0; /* produce poll event after each period */ |
24 |
|
|
22 |
|
25 |
static snd_pcm_sframes_t buffer_size; // size of buffer at sound card |
|
23 |
static snd_pcm_uframes_t buffer_size = 20000; // size of buffer at sound card |
26 |
static snd_pcm_sframes_t period_size; //samples per frame |
|
24 |
static snd_pcm_uframes_t period_size = 4000; //samples per frame |
27 |
static snd_output_t *output = NULL; |
|
25 |
static snd_output_t *output = NULL; |
28 |
|
|
26 |
|
29 |
#define MAX_BINS 8 |
|
27 |
#define MAX_BINS 8 |
30 |
|
|
28 |
|
31 |
int sample_count; |
|
29 |
int sample_count; |
Line 73... |
|
Line 71... |
73 |
} |
|
71 |
} |
74 |
|
|
72 |
|
75 |
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels) |
|
73 |
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels) |
76 |
{ |
|
74 |
{ |
77 |
unsigned int rrate; |
|
75 |
unsigned int rrate; |
78 |
snd_pcm_uframes_t size; |
|
- |
|
79 |
int err, dir; |
|
76 |
int err, dir; |
80 |
|
|
77 |
|
81 |
/* choose all parameters */ |
|
78 |
/* choose all parameters */ |
82 |
err = snd_pcm_hw_params_any(handle, params); |
|
79 |
err = snd_pcm_hw_params_any(handle, params); |
83 |
if (err < 0) { |
|
80 |
if (err < 0) { |
Line 118... |
|
Line 115... |
118 |
if (rrate != rate) { |
|
115 |
if (rrate != rate) { |
119 |
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err); |
|
116 |
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err); |
120 |
return -EINVAL; |
|
117 |
return -EINVAL; |
121 |
} |
|
118 |
} |
122 |
else printf("Rate set to %i Hz\n", rate, err); |
|
119 |
else printf("Rate set to %i Hz\n", rate, err); |
- |
|
|
120 |
|
123 |
/* set the buffer time */ |
|
121 |
/* set the buffer size */ |
124 |
err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir); |
|
122 |
err = snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size); |
125 |
if (err < 0) { |
|
- |
|
126 |
printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err)); |
|
- |
|
127 |
return err; |
|
- |
|
128 |
} |
|
- |
|
129 |
err = snd_pcm_hw_params_get_buffer_size(params, &size); |
|
- |
|
130 |
if (err < 0) { |
|
123 |
if (err < 0) { |
131 |
printf("Unable to get buffer size for playback: %s\n", snd_strerror(err)); |
|
124 |
printf("Unable to set buffer size %i for playback: %s\n", (int) buffer_size, snd_strerror(err)); |
132 |
return err; |
|
125 |
return err; |
133 |
} |
|
126 |
} |
134 |
buffer_size = size; |
|
127 |
else printf("Size of capture buffer set to %i\n", (int) buffer_size); |
- |
|
|
128 |
|
135 |
/* set the period time */ |
|
129 |
/* set the period time */ |
136 |
err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir); |
|
130 |
err = snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, &dir); |
137 |
if (err < 0) { |
|
131 |
if (err < 0) { |
138 |
printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err)); |
|
132 |
printf("Unable to set period time %i for playback: %s\n", (int) period_size, snd_strerror(err)); |
139 |
return err; |
|
133 |
return err; |
140 |
} |
|
134 |
} |
141 |
err = snd_pcm_hw_params_get_period_size(params, &size, &dir); |
|
- |
|
142 |
if (err < 0) { |
|
- |
|
143 |
printf("Unable to get period size for playback: %s\n", snd_strerror(err)); |
|
135 |
else printf("Size of capture period set to %i\n", (int) period_size); |
144 |
return err; |
|
- |
|
145 |
} |
|
136 |
|
146 |
period_size = size; |
|
- |
|
147 |
/* write the parameters to device */ |
|
137 |
/* write the parameters to device */ |
148 |
err = snd_pcm_hw_params(handle, params); |
|
138 |
err = snd_pcm_hw_params(handle, params); |
149 |
if (err < 0) { |
|
139 |
if (err < 0) { |
150 |
printf("Unable to set hw params for playback: %s\n", snd_strerror(err)); |
|
140 |
printf("Unable to set hw params for playback: %s\n", snd_strerror(err)); |
151 |
return err; |
|
141 |
return err; |
Line 199... |
|
Line 189... |
199 |
snd_pcm_channel_area_t *areas; |
|
189 |
snd_pcm_channel_area_t *areas; |
200 |
unsigned int period; |
|
190 |
unsigned int period; |
201 |
}; |
|
191 |
}; |
202 |
|
|
192 |
|
203 |
/////////// CALL BACK STUFF /////////////////// |
|
193 |
/////////// CALL BACK STUFF /////////////////// |
204 |
|
|
- |
|
205 |
static void async_capture_callback(snd_async_handler_t *ahandler) |
|
194 |
static void async_capture_callback(snd_async_handler_t *ahandler) |
206 |
{ |
|
195 |
{ |
207 |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler); |
|
196 |
snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler); |
208 |
int err; |
|
197 |
int err; |
209 |
short signal[100000]; |
|
198 |
short signal[100000]; |
210 |
unsigned int n,i; |
|
199 |
unsigned int n,i; |
211 |
|
|
200 |
|
212 |
while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer |
|
201 |
while (snd_pcm_avail_update(handle) >= period_size) { // read until data is ready in buffer |
213 |
|
|
202 |
|
214 |
err = snd_pcm_readi(handle, (signal), period_size); |
|
203 |
err = snd_pcm_readi(handle, signal, period_size); |
215 |
if (err < 0) { |
|
204 |
if (err < 0) { |
216 |
printf("Read error: %s\n", snd_strerror(err)); |
|
205 |
printf("Read error: %s\n", snd_strerror(err)); |
217 |
exit(EXIT_FAILURE); |
|
206 |
exit(EXIT_FAILURE); |
218 |
} |
|
207 |
} |
219 |
if (err != period_size) { |
|
208 |
if (err != period_size) { |
Line 222... |
|
Line 211... |
222 |
} |
|
211 |
} |
223 |
|
|
212 |
|
224 |
for ( n=0; n<period_size; n+=2 ){ |
|
213 |
for ( n=0; n<period_size; n+=2 ){ |
225 |
for ( i=0; i<MAX_BINS; i++ ) |
|
214 |
for ( i=0; i<MAX_BINS; i++ ) |
226 |
{ |
|
215 |
{ |
227 |
q0 = coefs[i] * q1[i] - q2[i] + signal[n]; |
|
216 |
q0 = coefs[i] * q1[i] - q2[i] + (double) signal[n]/32768.0; |
228 |
q2[i] = q1[i]; |
|
217 |
q2[i] = q1[i]; |
229 |
q1[i] = q0; |
|
218 |
q1[i] = q0; |
230 |
} |
|
219 |
} |
231 |
} |
|
220 |
} |
232 |
|
|
221 |
|
Line 235... |
|
Line 224... |
235 |
r[i] = (q1[i] * q1[i]) + (q2[i] * q2[i]) - (coefs[i] * q1[i] * q2[i]); |
|
224 |
r[i] = (q1[i] * q1[i]) + (q2[i] * q2[i]) - (coefs[i] * q1[i] * q2[i]); |
236 |
q1[i] = 0.0; |
|
225 |
q1[i] = 0.0; |
237 |
q2[i] = 0.0; |
|
226 |
q2[i] = 0.0; |
238 |
} |
|
227 |
} |
239 |
out=fopen("./output.txt","a"); |
|
228 |
out=fopen("./output.txt","a"); |
240 |
for(i=0;i<MAX_BINS;i++) fprintf(out,"%6f ",r[i]); |
|
229 |
for(i=0;i<MAX_BINS;i++) fprintf(out,"%.e5 ",r[i]); |
241 |
fprintf(out,"\n"); |
|
230 |
fprintf(out,"\n"); |
242 |
fclose(out); |
|
231 |
fclose(out); |
243 |
} |
|
232 |
} |
244 |
} |
|
233 |
} |
245 |
|
|
234 |
|
246 |
|
|
235 |
|
247 |
int main(int argc, char *argv[]) |
|
236 |
int main(int argc, char *argv[]) |
248 |
{ |
|
237 |
{ |
249 |
snd_pcm_t *playback_handle, *capture_handle; |
|
238 |
snd_pcm_t *capture_handle; |
250 |
int err; |
|
239 |
int err; |
251 |
snd_pcm_hw_params_t *hwparams; |
|
240 |
snd_pcm_hw_params_t *hwparams; |
252 |
snd_pcm_sw_params_t *swparams; |
|
241 |
snd_pcm_sw_params_t *swparams; |
253 |
signed short *frame; // pointer to array of samples |
|
242 |
// signed short *frame; // pointer to array of samples |
254 |
unsigned int chn; |
|
243 |
// unsigned int chn; |
255 |
snd_pcm_channel_area_t *areas; |
|
244 |
snd_pcm_channel_area_t *areas; |
256 |
|
|
245 |
|
257 |
struct async_private_data data; |
|
246 |
struct async_private_data data; |
258 |
snd_async_handler_t *chandler, *phandler; |
|
247 |
snd_async_handler_t *chandler; |
259 |
int count; |
|
248 |
int count; |
260 |
unsigned int i,j; |
|
249 |
unsigned int i,j; |
261 |
|
|
250 |
|
262 |
snd_pcm_hw_params_alloca(&hwparams); |
|
251 |
snd_pcm_hw_params_alloca(&hwparams); |
263 |
snd_pcm_sw_params_alloca(&swparams); |
|
252 |
snd_pcm_sw_params_alloca(&swparams); |
Line 301... |
|
Line 290... |
301 |
exit(EXIT_FAILURE); |
|
290 |
exit(EXIT_FAILURE); |
302 |
} |
|
291 |
} |
303 |
|
|
292 |
|
304 |
//wait until all samples aren't transmitted |
|
293 |
//wait until all samples aren't transmitted |
305 |
printf("processing audio input.. \n"); |
|
294 |
printf("processing audio input.. \n"); |
306 |
for (i=0; i<5000; i++) usleep( 1000); |
|
- |
|
307 |
|
|
- |
|
308 |
|
|
295 |
|
- |
|
|
296 |
while (1) usleep(1000); |
309 |
|
|
297 |
|
310 |
snd_pcm_close(capture_handle); |
|
298 |
snd_pcm_close(capture_handle); |
311 |
return 0; |
|
299 |
return 0; |
312 |
} |
|
300 |
} |
313 |
|
|
301 |
|