C#获取WAVE文件文件头信息

发表于:2007-06-21来源:作者:点击数: 标签:
using System; using System.IO; using System.Text; namespace WAV { /// summary /// Summary description for Wav. /// /summary public class Wav {public Wav() {// // TODO: Add constructor logic here // } [STAThread] static void Main(string[] a

   
using System;

using System.IO;

using System.Text;

namespace WAV

{

/// <summary>

/// Summary description for Wav.

/// </summary>

public class Wav

{public Wav()

{   //

// TODO: Add constructor logic here

//

}

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

string strpath=@"C:\Documents and Settings\Administrator\×à??\trojan\?3????ó?.wav";//=@"F:\Music";

if(args.Length>0)

{ strpath=args[0].Trim();

}

if(File.Exists(strpath))

{   GetWavInfo(strpath);

Console.WriteLine("GetWavInfo Suclearcase/" target="_blank" >ccessfully!");

//Console.WriteLine("");

}

else

{   Console.Write("Please Enter the write filepath!\n");

Console.Write("ó?·¨: WAV [Full Path Of Your WAV filepath]");

}

}

public struct WavInfo

{  public string groupid;

public string rifftype;

public long filesize;

public string chunkid;

public long chunksize;

public short wformattag; //????×?′?éùò?μ???ê?′úo?£?àyè?WAVE_FORMAT_PCM£?WAVE_F0RAM_ADPCMμèμè?£

public ushort wchannels; //????éùò?μ??μμàêy?£

public ulong  dwsamplespersec;//????????è??ùêy?£

public ulong  dwavgbytespersec;//????????μ?êy?Yá??£

public ushort wblockalign;//???????éμ?????μ¥???£

public ushort wbitspersample;//????????è??ù?ùDèμ????aêy?£

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);

System.BitConverter.ToInt32(bInfo,4);

wavInfo.filesize = System.BitConverter.ToInt32(bInfo,4);

//wavInfo.filesize = Convert.ToInt64(System.Text.Encoding.Default.GetString(bInfo,4,4));

wavInfo.rifftype = System.Text.Encoding.Default.GetString(bInfo,8,4);

wavInfo.chunkid = System.Text.Encoding.Default.GetString(bInfo,12,4);

wavInfo.chunksize = System.BitConverter.ToInt32(bInfo,16);

wavInfo.wformattag = System.BitConverter.ToInt16(bInfo,20);

wavInfo.wchannels = System.BitConverter.ToUInt16(bInfo,22);

wavInfo.dwsamplespersec = System.BitConverter.ToUInt32(bInfo,24);

wavInfo.dwavgbytespersec = System.BitConverter.ToUInt32(bInfo,28);

wavInfo.wblockalign = System.BitConverter.ToUInt16(bInfo,32);

wavInfo.wbitspersample = System.BitConverter.ToUInt16(bInfo,34);

wavInfo.datachunkid = System.Text.Encoding.Default.GetString(bInfo,36,4);

wavInfo.datasize = System.BitConverter.ToInt32(bInfo,40);

System.Console.WriteLine("groupid:"+wavInfo.groupid);

System.Console.WriteLine("filesize:"+wavInfo.filesize);

System.Console.WriteLine("rifftype:"+wavInfo.rifftype);

System.Console.WriteLine("chunkid:"+wavInfo.chunkid);

System.Console.WriteLine("chunksize:"+wavInfo.chunksize);

System.Console.WriteLine("wformattag:"+wavInfo.wformattag);

System.Console.WriteLine("wchannels:"+wavInfo.wchannels);

System.Console.WriteLine("dwsamplespersec:"+wavInfo.dwsamplespersec);

System.Console.WriteLine("dwavgbytespersec:"+wavInfo.dwavgbytespersec);

System.Console.WriteLine("wblockalign:"+wavInfo.wblockalign);

System.Console.WriteLine("wbitspersample:"+wavInfo.wbitspersample);

System.Console.WriteLine("datachunkid:"+wavInfo.datachunkid);

System.Console.WriteLine("datasize:"+wavInfo.datasize);

}

}

}

}

}

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)