Using System; Using System.IO; Using System.Text; Using System.Windows.Forms; Using System.Runtime.InteropServices; namespace TRAIN.WAV2PCM {public partial class Frmmain:form {///<summary>///ERROR message///</summary> Const string errfilenotexits = "File is not Exits."; Const string Errfileisnotwave = "File is not wava."; <summary>///Wave hander Information///</summary> struct Headertype {public byte[] riff;/*riff class resource file header 4byte*/public UINT File_len; /* File length 4byte*/public byte[] wave; * * "WAVE" logo 4byte*/public byte[] FMT; /* "FMT" logo 4byte*/public uint NI1; /* Transition byte 4byte*/public ushort Format_type; /* Format category (10H for PCM form of sound data) 2byte*/public ushort channels; /*channels 1 = mono; 2 = stereo 2byte*/public UINT frequency; /* Sampling frequency 4byte*/public uint trans_speed; /* Audio data transfer rate 4byte*/public ushort DataBlock; /* The adjustment of the Block (by byte) 2byte*/public ushort Sample_bits; /* Sample Data bits (8/16) 2byte*/public byte[]; /* Data marker "4byte*/" public uint Wav_len; /* Voice data length 4byte*/} privAte Headertype Wavhander; Define a header structure private byte[] buff = new BYTE[44]; Header byte private byte[] Databuff; Data byte public frmmain () {InitializeComponent (); Initialstruct (); ///<summary>///Initialize the array length in the struct body, allocating memory///</summary> private void Initialstruct () {Wavhander.riff = new byte[ 4];//riff wavhander.wave = new Byte[4];//wave wavhander.fmt = new Byte[4];//fmt Wavhander.data = new Byte[4];//data}/// <summary>///read PCM data,///</summary>///<param name= "filepath" > File path </param>///<returns& gt; Read successful return true </returns> private bool READPCM (string filepath) {string filename = filepath;//Temporary save file name if (file.exists F Ilename = = false)//file does not exist {throw new Exception (errfilenotexits);}//Read-only mode open FileStream file = new FileStream (FileName, Fi Lemode.open, FileAccess.Read, Fileshare.none); if (file = = null)//Open Success {file. Close ();//closes file throw new Exception (Errfileisnotwave); int filelen = (int) file. length;//Get file length Databuff = new byte[filelen+44];//AssignmentMemory file. Read (Databuff, filelen);//reading files, saving in memory file. Close ();//Turn off file return true; ///<summary>///to build file headers for PCM files, ready to convert to WAV files///</summary>///<returns> Build successful return True </returns> private bool Initheader () {Wavhander.riff = Encoding.ASCII.GetBytes ("riff");/*riff class resource file head 4byte*/wavhander.file_len= (UINT) (Databuff. Length); /* File length 4byte*/wavhander.wave=encoding.ascii.getbytes ("wave"); * * "WAVE" logo 4byte*/wavhander.fmt=encoding.ascii.getbytes ("FMT"); /* "FMT" logo 4byte*/wavhander.ni1 =0x10; /* Transition byte 4byte*/wavhander.format_type = 0x01; /* Format category (10H for PCM form of sound data) 2byte*/wavhander.channels = 0x01; /*channels 1 = mono; 2 = stereo 2byte*/wavhander.frequency = 0x1f40; /* Sampling frequency 4byte*/wavhander.trans_speed = 0x3e80; /* Audio data transfer rate 4byte*/Wavhander.datablock = 0x02; /* Data block adjustment (by byte) 2byte*/wavhander.sample_bits = 0x10; /* Sample Data bits (8/16) 2byte*/wavhander.data = Encoding.ASCII.GetBytes ("Data"); /* Data marker "4byte*/" Wavhander.wav_len = (UINT) (Databuff. LENGTH-44); /* The length of the speech data 4byte*/byte[] byt2;//temporary variables,Saves a 2-bit integer byte[] byt4;//a temporary variable, saving a 4-bit integer Encoding.ASCII.GetBytes (Encoding.ASCII.GetString (Wavhander.riff), 0,4,databuff, 0)/*riff type resource file head 4byte*/byt4 = bitconverter.getbytes (Wavhander.file_len); /* File length 4byte*/array.copy (byt4, 0, Databuff, 4, 4); Encoding.ASCII.GetBytes (Encoding.ASCII.GetString (Wavhander.wave), 0, 4, Databuff, 8);/* "Wave" flag 4byte*/ Encoding.ASCII.GetBytes (Encoding.ASCII.GetString (WAVHANDER.FMT), 0, 4, Databuff,);/* "FMT" flag 4byte*/BYT4 = Bitconverter.getbytes (WAVHANDER.NI1); * Transition byte 4byte*/array.copy (byt4, 0, databuff,16,4); Byt2 = Bitconverter.getbytes (Wavhander.format_type);///* Format category (10H for PCM form of sound data) 2byte*/array.copy (byt2, 0, Databuff, 20,2 ); Byt2 = Bitconverter.getbytes (wavhander.channels);/*channels 1 = mono; 2 = Stereo 2byte*/array.copy (byt2, 0, Databuff, 22, 2); BYT4 = Bitconverter.getbytes (wavhander.frequency);/* Sampling frequency 4byte*/array.copy (byt4, 0, Databuff, 24, 4); BYT4 = Bitconverter.getbytes (wavhander.trans_speed);/* Audio data transfer rate 4byte*/array.copy (byt4, 0, Databuff, 28, 4); Byt2 = BitConverter.getbytes (Wavhander.datablock); * The number of adjustments (in bytes) of the data Block 2byte*/array.copy (byt2, 0, Databuff, 32, 2); Byt2 = Bitconverter.getbytes (wavhander.sample_bits);/* Sample data digits (8/16) 2byte*/array.copy (byt2, 0, Databuff, 34, 2); Encoding.ASCII.GetBytes (Encoding.ASCII.GetString (Wavhander.data), 0, 4, Databuff, 36);/* Data marker "date" 4byte*/Byt4 = Bitconverter.getbytes (Wavhander.wav_len); /* Voice data length 4byte*/array.copy (byt4, 0, Databuff, 40, 4); return true; ///<summary>///read wave files, including header and data parts///</summary>///<param name= "filepath" > File path </param>/ <returns> Read successful return true </returns> private bool Readwav (string filepath) {string filename = filepath;//Save file name if (F Ile. Exists (fileName) ==false)//file does not exist {throw new Exception (errfilenotexits);}//Read-only open files FileStream file = new FileStream (fil ename, FileMode.Open, FileAccess.Read, Fileshare.none); if (File==null | |) file. LENGTH<44)//length less than 44, or open failed {file. Close ();//throw new Exception (Errfileisnotwave); } file. Read (Buff, 0, 44)//Read the file header if (fixeddata (buff) = false)//to save the header information by location to the structure throw new Exception (Errfileisnotwave); Databuff=new byte[wavhander.wav_len];//Allocate memory try {file. Read (databuff, 0, Databuff. LENGTH);//Read file data to return true; catch {return false;} finally {file. Close ();//Closed file}}///<summary>///Save header array information to structure///</summary>///<param name= "pbuff" > File header array </p Aram>///<returns> Save successfully returns True </returns> bool Fixeddata (byte[] pbuff) {array.copy (Pbuff,0,wavhander.riff, 0,4);/if (Encoding.ASCII.GetString (Wavhander.riff)!= "riff")//Determine file is Wava type return false; Wavhander.file_len=bitconverter.touint32 (pbuff,4); Array.copy (pbuff,8,wavhander.wave,0,4); Array.copy (pbuff,12,wavhander.fmt,0,4); Wavhander.ni1 = Bitconverter.touint32 (Pbuff, 16); Wavhander.format_type=bitconverter.touint16 (pbuff,20); Wavhander.channels=bitconverter.touint16 (pbuff,22); Wavhander.frequency=bitconverter.touint32 (pbuff,24); Wavhander.trans_speed=bitconverter.touint32 (pbuff,28); Wavhander.datablock=bitconverter.touint16 (pbuff,32); Wavhander.sample_bits=bitconverter.touint16 (pbuff,34); Array.copy (pbuff,36,wavhander.data,0,4); Wavhander.wav_len=bitconverter.touint32 (pbuff,40); return true; ///<summary>///get filename to be converted/button1 && button3///</summary>///<param name= "Sender" ></p aram>///<param name= "E" ></param> private void Button1_Click (object sender, EventArgs e) {button b = (But ton) sender; if (B.name = = "Button1") txtwav2pcm_wav. Text = GetOpenFileName ("WAV"); Else TXTPCM2WAV_PCM. Text = GetOpenFileName ("PCM"); ///<summary>///Open the dialog box to get the file path that was opened///</summary>///<param name= "type" >wav && Pcm</param >///<returns> filename </returns> private string GetOpenFileName (String type) { Openfiledialog1.initialdirectory = "c://";//initial path openfiledialog1.title = "";//initial file name is empty if (type = = "WAV")//wav file is open Openfiledialog1.filter = "All File|*.*|wav|*.wav"; else Openfiledialog1.filter = "All FILE|*.*|PCM|*.PCM "; Openfiledialog1.filterindex = 2; if (openfiledialog1.showdialog () = = DialogResult.OK) return openfiledialog1.filename; else return "";} private string GetSaveFileName (String type) {savefiledialog1.initialdirectory = "c://";//initial path Savefiledialog1.title = " ";//initial file name if (type = =" WAV ")//wav file is saved savefiledialog1.filter =" All File|*.*|wav|*.wav "; else Savefiledialog1.filter = "All FILE|*.*|PCM|*.PCM"; Savefiledialog1.filterindex = 2; if (savefiledialog1.showdialog () = = DialogResult.OK) return savefiledialog1.filename; else return "";} <summary>///Get file saved name button2&& button4///</summary>///<param name= "Sender" ></ param>///<param name= "E" ></param> private void button2_click (object sender, EventArgs e) {button b = (Bu Tton) sender; if (B.name = = "Button2") txtwav2pcm_pcm. Text = GetSaveFileName ("PCM"); Else Txtpcm2wav_wav. Text = GetSaveFileName ("WAV"); }///<summary>///wav->pcm///</summary>///<param name= "seNDEr "></param>///<param name=" E "></param> private void Btnwav2pcm_click (object sender, EventArgs e) {try {if (readwav) (txtwav2pcm_wav. Text) = = True) {WriteFile (TXTWAV2PCM_PCM). Text, Databuff); LSTMESSAGE.ITEMS.ADD ("PCM file conversion succeeded.") "+ System.DateTime.Now.ToString ());}} catch (Exception ex) {LSTMESSAGE.ITEMS.ADD (ex. message); }///<summary>///write file operations///</summary>///<param name= "filename" > File path </param>///<param n Ame= "Pbuff" > File data </param> private void WriteFile (string filename,byte[] pbuff) {if (file.exists (filename) = = true) file.delete (filename); FileStream sw=file.openwrite (filename); if (pbuff!= null && SW!= null) {sw. Write (pbuff, 0, Pbuff. Length); Sw. Close (); }///<summary>///pcm->wav///</summary>///<param name= "sender" ></param>///<param N Ame= "E" ></param> private void Btnpcm2wav_click (object sender, EventArgs e) {try {if (READPCM). Text) && Initheader ()) {WriteFile (txtpcm2wav_wav). Text, Databuff); LSTMESSAGE.ITEMS.ADD ("Wava file conversion succeeded.) "+ System.DateTime.Now.ToString ());}} catch (Exception ex) {LSTMESSAGE.ITEMS.ADD (ex. message); } } } }
There is nothing to do today, write a wav "= =" PCM file conversion program. Let's announce that we learn together. The PCM file is not played and is not known to be successful. only when studying.