Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30995332
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: 网络与安全

2017-12-25 17:31:20



点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;

  6. namespace WindowsWave
  7. {
  8.     class WaveFile
  9.     {
  10.         #region RIFF WAVE Chunk
  11.         private string Id; //文件标识
  12.         private double Size; //文件大小
  13.         private string Type; //文件类型
  14.         #endregion

  15.         #region Format Chunk
  16.         private string formatId;
  17.         private double formatSize; //数值为16或18,18则最后又附加信息
  18.         private int formatTag;
  19.         private int num_Channels; //声道数目,1--单声道;2--双声道
  20.         private int SamplesPerSec; //采样率
  21.         private int AvgBytesPerSec; //每秒所需字节数
  22.         private int BlockAlign; //数据块对齐单位(每个采样需要的字节数)
  23.         private int BitsPerSample; //每个采样需要的bit数
  24.         private string additionalInfo; //附加信息(可选,通过Size来判断有无)
  25.         /*
  26.          * 以'fmt'作为标示。一般情况下Size为16,此时最后附加信息没有;
  27.          * 如果为18则最后多了2个字节的附加信息。
  28.          * 主要由一些软件制成的wav格式中含有该2个字节的附加信息
  29.          */
  30.         #endregion

  31.         #region Fact Chunk(可选)
  32.         /*
  33.                 * Fact Chunk是可选字段,一般当wav文件由某些软件转化而成,则包含该Chunk。
  34.                 */
  35.         private string factId;
  36.         private int factSize;
  37.         private string factData;
  38.         #endregion

  39.         #region Data Chunk
  40.         private string dataId;
  41.         private int dataSize;
  42.         private List<double> wavdata = new List<double>(); //默认为单声道
  43.         #endregion

  44.         public int GetDataSize()
  45.         {
  46.             return dataSize;
  47.         }


  48.         ///
  49.         /// 读取波形文件并显示
  50.         ///
  51.         ///
  52.         public void ReadWAVFile(string filePath)
  53.         {
  54.             if (filePath == "") return;
  55.             byte[] id = new byte[4];
  56.             byte[] size = new byte[4];
  57.             byte[] type = new byte[4];

  58.             byte[] formatid = new byte[4];
  59.             byte[] formatsize = new byte[4];
  60.             byte[] formattag = new byte[2];
  61.             byte[] numchannels = new byte[2];
  62.             byte[] samplespersec = new byte[4];
  63.             byte[] avgbytespersec = new byte[4];
  64.             byte[] blockalign = new byte[2];
  65.             byte[] bitspersample = new byte[2];
  66.             byte[] additionalinfo = new byte[2]; //可选

  67.             byte[] factid = new byte[4];
  68.             byte[] factsize = new byte[4];
  69.             byte[] factdata = new byte[4];

  70.             byte[] dataid = new byte[4];
  71.             byte[] datasize = new byte[4];


  72.             using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  73.             {
  74.                 using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8))
  75.                 {
  76.                     #region RIFF WAVE Chunk
  77.                     br.Read(id, 0, 4);
  78.                     br.Read(size, 0, 4);
  79.                     br.Read(type, 0, 4);



  80.                     this.Id = getString(id, 4);
  81.                     long longsize = bytArray2Int(size);//十六进制转为十进制
  82.                     this.Size = longsize * 1.0;
  83.                     this.Type = getString(type, 4);
  84.                     #endregion

  85.                     #region Format Chunk
  86.                     br.Read(formatid, 0, 4);
  87.                     br.Read(formatsize, 0, 4);
  88.                     br.Read(formattag, 0, 2);
  89.                     br.Read(numchannels, 0, 2);
  90.                     br.Read(samplespersec, 0, 4);
  91.                     br.Read(avgbytespersec, 0, 4);
  92.                     br.Read(blockalign, 0, 2);
  93.                     br.Read(bitspersample, 0, 2);
  94.                     if (getString(formatsize, 2) == "18")
  95.                     {
  96.                         br.Read(additionalinfo, 0, 2);
  97.                         this.additionalInfo = getString(additionalinfo, 2); //附加信息
  98.                     }

  99.                     this.formatId = getString(formatid, 4);

  100.                     this.formatSize = bytArray2Int(formatsize);

  101.                     byte[] tmptag = composeByteArray(formattag);
  102.                     this.formatTag = bytArray2Int(tmptag);

  103.                     byte[] tmpchanels = composeByteArray(numchannels);
  104.                     this.num_Channels = bytArray2Int(tmpchanels); //声道数目,1--单声道;2--双声道

  105.                     this.SamplesPerSec = bytArray2Int(samplespersec); //采样率

  106.                     this.AvgBytesPerSec = bytArray2Int(avgbytespersec); //每秒所需字节数

  107.                     byte[] tmpblockalign = composeByteArray(blockalign);
  108.                     this.BlockAlign = bytArray2Int(tmpblockalign); //数据块对齐单位(每个采样需要的字节数)

  109.                     byte[] tmpbitspersample = composeByteArray(bitspersample);
  110.                     this.BitsPerSample = bytArray2Int(tmpbitspersample); // 每个采样需要的bit数
  111.                     #endregion

  112.                     #region Fact Chunk
  113.                     //byte[] verifyFactChunk = new byte[2];
  114.                     //br.Read(verifyFactChunk, 0, 2);
  115.                     //string test = getString(verifyFactChunk, 2);
  116.                     //if (getString(verifyFactChunk, 2) == "fa")
  117.                     //{
  118.                     // byte[] halffactId = new byte[2];
  119.                     // br.Read(halffactId, 0, 2);

  120.                     // byte[] factchunkid = new byte[4];
  121.                     // for (int i = 0; i < 2; i++)
  122.                     // {
  123.                     // factchunkid[i] = verifyFactChunk[i];
  124.                     // factchunkid[i + 2] = halffactId[i];
  125.                     // }

  126.                     // this.factId = getString(factchunkid, 4);

  127.                     // br.Read(factsize, 0, 4);
  128.                     // this.factSize = bytArray2Int(factsize);

  129.                     // br.Read(factdata, 0, 4);
  130.                     // this.factData = getString(factdata, 4);
  131.                     //}
  132.                     #endregion

  133.                     #region Data Chunk

  134.                     byte[] d_flag = new byte[1];
  135.                     while (true)
  136.                     {
  137.                         br.Read(d_flag, 0, 1);
  138.                         if (getString(d_flag, 1) == "d")
  139.                         {
  140.                             break;
  141.                         }

  142.                     }
  143.                     byte[] dt_id = new byte[4];
  144.                     dt_id[0] = d_flag[0];
  145.                     br.Read(dt_id, 1, 3);
  146.                     this.dataId = getString(dt_id, 4);

  147.                     br.Read(datasize, 0, 4);


  148.                     
  149.                     this.dataSize = bytArray2Int(datasize);

  150.                     /*
  151.                     List testl = new List();

  152.                     if (BitsPerSample == 8)
  153.                     {

  154.                         for (int i = 0; i < this.dataSize; i++)
  155.                         {
  156.                             byte wavdt = br.ReadByte();
  157.                             wavdata.Add(wavdt);
  158.                             Console.WriteLine(wavdt);
  159.                         }
  160.                     }
  161.                     else if (BitsPerSample == 16)
  162.                     {
  163.                         for (int i = 0; i < this.dataSize / 2; i++)
  164.                         {
  165.                             short wavdt = br.ReadInt16();
  166.                             wavdata.Add(wavdt);
  167.                             Console.WriteLine(wavdt);
  168.                         }
  169.                     }
  170.                     */
  171.                     #endregion
  172.                 }
  173.             }
  174.         }

  175.         ///
  176.         /// 数字节数组转换为int
  177.         ///
  178.         ///
  179.         ///
  180.         private int bytArray2Int(byte[] bytArray)
  181.         {
  182.             return bytArray[0] | (bytArray[1] << 8) | (bytArray[2] << 16) | (bytArray[3] << 24);
  183.         }

  184.         ///
  185.         /// 将字节数组转换为字符串
  186.         ///
  187.         ///
  188.         ///
  189.         ///
  190.         private string getString(byte[] bts, int len)
  191.         {
  192.             char[] tmp = new char[len];
  193.             for (int i = 0; i < len; i++)
  194.             {
  195.                 tmp[i] = (char)bts[i];
  196.             }
  197.             return new string(tmp);
  198.         }

  199.         ///
  200.         /// 组成4个元素的字节数组
  201.         ///
  202.         ///
  203.         ///
  204.         private byte[] composeByteArray(byte[] bt)
  205.         {
  206.             byte[] tmptag = new byte[4] { 0, 0, 0, 0 };
  207.             tmptag[0] = bt[0];
  208.             tmptag[1] = bt[1];
  209.             return tmptag;
  210.         }
  211.     }
  212. }


