0

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?

Community
  • 1
  • 1
suranga
  • 228
  • 1
  • 4
  • 15
  • Your code looks correct. Are you sure the wav file doesn't have the zeros in it? P.S. it should be `delete [] audiobuf;` – jaket Sep 07 '14 at 17:55
  • @jaket It does have some zeros but reads correctly upto 256 entries. Thanks for the correction – suranga Sep 09 '14 at 04:40
  • @Sam It is a single channel audio. I didn't try using `fstream` because I didn't find anything wrong with this code. – suranga Sep 09 '14 at 04:44

0 Answers0