I am trying to read the data part of a .wav file into a buffer. I have already read the header part according to C++ Reading the Data part of a WAV file
Therefore, my file pointer wavFile
now points to the beginning of the data section. Then I use the following code to read audio data into a buffer.
long bytes = wavHeader.bitsPerSample/8;
long buffsize= wavHeader.Subchunk2Size/bytes;
int16_T *audiobuf = new int16_T[buffsize];
fread(audiobuf,bytes,buffsize,wavFile);
// do some processing
delete audiobuf;
In my test audio file, bitsPerSample
is 16 and Subchunk2Size
is 79844. Therefore, buffsize
is 39922.
After running this code, I noticed that only first 256 positions of audiobuf
get filled. But theoretically there should be 39922 entries of audio data. How can I sort out this issue?