Simon
Simon

Reputation: 25984

Given a WAV file, its file size and sample rate, is it possible to calculate the sample count?

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?

Upvotes: 5

Views: 6251

Answers (3)

SuperLucky
SuperLucky

Reputation: 605

In PCM wav format the header contains the information called blockalign of how many bytes a single sample takes.

Typically if you has a standard RIFF PCM wav file with no metadata attached to it(usual case). The blockalign is a 2-byte-integer at offset 32(the 33th to 34th bytes from the begining of the wav file). And the file size of data called datasize is a 4-byte integer at offset 40(the 41th to 44th bytes form the begining of the wav file) .

Now datasize/blockalign is what you want.

PS

In case you have a more complicated wav format, if it's RIFF, the format infomation and data are put into different "chunks" (along with some other chunks you may not need), and the offsets talked above may not correct, then you should look into chunks. In your case, you need to find fmt and data chunk.

Each chunks starts with a 4 byte ASCII coded data called FOURCC, 'fmt ' indicates that chunk include format information and 'data' indicates a data chunk. Right after FOURCC is a 4-byte-integer telling the size(in bytes) of the chunk after (FOURCC and this 4 bytes are not count).

References:

A simple wav header reference HERE

More general RIFF wav format HERE

Upvotes: 2

Henrik Erlandsson
Henrik Erlandsson

Reputation: 3831

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 :)

Upvotes: 5

Mark Heath
Mark Heath

Reputation: 49482

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.

Upvotes: 4

Related Questions