-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordBlocksFFT.c
260 lines (208 loc) · 7.06 KB
/
recordBlocksFFT.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
alsa-record-example.c
pcm_i.c
A Minimal Capture Program
This program opens an audio interface for capture, configures it for
stereo, 16 bit, 44.1kHz, interleaved conventional read/write
access. Then its reads a chunk of random data from it, and exits. It
isn't meant to be a real program.
From on Paul David's tutorial : http://equalarea.com/paul/alsa-audio.html
Fixes rate and buffer problems
sudo apt-get install libasound2-dev
gcc -o alsa-record-example -lasound alsa-record-example.c && ./alsa-record-example hw:0
gcc -o alsa-record-example -lasound alsa-record-example.c && ./alsa-record-example plughw:1,0
gcc -o alsa-record-example alsa-record-example.c -lasound
gcc -o pcm_i -lasound pcm_i.c && ./pcm_i plughw:1,0
gbookFsrf08.js
gbF( 'srf08','t@pi','Mon Feb 10 22:10:41 UTC 2014','127.0.0.1',10,0,143,0,'comments');
*/
#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();
}
/* Computes the 1-D inverse fast Fourier transform. */
void ifft(fftw_complex *in, fftw_complex *out)
{
// create an IDFT plan
fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
// execute the plan
fftw_execute(plan);
// do some cleaning
fftw_destroy_plan(plan);
fftw_cleanup();
// scale the output to obtain the exact inverse
for (int i = 0; i < N; ++i) {
out[i][REAL] /= N;
out[i][IMAG] /= N;
}
}
/* Displays complex numbers in the form a +/- bi. */
double getMagnitude(fftw_complex y)
{
return(sqrt(y[IMAG]*y[IMAG]+y[REAL]*y[REAL]));
}
/* Displays the real parts of complex numbers. */
void displayReal(fftw_complex *y)
{
//for (int i = 0; i < N; ++i)
//cout << y[i][REAL] << endl;
}
int doFFT(int startbuffer, char *buffer[])
{
snd_pcm_format_t format = SND_PCM_FORMAT_FLOAT_LE;
// input array
fftw_complex xL[N];
fftw_complex xR[N];
// output array
fftw_complex yL[N];
fftw_complex yR[N];
float * tempL;
float * tempR;
int bufnum;
int cursor;
int bf=buffer_frames;
// fill the first array with some numbers
for (int i = 0; i < N; i++) {
bufnum=(startbuffer+(i/bf))%buffers;
cursor= i%bf ;
tempL = (float *)(&buffer[bufnum][cursor*8]);
tempR = (float *)(&buffer[bufnum][cursor*8]+4);
//printf( "f(%d,%d,%f,%f)\n", bufnum,cursor,*tempL,*tempR );
xL[i][REAL] = (double)*tempL;
xR[i][REAL] = (double)*tempR;
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]));
printf( "L(%f) R(%f)\n", getMagnitude(yL[10]),getMagnitude(yR[10]));
// display the results
//cout << "FFT =" << endl;
//displayComplex(y);
// compute the IFFT of y and store the results in x
//ifft(y, x);
// display the results
//cout << "\nIFFT =" << endl;
//displayReal(x);
return 0;
}
void main(int argc, char *argv[])
{
int i,buff_i,j;
short * sampleLP;
short * sampleRP;
int err;
char *buffer[buffers];
char *arg="default";
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_FLOAT_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: %ld ", (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);
}
//for (j = 0; j < buffer_frames; j+=2) {
// sampleLP = ( unsigned short * )(&buffer[i][j] );
//sampleRP = ( unsigned short * )(&buffer[i][j+1] );
//printf( "f(%hu,%hu,0,0)\n", sampleLP , sampleRP );
//printf( "f(%d,%d,0,0)\n", buffer[j], buffer[j+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);
}