Emran Sadeghi
Emran Sadeghi

Reputation: 612

Calculate duration in sec of wave PCM file with it size

i want to calculate duration of wave PCM file in sec with C# i found this https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ link that describe wave file header i use this but this method will not return correct answer

public static int GetDuration(this byte[] waveFile)
{
     int bitRate = BitConverter.ToInt32(waveFile, 28) * 8;
     int sampleRate = BitConverter.ToInt32(waveFile, 24);
     int headerSize = 36;
     short channels = BitConverter.ToInt16(waveFile, 22);
     return ((waveFile.Length - headerSize) / (sampleRate * (bitRate / 8))) / channels;
}

Upvotes: 0

Views: 1548

Answers (1)

Florian
Florian

Reputation: 5994

There should be a 32 bit number which contains the average number of bytes per second at offset 0x1C. Just divide the length of the file - 0x2C by the average bytes per second. Take a look at this: http://s14.directupload.net/images/140428/7suewylj.png (from wikipedia).

This method should work for most of the wave files out there.

Upvotes: 1

Related Questions