点击(此处)折叠或打开

  1. using System.Data;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;

  6. namespace WindowsWave
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.         }

  14.         private int RowsCnt;

  15.         private void Form1_Load(object sender, EventArgs e)
  16.         {
  17.             RowsCnt = 0;
  18.         }

  19.         private void ButtonAdd_Click(object sender, EventArgs e)
  20.         {
  21.             OpenFileDialog openFileDialog = new OpenFileDialog();
  22.             openFileDialog.InitialDirectory = ".\\";//注意这里写路径时要用c:\\而不是c:\
  23.             openFileDialog.Filter = "文本文件|*.*|语音文件|*.wav|所有文件|*.*";
  24.             openFileDialog.RestoreDirectory = true;
  25.             openFileDialog.FilterIndex = 1;
  26.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  27.             {
  28.                 string fName = openFileDialog.FileName;
  29.                 WaveFile myWave = new WaveFile();
  30.                 myWave.ReadWAVFile(fName);
  31.                 int filelen = myWave.GetDataSize();
  32.                 
  33.                 int index = this.dgViewFileList.Rows.Add();
  34.                 dgViewFileList.Rows[index].Cells[0].Value = RowsCnt.ToString();
  35.                 dgViewFileList.Rows[index].Cells[1].Value = fName;
  36.                 dgViewFileList.Rows[index].Cells[2].Value = filelen.ToString();
  37.                 RowsCnt++;
  38.                // File fileOpen = new File(fName);
  39.                // isFileHaveName = true;
  40.                // richTextBox1.Text = fileOpen.ReadFile();
  41.                // richTextBox1.AppendText("");
  42.             }
  43.         }

  44.         private void buttonDel_Click(object sender, EventArgs e)
  45.         {
  46.             foreach (DataGridViewRow r in dgViewFileList.SelectedRows)
  47.             {
  48.                 if (!r.IsNewRow)
  49.                 {
  50.                     dgViewFileList.Rows.Remove(r);
  51.                 }
  52.             }
  53.         }


  54.         private void buttonMoveUp_Click(object sender, EventArgs e) //up
  55.         {
  56.             try
  57.             {
  58.                 DataGridViewSelectedRowCollection dgvsrc = this.dgViewFileList.SelectedRows;//获取选中行的集合
  59.                 if (dgvsrc.Count > 0)
  60.                 {
  61.                     int index = this.dgViewFileList.SelectedRows[0].Index;//获取当前选中行的索引
  62.                     if (index > 0)//如果该行不是第一行
  63.                     {
  64.                         DataGridViewRow dgvr = this.dgViewFileList.Rows[index - dgvsrc.Count];//获取选中行的上一行
  65.                         this.dgViewFileList.Rows.RemoveAt(index - dgvsrc.Count);//删除原选中行的上一行
  66.                         this.dgViewFileList.Rows.Insert((index), dgvr);//将选中行的上一行插入到选中行的后面
  67.                         for (int i = 0; i < dgvsrc.Count; i++)//选中移动后的行
  68.                         {
  69.                             this.dgViewFileList.Rows[index - i - 1].Selected = true;
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.             catch { }
  75.         }

  76.         private void buttonMoveDown_Click(object sender, EventArgs e)
  77.         {
  78.             try
  79.             {
  80.                 DataGridViewSelectedRowCollection dgvsrc = this.dgViewFileList.SelectedRows;//获取选中行的集合
  81.                 if (dgvsrc.Count > 0)
  82.                 {
  83.                     int index = this.dgViewFileList.SelectedRows[0].Index;//获取当前选中行的索引
  84.                     if (index >= 0 & (this.dgViewFileList.RowCount - 1) != index)//如果该行不是最后一行
  85.                     {
  86.                         DataGridViewRow dgvr = this.dgViewFileList.Rows[index + 1];//获取选中行的下一行
  87.                         this.dgViewFileList.Rows.RemoveAt(index + 1);//删除原选中行的上一行
  88.                         this.dgViewFileList.Rows.Insert((index + 1 - dgvsrc.Count), dgvr);//将选中行的上一行插入到选中行的后面
  89.                         for (int i = 0; i < dgvsrc.Count; i++)//选中移动后的行
  90.                         {
  91.                             this.dgViewFileList.Rows[index + 1 - i].Selected = true;
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.             catch { }
  97.         }


  98.     }
  99. }

阅读(1593) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~