Commit 6c227d28a0e93eb8cfb3d0517fcd5e8d0be12f1c
1 parent
77461c77
premier push
Showing
25 changed files
with
830 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +{ | ||
2 | + "configurations": [ | ||
3 | + { | ||
4 | + "name": "Win32", | ||
5 | + "includePath": [ | ||
6 | + "${workspaceFolder}/**", | ||
7 | + "C:\\Users\\simon\\OneDrive\\Bureau\\TP6son" | ||
8 | + ], | ||
9 | + "defines": [ | ||
10 | + "_DEBUG", | ||
11 | + "UNICODE", | ||
12 | + "_UNICODE" | ||
13 | + ], | ||
14 | + "cStandard": "c17", | ||
15 | + "cppStandard": "c++17", | ||
16 | + "intelliSenseMode": "msvc-x64" | ||
17 | + } | ||
18 | + ], | ||
19 | + "version": 4 | ||
20 | +} | ||
0 | \ No newline at end of file | 21 | \ No newline at end of file |
@@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
1 | +{ | ||
2 | + // Use IntelliSense to learn about possible attributes. | ||
3 | + // Hover to view descriptions of existing attributes. | ||
4 | + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
5 | + "version": "0.2.0", | ||
6 | + "configurations": [ | ||
7 | + { | ||
8 | + "name": "(Windows) Lancer", | ||
9 | + "type": "cppvsdbg", | ||
10 | + "request": "launch", | ||
11 | + "program": "entrer le nom du programme, par exemple ${workspaceFolder}/a.exe", | ||
12 | + "args": [], | ||
13 | + "stopAtEntry": false, | ||
14 | + "cwd": "${workspaceFolder}", | ||
15 | + "environment": [], | ||
16 | + "externalConsole": false | ||
17 | + } | ||
18 | + ] | ||
19 | +} | ||
0 | \ No newline at end of file | 20 | \ No newline at end of file |
No preview for this file type
No preview for this file type
No preview for this file type
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 |
@@ -0,0 +1,57 @@ | @@ -0,0 +1,57 @@ | ||
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*4 | ||
12 | + | ||
13 | + | ||
14 | +int main(int argc, char **argv) | ||
15 | +{ | ||
16 | + float pi=3.141592; | ||
17 | + WavData w; | ||
18 | + | ||
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 | + | ||
33 | + test[i][0]=(double)data[i]; | ||
34 | + test[i][1]=(double)data[i]; | ||
35 | + } | ||
36 | + | ||
37 | + fft(32768*2,test,test2); | ||
38 | + | ||
39 | + ifft(32768*2,test1,test2); | ||
40 | + | ||
41 | + for(i=0;i<SIZE;i++){ | ||
42 | + data2[i]=test1[i][0]; | ||
43 | + } | ||
44 | + | ||
45 | + w.setAudioFormat(1); | ||
46 | + w.setNbrChanel(1); | ||
47 | + w.setFrequency(FREQ); | ||
48 | + w.setBytePerBloc(4); | ||
49 | + w.setBytePerSec(FREQ); | ||
50 | + w.setBitsPerSample(8); | ||
51 | + w.clearData(); | ||
52 | + | ||
53 | + w.setDatasize(SIZE); | ||
54 | + w.setData(data2); | ||
55 | + w.save("cowfft.WAV"); | ||
56 | + | ||
57 | +} | ||
0 | \ No newline at end of file | 58 | \ No newline at end of file |
@@ -0,0 +1,40 @@ | @@ -0,0 +1,40 @@ | ||
1 | +#include <iostream> | ||
2 | +#include "wavdata.h" | ||
3 | +#include "fft.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | +#define DELAY 5000 | ||
7 | +#define AMPLITUDE 0.5 | ||
8 | + | ||
9 | +int main(int argc, char **argv) | ||
10 | +{ | ||
11 | + WavData w; | ||
12 | + w.load("COW.WAV"); | ||
13 | + | ||
14 | + char *data = w.data(); | ||
15 | + char *data2 = new char[w.datasize()*2]; | ||
16 | + | ||
17 | + int i; | ||
18 | + for(i=0;i<w.datasize();i++){ | ||
19 | + data2[i]=data[i]; | ||
20 | + printf("%d ",data[i]);} | ||
21 | + for(;i<w.datasize()*2;i++) | ||
22 | + data2[i]=128; | ||
23 | + | ||
24 | + for(int i=DELAY;i<2*w.datasize();i++) | ||
25 | + { | ||
26 | + float value = (float)(unsigned char)data2[i-DELAY]-128.0; | ||
27 | + value = value * AMPLITUDE; | ||
28 | + int val = (unsigned int)value + (unsigned char)data2[i]; | ||
29 | + if(val>255)val=255; | ||
30 | + if(val<0)val=0; | ||
31 | + | ||
32 | + data2[i]= (unsigned char)(unsigned int)val; | ||
33 | + } | ||
34 | + | ||
35 | + w.clearData(); | ||
36 | + w.setDatasize(w.datasize()*2); | ||
37 | + w.setData(data2); | ||
38 | + | ||
39 | + w.save("COW_d.WAV"); | ||
40 | +} |
No preview for this file type
@@ -0,0 +1,69 @@ | @@ -0,0 +1,69 @@ | ||
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*4 | ||
12 | + | ||
13 | + | ||
14 | +int main(int argc, char **argv) | ||
15 | +{ | ||
16 | +//Definition variables | ||
17 | + int i; | ||
18 | + float pi=3.141592; | ||
19 | + double test[SIZE][2]; | ||
20 | + double test1[SIZE][2]; | ||
21 | + double test2[SIZE][2]; | ||
22 | + double test3[SIZE*2][2]; | ||
23 | + char *data = new char[SIZE]; | ||
24 | + char *data2 = new char[SIZE]; | ||
25 | + WavData w; | ||
26 | + | ||
27 | +//Creation de la donnée | ||
28 | + for(i=0;i<SIZE;i++){ | ||
29 | + float w=2.0*3.14*FREQLA; | ||
30 | + float t=(float)i/FREQ; | ||
31 | + data[i]=AMPLITUDE*(1+sin(w*t)); | ||
32 | + | ||
33 | + test[i][0]=(double)data[i]; | ||
34 | + test[i][1]=(double)data[i]; | ||
35 | + } | ||
36 | + | ||
37 | +//FFT | ||
38 | + fft(32768*4,test,test2); | ||
39 | + | ||
40 | +//Modification dans le domaine fréquentiel | ||
41 | + for(i=0;i<SIZE;i+=2){ | ||
42 | + test3[i][0]=test2[i][0]; | ||
43 | + printf("%lf ",test2[i][0]); | ||
44 | + test3[i+1][0]=test2[i][0]; | ||
45 | + test3[i][1]=0; | ||
46 | + test3[i+1][1]=0; | ||
47 | + } | ||
48 | + | ||
49 | +//FFT inverse | ||
50 | + ifft(32768*4,test1,test3); | ||
51 | +//Récupération des données | ||
52 | + | ||
53 | + for(i=0;i<SIZE;i++){ | ||
54 | + data2[i]=test1[i][0]; | ||
55 | + } | ||
56 | +//Preapation format final | ||
57 | + w.setAudioFormat(1); | ||
58 | + w.setNbrChanel(1); | ||
59 | + w.setFrequency(FREQ); | ||
60 | + w.setBytePerBloc(4); | ||
61 | + w.setBytePerSec(FREQ); | ||
62 | + w.setBitsPerSample(8); | ||
63 | + w.clearData(); | ||
64 | + | ||
65 | + w.setDatasize(SIZE); | ||
66 | + w.setData(data2); | ||
67 | + w.save("modifreq.WAV"); | ||
68 | + | ||
69 | +} | ||
0 | \ No newline at end of file | 70 | \ No newline at end of file |
No preview for this file type
@@ -0,0 +1,42 @@ | @@ -0,0 +1,42 @@ | ||
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*10 | ||
11 | + | ||
12 | + | ||
13 | +int main(int argc, char **argv) | ||
14 | +{ | ||
15 | + float pi=3.141592; | ||
16 | + WavData w; | ||
17 | + | ||
18 | + char *data = new char[SIZE]; | ||
19 | + char *data2 = new char[SIZE]; | ||
20 | + | ||
21 | + int i; | ||
22 | + for(i=0;i<SIZE;i++){ | ||
23 | + float w=2.0*3.14*FREQLA; | ||
24 | + float t=(float)i/FREQ; | ||
25 | + | ||
26 | + data[i]=AMPLITUDE*(1+sin(w*t)); | ||
27 | + } | ||
28 | + | ||
29 | + | ||
30 | + w.setAudioFormat(1); | ||
31 | + w.setNbrChanel(1); | ||
32 | + w.setFrequency(FREQ); | ||
33 | + w.setBytePerBloc(4); | ||
34 | + w.setBytePerSec(FREQ); | ||
35 | + w.setBitsPerSample(8); | ||
36 | + w.clearData(); | ||
37 | + | ||
38 | + w.setDatasize(SIZE); | ||
39 | + w.setData(data); | ||
40 | + w.save("purla.WAV"); | ||
41 | + | ||
42 | +} | ||
0 | \ No newline at end of file | 43 | \ No newline at end of file |
No preview for this file type
No preview for this file type
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +Dans un premier temps, la description du fichier, puis le format audio, échantillonnage, fréquence et le boc de données | ||
2 | +Le code a pour but d’allonger la durée de l’audio. En ajoutant un effet délay à partir d’un moment de l’audio. | ||
3 | +On a fait un test avec la valeur 128 et pas de son en sortie du fichier | ||
4 | + | ||
5 | + | ||
6 | +Multiplier par 2 le temps | ||
7 | +Diviser | ||
8 | +1 echantillion devient 2 | ||
9 | +Dans l’espace frequentielle | ||
10 | +FFT Deux dimensions, double le temps FFTinverse | ||
11 | + | ||
12 | +Son pur | ||
13 | +Dans cette partie, nous nous sommes basés sur la construction du fichier audio donné dans le main.cpp. | ||
14 | +C’est à dire que nous chargions le son cow afin de récupérer ses paramètres, puis en cherchant un peu nous avons trouvé les fonctions, dans le header de la bibliothèque, pour fixer les paramètres de notre format audio en sortie. | ||
15 | +Nous avons commencé à construire notre signal sinusoïdale à l’aide de la fonction sinus dans math.h et de notre fréquence |
No preview for this file type
No preview for this file type
@@ -0,0 +1,107 @@ | @@ -0,0 +1,107 @@ | ||
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 | + }*/ | ||
0 | \ No newline at end of file | 108 | \ No newline at end of file |
No preview for this file type
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
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 |
@@ -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 |