首页 > 开发 > 综合 > 正文

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

2024-07-21 02:17:59
字体:
来源:转载
供稿:网友

c#获取wave文件文件头信息



前些日子在论坛里问了,没人回答,今天有空自己写了一下

文件格式依据网站

http://www.moon-soft.com/program/format/



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 [email protected]"c:/documents and settings/administrator/桌面/trojan/怀念战友.wav";//[email protected]"f:/music";

if(args.length>0)

{

strpath=args[0].trim();

}

if(file.exists(strpath))

{

getwavinfo(strpath);

console.writeline("getwavinfo successfully!");

//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; //记录着此声音的格式代号,例如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);

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



}

}

}

}

}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表