解析WAV头部信息后,接下来就可以根据相关参数和DATA块数据绘制波形.

1.重新编码(转换为8bits,单声道数据)

  Public Function GetFormatData(ByVal pData() As Byte, ByVal pWaveHeader As waveHeaderStructre) As Byte()
        Dim temp As Integer
        Dim data() As Byte = {}
          Then
            )

                data(i) = pData(i)
            Next
          Then
             - )

                data(i) = pData(i * )
            Next
          Then
             - )

                temp = System.BitConverter.ToInt16(pData, i * ) /  +
                temp = , , temp)
                temp = , , temp)
                data(i) = temp
            Next
          Then
             - )

                temp = System.BitConverter.ToInt16(pData, i * ) /  +
                temp = , , temp)
                temp = , , temp)
                data(i) = temp
            Next
        End If
        Return data
    End Function

2.显示波形

    Public Sub PaintingWave(ByVal pData() As Byte, ByVal pWaveHeader As waveHeaderStructre, ByVal pBox As PictureBox)
        Dim pg As Graphics = pBox.CreateGraphics
        Dim pwidth As Integer = pBox.Width
        Dim pheight As Integer = pBox.Height
        Dim points As New List(Of PointF)
        Dim PImage As Bitmap = New Bitmap(pwidth, pheight)
        pg = Drawing.Graphics.FromImage(PImage)
         To pheight '绘制渐变背景
            pg.DrawLine( + Math.Abs(i / (pheight / ) - ), , , ), ), , i, pwidth, i)
        Next
          '波形的离散数据点
            points.Add()))
        Next
        pg.DrawLines(, , )), points.ToArray)
        pBox.Image = PImage
        pg.Dispose()
    End Sub

3.完整WAV分析封装类

  实现功能:头部信息解析,重新编码,波形可视化

  示例:

Class WaveClass
    Public waveHeadInf As waveHeaderStructre 'wav文件头部信息
    Dim fileData() As Byte '文件原始数据
    Public waveData() As Byte 'DATA块原始数据
    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
    Public Sub New(ByVal FileName As String)
        fileData = My.Computer.FileSystem.ReadAllBytes(FileName) '加载wav文件
        SplitWaveData(fileData) '分析头部,并获取DATA块数据
    End Sub
    Public Sub PaintWave(ByVal pBox As PictureBox, Optional ByVal pData() As Byte = Nothing) '指定PictureBox绘制波形
        If pData Is Nothing Then pData = GetFormatData()
        Dim pg As Graphics = pBox.CreateGraphics
        Dim pwidth As Integer = pBox.Width
        Dim pheight As Integer = pBox.Height
        Dim points As New List(Of PointF)
        Dim PImage As Bitmap = New Bitmap(pwidth, pheight)
        pg = Drawing.Graphics.FromImage(PImage)
         To pheight '绘制渐变背景
            pg.DrawLine( + Math.Abs(i / (pheight / ) - ), , , ), ), , i, pwidth, i)
        Next
          '波形的离散数据点
            points.Add()))
        Next
        pg.DrawLines(, , )), points.ToArray)
        pBox.Image = PImage
        pg.Dispose()
    End Sub
    Private Sub SplitWaveData(ByVal data As Byte()) '提取wav文件头部信息

        waveHeadInf.RIFF = , ), String)
        waveHeadInf.FileSize = System.BitConverter.ToUInt32(data, )
        waveHeadInf.WAVE = , ), String)
        'FormatChunk
        waveHeadInf.FORMAT = , ), String)
        waveHeadInf.FormatSize = System.BitConverter.ToUInt32(data, )
        waveHeadInf.FilePadding = System.BitConverter.ToUInt16(data, )
        waveHeadInf.FormatChannels = System.BitConverter.ToUInt16(data, )
        waveHeadInf.SamplesPerSecond = System.BitConverter.ToUInt32(data, )
        waveHeadInf.AverageBytesPerSecond = System.BitConverter.ToUInt32(data, )
        waveHeadInf.BytesPerSample = System.BitConverter.ToUInt16(data, )
        waveHeadInf.BitsPerSample = System.BitConverter.ToUInt16(data, )
         Then
            waveHeadInf.FormatExtra = System.BitConverter.ToUInt16(data, )
        Else
            waveHeadInf.FormatExtra =
        End If
        tempIndex =  + waveHeadInf.FormatSize
        'FactChunk
        waveHeadInf.FACT = ), String)
        If waveHeadInf.FACT = "fact" Then
            waveHeadInf.FactSize = System.BitConverter.ToUInt32(data, tempIndex + )
            waveHeadInf.FactInf = , System.BitConverter.ToUInt16(data, tempIndex + ), System.BitConverter.ToUInt32(data, tempIndex + ))
            tempIndex = tempIndex + waveHeadInf.FactSize +
        Else
            waveHeadInf.FACT = "NULL"
            waveHeadInf.FactSize =
            waveHeadInf.FactInf =
        End If
        'DataChunk
        waveHeadInf.DATA = ), String)
        waveHeadInf.DataSize = System.BitConverter.ToUInt32(data, tempIndex + )
        tempIndex = tempIndex +
        '提取DATA数据块
        ReDim waveData(data.Length - tempIndex)
        Array.Copy(data, tempIndex, waveData, , data.Length - tempIndex)
    End Sub
    Private Function GetFormatData() As Byte() '重新编码
        Dim temp As Integer
        Dim data() As Byte = {}
          Then
            )

                data(i) = waveData(i)
            Next
          Then
             - )

                data(i) = waveData(i * )
            Next
          Then
             - )

                temp = System.BitConverter.ToInt16(waveData, i * ) /  +
                temp = , , temp)
                temp = , , temp)
                data(i) = temp
            Next
          Then
             - )

                temp = System.BitConverter.ToInt16(waveData, i * ) /  +
                temp = , , temp)
                temp = , , temp)
                data(i) = temp
            Next
        End If
        Return data
    End Function
End Class
05-11 03:30