Results 1 to 9 of 9

Thread: Getting time length of a .wav song

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    3

    Getting time length of a .wav song

    I have some songs in the resources of my application and want to get their time length so that I can set a timer to tick when the song ends. And then start the next song. I want to do that in my code, and get the time length of a song as an integer value.

    I have been googling for it for many hours. I'm a beginner. So please try to explain in a simple language. Thanks in advance.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Getting time length of a .wav song

    There are two ways, a hard way (path of light) and an easy way (path of darkness).

    The hard way:
    Parse the header of your wav using this information:
    https://ccrma.stanford.edu/courses/4...ts/WaveFormat/

    Now, the Dark side way (using MCI):

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class MCI
    4.  
    5.     <DllImport("winmm", setlasterror:=True, entrypoint:="mciSendString", CharSet:=CharSet.Auto)> _
    6.     Private Shared Function mciSendString( _
    7.             ByVal lpstrCommand As String, _
    8.             ByVal lpstrReturnString As String, _
    9.             ByRef uReturnLength As Integer, _
    10.             ByVal hwndCallBack As IntPtr) As Integer
    11.     End Function
    12.  
    13.     Public Shared Function GetMediaLength(ByVal FileName As String) As ULong
    14.  
    15.         Dim MediaLength As ULong
    16.         Dim RetString As New String(" "c, 50)
    17.         Dim CommandString As String
    18.  
    19.         'open the media file
    20.         If Not IO.File.Exists(FileName) Then
    21.             Throw New IO.FileNotFoundException(String.Format("File {0} was not found.", FileName))
    22.             Return Nothing
    23.         End If
    24.  
    25.         CommandString = "Open " & FileName & " alias MediaFile"
    26.         mciSendString(CommandString, String.Empty, 0, IntPtr.Zero)
    27.  
    28.         'get the media file length
    29.         CommandString = "Set MediaFile time format milliseconds"
    30.         mciSendString(CommandString, String.Empty, 0, IntPtr.Zero)
    31.         CommandString = "Status MediaFile length"
    32.  
    33.         mciSendString(CommandString, RetString, RetString.Length, IntPtr.Zero)
    34.  
    35.         RetString = RetString.Trim
    36.  
    37.         If Not ULong.TryParse(RetString, MediaLength) Then
    38.             MsgBox(String.Format("Failed to retrieve the media length for '{0}'.", FileName))
    39.             Return Nothing
    40.         End If
    41.  
    42.         'close the media file
    43.         CommandString = "Close MediaFile"
    44.         mciSendString(CommandString, vbNullString, 0, IntPtr.Zero)
    45.         Return MediaLength
    46.     End Function
    47. End Class

    Usage:
    vb.net Code:
    1. MCI.GetMediaLength(filename) ' The return value will be in milliseconds.

    By the way this same function also works with all media formats supported by MCI (Media Control Interface)
    http://en.wikipedia.org/wiki/Media_Control_Interface

  3. #3
    Lively Member TETYYS's Avatar
    Join Date
    May 2010
    Location
    Spam Land
    Posts
    116

    Re: Getting time length of a .wav song

    dark side is better

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    3

    Re: Getting time length of a .wav song

    Wow! What a complication! I didn't understand neither of the ways. I copied and pasted the dark way. And used the MCI like this:

    Timer3.Interval = MCI.GetMediaLength(music(0))

    It gave me an error pointing to that line and saying:

    InvalidCastException was unhandled

    Converting from type "UnmanagedMemoryStream" to type "String" is invalid.

    What's the problem? I'm a beginner and I don't get it. Visual Basic is supposed to have easier ways for stuff like that.

  5. #5

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    3

    Thumbs up Re: Getting time length of a .wav song

    After some struggle I got it working. Thanks. Visual Basic should have had simpler ways of doing such stuff.

  7. #7
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169

    Re: Getting time length of a .wav song

    Just for the record, the so-called "hard way" isn't hard at all. In the header is a value, BytesPerSecond and we just divide this into the filesize (also in the header).

    The following code is VB6, but perhaps someone would be kind enough to convert it to VB.NET.

    Code:
    Type RIFFHEADER
       RIFFtag        As String * 4  ' "RIFF"
       Filesize       As Long
       WAVtag         As String * 4  ' "WAVE"
       FMTtag         As String * 4  ' "fmt "
       FMTsize        As Long
       CompressType   As Integer
       Channels       As Integer
       SampleRate     As Long        ' Samples per sec
       BytesPerSec    As Long        ' = SampleRate * BytesPerSample
       BytesPerSample As Integer
       BitsPerSample  As Integer
       End Type
       
    Dim WAVHEADER As RIFFHEADER
    
    Function WavLength(ByVal wName As String) As Long ' length in seconds
       Dim fv As Integer
       
       fv = FreeFile
       If Dir$(wName) = "" Then Exit Function   ' ensure we only open an existing file
       Open wName For Binary As fv
       Get #fv, 1, WAVHEADER
       Close #fv
       With WAVHEADER
          If .FMTtag <> "fmt " Then Exit Function
          WavLength = .Filesize \ .BytesPerSec
          End With
       End Function
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  8. #8
    New Member
    Join Date
    Jan 2015
    Posts
    14

    Re: Getting time length of a .wav song

    I too am trying to get the duration of a WAV file. The dark side MCI does not work for many of my WAV files. I'm not sure why, they play fine, and the Windows explorer seems to report a valid duration.

    I tried the above VB6 stuff and it works perfectly. I've been trying to get it converted to vb.net but i'm struggling. I can't figure out how to create a data structure or read a data stream into that data structure.

    Any one know?

  9. #9
    New Member
    Join Date
    Jan 2015
    Posts
    14

    Re: Getting time length of a .wav song

    I got something simple working--no error checking but you get the point

    Private Function GetDuration(ByVal filename As String) As Double

    Dim rifftag(4) As Char
    Dim Filesize As Long
    Dim WAVtag(4) As Char
    Dim FMTtag(4) As Char
    Dim FMTsize As Long
    Dim compresstype As Integer
    Dim channels As Integer
    Dim samplerate As Long
    Dim bytespersec As Long
    Dim bytespersample As Integer
    Dim bitspersample As Integer
    Dim wavlength As Double

    Dim reader = New BinaryReader(File.Open(strFileName, FileMode.Open))


    rifftag = reader.ReadChars(4)
    Filesize = reader.ReadInt32
    WAVtag = reader.ReadChars(4)
    FMTtag = reader.ReadChars(4)
    FMTsize = reader.ReadInt32
    compresstype = reader.ReadInt16
    channels = reader.ReadInt16
    samplerate = reader.ReadInt32
    bytespersec = reader.ReadInt32
    bytespersample = reader.ReadInt16
    bitspersample = reader.ReadInt16

    wavlength = (Filesize / bytespersec) * 1000 ' return in milliseconds

    reader.Close()

    Return wavlength

    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width