Wav length

How would one report the length of a wav file to VB?
I assume that the header of the wav contains the bitrate, so theoretically, one could use Filelen()/bitrate???

How do i extract this header?


Who is Participating?
To retrieve the time length of a wave file (in seconds) you must calculate:

time = FileLenght / (SampleRate * Channels * BitsPerSample/8)
      =  FileLenght / bytespersecond

All this information can be retrieved from the Wav file header or Format chunk
There's a little detail: A WAV file is divided in chunks, each chunk has a specific meaning,  and there are at least 2 chunks: The format and the data chunk.
1) The format chunk is often at the beginning at the file and has the information about the Waveform format of the file: (samplerate, channnels, bitspersample, blockalign, etc..)
2) The data chunk contains the actual waveform data.

All the chunks have at least two parameters: The chunkID and the chunksize:
   a) ChunkID: identifies the chunk ('fmt ' for the format chunk, and 'data' for the data chunk)
   b) Chunksize: is the lenght of the chunk in bytes (not including the bytes used by the chunks header) This is the parameter with which you can know where does a chunk end and where does the next chunk begin (if any).

There are other optional chunks that are application-specific (for example a wave file can contain a song's lyrics, if saved by a program that supports it). THIS MEANS THE THE FILELENGTH DOES NOT ALWAYS REPRESENT THE TRUE SIZE IN BYTES OF THE WAVE DATA.

Anyway, in the most of the cases you could use the above equation to calculate the playtime of a WAVE file

¿How to extract the Header?
You have to read the first 36 bytes of the file
Bytes <-->Meaning
4:    "RIFF"
4:    size of file including header
4:    "WAVE"
4:    "fmt "
4:    (don't remember)
2:    formatTag
2:    number of channels (mono - stereo)
4:    Samples per second
4:    Bytes per Second    
2:    blockAlign (minimum atomic unit of data) = bitspersample/8
2:    bits per sample

The next  bytes represent the other chunks ... for example
bytes 36 to 39:  "data"
bytes 40 to 43:  size of sound data in bytes (lets call it X)
bytes 44 to X:    Your wave data
I have some experience working with WAV file, but i don't understand quite well. Your question asks for the lenth of a Wav file... so I would say just the File lenth (This is obviously not what yo're asking for, I know)
What length are you talking about?... Te length in seconds?... The length in bytes of the data string?...
salocorganAuthor Commented:
Yes -- length timewize.  Seconds would be great.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

salocorganAuthor Commented:
OK. So i need filelen - header /bytespersecond. But when i open a wav file with a text editor i only get gibberish inbetween things like RIFF and WAVE and the rest.  What format is that? And how do i convert it??

Thanx again for your help, much appreciated.
Ok.... I just coded this lines and tested them (to make it easy place a command button on a form and paste the following code)

-----------------Code on a command button---------------
Private Sub Command1_Click()
    Dim MyInt As Integer
    Dim MyByte As Byte
    Dim MyStr As String * 4
    Dim MyLong As Long
    Dim SampleRate, BytesPerSample, FileSize As Long
    Open "d:\tmp.wav" For Binary Access Read Lock Read As #1
        Get #1, , MyStr:    Debug.Print "Riff = "; MyStr
        Get #1, , MyLong:   Debug.Print "File size = "; MyLong
        FileSize = MyLong
        Get #1, , MyStr:    Debug.Print "Wave = "; MyStr
        Get #1, , MyStr:    Debug.Print "Format = "; MyStr
        Get #1, , MyLong:   Debug.Print "Any = "; MyLong
        Get #1, , MyInt:    Debug.Print "formatTag = "; MyInt
        Get #1, , MyInt:    Debug.Print "Channels = "; MyInt
        Get #1, , MyLong:   Debug.Print "Samples per Sec = "; MyLong
        SampleRate = MyLong
        Get #1, , MyInt:    Debug.Print "Bytes per Sec = "; MyInt
        Get #1, , MyInt:    Debug.Print "BlockAlign = "; MyInt
        Get #1, , MyInt:    Debug.Print "Bytes per Sample = "; MyInt
        BytesPerSample = MyInt
    Close #1
    Debug.Print "Wave File Duration = "; _
        FileSize / (SampleRate * BytesPerSample); " seconds"
End Sub
----------Output of the program on the debug window------
Riff = RIFF
File size =  68950
Wave = WAVE
Format = fmt
Any =  18
formatTag =  1
Channels =  2
Samples per Sec =  11025
Bytes per Sec = -21436
BlockAlign =  0
Bytes per Sample =  4
Wave File Duration =  1.56349206349206  seconds

Which is exactly the length that I got on Win95 sound recorder...
What do you think now?
Oh... I forgot to tell somethin!
When you open a Wav file in notepad, you see only garbage, this is obvious because it's not a text file... it binary!
The trick is reading the file and putting them in the appropriate variables.... If you have to read a 2-byte-number you use an integer (Which needs 2 bytes to be stored), for a 4-bytes-number you'd use a long, etc.... Thet's different from a text file....
(Supose you have an integer i=1000. As you see we are using 4 bytes '1', '0', '0', '0'....That is TEXT. To store it as an integer you only need two bytes.
salocorganAuthor Commented:
What do i think now? I think that was an EXCELLENT answer! Youre brilliant!!! ;-)  Thanx again. :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.