博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 获取wave文件信息【转】
阅读量:6194 次
发布时间:2019-06-21

本文共 7959 字,大约阅读时间需要 26 分钟。

  

public class WaveHelper    {        ///         /// 数据流        ///         private Stream m_WaveData;        private bool m_WaveBool = false;        private RIFF_WAVE_Chunk _Header = new RIFF_WAVE_Chunk();        private Format_Chunk _Format = new Format_Chunk();        private Fact_Chunk _Fact = new Fact_Chunk();        private Data_Chunk _Data = new Data_Chunk();        public WaveHelper(string WaveFileName)        {            m_WaveData = new FileStream(WaveFileName, FileMode.Open);            try            {                LoadWave();                m_WaveData.Close();            }            catch            {                m_WaveData.Close();            }        }        public WaveHelper(Stream WaveStream)        {            m_WaveData = WaveStream;            try            {                LoadWave();                m_WaveData.Close();            }            catch            {                m_WaveData.Close();            }        }        private void LoadWave()        {            #region RIFF_WAVE_Chunk            byte[] _Temp4 = new byte[4];            byte[] _Temp2 = new byte[2];            m_WaveData.Read(_Temp4, 0, 4);            if (_Temp4[0] != _Header.szRiffID[0] || _Temp4[1] != _Header.szRiffID[1] || _Temp4[2] != _Header.szRiffID[2] || _Temp4[3] != _Header.szRiffID[3]) return;            m_WaveData.Read(_Temp4, 0, 4);            _Header.dwRiffSize = BitConverter.ToUInt32(_Temp4, 0);            m_WaveData.Read(_Temp4, 0, 4);            if (_Temp4[0] != _Header.szRiffFormat[0] || _Temp4[1] != _Header.szRiffFormat[1] || _Temp4[2] != _Header.szRiffFormat[2] || _Temp4[3] != _Header.szRiffFormat[3]) return;            #endregion            #region Format_Chunk            m_WaveData.Read(_Temp4, 0, 4);            if (_Temp4[0] != _Format.ID[0] || _Temp4[1] != _Format.ID[1] || _Temp4[2] != _Format.ID[2]) return;            m_WaveData.Read(_Temp4, 0, 4);            _Format.Size = BitConverter.ToUInt32(_Temp4, 0);            long _EndWave = _Format.Size + m_WaveData.Position;            m_WaveData.Read(_Temp2, 0, 2);            _Format.FormatTag = BitConverter.ToUInt16(_Temp2, 0);            m_WaveData.Read(_Temp2, 0, 2);            _Format.Channels = BitConverter.ToUInt16(_Temp2, 0);            m_WaveData.Read(_Temp4, 0, 4);            _Format.SamlesPerSec = BitConverter.ToUInt32(_Temp4, 0);            m_WaveData.Read(_Temp4, 0, 4);            _Format.AvgBytesPerSec = BitConverter.ToUInt32(_Temp4, 0);            m_WaveData.Read(_Temp2, 0, 2);            _Format.BlockAlign = BitConverter.ToUInt16(_Temp2, 0);            m_WaveData.Read(_Temp2, 0, 2);            _Format.BitsPerSample = BitConverter.ToUInt16(_Temp2, 0);            m_WaveData.Position += _EndWave - m_WaveData.Position;            #endregion            m_WaveData.Read(_Temp4, 0, 4);            if (_Temp4[0] == _Fact.ID[0] && _Temp4[1] == _Fact.ID[1] && _Temp4[2] == _Fact.ID[2] && _Temp4[3] == _Fact.ID[3])            {                #region  Fact_Chunk                m_WaveData.Read(_Temp4, 0, 4);                _Fact.Size = BitConverter.ToUInt32(_Temp4, 0);                m_WaveData.Position += _Fact.Size;                #endregion                m_WaveData.Read(_Temp4, 0, 4);            }            if (_Temp4[0] == _Data.ID[0] && _Temp4[1] == _Data.ID[1] && _Temp4[2] == _Data.ID[2] && _Temp4[3] == _Data.ID[3])            {                #region Data_Chunk                m_WaveData.Read(_Temp4, 0, 4);                _Data.Size = BitConverter.ToUInt32(_Temp4, 0);                _Data.FileBeginIndex = m_WaveData.Position;                _Data.FileOverIndex = m_WaveData.Position + _Data.Size;                m_Second = (double)_Data.Size / (double)_Format.AvgBytesPerSec;                #endregion            }            m_WaveBool = true;        }        #region 文件定义        ///         /// 文件头        ///         private class RIFF_WAVE_Chunk        {            ///             /// 文件前四个字节 为RIFF            ///             public byte[] szRiffID = new byte[] { 0x52, 0x49, 0x46, 0x46 };   // 'R','I','F','F'            ///             /// 数据大小 这个数字等于+8 =文件大小            ///             public uint dwRiffSize = 0;            ///             ///WAVE文件定义 为WAVE            ///             public byte[] szRiffFormat = new byte[] { 0x57, 0x41, 0x56, 0x45 }; // 'W','A','V','E'                 }        ///         /// 声音内容定义        ///         private class Format_Chunk        {            ///             /// 固定为  是"fmt "字后一位为0x20            ///             public byte[] ID = new byte[] { 0x66, 0x6D, 0x74, 0x20 };            ///             /// 区域大小            ///             public uint Size = 0;            ///             /// 记录着此声音的格式代号,例如1-WAVE_FORMAT_PCM, 2-WAVE_F0RAM_ADPCM等等。             ///             public ushort FormatTag = 1;            ///             /// 声道数目,1--单声道;2--双声道            ///             public ushort Channels = 2;            ///             /// 采样频率  一般有11025Hz(11kHz)、22050Hz(22kHz)和44100Hz(44kHz)三种            ///             public uint SamlesPerSec = 0;            ///             /// 每秒所需字节数            ///             public uint AvgBytesPerSec = 0;            ///             /// 数据块对齐单位(每个采样需要的字节数)            ///             public ushort BlockAlign = 0;            ///             /// 音频采样大小             ///             public ushort BitsPerSample = 0;            ///             /// ???            ///             public byte[] Temp = new byte[2];        }        ///         /// FACT        ///         private class Fact_Chunk        {            ///             /// 文件前四个字节 为fact            ///             public byte[] ID = new byte[] { 0x66, 0x61, 0x63, 0x74 };   // 'f','a','c','t'            ///             /// 数据大小            ///             public uint Size = 0;            ///             /// 临时数据            ///             public byte[] Temp;        }        ///         /// 数据区        ///         private class Data_Chunk        {            ///             /// 文件前四个字节 为RIFF            ///             public byte[] ID = new byte[] { 0x64, 0x61, 0x74, 0x61 };   // 'd','a','t','a'            ///             /// 大小            ///             public uint Size = 0;            ///             /// 开始播放的位置            ///             public long FileBeginIndex = 0;            ///             /// 结束播放的位置            ///             public long FileOverIndex = 0;        }        #endregion        #region 属性        ///         /// 是否成功打开文件        ///         public bool WaveBool { get { return m_WaveBool; } }        private double m_Second = 0;        ///         /// 秒单位        ///         public double Second { get { return m_Second; } }        ///         /// 格式        ///         public string FormatTag        {            get            {                switch (_Format.FormatTag)                {                    case 1:                        return "PCM";                    case 2:                        return "Microsoft ADPCM";                    default:                        return "Un";                }            }        }        ///         /// 频道        ///         public ushort Channels { get { return _Format.Channels; } }        ///         /// 采样级别        ///         public string SamlesPerSec        {            get            {                //switch (_Format.SamlesPerSec)                //{                //    case 11025:                //        return "11kHz";                //    case 22050:                //        return "22kHz";                //    case 44100:                //        return "44kHz";                //    default:                //        return _Format.SamlesPerSec.ToString() + "Hz";                //}                return _Format.SamlesPerSec.ToString();            }        }        ///         /// 采样大小(比特率)        ///         public ushort BitsPerSample { get { return _Format.BitsPerSample; } }        #endregion    }

 

转载于:https://www.cnblogs.com/mahatmasmile/p/3493038.html

你可能感兴趣的文章
yii 10.16
查看>>
python3.4学习笔记(四) 3.x和2.x的区别,持续更新
查看>>
SPOJ QTREE4 lct
查看>>
音乐还在陪伴我
查看>>
Sql Server参数化查询之where in和like实现详解
查看>>
高性能负载均衡之分类架构
查看>>
8分钟学会Consul集群搭建及微服务概念
查看>>
【转】理解红黑树
查看>>
OBJEct-c中NSDictionary的用法
查看>>
Safari/Chrome中placeholder属性实现不完整
查看>>
转载 - 18个最佳代码编辑器/IDE推荐
查看>>
用Opencv保存视频文件avi(转)
查看>>
几条常见的数据库分页 SQL 语句
查看>>
XCode最佳实践之最佳数据类型
查看>>
asp.net 中sender 的理解
查看>>
RSS文章订阅及生成RSS格式的xml
查看>>
你自认为理解了JavaScript?
查看>>
读《程序员的SQL金典》[4]--SQL调优
查看>>
死锁产生的原因及四个必要条件
查看>>
CSS3----background:-webkit-gradient()渐变效果
查看>>