Blame view

pur/wavdata.cpp 5.81 KB
6a961b68   rsimonin   modifications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  #include "wavdata.h"
  #include <iostream>
  
  
  using namespace std;
  
  WavData::WavData()
  {
  	_data=NULL;
  }
  
  int WavData::error(int i,int line)
  {
  	std::cerr << "Erreur "<<__FILE__<<" L"<<line<< ": ";
  	switch(i)
  	{
  	case -1: std::cerr << "ouverture du fichier";
  		break;
  	case -2: std::cerr << "le fichier n'est pas au format WAVE";
  		break;
  	default: std::cerr << "inconnue";
  		break;
  	}
  
  	std::cerr << "\n";
  	return i;
  }
  
  unsigned int WavData::fromLittleEndian(char * content, int size)
  {
  	unsigned int result = 0;
  	for(int i=size;i>=0;i--)
  	{
  		result *= 256;
  		result += (unsigned char)content[i];
  	}
  	return result;
  }
  
  char * WavData::toLittleEndian(unsigned int content, int size)
  {
  	char * result= new char [size*sizeof(char)];
  
  	for(int i=0;i<size;i++)
  	{
  		result += (char)(content%256);
  		content >>= 8;
  	}
  
  	return result;
  }
  
  void WavData::toLittleEndian(char* result,unsigned int content, int size)
  {
  	for(int i=0;i<size;i++)
  	{
  		result[i] = (char)(content%256);
  		content >>= 8;
  	}
  
  	return;
  }
  
  char * WavData::read(ifstream* file,unsigned int size)
  {
  	char * c = new char[size+1];
  	for(int i=0;i<size;i++)
  	{
  		c[i] = file->get();
  	}
  	c[size]='\0';
  	return c;
  }
  
  bool WavData::comp(char* c1, char *c2, int size)
  {
  	for(int i=0;i<size;i++)
  	{
  		if(c1[i]!=c2[i])return false;
  	}
  	return true;
  }
  
  int WavData::testString(ifstream* file,char*compstr,int size,char *mess)
  {
  	char * content = read(file,size);
  	std::cout << "| "<< mess << std::endl<<"|    ->"<< content << std::endl << "|"<<std::endl;
  	if(!comp(content,compstr,size))return error(-2,__LINE__);
  	
  	delete[] content;
  	return 1;
  }
  
  int WavData::testInt(ifstream* file,int size,char *mess,unsigned int *val)
  {
  	int test;
  	char * content = read(file,size);
  	
  	int value=fromLittleEndian(content, size);
  	std::cout << "| "<< mess << std::endl<<"|    ->"<<value << std::endl<<"|" <<std::endl;
  	if(val!=NULL)*val=value;
  	delete[] content;
  	return 1;
  }
  
  int WavData::openFormatBloc(ifstream* file)
  {
  	std::cout << "[Bloc de declaration d'un fichier au format WAVE]"<<std::endl<<"|"<<std::endl;
  
  	//  FileTypeBlocId
  	int r;
  	r = testString(file,"RIFF",4,	"FileTypeBlocID  (4 octets) : Constante 'RIFF'");
  	if(r<=0)return r;
  	
  	//  FileSize
  	r = testInt(file,4,				"FileSize        (4 octets) : Taille du fichier moins 8 octets");
  	if(r<=0)return r;
  	
  	//  FileFormatID
  	r = testString(file,"WAVE",4,	"FileFormatID    (4 octets) : Format = 'WAVE'");
  	if(r<=0)return r;
  	
  	std::cout << std::endl;
  	return 1;
  }
  
  int WavData::openDescriptionBloc(ifstream* file)
  {
  	std::cout << "[Bloc decrivant le format audio]"<<std::endl<<"|"<<std::endl;
  	
  	int r;
  
  	//  FormatBlocID
  	r = testString(file,"fmt ",4, "FormatBlocID    (4 octets) : Identifiant 'fmt'");
  	if(r<=0)return r;
  
  	//  BlocSize
  	r = testInt(file,4,"BlocSize        (4 octets) : Nombre d'octets du bloc - 8 (attendue: 16)");
  	if(r<=0)return r;
  	
  	//  AudioFormat
  	r = testInt(file,2,"AudioFormat     (2 octets) : Format du stockage dans le fichier (1: PCM, ...)",&_audioFormat);
  	if(r<=0)return r;
  	
  	//  NbrCanaux
  	r = testInt(file,2,"NbrCanaux       (2 octets) : Nombre de canaux (de 1 a 6)",&_nbrChanel);
  	if(r<=0)return r;
  	
  	//  Frequency
  	r = testInt(file,4,"Frequence       (4 octets) : Frequence d'echantillonnage (en Hertz)",&_frequency);
  	if(r<=0)return r;
  	
  	//  BytePerSec
  	r = testInt(file,4,"BytePerSec      (4 octets) : Nombre d'octets a lire par seconde",&_bytePerSec);
  	if(r<=0)return r;
  	
  	//   BytePerBloc
  	r = testInt(file,2,"BytePerBloc     (2 octets) : Nombre d'octets par bloc d'echantillonnage",&_bytePerBloc);
  	if(r<=0)return r;
  	
  	//   BitsPerSample
  	r = testInt(file,2,"BitsPerSample   (2 octets) : Nombre de bits pour le codage d'un echantillon",&_bitsPerSample);
  	if(r<=0)return r;
  
  	return 1;
  }
  
  int WavData::openDataBloc(ifstream* file)
  {
  	std::cout << "[Bloc des donnees]\n";
  	
  	int r,test;
  	//  DataBlocID
  	r = testString(file,"data",4,"DataBlocID      (4 octets) : Constante 'data'");
  	if(r<=0)return r;
  	
  	//  DataSize
  	r = testInt(file,3,"DataSize        (4 octets) : Nombre d'octets des donnees",&_datasize);
  	if(r<=0)return r;
  	
  	//  Data[];
  	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";
  	if(_data!=NULL)delete[] _data;
  	_data = read(file,_datasize);
  	
  	return 1;
  }
  
  int WavData::load(char * s)
  {
  	std::cout << "Load file: \"" << s << "\""<<std::endl<<std::endl;
  
  	ifstream file(s, ios::in | ios::binary );
  	if (!file)return error(-1,__LINE__);
  
  	if(openFormatBloc(&file)		!= 1)	{file.close(); return 0;}
  	if(openDescriptionBloc(&file)	!= 1)	{file.close(); return 0;}
  	if(openDataBloc(&file)			!= 1)	{file.close(); return 0;}
  	
  	std::cout << "close file"<< std::endl;
  	file.close();
  	std::cout << "file closed"<<std::endl;
  
  	return 1;
  }
  
  void WavData::write(ofstream *file,char* mess,unsigned int size)
  {
  	for(int i=0;i<size;i++)
  	{
  		file->put(mess[i]);
  	}
  }
  
  int WavData::saveFormatBloc(ofstream* file)
  {
  	char b[13] = "RIFF    WAVE";
  	
  	toLittleEndian(b+4,_datasize+36,4);
  	
  	write(file,b,12);
  	
  	return 1;
  }
  
  int WavData::saveDescriptionBloc(ofstream* file)
  {
  	char b[25] = "fmt                     ";
  	
  	toLittleEndian(b+4,0x10,4);
  	std::cout<<_audioFormat<<std::endl;
  	toLittleEndian(b+8,_audioFormat,2);
  	toLittleEndian(b+10,_nbrChanel,2);
  	toLittleEndian(b+12,_frequency,4);
  	toLittleEndian(b+16,_bytePerSec,4);
  	toLittleEndian(b+20,_bytePerBloc,2);
  	toLittleEndian(b+22,_bitsPerSample,2);
  	
  	write(file,b,24);
  	
  	return 1;
  }
  int WavData::saveDataBloc(ofstream* file)
  {
  	char b[9] = "data    ";
  	toLittleEndian(b+4,_datasize,4);
  	
  	write(file,b,8);
  	
  	write(file,_data,_datasize);
  	
  	return 1;
  }
  
  int WavData::save(char * s)
  {
  	std::cout << "Save file: \"" << s << "\"\n\n";
  
  	ofstream file(s, ios::out | ios::trunc | ios::binary);
  	if (!file)return error(-1,__LINE__);
  
  	saveFormatBloc(&file);
  	saveDescriptionBloc(&file);
  	saveDataBloc(&file);
  
  	file.close();
  
  	return 1;
  }