'How to accurately calculate number of samples in .wav file in javascript or c#

I'm trying to calculate the number of samples in a wave file. I use the formula:

Subchunk2Size    = NumSamples * NumChannels * BitsPerSample/8  

Using the formula above, I get a different values than what's reported in Audacity and other standard Wave Editors. I scanned the headers for all the required information, but the formula only works for some files.

Audio files: audio file 1 Audio File 2



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source