C # Get wave file header information

Source: Internet
Author: User

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

//

String strpath=@ "C:\Documents and settings\administrator\ desktop \trojan\ Miss comrades. 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 ("Usage: 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; Record the format code for this sound, such as WAVE_FORMAT_PCM,WAVE_F0RAM_ADPCM and so on.

public ushort Wchannels; The number of channels to record the sound.

The public ulong dwsamplespersec;//records the number of samples per second.

The public ulong dwavgbytespersec;//records the amount of data per second.

Public ushort wblockalign;//The alignment unit of the record block.

The public ushort wbitspersample;//records the number of bits required for each sample.

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

}

}

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.