www.pudn.com > SoundDone.rar > SoundDone.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
 
namespace cswavrec 
{ 
	///  
	/// Summary description for Form1. 
	///  
	public class MainForm : System.Windows.Forms.Form 
	{ 
		///  
		/// Required designer variable. 
		///  
		private System.ComponentModel.Container components = null; 
		private System.Windows.Forms.Button StopButton; 
		private System.Windows.Forms.Button button1; 
		private System.Windows.Forms.Button button2; 
		private System.Windows.Forms.Button StartButton; 
 
		//设置 WAVE 文件格式 
		private WaveLib.WaveFormat wavefmt = new WaveLib.WaveFormat(44100, 16, 2); 
		//用于保存 WAVE 文件 
		private static System.IO.MemoryStream waveFile ; 
		private System.Windows.Forms.Button button3; 
		private System.Windows.Forms.GroupBox groupBox2; 
		private System.Windows.Forms.Button button5; 
		private System.Windows.Forms.Button button4; 
		//暂存 WAVE 数据 
		private System.IO.MemoryStream waveStream ; 
 
		public MainForm() 
		{ 
			// 
			// Required for Windows Form Designer support 
			// 
			InitializeComponent(); 
		} 
 
		///  
		/// Clean up any resources being used. 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null)  
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows Form Designer generated code 
		///  
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		///  
		private void InitializeComponent() 
		{ 
			this.StartButton = new System.Windows.Forms.Button(); 
			this.StopButton = new System.Windows.Forms.Button(); 
			this.button1 = new System.Windows.Forms.Button(); 
			this.button2 = new System.Windows.Forms.Button(); 
			this.button3 = new System.Windows.Forms.Button(); 
			this.groupBox2 = new System.Windows.Forms.GroupBox(); 
			this.button5 = new System.Windows.Forms.Button(); 
			this.button4 = new System.Windows.Forms.Button(); 
			this.groupBox2.SuspendLayout(); 
			this.SuspendLayout(); 
			//  
			// StartButton 
			//  
			this.StartButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.StartButton.Location = new System.Drawing.Point(8, 16); 
			this.StartButton.Name = "StartButton"; 
			this.StartButton.Size = new System.Drawing.Size(86, 23); 
			this.StartButton.TabIndex = 0; 
			this.StartButton.Text = "录音"; 
			this.StartButton.Click += new System.EventHandler(this.StartButton_Click); 
			//  
			// StopButton 
			//  
			this.StopButton.Enabled = false; 
			this.StopButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.StopButton.Location = new System.Drawing.Point(104, 16); 
			this.StopButton.Name = "StopButton"; 
			this.StopButton.Size = new System.Drawing.Size(86, 23); 
			this.StopButton.TabIndex = 1; 
			this.StopButton.Text = "停止"; 
			this.StopButton.Click += new System.EventHandler(this.StopButton_Click); 
			//  
			// button1 
			//  
			this.button1.Enabled = false; 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.button1.Location = new System.Drawing.Point(72, 56); 
			this.button1.Name = "button1"; 
			this.button1.Size = new System.Drawing.Size(56, 23); 
			this.button1.TabIndex = 2; 
			this.button1.Text = "试听"; 
			this.button1.Click += new System.EventHandler(this.button1_Click); 
			//  
			// button2 
			//  
			this.button2.Enabled = false; 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.button2.Location = new System.Drawing.Point(8, 56); 
			this.button2.Name = "button2"; 
			this.button2.Size = new System.Drawing.Size(56, 23); 
			this.button2.TabIndex = 3; 
			this.button2.Text = "保存"; 
			this.button2.Click += new System.EventHandler(this.button2_Click); 
			//  
			// button3 
			//  
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.button3.Location = new System.Drawing.Point(216, 8); 
			this.button3.Name = "button3"; 
			this.button3.Size = new System.Drawing.Size(88, 23); 
			this.button3.TabIndex = 4; 
			this.button3.Text = "播放"; 
			this.button3.Click += new System.EventHandler(this.button3_Click); 
			//  
			// groupBox2 
			//  
			this.groupBox2.Controls.Add(this.button4); 
			this.groupBox2.Controls.Add(this.button2); 
			this.groupBox2.Controls.Add(this.button1); 
			this.groupBox2.Controls.Add(this.StopButton); 
			this.groupBox2.Controls.Add(this.StartButton); 
			this.groupBox2.Location = new System.Drawing.Point(8, 0); 
			this.groupBox2.Name = "groupBox2"; 
			this.groupBox2.Size = new System.Drawing.Size(200, 88); 
			this.groupBox2.TabIndex = 7; 
			this.groupBox2.TabStop = false; 
			//  
			// button5 
			//  
			this.button5.Enabled = false; 
			this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.button5.Location = new System.Drawing.Point(216, 48); 
			this.button5.Name = "button5"; 
			this.button5.Size = new System.Drawing.Size(88, 23); 
			this.button5.TabIndex = 8; 
			this.button5.Text = "结束"; 
			this.button5.Click += new System.EventHandler(this.button5_Click); 
			//  
			// button4 
			//  
			this.button4.Enabled = false; 
			this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup; 
			this.button4.Location = new System.Drawing.Point(136, 56); 
			this.button4.Name = "button4"; 
			this.button4.Size = new System.Drawing.Size(56, 23); 
			this.button4.TabIndex = 4; 
			this.button4.Text = "停试"; 
			this.button4.Click += new System.EventHandler(this.button4_Click_1); 
			//  
			// MainForm 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(314, 95); 
			this.Controls.Add(this.button5); 
			this.Controls.Add(this.groupBox2); 
			this.Controls.Add(this.button3); 
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 
			this.MaximizeBox = false; 
			this.MinimizeBox = false; 
			this.Name = "MainForm"; 
			this.Text = "录音机"; 
			this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing); 
			this.groupBox2.ResumeLayout(false); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		///  
		/// The main entry point for the application. 
		///  
		[STAThread] 
		static void Main()  
		{ 
			Application.Run(new MainForm()); 
		} 
 
