Search code examples
.netvb.netaudiovisual-studio-2019wav

Determine WAV Duration Time in Visual Studio 2019


First, Thank You for considering my question. I have searched for a solution to this problem, and have not found anything that makes use of the modern Visual Studio features/libraries/namespaces, etc...

I need to determine the duration (length of time) of a WAV audio file. I would like to get the results in milliseconds - I can format the data to my needs from there.

Can you please provide an example of how this might be done?

THANK YOU


Solution

  • For those searching for this solution, here is the debugged code. Note that the MilliSecond to timestring conversion was not included here. Also Note that based on the comments made to this posting, there is no standard Visual Basic Library that will derive the duration of a WAV file.


    Option Explicit On Option Strict On Imports System.IO

    Public Shared Function GetWAVDuration(ByVal strPathAndFilename As String) As String
    
        REM *** SEE ALSO
        REM *** http://soundfile.sapp.org/doc/WaveFormat/
        REM *** CAUTION
        REM *** On the WaveFormat web page, the positions of the values
        REM *** SampleRate and BitsPerSample have been reversed.
        REM *** https://stackoverflow.com/questions/65588931/determine-wav-duration-time-in-visual-studio-2019/65616287#65616287
    
        REM *** DEFINE LOCAL VARIABLES
    
        REM *** Define Visual Basic BYTE Array Types
        Dim byNumberOfChannels() As Byte
        Dim bySamplesPerSec() As Byte
        Dim byBitsPerSample() As Byte
        Dim bySubChunkToSizeData() As Byte
    
        Dim nNumberOfChannels As Int16
        Dim nSamplesPerSec As Int16
        Dim nBitsPerSample As Int32
        Dim nSubChunkToSizeData As Int32
    
        Dim dNumberOfSamples As Double
        Dim nDurationInMillis As Int32
    
        Dim strDuration As String
    
        REM *** INITIALIZE LOCAL VARIABLES
        byNumberOfChannels = New Byte(2) {}
        bySamplesPerSec = New Byte(2) {}
        byBitsPerSample = New Byte(4) {}
        bySubChunkToSizeData = New Byte(4) {}
    
        nNumberOfChannels = 0
        nSamplesPerSec = 0
        nBitsPerSample = 0L
        nSubChunkToSizeData = 0L
    
        dNumberOfSamples = 0.0
        nDurationInMillis = 0
    
        strDuration = ""
    
        REM *** Initialize the return string value
        GetWAVDuration = ""
    
        REM ***************************************************************************
    
        REM *** Open the Input File for READ Operations
        Using fsFileStream = File.OpenRead(strPathAndFilename)
    
            REM *** Get the Number of Audio Channels
            fsFileStream.Seek(22, SeekOrigin.Begin)
            fsFileStream.Read(byNumberOfChannels, 0, 2)
    
            REM *** Get the Number of Bits Per Audio Sample
            fsFileStream.Seek(24, SeekOrigin.Begin)
            fsFileStream.Read(byBitsPerSample, 0, 4)
    
            REM *** Get the number of samples taken per second
            fsFileStream.Seek(34, SeekOrigin.Begin)
            fsFileStream.Read(bySamplesPerSec, 0, 2)
    
            REM *** Retrieve the size of the WAV data
            REM *** payload in the file
            fsFileStream.Seek(40, SeekOrigin.Begin)
            fsFileStream.Read(bySubChunkToSizeData, 0, 4)
    
        End Using
    
        REM *** Convert Values from their BYTE representation
    
        nNumberOfChannels = BitConverter.ToInt16(byNumberOfChannels, 0)
        nBitsPerSample = BitConverter.ToInt32(byBitsPerSample, 0)
        nSamplesPerSec = BitConverter.ToInt16(bySamplesPerSec, 0)
        nSubChunkToSizeData = BitConverter.ToInt32(bySubChunkToSizeData, 0)
    
        REM *** Compute the Duration of the WAV File
        REM *** Derives the duration in milliseconds
    
        REM *** Determine the number of Sound Samples 
        dNumberOfSamples = (nSubChunkToSizeData * 8) / (nNumberOfChannels * nBitsPerSample)
        nDurationInMillis = Convert.ToInt32(1000 * Convert.ToSingle(dNumberOfSamples) / Convert.ToSingle(nSamplesPerSec))
    
        REM *** Convert the time in Milliseconds to a string format
        REM *** represented by "hh:mm:ss"
        strDuration = ConvertMillisToTimeString(nDurationInMillis)
    
        REM *** POST METHOD RETURNS
        GetWAVDuration = strDuration
    
    End Function