音频文件解析(二):WAV格式文件波形绘制
2024-09-20 02:55:00
解析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分析封装类
实现功能:头部信息解析,重新编码,波形可视化
示例:
Dim SamplesWave As New WaveClass("F:\Music\李宗盛-山丘.wav")
SamplesWave.PaintWave(PictureBox1)
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
最新文章
- IBatis.Net项目数据库SqlServer迁移至Oracle经验
- phpcms中的RBAC权限系统
- 设计模式学习之简单工厂(Simple Factory,创建型模式)(1)
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
- (转)未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService~~导出!解决方案。
- 第一个shell编程,输出hello world!
- php 去掉 头尾 空格 2种方法
- csapp lab3 bufbomb 缓存区溢出攻击 《深入理解计算机系统》
- javascript之自增自减典型运算(易错)
- ssm客户管理系统的设计与实现
- 对EF的封装
- 【转】priority_queue优先队列
- MyEclipse自动补全
- tomcat8常用配置说明
- 一次掌握 React 与 React Native 两个框架
- Starting MySQL....The server quit without updating PID file[失败]/lib/mysql/ip12189.pid). 错误一例
- [C#]SmtpClient发送邮件
- BZOJ1915[USACO 2010 Open Gold 1.Cow Hopscotch]——DP+斜率优化
- vue相关操作命令
- Redis 启动警告错误解决
热门文章
- OSX 下搭建Asp.Net vNext的开发环境
- Oracle expdp/impdp导出导入命令及数据库备份
- 获取机器安装.NET版本的几种方式
- 学习设计模式第三 - 基础使用UML表示关系
- Unity3d热更新全书-加载(二)如何在不用AssetBundle的前提下动态加载预设
- synchronized同步对象锁
- MVVM架构~knockoutjs系列之数组的$index和$data
- 爱上MVC~AuthorizeAttribute验证不通过如何停止当前上下文
- Atitit 判断判断一张图片是否包含另一张小图片
- SVN命令模式批量更新多个项目