This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.cpp
More file actions
347 lines (317 loc) · 12.8 KB
/
Copy pathprocessor.cpp
File metadata and controls
347 lines (317 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Wave Signal Processor
by Shawn Azdam
https://shawnazdam.com
spring 2012
*/
/* This program is a wave signal processor.
The program opens the file and modifies its contents .
The operations that could be run: up-sampling, down-sampling, selecting or deleting part of file .
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
/* The wav files header information is found on the first 44 bytes.
This structure is used to recover the header information from the wav file */
struct wav_hdr
{
char RIFF[4]; // RIFF String, "RIFF"
unsigned long ChunkSize; // RIFF Chunk Size
char WAVE[4]; // Wave String, "WAVE"
char fmt[4]; // fmt String, "fmt"
unsigned long Subchunk1Size; // Size of the FMT chunk
unsigned short AudioFormat; // Audio format 1=PCM , 6=mulaw , 7=alaw , 257=IBM Mu-Law , 258=IBM A-Law , 259=ADPCM
unsigned short NumChan; // Number of channels 1=Mono , 2=Stereo
unsigned long SampRate; // Sampling Frequency in (sample per second) or (Hz)
unsigned long BytesPerSec; // bytes per second
unsigned short BlockAlign; // 2=16-bit mono , 4=16-bit stereo
unsigned short bps; // Number of bits per sample
char Subchunk2ID[4]; // data String, "data"
unsigned long Subchunk2Size; // Sampled data length
};
wav_hdr wavHeader; // struct label
char path[1000] ; // Holds the path of file to open or save that user input.
FILE *wavFile ; // Wave file pointer
int wavSize ; // Keeps size of wave file
int n ; // Contains time of Down or Up Sampling
int NumSample ; // Number of samples in wave file
int ti , tf; // Initial and final time
int sampi , sampf , len; // Initial and final sample, length of new l and r.
unsigned __int16 *left_ch = NULL;
unsigned __int16 *right_ch = NULL;
int wavOpen(char path[]); // open function prototype
int getFileSize(FILE *inFile); // file size calculator function prototype
void print();
int _tmain(int argc, _TCHAR* argv[])
{
printf(" Wave Processor [Version 1.0] \n Copyright (c) 2012 Shawn Azdam. All rights reserved\n\n");
print();
printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n");
printf("Available Commands: \n");
printf("open \t\t open the wav file\n");
printf("save \t\t Save the modified wave signals into a new .wav file\n");
printf("ds \t\t Down-sample current signal\n");
printf("us \t\t Up-sample current signal \n");
printf("del \t\t Delete specific part of wave file \n");
printf("sel \t\t Select specific part of wave file \n");
printf("exit \t\t Quit the program \n");
printf("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n");
printf("Enter a command...\n");
// NULL initializing
// NULL initializing
unsigned __int16 *l = NULL;
unsigned __int16 *r = NULL;
while(1)
{
cin.ignore();
char StrIn[100] ; // Room for user input string.
gets(StrIn); // Gets user input string
if(strcmp(StrIn,"open") == 0) // You may see This step somwhere else in the code too.
{ // This part compares input string with pre-defined commands name to identify entered command.
printf("Please enter file path:\n");
scanf("%s",&path);
int res = 0;
res = wavOpen(path); // Examine the value that returns from "wavOpen" Function.
if(res = 1)
{
wavSize = getFileSize(wavFile);
// Printing wav header information.
printf("\t File size: \t %d Bytes\n",wavSize);
printf("\t Duration: \t %d second\n",wavSize/wavHeader.BytesPerSec);
printf("\t Audio format: \t ",wavHeader.AudioFormat);
switch (wavHeader.AudioFormat) // A switch control to printing relevant Audio Format.
{
case 1:
printf("PCM"); //Pulse Code Modulation, an audio encoding algorithm which encodes the amplitude of a repetitive series of audio samples
break;
case 6:
printf("mu-law"); // The compressing and expanding algorithm used primarily in North America and Japan when converting from analog to digital speech data.
break;
case 7:
printf("A-law"); // The compressing and expanding algorithm used in Europe, Latin America, and other countries when converting from analog to digital speech data.
break;
case 257:
printf("IBM mu-law"); // IBM developed mu-law standard
break;
case 258:
printf("IBM IBM A-law"); // IBM developed A-law standard
break;
case 259:
printf("ADPCM"); // Adaptive Differential Pulse Code Modulation, method for converting analog sound into low bit-rate digital signals through the use of a linear sampling algorithm.
break;
}
printf("\n\t Sampling Rate : \t %d Hz\n",wavHeader.SampRate);
printf("\t Number of samples : %d\n",NumSample);
printf("\t Bits Per Sample (bps): %d bits\n",wavHeader.bps);
if(wavHeader.NumChan == 1) // Printing number of channels ( Stereophonic or Monophonic)
printf("\t Number of channels: \t 1 (Mono) \n");
else
printf("\t Number of channels: \t 2 (Stereo) \n");
}
continue;
}
else if(strcmp(StrIn,"ds") == 0) // Down-sample command codes
{
printf("Please enter times that you want to down-sample the wave file: \n");
scanf("%d",n);
unsigned __int16 *l = new unsigned __int16 [NumSample]; // Memory allocating
unsigned __int16 *r = new unsigned __int16 [NumSample];
for(int c=0 ; c<11 ; c++) // for Loop to copying header to *l and *r
{
l[c]=left_ch[c];
r[c]=right_ch[c];
}
l[6]=0;
r[6]=0;
int p=11 , q=12; // Down-sampling and putting left and right_ch channels data in *l and *r at the same time
for(p , q ; q<NumSample ; p++, q+=2)
{
l[p]=left_ch[q];
r[p]=right_ch[q];
}
continue;
}
else if(strcmp(StrIn,"us") == 0) // Up-sample command codes
{
printf("Please enter times that you want to up-sample the wave file: \n");
scanf("%d",n);
unsigned __int16 *l = new unsigned __int16 [(NumSample*n)]; // Memory allocating
unsigned __int16 *r = new unsigned __int16 [(NumSample*n)];
for(int c=0 ; c<11 ; c++) // for Loop to copying header to *l and *r
{
l[c]=left_ch[c];
r[c]=right_ch[c];
}
l[6]=0;
r[6]=0;
int p=11 , q=11;
if(n=2) // Down-sampling and putting left and right_ch channels data in *l and *r at the same time
{
for(p , q ; q<(NumSample*n) ; p+=n, q++)
{
l[p]=left_ch[q];
l[p+1]=left_ch[q];
r[p]=right_ch[q];
r[p+1]=right_ch[q];
}
}
if(n=3)
{
for(p , q ; q<(NumSample*n) ; p+=n, q++)
{
l[p]=left_ch[q];
l[p+1]=left_ch[q];
l[p+2]=left_ch[q];
r[p]=right_ch[q];
r[p+1]=right_ch[q];
r[p+2]=right_ch[q];
}
}
continue;
}
else if(strcmp(StrIn,"save") == 0) // Save command codes
{
printf("Please enter file save path:\n");
scanf("%s",&path);
FILE *NewFile;
NewFile = fopen("path","wb+");
for( int k = 0 ; k<(NumSample/n) ; k++)
{
fwrite(&l[k],2,1,NewFile);
fwrite(&r[k],2,1,NewFile);
}
continue;
}
else if(strcmp(StrIn,"sel") == 0) // Select command codes
{
int ti , tf ;
int sampi , sampf , len;
printf("Please enter initial time:\n");
scanf("%d",&ti);
printf("Please enter final time:\n");
scanf("%d",&tf);
sampi = ti * wavHeader.SampRate; // Calculate the number of initial sample
sampf = tf * wavHeader.SampRate; // Calculate the number of final sample
len = sampf - sampi; // length of *l and *r to handling new command
unsigned __int16 *l = new unsigned __int16 [len]; // Memory allocating
unsigned __int16 *r = new unsigned __int16 [len];
for(int c=0 ; c<11 ; c++) // for Loop to copying header to *l and *r
{
l[c]=left_ch[c];
r[c]=right_ch[c];
}
for(int i = 11 , j=sampi ; j <= sampf ; i++ , j++)
{
l[i]=left_ch[j];
r[i]=right_ch[j];
}
continue;
}
else if(strcmp(StrIn,"del") == 0) // Delete command codes
{
printf("Please enter initial time:\n");
scanf("%s",&ti);
printf("Please enter final time:\n");
scanf("%s",&tf);
sampi = ti * wavHeader.SampRate; // Calculate the number of initial sample
sampf = tf * wavHeader.SampRate; // Calculate the number of final sample
len = sampf - sampi; // length of *l and *r to handling new command
unsigned __int16 *l = new unsigned __int16 [len]; // Memory allocating
unsigned __int16 *r = new unsigned __int16 [len];
for(int c=0 ; c<11 ; c++) // for Loop to copying header to *l and *r
{
l[c]=left_ch[c];
r[c]=right_ch[c];
}
for(int i = 11 ; i < sampi ; i++) // Except (0 - sampi) and (sampf - NumSample)
{ // other part of Left and right_ch will be deleted.
l[i]=left_ch[i];
r[i]=right_ch[i];
}
for(int j = sampf ; j < NumSample ; j++)
{
l[j]=left_ch[j];
r[j]=right_ch[j];
}
continue;
}
else if(strcmp(StrIn,"exit") == 0) // Exit command codes
{
exit(EXIT_SUCCESS); // This part exits program and send an event
// to windows event manager and notify
} // program exited in successfull manner.
else
{
printf("\t The Command that you entered is wrong \n");
}
}
_fcloseall(); // Closes all streams
// Deleting the memory that allocated before
delete left_ch;
delete right_ch;
delete l;
delete r;
return 0;
}
int wavOpen(char path[]) // Open function codes
{
wavFile = fopen(path,"rb"); // Open the file in read binary mode
if(wavFile != NULL) // Check file to sure about having data in file
{
printf(" \t->File opened Successfully\n");
int headerSize = sizeof(wavHeader); // Calculates the size of wav header
fread(&wavHeader,headerSize,1,wavFile); // Puts header in wavHeader structure
NumSample = (8*wavHeader.Subchunk2Size)/(wavHeader.bps * wavHeader.NumChan);// Calculating total number of samples
left_ch = new unsigned __int16 [NumSample];
right_ch = new unsigned __int16 [NumSample]; // Memory allocating
for( int i = 0 ; i<NumSample ; i++) // Reading wavFile and saving data
{ // in *left and *right_ch
fread(&left_ch[i],2,1,wavFile);
fread(&right_ch[i],2,1,wavFile);
}
return 1;
}
else // Prints proper error if file not exist or open with error
{
printf(" \t->File not found, check path correction and file existence\n");
return -1;
}
}
int getFileSize(FILE *inFile) // Seek end of file and with calling
{ // file cursor position calculate
int fileSize = 0; // file size in byte.
fseek(inFile,0,SEEK_END);
fileSize=ftell(inFile);
rewind(inFile);
return fileSize;
}
void print()
{
printf("__@@@@@_______________________@@\n");
printf("__@@__@@_____________________@@@@\n");
printf("___@@__@@____________________@@@@@\n");
printf("____@@_@@@___________________@@__@@\n");
printf("____@@_@@@___________________@@__@@\n");
printf("_____@@@@@___________________@@_@@@\n");
printf("_____@@@@______________@@@@ @@__@@\n");
printf("____@@@@_____________@@@@@@@@@_@@\n");
printf("___@@@_@@__@@@_______@@@@@@@@\n");
printf("__@@@___@@@@@@@@@_____@@@@@@\n");
printf("_@@@@__@@@@___@@@@_______________________@@@\n");
printf("_@@@__@@@@_@@@__@@@__________________@@@@@@\n");
printf("@@@@__@@@@@@@@@__@@@______________@@@@@@@@@\n");
printf("_@@@__@@@_@@__@__@@@___________@@@@@@@___@@\n");
printf("_@@@@__@@@@@@@@__@@________@@@@@@@@______@@\n");
printf("__@@@@____@@@__@@@______@@@@@@@@@@_______@@\n");
printf("___@@@@@@___@@@@@_____@@@@@@@@___@@_______@@\n");
printf("_____@@@@@@@@@@________@@@@@_____@@___@@@@@@\n");
printf("________@@@_@@@________@@________@@__@@@@@@@\n");
printf("_______@@@@@_@@_______@@@_____@@@@___@@@@@\n");
printf("_______@@@___@@_________@@___@@@@@@\n");
printf("_________@@@@@__________@@___@@@@@@\n");
printf("_________________________@@__@@@@\n");
printf("_____________________@@@@@@\n");
printf("____________________@@@@@@@\n");
printf("____________________@@@@@@\n");
}