1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.IO;
6: using System.Windows.Forms;
7: 8: namespace SingleChannleExtractor
9: {
10: public class WaveAccess
11: {
12: 13: private byte[] riff;
//4 14: private byte[] riffSize;
//4 15: private byte[] waveID;
//4 16: private byte[] fmtID;
//4 17: private byte[] notDefinition;
//4 18: private byte[] waveType;
//2 19: private byte[] channel;
//2 20: private byte[] sample;
//4 21: private byte[] send;
//4 22: private byte[] blockAjust;
//2 23: private byte[] bitNum;
//2 24: private byte[] unknown;
//2 25: private byte[] dataID;
//4 26: private byte[] dataLength;
//4 27: 28: short[] data;
29: private string longFileName;
30: 31: public string LongFileName
32: {
33: get {
return longFileName; }
34: }
35: 36: public string ShortFileName
37: {
38: get
39: {
40: int pos = LongFileName.LastIndexOf(
"\\"); 41: return LongFileName.Substring(pos + 1);
42: }
43: }
44: 45: public short[] Data
46: {
47: get { return data; }
48: set { data = value; }
49: }
50: 51: public string Riff
52: {
53: get { return Encoding.Default.GetString(riff); }
54: set { riff = Encoding.Default.GetBytes(value); }
55: }
56: 57: public uint RiffSize
58: {
59: get { return BitConverter.ToUInt32(riffSize,0); }
60: set { riffSize = BitConverter.GetBytes(value); }
61: }
62: 63: 64: public string WaveID
65: {
66: get { return Encoding.Default.GetString(waveID); }
67: set { waveID = Encoding.Default.GetBytes(value); }
68: }
69: 70: 71: public string FmtID
72: {
73: get { return Encoding.Default.GetString(fmtID); }
74: set { fmtID = Encoding.Default.GetBytes(value); }
75: }
76: 77: 78: public int NotDefinition
79: {
80: get { return BitConverter.ToInt32(notDefinition,0); }
81: set { notDefinition = BitConverter.GetBytes(value); }
82: }
83: 84: 85: public short WaveType
86: {
87: get { return BitConverter.ToInt16(waveType, 0); }
88: set { waveType = BitConverter.GetBytes(value); }
89: }
90: 91: 92: public ushort Channel
93: {
94: get { return BitConverter.ToUInt16(channel,0); }
95: set { channel = BitConverter.GetBytes(value); }
96: }
97: 98: 99: public uint Sample
100: {
101: get { return BitConverter.ToUInt32(sample,0); }
102: set { sample = BitConverter.GetBytes(value); }
103: }
104: 105: 106: public uint Send
107: {
108: get { return BitConverter.ToUInt32(send, 0); }
109: set { send = BitConverter.GetBytes(value); }
110: }
111: 112: 113: public ushort BlockAjust
114: {
115: get { return BitConverter.ToUInt16(blockAjust, 0); ; }
116: set { blockAjust = BitConverter.GetBytes(value); }
117: }
118: 119: 120: public ushort BitNum
121: {
122: get { return BitConverter.ToUInt16(bitNum, 0);}
123: set { bitNum = BitConverter.GetBytes(value); }
124: }
125: 126: 127: public ushort Unknown
128: {
129: get
130: {
131: if (unknown == null)
132: {
133: return 1;
134: }
135: else
136: return BitConverter.ToUInt16(unknown, 0);
137: }
138: 139: set { unknown = BitConverter.GetBytes(value); }
140: }
141: 142: 143: public string DataID
144: {
145: get { return Encoding.Default.GetString(dataID); }
146: set { dataID = Encoding.Default.GetBytes(value); }
147: }
148: 149: public uint DataLength
150: {
151: get { return BitConverter.ToUInt32(dataLength, 0); }
152: set { dataLength = BitConverter.GetBytes(value); }
153: }
154: 155: 156: public WaveAccess() { }
157: 158: public WaveAccess(string filepath)
159: {
160: try
161: {
162: riff = new byte[4];
163: riffSize = new byte[4];
164: waveID = new byte[4];
165: fmtID = new byte[4];
166: notDefinition = new byte[4];
167: waveType = new byte[2];
168: channel = new byte[2];
169: sample = new byte[4];
170: send = new byte[4];
171: blockAjust = new byte[2];
172: bitNum = new byte[2];
173: unknown = new byte[2];
174: dataID = new byte[4]; //52
175: dataLength = new byte[4]; //56 个字节
176: 177: longFileName = filepath;
178: 179: 180: FileStream fs = new FileStream(filepath,FileMode.Open);
181: BinaryReader bread = new BinaryReader(fs);
182: riff = bread.ReadBytes(4);
183: riffSize = bread.ReadBytes(4);
184: waveID = bread.ReadBytes(4);
185: fmtID = bread.ReadBytes(4);
186: notDefinition = bread.ReadBytes(4);
187: waveType = bread.ReadBytes(2);
188: channel = bread.ReadBytes(2);
189: sample = bread.ReadBytes(4);
190: send = bread.ReadBytes(4);
191: blockAjust = bread.ReadBytes(2);
192: bitNum = bread.ReadBytes(2);
193: if (BitConverter.ToUInt32(notDefinition,0)== 18 )
194: {
195: unknown = bread.ReadBytes(2);
196: }
197: dataID = bread.ReadBytes(4);
198: dataLength = bread.ReadBytes(4);
199: uint length = DataLength/2;
200: data = new short[length];
201: for (int i = 0; i < length; i++)
202: {
203: data[i] = bread.ReadInt16();//读入2字节有符号整数
204: }
205: fs.Close();
206: bread.Close();
207: }
208: catch (System.Exception ex)
209: {
210: Console.Write(ex.Message);
211: }
212: }
213: 214: public short[] GetData(uint begin,uint end )
215: {
216: if ((end - begin) >= Data.Length)
217: return Data;
218: else
219: {
220: uint temp = end - begin+1;
221: short[] dataTemp = new short[temp];
222: uint j = begin;
223: for (int i = 0; i
224: { 225: dataTemp[i] = Data[j]; 226: j++; 227: } 228: return dataTemp; 229: } 230: 231: } 232: 233: /// 234: /// 生成wav文件到系统 235: /// 236: /// 要保存的文件名 237: /// 238: public bool bulidWave(string fileName) 239: { 240: try 241: { 242: FileInfo fi = new FileInfo(fileName); 243: if (fi.Exists) 244: fi.Delete(); 245: FileStream fs = new FileStream(fileName, FileMode.CreateNew); 246: BinaryWriter bwriter = new BinaryWriter(fs); //二进制写入 247: bwriter.Seek(0, SeekOrigin.Begin); 248: bwriter.Write(Encoding.Default.GetBytes(this.Riff)); //不可以直接写入string类型的字符串,字符串会有串结束符,比原来的bytes多一个字节 249: bwriter.Write(this.RiffSize); 250: bwriter.Write(Encoding.Default.GetBytes(this.WaveID)); 251: bwriter.Write(Encoding.Default.GetBytes(this.FmtID)); 252: bwriter.Write(this.NotDefinition); 253: bwriter.Write(this.WaveType); 254: bwriter.Write(this.Channel); 255: bwriter.Write(this.Sample); 256: bwriter.Write(this.Send); 257: bwriter.Write(this.BlockAjust); 258: bwriter.Write(this.BitNum); 259: if (this.Unknown != 0) 260: bwriter.Write(this.Unknown); 261: bwriter.Write(Encoding.Default.GetBytes(this.DataID)); 262: bwriter.Write(this.DataLength); 263: 264: for (int i = 0; i < this.Data.Length; i++) 265: { 266: bwriter.Write(this.Data[i]); 267: } 268: 269: 270: bwriter.Flush(); 271: fs.Close(); 272: bwriter.Close(); 273: fi = null; 274: return true; 275: } 276: catch (System.Exception ex) 277: { 278: Console.Write(ex.Message); 279: return false; 280: } 281: } 282: 283: } 284: }