码迷,mamicode.com
首页 > 其他好文 > 详细

音频文件解析(一):WAV格式文件头部解析

时间:2015-04-07 00:39:21      阅读:519      评论:0      收藏:0      [点我收藏+]

标签:

  WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范,用于保存Windows平台的音频信息资源.


1.RIFF块(RIFF-Chunk)

偏移地址 字节数 数据类型 内容
&H00 4 String ‘RIFF‘文件标志
&H04 4 UInteger 文件总长
&H08  4  String  ‘WAVE‘文件标志

 

 

 

 

2.格式化块(Format-Chunk)

偏移地址 字节数 数据类型 内容
&H0C 4 String ‘fmt‘标志
&H10 4 UInteger 块长度
 &H12   2  UShort PCM格式类别 
&H14 2  UShort  声道数目 
&H18 4  UInteger  采样率 
 &H1C  4  UInteger  传输速率 
 &H1E  2 UShort   数据块对齐 
&H20 2 UShort   每样本bit数 
 &H22  2 UShort   可选 

 

 

 

 

 

 

 

 

 

3.附加块(Fact-Chunk) *当前块偏移地址需要根据实际数据变更

偏移地址 字节数 数据类型 内容
&H26 4 String ‘fact‘标志
&H2A 4 UInteger 块长度
&H2E 4 UInteger  附加信息

 

 

 

 

4.数据块(Data-Chunk) *当前块偏移地址需要根据实际数据变更

偏移地址 字节数 数据类型 内容
&H32 4 String ‘data‘文件标志
&H36 4 UInteger

数据块总长

 

 

 

 


0.构建一个waveHeaderStructre

    Structure waveHeaderStructre
        RiffChunk
        Dim RIFF As String
        Dim FileSize As UInteger
        Dim WAVE As String
        FormatChunk
        Dim FORMAT As String
        Dim FormatSize As UInteger
        Dim FilePadding As UShort
        Dim FormatChannels As UShort
        Dim SamplesPerSecond As UInteger
        Dim AverageBytesPerSecond As UInteger
        Dim BytesPerSample As UShort
        Dim BitsPerSample As UShort
        Dim FormatExtra As UShort
        FactChunk
        Dim FACT As String
        Dim FactSize As UInteger
        Dim FactInf As UInteger
        DataChunk
        Dim DATA As String
        Dim DataSize As UInteger
    End Structure

1.打开一个wav文件

  Dim data() As Byte = My.Computer.FileSystem.ReadAllBytes(wavFileName)

2.头部信息解析

  Public Sub SplitWaveData(ByVal data As Byte()) 提取wav文件头部信息
        Dim tempIndex As UShort = 0
        waveHeadInf.RIFF = CType(System.Text.Encoding.ASCII.GetChars(data, 0, 4), String)
        waveHeadInf.FileSize = System.BitConverter.ToUInt32(data, 4)
        waveHeadInf.WAVE = CType(System.Text.Encoding.ASCII.GetChars(data, 8, 4), String)
        FormatChunk
        waveHeadInf.FORMAT = CType(System.Text.Encoding.ASCII.GetChars(data, 12, 4), String)
        waveHeadInf.FormatSize = System.BitConverter.ToUInt32(data, 16)
        waveHeadInf.FilePadding = System.BitConverter.ToUInt16(data, 20)
        waveHeadInf.FormatChannels = System.BitConverter.ToUInt16(data, 22)
        waveHeadInf.SamplesPerSecond = System.BitConverter.ToUInt32(data, 24)
        waveHeadInf.AverageBytesPerSecond = System.BitConverter.ToUInt32(data, 28)
        waveHeadInf.BytesPerSample = System.BitConverter.ToUInt16(data, 32)
        waveHeadInf.BitsPerSample = System.BitConverter.ToUInt16(data, 34)
        If waveHeadInf.FormatSize = 18 Then
            waveHeadInf.FormatExtra = System.BitConverter.ToUInt16(data, 36)
        Else
            waveHeadInf.FormatExtra = 0
        End If
        tempIndex = 20 + waveHeadInf.FormatSize
        FactChunk
        waveHeadInf.FACT = CType(System.Text.Encoding.ASCII.GetChars(data, tempIndex, 4), String)
        If waveHeadInf.FACT = "fact" Then
            waveHeadInf.FactSize = System.BitConverter.ToUInt32(data, tempIndex + 4)
            waveHeadInf.FactInf = IIf(waveHeadInf.FactSize = 2, System.BitConverter.ToUInt16(data, tempIndex + 8), System.BitConverter.ToUInt32(data, tempIndex + 8))
            tempIndex = tempIndex + waveHeadInf.FactSize + 8
        Else
            waveHeadInf.FACT = "NULL"
            waveHeadInf.FactSize = 0
            waveHeadInf.FactInf = 0
        End If
        DataChunk
        waveHeadInf.DATA = CType(System.Text.Encoding.ASCII.GetChars(data, tempIndex, 4), String)
        waveHeadInf.DataSize = System.BitConverter.ToUInt32(data, tempIndex + 4)
        ShowWaveHeadInf()
    End Sub

3.ListView显示头部信息

    Public Sub ShowWaveHeadInf() 显示wav文件详细信息
        Dim Chunks As New List(Of String)
        Chunks.Add("RIFF标志-" & waveHeadInf.RIFF)
        Chunks.Add("文件长度-" & Hex(waveHeadInf.FileSize))
        Chunks.Add("WAVE标志-" & waveHeadInf.WAVE)
                Chunks.Add("FORMAT标志-" & waveHeadInf.FORMAT)
        Chunks.Add("Format长度-" & Hex(waveHeadInf.FormatSize))
        Chunks.Add("编码方式-" & Hex(waveHeadInf.FilePadding))
        Chunks.Add("声道数目-" & Hex(waveHeadInf.FormatChannels))
        Chunks.Add("采样频率-" & Hex(waveHeadInf.SamplesPerSecond))
        Chunks.Add("每秒所需字节数-" & Hex(waveHeadInf.AverageBytesPerSecond))
        Chunks.Add("数据块对齐单位-" & Hex(waveHeadInf.BytesPerSample))
        Chunks.Add("采样所需Bit数-" & Hex(waveHeadInf.BitsPerSample))
        Chunks.Add("附加信息-" & waveHeadInf.FormatExtra)
                Chunks.Add("FACT标志-" & waveHeadInf.FACT)
        Chunks.Add("FACT长度-" & Hex(waveHeadInf.FactSize))
        Chunks.Add("FACT信息-" & waveHeadInf.FactInf)
                Chunks.Add("DATA标志-" & waveHeadInf.DATA)
        Chunks.Add("DATA长度-" & Hex(waveHeadInf.DataSize))
        InfForm.ListView1.Items.Clear()
        For Each stringlist In Chunks
            ListView1.Items.Add(Split(stringlist, "-")(0))
            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Split(stringlist, "-")(1))
        Next
    End Sub

 

音频文件解析(一):WAV格式文件头部解析

标签:

原文地址:http://www.cnblogs.com/experdot/p/4396980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!