Question

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
Was it helpful?

Solution

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

OTHER TIPS

WAVE format

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)

A WAVE's format chunk (fmt) has the 'bytes per sample frame' specified as wBlockAlign.
So: framesTotal = data.ck_size / fmt.wBlockAlign;
and samplesTotal = framesTotal * wChannels;
Thus, samplesTotal===FramesTotal IIF wChannels === 1!!

Note how the above answer elegantly avoided to explain that key-equations the spec (and answers based on them) are WRONG: consider flor example a 2 channel 12 bits per second wave..
The spec explains we put each 12bps sample in a word:

  note: t=point in time, chan = channel
+---------------------------+---------------------------+-----
|         frame 1           |         frame 2           |  etc
+-------------+-------------+-------------+-------------+-----
| chan 1 @ t1 | chan 2 @ t1 | chan 1 @ t2 | chan 2 @ t2 |  etc
+------+------+------+------+------+------+------+------+-----
| byte | byte | byte | byte | byte | byte | byte | byte |  etc
+------+------+------+------+------+------+------+------+-----

So.. how many bytes does the sample-frame (BlockAlign) for a 2ch 12bps wave have according to spec?
<sarcasm> CEIL(wChannels * bps / 8) = 3 bytes.. </sarcasm>
Obviously the correct equation is: wBlockAlign=wChannels*CEIL(bps/8)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top