Take the 2-minute tour ×

Our app needs to know the sample count of the audio files it loads. The library we're using can reliably determine the sample rate, but not the sample count. Is it possible for us to calculate the sample count from just the file size and sample rate?

share|improve this question
add comment

2 Answers

Assuming the WAV file is PCM, you can calculate it using the size of the data chunk. The number of bytes per sample is simply the number of bits per sample divided by eight. The number of bits per sample will be present in the WAVEFORMAT structure. This can be used to accurately get the sample count.

share|improve this answer
can I work out the size of the data chunk from the file size, or do I need to know more - e.g. header size? –  Simon Apr 16 '10 at 8:33
WAV files always have a format chunk and a data chunk. They may have other chunks. The only way to be sure of the data chunk size is to find it and read its size parameter. What language & library are you using? –  Mark Heath Apr 16 '10 at 9:37
C# and MediaInfoLib. –  Simon Jun 17 '10 at 8:16
add comment

What mark said. No, normally you need to interpret the header. But if the format, number of channels, and number of bits per sample are known and the same for all files you could theoretically calculate it from the file size.

WAV is a simple format, unfortunately there have been many strange variations of the format from big and small hardware and software developers over the years. Usually you can count on the format being kosher if the files are coming from a modern mainstream wave editor. So, if the samples are standardized by exporting them from WaveLab or similar, you could save writing the (small) code for the header-interpreter.

The easiest-to-read .wav format description is here. StripWav is a small program to standardize samples; there's also a command-line tool which is more capable: sox. Sox supports batch jobs, so it would be better than using a wave editor - assuming the set of .wav files is a given and not 'dynamic'.

So: If you can standardize them once and for all with a sox batch job, it should be possible. I've used this format description and Sox to great effect several times, good luck :)

share|improve this answer
add comment

Your Answer


