I try to read data from a wav file by using fread(). It works all fine when I read the header.But when it starts to read sample data, it will only reads 4 bytes each time. Following is my code
// header varibles
char RIFF[4];
int chunkSize;
char WAVE[4];
char fmt[4];
int subChunk1Size;
int audioFormat;
int numChannels;
int sampleRate;
int byteRate;
int blockAlign;
int bitsPerSample;
char data[4];
int subChunk2Size;
FILE *wavFile;
string wavName;
cout << "Please type the name of the .wav file:";
cin >> wavName;
wavFile = fopen(wavName.c_str(),"r");
if(wavFile != NULL){
cout << "good"<<endl;
fread(RIFF, 4, 1, wavFile);
fread(&chunkSize, 4, 1, wavFile);
fread(WAVE, 4, 1, wavFile);
fread(fmt, 4, 1, wavFile);
fread(&subChunk1Size, 4, 1, wavFile);
fread(&audioFormat, 2, 1, wavFile);
fread(&numChannels, 2, 1, wavFile);
fread(&sampleRate, 4, 1, wavFile);
fread(&byteRate, 4, 1, wavFile);
fread(&blockAlign, 2, 1, wavFile);
fread(&bitsPerSample, 2, 1, wavFile);
fread(data, 4, 1, wavFile);
fread(&subChunk2Size, 4, 1, wavFile);
int samples[subChunk2Size/2];
fread(samples, 2, subChunk2Size/2, wavFile);
}
I already know that bitsPerSample is 16 so I should read 2 bytes as one sample. But no matter what number I put in the last line, it reads 4 bytes of data.
Here is the first 16 bytes of sample data in hexadecimal
0000 0000 0000 0000 0900 1600 1600 1600
The correct out put should be:
0 0 0 0 9 22 22 22
My output is:
0 0 1441801 1441814
Also, I need to draw a waveform by using those data. How can I do it in c++? Do I need to import any library or using api? I am using Mac OS.
Please help. Thank you!
Aucun commentaire:
Enregistrer un commentaire