class WAVReader
{
#region RIFF WAVE Chunk
private string Id; //文件标识
private double Size; //文件大小
private string Type; //文件类型
#endregion #region Format Chunk
private string formatId;
private double formatSize; //数值为16或18,18则最后又附加信息
private int formatTag;
private int num_Channels; //声道数目,1--单声道;2--双声道
private int SamplesPerSec; //采样率
private int AvgBytesPerSec; //每秒所需字节数
private int BlockAlign; //数据块对齐单位(每个采样需要的字节数)
private int BitsPerSample; //每个采样需要的bit数
private string additionalInfo; //附加信息(可选,通过Size来判断有无)
/*
* 以'fmt'作为标示。一般情况下Size为16,此时最后附加信息没有;
* 如果为18则最后多了2个字节的附加信息。
* 主要由一些软件制成的wav格式中含有该2个字节的附加信息
*/
#endregion #region Fact Chunk(可选)
/*
* Fact Chunk是可选字段,一般当wav文件由某些软件转化而成,则包含该Chunk。
*/
private string factId;
private int factSize;
private string factData;
#endregion #region Data Chunk
private string dataId;
private int dataSize;
private List<double> wavdata = new List<double>(); //默认为单声道
#endregion /// <summary>
/// 读取波形文件并显示
/// </summary>
/// <param name="filePath"></param>
public void ReadWAVFile(string filePath)
{
if (filePath == "") return;
byte[] id = new byte[];
byte[] size = new byte[];
byte[] type = new byte[]; byte[] formatid = new byte[];
byte[] formatsize = new byte[];
byte[] formattag = new byte[];
byte[] numchannels = new byte[];
byte[] samplespersec = new byte[];
byte[] avgbytespersec = new byte[];
byte[] blockalign = new byte[];
byte[] bitspersample = new byte[];
byte[] additionalinfo = new byte[]; //可选 byte[] factid = new byte[];
byte[] factsize = new byte[];
byte[] factdata = new byte[]; byte[] dataid = new byte[];
byte[] datasize = new byte[]; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8))
{
#region RIFF WAVE Chunk
br.Read(id, , );
br.Read(size, , );
br.Read(type, , ); this.Id = getString(id, );
long longsize = bytArray2Int(size);//十六进制转为十进制
this.Size = longsize * 1.0;
this.Type = getString(type, );
#endregion #region Format Chunk
br.Read(formatid, , );
br.Read(formatsize, , );
br.Read(formattag, , );
br.Read(numchannels, , );
br.Read(samplespersec, , );
br.Read(avgbytespersec, , );
br.Read(blockalign, , );
br.Read(bitspersample, , );
if (getString(formatsize, ) == "")
{
br.Read(additionalinfo, , );
this.additionalInfo = getString(additionalinfo, ); //附加信息
} this.formatId = getString(formatid, ); this.formatSize = bytArray2Int(formatsize); byte[] tmptag = composeByteArray(formattag);
this.formatTag = bytArray2Int(tmptag); byte[] tmpchanels = composeByteArray(numchannels);
this.num_Channels = bytArray2Int(tmpchanels); //声道数目,1--单声道;2--双声道 this.SamplesPerSec = bytArray2Int(samplespersec); //采样率 this.AvgBytesPerSec = bytArray2Int(avgbytespersec); //每秒所需字节数 byte[] tmpblockalign = composeByteArray(blockalign);
this.BlockAlign = bytArray2Int(tmpblockalign); //数据块对齐单位(每个采样需要的字节数) byte[] tmpbitspersample = composeByteArray(bitspersample);
this.BitsPerSample = bytArray2Int(tmpbitspersample); // 每个采样需要的bit数
#endregion #region Fact Chunk
//byte[] verifyFactChunk = new byte[2];
//br.Read(verifyFactChunk, 0, 2);
//string test = getString(verifyFactChunk, 2);
//if (getString(verifyFactChunk, 2) == "fa")
//{
// byte[] halffactId = new byte[2];
// br.Read(halffactId, 0, 2); // byte[] factchunkid = new byte[4];
// for (int i = 0; i < 2; i++)
// {
// factchunkid[i] = verifyFactChunk[i];
// factchunkid[i + 2] = halffactId[i];
// } // this.factId = getString(factchunkid, 4); // br.Read(factsize, 0, 4);
// this.factSize = bytArray2Int(factsize); // br.Read(factdata, 0, 4);
// this.factData = getString(factdata, 4);
//}
#endregion #region Data Chunk byte[] d_flag = new byte[];
while (true)
{
br.Read(d_flag, , );
if (getString(d_flag, ) == "d")
{
break;
} }
byte[] dt_id = new byte[];
dt_id[] = d_flag[];
br.Read(dt_id, , );
this.dataId = getString(dt_id, ); br.Read(datasize, , ); this.dataSize = bytArray2Int(datasize); List<string> testl = new List<string>(); if (BitsPerSample == )
{ for (int i = ; i < this.dataSize; i++)
{
byte wavdt = br.ReadByte();
wavdata.Add(wavdt);
Console.WriteLine(wavdt);
}
}
else if (BitsPerSample == )
{
for (int i = ; i < this.dataSize/; i++)
{
short wavdt = br.ReadInt16();
wavdata.Add(wavdt);
Console.WriteLine(wavdt);
}
}
#endregion
}
}
} /// <summary>
/// 数字节数组转换为int
/// </summary>
/// <param name="bytArray"></param>
/// <returns></returns>
private int bytArray2Int(byte[] bytArray)
{
return bytArray[] | (bytArray[] << ) | (bytArray[] << ) | (bytArray[] << );
} /// <summary>
/// 将字节数组转换为字符串
/// </summary>
/// <param name="bts"></param>
/// <param name="len"></param>
/// <returns></returns>
private string getString(byte[] bts, int len)
{
char[] tmp = new char[len];
for (int i = ; i < len; i++)
{
tmp[i] = (char)bts[i];
}
return new string(tmp);
} /// <summary>
/// 组成4个元素的字节数组
/// </summary>
/// <param name="bt"></param>
/// <returns></returns>
private byte[] composeByteArray(byte[] bt)
{
byte[] tmptag = new byte[] { , , , };
tmptag[] = bt[];
tmptag[] = bt[];
return tmptag;
}
}

