-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphFFT.c~
186 lines (148 loc) · 4.92 KB
/
graphFFT.c~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <math.h>
#include <fftw3.h>
// macros for the real and imaginary parts
#define REAL 0
#define IMAG 1
#define N 16384
#define buffers 16
#define buffer_frames 1024
/* Computes the 1-D fast Fourier transform. */
void fft(fftw_complex *in, fftw_complex *out)
{
// create a DFT plan
fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
// execute the plan
fftw_execute(plan);
// do some cleaning
fftw_destroy_plan(plan);
fftw_cleanup();
}
double getMagnitude(fftw_complex y)
{
return(sqrt(y[IMAG]*y[IMAG]+y[REAL]*y[REAL]));
}
int doFFT(int startbuffer, char *buffer[])
{
fftw_complex xL[N];
fftw_complex xR[N];
fftw_complex yL[N];
fftw_complex yR[N];
short * tempL;
short * tempR;
int bufnum;
int cursor;
for (int i = 0; i < N; ++i) {
bufnum=(startbuffer+(i/buffer_frames))%buffers;
cursor= i%buffer_frames ;
//printf( "f(%d,%d,0,0)\n", bufnum,cursor );
tempL = (short *)(&buffer[bufnum][cursor*2]);
tempR = (short *)(&buffer[bufnum][cursor*2+2]);
//printf( "f(%d,%d,%d,%d)\n", bufnum,cursor,tempL[0],tempR[0] );
xL[i][REAL] = (double)tempL[0]/32768.0;
xR[i][REAL] = (double)tempR[0]/32768.0;
xL[i][IMAG] = 0;
xR[i][IMAG] = 0;
}
// compute the FFT of x and store the results in y
fft(xL, yL);
fft(xR, yR);
printf( "L(%f) R(%f)\n", getMagnitude(yL[0]),getMagnitude(yR[0]));
return 0;
}
main (int argc, char *argv[])
{
int i,buff_i,j;
short * sampleLP;
short * sampleRP;
int err;
char *buffer[buffers];
char *arg="hw:1";
unsigned int rate = 48000;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_sframes_t avail_cap;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if ((err = snd_pcm_open (&capture_handle, arg, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
arg,
snd_strerror (err));
exit (1);
}
fprintf(stderr, "audio interface opened\n");
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params allocated\n");
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params initialized\n");
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params access setted\n");
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params format setted\n");
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params rate setted\n");
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params,2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params channels setted\n");
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "hw_params setted\n");
snd_pcm_hw_params_free (hw_params);
fprintf(stderr, "hw_params freed\n");
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
fprintf(stderr, "audio interface prepared\n");
fprintf(stderr, "buffer allocated\n");
for (i = 0; i < buffers; ++i) {
buffer[i] = malloc(buffer_frames * snd_pcm_format_width(format) / 8 * 2);
if ((err = snd_pcm_readi (capture_handle, buffer[i], buffer_frames)) != buffer_frames) {
fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err));
exit (1);
}
}
for (i = 0; i < 3200; ++i) {
avail_cap = snd_pcm_avail ( capture_handle );
fprintf (stderr,"snd_pcm_avail: %d ", (avail_cap=snd_pcm_avail_update( capture_handle )) );
if ((err = snd_pcm_readi (capture_handle, buffer[i%buffers], buffer_frames)) != buffer_frames) {
fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err));
exit (1);
}
doFFT(i%buffers,buffer);
printf( "\n");
}
free(buffer[0]);
fprintf(stderr, "buffer freed\n");
snd_pcm_close (capture_handle);
fprintf(stderr, "audio interface closed\n");
exit (0);
}