Finding number of samples in .wav file and Hex Editor
Solution 1
There is no simple formula for determining the number of samples in a WAV file. A so-called "canonical" WAV file consists of a 44-byte header followed by the actual sample data. So, if you know that the file uses 2 bytes per sample, then the number of samples is equal to the size of the file in bytes, minus 44 (for the header), and then divided by 2 (since there are 2 bytes per sample).
Unfortunately, not all WAV files are "canonical" like this. A WAV file uses the RIFF format, so the proper way to parse a WAV file is to search through the file and locate the various chunks.
Here is a sample (not sure what language you need to do this in):
http://msdn.microsoft.com/en-us/library/ms712835
Solution 2
You must read the fmt
header to determine the number of channels and bits per sample, then read the size of the data
chunk to determine how many bytes of data are in the audio. Then:
NumSamples = NumBytes / (NumChannels * BitsPerSample / 8)
james
Updated on June 15, 2022Comments
-
james almost 2 years
Need help with Hex Editor and audio files.I am having trouble figuring out the formula to get the number of samples in my .wav files.
I downloaded StripWav which tells me the number of samples in the .waves,but still cannot figure out the formula.
Can you please download these two .wavs,open them in a hex editor and tell me the formula to get the number of samples.
If you so kindly do this for me,pleas tell me the number of samples for each .wav so I can make sure the formula is correct.
http://sinewavemultimedia.com/1wav.wav http://sinewavemultimedia.com/2wav.wav
Here is a problem I have two programs,
One reads the wav data and the other shows the numsamples here is the data
RIFF 'WAVE' (wave file) <fmt > (format description) PCM format 2 channel 44100 frames per sec 176400 bytes per sec 4 bytes per frame 16 bits per sample <data> (waveform data - 92252 bytes)
But the other program says NumSamples is
23,063 samples
/*******UPDATE*********/ One more thing I did the calculation with 2 files This one is correct
92,296 bytes and num samples is 23,063`
But this other one is not coming out correctly it is over 2 megs i just subracted 44 bytes and I doing it wrong here? here is the filesize
2,473,696 bytes
But the correct numsamples is
617,400
-
james about 13 yearsUpdated my question please help further/
-
james about 13 yearsUpdated my question.Please help further
-
MusiGenesis about 13 yearsYour "other program" appears to be mis-labeling. 92252 bytes of data means 46126 samples (since your format is 2 bytes per sample). Because the file is stereo (2 channel), this means it contains 23063 frames, not 23063 samples - a 2-channel WAV file contains two samples per frame, whereas a mono file contains only one sample per frame.