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


//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR 
//  PURPOSE. 
// 
//  This material may not be duplicated in whole or in part, except for  
//  personal use, without the express written consent of the author.  
// 
//  Email:  ianier@hotmail.com 
// 
//  Copyright (C) 1999-2003 Ianier Munoz. All Rights Reserved. 
 
using System; 
using System.Runtime.InteropServices; 
 
namespace WaveLib 
{ 
	///  
	/// 音频格式 
	///  
	public enum WaveFormats 
	{ 
		Pcm = 1, 
		Float = 3 
	} 
 
	///  
	/// 代表音频的格式 
	///  
	[StructLayout(LayoutKind.Sequential)]  
	public class WaveFormat 
	{ 
		public short wFormatTag; 
		public short nChannels; 
		public int nSamplesPerSec; 
		public int nAvgBytesPerSec; 
		public short nBlockAlign; 
		public short wBitsPerSample; 
		public short cbSize; 
 
		///  
		/// WAVE 文件格式 
		///  
		/// 播放速率 
		/// 采样位数 
		/// 声道数 
		public WaveFormat(int rate, int bits, int channels) 
		{ 
			wFormatTag = (short)WaveFormats.Pcm; 
			nChannels = (short)channels; 
			nSamplesPerSec = rate; 
			wBitsPerSample = (short)bits; 
			cbSize = 0; 
                
			nBlockAlign = (short)(channels * (bits / 8)); 
			nAvgBytesPerSec = nSamplesPerSec * nBlockAlign; 
		} 
	} 
 
	internal class WaveNative 
	{ 
		// consts 
		///  
		/// no error 
		///  
		public const int MMSYSERR_NOERROR = 0; 
		///  
		/// 打开设备时消息,在此期间我们可以进行一些初始化工作  
		///  
		public const int MM_WOM_OPEN = 0x3BB; 
		public const int MM_WOM_CLOSE = 0x3BC; 
		public const int MM_WOM_DONE = 0x3BD; 
 
		///  
		/// 打开设备时消息,在此期间我们可以进行一些初始化工作    
		///  
		public const int MM_WIM_OPEN = 0x3BE; 
		///  
		/// 关闭录音设备时的消息  
		///  
		public const int MM_WIM_CLOSE = 0x3BF; 
		///  
		/// 当缓存已满或者停止录音时的消息,处理这个消息可以对缓存进行重新分配,实现不限长度录音  
		///  
		public const int MM_WIM_DATA = 0x3C0; 
 
		///  
		/// dwCallback is a FARPROC 
		///  
		public const int CALLBACK_FUNCTION = 0x00030000; 
 
		///  
		/// time in milliseconds 
		///  
		public const int TIME_MS = 0x0001; 
		///  
		/// number of wave samples  
		///  
		public const int TIME_SAMPLES = 0x0002; 
		///  
		/// current byte offset 
		///  
		public const int TIME_BYTES = 0x0004; 
 
		///  
		/// callbacks 
		///  
		public delegate void WaveDelegate(IntPtr hdrvr, int uMsg, int dwUser, ref WaveHdr wavhdr, int dwParam2); 
 
		///  
		/// WAVE 文件的缓冲区结构的信息 
		///  
		[StructLayout(LayoutKind.Sequential)] public struct WaveHdr 
		{ 
			///  
			/// pointer to locked data buffer 
			/// 指向锁定的数据缓冲区的指针  
			///  
			public IntPtr lpData; 
			///  
			/// length of data buffer 
			/// 数据缓冲区的大小  
			///  
			public int dwBufferLength; 
			///  
			/// used for input only 
			/// 录音时指明缓冲区中的数据量   
			///  
			public int dwBytesRecorded; 
			///  
			/// for client's use 
			/// 用户数据   
			///  
			public IntPtr dwUser; 
			///  
			/// assorted flags (see defines) 
			/// 提供缓冲区信息的标志 
			///  
			public int dwFlags; 
			///  
			/// loop control counter 
			/// 循环播放的次数  
			///  
			public int dwLoops; 
			///  
			/// PWaveHdr, reserved for driver 
			/// 保留   
			///  
			public IntPtr lpNext; 
			///  
			/// reserved for driver 
			/// 保留    
			///  
			public int reserved; 
		} 
 
		private const string mmdll = "winmm.dll"; 
 
		///  
		/// WaveOut calls 
		///  检取系统中存在的波形输出设备的数量 
		///  
		/// 设备数量 
		[DllImport(mmdll)] 
		public static extern int waveOutGetNumDevs(); 
		///  
		/// 为播放准备一个波形缓冲区 
		///  
		///  
		/// 准备的情况 
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutPrepareHeader(IntPtr hWaveOut, ref WaveHdr lpWaveOutHdr, int uSize); 
		///  
		///  清除由waveOutPrepareHeader函数实现的准备 
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutUnprepareHeader(IntPtr hWaveOut, ref WaveHdr lpWaveOutHdr, int uSize); 
		///  
		/// 向指定的波形输出设备发送一个数据块 
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutWrite(IntPtr hWaveOut, ref WaveHdr lpWaveOutHdr, int uSize); 
		///  
		/// 为播放打开一个波形输出设备 
		///  
		///  
		///  
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutOpen(out IntPtr hWaveOut, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags); 
		///  
		/// 重新输出 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutReset(IntPtr hWaveOut); 
		///  
		/// 关闭输出 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutClose(IntPtr hWaveOut); 
		///  
		/// 输出中止 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutPause(IntPtr hWaveOut); 
		///  
		/// 重新启动一个被暂停的波形输出设备 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutRestart(IntPtr hWaveOut); 
		///  
		/// 检取指定波形输出设备的当前播放位置 
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutGetPosition(IntPtr hWaveOut, out int lpInfo, int uSize); 
		///  
		/// 设置指定的波形输出设备的音量 
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutSetVolume(IntPtr hWaveOut, int dwVolume); 
		///  
		/// 查询指定波形输出设备的当前音量设置 
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveOutGetVolume(IntPtr hWaveOut, out int dwVolume); 
 
		///  
		/// 返回系统中存在的波形输入设备的数量 
		///  
		/// 设备的数量 
		// WaveIn calls 
		[DllImport(mmdll)] 
		public static extern int waveInGetNumDevs(); 
		///  
		/// 向波形输入设备添加一个输入缓冲区 
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInAddBuffer(IntPtr hwi, ref WaveHdr pwh, int cbwh); 
		///  
		/// 关闭指定的波形输入设置 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInClose(IntPtr hwi); 
		///  
		/// 为录音而打开一个波形输入设备 
		///  
		///  
		///  
		///  
		///  
		///  
		///  
		/// 异常号 
		[DllImport(mmdll)] 
		public static extern int waveInOpen(out IntPtr phwi, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags); 
		///  
		/// 为波形输入准备一个输入缓冲区 
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInPrepareHeader(IntPtr hWaveIn, ref WaveHdr lpWaveInHdr, int uSize); 
		///  
		///  清除由waveInPrepareHeader函数实现的准备 
		///  
		///  
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInUnprepareHeader(IntPtr hWaveIn, ref WaveHdr lpWaveInHdr, int uSize); 
		///  
		/// 停止给定的波形输入设备的输入,且将当前位置清零 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInReset(IntPtr hwi); 
		///  
		/// 启动在指定的波形输入设备的输入 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInStart(IntPtr hwi); 
		///  
		/// 停止在指定的波形输入设备上的输入 
		///  
		///  
		///  
		[DllImport(mmdll)] 
		public static extern int waveInStop(IntPtr hwi); 
	} 
}