首 页IT知识库收藏内容
当前位置:翔宇亭IT乐园IT知识库C#多媒体编程

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

减小字体 增大字体 作者:不详  来源:转载  发布时间:2010-11-21 21:02:53

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=@"D:\music\jintian.wav";//=@"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);

}

}

}

}

}

 


本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。

个人成长离不开各位的关注,你的关注就是我继续前行的动力。

知识评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
愿您的精彩评论引起共鸣,带来思考和价值。
用户名: 查看更多评论
分 值:100分 90分 80分 70分 60分 40分 20分
内 容:
验证码:
关于本站 | 网站帮助 | 广告合作 | 网站声明 | 友情连接 | 网站地图
本站部分内容来自互联网,如有侵权,请来信告之,谢谢!
Copyright © 2007-2024 biye5u.com. All Rights Reserved.