asp.net c# wave文件分析
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class admin_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public struct WavInfo
{
public string groupid;
public string rifftype;
public long filesize;
public string chunkid;
public long chunksize;
public short wformattag; //记录着此声音的格式代号,例如WAVE_FORMAT_PCM,WAVE_F0RAM_ADPCM等等。
public ushort wchannels; //记录声音的频道数。
public ulong dwsamplespersec;//记录每秒取样数。
public ulong dwavgbytespersec;//记录每秒的数据量。
public ushort wblockalign;//记录区块的对齐单位。
public ushort wbitspersample;//记录每个取样所需的位元数。
public string datachunkid;
public long datasize;
}
public static void GetWavInfo(string strpath)
{
WavInfo wavInfo = new WavInfo();
FileInfo fi = new FileInfo(strpath);
System.IO.FileStream fs = fi.OpenRead();
if (fs.Length >= 44)
{
byte[] bInfo = new byte[44];
fs.Read(bInfo, 0, 44);
System.Text.Encoding.Default.GetString(bInfo, 0, 4);
if (System.Text.Encoding.Default.GetString(bInfo, 0, 4) == "RIFF" && System.Text.Encoding.Default.GetString(bInfo, 8, 4) == "WAVE" && System.Text.Encoding.Default.GetString(bInfo, 12, 4) == "fmt ")
{
wavInfo.groupid = System.Text.Encoding.Default.GetString(bInfo, 0, 4);
BitConverter.ToInt32(bInfo, 4);
wavInfo.filesize = BitConverter.ToInt32(bInfo, 4);
wavInfo.rifftype = System.Text.Encoding.Default.GetString(bInfo, 8, 4);
wavInfo.chunkid = System.Text.Encoding.Default.GetString(bInfo, 12, 4);
wavInfo.chunksize = BitConverter.ToInt32(bInfo, 16);
wavInfo.wformattag = BitConverter.ToInt16(bInfo, 20);
wavInfo.wchannels = BitConverter.ToUInt16(bInfo, 22);
wavInfo.dwsamplespersec = BitConverter.ToUInt32(bInfo, 24);
wavInfo.dwavgbytespersec = BitConverter.ToUInt32(bInfo, 28);
wavInfo.wblockalign = BitConverter.ToUInt16(bInfo, 32);
wavInfo.wbitspersample = BitConverter.ToUInt16(bInfo, 34);
wavInfo.datachunkid = System.Text.Encoding.Default.GetString(bInfo, 36, 4);
wavInfo.datasize = BitConverter.ToInt32(bInfo, 40);
Console.WriteLine("groupid:" + wavInfo.groupid);
Console.WriteLine("filesize:" + wavInfo.filesize);
Console.WriteLine("rifftype:" + wavInfo.rifftype);
Console.WriteLine("chunkid:" + wavInfo.chunkid);
Console.WriteLine("chunksize:" + wavInfo.chunksize);
Console.WriteLine("wformattag:" + wavInfo.wformattag);
Console.WriteLine("wchannels:" + wavInfo.wchannels);
Console.WriteLine("dwsamplespersec:" + wavInfo.dwsamplespersec);
Console.WriteLine("dwavgbytespersec:" + wavInfo.dwavgbytespersec);
Console.WriteLine("wblockalign:" + wavInfo.wblockalign);
Console.WriteLine("wbitspersample:" + wavInfo.wbitspersample);
Console.WriteLine("datachunkid:" + wavInfo.datachunkid);
Console.WriteLine("datasize:" + wavInfo.datasize);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
GetWavInfo(FileUpload1.PostedFile.FileName);
}
}
//BitConverter.ToInt16(bInfo, 20) == 1 //pcm
//BitConverter.ToInt16(bInfo, 20) == 6 // CCITT A-Law
浙公网安备 33010602011771号