-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavproc.c
276 lines (215 loc) · 6.46 KB
/
wavproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#define MODE 1
int Check_RIFF(); //ελενχος αν υπαρχει RIFF
int Check_SizeOfFile(); //ο χορος που καταλαμβαζει το αρχειο(ή μποορει να χρισυμεφσει και για αν βρισκει το μεγεθος 4 bytes)
int Check_WAVE(); //ελενχος αν υπαρχει το WAVE
int Check_fmt(); //ελενχος αν υπαρχει το fmt
int Check_Format_Chunck(); //ελενχος αν υπαρχει το Format Chunck
int Check_Format_WAVE(); //ελενχος αν υπαρχει το Format WAVE
int little_endian_For_2Bytes(); //βρισκει το μεγεθος που εχουν 2 bytes
int MonoStereo(); //ελενχει αν ο ηχος ειναι στερεοφωνικος ή οχι
int SampleRate(); //πόσες τιμές, ανά δευτερόλεπτο,της συνάρτησης του αναλογικού ήχου έχουν καταγραϕεί στο αρχείο.
int BytesPerSec(); //για το πλήθος bytes ανά δευτερόλεπτο ήχου, που είναι καταχωρημένα στο αρχείο.
int BlockAlign(); //για το πλήθος των bytes που απαιτούνται για την καταχώρηση της πληροϕορίας του ήχου σε μία χρονικά στιγμή,για όλα τα κανάλια.
int BitsPerSample(); //για το πλήθος των bits που απαιτούνται για την καταχώρηση της πληροϕορίας του ήχου σε μία χρονικά στιγμή, για ένα μόνο κανάλι.
int Data_chunk(); //4 bytes με τους χαρακτήρες “data”
int SizeOfData(); //συμβολικά SizeOfData, για τον χώρο που καταλαμβάνουν τα δεδομένα του τμήματος data που θα ακολουθήσουν
int SizeOfExtra(int ); //μετραει τα bytes μετα τα sizeofdata
int main(int argc, char const *argv[])
{
int sizeoffile, tmp, samplerate, bytespersec, blockalign, bitspersample, sizeofdata, sizeofextra;
//ελενχος αν υπαρχει RIFF
if (Check_RIFF()){
fprintf(stderr,"Error! 'RIFF' not found\n");
return 0;
}
//ο χορος που καταλαμβαζει το αρχειο
fprintf(stderr,"size of file: %d\n",(sizeoffile = Check_SizeOfFile()) );
//ελενχος αν υπαρχει το WAVE
if (Check_WAVE()){
fprintf(stderr,"Error! 'WAVE' not found\n");
return 0;
}
//ελενχος αν υπαρχει το fmt
if(Check_fmt()){
fprintf(stderr,"Error! 'fmt' not found\n");
return 0;
}
//ελενχος αν υπαρχει το
if((tmp = Check_Format_Chunck()) == 16){
fprintf(stderr,"size of format chunck: %d\n",tmp );
}
else{
fprintf(stderr,"size of format chunck: %d\n",tmp );
fprintf(stderr,"Error! size of format chunck should be 16\n");
return 0;
}
if((tmp = Check_Format_WAVE()) == 1){
fprintf(stderr,"WAVE type format: %d\n",tmp );
}
else{
fprintf(stderr,"WAVE type format: %d\n",tmp );
fprintf(stderr,"Error! WAVE type format should be 1\n");
return 0;
}
tmp = MonoStereo();
if( (tmp == 1) || (tmp == 2) )
fprintf(stderr,"mono/stereo: %d\n",tmp );
else{
fprintf(stderr,"mono/stereo: %d\n",tmp );
fprintf(stderr,"Error! mono/stereo should be 1 or 2\n");
return 0;
}
fprintf(stderr,"sample rate: %d\n",(samplerate = SampleRate()));
fprintf(stderr,"bytes/sec: %d\n",(bytespersec = BytesPerSec()));
fprintf(stderr,"block alignment: %d\n",(blockalign = BlockAlign()));
if(bytespersec != (samplerate * blockalign)){
fprintf(stderr,"Error! bytes/second should be sample rate x block alignment\n");
return 0;
}
fprintf(stderr,"bits/sample: %d\n",(bitspersample = BitsPerSample()));
if ( !((bitspersample == 8) || (bitspersample == 16)) ){
fprintf(stderr,"Error! bits/sample should be 8 or 16\n");
return 0;
}
if (blockalign != (bitspersample / 8 * tmp) ){
fprintf(stderr,"Error! block alignment should be bits per sample / 8 x mono/stereo\n");
return 0;
}
if (Data_chunk()){
fprintf(stderr,"Error! 'data' not found\n");
return 0;
}
fprintf(stderr,"size of data chunk: %d\n", (sizeofdata = SizeOfData()));
sizeofextra = SizeOfExtra(sizeofdata);
if( !(sizeofextra) ){
fprintf(stderr,"Error! insufficient data\n");
return 0;
}
if ( (36 + sizeofextra + sizeofdata) != sizeoffile)
fprintf(stderr,"Error! bad file size\n");
return 0;
}
int Check_RIFF()
{
int tmp = Check_SizeOfFile();
if (tmp == 0x46464952)
return 0;
else
return 1;
}
int Check_SizeOfFile()
{
int b1, b2, b3, b4, numdiv, nummod, sum = 0;
b1 = getchar();
b2 = getchar();
b3 = getchar();
b4 = getchar();
if (b1 != 0x00){
numdiv = b1 / 16;
nummod = b1 % 16;
sum = sum + (nummod * 1) + (numdiv * 16);
}
if(b2 != 0x00){
numdiv = b2 / 16;
nummod = b2 % 16;
sum = sum + (nummod * 256) + (numdiv * 4096);
}
if(b3 != 0x00){
numdiv = b3 / 16;
nummod = b3 % 16;
sum = sum + (nummod * 65536) + (numdiv * 1048576);
}
if(b4 != 0x00){
numdiv = b4 / 16;
nummod = b4 % 16;
sum = sum + (nummod * 16777216) + (numdiv * 268435456);
}
return sum;
}
int Check_WAVE()
{
int tmp = Check_SizeOfFile();
if (tmp == 0x45564157)
return 0;
else
return 1;
}
int Check_fmt()
{
int tmp = Check_SizeOfFile();
if (tmp == 0x20746d66)
return 0;
else
return 1;
}
int Check_Format_Chunck()
{
return Check_SizeOfFile();
}
int Check_Format_WAVE()
{
return little_endian_For_2Bytes();
}
int little_endian_For_2Bytes()
{
int b1, b2, numdiv, nummod, sum = 0;
b1 = getchar();
b2 = getchar();
if (b1 != 0x00){
numdiv = b1/16;
nummod = b1%16;
sum = sum + (nummod * 1) + (numdiv * 16);
}
if(b2 != 0x00){
numdiv = b2 / 16;
nummod = b2 % 16;
sum = sum + (nummod * 256) + (numdiv * 4096);
}
return sum;
}
int MonoStereo()
{
return little_endian_For_2Bytes();
}
int SampleRate()
{
return Check_SizeOfFile();
}
int BytesPerSec()
{
return Check_SizeOfFile();
}
int BlockAlign()
{
return little_endian_For_2Bytes();
}
int BitsPerSample()
{
return little_endian_For_2Bytes();
}
int Data_chunk()
{
int tmp;
tmp = Check_SizeOfFile();
if (tmp == 0x61746164)
return 0;
else
return 1;
}
int SizeOfData()
{
return Check_SizeOfFile();
}
int SizeOfExtra(int sizeofdata)
{
int CountDataBytes = 0, CountExtraBytes = 0;
while ( getchar() != EOF ){
CountDataBytes++;
if(CountDataBytes > sizeofdata)
CountExtraBytes++;
}
if ( CountExtraBytes == 0)
return 0;
return CountExtraBytes;
}