Commit 6a961b68b14c2128fe682f5a99e3f65b40d449fa
1 parent
6c227d28
modifications
Showing
48 changed files
with
1606 additions
and
148 deletions
Show diff stats
com.WAV deleted
No preview for this file type
cowfft.WAV deleted
No preview for this file type
COW.WAV renamed to fft/COW.WAV
No preview for this file type
No preview for this file type
fft.cpp renamed to fft/fft.cpp
fft.h renamed to fft/fft.h
fftcow.cpp renamed to fft/fftcow.cpp
@@ -13,35 +13,38 @@ | @@ -13,35 +13,38 @@ | ||
13 | 13 | ||
14 | int main(int argc, char **argv) | 14 | int main(int argc, char **argv) |
15 | { | 15 | { |
16 | +//Definition variables | ||
17 | + int i; | ||
16 | float pi=3.141592; | 18 | float pi=3.141592; |
17 | WavData w; | 19 | WavData w; |
20 | + w.load("COW.WAV"); | ||
21 | + char *data = w.data(); | ||
22 | + printf("%d\n",w.datasize()); | ||
23 | + char *data2 = new char[w.datasize()]; | ||
24 | + | ||
25 | + double test[w.datasize()*2][2]; | ||
26 | + double test1[w.datasize()*2][2]; | ||
27 | + double test2[w.datasize()*2][2]; | ||
18 | 28 | ||
19 | - double test[SIZE][2]; | ||
20 | - double test1[SIZE][2]; | ||
21 | - double test2[SIZE][2]; | ||
22 | - | ||
23 | - char *data = new char[SIZE]; | ||
24 | - char *data2 = new char[SIZE]; | ||
25 | - | ||
26 | - int i; | ||
27 | - for(i=0;i<SIZE;i++){ | ||
28 | - float w=2.0*3.14*FREQLA; | ||
29 | - float t=(float)i/FREQ; | ||
30 | - | ||
31 | - data[i]=AMPLITUDE*(1+sin(w*t)); | ||
32 | - | 29 | +//Creation de la donnée |
30 | + | ||
31 | + for(i=0;i<w.datasize();i++){ | ||
33 | test[i][0]=(double)data[i]; | 32 | test[i][0]=(double)data[i]; |
34 | - test[i][1]=(double)data[i]; | 33 | + test[i][1]=0; |
35 | } | 34 | } |
36 | - | ||
37 | - fft(32768*2,test,test2); | ||
38 | 35 | ||
36 | +//FFT | ||
37 | + fft(32768*2,test,test2); | ||
38 | + | ||
39 | +//FFT inverse | ||
39 | ifft(32768*2,test1,test2); | 40 | ifft(32768*2,test1,test2); |
40 | 41 | ||
41 | - for(i=0;i<SIZE;i++){ | 42 | +//Récupération des données |
43 | + for(i=0;i<w.datasize();i++){ | ||
42 | data2[i]=test1[i][0]; | 44 | data2[i]=test1[i][0]; |
43 | } | 45 | } |
44 | 46 | ||
47 | +//Preapation format final | ||
45 | w.setAudioFormat(1); | 48 | w.setAudioFormat(1); |
46 | w.setNbrChanel(1); | 49 | w.setNbrChanel(1); |
47 | w.setFrequency(FREQ); | 50 | w.setFrequency(FREQ); |
@@ -50,7 +53,7 @@ int main(int argc, char **argv) | @@ -50,7 +53,7 @@ int main(int argc, char **argv) | ||
50 | w.setBitsPerSample(8); | 53 | w.setBitsPerSample(8); |
51 | w.clearData(); | 54 | w.clearData(); |
52 | 55 | ||
53 | - w.setDatasize(SIZE); | 56 | + w.setDatasize(w.datasize()); |
54 | w.setData(data2); | 57 | w.setData(data2); |
55 | w.save("cowfft.WAV"); | 58 | w.save("cowfft.WAV"); |
56 | 59 |
No preview for this file type
@@ -0,0 +1,61 @@ | @@ -0,0 +1,61 @@ | ||
1 | +#include <iostream> | ||
2 | +#include "wavdata.h" | ||
3 | +#include "fft.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | + | ||
7 | +#define FREQ 22400 | ||
8 | +#define AMPLITUDE 10 | ||
9 | +#define FREQDO 261 | ||
10 | +#define FREQLA 440 | ||
11 | +#define SIZE FREQ*2 | ||
12 | + | ||
13 | + | ||
14 | +int main(int argc, char **argv) | ||
15 | +{ | ||
16 | +//Definition variables | ||
17 | + int i,j; | ||
18 | + float pi=3.141592; | ||
19 | + double test[SIZE*2][2]; | ||
20 | + double test2[SIZE*2][2]; | ||
21 | + double test1[SIZE*2][2]; | ||
22 | + | ||
23 | + char *data2 = new char[SIZE]; | ||
24 | + WavData w; | ||
25 | + | ||
26 | +//Creation de la donnée | ||
27 | + for(i=0;i<SIZE;i++){ | ||
28 | + float wla=2.0*3.14*FREQLA; | ||
29 | + float t=(float)i/FREQ; | ||
30 | + | ||
31 | + test[i][0]=AMPLITUDE*(1+sin(wla*t)); | ||
32 | + test[i][1]=0; | ||
33 | + } | ||
34 | + | ||
35 | +//FFT | ||
36 | + fft(32768*2,test,test2); | ||
37 | + | ||
38 | +//FFT inverse | ||
39 | + ifft(32768*2,test1,test2); | ||
40 | + | ||
41 | +//Récupération des données | ||
42 | + | ||
43 | + for(j=0;j<SIZE;j++){ | ||
44 | + data2[j]=test1[j][0]; | ||
45 | + } | ||
46 | + | ||
47 | +//Preapation format final | ||
48 | + w.setAudioFormat(1); | ||
49 | + w.setNbrChanel(1); | ||
50 | + w.setFrequency(FREQ); | ||
51 | + w.setBytePerBloc(4); | ||
52 | + w.setBytePerSec(FREQ); | ||
53 | + w.setBitsPerSample(8); | ||
54 | + w.clearData(); | ||
55 | + | ||
56 | + w.setDatasize(SIZE); | ||
57 | + w.setData(data2); | ||
58 | + w.save("fftla.WAV"); | ||
59 | + | ||
60 | + | ||
61 | +} | ||
0 | \ No newline at end of file | 62 | \ No newline at end of file |
No preview for this file type
wavdata.cpp renamed to fft/wavdata.cpp
wavdata.h renamed to fft/wavdata.h
No preview for this file type
COW_d.WAV renamed to main/COW_d.WAV
No preview for this file type
@@ -0,0 +1,104 @@ | @@ -0,0 +1,104 @@ | ||
1 | +/*---------------------------------------------------------------------------- | ||
2 | + fft.c - fast Fourier transform and its inverse (both recursively) | ||
3 | + Copyright (C) 2004, Jerome R. Breitenbach. All rights reserved. | ||
4 | + | ||
5 | + The author gives permission to anyone to freely copy, distribute, and use | ||
6 | + this file, under the following conditions: | ||
7 | + - No changes are made. | ||
8 | + - No direct commercial advantage is obtained. | ||
9 | + - No liability is attributed to the author for any damages incurred. | ||
10 | + ----------------------------------------------------------------------------*/ | ||
11 | + | ||
12 | +/****************************************************************************** | ||
13 | + * This file defines a C function fft that, by calling another function * | ||
14 | + * fft_rec (also defined), calculates an FFT recursively. Usage: * | ||
15 | + * fft(N, x, X); * | ||
16 | + * Parameters: * | ||
17 | + * N: number of points in FFT (must equal 2^n for some integer n >= 1) * | ||
18 | + * x: pointer to N time-domain samples given in rectangular form (Re x, * | ||
19 | + * Im x) * | ||
20 | + * X: pointer to N frequency-domain samples calculated in rectangular form * | ||
21 | + * (Re X, Im X) * | ||
22 | + * Similarly, a function ifft with the same parameters is defined that * | ||
23 | + * calculates an inverse FFT (IFFT) recursively. Usage: * | ||
24 | + * ifft(N, x, X); * | ||
25 | + * Here, N and X are given, and x is calculated. * | ||
26 | + ******************************************************************************/ | ||
27 | + | ||
28 | +#include <stdlib.h> | ||
29 | +#include <math.h> | ||
30 | +#include "fft.h" | ||
31 | +/* macros */ | ||
32 | +#define TWO_PI (6.2831853071795864769252867665590057683943L) | ||
33 | + | ||
34 | +/* FFT */ | ||
35 | +void fft(int N, double (*x)[2], double (*X)[2]) | ||
36 | +{ | ||
37 | + /* Declare a pointer to scratch space. */ | ||
38 | + double (*XX)[2] = (double (*)[2]) malloc(2 * N * sizeof(double)); | ||
39 | + | ||
40 | + /* Calculate FFT by a recursion. */ | ||
41 | + fft_rec(N, 0, 1, x, X, XX); | ||
42 | + | ||
43 | + /* Free memory. */ | ||
44 | + free(XX); | ||
45 | +} | ||
46 | + | ||
47 | +/* FFT recursion */ | ||
48 | +void fft_rec(int N, int offset, int delta, | ||
49 | + double (*x)[2], double (*X)[2], double (*XX)[2]) | ||
50 | +{ | ||
51 | + int N2 = N/2; /* half the number of points in FFT */ | ||
52 | + int k; /* generic index */ | ||
53 | + double cs, sn; /* cosine and sine */ | ||
54 | + int k00, k01, k10, k11; /* indices for butterflies */ | ||
55 | + double tmp0, tmp1; /* temporary storage */ | ||
56 | + | ||
57 | + if(N != 2) /* Perform recursive step. */ | ||
58 | + { | ||
59 | + /* Calculate two (N/2)-point DFT's. */ | ||
60 | + fft_rec(N2, offset, 2*delta, x, XX, X); | ||
61 | + fft_rec(N2, offset+delta, 2*delta, x, XX, X); | ||
62 | + | ||
63 | + /* Combine the two (N/2)-point DFT's into one N-point DFT. */ | ||
64 | + for(k=0; k<N2; k++) | ||
65 | + { | ||
66 | + k00 = offset + k*delta; k01 = k00 + N2*delta; | ||
67 | + k10 = offset + 2*k*delta; k11 = k10 + delta; | ||
68 | + cs = cos(TWO_PI*k/(double)N); sn = sin(TWO_PI*k/(double)N); | ||
69 | + tmp0 = cs * XX[k11][0] + sn * XX[k11][1]; | ||
70 | + tmp1 = cs * XX[k11][1] - sn * XX[k11][0]; | ||
71 | + X[k01][0] = XX[k10][0] - tmp0; | ||
72 | + X[k01][1] = XX[k10][1] - tmp1; | ||
73 | + X[k00][0] = XX[k10][0] + tmp0; | ||
74 | + X[k00][1] = XX[k10][1] + tmp1; | ||
75 | + } | ||
76 | + } | ||
77 | + else /* Perform 2-point DFT. */ | ||
78 | + { | ||
79 | + k00 = offset; k01 = k00 + delta; | ||
80 | + X[k01][0] = x[k00][0] - x[k01][0]; | ||
81 | + X[k01][1] = x[k00][1] - x[k01][1]; | ||
82 | + X[k00][0] = x[k00][0] + x[k01][0]; | ||
83 | + X[k00][1] = x[k00][1] + x[k01][1]; | ||
84 | + } | ||
85 | +} | ||
86 | + | ||
87 | +/* IFFT */ | ||
88 | +void ifft(int N, double (*x)[2], double (*X)[2]) | ||
89 | +{ | ||
90 | + int N2 = N/2; /* half the number of points in IFFT */ | ||
91 | + int i; /* generic index */ | ||
92 | + double tmp0, tmp1; /* temporary storage */ | ||
93 | + | ||
94 | + /* Calculate IFFT via reciprocity property of DFT. */ | ||
95 | + fft(N, X, x); | ||
96 | + x[0][0] = x[0][0]/N; x[0][1] = x[0][1]/N; | ||
97 | + x[N2][0] = x[N2][0]/N; x[N2][1] = x[N2][1]/N; | ||
98 | + for(i=1; i<N2; i++) | ||
99 | + { | ||
100 | + tmp0 = x[i][0]/N; tmp1 = x[i][1]/N; | ||
101 | + x[i][0] = x[N-i][0]/N; x[i][1] = x[N-i][1]/N; | ||
102 | + x[N-i][0] = tmp0; x[N-i][1] = tmp1; | ||
103 | + } | ||
104 | +} |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +#ifndef FFT_H | ||
2 | +#define FFT_H | ||
3 | + | ||
4 | +/* function prototypes */ | ||
5 | +void fft(int N, double (*x)[2], double (*X)[2]); | ||
6 | +void fft_rec(int N, int offset, int delta, | ||
7 | + double (*x)[2], double (*X)[2], double (*XX)[2]); | ||
8 | +void ifft(int N, double (*x)[2], double (*X)[2]); | ||
9 | + | ||
10 | +#endif // FFT_H |
main.cpp renamed to main/main.cpp
@@ -0,0 +1,269 @@ | @@ -0,0 +1,269 @@ | ||
1 | +#include "wavdata.h" | ||
2 | +#include <iostream> | ||
3 | + | ||
4 | + | ||
5 | +using namespace std; | ||
6 | + | ||
7 | +WavData::WavData() | ||
8 | +{ | ||
9 | + _data=NULL; | ||
10 | +} | ||
11 | + | ||
12 | +int WavData::error(int i,int line) | ||
13 | +{ | ||
14 | + std::cerr << "Erreur "<<__FILE__<<" L"<<line<< ": "; | ||
15 | + switch(i) | ||
16 | + { | ||
17 | + case -1: std::cerr << "ouverture du fichier"; | ||
18 | + break; | ||
19 | + case -2: std::cerr << "le fichier n'est pas au format WAVE"; | ||
20 | + break; | ||
21 | + default: std::cerr << "inconnue"; | ||
22 | + break; | ||
23 | + } | ||
24 | + | ||
25 | + std::cerr << "\n"; | ||
26 | + return i; | ||
27 | +} | ||
28 | + | ||
29 | +unsigned int WavData::fromLittleEndian(char * content, int size) | ||
30 | +{ | ||
31 | + unsigned int result = 0; | ||
32 | + for(int i=size;i>=0;i--) | ||
33 | + { | ||
34 | + result *= 256; | ||
35 | + result += (unsigned char)content[i]; | ||
36 | + } | ||
37 | + return result; | ||
38 | +} | ||
39 | + | ||
40 | +char * WavData::toLittleEndian(unsigned int content, int size) | ||
41 | +{ | ||
42 | + char * result= new char [size*sizeof(char)]; | ||
43 | + | ||
44 | + for(int i=0;i<size;i++) | ||
45 | + { | ||
46 | + result += (char)(content%256); | ||
47 | + content >>= 8; | ||
48 | + } | ||
49 | + | ||
50 | + return result; | ||
51 | +} | ||
52 | + | ||
53 | +void WavData::toLittleEndian(char* result,unsigned int content, int size) | ||
54 | +{ | ||
55 | + for(int i=0;i<size;i++) | ||
56 | + { | ||
57 | + result[i] = (char)(content%256); | ||
58 | + content >>= 8; | ||
59 | + } | ||
60 | + | ||
61 | + return; | ||
62 | +} | ||
63 | + | ||
64 | +char * WavData::read(ifstream* file,unsigned int size) | ||
65 | +{ | ||
66 | + char * c = new char[size+1]; | ||
67 | + for(int i=0;i<size;i++) | ||
68 | + { | ||
69 | + c[i] = file->get(); | ||
70 | + } | ||
71 | + c[size]='\0'; | ||
72 | + return c; | ||
73 | +} | ||
74 | + | ||
75 | +bool WavData::comp(char* c1, char *c2, int size) | ||
76 | +{ | ||
77 | + for(int i=0;i<size;i++) | ||
78 | + { | ||
79 | + if(c1[i]!=c2[i])return false; | ||
80 | + } | ||
81 | + return true; | ||
82 | +} | ||
83 | + | ||
84 | +int WavData::testString(ifstream* file,char*compstr,int size,char *mess) | ||
85 | +{ | ||
86 | + char * content = read(file,size); | ||
87 | + std::cout << "| "<< mess << std::endl<<"| ->"<< content << std::endl << "|"<<std::endl; | ||
88 | + if(!comp(content,compstr,size))return error(-2,__LINE__); | ||
89 | + | ||
90 | + delete[] content; | ||
91 | + return 1; | ||
92 | +} | ||
93 | + | ||
94 | +int WavData::testInt(ifstream* file,int size,char *mess,unsigned int *val) | ||
95 | +{ | ||
96 | + int test; | ||
97 | + char * content = read(file,size); | ||
98 | + | ||
99 | + int value=fromLittleEndian(content, size); | ||
100 | + std::cout << "| "<< mess << std::endl<<"| ->"<<value << std::endl<<"|" <<std::endl; | ||
101 | + if(val!=NULL)*val=value; | ||
102 | + delete[] content; | ||
103 | + return 1; | ||
104 | +} | ||
105 | + | ||
106 | +int WavData::openFormatBloc(ifstream* file) | ||
107 | +{ | ||
108 | + std::cout << "[Bloc de declaration d'un fichier au format WAVE]"<<std::endl<<"|"<<std::endl; | ||
109 | + | ||
110 | + // FileTypeBlocId | ||
111 | + int r; | ||
112 | + r = testString(file,"RIFF",4, "FileTypeBlocID (4 octets) : Constante 'RIFF'"); | ||
113 | + if(r<=0)return r; | ||
114 | + | ||
115 | + // FileSize | ||
116 | + r = testInt(file,4, "FileSize (4 octets) : Taille du fichier moins 8 octets"); | ||
117 | + if(r<=0)return r; | ||
118 | + | ||
119 | + // FileFormatID | ||
120 | + r = testString(file,"WAVE",4, "FileFormatID (4 octets) : Format = 'WAVE'"); | ||
121 | + if(r<=0)return r; | ||
122 | + | ||
123 | + std::cout << std::endl; | ||
124 | + return 1; | ||
125 | +} | ||
126 | + | ||
127 | +int WavData::openDescriptionBloc(ifstream* file) | ||
128 | +{ | ||
129 | + std::cout << "[Bloc decrivant le format audio]"<<std::endl<<"|"<<std::endl; | ||
130 | + | ||
131 | + int r; | ||
132 | + | ||
133 | + // FormatBlocID | ||
134 | + r = testString(file,"fmt ",4, "FormatBlocID (4 octets) : Identifiant 'fmt'"); | ||
135 | + if(r<=0)return r; | ||
136 | + | ||
137 | + // BlocSize | ||
138 | + r = testInt(file,4,"BlocSize (4 octets) : Nombre d'octets du bloc - 8 (attendue: 16)"); | ||
139 | + if(r<=0)return r; | ||
140 | + | ||
141 | + // AudioFormat | ||
142 | + r = testInt(file,2,"AudioFormat (2 octets) : Format du stockage dans le fichier (1: PCM, ...)",&_audioFormat); | ||
143 | + if(r<=0)return r; | ||
144 | + | ||
145 | + // NbrCanaux | ||
146 | + r = testInt(file,2,"NbrCanaux (2 octets) : Nombre de canaux (de 1 a 6)",&_nbrChanel); | ||
147 | + if(r<=0)return r; | ||
148 | + | ||
149 | + // Frequency | ||
150 | + r = testInt(file,4,"Frequence (4 octets) : Frequence d'echantillonnage (en Hertz)",&_frequency); | ||
151 | + if(r<=0)return r; | ||
152 | + | ||
153 | + // BytePerSec | ||
154 | + r = testInt(file,4,"BytePerSec (4 octets) : Nombre d'octets a lire par seconde",&_bytePerSec); | ||
155 | + if(r<=0)return r; | ||
156 | + | ||
157 | + // BytePerBloc | ||
158 | + r = testInt(file,2,"BytePerBloc (2 octets) : Nombre d'octets par bloc d'echantillonnage",&_bytePerBloc); | ||
159 | + if(r<=0)return r; | ||
160 | + | ||
161 | + // BitsPerSample | ||
162 | + r = testInt(file,2,"BitsPerSample (2 octets) : Nombre de bits pour le codage d'un echantillon",&_bitsPerSample); | ||
163 | + if(r<=0)return r; | ||
164 | + | ||
165 | + return 1; | ||
166 | +} | ||
167 | + | ||
168 | +int WavData::openDataBloc(ifstream* file) | ||
169 | +{ | ||
170 | + std::cout << "[Bloc des donnees]\n"; | ||
171 | + | ||
172 | + int r,test; | ||
173 | + // DataBlocID | ||
174 | + r = testString(file,"data",4,"DataBlocID (4 octets) : Constante 'data'"); | ||
175 | + if(r<=0)return r; | ||
176 | + | ||
177 | + // DataSize | ||
178 | + r = testInt(file,3,"DataSize (4 octets) : Nombre d'octets des donnees",&_datasize); | ||
179 | + if(r<=0)return r; | ||
180 | + | ||
181 | + // Data[]; | ||
182 | + std::cout << "| Data[] : [Octets du Sample 1 du Canal 1] [Octets du Sample 1 du Canal 2] [Octets du Sample 2 du Canal 1] [Octets du Sample 2 du Canal 2]\n\n"; | ||
183 | + if(_data!=NULL)delete[] _data; | ||
184 | + _data = read(file,_datasize); | ||
185 | + | ||
186 | + return 1; | ||
187 | +} | ||
188 | + | ||
189 | +int WavData::load(char * s) | ||
190 | +{ | ||
191 | + std::cout << "Load file: \"" << s << "\""<<std::endl<<std::endl; | ||
192 | + | ||
193 | + ifstream file(s, ios::in | ios::binary ); | ||
194 | + if (!file)return error(-1,__LINE__); | ||
195 | + | ||
196 | + if(openFormatBloc(&file) != 1) {file.close(); return 0;} | ||
197 | + if(openDescriptionBloc(&file) != 1) {file.close(); return 0;} | ||
198 | + if(openDataBloc(&file) != 1) {file.close(); return 0;} | ||
199 | + | ||
200 | + std::cout << "close file"<< std::endl; | ||
201 | + file.close(); | ||
202 | + std::cout << "file closed"<<std::endl; | ||
203 | + | ||
204 | + return 1; | ||
205 | +} | ||
206 | + | ||
207 | +void WavData::write(ofstream *file,char* mess,unsigned int size) | ||
208 | +{ | ||
209 | + for(int i=0;i<size;i++) | ||
210 | + { | ||
211 | + file->put(mess[i]); | ||
212 | + } | ||
213 | +} | ||
214 | + | ||
215 | +int WavData::saveFormatBloc(ofstream* file) | ||
216 | +{ | ||
217 | + char b[13] = "RIFF WAVE"; | ||
218 | + | ||
219 | + toLittleEndian(b+4,_datasize+36,4); | ||
220 | + | ||
221 | + write(file,b,12); | ||
222 | + | ||
223 | + return 1; | ||
224 | +} | ||
225 | + | ||
226 | +int WavData::saveDescriptionBloc(ofstream* file) | ||
227 | +{ | ||
228 | + char b[25] = "fmt "; | ||
229 | + | ||
230 | + toLittleEndian(b+4,0x10,4); | ||
231 | + std::cout<<_audioFormat<<std::endl; | ||
232 | + toLittleEndian(b+8,_audioFormat,2); | ||
233 | + toLittleEndian(b+10,_nbrChanel,2); | ||
234 | + toLittleEndian(b+12,_frequency,4); | ||
235 | + toLittleEndian(b+16,_bytePerSec,4); | ||
236 | + toLittleEndian(b+20,_bytePerBloc,2); | ||
237 | + toLittleEndian(b+22,_bitsPerSample,2); | ||
238 | + | ||
239 | + write(file,b,24); | ||
240 | + | ||
241 | + return 1; | ||
242 | +} | ||
243 | +int WavData::saveDataBloc(ofstream* file) | ||
244 | +{ | ||
245 | + char b[9] = "data "; | ||
246 | + toLittleEndian(b+4,_datasize,4); | ||
247 | + | ||
248 | + write(file,b,8); | ||
249 | + | ||
250 | + write(file,_data,_datasize); | ||
251 | + | ||
252 | + return 1; | ||
253 | +} | ||
254 | + | ||
255 | +int WavData::save(char * s) | ||
256 | +{ | ||
257 | + std::cout << "Save file: \"" << s << "\"\n\n"; | ||
258 | + | ||
259 | + ofstream file(s, ios::out | ios::trunc | ios::binary); | ||
260 | + if (!file)return error(-1,__LINE__); | ||
261 | + | ||
262 | + saveFormatBloc(&file); | ||
263 | + saveDescriptionBloc(&file); | ||
264 | + saveDataBloc(&file); | ||
265 | + | ||
266 | + file.close(); | ||
267 | + | ||
268 | + return 1; | ||
269 | +} |
@@ -0,0 +1,66 @@ | @@ -0,0 +1,66 @@ | ||
1 | +#ifndef WAVDATA_H | ||
2 | +#define WAVDATA_H | ||
3 | + | ||
4 | +#include <iostream> | ||
5 | +#include <fstream> | ||
6 | + | ||
7 | +class WavData | ||
8 | +{ | ||
9 | + unsigned int _audioFormat; | ||
10 | + unsigned int _nbrChanel; | ||
11 | + unsigned int _frequency; | ||
12 | + unsigned int _bytePerBloc; | ||
13 | + unsigned int _bytePerSec; | ||
14 | + unsigned int _bitsPerSample; | ||
15 | + | ||
16 | + char * _data; | ||
17 | + unsigned int _datasize; | ||
18 | +public: | ||
19 | + WavData(); | ||
20 | + | ||
21 | + int load(char* s); | ||
22 | + int save(char* s); | ||
23 | + | ||
24 | + unsigned int fromLittleEndian(char * content, int size); | ||
25 | + char * toLittleEndian(unsigned int content, int size); | ||
26 | + void toLittleEndian(char* result,unsigned int content, int size); | ||
27 | + | ||
28 | + inline unsigned int audioFormat(){return _audioFormat;} | ||
29 | + inline unsigned int nbrChanel(){return _nbrChanel;} | ||
30 | + inline unsigned int frequency(){return _frequency;} | ||
31 | + inline unsigned int bytePerBloc(){return _bytePerBloc;} | ||
32 | + inline unsigned int bytePerSec(){return _bytePerSec;} | ||
33 | + inline unsigned int bitsPerSample(){return _bitsPerSample;} | ||
34 | + inline unsigned int datasize(){return _datasize;} | ||
35 | + inline char * data(){return _data;} | ||
36 | + | ||
37 | + inline void setAudioFormat(unsigned int a){_audioFormat=a;} | ||
38 | + inline void setNbrChanel(unsigned int a){_nbrChanel=a;} | ||
39 | + inline void setFrequency(unsigned int a){_frequency=a;} | ||
40 | + inline void setBytePerBloc(unsigned int a){_bytePerBloc=a;} | ||
41 | + inline void setBytePerSec(unsigned int a){_bytePerSec=a;} | ||
42 | + inline void setBitsPerSample(unsigned int a){_bitsPerSample=a;} | ||
43 | + inline void setData(char * a){_data=a;} | ||
44 | + inline void clearData(){delete[] _data;_data=NULL;} | ||
45 | + inline void setDatasize(unsigned int i){_datasize=i;} | ||
46 | + | ||
47 | +private: | ||
48 | + int error(int,int); | ||
49 | + int openFormatBloc(std::ifstream* file); | ||
50 | + int openDescriptionBloc(std::ifstream *file); | ||
51 | + int openDataBloc(std::ifstream *file); | ||
52 | + | ||
53 | + int saveFormatBloc(std::ofstream *file); | ||
54 | + int saveDescriptionBloc(std::ofstream *file); | ||
55 | + int saveDataBloc(std::ofstream *file); | ||
56 | + | ||
57 | + void write(std::ofstream *file,char* mess,unsigned int size); | ||
58 | + char * read(std::ifstream* file,unsigned int size); | ||
59 | + | ||
60 | + bool comp(char* c1, char *c2, int size); | ||
61 | + int testString(std::ifstream* file,char*compstr,int size,char *mess); | ||
62 | + int testInt(std::ifstream* file,int size,char *mess,unsigned int *val=NULL); | ||
63 | + | ||
64 | +}; | ||
65 | + | ||
66 | +#endif // WAVDATA_H |
modifreq.WAV deleted
No preview for this file type
pur.WAV deleted
No preview for this file type
@@ -0,0 +1,104 @@ | @@ -0,0 +1,104 @@ | ||
1 | +/*---------------------------------------------------------------------------- | ||
2 | + fft.c - fast Fourier transform and its inverse (both recursively) | ||
3 | + Copyright (C) 2004, Jerome R. Breitenbach. All rights reserved. | ||
4 | + | ||
5 | + The author gives permission to anyone to freely copy, distribute, and use | ||
6 | + this file, under the following conditions: | ||
7 | + - No changes are made. | ||
8 | + - No direct commercial advantage is obtained. | ||
9 | + - No liability is attributed to the author for any damages incurred. | ||
10 | + ----------------------------------------------------------------------------*/ | ||
11 | + | ||
12 | +/****************************************************************************** | ||
13 | + * This file defines a C function fft that, by calling another function * | ||
14 | + * fft_rec (also defined), calculates an FFT recursively. Usage: * | ||
15 | + * fft(N, x, X); * | ||
16 | + * Parameters: * | ||
17 | + * N: number of points in FFT (must equal 2^n for some integer n >= 1) * | ||
18 | + * x: pointer to N time-domain samples given in rectangular form (Re x, * | ||
19 | + * Im x) * | ||
20 | + * X: pointer to N frequency-domain samples calculated in rectangular form * | ||
21 | + * (Re X, Im X) * | ||
22 | + * Similarly, a function ifft with the same parameters is defined that * | ||
23 | + * calculates an inverse FFT (IFFT) recursively. Usage: * | ||
24 | + * ifft(N, x, X); * | ||
25 | + * Here, N and X are given, and x is calculated. * | ||
26 | + ******************************************************************************/ | ||
27 | + | ||
28 | +#include <stdlib.h> | ||
29 | +#include <math.h> | ||
30 | +#include "fft.h" | ||
31 | +/* macros */ | ||
32 | +#define TWO_PI (6.2831853071795864769252867665590057683943L) | ||
33 | + | ||
34 | +/* FFT */ | ||
35 | +void fft(int N, double (*x)[2], double (*X)[2]) | ||
36 | +{ | ||
37 | + /* Declare a pointer to scratch space. */ | ||
38 | + double (*XX)[2] = (double (*)[2]) malloc(2 * N * sizeof(double)); | ||
39 | + | ||
40 | + /* Calculate FFT by a recursion. */ | ||
41 | + fft_rec(N, 0, 1, x, X, XX); | ||
42 | + | ||
43 | + /* Free memory. */ | ||
44 | + free(XX); | ||
45 | +} | ||
46 | + | ||
47 | +/* FFT recursion */ | ||
48 | +void fft_rec(int N, int offset, int delta, | ||
49 | + double (*x)[2], double (*X)[2], double (*XX)[2]) | ||
50 | +{ | ||
51 | + int N2 = N/2; /* half the number of points in FFT */ | ||
52 | + int k; /* generic index */ | ||
53 | + double cs, sn; /* cosine and sine */ | ||
54 | + int k00, k01, k10, k11; /* indices for butterflies */ | ||
55 | + double tmp0, tmp1; /* temporary storage */ | ||
56 | + | ||
57 | + if(N != 2) /* Perform recursive step. */ | ||
58 | + { | ||
59 | + /* Calculate two (N/2)-point DFT's. */ | ||
60 | + fft_rec(N2, offset, 2*delta, x, XX, X); | ||
61 | + fft_rec(N2, offset+delta, 2*delta, x, XX, X); | ||
62 | + | ||
63 | + /* Combine the two (N/2)-point DFT's into one N-point DFT. */ | ||
64 | + for(k=0; k<N2; k++) | ||
65 | + { | ||
66 | + k00 = offset + k*delta; k01 = k00 + N2*delta; | ||
67 | + k10 = offset + 2*k*delta; k11 = k10 + delta; | ||
68 | + cs = cos(TWO_PI*k/(double)N); sn = sin(TWO_PI*k/(double)N); | ||
69 | + tmp0 = cs * XX[k11][0] + sn * XX[k11][1]; | ||
70 | + tmp1 = cs * XX[k11][1] - sn * XX[k11][0]; | ||
71 | + X[k01][0] = XX[k10][0] - tmp0; | ||
72 | + X[k01][1] = XX[k10][1] - tmp1; | ||
73 | + X[k00][0] = XX[k10][0] + tmp0; | ||
74 | + X[k00][1] = XX[k10][1] + tmp1; | ||
75 | + } | ||
76 | + } | ||
77 | + else /* Perform 2-point DFT. */ | ||
78 | + { | ||
79 | + k00 = offset; k01 = k00 + delta; | ||
80 | + X[k01][0] = x[k00][0] - x[k01][0]; | ||
81 | + X[k01][1] = x[k00][1] - x[k01][1]; | ||
82 | + X[k00][0] = x[k00][0] + x[k01][0]; | ||
83 | + X[k00][1] = x[k00][1] + x[k01][1]; | ||
84 | + } | ||
85 | +} | ||
86 | + | ||
87 | +/* IFFT */ | ||
88 | +void ifft(int N, double (*x)[2], double (*X)[2]) | ||
89 | +{ | ||
90 | + int N2 = N/2; /* half the number of points in IFFT */ | ||
91 | + int i; /* generic index */ | ||
92 | + double tmp0, tmp1; /* temporary storage */ | ||
93 | + | ||
94 | + /* Calculate IFFT via reciprocity property of DFT. */ | ||
95 | + fft(N, X, x); | ||
96 | + x[0][0] = x[0][0]/N; x[0][1] = x[0][1]/N; | ||
97 | + x[N2][0] = x[N2][0]/N; x[N2][1] = x[N2][1]/N; | ||
98 | + for(i=1; i<N2; i++) | ||
99 | + { | ||
100 | + tmp0 = x[i][0]/N; tmp1 = x[i][1]/N; | ||
101 | + x[i][0] = x[N-i][0]/N; x[i][1] = x[N-i][1]/N; | ||
102 | + x[N-i][0] = tmp0; x[N-i][1] = tmp1; | ||
103 | + } | ||
104 | +} |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +#ifndef FFT_H | ||
2 | +#define FFT_H | ||
3 | + | ||
4 | +/* function prototypes */ | ||
5 | +void fft(int N, double (*x)[2], double (*X)[2]); | ||
6 | +void fft_rec(int N, int offset, int delta, | ||
7 | + double (*x)[2], double (*X)[2], double (*XX)[2]); | ||
8 | +void ifft(int N, double (*x)[2], double (*X)[2]); | ||
9 | + | ||
10 | +#endif // FFT_H |
pur.cpp renamed to pur/pur.cpp
@@ -3,7 +3,7 @@ | @@ -3,7 +3,7 @@ | ||
3 | #include "fft.h" | 3 | #include "fft.h" |
4 | #include <math.h> | 4 | #include <math.h> |
5 | 5 | ||
6 | -#define FREQ 48000 | 6 | +#define FREQ 22400 |
7 | #define AMPLITUDE 10 | 7 | #define AMPLITUDE 10 |
8 | #define FREQDO 261 | 8 | #define FREQDO 261 |
9 | #define FREQLA 440 | 9 | #define FREQLA 440 |
@@ -14,19 +14,21 @@ int main(int argc, char **argv) | @@ -14,19 +14,21 @@ int main(int argc, char **argv) | ||
14 | { | 14 | { |
15 | float pi=3.141592; | 15 | float pi=3.141592; |
16 | WavData w; | 16 | WavData w; |
17 | - | ||
18 | char *data = new char[SIZE]; | 17 | char *data = new char[SIZE]; |
19 | char *data2 = new char[SIZE]; | 18 | char *data2 = new char[SIZE]; |
20 | - | ||
21 | int i; | 19 | int i; |
20 | + | ||
21 | + //Creation de la données | ||
22 | for(i=0;i<SIZE;i++){ | 22 | for(i=0;i<SIZE;i++){ |
23 | - float w=2.0*3.14*FREQLA; | 23 | + float wla=2.0*3.14*FREQLA; |
24 | + float wdo=2.0*3.14*FREQDO; | ||
24 | float t=(float)i/FREQ; | 25 | float t=(float)i/FREQ; |
25 | 26 | ||
26 | - data[i]=AMPLITUDE*(1+sin(w*t)); | 27 | + data[i]=AMPLITUDE*(1+sin(wla*t)); |
28 | + data2[i]=AMPLITUDE*(1+sin(wdo*t)); | ||
27 | } | 29 | } |
28 | 30 | ||
29 | - | 31 | + //setup du format audio de sortie |
30 | w.setAudioFormat(1); | 32 | w.setAudioFormat(1); |
31 | w.setNbrChanel(1); | 33 | w.setNbrChanel(1); |
32 | w.setFrequency(FREQ); | 34 | w.setFrequency(FREQ); |
@@ -35,8 +37,14 @@ int main(int argc, char **argv) | @@ -35,8 +37,14 @@ int main(int argc, char **argv) | ||
35 | w.setBitsPerSample(8); | 37 | w.setBitsPerSample(8); |
36 | w.clearData(); | 38 | w.clearData(); |
37 | 39 | ||
40 | + //implémentation des données de sortie | ||
38 | w.setDatasize(SIZE); | 41 | w.setDatasize(SIZE); |
39 | w.setData(data); | 42 | w.setData(data); |
40 | w.save("purla.WAV"); | 43 | w.save("purla.WAV"); |
41 | 44 | ||
45 | + w.clearData(); | ||
46 | + w.setDatasize(SIZE); | ||
47 | + w.setData(data2); | ||
48 | + w.save("purdo.WAV"); | ||
49 | + | ||
42 | } | 50 | } |
43 | \ No newline at end of file | 51 | \ No newline at end of file |
No preview for this file type
No preview for this file type
No preview for this file type
@@ -0,0 +1,269 @@ | @@ -0,0 +1,269 @@ | ||
1 | +#include "wavdata.h" | ||
2 | +#include <iostream> | ||
3 | + | ||
4 | + | ||
5 | +using namespace std; | ||
6 | + | ||
7 | +WavData::WavData() | ||
8 | +{ | ||
9 | + _data=NULL; | ||
10 | +} | ||
11 | + | ||
12 | +int WavData::error(int i,int line) | ||
13 | +{ | ||
14 | + std::cerr << "Erreur "<<__FILE__<<" L"<<line<< ": "; | ||
15 | + switch(i) | ||
16 | + { | ||
17 | + case -1: std::cerr << "ouverture du fichier"; | ||
18 | + break; | ||
19 | + case -2: std::cerr << "le fichier n'est pas au format WAVE"; | ||
20 | + break; | ||
21 | + default: std::cerr << "inconnue"; | ||
22 | + break; | ||
23 | + } | ||
24 | + | ||
25 | + std::cerr << "\n"; | ||
26 | + return i; | ||
27 | +} | ||
28 | + | ||
29 | +unsigned int WavData::fromLittleEndian(char * content, int size) | ||
30 | +{ | ||
31 | + unsigned int result = 0; | ||
32 | + for(int i=size;i>=0;i--) | ||
33 | + { | ||
34 | + result *= 256; | ||
35 | + result += (unsigned char)content[i]; | ||
36 | + } | ||
37 | + return result; | ||
38 | +} | ||
39 | + | ||
40 | +char * WavData::toLittleEndian(unsigned int content, int size) | ||
41 | +{ | ||
42 | + char * result= new char [size*sizeof(char)]; | ||
43 | + | ||
44 | + for(int i=0;i<size;i++) | ||
45 | + { | ||
46 | + result += (char)(content%256); | ||
47 | + content >>= 8; | ||
48 | + } | ||
49 | + | ||
50 | + return result; | ||
51 | +} | ||
52 | + | ||
53 | +void WavData::toLittleEndian(char* result,unsigned int content, int size) | ||
54 | +{ | ||
55 | + for(int i=0;i<size;i++) | ||
56 | + { | ||
57 | + result[i] = (char)(content%256); | ||
58 | + content >>= 8; | ||
59 | + } | ||
60 | + | ||
61 | + return; | ||
62 | +} | ||
63 | + | ||
64 | +char * WavData::read(ifstream* file,unsigned int size) | ||
65 | +{ | ||
66 | + char * c = new char[size+1]; | ||
67 | + for(int i=0;i<size;i++) | ||
68 | + { | ||
69 | + c[i] = file->get(); | ||
70 | + } | ||
71 | + c[size]='\0'; | ||
72 | + return c; | ||
73 | +} | ||
74 | + | ||
75 | +bool WavData::comp(char* c1, char *c2, int size) | ||
76 | +{ | ||
77 | + for(int i=0;i<size;i++) | ||
78 | + { | ||
79 | + if(c1[i]!=c2[i])return false; | ||
80 | + } | ||
81 | + return true; | ||
82 | +} | ||
83 | + | ||
84 | +int WavData::testString(ifstream* file,char*compstr,int size,char *mess) | ||
85 | +{ | ||
86 | + char * content = read(file,size); | ||
87 | + std::cout << "| "<< mess << std::endl<<"| ->"<< content << std::endl << "|"<<std::endl; | ||
88 | + if(!comp(content,compstr,size))return error(-2,__LINE__); | ||
89 | + | ||
90 | + delete[] content; | ||
91 | + return 1; | ||
92 | +} | ||
93 | + | ||
94 | +int WavData::testInt(ifstream* file,int size,char *mess,unsigned int *val) | ||
95 | +{ | ||
96 | + int test; | ||
97 | + char * content = read(file,size); | ||
98 | + | ||
99 | + int value=fromLittleEndian(content, size); | ||
100 | + std::cout << "| "<< mess << std::endl<<"| ->"<<value << std::endl<<"|" <<std::endl; | ||
101 | + if(val!=NULL)*val=value; | ||
102 | + delete[] content; | ||
103 | + return 1; | ||
104 | +} | ||
105 | + | ||
106 | +int WavData::openFormatBloc(ifstream* file) | ||
107 | +{ | ||
108 | + std::cout << "[Bloc de declaration d'un fichier au format WAVE]"<<std::endl<<"|"<<std::endl; | ||
109 | + | ||
110 | + // FileTypeBlocId | ||
111 | + int r; | ||
112 | + r = testString(file,"RIFF",4, "FileTypeBlocID (4 octets) : Constante 'RIFF'"); | ||
113 | + if(r<=0)return r; | ||
114 | + | ||
115 | + // FileSize | ||
116 | + r = testInt(file,4, "FileSize (4 octets) : Taille du fichier moins 8 octets"); | ||
117 | + if(r<=0)return r; | ||
118 | + | ||
119 | + // FileFormatID | ||
120 | + r = testString(file,"WAVE",4, "FileFormatID (4 octets) : Format = 'WAVE'"); | ||
121 | + if(r<=0)return r; | ||
122 | + | ||
123 | + std::cout << std::endl; | ||
124 | + return 1; | ||
125 | +} | ||
126 | + | ||
127 | +int WavData::openDescriptionBloc(ifstream* file) | ||
128 | +{ | ||
129 | + std::cout << "[Bloc decrivant le format audio]"<<std::endl<<"|"<<std::endl; | ||
130 | + | ||
131 | + int r; | ||
132 | + | ||
133 | + // FormatBlocID | ||
134 | + r = testString(file,"fmt ",4, "FormatBlocID (4 octets) : Identifiant 'fmt'"); | ||
135 | + if(r<=0)return r; | ||
136 | + | ||
137 | + // BlocSize | ||
138 | + r = testInt(file,4,"BlocSize (4 octets) : Nombre d'octets du bloc - 8 (attendue: 16)"); | ||
139 | + if(r<=0)return r; | ||
140 | + | ||
141 | + // AudioFormat | ||
142 | + r = testInt(file,2,"AudioFormat (2 octets) : Format du stockage dans le fichier (1: PCM, ...)",&_audioFormat); | ||
143 | + if(r<=0)return r; | ||
144 | + | ||
145 | + // NbrCanaux | ||
146 | + r = testInt(file,2,"NbrCanaux (2 octets) : Nombre de canaux (de 1 a 6)",&_nbrChanel); | ||
147 | + if(r<=0)return r; | ||
148 | + | ||
149 | + // Frequency | ||
150 | + r = testInt(file,4,"Frequence (4 octets) : Frequence d'echantillonnage (en Hertz)",&_frequency); | ||
151 | + if(r<=0)return r; | ||
152 | + | ||
153 | + // BytePerSec | ||
154 | + r = testInt(file,4,"BytePerSec (4 octets) : Nombre d'octets a lire par seconde",&_bytePerSec); | ||
155 | + if(r<=0)return r; | ||
156 | + | ||
157 | + // BytePerBloc | ||
158 | + r = testInt(file,2,"BytePerBloc (2 octets) : Nombre d'octets par bloc d'echantillonnage",&_bytePerBloc); | ||
159 | + if(r<=0)return r; | ||
160 | + | ||
161 | + // BitsPerSample | ||
162 | + r = testInt(file,2,"BitsPerSample (2 octets) : Nombre de bits pour le codage d'un echantillon",&_bitsPerSample); | ||
163 | + if(r<=0)return r; | ||
164 | + | ||
165 | + return 1; | ||
166 | +} | ||
167 | + | ||
168 | +int WavData::openDataBloc(ifstream* file) | ||
169 | +{ | ||
170 | + std::cout << "[Bloc des donnees]\n"; | ||
171 | + | ||
172 | + int r,test; | ||
173 | + // DataBlocID | ||
174 | + r = testString(file,"data",4,"DataBlocID (4 octets) : Constante 'data'"); | ||
175 | + if(r<=0)return r; | ||
176 | + | ||
177 | + // DataSize | ||
178 | + r = testInt(file,3,"DataSize (4 octets) : Nombre d'octets des donnees",&_datasize); | ||
179 | + if(r<=0)return r; | ||
180 | + | ||
181 | + // Data[]; | ||
182 | + std::cout << "| Data[] : [Octets du Sample 1 du Canal 1] [Octets du Sample 1 du Canal 2] [Octets du Sample 2 du Canal 1] [Octets du Sample 2 du Canal 2]\n\n"; | ||
183 | + if(_data!=NULL)delete[] _data; | ||
184 | + _data = read(file,_datasize); | ||
185 | + | ||
186 | + return 1; | ||
187 | +} | ||
188 | + | ||
189 | +int WavData::load(char * s) | ||
190 | +{ | ||
191 | + std::cout << "Load file: \"" << s << "\""<<std::endl<<std::endl; | ||
192 | + | ||
193 | + ifstream file(s, ios::in | ios::binary ); | ||
194 | + if (!file)return error(-1,__LINE__); | ||
195 | + | ||
196 | + if(openFormatBloc(&file) != 1) {file.close(); return 0;} | ||
197 | + if(openDescriptionBloc(&file) != 1) {file.close(); return 0;} | ||
198 | + if(openDataBloc(&file) != 1) {file.close(); return 0;} | ||
199 | + | ||
200 | + std::cout << "close file"<< std::endl; | ||
201 | + file.close(); | ||
202 | + std::cout << "file closed"<<std::endl; | ||
203 | + | ||
204 | + return 1; | ||
205 | +} | ||
206 | + | ||
207 | +void WavData::write(ofstream *file,char* mess,unsigned int size) | ||
208 | +{ | ||
209 | + for(int i=0;i<size;i++) | ||
210 | + { | ||
211 | + file->put(mess[i]); | ||
212 | + } | ||
213 | +} | ||
214 | + | ||
215 | +int WavData::saveFormatBloc(ofstream* file) | ||
216 | +{ | ||
217 | + char b[13] = "RIFF WAVE"; | ||
218 | + | ||
219 | + toLittleEndian(b+4,_datasize+36,4); | ||
220 | + | ||
221 | + write(file,b,12); | ||
222 | + | ||
223 | + return 1; | ||
224 | +} | ||
225 | + | ||
226 | +int WavData::saveDescriptionBloc(ofstream* file) | ||
227 | +{ | ||
228 | + char b[25] = "fmt "; | ||
229 | + | ||
230 | + toLittleEndian(b+4,0x10,4); | ||
231 | + std::cout<<_audioFormat<<std::endl; | ||
232 | + toLittleEndian(b+8,_audioFormat,2); | ||
233 | + toLittleEndian(b+10,_nbrChanel,2); | ||
234 | + toLittleEndian(b+12,_frequency,4); | ||
235 | + toLittleEndian(b+16,_bytePerSec,4); | ||
236 | + toLittleEndian(b+20,_bytePerBloc,2); | ||
237 | + toLittleEndian(b+22,_bitsPerSample,2); | ||
238 | + | ||
239 | + write(file,b,24); | ||
240 | + | ||
241 | + return 1; | ||
242 | +} | ||
243 | +int WavData::saveDataBloc(ofstream* file) | ||
244 | +{ | ||
245 | + char b[9] = "data "; | ||
246 | + toLittleEndian(b+4,_datasize,4); | ||
247 | + | ||
248 | + write(file,b,8); | ||
249 | + | ||
250 | + write(file,_data,_datasize); | ||
251 | + | ||
252 | + return 1; | ||
253 | +} | ||
254 | + | ||
255 | +int WavData::save(char * s) | ||
256 | +{ | ||
257 | + std::cout << "Save file: \"" << s << "\"\n\n"; | ||
258 | + | ||
259 | + ofstream file(s, ios::out | ios::trunc | ios::binary); | ||
260 | + if (!file)return error(-1,__LINE__); | ||
261 | + | ||
262 | + saveFormatBloc(&file); | ||
263 | + saveDescriptionBloc(&file); | ||
264 | + saveDataBloc(&file); | ||
265 | + | ||
266 | + file.close(); | ||
267 | + | ||
268 | + return 1; | ||
269 | +} |
@@ -0,0 +1,66 @@ | @@ -0,0 +1,66 @@ | ||
1 | +#ifndef WAVDATA_H | ||
2 | +#define WAVDATA_H | ||
3 | + | ||
4 | +#include <iostream> | ||
5 | +#include <fstream> | ||
6 | + | ||
7 | +class WavData | ||
8 | +{ | ||
9 | + unsigned int _audioFormat; | ||
10 | + unsigned int _nbrChanel; | ||
11 | + unsigned int _frequency; | ||
12 | + unsigned int _bytePerBloc; | ||
13 | + unsigned int _bytePerSec; | ||
14 | + unsigned int _bitsPerSample; | ||
15 | + | ||
16 | + char * _data; | ||
17 | + unsigned int _datasize; | ||
18 | +public: | ||
19 | + WavData(); | ||
20 | + | ||
21 | + int load(char* s); | ||
22 | + int save(char* s); | ||
23 | + | ||
24 | + unsigned int fromLittleEndian(char * content, int size); | ||
25 | + char * toLittleEndian(unsigned int content, int size); | ||
26 | + void toLittleEndian(char* result,unsigned int content, int size); | ||
27 | + | ||
28 | + inline unsigned int audioFormat(){return _audioFormat;} | ||
29 | + inline unsigned int nbrChanel(){return _nbrChanel;} | ||
30 | + inline unsigned int frequency(){return _frequency;} | ||
31 | + inline unsigned int bytePerBloc(){return _bytePerBloc;} | ||
32 | + inline unsigned int bytePerSec(){return _bytePerSec;} | ||
33 | + inline unsigned int bitsPerSample(){return _bitsPerSample;} | ||
34 | + inline unsigned int datasize(){return _datasize;} | ||
35 | + inline char * data(){return _data;} | ||
36 | + | ||
37 | + inline void setAudioFormat(unsigned int a){_audioFormat=a;} | ||
38 | + inline void setNbrChanel(unsigned int a){_nbrChanel=a;} | ||
39 | + inline void setFrequency(unsigned int a){_frequency=a;} | ||
40 | + inline void setBytePerBloc(unsigned int a){_bytePerBloc=a;} | ||
41 | + inline void setBytePerSec(unsigned int a){_bytePerSec=a;} | ||
42 | + inline void setBitsPerSample(unsigned int a){_bitsPerSample=a;} | ||
43 | + inline void setData(char * a){_data=a;} | ||
44 | + inline void clearData(){delete[] _data;_data=NULL;} | ||
45 | + inline void setDatasize(unsigned int i){_datasize=i;} | ||
46 | + | ||
47 | +private: | ||
48 | + int error(int,int); | ||
49 | + int openFormatBloc(std::ifstream* file); | ||
50 | + int openDescriptionBloc(std::ifstream *file); | ||
51 | + int openDataBloc(std::ifstream *file); | ||
52 | + | ||
53 | + int saveFormatBloc(std::ofstream *file); | ||
54 | + int saveDescriptionBloc(std::ofstream *file); | ||
55 | + int saveDataBloc(std::ofstream *file); | ||
56 | + | ||
57 | + void write(std::ofstream *file,char* mess,unsigned int size); | ||
58 | + char * read(std::ifstream* file,unsigned int size); | ||
59 | + | ||
60 | + bool comp(char* c1, char *c2, int size); | ||
61 | + int testString(std::ifstream* file,char*compstr,int size,char *mess); | ||
62 | + int testInt(std::ifstream* file,int size,char *mess,unsigned int *val=NULL); | ||
63 | + | ||
64 | +}; | ||
65 | + | ||
66 | +#endif // WAVDATA_H |
purfreq2.WAV deleted
No preview for this file type
purla.WAV deleted
No preview for this file type
No preview for this file type
@@ -0,0 +1,104 @@ | @@ -0,0 +1,104 @@ | ||
1 | +/*---------------------------------------------------------------------------- | ||
2 | + fft.c - fast Fourier transform and its inverse (both recursively) | ||
3 | + Copyright (C) 2004, Jerome R. Breitenbach. All rights reserved. | ||
4 | + | ||
5 | + The author gives permission to anyone to freely copy, distribute, and use | ||
6 | + this file, under the following conditions: | ||
7 | + - No changes are made. | ||
8 | + - No direct commercial advantage is obtained. | ||
9 | + - No liability is attributed to the author for any damages incurred. | ||
10 | + ----------------------------------------------------------------------------*/ | ||
11 | + | ||
12 | +/****************************************************************************** | ||
13 | + * This file defines a C function fft that, by calling another function * | ||
14 | + * fft_rec (also defined), calculates an FFT recursively. Usage: * | ||
15 | + * fft(N, x, X); * | ||
16 | + * Parameters: * | ||
17 | + * N: number of points in FFT (must equal 2^n for some integer n >= 1) * | ||
18 | + * x: pointer to N time-domain samples given in rectangular form (Re x, * | ||
19 | + * Im x) * | ||
20 | + * X: pointer to N frequency-domain samples calculated in rectangular form * | ||
21 | + * (Re X, Im X) * | ||
22 | + * Similarly, a function ifft with the same parameters is defined that * | ||
23 | + * calculates an inverse FFT (IFFT) recursively. Usage: * | ||
24 | + * ifft(N, x, X); * | ||
25 | + * Here, N and X are given, and x is calculated. * | ||
26 | + ******************************************************************************/ | ||
27 | + | ||
28 | +#include <stdlib.h> | ||
29 | +#include <math.h> | ||
30 | +#include "fft.h" | ||
31 | +/* macros */ | ||
32 | +#define TWO_PI (6.2831853071795864769252867665590057683943L) | ||
33 | + | ||
34 | +/* FFT */ | ||
35 | +void fft(int N, double (*x)[2], double (*X)[2]) | ||
36 | +{ | ||
37 | + /* Declare a pointer to scratch space. */ | ||
38 | + double (*XX)[2] = (double (*)[2]) malloc(2 * N * sizeof(double)); | ||
39 | + | ||
40 | + /* Calculate FFT by a recursion. */ | ||
41 | + fft_rec(N, 0, 1, x, X, XX); | ||
42 | + | ||
43 | + /* Free memory. */ | ||
44 | + free(XX); | ||
45 | +} | ||
46 | + | ||
47 | +/* FFT recursion */ | ||
48 | +void fft_rec(int N, int offset, int delta, | ||
49 | + double (*x)[2], double (*X)[2], double (*XX)[2]) | ||
50 | +{ | ||
51 | + int N2 = N/2; /* half the number of points in FFT */ | ||
52 | + int k; /* generic index */ | ||
53 | + double cs, sn; /* cosine and sine */ | ||
54 | + int k00, k01, k10, k11; /* indices for butterflies */ | ||
55 | + double tmp0, tmp1; /* temporary storage */ | ||
56 | + | ||
57 | + if(N != 2) /* Perform recursive step. */ | ||
58 | + { | ||
59 | + /* Calculate two (N/2)-point DFT's. */ | ||
60 | + fft_rec(N2, offset, 2*delta, x, XX, X); | ||
61 | + fft_rec(N2, offset+delta, 2*delta, x, XX, X); | ||
62 | + | ||
63 | + /* Combine the two (N/2)-point DFT's into one N-point DFT. */ | ||
64 | + for(k=0; k<N2; k++) | ||
65 | + { | ||
66 | + k00 = offset + k*delta; k01 = k00 + N2*delta; | ||
67 | + k10 = offset + 2*k*delta; k11 = k10 + delta; | ||
68 | + cs = cos(TWO_PI*k/(double)N); sn = sin(TWO_PI*k/(double)N); | ||
69 | + tmp0 = cs * XX[k11][0] + sn * XX[k11][1]; | ||
70 | + tmp1 = cs * XX[k11][1] - sn * XX[k11][0]; | ||
71 | + X[k01][0] = XX[k10][0] - tmp0; | ||
72 | + X[k01][1] = XX[k10][1] - tmp1; | ||
73 | + X[k00][0] = XX[k10][0] + tmp0; | ||
74 | + X[k00][1] = XX[k10][1] + tmp1; | ||
75 | + } | ||
76 | + } | ||
77 | + else /* Perform 2-point DFT. */ | ||
78 | + { | ||
79 | + k00 = offset; k01 = k00 + delta; | ||
80 | + X[k01][0] = x[k00][0] - x[k01][0]; | ||
81 | + X[k01][1] = x[k00][1] - x[k01][1]; | ||
82 | + X[k00][0] = x[k00][0] + x[k01][0]; | ||
83 | + X[k00][1] = x[k00][1] + x[k01][1]; | ||
84 | + } | ||
85 | +} | ||
86 | + | ||
87 | +/* IFFT */ | ||
88 | +void ifft(int N, double (*x)[2], double (*X)[2]) | ||
89 | +{ | ||
90 | + int N2 = N/2; /* half the number of points in IFFT */ | ||
91 | + int i; /* generic index */ | ||
92 | + double tmp0, tmp1; /* temporary storage */ | ||
93 | + | ||
94 | + /* Calculate IFFT via reciprocity property of DFT. */ | ||
95 | + fft(N, X, x); | ||
96 | + x[0][0] = x[0][0]/N; x[0][1] = x[0][1]/N; | ||
97 | + x[N2][0] = x[N2][0]/N; x[N2][1] = x[N2][1]/N; | ||
98 | + for(i=1; i<N2; i++) | ||
99 | + { | ||
100 | + tmp0 = x[i][0]/N; tmp1 = x[i][1]/N; | ||
101 | + x[i][0] = x[N-i][0]/N; x[i][1] = x[N-i][1]/N; | ||
102 | + x[N-i][0] = tmp0; x[N-i][1] = tmp1; | ||
103 | + } | ||
104 | +} |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +#ifndef FFT_H | ||
2 | +#define FFT_H | ||
3 | + | ||
4 | +/* function prototypes */ | ||
5 | +void fft(int N, double (*x)[2], double (*X)[2]); | ||
6 | +void fft_rec(int N, int offset, int delta, | ||
7 | + double (*x)[2], double (*X)[2], double (*XX)[2]); | ||
8 | +void ifft(int N, double (*x)[2], double (*X)[2]); | ||
9 | + | ||
10 | +#endif // FFT_H |
No preview for this file type
@@ -0,0 +1,76 @@ | @@ -0,0 +1,76 @@ | ||
1 | +#include <iostream> | ||
2 | +#include "wavdata.h" | ||
3 | +#include "fft.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | + | ||
7 | +#define FREQ 22400 | ||
8 | +#define AMPLITUDE 10 | ||
9 | +#define FREQDO 261 | ||
10 | +#define FREQLA 440 | ||
11 | +#define SIZE FREQ*2 | ||
12 | + | ||
13 | + | ||
14 | +int main(int argc, char **argv) | ||
15 | +{ | ||
16 | +//Definition variables | ||
17 | + int i,j; | ||
18 | + float pi=3.141592; | ||
19 | + double test[SIZE*2][2]; | ||
20 | + double test2[SIZE*2][2]; | ||
21 | + double test1[SIZE*2][2]; | ||
22 | + double test3[SIZE*2][2]; | ||
23 | + | ||
24 | + char *data2 = new char[SIZE]; | ||
25 | + WavData w; | ||
26 | + | ||
27 | +//Creation de la donnée | ||
28 | + printf("===Creation data\n"); | ||
29 | + for(i=0;i<SIZE;i++){ | ||
30 | + float wla=2.0*3.14*FREQLA; | ||
31 | + float t=(float)i/FREQ; | ||
32 | + | ||
33 | + test[i][0]=AMPLITUDE*(1+sin(wla*t)); | ||
34 | + test[i][1]=0; | ||
35 | + } | ||
36 | + | ||
37 | +//FFT | ||
38 | + printf("===fft\n"); | ||
39 | + fft(32768*2,test,test2); | ||
40 | + | ||
41 | +//Modification dans le domaine fréquentiel | ||
42 | + printf("===modif freq\n"); | ||
43 | + for(i=0;i<SIZE;i++){ | ||
44 | + int x=i; | ||
45 | + | ||
46 | + test3[2*x][0]=test2[x][0]; | ||
47 | + test3[2*x+1][0]=test2[x][0]; | ||
48 | + test3[2*x][1]=0; | ||
49 | + test3[2*x+1][1]=0; | ||
50 | + } | ||
51 | + | ||
52 | +//FFT inverse | ||
53 | + ifft(32768*2,test1,test3); | ||
54 | + | ||
55 | +//Récupération des données | ||
56 | + | ||
57 | + for(j=0;j<SIZE;j++){ | ||
58 | + data2[j]=10*(test1[j][0]+5); | ||
59 | + printf("%d %lf %lf\n ",j,test1[j][0],test[j][0]); | ||
60 | + } | ||
61 | + | ||
62 | +//Preapation format final | ||
63 | + w.setAudioFormat(1); | ||
64 | + w.setNbrChanel(1); | ||
65 | + w.setFrequency(FREQ); | ||
66 | + w.setBytePerBloc(4); | ||
67 | + w.setBytePerSec(FREQ); | ||
68 | + w.setBitsPerSample(8); | ||
69 | + w.clearData(); | ||
70 | + | ||
71 | + w.setDatasize(SIZE); | ||
72 | + w.setData(data2); | ||
73 | + w.save("lastrech.WAV"); | ||
74 | + | ||
75 | + | ||
76 | +} | ||
0 | \ No newline at end of file | 77 | \ No newline at end of file |
No preview for this file type
modifreq.cpp renamed to strech/modifreq.cpp
@@ -36,22 +36,28 @@ int main(int argc, char **argv) | @@ -36,22 +36,28 @@ int main(int argc, char **argv) | ||
36 | 36 | ||
37 | //FFT | 37 | //FFT |
38 | fft(32768*4,test,test2); | 38 | fft(32768*4,test,test2); |
39 | - | 39 | + |
40 | //Modification dans le domaine fréquentiel | 40 | //Modification dans le domaine fréquentiel |
41 | for(i=0;i<SIZE;i+=2){ | 41 | for(i=0;i<SIZE;i+=2){ |
42 | test3[i][0]=test2[i][0]; | 42 | test3[i][0]=test2[i][0]; |
43 | - printf("%lf ",test2[i][0]); | 43 | + //printf("%lf ",test2[i][0]); |
44 | test3[i+1][0]=test2[i][0]; | 44 | test3[i+1][0]=test2[i][0]; |
45 | test3[i][1]=0; | 45 | test3[i][1]=0; |
46 | test3[i+1][1]=0; | 46 | test3[i+1][1]=0; |
47 | } | 47 | } |
48 | - | 48 | + |
49 | //FFT inverse | 49 | //FFT inverse |
50 | ifft(32768*4,test1,test3); | 50 | ifft(32768*4,test1,test3); |
51 | + | ||
51 | //Récupération des données | 52 | //Récupération des données |
53 | + float moy=0.0; | ||
54 | + for(i=0;i<SIZE;i+=2){ | ||
55 | + moy=+test3[i][0]; | ||
56 | + } | ||
52 | 57 | ||
58 | + printf("%f ",moy); | ||
53 | for(i=0;i<SIZE;i++){ | 59 | for(i=0;i<SIZE;i++){ |
54 | - data2[i]=test1[i][0]; | 60 | + data2[i]=test1[i][0]*5+50; |
55 | } | 61 | } |
56 | //Preapation format final | 62 | //Preapation format final |
57 | w.setAudioFormat(1); | 63 | w.setAudioFormat(1); |
No preview for this file type
No preview for this file type
@@ -0,0 +1,76 @@ | @@ -0,0 +1,76 @@ | ||
1 | +#include <iostream> | ||
2 | +#include "wavdata.h" | ||
3 | +#include "fft.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | + | ||
7 | +#define FREQ 22400 | ||
8 | +#define AMPLITUDE 10 | ||
9 | +#define FREQDO 261 | ||
10 | +#define FREQLA 440 | ||
11 | +#define SIZE FREQ*2 | ||
12 | + | ||
13 | + | ||
14 | +int main(int argc, char **argv) | ||
15 | +{ | ||
16 | +//Definition variables | ||
17 | + int i; | ||
18 | + float pi=3.141592; | ||
19 | + WavData w; | ||
20 | + w.load("COW.WAV"); | ||
21 | + char *data = w.data(); | ||
22 | + double test[w.datasize()*2][2]; | ||
23 | + double test1[w.datasize()*2][2]; | ||
24 | + double test2[w.datasize()*2][2]; | ||
25 | + double test3[w.datasize()*2][2]; | ||
26 | + | ||
27 | + | ||
28 | + printf("===Création data\n"); | ||
29 | +//Creation de la donnée | ||
30 | + char *data2 = new char[w.datasize()*2]; | ||
31 | + printf("===load success\n"); | ||
32 | + | ||
33 | + for(i=0;i<w.datasize();i++){ | ||
34 | + test[i][0]=(double)data[i]; | ||
35 | + test[i][1]=0; | ||
36 | + } | ||
37 | + | ||
38 | +//FFT | ||
39 | + printf("===fft\n"); | ||
40 | + fft(32768*2,test,test2); | ||
41 | + | ||
42 | +//Modification dans le domaine fréquentiel | ||
43 | + printf("===modif freq\n"); | ||
44 | + for(i=1;i<w.datasize()+1;i++){ | ||
45 | + int x=i-1; | ||
46 | + int y=(2*i)-2; | ||
47 | + | ||
48 | + test3[2*x][0]=test2[x][0]; | ||
49 | + test3[2*x+1][0]=test2[x][0]; | ||
50 | + test3[2*x][1]=0; | ||
51 | + test3[2*x+1][1]=0; | ||
52 | + } | ||
53 | + | ||
54 | +//FFT inverse | ||
55 | + printf("===ifft\n"); | ||
56 | + ifft(32768*2,test1,test3); | ||
57 | + | ||
58 | +//Récupération des données | ||
59 | + for(i=0;i<w.datasize();i++){ | ||
60 | + data2[i]=test1[i][0]; | ||
61 | + } | ||
62 | + | ||
63 | +//Preapation format final | ||
64 | + w.setAudioFormat(1); | ||
65 | + w.setNbrChanel(1); | ||
66 | + w.setFrequency(FREQ); | ||
67 | + w.setBytePerBloc(4); | ||
68 | + w.setBytePerSec(FREQ); | ||
69 | + w.setBitsPerSample(8); | ||
70 | + w.clearData(); | ||
71 | + | ||
72 | + w.setDatasize(w.datasize()); | ||
73 | + w.setData(data2); | ||
74 | + w.save("strechcow.WAV"); | ||
75 | + | ||
76 | +} | ||
0 | \ No newline at end of file | 77 | \ No newline at end of file |
@@ -0,0 +1,269 @@ | @@ -0,0 +1,269 @@ | ||
1 | +#include "wavdata.h" | ||
2 | +#include <iostream> | ||
3 | + | ||
4 | + | ||
5 | +using namespace std; | ||
6 | + | ||
7 | +WavData::WavData() | ||
8 | +{ | ||
9 | + _data=NULL; | ||
10 | +} | ||
11 | + | ||
12 | +int WavData::error(int i,int line) | ||
13 | +{ | ||
14 | + std::cerr << "Erreur "<<__FILE__<<" L"<<line<< ": "; | ||
15 | + switch(i) | ||
16 | + { | ||
17 | + case -1: std::cerr << "ouverture du fichier"; | ||
18 | + break; | ||
19 | + case -2: std::cerr << "le fichier n'est pas au format WAVE"; | ||
20 | + break; | ||
21 | + default: std::cerr << "inconnue"; | ||
22 | + break; | ||
23 | + } | ||
24 | + | ||
25 | + std::cerr << "\n"; | ||
26 | + return i; | ||
27 | +} | ||
28 | + | ||
29 | +unsigned int WavData::fromLittleEndian(char * content, int size) | ||
30 | +{ | ||
31 | + unsigned int result = 0; | ||
32 | + for(int i=size;i>=0;i--) | ||
33 | + { | ||
34 | + result *= 256; | ||
35 | + result += (unsigned char)content[i]; | ||
36 | + } | ||
37 | + return result; | ||
38 | +} | ||
39 | + | ||
40 | +char * WavData::toLittleEndian(unsigned int content, int size) | ||
41 | +{ | ||
42 | + char * result= new char [size*sizeof(char)]; | ||
43 | + | ||
44 | + for(int i=0;i<size;i++) | ||
45 | + { | ||
46 | + result += (char)(content%256); | ||
47 | + content >>= 8; | ||
48 | + } | ||
49 | + | ||
50 | + return result; | ||
51 | +} | ||
52 | + | ||
53 | +void WavData::toLittleEndian(char* result,unsigned int content, int size) | ||
54 | +{ | ||
55 | + for(int i=0;i<size;i++) | ||
56 | + { | ||
57 | + result[i] = (char)(content%256); | ||
58 | + content >>= 8; | ||
59 | + } | ||
60 | + | ||
61 | + return; | ||
62 | +} | ||
63 | + | ||
64 | +char * WavData::read(ifstream* file,unsigned int size) | ||
65 | +{ | ||
66 | + char * c = new char[size+1]; | ||
67 | + for(int i=0;i<size;i++) | ||
68 | + { | ||
69 | + c[i] = file->get(); | ||
70 | + } | ||
71 | + c[size]='\0'; | ||
72 | + return c; | ||
73 | +} | ||
74 | + | ||
75 | +bool WavData::comp(char* c1, char *c2, int size) | ||
76 | +{ | ||
77 | + for(int i=0;i<size;i++) | ||
78 | + { | ||
79 | + if(c1[i]!=c2[i])return false; | ||
80 | + } | ||
81 | + return true; | ||
82 | +} | ||
83 | + | ||
84 | +int WavData::testString(ifstream* file,char*compstr,int size,char *mess) | ||
85 | +{ | ||
86 | + char * content = read(file,size); | ||
87 | + std::cout << "| "<< mess << std::endl<<"| ->"<< content << std::endl << "|"<<std::endl; | ||
88 | + if(!comp(content,compstr,size))return error(-2,__LINE__); | ||
89 | + | ||
90 | + delete[] content; | ||
91 | + return 1; | ||
92 | +} | ||
93 | + | ||
94 | +int WavData::testInt(ifstream* file,int size,char *mess,unsigned int *val) | ||
95 | +{ | ||
96 | + int test; | ||
97 | + char * content = read(file,size); | ||
98 | + | ||
99 | + int value=fromLittleEndian(content, size); | ||
100 | + std::cout << "| "<< mess << std::endl<<"| ->"<<value << std::endl<<"|" <<std::endl; | ||
101 | + if(val!=NULL)*val=value; | ||
102 | + delete[] content; | ||
103 | + return 1; | ||
104 | +} | ||
105 | + | ||
106 | +int WavData::openFormatBloc(ifstream* file) | ||
107 | +{ | ||
108 | + std::cout << "[Bloc de declaration d'un fichier au format WAVE]"<<std::endl<<"|"<<std::endl; | ||
109 | + | ||
110 | + // FileTypeBlocId | ||
111 | + int r; | ||
112 | + r = testString(file,"RIFF",4, "FileTypeBlocID (4 octets) : Constante 'RIFF'"); | ||
113 | + if(r<=0)return r; | ||
114 | + | ||
115 | + // FileSize | ||
116 | + r = testInt(file,4, "FileSize (4 octets) : Taille du fichier moins 8 octets"); | ||
117 | + if(r<=0)return r; | ||
118 | + | ||
119 | + // FileFormatID | ||
120 | + r = testString(file,"WAVE",4, "FileFormatID (4 octets) : Format = 'WAVE'"); | ||
121 | + if(r<=0)return r; | ||
122 | + | ||
123 | + std::cout << std::endl; | ||
124 | + return 1; | ||
125 | +} | ||
126 | + | ||
127 | +int WavData::openDescriptionBloc(ifstream* file) | ||
128 | +{ | ||
129 | + std::cout << "[Bloc decrivant le format audio]"<<std::endl<<"|"<<std::endl; | ||
130 | + | ||
131 | + int r; | ||
132 | + | ||
133 | + // FormatBlocID | ||
134 | + r = testString(file,"fmt ",4, "FormatBlocID (4 octets) : Identifiant 'fmt'"); | ||
135 | + if(r<=0)return r; | ||
136 | + | ||
137 | + // BlocSize | ||
138 | + r = testInt(file,4,"BlocSize (4 octets) : Nombre d'octets du bloc - 8 (attendue: 16)"); | ||
139 | + if(r<=0)return r; | ||
140 | + | ||
141 | + // AudioFormat | ||
142 | + r = testInt(file,2,"AudioFormat (2 octets) : Format du stockage dans le fichier (1: PCM, ...)",&_audioFormat); | ||
143 | + if(r<=0)return r; | ||
144 | + | ||
145 | + // NbrCanaux | ||
146 | + r = testInt(file,2,"NbrCanaux (2 octets) : Nombre de canaux (de 1 a 6)",&_nbrChanel); | ||
147 | + if(r<=0)return r; | ||
148 | + | ||
149 | + // Frequency | ||
150 | + r = testInt(file,4,"Frequence (4 octets) : Frequence d'echantillonnage (en Hertz)",&_frequency); | ||
151 | + if(r<=0)return r; | ||
152 | + | ||
153 | + // BytePerSec | ||
154 | + r = testInt(file,4,"BytePerSec (4 octets) : Nombre d'octets a lire par seconde",&_bytePerSec); | ||
155 | + if(r<=0)return r; | ||
156 | + | ||
157 | + // BytePerBloc | ||
158 | + r = testInt(file,2,"BytePerBloc (2 octets) : Nombre d'octets par bloc d'echantillonnage",&_bytePerBloc); | ||
159 | + if(r<=0)return r; | ||
160 | + | ||
161 | + // BitsPerSample | ||
162 | + r = testInt(file,2,"BitsPerSample (2 octets) : Nombre de bits pour le codage d'un echantillon",&_bitsPerSample); | ||
163 | + if(r<=0)return r; | ||
164 | + | ||
165 | + return 1; | ||
166 | +} | ||
167 | + | ||
168 | +int WavData::openDataBloc(ifstream* file) | ||
169 | +{ | ||
170 | + std::cout << "[Bloc des donnees]\n"; | ||
171 | + | ||
172 | + int r,test; | ||
173 | + // DataBlocID | ||
174 | + r = testString(file,"data",4,"DataBlocID (4 octets) : Constante 'data'"); | ||
175 | + if(r<=0)return r; | ||
176 | + | ||
177 | + // DataSize | ||
178 | + r = testInt(file,3,"DataSize (4 octets) : Nombre d'octets des donnees",&_datasize); | ||
179 | + if(r<=0)return r; | ||
180 | + | ||
181 | + // Data[]; | ||
182 | + std::cout << "| Data[] : [Octets du Sample 1 du Canal 1] [Octets du Sample 1 du Canal 2] [Octets du Sample 2 du Canal 1] [Octets du Sample 2 du Canal 2]\n\n"; | ||
183 | + if(_data!=NULL)delete[] _data; | ||
184 | + _data = read(file,_datasize); | ||
185 | + | ||
186 | + return 1; | ||
187 | +} | ||
188 | + | ||
189 | +int WavData::load(char * s) | ||
190 | +{ | ||
191 | + std::cout << "Load file: \"" << s << "\""<<std::endl<<std::endl; | ||
192 | + | ||
193 | + ifstream file(s, ios::in | ios::binary ); | ||
194 | + if (!file)return error(-1,__LINE__); | ||
195 | + | ||
196 | + if(openFormatBloc(&file) != 1) {file.close(); return 0;} | ||
197 | + if(openDescriptionBloc(&file) != 1) {file.close(); return 0;} | ||
198 | + if(openDataBloc(&file) != 1) {file.close(); return 0;} | ||
199 | + | ||
200 | + std::cout << "close file"<< std::endl; | ||
201 | + file.close(); | ||
202 | + std::cout << "file closed"<<std::endl; | ||
203 | + | ||
204 | + return 1; | ||
205 | +} | ||
206 | + | ||
207 | +void WavData::write(ofstream *file,char* mess,unsigned int size) | ||
208 | +{ | ||
209 | + for(int i=0;i<size;i++) | ||
210 | + { | ||
211 | + file->put(mess[i]); | ||
212 | + } | ||
213 | +} | ||
214 | + | ||
215 | +int WavData::saveFormatBloc(ofstream* file) | ||
216 | +{ | ||
217 | + char b[13] = "RIFF WAVE"; | ||
218 | + | ||
219 | + toLittleEndian(b+4,_datasize+36,4); | ||
220 | + | ||
221 | + write(file,b,12); | ||
222 | + | ||
223 | + return 1; | ||
224 | +} | ||
225 | + | ||
226 | +int WavData::saveDescriptionBloc(ofstream* file) | ||
227 | +{ | ||
228 | + char b[25] = "fmt "; | ||
229 | + | ||
230 | + toLittleEndian(b+4,0x10,4); | ||
231 | + std::cout<<_audioFormat<<std::endl; | ||
232 | + toLittleEndian(b+8,_audioFormat,2); | ||
233 | + toLittleEndian(b+10,_nbrChanel,2); | ||
234 | + toLittleEndian(b+12,_frequency,4); | ||
235 | + toLittleEndian(b+16,_bytePerSec,4); | ||
236 | + toLittleEndian(b+20,_bytePerBloc,2); | ||
237 | + toLittleEndian(b+22,_bitsPerSample,2); | ||
238 | + | ||
239 | + write(file,b,24); | ||
240 | + | ||
241 | + return 1; | ||
242 | +} | ||
243 | +int WavData::saveDataBloc(ofstream* file) | ||
244 | +{ | ||
245 | + char b[9] = "data "; | ||
246 | + toLittleEndian(b+4,_datasize,4); | ||
247 | + | ||
248 | + write(file,b,8); | ||
249 | + | ||
250 | + write(file,_data,_datasize); | ||
251 | + | ||
252 | + return 1; | ||
253 | +} | ||
254 | + | ||
255 | +int WavData::save(char * s) | ||
256 | +{ | ||
257 | + std::cout << "Save file: \"" << s << "\"\n\n"; | ||
258 | + | ||
259 | + ofstream file(s, ios::out | ios::trunc | ios::binary); | ||
260 | + if (!file)return error(-1,__LINE__); | ||
261 | + | ||
262 | + saveFormatBloc(&file); | ||
263 | + saveDescriptionBloc(&file); | ||
264 | + saveDataBloc(&file); | ||
265 | + | ||
266 | + file.close(); | ||
267 | + | ||
268 | + return 1; | ||
269 | +} |
@@ -0,0 +1,66 @@ | @@ -0,0 +1,66 @@ | ||
1 | +#ifndef WAVDATA_H | ||
2 | +#define WAVDATA_H | ||
3 | + | ||
4 | +#include <iostream> | ||
5 | +#include <fstream> | ||
6 | + | ||
7 | +class WavData | ||
8 | +{ | ||
9 | + unsigned int _audioFormat; | ||
10 | + unsigned int _nbrChanel; | ||
11 | + unsigned int _frequency; | ||
12 | + unsigned int _bytePerBloc; | ||
13 | + unsigned int _bytePerSec; | ||
14 | + unsigned int _bitsPerSample; | ||
15 | + | ||
16 | + char * _data; | ||
17 | + unsigned int _datasize; | ||
18 | +public: | ||
19 | + WavData(); | ||
20 | + | ||
21 | + int load(char* s); | ||
22 | + int save(char* s); | ||
23 | + | ||
24 | + unsigned int fromLittleEndian(char * content, int size); | ||
25 | + char * toLittleEndian(unsigned int content, int size); | ||
26 | + void toLittleEndian(char* result,unsigned int content, int size); | ||
27 | + | ||
28 | + inline unsigned int audioFormat(){return _audioFormat;} | ||
29 | + inline unsigned int nbrChanel(){return _nbrChanel;} | ||
30 | + inline unsigned int frequency(){return _frequency;} | ||
31 | + inline unsigned int bytePerBloc(){return _bytePerBloc;} | ||
32 | + inline unsigned int bytePerSec(){return _bytePerSec;} | ||
33 | + inline unsigned int bitsPerSample(){return _bitsPerSample;} | ||
34 | + inline unsigned int datasize(){return _datasize;} | ||
35 | + inline char * data(){return _data;} | ||
36 | + | ||
37 | + inline void setAudioFormat(unsigned int a){_audioFormat=a;} | ||
38 | + inline void setNbrChanel(unsigned int a){_nbrChanel=a;} | ||
39 | + inline void setFrequency(unsigned int a){_frequency=a;} | ||
40 | + inline void setBytePerBloc(unsigned int a){_bytePerBloc=a;} | ||
41 | + inline void setBytePerSec(unsigned int a){_bytePerSec=a;} | ||
42 | + inline void setBitsPerSample(unsigned int a){_bitsPerSample=a;} | ||
43 | + inline void setData(char * a){_data=a;} | ||
44 | + inline void clearData(){delete[] _data;_data=NULL;} | ||
45 | + inline void setDatasize(unsigned int i){_datasize=i;} | ||
46 | + | ||
47 | +private: | ||
48 | + int error(int,int); | ||
49 | + int openFormatBloc(std::ifstream* file); | ||
50 | + int openDescriptionBloc(std::ifstream *file); | ||
51 | + int openDataBloc(std::ifstream *file); | ||
52 | + | ||
53 | + int saveFormatBloc(std::ofstream *file); | ||
54 | + int saveDescriptionBloc(std::ofstream *file); | ||
55 | + int saveDataBloc(std::ofstream *file); | ||
56 | + | ||
57 | + void write(std::ofstream *file,char* mess,unsigned int size); | ||
58 | + char * read(std::ifstream* file,unsigned int size); | ||
59 | + | ||
60 | + bool comp(char* c1, char *c2, int size); | ||
61 | + int testString(std::ifstream* file,char*compstr,int size,char *mess); | ||
62 | + int testInt(std::ifstream* file,int size,char *mess,unsigned int *val=NULL); | ||
63 | + | ||
64 | +}; | ||
65 | + | ||
66 | +#endif // WAVDATA_H |
test deleted
No preview for this file type
test.WAV deleted
No preview for this file type
test.cpp deleted
@@ -1,107 +0,0 @@ | @@ -1,107 +0,0 @@ | ||
1 | -#include <iostream> | ||
2 | -#include "wavdata.h" | ||
3 | -#include "fft.h" | ||
4 | -#include <math.h> | ||
5 | - | ||
6 | -#define FREQ 48000 | ||
7 | -#define AMPLITUDE 10 | ||
8 | -#define FREQDO 261 | ||
9 | -#define FREQLA 440 | ||
10 | -#define SIZE FREQ*1 | ||
11 | - | ||
12 | - | ||
13 | -int main(int argc, char **argv) | ||
14 | -{ | ||
15 | - WavData w; | ||
16 | - int pi=3.14; | ||
17 | - char *data = new char[SIZE]; | ||
18 | - | ||
19 | - | ||
20 | - WavData w1; | ||
21 | - w1.load("COW.WAV"); | ||
22 | - char *dat = w1.data(); | ||
23 | - | ||
24 | - int i; | ||
25 | - for(i=0;i<SIZE;i++){ | ||
26 | - data[i]=AMPLITUDE*(sin(FREQLA*pi*2*i)+1); | ||
27 | - } | ||
28 | - | ||
29 | - w.setAudioFormat(1); | ||
30 | - w.setNbrChanel(1); | ||
31 | - w.setFrequency(FREQ); | ||
32 | - w.setBytePerBloc(4); | ||
33 | - w.setBytePerSec(FREQ); | ||
34 | - w.setBitsPerSample(8); | ||
35 | - w.clearData(); | ||
36 | - w.setDatasize(SIZE); | ||
37 | - w.setData(data); | ||
38 | - w.save("pur.WAV"); | ||
39 | - | ||
40 | -//----------------------------------------------------------------------------------------------// | ||
41 | - | ||
42 | - double test[SIZE][2]; | ||
43 | - double test1[SIZE][2]; | ||
44 | - double test2[SIZE][2]; | ||
45 | - double test3[SIZE*2][2]; | ||
46 | - double test4[SIZE][2]; | ||
47 | - | ||
48 | - for(i=0;i<SIZE;i++){ | ||
49 | - test[i][0]=AMPLITUDE*(sin(FREQLA*pi*2*i)+1); | ||
50 | - test[i][1]=0; | ||
51 | - } | ||
52 | - | ||
53 | - fft(512,test,test2); | ||
54 | - | ||
55 | - for(i=0;i<SIZE;i++){ | ||
56 | - test3[i*2][0]=test2[i][0]; | ||
57 | - test3[(i*2)+1][0]=test2[i][0]; | ||
58 | - test3[i][1]=0; | ||
59 | - } | ||
60 | - | ||
61 | - | ||
62 | - ifft(512,test1,test3); | ||
63 | - | ||
64 | - char *data2 = new char[SIZE*2]; | ||
65 | - for(i=0;i<SIZE*2;i++){ | ||
66 | - data2[i]=test1[i][0]; | ||
67 | - } | ||
68 | - | ||
69 | - w.clearData(); | ||
70 | - w.setDatasize(SIZE*2); | ||
71 | - w.setData(data2); | ||
72 | - w.save("test.WAV"); | ||
73 | - | ||
74 | -//----------------------------------------------------------------------------------------------// | ||
75 | - | ||
76 | - double test11[SIZE][2]; | ||
77 | - char *data22 = new char[SIZE]; | ||
78 | - | ||
79 | - for(i=0;i<SIZE;i++){ | ||
80 | - test4[i][0]=test2[i][0]; | ||
81 | - test4[i][1]=0; | ||
82 | - printf("%d ",test1[i][0]); | ||
83 | - } | ||
84 | - | ||
85 | - ifft(512,test11,test4); | ||
86 | - | ||
87 | - for(i=0;i<SIZE;i++){ | ||
88 | - data22[i]=test11[i][0]; | ||
89 | - } | ||
90 | - | ||
91 | - w.clearData(); | ||
92 | - w.setDatasize(SIZE); | ||
93 | - w.setData(data22); | ||
94 | - w.save("test2.WAV"); | ||
95 | -} | ||
96 | - | ||
97 | -/* | ||
98 | - for(i=0;i<w.datasize();i++){ | ||
99 | - printf("%d ",data[i]); | ||
100 | - } | ||
101 | - printf("\n\n"); | ||
102 | - | ||
103 | - for(i=0;i<100;i++){ | ||
104 | - | ||
105 | - printf("%f %f i=%d, ",test2[i][1],test2[i][2],i); | ||
106 | - | ||
107 | - }*/ | ||
108 | \ No newline at end of file | 0 | \ No newline at end of file |
test2.WAV deleted
No preview for this file type
testWAV.pro deleted
@@ -1,12 +0,0 @@ | @@ -1,12 +0,0 @@ | ||
1 | -###################################################################### | ||
2 | -# Automatically generated by qmake (2.01a) lun. mai 9 11:16:11 2011 | ||
3 | -###################################################################### | ||
4 | - | ||
5 | -TEMPLATE = app | ||
6 | -TARGET = | ||
7 | -DEPENDPATH += . | ||
8 | -INCLUDEPATH += . | ||
9 | - | ||
10 | -# Input | ||
11 | -HEADERS += fft.h wavdata.h | ||
12 | -SOURCES += fft.cpp main.cpp wavdata.cpp |