点击(此处)折叠或打开using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace WindowsWave{ class WaveFile { #region RIFF WAVE Chunk private string Id; //文件标识 private double Size; //文件大小 private string Type; //文件类型 #endregion #region Format Chunk private string formatId; private double formatSize; //数值为16或18,18则最后又附加信息 private int formatTag; private int num_Channels; //声道数目,1--单声道;2--双声道 private int SamplesPerSec; //采样率 private int AvgBytesPerSec; //每秒所需字节数 private int BlockAlign; //数据块对齐单位(每个采样需要的字节数) private int BitsPerSample; //每个采样需要的bit数 private string additionalInfo; //附加信息(可选,通过Size来判断有无) /* * 以'fmt'作为标示。一般情况下Size为16,此时最后附加信息没有; * 如果为18则最后多了2个字节的附加信息。 * 主要由一些软件制成的wav格式中含有该2个字节的附加信息 */ #endregion #region Fact Chunk(可选) /* * Fact Chunk是可选字段,一般当wav文件由某些软件转化而成,则包含该Chunk。 */ private string factId; private int factSize; private string factData; #endregion #region Data Chunk private string dataId; private int dataSize; private Listdouble> wavdata = new Listdouble>(); //默认为单声道 #endregion public int GetDataSize() { return dataSize; } /// /// public void ReadWAVFile(string filePath) { if (filePath == "") return; byte[] id = new byte[4]; byte[] size = new byte[4]; byte[] type = new byte[4]; byte[] formatid = new byte[4]; byte[] formatsize = new byte[4]; byte[] formattag = new byte[2]; byte[] numchannels = new byte[2]; byte[] samplespersec = new byte[4]; byte[] avgbytespersec = new byte[4]; byte[] blockalign = new byte[2]; byte[] bitspersample = new byte[2]; byte[] additionalinfo = new byte[2]; //可选 byte[] factid = new byte[4]; byte[] factsize = new byte[4]; byte[] factdata = new byte[4]; byte[] dataid = new byte[4]; byte[] datasize = new byte[4]; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8)) { #region RIFF WAVE Chunk br.Read(id, 0, 4); br.Read(size, 0, 4); br.Read(type, 0, 4); this.Id = getString(id, 4); long longsize = bytArray2Int(size);//十六进制转为十进制 this.Size = longsize * 1.0; this.Type = getString(type, 4); #endregion #region Format Chunk br.Read(formatid, 0, 4); br.Read(formatsize, 0, 4); br.Read(formattag, 0, 2); br.Read(numchannels, 0, 2); br.Read(samplespersec, 0, 4); br.Read(avgbytespersec, 0, 4); br.Read(blockalign, 0, 2); br.Read(bitspersample, 0, 2); if (getString(formatsize, 2) == "18") { br.Read(additionalinfo, 0, 2); this.additionalInfo = getString(additionalinfo, 2); //附加信息 } this.formatId = getString(formatid, 4); this.formatSize = bytArray2Int(formatsize); byte[] tmptag = composeByteArray(formattag); this.formatTag = bytArray2Int(tmptag); byte[] tmpchanels = composeByteArray(numchannels); this.num_Channels = bytArray2Int(tmpchanels); //声道数目,1--单声道;2--双声道 this.SamplesPerSec = bytArray2Int(samplespersec); //采样率 this.AvgBytesPerSec = bytArray2Int(avgbytespersec); //每秒所需字节数 byte[] tmpblockalign = composeByteArray(blockalign); this.BlockAlign = bytArray2Int(tmpblockalign); //数据块对齐单位(每个采样需要的字节数) byte[] tmpbitspersample = composeByteArray(bitspersample); this.BitsPerSample = bytArray2Int(tmpbitspersample); // 每个采样需要的bit数 #endregion #region Fact Chunk //byte[] verifyFactChunk = new byte[2]; //br.Read(verifyFactChunk, 0, 2); //string test = getString(verifyFactChunk, 2); //if (getString(verifyFactChunk, 2) == "fa") //{ // byte[] halffactId = new byte[2]; // br.Read(halffactId, 0, 2); // byte[] factchunkid = new byte[4]; // for (int i = 0; i // { // factchunkid[i] = verifyFactChunk[i]; // factchunkid[i + 2] = halffactId[i]; // } // this.factId = getString(factchunkid, 4); // br.Read(factsize, 0, 4); // this.factSize = bytArray2Int(factsize); // br.Read(factdata, 0, 4); // this.factData = getString(factdata, 4); //} #endregion #region Data Chunk byte[] d_flag = new byte[1]; while (true) { br.Read(d_flag, 0, 1); if (getString(d_flag, 1) == "d") { break; } } byte[] dt_id = new byte[4]; dt_id[0] = d_flag[0]; br.Read(dt_id, 1, 3); this.dataId = getString(dt_id, 4); br.Read(datasize, 0, 4); this.dataSize = bytArray2Int(datasize); /* List testl = new List(); if (BitsPerSample == 8) { for (int i = 0; i { byte wavdt = br.ReadByte(); wavdata.Add(wavdt); Console.WriteLine(wavdt); } } else if (BitsPerSample == 16) { for (int i = 0; i { short wavdt = br.ReadInt16(); wavdata.Add(wavdt); Console.WriteLine(wavdt); } } */ #endregion } } } /// /// /// private int bytArray2Int(byte[] bytArray) { return bytArray[0] | (bytArray[1] 8) | (bytArray[2] 16) | (bytArray[3] 24); } /// /// /// /// private string getString(byte[] bts, int len) { char[] tmp = new char[len]; for (int i = 0; i len; i++) { tmp[i] = (char)bts[i]; } return new string(tmp); } /// /// /// private byte[] composeByteArray(byte[] bt) { byte[] tmptag = new byte[4] { 0, 0, 0, 0 }; tmptag[0] = bt[0]; tmptag[1] = bt[1]; return tmptag; } }}点击(此处)折叠或打开using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsWave{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int RowsCnt; private void Form1_Load(object sender, EventArgs e) { RowsCnt = 0; } private void ButtonAdd_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = ".\\";//注意这里写路径时要用c:\\而不是c:\ openFileDialog.Filter = "文本文件|*.*|语音文件|*.wav|所有文件|*.*"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { string fName = openFileDialog.FileName; WaveFile myWave = new WaveFile(); myWave.ReadWAVFile(fName); int filelen = myWave.GetDataSize(); int index = this.dgViewFileList.Rows.Add(); dgViewFileList.Rows[index].Cells[0].Value = RowsCnt.ToString(); dgViewFileList.Rows[index].Cells[1].Value = fName; dgViewFileList.Rows[index].Cells[2].Value = filelen.ToString(); RowsCnt++; // File fileOpen = new File(fName); // isFileHaveName = true; // richTextBox1.Text = fileOpen.ReadFile(); // richTextBox1.AppendText(""); } } private void buttonDel_Click(object sender, EventArgs e) { foreach (DataGridViewRow r in dgViewFileList.SelectedRows) { if (!r.IsNewRow) { dgViewFileList.Rows.Remove(r); } } } private void buttonMoveUp_Click(object sender, EventArgs e) //up { try { DataGridViewSelectedRowCollection dgvsrc = this.dgViewFileList.SelectedRows;//获取选中行的集合 if (dgvsrc.Count > 0) { int index = this.dgViewFileList.SelectedRows[0].Index;//获取当前选中行的索引 if (index > 0)//如果该行不是第一行 { DataGridViewRow dgvr = this.dgViewFileList.Rows[index - dgvsrc.Count];//获取选中行的上一行 this.dgViewFileList.Rows.RemoveAt(index - dgvsrc.Count);//删除原选中行的上一行 this.dgViewFileList.Rows.Insert((index), dgvr);//将选中行的上一行插入到选中行的后面 for (int i = 0; i dgvsrc.Count; i++)//选中移动后的行 { this.dgViewFileList.Rows[index - i - 1].Selected = true; } } } } catch { } } private void buttonMoveDown_Click(object sender, EventArgs e) { try { DataGridViewSelectedRowCollection dgvsrc = this.dgViewFileList.SelectedRows;//获取选中行的集合 if (dgvsrc.Count > 0) { int index = this.dgViewFileList.SelectedRows[0].Index;//获取当前选中行的索引 if (index >= 0 & (this.dgViewFileList.RowCount - 1) != index)//如果该行不是最后一行 { DataGridViewRow dgvr = this.dgViewFileList.Rows[index + 1];//获取选中行的下一行 this.dgViewFileList.Rows.RemoveAt(index + 1);//删除原选中行的上一行 this.dgViewFileList.Rows.Insert((index + 1 - dgvsrc.Count), dgvr);//将选中行的上一行插入到选中行的后面 for (int i = 0; i dgvsrc.Count; i++)//选中移动后的行 { this.dgViewFileList.Rows[index + 1 - i].Selected = true; } } } } catch { } } }} 09-06 09:25