最新文章

  1. Mysql的用户名密码设置方法
  2. PHP入门【一】$_SERVER
  3. 地图、定位 CLLocationManager CLGeocoder CLPlacemark
  4. 使用RMAN验证备份的有效性
  5. C语言中链表节点的实现,以及如何实现泛型
  6. Ajax大文件切割传输
  7. [设计模式] 16 迭代器模式 Iterator Pattern
  8. 利用AVL树实现搬箱问题的best fit策略
  9. C# 中的数组(array)
  10. 新增的output元素 progress元素 meter元素 keygen元素
  11. Bootstrap_排版_代码
  12. [Leetcode][Python]51: N-Queens
  13. qtcreator增加doxygen注释
  14. 制作U盘启动盘(以CentOS6.3为例)
  15. Linux经常使用命令(十一) - more
  16. 【提示框】【计时事件】【cookie】
  17. Github链接及git学习心得总结
  18. SQL_sql语言的学习
  19. 挂载本地ISO
  20. java 中几种常用数据结构

热门文章

  1. javascript 缓动返回顶部案例
  2. scala面向对象.高阶函数,柯里化,Actor编程简介
  3. golang基础数据结构链表
  4. fffmgg
  5. Windows句柄数限制
  6. 二、Internet地址结构
  7. Winfrom多文档界面实现
  8. Netty入门(3) - ChannelHandler
  9. pytorch中如何使用DataLoader对数据集进行批处理
  10. luogu P2515 [HAOI2010]软件安装