		private void State(string state) 
		{ 
			this.Text= "录音机-"+state ; 
		} 
		///  
		/// 音频输出 
		///  
		private WaveLib.WaveOutPlayer m_Player; 
		///  
		/// 音频输入 
		///  
		private WaveLib.WaveInRecorder m_Recorder; 
		///  
		/// 音频流, 当前不用 
		///  
		private WaveLib.FifoStream m_Fifo = new WaveLib.FifoStream(); 
 
		///  
		/// 输出音频的缓冲区 
		///  
		private byte[] m_PlayBuffer; 
		///  
		/// 输入音频的缓冲区 
		///  
		private byte[] m_RecBuffer; 
 
		///  
		/// 从指定的音频数据区,取得指定大小的数据保存到音频流中,播音时使用 
		///  
		/// 非托管音频数据区的指针 
		/// 要取得的音频数据的大小 
		private void Filler(IntPtr data, int size) 
		{ 
			m_PlayBuffer = new byte[size]; 
			this.waveStream.Read(m_PlayBuffer, 0, size); 
			System.Runtime.InteropServices.Marshal.Copy(this.m_PlayBuffer,0,data,size) ; 
 
			if(this.waveStream.Position >= this.waveStream.Length) 
			{ 
				this.State("完毕") ; 
				//this.Stop() ; 
			} 
		} 
 
		///  
		/// 从指定的音频数据区,取得指定大小的数据保存到音频流中,录音时使用 
		///  
		/// 非托管音频数据区的指针 
		/// 要取得的音频数据的大小 
		private void DataArrived(IntPtr data, int size) 
		{ 
			//if (m_RecBuffer == null || m_RecBuffer.Length < size) 
				m_RecBuffer = new byte[size]; 
			System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size); 
			//m_Fifo.Write(m_RecBuffer, 0, m_RecBuffer.Length); 
			this.waveStream.Write(this.m_RecBuffer, 0, this.m_RecBuffer.Length) ; 
		} 
 
		///  
		/// 停止播音 
		///  
		private void Stop() 
		{ 
			if (m_Player != null) 
				try 
				{ 
					m_Player.Dispose(); 
				} 
				finally 
				{ 
					m_Player = null; 
				} 
			if (m_Recorder != null) 
				try 
				{ 
					m_Recorder.Dispose(); 
				} 
				finally 
				{ 
					m_Recorder = null; 
				} 
			m_Fifo.Flush(); // clear all pending data 
		} 
 
		///  
		/// 开始播音 
		///  
		private void Start() 
		{ 
			Stop(); 
 
			this.waveStream= new System.IO.MemoryStream() ; 
			try 
			{ 
				//开始录制 
				//用指定的设备、格式、缓冲区大小、缓冲区个数和缓冲区满时事件 
				m_Recorder = new WaveLib.WaveInRecorder(-1, this.wavefmt, 16384, 3, new WaveLib.BufferDoneEventHandler(DataArrived)); 
			} 
			catch 
			{ 
				Stop(); 
				throw; 
			} 
		} 
 
		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
		{ 
			Stop(); 
		} 
 
		private void StartButton_Click(object sender, System.EventArgs e) 
		{ 
			this.State("录音中...") ; 
			this.button1.Enabled= false ; 
			this.button2.Enabled= false ; 
			this.button3.Enabled= false ; 
			this.StopButton.Enabled= true ; 
			Start(); 
		} 
 
