.net - 在 Visual Studio 2019 中确定 WAV 持续时间

标签 .net vb.net audio visual-studio-2019 wav

首先,感谢您考虑我的问题。我已经搜索了此问题的解决方案,但没有找到任何使用现代 Visual Studio 功能/库/命名空间等的内容...

我需要确定 WAV 音频文件的持续时间(时间长度)。我想在几毫秒内得到结果 - 我可以从那里根据我的需要格式化数据。

您能否提供一个示例来说明如何做到这一点?

谢谢

最佳答案

对于那些寻找此解决方案的人,这里是调试后的代码。 请注意,此处不包括毫秒到时间字符串的转换。 另请注意,根据对此帖子的评论,没有标准的 Visual Basic 库可以导出 WAV 文件的持续时间。


选项显式打开 选项严格开启 导入 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

关于.net - 在 Visual Studio 2019 中确定 WAV 持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65588931/

相关文章:

vb.net - 理解一段代码

audio - CORONA SDK:随机播放声音

audio - 如何从原始 16 位、44100 赫兹、立体声 PCM 获得正确的声音(立体声)

winapi - 如何捕获正在播放的音频?

c# - 在 Windows 窗体中托管 WPF 时出现问题 - C#

c# - 为什么我的 Close 函数没有被调用?

.net - 解决方法 BeforeNavigate2 在 WPF .NET 中未触发

.net - Windows 窗体应用程序的颜色选择器

c# - VB.NET,创建一个返回类型为泛型的函数?

.net - 如何在VB.NET中跟踪鼠标单击和拖动事件?