How can I determine the length (i.e. duration) of a .wav file in C#?

后端 未结 15 2289
逝去的感伤
逝去的感伤 2020-11-30 00:26

In the uncompressed situation I know I need to read the wav header, pull out the number of channels, bits, and sample rate and work it out from there: (channels) * (bits) *

15条回答
  •  时光取名叫无心
    2020-11-30 01:13

    I'm going to assume that you're somewhat familiar with the structure of a .WAV file : it contains a WAVEFORMATEX header struct, followed by a number of other structs (or "chunks") containing various kinds of information. See Wikipedia for more info on the file format.

    First, iterate through the .wav file and add up the the unpadded lengths of the "data" chunks (the "data" chunk contains the audio data for the file; usually there is only one of these, but it's possible that there could be more than one). You now have the total size, in bytes, of the audio data.

    Next, get the "average bytes per second" member of the WAVEFORMATEX header struct of the file.

    Finally, divide the total size of the audio data by the average bytes per second - this will give you the duration of the file, in seconds.

    This works reasonably well for uncompressed and compressed files.

提交回复
热议问题