		private void StopButton_Click(object sender, System.EventArgs e) 
		{ 
			this.State("停止录音") ; 
			this.button1.Enabled= true ; 
			this.button2.Enabled= true ; 
			this.button3.Enabled= true ; 
			Stop(); 
			this.SaveHandle() ; 
		} 
		///  
		/// 保存 WAVE Chunk 
		///  
		private void SaveHandle() 
		{	 
			System.IO.BinaryWriter s= new System.IO.BinaryWriter(waveFile= new System.IO.MemoryStream()) ; 
			byte[] hd= new byte[4] ; 
		 
			//RIFF WAVE Chunk 
			//ID 
			//System.Text.Encoding.ASCII.GetBytes("RIFF",0,4,hd,0) ; 
			s.Write(new char[]{'R','I','F','F'}) ; 
			//Size 
			s.Write(new byte[4]) ; 
			//Type 
			//System.Text.Encoding.ASCII.GetBytes("WAVE",0,4,hd,0) ; 
			s.Write(new char[]{'W','A','V','E'}) ; 
			//Format Chunk 
			//ID 
			//System.Text.Encoding.ASCII.GetBytes("fmt ",0,4,hd,0) ; 
			s.Write(new char[]{'f','m','t',' '}) ; 
			//Size 
			s.Write(16) ; 
			//FormatTag  
			s.Write((short)1) ; 
			//Channels  
			s.Write((short)2) ; 
			//SamplesPerSec  
			s.Write(this.wavefmt.nSamplesPerSec) ; 
			//AvgBytesPerSec 
			s.Write(this.wavefmt.nAvgBytesPerSec) ; 
			//BlockAlign 
			s.Write(this.wavefmt.nBlockAlign) ; 
			//BitsPerSample 
			s.Write(this.wavefmt.wBitsPerSample) ; 
			//Data Chunk 
			//ID 
			s.Write(new char[]{'d','a','t','a'}) ; 
			//Size 
			s.Write((int)(this.waveStream.Length)) ; 
 
			byte[] buf= new byte[this.waveStream.Length] ; 
			this.waveStream.Seek(0,System.IO.SeekOrigin.Begin) ; 
			this.waveStream.Read(buf, 0, buf.Length) ; 
			s.Write(buf,0, buf.Length) ; 
			//waveData.Close() ; 
		} 
 
		private void button1_Click(object sender, System.EventArgs e) 
		{ 
			this.State("试听中...") ; 
			this.StopButton.Enabled= false ; 
			this.StartButton.Enabled= false ; 
			this.button1.Enabled= false ; 
			this.button4.Enabled= true ; 
 
			this.waveStream.Seek(0,System.IO.SeekOrigin.Begin) ; 
			//开始播放 
			//用指定的设备、格式、缓冲区大小、缓冲区个数和缓冲区满时事件 
			m_Player = new WaveLib.WaveOutPlayer(-1, this.wavefmt, 16384, 3, new WaveLib.BufferFillEventHandler(Filler)); 
		} 
 
		private void button2_Click(object sender, System.EventArgs e) 
		{ 
			SaveFileDialog sfDilg= new SaveFileDialog() ; 
			sfDilg.Filter= "*.wav|" ; 
			//sfDilg.AddExtension= true ; 
			sfDilg.DefaultExt= ".wav" ; 
			if(sfDilg.ShowDialog() == DialogResult.OK) 
			{ 
				System.IO.Stream file= System.IO.File.Create(sfDilg.FileName) ; 
				byte[] data= new byte[waveFile.Length] ; 
				waveFile.Seek(0,System.IO.SeekOrigin.Begin) ; 
				waveFile.Read(data, 0, data.Length) ; 
				file.Write(data, 0, data.Length) ; 
			} 
		} 
 
		private void button4_Click(object sender, System.EventArgs e) 
		{ 
			this.Stop() ; 
			this.Close() ; 
		} 
 
		private void button3_Click(object sender, System.EventArgs e) 
		{ 
			OpenFileDialog ofDilg= new OpenFileDialog() ; 
			ofDilg.Filter= "*.wav|" ; 
 
			if(ofDilg.ShowDialog() == DialogResult.OK) 
			{ 
				this.StartButton.Enabled= false ; 
				this.button5.Enabled= true ; 
				this.button3.Enabled= false ; 
 
				WaveLib.WaveStream waveS= new WaveLib.WaveStream(ofDilg.FileName) ; 
				byte[] data= new byte[waveS.Length] ; 
				waveS.Seek(0,System.IO.SeekOrigin.Begin) ; 
				waveS.Read(data,0, data.Length) ; 
				this.waveStream= new System.IO.MemoryStream() ; 
				this.waveStream.Write(data,0, data.Length) ; 
				this.waveStream.Seek(0,System.IO.SeekOrigin.Begin) ; 
 
				this.State(ofDilg.FileName.Substring(ofDilg.FileName.LastIndexOf('\\'))+"-播放中...") ; 
				this.m_Player= new WaveLib.WaveOutPlayer(-1, this.wavefmt, 16384, 3, new WaveLib.BufferFillEventHandler(Filler)) ; 
			} 
 
		} 
 
		private void button5_Click(object sender, System.EventArgs e) 
		{ 
			this.StartButton.Enabled= true ; 
			this.button3.Enabled= true ; 
			this.button5.Enabled= false ; 
			this.State("结束") ; 
			this.m_Player.Dispose() ; 
		} 
 
		private void button4_Click_1(object sender, System.EventArgs e) 
		{ 
			this.m_Player.Dispose() ; 
			this.StartButton.Enabled= true ; 
			this.button4.Enabled= false ; 
		} 
	} 
}