-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdataprocessing.c
More file actions
137 lines (105 loc) · 3.8 KB
/
dataprocessing.c
File metadata and controls
137 lines (105 loc) · 3.8 KB
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
#include "dataprocessing.h"
#include <assert.h>
#include <math.h>
enum {re, im}; //real and imaginary
extern volatile int keeprunning;
extern const int BUCKETS;
void setupDFTForSound(Visualizer_Pkg_ptr vis_pkg_ptr, Uint8* buffer, int bytesRead)
{
int bytewidth = vis_pkg_ptr->bitsize / 8;
SDL_AudioSpec* wavSpec = GetSDL_AudioSpec(vis_pkg_ptr);
int channels = wavSpec->channels;
SDL_AudioFormat fmt = wavSpec->format;
int frames = bytesRead / (bytewidth * channels);
struct FFTWop* fftwop = GetFFTWop(vis_pkg_ptr);
int count = 0, c;
for(c = 0; c < channels; ++c){
fftwop[c].p = fftw_plan_dft_1d(frames, fftwop[c].in,
fftwop[c].out, FFTW_FORWARD, FFTW_MEASURE);
}
while(count < frames){
for(c = 0; c< channels; ++c){
fftwop[c].in[count][re] = vis_pkg_ptr->GetAudioSample(buffer, fmt);
fftwop[c].in[count][im] = 0.0;
buffer+=bytewidth;
}
count++;
}
}
int getFileSize(FILE *inFile)
{
int fileSize = 0;
fseek(inFile,0,SEEK_END);
fileSize=ftell(inFile);
fseek(inFile,0,SEEK_SET);
return fileSize;
}
void processWAVFile(Uint32 wavLength, int buffer_size, Visualizer_Pkg_ptr vis_pkg_ptr)
{
struct FFTWop* dft = GetFFTWop(vis_pkg_ptr);
int channels = GetSDL_AudioSpec(vis_pkg_ptr)->channels;
FILE* wavFile = fopen(vis_pkg_ptr->filename, "r");
int filesize = getFileSize(wavFile);
Uint8* buffer = (Uint8*)malloc(buffer_size*sizeof(Uint8));
size_t bytesRead;
int packet_index = 0, i;
//Skip header information in .WAV file
bytesRead = fread(buffer, sizeof buffer[0], filesize-wavLength, wavFile);
//Reading actual audio data
while ((bytesRead = fread(buffer, sizeof buffer[0],
buffer_size/sizeof(buffer[0]), wavFile)) > 0){
vis_pkg_ptr->setupDFT(vis_pkg_ptr, buffer, bytesRead);
for(i = 0; i < channels; ++i){
fftw_execute(dft[i].p);
analyze_FFTW_Results(vis_pkg_ptr, dft[i], packet_index, i ,bytesRead);
fftw_destroy_plan(dft[i].p);
}
packet_index++;
}
/*MEMORY MANAGEMENT*/
free(buffer);
for(i = 0; i<channels; ++i){
free(dft[i].in);
free(dft[i].out);
}
free(dft);
fclose(wavFile);
}
void analyze_FFTW_Results(Visualizer_Pkg_ptr packet, struct FFTWop fftwop ,
int packet_index, int ch,size_t bytesRead)
{
double real, imag;
double peakmax = 1.7E-308 ;
int max_index = -1, i, j;
double magnitude;
double* peakmaxArray = (double*)malloc(BUCKETS*sizeof(double));
double nyquist = packet->wavSpec_ptr->freq / 2;
double freq_bin[] = {19.0, 140.0, 400.0, 2600.0, 5200.0, nyquist };
SDL_AudioSpec* wavSpec = GetSDL_AudioSpec(packet);
int frames = bytesRead / (wavSpec->channels * packet->bitsize / 8);
struct FFTW_Results* results = GetFFTW_Results(packet);
for(i = 0; i<BUCKETS; ++i) peakmaxArray[i] = 1.7E-308;
for(j = 0; j < frames/2; ++j){
real = fftwop.out[j][0];
imag = fftwop.out[j][1];
magnitude = sqrt(real*real+imag*imag);
double freq = j * (double)wavSpec->freq / frames;
for (i = 0; i < BUCKETS; ++i){
if((freq>freq_bin[i]) && (freq <=freq_bin[i+1])){
if (magnitude > peakmaxArray[i]){
peakmaxArray[i] = magnitude;
}
}
}
if(magnitude > peakmax){
peakmax = magnitude;
max_index = j;
}
}
results[packet_index].peakpower[ch] = 10*(log10(peakmax));
results[packet_index].peakfreq[ch] = max_index*(double)wavSpec->freq/frames;
for(i = 0; i< BUCKETS; ++i){
results[packet_index].peakmagMatrix[ch][i]=10*(log10(peakmaxArray[i]));
}
free(peakmaxArray);
}